blob: d13f4fb3853fd54cf6d2591aa1af21ea2c41acd1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
Roland Dreier2a1d9b72005-08-10 23:03:10 -07003 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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
Roland Dreiera4d61e82005-08-25 13:40:04 -070041#include <rdma/ib_cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
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
Ingo Molnar95ed6442006-01-13 14:51:39 -080053static DEFINE_MUTEX(pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
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
Roland Dreier31c02e22006-06-17 20:37:34 -070085 spin_lock_irqsave(&priv->lock, flags);
86 list_add_tail(&ah->list, &priv->dead_ahs);
87 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int ipoib_ib_post_receive(struct net_device *dev, int id)
91{
92 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreier1993d682005-10-28 15:30:34 -070093 struct ib_sge list;
94 struct ib_recv_wr param;
95 struct ib_recv_wr *bad_wr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 int ret;
97
Roland Dreier1993d682005-10-28 15:30:34 -070098 list.addr = priv->rx_ring[id].mapping;
99 list.length = IPOIB_BUF_SIZE;
100 list.lkey = priv->mr->lkey;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Roland Dreier1993d682005-10-28 15:30:34 -0700102 param.next = NULL;
103 param.wr_id = id | IPOIB_OP_RECV;
104 param.sg_list = &list;
105 param.num_sge = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Roland Dreier1993d682005-10-28 15:30:34 -0700107 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);
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800110 ib_dma_unmap_single(priv->ca, priv->rx_ring[id].mapping,
111 IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
Roland Dreier1993d682005-10-28 15:30:34 -0700112 dev_kfree_skb_any(priv->rx_ring[id].skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 priv->rx_ring[id].skb = NULL;
114 }
115
116 return ret;
117}
118
Roland Dreier1993d682005-10-28 15:30:34 -0700119static 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;
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800123 u64 addr;
Roland Dreier1993d682005-10-28 15:30:34 -0700124
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
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800136 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))) {
Roland Dreier1993d682005-10-28 15:30:34 -0700139 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static int ipoib_ib_post_receives(struct net_device *dev)
150{
151 struct ipoib_dev_priv *priv = netdev_priv(dev);
152 int i;
153
Shirley Ma0f485252006-04-10 09:43:58 -0700154 for (i = 0; i < ipoib_recvq_size; ++i) {
Roland Dreier1993d682005-10-28 15:30:34 -0700155 if (ipoib_alloc_rx_skb(dev, i)) {
156 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
157 return -ENOMEM;
158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 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
Roland Dreier2439a6e2006-09-22 15:22:52 -0700168static 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;
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800173 u64 addr;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700174
Roland Dreiera89875f2007-04-18 20:20:53 -0700175 ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
176 wr_id, wc->status);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700177
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);
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800192 ib_dma_unmap_single(priv->ca, addr,
193 IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700194 dev_kfree_skb_any(skb);
195 priv->rx_ring[wr_id].skb = NULL;
196 return;
197 }
198
199 /*
Roland Dreier1b844af2007-07-10 13:43:53 -0700200 * 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 /*
Roland Dreier2439a6e2006-09-22 15:22:52 -0700207 * 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))) {
Roland Dreierde903512007-09-28 15:33:51 -0700211 ++dev->stats.rx_dropped;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700212 goto repost;
213 }
214
215 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
216 wc->byte_len, wc->slid);
217
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800218 ib_dma_unmap_single(priv->ca, addr, IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700219
220 skb_put(skb, wc->byte_len);
221 skb_pull(skb, IB_GRH_BYTES);
222
Roland Dreier1b844af2007-07-10 13:43:53 -0700223 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
224 skb_reset_mac_header(skb);
225 skb_pull(skb, IPOIB_ENCAP_LEN);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700226
Roland Dreier1b844af2007-07-10 13:43:53 -0700227 dev->last_rx = jiffies;
Roland Dreierde903512007-09-28 15:33:51 -0700228 ++dev->stats.rx_packets;
229 dev->stats.rx_bytes += skb->len;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700230
Roland Dreier1b844af2007-07-10 13:43:53 -0700231 skb->dev = dev;
232 /* XXX get correct PACKET_ type here */
233 skb->pkt_type = PACKET_HOST;
Eli Cohen60461362008-04-16 21:01:10 -0700234
235 if (test_bit(IPOIB_FLAG_CSUM, &priv->flags) && likely(wc->csum_ok))
236 skb->ip_summed = CHECKSUM_UNNECESSARY;
237
Roland Dreier1b844af2007-07-10 13:43:53 -0700238 netif_receive_skb(skb);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700239
240repost:
241 if (unlikely(ipoib_ib_post_receive(dev, wr_id)))
242 ipoib_warn(priv, "ipoib_ib_post_receive failed "
243 "for buf %d\n", wr_id);
244}
245
Eli Cohen71437402008-01-30 18:30:53 +0200246static int ipoib_dma_map_tx(struct ib_device *ca,
247 struct ipoib_tx_buf *tx_req)
248{
249 struct sk_buff *skb = tx_req->skb;
250 u64 *mapping = tx_req->mapping;
251 int i;
252
253 mapping[0] = ib_dma_map_single(ca, skb->data, skb_headlen(skb),
254 DMA_TO_DEVICE);
255 if (unlikely(ib_dma_mapping_error(ca, mapping[0])))
256 return -EIO;
257
258 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
259 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
260 mapping[i + 1] = ib_dma_map_page(ca, frag->page,
261 frag->page_offset, frag->size,
262 DMA_TO_DEVICE);
263 if (unlikely(ib_dma_mapping_error(ca, mapping[i + 1])))
264 goto partial_error;
265 }
266 return 0;
267
268partial_error:
269 ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
270
271 for (; i > 0; --i) {
272 skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
273 ib_dma_unmap_page(ca, mapping[i], frag->size, DMA_TO_DEVICE);
274 }
275 return -EIO;
276}
277
278static void ipoib_dma_unmap_tx(struct ib_device *ca,
279 struct ipoib_tx_buf *tx_req)
280{
281 struct sk_buff *skb = tx_req->skb;
282 u64 *mapping = tx_req->mapping;
283 int i;
284
285 ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
286
287 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
288 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
289 ib_dma_unmap_page(ca, mapping[i + 1], frag->size,
290 DMA_TO_DEVICE);
291 }
292}
293
Roland Dreier2439a6e2006-09-22 15:22:52 -0700294static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 struct ipoib_dev_priv *priv = netdev_priv(dev);
297 unsigned int wr_id = wc->wr_id;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700298 struct ipoib_tx_buf *tx_req;
299 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Roland Dreiera89875f2007-04-18 20:20:53 -0700301 ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
302 wr_id, wc->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Roland Dreier2439a6e2006-09-22 15:22:52 -0700304 if (unlikely(wr_id >= ipoib_sendq_size)) {
305 ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
306 wr_id, ipoib_sendq_size);
307 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
Roland Dreier2439a6e2006-09-22 15:22:52 -0700309
310 tx_req = &priv->tx_ring[wr_id];
311
Eli Cohen71437402008-01-30 18:30:53 +0200312 ipoib_dma_unmap_tx(priv->ca, tx_req);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700313
Roland Dreierde903512007-09-28 15:33:51 -0700314 ++dev->stats.tx_packets;
315 dev->stats.tx_bytes += tx_req->skb->len;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700316
317 dev_kfree_skb_any(tx_req->skb);
318
319 spin_lock_irqsave(&priv->tx_lock, flags);
320 ++priv->tx_tail;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300321 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
322 netif_queue_stopped(dev) &&
323 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
Roland Dreier2439a6e2006-09-22 15:22:52 -0700324 netif_wake_queue(dev);
325 spin_unlock_irqrestore(&priv->tx_lock, flags);
326
327 if (wc->status != IB_WC_SUCCESS &&
328 wc->status != IB_WC_WR_FLUSH_ERR)
329 ipoib_warn(priv, "failed send event "
330 "(status=%d, wrid=%d vend_err %x)\n",
331 wc->status, wr_id, wc->vendor_err);
332}
333
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700334int ipoib_poll(struct napi_struct *napi, int budget)
Roland Dreier2439a6e2006-09-22 15:22:52 -0700335{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700336 struct ipoib_dev_priv *priv = container_of(napi, struct ipoib_dev_priv, napi);
337 struct net_device *dev = priv->dev;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700338 int done;
339 int t;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700340 int n, i;
341
342 done = 0;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700343
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700344poll_more:
345 while (done < budget) {
346 int max = (budget - done);
347
Roland Dreier8d1cc862007-05-06 21:05:32 -0700348 t = min(IPOIB_NUM_WC, max);
349 n = ib_poll_cq(priv->cq, t, priv->ibwc);
350
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700351 for (i = 0; i < n; i++) {
Roland Dreier8d1cc862007-05-06 21:05:32 -0700352 struct ib_wc *wc = priv->ibwc + i;
353
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300354 if (wc->wr_id & IPOIB_OP_RECV) {
Roland Dreier8d1cc862007-05-06 21:05:32 -0700355 ++done;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300356 if (wc->wr_id & IPOIB_OP_CM)
357 ipoib_cm_handle_rx_wc(dev, wc);
358 else
359 ipoib_ib_handle_rx_wc(dev, wc);
360 } else {
361 if (wc->wr_id & IPOIB_OP_CM)
362 ipoib_cm_handle_tx_wc(dev, wc);
363 else
364 ipoib_ib_handle_tx_wc(dev, wc);
365 }
Roland Dreier8d1cc862007-05-06 21:05:32 -0700366 }
367
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700368 if (n != t)
Roland Dreier8d1cc862007-05-06 21:05:32 -0700369 break;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700370 }
371
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700372 if (done < budget) {
373 netif_rx_complete(dev, napi);
Roland Dreier8d1cc862007-05-06 21:05:32 -0700374 if (unlikely(ib_req_notify_cq(priv->cq,
375 IB_CQ_NEXT_COMP |
376 IB_CQ_REPORT_MISSED_EVENTS)) &&
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700377 netif_rx_reschedule(dev, napi))
378 goto poll_more;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700379 }
380
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700381 return done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
385{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700386 struct net_device *dev = dev_ptr;
387 struct ipoib_dev_priv *priv = netdev_priv(dev);
388
389 netif_rx_schedule(dev, &priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
392static inline int post_send(struct ipoib_dev_priv *priv,
393 unsigned int wr_id,
394 struct ib_ah *address, u32 qpn,
Eli Cohen71437402008-01-30 18:30:53 +0200395 u64 *mapping, int headlen,
396 skb_frag_t *frags,
397 int nr_frags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 struct ib_send_wr *bad_wr;
Eli Cohen71437402008-01-30 18:30:53 +0200400 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Eli Cohen71437402008-01-30 18:30:53 +0200402 priv->tx_sge[0].addr = mapping[0];
403 priv->tx_sge[0].length = headlen;
404 for (i = 0; i < nr_frags; ++i) {
405 priv->tx_sge[i + 1].addr = mapping[i + 1];
406 priv->tx_sge[i + 1].length = frags[i].size;
407 }
408 priv->tx_wr.num_sge = nr_frags + 1;
409 priv->tx_wr.wr_id = wr_id;
410 priv->tx_wr.wr.ud.remote_qpn = qpn;
411 priv->tx_wr.wr.ud.ah = address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
414}
415
416void ipoib_send(struct net_device *dev, struct sk_buff *skb,
417 struct ipoib_ah *address, u32 qpn)
418{
419 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreier1993d682005-10-28 15:30:34 -0700420 struct ipoib_tx_buf *tx_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Michael S. Tsirkin77d8e1ef2007-03-21 15:45:05 +0200422 if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
Michael S. Tsirkin77d8e1ef2007-03-21 15:45:05 +0200424 skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
Roland Dreierde903512007-09-28 15:33:51 -0700425 ++dev->stats.tx_dropped;
426 ++dev->stats.tx_errors;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200427 ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return;
429 }
430
431 ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
432 skb->len, address, qpn);
433
434 /*
435 * We put the skb into the tx_ring _before_ we call post_send()
436 * because it's entirely possible that the completion handler will
437 * run before we execute anything after the post_send(). That
438 * means we have to make sure everything is properly recorded and
439 * our state is consistent before we call post_send().
440 */
Shirley Ma0f485252006-04-10 09:43:58 -0700441 tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 tx_req->skb = skb;
Eli Cohen71437402008-01-30 18:30:53 +0200443 if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) {
Roland Dreierde903512007-09-28 15:33:51 -0700444 ++dev->stats.tx_errors;
Roland Dreier73fbe8b2006-10-10 12:50:38 -0700445 dev_kfree_skb_any(skb);
446 return;
447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Eli Cohen60461362008-04-16 21:01:10 -0700449 if (skb->ip_summed == CHECKSUM_PARTIAL)
450 priv->tx_wr.send_flags |= IB_SEND_IP_CSUM;
451 else
452 priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
453
Shirley Ma0f485252006-04-10 09:43:58 -0700454 if (unlikely(post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
Eli Cohen71437402008-01-30 18:30:53 +0200455 address->ah, qpn,
456 tx_req->mapping, skb_headlen(skb),
457 skb_shinfo(skb)->frags, skb_shinfo(skb)->nr_frags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 ipoib_warn(priv, "post_send failed\n");
Roland Dreierde903512007-09-28 15:33:51 -0700459 ++dev->stats.tx_errors;
Eli Cohen71437402008-01-30 18:30:53 +0200460 ipoib_dma_unmap_tx(priv->ca, tx_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 dev_kfree_skb_any(skb);
462 } else {
463 dev->trans_start = jiffies;
464
465 address->last_send = priv->tx_head;
466 ++priv->tx_head;
467
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300468 if (++priv->tx_outstanding == ipoib_sendq_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
470 netif_stop_queue(dev);
471 }
472 }
473}
474
475static void __ipoib_reap_ah(struct net_device *dev)
476{
477 struct ipoib_dev_priv *priv = netdev_priv(dev);
478 struct ipoib_ah *ah, *tah;
479 LIST_HEAD(remove_list);
480
Roland Dreier31c02e22006-06-17 20:37:34 -0700481 spin_lock_irq(&priv->tx_lock);
482 spin_lock(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
Roland Dreier21818582005-07-27 14:41:32 -0700484 if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 list_del(&ah->list);
Roland Dreier31c02e22006-06-17 20:37:34 -0700486 ib_destroy_ah(ah->ah);
487 kfree(ah);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
Roland Dreier31c02e22006-06-17 20:37:34 -0700489 spin_unlock(&priv->lock);
490 spin_unlock_irq(&priv->tx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
David Howellsc4028952006-11-22 14:57:56 +0000493void ipoib_reap_ah(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
David Howellsc4028952006-11-22 14:57:56 +0000495 struct ipoib_dev_priv *priv =
496 container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
497 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 __ipoib_reap_ah(dev);
500
501 if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
Anton Blanchard69fc5072007-10-15 00:50:56 -0500502 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
503 round_jiffies_relative(HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
506int ipoib_ib_dev_open(struct net_device *dev)
507{
508 struct ipoib_dev_priv *priv = netdev_priv(dev);
509 int ret;
510
Yosef Etigin26bbf132007-05-19 08:51:54 -0700511 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {
512 ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);
513 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
514 return -1;
515 }
516 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
517
Roland Dreier5b6810e2005-10-11 11:08:24 -0700518 ret = ipoib_init_qp(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (ret) {
Roland Dreier5b6810e2005-10-11 11:08:24 -0700520 ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return -1;
522 }
523
524 ret = ipoib_ib_post_receives(dev);
525 if (ret) {
526 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700527 ipoib_ib_dev_stop(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return -1;
529 }
530
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200531 ret = ipoib_cm_dev_open(dev);
532 if (ret) {
Michael S. Tsirkin24bd1e42007-05-18 16:12:54 +0300533 ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700534 ipoib_ib_dev_stop(dev, 1);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200535 return -1;
536 }
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 clear_bit(IPOIB_STOP_REAPER, &priv->flags);
Anton Blanchard69fc5072007-10-15 00:50:56 -0500539 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
540 round_jiffies_relative(HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Leonid Arsh7a343d42006-03-23 19:52:51 +0200542 set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return 0;
545}
546
Leonid Arsh7a343d42006-03-23 19:52:51 +0200547static void ipoib_pkey_dev_check_presence(struct net_device *dev)
548{
549 struct ipoib_dev_priv *priv = netdev_priv(dev);
550 u16 pkey_index = 0;
551
552 if (ib_find_cached_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
553 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
554 else
555 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
556}
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558int ipoib_ib_dev_up(struct net_device *dev)
559{
560 struct ipoib_dev_priv *priv = netdev_priv(dev);
561
Leonid Arsh7a343d42006-03-23 19:52:51 +0200562 ipoib_pkey_dev_check_presence(dev);
563
564 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
565 ipoib_dbg(priv, "PKEY is not assigned.\n");
566 return 0;
567 }
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
570
571 return ipoib_mcast_start_thread(dev);
572}
573
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800574int ipoib_ib_dev_down(struct net_device *dev, int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 struct ipoib_dev_priv *priv = netdev_priv(dev);
577
578 ipoib_dbg(priv, "downing ib_dev\n");
579
580 clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
581 netif_carrier_off(dev);
582
583 /* Shutdown the P_Key thread if still active */
584 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
Ingo Molnar95ed6442006-01-13 14:51:39 -0800585 mutex_lock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 set_bit(IPOIB_PKEY_STOP, &priv->flags);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700587 cancel_delayed_work(&priv->pkey_poll_task);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800588 mutex_unlock(&pkey_mutex);
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800589 if (flush)
590 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800593 ipoib_mcast_stop_thread(dev, flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 ipoib_mcast_dev_flush(dev);
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 ipoib_flush_paths(dev);
597
598 return 0;
599}
600
601static int recvs_pending(struct net_device *dev)
602{
603 struct ipoib_dev_priv *priv = netdev_priv(dev);
604 int pending = 0;
605 int i;
606
Shirley Ma0f485252006-04-10 09:43:58 -0700607 for (i = 0; i < ipoib_recvq_size; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (priv->rx_ring[i].skb)
609 ++pending;
610
611 return pending;
612}
613
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300614void ipoib_drain_cq(struct net_device *dev)
615{
616 struct ipoib_dev_priv *priv = netdev_priv(dev);
617 int i, n;
618 do {
619 n = ib_poll_cq(priv->cq, IPOIB_NUM_WC, priv->ibwc);
620 for (i = 0; i < n; ++i) {
Roland Dreierce423ef2007-10-09 19:59:04 -0700621 /*
622 * Convert any successful completions to flush
623 * errors to avoid passing packets up the
624 * stack after bringing the device down.
625 */
626 if (priv->ibwc[i].status == IB_WC_SUCCESS)
627 priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
628
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300629 if (priv->ibwc[i].wr_id & IPOIB_OP_RECV) {
630 if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
631 ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
632 else
633 ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
634 } else {
635 if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
636 ipoib_cm_handle_tx_wc(dev, priv->ibwc + i);
637 else
638 ipoib_ib_handle_tx_wc(dev, priv->ibwc + i);
639 }
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300640 }
641 } while (n == IPOIB_NUM_WC);
642}
643
Yosef Etigin26bbf132007-05-19 08:51:54 -0700644int ipoib_ib_dev_stop(struct net_device *dev, int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
646 struct ipoib_dev_priv *priv = netdev_priv(dev);
647 struct ib_qp_attr qp_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 unsigned long begin;
Roland Dreier1993d682005-10-28 15:30:34 -0700649 struct ipoib_tx_buf *tx_req;
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300650 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Leonid Arsh7a343d42006-03-23 19:52:51 +0200652 clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
653
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200654 ipoib_cm_dev_stop(dev);
655
Roland Dreier3bc12e72005-10-30 13:20:09 -0800656 /*
657 * Move our QP to the error state and then reinitialize in
658 * when all work requests have completed or have been flushed.
659 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 qp_attr.qp_state = IB_QPS_ERR;
Roland Dreier3bc12e72005-10-30 13:20:09 -0800661 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
663
664 /* Wait for all sends and receives to complete */
665 begin = jiffies;
666
667 while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
668 if (time_after(jiffies, begin + 5 * HZ)) {
669 ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
670 priv->tx_head - priv->tx_tail, recvs_pending(dev));
671
672 /*
673 * assume the HW is wedged and just free up
674 * all our pending work requests.
675 */
Roland Dreier21818582005-07-27 14:41:32 -0700676 while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 tx_req = &priv->tx_ring[priv->tx_tail &
Shirley Ma0f485252006-04-10 09:43:58 -0700678 (ipoib_sendq_size - 1)];
Eli Cohen71437402008-01-30 18:30:53 +0200679 ipoib_dma_unmap_tx(priv->ca, tx_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 dev_kfree_skb_any(tx_req->skb);
681 ++priv->tx_tail;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300682 --priv->tx_outstanding;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800685 for (i = 0; i < ipoib_recvq_size; ++i) {
686 struct ipoib_rx_buf *rx_req;
687
688 rx_req = &priv->rx_ring[i];
689 if (!rx_req->skb)
690 continue;
691 ib_dma_unmap_single(priv->ca,
692 rx_req->mapping,
693 IPOIB_BUF_SIZE,
694 DMA_FROM_DEVICE);
695 dev_kfree_skb_any(rx_req->skb);
696 rx_req->skb = NULL;
697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 goto timeout;
700 }
701
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300702 ipoib_drain_cq(dev);
Roland Dreier8d1cc862007-05-06 21:05:32 -0700703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 msleep(1);
705 }
706
707 ipoib_dbg(priv, "All sends and receives done.\n");
708
709timeout:
710 qp_attr.qp_state = IB_QPS_RESET;
Roland Dreier3bc12e72005-10-30 13:20:09 -0800711 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 ipoib_warn(priv, "Failed to modify QP to RESET state\n");
713
714 /* Wait for all AHs to be reaped */
715 set_bit(IPOIB_STOP_REAPER, &priv->flags);
716 cancel_delayed_work(&priv->ah_reap_task);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700717 if (flush)
718 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 begin = jiffies;
721
722 while (!list_empty(&priv->dead_ahs)) {
723 __ipoib_reap_ah(dev);
724
725 if (time_after(jiffies, begin + HZ)) {
726 ipoib_warn(priv, "timing out; will leak address handles\n");
727 break;
728 }
729
730 msleep(1);
731 }
732
Roland Dreier8d1cc862007-05-06 21:05:32 -0700733 ib_req_notify_cq(priv->cq, IB_CQ_NEXT_COMP);
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return 0;
736}
737
738int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
739{
740 struct ipoib_dev_priv *priv = netdev_priv(dev);
741
742 priv->ca = ca;
743 priv->port = port;
744 priv->qp = NULL;
745
746 if (ipoib_transport_dev_init(dev, ca)) {
747 printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
748 return -ENODEV;
749 }
750
751 if (dev->flags & IFF_UP) {
752 if (ipoib_ib_dev_open(dev)) {
753 ipoib_transport_dev_cleanup(dev);
754 return -ENODEV;
755 }
756 }
757
758 return 0;
759}
760
Yosef Etigin26bbf132007-05-19 08:51:54 -0700761static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv, int pkey_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Yosef Etigin26bbf132007-05-19 08:51:54 -0700763 struct ipoib_dev_priv *cpriv;
David Howellsc4028952006-11-22 14:57:56 +0000764 struct net_device *dev = priv->dev;
Yosef Etigin26bbf132007-05-19 08:51:54 -0700765 u16 new_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Yosef Etigin26bbf132007-05-19 08:51:54 -0700767 mutex_lock(&priv->vlan_mutex);
768
769 /*
770 * Flush any child interfaces too -- they might be up even if
771 * the parent is down.
772 */
773 list_for_each_entry(cpriv, &priv->child_intfs, list)
774 __ipoib_ib_dev_flush(cpriv, pkey_event);
775
776 mutex_unlock(&priv->vlan_mutex);
777
778 if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
Leonid Arsh7a343d42006-03-23 19:52:51 +0200779 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return;
Leonid Arsh7a343d42006-03-23 19:52:51 +0200781 }
782
783 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
784 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
785 return;
786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Yosef Etigin26bbf132007-05-19 08:51:54 -0700788 if (pkey_event) {
789 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
790 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
791 ipoib_ib_dev_down(dev, 0);
Jack Morgenstein167c4262008-02-13 16:23:50 +0200792 ipoib_ib_dev_stop(dev, 0);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700793 ipoib_pkey_dev_delay_open(dev);
794 return;
795 }
796 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
797
798 /* restart QP only if P_Key index is changed */
799 if (new_index == priv->pkey_index) {
800 ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
801 return;
802 }
803 priv->pkey_index = new_index;
804 }
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 ipoib_dbg(priv, "flushing\n");
807
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800808 ipoib_ib_dev_down(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Yosef Etigin26bbf132007-05-19 08:51:54 -0700810 if (pkey_event) {
811 ipoib_ib_dev_stop(dev, 0);
812 ipoib_ib_dev_open(dev);
813 }
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 /*
816 * The device could have been brought down between the start and when
817 * we get here, don't bring it back up if it's not configured up
818 */
Eli Cohen5ccd0252006-09-22 15:22:56 -0700819 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 ipoib_ib_dev_up(dev);
David Howellsc4028952006-11-22 14:57:56 +0000821 ipoib_mcast_restart_task(&priv->restart_task);
Eli Cohen5ccd0252006-09-22 15:22:56 -0700822 }
Yosef Etigin26bbf132007-05-19 08:51:54 -0700823}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Yosef Etigin26bbf132007-05-19 08:51:54 -0700825void ipoib_ib_dev_flush(struct work_struct *work)
826{
827 struct ipoib_dev_priv *priv =
828 container_of(work, struct ipoib_dev_priv, flush_task);
Michael S. Tsirkin4f710552005-11-29 10:53:30 -0800829
Yosef Etigin26bbf132007-05-19 08:51:54 -0700830 ipoib_dbg(priv, "Flushing %s\n", priv->dev->name);
831 __ipoib_ib_dev_flush(priv, 0);
832}
Michael S. Tsirkin4f710552005-11-29 10:53:30 -0800833
Yosef Etigin26bbf132007-05-19 08:51:54 -0700834void ipoib_pkey_event(struct work_struct *work)
835{
836 struct ipoib_dev_priv *priv =
837 container_of(work, struct ipoib_dev_priv, pkey_event_task);
838
839 ipoib_dbg(priv, "Flushing %s and restarting its QP\n", priv->dev->name);
840 __ipoib_ib_dev_flush(priv, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
843void ipoib_ib_dev_cleanup(struct net_device *dev)
844{
845 struct ipoib_dev_priv *priv = netdev_priv(dev);
846
847 ipoib_dbg(priv, "cleaning up ib_dev\n");
848
Roland Dreier8d2cae02005-09-20 10:52:04 -0700849 ipoib_mcast_stop_thread(dev, 1);
Eli Cohen988bd502006-01-12 14:32:20 -0800850 ipoib_mcast_dev_flush(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 ipoib_transport_dev_cleanup(dev);
853}
854
855/*
856 * Delayed P_Key Assigment Interim Support
857 *
858 * The following is initial implementation of delayed P_Key assigment
859 * mechanism. It is using the same approach implemented for the multicast
860 * group join. The single goal of this implementation is to quickly address
861 * Bug #2507. This implementation will probably be removed when the P_Key
862 * change async notification is available.
863 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
David Howellsc4028952006-11-22 14:57:56 +0000865void ipoib_pkey_poll(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
David Howellsc4028952006-11-22 14:57:56 +0000867 struct ipoib_dev_priv *priv =
Yosef Etigin26bbf132007-05-19 08:51:54 -0700868 container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
David Howellsc4028952006-11-22 14:57:56 +0000869 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 ipoib_pkey_dev_check_presence(dev);
872
873 if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
874 ipoib_open(dev);
875 else {
Ingo Molnar95ed6442006-01-13 14:51:39 -0800876 mutex_lock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
878 queue_delayed_work(ipoib_workqueue,
Yosef Etigin26bbf132007-05-19 08:51:54 -0700879 &priv->pkey_poll_task,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800881 mutex_unlock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
883}
884
885int ipoib_pkey_dev_delay_open(struct net_device *dev)
886{
887 struct ipoib_dev_priv *priv = netdev_priv(dev);
888
889 /* Look for the interface pkey value in the IB Port P_Key table and */
890 /* set the interface pkey assigment flag */
891 ipoib_pkey_dev_check_presence(dev);
892
893 /* P_Key value not assigned yet - start polling */
894 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
Ingo Molnar95ed6442006-01-13 14:51:39 -0800895 mutex_lock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 clear_bit(IPOIB_PKEY_STOP, &priv->flags);
897 queue_delayed_work(ipoib_workqueue,
Yosef Etigin26bbf132007-05-19 08:51:54 -0700898 &priv->pkey_poll_task,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800900 mutex_unlock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return 1;
902 }
903
904 return 0;
905}