ipoib_ib.c 27 KB

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