ipoib_ib.c 23 KB

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