ipoib_ib.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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. * $Id: ipoib_ib.c 1386 2004-12-27 16:23:17Z roland $
  36. */
  37. #include <linux/delay.h>
  38. #include <linux/dma-mapping.h>
  39. #include <rdma/ib_cache.h>
  40. #include <linux/ip.h>
  41. #include <linux/tcp.h>
  42. #include "ipoib.h"
  43. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
  44. static int data_debug_level;
  45. module_param(data_debug_level, int, 0644);
  46. MODULE_PARM_DESC(data_debug_level,
  47. "Enable data path debug tracing if > 0");
  48. #endif
  49. static DEFINE_MUTEX(pkey_mutex);
  50. struct ipoib_ah *ipoib_create_ah(struct net_device *dev,
  51. struct ib_pd *pd, struct ib_ah_attr *attr)
  52. {
  53. struct ipoib_ah *ah;
  54. ah = kmalloc(sizeof *ah, GFP_KERNEL);
  55. if (!ah)
  56. return NULL;
  57. ah->dev = dev;
  58. ah->last_send = 0;
  59. kref_init(&ah->ref);
  60. ah->ah = ib_create_ah(pd, attr);
  61. if (IS_ERR(ah->ah)) {
  62. kfree(ah);
  63. ah = NULL;
  64. } else
  65. ipoib_dbg(netdev_priv(dev), "Created ah %p\n", ah->ah);
  66. return ah;
  67. }
  68. void ipoib_free_ah(struct kref *kref)
  69. {
  70. struct ipoib_ah *ah = container_of(kref, struct ipoib_ah, ref);
  71. struct ipoib_dev_priv *priv = netdev_priv(ah->dev);
  72. unsigned long flags;
  73. spin_lock_irqsave(&priv->lock, flags);
  74. list_add_tail(&ah->list, &priv->dead_ahs);
  75. spin_unlock_irqrestore(&priv->lock, flags);
  76. }
  77. static void ipoib_ud_dma_unmap_rx(struct ipoib_dev_priv *priv,
  78. u64 mapping[IPOIB_UD_RX_SG])
  79. {
  80. if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
  81. ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_UD_HEAD_SIZE,
  82. DMA_FROM_DEVICE);
  83. ib_dma_unmap_page(priv->ca, mapping[1], PAGE_SIZE,
  84. DMA_FROM_DEVICE);
  85. } else
  86. ib_dma_unmap_single(priv->ca, mapping[0],
  87. IPOIB_UD_BUF_SIZE(priv->max_ib_mtu),
  88. DMA_FROM_DEVICE);
  89. }
  90. static void ipoib_ud_skb_put_frags(struct ipoib_dev_priv *priv,
  91. struct sk_buff *skb,
  92. unsigned int length)
  93. {
  94. if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
  95. skb_frag_t *frag = &skb_shinfo(skb)->frags[0];
  96. unsigned int size;
  97. /*
  98. * There is only two buffers needed for max_payload = 4K,
  99. * first buf size is IPOIB_UD_HEAD_SIZE
  100. */
  101. skb->tail += IPOIB_UD_HEAD_SIZE;
  102. skb->len += length;
  103. size = length - IPOIB_UD_HEAD_SIZE;
  104. frag->size = size;
  105. skb->data_len += size;
  106. skb->truesize += size;
  107. } else
  108. skb_put(skb, length);
  109. }
  110. static int ipoib_ib_post_receive(struct net_device *dev, int id)
  111. {
  112. struct ipoib_dev_priv *priv = netdev_priv(dev);
  113. struct ib_recv_wr *bad_wr;
  114. int ret;
  115. priv->rx_wr.wr_id = id | IPOIB_OP_RECV;
  116. priv->rx_sge[0].addr = priv->rx_ring[id].mapping[0];
  117. priv->rx_sge[1].addr = priv->rx_ring[id].mapping[1];
  118. ret = ib_post_recv(priv->qp, &priv->rx_wr, &bad_wr);
  119. if (unlikely(ret)) {
  120. ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);
  121. ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[id].mapping);
  122. dev_kfree_skb_any(priv->rx_ring[id].skb);
  123. priv->rx_ring[id].skb = NULL;
  124. }
  125. return ret;
  126. }
  127. static struct sk_buff *ipoib_alloc_rx_skb(struct net_device *dev, int id)
  128. {
  129. struct ipoib_dev_priv *priv = netdev_priv(dev);
  130. struct sk_buff *skb;
  131. int buf_size;
  132. u64 *mapping;
  133. if (ipoib_ud_need_sg(priv->max_ib_mtu))
  134. buf_size = IPOIB_UD_HEAD_SIZE;
  135. else
  136. buf_size = IPOIB_UD_BUF_SIZE(priv->max_ib_mtu);
  137. skb = dev_alloc_skb(buf_size + 4);
  138. if (unlikely(!skb))
  139. return NULL;
  140. /*
  141. * IB will leave a 40 byte gap for a GRH and IPoIB adds a 4 byte
  142. * header. So we need 4 more bytes to get to 48 and align the
  143. * IP header to a multiple of 16.
  144. */
  145. skb_reserve(skb, 4);
  146. mapping = priv->rx_ring[id].mapping;
  147. mapping[0] = ib_dma_map_single(priv->ca, skb->data, buf_size,
  148. DMA_FROM_DEVICE);
  149. if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0])))
  150. goto error;
  151. if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
  152. struct page *page = alloc_page(GFP_ATOMIC);
  153. if (!page)
  154. goto partial_error;
  155. skb_fill_page_desc(skb, 0, page, 0, PAGE_SIZE);
  156. mapping[1] =
  157. ib_dma_map_page(priv->ca, skb_shinfo(skb)->frags[0].page,
  158. 0, PAGE_SIZE, DMA_FROM_DEVICE);
  159. if (unlikely(ib_dma_mapping_error(priv->ca, mapping[1])))
  160. goto partial_error;
  161. }
  162. priv->rx_ring[id].skb = skb;
  163. return skb;
  164. partial_error:
  165. ib_dma_unmap_single(priv->ca, mapping[0], buf_size, DMA_FROM_DEVICE);
  166. error:
  167. dev_kfree_skb_any(skb);
  168. return NULL;
  169. }
  170. static int ipoib_ib_post_receives(struct net_device *dev)
  171. {
  172. struct ipoib_dev_priv *priv = netdev_priv(dev);
  173. int i;
  174. for (i = 0; i < ipoib_recvq_size; ++i) {
  175. if (!ipoib_alloc_rx_skb(dev, i)) {
  176. ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
  177. return -ENOMEM;
  178. }
  179. if (ipoib_ib_post_receive(dev, i)) {
  180. ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
  181. return -EIO;
  182. }
  183. }
  184. return 0;
  185. }
  186. static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
  187. {
  188. struct ipoib_dev_priv *priv = netdev_priv(dev);
  189. unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
  190. struct sk_buff *skb;
  191. u64 mapping[IPOIB_UD_RX_SG];
  192. ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
  193. wr_id, wc->status);
  194. if (unlikely(wr_id >= ipoib_recvq_size)) {
  195. ipoib_warn(priv, "recv completion event with wrid %d (> %d)\n",
  196. wr_id, ipoib_recvq_size);
  197. return;
  198. }
  199. skb = priv->rx_ring[wr_id].skb;
  200. if (unlikely(wc->status != IB_WC_SUCCESS)) {
  201. if (wc->status != IB_WC_WR_FLUSH_ERR)
  202. ipoib_warn(priv, "failed recv event "
  203. "(status=%d, wrid=%d vend_err %x)\n",
  204. wc->status, wr_id, wc->vendor_err);
  205. ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[wr_id].mapping);
  206. dev_kfree_skb_any(skb);
  207. priv->rx_ring[wr_id].skb = NULL;
  208. return;
  209. }
  210. /*
  211. * Drop packets that this interface sent, ie multicast packets
  212. * that the HCA has replicated.
  213. */
  214. if (wc->slid == priv->local_lid && wc->src_qp == priv->qp->qp_num)
  215. goto repost;
  216. memcpy(mapping, priv->rx_ring[wr_id].mapping,
  217. IPOIB_UD_RX_SG * sizeof *mapping);
  218. /*
  219. * If we can't allocate a new RX buffer, dump
  220. * this packet and reuse the old buffer.
  221. */
  222. if (unlikely(!ipoib_alloc_rx_skb(dev, wr_id))) {
  223. ++dev->stats.rx_dropped;
  224. goto repost;
  225. }
  226. ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
  227. wc->byte_len, wc->slid);
  228. ipoib_ud_dma_unmap_rx(priv, mapping);
  229. ipoib_ud_skb_put_frags(priv, skb, wc->byte_len);
  230. skb_pull(skb, IB_GRH_BYTES);
  231. skb->protocol = ((struct ipoib_header *) skb->data)->proto;
  232. skb_reset_mac_header(skb);
  233. skb_pull(skb, IPOIB_ENCAP_LEN);
  234. dev->last_rx = jiffies;
  235. ++dev->stats.rx_packets;
  236. dev->stats.rx_bytes += skb->len;
  237. skb->dev = dev;
  238. /* XXX get correct PACKET_ type here */
  239. skb->pkt_type = PACKET_HOST;
  240. if (test_bit(IPOIB_FLAG_CSUM, &priv->flags) && likely(wc->csum_ok))
  241. skb->ip_summed = CHECKSUM_UNNECESSARY;
  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. netif_rx_complete(dev, napi);
  364. if (unlikely(ib_req_notify_cq(priv->recv_cq,
  365. IB_CQ_NEXT_COMP |
  366. IB_CQ_REPORT_MISSED_EVENTS)) &&
  367. netif_rx_reschedule(dev, napi))
  368. goto poll_more;
  369. }
  370. return done;
  371. }
  372. void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
  373. {
  374. struct net_device *dev = dev_ptr;
  375. struct ipoib_dev_priv *priv = netdev_priv(dev);
  376. netif_rx_schedule(dev, &priv->napi);
  377. }
  378. static void drain_tx_cq(struct net_device *dev)
  379. {
  380. struct ipoib_dev_priv *priv = netdev_priv(dev);
  381. unsigned long flags;
  382. spin_lock_irqsave(&priv->tx_lock, flags);
  383. while (poll_tx(priv))
  384. ; /* nothing */
  385. if (netif_queue_stopped(dev))
  386. mod_timer(&priv->poll_timer, jiffies + 1);
  387. spin_unlock_irqrestore(&priv->tx_lock, flags);
  388. }
  389. void ipoib_send_comp_handler(struct ib_cq *cq, void *dev_ptr)
  390. {
  391. drain_tx_cq((struct net_device *)dev_ptr);
  392. }
  393. static inline int post_send(struct ipoib_dev_priv *priv,
  394. unsigned int wr_id,
  395. struct ib_ah *address, u32 qpn,
  396. struct ipoib_tx_buf *tx_req,
  397. void *head, int hlen)
  398. {
  399. struct ib_send_wr *bad_wr;
  400. int i, off;
  401. struct sk_buff *skb = tx_req->skb;
  402. skb_frag_t *frags = skb_shinfo(skb)->frags;
  403. int nr_frags = skb_shinfo(skb)->nr_frags;
  404. u64 *mapping = tx_req->mapping;
  405. if (skb_headlen(skb)) {
  406. priv->tx_sge[0].addr = mapping[0];
  407. priv->tx_sge[0].length = skb_headlen(skb);
  408. off = 1;
  409. } else
  410. off = 0;
  411. for (i = 0; i < nr_frags; ++i) {
  412. priv->tx_sge[i + off].addr = mapping[i + off];
  413. priv->tx_sge[i + off].length = frags[i].size;
  414. }
  415. priv->tx_wr.num_sge = nr_frags + off;
  416. priv->tx_wr.wr_id = wr_id;
  417. priv->tx_wr.wr.ud.remote_qpn = qpn;
  418. priv->tx_wr.wr.ud.ah = address;
  419. if (head) {
  420. priv->tx_wr.wr.ud.mss = skb_shinfo(skb)->gso_size;
  421. priv->tx_wr.wr.ud.header = head;
  422. priv->tx_wr.wr.ud.hlen = hlen;
  423. priv->tx_wr.opcode = IB_WR_LSO;
  424. } else
  425. priv->tx_wr.opcode = IB_WR_SEND;
  426. return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
  427. }
  428. void ipoib_send(struct net_device *dev, struct sk_buff *skb,
  429. struct ipoib_ah *address, u32 qpn)
  430. {
  431. struct ipoib_dev_priv *priv = netdev_priv(dev);
  432. struct ipoib_tx_buf *tx_req;
  433. int hlen;
  434. void *phead;
  435. if (skb_is_gso(skb)) {
  436. hlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
  437. phead = skb->data;
  438. if (unlikely(!skb_pull(skb, hlen))) {
  439. ipoib_warn(priv, "linear data too small\n");
  440. ++dev->stats.tx_dropped;
  441. ++dev->stats.tx_errors;
  442. dev_kfree_skb_any(skb);
  443. return;
  444. }
  445. } else {
  446. if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
  447. ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
  448. skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
  449. ++dev->stats.tx_dropped;
  450. ++dev->stats.tx_errors;
  451. ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
  452. return;
  453. }
  454. phead = NULL;
  455. hlen = 0;
  456. }
  457. ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
  458. skb->len, address, qpn);
  459. /*
  460. * We put the skb into the tx_ring _before_ we call post_send()
  461. * because it's entirely possible that the completion handler will
  462. * run before we execute anything after the post_send(). That
  463. * means we have to make sure everything is properly recorded and
  464. * our state is consistent before we call post_send().
  465. */
  466. tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
  467. tx_req->skb = skb;
  468. if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) {
  469. ++dev->stats.tx_errors;
  470. dev_kfree_skb_any(skb);
  471. return;
  472. }
  473. if (skb->ip_summed == CHECKSUM_PARTIAL)
  474. priv->tx_wr.send_flags |= IB_SEND_IP_CSUM;
  475. else
  476. priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
  477. if (++priv->tx_outstanding == ipoib_sendq_size) {
  478. ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
  479. if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP))
  480. ipoib_warn(priv, "request notify on send CQ failed\n");
  481. netif_stop_queue(dev);
  482. }
  483. if (unlikely(post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
  484. address->ah, qpn, tx_req, phead, hlen))) {
  485. ipoib_warn(priv, "post_send failed\n");
  486. ++dev->stats.tx_errors;
  487. --priv->tx_outstanding;
  488. ipoib_dma_unmap_tx(priv->ca, tx_req);
  489. dev_kfree_skb_any(skb);
  490. if (netif_queue_stopped(dev))
  491. netif_wake_queue(dev);
  492. } else {
  493. dev->trans_start = jiffies;
  494. address->last_send = priv->tx_head;
  495. ++priv->tx_head;
  496. skb_orphan(skb);
  497. }
  498. if (unlikely(priv->tx_outstanding > MAX_SEND_CQE))
  499. while (poll_tx(priv))
  500. ; /* nothing */
  501. }
  502. static void __ipoib_reap_ah(struct net_device *dev)
  503. {
  504. struct ipoib_dev_priv *priv = netdev_priv(dev);
  505. struct ipoib_ah *ah, *tah;
  506. LIST_HEAD(remove_list);
  507. spin_lock_irq(&priv->tx_lock);
  508. spin_lock(&priv->lock);
  509. list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
  510. if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
  511. list_del(&ah->list);
  512. ib_destroy_ah(ah->ah);
  513. kfree(ah);
  514. }
  515. spin_unlock(&priv->lock);
  516. spin_unlock_irq(&priv->tx_lock);
  517. }
  518. void ipoib_reap_ah(struct work_struct *work)
  519. {
  520. struct ipoib_dev_priv *priv =
  521. container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
  522. struct net_device *dev = priv->dev;
  523. __ipoib_reap_ah(dev);
  524. if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
  525. queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
  526. round_jiffies_relative(HZ));
  527. }
  528. static void ipoib_ib_tx_timer_func(unsigned long ctx)
  529. {
  530. drain_tx_cq((struct net_device *)ctx);
  531. }
  532. int ipoib_ib_dev_open(struct net_device *dev)
  533. {
  534. struct ipoib_dev_priv *priv = netdev_priv(dev);
  535. int ret;
  536. if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {
  537. ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);
  538. clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  539. return -1;
  540. }
  541. set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  542. ret = ipoib_init_qp(dev);
  543. if (ret) {
  544. ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
  545. return -1;
  546. }
  547. ret = ipoib_ib_post_receives(dev);
  548. if (ret) {
  549. ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
  550. ipoib_ib_dev_stop(dev, 1);
  551. return -1;
  552. }
  553. ret = ipoib_cm_dev_open(dev);
  554. if (ret) {
  555. ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
  556. ipoib_ib_dev_stop(dev, 1);
  557. return -1;
  558. }
  559. clear_bit(IPOIB_STOP_REAPER, &priv->flags);
  560. queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
  561. round_jiffies_relative(HZ));
  562. init_timer(&priv->poll_timer);
  563. priv->poll_timer.function = ipoib_ib_tx_timer_func;
  564. priv->poll_timer.data = (unsigned long)dev;
  565. set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
  566. return 0;
  567. }
  568. static void ipoib_pkey_dev_check_presence(struct net_device *dev)
  569. {
  570. struct ipoib_dev_priv *priv = netdev_priv(dev);
  571. u16 pkey_index = 0;
  572. if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
  573. clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  574. else
  575. set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  576. }
  577. int ipoib_ib_dev_up(struct net_device *dev)
  578. {
  579. struct ipoib_dev_priv *priv = netdev_priv(dev);
  580. ipoib_pkey_dev_check_presence(dev);
  581. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  582. ipoib_dbg(priv, "PKEY is not assigned.\n");
  583. return 0;
  584. }
  585. set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
  586. return ipoib_mcast_start_thread(dev);
  587. }
  588. int ipoib_ib_dev_down(struct net_device *dev, int flush)
  589. {
  590. struct ipoib_dev_priv *priv = netdev_priv(dev);
  591. ipoib_dbg(priv, "downing ib_dev\n");
  592. clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
  593. netif_carrier_off(dev);
  594. /* Shutdown the P_Key thread if still active */
  595. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  596. mutex_lock(&pkey_mutex);
  597. set_bit(IPOIB_PKEY_STOP, &priv->flags);
  598. cancel_delayed_work(&priv->pkey_poll_task);
  599. mutex_unlock(&pkey_mutex);
  600. if (flush)
  601. flush_workqueue(ipoib_workqueue);
  602. }
  603. ipoib_mcast_stop_thread(dev, flush);
  604. ipoib_mcast_dev_flush(dev);
  605. ipoib_flush_paths(dev);
  606. return 0;
  607. }
  608. static int recvs_pending(struct net_device *dev)
  609. {
  610. struct ipoib_dev_priv *priv = netdev_priv(dev);
  611. int pending = 0;
  612. int i;
  613. for (i = 0; i < ipoib_recvq_size; ++i)
  614. if (priv->rx_ring[i].skb)
  615. ++pending;
  616. return pending;
  617. }
  618. void ipoib_drain_cq(struct net_device *dev)
  619. {
  620. struct ipoib_dev_priv *priv = netdev_priv(dev);
  621. int i, n;
  622. do {
  623. n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);
  624. for (i = 0; i < n; ++i) {
  625. /*
  626. * Convert any successful completions to flush
  627. * errors to avoid passing packets up the
  628. * stack after bringing the device down.
  629. */
  630. if (priv->ibwc[i].status == IB_WC_SUCCESS)
  631. priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
  632. if (priv->ibwc[i].wr_id & IPOIB_OP_RECV) {
  633. if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
  634. ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
  635. else
  636. ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
  637. } else
  638. ipoib_cm_handle_tx_wc(dev, priv->ibwc + i);
  639. }
  640. } while (n == IPOIB_NUM_WC);
  641. while (poll_tx(priv))
  642. ; /* nothing */
  643. }
  644. int ipoib_ib_dev_stop(struct net_device *dev, int flush)
  645. {
  646. struct ipoib_dev_priv *priv = netdev_priv(dev);
  647. struct ib_qp_attr qp_attr;
  648. unsigned long begin;
  649. struct ipoib_tx_buf *tx_req;
  650. int i;
  651. clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
  652. ipoib_cm_dev_stop(dev);
  653. /*
  654. * Move our QP to the error state and then reinitialize in
  655. * when all work requests have completed or have been flushed.
  656. */
  657. qp_attr.qp_state = IB_QPS_ERR;
  658. if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
  659. ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
  660. /* Wait for all sends and receives to complete */
  661. begin = jiffies;
  662. while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
  663. if (time_after(jiffies, begin + 5 * HZ)) {
  664. ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
  665. priv->tx_head - priv->tx_tail, recvs_pending(dev));
  666. /*
  667. * assume the HW is wedged and just free up
  668. * all our pending work requests.
  669. */
  670. while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
  671. tx_req = &priv->tx_ring[priv->tx_tail &
  672. (ipoib_sendq_size - 1)];
  673. ipoib_dma_unmap_tx(priv->ca, tx_req);
  674. dev_kfree_skb_any(tx_req->skb);
  675. ++priv->tx_tail;
  676. --priv->tx_outstanding;
  677. }
  678. for (i = 0; i < ipoib_recvq_size; ++i) {
  679. struct ipoib_rx_buf *rx_req;
  680. rx_req = &priv->rx_ring[i];
  681. if (!rx_req->skb)
  682. continue;
  683. ipoib_ud_dma_unmap_rx(priv,
  684. priv->rx_ring[i].mapping);
  685. dev_kfree_skb_any(rx_req->skb);
  686. rx_req->skb = NULL;
  687. }
  688. goto timeout;
  689. }
  690. ipoib_drain_cq(dev);
  691. msleep(1);
  692. }
  693. ipoib_dbg(priv, "All sends and receives done.\n");
  694. timeout:
  695. del_timer_sync(&priv->poll_timer);
  696. qp_attr.qp_state = IB_QPS_RESET;
  697. if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
  698. ipoib_warn(priv, "Failed to modify QP to RESET state\n");
  699. /* Wait for all AHs to be reaped */
  700. set_bit(IPOIB_STOP_REAPER, &priv->flags);
  701. cancel_delayed_work(&priv->ah_reap_task);
  702. if (flush)
  703. flush_workqueue(ipoib_workqueue);
  704. begin = jiffies;
  705. while (!list_empty(&priv->dead_ahs)) {
  706. __ipoib_reap_ah(dev);
  707. if (time_after(jiffies, begin + HZ)) {
  708. ipoib_warn(priv, "timing out; will leak address handles\n");
  709. break;
  710. }
  711. msleep(1);
  712. }
  713. ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP);
  714. return 0;
  715. }
  716. int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
  717. {
  718. struct ipoib_dev_priv *priv = netdev_priv(dev);
  719. priv->ca = ca;
  720. priv->port = port;
  721. priv->qp = NULL;
  722. if (ipoib_transport_dev_init(dev, ca)) {
  723. printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
  724. return -ENODEV;
  725. }
  726. if (dev->flags & IFF_UP) {
  727. if (ipoib_ib_dev_open(dev)) {
  728. ipoib_transport_dev_cleanup(dev);
  729. return -ENODEV;
  730. }
  731. }
  732. return 0;
  733. }
  734. static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv, int pkey_event)
  735. {
  736. struct ipoib_dev_priv *cpriv;
  737. struct net_device *dev = priv->dev;
  738. u16 new_index;
  739. mutex_lock(&priv->vlan_mutex);
  740. /*
  741. * Flush any child interfaces too -- they might be up even if
  742. * the parent is down.
  743. */
  744. list_for_each_entry(cpriv, &priv->child_intfs, list)
  745. __ipoib_ib_dev_flush(cpriv, pkey_event);
  746. mutex_unlock(&priv->vlan_mutex);
  747. if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
  748. ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
  749. return;
  750. }
  751. if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
  752. ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
  753. return;
  754. }
  755. if (pkey_event) {
  756. if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
  757. clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  758. ipoib_ib_dev_down(dev, 0);
  759. ipoib_ib_dev_stop(dev, 0);
  760. if (ipoib_pkey_dev_delay_open(dev))
  761. return;
  762. }
  763. /* restart QP only if P_Key index is changed */
  764. if (test_and_set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) &&
  765. new_index == priv->pkey_index) {
  766. ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
  767. return;
  768. }
  769. priv->pkey_index = new_index;
  770. }
  771. ipoib_dbg(priv, "flushing\n");
  772. ipoib_ib_dev_down(dev, 0);
  773. if (pkey_event) {
  774. ipoib_ib_dev_stop(dev, 0);
  775. ipoib_ib_dev_open(dev);
  776. }
  777. /*
  778. * The device could have been brought down between the start and when
  779. * we get here, don't bring it back up if it's not configured up
  780. */
  781. if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
  782. ipoib_ib_dev_up(dev);
  783. ipoib_mcast_restart_task(&priv->restart_task);
  784. }
  785. }
  786. void ipoib_ib_dev_flush(struct work_struct *work)
  787. {
  788. struct ipoib_dev_priv *priv =
  789. container_of(work, struct ipoib_dev_priv, flush_task);
  790. ipoib_dbg(priv, "Flushing %s\n", priv->dev->name);
  791. __ipoib_ib_dev_flush(priv, 0);
  792. }
  793. void ipoib_pkey_event(struct work_struct *work)
  794. {
  795. struct ipoib_dev_priv *priv =
  796. container_of(work, struct ipoib_dev_priv, pkey_event_task);
  797. ipoib_dbg(priv, "Flushing %s and restarting its QP\n", priv->dev->name);
  798. __ipoib_ib_dev_flush(priv, 1);
  799. }
  800. void ipoib_ib_dev_cleanup(struct net_device *dev)
  801. {
  802. struct ipoib_dev_priv *priv = netdev_priv(dev);
  803. ipoib_dbg(priv, "cleaning up ib_dev\n");
  804. ipoib_mcast_stop_thread(dev, 1);
  805. ipoib_mcast_dev_flush(dev);
  806. ipoib_transport_dev_cleanup(dev);
  807. }
  808. /*
  809. * Delayed P_Key Assigment Interim Support
  810. *
  811. * The following is initial implementation of delayed P_Key assigment
  812. * mechanism. It is using the same approach implemented for the multicast
  813. * group join. The single goal of this implementation is to quickly address
  814. * Bug #2507. This implementation will probably be removed when the P_Key
  815. * change async notification is available.
  816. */
  817. void ipoib_pkey_poll(struct work_struct *work)
  818. {
  819. struct ipoib_dev_priv *priv =
  820. container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
  821. struct net_device *dev = priv->dev;
  822. ipoib_pkey_dev_check_presence(dev);
  823. if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
  824. ipoib_open(dev);
  825. else {
  826. mutex_lock(&pkey_mutex);
  827. if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
  828. queue_delayed_work(ipoib_workqueue,
  829. &priv->pkey_poll_task,
  830. HZ);
  831. mutex_unlock(&pkey_mutex);
  832. }
  833. }
  834. int ipoib_pkey_dev_delay_open(struct net_device *dev)
  835. {
  836. struct ipoib_dev_priv *priv = netdev_priv(dev);
  837. /* Look for the interface pkey value in the IB Port P_Key table and */
  838. /* set the interface pkey assigment flag */
  839. ipoib_pkey_dev_check_presence(dev);
  840. /* P_Key value not assigned yet - start polling */
  841. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  842. mutex_lock(&pkey_mutex);
  843. clear_bit(IPOIB_PKEY_STOP, &priv->flags);
  844. queue_delayed_work(ipoib_workqueue,
  845. &priv->pkey_poll_task,
  846. HZ);
  847. mutex_unlock(&pkey_mutex);
  848. return 1;
  849. }
  850. return 0;
  851. }