ipoib_ib.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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. napi_gro_receive(&priv->napi, skb);
  247. repost:
  248. if (unlikely(ipoib_ib_post_receive(dev, wr_id)))
  249. ipoib_warn(priv, "ipoib_ib_post_receive failed "
  250. "for buf %d\n", wr_id);
  251. }
  252. static int ipoib_dma_map_tx(struct ib_device *ca,
  253. struct ipoib_tx_buf *tx_req)
  254. {
  255. struct sk_buff *skb = tx_req->skb;
  256. u64 *mapping = tx_req->mapping;
  257. int i;
  258. int off;
  259. if (skb_headlen(skb)) {
  260. mapping[0] = ib_dma_map_single(ca, skb->data, skb_headlen(skb),
  261. DMA_TO_DEVICE);
  262. if (unlikely(ib_dma_mapping_error(ca, mapping[0])))
  263. return -EIO;
  264. off = 1;
  265. } else
  266. off = 0;
  267. for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
  268. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  269. mapping[i + off] = ib_dma_map_page(ca, frag->page,
  270. frag->page_offset, frag->size,
  271. DMA_TO_DEVICE);
  272. if (unlikely(ib_dma_mapping_error(ca, mapping[i + off])))
  273. goto partial_error;
  274. }
  275. return 0;
  276. partial_error:
  277. for (; i > 0; --i) {
  278. skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
  279. ib_dma_unmap_page(ca, mapping[i - !off], frag->size, DMA_TO_DEVICE);
  280. }
  281. if (off)
  282. ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
  283. return -EIO;
  284. }
  285. static void ipoib_dma_unmap_tx(struct ib_device *ca,
  286. struct ipoib_tx_buf *tx_req)
  287. {
  288. struct sk_buff *skb = tx_req->skb;
  289. u64 *mapping = tx_req->mapping;
  290. int i;
  291. int off;
  292. if (skb_headlen(skb)) {
  293. ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
  294. off = 1;
  295. } else
  296. off = 0;
  297. for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
  298. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  299. ib_dma_unmap_page(ca, mapping[i + off], frag->size,
  300. DMA_TO_DEVICE);
  301. }
  302. }
  303. static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
  304. {
  305. struct ipoib_dev_priv *priv = netdev_priv(dev);
  306. unsigned int wr_id = wc->wr_id;
  307. struct ipoib_tx_buf *tx_req;
  308. ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
  309. wr_id, wc->status);
  310. if (unlikely(wr_id >= ipoib_sendq_size)) {
  311. ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
  312. wr_id, ipoib_sendq_size);
  313. return;
  314. }
  315. tx_req = &priv->tx_ring[wr_id];
  316. ipoib_dma_unmap_tx(priv->ca, tx_req);
  317. ++dev->stats.tx_packets;
  318. dev->stats.tx_bytes += tx_req->skb->len;
  319. dev_kfree_skb_any(tx_req->skb);
  320. ++priv->tx_tail;
  321. if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
  322. netif_queue_stopped(dev) &&
  323. test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
  324. netif_wake_queue(dev);
  325. if (wc->status != IB_WC_SUCCESS &&
  326. wc->status != IB_WC_WR_FLUSH_ERR)
  327. ipoib_warn(priv, "failed send event "
  328. "(status=%d, wrid=%d vend_err %x)\n",
  329. wc->status, wr_id, wc->vendor_err);
  330. }
  331. static int poll_tx(struct ipoib_dev_priv *priv)
  332. {
  333. int n, i;
  334. n = ib_poll_cq(priv->send_cq, MAX_SEND_CQE, priv->send_wc);
  335. for (i = 0; i < n; ++i)
  336. ipoib_ib_handle_tx_wc(priv->dev, priv->send_wc + i);
  337. return n == MAX_SEND_CQE;
  338. }
  339. int ipoib_poll(struct napi_struct *napi, int budget)
  340. {
  341. struct ipoib_dev_priv *priv = container_of(napi, struct ipoib_dev_priv, napi);
  342. struct net_device *dev = priv->dev;
  343. int done;
  344. int t;
  345. int n, i;
  346. done = 0;
  347. poll_more:
  348. while (done < budget) {
  349. int max = (budget - done);
  350. t = min(IPOIB_NUM_WC, max);
  351. n = ib_poll_cq(priv->recv_cq, t, priv->ibwc);
  352. for (i = 0; i < n; i++) {
  353. struct ib_wc *wc = priv->ibwc + i;
  354. if (wc->wr_id & IPOIB_OP_RECV) {
  355. ++done;
  356. if (wc->wr_id & IPOIB_OP_CM)
  357. ipoib_cm_handle_rx_wc(dev, wc);
  358. else
  359. ipoib_ib_handle_rx_wc(dev, wc);
  360. } else
  361. ipoib_cm_handle_tx_wc(priv->dev, wc);
  362. }
  363. if (n != t)
  364. break;
  365. }
  366. if (done < budget) {
  367. napi_complete(napi);
  368. if (unlikely(ib_req_notify_cq(priv->recv_cq,
  369. IB_CQ_NEXT_COMP |
  370. IB_CQ_REPORT_MISSED_EVENTS)) &&
  371. napi_reschedule(napi))
  372. goto poll_more;
  373. }
  374. return done;
  375. }
  376. void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
  377. {
  378. struct net_device *dev = dev_ptr;
  379. struct ipoib_dev_priv *priv = netdev_priv(dev);
  380. napi_schedule(&priv->napi);
  381. }
  382. static void drain_tx_cq(struct net_device *dev)
  383. {
  384. struct ipoib_dev_priv *priv = netdev_priv(dev);
  385. netif_tx_lock(dev);
  386. while (poll_tx(priv))
  387. ; /* nothing */
  388. if (netif_queue_stopped(dev))
  389. mod_timer(&priv->poll_timer, jiffies + 1);
  390. netif_tx_unlock(dev);
  391. }
  392. void ipoib_send_comp_handler(struct ib_cq *cq, void *dev_ptr)
  393. {
  394. struct ipoib_dev_priv *priv = netdev_priv(dev_ptr);
  395. mod_timer(&priv->poll_timer, jiffies);
  396. }
  397. static inline int post_send(struct ipoib_dev_priv *priv,
  398. unsigned int wr_id,
  399. struct ib_ah *address, u32 qpn,
  400. struct ipoib_tx_buf *tx_req,
  401. void *head, int hlen)
  402. {
  403. struct ib_send_wr *bad_wr;
  404. int i, off;
  405. struct sk_buff *skb = tx_req->skb;
  406. skb_frag_t *frags = skb_shinfo(skb)->frags;
  407. int nr_frags = skb_shinfo(skb)->nr_frags;
  408. u64 *mapping = tx_req->mapping;
  409. if (skb_headlen(skb)) {
  410. priv->tx_sge[0].addr = mapping[0];
  411. priv->tx_sge[0].length = skb_headlen(skb);
  412. off = 1;
  413. } else
  414. off = 0;
  415. for (i = 0; i < nr_frags; ++i) {
  416. priv->tx_sge[i + off].addr = mapping[i + off];
  417. priv->tx_sge[i + off].length = frags[i].size;
  418. }
  419. priv->tx_wr.num_sge = nr_frags + off;
  420. priv->tx_wr.wr_id = wr_id;
  421. priv->tx_wr.wr.ud.remote_qpn = qpn;
  422. priv->tx_wr.wr.ud.ah = address;
  423. if (head) {
  424. priv->tx_wr.wr.ud.mss = skb_shinfo(skb)->gso_size;
  425. priv->tx_wr.wr.ud.header = head;
  426. priv->tx_wr.wr.ud.hlen = hlen;
  427. priv->tx_wr.opcode = IB_WR_LSO;
  428. } else
  429. priv->tx_wr.opcode = IB_WR_SEND;
  430. return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
  431. }
  432. void ipoib_send(struct net_device *dev, struct sk_buff *skb,
  433. struct ipoib_ah *address, u32 qpn)
  434. {
  435. struct ipoib_dev_priv *priv = netdev_priv(dev);
  436. struct ipoib_tx_buf *tx_req;
  437. int hlen, rc;
  438. void *phead;
  439. if (skb_is_gso(skb)) {
  440. hlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
  441. phead = skb->data;
  442. if (unlikely(!skb_pull(skb, hlen))) {
  443. ipoib_warn(priv, "linear data too small\n");
  444. ++dev->stats.tx_dropped;
  445. ++dev->stats.tx_errors;
  446. dev_kfree_skb_any(skb);
  447. return;
  448. }
  449. } else {
  450. if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
  451. ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
  452. skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
  453. ++dev->stats.tx_dropped;
  454. ++dev->stats.tx_errors;
  455. ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
  456. return;
  457. }
  458. phead = NULL;
  459. hlen = 0;
  460. }
  461. ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
  462. skb->len, address, qpn);
  463. /*
  464. * We put the skb into the tx_ring _before_ we call post_send()
  465. * because it's entirely possible that the completion handler will
  466. * run before we execute anything after the post_send(). That
  467. * means we have to make sure everything is properly recorded and
  468. * our state is consistent before we call post_send().
  469. */
  470. tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
  471. tx_req->skb = skb;
  472. if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) {
  473. ++dev->stats.tx_errors;
  474. dev_kfree_skb_any(skb);
  475. return;
  476. }
  477. if (skb->ip_summed == CHECKSUM_PARTIAL)
  478. priv->tx_wr.send_flags |= IB_SEND_IP_CSUM;
  479. else
  480. priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
  481. if (++priv->tx_outstanding == ipoib_sendq_size) {
  482. ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
  483. if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP))
  484. ipoib_warn(priv, "request notify on send CQ failed\n");
  485. netif_stop_queue(dev);
  486. }
  487. rc = post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
  488. address->ah, qpn, tx_req, phead, hlen);
  489. if (unlikely(rc)) {
  490. ipoib_warn(priv, "post_send failed, error %d\n", rc);
  491. ++dev->stats.tx_errors;
  492. --priv->tx_outstanding;
  493. ipoib_dma_unmap_tx(priv->ca, tx_req);
  494. dev_kfree_skb_any(skb);
  495. if (netif_queue_stopped(dev))
  496. netif_wake_queue(dev);
  497. } else {
  498. dev->trans_start = jiffies;
  499. address->last_send = priv->tx_head;
  500. ++priv->tx_head;
  501. skb_orphan(skb);
  502. }
  503. if (unlikely(priv->tx_outstanding > MAX_SEND_CQE))
  504. while (poll_tx(priv))
  505. ; /* nothing */
  506. }
  507. static void __ipoib_reap_ah(struct net_device *dev)
  508. {
  509. struct ipoib_dev_priv *priv = netdev_priv(dev);
  510. struct ipoib_ah *ah, *tah;
  511. LIST_HEAD(remove_list);
  512. unsigned long flags;
  513. netif_tx_lock_bh(dev);
  514. spin_lock_irqsave(&priv->lock, flags);
  515. list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
  516. if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
  517. list_del(&ah->list);
  518. ib_destroy_ah(ah->ah);
  519. kfree(ah);
  520. }
  521. spin_unlock_irqrestore(&priv->lock, flags);
  522. netif_tx_unlock_bh(dev);
  523. }
  524. void ipoib_reap_ah(struct work_struct *work)
  525. {
  526. struct ipoib_dev_priv *priv =
  527. container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
  528. struct net_device *dev = priv->dev;
  529. __ipoib_reap_ah(dev);
  530. if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
  531. queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
  532. round_jiffies_relative(HZ));
  533. }
  534. static void ipoib_ib_tx_timer_func(unsigned long ctx)
  535. {
  536. drain_tx_cq((struct net_device *)ctx);
  537. }
  538. int ipoib_ib_dev_open(struct net_device *dev)
  539. {
  540. struct ipoib_dev_priv *priv = netdev_priv(dev);
  541. int ret;
  542. if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {
  543. ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);
  544. clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  545. return -1;
  546. }
  547. set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  548. ret = ipoib_init_qp(dev);
  549. if (ret) {
  550. ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
  551. return -1;
  552. }
  553. ret = ipoib_ib_post_receives(dev);
  554. if (ret) {
  555. ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
  556. ipoib_ib_dev_stop(dev, 1);
  557. return -1;
  558. }
  559. ret = ipoib_cm_dev_open(dev);
  560. if (ret) {
  561. ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
  562. ipoib_ib_dev_stop(dev, 1);
  563. return -1;
  564. }
  565. clear_bit(IPOIB_STOP_REAPER, &priv->flags);
  566. queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
  567. round_jiffies_relative(HZ));
  568. if (!test_and_set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
  569. napi_enable(&priv->napi);
  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. if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
  663. napi_disable(&priv->napi);
  664. ipoib_cm_dev_stop(dev);
  665. /*
  666. * Move our QP to the error state and then reinitialize in
  667. * when all work requests have completed or have been flushed.
  668. */
  669. qp_attr.qp_state = IB_QPS_ERR;
  670. if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
  671. ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
  672. /* Wait for all sends and receives to complete */
  673. begin = jiffies;
  674. while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
  675. if (time_after(jiffies, begin + 5 * HZ)) {
  676. ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
  677. priv->tx_head - priv->tx_tail, recvs_pending(dev));
  678. /*
  679. * assume the HW is wedged and just free up
  680. * all our pending work requests.
  681. */
  682. while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
  683. tx_req = &priv->tx_ring[priv->tx_tail &
  684. (ipoib_sendq_size - 1)];
  685. ipoib_dma_unmap_tx(priv->ca, tx_req);
  686. dev_kfree_skb_any(tx_req->skb);
  687. ++priv->tx_tail;
  688. --priv->tx_outstanding;
  689. }
  690. for (i = 0; i < ipoib_recvq_size; ++i) {
  691. struct ipoib_rx_buf *rx_req;
  692. rx_req = &priv->rx_ring[i];
  693. if (!rx_req->skb)
  694. continue;
  695. ipoib_ud_dma_unmap_rx(priv,
  696. priv->rx_ring[i].mapping);
  697. dev_kfree_skb_any(rx_req->skb);
  698. rx_req->skb = NULL;
  699. }
  700. goto timeout;
  701. }
  702. ipoib_drain_cq(dev);
  703. msleep(1);
  704. }
  705. ipoib_dbg(priv, "All sends and receives done.\n");
  706. timeout:
  707. del_timer_sync(&priv->poll_timer);
  708. qp_attr.qp_state = IB_QPS_RESET;
  709. if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
  710. ipoib_warn(priv, "Failed to modify QP to RESET state\n");
  711. /* Wait for all AHs to be reaped */
  712. set_bit(IPOIB_STOP_REAPER, &priv->flags);
  713. cancel_delayed_work(&priv->ah_reap_task);
  714. if (flush)
  715. flush_workqueue(ipoib_workqueue);
  716. begin = jiffies;
  717. while (!list_empty(&priv->dead_ahs)) {
  718. __ipoib_reap_ah(dev);
  719. if (time_after(jiffies, begin + HZ)) {
  720. ipoib_warn(priv, "timing out; will leak address handles\n");
  721. break;
  722. }
  723. msleep(1);
  724. }
  725. ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP);
  726. return 0;
  727. }
  728. int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
  729. {
  730. struct ipoib_dev_priv *priv = netdev_priv(dev);
  731. priv->ca = ca;
  732. priv->port = port;
  733. priv->qp = NULL;
  734. if (ipoib_transport_dev_init(dev, ca)) {
  735. printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
  736. return -ENODEV;
  737. }
  738. setup_timer(&priv->poll_timer, ipoib_ib_tx_timer_func,
  739. (unsigned long) dev);
  740. if (dev->flags & IFF_UP) {
  741. if (ipoib_ib_dev_open(dev)) {
  742. ipoib_transport_dev_cleanup(dev);
  743. return -ENODEV;
  744. }
  745. }
  746. return 0;
  747. }
  748. static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv,
  749. enum ipoib_flush_level level)
  750. {
  751. struct ipoib_dev_priv *cpriv;
  752. struct net_device *dev = priv->dev;
  753. u16 new_index;
  754. mutex_lock(&priv->vlan_mutex);
  755. /*
  756. * Flush any child interfaces too -- they might be up even if
  757. * the parent is down.
  758. */
  759. list_for_each_entry(cpriv, &priv->child_intfs, list)
  760. __ipoib_ib_dev_flush(cpriv, level);
  761. mutex_unlock(&priv->vlan_mutex);
  762. if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
  763. ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
  764. return;
  765. }
  766. if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
  767. ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
  768. return;
  769. }
  770. if (level == IPOIB_FLUSH_HEAVY) {
  771. if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
  772. clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  773. ipoib_ib_dev_down(dev, 0);
  774. ipoib_ib_dev_stop(dev, 0);
  775. if (ipoib_pkey_dev_delay_open(dev))
  776. return;
  777. }
  778. /* restart QP only if P_Key index is changed */
  779. if (test_and_set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) &&
  780. new_index == priv->pkey_index) {
  781. ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
  782. return;
  783. }
  784. priv->pkey_index = new_index;
  785. }
  786. if (level == IPOIB_FLUSH_LIGHT) {
  787. ipoib_mark_paths_invalid(dev);
  788. ipoib_mcast_dev_flush(dev);
  789. }
  790. if (level >= IPOIB_FLUSH_NORMAL)
  791. ipoib_ib_dev_down(dev, 0);
  792. if (level == IPOIB_FLUSH_HEAVY) {
  793. ipoib_ib_dev_stop(dev, 0);
  794. ipoib_ib_dev_open(dev);
  795. }
  796. /*
  797. * The device could have been brought down between the start and when
  798. * we get here, don't bring it back up if it's not configured up
  799. */
  800. if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
  801. if (level >= IPOIB_FLUSH_NORMAL)
  802. ipoib_ib_dev_up(dev);
  803. ipoib_mcast_restart_task(&priv->restart_task);
  804. }
  805. }
  806. void ipoib_ib_dev_flush_light(struct work_struct *work)
  807. {
  808. struct ipoib_dev_priv *priv =
  809. container_of(work, struct ipoib_dev_priv, flush_light);
  810. __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_LIGHT);
  811. }
  812. void ipoib_ib_dev_flush_normal(struct work_struct *work)
  813. {
  814. struct ipoib_dev_priv *priv =
  815. container_of(work, struct ipoib_dev_priv, flush_normal);
  816. __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_NORMAL);
  817. }
  818. void ipoib_ib_dev_flush_heavy(struct work_struct *work)
  819. {
  820. struct ipoib_dev_priv *priv =
  821. container_of(work, struct ipoib_dev_priv, flush_heavy);
  822. __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_HEAVY);
  823. }
  824. void ipoib_ib_dev_cleanup(struct net_device *dev)
  825. {
  826. struct ipoib_dev_priv *priv = netdev_priv(dev);
  827. ipoib_dbg(priv, "cleaning up ib_dev\n");
  828. ipoib_mcast_stop_thread(dev, 1);
  829. ipoib_mcast_dev_flush(dev);
  830. ipoib_transport_dev_cleanup(dev);
  831. }
  832. /*
  833. * Delayed P_Key Assigment Interim Support
  834. *
  835. * The following is initial implementation of delayed P_Key assigment
  836. * mechanism. It is using the same approach implemented for the multicast
  837. * group join. The single goal of this implementation is to quickly address
  838. * Bug #2507. This implementation will probably be removed when the P_Key
  839. * change async notification is available.
  840. */
  841. void ipoib_pkey_poll(struct work_struct *work)
  842. {
  843. struct ipoib_dev_priv *priv =
  844. container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
  845. struct net_device *dev = priv->dev;
  846. ipoib_pkey_dev_check_presence(dev);
  847. if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
  848. ipoib_open(dev);
  849. else {
  850. mutex_lock(&pkey_mutex);
  851. if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
  852. queue_delayed_work(ipoib_workqueue,
  853. &priv->pkey_poll_task,
  854. HZ);
  855. mutex_unlock(&pkey_mutex);
  856. }
  857. }
  858. int ipoib_pkey_dev_delay_open(struct net_device *dev)
  859. {
  860. struct ipoib_dev_priv *priv = netdev_priv(dev);
  861. /* Look for the interface pkey value in the IB Port P_Key table and */
  862. /* set the interface pkey assigment flag */
  863. ipoib_pkey_dev_check_presence(dev);
  864. /* P_Key value not assigned yet - start polling */
  865. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  866. mutex_lock(&pkey_mutex);
  867. clear_bit(IPOIB_PKEY_STOP, &priv->flags);
  868. queue_delayed_work(ipoib_workqueue,
  869. &priv->pkey_poll_task,
  870. HZ);
  871. mutex_unlock(&pkey_mutex);
  872. return 1;
  873. }
  874. return 0;
  875. }