ipoib_ib.c 34 KB

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