ipoib_ib.c 28 KB

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