ipoib_ib.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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/dma-mapping.h>
  37. #include <rdma/ib_cache.h>
  38. #include <linux/ip.h>
  39. #include <linux/tcp.h>
  40. #include "ipoib.h"
  41. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
  42. static int data_debug_level;
  43. module_param(data_debug_level, int, 0644);
  44. MODULE_PARM_DESC(data_debug_level,
  45. "Enable data path debug tracing if > 0");
  46. #endif
  47. static DEFINE_MUTEX(pkey_mutex);
  48. struct ipoib_ah *ipoib_create_ah(struct net_device *dev,
  49. struct ib_pd *pd, struct ib_ah_attr *attr)
  50. {
  51. struct ipoib_ah *ah;
  52. ah = kmalloc(sizeof *ah, GFP_KERNEL);
  53. if (!ah)
  54. return NULL;
  55. ah->dev = dev;
  56. ah->last_send = 0;
  57. kref_init(&ah->ref);
  58. ah->ah = ib_create_ah(pd, attr);
  59. if (IS_ERR(ah->ah)) {
  60. kfree(ah);
  61. ah = NULL;
  62. } else
  63. ipoib_dbg(netdev_priv(dev), "Created ah %p\n", ah->ah);
  64. return ah;
  65. }
  66. void ipoib_free_ah(struct kref *kref)
  67. {
  68. struct ipoib_ah *ah = container_of(kref, struct ipoib_ah, ref);
  69. struct ipoib_dev_priv *priv = netdev_priv(ah->dev);
  70. unsigned long flags;
  71. spin_lock_irqsave(&priv->lock, flags);
  72. list_add_tail(&ah->list, &priv->dead_ahs);
  73. spin_unlock_irqrestore(&priv->lock, flags);
  74. }
  75. static void ipoib_ud_dma_unmap_rx(struct ipoib_dev_priv *priv,
  76. u64 mapping[IPOIB_UD_RX_SG])
  77. {
  78. if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
  79. ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_UD_HEAD_SIZE,
  80. DMA_FROM_DEVICE);
  81. ib_dma_unmap_page(priv->ca, mapping[1], PAGE_SIZE,
  82. DMA_FROM_DEVICE);
  83. } else
  84. ib_dma_unmap_single(priv->ca, mapping[0],
  85. IPOIB_UD_BUF_SIZE(priv->max_ib_mtu),
  86. DMA_FROM_DEVICE);
  87. }
  88. static void ipoib_ud_skb_put_frags(struct ipoib_dev_priv *priv,
  89. struct sk_buff *skb,
  90. unsigned int length)
  91. {
  92. if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
  93. skb_frag_t *frag = &skb_shinfo(skb)->frags[0];
  94. unsigned int size;
  95. /*
  96. * There is only two buffers needed for max_payload = 4K,
  97. * first buf size is IPOIB_UD_HEAD_SIZE
  98. */
  99. skb->tail += IPOIB_UD_HEAD_SIZE;
  100. skb->len += length;
  101. size = length - IPOIB_UD_HEAD_SIZE;
  102. frag->size = size;
  103. skb->data_len += size;
  104. skb->truesize += size;
  105. } else
  106. skb_put(skb, length);
  107. }
  108. static int ipoib_ib_post_receive(struct net_device *dev, int id)
  109. {
  110. struct ipoib_dev_priv *priv = netdev_priv(dev);
  111. struct ib_recv_wr *bad_wr;
  112. int ret;
  113. priv->rx_wr.wr_id = id | IPOIB_OP_RECV;
  114. priv->rx_sge[0].addr = priv->rx_ring[id].mapping[0];
  115. priv->rx_sge[1].addr = priv->rx_ring[id].mapping[1];
  116. ret = ib_post_recv(priv->qp, &priv->rx_wr, &bad_wr);
  117. if (unlikely(ret)) {
  118. ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);
  119. ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[id].mapping);
  120. dev_kfree_skb_any(priv->rx_ring[id].skb);
  121. priv->rx_ring[id].skb = NULL;
  122. }
  123. return ret;
  124. }
  125. static struct sk_buff *ipoib_alloc_rx_skb(struct net_device *dev, int id)
  126. {
  127. struct ipoib_dev_priv *priv = netdev_priv(dev);
  128. struct sk_buff *skb;
  129. int buf_size;
  130. u64 *mapping;
  131. if (ipoib_ud_need_sg(priv->max_ib_mtu))
  132. buf_size = IPOIB_UD_HEAD_SIZE;
  133. else
  134. buf_size = IPOIB_UD_BUF_SIZE(priv->max_ib_mtu);
  135. skb = dev_alloc_skb(buf_size + 4);
  136. if (unlikely(!skb))
  137. return NULL;
  138. /*
  139. * IB will leave a 40 byte gap for a GRH and IPoIB adds a 4 byte
  140. * header. So we need 4 more bytes to get to 48 and align the
  141. * IP header to a multiple of 16.
  142. */
  143. skb_reserve(skb, 4);
  144. mapping = priv->rx_ring[id].mapping;
  145. mapping[0] = ib_dma_map_single(priv->ca, skb->data, buf_size,
  146. DMA_FROM_DEVICE);
  147. if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0])))
  148. goto error;
  149. if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
  150. struct page *page = alloc_page(GFP_ATOMIC);
  151. if (!page)
  152. goto partial_error;
  153. skb_fill_page_desc(skb, 0, page, 0, PAGE_SIZE);
  154. mapping[1] =
  155. ib_dma_map_page(priv->ca, skb_shinfo(skb)->frags[0].page,
  156. 0, PAGE_SIZE, DMA_FROM_DEVICE);
  157. if (unlikely(ib_dma_mapping_error(priv->ca, mapping[1])))
  158. goto partial_error;
  159. }
  160. priv->rx_ring[id].skb = skb;
  161. return skb;
  162. partial_error:
  163. ib_dma_unmap_single(priv->ca, mapping[0], buf_size, DMA_FROM_DEVICE);
  164. error:
  165. dev_kfree_skb_any(skb);
  166. return NULL;
  167. }
  168. static int ipoib_ib_post_receives(struct net_device *dev)
  169. {
  170. struct ipoib_dev_priv *priv = netdev_priv(dev);
  171. int i;
  172. for (i = 0; i < ipoib_recvq_size; ++i) {
  173. if (!ipoib_alloc_rx_skb(dev, i)) {
  174. ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
  175. return -ENOMEM;
  176. }
  177. if (ipoib_ib_post_receive(dev, i)) {
  178. ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
  179. return -EIO;
  180. }
  181. }
  182. return 0;
  183. }
  184. static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
  185. {
  186. struct ipoib_dev_priv *priv = netdev_priv(dev);
  187. unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
  188. struct sk_buff *skb;
  189. u64 mapping[IPOIB_UD_RX_SG];
  190. ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
  191. wr_id, wc->status);
  192. if (unlikely(wr_id >= ipoib_recvq_size)) {
  193. ipoib_warn(priv, "recv completion event with wrid %d (> %d)\n",
  194. wr_id, ipoib_recvq_size);
  195. return;
  196. }
  197. skb = priv->rx_ring[wr_id].skb;
  198. if (unlikely(wc->status != IB_WC_SUCCESS)) {
  199. if (wc->status != IB_WC_WR_FLUSH_ERR)
  200. ipoib_warn(priv, "failed recv event "
  201. "(status=%d, wrid=%d vend_err %x)\n",
  202. wc->status, wr_id, wc->vendor_err);
  203. ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[wr_id].mapping);
  204. dev_kfree_skb_any(skb);
  205. priv->rx_ring[wr_id].skb = NULL;
  206. return;
  207. }
  208. /*
  209. * Drop packets that this interface sent, ie multicast packets
  210. * that the HCA has replicated.
  211. */
  212. if (wc->slid == priv->local_lid && wc->src_qp == priv->qp->qp_num)
  213. goto repost;
  214. memcpy(mapping, priv->rx_ring[wr_id].mapping,
  215. IPOIB_UD_RX_SG * sizeof *mapping);
  216. /*
  217. * If we can't allocate a new RX buffer, dump
  218. * this packet and reuse the old buffer.
  219. */
  220. if (unlikely(!ipoib_alloc_rx_skb(dev, wr_id))) {
  221. ++dev->stats.rx_dropped;
  222. goto repost;
  223. }
  224. ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
  225. wc->byte_len, wc->slid);
  226. ipoib_ud_dma_unmap_rx(priv, mapping);
  227. ipoib_ud_skb_put_frags(priv, skb, wc->byte_len);
  228. skb_pull(skb, IB_GRH_BYTES);
  229. skb->protocol = ((struct ipoib_header *) skb->data)->proto;
  230. skb_reset_mac_header(skb);
  231. skb_pull(skb, IPOIB_ENCAP_LEN);
  232. dev->last_rx = jiffies;
  233. ++dev->stats.rx_packets;
  234. dev->stats.rx_bytes += skb->len;
  235. skb->dev = dev;
  236. /* XXX get correct PACKET_ type here */
  237. skb->pkt_type = PACKET_HOST;
  238. if (test_bit(IPOIB_FLAG_CSUM, &priv->flags) && likely(wc->csum_ok))
  239. skb->ip_summed = CHECKSUM_UNNECESSARY;
  240. if (dev->features & NETIF_F_LRO)
  241. lro_receive_skb(&priv->lro.lro_mgr, skb, NULL);
  242. else
  243. netif_receive_skb(skb);
  244. repost:
  245. if (unlikely(ipoib_ib_post_receive(dev, wr_id)))
  246. ipoib_warn(priv, "ipoib_ib_post_receive failed "
  247. "for buf %d\n", wr_id);
  248. }
  249. static int ipoib_dma_map_tx(struct ib_device *ca,
  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. mapping[0] = ib_dma_map_single(ca, skb->data, skb_headlen(skb),
  258. DMA_TO_DEVICE);
  259. if (unlikely(ib_dma_mapping_error(ca, mapping[0])))
  260. return -EIO;
  261. off = 1;
  262. } else
  263. off = 0;
  264. for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
  265. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  266. mapping[i + off] = ib_dma_map_page(ca, frag->page,
  267. frag->page_offset, frag->size,
  268. DMA_TO_DEVICE);
  269. if (unlikely(ib_dma_mapping_error(ca, mapping[i + off])))
  270. goto partial_error;
  271. }
  272. return 0;
  273. partial_error:
  274. for (; i > 0; --i) {
  275. skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
  276. ib_dma_unmap_page(ca, mapping[i - !off], frag->size, DMA_TO_DEVICE);
  277. }
  278. if (off)
  279. ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
  280. return -EIO;
  281. }
  282. static void ipoib_dma_unmap_tx(struct ib_device *ca,
  283. struct ipoib_tx_buf *tx_req)
  284. {
  285. struct sk_buff *skb = tx_req->skb;
  286. u64 *mapping = tx_req->mapping;
  287. int i;
  288. int off;
  289. if (skb_headlen(skb)) {
  290. ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
  291. off = 1;
  292. } else
  293. off = 0;
  294. for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
  295. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  296. ib_dma_unmap_page(ca, mapping[i + off], frag->size,
  297. DMA_TO_DEVICE);
  298. }
  299. }
  300. static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
  301. {
  302. struct ipoib_dev_priv *priv = netdev_priv(dev);
  303. unsigned int wr_id = wc->wr_id;
  304. struct ipoib_tx_buf *tx_req;
  305. ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
  306. wr_id, wc->status);
  307. if (unlikely(wr_id >= ipoib_sendq_size)) {
  308. ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
  309. wr_id, ipoib_sendq_size);
  310. return;
  311. }
  312. tx_req = &priv->tx_ring[wr_id];
  313. ipoib_dma_unmap_tx(priv->ca, tx_req);
  314. ++dev->stats.tx_packets;
  315. dev->stats.tx_bytes += tx_req->skb->len;
  316. dev_kfree_skb_any(tx_req->skb);
  317. ++priv->tx_tail;
  318. if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
  319. netif_queue_stopped(dev) &&
  320. test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
  321. netif_wake_queue(dev);
  322. if (wc->status != IB_WC_SUCCESS &&
  323. wc->status != IB_WC_WR_FLUSH_ERR)
  324. ipoib_warn(priv, "failed send event "
  325. "(status=%d, wrid=%d vend_err %x)\n",
  326. wc->status, wr_id, wc->vendor_err);
  327. }
  328. static int poll_tx(struct ipoib_dev_priv *priv)
  329. {
  330. int n, i;
  331. n = ib_poll_cq(priv->send_cq, MAX_SEND_CQE, priv->send_wc);
  332. for (i = 0; i < n; ++i)
  333. ipoib_ib_handle_tx_wc(priv->dev, priv->send_wc + i);
  334. return n == MAX_SEND_CQE;
  335. }
  336. int ipoib_poll(struct napi_struct *napi, int budget)
  337. {
  338. struct ipoib_dev_priv *priv = container_of(napi, struct ipoib_dev_priv, napi);
  339. struct net_device *dev = priv->dev;
  340. int done;
  341. int t;
  342. int n, i;
  343. done = 0;
  344. poll_more:
  345. while (done < budget) {
  346. int max = (budget - done);
  347. t = min(IPOIB_NUM_WC, max);
  348. n = ib_poll_cq(priv->recv_cq, t, priv->ibwc);
  349. for (i = 0; i < n; i++) {
  350. struct ib_wc *wc = priv->ibwc + i;
  351. if (wc->wr_id & IPOIB_OP_RECV) {
  352. ++done;
  353. if (wc->wr_id & IPOIB_OP_CM)
  354. ipoib_cm_handle_rx_wc(dev, wc);
  355. else
  356. ipoib_ib_handle_rx_wc(dev, wc);
  357. } else
  358. ipoib_cm_handle_tx_wc(priv->dev, wc);
  359. }
  360. if (n != t)
  361. break;
  362. }
  363. if (done < budget) {
  364. if (dev->features & NETIF_F_LRO)
  365. lro_flush_all(&priv->lro.lro_mgr);
  366. napi_complete(napi);
  367. if (unlikely(ib_req_notify_cq(priv->recv_cq,
  368. IB_CQ_NEXT_COMP |
  369. IB_CQ_REPORT_MISSED_EVENTS)) &&
  370. napi_reschedule(napi))
  371. goto poll_more;
  372. }
  373. return done;
  374. }
  375. void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
  376. {
  377. struct net_device *dev = dev_ptr;
  378. struct ipoib_dev_priv *priv = netdev_priv(dev);
  379. napi_schedule(&priv->napi);
  380. }
  381. static void drain_tx_cq(struct net_device *dev)
  382. {
  383. struct ipoib_dev_priv *priv = netdev_priv(dev);
  384. netif_tx_lock(dev);
  385. while (poll_tx(priv))
  386. ; /* nothing */
  387. if (netif_queue_stopped(dev))
  388. mod_timer(&priv->poll_timer, jiffies + 1);
  389. netif_tx_unlock(dev);
  390. }
  391. void ipoib_send_comp_handler(struct ib_cq *cq, void *dev_ptr)
  392. {
  393. struct ipoib_dev_priv *priv = netdev_priv(dev_ptr);
  394. mod_timer(&priv->poll_timer, jiffies);
  395. }
  396. static inline int post_send(struct ipoib_dev_priv *priv,
  397. unsigned int wr_id,
  398. struct ib_ah *address, u32 qpn,
  399. struct ipoib_tx_buf *tx_req,
  400. void *head, int hlen)
  401. {
  402. struct ib_send_wr *bad_wr;
  403. int i, off;
  404. struct sk_buff *skb = tx_req->skb;
  405. skb_frag_t *frags = skb_shinfo(skb)->frags;
  406. int nr_frags = skb_shinfo(skb)->nr_frags;
  407. u64 *mapping = tx_req->mapping;
  408. if (skb_headlen(skb)) {
  409. priv->tx_sge[0].addr = mapping[0];
  410. priv->tx_sge[0].length = skb_headlen(skb);
  411. off = 1;
  412. } else
  413. off = 0;
  414. for (i = 0; i < nr_frags; ++i) {
  415. priv->tx_sge[i + off].addr = mapping[i + off];
  416. priv->tx_sge[i + off].length = frags[i].size;
  417. }
  418. priv->tx_wr.num_sge = nr_frags + off;
  419. priv->tx_wr.wr_id = wr_id;
  420. priv->tx_wr.wr.ud.remote_qpn = qpn;
  421. priv->tx_wr.wr.ud.ah = address;
  422. if (head) {
  423. priv->tx_wr.wr.ud.mss = skb_shinfo(skb)->gso_size;
  424. priv->tx_wr.wr.ud.header = head;
  425. priv->tx_wr.wr.ud.hlen = hlen;
  426. priv->tx_wr.opcode = IB_WR_LSO;
  427. } else
  428. priv->tx_wr.opcode = IB_WR_SEND;
  429. return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
  430. }
  431. void ipoib_send(struct net_device *dev, struct sk_buff *skb,
  432. struct ipoib_ah *address, u32 qpn)
  433. {
  434. struct ipoib_dev_priv *priv = netdev_priv(dev);
  435. struct ipoib_tx_buf *tx_req;
  436. int hlen;
  437. void *phead;
  438. if (skb_is_gso(skb)) {
  439. hlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
  440. phead = skb->data;
  441. if (unlikely(!skb_pull(skb, hlen))) {
  442. ipoib_warn(priv, "linear data too small\n");
  443. ++dev->stats.tx_dropped;
  444. ++dev->stats.tx_errors;
  445. dev_kfree_skb_any(skb);
  446. return;
  447. }
  448. } else {
  449. if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
  450. ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
  451. skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
  452. ++dev->stats.tx_dropped;
  453. ++dev->stats.tx_errors;
  454. ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
  455. return;
  456. }
  457. phead = NULL;
  458. hlen = 0;
  459. }
  460. ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
  461. skb->len, address, qpn);
  462. /*
  463. * We put the skb into the tx_ring _before_ we call post_send()
  464. * because it's entirely possible that the completion handler will
  465. * run before we execute anything after the post_send(). That
  466. * means we have to make sure everything is properly recorded and
  467. * our state is consistent before we call post_send().
  468. */
  469. tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
  470. tx_req->skb = skb;
  471. if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) {
  472. ++dev->stats.tx_errors;
  473. dev_kfree_skb_any(skb);
  474. return;
  475. }
  476. if (skb->ip_summed == CHECKSUM_PARTIAL)
  477. priv->tx_wr.send_flags |= IB_SEND_IP_CSUM;
  478. else
  479. priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
  480. if (++priv->tx_outstanding == ipoib_sendq_size) {
  481. ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
  482. if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP))
  483. ipoib_warn(priv, "request notify on send CQ failed\n");
  484. netif_stop_queue(dev);
  485. }
  486. if (unlikely(post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
  487. address->ah, qpn, tx_req, phead, hlen))) {
  488. ipoib_warn(priv, "post_send failed\n");
  489. ++dev->stats.tx_errors;
  490. --priv->tx_outstanding;
  491. ipoib_dma_unmap_tx(priv->ca, tx_req);
  492. dev_kfree_skb_any(skb);
  493. if (netif_queue_stopped(dev))
  494. netif_wake_queue(dev);
  495. } else {
  496. dev->trans_start = jiffies;
  497. address->last_send = priv->tx_head;
  498. ++priv->tx_head;
  499. skb_orphan(skb);
  500. }
  501. if (unlikely(priv->tx_outstanding > MAX_SEND_CQE))
  502. while (poll_tx(priv))
  503. ; /* nothing */
  504. }
  505. static void __ipoib_reap_ah(struct net_device *dev)
  506. {
  507. struct ipoib_dev_priv *priv = netdev_priv(dev);
  508. struct ipoib_ah *ah, *tah;
  509. LIST_HEAD(remove_list);
  510. unsigned long flags;
  511. netif_tx_lock_bh(dev);
  512. spin_lock_irqsave(&priv->lock, flags);
  513. list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
  514. if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
  515. list_del(&ah->list);
  516. ib_destroy_ah(ah->ah);
  517. kfree(ah);
  518. }
  519. spin_unlock_irqrestore(&priv->lock, flags);
  520. netif_tx_unlock_bh(dev);
  521. }
  522. void ipoib_reap_ah(struct work_struct *work)
  523. {
  524. struct ipoib_dev_priv *priv =
  525. container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
  526. struct net_device *dev = priv->dev;
  527. __ipoib_reap_ah(dev);
  528. if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
  529. queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
  530. round_jiffies_relative(HZ));
  531. }
  532. static void ipoib_ib_tx_timer_func(unsigned long ctx)
  533. {
  534. drain_tx_cq((struct net_device *)ctx);
  535. }
  536. int ipoib_ib_dev_open(struct net_device *dev)
  537. {
  538. struct ipoib_dev_priv *priv = netdev_priv(dev);
  539. int ret;
  540. if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {
  541. ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);
  542. clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  543. return -1;
  544. }
  545. set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  546. ret = ipoib_init_qp(dev);
  547. if (ret) {
  548. ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
  549. return -1;
  550. }
  551. ret = ipoib_ib_post_receives(dev);
  552. if (ret) {
  553. ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
  554. ipoib_ib_dev_stop(dev, 1);
  555. return -1;
  556. }
  557. ret = ipoib_cm_dev_open(dev);
  558. if (ret) {
  559. ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
  560. ipoib_ib_dev_stop(dev, 1);
  561. return -1;
  562. }
  563. clear_bit(IPOIB_STOP_REAPER, &priv->flags);
  564. queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
  565. round_jiffies_relative(HZ));
  566. if (!test_and_set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
  567. napi_enable(&priv->napi);
  568. return 0;
  569. }
  570. static void ipoib_pkey_dev_check_presence(struct net_device *dev)
  571. {
  572. struct ipoib_dev_priv *priv = netdev_priv(dev);
  573. u16 pkey_index = 0;
  574. if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
  575. clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  576. else
  577. set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  578. }
  579. int ipoib_ib_dev_up(struct net_device *dev)
  580. {
  581. struct ipoib_dev_priv *priv = netdev_priv(dev);
  582. ipoib_pkey_dev_check_presence(dev);
  583. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  584. ipoib_dbg(priv, "PKEY is not assigned.\n");
  585. return 0;
  586. }
  587. set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
  588. return ipoib_mcast_start_thread(dev);
  589. }
  590. int ipoib_ib_dev_down(struct net_device *dev, int flush)
  591. {
  592. struct ipoib_dev_priv *priv = netdev_priv(dev);
  593. ipoib_dbg(priv, "downing ib_dev\n");
  594. clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
  595. netif_carrier_off(dev);
  596. /* Shutdown the P_Key thread if still active */
  597. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  598. mutex_lock(&pkey_mutex);
  599. set_bit(IPOIB_PKEY_STOP, &priv->flags);
  600. cancel_delayed_work(&priv->pkey_poll_task);
  601. mutex_unlock(&pkey_mutex);
  602. if (flush)
  603. flush_workqueue(ipoib_workqueue);
  604. }
  605. ipoib_mcast_stop_thread(dev, flush);
  606. ipoib_mcast_dev_flush(dev);
  607. ipoib_flush_paths(dev);
  608. return 0;
  609. }
  610. static int recvs_pending(struct net_device *dev)
  611. {
  612. struct ipoib_dev_priv *priv = netdev_priv(dev);
  613. int pending = 0;
  614. int i;
  615. for (i = 0; i < ipoib_recvq_size; ++i)
  616. if (priv->rx_ring[i].skb)
  617. ++pending;
  618. return pending;
  619. }
  620. void ipoib_drain_cq(struct net_device *dev)
  621. {
  622. struct ipoib_dev_priv *priv = netdev_priv(dev);
  623. int i, n;
  624. /*
  625. * We call completion handling routines that expect to be
  626. * called from the BH-disabled NAPI poll context, so disable
  627. * BHs here too.
  628. */
  629. local_bh_disable();
  630. do {
  631. n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);
  632. for (i = 0; i < n; ++i) {
  633. /*
  634. * Convert any successful completions to flush
  635. * errors to avoid passing packets up the
  636. * stack after bringing the device down.
  637. */
  638. if (priv->ibwc[i].status == IB_WC_SUCCESS)
  639. priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
  640. if (priv->ibwc[i].wr_id & IPOIB_OP_RECV) {
  641. if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
  642. ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
  643. else
  644. ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
  645. } else
  646. ipoib_cm_handle_tx_wc(dev, priv->ibwc + i);
  647. }
  648. } while (n == IPOIB_NUM_WC);
  649. while (poll_tx(priv))
  650. ; /* nothing */
  651. local_bh_enable();
  652. }
  653. int ipoib_ib_dev_stop(struct net_device *dev, int flush)
  654. {
  655. struct ipoib_dev_priv *priv = netdev_priv(dev);
  656. struct ib_qp_attr qp_attr;
  657. unsigned long begin;
  658. struct ipoib_tx_buf *tx_req;
  659. int i;
  660. if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
  661. napi_disable(&priv->napi);
  662. ipoib_cm_dev_stop(dev);
  663. /*
  664. * Move our QP to the error state and then reinitialize in
  665. * when all work requests have completed or have been flushed.
  666. */
  667. qp_attr.qp_state = IB_QPS_ERR;
  668. if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
  669. ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
  670. /* Wait for all sends and receives to complete */
  671. begin = jiffies;
  672. while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
  673. if (time_after(jiffies, begin + 5 * HZ)) {
  674. ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
  675. priv->tx_head - priv->tx_tail, recvs_pending(dev));
  676. /*
  677. * assume the HW is wedged and just free up
  678. * all our pending work requests.
  679. */
  680. while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
  681. tx_req = &priv->tx_ring[priv->tx_tail &
  682. (ipoib_sendq_size - 1)];
  683. ipoib_dma_unmap_tx(priv->ca, tx_req);
  684. dev_kfree_skb_any(tx_req->skb);
  685. ++priv->tx_tail;
  686. --priv->tx_outstanding;
  687. }
  688. for (i = 0; i < ipoib_recvq_size; ++i) {
  689. struct ipoib_rx_buf *rx_req;
  690. rx_req = &priv->rx_ring[i];
  691. if (!rx_req->skb)
  692. continue;
  693. ipoib_ud_dma_unmap_rx(priv,
  694. priv->rx_ring[i].mapping);
  695. dev_kfree_skb_any(rx_req->skb);
  696. rx_req->skb = NULL;
  697. }
  698. goto timeout;
  699. }
  700. ipoib_drain_cq(dev);
  701. msleep(1);
  702. }
  703. ipoib_dbg(priv, "All sends and receives done.\n");
  704. timeout:
  705. del_timer_sync(&priv->poll_timer);
  706. qp_attr.qp_state = IB_QPS_RESET;
  707. if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
  708. ipoib_warn(priv, "Failed to modify QP to RESET state\n");
  709. /* Wait for all AHs to be reaped */
  710. set_bit(IPOIB_STOP_REAPER, &priv->flags);
  711. cancel_delayed_work(&priv->ah_reap_task);
  712. if (flush)
  713. flush_workqueue(ipoib_workqueue);
  714. begin = jiffies;
  715. while (!list_empty(&priv->dead_ahs)) {
  716. __ipoib_reap_ah(dev);
  717. if (time_after(jiffies, begin + HZ)) {
  718. ipoib_warn(priv, "timing out; will leak address handles\n");
  719. break;
  720. }
  721. msleep(1);
  722. }
  723. ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP);
  724. return 0;
  725. }
  726. int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
  727. {
  728. struct ipoib_dev_priv *priv = netdev_priv(dev);
  729. priv->ca = ca;
  730. priv->port = port;
  731. priv->qp = NULL;
  732. if (ipoib_transport_dev_init(dev, ca)) {
  733. printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
  734. return -ENODEV;
  735. }
  736. setup_timer(&priv->poll_timer, ipoib_ib_tx_timer_func,
  737. (unsigned long) dev);
  738. if (dev->flags & IFF_UP) {
  739. if (ipoib_ib_dev_open(dev)) {
  740. ipoib_transport_dev_cleanup(dev);
  741. return -ENODEV;
  742. }
  743. }
  744. return 0;
  745. }
  746. static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv,
  747. enum ipoib_flush_level level)
  748. {
  749. struct ipoib_dev_priv *cpriv;
  750. struct net_device *dev = priv->dev;
  751. u16 new_index;
  752. mutex_lock(&priv->vlan_mutex);
  753. /*
  754. * Flush any child interfaces too -- they might be up even if
  755. * the parent is down.
  756. */
  757. list_for_each_entry(cpriv, &priv->child_intfs, list)
  758. __ipoib_ib_dev_flush(cpriv, level);
  759. mutex_unlock(&priv->vlan_mutex);
  760. if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
  761. ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
  762. return;
  763. }
  764. if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
  765. ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
  766. return;
  767. }
  768. if (level == IPOIB_FLUSH_HEAVY) {
  769. if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
  770. clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  771. ipoib_ib_dev_down(dev, 0);
  772. ipoib_ib_dev_stop(dev, 0);
  773. if (ipoib_pkey_dev_delay_open(dev))
  774. return;
  775. }
  776. /* restart QP only if P_Key index is changed */
  777. if (test_and_set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) &&
  778. new_index == priv->pkey_index) {
  779. ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
  780. return;
  781. }
  782. priv->pkey_index = new_index;
  783. }
  784. if (level == IPOIB_FLUSH_LIGHT) {
  785. ipoib_mark_paths_invalid(dev);
  786. ipoib_mcast_dev_flush(dev);
  787. }
  788. if (level >= IPOIB_FLUSH_NORMAL)
  789. ipoib_ib_dev_down(dev, 0);
  790. if (level == IPOIB_FLUSH_HEAVY) {
  791. ipoib_ib_dev_stop(dev, 0);
  792. ipoib_ib_dev_open(dev);
  793. }
  794. /*
  795. * The device could have been brought down between the start and when
  796. * we get here, don't bring it back up if it's not configured up
  797. */
  798. if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
  799. if (level >= IPOIB_FLUSH_NORMAL)
  800. ipoib_ib_dev_up(dev);
  801. ipoib_mcast_restart_task(&priv->restart_task);
  802. }
  803. }
  804. void ipoib_ib_dev_flush_light(struct work_struct *work)
  805. {
  806. struct ipoib_dev_priv *priv =
  807. container_of(work, struct ipoib_dev_priv, flush_light);
  808. __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_LIGHT);
  809. }
  810. void ipoib_ib_dev_flush_normal(struct work_struct *work)
  811. {
  812. struct ipoib_dev_priv *priv =
  813. container_of(work, struct ipoib_dev_priv, flush_normal);
  814. __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_NORMAL);
  815. }
  816. void ipoib_ib_dev_flush_heavy(struct work_struct *work)
  817. {
  818. struct ipoib_dev_priv *priv =
  819. container_of(work, struct ipoib_dev_priv, flush_heavy);
  820. __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_HEAVY);
  821. }
  822. void ipoib_ib_dev_cleanup(struct net_device *dev)
  823. {
  824. struct ipoib_dev_priv *priv = netdev_priv(dev);
  825. ipoib_dbg(priv, "cleaning up ib_dev\n");
  826. ipoib_mcast_stop_thread(dev, 1);
  827. ipoib_mcast_dev_flush(dev);
  828. ipoib_transport_dev_cleanup(dev);
  829. }
  830. /*
  831. * Delayed P_Key Assigment Interim Support
  832. *
  833. * The following is initial implementation of delayed P_Key assigment
  834. * mechanism. It is using the same approach implemented for the multicast
  835. * group join. The single goal of this implementation is to quickly address
  836. * Bug #2507. This implementation will probably be removed when the P_Key
  837. * change async notification is available.
  838. */
  839. void ipoib_pkey_poll(struct work_struct *work)
  840. {
  841. struct ipoib_dev_priv *priv =
  842. container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
  843. struct net_device *dev = priv->dev;
  844. ipoib_pkey_dev_check_presence(dev);
  845. if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
  846. ipoib_open(dev);
  847. else {
  848. mutex_lock(&pkey_mutex);
  849. if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
  850. queue_delayed_work(ipoib_workqueue,
  851. &priv->pkey_poll_task,
  852. HZ);
  853. mutex_unlock(&pkey_mutex);
  854. }
  855. }
  856. int ipoib_pkey_dev_delay_open(struct net_device *dev)
  857. {
  858. struct ipoib_dev_priv *priv = netdev_priv(dev);
  859. /* Look for the interface pkey value in the IB Port P_Key table and */
  860. /* set the interface pkey assigment flag */
  861. ipoib_pkey_dev_check_presence(dev);
  862. /* P_Key value not assigned yet - start polling */
  863. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  864. mutex_lock(&pkey_mutex);
  865. clear_bit(IPOIB_PKEY_STOP, &priv->flags);
  866. queue_delayed_work(ipoib_workqueue,
  867. &priv->pkey_poll_task,
  868. HZ);
  869. mutex_unlock(&pkey_mutex);
  870. return 1;
  871. }
  872. return 0;
  873. }