ipoib_ib.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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_reset_mac_header(skb);
  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_receive_skb(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. int ipoib_poll(struct net_device *dev, int *budget)
  233. {
  234. struct ipoib_dev_priv *priv = netdev_priv(dev);
  235. int max = min(*budget, dev->quota);
  236. int done;
  237. int t;
  238. int empty;
  239. int n, i;
  240. done = 0;
  241. empty = 0;
  242. while (max) {
  243. t = min(IPOIB_NUM_WC, max);
  244. n = ib_poll_cq(priv->cq, t, priv->ibwc);
  245. for (i = 0; i < n; ++i) {
  246. struct ib_wc *wc = priv->ibwc + i;
  247. if (wc->wr_id & IPOIB_CM_OP_SRQ) {
  248. ++done;
  249. --max;
  250. ipoib_cm_handle_rx_wc(dev, wc);
  251. } else if (wc->wr_id & IPOIB_OP_RECV) {
  252. ++done;
  253. --max;
  254. ipoib_ib_handle_rx_wc(dev, wc);
  255. } else
  256. ipoib_ib_handle_tx_wc(dev, wc);
  257. }
  258. if (n != t) {
  259. empty = 1;
  260. break;
  261. }
  262. }
  263. dev->quota -= done;
  264. *budget -= done;
  265. if (empty) {
  266. netif_rx_complete(dev);
  267. if (unlikely(ib_req_notify_cq(priv->cq,
  268. IB_CQ_NEXT_COMP |
  269. IB_CQ_REPORT_MISSED_EVENTS)) &&
  270. netif_rx_reschedule(dev, 0))
  271. return 1;
  272. return 0;
  273. }
  274. return 1;
  275. }
  276. void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
  277. {
  278. netif_rx_schedule(dev_ptr);
  279. }
  280. static inline int post_send(struct ipoib_dev_priv *priv,
  281. unsigned int wr_id,
  282. struct ib_ah *address, u32 qpn,
  283. u64 addr, int len)
  284. {
  285. struct ib_send_wr *bad_wr;
  286. priv->tx_sge.addr = addr;
  287. priv->tx_sge.length = len;
  288. priv->tx_wr.wr_id = wr_id;
  289. priv->tx_wr.wr.ud.remote_qpn = qpn;
  290. priv->tx_wr.wr.ud.ah = address;
  291. return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
  292. }
  293. void ipoib_send(struct net_device *dev, struct sk_buff *skb,
  294. struct ipoib_ah *address, u32 qpn)
  295. {
  296. struct ipoib_dev_priv *priv = netdev_priv(dev);
  297. struct ipoib_tx_buf *tx_req;
  298. u64 addr;
  299. if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
  300. ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
  301. skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
  302. ++priv->stats.tx_dropped;
  303. ++priv->stats.tx_errors;
  304. ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
  305. return;
  306. }
  307. ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
  308. skb->len, address, qpn);
  309. /*
  310. * We put the skb into the tx_ring _before_ we call post_send()
  311. * because it's entirely possible that the completion handler will
  312. * run before we execute anything after the post_send(). That
  313. * means we have to make sure everything is properly recorded and
  314. * our state is consistent before we call post_send().
  315. */
  316. tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
  317. tx_req->skb = skb;
  318. addr = ib_dma_map_single(priv->ca, skb->data, skb->len,
  319. DMA_TO_DEVICE);
  320. if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
  321. ++priv->stats.tx_errors;
  322. dev_kfree_skb_any(skb);
  323. return;
  324. }
  325. tx_req->mapping = addr;
  326. if (unlikely(post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
  327. address->ah, qpn, addr, skb->len))) {
  328. ipoib_warn(priv, "post_send failed\n");
  329. ++priv->stats.tx_errors;
  330. ib_dma_unmap_single(priv->ca, addr, skb->len, DMA_TO_DEVICE);
  331. dev_kfree_skb_any(skb);
  332. } else {
  333. dev->trans_start = jiffies;
  334. address->last_send = priv->tx_head;
  335. ++priv->tx_head;
  336. if (priv->tx_head - priv->tx_tail == ipoib_sendq_size) {
  337. ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
  338. netif_stop_queue(dev);
  339. set_bit(IPOIB_FLAG_NETIF_STOPPED, &priv->flags);
  340. }
  341. }
  342. }
  343. static void __ipoib_reap_ah(struct net_device *dev)
  344. {
  345. struct ipoib_dev_priv *priv = netdev_priv(dev);
  346. struct ipoib_ah *ah, *tah;
  347. LIST_HEAD(remove_list);
  348. spin_lock_irq(&priv->tx_lock);
  349. spin_lock(&priv->lock);
  350. list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
  351. if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
  352. list_del(&ah->list);
  353. ib_destroy_ah(ah->ah);
  354. kfree(ah);
  355. }
  356. spin_unlock(&priv->lock);
  357. spin_unlock_irq(&priv->tx_lock);
  358. }
  359. void ipoib_reap_ah(struct work_struct *work)
  360. {
  361. struct ipoib_dev_priv *priv =
  362. container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
  363. struct net_device *dev = priv->dev;
  364. __ipoib_reap_ah(dev);
  365. if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
  366. queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
  367. }
  368. int ipoib_ib_dev_open(struct net_device *dev)
  369. {
  370. struct ipoib_dev_priv *priv = netdev_priv(dev);
  371. int ret;
  372. ret = ipoib_init_qp(dev);
  373. if (ret) {
  374. ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
  375. return -1;
  376. }
  377. ret = ipoib_ib_post_receives(dev);
  378. if (ret) {
  379. ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
  380. ipoib_ib_dev_stop(dev);
  381. return -1;
  382. }
  383. ret = ipoib_cm_dev_open(dev);
  384. if (ret) {
  385. ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
  386. ipoib_ib_dev_stop(dev);
  387. return -1;
  388. }
  389. clear_bit(IPOIB_STOP_REAPER, &priv->flags);
  390. queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
  391. set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
  392. return 0;
  393. }
  394. static void ipoib_pkey_dev_check_presence(struct net_device *dev)
  395. {
  396. struct ipoib_dev_priv *priv = netdev_priv(dev);
  397. u16 pkey_index = 0;
  398. if (ib_find_cached_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
  399. clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  400. else
  401. set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  402. }
  403. int ipoib_ib_dev_up(struct net_device *dev)
  404. {
  405. struct ipoib_dev_priv *priv = netdev_priv(dev);
  406. ipoib_pkey_dev_check_presence(dev);
  407. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  408. ipoib_dbg(priv, "PKEY is not assigned.\n");
  409. return 0;
  410. }
  411. set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
  412. return ipoib_mcast_start_thread(dev);
  413. }
  414. int ipoib_ib_dev_down(struct net_device *dev, int flush)
  415. {
  416. struct ipoib_dev_priv *priv = netdev_priv(dev);
  417. ipoib_dbg(priv, "downing ib_dev\n");
  418. clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
  419. netif_carrier_off(dev);
  420. /* Shutdown the P_Key thread if still active */
  421. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  422. mutex_lock(&pkey_mutex);
  423. set_bit(IPOIB_PKEY_STOP, &priv->flags);
  424. cancel_delayed_work(&priv->pkey_task);
  425. mutex_unlock(&pkey_mutex);
  426. if (flush)
  427. flush_workqueue(ipoib_workqueue);
  428. }
  429. ipoib_mcast_stop_thread(dev, flush);
  430. ipoib_mcast_dev_flush(dev);
  431. ipoib_flush_paths(dev);
  432. return 0;
  433. }
  434. static int recvs_pending(struct net_device *dev)
  435. {
  436. struct ipoib_dev_priv *priv = netdev_priv(dev);
  437. int pending = 0;
  438. int i;
  439. for (i = 0; i < ipoib_recvq_size; ++i)
  440. if (priv->rx_ring[i].skb)
  441. ++pending;
  442. return pending;
  443. }
  444. int ipoib_ib_dev_stop(struct net_device *dev)
  445. {
  446. struct ipoib_dev_priv *priv = netdev_priv(dev);
  447. struct ib_qp_attr qp_attr;
  448. unsigned long begin;
  449. struct ipoib_tx_buf *tx_req;
  450. int i, n;
  451. clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
  452. netif_poll_disable(dev);
  453. ipoib_cm_dev_stop(dev);
  454. /*
  455. * Move our QP to the error state and then reinitialize in
  456. * when all work requests have completed or have been flushed.
  457. */
  458. qp_attr.qp_state = IB_QPS_ERR;
  459. if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
  460. ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
  461. /* Wait for all sends and receives to complete */
  462. begin = jiffies;
  463. while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
  464. if (time_after(jiffies, begin + 5 * HZ)) {
  465. ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
  466. priv->tx_head - priv->tx_tail, recvs_pending(dev));
  467. /*
  468. * assume the HW is wedged and just free up
  469. * all our pending work requests.
  470. */
  471. while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
  472. tx_req = &priv->tx_ring[priv->tx_tail &
  473. (ipoib_sendq_size - 1)];
  474. ib_dma_unmap_single(priv->ca,
  475. tx_req->mapping,
  476. tx_req->skb->len,
  477. DMA_TO_DEVICE);
  478. dev_kfree_skb_any(tx_req->skb);
  479. ++priv->tx_tail;
  480. }
  481. for (i = 0; i < ipoib_recvq_size; ++i) {
  482. struct ipoib_rx_buf *rx_req;
  483. rx_req = &priv->rx_ring[i];
  484. if (!rx_req->skb)
  485. continue;
  486. ib_dma_unmap_single(priv->ca,
  487. rx_req->mapping,
  488. IPOIB_BUF_SIZE,
  489. DMA_FROM_DEVICE);
  490. dev_kfree_skb_any(rx_req->skb);
  491. rx_req->skb = NULL;
  492. }
  493. goto timeout;
  494. }
  495. do {
  496. n = ib_poll_cq(priv->cq, IPOIB_NUM_WC, priv->ibwc);
  497. for (i = 0; i < n; ++i) {
  498. if (priv->ibwc[i].wr_id & IPOIB_CM_OP_SRQ)
  499. ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
  500. else if (priv->ibwc[i].wr_id & IPOIB_OP_RECV)
  501. ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
  502. else
  503. ipoib_ib_handle_tx_wc(dev, priv->ibwc + i);
  504. }
  505. } while (n == IPOIB_NUM_WC);
  506. msleep(1);
  507. }
  508. ipoib_dbg(priv, "All sends and receives done.\n");
  509. timeout:
  510. qp_attr.qp_state = IB_QPS_RESET;
  511. if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
  512. ipoib_warn(priv, "Failed to modify QP to RESET state\n");
  513. /* Wait for all AHs to be reaped */
  514. set_bit(IPOIB_STOP_REAPER, &priv->flags);
  515. cancel_delayed_work(&priv->ah_reap_task);
  516. flush_workqueue(ipoib_workqueue);
  517. begin = jiffies;
  518. while (!list_empty(&priv->dead_ahs)) {
  519. __ipoib_reap_ah(dev);
  520. if (time_after(jiffies, begin + HZ)) {
  521. ipoib_warn(priv, "timing out; will leak address handles\n");
  522. break;
  523. }
  524. msleep(1);
  525. }
  526. netif_poll_enable(dev);
  527. ib_req_notify_cq(priv->cq, IB_CQ_NEXT_COMP);
  528. return 0;
  529. }
  530. int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
  531. {
  532. struct ipoib_dev_priv *priv = netdev_priv(dev);
  533. priv->ca = ca;
  534. priv->port = port;
  535. priv->qp = NULL;
  536. if (ipoib_transport_dev_init(dev, ca)) {
  537. printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
  538. return -ENODEV;
  539. }
  540. if (dev->flags & IFF_UP) {
  541. if (ipoib_ib_dev_open(dev)) {
  542. ipoib_transport_dev_cleanup(dev);
  543. return -ENODEV;
  544. }
  545. }
  546. return 0;
  547. }
  548. void ipoib_ib_dev_flush(struct work_struct *work)
  549. {
  550. struct ipoib_dev_priv *cpriv, *priv =
  551. container_of(work, struct ipoib_dev_priv, flush_task);
  552. struct net_device *dev = priv->dev;
  553. if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags) ) {
  554. ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
  555. return;
  556. }
  557. if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
  558. ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
  559. return;
  560. }
  561. ipoib_dbg(priv, "flushing\n");
  562. ipoib_ib_dev_down(dev, 0);
  563. /*
  564. * The device could have been brought down between the start and when
  565. * we get here, don't bring it back up if it's not configured up
  566. */
  567. if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
  568. ipoib_ib_dev_up(dev);
  569. ipoib_mcast_restart_task(&priv->restart_task);
  570. }
  571. mutex_lock(&priv->vlan_mutex);
  572. /* Flush any child interfaces too */
  573. list_for_each_entry(cpriv, &priv->child_intfs, list)
  574. ipoib_ib_dev_flush(&cpriv->flush_task);
  575. mutex_unlock(&priv->vlan_mutex);
  576. }
  577. void ipoib_ib_dev_cleanup(struct net_device *dev)
  578. {
  579. struct ipoib_dev_priv *priv = netdev_priv(dev);
  580. ipoib_dbg(priv, "cleaning up ib_dev\n");
  581. ipoib_mcast_stop_thread(dev, 1);
  582. ipoib_mcast_dev_flush(dev);
  583. ipoib_transport_dev_cleanup(dev);
  584. }
  585. /*
  586. * Delayed P_Key Assigment Interim Support
  587. *
  588. * The following is initial implementation of delayed P_Key assigment
  589. * mechanism. It is using the same approach implemented for the multicast
  590. * group join. The single goal of this implementation is to quickly address
  591. * Bug #2507. This implementation will probably be removed when the P_Key
  592. * change async notification is available.
  593. */
  594. void ipoib_pkey_poll(struct work_struct *work)
  595. {
  596. struct ipoib_dev_priv *priv =
  597. container_of(work, struct ipoib_dev_priv, pkey_task.work);
  598. struct net_device *dev = priv->dev;
  599. ipoib_pkey_dev_check_presence(dev);
  600. if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
  601. ipoib_open(dev);
  602. else {
  603. mutex_lock(&pkey_mutex);
  604. if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
  605. queue_delayed_work(ipoib_workqueue,
  606. &priv->pkey_task,
  607. HZ);
  608. mutex_unlock(&pkey_mutex);
  609. }
  610. }
  611. int ipoib_pkey_dev_delay_open(struct net_device *dev)
  612. {
  613. struct ipoib_dev_priv *priv = netdev_priv(dev);
  614. /* Look for the interface pkey value in the IB Port P_Key table and */
  615. /* set the interface pkey assigment flag */
  616. ipoib_pkey_dev_check_presence(dev);
  617. /* P_Key value not assigned yet - start polling */
  618. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  619. mutex_lock(&pkey_mutex);
  620. clear_bit(IPOIB_PKEY_STOP, &priv->flags);
  621. queue_delayed_work(ipoib_workqueue,
  622. &priv->pkey_task,
  623. HZ);
  624. mutex_unlock(&pkey_mutex);
  625. return 1;
  626. }
  627. return 0;
  628. }