ipoib_ib.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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 <linux/ip.h>
  41. #include <linux/tcp.h>
  42. #include "ipoib.h"
  43. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
  44. static int data_debug_level;
  45. module_param(data_debug_level, int, 0644);
  46. MODULE_PARM_DESC(data_debug_level,
  47. "Enable data path debug tracing if > 0");
  48. #endif
  49. static DEFINE_MUTEX(pkey_mutex);
  50. struct ipoib_ah *ipoib_create_ah(struct net_device *dev,
  51. struct ib_pd *pd, struct ib_ah_attr *attr)
  52. {
  53. struct ipoib_ah *ah;
  54. ah = kmalloc(sizeof *ah, GFP_KERNEL);
  55. if (!ah)
  56. return NULL;
  57. ah->dev = dev;
  58. ah->last_send = 0;
  59. kref_init(&ah->ref);
  60. ah->ah = ib_create_ah(pd, attr);
  61. if (IS_ERR(ah->ah)) {
  62. kfree(ah);
  63. ah = NULL;
  64. } else
  65. ipoib_dbg(netdev_priv(dev), "Created ah %p\n", ah->ah);
  66. return ah;
  67. }
  68. void ipoib_free_ah(struct kref *kref)
  69. {
  70. struct ipoib_ah *ah = container_of(kref, struct ipoib_ah, ref);
  71. struct ipoib_dev_priv *priv = netdev_priv(ah->dev);
  72. unsigned long flags;
  73. spin_lock_irqsave(&priv->lock, flags);
  74. list_add_tail(&ah->list, &priv->dead_ahs);
  75. spin_unlock_irqrestore(&priv->lock, flags);
  76. }
  77. static int ipoib_ib_post_receive(struct net_device *dev, int id)
  78. {
  79. struct ipoib_dev_priv *priv = netdev_priv(dev);
  80. struct ib_sge list;
  81. struct ib_recv_wr param;
  82. struct ib_recv_wr *bad_wr;
  83. int ret;
  84. list.addr = priv->rx_ring[id].mapping;
  85. list.length = IPOIB_BUF_SIZE;
  86. list.lkey = priv->mr->lkey;
  87. param.next = NULL;
  88. param.wr_id = id | IPOIB_OP_RECV;
  89. param.sg_list = &list;
  90. param.num_sge = 1;
  91. ret = ib_post_recv(priv->qp, &param, &bad_wr);
  92. if (unlikely(ret)) {
  93. ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);
  94. ib_dma_unmap_single(priv->ca, priv->rx_ring[id].mapping,
  95. IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
  96. dev_kfree_skb_any(priv->rx_ring[id].skb);
  97. priv->rx_ring[id].skb = NULL;
  98. }
  99. return ret;
  100. }
  101. static int ipoib_alloc_rx_skb(struct net_device *dev, int id)
  102. {
  103. struct ipoib_dev_priv *priv = netdev_priv(dev);
  104. struct sk_buff *skb;
  105. u64 addr;
  106. skb = dev_alloc_skb(IPOIB_BUF_SIZE + 4);
  107. if (!skb)
  108. return -ENOMEM;
  109. /*
  110. * IB will leave a 40 byte gap for a GRH and IPoIB adds a 4 byte
  111. * header. So we need 4 more bytes to get to 48 and align the
  112. * IP header to a multiple of 16.
  113. */
  114. skb_reserve(skb, 4);
  115. addr = ib_dma_map_single(priv->ca, skb->data, IPOIB_BUF_SIZE,
  116. DMA_FROM_DEVICE);
  117. if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
  118. dev_kfree_skb_any(skb);
  119. return -EIO;
  120. }
  121. priv->rx_ring[id].skb = skb;
  122. priv->rx_ring[id].mapping = addr;
  123. return 0;
  124. }
  125. static int ipoib_ib_post_receives(struct net_device *dev)
  126. {
  127. struct ipoib_dev_priv *priv = netdev_priv(dev);
  128. int i;
  129. for (i = 0; i < ipoib_recvq_size; ++i) {
  130. if (ipoib_alloc_rx_skb(dev, i)) {
  131. ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
  132. return -ENOMEM;
  133. }
  134. if (ipoib_ib_post_receive(dev, i)) {
  135. ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
  136. return -EIO;
  137. }
  138. }
  139. return 0;
  140. }
  141. static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
  142. {
  143. struct ipoib_dev_priv *priv = netdev_priv(dev);
  144. unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
  145. struct sk_buff *skb;
  146. u64 addr;
  147. ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
  148. wr_id, wc->status);
  149. if (unlikely(wr_id >= ipoib_recvq_size)) {
  150. ipoib_warn(priv, "recv completion event with wrid %d (> %d)\n",
  151. wr_id, ipoib_recvq_size);
  152. return;
  153. }
  154. skb = priv->rx_ring[wr_id].skb;
  155. addr = priv->rx_ring[wr_id].mapping;
  156. if (unlikely(wc->status != IB_WC_SUCCESS)) {
  157. if (wc->status != IB_WC_WR_FLUSH_ERR)
  158. ipoib_warn(priv, "failed recv event "
  159. "(status=%d, wrid=%d vend_err %x)\n",
  160. wc->status, wr_id, wc->vendor_err);
  161. ib_dma_unmap_single(priv->ca, addr,
  162. IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
  163. dev_kfree_skb_any(skb);
  164. priv->rx_ring[wr_id].skb = NULL;
  165. return;
  166. }
  167. /*
  168. * Drop packets that this interface sent, ie multicast packets
  169. * that the HCA has replicated.
  170. */
  171. if (wc->slid == priv->local_lid && wc->src_qp == priv->qp->qp_num)
  172. goto repost;
  173. /*
  174. * If we can't allocate a new RX buffer, dump
  175. * this packet and reuse the old buffer.
  176. */
  177. if (unlikely(ipoib_alloc_rx_skb(dev, wr_id))) {
  178. ++dev->stats.rx_dropped;
  179. goto repost;
  180. }
  181. ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
  182. wc->byte_len, wc->slid);
  183. ib_dma_unmap_single(priv->ca, addr, IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
  184. skb_put(skb, wc->byte_len);
  185. skb_pull(skb, IB_GRH_BYTES);
  186. skb->protocol = ((struct ipoib_header *) skb->data)->proto;
  187. skb_reset_mac_header(skb);
  188. skb_pull(skb, IPOIB_ENCAP_LEN);
  189. dev->last_rx = jiffies;
  190. ++dev->stats.rx_packets;
  191. dev->stats.rx_bytes += skb->len;
  192. skb->dev = dev;
  193. /* XXX get correct PACKET_ type here */
  194. skb->pkt_type = PACKET_HOST;
  195. if (test_bit(IPOIB_FLAG_CSUM, &priv->flags) && likely(wc->csum_ok))
  196. skb->ip_summed = CHECKSUM_UNNECESSARY;
  197. netif_receive_skb(skb);
  198. repost:
  199. if (unlikely(ipoib_ib_post_receive(dev, wr_id)))
  200. ipoib_warn(priv, "ipoib_ib_post_receive failed "
  201. "for buf %d\n", wr_id);
  202. }
  203. static int ipoib_dma_map_tx(struct ib_device *ca,
  204. struct ipoib_tx_buf *tx_req)
  205. {
  206. struct sk_buff *skb = tx_req->skb;
  207. u64 *mapping = tx_req->mapping;
  208. int i;
  209. int off;
  210. if (skb_headlen(skb)) {
  211. mapping[0] = ib_dma_map_single(ca, skb->data, skb_headlen(skb),
  212. DMA_TO_DEVICE);
  213. if (unlikely(ib_dma_mapping_error(ca, mapping[0])))
  214. return -EIO;
  215. off = 1;
  216. } else
  217. off = 0;
  218. for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
  219. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  220. mapping[i + off] = ib_dma_map_page(ca, frag->page,
  221. frag->page_offset, frag->size,
  222. DMA_TO_DEVICE);
  223. if (unlikely(ib_dma_mapping_error(ca, mapping[i + off])))
  224. goto partial_error;
  225. }
  226. return 0;
  227. partial_error:
  228. for (; i > 0; --i) {
  229. skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
  230. ib_dma_unmap_page(ca, mapping[i - !off], frag->size, DMA_TO_DEVICE);
  231. }
  232. if (off)
  233. ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
  234. return -EIO;
  235. }
  236. static void ipoib_dma_unmap_tx(struct ib_device *ca,
  237. struct ipoib_tx_buf *tx_req)
  238. {
  239. struct sk_buff *skb = tx_req->skb;
  240. u64 *mapping = tx_req->mapping;
  241. int i;
  242. int off;
  243. if (skb_headlen(skb)) {
  244. ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
  245. off = 1;
  246. } else
  247. off = 0;
  248. for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
  249. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  250. ib_dma_unmap_page(ca, mapping[i + off], frag->size,
  251. DMA_TO_DEVICE);
  252. }
  253. }
  254. static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
  255. {
  256. struct ipoib_dev_priv *priv = netdev_priv(dev);
  257. unsigned int wr_id = wc->wr_id;
  258. struct ipoib_tx_buf *tx_req;
  259. unsigned long flags;
  260. ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
  261. wr_id, wc->status);
  262. if (unlikely(wr_id >= ipoib_sendq_size)) {
  263. ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
  264. wr_id, ipoib_sendq_size);
  265. return;
  266. }
  267. tx_req = &priv->tx_ring[wr_id];
  268. ipoib_dma_unmap_tx(priv->ca, tx_req);
  269. ++dev->stats.tx_packets;
  270. dev->stats.tx_bytes += tx_req->skb->len;
  271. dev_kfree_skb_any(tx_req->skb);
  272. spin_lock_irqsave(&priv->tx_lock, flags);
  273. ++priv->tx_tail;
  274. if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
  275. netif_queue_stopped(dev) &&
  276. test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
  277. netif_wake_queue(dev);
  278. spin_unlock_irqrestore(&priv->tx_lock, flags);
  279. if (wc->status != IB_WC_SUCCESS &&
  280. wc->status != IB_WC_WR_FLUSH_ERR)
  281. ipoib_warn(priv, "failed send event "
  282. "(status=%d, wrid=%d vend_err %x)\n",
  283. wc->status, wr_id, wc->vendor_err);
  284. }
  285. int ipoib_poll(struct napi_struct *napi, int budget)
  286. {
  287. struct ipoib_dev_priv *priv = container_of(napi, struct ipoib_dev_priv, napi);
  288. struct net_device *dev = priv->dev;
  289. int done;
  290. int t;
  291. int n, i;
  292. done = 0;
  293. poll_more:
  294. while (done < budget) {
  295. int max = (budget - done);
  296. t = min(IPOIB_NUM_WC, max);
  297. n = ib_poll_cq(priv->cq, t, priv->ibwc);
  298. for (i = 0; i < n; i++) {
  299. struct ib_wc *wc = priv->ibwc + i;
  300. if (wc->wr_id & IPOIB_OP_RECV) {
  301. ++done;
  302. if (wc->wr_id & IPOIB_OP_CM)
  303. ipoib_cm_handle_rx_wc(dev, wc);
  304. else
  305. ipoib_ib_handle_rx_wc(dev, wc);
  306. } else {
  307. if (wc->wr_id & IPOIB_OP_CM)
  308. ipoib_cm_handle_tx_wc(dev, wc);
  309. else
  310. ipoib_ib_handle_tx_wc(dev, wc);
  311. }
  312. }
  313. if (n != t)
  314. break;
  315. }
  316. if (done < budget) {
  317. netif_rx_complete(dev, napi);
  318. if (unlikely(ib_req_notify_cq(priv->cq,
  319. IB_CQ_NEXT_COMP |
  320. IB_CQ_REPORT_MISSED_EVENTS)) &&
  321. netif_rx_reschedule(dev, napi))
  322. goto poll_more;
  323. }
  324. return done;
  325. }
  326. void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
  327. {
  328. struct net_device *dev = dev_ptr;
  329. struct ipoib_dev_priv *priv = netdev_priv(dev);
  330. netif_rx_schedule(dev, &priv->napi);
  331. }
  332. static inline int post_send(struct ipoib_dev_priv *priv,
  333. unsigned int wr_id,
  334. struct ib_ah *address, u32 qpn,
  335. struct ipoib_tx_buf *tx_req,
  336. void *head, int hlen)
  337. {
  338. struct ib_send_wr *bad_wr;
  339. int i, off;
  340. struct sk_buff *skb = tx_req->skb;
  341. skb_frag_t *frags = skb_shinfo(skb)->frags;
  342. int nr_frags = skb_shinfo(skb)->nr_frags;
  343. u64 *mapping = tx_req->mapping;
  344. if (skb_headlen(skb)) {
  345. priv->tx_sge[0].addr = mapping[0];
  346. priv->tx_sge[0].length = skb_headlen(skb);
  347. off = 1;
  348. } else
  349. off = 0;
  350. for (i = 0; i < nr_frags; ++i) {
  351. priv->tx_sge[i + off].addr = mapping[i + off];
  352. priv->tx_sge[i + off].length = frags[i].size;
  353. }
  354. priv->tx_wr.num_sge = nr_frags + off;
  355. priv->tx_wr.wr_id = wr_id;
  356. priv->tx_wr.wr.ud.remote_qpn = qpn;
  357. priv->tx_wr.wr.ud.ah = address;
  358. if (head) {
  359. priv->tx_wr.wr.ud.mss = skb_shinfo(skb)->gso_size;
  360. priv->tx_wr.wr.ud.header = head;
  361. priv->tx_wr.wr.ud.hlen = hlen;
  362. priv->tx_wr.opcode = IB_WR_LSO;
  363. } else
  364. priv->tx_wr.opcode = IB_WR_SEND;
  365. return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
  366. }
  367. void ipoib_send(struct net_device *dev, struct sk_buff *skb,
  368. struct ipoib_ah *address, u32 qpn)
  369. {
  370. struct ipoib_dev_priv *priv = netdev_priv(dev);
  371. struct ipoib_tx_buf *tx_req;
  372. int hlen;
  373. void *phead;
  374. if (skb_is_gso(skb)) {
  375. hlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
  376. phead = skb->data;
  377. if (unlikely(!skb_pull(skb, hlen))) {
  378. ipoib_warn(priv, "linear data too small\n");
  379. ++dev->stats.tx_dropped;
  380. ++dev->stats.tx_errors;
  381. dev_kfree_skb_any(skb);
  382. return;
  383. }
  384. } else {
  385. if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
  386. ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
  387. skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
  388. ++dev->stats.tx_dropped;
  389. ++dev->stats.tx_errors;
  390. ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
  391. return;
  392. }
  393. phead = NULL;
  394. hlen = 0;
  395. }
  396. ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
  397. skb->len, address, qpn);
  398. /*
  399. * We put the skb into the tx_ring _before_ we call post_send()
  400. * because it's entirely possible that the completion handler will
  401. * run before we execute anything after the post_send(). That
  402. * means we have to make sure everything is properly recorded and
  403. * our state is consistent before we call post_send().
  404. */
  405. tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
  406. tx_req->skb = skb;
  407. if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) {
  408. ++dev->stats.tx_errors;
  409. dev_kfree_skb_any(skb);
  410. return;
  411. }
  412. if (skb->ip_summed == CHECKSUM_PARTIAL)
  413. priv->tx_wr.send_flags |= IB_SEND_IP_CSUM;
  414. else
  415. priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
  416. if (unlikely(post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
  417. address->ah, qpn, tx_req, phead, hlen))) {
  418. ipoib_warn(priv, "post_send failed\n");
  419. ++dev->stats.tx_errors;
  420. ipoib_dma_unmap_tx(priv->ca, tx_req);
  421. dev_kfree_skb_any(skb);
  422. } else {
  423. dev->trans_start = jiffies;
  424. address->last_send = priv->tx_head;
  425. ++priv->tx_head;
  426. if (++priv->tx_outstanding == ipoib_sendq_size) {
  427. ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
  428. netif_stop_queue(dev);
  429. }
  430. }
  431. }
  432. static void __ipoib_reap_ah(struct net_device *dev)
  433. {
  434. struct ipoib_dev_priv *priv = netdev_priv(dev);
  435. struct ipoib_ah *ah, *tah;
  436. LIST_HEAD(remove_list);
  437. spin_lock_irq(&priv->tx_lock);
  438. spin_lock(&priv->lock);
  439. list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
  440. if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
  441. list_del(&ah->list);
  442. ib_destroy_ah(ah->ah);
  443. kfree(ah);
  444. }
  445. spin_unlock(&priv->lock);
  446. spin_unlock_irq(&priv->tx_lock);
  447. }
  448. void ipoib_reap_ah(struct work_struct *work)
  449. {
  450. struct ipoib_dev_priv *priv =
  451. container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
  452. struct net_device *dev = priv->dev;
  453. __ipoib_reap_ah(dev);
  454. if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
  455. queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
  456. round_jiffies_relative(HZ));
  457. }
  458. int ipoib_ib_dev_open(struct net_device *dev)
  459. {
  460. struct ipoib_dev_priv *priv = netdev_priv(dev);
  461. int ret;
  462. if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {
  463. ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);
  464. clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  465. return -1;
  466. }
  467. set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  468. ret = ipoib_init_qp(dev);
  469. if (ret) {
  470. ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
  471. return -1;
  472. }
  473. ret = ipoib_ib_post_receives(dev);
  474. if (ret) {
  475. ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
  476. ipoib_ib_dev_stop(dev, 1);
  477. return -1;
  478. }
  479. ret = ipoib_cm_dev_open(dev);
  480. if (ret) {
  481. ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
  482. ipoib_ib_dev_stop(dev, 1);
  483. return -1;
  484. }
  485. clear_bit(IPOIB_STOP_REAPER, &priv->flags);
  486. queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
  487. round_jiffies_relative(HZ));
  488. set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
  489. return 0;
  490. }
  491. static void ipoib_pkey_dev_check_presence(struct net_device *dev)
  492. {
  493. struct ipoib_dev_priv *priv = netdev_priv(dev);
  494. u16 pkey_index = 0;
  495. if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
  496. clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  497. else
  498. set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  499. }
  500. int ipoib_ib_dev_up(struct net_device *dev)
  501. {
  502. struct ipoib_dev_priv *priv = netdev_priv(dev);
  503. ipoib_pkey_dev_check_presence(dev);
  504. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  505. ipoib_dbg(priv, "PKEY is not assigned.\n");
  506. return 0;
  507. }
  508. set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
  509. return ipoib_mcast_start_thread(dev);
  510. }
  511. int ipoib_ib_dev_down(struct net_device *dev, int flush)
  512. {
  513. struct ipoib_dev_priv *priv = netdev_priv(dev);
  514. ipoib_dbg(priv, "downing ib_dev\n");
  515. clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
  516. netif_carrier_off(dev);
  517. /* Shutdown the P_Key thread if still active */
  518. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  519. mutex_lock(&pkey_mutex);
  520. set_bit(IPOIB_PKEY_STOP, &priv->flags);
  521. cancel_delayed_work(&priv->pkey_poll_task);
  522. mutex_unlock(&pkey_mutex);
  523. if (flush)
  524. flush_workqueue(ipoib_workqueue);
  525. }
  526. ipoib_mcast_stop_thread(dev, flush);
  527. ipoib_mcast_dev_flush(dev);
  528. ipoib_flush_paths(dev);
  529. return 0;
  530. }
  531. static int recvs_pending(struct net_device *dev)
  532. {
  533. struct ipoib_dev_priv *priv = netdev_priv(dev);
  534. int pending = 0;
  535. int i;
  536. for (i = 0; i < ipoib_recvq_size; ++i)
  537. if (priv->rx_ring[i].skb)
  538. ++pending;
  539. return pending;
  540. }
  541. void ipoib_drain_cq(struct net_device *dev)
  542. {
  543. struct ipoib_dev_priv *priv = netdev_priv(dev);
  544. int i, n;
  545. do {
  546. n = ib_poll_cq(priv->cq, IPOIB_NUM_WC, priv->ibwc);
  547. for (i = 0; i < n; ++i) {
  548. /*
  549. * Convert any successful completions to flush
  550. * errors to avoid passing packets up the
  551. * stack after bringing the device down.
  552. */
  553. if (priv->ibwc[i].status == IB_WC_SUCCESS)
  554. priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
  555. if (priv->ibwc[i].wr_id & IPOIB_OP_RECV) {
  556. if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
  557. ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
  558. else
  559. ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
  560. } else {
  561. if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
  562. ipoib_cm_handle_tx_wc(dev, priv->ibwc + i);
  563. else
  564. ipoib_ib_handle_tx_wc(dev, priv->ibwc + i);
  565. }
  566. }
  567. } while (n == IPOIB_NUM_WC);
  568. }
  569. int ipoib_ib_dev_stop(struct net_device *dev, int flush)
  570. {
  571. struct ipoib_dev_priv *priv = netdev_priv(dev);
  572. struct ib_qp_attr qp_attr;
  573. unsigned long begin;
  574. struct ipoib_tx_buf *tx_req;
  575. int i;
  576. clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
  577. ipoib_cm_dev_stop(dev);
  578. /*
  579. * Move our QP to the error state and then reinitialize in
  580. * when all work requests have completed or have been flushed.
  581. */
  582. qp_attr.qp_state = IB_QPS_ERR;
  583. if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
  584. ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
  585. /* Wait for all sends and receives to complete */
  586. begin = jiffies;
  587. while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
  588. if (time_after(jiffies, begin + 5 * HZ)) {
  589. ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
  590. priv->tx_head - priv->tx_tail, recvs_pending(dev));
  591. /*
  592. * assume the HW is wedged and just free up
  593. * all our pending work requests.
  594. */
  595. while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
  596. tx_req = &priv->tx_ring[priv->tx_tail &
  597. (ipoib_sendq_size - 1)];
  598. ipoib_dma_unmap_tx(priv->ca, tx_req);
  599. dev_kfree_skb_any(tx_req->skb);
  600. ++priv->tx_tail;
  601. --priv->tx_outstanding;
  602. }
  603. for (i = 0; i < ipoib_recvq_size; ++i) {
  604. struct ipoib_rx_buf *rx_req;
  605. rx_req = &priv->rx_ring[i];
  606. if (!rx_req->skb)
  607. continue;
  608. ib_dma_unmap_single(priv->ca,
  609. rx_req->mapping,
  610. IPOIB_BUF_SIZE,
  611. DMA_FROM_DEVICE);
  612. dev_kfree_skb_any(rx_req->skb);
  613. rx_req->skb = NULL;
  614. }
  615. goto timeout;
  616. }
  617. ipoib_drain_cq(dev);
  618. msleep(1);
  619. }
  620. ipoib_dbg(priv, "All sends and receives done.\n");
  621. timeout:
  622. qp_attr.qp_state = IB_QPS_RESET;
  623. if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
  624. ipoib_warn(priv, "Failed to modify QP to RESET state\n");
  625. /* Wait for all AHs to be reaped */
  626. set_bit(IPOIB_STOP_REAPER, &priv->flags);
  627. cancel_delayed_work(&priv->ah_reap_task);
  628. if (flush)
  629. flush_workqueue(ipoib_workqueue);
  630. begin = jiffies;
  631. while (!list_empty(&priv->dead_ahs)) {
  632. __ipoib_reap_ah(dev);
  633. if (time_after(jiffies, begin + HZ)) {
  634. ipoib_warn(priv, "timing out; will leak address handles\n");
  635. break;
  636. }
  637. msleep(1);
  638. }
  639. ib_req_notify_cq(priv->cq, IB_CQ_NEXT_COMP);
  640. return 0;
  641. }
  642. int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
  643. {
  644. struct ipoib_dev_priv *priv = netdev_priv(dev);
  645. priv->ca = ca;
  646. priv->port = port;
  647. priv->qp = NULL;
  648. if (ipoib_transport_dev_init(dev, ca)) {
  649. printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
  650. return -ENODEV;
  651. }
  652. if (dev->flags & IFF_UP) {
  653. if (ipoib_ib_dev_open(dev)) {
  654. ipoib_transport_dev_cleanup(dev);
  655. return -ENODEV;
  656. }
  657. }
  658. return 0;
  659. }
  660. static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv, int pkey_event)
  661. {
  662. struct ipoib_dev_priv *cpriv;
  663. struct net_device *dev = priv->dev;
  664. u16 new_index;
  665. mutex_lock(&priv->vlan_mutex);
  666. /*
  667. * Flush any child interfaces too -- they might be up even if
  668. * the parent is down.
  669. */
  670. list_for_each_entry(cpriv, &priv->child_intfs, list)
  671. __ipoib_ib_dev_flush(cpriv, pkey_event);
  672. mutex_unlock(&priv->vlan_mutex);
  673. if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
  674. ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
  675. return;
  676. }
  677. if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
  678. ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
  679. return;
  680. }
  681. if (pkey_event) {
  682. if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
  683. clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  684. ipoib_ib_dev_down(dev, 0);
  685. ipoib_ib_dev_stop(dev, 0);
  686. if (ipoib_pkey_dev_delay_open(dev))
  687. return;
  688. }
  689. /* restart QP only if P_Key index is changed */
  690. if (test_and_set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) &&
  691. new_index == priv->pkey_index) {
  692. ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
  693. return;
  694. }
  695. priv->pkey_index = new_index;
  696. }
  697. ipoib_dbg(priv, "flushing\n");
  698. ipoib_ib_dev_down(dev, 0);
  699. if (pkey_event) {
  700. ipoib_ib_dev_stop(dev, 0);
  701. ipoib_ib_dev_open(dev);
  702. }
  703. /*
  704. * The device could have been brought down between the start and when
  705. * we get here, don't bring it back up if it's not configured up
  706. */
  707. if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
  708. ipoib_ib_dev_up(dev);
  709. ipoib_mcast_restart_task(&priv->restart_task);
  710. }
  711. }
  712. void ipoib_ib_dev_flush(struct work_struct *work)
  713. {
  714. struct ipoib_dev_priv *priv =
  715. container_of(work, struct ipoib_dev_priv, flush_task);
  716. ipoib_dbg(priv, "Flushing %s\n", priv->dev->name);
  717. __ipoib_ib_dev_flush(priv, 0);
  718. }
  719. void ipoib_pkey_event(struct work_struct *work)
  720. {
  721. struct ipoib_dev_priv *priv =
  722. container_of(work, struct ipoib_dev_priv, pkey_event_task);
  723. ipoib_dbg(priv, "Flushing %s and restarting its QP\n", priv->dev->name);
  724. __ipoib_ib_dev_flush(priv, 1);
  725. }
  726. void ipoib_ib_dev_cleanup(struct net_device *dev)
  727. {
  728. struct ipoib_dev_priv *priv = netdev_priv(dev);
  729. ipoib_dbg(priv, "cleaning up ib_dev\n");
  730. ipoib_mcast_stop_thread(dev, 1);
  731. ipoib_mcast_dev_flush(dev);
  732. ipoib_transport_dev_cleanup(dev);
  733. }
  734. /*
  735. * Delayed P_Key Assigment Interim Support
  736. *
  737. * The following is initial implementation of delayed P_Key assigment
  738. * mechanism. It is using the same approach implemented for the multicast
  739. * group join. The single goal of this implementation is to quickly address
  740. * Bug #2507. This implementation will probably be removed when the P_Key
  741. * change async notification is available.
  742. */
  743. void ipoib_pkey_poll(struct work_struct *work)
  744. {
  745. struct ipoib_dev_priv *priv =
  746. container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
  747. struct net_device *dev = priv->dev;
  748. ipoib_pkey_dev_check_presence(dev);
  749. if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
  750. ipoib_open(dev);
  751. else {
  752. mutex_lock(&pkey_mutex);
  753. if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
  754. queue_delayed_work(ipoib_workqueue,
  755. &priv->pkey_poll_task,
  756. HZ);
  757. mutex_unlock(&pkey_mutex);
  758. }
  759. }
  760. int ipoib_pkey_dev_delay_open(struct net_device *dev)
  761. {
  762. struct ipoib_dev_priv *priv = netdev_priv(dev);
  763. /* Look for the interface pkey value in the IB Port P_Key table and */
  764. /* set the interface pkey assigment flag */
  765. ipoib_pkey_dev_check_presence(dev);
  766. /* P_Key value not assigned yet - start polling */
  767. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  768. mutex_lock(&pkey_mutex);
  769. clear_bit(IPOIB_PKEY_STOP, &priv->flags);
  770. queue_delayed_work(ipoib_workqueue,
  771. &priv->pkey_poll_task,
  772. HZ);
  773. mutex_unlock(&pkey_mutex);
  774. return 1;
  775. }
  776. return 0;
  777. }