ipoib_ib.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  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 <rdma/ib_cache.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. struct ipoib_ah *ipoib_create_ah(struct net_device *dev,
  50. struct ib_pd *pd, struct rdma_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 = rdma_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(ipoib_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 = ipoib_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. ib_dma_unmap_single(priv->ca, mapping[0],
  83. IPOIB_UD_BUF_SIZE(priv->max_ib_mtu),
  84. DMA_FROM_DEVICE);
  85. }
  86. static int ipoib_ib_post_receive(struct net_device *dev, int id)
  87. {
  88. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  89. int ret;
  90. priv->rx_wr.wr_id = id | IPOIB_OP_RECV;
  91. priv->rx_sge[0].addr = priv->rx_ring[id].mapping[0];
  92. priv->rx_sge[1].addr = priv->rx_ring[id].mapping[1];
  93. ret = ib_post_recv(priv->qp, &priv->rx_wr, NULL);
  94. if (unlikely(ret)) {
  95. ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);
  96. ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[id].mapping);
  97. dev_kfree_skb_any(priv->rx_ring[id].skb);
  98. priv->rx_ring[id].skb = NULL;
  99. }
  100. return ret;
  101. }
  102. static struct sk_buff *ipoib_alloc_rx_skb(struct net_device *dev, int id)
  103. {
  104. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  105. struct sk_buff *skb;
  106. int buf_size;
  107. u64 *mapping;
  108. buf_size = IPOIB_UD_BUF_SIZE(priv->max_ib_mtu);
  109. skb = dev_alloc_skb(buf_size + IPOIB_HARD_LEN);
  110. if (unlikely(!skb))
  111. return NULL;
  112. /*
  113. * the IP header will be at IPOIP_HARD_LEN + IB_GRH_BYTES, that is
  114. * 64 bytes aligned
  115. */
  116. skb_reserve(skb, sizeof(struct ipoib_pseudo_header));
  117. mapping = priv->rx_ring[id].mapping;
  118. mapping[0] = ib_dma_map_single(priv->ca, skb->data, buf_size,
  119. DMA_FROM_DEVICE);
  120. if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0])))
  121. goto error;
  122. priv->rx_ring[id].skb = skb;
  123. return skb;
  124. error:
  125. dev_kfree_skb_any(skb);
  126. return NULL;
  127. }
  128. static int ipoib_ib_post_receives(struct net_device *dev)
  129. {
  130. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  131. int i;
  132. for (i = 0; i < ipoib_recvq_size; ++i) {
  133. if (!ipoib_alloc_rx_skb(dev, i)) {
  134. ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
  135. return -ENOMEM;
  136. }
  137. if (ipoib_ib_post_receive(dev, i)) {
  138. ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
  139. return -EIO;
  140. }
  141. }
  142. return 0;
  143. }
  144. static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
  145. {
  146. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  147. unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
  148. struct sk_buff *skb;
  149. u64 mapping[IPOIB_UD_RX_SG];
  150. union ib_gid *dgid;
  151. union ib_gid *sgid;
  152. ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
  153. wr_id, wc->status);
  154. if (unlikely(wr_id >= ipoib_recvq_size)) {
  155. ipoib_warn(priv, "recv completion event with wrid %d (> %d)\n",
  156. wr_id, ipoib_recvq_size);
  157. return;
  158. }
  159. skb = priv->rx_ring[wr_id].skb;
  160. if (unlikely(wc->status != IB_WC_SUCCESS)) {
  161. if (wc->status != IB_WC_WR_FLUSH_ERR)
  162. ipoib_warn(priv,
  163. "failed recv event (status=%d, wrid=%d vend_err %#x)\n",
  164. wc->status, wr_id, wc->vendor_err);
  165. ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[wr_id].mapping);
  166. dev_kfree_skb_any(skb);
  167. priv->rx_ring[wr_id].skb = NULL;
  168. return;
  169. }
  170. memcpy(mapping, priv->rx_ring[wr_id].mapping,
  171. IPOIB_UD_RX_SG * sizeof(*mapping));
  172. /*
  173. * If we can't allocate a new RX buffer, dump
  174. * this packet and reuse the old buffer.
  175. */
  176. if (unlikely(!ipoib_alloc_rx_skb(dev, wr_id))) {
  177. ++dev->stats.rx_dropped;
  178. goto repost;
  179. }
  180. ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
  181. wc->byte_len, wc->slid);
  182. ipoib_ud_dma_unmap_rx(priv, mapping);
  183. skb_put(skb, wc->byte_len);
  184. /* First byte of dgid signals multicast when 0xff */
  185. dgid = &((struct ib_grh *)skb->data)->dgid;
  186. if (!(wc->wc_flags & IB_WC_GRH) || dgid->raw[0] != 0xff)
  187. skb->pkt_type = PACKET_HOST;
  188. else if (memcmp(dgid, dev->broadcast + 4, sizeof(union ib_gid)) == 0)
  189. skb->pkt_type = PACKET_BROADCAST;
  190. else
  191. skb->pkt_type = PACKET_MULTICAST;
  192. sgid = &((struct ib_grh *)skb->data)->sgid;
  193. /*
  194. * Drop packets that this interface sent, ie multicast packets
  195. * that the HCA has replicated.
  196. */
  197. if (wc->slid == priv->local_lid && wc->src_qp == priv->qp->qp_num) {
  198. int need_repost = 1;
  199. if ((wc->wc_flags & IB_WC_GRH) &&
  200. sgid->global.interface_id != priv->local_gid.global.interface_id)
  201. need_repost = 0;
  202. if (need_repost) {
  203. dev_kfree_skb_any(skb);
  204. goto repost;
  205. }
  206. }
  207. skb_pull(skb, IB_GRH_BYTES);
  208. skb->protocol = ((struct ipoib_header *) skb->data)->proto;
  209. skb_add_pseudo_hdr(skb);
  210. ++dev->stats.rx_packets;
  211. dev->stats.rx_bytes += skb->len;
  212. if (skb->pkt_type == PACKET_MULTICAST)
  213. dev->stats.multicast++;
  214. skb->dev = dev;
  215. if ((dev->features & NETIF_F_RXCSUM) &&
  216. likely(wc->wc_flags & IB_WC_IP_CSUM_OK))
  217. skb->ip_summed = CHECKSUM_UNNECESSARY;
  218. napi_gro_receive(&priv->recv_napi, skb);
  219. repost:
  220. if (unlikely(ipoib_ib_post_receive(dev, wr_id)))
  221. ipoib_warn(priv, "ipoib_ib_post_receive failed "
  222. "for buf %d\n", wr_id);
  223. }
  224. int ipoib_dma_map_tx(struct ib_device *ca, struct ipoib_tx_buf *tx_req)
  225. {
  226. struct sk_buff *skb = tx_req->skb;
  227. u64 *mapping = tx_req->mapping;
  228. int i;
  229. int off;
  230. if (skb_headlen(skb)) {
  231. mapping[0] = ib_dma_map_single(ca, skb->data, skb_headlen(skb),
  232. DMA_TO_DEVICE);
  233. if (unlikely(ib_dma_mapping_error(ca, mapping[0])))
  234. return -EIO;
  235. off = 1;
  236. } else
  237. off = 0;
  238. for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
  239. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  240. mapping[i + off] = ib_dma_map_page(ca,
  241. skb_frag_page(frag),
  242. frag->page_offset, skb_frag_size(frag),
  243. DMA_TO_DEVICE);
  244. if (unlikely(ib_dma_mapping_error(ca, mapping[i + off])))
  245. goto partial_error;
  246. }
  247. return 0;
  248. partial_error:
  249. for (; i > 0; --i) {
  250. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
  251. ib_dma_unmap_page(ca, mapping[i - !off], skb_frag_size(frag), DMA_TO_DEVICE);
  252. }
  253. if (off)
  254. ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
  255. return -EIO;
  256. }
  257. void ipoib_dma_unmap_tx(struct ipoib_dev_priv *priv,
  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. ib_dma_unmap_single(priv->ca, mapping[0], skb_headlen(skb),
  266. DMA_TO_DEVICE);
  267. off = 1;
  268. } else
  269. off = 0;
  270. for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
  271. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  272. ib_dma_unmap_page(priv->ca, mapping[i + off],
  273. skb_frag_size(frag), DMA_TO_DEVICE);
  274. }
  275. }
  276. /*
  277. * As the result of a completion error the QP Can be transferred to SQE states.
  278. * The function checks if the (send)QP is in SQE state and
  279. * moves it back to RTS state, that in order to have it functional again.
  280. */
  281. static void ipoib_qp_state_validate_work(struct work_struct *work)
  282. {
  283. struct ipoib_qp_state_validate *qp_work =
  284. container_of(work, struct ipoib_qp_state_validate, work);
  285. struct ipoib_dev_priv *priv = qp_work->priv;
  286. struct ib_qp_attr qp_attr;
  287. struct ib_qp_init_attr query_init_attr;
  288. int ret;
  289. ret = ib_query_qp(priv->qp, &qp_attr, IB_QP_STATE, &query_init_attr);
  290. if (ret) {
  291. ipoib_warn(priv, "%s: Failed to query QP ret: %d\n",
  292. __func__, ret);
  293. goto free_res;
  294. }
  295. pr_info("%s: QP: 0x%x is in state: %d\n",
  296. __func__, priv->qp->qp_num, qp_attr.qp_state);
  297. /* currently support only in SQE->RTS transition*/
  298. if (qp_attr.qp_state == IB_QPS_SQE) {
  299. qp_attr.qp_state = IB_QPS_RTS;
  300. ret = ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE);
  301. if (ret) {
  302. pr_warn("failed(%d) modify QP:0x%x SQE->RTS\n",
  303. ret, priv->qp->qp_num);
  304. goto free_res;
  305. }
  306. pr_info("%s: QP: 0x%x moved from IB_QPS_SQE to IB_QPS_RTS\n",
  307. __func__, priv->qp->qp_num);
  308. } else {
  309. pr_warn("QP (%d) will stay in state: %d\n",
  310. priv->qp->qp_num, qp_attr.qp_state);
  311. }
  312. free_res:
  313. kfree(qp_work);
  314. }
  315. static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
  316. {
  317. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  318. unsigned int wr_id = wc->wr_id;
  319. struct ipoib_tx_buf *tx_req;
  320. ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
  321. wr_id, wc->status);
  322. if (unlikely(wr_id >= ipoib_sendq_size)) {
  323. ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
  324. wr_id, ipoib_sendq_size);
  325. return;
  326. }
  327. tx_req = &priv->tx_ring[wr_id];
  328. ipoib_dma_unmap_tx(priv, tx_req);
  329. ++dev->stats.tx_packets;
  330. dev->stats.tx_bytes += tx_req->skb->len;
  331. dev_kfree_skb_any(tx_req->skb);
  332. ++priv->tx_tail;
  333. ++priv->global_tx_tail;
  334. if (unlikely(netif_queue_stopped(dev) &&
  335. ((priv->global_tx_head - priv->global_tx_tail) <=
  336. ipoib_sendq_size >> 1) &&
  337. test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)))
  338. netif_wake_queue(dev);
  339. if (wc->status != IB_WC_SUCCESS &&
  340. wc->status != IB_WC_WR_FLUSH_ERR) {
  341. struct ipoib_qp_state_validate *qp_work;
  342. ipoib_warn(priv,
  343. "failed send event (status=%d, wrid=%d vend_err %#x)\n",
  344. wc->status, wr_id, wc->vendor_err);
  345. qp_work = kzalloc(sizeof(*qp_work), GFP_ATOMIC);
  346. if (!qp_work)
  347. return;
  348. INIT_WORK(&qp_work->work, ipoib_qp_state_validate_work);
  349. qp_work->priv = priv;
  350. queue_work(priv->wq, &qp_work->work);
  351. }
  352. }
  353. static int poll_tx(struct ipoib_dev_priv *priv)
  354. {
  355. int n, i;
  356. struct ib_wc *wc;
  357. n = ib_poll_cq(priv->send_cq, MAX_SEND_CQE, priv->send_wc);
  358. for (i = 0; i < n; ++i) {
  359. wc = priv->send_wc + i;
  360. if (wc->wr_id & IPOIB_OP_CM)
  361. ipoib_cm_handle_tx_wc(priv->dev, priv->send_wc + i);
  362. else
  363. ipoib_ib_handle_tx_wc(priv->dev, priv->send_wc + i);
  364. }
  365. return n == MAX_SEND_CQE;
  366. }
  367. int ipoib_rx_poll(struct napi_struct *napi, int budget)
  368. {
  369. struct ipoib_dev_priv *priv =
  370. container_of(napi, struct ipoib_dev_priv, recv_napi);
  371. struct net_device *dev = priv->dev;
  372. int done;
  373. int t;
  374. int n, i;
  375. done = 0;
  376. poll_more:
  377. while (done < budget) {
  378. int max = (budget - done);
  379. t = min(IPOIB_NUM_WC, max);
  380. n = ib_poll_cq(priv->recv_cq, t, priv->ibwc);
  381. for (i = 0; i < n; i++) {
  382. struct ib_wc *wc = priv->ibwc + i;
  383. if (wc->wr_id & IPOIB_OP_RECV) {
  384. ++done;
  385. if (wc->wr_id & IPOIB_OP_CM)
  386. ipoib_cm_handle_rx_wc(dev, wc);
  387. else
  388. ipoib_ib_handle_rx_wc(dev, wc);
  389. } else {
  390. pr_warn("%s: Got unexpected wqe id\n", __func__);
  391. }
  392. }
  393. if (n != t)
  394. break;
  395. }
  396. if (done < budget) {
  397. napi_complete(napi);
  398. if (unlikely(ib_req_notify_cq(priv->recv_cq,
  399. IB_CQ_NEXT_COMP |
  400. IB_CQ_REPORT_MISSED_EVENTS)) &&
  401. napi_reschedule(napi))
  402. goto poll_more;
  403. }
  404. return done;
  405. }
  406. int ipoib_tx_poll(struct napi_struct *napi, int budget)
  407. {
  408. struct ipoib_dev_priv *priv = container_of(napi, struct ipoib_dev_priv,
  409. send_napi);
  410. struct net_device *dev = priv->dev;
  411. int n, i;
  412. struct ib_wc *wc;
  413. poll_more:
  414. n = ib_poll_cq(priv->send_cq, MAX_SEND_CQE, priv->send_wc);
  415. for (i = 0; i < n; i++) {
  416. wc = priv->send_wc + i;
  417. if (wc->wr_id & IPOIB_OP_CM)
  418. ipoib_cm_handle_tx_wc(dev, wc);
  419. else
  420. ipoib_ib_handle_tx_wc(dev, wc);
  421. }
  422. if (n < budget) {
  423. napi_complete(napi);
  424. if (unlikely(ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP |
  425. IB_CQ_REPORT_MISSED_EVENTS)) &&
  426. napi_reschedule(napi))
  427. goto poll_more;
  428. }
  429. return n < 0 ? 0 : n;
  430. }
  431. void ipoib_ib_rx_completion(struct ib_cq *cq, void *ctx_ptr)
  432. {
  433. struct ipoib_dev_priv *priv = ctx_ptr;
  434. napi_schedule(&priv->recv_napi);
  435. }
  436. void ipoib_ib_tx_completion(struct ib_cq *cq, void *ctx_ptr)
  437. {
  438. struct ipoib_dev_priv *priv = ctx_ptr;
  439. napi_schedule(&priv->send_napi);
  440. }
  441. static inline int post_send(struct ipoib_dev_priv *priv,
  442. unsigned int wr_id,
  443. struct ib_ah *address, u32 dqpn,
  444. struct ipoib_tx_buf *tx_req,
  445. void *head, int hlen)
  446. {
  447. struct sk_buff *skb = tx_req->skb;
  448. ipoib_build_sge(priv, tx_req);
  449. priv->tx_wr.wr.wr_id = wr_id;
  450. priv->tx_wr.remote_qpn = dqpn;
  451. priv->tx_wr.ah = address;
  452. if (head) {
  453. priv->tx_wr.mss = skb_shinfo(skb)->gso_size;
  454. priv->tx_wr.header = head;
  455. priv->tx_wr.hlen = hlen;
  456. priv->tx_wr.wr.opcode = IB_WR_LSO;
  457. } else
  458. priv->tx_wr.wr.opcode = IB_WR_SEND;
  459. return ib_post_send(priv->qp, &priv->tx_wr.wr, NULL);
  460. }
  461. int ipoib_send(struct net_device *dev, struct sk_buff *skb,
  462. struct ib_ah *address, u32 dqpn)
  463. {
  464. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  465. struct ipoib_tx_buf *tx_req;
  466. int hlen, rc;
  467. void *phead;
  468. unsigned int usable_sge = priv->max_send_sge - !!skb_headlen(skb);
  469. if (skb_is_gso(skb)) {
  470. hlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
  471. phead = skb->data;
  472. if (unlikely(!skb_pull(skb, hlen))) {
  473. ipoib_warn(priv, "linear data too small\n");
  474. ++dev->stats.tx_dropped;
  475. ++dev->stats.tx_errors;
  476. dev_kfree_skb_any(skb);
  477. return -1;
  478. }
  479. } else {
  480. if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
  481. ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
  482. skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
  483. ++dev->stats.tx_dropped;
  484. ++dev->stats.tx_errors;
  485. ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
  486. return -1;
  487. }
  488. phead = NULL;
  489. hlen = 0;
  490. }
  491. if (skb_shinfo(skb)->nr_frags > usable_sge) {
  492. if (skb_linearize(skb) < 0) {
  493. ipoib_warn(priv, "skb could not be linearized\n");
  494. ++dev->stats.tx_dropped;
  495. ++dev->stats.tx_errors;
  496. dev_kfree_skb_any(skb);
  497. return -1;
  498. }
  499. /* Does skb_linearize return ok without reducing nr_frags? */
  500. if (skb_shinfo(skb)->nr_frags > usable_sge) {
  501. ipoib_warn(priv, "too many frags after skb linearize\n");
  502. ++dev->stats.tx_dropped;
  503. ++dev->stats.tx_errors;
  504. dev_kfree_skb_any(skb);
  505. return -1;
  506. }
  507. }
  508. ipoib_dbg_data(priv,
  509. "sending packet, length=%d address=%p dqpn=0x%06x\n",
  510. skb->len, address, dqpn);
  511. /*
  512. * We put the skb into the tx_ring _before_ we call post_send()
  513. * because it's entirely possible that the completion handler will
  514. * run before we execute anything after the post_send(). That
  515. * means we have to make sure everything is properly recorded and
  516. * our state is consistent before we call post_send().
  517. */
  518. tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
  519. tx_req->skb = skb;
  520. if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) {
  521. ++dev->stats.tx_errors;
  522. dev_kfree_skb_any(skb);
  523. return -1;
  524. }
  525. if (skb->ip_summed == CHECKSUM_PARTIAL)
  526. priv->tx_wr.wr.send_flags |= IB_SEND_IP_CSUM;
  527. else
  528. priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM;
  529. /* increase the tx_head after send success, but use it for queue state */
  530. if ((priv->global_tx_head - priv->global_tx_tail) ==
  531. ipoib_sendq_size - 1) {
  532. ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
  533. netif_stop_queue(dev);
  534. }
  535. skb_orphan(skb);
  536. skb_dst_drop(skb);
  537. if (netif_queue_stopped(dev))
  538. if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP |
  539. IB_CQ_REPORT_MISSED_EVENTS) < 0)
  540. ipoib_warn(priv, "request notify on send CQ failed\n");
  541. rc = post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
  542. address, dqpn, tx_req, phead, hlen);
  543. if (unlikely(rc)) {
  544. ipoib_warn(priv, "post_send failed, error %d\n", rc);
  545. ++dev->stats.tx_errors;
  546. ipoib_dma_unmap_tx(priv, tx_req);
  547. dev_kfree_skb_any(skb);
  548. if (netif_queue_stopped(dev))
  549. netif_wake_queue(dev);
  550. rc = 0;
  551. } else {
  552. netif_trans_update(dev);
  553. rc = priv->tx_head;
  554. ++priv->tx_head;
  555. ++priv->global_tx_head;
  556. }
  557. return rc;
  558. }
  559. static void ipoib_reap_dead_ahs(struct ipoib_dev_priv *priv)
  560. {
  561. struct ipoib_ah *ah, *tah;
  562. LIST_HEAD(remove_list);
  563. unsigned long flags;
  564. netif_tx_lock_bh(priv->dev);
  565. spin_lock_irqsave(&priv->lock, flags);
  566. list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
  567. if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
  568. list_del(&ah->list);
  569. rdma_destroy_ah(ah->ah);
  570. kfree(ah);
  571. }
  572. spin_unlock_irqrestore(&priv->lock, flags);
  573. netif_tx_unlock_bh(priv->dev);
  574. }
  575. void ipoib_reap_ah(struct work_struct *work)
  576. {
  577. struct ipoib_dev_priv *priv =
  578. container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
  579. ipoib_reap_dead_ahs(priv);
  580. if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
  581. queue_delayed_work(priv->wq, &priv->ah_reap_task,
  582. round_jiffies_relative(HZ));
  583. }
  584. static void ipoib_start_ah_reaper(struct ipoib_dev_priv *priv)
  585. {
  586. clear_bit(IPOIB_STOP_REAPER, &priv->flags);
  587. queue_delayed_work(priv->wq, &priv->ah_reap_task,
  588. round_jiffies_relative(HZ));
  589. }
  590. static void ipoib_stop_ah_reaper(struct ipoib_dev_priv *priv)
  591. {
  592. set_bit(IPOIB_STOP_REAPER, &priv->flags);
  593. cancel_delayed_work(&priv->ah_reap_task);
  594. /*
  595. * After ipoib_stop_ah_reaper() we always go through
  596. * ipoib_reap_dead_ahs() which ensures the work is really stopped and
  597. * does a final flush out of the dead_ah's list
  598. */
  599. }
  600. static int recvs_pending(struct net_device *dev)
  601. {
  602. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  603. int pending = 0;
  604. int i;
  605. for (i = 0; i < ipoib_recvq_size; ++i)
  606. if (priv->rx_ring[i].skb)
  607. ++pending;
  608. return pending;
  609. }
  610. static void check_qp_movement_and_print(struct ipoib_dev_priv *priv,
  611. struct ib_qp *qp,
  612. enum ib_qp_state new_state)
  613. {
  614. struct ib_qp_attr qp_attr;
  615. struct ib_qp_init_attr query_init_attr;
  616. int ret;
  617. ret = ib_query_qp(qp, &qp_attr, IB_QP_STATE, &query_init_attr);
  618. if (ret) {
  619. ipoib_warn(priv, "%s: Failed to query QP\n", __func__);
  620. return;
  621. }
  622. /* print according to the new-state and the previous state.*/
  623. if (new_state == IB_QPS_ERR && qp_attr.qp_state == IB_QPS_RESET)
  624. ipoib_dbg(priv, "Failed modify QP, IB_QPS_RESET to IB_QPS_ERR, acceptable\n");
  625. else
  626. ipoib_warn(priv, "Failed to modify QP to state: %d from state: %d\n",
  627. new_state, qp_attr.qp_state);
  628. }
  629. static void ipoib_napi_enable(struct net_device *dev)
  630. {
  631. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  632. napi_enable(&priv->recv_napi);
  633. napi_enable(&priv->send_napi);
  634. }
  635. static void ipoib_napi_disable(struct net_device *dev)
  636. {
  637. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  638. napi_disable(&priv->recv_napi);
  639. napi_disable(&priv->send_napi);
  640. }
  641. int ipoib_ib_dev_stop_default(struct net_device *dev)
  642. {
  643. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  644. struct ib_qp_attr qp_attr;
  645. unsigned long begin;
  646. struct ipoib_tx_buf *tx_req;
  647. int i;
  648. if (test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
  649. ipoib_napi_disable(dev);
  650. ipoib_cm_dev_stop(dev);
  651. /*
  652. * Move our QP to the error state and then reinitialize in
  653. * when all work requests have completed or have been flushed.
  654. */
  655. qp_attr.qp_state = IB_QPS_ERR;
  656. if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
  657. check_qp_movement_and_print(priv, priv->qp, IB_QPS_ERR);
  658. /* Wait for all sends and receives to complete */
  659. begin = jiffies;
  660. while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
  661. if (time_after(jiffies, begin + 5 * HZ)) {
  662. ipoib_warn(priv,
  663. "timing out; %d sends %d receives not completed\n",
  664. priv->tx_head - priv->tx_tail,
  665. 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, tx_req);
  674. dev_kfree_skb_any(tx_req->skb);
  675. ++priv->tx_tail;
  676. ++priv->global_tx_tail;
  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. usleep_range(1000, 2000);
  692. }
  693. ipoib_dbg(priv, "All sends and receives done.\n");
  694. timeout:
  695. qp_attr.qp_state = IB_QPS_RESET;
  696. if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
  697. ipoib_warn(priv, "Failed to modify QP to RESET state\n");
  698. ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP);
  699. return 0;
  700. }
  701. int ipoib_ib_dev_open_default(struct net_device *dev)
  702. {
  703. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  704. int ret;
  705. ret = ipoib_init_qp(dev);
  706. if (ret) {
  707. ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
  708. return -1;
  709. }
  710. ret = ipoib_ib_post_receives(dev);
  711. if (ret) {
  712. ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
  713. goto out;
  714. }
  715. ret = ipoib_cm_dev_open(dev);
  716. if (ret) {
  717. ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
  718. goto out;
  719. }
  720. if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
  721. ipoib_napi_enable(dev);
  722. return 0;
  723. out:
  724. return -1;
  725. }
  726. int ipoib_ib_dev_open(struct net_device *dev)
  727. {
  728. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  729. ipoib_pkey_dev_check_presence(dev);
  730. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  731. ipoib_warn(priv, "P_Key 0x%04x is %s\n", priv->pkey,
  732. (!(priv->pkey & 0x7fff) ? "Invalid" : "not found"));
  733. return -1;
  734. }
  735. ipoib_start_ah_reaper(priv);
  736. if (priv->rn_ops->ndo_open(dev)) {
  737. pr_warn("%s: Failed to open dev\n", dev->name);
  738. goto dev_stop;
  739. }
  740. set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
  741. return 0;
  742. dev_stop:
  743. ipoib_stop_ah_reaper(priv);
  744. return -1;
  745. }
  746. void ipoib_ib_dev_stop(struct net_device *dev)
  747. {
  748. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  749. priv->rn_ops->ndo_stop(dev);
  750. clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
  751. ipoib_stop_ah_reaper(priv);
  752. }
  753. void ipoib_pkey_dev_check_presence(struct net_device *dev)
  754. {
  755. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  756. struct rdma_netdev *rn = netdev_priv(dev);
  757. if (!(priv->pkey & 0x7fff) ||
  758. ib_find_pkey(priv->ca, priv->port, priv->pkey,
  759. &priv->pkey_index)) {
  760. clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  761. } else {
  762. if (rn->set_id)
  763. rn->set_id(dev, priv->pkey_index);
  764. set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  765. }
  766. }
  767. void ipoib_ib_dev_up(struct net_device *dev)
  768. {
  769. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  770. ipoib_pkey_dev_check_presence(dev);
  771. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  772. ipoib_dbg(priv, "PKEY is not assigned.\n");
  773. return;
  774. }
  775. set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
  776. ipoib_mcast_start_thread(dev);
  777. }
  778. void ipoib_ib_dev_down(struct net_device *dev)
  779. {
  780. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  781. ipoib_dbg(priv, "downing ib_dev\n");
  782. clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
  783. netif_carrier_off(dev);
  784. ipoib_mcast_stop_thread(dev);
  785. ipoib_mcast_dev_flush(dev);
  786. ipoib_flush_paths(dev);
  787. }
  788. void ipoib_drain_cq(struct net_device *dev)
  789. {
  790. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  791. int i, n;
  792. /*
  793. * We call completion handling routines that expect to be
  794. * called from the BH-disabled NAPI poll context, so disable
  795. * BHs here too.
  796. */
  797. local_bh_disable();
  798. do {
  799. n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);
  800. for (i = 0; i < n; ++i) {
  801. /*
  802. * Convert any successful completions to flush
  803. * errors to avoid passing packets up the
  804. * stack after bringing the device down.
  805. */
  806. if (priv->ibwc[i].status == IB_WC_SUCCESS)
  807. priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
  808. if (priv->ibwc[i].wr_id & IPOIB_OP_RECV) {
  809. if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
  810. ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
  811. else
  812. ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
  813. } else {
  814. pr_warn("%s: Got unexpected wqe id\n", __func__);
  815. }
  816. }
  817. } while (n == IPOIB_NUM_WC);
  818. while (poll_tx(priv))
  819. ; /* nothing */
  820. local_bh_enable();
  821. }
  822. /*
  823. * Takes whatever value which is in pkey index 0 and updates priv->pkey
  824. * returns 0 if the pkey value was changed.
  825. */
  826. static inline int update_parent_pkey(struct ipoib_dev_priv *priv)
  827. {
  828. int result;
  829. u16 prev_pkey;
  830. prev_pkey = priv->pkey;
  831. result = ib_query_pkey(priv->ca, priv->port, 0, &priv->pkey);
  832. if (result) {
  833. ipoib_warn(priv, "ib_query_pkey port %d failed (ret = %d)\n",
  834. priv->port, result);
  835. return result;
  836. }
  837. priv->pkey |= 0x8000;
  838. if (prev_pkey != priv->pkey) {
  839. ipoib_dbg(priv, "pkey changed from 0x%x to 0x%x\n",
  840. prev_pkey, priv->pkey);
  841. /*
  842. * Update the pkey in the broadcast address, while making sure to set
  843. * the full membership bit, so that we join the right broadcast group.
  844. */
  845. priv->dev->broadcast[8] = priv->pkey >> 8;
  846. priv->dev->broadcast[9] = priv->pkey & 0xff;
  847. return 0;
  848. }
  849. return 1;
  850. }
  851. /*
  852. * returns 0 if pkey value was found in a different slot.
  853. */
  854. static inline int update_child_pkey(struct ipoib_dev_priv *priv)
  855. {
  856. u16 old_index = priv->pkey_index;
  857. priv->pkey_index = 0;
  858. ipoib_pkey_dev_check_presence(priv->dev);
  859. if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) &&
  860. (old_index == priv->pkey_index))
  861. return 1;
  862. return 0;
  863. }
  864. /*
  865. * returns true if the device address of the ipoib interface has changed and the
  866. * new address is a valid one (i.e in the gid table), return false otherwise.
  867. */
  868. static bool ipoib_dev_addr_changed_valid(struct ipoib_dev_priv *priv)
  869. {
  870. union ib_gid search_gid;
  871. union ib_gid gid0;
  872. union ib_gid *netdev_gid;
  873. int err;
  874. u16 index;
  875. u8 port;
  876. bool ret = false;
  877. netdev_gid = (union ib_gid *)(priv->dev->dev_addr + 4);
  878. if (rdma_query_gid(priv->ca, priv->port, 0, &gid0))
  879. return false;
  880. netif_addr_lock_bh(priv->dev);
  881. /* The subnet prefix may have changed, update it now so we won't have
  882. * to do it later
  883. */
  884. priv->local_gid.global.subnet_prefix = gid0.global.subnet_prefix;
  885. netdev_gid->global.subnet_prefix = gid0.global.subnet_prefix;
  886. search_gid.global.subnet_prefix = gid0.global.subnet_prefix;
  887. search_gid.global.interface_id = priv->local_gid.global.interface_id;
  888. netif_addr_unlock_bh(priv->dev);
  889. err = ib_find_gid(priv->ca, &search_gid, &port, &index);
  890. netif_addr_lock_bh(priv->dev);
  891. if (search_gid.global.interface_id !=
  892. priv->local_gid.global.interface_id)
  893. /* There was a change while we were looking up the gid, bail
  894. * here and let the next work sort this out
  895. */
  896. goto out;
  897. /* The next section of code needs some background:
  898. * Per IB spec the port GUID can't change if the HCA is powered on.
  899. * port GUID is the basis for GID at index 0 which is the basis for
  900. * the default device address of a ipoib interface.
  901. *
  902. * so it seems the flow should be:
  903. * if user_changed_dev_addr && gid in gid tbl
  904. * set bit dev_addr_set
  905. * return true
  906. * else
  907. * return false
  908. *
  909. * The issue is that there are devices that don't follow the spec,
  910. * they change the port GUID when the HCA is powered, so in order
  911. * not to break userspace applications, We need to check if the
  912. * user wanted to control the device address and we assume that
  913. * if he sets the device address back to be based on GID index 0,
  914. * he no longer wishs to control it.
  915. *
  916. * If the user doesn't control the the device address,
  917. * IPOIB_FLAG_DEV_ADDR_SET is set and ib_find_gid failed it means
  918. * the port GUID has changed and GID at index 0 has changed
  919. * so we need to change priv->local_gid and priv->dev->dev_addr
  920. * to reflect the new GID.
  921. */
  922. if (!test_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags)) {
  923. if (!err && port == priv->port) {
  924. set_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
  925. if (index == 0)
  926. clear_bit(IPOIB_FLAG_DEV_ADDR_CTRL,
  927. &priv->flags);
  928. else
  929. set_bit(IPOIB_FLAG_DEV_ADDR_CTRL, &priv->flags);
  930. ret = true;
  931. } else {
  932. ret = false;
  933. }
  934. } else {
  935. if (!err && port == priv->port) {
  936. ret = true;
  937. } else {
  938. if (!test_bit(IPOIB_FLAG_DEV_ADDR_CTRL, &priv->flags)) {
  939. memcpy(&priv->local_gid, &gid0,
  940. sizeof(priv->local_gid));
  941. memcpy(priv->dev->dev_addr + 4, &gid0,
  942. sizeof(priv->local_gid));
  943. ret = true;
  944. }
  945. }
  946. }
  947. out:
  948. netif_addr_unlock_bh(priv->dev);
  949. return ret;
  950. }
  951. static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv,
  952. enum ipoib_flush_level level,
  953. int nesting)
  954. {
  955. struct ipoib_dev_priv *cpriv;
  956. struct net_device *dev = priv->dev;
  957. int result;
  958. down_read_nested(&priv->vlan_rwsem, nesting);
  959. /*
  960. * Flush any child interfaces too -- they might be up even if
  961. * the parent is down.
  962. */
  963. list_for_each_entry(cpriv, &priv->child_intfs, list)
  964. __ipoib_ib_dev_flush(cpriv, level, nesting + 1);
  965. up_read(&priv->vlan_rwsem);
  966. if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags) &&
  967. level != IPOIB_FLUSH_HEAVY) {
  968. /* Make sure the dev_addr is set even if not flushing */
  969. if (level == IPOIB_FLUSH_LIGHT)
  970. ipoib_dev_addr_changed_valid(priv);
  971. ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
  972. return;
  973. }
  974. if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
  975. /* interface is down. update pkey and leave. */
  976. if (level == IPOIB_FLUSH_HEAVY) {
  977. if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
  978. update_parent_pkey(priv);
  979. else
  980. update_child_pkey(priv);
  981. } else if (level == IPOIB_FLUSH_LIGHT)
  982. ipoib_dev_addr_changed_valid(priv);
  983. ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
  984. return;
  985. }
  986. if (level == IPOIB_FLUSH_HEAVY) {
  987. /* child devices chase their origin pkey value, while non-child
  988. * (parent) devices should always takes what present in pkey index 0
  989. */
  990. if (test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
  991. result = update_child_pkey(priv);
  992. if (result) {
  993. /* restart QP only if P_Key index is changed */
  994. ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
  995. return;
  996. }
  997. } else {
  998. result = update_parent_pkey(priv);
  999. /* restart QP only if P_Key value changed */
  1000. if (result) {
  1001. ipoib_dbg(priv, "Not flushing - P_Key value not changed.\n");
  1002. return;
  1003. }
  1004. }
  1005. }
  1006. if (level == IPOIB_FLUSH_LIGHT) {
  1007. int oper_up;
  1008. ipoib_mark_paths_invalid(dev);
  1009. /* Set IPoIB operation as down to prevent races between:
  1010. * the flush flow which leaves MCG and on the fly joins
  1011. * which can happen during that time. mcast restart task
  1012. * should deal with join requests we missed.
  1013. */
  1014. oper_up = test_and_clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
  1015. ipoib_mcast_dev_flush(dev);
  1016. if (oper_up)
  1017. set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
  1018. ipoib_reap_dead_ahs(priv);
  1019. }
  1020. if (level >= IPOIB_FLUSH_NORMAL)
  1021. ipoib_ib_dev_down(dev);
  1022. if (level == IPOIB_FLUSH_HEAVY) {
  1023. if (test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
  1024. ipoib_ib_dev_stop(dev);
  1025. if (ipoib_ib_dev_open(dev))
  1026. return;
  1027. if (netif_queue_stopped(dev))
  1028. netif_start_queue(dev);
  1029. }
  1030. /*
  1031. * The device could have been brought down between the start and when
  1032. * we get here, don't bring it back up if it's not configured up
  1033. */
  1034. if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
  1035. if (level >= IPOIB_FLUSH_NORMAL)
  1036. ipoib_ib_dev_up(dev);
  1037. if (ipoib_dev_addr_changed_valid(priv))
  1038. ipoib_mcast_restart_task(&priv->restart_task);
  1039. }
  1040. }
  1041. void ipoib_ib_dev_flush_light(struct work_struct *work)
  1042. {
  1043. struct ipoib_dev_priv *priv =
  1044. container_of(work, struct ipoib_dev_priv, flush_light);
  1045. __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_LIGHT, 0);
  1046. }
  1047. void ipoib_ib_dev_flush_normal(struct work_struct *work)
  1048. {
  1049. struct ipoib_dev_priv *priv =
  1050. container_of(work, struct ipoib_dev_priv, flush_normal);
  1051. __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_NORMAL, 0);
  1052. }
  1053. void ipoib_ib_dev_flush_heavy(struct work_struct *work)
  1054. {
  1055. struct ipoib_dev_priv *priv =
  1056. container_of(work, struct ipoib_dev_priv, flush_heavy);
  1057. rtnl_lock();
  1058. __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_HEAVY, 0);
  1059. rtnl_unlock();
  1060. }
  1061. void ipoib_ib_dev_cleanup(struct net_device *dev)
  1062. {
  1063. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  1064. ipoib_dbg(priv, "cleaning up ib_dev\n");
  1065. /*
  1066. * We must make sure there are no more (path) completions
  1067. * that may wish to touch priv fields that are no longer valid
  1068. */
  1069. ipoib_flush_paths(dev);
  1070. ipoib_mcast_stop_thread(dev);
  1071. ipoib_mcast_dev_flush(dev);
  1072. /*
  1073. * All of our ah references aren't free until after
  1074. * ipoib_mcast_dev_flush(), ipoib_flush_paths, and
  1075. * the neighbor garbage collection is stopped and reaped.
  1076. * That should all be done now, so make a final ah flush.
  1077. */
  1078. ipoib_reap_dead_ahs(priv);
  1079. clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  1080. priv->rn_ops->ndo_uninit(dev);
  1081. if (priv->pd) {
  1082. ib_dealloc_pd(priv->pd);
  1083. priv->pd = NULL;
  1084. }
  1085. }