ipoib_ib.c 27 KB

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