blob: 59d9594ed6d97e0fc243066f42f94c8f8f3c0ca6 [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
53#define IPOIB_OP_RECV (1ul << 31)
54
Ingo Molnar95ed6442006-01-13 14:51:39 -080055static DEFINE_MUTEX(pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57struct ipoib_ah *ipoib_create_ah(struct net_device *dev,
58 struct ib_pd *pd, struct ib_ah_attr *attr)
59{
60 struct ipoib_ah *ah;
61
62 ah = kmalloc(sizeof *ah, GFP_KERNEL);
63 if (!ah)
64 return NULL;
65
66 ah->dev = dev;
67 ah->last_send = 0;
68 kref_init(&ah->ref);
69
70 ah->ah = ib_create_ah(pd, attr);
71 if (IS_ERR(ah->ah)) {
72 kfree(ah);
73 ah = NULL;
74 } else
75 ipoib_dbg(netdev_priv(dev), "Created ah %p\n", ah->ah);
76
77 return ah;
78}
79
80void ipoib_free_ah(struct kref *kref)
81{
82 struct ipoib_ah *ah = container_of(kref, struct ipoib_ah, ref);
83 struct ipoib_dev_priv *priv = netdev_priv(ah->dev);
84
85 unsigned long flags;
86
Roland Dreier31c02e22006-06-17 20:37:34 -070087 spin_lock_irqsave(&priv->lock, flags);
88 list_add_tail(&ah->list, &priv->dead_ahs);
89 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int ipoib_ib_post_receive(struct net_device *dev, int id)
93{
94 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreier1993d682005-10-28 15:30:34 -070095 struct ib_sge list;
96 struct ib_recv_wr param;
97 struct ib_recv_wr *bad_wr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 int ret;
99
Roland Dreier1993d682005-10-28 15:30:34 -0700100 list.addr = priv->rx_ring[id].mapping;
101 list.length = IPOIB_BUF_SIZE;
102 list.lkey = priv->mr->lkey;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Roland Dreier1993d682005-10-28 15:30:34 -0700104 param.next = NULL;
105 param.wr_id = id | IPOIB_OP_RECV;
106 param.sg_list = &list;
107 param.num_sge = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Roland Dreier1993d682005-10-28 15:30:34 -0700109 ret = ib_post_recv(priv->qp, &param, &bad_wr);
110 if (unlikely(ret)) {
111 ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800112 ib_dma_unmap_single(priv->ca, priv->rx_ring[id].mapping,
113 IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
Roland Dreier1993d682005-10-28 15:30:34 -0700114 dev_kfree_skb_any(priv->rx_ring[id].skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 priv->rx_ring[id].skb = NULL;
116 }
117
118 return ret;
119}
120
Roland Dreier1993d682005-10-28 15:30:34 -0700121static int ipoib_alloc_rx_skb(struct net_device *dev, int id)
122{
123 struct ipoib_dev_priv *priv = netdev_priv(dev);
124 struct sk_buff *skb;
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800125 u64 addr;
Roland Dreier1993d682005-10-28 15:30:34 -0700126
127 skb = dev_alloc_skb(IPOIB_BUF_SIZE + 4);
128 if (!skb)
129 return -ENOMEM;
130
131 /*
132 * IB will leave a 40 byte gap for a GRH and IPoIB adds a 4 byte
133 * header. So we need 4 more bytes to get to 48 and align the
134 * IP header to a multiple of 16.
135 */
136 skb_reserve(skb, 4);
137
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800138 addr = ib_dma_map_single(priv->ca, skb->data, IPOIB_BUF_SIZE,
139 DMA_FROM_DEVICE);
140 if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
Roland Dreier1993d682005-10-28 15:30:34 -0700141 dev_kfree_skb_any(skb);
142 return -EIO;
143 }
144
145 priv->rx_ring[id].skb = skb;
146 priv->rx_ring[id].mapping = addr;
147
148 return 0;
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static int ipoib_ib_post_receives(struct net_device *dev)
152{
153 struct ipoib_dev_priv *priv = netdev_priv(dev);
154 int i;
155
Shirley Ma0f485252006-04-10 09:43:58 -0700156 for (i = 0; i < ipoib_recvq_size; ++i) {
Roland Dreier1993d682005-10-28 15:30:34 -0700157 if (ipoib_alloc_rx_skb(dev, i)) {
158 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
159 return -ENOMEM;
160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (ipoib_ib_post_receive(dev, i)) {
162 ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
163 return -EIO;
164 }
165 }
166
167 return 0;
168}
169
Roland Dreier2439a6e2006-09-22 15:22:52 -0700170static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
171{
172 struct ipoib_dev_priv *priv = netdev_priv(dev);
173 unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
174 struct sk_buff *skb;
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800175 u64 addr;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700176
177 ipoib_dbg_data(priv, "recv completion: id %d, op %d, status: %d\n",
178 wr_id, wc->opcode, wc->status);
179
180 if (unlikely(wr_id >= ipoib_recvq_size)) {
181 ipoib_warn(priv, "recv completion event with wrid %d (> %d)\n",
182 wr_id, ipoib_recvq_size);
183 return;
184 }
185
186 skb = priv->rx_ring[wr_id].skb;
187 addr = priv->rx_ring[wr_id].mapping;
188
189 if (unlikely(wc->status != IB_WC_SUCCESS)) {
190 if (wc->status != IB_WC_WR_FLUSH_ERR)
191 ipoib_warn(priv, "failed recv event "
192 "(status=%d, wrid=%d vend_err %x)\n",
193 wc->status, wr_id, wc->vendor_err);
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800194 ib_dma_unmap_single(priv->ca, addr,
195 IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700196 dev_kfree_skb_any(skb);
197 priv->rx_ring[wr_id].skb = NULL;
198 return;
199 }
200
201 /*
202 * If we can't allocate a new RX buffer, dump
203 * this packet and reuse the old buffer.
204 */
205 if (unlikely(ipoib_alloc_rx_skb(dev, wr_id))) {
206 ++priv->stats.rx_dropped;
207 goto repost;
208 }
209
210 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
211 wc->byte_len, wc->slid);
212
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800213 ib_dma_unmap_single(priv->ca, addr, IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700214
215 skb_put(skb, wc->byte_len);
216 skb_pull(skb, IB_GRH_BYTES);
217
218 if (wc->slid != priv->local_lid ||
219 wc->src_qp != priv->qp->qp_num) {
220 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
221 skb->mac.raw = skb->data;
222 skb_pull(skb, IPOIB_ENCAP_LEN);
223
224 dev->last_rx = jiffies;
225 ++priv->stats.rx_packets;
226 priv->stats.rx_bytes += skb->len;
227
228 skb->dev = dev;
229 /* XXX get correct PACKET_ type here */
230 skb->pkt_type = PACKET_HOST;
231 netif_rx_ni(skb);
232 } else {
233 ipoib_dbg_data(priv, "dropping loopback packet\n");
234 dev_kfree_skb_any(skb);
235 }
236
237repost:
238 if (unlikely(ipoib_ib_post_receive(dev, wr_id)))
239 ipoib_warn(priv, "ipoib_ib_post_receive failed "
240 "for buf %d\n", wr_id);
241}
242
243static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 struct ipoib_dev_priv *priv = netdev_priv(dev);
246 unsigned int wr_id = wc->wr_id;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700247 struct ipoib_tx_buf *tx_req;
248 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Roland Dreier2439a6e2006-09-22 15:22:52 -0700250 ipoib_dbg_data(priv, "send completion: id %d, op %d, status: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 wr_id, wc->opcode, wc->status);
252
Roland Dreier2439a6e2006-09-22 15:22:52 -0700253 if (unlikely(wr_id >= ipoib_sendq_size)) {
254 ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
255 wr_id, ipoib_sendq_size);
256 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
Roland Dreier2439a6e2006-09-22 15:22:52 -0700258
259 tx_req = &priv->tx_ring[wr_id];
260
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800261 ib_dma_unmap_single(priv->ca, tx_req->mapping,
262 tx_req->skb->len, DMA_TO_DEVICE);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700263
264 ++priv->stats.tx_packets;
265 priv->stats.tx_bytes += tx_req->skb->len;
266
267 dev_kfree_skb_any(tx_req->skb);
268
269 spin_lock_irqsave(&priv->tx_lock, flags);
270 ++priv->tx_tail;
271 if (netif_queue_stopped(dev) &&
272 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags) &&
273 priv->tx_head - priv->tx_tail <= ipoib_sendq_size >> 1)
274 netif_wake_queue(dev);
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
284static void ipoib_ib_handle_wc(struct net_device *dev, struct ib_wc *wc)
285{
286 if (wc->wr_id & IPOIB_OP_RECV)
287 ipoib_ib_handle_rx_wc(dev, wc);
288 else
289 ipoib_ib_handle_tx_wc(dev, wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
292void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
293{
294 struct net_device *dev = (struct net_device *) dev_ptr;
295 struct ipoib_dev_priv *priv = netdev_priv(dev);
296 int n, i;
297
298 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
299 do {
300 n = ib_poll_cq(cq, IPOIB_NUM_WC, priv->ibwc);
301 for (i = 0; i < n; ++i)
302 ipoib_ib_handle_wc(dev, priv->ibwc + i);
303 } while (n == IPOIB_NUM_WC);
304}
305
306static inline int post_send(struct ipoib_dev_priv *priv,
307 unsigned int wr_id,
308 struct ib_ah *address, u32 qpn,
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800309 u64 addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 struct ib_send_wr *bad_wr;
312
313 priv->tx_sge.addr = addr;
314 priv->tx_sge.length = len;
315
316 priv->tx_wr.wr_id = wr_id;
317 priv->tx_wr.wr.ud.remote_qpn = qpn;
318 priv->tx_wr.wr.ud.ah = address;
319
320 return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
321}
322
323void ipoib_send(struct net_device *dev, struct sk_buff *skb,
324 struct ipoib_ah *address, u32 qpn)
325{
326 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreier1993d682005-10-28 15:30:34 -0700327 struct ipoib_tx_buf *tx_req;
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800328 u64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Eli Cohena8bfca02006-09-22 15:22:58 -0700330 if (unlikely(skb->len > dev->mtu + INFINIBAND_ALEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
332 skb->len, dev->mtu + INFINIBAND_ALEN);
333 ++priv->stats.tx_dropped;
334 ++priv->stats.tx_errors;
335 dev_kfree_skb_any(skb);
336 return;
337 }
338
339 ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
340 skb->len, address, qpn);
341
342 /*
343 * We put the skb into the tx_ring _before_ we call post_send()
344 * because it's entirely possible that the completion handler will
345 * run before we execute anything after the post_send(). That
346 * means we have to make sure everything is properly recorded and
347 * our state is consistent before we call post_send().
348 */
Shirley Ma0f485252006-04-10 09:43:58 -0700349 tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 tx_req->skb = skb;
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800351 addr = ib_dma_map_single(priv->ca, skb->data, skb->len,
352 DMA_TO_DEVICE);
353 if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
Roland Dreier73fbe8b2006-10-10 12:50:38 -0700354 ++priv->stats.tx_errors;
355 dev_kfree_skb_any(skb);
356 return;
357 }
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800358 tx_req->mapping = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Shirley Ma0f485252006-04-10 09:43:58 -0700360 if (unlikely(post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 address->ah, qpn, addr, skb->len))) {
362 ipoib_warn(priv, "post_send failed\n");
363 ++priv->stats.tx_errors;
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800364 ib_dma_unmap_single(priv->ca, addr, skb->len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 dev_kfree_skb_any(skb);
366 } else {
367 dev->trans_start = jiffies;
368
369 address->last_send = priv->tx_head;
370 ++priv->tx_head;
371
Shirley Ma0f485252006-04-10 09:43:58 -0700372 if (priv->tx_head - priv->tx_tail == ipoib_sendq_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
374 netif_stop_queue(dev);
375 }
376 }
377}
378
379static void __ipoib_reap_ah(struct net_device *dev)
380{
381 struct ipoib_dev_priv *priv = netdev_priv(dev);
382 struct ipoib_ah *ah, *tah;
383 LIST_HEAD(remove_list);
384
Roland Dreier31c02e22006-06-17 20:37:34 -0700385 spin_lock_irq(&priv->tx_lock);
386 spin_lock(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
Roland Dreier21818582005-07-27 14:41:32 -0700388 if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 list_del(&ah->list);
Roland Dreier31c02e22006-06-17 20:37:34 -0700390 ib_destroy_ah(ah->ah);
391 kfree(ah);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
Roland Dreier31c02e22006-06-17 20:37:34 -0700393 spin_unlock(&priv->lock);
394 spin_unlock_irq(&priv->tx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
David Howellsc4028952006-11-22 14:57:56 +0000397void ipoib_reap_ah(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
David Howellsc4028952006-11-22 14:57:56 +0000399 struct ipoib_dev_priv *priv =
400 container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
401 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 __ipoib_reap_ah(dev);
404
405 if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
406 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
407}
408
409int ipoib_ib_dev_open(struct net_device *dev)
410{
411 struct ipoib_dev_priv *priv = netdev_priv(dev);
412 int ret;
413
Roland Dreier5b6810e2005-10-11 11:08:24 -0700414 ret = ipoib_init_qp(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (ret) {
Roland Dreier5b6810e2005-10-11 11:08:24 -0700416 ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return -1;
418 }
419
420 ret = ipoib_ib_post_receives(dev);
421 if (ret) {
422 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
Eli Cohen54d07e22006-03-02 11:05:19 -0800423 ipoib_ib_dev_stop(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return -1;
425 }
426
427 clear_bit(IPOIB_STOP_REAPER, &priv->flags);
428 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
429
Leonid Arsh7a343d42006-03-23 19:52:51 +0200430 set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return 0;
433}
434
Leonid Arsh7a343d42006-03-23 19:52:51 +0200435static void ipoib_pkey_dev_check_presence(struct net_device *dev)
436{
437 struct ipoib_dev_priv *priv = netdev_priv(dev);
438 u16 pkey_index = 0;
439
440 if (ib_find_cached_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
441 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
442 else
443 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
444}
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446int ipoib_ib_dev_up(struct net_device *dev)
447{
448 struct ipoib_dev_priv *priv = netdev_priv(dev);
449
Leonid Arsh7a343d42006-03-23 19:52:51 +0200450 ipoib_pkey_dev_check_presence(dev);
451
452 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
453 ipoib_dbg(priv, "PKEY is not assigned.\n");
454 return 0;
455 }
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
458
459 return ipoib_mcast_start_thread(dev);
460}
461
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800462int ipoib_ib_dev_down(struct net_device *dev, int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 struct ipoib_dev_priv *priv = netdev_priv(dev);
465
466 ipoib_dbg(priv, "downing ib_dev\n");
467
468 clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
469 netif_carrier_off(dev);
470
471 /* Shutdown the P_Key thread if still active */
472 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
Ingo Molnar95ed6442006-01-13 14:51:39 -0800473 mutex_lock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 set_bit(IPOIB_PKEY_STOP, &priv->flags);
475 cancel_delayed_work(&priv->pkey_task);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800476 mutex_unlock(&pkey_mutex);
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800477 if (flush)
478 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
480
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800481 ipoib_mcast_stop_thread(dev, flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 ipoib_mcast_dev_flush(dev);
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 ipoib_flush_paths(dev);
485
486 return 0;
487}
488
489static int recvs_pending(struct net_device *dev)
490{
491 struct ipoib_dev_priv *priv = netdev_priv(dev);
492 int pending = 0;
493 int i;
494
Shirley Ma0f485252006-04-10 09:43:58 -0700495 for (i = 0; i < ipoib_recvq_size; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (priv->rx_ring[i].skb)
497 ++pending;
498
499 return pending;
500}
501
502int ipoib_ib_dev_stop(struct net_device *dev)
503{
504 struct ipoib_dev_priv *priv = netdev_priv(dev);
505 struct ib_qp_attr qp_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 unsigned long begin;
Roland Dreier1993d682005-10-28 15:30:34 -0700507 struct ipoib_tx_buf *tx_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 int i;
509
Leonid Arsh7a343d42006-03-23 19:52:51 +0200510 clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
511
Roland Dreier3bc12e72005-10-30 13:20:09 -0800512 /*
513 * Move our QP to the error state and then reinitialize in
514 * when all work requests have completed or have been flushed.
515 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 qp_attr.qp_state = IB_QPS_ERR;
Roland Dreier3bc12e72005-10-30 13:20:09 -0800517 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
519
520 /* Wait for all sends and receives to complete */
521 begin = jiffies;
522
523 while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
524 if (time_after(jiffies, begin + 5 * HZ)) {
525 ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
526 priv->tx_head - priv->tx_tail, recvs_pending(dev));
527
528 /*
529 * assume the HW is wedged and just free up
530 * all our pending work requests.
531 */
Roland Dreier21818582005-07-27 14:41:32 -0700532 while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 tx_req = &priv->tx_ring[priv->tx_tail &
Shirley Ma0f485252006-04-10 09:43:58 -0700534 (ipoib_sendq_size - 1)];
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800535 ib_dma_unmap_single(priv->ca,
536 tx_req->mapping,
537 tx_req->skb->len,
538 DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 dev_kfree_skb_any(tx_req->skb);
540 ++priv->tx_tail;
541 }
542
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800543 for (i = 0; i < ipoib_recvq_size; ++i) {
544 struct ipoib_rx_buf *rx_req;
545
546 rx_req = &priv->rx_ring[i];
547 if (!rx_req->skb)
548 continue;
549 ib_dma_unmap_single(priv->ca,
550 rx_req->mapping,
551 IPOIB_BUF_SIZE,
552 DMA_FROM_DEVICE);
553 dev_kfree_skb_any(rx_req->skb);
554 rx_req->skb = NULL;
555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 goto timeout;
558 }
559
560 msleep(1);
561 }
562
563 ipoib_dbg(priv, "All sends and receives done.\n");
564
565timeout:
566 qp_attr.qp_state = IB_QPS_RESET;
Roland Dreier3bc12e72005-10-30 13:20:09 -0800567 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 ipoib_warn(priv, "Failed to modify QP to RESET state\n");
569
570 /* Wait for all AHs to be reaped */
571 set_bit(IPOIB_STOP_REAPER, &priv->flags);
572 cancel_delayed_work(&priv->ah_reap_task);
573 flush_workqueue(ipoib_workqueue);
574
575 begin = jiffies;
576
577 while (!list_empty(&priv->dead_ahs)) {
578 __ipoib_reap_ah(dev);
579
580 if (time_after(jiffies, begin + HZ)) {
581 ipoib_warn(priv, "timing out; will leak address handles\n");
582 break;
583 }
584
585 msleep(1);
586 }
587
588 return 0;
589}
590
591int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
592{
593 struct ipoib_dev_priv *priv = netdev_priv(dev);
594
595 priv->ca = ca;
596 priv->port = port;
597 priv->qp = NULL;
598
599 if (ipoib_transport_dev_init(dev, ca)) {
600 printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
601 return -ENODEV;
602 }
603
604 if (dev->flags & IFF_UP) {
605 if (ipoib_ib_dev_open(dev)) {
606 ipoib_transport_dev_cleanup(dev);
607 return -ENODEV;
608 }
609 }
610
611 return 0;
612}
613
David Howellsc4028952006-11-22 14:57:56 +0000614void ipoib_ib_dev_flush(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
David Howellsc4028952006-11-22 14:57:56 +0000616 struct ipoib_dev_priv *cpriv, *priv =
617 container_of(work, struct ipoib_dev_priv, flush_task);
618 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Leonid Arsh7a343d42006-03-23 19:52:51 +0200620 if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags) ) {
621 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return;
Leonid Arsh7a343d42006-03-23 19:52:51 +0200623 }
624
625 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
626 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
627 return;
628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 ipoib_dbg(priv, "flushing\n");
631
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800632 ipoib_ib_dev_down(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 /*
635 * The device could have been brought down between the start and when
636 * we get here, don't bring it back up if it's not configured up
637 */
Eli Cohen5ccd0252006-09-22 15:22:56 -0700638 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 ipoib_ib_dev_up(dev);
David Howellsc4028952006-11-22 14:57:56 +0000640 ipoib_mcast_restart_task(&priv->restart_task);
Eli Cohen5ccd0252006-09-22 15:22:56 -0700641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Ingo Molnar95ed6442006-01-13 14:51:39 -0800643 mutex_lock(&priv->vlan_mutex);
Michael S. Tsirkin4f710552005-11-29 10:53:30 -0800644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 /* Flush any child interfaces too */
646 list_for_each_entry(cpriv, &priv->child_intfs, list)
David Howellsc4028952006-11-22 14:57:56 +0000647 ipoib_ib_dev_flush(&cpriv->flush_task);
Michael S. Tsirkin4f710552005-11-29 10:53:30 -0800648
Ingo Molnar95ed6442006-01-13 14:51:39 -0800649 mutex_unlock(&priv->vlan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
652void ipoib_ib_dev_cleanup(struct net_device *dev)
653{
654 struct ipoib_dev_priv *priv = netdev_priv(dev);
655
656 ipoib_dbg(priv, "cleaning up ib_dev\n");
657
Roland Dreier8d2cae02005-09-20 10:52:04 -0700658 ipoib_mcast_stop_thread(dev, 1);
Eli Cohen988bd502006-01-12 14:32:20 -0800659 ipoib_mcast_dev_flush(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 ipoib_transport_dev_cleanup(dev);
662}
663
664/*
665 * Delayed P_Key Assigment Interim Support
666 *
667 * The following is initial implementation of delayed P_Key assigment
668 * mechanism. It is using the same approach implemented for the multicast
669 * group join. The single goal of this implementation is to quickly address
670 * Bug #2507. This implementation will probably be removed when the P_Key
671 * change async notification is available.
672 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
David Howellsc4028952006-11-22 14:57:56 +0000674void ipoib_pkey_poll(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
David Howellsc4028952006-11-22 14:57:56 +0000676 struct ipoib_dev_priv *priv =
677 container_of(work, struct ipoib_dev_priv, pkey_task.work);
678 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 ipoib_pkey_dev_check_presence(dev);
681
682 if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
683 ipoib_open(dev);
684 else {
Ingo Molnar95ed6442006-01-13 14:51:39 -0800685 mutex_lock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
687 queue_delayed_work(ipoib_workqueue,
688 &priv->pkey_task,
689 HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800690 mutex_unlock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692}
693
694int ipoib_pkey_dev_delay_open(struct net_device *dev)
695{
696 struct ipoib_dev_priv *priv = netdev_priv(dev);
697
698 /* Look for the interface pkey value in the IB Port P_Key table and */
699 /* set the interface pkey assigment flag */
700 ipoib_pkey_dev_check_presence(dev);
701
702 /* P_Key value not assigned yet - start polling */
703 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
Ingo Molnar95ed6442006-01-13 14:51:39 -0800704 mutex_lock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 clear_bit(IPOIB_PKEY_STOP, &priv->flags);
706 queue_delayed_work(ipoib_workqueue,
707 &priv->pkey_task,
708 HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800709 mutex_unlock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return 1;
711 }
712
713 return 0;
714}