ipoib_ib.c 27 KB

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