IPoIB/cm: Drain cq in ipoib_cm_dev_stop()
[linux-2.6/verdex.git] / drivers / infiniband / ulp / ipoib / ipoib_ib.c
blob8404f05b2b6eefd04a018b5da67698b65cfb09fd
1 /*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
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.
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:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer.
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.
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.
35 * $Id: ipoib_ib.c 1386 2004-12-27 16:23:17Z roland $
38 #include <linux/delay.h>
39 #include <linux/dma-mapping.h>
41 #include <rdma/ib_cache.h>
43 #include "ipoib.h"
45 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
46 static int data_debug_level;
48 module_param(data_debug_level, int, 0644);
49 MODULE_PARM_DESC(data_debug_level,
50 "Enable data path debug tracing if > 0");
51 #endif
53 static DEFINE_MUTEX(pkey_mutex);
55 struct ipoib_ah *ipoib_create_ah(struct net_device *dev,
56 struct ib_pd *pd, struct ib_ah_attr *attr)
58 struct ipoib_ah *ah;
60 ah = kmalloc(sizeof *ah, GFP_KERNEL);
61 if (!ah)
62 return NULL;
64 ah->dev = dev;
65 ah->last_send = 0;
66 kref_init(&ah->ref);
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);
75 return ah;
78 void ipoib_free_ah(struct kref *kref)
80 struct ipoib_ah *ah = container_of(kref, struct ipoib_ah, ref);
81 struct ipoib_dev_priv *priv = netdev_priv(ah->dev);
83 unsigned long flags;
85 spin_lock_irqsave(&priv->lock, flags);
86 list_add_tail(&ah->list, &priv->dead_ahs);
87 spin_unlock_irqrestore(&priv->lock, flags);
90 static int ipoib_ib_post_receive(struct net_device *dev, int id)
92 struct ipoib_dev_priv *priv = netdev_priv(dev);
93 struct ib_sge list;
94 struct ib_recv_wr param;
95 struct ib_recv_wr *bad_wr;
96 int ret;
98 list.addr = priv->rx_ring[id].mapping;
99 list.length = IPOIB_BUF_SIZE;
100 list.lkey = priv->mr->lkey;
102 param.next = NULL;
103 param.wr_id = id | IPOIB_OP_RECV;
104 param.sg_list = &list;
105 param.num_sge = 1;
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);
110 ib_dma_unmap_single(priv->ca, priv->rx_ring[id].mapping,
111 IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
112 dev_kfree_skb_any(priv->rx_ring[id].skb);
113 priv->rx_ring[id].skb = NULL;
116 return ret;
119 static int ipoib_alloc_rx_skb(struct net_device *dev, int id)
121 struct ipoib_dev_priv *priv = netdev_priv(dev);
122 struct sk_buff *skb;
123 u64 addr;
125 skb = dev_alloc_skb(IPOIB_BUF_SIZE + 4);
126 if (!skb)
127 return -ENOMEM;
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.
134 skb_reserve(skb, 4);
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))) {
139 dev_kfree_skb_any(skb);
140 return -EIO;
143 priv->rx_ring[id].skb = skb;
144 priv->rx_ring[id].mapping = addr;
146 return 0;
149 static int ipoib_ib_post_receives(struct net_device *dev)
151 struct ipoib_dev_priv *priv = netdev_priv(dev);
152 int i;
154 for (i = 0; i < ipoib_recvq_size; ++i) {
155 if (ipoib_alloc_rx_skb(dev, i)) {
156 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
157 return -ENOMEM;
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;
165 return 0;
168 static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
170 struct ipoib_dev_priv *priv = netdev_priv(dev);
171 unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
172 struct sk_buff *skb;
173 u64 addr;
175 ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
176 wr_id, wc->status);
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;
184 skb = priv->rx_ring[wr_id].skb;
185 addr = priv->rx_ring[wr_id].mapping;
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);
192 ib_dma_unmap_single(priv->ca, addr,
193 IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
194 dev_kfree_skb_any(skb);
195 priv->rx_ring[wr_id].skb = NULL;
196 return;
200 * If we can't allocate a new RX buffer, dump
201 * this packet and reuse the old buffer.
203 if (unlikely(ipoib_alloc_rx_skb(dev, wr_id))) {
204 ++priv->stats.rx_dropped;
205 goto repost;
208 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
209 wc->byte_len, wc->slid);
211 ib_dma_unmap_single(priv->ca, addr, IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
213 skb_put(skb, wc->byte_len);
214 skb_pull(skb, IB_GRH_BYTES);
216 if (wc->slid != priv->local_lid ||
217 wc->src_qp != priv->qp->qp_num) {
218 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
219 skb_reset_mac_header(skb);
220 skb_pull(skb, IPOIB_ENCAP_LEN);
222 dev->last_rx = jiffies;
223 ++priv->stats.rx_packets;
224 priv->stats.rx_bytes += skb->len;
226 skb->dev = dev;
227 /* XXX get correct PACKET_ type here */
228 skb->pkt_type = PACKET_HOST;
229 netif_receive_skb(skb);
230 } else {
231 ipoib_dbg_data(priv, "dropping loopback packet\n");
232 dev_kfree_skb_any(skb);
235 repost:
236 if (unlikely(ipoib_ib_post_receive(dev, wr_id)))
237 ipoib_warn(priv, "ipoib_ib_post_receive failed "
238 "for buf %d\n", wr_id);
241 static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
243 struct ipoib_dev_priv *priv = netdev_priv(dev);
244 unsigned int wr_id = wc->wr_id;
245 struct ipoib_tx_buf *tx_req;
246 unsigned long flags;
248 ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
249 wr_id, wc->status);
251 if (unlikely(wr_id >= ipoib_sendq_size)) {
252 ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
253 wr_id, ipoib_sendq_size);
254 return;
257 tx_req = &priv->tx_ring[wr_id];
259 ib_dma_unmap_single(priv->ca, tx_req->mapping,
260 tx_req->skb->len, DMA_TO_DEVICE);
262 ++priv->stats.tx_packets;
263 priv->stats.tx_bytes += tx_req->skb->len;
265 dev_kfree_skb_any(tx_req->skb);
267 spin_lock_irqsave(&priv->tx_lock, flags);
268 ++priv->tx_tail;
269 if (unlikely(test_bit(IPOIB_FLAG_NETIF_STOPPED, &priv->flags)) &&
270 priv->tx_head - priv->tx_tail <= ipoib_sendq_size >> 1) {
271 clear_bit(IPOIB_FLAG_NETIF_STOPPED, &priv->flags);
272 netif_wake_queue(dev);
274 spin_unlock_irqrestore(&priv->tx_lock, flags);
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);
283 int ipoib_poll(struct net_device *dev, int *budget)
285 struct ipoib_dev_priv *priv = netdev_priv(dev);
286 int max = min(*budget, dev->quota);
287 int done;
288 int t;
289 int empty;
290 int n, i;
292 done = 0;
293 empty = 0;
295 while (max) {
296 t = min(IPOIB_NUM_WC, max);
297 n = ib_poll_cq(priv->cq, t, priv->ibwc);
299 for (i = 0; i < n; ++i) {
300 struct ib_wc *wc = priv->ibwc + i;
302 if (wc->wr_id & IPOIB_CM_OP_SRQ) {
303 ++done;
304 --max;
305 ipoib_cm_handle_rx_wc(dev, wc);
306 } else if (wc->wr_id & IPOIB_OP_RECV) {
307 ++done;
308 --max;
309 ipoib_ib_handle_rx_wc(dev, wc);
310 } else
311 ipoib_ib_handle_tx_wc(dev, wc);
314 if (n != t) {
315 empty = 1;
316 break;
320 dev->quota -= done;
321 *budget -= done;
323 if (empty) {
324 netif_rx_complete(dev);
325 if (unlikely(ib_req_notify_cq(priv->cq,
326 IB_CQ_NEXT_COMP |
327 IB_CQ_REPORT_MISSED_EVENTS)) &&
328 netif_rx_reschedule(dev, 0))
329 return 1;
331 return 0;
334 return 1;
337 void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
339 netif_rx_schedule(dev_ptr);
342 static inline int post_send(struct ipoib_dev_priv *priv,
343 unsigned int wr_id,
344 struct ib_ah *address, u32 qpn,
345 u64 addr, int len)
347 struct ib_send_wr *bad_wr;
349 priv->tx_sge.addr = addr;
350 priv->tx_sge.length = len;
352 priv->tx_wr.wr_id = wr_id;
353 priv->tx_wr.wr.ud.remote_qpn = qpn;
354 priv->tx_wr.wr.ud.ah = address;
356 return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
359 void ipoib_send(struct net_device *dev, struct sk_buff *skb,
360 struct ipoib_ah *address, u32 qpn)
362 struct ipoib_dev_priv *priv = netdev_priv(dev);
363 struct ipoib_tx_buf *tx_req;
364 u64 addr;
366 if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
367 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
368 skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
369 ++priv->stats.tx_dropped;
370 ++priv->stats.tx_errors;
371 ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
372 return;
375 ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
376 skb->len, address, qpn);
379 * We put the skb into the tx_ring _before_ we call post_send()
380 * because it's entirely possible that the completion handler will
381 * run before we execute anything after the post_send(). That
382 * means we have to make sure everything is properly recorded and
383 * our state is consistent before we call post_send().
385 tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
386 tx_req->skb = skb;
387 addr = ib_dma_map_single(priv->ca, skb->data, skb->len,
388 DMA_TO_DEVICE);
389 if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
390 ++priv->stats.tx_errors;
391 dev_kfree_skb_any(skb);
392 return;
394 tx_req->mapping = addr;
396 if (unlikely(post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
397 address->ah, qpn, addr, skb->len))) {
398 ipoib_warn(priv, "post_send failed\n");
399 ++priv->stats.tx_errors;
400 ib_dma_unmap_single(priv->ca, addr, skb->len, DMA_TO_DEVICE);
401 dev_kfree_skb_any(skb);
402 } else {
403 dev->trans_start = jiffies;
405 address->last_send = priv->tx_head;
406 ++priv->tx_head;
408 if (priv->tx_head - priv->tx_tail == ipoib_sendq_size) {
409 ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
410 netif_stop_queue(dev);
411 set_bit(IPOIB_FLAG_NETIF_STOPPED, &priv->flags);
416 static void __ipoib_reap_ah(struct net_device *dev)
418 struct ipoib_dev_priv *priv = netdev_priv(dev);
419 struct ipoib_ah *ah, *tah;
420 LIST_HEAD(remove_list);
422 spin_lock_irq(&priv->tx_lock);
423 spin_lock(&priv->lock);
424 list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
425 if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
426 list_del(&ah->list);
427 ib_destroy_ah(ah->ah);
428 kfree(ah);
430 spin_unlock(&priv->lock);
431 spin_unlock_irq(&priv->tx_lock);
434 void ipoib_reap_ah(struct work_struct *work)
436 struct ipoib_dev_priv *priv =
437 container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
438 struct net_device *dev = priv->dev;
440 __ipoib_reap_ah(dev);
442 if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
443 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
446 int ipoib_ib_dev_open(struct net_device *dev)
448 struct ipoib_dev_priv *priv = netdev_priv(dev);
449 int ret;
451 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {
452 ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);
453 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
454 return -1;
456 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
458 ret = ipoib_init_qp(dev);
459 if (ret) {
460 ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
461 return -1;
464 ret = ipoib_ib_post_receives(dev);
465 if (ret) {
466 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
467 ipoib_ib_dev_stop(dev, 1);
468 return -1;
471 ret = ipoib_cm_dev_open(dev);
472 if (ret) {
473 ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
474 ipoib_ib_dev_stop(dev, 1);
475 return -1;
478 clear_bit(IPOIB_STOP_REAPER, &priv->flags);
479 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
481 set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
483 return 0;
486 static void ipoib_pkey_dev_check_presence(struct net_device *dev)
488 struct ipoib_dev_priv *priv = netdev_priv(dev);
489 u16 pkey_index = 0;
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);
497 int ipoib_ib_dev_up(struct net_device *dev)
499 struct ipoib_dev_priv *priv = netdev_priv(dev);
501 ipoib_pkey_dev_check_presence(dev);
503 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
504 ipoib_dbg(priv, "PKEY is not assigned.\n");
505 return 0;
508 set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
510 return ipoib_mcast_start_thread(dev);
513 int ipoib_ib_dev_down(struct net_device *dev, int flush)
515 struct ipoib_dev_priv *priv = netdev_priv(dev);
517 ipoib_dbg(priv, "downing ib_dev\n");
519 clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
520 netif_carrier_off(dev);
522 /* Shutdown the P_Key thread if still active */
523 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
524 mutex_lock(&pkey_mutex);
525 set_bit(IPOIB_PKEY_STOP, &priv->flags);
526 cancel_delayed_work(&priv->pkey_poll_task);
527 mutex_unlock(&pkey_mutex);
528 if (flush)
529 flush_workqueue(ipoib_workqueue);
532 ipoib_mcast_stop_thread(dev, flush);
533 ipoib_mcast_dev_flush(dev);
535 ipoib_flush_paths(dev);
537 return 0;
540 static int recvs_pending(struct net_device *dev)
542 struct ipoib_dev_priv *priv = netdev_priv(dev);
543 int pending = 0;
544 int i;
546 for (i = 0; i < ipoib_recvq_size; ++i)
547 if (priv->rx_ring[i].skb)
548 ++pending;
550 return pending;
553 void ipoib_drain_cq(struct net_device *dev)
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) {
560 if (priv->ibwc[i].wr_id & IPOIB_CM_OP_SRQ)
561 ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
562 else if (priv->ibwc[i].wr_id & IPOIB_OP_RECV)
563 ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
564 else
565 ipoib_ib_handle_tx_wc(dev, priv->ibwc + i);
567 } while (n == IPOIB_NUM_WC);
570 int ipoib_ib_dev_stop(struct net_device *dev, int flush)
572 struct ipoib_dev_priv *priv = netdev_priv(dev);
573 struct ib_qp_attr qp_attr;
574 unsigned long begin;
575 struct ipoib_tx_buf *tx_req;
576 int i;
578 clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
579 netif_poll_disable(dev);
581 ipoib_cm_dev_stop(dev);
584 * Move our QP to the error state and then reinitialize in
585 * when all work requests have completed or have been flushed.
587 qp_attr.qp_state = IB_QPS_ERR;
588 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
589 ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
591 /* Wait for all sends and receives to complete */
592 begin = jiffies;
594 while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
595 if (time_after(jiffies, begin + 5 * HZ)) {
596 ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
597 priv->tx_head - priv->tx_tail, recvs_pending(dev));
600 * assume the HW is wedged and just free up
601 * all our pending work requests.
603 while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
604 tx_req = &priv->tx_ring[priv->tx_tail &
605 (ipoib_sendq_size - 1)];
606 ib_dma_unmap_single(priv->ca,
607 tx_req->mapping,
608 tx_req->skb->len,
609 DMA_TO_DEVICE);
610 dev_kfree_skb_any(tx_req->skb);
611 ++priv->tx_tail;
614 for (i = 0; i < ipoib_recvq_size; ++i) {
615 struct ipoib_rx_buf *rx_req;
617 rx_req = &priv->rx_ring[i];
618 if (!rx_req->skb)
619 continue;
620 ib_dma_unmap_single(priv->ca,
621 rx_req->mapping,
622 IPOIB_BUF_SIZE,
623 DMA_FROM_DEVICE);
624 dev_kfree_skb_any(rx_req->skb);
625 rx_req->skb = NULL;
628 goto timeout;
631 ipoib_drain_cq(dev);
633 msleep(1);
636 ipoib_dbg(priv, "All sends and receives done.\n");
638 timeout:
639 qp_attr.qp_state = IB_QPS_RESET;
640 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
641 ipoib_warn(priv, "Failed to modify QP to RESET state\n");
643 /* Wait for all AHs to be reaped */
644 set_bit(IPOIB_STOP_REAPER, &priv->flags);
645 cancel_delayed_work(&priv->ah_reap_task);
646 if (flush)
647 flush_workqueue(ipoib_workqueue);
649 begin = jiffies;
651 while (!list_empty(&priv->dead_ahs)) {
652 __ipoib_reap_ah(dev);
654 if (time_after(jiffies, begin + HZ)) {
655 ipoib_warn(priv, "timing out; will leak address handles\n");
656 break;
659 msleep(1);
662 netif_poll_enable(dev);
663 ib_req_notify_cq(priv->cq, IB_CQ_NEXT_COMP);
665 return 0;
668 int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
670 struct ipoib_dev_priv *priv = netdev_priv(dev);
672 priv->ca = ca;
673 priv->port = port;
674 priv->qp = NULL;
676 if (ipoib_transport_dev_init(dev, ca)) {
677 printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
678 return -ENODEV;
681 if (dev->flags & IFF_UP) {
682 if (ipoib_ib_dev_open(dev)) {
683 ipoib_transport_dev_cleanup(dev);
684 return -ENODEV;
688 return 0;
691 static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv, int pkey_event)
693 struct ipoib_dev_priv *cpriv;
694 struct net_device *dev = priv->dev;
695 u16 new_index;
697 mutex_lock(&priv->vlan_mutex);
700 * Flush any child interfaces too -- they might be up even if
701 * the parent is down.
703 list_for_each_entry(cpriv, &priv->child_intfs, list)
704 __ipoib_ib_dev_flush(cpriv, pkey_event);
706 mutex_unlock(&priv->vlan_mutex);
708 if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
709 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
710 return;
713 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
714 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
715 return;
718 if (pkey_event) {
719 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
720 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
721 ipoib_ib_dev_down(dev, 0);
722 ipoib_pkey_dev_delay_open(dev);
723 return;
725 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
727 /* restart QP only if P_Key index is changed */
728 if (new_index == priv->pkey_index) {
729 ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
730 return;
732 priv->pkey_index = new_index;
735 ipoib_dbg(priv, "flushing\n");
737 ipoib_ib_dev_down(dev, 0);
739 if (pkey_event) {
740 ipoib_ib_dev_stop(dev, 0);
741 ipoib_ib_dev_open(dev);
745 * The device could have been brought down between the start and when
746 * we get here, don't bring it back up if it's not configured up
748 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
749 ipoib_ib_dev_up(dev);
750 ipoib_mcast_restart_task(&priv->restart_task);
754 void ipoib_ib_dev_flush(struct work_struct *work)
756 struct ipoib_dev_priv *priv =
757 container_of(work, struct ipoib_dev_priv, flush_task);
759 ipoib_dbg(priv, "Flushing %s\n", priv->dev->name);
760 __ipoib_ib_dev_flush(priv, 0);
763 void ipoib_pkey_event(struct work_struct *work)
765 struct ipoib_dev_priv *priv =
766 container_of(work, struct ipoib_dev_priv, pkey_event_task);
768 ipoib_dbg(priv, "Flushing %s and restarting its QP\n", priv->dev->name);
769 __ipoib_ib_dev_flush(priv, 1);
772 void ipoib_ib_dev_cleanup(struct net_device *dev)
774 struct ipoib_dev_priv *priv = netdev_priv(dev);
776 ipoib_dbg(priv, "cleaning up ib_dev\n");
778 ipoib_mcast_stop_thread(dev, 1);
779 ipoib_mcast_dev_flush(dev);
781 ipoib_transport_dev_cleanup(dev);
785 * Delayed P_Key Assigment Interim Support
787 * The following is initial implementation of delayed P_Key assigment
788 * mechanism. It is using the same approach implemented for the multicast
789 * group join. The single goal of this implementation is to quickly address
790 * Bug #2507. This implementation will probably be removed when the P_Key
791 * change async notification is available.
794 void ipoib_pkey_poll(struct work_struct *work)
796 struct ipoib_dev_priv *priv =
797 container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
798 struct net_device *dev = priv->dev;
800 ipoib_pkey_dev_check_presence(dev);
802 if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
803 ipoib_open(dev);
804 else {
805 mutex_lock(&pkey_mutex);
806 if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
807 queue_delayed_work(ipoib_workqueue,
808 &priv->pkey_poll_task,
809 HZ);
810 mutex_unlock(&pkey_mutex);
814 int ipoib_pkey_dev_delay_open(struct net_device *dev)
816 struct ipoib_dev_priv *priv = netdev_priv(dev);
818 /* Look for the interface pkey value in the IB Port P_Key table and */
819 /* set the interface pkey assigment flag */
820 ipoib_pkey_dev_check_presence(dev);
822 /* P_Key value not assigned yet - start polling */
823 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
824 mutex_lock(&pkey_mutex);
825 clear_bit(IPOIB_PKEY_STOP, &priv->flags);
826 queue_delayed_work(ipoib_workqueue,
827 &priv->pkey_poll_task,
828 HZ);
829 mutex_unlock(&pkey_mutex);
830 return 1;
833 return 0;