IPoIB: Use round_jiffies() for ah_reap_task
[pandora-kernel.git] / drivers / infiniband / ulp / ipoib / ipoib_ib.c
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.
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
41 #include <rdma/ib_cache.h>
42
43 #include "ipoib.h"
44
45 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
46 static int data_debug_level;
47
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
52
53 static DEFINE_MUTEX(pkey_mutex);
54
55 struct 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
78 void 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
85         spin_lock_irqsave(&priv->lock, flags);
86         list_add_tail(&ah->list, &priv->dead_ahs);
87         spin_unlock_irqrestore(&priv->lock, flags);
88 }
89
90 static int ipoib_ib_post_receive(struct net_device *dev, int id)
91 {
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;
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);
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;
114         }
115
116         return ret;
117 }
118
119 static int ipoib_alloc_rx_skb(struct net_device *dev, int id)
120 {
121         struct ipoib_dev_priv *priv = netdev_priv(dev);
122         struct sk_buff *skb;
123         u64 addr;
124
125         skb = dev_alloc_skb(IPOIB_BUF_SIZE + 4);
126         if (!skb)
127                 return -ENOMEM;
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
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;
141         }
142
143         priv->rx_ring[id].skb     = skb;
144         priv->rx_ring[id].mapping = addr;
145
146         return 0;
147 }
148
149 static int ipoib_ib_post_receives(struct net_device *dev)
150 {
151         struct ipoib_dev_priv *priv = netdev_priv(dev);
152         int i;
153
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;
158                 }
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
168 static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
169 {
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;
174
175         ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
176                        wr_id, wc->status);
177
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);
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;
197         }
198
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
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))) {
211                 ++dev->stats.rx_dropped;
212                 goto repost;
213         }
214
215         ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
216                        wc->byte_len, wc->slid);
217
218         ib_dma_unmap_single(priv->ca, addr, IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
219
220         skb_put(skb, wc->byte_len);
221         skb_pull(skb, IB_GRH_BYTES);
222
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;
228         ++dev->stats.rx_packets;
229         dev->stats.rx_bytes += skb->len;
230
231         skb->dev = dev;
232         /* XXX get correct PACKET_ type here */
233         skb->pkt_type = PACKET_HOST;
234         netif_receive_skb(skb);
235
236 repost:
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 }
241
242 static 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;
248
249         ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
250                        wr_id, wc->status);
251
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;
256         }
257
258         tx_req = &priv->tx_ring[wr_id];
259
260         ib_dma_unmap_single(priv->ca, tx_req->mapping,
261                             tx_req->skb->len, DMA_TO_DEVICE);
262
263         ++dev->stats.tx_packets;
264         dev->stats.tx_bytes += tx_req->skb->len;
265
266         dev_kfree_skb_any(tx_req->skb);
267
268         spin_lock_irqsave(&priv->tx_lock, flags);
269         ++priv->tx_tail;
270         if (unlikely(test_bit(IPOIB_FLAG_NETIF_STOPPED, &priv->flags)) &&
271             priv->tx_head - priv->tx_tail <= ipoib_sendq_size >> 1) {
272                 clear_bit(IPOIB_FLAG_NETIF_STOPPED, &priv->flags);
273                 netif_wake_queue(dev);
274         }
275         spin_unlock_irqrestore(&priv->tx_lock, flags);
276
277         if (wc->status != IB_WC_SUCCESS &&
278             wc->status != IB_WC_WR_FLUSH_ERR)
279                 ipoib_warn(priv, "failed send event "
280                            "(status=%d, wrid=%d vend_err %x)\n",
281                            wc->status, wr_id, wc->vendor_err);
282 }
283
284 int ipoib_poll(struct napi_struct *napi, int budget)
285 {
286         struct ipoib_dev_priv *priv = container_of(napi, struct ipoib_dev_priv, napi);
287         struct net_device *dev = priv->dev;
288         int done;
289         int t;
290         int n, i;
291
292         done  = 0;
293
294 poll_more:
295         while (done < budget) {
296                 int max = (budget - done);
297
298                 t = min(IPOIB_NUM_WC, max);
299                 n = ib_poll_cq(priv->cq, t, priv->ibwc);
300
301                 for (i = 0; i < n; i++) {
302                         struct ib_wc *wc = priv->ibwc + i;
303
304                         if (wc->wr_id & IPOIB_CM_OP_SRQ) {
305                                 ++done;
306                                 ipoib_cm_handle_rx_wc(dev, wc);
307                         } else if (wc->wr_id & IPOIB_OP_RECV) {
308                                 ++done;
309                                 ipoib_ib_handle_rx_wc(dev, wc);
310                         } else
311                                 ipoib_ib_handle_tx_wc(dev, wc);
312                 }
313
314                 if (n != t)
315                         break;
316         }
317
318         if (done < budget) {
319                 netif_rx_complete(dev, napi);
320                 if (unlikely(ib_req_notify_cq(priv->cq,
321                                               IB_CQ_NEXT_COMP |
322                                               IB_CQ_REPORT_MISSED_EVENTS)) &&
323                     netif_rx_reschedule(dev, napi))
324                         goto poll_more;
325         }
326
327         return done;
328 }
329
330 void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
331 {
332         struct net_device *dev = dev_ptr;
333         struct ipoib_dev_priv *priv = netdev_priv(dev);
334
335         netif_rx_schedule(dev, &priv->napi);
336 }
337
338 static inline int post_send(struct ipoib_dev_priv *priv,
339                             unsigned int wr_id,
340                             struct ib_ah *address, u32 qpn,
341                             u64 addr, int len)
342 {
343         struct ib_send_wr *bad_wr;
344
345         priv->tx_sge.addr             = addr;
346         priv->tx_sge.length           = len;
347
348         priv->tx_wr.wr_id             = wr_id;
349         priv->tx_wr.wr.ud.remote_qpn  = qpn;
350         priv->tx_wr.wr.ud.ah          = address;
351
352         return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
353 }
354
355 void ipoib_send(struct net_device *dev, struct sk_buff *skb,
356                 struct ipoib_ah *address, u32 qpn)
357 {
358         struct ipoib_dev_priv *priv = netdev_priv(dev);
359         struct ipoib_tx_buf *tx_req;
360         u64 addr;
361
362         if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
363                 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
364                            skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
365                 ++dev->stats.tx_dropped;
366                 ++dev->stats.tx_errors;
367                 ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
368                 return;
369         }
370
371         ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
372                        skb->len, address, qpn);
373
374         /*
375          * We put the skb into the tx_ring _before_ we call post_send()
376          * because it's entirely possible that the completion handler will
377          * run before we execute anything after the post_send().  That
378          * means we have to make sure everything is properly recorded and
379          * our state is consistent before we call post_send().
380          */
381         tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
382         tx_req->skb = skb;
383         addr = ib_dma_map_single(priv->ca, skb->data, skb->len,
384                                  DMA_TO_DEVICE);
385         if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
386                 ++dev->stats.tx_errors;
387                 dev_kfree_skb_any(skb);
388                 return;
389         }
390         tx_req->mapping = addr;
391
392         if (unlikely(post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
393                                address->ah, qpn, addr, skb->len))) {
394                 ipoib_warn(priv, "post_send failed\n");
395                 ++dev->stats.tx_errors;
396                 ib_dma_unmap_single(priv->ca, addr, skb->len, DMA_TO_DEVICE);
397                 dev_kfree_skb_any(skb);
398         } else {
399                 dev->trans_start = jiffies;
400
401                 address->last_send = priv->tx_head;
402                 ++priv->tx_head;
403
404                 if (priv->tx_head - priv->tx_tail == ipoib_sendq_size) {
405                         ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
406                         netif_stop_queue(dev);
407                         set_bit(IPOIB_FLAG_NETIF_STOPPED, &priv->flags);
408                 }
409         }
410 }
411
412 static void __ipoib_reap_ah(struct net_device *dev)
413 {
414         struct ipoib_dev_priv *priv = netdev_priv(dev);
415         struct ipoib_ah *ah, *tah;
416         LIST_HEAD(remove_list);
417
418         spin_lock_irq(&priv->tx_lock);
419         spin_lock(&priv->lock);
420         list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
421                 if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
422                         list_del(&ah->list);
423                         ib_destroy_ah(ah->ah);
424                         kfree(ah);
425                 }
426         spin_unlock(&priv->lock);
427         spin_unlock_irq(&priv->tx_lock);
428 }
429
430 void ipoib_reap_ah(struct work_struct *work)
431 {
432         struct ipoib_dev_priv *priv =
433                 container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
434         struct net_device *dev = priv->dev;
435
436         __ipoib_reap_ah(dev);
437
438         if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
439                 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
440                                    round_jiffies_relative(HZ));
441 }
442
443 int ipoib_ib_dev_open(struct net_device *dev)
444 {
445         struct ipoib_dev_priv *priv = netdev_priv(dev);
446         int ret;
447
448         if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {
449                 ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);
450                 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
451                 return -1;
452         }
453         set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
454
455         ret = ipoib_init_qp(dev);
456         if (ret) {
457                 ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
458                 return -1;
459         }
460
461         ret = ipoib_ib_post_receives(dev);
462         if (ret) {
463                 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
464                 ipoib_ib_dev_stop(dev, 1);
465                 return -1;
466         }
467
468         ret = ipoib_cm_dev_open(dev);
469         if (ret) {
470                 ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
471                 ipoib_ib_dev_stop(dev, 1);
472                 return -1;
473         }
474
475         clear_bit(IPOIB_STOP_REAPER, &priv->flags);
476         queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
477                            round_jiffies_relative(HZ));
478
479         set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
480
481         return 0;
482 }
483
484 static void ipoib_pkey_dev_check_presence(struct net_device *dev)
485 {
486         struct ipoib_dev_priv *priv = netdev_priv(dev);
487         u16 pkey_index = 0;
488
489         if (ib_find_cached_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
490                 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
491         else
492                 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
493 }
494
495 int ipoib_ib_dev_up(struct net_device *dev)
496 {
497         struct ipoib_dev_priv *priv = netdev_priv(dev);
498
499         ipoib_pkey_dev_check_presence(dev);
500
501         if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
502                 ipoib_dbg(priv, "PKEY is not assigned.\n");
503                 return 0;
504         }
505
506         set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
507
508         return ipoib_mcast_start_thread(dev);
509 }
510
511 int ipoib_ib_dev_down(struct net_device *dev, int flush)
512 {
513         struct ipoib_dev_priv *priv = netdev_priv(dev);
514
515         ipoib_dbg(priv, "downing ib_dev\n");
516
517         clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
518         netif_carrier_off(dev);
519
520         /* Shutdown the P_Key thread if still active */
521         if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
522                 mutex_lock(&pkey_mutex);
523                 set_bit(IPOIB_PKEY_STOP, &priv->flags);
524                 cancel_delayed_work(&priv->pkey_poll_task);
525                 mutex_unlock(&pkey_mutex);
526                 if (flush)
527                         flush_workqueue(ipoib_workqueue);
528         }
529
530         ipoib_mcast_stop_thread(dev, flush);
531         ipoib_mcast_dev_flush(dev);
532
533         ipoib_flush_paths(dev);
534
535         return 0;
536 }
537
538 static int recvs_pending(struct net_device *dev)
539 {
540         struct ipoib_dev_priv *priv = netdev_priv(dev);
541         int pending = 0;
542         int i;
543
544         for (i = 0; i < ipoib_recvq_size; ++i)
545                 if (priv->rx_ring[i].skb)
546                         ++pending;
547
548         return pending;
549 }
550
551 void ipoib_drain_cq(struct net_device *dev)
552 {
553         struct ipoib_dev_priv *priv = netdev_priv(dev);
554         int i, n;
555         do {
556                 n = ib_poll_cq(priv->cq, IPOIB_NUM_WC, priv->ibwc);
557                 for (i = 0; i < n; ++i) {
558                         /*
559                          * Convert any successful completions to flush
560                          * errors to avoid passing packets up the
561                          * stack after bringing the device down.
562                          */
563                         if (priv->ibwc[i].status == IB_WC_SUCCESS)
564                                 priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
565
566                         if (priv->ibwc[i].wr_id & IPOIB_CM_OP_SRQ)
567                                 ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
568                         else if (priv->ibwc[i].wr_id & IPOIB_OP_RECV)
569                                 ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
570                         else
571                                 ipoib_ib_handle_tx_wc(dev, priv->ibwc + i);
572                 }
573         } while (n == IPOIB_NUM_WC);
574 }
575
576 int ipoib_ib_dev_stop(struct net_device *dev, int flush)
577 {
578         struct ipoib_dev_priv *priv = netdev_priv(dev);
579         struct ib_qp_attr qp_attr;
580         unsigned long begin;
581         struct ipoib_tx_buf *tx_req;
582         int i;
583
584         clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
585
586         ipoib_cm_dev_stop(dev);
587
588         /*
589          * Move our QP to the error state and then reinitialize in
590          * when all work requests have completed or have been flushed.
591          */
592         qp_attr.qp_state = IB_QPS_ERR;
593         if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
594                 ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
595
596         /* Wait for all sends and receives to complete */
597         begin = jiffies;
598
599         while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
600                 if (time_after(jiffies, begin + 5 * HZ)) {
601                         ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
602                                    priv->tx_head - priv->tx_tail, recvs_pending(dev));
603
604                         /*
605                          * assume the HW is wedged and just free up
606                          * all our pending work requests.
607                          */
608                         while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
609                                 tx_req = &priv->tx_ring[priv->tx_tail &
610                                                         (ipoib_sendq_size - 1)];
611                                 ib_dma_unmap_single(priv->ca,
612                                                     tx_req->mapping,
613                                                     tx_req->skb->len,
614                                                     DMA_TO_DEVICE);
615                                 dev_kfree_skb_any(tx_req->skb);
616                                 ++priv->tx_tail;
617                         }
618
619                         for (i = 0; i < ipoib_recvq_size; ++i) {
620                                 struct ipoib_rx_buf *rx_req;
621
622                                 rx_req = &priv->rx_ring[i];
623                                 if (!rx_req->skb)
624                                         continue;
625                                 ib_dma_unmap_single(priv->ca,
626                                                     rx_req->mapping,
627                                                     IPOIB_BUF_SIZE,
628                                                     DMA_FROM_DEVICE);
629                                 dev_kfree_skb_any(rx_req->skb);
630                                 rx_req->skb = NULL;
631                         }
632
633                         goto timeout;
634                 }
635
636                 ipoib_drain_cq(dev);
637
638                 msleep(1);
639         }
640
641         ipoib_dbg(priv, "All sends and receives done.\n");
642
643 timeout:
644         qp_attr.qp_state = IB_QPS_RESET;
645         if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
646                 ipoib_warn(priv, "Failed to modify QP to RESET state\n");
647
648         /* Wait for all AHs to be reaped */
649         set_bit(IPOIB_STOP_REAPER, &priv->flags);
650         cancel_delayed_work(&priv->ah_reap_task);
651         if (flush)
652                 flush_workqueue(ipoib_workqueue);
653
654         begin = jiffies;
655
656         while (!list_empty(&priv->dead_ahs)) {
657                 __ipoib_reap_ah(dev);
658
659                 if (time_after(jiffies, begin + HZ)) {
660                         ipoib_warn(priv, "timing out; will leak address handles\n");
661                         break;
662                 }
663
664                 msleep(1);
665         }
666
667         ib_req_notify_cq(priv->cq, IB_CQ_NEXT_COMP);
668
669         return 0;
670 }
671
672 int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
673 {
674         struct ipoib_dev_priv *priv = netdev_priv(dev);
675
676         priv->ca = ca;
677         priv->port = port;
678         priv->qp = NULL;
679
680         if (ipoib_transport_dev_init(dev, ca)) {
681                 printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
682                 return -ENODEV;
683         }
684
685         if (dev->flags & IFF_UP) {
686                 if (ipoib_ib_dev_open(dev)) {
687                         ipoib_transport_dev_cleanup(dev);
688                         return -ENODEV;
689                 }
690         }
691
692         return 0;
693 }
694
695 static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv, int pkey_event)
696 {
697         struct ipoib_dev_priv *cpriv;
698         struct net_device *dev = priv->dev;
699         u16 new_index;
700
701         mutex_lock(&priv->vlan_mutex);
702
703         /*
704          * Flush any child interfaces too -- they might be up even if
705          * the parent is down.
706          */
707         list_for_each_entry(cpriv, &priv->child_intfs, list)
708                 __ipoib_ib_dev_flush(cpriv, pkey_event);
709
710         mutex_unlock(&priv->vlan_mutex);
711
712         if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
713                 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
714                 return;
715         }
716
717         if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
718                 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
719                 return;
720         }
721
722         if (pkey_event) {
723                 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
724                         clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
725                         ipoib_ib_dev_down(dev, 0);
726                         ipoib_pkey_dev_delay_open(dev);
727                         return;
728                 }
729                 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
730
731                 /* restart QP only if P_Key index is changed */
732                 if (new_index == priv->pkey_index) {
733                         ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
734                         return;
735                 }
736                 priv->pkey_index = new_index;
737         }
738
739         ipoib_dbg(priv, "flushing\n");
740
741         ipoib_ib_dev_down(dev, 0);
742
743         if (pkey_event) {
744                 ipoib_ib_dev_stop(dev, 0);
745                 ipoib_ib_dev_open(dev);
746         }
747
748         /*
749          * The device could have been brought down between the start and when
750          * we get here, don't bring it back up if it's not configured up
751          */
752         if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
753                 ipoib_ib_dev_up(dev);
754                 ipoib_mcast_restart_task(&priv->restart_task);
755         }
756 }
757
758 void ipoib_ib_dev_flush(struct work_struct *work)
759 {
760         struct ipoib_dev_priv *priv =
761                 container_of(work, struct ipoib_dev_priv, flush_task);
762
763         ipoib_dbg(priv, "Flushing %s\n", priv->dev->name);
764         __ipoib_ib_dev_flush(priv, 0);
765 }
766
767 void ipoib_pkey_event(struct work_struct *work)
768 {
769         struct ipoib_dev_priv *priv =
770                 container_of(work, struct ipoib_dev_priv, pkey_event_task);
771
772         ipoib_dbg(priv, "Flushing %s and restarting its QP\n", priv->dev->name);
773         __ipoib_ib_dev_flush(priv, 1);
774 }
775
776 void ipoib_ib_dev_cleanup(struct net_device *dev)
777 {
778         struct ipoib_dev_priv *priv = netdev_priv(dev);
779
780         ipoib_dbg(priv, "cleaning up ib_dev\n");
781
782         ipoib_mcast_stop_thread(dev, 1);
783         ipoib_mcast_dev_flush(dev);
784
785         ipoib_transport_dev_cleanup(dev);
786 }
787
788 /*
789  * Delayed P_Key Assigment Interim Support
790  *
791  * The following is initial implementation of delayed P_Key assigment
792  * mechanism. It is using the same approach implemented for the multicast
793  * group join. The single goal of this implementation is to quickly address
794  * Bug #2507. This implementation will probably be removed when the P_Key
795  * change async notification is available.
796  */
797
798 void ipoib_pkey_poll(struct work_struct *work)
799 {
800         struct ipoib_dev_priv *priv =
801                 container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
802         struct net_device *dev = priv->dev;
803
804         ipoib_pkey_dev_check_presence(dev);
805
806         if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
807                 ipoib_open(dev);
808         else {
809                 mutex_lock(&pkey_mutex);
810                 if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
811                         queue_delayed_work(ipoib_workqueue,
812                                            &priv->pkey_poll_task,
813                                            HZ);
814                 mutex_unlock(&pkey_mutex);
815         }
816 }
817
818 int ipoib_pkey_dev_delay_open(struct net_device *dev)
819 {
820         struct ipoib_dev_priv *priv = netdev_priv(dev);
821
822         /* Look for the interface pkey value in the IB Port P_Key table and */
823         /* set the interface pkey assigment flag                            */
824         ipoib_pkey_dev_check_presence(dev);
825
826         /* P_Key value not assigned yet - start polling */
827         if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
828                 mutex_lock(&pkey_mutex);
829                 clear_bit(IPOIB_PKEY_STOP, &priv->flags);
830                 queue_delayed_work(ipoib_workqueue,
831                                    &priv->pkey_poll_task,
832                                    HZ);
833                 mutex_unlock(&pkey_mutex);
834                 return 1;
835         }
836
837         return 0;
838 }