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> |
10313cbb | 38 | #include <linux/vmalloc.h> |
839fcaba | 39 | |
68e995a2 PS |
40 | #include "ipoib.h" |
41 | ||
42 | int ipoib_max_conn_qp = 128; | |
43 | ||
44 | module_param_named(max_nonsrq_conn_qp, ipoib_max_conn_qp, int, 0444); | |
45 | MODULE_PARM_DESC(max_nonsrq_conn_qp, | |
46 | "Max number of connected-mode QPs per interface " | |
47 | "(applied only if shared receive queue is not available)"); | |
48 | ||
839fcaba MT |
49 | #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA |
50 | static int data_debug_level; | |
51 | ||
52 | module_param_named(cm_data_debug_level, data_debug_level, int, 0644); | |
53 | MODULE_PARM_DESC(cm_data_debug_level, | |
54 | "Enable data path debug tracing for connected mode if > 0"); | |
55 | #endif | |
56 | ||
839fcaba MT |
57 | #define IPOIB_CM_IETF_ID 0x1000000000000000ULL |
58 | ||
59 | #define IPOIB_CM_RX_UPDATE_TIME (256 * HZ) | |
60 | #define IPOIB_CM_RX_TIMEOUT (2 * 256 * HZ) | |
61 | #define IPOIB_CM_RX_DELAY (3 * 256 * HZ) | |
62 | #define IPOIB_CM_RX_UPDATE_MASK (0x3) | |
63 | ||
518b1646 MT |
64 | static struct ib_qp_attr ipoib_cm_err_attr = { |
65 | .qp_state = IB_QPS_ERR | |
66 | }; | |
67 | ||
09f60f8f | 68 | #define IPOIB_CM_RX_DRAIN_WRID 0xffffffff |
518b1646 | 69 | |
ec56dc0b MT |
70 | static struct ib_send_wr ipoib_cm_rx_drain_wr = { |
71 | .wr_id = IPOIB_CM_RX_DRAIN_WRID, | |
72 | .opcode = IB_WR_SEND, | |
518b1646 MT |
73 | }; |
74 | ||
839fcaba MT |
75 | static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id, |
76 | struct ib_cm_event *event); | |
77 | ||
1812063b | 78 | static void ipoib_cm_dma_unmap_rx(struct ipoib_dev_priv *priv, int frags, |
839fcaba MT |
79 | u64 mapping[IPOIB_CM_RX_SG]) |
80 | { | |
81 | int i; | |
82 | ||
83 | ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE); | |
84 | ||
1812063b | 85 | for (i = 0; i < frags; ++i) |
839fcaba MT |
86 | ib_dma_unmap_single(priv->ca, mapping[i + 1], PAGE_SIZE, DMA_FROM_DEVICE); |
87 | } | |
88 | ||
68e995a2 | 89 | static int ipoib_cm_post_receive_srq(struct net_device *dev, int id) |
839fcaba MT |
90 | { |
91 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
92 | struct ib_recv_wr *bad_wr; | |
93 | int i, ret; | |
94 | ||
1b524963 | 95 | priv->cm.rx_wr.wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV; |
839fcaba | 96 | |
586a6934 | 97 | for (i = 0; i < priv->cm.num_frags; ++i) |
839fcaba MT |
98 | priv->cm.rx_sge[i].addr = priv->cm.srq_ring[id].mapping[i]; |
99 | ||
100 | ret = ib_post_srq_recv(priv->cm.srq, &priv->cm.rx_wr, &bad_wr); | |
101 | if (unlikely(ret)) { | |
102 | ipoib_warn(priv, "post srq failed for buf %d (%d)\n", id, ret); | |
586a6934 | 103 | ipoib_cm_dma_unmap_rx(priv, priv->cm.num_frags - 1, |
1812063b | 104 | priv->cm.srq_ring[id].mapping); |
839fcaba MT |
105 | dev_kfree_skb_any(priv->cm.srq_ring[id].skb); |
106 | priv->cm.srq_ring[id].skb = NULL; | |
107 | } | |
108 | ||
109 | return ret; | |
110 | } | |
111 | ||
68e995a2 | 112 | static int ipoib_cm_post_receive_nonsrq(struct net_device *dev, |
a7d834c4 RD |
113 | struct ipoib_cm_rx *rx, |
114 | struct ib_recv_wr *wr, | |
115 | struct ib_sge *sge, int id) | |
68e995a2 PS |
116 | { |
117 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
118 | struct ib_recv_wr *bad_wr; | |
119 | int i, ret; | |
120 | ||
a7d834c4 | 121 | wr->wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV; |
68e995a2 PS |
122 | |
123 | for (i = 0; i < IPOIB_CM_RX_SG; ++i) | |
a7d834c4 | 124 | sge[i].addr = rx->rx_ring[id].mapping[i]; |
68e995a2 | 125 | |
a7d834c4 | 126 | ret = ib_post_recv(rx->qp, wr, &bad_wr); |
68e995a2 PS |
127 | if (unlikely(ret)) { |
128 | ipoib_warn(priv, "post recv failed for buf %d (%d)\n", id, ret); | |
129 | ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1, | |
130 | rx->rx_ring[id].mapping); | |
131 | dev_kfree_skb_any(rx->rx_ring[id].skb); | |
132 | rx->rx_ring[id].skb = NULL; | |
133 | } | |
134 | ||
135 | return ret; | |
136 | } | |
137 | ||
138 | static struct sk_buff *ipoib_cm_alloc_rx_skb(struct net_device *dev, | |
139 | struct ipoib_cm_rx_buf *rx_ring, | |
140 | int id, int frags, | |
1812063b | 141 | u64 mapping[IPOIB_CM_RX_SG]) |
839fcaba MT |
142 | { |
143 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
144 | struct sk_buff *skb; | |
145 | int i; | |
146 | ||
147 | skb = dev_alloc_skb(IPOIB_CM_HEAD_SIZE + 12); | |
148 | if (unlikely(!skb)) | |
1812063b | 149 | return NULL; |
839fcaba MT |
150 | |
151 | /* | |
152 | * IPoIB adds a 4 byte header. So we need 12 more bytes to align the | |
153 | * IP header to a multiple of 16. | |
154 | */ | |
155 | skb_reserve(skb, 12); | |
156 | ||
157 | mapping[0] = ib_dma_map_single(priv->ca, skb->data, IPOIB_CM_HEAD_SIZE, | |
158 | DMA_FROM_DEVICE); | |
159 | if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0]))) { | |
160 | dev_kfree_skb_any(skb); | |
1812063b | 161 | return NULL; |
839fcaba MT |
162 | } |
163 | ||
1812063b | 164 | for (i = 0; i < frags; i++) { |
839fcaba MT |
165 | struct page *page = alloc_page(GFP_ATOMIC); |
166 | ||
167 | if (!page) | |
168 | goto partial_error; | |
169 | skb_fill_page_desc(skb, i, page, 0, PAGE_SIZE); | |
170 | ||
171 | mapping[i + 1] = ib_dma_map_page(priv->ca, skb_shinfo(skb)->frags[i].page, | |
6371ea3d | 172 | 0, PAGE_SIZE, DMA_FROM_DEVICE); |
839fcaba MT |
173 | if (unlikely(ib_dma_mapping_error(priv->ca, mapping[i + 1]))) |
174 | goto partial_error; | |
175 | } | |
176 | ||
68e995a2 | 177 | rx_ring[id].skb = skb; |
1812063b | 178 | return skb; |
839fcaba MT |
179 | |
180 | partial_error: | |
181 | ||
182 | ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE); | |
183 | ||
841adfca RC |
184 | for (; i > 0; --i) |
185 | ib_dma_unmap_single(priv->ca, mapping[i], PAGE_SIZE, DMA_FROM_DEVICE); | |
839fcaba | 186 | |
8a2e65f8 | 187 | dev_kfree_skb_any(skb); |
1812063b | 188 | return NULL; |
839fcaba MT |
189 | } |
190 | ||
1efb6144 RD |
191 | static void ipoib_cm_free_rx_ring(struct net_device *dev, |
192 | struct ipoib_cm_rx_buf *rx_ring) | |
193 | { | |
194 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
195 | int i; | |
196 | ||
197 | for (i = 0; i < ipoib_recvq_size; ++i) | |
198 | if (rx_ring[i].skb) { | |
199 | ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1, | |
200 | rx_ring[i].mapping); | |
201 | dev_kfree_skb_any(rx_ring[i].skb); | |
202 | } | |
203 | ||
b1404069 | 204 | vfree(rx_ring); |
1efb6144 RD |
205 | } |
206 | ||
2337f809 | 207 | static void ipoib_cm_start_rx_drain(struct ipoib_dev_priv *priv) |
518b1646 | 208 | { |
ec56dc0b MT |
209 | struct ib_send_wr *bad_wr; |
210 | struct ipoib_cm_rx *p; | |
518b1646 | 211 | |
ec56dc0b | 212 | /* We only reserved 1 extra slot in CQ for drain WRs, so |
518b1646 MT |
213 | * make sure we have at most 1 outstanding WR. */ |
214 | if (list_empty(&priv->cm.rx_flush_list) || | |
215 | !list_empty(&priv->cm.rx_drain_list)) | |
216 | return; | |
217 | ||
ec56dc0b MT |
218 | /* |
219 | * QPs on flush list are error state. This way, a "flush | |
220 | * error" WC will be immediately generated for each WR we post. | |
221 | */ | |
222 | p = list_entry(priv->cm.rx_flush_list.next, typeof(*p), list); | |
223 | if (ib_post_send(p->qp, &ipoib_cm_rx_drain_wr, &bad_wr)) | |
224 | ipoib_warn(priv, "failed to post drain wr\n"); | |
518b1646 MT |
225 | |
226 | list_splice_init(&priv->cm.rx_flush_list, &priv->cm.rx_drain_list); | |
227 | } | |
228 | ||
229 | static void ipoib_cm_rx_event_handler(struct ib_event *event, void *ctx) | |
230 | { | |
231 | struct ipoib_cm_rx *p = ctx; | |
232 | struct ipoib_dev_priv *priv = netdev_priv(p->dev); | |
233 | unsigned long flags; | |
234 | ||
235 | if (event->event != IB_EVENT_QP_LAST_WQE_REACHED) | |
236 | return; | |
237 | ||
238 | spin_lock_irqsave(&priv->lock, flags); | |
239 | list_move(&p->list, &priv->cm.rx_flush_list); | |
240 | p->state = IPOIB_CM_RX_FLUSH; | |
241 | ipoib_cm_start_rx_drain(priv); | |
242 | spin_unlock_irqrestore(&priv->lock, flags); | |
243 | } | |
244 | ||
839fcaba MT |
245 | static struct ib_qp *ipoib_cm_create_rx_qp(struct net_device *dev, |
246 | struct ipoib_cm_rx *p) | |
247 | { | |
248 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
249 | struct ib_qp_init_attr attr = { | |
518b1646 | 250 | .event_handler = ipoib_cm_rx_event_handler, |
f56bcd80 EC |
251 | .send_cq = priv->recv_cq, /* For drain WR */ |
252 | .recv_cq = priv->recv_cq, | |
839fcaba | 253 | .srq = priv->cm.srq, |
ec56dc0b | 254 | .cap.max_send_wr = 1, /* For drain WR */ |
839fcaba MT |
255 | .cap.max_send_sge = 1, /* FIXME: 0 Seems not to work */ |
256 | .sq_sig_type = IB_SIGNAL_ALL_WR, | |
257 | .qp_type = IB_QPT_RC, | |
258 | .qp_context = p, | |
259 | }; | |
68e995a2 PS |
260 | |
261 | if (!ipoib_cm_has_srq(dev)) { | |
262 | attr.cap.max_recv_wr = ipoib_recvq_size; | |
263 | attr.cap.max_recv_sge = IPOIB_CM_RX_SG; | |
264 | } | |
265 | ||
839fcaba MT |
266 | return ib_create_qp(priv->pd, &attr); |
267 | } | |
268 | ||
269 | static int ipoib_cm_modify_rx_qp(struct net_device *dev, | |
68e995a2 PS |
270 | struct ib_cm_id *cm_id, struct ib_qp *qp, |
271 | unsigned psn) | |
839fcaba MT |
272 | { |
273 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
274 | struct ib_qp_attr qp_attr; | |
275 | int qp_attr_mask, ret; | |
276 | ||
277 | qp_attr.qp_state = IB_QPS_INIT; | |
278 | ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask); | |
279 | if (ret) { | |
280 | ipoib_warn(priv, "failed to init QP attr for INIT: %d\n", ret); | |
281 | return ret; | |
282 | } | |
283 | ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask); | |
284 | if (ret) { | |
285 | ipoib_warn(priv, "failed to modify QP to INIT: %d\n", ret); | |
286 | return ret; | |
287 | } | |
288 | qp_attr.qp_state = IB_QPS_RTR; | |
289 | ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask); | |
290 | if (ret) { | |
291 | ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret); | |
292 | return ret; | |
293 | } | |
294 | qp_attr.rq_psn = psn; | |
295 | ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask); | |
296 | if (ret) { | |
297 | ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret); | |
298 | return ret; | |
299 | } | |
ec56dc0b MT |
300 | |
301 | /* | |
302 | * Current Mellanox HCA firmware won't generate completions | |
303 | * with error for drain WRs unless the QP has been moved to | |
304 | * RTS first. This work-around leaves a window where a QP has | |
305 | * moved to error asynchronously, but this will eventually get | |
306 | * fixed in firmware, so let's not error out if modify QP | |
307 | * fails. | |
308 | */ | |
309 | qp_attr.qp_state = IB_QPS_RTS; | |
310 | ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask); | |
311 | if (ret) { | |
312 | ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret); | |
313 | return 0; | |
314 | } | |
315 | ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask); | |
316 | if (ret) { | |
317 | ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret); | |
318 | return 0; | |
319 | } | |
320 | ||
839fcaba MT |
321 | return 0; |
322 | } | |
323 | ||
a7d834c4 RD |
324 | static void ipoib_cm_init_rx_wr(struct net_device *dev, |
325 | struct ib_recv_wr *wr, | |
326 | struct ib_sge *sge) | |
327 | { | |
328 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
329 | int i; | |
330 | ||
331 | for (i = 0; i < priv->cm.num_frags; ++i) | |
332 | sge[i].lkey = priv->mr->lkey; | |
333 | ||
334 | sge[0].length = IPOIB_CM_HEAD_SIZE; | |
335 | for (i = 1; i < priv->cm.num_frags; ++i) | |
336 | sge[i].length = PAGE_SIZE; | |
337 | ||
338 | wr->next = NULL; | |
e0819816 | 339 | wr->sg_list = sge; |
a7d834c4 RD |
340 | wr->num_sge = priv->cm.num_frags; |
341 | } | |
342 | ||
68e995a2 PS |
343 | static int ipoib_cm_nonsrq_init_rx(struct net_device *dev, struct ib_cm_id *cm_id, |
344 | struct ipoib_cm_rx *rx) | |
345 | { | |
346 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
a7d834c4 RD |
347 | struct { |
348 | struct ib_recv_wr wr; | |
349 | struct ib_sge sge[IPOIB_CM_RX_SG]; | |
350 | } *t; | |
68e995a2 PS |
351 | int ret; |
352 | int i; | |
353 | ||
b1404069 DW |
354 | rx->rx_ring = vmalloc(ipoib_recvq_size * sizeof *rx->rx_ring); |
355 | if (!rx->rx_ring) { | |
356 | printk(KERN_WARNING "%s: failed to allocate CM non-SRQ ring (%d entries)\n", | |
357 | priv->ca->name, ipoib_recvq_size); | |
68e995a2 | 358 | return -ENOMEM; |
b1404069 DW |
359 | } |
360 | ||
361 | memset(rx->rx_ring, 0, ipoib_recvq_size * sizeof *rx->rx_ring); | |
68e995a2 | 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 */ | |
1812063b | 541 | skb_fill_page_desc(toskb, i, frag->page, 0, PAGE_SIZE); |
839fcaba MT |
542 | --skb_shinfo(skb)->nr_frags; |
543 | } else { | |
544 | size = min(length, (unsigned) PAGE_SIZE); | |
545 | ||
546 | frag->size = size; | |
547 | skb->data_len += size; | |
548 | skb->truesize += size; | |
549 | skb->len += size; | |
550 | length -= size; | |
551 | } | |
552 | } | |
553 | } | |
554 | ||
555 | void ipoib_cm_handle_rx_wc(struct net_device *dev, struct ib_wc *wc) | |
556 | { | |
557 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
68e995a2 | 558 | struct ipoib_cm_rx_buf *rx_ring; |
1b524963 | 559 | unsigned int wr_id = wc->wr_id & ~(IPOIB_OP_CM | IPOIB_OP_RECV); |
1812063b | 560 | struct sk_buff *skb, *newskb; |
839fcaba MT |
561 | struct ipoib_cm_rx *p; |
562 | unsigned long flags; | |
563 | u64 mapping[IPOIB_CM_RX_SG]; | |
1812063b | 564 | int frags; |
68e995a2 | 565 | int has_srq; |
f89271da | 566 | struct sk_buff *small_skb; |
839fcaba | 567 | |
a89875fc RD |
568 | ipoib_dbg_data(priv, "cm recv completion: id %d, status: %d\n", |
569 | wr_id, wc->status); | |
839fcaba MT |
570 | |
571 | if (unlikely(wr_id >= ipoib_recvq_size)) { | |
1b524963 | 572 | if (wr_id == (IPOIB_CM_RX_DRAIN_WRID & ~(IPOIB_OP_CM | IPOIB_OP_RECV))) { |
518b1646 MT |
573 | spin_lock_irqsave(&priv->lock, flags); |
574 | list_splice_init(&priv->cm.rx_drain_list, &priv->cm.rx_reap_list); | |
575 | ipoib_cm_start_rx_drain(priv); | |
576 | queue_work(ipoib_workqueue, &priv->cm.rx_reap_task); | |
577 | spin_unlock_irqrestore(&priv->lock, flags); | |
578 | } else | |
579 | ipoib_warn(priv, "cm recv completion event with wrid %d (> %d)\n", | |
580 | wr_id, ipoib_recvq_size); | |
839fcaba MT |
581 | return; |
582 | } | |
583 | ||
68e995a2 PS |
584 | p = wc->qp->qp_context; |
585 | ||
586 | has_srq = ipoib_cm_has_srq(dev); | |
587 | rx_ring = has_srq ? priv->cm.srq_ring : p->rx_ring; | |
588 | ||
589 | skb = rx_ring[wr_id].skb; | |
839fcaba MT |
590 | |
591 | if (unlikely(wc->status != IB_WC_SUCCESS)) { | |
592 | ipoib_dbg(priv, "cm recv error " | |
593 | "(status=%d, wrid=%d vend_err %x)\n", | |
594 | wc->status, wr_id, wc->vendor_err); | |
de903512 | 595 | ++dev->stats.rx_dropped; |
68e995a2 PS |
596 | if (has_srq) |
597 | goto repost; | |
598 | else { | |
599 | if (!--p->recv_count) { | |
600 | spin_lock_irqsave(&priv->lock, flags); | |
601 | list_move(&p->list, &priv->cm.rx_reap_list); | |
602 | spin_unlock_irqrestore(&priv->lock, flags); | |
603 | queue_work(ipoib_workqueue, &priv->cm.rx_reap_task); | |
604 | } | |
605 | return; | |
606 | } | |
839fcaba MT |
607 | } |
608 | ||
fd312561 | 609 | if (unlikely(!(wr_id & IPOIB_CM_RX_UPDATE_MASK))) { |
d6ef7d68 | 610 | if (p && time_after_eq(jiffies, p->jiffies + IPOIB_CM_RX_UPDATE_TIME)) { |
839fcaba MT |
611 | spin_lock_irqsave(&priv->lock, flags); |
612 | p->jiffies = jiffies; | |
518b1646 MT |
613 | /* Move this entry to list head, but do not re-add it |
614 | * if it has been moved out of list. */ | |
615 | if (p->state == IPOIB_CM_RX_LIVE) | |
839fcaba MT |
616 | list_move(&p->list, &priv->cm.passive_ids); |
617 | spin_unlock_irqrestore(&priv->lock, flags); | |
839fcaba MT |
618 | } |
619 | } | |
620 | ||
f89271da EC |
621 | if (wc->byte_len < IPOIB_CM_COPYBREAK) { |
622 | int dlen = wc->byte_len; | |
623 | ||
624 | small_skb = dev_alloc_skb(dlen + 12); | |
625 | if (small_skb) { | |
626 | skb_reserve(small_skb, 12); | |
627 | ib_dma_sync_single_for_cpu(priv->ca, rx_ring[wr_id].mapping[0], | |
628 | dlen, DMA_FROM_DEVICE); | |
629 | skb_copy_from_linear_data(skb, small_skb->data, dlen); | |
630 | ib_dma_sync_single_for_device(priv->ca, rx_ring[wr_id].mapping[0], | |
631 | dlen, DMA_FROM_DEVICE); | |
632 | skb_put(small_skb, dlen); | |
633 | skb = small_skb; | |
634 | goto copied; | |
635 | } | |
636 | } | |
637 | ||
1812063b MT |
638 | frags = PAGE_ALIGN(wc->byte_len - min(wc->byte_len, |
639 | (unsigned)IPOIB_CM_HEAD_SIZE)) / PAGE_SIZE; | |
640 | ||
68e995a2 | 641 | newskb = ipoib_cm_alloc_rx_skb(dev, rx_ring, wr_id, frags, mapping); |
1812063b | 642 | if (unlikely(!newskb)) { |
839fcaba MT |
643 | /* |
644 | * If we can't allocate a new RX buffer, dump | |
645 | * this packet and reuse the old buffer. | |
646 | */ | |
647 | ipoib_dbg(priv, "failed to allocate receive buffer %d\n", wr_id); | |
de903512 | 648 | ++dev->stats.rx_dropped; |
839fcaba MT |
649 | goto repost; |
650 | } | |
651 | ||
68e995a2 PS |
652 | ipoib_cm_dma_unmap_rx(priv, frags, rx_ring[wr_id].mapping); |
653 | memcpy(rx_ring[wr_id].mapping, mapping, (frags + 1) * sizeof *mapping); | |
839fcaba MT |
654 | |
655 | ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n", | |
656 | wc->byte_len, wc->slid); | |
657 | ||
1812063b | 658 | skb_put_frags(skb, IPOIB_CM_HEAD_SIZE, wc->byte_len, newskb); |
839fcaba | 659 | |
f89271da | 660 | copied: |
839fcaba | 661 | skb->protocol = ((struct ipoib_header *) skb->data)->proto; |
459a98ed | 662 | skb_reset_mac_header(skb); |
839fcaba MT |
663 | skb_pull(skb, IPOIB_ENCAP_LEN); |
664 | ||
665 | dev->last_rx = jiffies; | |
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 MT |
711 | u64 addr; |
712 | ||
713 | if (unlikely(skb->len > tx->mtu)) { | |
714 | ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n", | |
715 | skb->len, tx->mtu); | |
de903512 RD |
716 | ++dev->stats.tx_dropped; |
717 | ++dev->stats.tx_errors; | |
77d8e1ef | 718 | ipoib_cm_skb_too_long(dev, skb, tx->mtu - IPOIB_ENCAP_LEN); |
839fcaba MT |
719 | return; |
720 | } | |
721 | ||
722 | ipoib_dbg_data(priv, "sending packet: head 0x%x length %d connection 0x%x\n", | |
723 | tx->tx_head, skb->len, tx->qp->qp_num); | |
724 | ||
725 | /* | |
726 | * We put the skb into the tx_ring _before_ we call post_send() | |
727 | * because it's entirely possible that the completion handler will | |
728 | * run before we execute anything after the post_send(). That | |
729 | * means we have to make sure everything is properly recorded and | |
730 | * our state is consistent before we call post_send(). | |
731 | */ | |
732 | tx_req = &tx->tx_ring[tx->tx_head & (ipoib_sendq_size - 1)]; | |
733 | tx_req->skb = skb; | |
734 | addr = ib_dma_map_single(priv->ca, skb->data, skb->len, DMA_TO_DEVICE); | |
735 | if (unlikely(ib_dma_mapping_error(priv->ca, addr))) { | |
de903512 | 736 | ++dev->stats.tx_errors; |
839fcaba MT |
737 | dev_kfree_skb_any(skb); |
738 | return; | |
739 | } | |
740 | ||
e112373f | 741 | tx_req->mapping = addr; |
839fcaba MT |
742 | |
743 | if (unlikely(post_send(priv, tx, tx->tx_head & (ipoib_sendq_size - 1), | |
2337f809 | 744 | addr, skb->len))) { |
839fcaba | 745 | ipoib_warn(priv, "post_send failed\n"); |
de903512 | 746 | ++dev->stats.tx_errors; |
839fcaba MT |
747 | ib_dma_unmap_single(priv->ca, addr, skb->len, DMA_TO_DEVICE); |
748 | dev_kfree_skb_any(skb); | |
749 | } else { | |
750 | dev->trans_start = jiffies; | |
751 | ++tx->tx_head; | |
752 | ||
1b524963 | 753 | if (++priv->tx_outstanding == ipoib_sendq_size) { |
839fcaba MT |
754 | ipoib_dbg(priv, "TX ring 0x%x full, stopping kernel net queue\n", |
755 | tx->qp->qp_num); | |
756 | netif_stop_queue(dev); | |
839fcaba MT |
757 | } |
758 | } | |
759 | } | |
760 | ||
1b524963 | 761 | void ipoib_cm_handle_tx_wc(struct net_device *dev, struct ib_wc *wc) |
839fcaba MT |
762 | { |
763 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
1b524963 MT |
764 | struct ipoib_cm_tx *tx = wc->qp->qp_context; |
765 | unsigned int wr_id = wc->wr_id & ~IPOIB_OP_CM; | |
e112373f | 766 | struct ipoib_cm_tx_buf *tx_req; |
839fcaba MT |
767 | unsigned long flags; |
768 | ||
a89875fc RD |
769 | ipoib_dbg_data(priv, "cm send completion: id %d, status: %d\n", |
770 | wr_id, wc->status); | |
839fcaba MT |
771 | |
772 | if (unlikely(wr_id >= ipoib_sendq_size)) { | |
773 | ipoib_warn(priv, "cm send completion event with wrid %d (> %d)\n", | |
774 | wr_id, ipoib_sendq_size); | |
775 | return; | |
776 | } | |
777 | ||
778 | tx_req = &tx->tx_ring[wr_id]; | |
779 | ||
e112373f | 780 | ib_dma_unmap_single(priv->ca, tx_req->mapping, tx_req->skb->len, DMA_TO_DEVICE); |
839fcaba MT |
781 | |
782 | /* FIXME: is this right? Shouldn't we only increment on success? */ | |
de903512 RD |
783 | ++dev->stats.tx_packets; |
784 | dev->stats.tx_bytes += tx_req->skb->len; | |
839fcaba MT |
785 | |
786 | dev_kfree_skb_any(tx_req->skb); | |
787 | ||
943c246e RD |
788 | netif_tx_lock(dev); |
789 | ||
839fcaba | 790 | ++tx->tx_tail; |
1b524963 MT |
791 | if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) && |
792 | netif_queue_stopped(dev) && | |
793 | test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) | |
839fcaba | 794 | netif_wake_queue(dev); |
839fcaba MT |
795 | |
796 | if (wc->status != IB_WC_SUCCESS && | |
797 | wc->status != IB_WC_WR_FLUSH_ERR) { | |
798 | struct ipoib_neigh *neigh; | |
799 | ||
800 | ipoib_dbg(priv, "failed cm send event " | |
801 | "(status=%d, wrid=%d vend_err %x)\n", | |
802 | wc->status, wr_id, wc->vendor_err); | |
803 | ||
943c246e | 804 | spin_lock_irqsave(&priv->lock, flags); |
839fcaba MT |
805 | neigh = tx->neigh; |
806 | ||
807 | if (neigh) { | |
808 | neigh->cm = NULL; | |
809 | list_del(&neigh->list); | |
810 | if (neigh->ah) | |
811 | ipoib_put_ah(neigh->ah); | |
812 | ipoib_neigh_free(dev, neigh); | |
813 | ||
814 | tx->neigh = NULL; | |
815 | } | |
816 | ||
839fcaba MT |
817 | if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) { |
818 | list_move(&tx->list, &priv->cm.reap_list); | |
819 | queue_work(ipoib_workqueue, &priv->cm.reap_task); | |
820 | } | |
821 | ||
822 | clear_bit(IPOIB_FLAG_OPER_UP, &tx->flags); | |
823 | ||
943c246e | 824 | spin_unlock_irqrestore(&priv->lock, flags); |
839fcaba MT |
825 | } |
826 | ||
943c246e | 827 | netif_tx_unlock(dev); |
839fcaba MT |
828 | } |
829 | ||
839fcaba MT |
830 | int ipoib_cm_dev_open(struct net_device *dev) |
831 | { | |
832 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
833 | int ret; | |
834 | ||
835 | if (!IPOIB_CM_SUPPORTED(dev->dev_addr)) | |
836 | return 0; | |
837 | ||
838 | priv->cm.id = ib_create_cm_id(priv->ca, ipoib_cm_rx_handler, dev); | |
839 | if (IS_ERR(priv->cm.id)) { | |
840 | printk(KERN_WARNING "%s: failed to create CM ID\n", priv->ca->name); | |
347fcfbe | 841 | ret = PTR_ERR(priv->cm.id); |
518b1646 | 842 | goto err_cm; |
839fcaba MT |
843 | } |
844 | ||
845 | ret = ib_cm_listen(priv->cm.id, cpu_to_be64(IPOIB_CM_IETF_ID | priv->qp->qp_num), | |
846 | 0, NULL); | |
847 | if (ret) { | |
848 | printk(KERN_WARNING "%s: failed to listen on ID 0x%llx\n", priv->ca->name, | |
849 | IPOIB_CM_IETF_ID | priv->qp->qp_num); | |
518b1646 | 850 | goto err_listen; |
839fcaba | 851 | } |
518b1646 | 852 | |
839fcaba | 853 | return 0; |
518b1646 MT |
854 | |
855 | err_listen: | |
856 | ib_destroy_cm_id(priv->cm.id); | |
857 | err_cm: | |
858 | priv->cm.id = NULL; | |
518b1646 | 859 | return ret; |
839fcaba MT |
860 | } |
861 | ||
efcd9971 RD |
862 | static void ipoib_cm_free_rx_reap_list(struct net_device *dev) |
863 | { | |
864 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
865 | struct ipoib_cm_rx *rx, *n; | |
866 | LIST_HEAD(list); | |
867 | ||
868 | spin_lock_irq(&priv->lock); | |
869 | list_splice_init(&priv->cm.rx_reap_list, &list); | |
870 | spin_unlock_irq(&priv->lock); | |
871 | ||
872 | list_for_each_entry_safe(rx, n, &list, list) { | |
873 | ib_destroy_cm_id(rx->id); | |
874 | ib_destroy_qp(rx->qp); | |
68e995a2 PS |
875 | if (!ipoib_cm_has_srq(dev)) { |
876 | ipoib_cm_free_rx_ring(priv->dev, rx->rx_ring); | |
877 | spin_lock_irq(&priv->lock); | |
878 | --priv->cm.nonsrq_conn_qp; | |
879 | spin_unlock_irq(&priv->lock); | |
880 | } | |
efcd9971 RD |
881 | kfree(rx); |
882 | } | |
883 | } | |
884 | ||
839fcaba MT |
885 | void ipoib_cm_dev_stop(struct net_device *dev) |
886 | { | |
887 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
efcd9971 | 888 | struct ipoib_cm_rx *p; |
518b1646 | 889 | unsigned long begin; |
518b1646 | 890 | int ret; |
839fcaba | 891 | |
347fcfbe | 892 | if (!IPOIB_CM_SUPPORTED(dev->dev_addr) || !priv->cm.id) |
839fcaba MT |
893 | return; |
894 | ||
895 | ib_destroy_cm_id(priv->cm.id); | |
347fcfbe | 896 | priv->cm.id = NULL; |
518b1646 | 897 | |
37aebbde | 898 | spin_lock_irq(&priv->lock); |
839fcaba MT |
899 | while (!list_empty(&priv->cm.passive_ids)) { |
900 | p = list_entry(priv->cm.passive_ids.next, typeof(*p), list); | |
518b1646 MT |
901 | list_move(&p->list, &priv->cm.rx_error_list); |
902 | p->state = IPOIB_CM_RX_ERROR; | |
37aebbde | 903 | spin_unlock_irq(&priv->lock); |
518b1646 MT |
904 | ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE); |
905 | if (ret) | |
906 | ipoib_warn(priv, "unable to move qp to error state: %d\n", ret); | |
907 | spin_lock_irq(&priv->lock); | |
908 | } | |
909 | ||
910 | /* Wait for all RX to be drained */ | |
911 | begin = jiffies; | |
912 | ||
913 | while (!list_empty(&priv->cm.rx_error_list) || | |
914 | !list_empty(&priv->cm.rx_flush_list) || | |
915 | !list_empty(&priv->cm.rx_drain_list)) { | |
8fd357a6 | 916 | if (time_after(jiffies, begin + 5 * HZ)) { |
518b1646 MT |
917 | ipoib_warn(priv, "RX drain timing out\n"); |
918 | ||
919 | /* | |
920 | * assume the HW is wedged and just free up everything. | |
921 | */ | |
ec229e5e PS |
922 | list_splice_init(&priv->cm.rx_flush_list, |
923 | &priv->cm.rx_reap_list); | |
924 | list_splice_init(&priv->cm.rx_error_list, | |
925 | &priv->cm.rx_reap_list); | |
926 | list_splice_init(&priv->cm.rx_drain_list, | |
927 | &priv->cm.rx_reap_list); | |
518b1646 MT |
928 | break; |
929 | } | |
930 | spin_unlock_irq(&priv->lock); | |
931 | msleep(1); | |
2dfbfc37 | 932 | ipoib_drain_cq(dev); |
518b1646 MT |
933 | spin_lock_irq(&priv->lock); |
934 | } | |
935 | ||
518b1646 MT |
936 | spin_unlock_irq(&priv->lock); |
937 | ||
efcd9971 | 938 | ipoib_cm_free_rx_reap_list(dev); |
839fcaba MT |
939 | |
940 | cancel_delayed_work(&priv->cm.stale_task); | |
941 | } | |
942 | ||
943 | static int ipoib_cm_rep_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event) | |
944 | { | |
945 | struct ipoib_cm_tx *p = cm_id->context; | |
946 | struct ipoib_dev_priv *priv = netdev_priv(p->dev); | |
947 | struct ipoib_cm_data *data = event->private_data; | |
948 | struct sk_buff_head skqueue; | |
949 | struct ib_qp_attr qp_attr; | |
950 | int qp_attr_mask, ret; | |
951 | struct sk_buff *skb; | |
839fcaba MT |
952 | |
953 | p->mtu = be32_to_cpu(data->mtu); | |
954 | ||
82c3aca6 MT |
955 | if (p->mtu <= IPOIB_ENCAP_LEN) { |
956 | ipoib_warn(priv, "Rejecting connection: mtu %d <= %d\n", | |
957 | p->mtu, IPOIB_ENCAP_LEN); | |
839fcaba MT |
958 | return -EINVAL; |
959 | } | |
960 | ||
961 | qp_attr.qp_state = IB_QPS_RTR; | |
962 | ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask); | |
963 | if (ret) { | |
964 | ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret); | |
965 | return ret; | |
966 | } | |
967 | ||
968 | qp_attr.rq_psn = 0 /* FIXME */; | |
969 | ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask); | |
970 | if (ret) { | |
971 | ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret); | |
972 | return ret; | |
973 | } | |
974 | ||
975 | qp_attr.qp_state = IB_QPS_RTS; | |
976 | ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask); | |
977 | if (ret) { | |
978 | ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret); | |
979 | return ret; | |
980 | } | |
981 | ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask); | |
982 | if (ret) { | |
983 | ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret); | |
984 | return ret; | |
985 | } | |
986 | ||
987 | skb_queue_head_init(&skqueue); | |
988 | ||
37aebbde | 989 | spin_lock_irq(&priv->lock); |
839fcaba MT |
990 | set_bit(IPOIB_FLAG_OPER_UP, &p->flags); |
991 | if (p->neigh) | |
992 | while ((skb = __skb_dequeue(&p->neigh->queue))) | |
993 | __skb_queue_tail(&skqueue, skb); | |
37aebbde | 994 | spin_unlock_irq(&priv->lock); |
839fcaba MT |
995 | |
996 | while ((skb = __skb_dequeue(&skqueue))) { | |
997 | skb->dev = p->dev; | |
998 | if (dev_queue_xmit(skb)) | |
999 | ipoib_warn(priv, "dev_queue_xmit failed " | |
1000 | "to requeue packet\n"); | |
1001 | } | |
1002 | ||
1003 | ret = ib_send_cm_rtu(cm_id, NULL, 0); | |
1004 | if (ret) { | |
1005 | ipoib_warn(priv, "failed to send RTU: %d\n", ret); | |
1006 | return ret; | |
1007 | } | |
1008 | return 0; | |
1009 | } | |
1010 | ||
1b524963 | 1011 | static struct ib_qp *ipoib_cm_create_tx_qp(struct net_device *dev, struct ipoib_cm_tx *tx) |
839fcaba MT |
1012 | { |
1013 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
ede6bc04 | 1014 | struct ib_qp_init_attr attr = { |
f56bcd80 EC |
1015 | .send_cq = priv->recv_cq, |
1016 | .recv_cq = priv->recv_cq, | |
ede6bc04 DB |
1017 | .srq = priv->cm.srq, |
1018 | .cap.max_send_wr = ipoib_sendq_size, | |
1019 | .cap.max_send_sge = 1, | |
1020 | .sq_sig_type = IB_SIGNAL_ALL_WR, | |
1021 | .qp_type = IB_QPT_RC, | |
1b524963 | 1022 | .qp_context = tx |
2337f809 | 1023 | }; |
ede6bc04 | 1024 | |
839fcaba MT |
1025 | return ib_create_qp(priv->pd, &attr); |
1026 | } | |
1027 | ||
1028 | static int ipoib_cm_send_req(struct net_device *dev, | |
1029 | struct ib_cm_id *id, struct ib_qp *qp, | |
1030 | u32 qpn, | |
1031 | struct ib_sa_path_rec *pathrec) | |
1032 | { | |
1033 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
1034 | struct ipoib_cm_data data = {}; | |
1035 | struct ib_cm_req_param req = {}; | |
1036 | ||
1037 | data.qpn = cpu_to_be32(priv->qp->qp_num); | |
1038 | data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE); | |
1039 | ||
2337f809 RD |
1040 | req.primary_path = pathrec; |
1041 | req.alternate_path = NULL; | |
1042 | req.service_id = cpu_to_be64(IPOIB_CM_IETF_ID | qpn); | |
1043 | req.qp_num = qp->qp_num; | |
1044 | req.qp_type = qp->qp_type; | |
1045 | req.private_data = &data; | |
1046 | req.private_data_len = sizeof data; | |
1047 | req.flow_control = 0; | |
839fcaba | 1048 | |
2337f809 | 1049 | req.starting_psn = 0; /* FIXME */ |
839fcaba MT |
1050 | |
1051 | /* | |
1052 | * Pick some arbitrary defaults here; we could make these | |
1053 | * module parameters if anyone cared about setting them. | |
1054 | */ | |
2337f809 RD |
1055 | req.responder_resources = 4; |
1056 | req.remote_cm_response_timeout = 20; | |
1057 | req.local_cm_response_timeout = 20; | |
1058 | req.retry_count = 0; /* RFC draft warns against retries */ | |
1059 | req.rnr_retry_count = 0; /* RFC draft warns against retries */ | |
1060 | req.max_cm_retries = 15; | |
68e995a2 | 1061 | req.srq = ipoib_cm_has_srq(dev); |
839fcaba MT |
1062 | return ib_send_cm_req(id, &req); |
1063 | } | |
1064 | ||
1065 | static int ipoib_cm_modify_tx_init(struct net_device *dev, | |
1066 | struct ib_cm_id *cm_id, struct ib_qp *qp) | |
1067 | { | |
1068 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
1069 | struct ib_qp_attr qp_attr; | |
1070 | int qp_attr_mask, ret; | |
9fdd5e5b | 1071 | ret = ib_find_pkey(priv->ca, priv->port, priv->pkey, &qp_attr.pkey_index); |
839fcaba | 1072 | if (ret) { |
9fdd5e5b | 1073 | ipoib_warn(priv, "pkey 0x%x not found: %d\n", priv->pkey, ret); |
839fcaba MT |
1074 | return ret; |
1075 | } | |
1076 | ||
1077 | qp_attr.qp_state = IB_QPS_INIT; | |
1078 | qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE; | |
1079 | qp_attr.port_num = priv->port; | |
1080 | qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT; | |
1081 | ||
1082 | ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask); | |
1083 | if (ret) { | |
1084 | ipoib_warn(priv, "failed to modify tx QP to INIT: %d\n", ret); | |
1085 | return ret; | |
1086 | } | |
1087 | return 0; | |
1088 | } | |
1089 | ||
1090 | static int ipoib_cm_tx_init(struct ipoib_cm_tx *p, u32 qpn, | |
1091 | struct ib_sa_path_rec *pathrec) | |
1092 | { | |
1093 | struct ipoib_dev_priv *priv = netdev_priv(p->dev); | |
1094 | int ret; | |
1095 | ||
10313cbb | 1096 | p->tx_ring = vmalloc(ipoib_sendq_size * sizeof *p->tx_ring); |
839fcaba MT |
1097 | if (!p->tx_ring) { |
1098 | ipoib_warn(priv, "failed to allocate tx ring\n"); | |
1099 | ret = -ENOMEM; | |
1100 | goto err_tx; | |
1101 | } | |
10313cbb | 1102 | memset(p->tx_ring, 0, ipoib_sendq_size * sizeof *p->tx_ring); |
839fcaba | 1103 | |
1b524963 | 1104 | p->qp = ipoib_cm_create_tx_qp(p->dev, p); |
839fcaba MT |
1105 | if (IS_ERR(p->qp)) { |
1106 | ret = PTR_ERR(p->qp); | |
1107 | ipoib_warn(priv, "failed to allocate tx qp: %d\n", ret); | |
1108 | goto err_qp; | |
1109 | } | |
1110 | ||
1111 | p->id = ib_create_cm_id(priv->ca, ipoib_cm_tx_handler, p); | |
1112 | if (IS_ERR(p->id)) { | |
1113 | ret = PTR_ERR(p->id); | |
1114 | ipoib_warn(priv, "failed to create tx cm id: %d\n", ret); | |
1115 | goto err_id; | |
1116 | } | |
1117 | ||
1118 | ret = ipoib_cm_modify_tx_init(p->dev, p->id, p->qp); | |
1119 | if (ret) { | |
1120 | ipoib_warn(priv, "failed to modify tx qp to rtr: %d\n", ret); | |
1121 | goto err_modify; | |
1122 | } | |
1123 | ||
1124 | ret = ipoib_cm_send_req(p->dev, p->id, p->qp, qpn, pathrec); | |
1125 | if (ret) { | |
1126 | ipoib_warn(priv, "failed to send cm req: %d\n", ret); | |
1127 | goto err_send_cm; | |
1128 | } | |
1129 | ||
5b095d98 | 1130 | ipoib_dbg(priv, "Request connection 0x%x for gid %pI6 qpn 0x%x\n", |
fcace2fe | 1131 | p->qp->qp_num, pathrec->dgid.raw, qpn); |
839fcaba MT |
1132 | |
1133 | return 0; | |
1134 | ||
1135 | err_send_cm: | |
1136 | err_modify: | |
1137 | ib_destroy_cm_id(p->id); | |
1138 | err_id: | |
1139 | p->id = NULL; | |
1140 | ib_destroy_qp(p->qp); | |
839fcaba MT |
1141 | err_qp: |
1142 | p->qp = NULL; | |
10313cbb | 1143 | vfree(p->tx_ring); |
839fcaba MT |
1144 | err_tx: |
1145 | return ret; | |
1146 | } | |
1147 | ||
1148 | static void ipoib_cm_tx_destroy(struct ipoib_cm_tx *p) | |
1149 | { | |
1150 | struct ipoib_dev_priv *priv = netdev_priv(p->dev); | |
e112373f | 1151 | struct ipoib_cm_tx_buf *tx_req; |
1b524963 | 1152 | unsigned long begin; |
839fcaba MT |
1153 | |
1154 | ipoib_dbg(priv, "Destroy active connection 0x%x head 0x%x tail 0x%x\n", | |
1155 | p->qp ? p->qp->qp_num : 0, p->tx_head, p->tx_tail); | |
1156 | ||
1157 | if (p->id) | |
1158 | ib_destroy_cm_id(p->id); | |
1159 | ||
839fcaba | 1160 | if (p->tx_ring) { |
1b524963 MT |
1161 | /* Wait for all sends to complete */ |
1162 | begin = jiffies; | |
839fcaba | 1163 | while ((int) p->tx_tail - (int) p->tx_head < 0) { |
1b524963 MT |
1164 | if (time_after(jiffies, begin + 5 * HZ)) { |
1165 | ipoib_warn(priv, "timing out; %d sends not completed\n", | |
1166 | p->tx_head - p->tx_tail); | |
1167 | goto timeout; | |
1168 | } | |
1169 | ||
1170 | msleep(1); | |
839fcaba | 1171 | } |
1b524963 MT |
1172 | } |
1173 | ||
1174 | timeout: | |
839fcaba | 1175 | |
1b524963 MT |
1176 | while ((int) p->tx_tail - (int) p->tx_head < 0) { |
1177 | tx_req = &p->tx_ring[p->tx_tail & (ipoib_sendq_size - 1)]; | |
e112373f | 1178 | ib_dma_unmap_single(priv->ca, tx_req->mapping, tx_req->skb->len, |
1b524963 MT |
1179 | DMA_TO_DEVICE); |
1180 | dev_kfree_skb_any(tx_req->skb); | |
1181 | ++p->tx_tail; | |
943c246e | 1182 | netif_tx_lock_bh(p->dev); |
1b524963 MT |
1183 | if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) && |
1184 | netif_queue_stopped(p->dev) && | |
1185 | test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) | |
1186 | netif_wake_queue(p->dev); | |
943c246e | 1187 | netif_tx_unlock_bh(p->dev); |
839fcaba MT |
1188 | } |
1189 | ||
1b524963 MT |
1190 | if (p->qp) |
1191 | ib_destroy_qp(p->qp); | |
1192 | ||
10313cbb | 1193 | vfree(p->tx_ring); |
839fcaba MT |
1194 | kfree(p); |
1195 | } | |
1196 | ||
1197 | static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id, | |
1198 | struct ib_cm_event *event) | |
1199 | { | |
1200 | struct ipoib_cm_tx *tx = cm_id->context; | |
1201 | struct ipoib_dev_priv *priv = netdev_priv(tx->dev); | |
1202 | struct net_device *dev = priv->dev; | |
1203 | struct ipoib_neigh *neigh; | |
943c246e | 1204 | unsigned long flags; |
839fcaba MT |
1205 | int ret; |
1206 | ||
1207 | switch (event->event) { | |
1208 | case IB_CM_DREQ_RECEIVED: | |
1209 | ipoib_dbg(priv, "DREQ received.\n"); | |
1210 | ib_send_cm_drep(cm_id, NULL, 0); | |
1211 | break; | |
1212 | case IB_CM_REP_RECEIVED: | |
1213 | ipoib_dbg(priv, "REP received.\n"); | |
1214 | ret = ipoib_cm_rep_handler(cm_id, event); | |
1215 | if (ret) | |
1216 | ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, | |
1217 | NULL, 0, NULL, 0); | |
1218 | break; | |
1219 | case IB_CM_REQ_ERROR: | |
1220 | case IB_CM_REJ_RECEIVED: | |
1221 | case IB_CM_TIMEWAIT_EXIT: | |
1222 | ipoib_dbg(priv, "CM error %d.\n", event->event); | |
943c246e RD |
1223 | netif_tx_lock_bh(dev); |
1224 | spin_lock_irqsave(&priv->lock, flags); | |
839fcaba MT |
1225 | neigh = tx->neigh; |
1226 | ||
1227 | if (neigh) { | |
1228 | neigh->cm = NULL; | |
1229 | list_del(&neigh->list); | |
1230 | if (neigh->ah) | |
1231 | ipoib_put_ah(neigh->ah); | |
1232 | ipoib_neigh_free(dev, neigh); | |
1233 | ||
1234 | tx->neigh = NULL; | |
1235 | } | |
1236 | ||
1237 | if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) { | |
1238 | list_move(&tx->list, &priv->cm.reap_list); | |
1239 | queue_work(ipoib_workqueue, &priv->cm.reap_task); | |
1240 | } | |
1241 | ||
943c246e RD |
1242 | spin_unlock_irqrestore(&priv->lock, flags); |
1243 | netif_tx_unlock_bh(dev); | |
839fcaba MT |
1244 | break; |
1245 | default: | |
1246 | break; | |
1247 | } | |
1248 | ||
1249 | return 0; | |
1250 | } | |
1251 | ||
1252 | struct ipoib_cm_tx *ipoib_cm_create_tx(struct net_device *dev, struct ipoib_path *path, | |
1253 | struct ipoib_neigh *neigh) | |
1254 | { | |
1255 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
1256 | struct ipoib_cm_tx *tx; | |
1257 | ||
1258 | tx = kzalloc(sizeof *tx, GFP_ATOMIC); | |
1259 | if (!tx) | |
1260 | return NULL; | |
1261 | ||
1262 | neigh->cm = tx; | |
1263 | tx->neigh = neigh; | |
1264 | tx->path = path; | |
1265 | tx->dev = dev; | |
1266 | list_add(&tx->list, &priv->cm.start_list); | |
1267 | set_bit(IPOIB_FLAG_INITIALIZED, &tx->flags); | |
1268 | queue_work(ipoib_workqueue, &priv->cm.start_task); | |
1269 | return tx; | |
1270 | } | |
1271 | ||
1272 | void ipoib_cm_destroy_tx(struct ipoib_cm_tx *tx) | |
1273 | { | |
1274 | struct ipoib_dev_priv *priv = netdev_priv(tx->dev); | |
1275 | if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) { | |
1276 | list_move(&tx->list, &priv->cm.reap_list); | |
1277 | queue_work(ipoib_workqueue, &priv->cm.reap_task); | |
5b095d98 | 1278 | ipoib_dbg(priv, "Reap connection for gid %pI6\n", |
fcace2fe | 1279 | tx->neigh->dgid.raw); |
839fcaba MT |
1280 | tx->neigh = NULL; |
1281 | } | |
1282 | } | |
1283 | ||
1284 | static void ipoib_cm_tx_start(struct work_struct *work) | |
1285 | { | |
1286 | struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv, | |
1287 | cm.start_task); | |
1288 | struct net_device *dev = priv->dev; | |
1289 | struct ipoib_neigh *neigh; | |
1290 | struct ipoib_cm_tx *p; | |
1291 | unsigned long flags; | |
1292 | int ret; | |
1293 | ||
1294 | struct ib_sa_path_rec pathrec; | |
1295 | u32 qpn; | |
1296 | ||
943c246e RD |
1297 | netif_tx_lock_bh(dev); |
1298 | spin_lock_irqsave(&priv->lock, flags); | |
1299 | ||
839fcaba MT |
1300 | while (!list_empty(&priv->cm.start_list)) { |
1301 | p = list_entry(priv->cm.start_list.next, typeof(*p), list); | |
1302 | list_del_init(&p->list); | |
1303 | neigh = p->neigh; | |
1304 | qpn = IPOIB_QPN(neigh->neighbour->ha); | |
1305 | memcpy(&pathrec, &p->path->pathrec, sizeof pathrec); | |
943c246e RD |
1306 | |
1307 | spin_unlock_irqrestore(&priv->lock, flags); | |
1308 | netif_tx_unlock_bh(dev); | |
1309 | ||
839fcaba | 1310 | ret = ipoib_cm_tx_init(p, qpn, &pathrec); |
943c246e RD |
1311 | |
1312 | netif_tx_lock_bh(dev); | |
1313 | spin_lock_irqsave(&priv->lock, flags); | |
1314 | ||
839fcaba MT |
1315 | if (ret) { |
1316 | neigh = p->neigh; | |
1317 | if (neigh) { | |
1318 | neigh->cm = NULL; | |
1319 | list_del(&neigh->list); | |
1320 | if (neigh->ah) | |
1321 | ipoib_put_ah(neigh->ah); | |
1322 | ipoib_neigh_free(dev, neigh); | |
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)); | |
1376 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | |
1377 | else if (skb->protocol == htons(ETH_P_IPV6)) | |
20089ca5 | 1378 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, priv->dev); |
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 ED |
1396 | if (skb_dst(skb)) |
1397 | skb_dst(skb)->ops->update_pmtu(skb_dst(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 | ||
1451 | static ssize_t set_mode(struct device *d, struct device_attribute *attr, | |
1452 | const char *buf, size_t count) | |
1453 | { | |
1454 | struct net_device *dev = to_net_dev(d); | |
1455 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
1456 | ||
26574401 EB |
1457 | if (!rtnl_trylock()) |
1458 | return restart_syscall(); | |
1459 | ||
839fcaba MT |
1460 | /* flush paths if we switch modes so that connections are restarted */ |
1461 | if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) { | |
1462 | set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags); | |
1463 | ipoib_warn(priv, "enabling connected mode " | |
1464 | "will cause multicast packet drops\n"); | |
6046136c | 1465 | |
40ca1988 | 1466 | dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_TSO); |
c8c2afe3 | 1467 | rtnl_unlock(); |
6046136c EC |
1468 | priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM; |
1469 | ||
839fcaba MT |
1470 | ipoib_flush_paths(dev); |
1471 | return count; | |
1472 | } | |
1473 | ||
1474 | if (!strcmp(buf, "datagram\n")) { | |
1475 | clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags); | |
6046136c | 1476 | |
40ca1988 | 1477 | if (test_bit(IPOIB_FLAG_CSUM, &priv->flags)) { |
6046136c | 1478 | dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG; |
40ca1988 EC |
1479 | if (priv->hca_caps & IB_DEVICE_UD_TSO) |
1480 | dev->features |= NETIF_F_TSO; | |
1481 | } | |
bd360671 | 1482 | dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu)); |
c8c2afe3 EC |
1483 | rtnl_unlock(); |
1484 | ipoib_flush_paths(dev); | |
6046136c | 1485 | |
839fcaba MT |
1486 | return count; |
1487 | } | |
26574401 | 1488 | rtnl_unlock(); |
839fcaba MT |
1489 | |
1490 | return -EINVAL; | |
1491 | } | |
1492 | ||
551fd612 | 1493 | static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO, show_mode, set_mode); |
839fcaba MT |
1494 | |
1495 | int ipoib_cm_add_mode_attr(struct net_device *dev) | |
1496 | { | |
1497 | return device_create_file(&dev->dev, &dev_attr_mode); | |
1498 | } | |
1499 | ||
586a6934 | 1500 | static void ipoib_cm_create_srq(struct net_device *dev, int max_sge) |
839fcaba MT |
1501 | { |
1502 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
1503 | struct ib_srq_init_attr srq_init_attr = { | |
1504 | .attr = { | |
1505 | .max_wr = ipoib_recvq_size, | |
586a6934 | 1506 | .max_sge = max_sge |
839fcaba MT |
1507 | } |
1508 | }; | |
7b3687df RD |
1509 | |
1510 | priv->cm.srq = ib_create_srq(priv->pd, &srq_init_attr); | |
1511 | if (IS_ERR(priv->cm.srq)) { | |
68e995a2 PS |
1512 | if (PTR_ERR(priv->cm.srq) != -ENOSYS) |
1513 | printk(KERN_WARNING "%s: failed to allocate SRQ, error %ld\n", | |
1514 | priv->ca->name, PTR_ERR(priv->cm.srq)); | |
7b3687df | 1515 | priv->cm.srq = NULL; |
68e995a2 | 1516 | return; |
7b3687df RD |
1517 | } |
1518 | ||
b1404069 | 1519 | priv->cm.srq_ring = vmalloc(ipoib_recvq_size * sizeof *priv->cm.srq_ring); |
7b3687df | 1520 | if (!priv->cm.srq_ring) { |
68e995a2 | 1521 | printk(KERN_WARNING "%s: failed to allocate CM SRQ ring (%d entries)\n", |
7b3687df RD |
1522 | priv->ca->name, ipoib_recvq_size); |
1523 | ib_destroy_srq(priv->cm.srq); | |
1524 | priv->cm.srq = NULL; | |
b1404069 | 1525 | return; |
7b3687df | 1526 | } |
b1404069 DW |
1527 | |
1528 | memset(priv->cm.srq_ring, 0, ipoib_recvq_size * sizeof *priv->cm.srq_ring); | |
7b3687df RD |
1529 | } |
1530 | ||
1531 | int ipoib_cm_dev_init(struct net_device *dev) | |
1532 | { | |
1533 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
586a6934 PS |
1534 | int i, ret; |
1535 | struct ib_device_attr attr; | |
839fcaba MT |
1536 | |
1537 | INIT_LIST_HEAD(&priv->cm.passive_ids); | |
1538 | INIT_LIST_HEAD(&priv->cm.reap_list); | |
1539 | INIT_LIST_HEAD(&priv->cm.start_list); | |
518b1646 MT |
1540 | INIT_LIST_HEAD(&priv->cm.rx_error_list); |
1541 | INIT_LIST_HEAD(&priv->cm.rx_flush_list); | |
1542 | INIT_LIST_HEAD(&priv->cm.rx_drain_list); | |
1543 | INIT_LIST_HEAD(&priv->cm.rx_reap_list); | |
839fcaba MT |
1544 | INIT_WORK(&priv->cm.start_task, ipoib_cm_tx_start); |
1545 | INIT_WORK(&priv->cm.reap_task, ipoib_cm_tx_reap); | |
1546 | INIT_WORK(&priv->cm.skb_task, ipoib_cm_skb_reap); | |
518b1646 | 1547 | INIT_WORK(&priv->cm.rx_reap_task, ipoib_cm_rx_reap); |
839fcaba MT |
1548 | INIT_DELAYED_WORK(&priv->cm.stale_task, ipoib_cm_stale_task); |
1549 | ||
1550 | skb_queue_head_init(&priv->cm.skb_queue); | |
1551 | ||
586a6934 PS |
1552 | ret = ib_query_device(priv->ca, &attr); |
1553 | if (ret) { | |
1554 | printk(KERN_WARNING "ib_query_device() failed with %d\n", ret); | |
1555 | return ret; | |
1556 | } | |
1557 | ||
1558 | ipoib_dbg(priv, "max_srq_sge=%d\n", attr.max_srq_sge); | |
1559 | ||
1560 | attr.max_srq_sge = min_t(int, IPOIB_CM_RX_SG, attr.max_srq_sge); | |
1561 | ipoib_cm_create_srq(dev, attr.max_srq_sge); | |
1562 | if (ipoib_cm_has_srq(dev)) { | |
1563 | priv->cm.max_cm_mtu = attr.max_srq_sge * PAGE_SIZE - 0x10; | |
1564 | priv->cm.num_frags = attr.max_srq_sge; | |
1565 | ipoib_dbg(priv, "max_cm_mtu = 0x%x, num_frags=%d\n", | |
1566 | priv->cm.max_cm_mtu, priv->cm.num_frags); | |
1567 | } else { | |
1568 | priv->cm.max_cm_mtu = IPOIB_CM_MTU; | |
1569 | priv->cm.num_frags = IPOIB_CM_RX_SG; | |
1570 | } | |
1571 | ||
a7d834c4 | 1572 | ipoib_cm_init_rx_wr(dev, &priv->cm.rx_wr, priv->cm.rx_sge); |
68e995a2 PS |
1573 | |
1574 | if (ipoib_cm_has_srq(dev)) { | |
1575 | for (i = 0; i < ipoib_recvq_size; ++i) { | |
1576 | if (!ipoib_cm_alloc_rx_skb(dev, priv->cm.srq_ring, i, | |
586a6934 | 1577 | priv->cm.num_frags - 1, |
68e995a2 PS |
1578 | priv->cm.srq_ring[i].mapping)) { |
1579 | ipoib_warn(priv, "failed to allocate " | |
1580 | "receive buffer %d\n", i); | |
1581 | ipoib_cm_dev_cleanup(dev); | |
1582 | return -ENOMEM; | |
1583 | } | |
7b3687df | 1584 | |
68e995a2 PS |
1585 | if (ipoib_cm_post_receive_srq(dev, i)) { |
1586 | ipoib_warn(priv, "ipoib_cm_post_receive_srq " | |
1587 | "failed for buf %d\n", i); | |
1588 | ipoib_cm_dev_cleanup(dev); | |
1589 | return -EIO; | |
1590 | } | |
839fcaba MT |
1591 | } |
1592 | } | |
1593 | ||
1594 | priv->dev->dev_addr[0] = IPOIB_FLAGS_RC; | |
1595 | return 0; | |
1596 | } | |
1597 | ||
1598 | void ipoib_cm_dev_cleanup(struct net_device *dev) | |
1599 | { | |
1600 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
1efb6144 | 1601 | int ret; |
839fcaba MT |
1602 | |
1603 | if (!priv->cm.srq) | |
1604 | return; | |
1605 | ||
1606 | ipoib_dbg(priv, "Cleanup ipoib connected mode.\n"); | |
1607 | ||
1608 | ret = ib_destroy_srq(priv->cm.srq); | |
1609 | if (ret) | |
1610 | ipoib_warn(priv, "ib_destroy_srq failed: %d\n", ret); | |
1611 | ||
1612 | priv->cm.srq = NULL; | |
1613 | if (!priv->cm.srq_ring) | |
1614 | return; | |
1efb6144 RD |
1615 | |
1616 | ipoib_cm_free_rx_ring(dev, priv->cm.srq_ring); | |
839fcaba MT |
1617 | priv->cm.srq_ring = NULL; |
1618 | } |