blob: 8bf5e9ec7c9522345696a2819ee99f444af341e7 [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);
112 dma_unmap_single(priv->ca->dma_device,
113 priv->rx_ring[id].mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
Roland Dreier1993d682005-10-28 15:30:34 -0700115 dev_kfree_skb_any(priv->rx_ring[id].skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 priv->rx_ring[id].skb = NULL;
117 }
118
119 return ret;
120}
121
Roland Dreier1993d682005-10-28 15:30:34 -0700122static int ipoib_alloc_rx_skb(struct net_device *dev, int id)
123{
124 struct ipoib_dev_priv *priv = netdev_priv(dev);
125 struct sk_buff *skb;
126 dma_addr_t addr;
127
128 skb = dev_alloc_skb(IPOIB_BUF_SIZE + 4);
129 if (!skb)
130 return -ENOMEM;
131
132 /*
133 * IB will leave a 40 byte gap for a GRH and IPoIB adds a 4 byte
134 * header. So we need 4 more bytes to get to 48 and align the
135 * IP header to a multiple of 16.
136 */
137 skb_reserve(skb, 4);
138
139 addr = dma_map_single(priv->ca->dma_device,
140 skb->data, IPOIB_BUF_SIZE,
141 DMA_FROM_DEVICE);
142 if (unlikely(dma_mapping_error(addr))) {
143 dev_kfree_skb_any(skb);
144 return -EIO;
145 }
146
147 priv->rx_ring[id].skb = skb;
148 priv->rx_ring[id].mapping = addr;
149
150 return 0;
151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static int ipoib_ib_post_receives(struct net_device *dev)
154{
155 struct ipoib_dev_priv *priv = netdev_priv(dev);
156 int i;
157
Shirley Ma0f485252006-04-10 09:43:58 -0700158 for (i = 0; i < ipoib_recvq_size; ++i) {
Roland Dreier1993d682005-10-28 15:30:34 -0700159 if (ipoib_alloc_rx_skb(dev, i)) {
160 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
161 return -ENOMEM;
162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (ipoib_ib_post_receive(dev, i)) {
164 ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
165 return -EIO;
166 }
167 }
168
169 return 0;
170}
171
Roland Dreier2439a6e2006-09-22 15:22:52 -0700172static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
173{
174 struct ipoib_dev_priv *priv = netdev_priv(dev);
175 unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
176 struct sk_buff *skb;
177 dma_addr_t addr;
178
179 ipoib_dbg_data(priv, "recv completion: id %d, op %d, status: %d\n",
180 wr_id, wc->opcode, wc->status);
181
182 if (unlikely(wr_id >= ipoib_recvq_size)) {
183 ipoib_warn(priv, "recv completion event with wrid %d (> %d)\n",
184 wr_id, ipoib_recvq_size);
185 return;
186 }
187
188 skb = priv->rx_ring[wr_id].skb;
189 addr = priv->rx_ring[wr_id].mapping;
190
191 if (unlikely(wc->status != IB_WC_SUCCESS)) {
192 if (wc->status != IB_WC_WR_FLUSH_ERR)
193 ipoib_warn(priv, "failed recv event "
194 "(status=%d, wrid=%d vend_err %x)\n",
195 wc->status, wr_id, wc->vendor_err);
196 dma_unmap_single(priv->ca->dma_device, addr,
197 IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
198 dev_kfree_skb_any(skb);
199 priv->rx_ring[wr_id].skb = NULL;
200 return;
201 }
202
203 /*
204 * If we can't allocate a new RX buffer, dump
205 * this packet and reuse the old buffer.
206 */
207 if (unlikely(ipoib_alloc_rx_skb(dev, wr_id))) {
208 ++priv->stats.rx_dropped;
209 goto repost;
210 }
211
212 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
213 wc->byte_len, wc->slid);
214
215 dma_unmap_single(priv->ca->dma_device, addr,
216 IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
217
218 skb_put(skb, wc->byte_len);
219 skb_pull(skb, IB_GRH_BYTES);
220
221 if (wc->slid != priv->local_lid ||
222 wc->src_qp != priv->qp->qp_num) {
223 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
224 skb->mac.raw = skb->data;
225 skb_pull(skb, IPOIB_ENCAP_LEN);
226
227 dev->last_rx = jiffies;
228 ++priv->stats.rx_packets;
229 priv->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_rx_ni(skb);
235 } else {
236 ipoib_dbg_data(priv, "dropping loopback packet\n");
237 dev_kfree_skb_any(skb);
238 }
239
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
246static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 struct ipoib_dev_priv *priv = netdev_priv(dev);
249 unsigned int wr_id = wc->wr_id;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700250 struct ipoib_tx_buf *tx_req;
251 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Roland Dreier2439a6e2006-09-22 15:22:52 -0700253 ipoib_dbg_data(priv, "send completion: id %d, op %d, status: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 wr_id, wc->opcode, wc->status);
255
Roland Dreier2439a6e2006-09-22 15:22:52 -0700256 if (unlikely(wr_id >= ipoib_sendq_size)) {
257 ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
258 wr_id, ipoib_sendq_size);
259 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
Roland Dreier2439a6e2006-09-22 15:22:52 -0700261
262 tx_req = &priv->tx_ring[wr_id];
263
264 dma_unmap_single(priv->ca->dma_device,
265 pci_unmap_addr(tx_req, mapping),
266 tx_req->skb->len,
267 DMA_TO_DEVICE);
268
269 ++priv->stats.tx_packets;
270 priv->stats.tx_bytes += tx_req->skb->len;
271
272 dev_kfree_skb_any(tx_req->skb);
273
274 spin_lock_irqsave(&priv->tx_lock, flags);
275 ++priv->tx_tail;
276 if (netif_queue_stopped(dev) &&
277 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags) &&
278 priv->tx_head - priv->tx_tail <= ipoib_sendq_size >> 1)
279 netif_wake_queue(dev);
280 spin_unlock_irqrestore(&priv->tx_lock, flags);
281
282 if (wc->status != IB_WC_SUCCESS &&
283 wc->status != IB_WC_WR_FLUSH_ERR)
284 ipoib_warn(priv, "failed send event "
285 "(status=%d, wrid=%d vend_err %x)\n",
286 wc->status, wr_id, wc->vendor_err);
287}
288
289static void ipoib_ib_handle_wc(struct net_device *dev, struct ib_wc *wc)
290{
291 if (wc->wr_id & IPOIB_OP_RECV)
292 ipoib_ib_handle_rx_wc(dev, wc);
293 else
294 ipoib_ib_handle_tx_wc(dev, wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
297void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
298{
299 struct net_device *dev = (struct net_device *) dev_ptr;
300 struct ipoib_dev_priv *priv = netdev_priv(dev);
301 int n, i;
302
303 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
304 do {
305 n = ib_poll_cq(cq, IPOIB_NUM_WC, priv->ibwc);
306 for (i = 0; i < n; ++i)
307 ipoib_ib_handle_wc(dev, priv->ibwc + i);
308 } while (n == IPOIB_NUM_WC);
309}
310
311static inline int post_send(struct ipoib_dev_priv *priv,
312 unsigned int wr_id,
313 struct ib_ah *address, u32 qpn,
314 dma_addr_t addr, int len)
315{
316 struct ib_send_wr *bad_wr;
317
318 priv->tx_sge.addr = addr;
319 priv->tx_sge.length = len;
320
321 priv->tx_wr.wr_id = wr_id;
322 priv->tx_wr.wr.ud.remote_qpn = qpn;
323 priv->tx_wr.wr.ud.ah = address;
324
325 return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
326}
327
328void ipoib_send(struct net_device *dev, struct sk_buff *skb,
329 struct ipoib_ah *address, u32 qpn)
330{
331 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreier1993d682005-10-28 15:30:34 -0700332 struct ipoib_tx_buf *tx_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 dma_addr_t addr;
334
Eli Cohena8bfca02006-09-22 15:22:58 -0700335 if (unlikely(skb->len > dev->mtu + INFINIBAND_ALEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
337 skb->len, dev->mtu + INFINIBAND_ALEN);
338 ++priv->stats.tx_dropped;
339 ++priv->stats.tx_errors;
340 dev_kfree_skb_any(skb);
341 return;
342 }
343
344 ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
345 skb->len, address, qpn);
346
347 /*
348 * We put the skb into the tx_ring _before_ we call post_send()
349 * because it's entirely possible that the completion handler will
350 * run before we execute anything after the post_send(). That
351 * means we have to make sure everything is properly recorded and
352 * our state is consistent before we call post_send().
353 */
Shirley Ma0f485252006-04-10 09:43:58 -0700354 tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 tx_req->skb = skb;
356 addr = dma_map_single(priv->ca->dma_device, skb->data, skb->len,
357 DMA_TO_DEVICE);
Roland Dreier73fbe8b2006-10-10 12:50:38 -0700358 if (unlikely(dma_mapping_error(addr))) {
359 ++priv->stats.tx_errors;
360 dev_kfree_skb_any(skb);
361 return;
362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 pci_unmap_addr_set(tx_req, mapping, addr);
364
Shirley Ma0f485252006-04-10 09:43:58 -0700365 if (unlikely(post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 address->ah, qpn, addr, skb->len))) {
367 ipoib_warn(priv, "post_send failed\n");
368 ++priv->stats.tx_errors;
369 dma_unmap_single(priv->ca->dma_device, addr, skb->len,
370 DMA_TO_DEVICE);
371 dev_kfree_skb_any(skb);
372 } else {
373 dev->trans_start = jiffies;
374
375 address->last_send = priv->tx_head;
376 ++priv->tx_head;
377
Shirley Ma0f485252006-04-10 09:43:58 -0700378 if (priv->tx_head - priv->tx_tail == ipoib_sendq_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
380 netif_stop_queue(dev);
381 }
382 }
383}
384
385static void __ipoib_reap_ah(struct net_device *dev)
386{
387 struct ipoib_dev_priv *priv = netdev_priv(dev);
388 struct ipoib_ah *ah, *tah;
389 LIST_HEAD(remove_list);
390
Roland Dreier31c02e22006-06-17 20:37:34 -0700391 spin_lock_irq(&priv->tx_lock);
392 spin_lock(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
Roland Dreier21818582005-07-27 14:41:32 -0700394 if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 list_del(&ah->list);
Roland Dreier31c02e22006-06-17 20:37:34 -0700396 ib_destroy_ah(ah->ah);
397 kfree(ah);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Roland Dreier31c02e22006-06-17 20:37:34 -0700399 spin_unlock(&priv->lock);
400 spin_unlock_irq(&priv->tx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
403void ipoib_reap_ah(void *dev_ptr)
404{
405 struct net_device *dev = dev_ptr;
406 struct ipoib_dev_priv *priv = netdev_priv(dev);
407
408 __ipoib_reap_ah(dev);
409
410 if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
411 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
412}
413
414int ipoib_ib_dev_open(struct net_device *dev)
415{
416 struct ipoib_dev_priv *priv = netdev_priv(dev);
417 int ret;
418
Roland Dreier5b6810e2005-10-11 11:08:24 -0700419 ret = ipoib_init_qp(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (ret) {
Roland Dreier5b6810e2005-10-11 11:08:24 -0700421 ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return -1;
423 }
424
425 ret = ipoib_ib_post_receives(dev);
426 if (ret) {
427 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
Eli Cohen54d07e22006-03-02 11:05:19 -0800428 ipoib_ib_dev_stop(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return -1;
430 }
431
432 clear_bit(IPOIB_STOP_REAPER, &priv->flags);
433 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
434
Leonid Arsh7a343d42006-03-23 19:52:51 +0200435 set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return 0;
438}
439
Leonid Arsh7a343d42006-03-23 19:52:51 +0200440static void ipoib_pkey_dev_check_presence(struct net_device *dev)
441{
442 struct ipoib_dev_priv *priv = netdev_priv(dev);
443 u16 pkey_index = 0;
444
445 if (ib_find_cached_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
446 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
447 else
448 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
449}
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451int ipoib_ib_dev_up(struct net_device *dev)
452{
453 struct ipoib_dev_priv *priv = netdev_priv(dev);
454
Leonid Arsh7a343d42006-03-23 19:52:51 +0200455 ipoib_pkey_dev_check_presence(dev);
456
457 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
458 ipoib_dbg(priv, "PKEY is not assigned.\n");
459 return 0;
460 }
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
463
464 return ipoib_mcast_start_thread(dev);
465}
466
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800467int ipoib_ib_dev_down(struct net_device *dev, int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 struct ipoib_dev_priv *priv = netdev_priv(dev);
470
471 ipoib_dbg(priv, "downing ib_dev\n");
472
473 clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
474 netif_carrier_off(dev);
475
476 /* Shutdown the P_Key thread if still active */
477 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
Ingo Molnar95ed6442006-01-13 14:51:39 -0800478 mutex_lock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 set_bit(IPOIB_PKEY_STOP, &priv->flags);
480 cancel_delayed_work(&priv->pkey_task);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800481 mutex_unlock(&pkey_mutex);
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800482 if (flush)
483 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
485
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800486 ipoib_mcast_stop_thread(dev, flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 ipoib_mcast_dev_flush(dev);
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 ipoib_flush_paths(dev);
490
491 return 0;
492}
493
494static int recvs_pending(struct net_device *dev)
495{
496 struct ipoib_dev_priv *priv = netdev_priv(dev);
497 int pending = 0;
498 int i;
499
Shirley Ma0f485252006-04-10 09:43:58 -0700500 for (i = 0; i < ipoib_recvq_size; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (priv->rx_ring[i].skb)
502 ++pending;
503
504 return pending;
505}
506
507int ipoib_ib_dev_stop(struct net_device *dev)
508{
509 struct ipoib_dev_priv *priv = netdev_priv(dev);
510 struct ib_qp_attr qp_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 unsigned long begin;
Roland Dreier1993d682005-10-28 15:30:34 -0700512 struct ipoib_tx_buf *tx_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 int i;
514
Leonid Arsh7a343d42006-03-23 19:52:51 +0200515 clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
516
Roland Dreier3bc12e72005-10-30 13:20:09 -0800517 /*
518 * Move our QP to the error state and then reinitialize in
519 * when all work requests have completed or have been flushed.
520 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 qp_attr.qp_state = IB_QPS_ERR;
Roland Dreier3bc12e72005-10-30 13:20:09 -0800522 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
524
525 /* Wait for all sends and receives to complete */
526 begin = jiffies;
527
528 while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
529 if (time_after(jiffies, begin + 5 * HZ)) {
530 ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
531 priv->tx_head - priv->tx_tail, recvs_pending(dev));
532
533 /*
534 * assume the HW is wedged and just free up
535 * all our pending work requests.
536 */
Roland Dreier21818582005-07-27 14:41:32 -0700537 while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 tx_req = &priv->tx_ring[priv->tx_tail &
Shirley Ma0f485252006-04-10 09:43:58 -0700539 (ipoib_sendq_size - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 dma_unmap_single(priv->ca->dma_device,
541 pci_unmap_addr(tx_req, mapping),
542 tx_req->skb->len,
543 DMA_TO_DEVICE);
544 dev_kfree_skb_any(tx_req->skb);
545 ++priv->tx_tail;
546 }
547
Shirley Ma0f485252006-04-10 09:43:58 -0700548 for (i = 0; i < ipoib_recvq_size; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (priv->rx_ring[i].skb) {
550 dma_unmap_single(priv->ca->dma_device,
551 pci_unmap_addr(&priv->rx_ring[i],
552 mapping),
553 IPOIB_BUF_SIZE,
554 DMA_FROM_DEVICE);
555 dev_kfree_skb_any(priv->rx_ring[i].skb);
556 priv->rx_ring[i].skb = NULL;
557 }
558
559 goto timeout;
560 }
561
562 msleep(1);
563 }
564
565 ipoib_dbg(priv, "All sends and receives done.\n");
566
567timeout:
568 qp_attr.qp_state = IB_QPS_RESET;
Roland Dreier3bc12e72005-10-30 13:20:09 -0800569 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 ipoib_warn(priv, "Failed to modify QP to RESET state\n");
571
572 /* Wait for all AHs to be reaped */
573 set_bit(IPOIB_STOP_REAPER, &priv->flags);
574 cancel_delayed_work(&priv->ah_reap_task);
575 flush_workqueue(ipoib_workqueue);
576
577 begin = jiffies;
578
579 while (!list_empty(&priv->dead_ahs)) {
580 __ipoib_reap_ah(dev);
581
582 if (time_after(jiffies, begin + HZ)) {
583 ipoib_warn(priv, "timing out; will leak address handles\n");
584 break;
585 }
586
587 msleep(1);
588 }
589
590 return 0;
591}
592
593int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
594{
595 struct ipoib_dev_priv *priv = netdev_priv(dev);
596
597 priv->ca = ca;
598 priv->port = port;
599 priv->qp = NULL;
600
601 if (ipoib_transport_dev_init(dev, ca)) {
602 printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
603 return -ENODEV;
604 }
605
606 if (dev->flags & IFF_UP) {
607 if (ipoib_ib_dev_open(dev)) {
608 ipoib_transport_dev_cleanup(dev);
609 return -ENODEV;
610 }
611 }
612
613 return 0;
614}
615
616void ipoib_ib_dev_flush(void *_dev)
617{
618 struct net_device *dev = (struct net_device *)_dev;
619 struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv;
620
Leonid Arsh7a343d42006-03-23 19:52:51 +0200621 if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags) ) {
622 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return;
Leonid Arsh7a343d42006-03-23 19:52:51 +0200624 }
625
626 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
627 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
628 return;
629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 ipoib_dbg(priv, "flushing\n");
632
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800633 ipoib_ib_dev_down(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 /*
636 * The device could have been brought down between the start and when
637 * we get here, don't bring it back up if it's not configured up
638 */
Eli Cohen5ccd0252006-09-22 15:22:56 -0700639 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 ipoib_ib_dev_up(dev);
Eli Cohen5ccd0252006-09-22 15:22:56 -0700641 ipoib_mcast_restart_task(dev);
642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Ingo Molnar95ed6442006-01-13 14:51:39 -0800644 mutex_lock(&priv->vlan_mutex);
Michael S. Tsirkin4f710552005-11-29 10:53:30 -0800645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 /* Flush any child interfaces too */
647 list_for_each_entry(cpriv, &priv->child_intfs, list)
Leonid Arsh6f633c82006-03-24 15:47:25 -0800648 ipoib_ib_dev_flush(cpriv->dev);
Michael S. Tsirkin4f710552005-11-29 10:53:30 -0800649
Ingo Molnar95ed6442006-01-13 14:51:39 -0800650 mutex_unlock(&priv->vlan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
653void ipoib_ib_dev_cleanup(struct net_device *dev)
654{
655 struct ipoib_dev_priv *priv = netdev_priv(dev);
656
657 ipoib_dbg(priv, "cleaning up ib_dev\n");
658
Roland Dreier8d2cae02005-09-20 10:52:04 -0700659 ipoib_mcast_stop_thread(dev, 1);
Eli Cohen988bd502006-01-12 14:32:20 -0800660 ipoib_mcast_dev_flush(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 ipoib_transport_dev_cleanup(dev);
663}
664
665/*
666 * Delayed P_Key Assigment Interim Support
667 *
668 * The following is initial implementation of delayed P_Key assigment
669 * mechanism. It is using the same approach implemented for the multicast
670 * group join. The single goal of this implementation is to quickly address
671 * Bug #2507. This implementation will probably be removed when the P_Key
672 * change async notification is available.
673 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675void ipoib_pkey_poll(void *dev_ptr)
676{
677 struct net_device *dev = dev_ptr;
678 struct ipoib_dev_priv *priv = netdev_priv(dev);
679
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}