ipoib_ib.c 33 KB

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