ipoib_ib.c 27 KB

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