IPoIB: Add high DMA feature flag
[deliverable/linux.git] / drivers / infiniband / ulp / ipoib / ipoib_ib.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
2a1d9b7f
RD
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
5 * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
1da177e4
LT
6 *
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
12 *
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
16 *
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer.
20 *
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * SOFTWARE.
34 *
35 * $Id: ipoib_ib.c 1386 2004-12-27 16:23:17Z roland $
36 */
37
38#include <linux/delay.h>
39#include <linux/dma-mapping.h>
40
a4d61e84 41#include <rdma/ib_cache.h>
1da177e4
LT
42
43#include "ipoib.h"
44
45#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
46static int data_debug_level;
47
48module_param(data_debug_level, int, 0644);
49MODULE_PARM_DESC(data_debug_level,
50 "Enable data path debug tracing if > 0");
51#endif
52
95ed644f 53static DEFINE_MUTEX(pkey_mutex);
1da177e4
LT
54
55struct ipoib_ah *ipoib_create_ah(struct net_device *dev,
56 struct ib_pd *pd, struct ib_ah_attr *attr)
57{
58 struct ipoib_ah *ah;
59
60 ah = kmalloc(sizeof *ah, GFP_KERNEL);
61 if (!ah)
62 return NULL;
63
64 ah->dev = dev;
65 ah->last_send = 0;
66 kref_init(&ah->ref);
67
68 ah->ah = ib_create_ah(pd, attr);
69 if (IS_ERR(ah->ah)) {
70 kfree(ah);
71 ah = NULL;
72 } else
73 ipoib_dbg(netdev_priv(dev), "Created ah %p\n", ah->ah);
74
75 return ah;
76}
77
78void ipoib_free_ah(struct kref *kref)
79{
80 struct ipoib_ah *ah = container_of(kref, struct ipoib_ah, ref);
81 struct ipoib_dev_priv *priv = netdev_priv(ah->dev);
82
83 unsigned long flags;
84
31c02e21
RD
85 spin_lock_irqsave(&priv->lock, flags);
86 list_add_tail(&ah->list, &priv->dead_ahs);
87 spin_unlock_irqrestore(&priv->lock, flags);
1da177e4
LT
88}
89
1993d683 90static int ipoib_ib_post_receive(struct net_device *dev, int id)
1da177e4 91{
1993d683
RD
92 struct ipoib_dev_priv *priv = netdev_priv(dev);
93 struct ib_sge list;
94 struct ib_recv_wr param;
1da177e4 95 struct ib_recv_wr *bad_wr;
1993d683
RD
96 int ret;
97
98 list.addr = priv->rx_ring[id].mapping;
99 list.length = IPOIB_BUF_SIZE;
100 list.lkey = priv->mr->lkey;
101
102 param.next = NULL;
103 param.wr_id = id | IPOIB_OP_RECV;
104 param.sg_list = &list;
105 param.num_sge = 1;
106
107 ret = ib_post_recv(priv->qp, &param, &bad_wr);
108 if (unlikely(ret)) {
109 ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);
37ccf9df
RC
110 ib_dma_unmap_single(priv->ca, priv->rx_ring[id].mapping,
111 IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
1993d683
RD
112 dev_kfree_skb_any(priv->rx_ring[id].skb);
113 priv->rx_ring[id].skb = NULL;
114 }
1da177e4 115
1993d683 116 return ret;
1da177e4
LT
117}
118
1993d683 119static int ipoib_alloc_rx_skb(struct net_device *dev, int id)
1da177e4
LT
120{
121 struct ipoib_dev_priv *priv = netdev_priv(dev);
122 struct sk_buff *skb;
37ccf9df 123 u64 addr;
1da177e4
LT
124
125 skb = dev_alloc_skb(IPOIB_BUF_SIZE + 4);
1993d683 126 if (!skb)
1da177e4 127 return -ENOMEM;
1993d683
RD
128
129 /*
130 * IB will leave a 40 byte gap for a GRH and IPoIB adds a 4 byte
131 * header. So we need 4 more bytes to get to 48 and align the
132 * IP header to a multiple of 16.
133 */
134 skb_reserve(skb, 4);
135
37ccf9df
RC
136 addr = ib_dma_map_single(priv->ca, skb->data, IPOIB_BUF_SIZE,
137 DMA_FROM_DEVICE);
138 if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
1da177e4 139 dev_kfree_skb_any(skb);
1993d683 140 return -EIO;
1da177e4
LT
141 }
142
1993d683
RD
143 priv->rx_ring[id].skb = skb;
144 priv->rx_ring[id].mapping = addr;
145
146 return 0;
1da177e4
LT
147}
148
149static int ipoib_ib_post_receives(struct net_device *dev)
150{
151 struct ipoib_dev_priv *priv = netdev_priv(dev);
152 int i;
153
0f485251 154 for (i = 0; i < ipoib_recvq_size; ++i) {
1993d683
RD
155 if (ipoib_alloc_rx_skb(dev, i)) {
156 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
157 return -ENOMEM;
158 }
1da177e4
LT
159 if (ipoib_ib_post_receive(dev, i)) {
160 ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
161 return -EIO;
162 }
163 }
164
165 return 0;
166}
167
2439a6e6 168static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
1da177e4
LT
169{
170 struct ipoib_dev_priv *priv = netdev_priv(dev);
2439a6e6
RD
171 unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
172 struct sk_buff *skb;
37ccf9df 173 u64 addr;
1da177e4 174
a89875fc
RD
175 ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
176 wr_id, wc->status);
1da177e4 177
2439a6e6
RD
178 if (unlikely(wr_id >= ipoib_recvq_size)) {
179 ipoib_warn(priv, "recv completion event with wrid %d (> %d)\n",
180 wr_id, ipoib_recvq_size);
181 return;
182 }
183
184 skb = priv->rx_ring[wr_id].skb;
185 addr = priv->rx_ring[wr_id].mapping;
186
187 if (unlikely(wc->status != IB_WC_SUCCESS)) {
188 if (wc->status != IB_WC_WR_FLUSH_ERR)
189 ipoib_warn(priv, "failed recv event "
190 "(status=%d, wrid=%d vend_err %x)\n",
191 wc->status, wr_id, wc->vendor_err);
37ccf9df
RC
192 ib_dma_unmap_single(priv->ca, addr,
193 IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
2439a6e6
RD
194 dev_kfree_skb_any(skb);
195 priv->rx_ring[wr_id].skb = NULL;
196 return;
197 }
1da177e4 198
1b844afe
RD
199 /*
200 * Drop packets that this interface sent, ie multicast packets
201 * that the HCA has replicated.
202 */
203 if (wc->slid == priv->local_lid && wc->src_qp == priv->qp->qp_num)
204 goto repost;
205
2439a6e6
RD
206 /*
207 * If we can't allocate a new RX buffer, dump
208 * this packet and reuse the old buffer.
209 */
210 if (unlikely(ipoib_alloc_rx_skb(dev, wr_id))) {
de903512 211 ++dev->stats.rx_dropped;
2439a6e6
RD
212 goto repost;
213 }
214
215 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
216 wc->byte_len, wc->slid);
217
37ccf9df 218 ib_dma_unmap_single(priv->ca, addr, IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
2439a6e6
RD
219
220 skb_put(skb, wc->byte_len);
221 skb_pull(skb, IB_GRH_BYTES);
222
1b844afe
RD
223 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
224 skb_reset_mac_header(skb);
225 skb_pull(skb, IPOIB_ENCAP_LEN);
226
227 dev->last_rx = jiffies;
de903512
RD
228 ++dev->stats.rx_packets;
229 dev->stats.rx_bytes += skb->len;
1b844afe
RD
230
231 skb->dev = dev;
232 /* XXX get correct PACKET_ type here */
233 skb->pkt_type = PACKET_HOST;
234 netif_receive_skb(skb);
1da177e4 235
2439a6e6
RD
236repost:
237 if (unlikely(ipoib_ib_post_receive(dev, wr_id)))
238 ipoib_warn(priv, "ipoib_ib_post_receive failed "
239 "for buf %d\n", wr_id);
240}
1da177e4 241
2439a6e6
RD
242static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
243{
244 struct ipoib_dev_priv *priv = netdev_priv(dev);
245 unsigned int wr_id = wc->wr_id;
246 struct ipoib_tx_buf *tx_req;
247 unsigned long flags;
1da177e4 248
a89875fc
RD
249 ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
250 wr_id, wc->status);
1da177e4 251
2439a6e6
RD
252 if (unlikely(wr_id >= ipoib_sendq_size)) {
253 ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
254 wr_id, ipoib_sendq_size);
255 return;
1da177e4 256 }
2439a6e6
RD
257
258 tx_req = &priv->tx_ring[wr_id];
259
37ccf9df
RC
260 ib_dma_unmap_single(priv->ca, tx_req->mapping,
261 tx_req->skb->len, DMA_TO_DEVICE);
2439a6e6 262
de903512
RD
263 ++dev->stats.tx_packets;
264 dev->stats.tx_bytes += tx_req->skb->len;
2439a6e6
RD
265
266 dev_kfree_skb_any(tx_req->skb);
267
268 spin_lock_irqsave(&priv->tx_lock, flags);
269 ++priv->tx_tail;
1b524963
MT
270 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
271 netif_queue_stopped(dev) &&
272 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
2439a6e6
RD
273 netif_wake_queue(dev);
274 spin_unlock_irqrestore(&priv->tx_lock, flags);
275
276 if (wc->status != IB_WC_SUCCESS &&
277 wc->status != IB_WC_WR_FLUSH_ERR)
278 ipoib_warn(priv, "failed send event "
279 "(status=%d, wrid=%d vend_err %x)\n",
280 wc->status, wr_id, wc->vendor_err);
281}
282
bea3348e 283int ipoib_poll(struct napi_struct *napi, int budget)
2439a6e6 284{
bea3348e
SH
285 struct ipoib_dev_priv *priv = container_of(napi, struct ipoib_dev_priv, napi);
286 struct net_device *dev = priv->dev;
8d1cc86a
RD
287 int done;
288 int t;
8d1cc86a
RD
289 int n, i;
290
291 done = 0;
8d1cc86a 292
bea3348e
SH
293poll_more:
294 while (done < budget) {
295 int max = (budget - done);
296
8d1cc86a
RD
297 t = min(IPOIB_NUM_WC, max);
298 n = ib_poll_cq(priv->cq, t, priv->ibwc);
299
bea3348e 300 for (i = 0; i < n; i++) {
8d1cc86a
RD
301 struct ib_wc *wc = priv->ibwc + i;
302
1b524963 303 if (wc->wr_id & IPOIB_OP_RECV) {
8d1cc86a 304 ++done;
1b524963
MT
305 if (wc->wr_id & IPOIB_OP_CM)
306 ipoib_cm_handle_rx_wc(dev, wc);
307 else
308 ipoib_ib_handle_rx_wc(dev, wc);
309 } else {
310 if (wc->wr_id & IPOIB_OP_CM)
311 ipoib_cm_handle_tx_wc(dev, wc);
312 else
313 ipoib_ib_handle_tx_wc(dev, wc);
314 }
8d1cc86a
RD
315 }
316
bea3348e 317 if (n != t)
8d1cc86a 318 break;
8d1cc86a
RD
319 }
320
bea3348e
SH
321 if (done < budget) {
322 netif_rx_complete(dev, napi);
8d1cc86a
RD
323 if (unlikely(ib_req_notify_cq(priv->cq,
324 IB_CQ_NEXT_COMP |
325 IB_CQ_REPORT_MISSED_EVENTS)) &&
bea3348e
SH
326 netif_rx_reschedule(dev, napi))
327 goto poll_more;
8d1cc86a
RD
328 }
329
bea3348e 330 return done;
1da177e4
LT
331}
332
333void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
334{
bea3348e
SH
335 struct net_device *dev = dev_ptr;
336 struct ipoib_dev_priv *priv = netdev_priv(dev);
337
338 netif_rx_schedule(dev, &priv->napi);
1da177e4
LT
339}
340
341static inline int post_send(struct ipoib_dev_priv *priv,
342 unsigned int wr_id,
343 struct ib_ah *address, u32 qpn,
37ccf9df 344 u64 addr, int len)
1da177e4
LT
345{
346 struct ib_send_wr *bad_wr;
347
2337f809
RD
348 priv->tx_sge.addr = addr;
349 priv->tx_sge.length = len;
1da177e4 350
2337f809 351 priv->tx_wr.wr_id = wr_id;
1da177e4 352 priv->tx_wr.wr.ud.remote_qpn = qpn;
2337f809 353 priv->tx_wr.wr.ud.ah = address;
1da177e4
LT
354
355 return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
356}
357
358void ipoib_send(struct net_device *dev, struct sk_buff *skb,
359 struct ipoib_ah *address, u32 qpn)
360{
361 struct ipoib_dev_priv *priv = netdev_priv(dev);
1993d683 362 struct ipoib_tx_buf *tx_req;
37ccf9df 363 u64 addr;
1da177e4 364
77d8e1ef 365 if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
1da177e4 366 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
77d8e1ef 367 skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
de903512
RD
368 ++dev->stats.tx_dropped;
369 ++dev->stats.tx_errors;
839fcaba 370 ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
1da177e4
LT
371 return;
372 }
373
374 ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
375 skb->len, address, qpn);
376
377 /*
378 * We put the skb into the tx_ring _before_ we call post_send()
379 * because it's entirely possible that the completion handler will
380 * run before we execute anything after the post_send(). That
381 * means we have to make sure everything is properly recorded and
382 * our state is consistent before we call post_send().
383 */
0f485251 384 tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
1da177e4 385 tx_req->skb = skb;
37ccf9df
RC
386 addr = ib_dma_map_single(priv->ca, skb->data, skb->len,
387 DMA_TO_DEVICE);
388 if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
de903512 389 ++dev->stats.tx_errors;
73fbe8be
RD
390 dev_kfree_skb_any(skb);
391 return;
392 }
37ccf9df 393 tx_req->mapping = addr;
1da177e4 394
0f485251 395 if (unlikely(post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
1da177e4
LT
396 address->ah, qpn, addr, skb->len))) {
397 ipoib_warn(priv, "post_send failed\n");
de903512 398 ++dev->stats.tx_errors;
37ccf9df 399 ib_dma_unmap_single(priv->ca, addr, skb->len, DMA_TO_DEVICE);
1da177e4
LT
400 dev_kfree_skb_any(skb);
401 } else {
402 dev->trans_start = jiffies;
403
404 address->last_send = priv->tx_head;
405 ++priv->tx_head;
406
1b524963 407 if (++priv->tx_outstanding == ipoib_sendq_size) {
1da177e4
LT
408 ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
409 netif_stop_queue(dev);
410 }
411 }
412}
413
414static void __ipoib_reap_ah(struct net_device *dev)
415{
416 struct ipoib_dev_priv *priv = netdev_priv(dev);
417 struct ipoib_ah *ah, *tah;
418 LIST_HEAD(remove_list);
419
31c02e21
RD
420 spin_lock_irq(&priv->tx_lock);
421 spin_lock(&priv->lock);
1da177e4 422 list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
2181858b 423 if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
1da177e4 424 list_del(&ah->list);
31c02e21
RD
425 ib_destroy_ah(ah->ah);
426 kfree(ah);
1da177e4 427 }
31c02e21
RD
428 spin_unlock(&priv->lock);
429 spin_unlock_irq(&priv->tx_lock);
1da177e4
LT
430}
431
c4028958 432void ipoib_reap_ah(struct work_struct *work)
1da177e4 433{
c4028958
DH
434 struct ipoib_dev_priv *priv =
435 container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
436 struct net_device *dev = priv->dev;
1da177e4
LT
437
438 __ipoib_reap_ah(dev);
439
440 if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
69fc507a
AB
441 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
442 round_jiffies_relative(HZ));
1da177e4
LT
443}
444
445int ipoib_ib_dev_open(struct net_device *dev)
446{
447 struct ipoib_dev_priv *priv = netdev_priv(dev);
448 int ret;
449
26bbf13c
YE
450 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {
451 ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);
452 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
453 return -1;
454 }
455 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
456
5b6810e0 457 ret = ipoib_init_qp(dev);
1da177e4 458 if (ret) {
5b6810e0 459 ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
1da177e4
LT
460 return -1;
461 }
462
463 ret = ipoib_ib_post_receives(dev);
464 if (ret) {
465 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
26bbf13c 466 ipoib_ib_dev_stop(dev, 1);
1da177e4
LT
467 return -1;
468 }
469
839fcaba
MT
470 ret = ipoib_cm_dev_open(dev);
471 if (ret) {
24bd1e4e 472 ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
26bbf13c 473 ipoib_ib_dev_stop(dev, 1);
839fcaba
MT
474 return -1;
475 }
476
1da177e4 477 clear_bit(IPOIB_STOP_REAPER, &priv->flags);
69fc507a
AB
478 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
479 round_jiffies_relative(HZ));
1da177e4 480
7a343d4c
LA
481 set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
482
1da177e4
LT
483 return 0;
484}
485
7a343d4c
LA
486static void ipoib_pkey_dev_check_presence(struct net_device *dev)
487{
488 struct ipoib_dev_priv *priv = netdev_priv(dev);
489 u16 pkey_index = 0;
490
491 if (ib_find_cached_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
492 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
493 else
494 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
495}
496
1da177e4
LT
497int ipoib_ib_dev_up(struct net_device *dev)
498{
499 struct ipoib_dev_priv *priv = netdev_priv(dev);
500
7a343d4c
LA
501 ipoib_pkey_dev_check_presence(dev);
502
503 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
504 ipoib_dbg(priv, "PKEY is not assigned.\n");
505 return 0;
506 }
507
1da177e4
LT
508 set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
509
510 return ipoib_mcast_start_thread(dev);
511}
512
0b3ea082 513int ipoib_ib_dev_down(struct net_device *dev, int flush)
1da177e4
LT
514{
515 struct ipoib_dev_priv *priv = netdev_priv(dev);
516
517 ipoib_dbg(priv, "downing ib_dev\n");
518
519 clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
520 netif_carrier_off(dev);
521
522 /* Shutdown the P_Key thread if still active */
523 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
95ed644f 524 mutex_lock(&pkey_mutex);
1da177e4 525 set_bit(IPOIB_PKEY_STOP, &priv->flags);
26bbf13c 526 cancel_delayed_work(&priv->pkey_poll_task);
95ed644f 527 mutex_unlock(&pkey_mutex);
0b3ea082
JM
528 if (flush)
529 flush_workqueue(ipoib_workqueue);
1da177e4
LT
530 }
531
0b3ea082 532 ipoib_mcast_stop_thread(dev, flush);
1da177e4
LT
533 ipoib_mcast_dev_flush(dev);
534
1da177e4
LT
535 ipoib_flush_paths(dev);
536
537 return 0;
538}
539
540static int recvs_pending(struct net_device *dev)
541{
542 struct ipoib_dev_priv *priv = netdev_priv(dev);
543 int pending = 0;
544 int i;
545
0f485251 546 for (i = 0; i < ipoib_recvq_size; ++i)
1da177e4
LT
547 if (priv->rx_ring[i].skb)
548 ++pending;
549
550 return pending;
551}
552
2dfbfc37
MT
553void ipoib_drain_cq(struct net_device *dev)
554{
555 struct ipoib_dev_priv *priv = netdev_priv(dev);
556 int i, n;
557 do {
558 n = ib_poll_cq(priv->cq, IPOIB_NUM_WC, priv->ibwc);
559 for (i = 0; i < n; ++i) {
ce423ef5
RD
560 /*
561 * Convert any successful completions to flush
562 * errors to avoid passing packets up the
563 * stack after bringing the device down.
564 */
565 if (priv->ibwc[i].status == IB_WC_SUCCESS)
566 priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
567
1b524963
MT
568 if (priv->ibwc[i].wr_id & IPOIB_OP_RECV) {
569 if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
570 ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
571 else
572 ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
573 } else {
574 if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
575 ipoib_cm_handle_tx_wc(dev, priv->ibwc + i);
576 else
577 ipoib_ib_handle_tx_wc(dev, priv->ibwc + i);
578 }
2dfbfc37
MT
579 }
580 } while (n == IPOIB_NUM_WC);
581}
582
26bbf13c 583int ipoib_ib_dev_stop(struct net_device *dev, int flush)
1da177e4
LT
584{
585 struct ipoib_dev_priv *priv = netdev_priv(dev);
586 struct ib_qp_attr qp_attr;
1da177e4 587 unsigned long begin;
1993d683 588 struct ipoib_tx_buf *tx_req;
2dfbfc37 589 int i;
1da177e4 590
7a343d4c
LA
591 clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
592
839fcaba
MT
593 ipoib_cm_dev_stop(dev);
594
3bc12e75
RD
595 /*
596 * Move our QP to the error state and then reinitialize in
597 * when all work requests have completed or have been flushed.
598 */
1da177e4 599 qp_attr.qp_state = IB_QPS_ERR;
3bc12e75 600 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
1da177e4
LT
601 ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
602
603 /* Wait for all sends and receives to complete */
604 begin = jiffies;
605
606 while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
607 if (time_after(jiffies, begin + 5 * HZ)) {
608 ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
609 priv->tx_head - priv->tx_tail, recvs_pending(dev));
610
611 /*
612 * assume the HW is wedged and just free up
613 * all our pending work requests.
614 */
2181858b 615 while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
1da177e4 616 tx_req = &priv->tx_ring[priv->tx_tail &
0f485251 617 (ipoib_sendq_size - 1)];
37ccf9df
RC
618 ib_dma_unmap_single(priv->ca,
619 tx_req->mapping,
620 tx_req->skb->len,
621 DMA_TO_DEVICE);
1da177e4
LT
622 dev_kfree_skb_any(tx_req->skb);
623 ++priv->tx_tail;
1b524963 624 --priv->tx_outstanding;
1da177e4
LT
625 }
626
37ccf9df
RC
627 for (i = 0; i < ipoib_recvq_size; ++i) {
628 struct ipoib_rx_buf *rx_req;
629
630 rx_req = &priv->rx_ring[i];
631 if (!rx_req->skb)
632 continue;
633 ib_dma_unmap_single(priv->ca,
634 rx_req->mapping,
635 IPOIB_BUF_SIZE,
636 DMA_FROM_DEVICE);
637 dev_kfree_skb_any(rx_req->skb);
638 rx_req->skb = NULL;
639 }
1da177e4
LT
640
641 goto timeout;
642 }
643
2dfbfc37 644 ipoib_drain_cq(dev);
8d1cc86a 645
1da177e4
LT
646 msleep(1);
647 }
648
649 ipoib_dbg(priv, "All sends and receives done.\n");
650
651timeout:
652 qp_attr.qp_state = IB_QPS_RESET;
3bc12e75 653 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
1da177e4
LT
654 ipoib_warn(priv, "Failed to modify QP to RESET state\n");
655
656 /* Wait for all AHs to be reaped */
657 set_bit(IPOIB_STOP_REAPER, &priv->flags);
658 cancel_delayed_work(&priv->ah_reap_task);
26bbf13c
YE
659 if (flush)
660 flush_workqueue(ipoib_workqueue);
1da177e4
LT
661
662 begin = jiffies;
663
664 while (!list_empty(&priv->dead_ahs)) {
665 __ipoib_reap_ah(dev);
666
667 if (time_after(jiffies, begin + HZ)) {
668 ipoib_warn(priv, "timing out; will leak address handles\n");
669 break;
670 }
671
672 msleep(1);
673 }
674
8d1cc86a
RD
675 ib_req_notify_cq(priv->cq, IB_CQ_NEXT_COMP);
676
1da177e4
LT
677 return 0;
678}
679
680int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
681{
682 struct ipoib_dev_priv *priv = netdev_priv(dev);
683
684 priv->ca = ca;
685 priv->port = port;
686 priv->qp = NULL;
687
688 if (ipoib_transport_dev_init(dev, ca)) {
689 printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
690 return -ENODEV;
691 }
692
693 if (dev->flags & IFF_UP) {
694 if (ipoib_ib_dev_open(dev)) {
695 ipoib_transport_dev_cleanup(dev);
696 return -ENODEV;
697 }
698 }
699
700 return 0;
701}
702
26bbf13c 703static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv, int pkey_event)
1da177e4 704{
26bbf13c 705 struct ipoib_dev_priv *cpriv;
c4028958 706 struct net_device *dev = priv->dev;
26bbf13c
YE
707 u16 new_index;
708
709 mutex_lock(&priv->vlan_mutex);
1da177e4 710
26bbf13c
YE
711 /*
712 * Flush any child interfaces too -- they might be up even if
713 * the parent is down.
714 */
715 list_for_each_entry(cpriv, &priv->child_intfs, list)
716 __ipoib_ib_dev_flush(cpriv, pkey_event);
717
718 mutex_unlock(&priv->vlan_mutex);
719
720 if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
7a343d4c
LA
721 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
722 return;
723 }
724
725 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
726 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
1da177e4 727 return;
7a343d4c 728 }
1da177e4 729
26bbf13c
YE
730 if (pkey_event) {
731 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
732 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
733 ipoib_ib_dev_down(dev, 0);
734 ipoib_pkey_dev_delay_open(dev);
735 return;
736 }
737 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
738
739 /* restart QP only if P_Key index is changed */
740 if (new_index == priv->pkey_index) {
741 ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
742 return;
743 }
744 priv->pkey_index = new_index;
745 }
746
1da177e4
LT
747 ipoib_dbg(priv, "flushing\n");
748
0b3ea082 749 ipoib_ib_dev_down(dev, 0);
1da177e4 750
26bbf13c
YE
751 if (pkey_event) {
752 ipoib_ib_dev_stop(dev, 0);
753 ipoib_ib_dev_open(dev);
754 }
755
1da177e4
LT
756 /*
757 * The device could have been brought down between the start and when
758 * we get here, don't bring it back up if it's not configured up
759 */
5ccd0255 760 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
1da177e4 761 ipoib_ib_dev_up(dev);
c4028958 762 ipoib_mcast_restart_task(&priv->restart_task);
5ccd0255 763 }
26bbf13c 764}
1da177e4 765
26bbf13c
YE
766void ipoib_ib_dev_flush(struct work_struct *work)
767{
768 struct ipoib_dev_priv *priv =
769 container_of(work, struct ipoib_dev_priv, flush_task);
4f71055a 770
26bbf13c
YE
771 ipoib_dbg(priv, "Flushing %s\n", priv->dev->name);
772 __ipoib_ib_dev_flush(priv, 0);
773}
4f71055a 774
26bbf13c
YE
775void ipoib_pkey_event(struct work_struct *work)
776{
777 struct ipoib_dev_priv *priv =
778 container_of(work, struct ipoib_dev_priv, pkey_event_task);
779
780 ipoib_dbg(priv, "Flushing %s and restarting its QP\n", priv->dev->name);
781 __ipoib_ib_dev_flush(priv, 1);
1da177e4
LT
782}
783
784void ipoib_ib_dev_cleanup(struct net_device *dev)
785{
786 struct ipoib_dev_priv *priv = netdev_priv(dev);
787
788 ipoib_dbg(priv, "cleaning up ib_dev\n");
789
8d2cae06 790 ipoib_mcast_stop_thread(dev, 1);
988bd503 791 ipoib_mcast_dev_flush(dev);
1da177e4
LT
792
793 ipoib_transport_dev_cleanup(dev);
794}
795
796/*
797 * Delayed P_Key Assigment Interim Support
798 *
799 * The following is initial implementation of delayed P_Key assigment
800 * mechanism. It is using the same approach implemented for the multicast
801 * group join. The single goal of this implementation is to quickly address
802 * Bug #2507. This implementation will probably be removed when the P_Key
803 * change async notification is available.
804 */
1da177e4 805
c4028958 806void ipoib_pkey_poll(struct work_struct *work)
1da177e4 807{
c4028958 808 struct ipoib_dev_priv *priv =
26bbf13c 809 container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
c4028958 810 struct net_device *dev = priv->dev;
1da177e4
LT
811
812 ipoib_pkey_dev_check_presence(dev);
813
814 if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
815 ipoib_open(dev);
816 else {
95ed644f 817 mutex_lock(&pkey_mutex);
1da177e4
LT
818 if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
819 queue_delayed_work(ipoib_workqueue,
26bbf13c 820 &priv->pkey_poll_task,
1da177e4 821 HZ);
95ed644f 822 mutex_unlock(&pkey_mutex);
1da177e4
LT
823 }
824}
825
826int ipoib_pkey_dev_delay_open(struct net_device *dev)
827{
828 struct ipoib_dev_priv *priv = netdev_priv(dev);
829
830 /* Look for the interface pkey value in the IB Port P_Key table and */
831 /* set the interface pkey assigment flag */
832 ipoib_pkey_dev_check_presence(dev);
833
834 /* P_Key value not assigned yet - start polling */
835 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
95ed644f 836 mutex_lock(&pkey_mutex);
1da177e4
LT
837 clear_bit(IPOIB_PKEY_STOP, &priv->flags);
838 queue_delayed_work(ipoib_workqueue,
26bbf13c 839 &priv->pkey_poll_task,
1da177e4 840 HZ);
95ed644f 841 mutex_unlock(&pkey_mutex);
1da177e4
LT
842 return 1;
843 }
844
845 return 0;
846}
This page took 0.348555 seconds and 5 git commands to generate.