ipoib_ib.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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(dev, 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(dev, 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(dev, &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. init_timer(&priv->poll_timer);
  567. priv->poll_timer.function = ipoib_ib_tx_timer_func;
  568. priv->poll_timer.data = (unsigned long)dev;
  569. set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
  570. return 0;
  571. }
  572. static void ipoib_pkey_dev_check_presence(struct net_device *dev)
  573. {
  574. struct ipoib_dev_priv *priv = netdev_priv(dev);
  575. u16 pkey_index = 0;
  576. if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
  577. clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  578. else
  579. set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  580. }
  581. int ipoib_ib_dev_up(struct net_device *dev)
  582. {
  583. struct ipoib_dev_priv *priv = netdev_priv(dev);
  584. ipoib_pkey_dev_check_presence(dev);
  585. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  586. ipoib_dbg(priv, "PKEY is not assigned.\n");
  587. return 0;
  588. }
  589. set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
  590. return ipoib_mcast_start_thread(dev);
  591. }
  592. int ipoib_ib_dev_down(struct net_device *dev, int flush)
  593. {
  594. struct ipoib_dev_priv *priv = netdev_priv(dev);
  595. ipoib_dbg(priv, "downing ib_dev\n");
  596. clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
  597. netif_carrier_off(dev);
  598. /* Shutdown the P_Key thread if still active */
  599. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  600. mutex_lock(&pkey_mutex);
  601. set_bit(IPOIB_PKEY_STOP, &priv->flags);
  602. cancel_delayed_work(&priv->pkey_poll_task);
  603. mutex_unlock(&pkey_mutex);
  604. if (flush)
  605. flush_workqueue(ipoib_workqueue);
  606. }
  607. ipoib_mcast_stop_thread(dev, flush);
  608. ipoib_mcast_dev_flush(dev);
  609. ipoib_flush_paths(dev);
  610. return 0;
  611. }
  612. static int recvs_pending(struct net_device *dev)
  613. {
  614. struct ipoib_dev_priv *priv = netdev_priv(dev);
  615. int pending = 0;
  616. int i;
  617. for (i = 0; i < ipoib_recvq_size; ++i)
  618. if (priv->rx_ring[i].skb)
  619. ++pending;
  620. return pending;
  621. }
  622. void ipoib_drain_cq(struct net_device *dev)
  623. {
  624. struct ipoib_dev_priv *priv = netdev_priv(dev);
  625. int i, n;
  626. /*
  627. * We call completion handling routines that expect to be
  628. * called from the BH-disabled NAPI poll context, so disable
  629. * BHs here too.
  630. */
  631. local_bh_disable();
  632. do {
  633. n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);
  634. for (i = 0; i < n; ++i) {
  635. /*
  636. * Convert any successful completions to flush
  637. * errors to avoid passing packets up the
  638. * stack after bringing the device down.
  639. */
  640. if (priv->ibwc[i].status == IB_WC_SUCCESS)
  641. priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
  642. if (priv->ibwc[i].wr_id & IPOIB_OP_RECV) {
  643. if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
  644. ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
  645. else
  646. ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
  647. } else
  648. ipoib_cm_handle_tx_wc(dev, priv->ibwc + i);
  649. }
  650. } while (n == IPOIB_NUM_WC);
  651. while (poll_tx(priv))
  652. ; /* nothing */
  653. local_bh_enable();
  654. }
  655. int ipoib_ib_dev_stop(struct net_device *dev, int flush)
  656. {
  657. struct ipoib_dev_priv *priv = netdev_priv(dev);
  658. struct ib_qp_attr qp_attr;
  659. unsigned long begin;
  660. struct ipoib_tx_buf *tx_req;
  661. int i;
  662. clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
  663. ipoib_cm_dev_stop(dev);
  664. /*
  665. * Move our QP to the error state and then reinitialize in
  666. * when all work requests have completed or have been flushed.
  667. */
  668. qp_attr.qp_state = IB_QPS_ERR;
  669. if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
  670. ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
  671. /* Wait for all sends and receives to complete */
  672. begin = jiffies;
  673. while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
  674. if (time_after(jiffies, begin + 5 * HZ)) {
  675. ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
  676. priv->tx_head - priv->tx_tail, recvs_pending(dev));
  677. /*
  678. * assume the HW is wedged and just free up
  679. * all our pending work requests.
  680. */
  681. while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
  682. tx_req = &priv->tx_ring[priv->tx_tail &
  683. (ipoib_sendq_size - 1)];
  684. ipoib_dma_unmap_tx(priv->ca, tx_req);
  685. dev_kfree_skb_any(tx_req->skb);
  686. ++priv->tx_tail;
  687. --priv->tx_outstanding;
  688. }
  689. for (i = 0; i < ipoib_recvq_size; ++i) {
  690. struct ipoib_rx_buf *rx_req;
  691. rx_req = &priv->rx_ring[i];
  692. if (!rx_req->skb)
  693. continue;
  694. ipoib_ud_dma_unmap_rx(priv,
  695. priv->rx_ring[i].mapping);
  696. dev_kfree_skb_any(rx_req->skb);
  697. rx_req->skb = NULL;
  698. }
  699. goto timeout;
  700. }
  701. ipoib_drain_cq(dev);
  702. msleep(1);
  703. }
  704. ipoib_dbg(priv, "All sends and receives done.\n");
  705. timeout:
  706. del_timer_sync(&priv->poll_timer);
  707. qp_attr.qp_state = IB_QPS_RESET;
  708. if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
  709. ipoib_warn(priv, "Failed to modify QP to RESET state\n");
  710. /* Wait for all AHs to be reaped */
  711. set_bit(IPOIB_STOP_REAPER, &priv->flags);
  712. cancel_delayed_work(&priv->ah_reap_task);
  713. if (flush)
  714. flush_workqueue(ipoib_workqueue);
  715. begin = jiffies;
  716. while (!list_empty(&priv->dead_ahs)) {
  717. __ipoib_reap_ah(dev);
  718. if (time_after(jiffies, begin + HZ)) {
  719. ipoib_warn(priv, "timing out; will leak address handles\n");
  720. break;
  721. }
  722. msleep(1);
  723. }
  724. ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP);
  725. return 0;
  726. }
  727. int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
  728. {
  729. struct ipoib_dev_priv *priv = netdev_priv(dev);
  730. priv->ca = ca;
  731. priv->port = port;
  732. priv->qp = NULL;
  733. if (ipoib_transport_dev_init(dev, ca)) {
  734. printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
  735. return -ENODEV;
  736. }
  737. if (dev->flags & IFF_UP) {
  738. if (ipoib_ib_dev_open(dev)) {
  739. ipoib_transport_dev_cleanup(dev);
  740. return -ENODEV;
  741. }
  742. }
  743. return 0;
  744. }
  745. static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv,
  746. enum ipoib_flush_level level)
  747. {
  748. struct ipoib_dev_priv *cpriv;
  749. struct net_device *dev = priv->dev;
  750. u16 new_index;
  751. mutex_lock(&priv->vlan_mutex);
  752. /*
  753. * Flush any child interfaces too -- they might be up even if
  754. * the parent is down.
  755. */
  756. list_for_each_entry(cpriv, &priv->child_intfs, list)
  757. __ipoib_ib_dev_flush(cpriv, level);
  758. mutex_unlock(&priv->vlan_mutex);
  759. if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
  760. ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
  761. return;
  762. }
  763. if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
  764. ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
  765. return;
  766. }
  767. if (level == IPOIB_FLUSH_HEAVY) {
  768. if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
  769. clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  770. ipoib_ib_dev_down(dev, 0);
  771. ipoib_ib_dev_stop(dev, 0);
  772. if (ipoib_pkey_dev_delay_open(dev))
  773. return;
  774. }
  775. /* restart QP only if P_Key index is changed */
  776. if (test_and_set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) &&
  777. new_index == priv->pkey_index) {
  778. ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
  779. return;
  780. }
  781. priv->pkey_index = new_index;
  782. }
  783. if (level == IPOIB_FLUSH_LIGHT) {
  784. ipoib_mark_paths_invalid(dev);
  785. ipoib_mcast_dev_flush(dev);
  786. }
  787. if (level >= IPOIB_FLUSH_NORMAL)
  788. ipoib_ib_dev_down(dev, 0);
  789. if (level == IPOIB_FLUSH_HEAVY) {
  790. ipoib_ib_dev_stop(dev, 0);
  791. ipoib_ib_dev_open(dev);
  792. }
  793. /*
  794. * The device could have been brought down between the start and when
  795. * we get here, don't bring it back up if it's not configured up
  796. */
  797. if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
  798. if (level >= IPOIB_FLUSH_NORMAL)
  799. ipoib_ib_dev_up(dev);
  800. ipoib_mcast_restart_task(&priv->restart_task);
  801. }
  802. }
  803. void ipoib_ib_dev_flush_light(struct work_struct *work)
  804. {
  805. struct ipoib_dev_priv *priv =
  806. container_of(work, struct ipoib_dev_priv, flush_light);
  807. __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_LIGHT);
  808. }
  809. void ipoib_ib_dev_flush_normal(struct work_struct *work)
  810. {
  811. struct ipoib_dev_priv *priv =
  812. container_of(work, struct ipoib_dev_priv, flush_normal);
  813. __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_NORMAL);
  814. }
  815. void ipoib_ib_dev_flush_heavy(struct work_struct *work)
  816. {
  817. struct ipoib_dev_priv *priv =
  818. container_of(work, struct ipoib_dev_priv, flush_heavy);
  819. __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_HEAVY);
  820. }
  821. void ipoib_ib_dev_cleanup(struct net_device *dev)
  822. {
  823. struct ipoib_dev_priv *priv = netdev_priv(dev);
  824. ipoib_dbg(priv, "cleaning up ib_dev\n");
  825. ipoib_mcast_stop_thread(dev, 1);
  826. ipoib_mcast_dev_flush(dev);
  827. ipoib_transport_dev_cleanup(dev);
  828. }
  829. /*
  830. * Delayed P_Key Assigment Interim Support
  831. *
  832. * The following is initial implementation of delayed P_Key assigment
  833. * mechanism. It is using the same approach implemented for the multicast
  834. * group join. The single goal of this implementation is to quickly address
  835. * Bug #2507. This implementation will probably be removed when the P_Key
  836. * change async notification is available.
  837. */
  838. void ipoib_pkey_poll(struct work_struct *work)
  839. {
  840. struct ipoib_dev_priv *priv =
  841. container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
  842. struct net_device *dev = priv->dev;
  843. ipoib_pkey_dev_check_presence(dev);
  844. if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
  845. ipoib_open(dev);
  846. else {
  847. mutex_lock(&pkey_mutex);
  848. if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
  849. queue_delayed_work(ipoib_workqueue,
  850. &priv->pkey_poll_task,
  851. HZ);
  852. mutex_unlock(&pkey_mutex);
  853. }
  854. }
  855. int ipoib_pkey_dev_delay_open(struct net_device *dev)
  856. {
  857. struct ipoib_dev_priv *priv = netdev_priv(dev);
  858. /* Look for the interface pkey value in the IB Port P_Key table and */
  859. /* set the interface pkey assigment flag */
  860. ipoib_pkey_dev_check_presence(dev);
  861. /* P_Key value not assigned yet - start polling */
  862. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  863. mutex_lock(&pkey_mutex);
  864. clear_bit(IPOIB_PKEY_STOP, &priv->flags);
  865. queue_delayed_work(ipoib_workqueue,
  866. &priv->pkey_poll_task,
  867. HZ);
  868. mutex_unlock(&pkey_mutex);
  869. return 1;
  870. }
  871. return 0;
  872. }