ipoib_ib.c 27 KB

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