blob: e3b0937011d122df60b7a38e3a5410c565733327 [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 /*
200 * If we can't allocate a new RX buffer, dump
201 * this packet and reuse the old buffer.
202 */
203 if (unlikely(ipoib_alloc_rx_skb(dev, wr_id))) {
204 ++priv->stats.rx_dropped;
205 goto repost;
206 }
207
208 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
209 wc->byte_len, wc->slid);
210
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800211 ib_dma_unmap_single(priv->ca, addr, IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700212
213 skb_put(skb, wc->byte_len);
214 skb_pull(skb, IB_GRH_BYTES);
215
216 if (wc->slid != priv->local_lid ||
217 wc->src_qp != priv->qp->qp_num) {
218 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700219 skb_reset_mac_header(skb);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700220 skb_pull(skb, IPOIB_ENCAP_LEN);
221
222 dev->last_rx = jiffies;
223 ++priv->stats.rx_packets;
224 priv->stats.rx_bytes += skb->len;
225
226 skb->dev = dev;
227 /* XXX get correct PACKET_ type here */
228 skb->pkt_type = PACKET_HOST;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700229 netif_receive_skb(skb);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700230 } else {
231 ipoib_dbg_data(priv, "dropping loopback packet\n");
232 dev_kfree_skb_any(skb);
233 }
234
235repost:
236 if (unlikely(ipoib_ib_post_receive(dev, wr_id)))
237 ipoib_warn(priv, "ipoib_ib_post_receive failed "
238 "for buf %d\n", wr_id);
239}
240
241static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 struct ipoib_dev_priv *priv = netdev_priv(dev);
244 unsigned int wr_id = wc->wr_id;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700245 struct ipoib_tx_buf *tx_req;
246 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Roland Dreiera89875f2007-04-18 20:20:53 -0700248 ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
249 wr_id, wc->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Roland Dreier2439a6e2006-09-22 15:22:52 -0700251 if (unlikely(wr_id >= ipoib_sendq_size)) {
252 ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
253 wr_id, ipoib_sendq_size);
254 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
Roland Dreier2439a6e2006-09-22 15:22:52 -0700256
257 tx_req = &priv->tx_ring[wr_id];
258
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800259 ib_dma_unmap_single(priv->ca, tx_req->mapping,
260 tx_req->skb->len, DMA_TO_DEVICE);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700261
262 ++priv->stats.tx_packets;
263 priv->stats.tx_bytes += tx_req->skb->len;
264
265 dev_kfree_skb_any(tx_req->skb);
266
267 spin_lock_irqsave(&priv->tx_lock, flags);
268 ++priv->tx_tail;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200269 if (unlikely(test_bit(IPOIB_FLAG_NETIF_STOPPED, &priv->flags)) &&
270 priv->tx_head - priv->tx_tail <= ipoib_sendq_size >> 1) {
271 clear_bit(IPOIB_FLAG_NETIF_STOPPED, &priv->flags);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700272 netif_wake_queue(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200273 }
Roland Dreier2439a6e2006-09-22 15:22:52 -0700274 spin_unlock_irqrestore(&priv->tx_lock, flags);
275
276 if (wc->status != IB_WC_SUCCESS &&
277 wc->status != IB_WC_WR_FLUSH_ERR)
278 ipoib_warn(priv, "failed send event "
279 "(status=%d, wrid=%d vend_err %x)\n",
280 wc->status, wr_id, wc->vendor_err);
281}
282
Roland Dreier8d1cc862007-05-06 21:05:32 -0700283int ipoib_poll(struct net_device *dev, int *budget)
Roland Dreier2439a6e2006-09-22 15:22:52 -0700284{
Roland Dreier8d1cc862007-05-06 21:05:32 -0700285 struct ipoib_dev_priv *priv = netdev_priv(dev);
286 int max = min(*budget, dev->quota);
287 int done;
288 int t;
289 int empty;
290 int n, i;
291
292 done = 0;
293 empty = 0;
294
295 while (max) {
296 t = min(IPOIB_NUM_WC, max);
297 n = ib_poll_cq(priv->cq, t, priv->ibwc);
298
299 for (i = 0; i < n; ++i) {
300 struct ib_wc *wc = priv->ibwc + i;
301
302 if (wc->wr_id & IPOIB_CM_OP_SRQ) {
303 ++done;
304 --max;
305 ipoib_cm_handle_rx_wc(dev, wc);
306 } else if (wc->wr_id & IPOIB_OP_RECV) {
307 ++done;
308 --max;
309 ipoib_ib_handle_rx_wc(dev, wc);
310 } else
311 ipoib_ib_handle_tx_wc(dev, wc);
312 }
313
314 if (n != t) {
315 empty = 1;
316 break;
317 }
318 }
319
320 dev->quota -= done;
321 *budget -= done;
322
323 if (empty) {
324 netif_rx_complete(dev);
325 if (unlikely(ib_req_notify_cq(priv->cq,
326 IB_CQ_NEXT_COMP |
327 IB_CQ_REPORT_MISSED_EVENTS)) &&
328 netif_rx_reschedule(dev, 0))
329 return 1;
330
331 return 0;
332 }
333
334 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
338{
Roland Dreier8d1cc862007-05-06 21:05:32 -0700339 netif_rx_schedule(dev_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
342static inline int post_send(struct ipoib_dev_priv *priv,
343 unsigned int wr_id,
344 struct ib_ah *address, u32 qpn,
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800345 u64 addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 struct ib_send_wr *bad_wr;
348
349 priv->tx_sge.addr = addr;
350 priv->tx_sge.length = len;
351
352 priv->tx_wr.wr_id = wr_id;
353 priv->tx_wr.wr.ud.remote_qpn = qpn;
354 priv->tx_wr.wr.ud.ah = address;
355
356 return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
357}
358
359void ipoib_send(struct net_device *dev, struct sk_buff *skb,
360 struct ipoib_ah *address, u32 qpn)
361{
362 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreier1993d682005-10-28 15:30:34 -0700363 struct ipoib_tx_buf *tx_req;
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800364 u64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Michael S. Tsirkin77d8e1e2007-03-21 15:45:05 +0200366 if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
Michael S. Tsirkin77d8e1e2007-03-21 15:45:05 +0200368 skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 ++priv->stats.tx_dropped;
370 ++priv->stats.tx_errors;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200371 ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return;
373 }
374
375 ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
376 skb->len, address, qpn);
377
378 /*
379 * We put the skb into the tx_ring _before_ we call post_send()
380 * because it's entirely possible that the completion handler will
381 * run before we execute anything after the post_send(). That
382 * means we have to make sure everything is properly recorded and
383 * our state is consistent before we call post_send().
384 */
Shirley Ma0f485252006-04-10 09:43:58 -0700385 tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 tx_req->skb = skb;
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800387 addr = ib_dma_map_single(priv->ca, skb->data, skb->len,
388 DMA_TO_DEVICE);
389 if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
Roland Dreier73fbe8b2006-10-10 12:50:38 -0700390 ++priv->stats.tx_errors;
391 dev_kfree_skb_any(skb);
392 return;
393 }
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800394 tx_req->mapping = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Shirley Ma0f485252006-04-10 09:43:58 -0700396 if (unlikely(post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 address->ah, qpn, addr, skb->len))) {
398 ipoib_warn(priv, "post_send failed\n");
399 ++priv->stats.tx_errors;
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800400 ib_dma_unmap_single(priv->ca, addr, skb->len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 dev_kfree_skb_any(skb);
402 } else {
403 dev->trans_start = jiffies;
404
405 address->last_send = priv->tx_head;
406 ++priv->tx_head;
407
Shirley Ma0f485252006-04-10 09:43:58 -0700408 if (priv->tx_head - priv->tx_tail == ipoib_sendq_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
410 netif_stop_queue(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200411 set_bit(IPOIB_FLAG_NETIF_STOPPED, &priv->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413 }
414}
415
416static void __ipoib_reap_ah(struct net_device *dev)
417{
418 struct ipoib_dev_priv *priv = netdev_priv(dev);
419 struct ipoib_ah *ah, *tah;
420 LIST_HEAD(remove_list);
421
Roland Dreier31c02e22006-06-17 20:37:34 -0700422 spin_lock_irq(&priv->tx_lock);
423 spin_lock(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
Roland Dreier21818582005-07-27 14:41:32 -0700425 if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 list_del(&ah->list);
Roland Dreier31c02e22006-06-17 20:37:34 -0700427 ib_destroy_ah(ah->ah);
428 kfree(ah);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
Roland Dreier31c02e22006-06-17 20:37:34 -0700430 spin_unlock(&priv->lock);
431 spin_unlock_irq(&priv->tx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
David Howellsc4028952006-11-22 14:57:56 +0000434void ipoib_reap_ah(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
David Howellsc4028952006-11-22 14:57:56 +0000436 struct ipoib_dev_priv *priv =
437 container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
438 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 __ipoib_reap_ah(dev);
441
442 if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
443 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
444}
445
446int ipoib_ib_dev_open(struct net_device *dev)
447{
448 struct ipoib_dev_priv *priv = netdev_priv(dev);
449 int ret;
450
Yosef Etigin26bbf132007-05-19 08:51:54 -0700451 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {
452 ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);
453 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
454 return -1;
455 }
456 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
457
Roland Dreier5b6810e2005-10-11 11:08:24 -0700458 ret = ipoib_init_qp(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (ret) {
Roland Dreier5b6810e2005-10-11 11:08:24 -0700460 ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return -1;
462 }
463
464 ret = ipoib_ib_post_receives(dev);
465 if (ret) {
466 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700467 ipoib_ib_dev_stop(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return -1;
469 }
470
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200471 ret = ipoib_cm_dev_open(dev);
472 if (ret) {
473 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700474 ipoib_ib_dev_stop(dev, 1);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200475 return -1;
476 }
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 clear_bit(IPOIB_STOP_REAPER, &priv->flags);
479 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
480
Leonid Arsh7a343d42006-03-23 19:52:51 +0200481 set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return 0;
484}
485
Leonid Arsh7a343d42006-03-23 19:52:51 +0200486static void ipoib_pkey_dev_check_presence(struct net_device *dev)
487{
488 struct ipoib_dev_priv *priv = netdev_priv(dev);
489 u16 pkey_index = 0;
490
491 if (ib_find_cached_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
492 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
493 else
494 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
495}
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497int ipoib_ib_dev_up(struct net_device *dev)
498{
499 struct ipoib_dev_priv *priv = netdev_priv(dev);
500
Leonid Arsh7a343d42006-03-23 19:52:51 +0200501 ipoib_pkey_dev_check_presence(dev);
502
503 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
504 ipoib_dbg(priv, "PKEY is not assigned.\n");
505 return 0;
506 }
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
509
510 return ipoib_mcast_start_thread(dev);
511}
512
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800513int ipoib_ib_dev_down(struct net_device *dev, int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 struct ipoib_dev_priv *priv = netdev_priv(dev);
516
517 ipoib_dbg(priv, "downing ib_dev\n");
518
519 clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
520 netif_carrier_off(dev);
521
522 /* Shutdown the P_Key thread if still active */
523 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
Ingo Molnar95ed6442006-01-13 14:51:39 -0800524 mutex_lock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 set_bit(IPOIB_PKEY_STOP, &priv->flags);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700526 cancel_delayed_work(&priv->pkey_poll_task);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800527 mutex_unlock(&pkey_mutex);
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800528 if (flush)
529 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800532 ipoib_mcast_stop_thread(dev, flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 ipoib_mcast_dev_flush(dev);
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 ipoib_flush_paths(dev);
536
537 return 0;
538}
539
540static int recvs_pending(struct net_device *dev)
541{
542 struct ipoib_dev_priv *priv = netdev_priv(dev);
543 int pending = 0;
544 int i;
545
Shirley Ma0f485252006-04-10 09:43:58 -0700546 for (i = 0; i < ipoib_recvq_size; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (priv->rx_ring[i].skb)
548 ++pending;
549
550 return pending;
551}
552
Yosef Etigin26bbf132007-05-19 08:51:54 -0700553int ipoib_ib_dev_stop(struct net_device *dev, int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 struct ipoib_dev_priv *priv = netdev_priv(dev);
556 struct ib_qp_attr qp_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 unsigned long begin;
Roland Dreier1993d682005-10-28 15:30:34 -0700558 struct ipoib_tx_buf *tx_req;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700559 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Leonid Arsh7a343d42006-03-23 19:52:51 +0200561 clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
Roland Dreier8d1cc862007-05-06 21:05:32 -0700562 netif_poll_disable(dev);
Leonid Arsh7a343d42006-03-23 19:52:51 +0200563
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200564 ipoib_cm_dev_stop(dev);
565
Roland Dreier3bc12e72005-10-30 13:20:09 -0800566 /*
567 * Move our QP to the error state and then reinitialize in
568 * when all work requests have completed or have been flushed.
569 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 qp_attr.qp_state = IB_QPS_ERR;
Roland Dreier3bc12e72005-10-30 13:20:09 -0800571 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
573
574 /* Wait for all sends and receives to complete */
575 begin = jiffies;
576
577 while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
578 if (time_after(jiffies, begin + 5 * HZ)) {
579 ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
580 priv->tx_head - priv->tx_tail, recvs_pending(dev));
581
582 /*
583 * assume the HW is wedged and just free up
584 * all our pending work requests.
585 */
Roland Dreier21818582005-07-27 14:41:32 -0700586 while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 tx_req = &priv->tx_ring[priv->tx_tail &
Shirley Ma0f485252006-04-10 09:43:58 -0700588 (ipoib_sendq_size - 1)];
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800589 ib_dma_unmap_single(priv->ca,
590 tx_req->mapping,
591 tx_req->skb->len,
592 DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 dev_kfree_skb_any(tx_req->skb);
594 ++priv->tx_tail;
595 }
596
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800597 for (i = 0; i < ipoib_recvq_size; ++i) {
598 struct ipoib_rx_buf *rx_req;
599
600 rx_req = &priv->rx_ring[i];
601 if (!rx_req->skb)
602 continue;
603 ib_dma_unmap_single(priv->ca,
604 rx_req->mapping,
605 IPOIB_BUF_SIZE,
606 DMA_FROM_DEVICE);
607 dev_kfree_skb_any(rx_req->skb);
608 rx_req->skb = NULL;
609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 goto timeout;
612 }
613
Roland Dreier8d1cc862007-05-06 21:05:32 -0700614 do {
615 n = ib_poll_cq(priv->cq, IPOIB_NUM_WC, priv->ibwc);
616 for (i = 0; i < n; ++i) {
617 if (priv->ibwc[i].wr_id & IPOIB_CM_OP_SRQ)
618 ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
619 else if (priv->ibwc[i].wr_id & IPOIB_OP_RECV)
620 ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
621 else
622 ipoib_ib_handle_tx_wc(dev, priv->ibwc + i);
623 }
624 } while (n == IPOIB_NUM_WC);
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 msleep(1);
627 }
628
629 ipoib_dbg(priv, "All sends and receives done.\n");
630
631timeout:
632 qp_attr.qp_state = IB_QPS_RESET;
Roland Dreier3bc12e72005-10-30 13:20:09 -0800633 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 ipoib_warn(priv, "Failed to modify QP to RESET state\n");
635
636 /* Wait for all AHs to be reaped */
637 set_bit(IPOIB_STOP_REAPER, &priv->flags);
638 cancel_delayed_work(&priv->ah_reap_task);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700639 if (flush)
640 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 begin = jiffies;
643
644 while (!list_empty(&priv->dead_ahs)) {
645 __ipoib_reap_ah(dev);
646
647 if (time_after(jiffies, begin + HZ)) {
648 ipoib_warn(priv, "timing out; will leak address handles\n");
649 break;
650 }
651
652 msleep(1);
653 }
654
Roland Dreier8d1cc862007-05-06 21:05:32 -0700655 netif_poll_enable(dev);
656 ib_req_notify_cq(priv->cq, IB_CQ_NEXT_COMP);
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return 0;
659}
660
661int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
662{
663 struct ipoib_dev_priv *priv = netdev_priv(dev);
664
665 priv->ca = ca;
666 priv->port = port;
667 priv->qp = NULL;
668
669 if (ipoib_transport_dev_init(dev, ca)) {
670 printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
671 return -ENODEV;
672 }
673
674 if (dev->flags & IFF_UP) {
675 if (ipoib_ib_dev_open(dev)) {
676 ipoib_transport_dev_cleanup(dev);
677 return -ENODEV;
678 }
679 }
680
681 return 0;
682}
683
Yosef Etigin26bbf132007-05-19 08:51:54 -0700684static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv, int pkey_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Yosef Etigin26bbf132007-05-19 08:51:54 -0700686 struct ipoib_dev_priv *cpriv;
David Howellsc4028952006-11-22 14:57:56 +0000687 struct net_device *dev = priv->dev;
Yosef Etigin26bbf132007-05-19 08:51:54 -0700688 u16 new_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Yosef Etigin26bbf132007-05-19 08:51:54 -0700690 mutex_lock(&priv->vlan_mutex);
691
692 /*
693 * Flush any child interfaces too -- they might be up even if
694 * the parent is down.
695 */
696 list_for_each_entry(cpriv, &priv->child_intfs, list)
697 __ipoib_ib_dev_flush(cpriv, pkey_event);
698
699 mutex_unlock(&priv->vlan_mutex);
700
701 if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
Leonid Arsh7a343d42006-03-23 19:52:51 +0200702 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return;
Leonid Arsh7a343d42006-03-23 19:52:51 +0200704 }
705
706 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
707 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
708 return;
709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Yosef Etigin26bbf132007-05-19 08:51:54 -0700711 if (pkey_event) {
712 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
713 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
714 ipoib_ib_dev_down(dev, 0);
715 ipoib_pkey_dev_delay_open(dev);
716 return;
717 }
718 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
719
720 /* restart QP only if P_Key index is changed */
721 if (new_index == priv->pkey_index) {
722 ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
723 return;
724 }
725 priv->pkey_index = new_index;
726 }
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 ipoib_dbg(priv, "flushing\n");
729
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800730 ipoib_ib_dev_down(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Yosef Etigin26bbf132007-05-19 08:51:54 -0700732 if (pkey_event) {
733 ipoib_ib_dev_stop(dev, 0);
734 ipoib_ib_dev_open(dev);
735 }
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 /*
738 * The device could have been brought down between the start and when
739 * we get here, don't bring it back up if it's not configured up
740 */
Eli Cohen5ccd0252006-09-22 15:22:56 -0700741 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 ipoib_ib_dev_up(dev);
David Howellsc4028952006-11-22 14:57:56 +0000743 ipoib_mcast_restart_task(&priv->restart_task);
Eli Cohen5ccd0252006-09-22 15:22:56 -0700744 }
Yosef Etigin26bbf132007-05-19 08:51:54 -0700745}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Yosef Etigin26bbf132007-05-19 08:51:54 -0700747void ipoib_ib_dev_flush(struct work_struct *work)
748{
749 struct ipoib_dev_priv *priv =
750 container_of(work, struct ipoib_dev_priv, flush_task);
Michael S. Tsirkin4f710552005-11-29 10:53:30 -0800751
Yosef Etigin26bbf132007-05-19 08:51:54 -0700752 ipoib_dbg(priv, "Flushing %s\n", priv->dev->name);
753 __ipoib_ib_dev_flush(priv, 0);
754}
Michael S. Tsirkin4f710552005-11-29 10:53:30 -0800755
Yosef Etigin26bbf132007-05-19 08:51:54 -0700756void ipoib_pkey_event(struct work_struct *work)
757{
758 struct ipoib_dev_priv *priv =
759 container_of(work, struct ipoib_dev_priv, pkey_event_task);
760
761 ipoib_dbg(priv, "Flushing %s and restarting its QP\n", priv->dev->name);
762 __ipoib_ib_dev_flush(priv, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
765void ipoib_ib_dev_cleanup(struct net_device *dev)
766{
767 struct ipoib_dev_priv *priv = netdev_priv(dev);
768
769 ipoib_dbg(priv, "cleaning up ib_dev\n");
770
Roland Dreier8d2cae02005-09-20 10:52:04 -0700771 ipoib_mcast_stop_thread(dev, 1);
Eli Cohen988bd502006-01-12 14:32:20 -0800772 ipoib_mcast_dev_flush(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 ipoib_transport_dev_cleanup(dev);
775}
776
777/*
778 * Delayed P_Key Assigment Interim Support
779 *
780 * The following is initial implementation of delayed P_Key assigment
781 * mechanism. It is using the same approach implemented for the multicast
782 * group join. The single goal of this implementation is to quickly address
783 * Bug #2507. This implementation will probably be removed when the P_Key
784 * change async notification is available.
785 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
David Howellsc4028952006-11-22 14:57:56 +0000787void ipoib_pkey_poll(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
David Howellsc4028952006-11-22 14:57:56 +0000789 struct ipoib_dev_priv *priv =
Yosef Etigin26bbf132007-05-19 08:51:54 -0700790 container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
David Howellsc4028952006-11-22 14:57:56 +0000791 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 ipoib_pkey_dev_check_presence(dev);
794
795 if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
796 ipoib_open(dev);
797 else {
Ingo Molnar95ed6442006-01-13 14:51:39 -0800798 mutex_lock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
800 queue_delayed_work(ipoib_workqueue,
Yosef Etigin26bbf132007-05-19 08:51:54 -0700801 &priv->pkey_poll_task,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800803 mutex_unlock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805}
806
807int ipoib_pkey_dev_delay_open(struct net_device *dev)
808{
809 struct ipoib_dev_priv *priv = netdev_priv(dev);
810
811 /* Look for the interface pkey value in the IB Port P_Key table and */
812 /* set the interface pkey assigment flag */
813 ipoib_pkey_dev_check_presence(dev);
814
815 /* P_Key value not assigned yet - start polling */
816 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
Ingo Molnar95ed6442006-01-13 14:51:39 -0800817 mutex_lock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 clear_bit(IPOIB_PKEY_STOP, &priv->flags);
819 queue_delayed_work(ipoib_workqueue,
Yosef Etigin26bbf132007-05-19 08:51:54 -0700820 &priv->pkey_poll_task,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800822 mutex_unlock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return 1;
824 }
825
826 return 0;
827}