ipoib_ib.c 25 KB

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