ipoib_ib.c 35 KB

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