blob: 10944888cffd79de3c8b4751ceac2929a0296141 [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))) {
211 ++priv->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
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;
228 ++priv->stats.rx_packets;
229 priv->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;
234 netif_receive_skb(skb);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700235
236repost:
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
242static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 struct ipoib_dev_priv *priv = netdev_priv(dev);
245 unsigned int wr_id = wc->wr_id;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700246 struct ipoib_tx_buf *tx_req;
247 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Roland Dreiera89875f2007-04-18 20:20:53 -0700249 ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
250 wr_id, wc->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Roland Dreier2439a6e2006-09-22 15:22:52 -0700252 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
Roland Dreier2439a6e2006-09-22 15:22:52 -0700257
258 tx_req = &priv->tx_ring[wr_id];
259
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800260 ib_dma_unmap_single(priv->ca, tx_req->mapping,
261 tx_req->skb->len, DMA_TO_DEVICE);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700262
263 ++priv->stats.tx_packets;
264 priv->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;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200270 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);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700273 netif_wake_queue(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200274 }
Roland Dreier2439a6e2006-09-22 15:22:52 -0700275 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
Roland Dreier8d1cc862007-05-06 21:05:32 -0700284int ipoib_poll(struct net_device *dev, int *budget)
Roland Dreier2439a6e2006-09-22 15:22:52 -0700285{
Roland Dreier8d1cc862007-05-06 21:05:32 -0700286 struct ipoib_dev_priv *priv = netdev_priv(dev);
287 int max = min(*budget, dev->quota);
288 int done;
289 int t;
290 int empty;
291 int n, i;
292
293 done = 0;
294 empty = 0;
295
296 while (max) {
297 t = min(IPOIB_NUM_WC, max);
298 n = ib_poll_cq(priv->cq, t, priv->ibwc);
299
300 for (i = 0; i < n; ++i) {
301 struct ib_wc *wc = priv->ibwc + i;
302
303 if (wc->wr_id & IPOIB_CM_OP_SRQ) {
304 ++done;
305 --max;
306 ipoib_cm_handle_rx_wc(dev, wc);
307 } else if (wc->wr_id & IPOIB_OP_RECV) {
308 ++done;
309 --max;
310 ipoib_ib_handle_rx_wc(dev, wc);
311 } else
312 ipoib_ib_handle_tx_wc(dev, wc);
313 }
314
315 if (n != t) {
316 empty = 1;
317 break;
318 }
319 }
320
321 dev->quota -= done;
322 *budget -= done;
323
324 if (empty) {
325 netif_rx_complete(dev);
326 if (unlikely(ib_req_notify_cq(priv->cq,
327 IB_CQ_NEXT_COMP |
328 IB_CQ_REPORT_MISSED_EVENTS)) &&
329 netif_rx_reschedule(dev, 0))
330 return 1;
331
332 return 0;
333 }
334
335 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
338void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
339{
Roland Dreier8d1cc862007-05-06 21:05:32 -0700340 netif_rx_schedule(dev_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343static inline int post_send(struct ipoib_dev_priv *priv,
344 unsigned int wr_id,
345 struct ib_ah *address, u32 qpn,
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800346 u64 addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 struct ib_send_wr *bad_wr;
349
350 priv->tx_sge.addr = addr;
351 priv->tx_sge.length = len;
352
353 priv->tx_wr.wr_id = wr_id;
354 priv->tx_wr.wr.ud.remote_qpn = qpn;
355 priv->tx_wr.wr.ud.ah = address;
356
357 return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
358}
359
360void ipoib_send(struct net_device *dev, struct sk_buff *skb,
361 struct ipoib_ah *address, u32 qpn)
362{
363 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreier1993d682005-10-28 15:30:34 -0700364 struct ipoib_tx_buf *tx_req;
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800365 u64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Michael S. Tsirkin77d8e1ef2007-03-21 15:45:05 +0200367 if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
Michael S. Tsirkin77d8e1ef2007-03-21 15:45:05 +0200369 skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 ++priv->stats.tx_dropped;
371 ++priv->stats.tx_errors;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200372 ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return;
374 }
375
376 ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
377 skb->len, address, qpn);
378
379 /*
380 * We put the skb into the tx_ring _before_ we call post_send()
381 * because it's entirely possible that the completion handler will
382 * run before we execute anything after the post_send(). That
383 * means we have to make sure everything is properly recorded and
384 * our state is consistent before we call post_send().
385 */
Shirley Ma0f485252006-04-10 09:43:58 -0700386 tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 tx_req->skb = skb;
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800388 addr = ib_dma_map_single(priv->ca, skb->data, skb->len,
389 DMA_TO_DEVICE);
390 if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
Roland Dreier73fbe8b2006-10-10 12:50:38 -0700391 ++priv->stats.tx_errors;
392 dev_kfree_skb_any(skb);
393 return;
394 }
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800395 tx_req->mapping = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Shirley Ma0f485252006-04-10 09:43:58 -0700397 if (unlikely(post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 address->ah, qpn, addr, skb->len))) {
399 ipoib_warn(priv, "post_send failed\n");
400 ++priv->stats.tx_errors;
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800401 ib_dma_unmap_single(priv->ca, addr, skb->len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 dev_kfree_skb_any(skb);
403 } else {
404 dev->trans_start = jiffies;
405
406 address->last_send = priv->tx_head;
407 ++priv->tx_head;
408
Shirley Ma0f485252006-04-10 09:43:58 -0700409 if (priv->tx_head - priv->tx_tail == ipoib_sendq_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
411 netif_stop_queue(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200412 set_bit(IPOIB_FLAG_NETIF_STOPPED, &priv->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414 }
415}
416
417static void __ipoib_reap_ah(struct net_device *dev)
418{
419 struct ipoib_dev_priv *priv = netdev_priv(dev);
420 struct ipoib_ah *ah, *tah;
421 LIST_HEAD(remove_list);
422
Roland Dreier31c02e22006-06-17 20:37:34 -0700423 spin_lock_irq(&priv->tx_lock);
424 spin_lock(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
Roland Dreier21818582005-07-27 14:41:32 -0700426 if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 list_del(&ah->list);
Roland Dreier31c02e22006-06-17 20:37:34 -0700428 ib_destroy_ah(ah->ah);
429 kfree(ah);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
Roland Dreier31c02e22006-06-17 20:37:34 -0700431 spin_unlock(&priv->lock);
432 spin_unlock_irq(&priv->tx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
David Howellsc4028952006-11-22 14:57:56 +0000435void ipoib_reap_ah(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
David Howellsc4028952006-11-22 14:57:56 +0000437 struct ipoib_dev_priv *priv =
438 container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
439 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 __ipoib_reap_ah(dev);
442
443 if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
444 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
445}
446
447int ipoib_ib_dev_open(struct net_device *dev)
448{
449 struct ipoib_dev_priv *priv = netdev_priv(dev);
450 int ret;
451
Yosef Etigin26bbf132007-05-19 08:51:54 -0700452 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {
453 ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);
454 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
455 return -1;
456 }
457 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
458
Roland Dreier5b6810e2005-10-11 11:08:24 -0700459 ret = ipoib_init_qp(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (ret) {
Roland Dreier5b6810e2005-10-11 11:08:24 -0700461 ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return -1;
463 }
464
465 ret = ipoib_ib_post_receives(dev);
466 if (ret) {
467 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700468 ipoib_ib_dev_stop(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return -1;
470 }
471
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200472 ret = ipoib_cm_dev_open(dev);
473 if (ret) {
Michael S. Tsirkin24bd1e42007-05-18 16:12:54 +0300474 ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700475 ipoib_ib_dev_stop(dev, 1);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200476 return -1;
477 }
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 clear_bit(IPOIB_STOP_REAPER, &priv->flags);
480 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
481
Leonid Arsh7a343d42006-03-23 19:52:51 +0200482 set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return 0;
485}
486
Leonid Arsh7a343d42006-03-23 19:52:51 +0200487static void ipoib_pkey_dev_check_presence(struct net_device *dev)
488{
489 struct ipoib_dev_priv *priv = netdev_priv(dev);
490 u16 pkey_index = 0;
491
492 if (ib_find_cached_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
493 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
494 else
495 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
496}
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498int ipoib_ib_dev_up(struct net_device *dev)
499{
500 struct ipoib_dev_priv *priv = netdev_priv(dev);
501
Leonid Arsh7a343d42006-03-23 19:52:51 +0200502 ipoib_pkey_dev_check_presence(dev);
503
504 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
505 ipoib_dbg(priv, "PKEY is not assigned.\n");
506 return 0;
507 }
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
510
511 return ipoib_mcast_start_thread(dev);
512}
513
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800514int ipoib_ib_dev_down(struct net_device *dev, int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 struct ipoib_dev_priv *priv = netdev_priv(dev);
517
518 ipoib_dbg(priv, "downing ib_dev\n");
519
520 clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
521 netif_carrier_off(dev);
522
523 /* Shutdown the P_Key thread if still active */
524 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
Ingo Molnar95ed6442006-01-13 14:51:39 -0800525 mutex_lock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 set_bit(IPOIB_PKEY_STOP, &priv->flags);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700527 cancel_delayed_work(&priv->pkey_poll_task);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800528 mutex_unlock(&pkey_mutex);
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800529 if (flush)
530 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800533 ipoib_mcast_stop_thread(dev, flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 ipoib_mcast_dev_flush(dev);
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 ipoib_flush_paths(dev);
537
538 return 0;
539}
540
541static int recvs_pending(struct net_device *dev)
542{
543 struct ipoib_dev_priv *priv = netdev_priv(dev);
544 int pending = 0;
545 int i;
546
Shirley Ma0f485252006-04-10 09:43:58 -0700547 for (i = 0; i < ipoib_recvq_size; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (priv->rx_ring[i].skb)
549 ++pending;
550
551 return pending;
552}
553
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300554void ipoib_drain_cq(struct net_device *dev)
555{
556 struct ipoib_dev_priv *priv = netdev_priv(dev);
557 int i, n;
558 do {
559 n = ib_poll_cq(priv->cq, IPOIB_NUM_WC, priv->ibwc);
560 for (i = 0; i < n; ++i) {
561 if (priv->ibwc[i].wr_id & IPOIB_CM_OP_SRQ)
562 ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
563 else if (priv->ibwc[i].wr_id & IPOIB_OP_RECV)
564 ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
565 else
566 ipoib_ib_handle_tx_wc(dev, priv->ibwc + i);
567 }
568 } while (n == IPOIB_NUM_WC);
569}
570
Yosef Etigin26bbf132007-05-19 08:51:54 -0700571int ipoib_ib_dev_stop(struct net_device *dev, int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
573 struct ipoib_dev_priv *priv = netdev_priv(dev);
574 struct ib_qp_attr qp_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 unsigned long begin;
Roland Dreier1993d682005-10-28 15:30:34 -0700576 struct ipoib_tx_buf *tx_req;
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300577 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Leonid Arsh7a343d42006-03-23 19:52:51 +0200579 clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
Roland Dreier8d1cc862007-05-06 21:05:32 -0700580 netif_poll_disable(dev);
Leonid Arsh7a343d42006-03-23 19:52:51 +0200581
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200582 ipoib_cm_dev_stop(dev);
583
Roland Dreier3bc12e72005-10-30 13:20:09 -0800584 /*
585 * Move our QP to the error state and then reinitialize in
586 * when all work requests have completed or have been flushed.
587 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 qp_attr.qp_state = IB_QPS_ERR;
Roland Dreier3bc12e72005-10-30 13:20:09 -0800589 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
591
592 /* Wait for all sends and receives to complete */
593 begin = jiffies;
594
595 while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
596 if (time_after(jiffies, begin + 5 * HZ)) {
597 ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
598 priv->tx_head - priv->tx_tail, recvs_pending(dev));
599
600 /*
601 * assume the HW is wedged and just free up
602 * all our pending work requests.
603 */
Roland Dreier21818582005-07-27 14:41:32 -0700604 while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 tx_req = &priv->tx_ring[priv->tx_tail &
Shirley Ma0f485252006-04-10 09:43:58 -0700606 (ipoib_sendq_size - 1)];
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800607 ib_dma_unmap_single(priv->ca,
608 tx_req->mapping,
609 tx_req->skb->len,
610 DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 dev_kfree_skb_any(tx_req->skb);
612 ++priv->tx_tail;
613 }
614
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800615 for (i = 0; i < ipoib_recvq_size; ++i) {
616 struct ipoib_rx_buf *rx_req;
617
618 rx_req = &priv->rx_ring[i];
619 if (!rx_req->skb)
620 continue;
621 ib_dma_unmap_single(priv->ca,
622 rx_req->mapping,
623 IPOIB_BUF_SIZE,
624 DMA_FROM_DEVICE);
625 dev_kfree_skb_any(rx_req->skb);
626 rx_req->skb = NULL;
627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 goto timeout;
630 }
631
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300632 ipoib_drain_cq(dev);
Roland Dreier8d1cc862007-05-06 21:05:32 -0700633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 msleep(1);
635 }
636
637 ipoib_dbg(priv, "All sends and receives done.\n");
638
639timeout:
640 qp_attr.qp_state = IB_QPS_RESET;
Roland Dreier3bc12e72005-10-30 13:20:09 -0800641 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 ipoib_warn(priv, "Failed to modify QP to RESET state\n");
643
644 /* Wait for all AHs to be reaped */
645 set_bit(IPOIB_STOP_REAPER, &priv->flags);
646 cancel_delayed_work(&priv->ah_reap_task);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700647 if (flush)
648 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 begin = jiffies;
651
652 while (!list_empty(&priv->dead_ahs)) {
653 __ipoib_reap_ah(dev);
654
655 if (time_after(jiffies, begin + HZ)) {
656 ipoib_warn(priv, "timing out; will leak address handles\n");
657 break;
658 }
659
660 msleep(1);
661 }
662
Roland Dreier8d1cc862007-05-06 21:05:32 -0700663 netif_poll_enable(dev);
664 ib_req_notify_cq(priv->cq, IB_CQ_NEXT_COMP);
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return 0;
667}
668
669int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
670{
671 struct ipoib_dev_priv *priv = netdev_priv(dev);
672
673 priv->ca = ca;
674 priv->port = port;
675 priv->qp = NULL;
676
677 if (ipoib_transport_dev_init(dev, ca)) {
678 printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
679 return -ENODEV;
680 }
681
682 if (dev->flags & IFF_UP) {
683 if (ipoib_ib_dev_open(dev)) {
684 ipoib_transport_dev_cleanup(dev);
685 return -ENODEV;
686 }
687 }
688
689 return 0;
690}
691
Yosef Etigin26bbf132007-05-19 08:51:54 -0700692static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv, int pkey_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Yosef Etigin26bbf132007-05-19 08:51:54 -0700694 struct ipoib_dev_priv *cpriv;
David Howellsc4028952006-11-22 14:57:56 +0000695 struct net_device *dev = priv->dev;
Yosef Etigin26bbf132007-05-19 08:51:54 -0700696 u16 new_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Yosef Etigin26bbf132007-05-19 08:51:54 -0700698 mutex_lock(&priv->vlan_mutex);
699
700 /*
701 * Flush any child interfaces too -- they might be up even if
702 * the parent is down.
703 */
704 list_for_each_entry(cpriv, &priv->child_intfs, list)
705 __ipoib_ib_dev_flush(cpriv, pkey_event);
706
707 mutex_unlock(&priv->vlan_mutex);
708
709 if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
Leonid Arsh7a343d42006-03-23 19:52:51 +0200710 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return;
Leonid Arsh7a343d42006-03-23 19:52:51 +0200712 }
713
714 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
715 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
716 return;
717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Yosef Etigin26bbf132007-05-19 08:51:54 -0700719 if (pkey_event) {
720 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
721 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
722 ipoib_ib_dev_down(dev, 0);
723 ipoib_pkey_dev_delay_open(dev);
724 return;
725 }
726 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
727
728 /* restart QP only if P_Key index is changed */
729 if (new_index == priv->pkey_index) {
730 ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
731 return;
732 }
733 priv->pkey_index = new_index;
734 }
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 ipoib_dbg(priv, "flushing\n");
737
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800738 ipoib_ib_dev_down(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Yosef Etigin26bbf132007-05-19 08:51:54 -0700740 if (pkey_event) {
741 ipoib_ib_dev_stop(dev, 0);
742 ipoib_ib_dev_open(dev);
743 }
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 /*
746 * The device could have been brought down between the start and when
747 * we get here, don't bring it back up if it's not configured up
748 */
Eli Cohen5ccd0252006-09-22 15:22:56 -0700749 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 ipoib_ib_dev_up(dev);
David Howellsc4028952006-11-22 14:57:56 +0000751 ipoib_mcast_restart_task(&priv->restart_task);
Eli Cohen5ccd0252006-09-22 15:22:56 -0700752 }
Yosef Etigin26bbf132007-05-19 08:51:54 -0700753}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Yosef Etigin26bbf132007-05-19 08:51:54 -0700755void ipoib_ib_dev_flush(struct work_struct *work)
756{
757 struct ipoib_dev_priv *priv =
758 container_of(work, struct ipoib_dev_priv, flush_task);
Michael S. Tsirkin4f710552005-11-29 10:53:30 -0800759
Yosef Etigin26bbf132007-05-19 08:51:54 -0700760 ipoib_dbg(priv, "Flushing %s\n", priv->dev->name);
761 __ipoib_ib_dev_flush(priv, 0);
762}
Michael S. Tsirkin4f710552005-11-29 10:53:30 -0800763
Yosef Etigin26bbf132007-05-19 08:51:54 -0700764void ipoib_pkey_event(struct work_struct *work)
765{
766 struct ipoib_dev_priv *priv =
767 container_of(work, struct ipoib_dev_priv, pkey_event_task);
768
769 ipoib_dbg(priv, "Flushing %s and restarting its QP\n", priv->dev->name);
770 __ipoib_ib_dev_flush(priv, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
773void ipoib_ib_dev_cleanup(struct net_device *dev)
774{
775 struct ipoib_dev_priv *priv = netdev_priv(dev);
776
777 ipoib_dbg(priv, "cleaning up ib_dev\n");
778
Roland Dreier8d2cae02005-09-20 10:52:04 -0700779 ipoib_mcast_stop_thread(dev, 1);
Eli Cohen988bd502006-01-12 14:32:20 -0800780 ipoib_mcast_dev_flush(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 ipoib_transport_dev_cleanup(dev);
783}
784
785/*
786 * Delayed P_Key Assigment Interim Support
787 *
788 * The following is initial implementation of delayed P_Key assigment
789 * mechanism. It is using the same approach implemented for the multicast
790 * group join. The single goal of this implementation is to quickly address
791 * Bug #2507. This implementation will probably be removed when the P_Key
792 * change async notification is available.
793 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
David Howellsc4028952006-11-22 14:57:56 +0000795void ipoib_pkey_poll(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
David Howellsc4028952006-11-22 14:57:56 +0000797 struct ipoib_dev_priv *priv =
Yosef Etigin26bbf132007-05-19 08:51:54 -0700798 container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
David Howellsc4028952006-11-22 14:57:56 +0000799 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 ipoib_pkey_dev_check_presence(dev);
802
803 if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
804 ipoib_open(dev);
805 else {
Ingo Molnar95ed6442006-01-13 14:51:39 -0800806 mutex_lock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
808 queue_delayed_work(ipoib_workqueue,
Yosef Etigin26bbf132007-05-19 08:51:54 -0700809 &priv->pkey_poll_task,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800811 mutex_unlock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813}
814
815int ipoib_pkey_dev_delay_open(struct net_device *dev)
816{
817 struct ipoib_dev_priv *priv = netdev_priv(dev);
818
819 /* Look for the interface pkey value in the IB Port P_Key table and */
820 /* set the interface pkey assigment flag */
821 ipoib_pkey_dev_check_presence(dev);
822
823 /* P_Key value not assigned yet - start polling */
824 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
Ingo Molnar95ed6442006-01-13 14:51:39 -0800825 mutex_lock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 clear_bit(IPOIB_PKEY_STOP, &priv->flags);
827 queue_delayed_work(ipoib_workqueue,
Yosef Etigin26bbf132007-05-19 08:51:54 -0700828 &priv->pkey_poll_task,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800830 mutex_unlock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return 1;
832 }
833
834 return 0;
835}