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 | |
| 53 | #define IPOIB_OP_RECV (1ul << 31) |
| 54 | |
Ingo Molnar | 95ed644 | 2006-01-13 14:51:39 -0800 | [diff] [blame] | 55 | static DEFINE_MUTEX(pkey_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | struct ipoib_ah *ipoib_create_ah(struct net_device *dev, |
| 58 | struct ib_pd *pd, struct ib_ah_attr *attr) |
| 59 | { |
| 60 | struct ipoib_ah *ah; |
| 61 | |
| 62 | ah = kmalloc(sizeof *ah, GFP_KERNEL); |
| 63 | if (!ah) |
| 64 | return NULL; |
| 65 | |
| 66 | ah->dev = dev; |
| 67 | ah->last_send = 0; |
| 68 | kref_init(&ah->ref); |
| 69 | |
| 70 | ah->ah = ib_create_ah(pd, attr); |
| 71 | if (IS_ERR(ah->ah)) { |
| 72 | kfree(ah); |
| 73 | ah = NULL; |
| 74 | } else |
| 75 | ipoib_dbg(netdev_priv(dev), "Created ah %p\n", ah->ah); |
| 76 | |
| 77 | return ah; |
| 78 | } |
| 79 | |
| 80 | void ipoib_free_ah(struct kref *kref) |
| 81 | { |
| 82 | struct ipoib_ah *ah = container_of(kref, struct ipoib_ah, ref); |
| 83 | struct ipoib_dev_priv *priv = netdev_priv(ah->dev); |
| 84 | |
| 85 | unsigned long flags; |
| 86 | |
Roland Dreier | 2181858 | 2005-07-27 14:41:32 -0700 | [diff] [blame] | 87 | if ((int) priv->tx_tail - (int) ah->last_send >= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | ipoib_dbg(priv, "Freeing ah %p\n", ah->ah); |
| 89 | ib_destroy_ah(ah->ah); |
| 90 | kfree(ah); |
| 91 | } else { |
| 92 | spin_lock_irqsave(&priv->lock, flags); |
| 93 | list_add_tail(&ah->list, &priv->dead_ahs); |
| 94 | spin_unlock_irqrestore(&priv->lock, flags); |
| 95 | } |
| 96 | } |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | static int ipoib_ib_post_receive(struct net_device *dev, int id) |
| 99 | { |
| 100 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 101 | struct ib_sge list; |
| 102 | struct ib_recv_wr param; |
| 103 | struct ib_recv_wr *bad_wr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | int ret; |
| 105 | |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 106 | list.addr = priv->rx_ring[id].mapping; |
| 107 | list.length = IPOIB_BUF_SIZE; |
| 108 | list.lkey = priv->mr->lkey; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 110 | param.next = NULL; |
| 111 | param.wr_id = id | IPOIB_OP_RECV; |
| 112 | param.sg_list = &list; |
| 113 | param.num_sge = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 115 | ret = ib_post_recv(priv->qp, ¶m, &bad_wr); |
| 116 | if (unlikely(ret)) { |
| 117 | ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret); |
| 118 | dma_unmap_single(priv->ca->dma_device, |
| 119 | priv->rx_ring[id].mapping, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | IPOIB_BUF_SIZE, DMA_FROM_DEVICE); |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 121 | dev_kfree_skb_any(priv->rx_ring[id].skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | priv->rx_ring[id].skb = NULL; |
| 123 | } |
| 124 | |
| 125 | return ret; |
| 126 | } |
| 127 | |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 128 | static int ipoib_alloc_rx_skb(struct net_device *dev, int id) |
| 129 | { |
| 130 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 131 | struct sk_buff *skb; |
| 132 | dma_addr_t addr; |
| 133 | |
| 134 | skb = dev_alloc_skb(IPOIB_BUF_SIZE + 4); |
| 135 | if (!skb) |
| 136 | return -ENOMEM; |
| 137 | |
| 138 | /* |
| 139 | * IB will leave a 40 byte gap for a GRH and IPoIB adds a 4 byte |
| 140 | * header. So we need 4 more bytes to get to 48 and align the |
| 141 | * IP header to a multiple of 16. |
| 142 | */ |
| 143 | skb_reserve(skb, 4); |
| 144 | |
| 145 | addr = dma_map_single(priv->ca->dma_device, |
| 146 | skb->data, IPOIB_BUF_SIZE, |
| 147 | DMA_FROM_DEVICE); |
| 148 | if (unlikely(dma_mapping_error(addr))) { |
| 149 | dev_kfree_skb_any(skb); |
| 150 | return -EIO; |
| 151 | } |
| 152 | |
| 153 | priv->rx_ring[id].skb = skb; |
| 154 | priv->rx_ring[id].mapping = addr; |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | static int ipoib_ib_post_receives(struct net_device *dev) |
| 160 | { |
| 161 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 162 | int i; |
| 163 | |
| 164 | for (i = 0; i < IPOIB_RX_RING_SIZE; ++i) { |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 165 | if (ipoib_alloc_rx_skb(dev, i)) { |
| 166 | ipoib_warn(priv, "failed to allocate receive buffer %d\n", i); |
| 167 | return -ENOMEM; |
| 168 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | if (ipoib_ib_post_receive(dev, i)) { |
| 170 | ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i); |
| 171 | return -EIO; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static void ipoib_ib_handle_wc(struct net_device *dev, |
| 179 | struct ib_wc *wc) |
| 180 | { |
| 181 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 182 | unsigned int wr_id = wc->wr_id; |
| 183 | |
| 184 | ipoib_dbg_data(priv, "called: id %d, op %d, status: %d\n", |
| 185 | wr_id, wc->opcode, wc->status); |
| 186 | |
| 187 | if (wr_id & IPOIB_OP_RECV) { |
| 188 | wr_id &= ~IPOIB_OP_RECV; |
| 189 | |
| 190 | if (wr_id < IPOIB_RX_RING_SIZE) { |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 191 | struct sk_buff *skb = priv->rx_ring[wr_id].skb; |
| 192 | dma_addr_t addr = priv->rx_ring[wr_id].mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 194 | if (unlikely(wc->status != IB_WC_SUCCESS)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | if (wc->status != IB_WC_WR_FLUSH_ERR) |
| 196 | ipoib_warn(priv, "failed recv event " |
| 197 | "(status=%d, wrid=%d vend_err %x)\n", |
| 198 | wc->status, wr_id, wc->vendor_err); |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 199 | dma_unmap_single(priv->ca->dma_device, addr, |
| 200 | IPOIB_BUF_SIZE, DMA_FROM_DEVICE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | dev_kfree_skb_any(skb); |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 202 | priv->rx_ring[wr_id].skb = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | return; |
| 204 | } |
| 205 | |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 206 | /* |
| 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))) { |
| 211 | ++priv->stats.rx_dropped; |
| 212 | goto repost; |
| 213 | } |
| 214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n", |
| 216 | wc->byte_len, wc->slid); |
| 217 | |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 218 | dma_unmap_single(priv->ca->dma_device, addr, |
| 219 | IPOIB_BUF_SIZE, DMA_FROM_DEVICE); |
| 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | skb_put(skb, wc->byte_len); |
| 222 | skb_pull(skb, IB_GRH_BYTES); |
| 223 | |
| 224 | if (wc->slid != priv->local_lid || |
| 225 | wc->src_qp != priv->qp->qp_num) { |
| 226 | skb->protocol = ((struct ipoib_header *) skb->data)->proto; |
Hal Rosenstock | 62241eb | 2005-04-16 15:26:05 -0700 | [diff] [blame] | 227 | skb->mac.raw = skb->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | skb_pull(skb, IPOIB_ENCAP_LEN); |
| 229 | |
| 230 | dev->last_rx = jiffies; |
| 231 | ++priv->stats.rx_packets; |
| 232 | priv->stats.rx_bytes += skb->len; |
| 233 | |
| 234 | skb->dev = dev; |
| 235 | /* XXX get correct PACKET_ type here */ |
| 236 | skb->pkt_type = PACKET_HOST; |
| 237 | netif_rx_ni(skb); |
| 238 | } else { |
| 239 | ipoib_dbg_data(priv, "dropping loopback packet\n"); |
| 240 | dev_kfree_skb_any(skb); |
| 241 | } |
| 242 | |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 243 | repost: |
| 244 | if (unlikely(ipoib_ib_post_receive(dev, wr_id))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | ipoib_warn(priv, "ipoib_ib_post_receive failed " |
| 246 | "for buf %d\n", wr_id); |
| 247 | } else |
| 248 | ipoib_warn(priv, "completion event with wrid %d\n", |
| 249 | wr_id); |
| 250 | |
| 251 | } else { |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 252 | struct ipoib_tx_buf *tx_req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | unsigned long flags; |
| 254 | |
| 255 | if (wr_id >= IPOIB_TX_RING_SIZE) { |
| 256 | ipoib_warn(priv, "completion event with wrid %d (> %d)\n", |
| 257 | wr_id, IPOIB_TX_RING_SIZE); |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | ipoib_dbg_data(priv, "send complete, wrid %d\n", wr_id); |
| 262 | |
| 263 | tx_req = &priv->tx_ring[wr_id]; |
| 264 | |
| 265 | dma_unmap_single(priv->ca->dma_device, |
| 266 | pci_unmap_addr(tx_req, mapping), |
| 267 | tx_req->skb->len, |
| 268 | DMA_TO_DEVICE); |
| 269 | |
| 270 | ++priv->stats.tx_packets; |
| 271 | priv->stats.tx_bytes += tx_req->skb->len; |
| 272 | |
| 273 | dev_kfree_skb_any(tx_req->skb); |
| 274 | |
| 275 | spin_lock_irqsave(&priv->tx_lock, flags); |
| 276 | ++priv->tx_tail; |
| 277 | if (netif_queue_stopped(dev) && |
| 278 | priv->tx_head - priv->tx_tail <= IPOIB_TX_RING_SIZE / 2) |
| 279 | netif_wake_queue(dev); |
| 280 | spin_unlock_irqrestore(&priv->tx_lock, flags); |
| 281 | |
| 282 | if (wc->status != IB_WC_SUCCESS && |
| 283 | wc->status != IB_WC_WR_FLUSH_ERR) |
| 284 | ipoib_warn(priv, "failed send event " |
| 285 | "(status=%d, wrid=%d vend_err %x)\n", |
| 286 | wc->status, wr_id, wc->vendor_err); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr) |
| 291 | { |
| 292 | struct net_device *dev = (struct net_device *) dev_ptr; |
| 293 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 294 | int n, i; |
| 295 | |
| 296 | ib_req_notify_cq(cq, IB_CQ_NEXT_COMP); |
| 297 | do { |
| 298 | n = ib_poll_cq(cq, IPOIB_NUM_WC, priv->ibwc); |
| 299 | for (i = 0; i < n; ++i) |
| 300 | ipoib_ib_handle_wc(dev, priv->ibwc + i); |
| 301 | } while (n == IPOIB_NUM_WC); |
| 302 | } |
| 303 | |
| 304 | static inline int post_send(struct ipoib_dev_priv *priv, |
| 305 | unsigned int wr_id, |
| 306 | struct ib_ah *address, u32 qpn, |
| 307 | dma_addr_t addr, int len) |
| 308 | { |
| 309 | struct ib_send_wr *bad_wr; |
| 310 | |
| 311 | priv->tx_sge.addr = addr; |
| 312 | priv->tx_sge.length = len; |
| 313 | |
| 314 | priv->tx_wr.wr_id = wr_id; |
| 315 | priv->tx_wr.wr.ud.remote_qpn = qpn; |
| 316 | priv->tx_wr.wr.ud.ah = address; |
| 317 | |
| 318 | return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr); |
| 319 | } |
| 320 | |
| 321 | void ipoib_send(struct net_device *dev, struct sk_buff *skb, |
| 322 | struct ipoib_ah *address, u32 qpn) |
| 323 | { |
| 324 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 325 | struct ipoib_tx_buf *tx_req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | dma_addr_t addr; |
| 327 | |
| 328 | if (skb->len > dev->mtu + INFINIBAND_ALEN) { |
| 329 | ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n", |
| 330 | skb->len, dev->mtu + INFINIBAND_ALEN); |
| 331 | ++priv->stats.tx_dropped; |
| 332 | ++priv->stats.tx_errors; |
| 333 | dev_kfree_skb_any(skb); |
| 334 | return; |
| 335 | } |
| 336 | |
| 337 | ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n", |
| 338 | skb->len, address, qpn); |
| 339 | |
| 340 | /* |
| 341 | * We put the skb into the tx_ring _before_ we call post_send() |
| 342 | * because it's entirely possible that the completion handler will |
| 343 | * run before we execute anything after the post_send(). That |
| 344 | * means we have to make sure everything is properly recorded and |
| 345 | * our state is consistent before we call post_send(). |
| 346 | */ |
| 347 | tx_req = &priv->tx_ring[priv->tx_head & (IPOIB_TX_RING_SIZE - 1)]; |
| 348 | tx_req->skb = skb; |
| 349 | addr = dma_map_single(priv->ca->dma_device, skb->data, skb->len, |
| 350 | DMA_TO_DEVICE); |
| 351 | pci_unmap_addr_set(tx_req, mapping, addr); |
| 352 | |
| 353 | if (unlikely(post_send(priv, priv->tx_head & (IPOIB_TX_RING_SIZE - 1), |
| 354 | address->ah, qpn, addr, skb->len))) { |
| 355 | ipoib_warn(priv, "post_send failed\n"); |
| 356 | ++priv->stats.tx_errors; |
| 357 | dma_unmap_single(priv->ca->dma_device, addr, skb->len, |
| 358 | DMA_TO_DEVICE); |
| 359 | dev_kfree_skb_any(skb); |
| 360 | } else { |
| 361 | dev->trans_start = jiffies; |
| 362 | |
| 363 | address->last_send = priv->tx_head; |
| 364 | ++priv->tx_head; |
| 365 | |
| 366 | if (priv->tx_head - priv->tx_tail == IPOIB_TX_RING_SIZE) { |
| 367 | ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n"); |
| 368 | netif_stop_queue(dev); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | static void __ipoib_reap_ah(struct net_device *dev) |
| 374 | { |
| 375 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 376 | struct ipoib_ah *ah, *tah; |
| 377 | LIST_HEAD(remove_list); |
| 378 | |
| 379 | spin_lock_irq(&priv->lock); |
| 380 | list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list) |
Roland Dreier | 2181858 | 2005-07-27 14:41:32 -0700 | [diff] [blame] | 381 | if ((int) priv->tx_tail - (int) ah->last_send >= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | list_del(&ah->list); |
| 383 | list_add_tail(&ah->list, &remove_list); |
| 384 | } |
| 385 | spin_unlock_irq(&priv->lock); |
| 386 | |
| 387 | list_for_each_entry_safe(ah, tah, &remove_list, list) { |
| 388 | ipoib_dbg(priv, "Reaping ah %p\n", ah->ah); |
| 389 | ib_destroy_ah(ah->ah); |
| 390 | kfree(ah); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | void ipoib_reap_ah(void *dev_ptr) |
| 395 | { |
| 396 | struct net_device *dev = dev_ptr; |
| 397 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 398 | |
| 399 | __ipoib_reap_ah(dev); |
| 400 | |
| 401 | if (!test_bit(IPOIB_STOP_REAPER, &priv->flags)) |
| 402 | queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ); |
| 403 | } |
| 404 | |
| 405 | int ipoib_ib_dev_open(struct net_device *dev) |
| 406 | { |
| 407 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 408 | int ret; |
| 409 | |
Roland Dreier | 5b6810e | 2005-10-11 11:08:24 -0700 | [diff] [blame] | 410 | ret = ipoib_init_qp(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | if (ret) { |
Roland Dreier | 5b6810e | 2005-10-11 11:08:24 -0700 | [diff] [blame] | 412 | ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | return -1; |
| 414 | } |
| 415 | |
| 416 | ret = ipoib_ib_post_receives(dev); |
| 417 | if (ret) { |
| 418 | ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret); |
Eli Cohen | 54d07e2 | 2006-03-02 11:05:19 -0800 | [diff] [blame] | 419 | ipoib_ib_dev_stop(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | return -1; |
| 421 | } |
| 422 | |
| 423 | clear_bit(IPOIB_STOP_REAPER, &priv->flags); |
| 424 | queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ); |
| 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | int ipoib_ib_dev_up(struct net_device *dev) |
| 430 | { |
| 431 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 432 | |
| 433 | set_bit(IPOIB_FLAG_OPER_UP, &priv->flags); |
| 434 | |
| 435 | return ipoib_mcast_start_thread(dev); |
| 436 | } |
| 437 | |
Jack Morgenstein | 0b3ea08 | 2006-03-20 10:08:24 -0800 | [diff] [blame^] | 438 | int ipoib_ib_dev_down(struct net_device *dev, int flush) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | { |
| 440 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 441 | |
| 442 | ipoib_dbg(priv, "downing ib_dev\n"); |
| 443 | |
| 444 | clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags); |
| 445 | netif_carrier_off(dev); |
| 446 | |
| 447 | /* Shutdown the P_Key thread if still active */ |
| 448 | if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) { |
Ingo Molnar | 95ed644 | 2006-01-13 14:51:39 -0800 | [diff] [blame] | 449 | mutex_lock(&pkey_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | set_bit(IPOIB_PKEY_STOP, &priv->flags); |
| 451 | cancel_delayed_work(&priv->pkey_task); |
Ingo Molnar | 95ed644 | 2006-01-13 14:51:39 -0800 | [diff] [blame] | 452 | mutex_unlock(&pkey_mutex); |
Jack Morgenstein | 0b3ea08 | 2006-03-20 10:08:24 -0800 | [diff] [blame^] | 453 | if (flush) |
| 454 | flush_workqueue(ipoib_workqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Jack Morgenstein | 0b3ea08 | 2006-03-20 10:08:24 -0800 | [diff] [blame^] | 457 | ipoib_mcast_stop_thread(dev, flush); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | ipoib_mcast_dev_flush(dev); |
| 459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | ipoib_flush_paths(dev); |
| 461 | |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | static int recvs_pending(struct net_device *dev) |
| 466 | { |
| 467 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 468 | int pending = 0; |
| 469 | int i; |
| 470 | |
| 471 | for (i = 0; i < IPOIB_RX_RING_SIZE; ++i) |
| 472 | if (priv->rx_ring[i].skb) |
| 473 | ++pending; |
| 474 | |
| 475 | return pending; |
| 476 | } |
| 477 | |
| 478 | int ipoib_ib_dev_stop(struct net_device *dev) |
| 479 | { |
| 480 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 481 | struct ib_qp_attr qp_attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | unsigned long begin; |
Roland Dreier | 1993d68 | 2005-10-28 15:30:34 -0700 | [diff] [blame] | 483 | struct ipoib_tx_buf *tx_req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | int i; |
| 485 | |
Roland Dreier | 3bc12e7 | 2005-10-30 13:20:09 -0800 | [diff] [blame] | 486 | /* |
| 487 | * Move our QP to the error state and then reinitialize in |
| 488 | * when all work requests have completed or have been flushed. |
| 489 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | qp_attr.qp_state = IB_QPS_ERR; |
Roland Dreier | 3bc12e7 | 2005-10-30 13:20:09 -0800 | [diff] [blame] | 491 | if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | ipoib_warn(priv, "Failed to modify QP to ERROR state\n"); |
| 493 | |
| 494 | /* Wait for all sends and receives to complete */ |
| 495 | begin = jiffies; |
| 496 | |
| 497 | while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) { |
| 498 | if (time_after(jiffies, begin + 5 * HZ)) { |
| 499 | ipoib_warn(priv, "timing out; %d sends %d receives not completed\n", |
| 500 | priv->tx_head - priv->tx_tail, recvs_pending(dev)); |
| 501 | |
| 502 | /* |
| 503 | * assume the HW is wedged and just free up |
| 504 | * all our pending work requests. |
| 505 | */ |
Roland Dreier | 2181858 | 2005-07-27 14:41:32 -0700 | [diff] [blame] | 506 | while ((int) priv->tx_tail - (int) priv->tx_head < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | tx_req = &priv->tx_ring[priv->tx_tail & |
| 508 | (IPOIB_TX_RING_SIZE - 1)]; |
| 509 | dma_unmap_single(priv->ca->dma_device, |
| 510 | pci_unmap_addr(tx_req, mapping), |
| 511 | tx_req->skb->len, |
| 512 | DMA_TO_DEVICE); |
| 513 | dev_kfree_skb_any(tx_req->skb); |
| 514 | ++priv->tx_tail; |
| 515 | } |
| 516 | |
| 517 | for (i = 0; i < IPOIB_RX_RING_SIZE; ++i) |
| 518 | if (priv->rx_ring[i].skb) { |
| 519 | dma_unmap_single(priv->ca->dma_device, |
| 520 | pci_unmap_addr(&priv->rx_ring[i], |
| 521 | mapping), |
| 522 | IPOIB_BUF_SIZE, |
| 523 | DMA_FROM_DEVICE); |
| 524 | dev_kfree_skb_any(priv->rx_ring[i].skb); |
| 525 | priv->rx_ring[i].skb = NULL; |
| 526 | } |
| 527 | |
| 528 | goto timeout; |
| 529 | } |
| 530 | |
| 531 | msleep(1); |
| 532 | } |
| 533 | |
| 534 | ipoib_dbg(priv, "All sends and receives done.\n"); |
| 535 | |
| 536 | timeout: |
| 537 | qp_attr.qp_state = IB_QPS_RESET; |
Roland Dreier | 3bc12e7 | 2005-10-30 13:20:09 -0800 | [diff] [blame] | 538 | if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | ipoib_warn(priv, "Failed to modify QP to RESET state\n"); |
| 540 | |
| 541 | /* Wait for all AHs to be reaped */ |
| 542 | set_bit(IPOIB_STOP_REAPER, &priv->flags); |
| 543 | cancel_delayed_work(&priv->ah_reap_task); |
| 544 | flush_workqueue(ipoib_workqueue); |
| 545 | |
| 546 | begin = jiffies; |
| 547 | |
| 548 | while (!list_empty(&priv->dead_ahs)) { |
| 549 | __ipoib_reap_ah(dev); |
| 550 | |
| 551 | if (time_after(jiffies, begin + HZ)) { |
| 552 | ipoib_warn(priv, "timing out; will leak address handles\n"); |
| 553 | break; |
| 554 | } |
| 555 | |
| 556 | msleep(1); |
| 557 | } |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port) |
| 563 | { |
| 564 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 565 | |
| 566 | priv->ca = ca; |
| 567 | priv->port = port; |
| 568 | priv->qp = NULL; |
| 569 | |
| 570 | if (ipoib_transport_dev_init(dev, ca)) { |
| 571 | printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name); |
| 572 | return -ENODEV; |
| 573 | } |
| 574 | |
| 575 | if (dev->flags & IFF_UP) { |
| 576 | if (ipoib_ib_dev_open(dev)) { |
| 577 | ipoib_transport_dev_cleanup(dev); |
| 578 | return -ENODEV; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | void ipoib_ib_dev_flush(void *_dev) |
| 586 | { |
| 587 | struct net_device *dev = (struct net_device *)_dev; |
| 588 | struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv; |
| 589 | |
| 590 | if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) |
| 591 | return; |
| 592 | |
| 593 | ipoib_dbg(priv, "flushing\n"); |
| 594 | |
Jack Morgenstein | 0b3ea08 | 2006-03-20 10:08:24 -0800 | [diff] [blame^] | 595 | ipoib_ib_dev_down(dev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
| 597 | /* |
| 598 | * The device could have been brought down between the start and when |
| 599 | * we get here, don't bring it back up if it's not configured up |
| 600 | */ |
| 601 | if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) |
| 602 | ipoib_ib_dev_up(dev); |
| 603 | |
Ingo Molnar | 95ed644 | 2006-01-13 14:51:39 -0800 | [diff] [blame] | 604 | mutex_lock(&priv->vlan_mutex); |
Michael S. Tsirkin | 4f71055 | 2005-11-29 10:53:30 -0800 | [diff] [blame] | 605 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | /* Flush any child interfaces too */ |
| 607 | list_for_each_entry(cpriv, &priv->child_intfs, list) |
| 608 | ipoib_ib_dev_flush(&cpriv->dev); |
Michael S. Tsirkin | 4f71055 | 2005-11-29 10:53:30 -0800 | [diff] [blame] | 609 | |
Ingo Molnar | 95ed644 | 2006-01-13 14:51:39 -0800 | [diff] [blame] | 610 | mutex_unlock(&priv->vlan_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | void ipoib_ib_dev_cleanup(struct net_device *dev) |
| 614 | { |
| 615 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 616 | |
| 617 | ipoib_dbg(priv, "cleaning up ib_dev\n"); |
| 618 | |
Roland Dreier | 8d2cae0 | 2005-09-20 10:52:04 -0700 | [diff] [blame] | 619 | ipoib_mcast_stop_thread(dev, 1); |
Eli Cohen | 988bd50 | 2006-01-12 14:32:20 -0800 | [diff] [blame] | 620 | ipoib_mcast_dev_flush(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
| 622 | ipoib_transport_dev_cleanup(dev); |
| 623 | } |
| 624 | |
| 625 | /* |
| 626 | * Delayed P_Key Assigment Interim Support |
| 627 | * |
| 628 | * The following is initial implementation of delayed P_Key assigment |
| 629 | * mechanism. It is using the same approach implemented for the multicast |
| 630 | * group join. The single goal of this implementation is to quickly address |
| 631 | * Bug #2507. This implementation will probably be removed when the P_Key |
| 632 | * change async notification is available. |
| 633 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | |
| 635 | static void ipoib_pkey_dev_check_presence(struct net_device *dev) |
| 636 | { |
| 637 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 638 | u16 pkey_index = 0; |
| 639 | |
| 640 | if (ib_find_cached_pkey(priv->ca, priv->port, priv->pkey, &pkey_index)) |
| 641 | clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags); |
| 642 | else |
| 643 | set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags); |
| 644 | } |
| 645 | |
| 646 | void ipoib_pkey_poll(void *dev_ptr) |
| 647 | { |
| 648 | struct net_device *dev = dev_ptr; |
| 649 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 650 | |
| 651 | ipoib_pkey_dev_check_presence(dev); |
| 652 | |
| 653 | if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) |
| 654 | ipoib_open(dev); |
| 655 | else { |
Ingo Molnar | 95ed644 | 2006-01-13 14:51:39 -0800 | [diff] [blame] | 656 | mutex_lock(&pkey_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | if (!test_bit(IPOIB_PKEY_STOP, &priv->flags)) |
| 658 | queue_delayed_work(ipoib_workqueue, |
| 659 | &priv->pkey_task, |
| 660 | HZ); |
Ingo Molnar | 95ed644 | 2006-01-13 14:51:39 -0800 | [diff] [blame] | 661 | mutex_unlock(&pkey_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
| 665 | int ipoib_pkey_dev_delay_open(struct net_device *dev) |
| 666 | { |
| 667 | struct ipoib_dev_priv *priv = netdev_priv(dev); |
| 668 | |
| 669 | /* Look for the interface pkey value in the IB Port P_Key table and */ |
| 670 | /* set the interface pkey assigment flag */ |
| 671 | ipoib_pkey_dev_check_presence(dev); |
| 672 | |
| 673 | /* P_Key value not assigned yet - start polling */ |
| 674 | if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) { |
Ingo Molnar | 95ed644 | 2006-01-13 14:51:39 -0800 | [diff] [blame] | 675 | mutex_lock(&pkey_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | clear_bit(IPOIB_PKEY_STOP, &priv->flags); |
| 677 | queue_delayed_work(ipoib_workqueue, |
| 678 | &priv->pkey_task, |
| 679 | HZ); |
Ingo Molnar | 95ed644 | 2006-01-13 14:51:39 -0800 | [diff] [blame] | 680 | mutex_unlock(&pkey_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | return 1; |
| 682 | } |
| 683 | |
| 684 | return 0; |
| 685 | } |