ipoib_ib.c 27 KB

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