ipoib_ib.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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. netif_rx_complete(dev, napi);
  367. if (unlikely(ib_req_notify_cq(priv->recv_cq,
  368. IB_CQ_NEXT_COMP |
  369. IB_CQ_REPORT_MISSED_EVENTS)) &&
  370. netif_rx_reschedule(dev, 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. netif_rx_schedule(dev, &priv->napi);
  380. }
  381. static void drain_tx_cq(struct net_device *dev)
  382. {
  383. struct ipoib_dev_priv *priv = netdev_priv(dev);
  384. unsigned long flags;
  385. spin_lock_irqsave(&priv->tx_lock, flags);
  386. while (poll_tx(priv))
  387. ; /* nothing */
  388. if (netif_queue_stopped(dev))
  389. mod_timer(&priv->poll_timer, jiffies + 1);
  390. spin_unlock_irqrestore(&priv->tx_lock, flags);
  391. }
  392. void ipoib_send_comp_handler(struct ib_cq *cq, void *dev_ptr)
  393. {
  394. drain_tx_cq((struct net_device *)dev_ptr);
  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. spin_lock_irq(&priv->tx_lock);
  511. spin_lock(&priv->lock);
  512. list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
  513. if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
  514. list_del(&ah->list);
  515. ib_destroy_ah(ah->ah);
  516. kfree(ah);
  517. }
  518. spin_unlock(&priv->lock);
  519. spin_unlock_irq(&priv->tx_lock);
  520. }
  521. void ipoib_reap_ah(struct work_struct *work)
  522. {
  523. struct ipoib_dev_priv *priv =
  524. container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
  525. struct net_device *dev = priv->dev;
  526. __ipoib_reap_ah(dev);
  527. if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
  528. queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
  529. round_jiffies_relative(HZ));
  530. }
  531. static void ipoib_ib_tx_timer_func(unsigned long ctx)
  532. {
  533. drain_tx_cq((struct net_device *)ctx);
  534. }
  535. int ipoib_ib_dev_open(struct net_device *dev)
  536. {
  537. struct ipoib_dev_priv *priv = netdev_priv(dev);
  538. int ret;
  539. if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {
  540. ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);
  541. clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  542. return -1;
  543. }
  544. set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  545. ret = ipoib_init_qp(dev);
  546. if (ret) {
  547. ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
  548. return -1;
  549. }
  550. ret = ipoib_ib_post_receives(dev);
  551. if (ret) {
  552. ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
  553. ipoib_ib_dev_stop(dev, 1);
  554. return -1;
  555. }
  556. ret = ipoib_cm_dev_open(dev);
  557. if (ret) {
  558. ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
  559. ipoib_ib_dev_stop(dev, 1);
  560. return -1;
  561. }
  562. clear_bit(IPOIB_STOP_REAPER, &priv->flags);
  563. queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
  564. round_jiffies_relative(HZ));
  565. init_timer(&priv->poll_timer);
  566. priv->poll_timer.function = ipoib_ib_tx_timer_func;
  567. priv->poll_timer.data = (unsigned long)dev;
  568. set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
  569. return 0;
  570. }
  571. static void ipoib_pkey_dev_check_presence(struct net_device *dev)
  572. {
  573. struct ipoib_dev_priv *priv = netdev_priv(dev);
  574. u16 pkey_index = 0;
  575. if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
  576. clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  577. else
  578. set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  579. }
  580. int ipoib_ib_dev_up(struct net_device *dev)
  581. {
  582. struct ipoib_dev_priv *priv = netdev_priv(dev);
  583. ipoib_pkey_dev_check_presence(dev);
  584. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  585. ipoib_dbg(priv, "PKEY is not assigned.\n");
  586. return 0;
  587. }
  588. set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
  589. return ipoib_mcast_start_thread(dev);
  590. }
  591. int ipoib_ib_dev_down(struct net_device *dev, int flush)
  592. {
  593. struct ipoib_dev_priv *priv = netdev_priv(dev);
  594. ipoib_dbg(priv, "downing ib_dev\n");
  595. clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
  596. netif_carrier_off(dev);
  597. /* Shutdown the P_Key thread if still active */
  598. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  599. mutex_lock(&pkey_mutex);
  600. set_bit(IPOIB_PKEY_STOP, &priv->flags);
  601. cancel_delayed_work(&priv->pkey_poll_task);
  602. mutex_unlock(&pkey_mutex);
  603. if (flush)
  604. flush_workqueue(ipoib_workqueue);
  605. }
  606. ipoib_mcast_stop_thread(dev, flush);
  607. ipoib_mcast_dev_flush(dev);
  608. ipoib_flush_paths(dev);
  609. return 0;
  610. }
  611. static int recvs_pending(struct net_device *dev)
  612. {
  613. struct ipoib_dev_priv *priv = netdev_priv(dev);
  614. int pending = 0;
  615. int i;
  616. for (i = 0; i < ipoib_recvq_size; ++i)
  617. if (priv->rx_ring[i].skb)
  618. ++pending;
  619. return pending;
  620. }
  621. void ipoib_drain_cq(struct net_device *dev)
  622. {
  623. struct ipoib_dev_priv *priv = netdev_priv(dev);
  624. int i, n;
  625. do {
  626. n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);
  627. for (i = 0; i < n; ++i) {
  628. /*
  629. * Convert any successful completions to flush
  630. * errors to avoid passing packets up the
  631. * stack after bringing the device down.
  632. */
  633. if (priv->ibwc[i].status == IB_WC_SUCCESS)
  634. priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
  635. if (priv->ibwc[i].wr_id & IPOIB_OP_RECV) {
  636. if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
  637. ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
  638. else
  639. ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
  640. } else
  641. ipoib_cm_handle_tx_wc(dev, priv->ibwc + i);
  642. }
  643. } while (n == IPOIB_NUM_WC);
  644. while (poll_tx(priv))
  645. ; /* nothing */
  646. }
  647. int ipoib_ib_dev_stop(struct net_device *dev, int flush)
  648. {
  649. struct ipoib_dev_priv *priv = netdev_priv(dev);
  650. struct ib_qp_attr qp_attr;
  651. unsigned long begin;
  652. struct ipoib_tx_buf *tx_req;
  653. int i;
  654. clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
  655. ipoib_cm_dev_stop(dev);
  656. /*
  657. * Move our QP to the error state and then reinitialize in
  658. * when all work requests have completed or have been flushed.
  659. */
  660. qp_attr.qp_state = IB_QPS_ERR;
  661. if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
  662. ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
  663. /* Wait for all sends and receives to complete */
  664. begin = jiffies;
  665. while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
  666. if (time_after(jiffies, begin + 5 * HZ)) {
  667. ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
  668. priv->tx_head - priv->tx_tail, recvs_pending(dev));
  669. /*
  670. * assume the HW is wedged and just free up
  671. * all our pending work requests.
  672. */
  673. while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
  674. tx_req = &priv->tx_ring[priv->tx_tail &
  675. (ipoib_sendq_size - 1)];
  676. ipoib_dma_unmap_tx(priv->ca, tx_req);
  677. dev_kfree_skb_any(tx_req->skb);
  678. ++priv->tx_tail;
  679. --priv->tx_outstanding;
  680. }
  681. for (i = 0; i < ipoib_recvq_size; ++i) {
  682. struct ipoib_rx_buf *rx_req;
  683. rx_req = &priv->rx_ring[i];
  684. if (!rx_req->skb)
  685. continue;
  686. ipoib_ud_dma_unmap_rx(priv,
  687. priv->rx_ring[i].mapping);
  688. dev_kfree_skb_any(rx_req->skb);
  689. rx_req->skb = NULL;
  690. }
  691. goto timeout;
  692. }
  693. ipoib_drain_cq(dev);
  694. msleep(1);
  695. }
  696. ipoib_dbg(priv, "All sends and receives done.\n");
  697. timeout:
  698. del_timer_sync(&priv->poll_timer);
  699. qp_attr.qp_state = IB_QPS_RESET;
  700. if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
  701. ipoib_warn(priv, "Failed to modify QP to RESET state\n");
  702. /* Wait for all AHs to be reaped */
  703. set_bit(IPOIB_STOP_REAPER, &priv->flags);
  704. cancel_delayed_work(&priv->ah_reap_task);
  705. if (flush)
  706. flush_workqueue(ipoib_workqueue);
  707. begin = jiffies;
  708. while (!list_empty(&priv->dead_ahs)) {
  709. __ipoib_reap_ah(dev);
  710. if (time_after(jiffies, begin + HZ)) {
  711. ipoib_warn(priv, "timing out; will leak address handles\n");
  712. break;
  713. }
  714. msleep(1);
  715. }
  716. ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP);
  717. return 0;
  718. }
  719. int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
  720. {
  721. struct ipoib_dev_priv *priv = netdev_priv(dev);
  722. priv->ca = ca;
  723. priv->port = port;
  724. priv->qp = NULL;
  725. if (ipoib_transport_dev_init(dev, ca)) {
  726. printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
  727. return -ENODEV;
  728. }
  729. if (dev->flags & IFF_UP) {
  730. if (ipoib_ib_dev_open(dev)) {
  731. ipoib_transport_dev_cleanup(dev);
  732. return -ENODEV;
  733. }
  734. }
  735. return 0;
  736. }
  737. static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv,
  738. enum ipoib_flush_level level)
  739. {
  740. struct ipoib_dev_priv *cpriv;
  741. struct net_device *dev = priv->dev;
  742. u16 new_index;
  743. mutex_lock(&priv->vlan_mutex);
  744. /*
  745. * Flush any child interfaces too -- they might be up even if
  746. * the parent is down.
  747. */
  748. list_for_each_entry(cpriv, &priv->child_intfs, list)
  749. __ipoib_ib_dev_flush(cpriv, level);
  750. mutex_unlock(&priv->vlan_mutex);
  751. if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
  752. ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
  753. return;
  754. }
  755. if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
  756. ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
  757. return;
  758. }
  759. if (level == IPOIB_FLUSH_HEAVY) {
  760. if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
  761. clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
  762. ipoib_ib_dev_down(dev, 0);
  763. ipoib_ib_dev_stop(dev, 0);
  764. if (ipoib_pkey_dev_delay_open(dev))
  765. return;
  766. }
  767. /* restart QP only if P_Key index is changed */
  768. if (test_and_set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) &&
  769. new_index == priv->pkey_index) {
  770. ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
  771. return;
  772. }
  773. priv->pkey_index = new_index;
  774. }
  775. if (level == IPOIB_FLUSH_LIGHT) {
  776. ipoib_mark_paths_invalid(dev);
  777. ipoib_mcast_dev_flush(dev);
  778. }
  779. if (level >= IPOIB_FLUSH_NORMAL)
  780. ipoib_ib_dev_down(dev, 0);
  781. if (level == IPOIB_FLUSH_HEAVY) {
  782. ipoib_ib_dev_stop(dev, 0);
  783. ipoib_ib_dev_open(dev);
  784. }
  785. /*
  786. * The device could have been brought down between the start and when
  787. * we get here, don't bring it back up if it's not configured up
  788. */
  789. if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
  790. if (level >= IPOIB_FLUSH_NORMAL)
  791. ipoib_ib_dev_up(dev);
  792. ipoib_mcast_restart_task(&priv->restart_task);
  793. }
  794. }
  795. void ipoib_ib_dev_flush_light(struct work_struct *work)
  796. {
  797. struct ipoib_dev_priv *priv =
  798. container_of(work, struct ipoib_dev_priv, flush_light);
  799. __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_LIGHT);
  800. }
  801. void ipoib_ib_dev_flush_normal(struct work_struct *work)
  802. {
  803. struct ipoib_dev_priv *priv =
  804. container_of(work, struct ipoib_dev_priv, flush_normal);
  805. __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_NORMAL);
  806. }
  807. void ipoib_ib_dev_flush_heavy(struct work_struct *work)
  808. {
  809. struct ipoib_dev_priv *priv =
  810. container_of(work, struct ipoib_dev_priv, flush_heavy);
  811. __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_HEAVY);
  812. }
  813. void ipoib_ib_dev_cleanup(struct net_device *dev)
  814. {
  815. struct ipoib_dev_priv *priv = netdev_priv(dev);
  816. ipoib_dbg(priv, "cleaning up ib_dev\n");
  817. ipoib_mcast_stop_thread(dev, 1);
  818. ipoib_mcast_dev_flush(dev);
  819. ipoib_transport_dev_cleanup(dev);
  820. }
  821. /*
  822. * Delayed P_Key Assigment Interim Support
  823. *
  824. * The following is initial implementation of delayed P_Key assigment
  825. * mechanism. It is using the same approach implemented for the multicast
  826. * group join. The single goal of this implementation is to quickly address
  827. * Bug #2507. This implementation will probably be removed when the P_Key
  828. * change async notification is available.
  829. */
  830. void ipoib_pkey_poll(struct work_struct *work)
  831. {
  832. struct ipoib_dev_priv *priv =
  833. container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
  834. struct net_device *dev = priv->dev;
  835. ipoib_pkey_dev_check_presence(dev);
  836. if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
  837. ipoib_open(dev);
  838. else {
  839. mutex_lock(&pkey_mutex);
  840. if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
  841. queue_delayed_work(ipoib_workqueue,
  842. &priv->pkey_poll_task,
  843. HZ);
  844. mutex_unlock(&pkey_mutex);
  845. }
  846. }
  847. int ipoib_pkey_dev_delay_open(struct net_device *dev)
  848. {
  849. struct ipoib_dev_priv *priv = netdev_priv(dev);
  850. /* Look for the interface pkey value in the IB Port P_Key table and */
  851. /* set the interface pkey assigment flag */
  852. ipoib_pkey_dev_check_presence(dev);
  853. /* P_Key value not assigned yet - start polling */
  854. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
  855. mutex_lock(&pkey_mutex);
  856. clear_bit(IPOIB_PKEY_STOP, &priv->flags);
  857. queue_delayed_work(ipoib_workqueue,
  858. &priv->pkey_poll_task,
  859. HZ);
  860. mutex_unlock(&pkey_mutex);
  861. return 1;
  862. }
  863. return 0;
  864. }