ipoib_ib.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. /*
  2. * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  4. * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
  5. * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the
  11. * OpenIB.org BSD license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials
  24. * provided with the distribution.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. * SOFTWARE.
  34. */
  35. #include <linux/delay.h>
  36. #include <linux/moduleparam.h>
  37. #include <linux/dma-mapping.h>
  38. #include <linux/slab.h>
  39. #include <linux/ip.h>
  40. #include <linux/tcp.h>
  41. #include "ipoib.h"
  42. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
  43. static int data_debug_level;
  44. module_param(data_debug_level, int, 0644);
  45. MODULE_PARM_DESC(data_debug_level,
  46. "Enable data path debug tracing if > 0");
  47. #endif
  48. static DEFINE_MUTEX(pkey_mutex);
  49. struct ipoib_ah *ipoib_create_ah(struct net_device *dev,
  50. struct ib_pd *pd, struct ib_ah_attr *attr)
  51. {
  52. struct ipoib_ah *ah;
  53. struct ib_ah *vah;
  54. ah = kmalloc(sizeof *ah, GFP_KERNEL);
  55. if (!ah)
  56. return ERR_PTR(-ENOMEM);
  57. ah->dev = dev;
  58. ah->last_send = 0;
  59. kref_init(&ah->ref);
  60. vah = ib_create_ah(pd, attr);
  61. if (IS_ERR(vah)) {
  62. kfree(ah);
  63. ah = (struct ipoib_ah *)vah;
  64. } else {
  65. ah->ah = vah;
  66. ipoib_dbg(netdev_priv(dev), "Created ah %p\n", ah->ah);
  67. }
  68. return ah;
  69. }
  70. void ipoib_free_ah(struct kref *kref)
  71. {
  72. struct ipoib_ah *ah = container_of(kref, struct ipoib_ah, ref);
  73. struct ipoib_dev_priv *priv = netdev_priv(ah->dev);
  74. unsigned long flags;
  75. spin_lock_irqsave(&priv->lock, flags);
  76. list_add_tail(&ah->list, &priv->dead_ahs);
  77. spin_unlock_irqrestore(&priv->lock, flags);
  78. }
  79. static void ipoib_ud_dma_unmap_rx(struct ipoib_dev_priv *priv,
  80. u64 mapping[IPOIB_UD_RX_SG])
  81. {
  82. 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 = netdev_priv(dev);
  89. struct ib_recv_wr *bad_wr;
  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, &bad_wr);
  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 = netdev_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_ENCAP_LEN);
  111. if (unlikely(!skb))
  112. return NULL;
  113. /*
  114. * IB will leave a 40 byte gap for a GRH and IPoIB adds a 4 byte
  115. * header. So we need 4 more bytes to get to 48 and align the
  116. * IP header to a multiple of 16.
  117. */
  118. skb_reserve(skb, 4);
  119. mapping = priv->rx_ring[id].mapping;
  120. mapping[0] = ib_dma_map_single(priv->ca, skb->data, buf_size,
  121. DMA_FROM_DEVICE);
  122. if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0])))
  123. goto error;
  124. priv->rx_ring[id].skb = skb;
  125. return skb;
  126. error:
  127. dev_kfree_skb_any(skb);
  128. return NULL;
  129. }
  130. static int ipoib_ib_post_receives(struct net_device *dev)
  131. {
  132. struct ipoib_dev_priv *priv = netdev_priv(dev);
  133. int i;
  134. for (i = 0; i < ipoib_recvq_size; ++i) {
  135. if (!ipoib_alloc_rx_skb(dev, i)) {
  136. ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
  137. return -ENOMEM;
  138. }
  139. if (ipoib_ib_post_receive(dev, i)) {
  140. ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
  141. return -EIO;
  142. }
  143. }
  144. return 0;
  145. }
  146. static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
  147. {
  148. struct ipoib_dev_priv *priv = netdev_priv(dev);
  149. unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
  150. struct sk_buff *skb;
  151. u64 mapping[IPOIB_UD_RX_SG];
  152. union ib_gid *dgid;
  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, "failed recv event "
  164. "(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. /*
  172. * Drop packets that this interface sent, ie multicast packets
  173. * that the HCA has replicated.
  174. */
  175. if (wc->slid == priv->local_lid && wc->src_qp == priv->qp->qp_num)
  176. goto repost;
  177. memcpy(mapping, priv->rx_ring[wr_id].mapping,
  178. IPOIB_UD_RX_SG * sizeof *mapping);
  179. /*
  180. * If we can't allocate a new RX buffer, dump
  181. * this packet and reuse the old buffer.
  182. */
  183. if (unlikely(!ipoib_alloc_rx_skb(dev, wr_id))) {
  184. ++dev->stats.rx_dropped;
  185. goto repost;
  186. }
  187. ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
  188. wc->byte_len, wc->slid);
  189. ipoib_ud_dma_unmap_rx(priv, mapping);
  190. skb_put(skb, wc->byte_len);
  191. /* First byte of dgid signals multicast when 0xff */
  192. dgid = &((struct ib_grh *)skb->data)->dgid;
  193. if (!(wc->wc_flags & IB_WC_GRH) || dgid->raw[0] != 0xff)
  194. skb->pkt_type = PACKET_HOST;
  195. else if (memcmp(dgid, dev->broadcast + 4, sizeof(union ib_gid)) == 0)
  196. skb->pkt_type = PACKET_BROADCAST;
  197. else
  198. skb->pkt_type = PACKET_MULTICAST;
  199. skb_pull(skb, IB_GRH_BYTES);
  200. skb->protocol = ((struct ipoib_header *) skb->data)->proto;
  201. skb_reset_mac_header(skb);
  202. skb_pull(skb, IPOIB_ENCAP_LEN);
  203. skb->truesize = SKB_TRUESIZE(skb->len);
  204. ++dev->stats.rx_packets;
  205. dev->stats.rx_bytes += skb->len;
  206. skb->dev = dev;
  207. if ((dev->features & NETIF_F_RXCSUM) &&
  208. likely(wc->wc_flags & IB_WC_IP_CSUM_OK))
  209. skb->ip_summed = CHECKSUM_UNNECESSARY;
  210. napi_gro_receive(&priv->napi, skb);
  211. repost:
  212. if (unlikely(ipoib_ib_post_receive(dev, wr_id)))
  213. ipoib_warn(priv, "ipoib_ib_post_receive failed "
  214. "for buf %d\n", wr_id);
  215. }
  216. int ipoib_dma_map_tx(struct ib_device *ca, struct ipoib_tx_buf *tx_req)
  217. {
  218. struct sk_buff *skb = tx_req->skb;
  219. u64 *mapping = tx_req->mapping;
  220. int i;
  221. int off;
  222. if (skb_headlen(skb)) {
  223. mapping[0] = ib_dma_map_single(ca, skb->data, skb_headlen(skb),
  224. DMA_TO_DEVICE);
  225. if (unlikely(ib_dma_mapping_error(ca, mapping[0])))
  226. return -EIO;
  227. off = 1;
  228. } else
  229. off = 0;
  230. for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
  231. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  232. mapping[i + off] = ib_dma_map_page(ca,
  233. skb_frag_page(frag),
  234. frag->page_offset, skb_frag_size(frag),
  235. DMA_TO_DEVICE);
  236. if (unlikely(ib_dma_mapping_error(ca, mapping[i + off])))
  237. goto partial_error;
  238. }
  239. return 0;
  240. partial_error:
  241. for (; i > 0; --i) {
  242. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
  243. ib_dma_unmap_page(ca, mapping[i - !off], skb_frag_size(frag), DMA_TO_DEVICE);
  244. }
  245. if (off)
  246. ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
  247. return -EIO;
  248. }
  249. void ipoib_dma_unmap_tx(struct ipoib_dev_priv *priv,
  250. struct ipoib_tx_buf *tx_req)
  251. {
  252. struct sk_buff *skb = tx_req->skb;
  253. u64 *mapping = tx_req->mapping;
  254. int i;
  255. int off;
  256. if (skb_headlen(skb)) {
  257. ib_dma_unmap_single(priv->ca, mapping[0], skb_headlen(skb),
  258. DMA_TO_DEVICE);
  259. off = 1;
  260. } else
  261. off = 0;
  262. for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
  263. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  264. ib_dma_unmap_page(priv->ca, mapping[i + off],
  265. skb_frag_size(frag), DMA_TO_DEVICE);
  266. }
  267. }
  268. /*
  269. * As the result of a completion error the QP Can be transferred to SQE states.
  270. * The function checks if the (send)QP is in SQE state and
  271. * moves it back to RTS state, that in order to have it functional again.
  272. */
  273. static void ipoib_qp_state_validate_work(struct work_struct *work)
  274. {
  275. struct ipoib_qp_state_validate *qp_work =
  276. container_of(work, struct ipoib_qp_state_validate, work);
  277. struct ipoib_dev_priv *priv = qp_work->priv;
  278. struct ib_qp_attr qp_attr;
  279. struct ib_qp_init_attr query_init_attr;
  280. int ret;
  281. ret = ib_query_qp(priv->qp, &qp_attr, IB_QP_STATE, &query_init_attr);
  282. if (ret) {
  283. ipoib_warn(priv, "%s: Failed to query QP ret: %d\n",
  284. __func__, ret);
  285. goto free_res;
  286. }
  287. pr_info("%s: QP: 0x%x is in state: %d\n",
  288. __func__, priv->qp->qp_num, qp_attr.qp_state);
  289. /* currently support only in SQE->RTS transition*/
  290. if (qp_attr.qp_state == IB_QPS_SQE) {
  291. qp_attr.qp_state = IB_QPS_RTS;
  292. ret = ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE);
  293. if (ret) {
  294. pr_warn("failed(%d) modify QP:0x%x SQE->RTS\n",
  295. ret, priv->qp->qp_num);
  296. goto free_res;
  297. }
  298. pr_info("%s: QP: 0x%x moved from IB_QPS_SQE to IB_QPS_RTS\n",
  299. __func__, priv->qp->qp_num);
  300. } else {
  301. pr_warn("QP (%d) will stay in state: %d\n",
  302. priv->qp->qp_num, qp_attr.qp_state);
  303. }
  304. free_res:
  305. kfree(qp_work);
  306. }
  307. static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
  308. {
  309. struct ipoib_dev_priv *priv = netdev_priv(dev);
  310. unsigned int wr_id = wc->wr_id;
  311. struct ipoib_tx_buf *tx_req;
  312. ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
  313. wr_id, wc->status);
  314. if (unlikely(wr_id >= ipoib_sendq_size)) {
  315. ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
  316. wr_id, ipoib_sendq_size);
  317. return;
  318. }
  319. tx_req = &priv->tx_ring[wr_id];
  320. ipoib_dma_unmap_tx(priv, tx_req);
  321. ++dev->stats.tx_packets;
  322. dev->stats.tx_bytes += tx_req->skb->len;
  323. dev_kfree_skb_any(tx_req->skb);
  324. ++priv->tx_tail;
  325. if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
  326. netif_queue_stopped(dev) &&
  327. test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
  328. netif_wake_queue(dev);
  329. if (wc->status != IB_WC_SUCCESS &&
  330. wc->status != IB_WC_WR_FLUSH_ERR) {
  331. struct ipoib_qp_state_validate *qp_work;
  332. ipoib_warn(priv, "failed send event "
  333. "(status=%d, wrid=%d vend_err %x)\n",
  334. wc->status, wr_id, wc->vendor_err);
  335. qp_work = kzalloc(sizeof(*qp_work), GFP_ATOMIC);
  336. if (!qp_work) {
  337. ipoib_warn(priv, "%s Failed alloc ipoib_qp_state_validate for qp: 0x%x\n",
  338. __func__, priv->qp->qp_num);
  339. return;
  340. }
  341. INIT_WORK(&qp_work->work, ipoib_qp_state_validate_work);
  342. qp_work->priv = priv;
  343. queue_work(priv->wq, &qp_work->work);
  344. }
  345. }
  346. static int poll_tx(struct ipoib_dev_priv *priv)
  347. {
  348. int n, i;
  349. n = ib_poll_cq(priv->send_cq, MAX_SEND_CQE, priv->send_wc);
  350. for (i = 0; i < n; ++i)
  351. ipoib_ib_handle_tx_wc(priv->dev, priv->send_wc + i);
  352. return n == MAX_SEND_CQE;
  353. }
  354. int ipoib_poll(struct napi_struct *napi, int budget)
  355. {
  356. struct ipoib_dev_priv *priv = container_of(napi, struct ipoib_dev_priv, napi);
  357. struct net_device *dev = priv->dev;
  358. int done;
  359. int t;
  360. int n, i;
  361. done = 0;
  362. poll_more:
  363. while (done < budget) {
  364. int max = (budget - done);
  365. t = min(IPOIB_NUM_WC, max);
  366. n = ib_poll_cq(priv->recv_cq, t, priv->ibwc);
  367. for (i = 0; i < n; i++) {
  368. struct ib_wc *wc = priv->ibwc + i;
  369. if (wc->wr_id & IPOIB_OP_RECV) {
  370. ++done;
  371. if (wc->wr_id & IPOIB_OP_CM)
  372. ipoib_cm_handle_rx_wc(dev, wc);
  373. else
  374. ipoib_ib_handle_rx_wc(dev, wc);
  375. } else
  376. ipoib_cm_handle_tx_wc(priv->dev, wc);
  377. }
  378. if (n != t)
  379. break;
  380. }
  381. if (done < budget) {
  382. napi_complete(napi);
  383. if (unlikely(ib_req_notify_cq(priv->recv_cq,
  384. IB_CQ_NEXT_COMP |
  385. IB_CQ_REPORT_MISSED_EVENTS)) &&
  386. napi_reschedule(napi))
  387. goto poll_more;
  388. }
  389. return done;
  390. }
  391. void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
  392. {
  393. struct net_device *dev = dev_ptr;
  394. struct ipoib_dev_priv *priv = netdev_priv(dev);
  395. napi_schedule(&priv->napi);
  396. }
  397. static void drain_tx_cq(struct net_device *dev)
  398. {
  399. struct ipoib_dev_priv *priv = netdev_priv(dev);
  400. netif_tx_lock(dev);
  401. while (poll_tx(priv))
  402. ; /* nothing */
  403. if (netif_queue_stopped(dev))
  404. mod_timer(&priv->poll_timer, jiffies + 1);
  405. netif_tx_unlock(dev);
  406. }
  407. void ipoib_send_comp_handler(struct ib_cq *cq, void *dev_ptr)
  408. {
  409. struct ipoib_dev_priv *priv = netdev_priv(dev_ptr);
  410. mod_timer(&priv->poll_timer, jiffies);
  411. }
  412. static inline int post_send(struct ipoib_dev_priv *priv,
  413. unsigned int wr_id,
  414. struct ib_ah *address, u32 qpn,
  415. struct ipoib_tx_buf *tx_req,
  416. void *head, int hlen)
  417. {
  418. struct ib_send_wr *bad_wr;
  419. struct sk_buff *skb = tx_req->skb;
  420. ipoib_build_sge(priv, tx_req);
  421. priv->tx_wr.wr_id = wr_id;
  422. priv->tx_wr.wr.ud.remote_qpn = qpn;
  423. priv->tx_wr.wr.ud.ah = address;
  424. if (head) {
  425. priv->tx_wr.wr.ud.mss = skb_shinfo(skb)->gso_size;
  426. priv->tx_wr.wr.ud.header = head;
  427. priv->tx_wr.wr.ud.hlen = hlen;
  428. priv->tx_wr.opcode = IB_WR_LSO;
  429. } else
  430. priv->tx_wr.opcode = IB_WR_SEND;
  431. return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
  432. }
  433. void ipoib_send(struct net_device *dev, struct sk_buff *skb,
  434. struct ipoib_ah *address, u32 qpn)
  435. {
  436. struct ipoib_dev_priv *priv = netdev_priv(dev);
  437. struct ipoib_tx_buf *tx_req;
  438. int hlen, rc;
  439. void *phead;
  440. if (skb_is_gso(skb)) {
  441. hlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
  442. phead = skb->data;
  443. if (unlikely(!skb_pull(skb, hlen))) {
  444. ipoib_warn(priv, "linear data too small\n");
  445. ++dev->stats.tx_dropped;
  446. ++dev->stats.tx_errors;
  447. dev_kfree_skb_any(skb);
  448. return;
  449. }
  450. } else {
  451. if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
  452. ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
  453. skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
  454. ++dev->stats.tx_dropped;
  455. ++dev->stats.tx_errors;
  456. ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
  457. return;
  458. }
  459. phead = NULL;
  460. hlen = 0;
  461. }
  462. ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
  463. skb->len, address, qpn);
  464. /*
  465. * We put the skb into the tx_ring _before_ we call post_send()
  466. * because it's entirely possible that the completion handler will
  467. * run before we execute anything after the post_send(). That
  468. * means we have to make sure everything is properly recorded and
  469. * our state is consistent before we call post_send().
  470. */
  471. tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
  472. tx_req->skb = skb;
  473. if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) {
  474. ++dev->stats.tx_errors;
  475. dev_kfree_skb_any(skb);
  476. return;
  477. }
  478. if (skb->ip_summed == CHECKSUM_PARTIAL)
  479. priv->tx_wr.send_flags |= IB_SEND_IP_CSUM;
  480. else
  481. priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
  482. if (++priv->tx_outstanding == ipoib_sendq_size) {
  483. ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
  484. if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP))
  485. ipoib_warn(priv, "request notify on send CQ failed\n");
  486. netif_stop_queue(dev);
  487. }
  488. skb_orphan(skb);
  489. skb_dst_drop(skb);
  490. rc = post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
  491. address->ah, qpn, tx_req, phead, hlen);
  492. if (unlikely(rc)) {
  493. ipoib_warn(priv, "post_send failed, error %d\n", rc);
  494. ++dev->stats.tx_errors;
  495. --priv->tx_outstanding;
  496. ipoib_dma_unmap_tx(priv, tx_req);
  497. dev_kfree_skb_any(skb);
  498. if (netif_queue_stopped(dev))
  499. netif_wake_queue(dev);
  500. } else {
  501. dev->trans_start = jiffies;
  502. address->last_send = priv->tx_head;
  503. ++priv->tx_head;
  504. }
  505. if (unlikely(priv->tx_outstanding > MAX_SEND_CQE))
  506. while (poll_tx(priv))
  507. ; /* nothing */
  508. }
  509. static void __ipoib_reap_ah(struct net_device *dev)
  510. {
  511. struct ipoib_dev_priv *priv = netdev_priv(dev);
  512. struct ipoib_ah *ah, *tah;
  513. LIST_HEAD(remove_list);
  514. unsigned long flags;
  515. netif_tx_lock_bh(dev);
  516. spin_lock_irqsave(&priv->lock, flags);
  517. list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
  518. if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
  519. list_del(&ah->list);
  520. ib_destroy_ah(ah->ah);
  521. kfree(ah);
  522. }
  523. spin_unlock_irqrestore(&priv->lock, flags);
  524. netif_tx_unlock_bh(dev);
  525. }
  526. void ipoib_reap_ah(struct work_struct *work)
  527. {
  528. struct ipoib_dev_priv *priv =
  529. container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
  530. struct net_device *dev = priv->dev;
  531. __ipoib_reap_ah(dev);
  532. if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
  533. queue_delayed_work(priv->wq, &priv->ah_reap_task,
  534. round_jiffies_relative(HZ));
  535. }
  536. static void ipoib_flush_ah(struct net_device *dev)
  537. {
  538. struct ipoib_dev_priv *priv = netdev_priv(dev);
  539. cancel_delayed_work(&priv->ah_reap_task);
  540. flush_workqueue(priv->wq);
  541. ipoib_reap_ah(&priv->ah_reap_task.work);
  542. }
  543. static void ipoib_stop_ah(struct net_device *dev)
  544. {
  545. struct ipoib_dev_priv *priv = netdev_priv(dev);
  546. set_bit(IPOIB_STOP_REAPER, &priv->flags);
  547. ipoib_flush_ah(dev);
  548. }
  549. static void ipoib_ib_tx_timer_func(unsigned long ctx)
  550. {
  551. drain_tx_cq((struct net_device *)ctx);
  552. }
  553. int ipoib_ib_dev_open(struct net_device *dev)
  554. {
  555. struct ipoib_dev_priv *priv = netdev_priv(dev);
  556. int ret;
  557. ipoib_pkey_dev_check_presence(dev);
  558. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  559. ipoib_warn(priv, "P_Key 0x%04x is %s\n", priv->pkey,
  560. (!(priv->pkey & 0x7fff) ? "Invalid" : "not found"));
  561. return -1;
  562. }
  563. ret = ipoib_init_qp(dev);
  564. if (ret) {
  565. ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
  566. return -1;
  567. }
  568. ret = ipoib_ib_post_receives(dev);
  569. if (ret) {
  570. ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
  571. goto dev_stop;
  572. }
  573. ret = ipoib_cm_dev_open(dev);
  574. if (ret) {
  575. ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
  576. goto dev_stop;
  577. }
  578. clear_bit(IPOIB_STOP_REAPER, &priv->flags);
  579. queue_delayed_work(priv->wq, &priv->ah_reap_task,
  580. round_jiffies_relative(HZ));
  581. if (!test_and_set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
  582. napi_enable(&priv->napi);
  583. return 0;
  584. dev_stop:
  585. if (!test_and_set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
  586. napi_enable(&priv->napi);
  587. ipoib_ib_dev_stop(dev);
  588. return -1;
  589. }
  590. void ipoib_pkey_dev_check_presence(struct net_device *dev)
  591. {
  592. struct ipoib_dev_priv *priv = netdev_priv(dev);
  593. if (!(priv->pkey & 0x7fff) ||
  594. ib_find_pkey(priv->ca, priv->port, priv->pkey,
  595. &priv->pkey_index))
  596. clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  597. else
  598. set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  599. }
  600. int ipoib_ib_dev_up(struct net_device *dev)
  601. {
  602. struct ipoib_dev_priv *priv = netdev_priv(dev);
  603. ipoib_pkey_dev_check_presence(dev);
  604. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  605. ipoib_dbg(priv, "PKEY is not assigned.\n");
  606. return 0;
  607. }
  608. set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
  609. return ipoib_mcast_start_thread(dev);
  610. }
  611. int ipoib_ib_dev_down(struct net_device *dev)
  612. {
  613. struct ipoib_dev_priv *priv = netdev_priv(dev);
  614. ipoib_dbg(priv, "downing ib_dev\n");
  615. clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
  616. netif_carrier_off(dev);
  617. ipoib_mcast_stop_thread(dev);
  618. ipoib_mcast_dev_flush(dev);
  619. ipoib_flush_paths(dev);
  620. return 0;
  621. }
  622. static int recvs_pending(struct net_device *dev)
  623. {
  624. struct ipoib_dev_priv *priv = netdev_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. void ipoib_drain_cq(struct net_device *dev)
  633. {
  634. struct ipoib_dev_priv *priv = netdev_priv(dev);
  635. int i, n;
  636. /*
  637. * We call completion handling routines that expect to be
  638. * called from the BH-disabled NAPI poll context, so disable
  639. * BHs here too.
  640. */
  641. local_bh_disable();
  642. do {
  643. n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);
  644. for (i = 0; i < n; ++i) {
  645. /*
  646. * Convert any successful completions to flush
  647. * errors to avoid passing packets up the
  648. * stack after bringing the device down.
  649. */
  650. if (priv->ibwc[i].status == IB_WC_SUCCESS)
  651. priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
  652. if (priv->ibwc[i].wr_id & IPOIB_OP_RECV) {
  653. if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
  654. ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
  655. else
  656. ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
  657. } else
  658. ipoib_cm_handle_tx_wc(dev, priv->ibwc + i);
  659. }
  660. } while (n == IPOIB_NUM_WC);
  661. while (poll_tx(priv))
  662. ; /* nothing */
  663. local_bh_enable();
  664. }
  665. int ipoib_ib_dev_stop(struct net_device *dev)
  666. {
  667. struct ipoib_dev_priv *priv = netdev_priv(dev);
  668. struct ib_qp_attr qp_attr;
  669. unsigned long begin;
  670. struct ipoib_tx_buf *tx_req;
  671. int i;
  672. if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
  673. napi_disable(&priv->napi);
  674. ipoib_cm_dev_stop(dev);
  675. /*
  676. * Move our QP to the error state and then reinitialize in
  677. * when all work requests have completed or have been flushed.
  678. */
  679. qp_attr.qp_state = IB_QPS_ERR;
  680. if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
  681. ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
  682. /* Wait for all sends and receives to complete */
  683. begin = jiffies;
  684. while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
  685. if (time_after(jiffies, begin + 5 * HZ)) {
  686. ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
  687. priv->tx_head - priv->tx_tail, 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->tx_outstanding;
  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. msleep(1);
  714. }
  715. ipoib_dbg(priv, "All sends and receives done.\n");
  716. timeout:
  717. del_timer_sync(&priv->poll_timer);
  718. qp_attr.qp_state = IB_QPS_RESET;
  719. if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
  720. ipoib_warn(priv, "Failed to modify QP to RESET state\n");
  721. ipoib_flush_ah(dev);
  722. ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP);
  723. return 0;
  724. }
  725. int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
  726. {
  727. struct ipoib_dev_priv *priv = netdev_priv(dev);
  728. priv->ca = ca;
  729. priv->port = port;
  730. priv->qp = NULL;
  731. if (ipoib_transport_dev_init(dev, ca)) {
  732. printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
  733. return -ENODEV;
  734. }
  735. setup_timer(&priv->poll_timer, ipoib_ib_tx_timer_func,
  736. (unsigned long) dev);
  737. if (dev->flags & IFF_UP) {
  738. if (ipoib_ib_dev_open(dev)) {
  739. ipoib_transport_dev_cleanup(dev);
  740. return -ENODEV;
  741. }
  742. }
  743. return 0;
  744. }
  745. /*
  746. * Takes whatever value which is in pkey index 0 and updates priv->pkey
  747. * returns 0 if the pkey value was changed.
  748. */
  749. static inline int update_parent_pkey(struct ipoib_dev_priv *priv)
  750. {
  751. int result;
  752. u16 prev_pkey;
  753. prev_pkey = priv->pkey;
  754. result = ib_query_pkey(priv->ca, priv->port, 0, &priv->pkey);
  755. if (result) {
  756. ipoib_warn(priv, "ib_query_pkey port %d failed (ret = %d)\n",
  757. priv->port, result);
  758. return result;
  759. }
  760. priv->pkey |= 0x8000;
  761. if (prev_pkey != priv->pkey) {
  762. ipoib_dbg(priv, "pkey changed from 0x%x to 0x%x\n",
  763. prev_pkey, priv->pkey);
  764. /*
  765. * Update the pkey in the broadcast address, while making sure to set
  766. * the full membership bit, so that we join the right broadcast group.
  767. */
  768. priv->dev->broadcast[8] = priv->pkey >> 8;
  769. priv->dev->broadcast[9] = priv->pkey & 0xff;
  770. return 0;
  771. }
  772. return 1;
  773. }
  774. /*
  775. * returns 0 if pkey value was found in a different slot.
  776. */
  777. static inline int update_child_pkey(struct ipoib_dev_priv *priv)
  778. {
  779. u16 old_index = priv->pkey_index;
  780. priv->pkey_index = 0;
  781. ipoib_pkey_dev_check_presence(priv->dev);
  782. if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) &&
  783. (old_index == priv->pkey_index))
  784. return 1;
  785. return 0;
  786. }
  787. static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv,
  788. enum ipoib_flush_level level,
  789. int nesting)
  790. {
  791. struct ipoib_dev_priv *cpriv;
  792. struct net_device *dev = priv->dev;
  793. int result;
  794. down_read_nested(&priv->vlan_rwsem, nesting);
  795. /*
  796. * Flush any child interfaces too -- they might be up even if
  797. * the parent is down.
  798. */
  799. list_for_each_entry(cpriv, &priv->child_intfs, list)
  800. __ipoib_ib_dev_flush(cpriv, level, nesting + 1);
  801. up_read(&priv->vlan_rwsem);
  802. if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags) &&
  803. level != IPOIB_FLUSH_HEAVY) {
  804. ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
  805. return;
  806. }
  807. if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
  808. /* interface is down. update pkey and leave. */
  809. if (level == IPOIB_FLUSH_HEAVY) {
  810. if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
  811. update_parent_pkey(priv);
  812. else
  813. update_child_pkey(priv);
  814. }
  815. ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
  816. return;
  817. }
  818. if (level == IPOIB_FLUSH_HEAVY) {
  819. /* child devices chase their origin pkey value, while non-child
  820. * (parent) devices should always takes what present in pkey index 0
  821. */
  822. if (test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
  823. result = update_child_pkey(priv);
  824. if (result) {
  825. /* restart QP only if P_Key index is changed */
  826. ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
  827. return;
  828. }
  829. } else {
  830. result = update_parent_pkey(priv);
  831. /* restart QP only if P_Key value changed */
  832. if (result) {
  833. ipoib_dbg(priv, "Not flushing - P_Key value not changed.\n");
  834. return;
  835. }
  836. }
  837. }
  838. if (level == IPOIB_FLUSH_LIGHT) {
  839. ipoib_mark_paths_invalid(dev);
  840. ipoib_mcast_dev_flush(dev);
  841. ipoib_flush_ah(dev);
  842. }
  843. if (level >= IPOIB_FLUSH_NORMAL)
  844. ipoib_ib_dev_down(dev);
  845. if (level == IPOIB_FLUSH_HEAVY) {
  846. if (test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
  847. ipoib_ib_dev_stop(dev);
  848. if (ipoib_ib_dev_open(dev) != 0)
  849. return;
  850. if (netif_queue_stopped(dev))
  851. netif_start_queue(dev);
  852. }
  853. /*
  854. * The device could have been brought down between the start and when
  855. * we get here, don't bring it back up if it's not configured up
  856. */
  857. if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
  858. if (level >= IPOIB_FLUSH_NORMAL)
  859. ipoib_ib_dev_up(dev);
  860. ipoib_mcast_restart_task(&priv->restart_task);
  861. }
  862. }
  863. void ipoib_ib_dev_flush_light(struct work_struct *work)
  864. {
  865. struct ipoib_dev_priv *priv =
  866. container_of(work, struct ipoib_dev_priv, flush_light);
  867. __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_LIGHT, 0);
  868. }
  869. void ipoib_ib_dev_flush_normal(struct work_struct *work)
  870. {
  871. struct ipoib_dev_priv *priv =
  872. container_of(work, struct ipoib_dev_priv, flush_normal);
  873. __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_NORMAL, 0);
  874. }
  875. void ipoib_ib_dev_flush_heavy(struct work_struct *work)
  876. {
  877. struct ipoib_dev_priv *priv =
  878. container_of(work, struct ipoib_dev_priv, flush_heavy);
  879. __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_HEAVY, 0);
  880. }
  881. void ipoib_ib_dev_cleanup(struct net_device *dev)
  882. {
  883. struct ipoib_dev_priv *priv = netdev_priv(dev);
  884. ipoib_dbg(priv, "cleaning up ib_dev\n");
  885. /*
  886. * We must make sure there are no more (path) completions
  887. * that may wish to touch priv fields that are no longer valid
  888. */
  889. ipoib_flush_paths(dev);
  890. ipoib_mcast_stop_thread(dev);
  891. ipoib_mcast_dev_flush(dev);
  892. /*
  893. * All of our ah references aren't free until after
  894. * ipoib_mcast_dev_flush(), ipoib_flush_paths, and
  895. * the neighbor garbage collection is stopped and reaped.
  896. * That should all be done now, so make a final ah flush.
  897. */
  898. ipoib_stop_ah(dev);
  899. ipoib_transport_dev_cleanup(dev);
  900. }