ipoib_ib.c 27 KB

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