Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved. |
Roland Dreier | 2a1d9b7 | 2005-08-10 23:03:10 -0700 | [diff] [blame] | 3 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 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 Dreier | a4d61e8 | 2005-08-25 13:40:04 -0700 | [diff] [blame] | 41 | #include <rdma/ib_cache.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | #include "ipoib.h" |
| 44 | |
| 45 | #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA |
| 46 | static int data_debug_level; |
| 47 | |
| 48 | module_param(data_debug_level, int, 0644); |
| 49 | MODULE_PARM_DESC(data_debug_level, |
| 50 | "Enable data path debug tracing if > 0"); |
| 51 | #endif |
| 52 | |
Ingo Molnar | 95ed644 | 2006-01-13 14:51:39 -0800 | [diff] [blame] | 53 | static DEFINE_MUTEX(pkey_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | struct 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 | |
| 78 | void 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 Dreier | 31c02e2 | 2006-06-17 20:37:34 -0700 | [diff] [blame] | 85 | spin_lock_irqsave(&priv->lock, flags); |
| 86 | list_add_tail(&ah->list, &priv->dead_ahs); |
| 87 | spin_unlock_irqrestore(&priv->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | static int ipoib_ib_post_receive(struct net_device *dev, int id) |
| 91 | { |
| 92 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 93 | struct ib_sge list; |
| 94 | struct ib_recv_wr param; |
| 95 | struct ib_recv_wr *bad_wr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | int ret; |
| 97 | |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 98 | list.addr = priv->rx_ring[id].mapping; |
| 99 | list.length = IPOIB_BUF_SIZE; |
| 100 | list.lkey = priv->mr->lkey; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 102 | param.next = NULL; |
| 103 | param.wr_id = id | IPOIB_OP_RECV; |
| 104 | param.sg_list = &list; |
| 105 | param.num_sge = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 107 | ret = ib_post_recv(priv->qp, ¶m, &bad_wr); |
| 108 | if (unlikely(ret)) { |
| 109 | ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret); |
Ralph Campbell | 37ccf9d | 2006-12-12 14:30:48 -0800 | [diff] [blame] | 110 | ib_dma_unmap_single(priv->ca, priv->rx_ring[id].mapping, |
| 111 | IPOIB_BUF_SIZE, DMA_FROM_DEVICE); |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 112 | dev_kfree_skb_any(priv->rx_ring[id].skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | priv->rx_ring[id].skb = NULL; |
| 114 | } |
| 115 | |
| 116 | return ret; |
| 117 | } |
| 118 | |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 119 | static 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 Campbell | 37ccf9d | 2006-12-12 14:30:48 -0800 | [diff] [blame] | 123 | u64 addr; |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 124 | |
| 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 Campbell | 37ccf9d | 2006-12-12 14:30:48 -0800 | [diff] [blame] | 136 | 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 Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 139 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | static int ipoib_ib_post_receives(struct net_device *dev) |
| 150 | { |
| 151 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 152 | int i; |
| 153 | |
Shirley Ma | 0f48525 | 2006-04-10 09:43:58 -0700 | [diff] [blame] | 154 | for (i = 0; i < ipoib_recvq_size; ++i) { |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 155 | if (ipoib_alloc_rx_skb(dev, i)) { |
| 156 | ipoib_warn(priv, "failed to allocate receive buffer %d\n", i); |
| 157 | return -ENOMEM; |
| 158 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | 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 Dreier | 2439a6e | 2006-09-22 15:22:52 -0700 | [diff] [blame] | 168 | static 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 Campbell | 37ccf9d | 2006-12-12 14:30:48 -0800 | [diff] [blame] | 173 | u64 addr; |
Roland Dreier | 2439a6e | 2006-09-22 15:22:52 -0700 | [diff] [blame] | 174 | |
Roland Dreier | a89875f | 2007-04-18 20:20:53 -0700 | [diff] [blame] | 175 | ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n", |
| 176 | wr_id, wc->status); |
Roland Dreier | 2439a6e | 2006-09-22 15:22:52 -0700 | [diff] [blame] | 177 | |
| 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 Campbell | 37ccf9d | 2006-12-12 14:30:48 -0800 | [diff] [blame] | 192 | ib_dma_unmap_single(priv->ca, addr, |
| 193 | IPOIB_BUF_SIZE, DMA_FROM_DEVICE); |
Roland Dreier | 2439a6e | 2006-09-22 15:22:52 -0700 | [diff] [blame] | 194 | dev_kfree_skb_any(skb); |
| 195 | priv->rx_ring[wr_id].skb = NULL; |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | /* |
Roland Dreier | 1b844af | 2007-07-10 13:43:53 -0700 | [diff] [blame] | 200 | * 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 Dreier | 2439a6e | 2006-09-22 15:22:52 -0700 | [diff] [blame] | 207 | * If we can't allocate a new RX buffer, dump |
| 208 | * this packet and reuse the old buffer. |
| 209 | */ |
| 210 | if (unlikely(ipoib_alloc_rx_skb(dev, wr_id))) { |
Roland Dreier | de90351 | 2007-09-28 15:33:51 -0700 | [diff] [blame] | 211 | ++dev->stats.rx_dropped; |
Roland Dreier | 2439a6e | 2006-09-22 15:22:52 -0700 | [diff] [blame] | 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 Campbell | 37ccf9d | 2006-12-12 14:30:48 -0800 | [diff] [blame] | 218 | ib_dma_unmap_single(priv->ca, addr, IPOIB_BUF_SIZE, DMA_FROM_DEVICE); |
Roland Dreier | 2439a6e | 2006-09-22 15:22:52 -0700 | [diff] [blame] | 219 | |
| 220 | skb_put(skb, wc->byte_len); |
| 221 | skb_pull(skb, IB_GRH_BYTES); |
| 222 | |
Roland Dreier | 1b844af | 2007-07-10 13:43:53 -0700 | [diff] [blame] | 223 | skb->protocol = ((struct ipoib_header *) skb->data)->proto; |
| 224 | skb_reset_mac_header(skb); |
| 225 | skb_pull(skb, IPOIB_ENCAP_LEN); |
Roland Dreier | 2439a6e | 2006-09-22 15:22:52 -0700 | [diff] [blame] | 226 | |
Roland Dreier | 1b844af | 2007-07-10 13:43:53 -0700 | [diff] [blame] | 227 | dev->last_rx = jiffies; |
Roland Dreier | de90351 | 2007-09-28 15:33:51 -0700 | [diff] [blame] | 228 | ++dev->stats.rx_packets; |
| 229 | dev->stats.rx_bytes += skb->len; |
Roland Dreier | 2439a6e | 2006-09-22 15:22:52 -0700 | [diff] [blame] | 230 | |
Roland Dreier | 1b844af | 2007-07-10 13:43:53 -0700 | [diff] [blame] | 231 | skb->dev = dev; |
| 232 | /* XXX get correct PACKET_ type here */ |
| 233 | skb->pkt_type = PACKET_HOST; |
| 234 | netif_receive_skb(skb); |
Roland Dreier | 2439a6e | 2006-09-22 15:22:52 -0700 | [diff] [blame] | 235 | |
| 236 | repost: |
| 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 | |
Eli Cohen | 7143740 | 2008-01-30 18:30:53 +0200 | [diff] [blame] | 242 | static int ipoib_dma_map_tx(struct ib_device *ca, |
| 243 | struct ipoib_tx_buf *tx_req) |
| 244 | { |
| 245 | struct sk_buff *skb = tx_req->skb; |
| 246 | u64 *mapping = tx_req->mapping; |
| 247 | int i; |
| 248 | |
| 249 | mapping[0] = ib_dma_map_single(ca, skb->data, skb_headlen(skb), |
| 250 | DMA_TO_DEVICE); |
| 251 | if (unlikely(ib_dma_mapping_error(ca, mapping[0]))) |
| 252 | return -EIO; |
| 253 | |
| 254 | for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) { |
| 255 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 256 | mapping[i + 1] = ib_dma_map_page(ca, frag->page, |
| 257 | frag->page_offset, frag->size, |
| 258 | DMA_TO_DEVICE); |
| 259 | if (unlikely(ib_dma_mapping_error(ca, mapping[i + 1]))) |
| 260 | goto partial_error; |
| 261 | } |
| 262 | return 0; |
| 263 | |
| 264 | partial_error: |
| 265 | ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE); |
| 266 | |
| 267 | for (; i > 0; --i) { |
| 268 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1]; |
| 269 | ib_dma_unmap_page(ca, mapping[i], frag->size, DMA_TO_DEVICE); |
| 270 | } |
| 271 | return -EIO; |
| 272 | } |
| 273 | |
| 274 | static void ipoib_dma_unmap_tx(struct ib_device *ca, |
| 275 | struct ipoib_tx_buf *tx_req) |
| 276 | { |
| 277 | struct sk_buff *skb = tx_req->skb; |
| 278 | u64 *mapping = tx_req->mapping; |
| 279 | int i; |
| 280 | |
| 281 | ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE); |
| 282 | |
| 283 | for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) { |
| 284 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 285 | ib_dma_unmap_page(ca, mapping[i + 1], frag->size, |
| 286 | DMA_TO_DEVICE); |
| 287 | } |
| 288 | } |
| 289 | |
Roland Dreier | 2439a6e | 2006-09-22 15:22:52 -0700 | [diff] [blame] | 290 | static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | { |
| 292 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 293 | unsigned int wr_id = wc->wr_id; |
Roland Dreier | 2439a6e | 2006-09-22 15:22:52 -0700 | [diff] [blame] | 294 | struct ipoib_tx_buf *tx_req; |
| 295 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | |
Roland Dreier | a89875f | 2007-04-18 20:20:53 -0700 | [diff] [blame] | 297 | ipoib_dbg_data(priv, "send completion: id %d, status: %d\n", |
| 298 | wr_id, wc->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
Roland Dreier | 2439a6e | 2006-09-22 15:22:52 -0700 | [diff] [blame] | 300 | if (unlikely(wr_id >= ipoib_sendq_size)) { |
| 301 | ipoib_warn(priv, "send completion event with wrid %d (> %d)\n", |
| 302 | wr_id, ipoib_sendq_size); |
| 303 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | } |
Roland Dreier | 2439a6e | 2006-09-22 15:22:52 -0700 | [diff] [blame] | 305 | |
| 306 | tx_req = &priv->tx_ring[wr_id]; |
| 307 | |
Eli Cohen | 7143740 | 2008-01-30 18:30:53 +0200 | [diff] [blame] | 308 | ipoib_dma_unmap_tx(priv->ca, tx_req); |
Roland Dreier | 2439a6e | 2006-09-22 15:22:52 -0700 | [diff] [blame] | 309 | |
Roland Dreier | de90351 | 2007-09-28 15:33:51 -0700 | [diff] [blame] | 310 | ++dev->stats.tx_packets; |
| 311 | dev->stats.tx_bytes += tx_req->skb->len; |
Roland Dreier | 2439a6e | 2006-09-22 15:22:52 -0700 | [diff] [blame] | 312 | |
| 313 | dev_kfree_skb_any(tx_req->skb); |
| 314 | |
| 315 | spin_lock_irqsave(&priv->tx_lock, flags); |
| 316 | ++priv->tx_tail; |
Michael S. Tsirkin | 1b52496 | 2007-08-16 15:36:16 +0300 | [diff] [blame] | 317 | if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) && |
| 318 | netif_queue_stopped(dev) && |
| 319 | test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) |
Roland Dreier | 2439a6e | 2006-09-22 15:22:52 -0700 | [diff] [blame] | 320 | netif_wake_queue(dev); |
| 321 | spin_unlock_irqrestore(&priv->tx_lock, flags); |
| 322 | |
| 323 | if (wc->status != IB_WC_SUCCESS && |
| 324 | wc->status != IB_WC_WR_FLUSH_ERR) |
| 325 | ipoib_warn(priv, "failed send event " |
| 326 | "(status=%d, wrid=%d vend_err %x)\n", |
| 327 | wc->status, wr_id, wc->vendor_err); |
| 328 | } |
| 329 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 330 | int ipoib_poll(struct napi_struct *napi, int budget) |
Roland Dreier | 2439a6e | 2006-09-22 15:22:52 -0700 | [diff] [blame] | 331 | { |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 332 | struct ipoib_dev_priv *priv = container_of(napi, struct ipoib_dev_priv, napi); |
| 333 | struct net_device *dev = priv->dev; |
Roland Dreier | 8d1cc86 | 2007-05-06 21:05:32 -0700 | [diff] [blame] | 334 | int done; |
| 335 | int t; |
Roland Dreier | 8d1cc86 | 2007-05-06 21:05:32 -0700 | [diff] [blame] | 336 | int n, i; |
| 337 | |
| 338 | done = 0; |
Roland Dreier | 8d1cc86 | 2007-05-06 21:05:32 -0700 | [diff] [blame] | 339 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 340 | poll_more: |
| 341 | while (done < budget) { |
| 342 | int max = (budget - done); |
| 343 | |
Roland Dreier | 8d1cc86 | 2007-05-06 21:05:32 -0700 | [diff] [blame] | 344 | t = min(IPOIB_NUM_WC, max); |
| 345 | n = ib_poll_cq(priv->cq, t, priv->ibwc); |
| 346 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 347 | for (i = 0; i < n; i++) { |
Roland Dreier | 8d1cc86 | 2007-05-06 21:05:32 -0700 | [diff] [blame] | 348 | struct ib_wc *wc = priv->ibwc + i; |
| 349 | |
Michael S. Tsirkin | 1b52496 | 2007-08-16 15:36:16 +0300 | [diff] [blame] | 350 | if (wc->wr_id & IPOIB_OP_RECV) { |
Roland Dreier | 8d1cc86 | 2007-05-06 21:05:32 -0700 | [diff] [blame] | 351 | ++done; |
Michael S. Tsirkin | 1b52496 | 2007-08-16 15:36:16 +0300 | [diff] [blame] | 352 | if (wc->wr_id & IPOIB_OP_CM) |
| 353 | ipoib_cm_handle_rx_wc(dev, wc); |
| 354 | else |
| 355 | ipoib_ib_handle_rx_wc(dev, wc); |
| 356 | } else { |
| 357 | if (wc->wr_id & IPOIB_OP_CM) |
| 358 | ipoib_cm_handle_tx_wc(dev, wc); |
| 359 | else |
| 360 | ipoib_ib_handle_tx_wc(dev, wc); |
| 361 | } |
Roland Dreier | 8d1cc86 | 2007-05-06 21:05:32 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 364 | if (n != t) |
Roland Dreier | 8d1cc86 | 2007-05-06 21:05:32 -0700 | [diff] [blame] | 365 | break; |
Roland Dreier | 8d1cc86 | 2007-05-06 21:05:32 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 368 | if (done < budget) { |
| 369 | netif_rx_complete(dev, napi); |
Roland Dreier | 8d1cc86 | 2007-05-06 21:05:32 -0700 | [diff] [blame] | 370 | if (unlikely(ib_req_notify_cq(priv->cq, |
| 371 | IB_CQ_NEXT_COMP | |
| 372 | IB_CQ_REPORT_MISSED_EVENTS)) && |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 373 | netif_rx_reschedule(dev, napi)) |
| 374 | goto poll_more; |
Roland Dreier | 8d1cc86 | 2007-05-06 21:05:32 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 377 | return done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr) |
| 381 | { |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 382 | struct net_device *dev = dev_ptr; |
| 383 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 384 | |
| 385 | netif_rx_schedule(dev, &priv->napi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | static inline int post_send(struct ipoib_dev_priv *priv, |
| 389 | unsigned int wr_id, |
| 390 | struct ib_ah *address, u32 qpn, |
Eli Cohen | 7143740 | 2008-01-30 18:30:53 +0200 | [diff] [blame] | 391 | u64 *mapping, int headlen, |
| 392 | skb_frag_t *frags, |
| 393 | int nr_frags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | { |
| 395 | struct ib_send_wr *bad_wr; |
Eli Cohen | 7143740 | 2008-01-30 18:30:53 +0200 | [diff] [blame] | 396 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | |
Eli Cohen | 7143740 | 2008-01-30 18:30:53 +0200 | [diff] [blame] | 398 | priv->tx_sge[0].addr = mapping[0]; |
| 399 | priv->tx_sge[0].length = headlen; |
| 400 | for (i = 0; i < nr_frags; ++i) { |
| 401 | priv->tx_sge[i + 1].addr = mapping[i + 1]; |
| 402 | priv->tx_sge[i + 1].length = frags[i].size; |
| 403 | } |
| 404 | priv->tx_wr.num_sge = nr_frags + 1; |
| 405 | priv->tx_wr.wr_id = wr_id; |
| 406 | priv->tx_wr.wr.ud.remote_qpn = qpn; |
| 407 | priv->tx_wr.wr.ud.ah = address; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | |
| 409 | return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr); |
| 410 | } |
| 411 | |
| 412 | void ipoib_send(struct net_device *dev, struct sk_buff *skb, |
| 413 | struct ipoib_ah *address, u32 qpn) |
| 414 | { |
| 415 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 416 | struct ipoib_tx_buf *tx_req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
Michael S. Tsirkin | 77d8e1e | 2007-03-21 15:45:05 +0200 | [diff] [blame] | 418 | if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n", |
Michael S. Tsirkin | 77d8e1e | 2007-03-21 15:45:05 +0200 | [diff] [blame] | 420 | skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN); |
Roland Dreier | de90351 | 2007-09-28 15:33:51 -0700 | [diff] [blame] | 421 | ++dev->stats.tx_dropped; |
| 422 | ++dev->stats.tx_errors; |
Michael S. Tsirkin | 839fcab | 2007-02-05 22:12:23 +0200 | [diff] [blame] | 423 | ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | return; |
| 425 | } |
| 426 | |
| 427 | ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n", |
| 428 | skb->len, address, qpn); |
| 429 | |
| 430 | /* |
| 431 | * We put the skb into the tx_ring _before_ we call post_send() |
| 432 | * because it's entirely possible that the completion handler will |
| 433 | * run before we execute anything after the post_send(). That |
| 434 | * means we have to make sure everything is properly recorded and |
| 435 | * our state is consistent before we call post_send(). |
| 436 | */ |
Shirley Ma | 0f48525 | 2006-04-10 09:43:58 -0700 | [diff] [blame] | 437 | tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | tx_req->skb = skb; |
Eli Cohen | 7143740 | 2008-01-30 18:30:53 +0200 | [diff] [blame] | 439 | if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) { |
Roland Dreier | de90351 | 2007-09-28 15:33:51 -0700 | [diff] [blame] | 440 | ++dev->stats.tx_errors; |
Roland Dreier | 73fbe8b | 2006-10-10 12:50:38 -0700 | [diff] [blame] | 441 | dev_kfree_skb_any(skb); |
| 442 | return; |
| 443 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
Shirley Ma | 0f48525 | 2006-04-10 09:43:58 -0700 | [diff] [blame] | 445 | if (unlikely(post_send(priv, priv->tx_head & (ipoib_sendq_size - 1), |
Eli Cohen | 7143740 | 2008-01-30 18:30:53 +0200 | [diff] [blame] | 446 | address->ah, qpn, |
| 447 | tx_req->mapping, skb_headlen(skb), |
| 448 | skb_shinfo(skb)->frags, skb_shinfo(skb)->nr_frags))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | ipoib_warn(priv, "post_send failed\n"); |
Roland Dreier | de90351 | 2007-09-28 15:33:51 -0700 | [diff] [blame] | 450 | ++dev->stats.tx_errors; |
Eli Cohen | 7143740 | 2008-01-30 18:30:53 +0200 | [diff] [blame] | 451 | ipoib_dma_unmap_tx(priv->ca, tx_req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | dev_kfree_skb_any(skb); |
| 453 | } else { |
| 454 | dev->trans_start = jiffies; |
| 455 | |
| 456 | address->last_send = priv->tx_head; |
| 457 | ++priv->tx_head; |
| 458 | |
Michael S. Tsirkin | 1b52496 | 2007-08-16 15:36:16 +0300 | [diff] [blame] | 459 | if (++priv->tx_outstanding == ipoib_sendq_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n"); |
| 461 | netif_stop_queue(dev); |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | static void __ipoib_reap_ah(struct net_device *dev) |
| 467 | { |
| 468 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 469 | struct ipoib_ah *ah, *tah; |
| 470 | LIST_HEAD(remove_list); |
| 471 | |
Roland Dreier | 31c02e2 | 2006-06-17 20:37:34 -0700 | [diff] [blame] | 472 | spin_lock_irq(&priv->tx_lock); |
| 473 | spin_lock(&priv->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list) |
Roland Dreier | 2181858 | 2005-07-27 14:41:32 -0700 | [diff] [blame] | 475 | if ((int) priv->tx_tail - (int) ah->last_send >= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | list_del(&ah->list); |
Roland Dreier | 31c02e2 | 2006-06-17 20:37:34 -0700 | [diff] [blame] | 477 | ib_destroy_ah(ah->ah); |
| 478 | kfree(ah); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | } |
Roland Dreier | 31c02e2 | 2006-06-17 20:37:34 -0700 | [diff] [blame] | 480 | spin_unlock(&priv->lock); |
| 481 | spin_unlock_irq(&priv->tx_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | } |
| 483 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 484 | void ipoib_reap_ah(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 486 | struct ipoib_dev_priv *priv = |
| 487 | container_of(work, struct ipoib_dev_priv, ah_reap_task.work); |
| 488 | struct net_device *dev = priv->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
| 490 | __ipoib_reap_ah(dev); |
| 491 | |
| 492 | if (!test_bit(IPOIB_STOP_REAPER, &priv->flags)) |
Anton Blanchard | 69fc507 | 2007-10-15 00:50:56 -0500 | [diff] [blame] | 493 | queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, |
| 494 | round_jiffies_relative(HZ)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | int ipoib_ib_dev_open(struct net_device *dev) |
| 498 | { |
| 499 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 500 | int ret; |
| 501 | |
Yosef Etigin | 26bbf13 | 2007-05-19 08:51:54 -0700 | [diff] [blame] | 502 | if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) { |
| 503 | ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey); |
| 504 | clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags); |
| 505 | return -1; |
| 506 | } |
| 507 | set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags); |
| 508 | |
Roland Dreier | 5b6810e | 2005-10-11 11:08:24 -0700 | [diff] [blame] | 509 | ret = ipoib_init_qp(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | if (ret) { |
Roland Dreier | 5b6810e | 2005-10-11 11:08:24 -0700 | [diff] [blame] | 511 | ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | return -1; |
| 513 | } |
| 514 | |
| 515 | ret = ipoib_ib_post_receives(dev); |
| 516 | if (ret) { |
| 517 | ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret); |
Yosef Etigin | 26bbf13 | 2007-05-19 08:51:54 -0700 | [diff] [blame] | 518 | ipoib_ib_dev_stop(dev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | return -1; |
| 520 | } |
| 521 | |
Michael S. Tsirkin | 839fcab | 2007-02-05 22:12:23 +0200 | [diff] [blame] | 522 | ret = ipoib_cm_dev_open(dev); |
| 523 | if (ret) { |
Michael S. Tsirkin | 24bd1e4 | 2007-05-18 16:12:54 +0300 | [diff] [blame] | 524 | ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret); |
Yosef Etigin | 26bbf13 | 2007-05-19 08:51:54 -0700 | [diff] [blame] | 525 | ipoib_ib_dev_stop(dev, 1); |
Michael S. Tsirkin | 839fcab | 2007-02-05 22:12:23 +0200 | [diff] [blame] | 526 | return -1; |
| 527 | } |
| 528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | clear_bit(IPOIB_STOP_REAPER, &priv->flags); |
Anton Blanchard | 69fc507 | 2007-10-15 00:50:56 -0500 | [diff] [blame] | 530 | queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, |
| 531 | round_jiffies_relative(HZ)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | |
Leonid Arsh | 7a343d4 | 2006-03-23 19:52:51 +0200 | [diff] [blame] | 533 | set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags); |
| 534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | return 0; |
| 536 | } |
| 537 | |
Leonid Arsh | 7a343d4 | 2006-03-23 19:52:51 +0200 | [diff] [blame] | 538 | static void ipoib_pkey_dev_check_presence(struct net_device *dev) |
| 539 | { |
| 540 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 541 | u16 pkey_index = 0; |
| 542 | |
| 543 | if (ib_find_cached_pkey(priv->ca, priv->port, priv->pkey, &pkey_index)) |
| 544 | clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags); |
| 545 | else |
| 546 | set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags); |
| 547 | } |
| 548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | int ipoib_ib_dev_up(struct net_device *dev) |
| 550 | { |
| 551 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 552 | |
Leonid Arsh | 7a343d4 | 2006-03-23 19:52:51 +0200 | [diff] [blame] | 553 | ipoib_pkey_dev_check_presence(dev); |
| 554 | |
| 555 | if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) { |
| 556 | ipoib_dbg(priv, "PKEY is not assigned.\n"); |
| 557 | return 0; |
| 558 | } |
| 559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | set_bit(IPOIB_FLAG_OPER_UP, &priv->flags); |
| 561 | |
| 562 | return ipoib_mcast_start_thread(dev); |
| 563 | } |
| 564 | |
Jack Morgenstein | 0b3ea08 | 2006-03-20 10:08:24 -0800 | [diff] [blame] | 565 | int ipoib_ib_dev_down(struct net_device *dev, int flush) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | { |
| 567 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 568 | |
| 569 | ipoib_dbg(priv, "downing ib_dev\n"); |
| 570 | |
| 571 | clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags); |
| 572 | netif_carrier_off(dev); |
| 573 | |
| 574 | /* Shutdown the P_Key thread if still active */ |
| 575 | if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) { |
Ingo Molnar | 95ed644 | 2006-01-13 14:51:39 -0800 | [diff] [blame] | 576 | mutex_lock(&pkey_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | set_bit(IPOIB_PKEY_STOP, &priv->flags); |
Yosef Etigin | 26bbf13 | 2007-05-19 08:51:54 -0700 | [diff] [blame] | 578 | cancel_delayed_work(&priv->pkey_poll_task); |
Ingo Molnar | 95ed644 | 2006-01-13 14:51:39 -0800 | [diff] [blame] | 579 | mutex_unlock(&pkey_mutex); |
Jack Morgenstein | 0b3ea08 | 2006-03-20 10:08:24 -0800 | [diff] [blame] | 580 | if (flush) |
| 581 | flush_workqueue(ipoib_workqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Jack Morgenstein | 0b3ea08 | 2006-03-20 10:08:24 -0800 | [diff] [blame] | 584 | ipoib_mcast_stop_thread(dev, flush); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | ipoib_mcast_dev_flush(dev); |
| 586 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | ipoib_flush_paths(dev); |
| 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | static int recvs_pending(struct net_device *dev) |
| 593 | { |
| 594 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 595 | int pending = 0; |
| 596 | int i; |
| 597 | |
Shirley Ma | 0f48525 | 2006-04-10 09:43:58 -0700 | [diff] [blame] | 598 | for (i = 0; i < ipoib_recvq_size; ++i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | if (priv->rx_ring[i].skb) |
| 600 | ++pending; |
| 601 | |
| 602 | return pending; |
| 603 | } |
| 604 | |
Michael S. Tsirkin | 2dfbfc3 | 2007-05-24 18:32:46 +0300 | [diff] [blame] | 605 | void ipoib_drain_cq(struct net_device *dev) |
| 606 | { |
| 607 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 608 | int i, n; |
| 609 | do { |
| 610 | n = ib_poll_cq(priv->cq, IPOIB_NUM_WC, priv->ibwc); |
| 611 | for (i = 0; i < n; ++i) { |
Roland Dreier | ce423ef | 2007-10-09 19:59:04 -0700 | [diff] [blame] | 612 | /* |
| 613 | * Convert any successful completions to flush |
| 614 | * errors to avoid passing packets up the |
| 615 | * stack after bringing the device down. |
| 616 | */ |
| 617 | if (priv->ibwc[i].status == IB_WC_SUCCESS) |
| 618 | priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR; |
| 619 | |
Michael S. Tsirkin | 1b52496 | 2007-08-16 15:36:16 +0300 | [diff] [blame] | 620 | if (priv->ibwc[i].wr_id & IPOIB_OP_RECV) { |
| 621 | if (priv->ibwc[i].wr_id & IPOIB_OP_CM) |
| 622 | ipoib_cm_handle_rx_wc(dev, priv->ibwc + i); |
| 623 | else |
| 624 | ipoib_ib_handle_rx_wc(dev, priv->ibwc + i); |
| 625 | } else { |
| 626 | if (priv->ibwc[i].wr_id & IPOIB_OP_CM) |
| 627 | ipoib_cm_handle_tx_wc(dev, priv->ibwc + i); |
| 628 | else |
| 629 | ipoib_ib_handle_tx_wc(dev, priv->ibwc + i); |
| 630 | } |
Michael S. Tsirkin | 2dfbfc3 | 2007-05-24 18:32:46 +0300 | [diff] [blame] | 631 | } |
| 632 | } while (n == IPOIB_NUM_WC); |
| 633 | } |
| 634 | |
Yosef Etigin | 26bbf13 | 2007-05-19 08:51:54 -0700 | [diff] [blame] | 635 | int ipoib_ib_dev_stop(struct net_device *dev, int flush) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | { |
| 637 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 638 | struct ib_qp_attr qp_attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | unsigned long begin; |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 640 | struct ipoib_tx_buf *tx_req; |
Michael S. Tsirkin | 2dfbfc3 | 2007-05-24 18:32:46 +0300 | [diff] [blame] | 641 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | |
Leonid Arsh | 7a343d4 | 2006-03-23 19:52:51 +0200 | [diff] [blame] | 643 | clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags); |
| 644 | |
Michael S. Tsirkin | 839fcab | 2007-02-05 22:12:23 +0200 | [diff] [blame] | 645 | ipoib_cm_dev_stop(dev); |
| 646 | |
Roland Dreier | 3bc12e7 | 2005-10-30 13:20:09 -0800 | [diff] [blame] | 647 | /* |
| 648 | * Move our QP to the error state and then reinitialize in |
| 649 | * when all work requests have completed or have been flushed. |
| 650 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | qp_attr.qp_state = IB_QPS_ERR; |
Roland Dreier | 3bc12e7 | 2005-10-30 13:20:09 -0800 | [diff] [blame] | 652 | if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | ipoib_warn(priv, "Failed to modify QP to ERROR state\n"); |
| 654 | |
| 655 | /* Wait for all sends and receives to complete */ |
| 656 | begin = jiffies; |
| 657 | |
| 658 | while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) { |
| 659 | if (time_after(jiffies, begin + 5 * HZ)) { |
| 660 | ipoib_warn(priv, "timing out; %d sends %d receives not completed\n", |
| 661 | priv->tx_head - priv->tx_tail, recvs_pending(dev)); |
| 662 | |
| 663 | /* |
| 664 | * assume the HW is wedged and just free up |
| 665 | * all our pending work requests. |
| 666 | */ |
Roland Dreier | 2181858 | 2005-07-27 14:41:32 -0700 | [diff] [blame] | 667 | while ((int) priv->tx_tail - (int) priv->tx_head < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | tx_req = &priv->tx_ring[priv->tx_tail & |
Shirley Ma | 0f48525 | 2006-04-10 09:43:58 -0700 | [diff] [blame] | 669 | (ipoib_sendq_size - 1)]; |
Eli Cohen | 7143740 | 2008-01-30 18:30:53 +0200 | [diff] [blame] | 670 | ipoib_dma_unmap_tx(priv->ca, tx_req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | dev_kfree_skb_any(tx_req->skb); |
| 672 | ++priv->tx_tail; |
Michael S. Tsirkin | 1b52496 | 2007-08-16 15:36:16 +0300 | [diff] [blame] | 673 | --priv->tx_outstanding; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Ralph Campbell | 37ccf9d | 2006-12-12 14:30:48 -0800 | [diff] [blame] | 676 | for (i = 0; i < ipoib_recvq_size; ++i) { |
| 677 | struct ipoib_rx_buf *rx_req; |
| 678 | |
| 679 | rx_req = &priv->rx_ring[i]; |
| 680 | if (!rx_req->skb) |
| 681 | continue; |
| 682 | ib_dma_unmap_single(priv->ca, |
| 683 | rx_req->mapping, |
| 684 | IPOIB_BUF_SIZE, |
| 685 | DMA_FROM_DEVICE); |
| 686 | dev_kfree_skb_any(rx_req->skb); |
| 687 | rx_req->skb = NULL; |
| 688 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | |
| 690 | goto timeout; |
| 691 | } |
| 692 | |
Michael S. Tsirkin | 2dfbfc3 | 2007-05-24 18:32:46 +0300 | [diff] [blame] | 693 | ipoib_drain_cq(dev); |
Roland Dreier | 8d1cc86 | 2007-05-06 21:05:32 -0700 | [diff] [blame] | 694 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | msleep(1); |
| 696 | } |
| 697 | |
| 698 | ipoib_dbg(priv, "All sends and receives done.\n"); |
| 699 | |
| 700 | timeout: |
| 701 | qp_attr.qp_state = IB_QPS_RESET; |
Roland Dreier | 3bc12e7 | 2005-10-30 13:20:09 -0800 | [diff] [blame] | 702 | if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | ipoib_warn(priv, "Failed to modify QP to RESET state\n"); |
| 704 | |
| 705 | /* Wait for all AHs to be reaped */ |
| 706 | set_bit(IPOIB_STOP_REAPER, &priv->flags); |
| 707 | cancel_delayed_work(&priv->ah_reap_task); |
Yosef Etigin | 26bbf13 | 2007-05-19 08:51:54 -0700 | [diff] [blame] | 708 | if (flush) |
| 709 | flush_workqueue(ipoib_workqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | |
| 711 | begin = jiffies; |
| 712 | |
| 713 | while (!list_empty(&priv->dead_ahs)) { |
| 714 | __ipoib_reap_ah(dev); |
| 715 | |
| 716 | if (time_after(jiffies, begin + HZ)) { |
| 717 | ipoib_warn(priv, "timing out; will leak address handles\n"); |
| 718 | break; |
| 719 | } |
| 720 | |
| 721 | msleep(1); |
| 722 | } |
| 723 | |
Roland Dreier | 8d1cc86 | 2007-05-06 21:05:32 -0700 | [diff] [blame] | 724 | ib_req_notify_cq(priv->cq, IB_CQ_NEXT_COMP); |
| 725 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port) |
| 730 | { |
| 731 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 732 | |
| 733 | priv->ca = ca; |
| 734 | priv->port = port; |
| 735 | priv->qp = NULL; |
| 736 | |
| 737 | if (ipoib_transport_dev_init(dev, ca)) { |
| 738 | printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name); |
| 739 | return -ENODEV; |
| 740 | } |
| 741 | |
| 742 | if (dev->flags & IFF_UP) { |
| 743 | if (ipoib_ib_dev_open(dev)) { |
| 744 | ipoib_transport_dev_cleanup(dev); |
| 745 | return -ENODEV; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | return 0; |
| 750 | } |
| 751 | |
Yosef Etigin | 26bbf13 | 2007-05-19 08:51:54 -0700 | [diff] [blame] | 752 | static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv, int pkey_event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | { |
Yosef Etigin | 26bbf13 | 2007-05-19 08:51:54 -0700 | [diff] [blame] | 754 | struct ipoib_dev_priv *cpriv; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 755 | struct net_device *dev = priv->dev; |
Yosef Etigin | 26bbf13 | 2007-05-19 08:51:54 -0700 | [diff] [blame] | 756 | u16 new_index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | |
Yosef Etigin | 26bbf13 | 2007-05-19 08:51:54 -0700 | [diff] [blame] | 758 | mutex_lock(&priv->vlan_mutex); |
| 759 | |
| 760 | /* |
| 761 | * Flush any child interfaces too -- they might be up even if |
| 762 | * the parent is down. |
| 763 | */ |
| 764 | list_for_each_entry(cpriv, &priv->child_intfs, list) |
| 765 | __ipoib_ib_dev_flush(cpriv, pkey_event); |
| 766 | |
| 767 | mutex_unlock(&priv->vlan_mutex); |
| 768 | |
| 769 | if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) { |
Leonid Arsh | 7a343d4 | 2006-03-23 19:52:51 +0200 | [diff] [blame] | 770 | ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | return; |
Leonid Arsh | 7a343d4 | 2006-03-23 19:52:51 +0200 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) { |
| 775 | ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n"); |
| 776 | return; |
| 777 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | |
Yosef Etigin | 26bbf13 | 2007-05-19 08:51:54 -0700 | [diff] [blame] | 779 | if (pkey_event) { |
| 780 | if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) { |
| 781 | clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags); |
| 782 | ipoib_ib_dev_down(dev, 0); |
Jack Morgenstein | 167c426 | 2008-02-13 16:23:50 +0200 | [diff] [blame] | 783 | ipoib_ib_dev_stop(dev, 0); |
Yosef Etigin | 26bbf13 | 2007-05-19 08:51:54 -0700 | [diff] [blame] | 784 | ipoib_pkey_dev_delay_open(dev); |
| 785 | return; |
| 786 | } |
| 787 | set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags); |
| 788 | |
| 789 | /* restart QP only if P_Key index is changed */ |
| 790 | if (new_index == priv->pkey_index) { |
| 791 | ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n"); |
| 792 | return; |
| 793 | } |
| 794 | priv->pkey_index = new_index; |
| 795 | } |
| 796 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | ipoib_dbg(priv, "flushing\n"); |
| 798 | |
Jack Morgenstein | 0b3ea08 | 2006-03-20 10:08:24 -0800 | [diff] [blame] | 799 | ipoib_ib_dev_down(dev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | |
Yosef Etigin | 26bbf13 | 2007-05-19 08:51:54 -0700 | [diff] [blame] | 801 | if (pkey_event) { |
| 802 | ipoib_ib_dev_stop(dev, 0); |
| 803 | ipoib_ib_dev_open(dev); |
| 804 | } |
| 805 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | /* |
| 807 | * The device could have been brought down between the start and when |
| 808 | * we get here, don't bring it back up if it's not configured up |
| 809 | */ |
Eli Cohen | 5ccd025 | 2006-09-22 15:22:56 -0700 | [diff] [blame] | 810 | if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | ipoib_ib_dev_up(dev); |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 812 | ipoib_mcast_restart_task(&priv->restart_task); |
Eli Cohen | 5ccd025 | 2006-09-22 15:22:56 -0700 | [diff] [blame] | 813 | } |
Yosef Etigin | 26bbf13 | 2007-05-19 08:51:54 -0700 | [diff] [blame] | 814 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | |
Yosef Etigin | 26bbf13 | 2007-05-19 08:51:54 -0700 | [diff] [blame] | 816 | void ipoib_ib_dev_flush(struct work_struct *work) |
| 817 | { |
| 818 | struct ipoib_dev_priv *priv = |
| 819 | container_of(work, struct ipoib_dev_priv, flush_task); |
Michael S. Tsirkin | 4f71055 | 2005-11-29 10:53:30 -0800 | [diff] [blame] | 820 | |
Yosef Etigin | 26bbf13 | 2007-05-19 08:51:54 -0700 | [diff] [blame] | 821 | ipoib_dbg(priv, "Flushing %s\n", priv->dev->name); |
| 822 | __ipoib_ib_dev_flush(priv, 0); |
| 823 | } |
Michael S. Tsirkin | 4f71055 | 2005-11-29 10:53:30 -0800 | [diff] [blame] | 824 | |
Yosef Etigin | 26bbf13 | 2007-05-19 08:51:54 -0700 | [diff] [blame] | 825 | void ipoib_pkey_event(struct work_struct *work) |
| 826 | { |
| 827 | struct ipoib_dev_priv *priv = |
| 828 | container_of(work, struct ipoib_dev_priv, pkey_event_task); |
| 829 | |
| 830 | ipoib_dbg(priv, "Flushing %s and restarting its QP\n", priv->dev->name); |
| 831 | __ipoib_ib_dev_flush(priv, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | void ipoib_ib_dev_cleanup(struct net_device *dev) |
| 835 | { |
| 836 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 837 | |
| 838 | ipoib_dbg(priv, "cleaning up ib_dev\n"); |
| 839 | |
Roland Dreier | 8d2cae0 | 2005-09-20 10:52:04 -0700 | [diff] [blame] | 840 | ipoib_mcast_stop_thread(dev, 1); |
Eli Cohen | 988bd50 | 2006-01-12 14:32:20 -0800 | [diff] [blame] | 841 | ipoib_mcast_dev_flush(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | |
| 843 | ipoib_transport_dev_cleanup(dev); |
| 844 | } |
| 845 | |
| 846 | /* |
| 847 | * Delayed P_Key Assigment Interim Support |
| 848 | * |
| 849 | * The following is initial implementation of delayed P_Key assigment |
| 850 | * mechanism. It is using the same approach implemented for the multicast |
| 851 | * group join. The single goal of this implementation is to quickly address |
| 852 | * Bug #2507. This implementation will probably be removed when the P_Key |
| 853 | * change async notification is available. |
| 854 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 856 | void ipoib_pkey_poll(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 858 | struct ipoib_dev_priv *priv = |
Yosef Etigin | 26bbf13 | 2007-05-19 08:51:54 -0700 | [diff] [blame] | 859 | container_of(work, struct ipoib_dev_priv, pkey_poll_task.work); |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 860 | struct net_device *dev = priv->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | |
| 862 | ipoib_pkey_dev_check_presence(dev); |
| 863 | |
| 864 | if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) |
| 865 | ipoib_open(dev); |
| 866 | else { |
Ingo Molnar | 95ed644 | 2006-01-13 14:51:39 -0800 | [diff] [blame] | 867 | mutex_lock(&pkey_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | if (!test_bit(IPOIB_PKEY_STOP, &priv->flags)) |
| 869 | queue_delayed_work(ipoib_workqueue, |
Yosef Etigin | 26bbf13 | 2007-05-19 08:51:54 -0700 | [diff] [blame] | 870 | &priv->pkey_poll_task, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | HZ); |
Ingo Molnar | 95ed644 | 2006-01-13 14:51:39 -0800 | [diff] [blame] | 872 | mutex_unlock(&pkey_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | |
| 876 | int ipoib_pkey_dev_delay_open(struct net_device *dev) |
| 877 | { |
| 878 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 879 | |
| 880 | /* Look for the interface pkey value in the IB Port P_Key table and */ |
| 881 | /* set the interface pkey assigment flag */ |
| 882 | ipoib_pkey_dev_check_presence(dev); |
| 883 | |
| 884 | /* P_Key value not assigned yet - start polling */ |
| 885 | if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) { |
Ingo Molnar | 95ed644 | 2006-01-13 14:51:39 -0800 | [diff] [blame] | 886 | mutex_lock(&pkey_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | clear_bit(IPOIB_PKEY_STOP, &priv->flags); |
| 888 | queue_delayed_work(ipoib_workqueue, |
Yosef Etigin | 26bbf13 | 2007-05-19 08:51:54 -0700 | [diff] [blame] | 889 | &priv->pkey_poll_task, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | HZ); |
Ingo Molnar | 95ed644 | 2006-01-13 14:51:39 -0800 | [diff] [blame] | 891 | mutex_unlock(&pkey_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | return 1; |
| 893 | } |
| 894 | |
| 895 | return 0; |
| 896 | } |