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