ipoib_ib.c 21 KB

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