ipoib_ib.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. /*
  2. * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
  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.
  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. #include <linux/delay.h>
  38. #include <linux/dma-mapping.h>
  39. #include <rdma/ib_cache.h>
  40. #include "ipoib.h"
  41. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
  42. static int data_debug_level;
  43. module_param(data_debug_level, int, 0644);
  44. MODULE_PARM_DESC(data_debug_level,
  45. "Enable data path debug tracing if > 0");
  46. #endif
  47. static DEFINE_MUTEX(pkey_mutex);
  48. struct ipoib_ah *ipoib_create_ah(struct net_device *dev,
  49. struct ib_pd *pd, struct ib_ah_attr *attr)
  50. {
  51. struct ipoib_ah *ah;
  52. ah = kmalloc(sizeof *ah, GFP_KERNEL);
  53. if (!ah)
  54. return NULL;
  55. ah->dev = dev;
  56. ah->last_send = 0;
  57. kref_init(&ah->ref);
  58. ah->ah = ib_create_ah(pd, attr);
  59. if (IS_ERR(ah->ah)) {
  60. kfree(ah);
  61. ah = NULL;
  62. } else
  63. ipoib_dbg(netdev_priv(dev), "Created ah %p\n", ah->ah);
  64. return ah;
  65. }
  66. void ipoib_free_ah(struct kref *kref)
  67. {
  68. struct ipoib_ah *ah = container_of(kref, struct ipoib_ah, ref);
  69. struct ipoib_dev_priv *priv = netdev_priv(ah->dev);
  70. unsigned long flags;
  71. spin_lock_irqsave(&priv->lock, flags);
  72. list_add_tail(&ah->list, &priv->dead_ahs);
  73. spin_unlock_irqrestore(&priv->lock, flags);
  74. }
  75. static int ipoib_ib_post_receive(struct net_device *dev, int id)
  76. {
  77. struct ipoib_dev_priv *priv = netdev_priv(dev);
  78. struct ib_sge list;
  79. struct ib_recv_wr param;
  80. struct ib_recv_wr *bad_wr;
  81. int ret;
  82. list.addr = priv->rx_ring[id].mapping;
  83. list.length = IPOIB_BUF_SIZE;
  84. list.lkey = priv->mr->lkey;
  85. param.next = NULL;
  86. param.wr_id = id | IPOIB_OP_RECV;
  87. param.sg_list = &list;
  88. param.num_sge = 1;
  89. ret = ib_post_recv(priv->qp, &param, &bad_wr);
  90. if (unlikely(ret)) {
  91. ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);
  92. ib_dma_unmap_single(priv->ca, priv->rx_ring[id].mapping,
  93. IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
  94. dev_kfree_skb_any(priv->rx_ring[id].skb);
  95. priv->rx_ring[id].skb = NULL;
  96. }
  97. return ret;
  98. }
  99. static int ipoib_alloc_rx_skb(struct net_device *dev, int id)
  100. {
  101. struct ipoib_dev_priv *priv = netdev_priv(dev);
  102. struct sk_buff *skb;
  103. u64 addr;
  104. skb = dev_alloc_skb(IPOIB_BUF_SIZE + 4);
  105. if (!skb)
  106. return -ENOMEM;
  107. /*
  108. * IB will leave a 40 byte gap for a GRH and IPoIB adds a 4 byte
  109. * header. So we need 4 more bytes to get to 48 and align the
  110. * IP header to a multiple of 16.
  111. */
  112. skb_reserve(skb, 4);
  113. addr = ib_dma_map_single(priv->ca, skb->data, IPOIB_BUF_SIZE,
  114. DMA_FROM_DEVICE);
  115. if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
  116. dev_kfree_skb_any(skb);
  117. return -EIO;
  118. }
  119. priv->rx_ring[id].skb = skb;
  120. priv->rx_ring[id].mapping = addr;
  121. return 0;
  122. }
  123. static int ipoib_ib_post_receives(struct net_device *dev)
  124. {
  125. struct ipoib_dev_priv *priv = netdev_priv(dev);
  126. int i;
  127. for (i = 0; i < ipoib_recvq_size; ++i) {
  128. if (ipoib_alloc_rx_skb(dev, i)) {
  129. ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
  130. return -ENOMEM;
  131. }
  132. if (ipoib_ib_post_receive(dev, i)) {
  133. ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
  134. return -EIO;
  135. }
  136. }
  137. return 0;
  138. }
  139. static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
  140. {
  141. struct ipoib_dev_priv *priv = netdev_priv(dev);
  142. unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
  143. struct sk_buff *skb;
  144. u64 addr;
  145. ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
  146. wr_id, wc->status);
  147. if (unlikely(wr_id >= ipoib_recvq_size)) {
  148. ipoib_warn(priv, "recv completion event with wrid %d (> %d)\n",
  149. wr_id, ipoib_recvq_size);
  150. return;
  151. }
  152. skb = priv->rx_ring[wr_id].skb;
  153. addr = priv->rx_ring[wr_id].mapping;
  154. if (unlikely(wc->status != IB_WC_SUCCESS)) {
  155. if (wc->status != IB_WC_WR_FLUSH_ERR)
  156. ipoib_warn(priv, "failed recv event "
  157. "(status=%d, wrid=%d vend_err %x)\n",
  158. wc->status, wr_id, wc->vendor_err);
  159. ib_dma_unmap_single(priv->ca, addr,
  160. IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
  161. dev_kfree_skb_any(skb);
  162. priv->rx_ring[wr_id].skb = NULL;
  163. return;
  164. }
  165. /*
  166. * If we can't allocate a new RX buffer, dump
  167. * this packet and reuse the old buffer.
  168. */
  169. if (unlikely(ipoib_alloc_rx_skb(dev, wr_id))) {
  170. ++priv->stats.rx_dropped;
  171. goto repost;
  172. }
  173. ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
  174. wc->byte_len, wc->slid);
  175. ib_dma_unmap_single(priv->ca, addr, IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
  176. skb_put(skb, wc->byte_len);
  177. skb_pull(skb, IB_GRH_BYTES);
  178. if (wc->slid != priv->local_lid ||
  179. wc->src_qp != priv->qp->qp_num) {
  180. skb->protocol = ((struct ipoib_header *) skb->data)->proto;
  181. skb->mac.raw = skb->data;
  182. skb_pull(skb, IPOIB_ENCAP_LEN);
  183. dev->last_rx = jiffies;
  184. ++priv->stats.rx_packets;
  185. priv->stats.rx_bytes += skb->len;
  186. skb->dev = dev;
  187. /* XXX get correct PACKET_ type here */
  188. skb->pkt_type = PACKET_HOST;
  189. netif_rx_ni(skb);
  190. } else {
  191. ipoib_dbg_data(priv, "dropping loopback packet\n");
  192. dev_kfree_skb_any(skb);
  193. }
  194. repost:
  195. if (unlikely(ipoib_ib_post_receive(dev, wr_id)))
  196. ipoib_warn(priv, "ipoib_ib_post_receive failed "
  197. "for buf %d\n", wr_id);
  198. }
  199. static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
  200. {
  201. struct ipoib_dev_priv *priv = netdev_priv(dev);
  202. unsigned int wr_id = wc->wr_id;
  203. struct ipoib_tx_buf *tx_req;
  204. unsigned long flags;
  205. ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
  206. wr_id, wc->status);
  207. if (unlikely(wr_id >= ipoib_sendq_size)) {
  208. ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
  209. wr_id, ipoib_sendq_size);
  210. return;
  211. }
  212. tx_req = &priv->tx_ring[wr_id];
  213. ib_dma_unmap_single(priv->ca, tx_req->mapping,
  214. tx_req->skb->len, DMA_TO_DEVICE);
  215. ++priv->stats.tx_packets;
  216. priv->stats.tx_bytes += tx_req->skb->len;
  217. dev_kfree_skb_any(tx_req->skb);
  218. spin_lock_irqsave(&priv->tx_lock, flags);
  219. ++priv->tx_tail;
  220. if (unlikely(test_bit(IPOIB_FLAG_NETIF_STOPPED, &priv->flags)) &&
  221. priv->tx_head - priv->tx_tail <= ipoib_sendq_size >> 1) {
  222. clear_bit(IPOIB_FLAG_NETIF_STOPPED, &priv->flags);
  223. netif_wake_queue(dev);
  224. }
  225. spin_unlock_irqrestore(&priv->tx_lock, flags);
  226. if (wc->status != IB_WC_SUCCESS &&
  227. wc->status != IB_WC_WR_FLUSH_ERR)
  228. ipoib_warn(priv, "failed send event "
  229. "(status=%d, wrid=%d vend_err %x)\n",
  230. wc->status, wr_id, wc->vendor_err);
  231. }
  232. static void ipoib_ib_handle_wc(struct net_device *dev, struct ib_wc *wc)
  233. {
  234. if (wc->wr_id & IPOIB_CM_OP_SRQ)
  235. ipoib_cm_handle_rx_wc(dev, wc);
  236. else if (wc->wr_id & IPOIB_OP_RECV)
  237. ipoib_ib_handle_rx_wc(dev, wc);
  238. else
  239. ipoib_ib_handle_tx_wc(dev, wc);
  240. }
  241. void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
  242. {
  243. struct net_device *dev = (struct net_device *) dev_ptr;
  244. struct ipoib_dev_priv *priv = netdev_priv(dev);
  245. int n, i;
  246. ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
  247. do {
  248. n = ib_poll_cq(cq, IPOIB_NUM_WC, priv->ibwc);
  249. for (i = 0; i < n; ++i)
  250. ipoib_ib_handle_wc(dev, priv->ibwc + i);
  251. } while (n == IPOIB_NUM_WC);
  252. }
  253. static inline int post_send(struct ipoib_dev_priv *priv,
  254. unsigned int wr_id,
  255. struct ib_ah *address, u32 qpn,
  256. u64 addr, int len)
  257. {
  258. struct ib_send_wr *bad_wr;
  259. priv->tx_sge.addr = addr;
  260. priv->tx_sge.length = len;
  261. priv->tx_wr.wr_id = wr_id;
  262. priv->tx_wr.wr.ud.remote_qpn = qpn;
  263. priv->tx_wr.wr.ud.ah = address;
  264. return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
  265. }
  266. void ipoib_send(struct net_device *dev, struct sk_buff *skb,
  267. struct ipoib_ah *address, u32 qpn)
  268. {
  269. struct ipoib_dev_priv *priv = netdev_priv(dev);
  270. struct ipoib_tx_buf *tx_req;
  271. u64 addr;
  272. if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
  273. ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
  274. skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
  275. ++priv->stats.tx_dropped;
  276. ++priv->stats.tx_errors;
  277. ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
  278. return;
  279. }
  280. ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
  281. skb->len, address, qpn);
  282. /*
  283. * We put the skb into the tx_ring _before_ we call post_send()
  284. * because it's entirely possible that the completion handler will
  285. * run before we execute anything after the post_send(). That
  286. * means we have to make sure everything is properly recorded and
  287. * our state is consistent before we call post_send().
  288. */
  289. tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
  290. tx_req->skb = skb;
  291. addr = ib_dma_map_single(priv->ca, skb->data, skb->len,
  292. DMA_TO_DEVICE);
  293. if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
  294. ++priv->stats.tx_errors;
  295. dev_kfree_skb_any(skb);
  296. return;
  297. }
  298. tx_req->mapping = addr;
  299. if (unlikely(post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
  300. address->ah, qpn, addr, skb->len))) {
  301. ipoib_warn(priv, "post_send failed\n");
  302. ++priv->stats.tx_errors;
  303. ib_dma_unmap_single(priv->ca, addr, skb->len, DMA_TO_DEVICE);
  304. dev_kfree_skb_any(skb);
  305. } else {
  306. dev->trans_start = jiffies;
  307. address->last_send = priv->tx_head;
  308. ++priv->tx_head;
  309. if (priv->tx_head - priv->tx_tail == ipoib_sendq_size) {
  310. ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
  311. netif_stop_queue(dev);
  312. set_bit(IPOIB_FLAG_NETIF_STOPPED, &priv->flags);
  313. }
  314. }
  315. }
  316. static void __ipoib_reap_ah(struct net_device *dev)
  317. {
  318. struct ipoib_dev_priv *priv = netdev_priv(dev);
  319. struct ipoib_ah *ah, *tah;
  320. LIST_HEAD(remove_list);
  321. spin_lock_irq(&priv->tx_lock);
  322. spin_lock(&priv->lock);
  323. list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
  324. if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
  325. list_del(&ah->list);
  326. ib_destroy_ah(ah->ah);
  327. kfree(ah);
  328. }
  329. spin_unlock(&priv->lock);
  330. spin_unlock_irq(&priv->tx_lock);
  331. }
  332. void ipoib_reap_ah(struct work_struct *work)
  333. {
  334. struct ipoib_dev_priv *priv =
  335. container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
  336. struct net_device *dev = priv->dev;
  337. __ipoib_reap_ah(dev);
  338. if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
  339. queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
  340. }
  341. int ipoib_ib_dev_open(struct net_device *dev)
  342. {
  343. struct ipoib_dev_priv *priv = netdev_priv(dev);
  344. int ret;
  345. ret = ipoib_init_qp(dev);
  346. if (ret) {
  347. ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
  348. return -1;
  349. }
  350. ret = ipoib_ib_post_receives(dev);
  351. if (ret) {
  352. ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
  353. ipoib_ib_dev_stop(dev);
  354. return -1;
  355. }
  356. ret = ipoib_cm_dev_open(dev);
  357. if (ret) {
  358. ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
  359. ipoib_ib_dev_stop(dev);
  360. return -1;
  361. }
  362. clear_bit(IPOIB_STOP_REAPER, &priv->flags);
  363. queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
  364. set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
  365. return 0;
  366. }
  367. static void ipoib_pkey_dev_check_presence(struct net_device *dev)
  368. {
  369. struct ipoib_dev_priv *priv = netdev_priv(dev);
  370. u16 pkey_index = 0;
  371. if (ib_find_cached_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
  372. clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  373. else
  374. set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  375. }
  376. int ipoib_ib_dev_up(struct net_device *dev)
  377. {
  378. struct ipoib_dev_priv *priv = netdev_priv(dev);
  379. ipoib_pkey_dev_check_presence(dev);
  380. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  381. ipoib_dbg(priv, "PKEY is not assigned.\n");
  382. return 0;
  383. }
  384. set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
  385. return ipoib_mcast_start_thread(dev);
  386. }
  387. int ipoib_ib_dev_down(struct net_device *dev, int flush)
  388. {
  389. struct ipoib_dev_priv *priv = netdev_priv(dev);
  390. ipoib_dbg(priv, "downing ib_dev\n");
  391. clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
  392. netif_carrier_off(dev);
  393. /* Shutdown the P_Key thread if still active */
  394. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  395. mutex_lock(&pkey_mutex);
  396. set_bit(IPOIB_PKEY_STOP, &priv->flags);
  397. cancel_delayed_work(&priv->pkey_task);
  398. mutex_unlock(&pkey_mutex);
  399. if (flush)
  400. flush_workqueue(ipoib_workqueue);
  401. }
  402. ipoib_mcast_stop_thread(dev, flush);
  403. ipoib_mcast_dev_flush(dev);
  404. ipoib_flush_paths(dev);
  405. return 0;
  406. }
  407. static int recvs_pending(struct net_device *dev)
  408. {
  409. struct ipoib_dev_priv *priv = netdev_priv(dev);
  410. int pending = 0;
  411. int i;
  412. for (i = 0; i < ipoib_recvq_size; ++i)
  413. if (priv->rx_ring[i].skb)
  414. ++pending;
  415. return pending;
  416. }
  417. int ipoib_ib_dev_stop(struct net_device *dev)
  418. {
  419. struct ipoib_dev_priv *priv = netdev_priv(dev);
  420. struct ib_qp_attr qp_attr;
  421. unsigned long begin;
  422. struct ipoib_tx_buf *tx_req;
  423. int i;
  424. clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
  425. ipoib_cm_dev_stop(dev);
  426. /*
  427. * Move our QP to the error state and then reinitialize in
  428. * when all work requests have completed or have been flushed.
  429. */
  430. qp_attr.qp_state = IB_QPS_ERR;
  431. if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
  432. ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
  433. /* Wait for all sends and receives to complete */
  434. begin = jiffies;
  435. while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
  436. if (time_after(jiffies, begin + 5 * HZ)) {
  437. ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
  438. priv->tx_head - priv->tx_tail, recvs_pending(dev));
  439. /*
  440. * assume the HW is wedged and just free up
  441. * all our pending work requests.
  442. */
  443. while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
  444. tx_req = &priv->tx_ring[priv->tx_tail &
  445. (ipoib_sendq_size - 1)];
  446. ib_dma_unmap_single(priv->ca,
  447. tx_req->mapping,
  448. tx_req->skb->len,
  449. DMA_TO_DEVICE);
  450. dev_kfree_skb_any(tx_req->skb);
  451. ++priv->tx_tail;
  452. }
  453. for (i = 0; i < ipoib_recvq_size; ++i) {
  454. struct ipoib_rx_buf *rx_req;
  455. rx_req = &priv->rx_ring[i];
  456. if (!rx_req->skb)
  457. continue;
  458. ib_dma_unmap_single(priv->ca,
  459. rx_req->mapping,
  460. IPOIB_BUF_SIZE,
  461. DMA_FROM_DEVICE);
  462. dev_kfree_skb_any(rx_req->skb);
  463. rx_req->skb = NULL;
  464. }
  465. goto timeout;
  466. }
  467. msleep(1);
  468. }
  469. ipoib_dbg(priv, "All sends and receives done.\n");
  470. timeout:
  471. qp_attr.qp_state = IB_QPS_RESET;
  472. if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
  473. ipoib_warn(priv, "Failed to modify QP to RESET state\n");
  474. /* Wait for all AHs to be reaped */
  475. set_bit(IPOIB_STOP_REAPER, &priv->flags);
  476. cancel_delayed_work(&priv->ah_reap_task);
  477. flush_workqueue(ipoib_workqueue);
  478. begin = jiffies;
  479. while (!list_empty(&priv->dead_ahs)) {
  480. __ipoib_reap_ah(dev);
  481. if (time_after(jiffies, begin + HZ)) {
  482. ipoib_warn(priv, "timing out; will leak address handles\n");
  483. break;
  484. }
  485. msleep(1);
  486. }
  487. return 0;
  488. }
  489. int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
  490. {
  491. struct ipoib_dev_priv *priv = netdev_priv(dev);
  492. priv->ca = ca;
  493. priv->port = port;
  494. priv->qp = NULL;
  495. if (ipoib_transport_dev_init(dev, ca)) {
  496. printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
  497. return -ENODEV;
  498. }
  499. if (dev->flags & IFF_UP) {
  500. if (ipoib_ib_dev_open(dev)) {
  501. ipoib_transport_dev_cleanup(dev);
  502. return -ENODEV;
  503. }
  504. }
  505. return 0;
  506. }
  507. void ipoib_ib_dev_flush(struct work_struct *work)
  508. {
  509. struct ipoib_dev_priv *cpriv, *priv =
  510. container_of(work, struct ipoib_dev_priv, flush_task);
  511. struct net_device *dev = priv->dev;
  512. if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags) ) {
  513. ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
  514. return;
  515. }
  516. if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
  517. ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
  518. return;
  519. }
  520. ipoib_dbg(priv, "flushing\n");
  521. ipoib_ib_dev_down(dev, 0);
  522. /*
  523. * The device could have been brought down between the start and when
  524. * we get here, don't bring it back up if it's not configured up
  525. */
  526. if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
  527. ipoib_ib_dev_up(dev);
  528. ipoib_mcast_restart_task(&priv->restart_task);
  529. }
  530. mutex_lock(&priv->vlan_mutex);
  531. /* Flush any child interfaces too */
  532. list_for_each_entry(cpriv, &priv->child_intfs, list)
  533. ipoib_ib_dev_flush(&cpriv->flush_task);
  534. mutex_unlock(&priv->vlan_mutex);
  535. }
  536. void ipoib_ib_dev_cleanup(struct net_device *dev)
  537. {
  538. struct ipoib_dev_priv *priv = netdev_priv(dev);
  539. ipoib_dbg(priv, "cleaning up ib_dev\n");
  540. ipoib_mcast_stop_thread(dev, 1);
  541. ipoib_mcast_dev_flush(dev);
  542. ipoib_transport_dev_cleanup(dev);
  543. }
  544. /*
  545. * Delayed P_Key Assigment Interim Support
  546. *
  547. * The following is initial implementation of delayed P_Key assigment
  548. * mechanism. It is using the same approach implemented for the multicast
  549. * group join. The single goal of this implementation is to quickly address
  550. * Bug #2507. This implementation will probably be removed when the P_Key
  551. * change async notification is available.
  552. */
  553. void ipoib_pkey_poll(struct work_struct *work)
  554. {
  555. struct ipoib_dev_priv *priv =
  556. container_of(work, struct ipoib_dev_priv, pkey_task.work);
  557. struct net_device *dev = priv->dev;
  558. ipoib_pkey_dev_check_presence(dev);
  559. if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
  560. ipoib_open(dev);
  561. else {
  562. mutex_lock(&pkey_mutex);
  563. if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
  564. queue_delayed_work(ipoib_workqueue,
  565. &priv->pkey_task,
  566. HZ);
  567. mutex_unlock(&pkey_mutex);
  568. }
  569. }
  570. int ipoib_pkey_dev_delay_open(struct net_device *dev)
  571. {
  572. struct ipoib_dev_priv *priv = netdev_priv(dev);
  573. /* Look for the interface pkey value in the IB Port P_Key table and */
  574. /* set the interface pkey assigment flag */
  575. ipoib_pkey_dev_check_presence(dev);
  576. /* P_Key value not assigned yet - start polling */
  577. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  578. mutex_lock(&pkey_mutex);
  579. clear_bit(IPOIB_PKEY_STOP, &priv->flags);
  580. queue_delayed_work(ipoib_workqueue,
  581. &priv->pkey_task,
  582. HZ);
  583. mutex_unlock(&pkey_mutex);
  584. return 1;
  585. }
  586. return 0;
  587. }