ipoib_ib.c 21 KB

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