en_tx.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /*
  2. * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <asm/page.h>
  34. #include <linux/mlx4/cq.h>
  35. #include <linux/slab.h>
  36. #include <linux/mlx4/qp.h>
  37. #include <linux/skbuff.h>
  38. #include <linux/if_vlan.h>
  39. #include <linux/vmalloc.h>
  40. #include <linux/tcp.h>
  41. #include <linux/moduleparam.h>
  42. #include "mlx4_en.h"
  43. enum {
  44. MAX_INLINE = 104, /* 128 - 16 - 4 - 4 */
  45. MAX_BF = 256,
  46. };
  47. static int inline_thold __read_mostly = MAX_INLINE;
  48. module_param_named(inline_thold, inline_thold, int, 0444);
  49. MODULE_PARM_DESC(inline_thold, "threshold for using inline data");
  50. int mlx4_en_create_tx_ring(struct mlx4_en_priv *priv,
  51. struct mlx4_en_tx_ring *ring, int qpn, u32 size,
  52. u16 stride)
  53. {
  54. struct mlx4_en_dev *mdev = priv->mdev;
  55. int tmp;
  56. int err;
  57. ring->size = size;
  58. ring->size_mask = size - 1;
  59. ring->stride = stride;
  60. inline_thold = min(inline_thold, MAX_INLINE);
  61. spin_lock_init(&ring->comp_lock);
  62. tmp = size * sizeof(struct mlx4_en_tx_info);
  63. ring->tx_info = vmalloc(tmp);
  64. if (!ring->tx_info) {
  65. en_err(priv, "Failed allocating tx_info ring\n");
  66. return -ENOMEM;
  67. }
  68. en_dbg(DRV, priv, "Allocated tx_info ring at addr:%p size:%d\n",
  69. ring->tx_info, tmp);
  70. ring->bounce_buf = kmalloc(MAX_DESC_SIZE, GFP_KERNEL);
  71. if (!ring->bounce_buf) {
  72. en_err(priv, "Failed allocating bounce buffer\n");
  73. err = -ENOMEM;
  74. goto err_tx;
  75. }
  76. ring->buf_size = ALIGN(size * ring->stride, MLX4_EN_PAGE_SIZE);
  77. err = mlx4_alloc_hwq_res(mdev->dev, &ring->wqres, ring->buf_size,
  78. 2 * PAGE_SIZE);
  79. if (err) {
  80. en_err(priv, "Failed allocating hwq resources\n");
  81. goto err_bounce;
  82. }
  83. err = mlx4_en_map_buffer(&ring->wqres.buf);
  84. if (err) {
  85. en_err(priv, "Failed to map TX buffer\n");
  86. goto err_hwq_res;
  87. }
  88. ring->buf = ring->wqres.buf.direct.buf;
  89. en_dbg(DRV, priv, "Allocated TX ring (addr:%p) - buf:%p size:%d "
  90. "buf_size:%d dma:%llx\n", ring, ring->buf, ring->size,
  91. ring->buf_size, (unsigned long long) ring->wqres.buf.direct.map);
  92. ring->qpn = qpn;
  93. err = mlx4_qp_alloc(mdev->dev, ring->qpn, &ring->qp);
  94. if (err) {
  95. en_err(priv, "Failed allocating qp %d\n", ring->qpn);
  96. goto err_map;
  97. }
  98. ring->qp.event = mlx4_en_sqp_event;
  99. err = mlx4_bf_alloc(mdev->dev, &ring->bf);
  100. if (err) {
  101. en_dbg(DRV, priv, "working without blueflame (%d)", err);
  102. ring->bf.uar = &mdev->priv_uar;
  103. ring->bf.uar->map = mdev->uar_map;
  104. ring->bf_enabled = false;
  105. } else
  106. ring->bf_enabled = true;
  107. return 0;
  108. err_map:
  109. mlx4_en_unmap_buffer(&ring->wqres.buf);
  110. err_hwq_res:
  111. mlx4_free_hwq_res(mdev->dev, &ring->wqres, ring->buf_size);
  112. err_bounce:
  113. kfree(ring->bounce_buf);
  114. ring->bounce_buf = NULL;
  115. err_tx:
  116. vfree(ring->tx_info);
  117. ring->tx_info = NULL;
  118. return err;
  119. }
  120. void mlx4_en_destroy_tx_ring(struct mlx4_en_priv *priv,
  121. struct mlx4_en_tx_ring *ring)
  122. {
  123. struct mlx4_en_dev *mdev = priv->mdev;
  124. en_dbg(DRV, priv, "Destroying tx ring, qpn: %d\n", ring->qpn);
  125. if (ring->bf_enabled)
  126. mlx4_bf_free(mdev->dev, &ring->bf);
  127. mlx4_qp_remove(mdev->dev, &ring->qp);
  128. mlx4_qp_free(mdev->dev, &ring->qp);
  129. mlx4_qp_release_range(mdev->dev, ring->qpn, 1);
  130. mlx4_en_unmap_buffer(&ring->wqres.buf);
  131. mlx4_free_hwq_res(mdev->dev, &ring->wqres, ring->buf_size);
  132. kfree(ring->bounce_buf);
  133. ring->bounce_buf = NULL;
  134. vfree(ring->tx_info);
  135. ring->tx_info = NULL;
  136. }
  137. int mlx4_en_activate_tx_ring(struct mlx4_en_priv *priv,
  138. struct mlx4_en_tx_ring *ring,
  139. int cq)
  140. {
  141. struct mlx4_en_dev *mdev = priv->mdev;
  142. int err;
  143. ring->cqn = cq;
  144. ring->prod = 0;
  145. ring->cons = 0xffffffff;
  146. ring->last_nr_txbb = 1;
  147. ring->poll_cnt = 0;
  148. ring->blocked = 0;
  149. memset(ring->tx_info, 0, ring->size * sizeof(struct mlx4_en_tx_info));
  150. memset(ring->buf, 0, ring->buf_size);
  151. ring->qp_state = MLX4_QP_STATE_RST;
  152. ring->doorbell_qpn = ring->qp.qpn << 8;
  153. mlx4_en_fill_qp_context(priv, ring->size, ring->stride, 1, 0, ring->qpn,
  154. ring->cqn, &ring->context);
  155. if (ring->bf_enabled)
  156. ring->context.usr_page = cpu_to_be32(ring->bf.uar->index);
  157. err = mlx4_qp_to_ready(mdev->dev, &ring->wqres.mtt, &ring->context,
  158. &ring->qp, &ring->qp_state);
  159. return err;
  160. }
  161. void mlx4_en_deactivate_tx_ring(struct mlx4_en_priv *priv,
  162. struct mlx4_en_tx_ring *ring)
  163. {
  164. struct mlx4_en_dev *mdev = priv->mdev;
  165. mlx4_qp_modify(mdev->dev, NULL, ring->qp_state,
  166. MLX4_QP_STATE_RST, NULL, 0, 0, &ring->qp);
  167. }
  168. static u32 mlx4_en_free_tx_desc(struct mlx4_en_priv *priv,
  169. struct mlx4_en_tx_ring *ring,
  170. int index, u8 owner)
  171. {
  172. struct mlx4_en_dev *mdev = priv->mdev;
  173. struct mlx4_en_tx_info *tx_info = &ring->tx_info[index];
  174. struct mlx4_en_tx_desc *tx_desc = ring->buf + index * TXBB_SIZE;
  175. struct mlx4_wqe_data_seg *data = (void *) tx_desc + tx_info->data_offset;
  176. struct sk_buff *skb = tx_info->skb;
  177. struct skb_frag_struct *frag;
  178. void *end = ring->buf + ring->buf_size;
  179. int frags = skb_shinfo(skb)->nr_frags;
  180. int i;
  181. __be32 *ptr = (__be32 *)tx_desc;
  182. __be32 stamp = cpu_to_be32(STAMP_VAL | (!!owner << STAMP_SHIFT));
  183. /* Optimize the common case when there are no wraparounds */
  184. if (likely((void *) tx_desc + tx_info->nr_txbb * TXBB_SIZE <= end)) {
  185. if (!tx_info->inl) {
  186. if (tx_info->linear) {
  187. pci_unmap_single(mdev->pdev,
  188. (dma_addr_t) be64_to_cpu(data->addr),
  189. be32_to_cpu(data->byte_count),
  190. PCI_DMA_TODEVICE);
  191. ++data;
  192. }
  193. for (i = 0; i < frags; i++) {
  194. frag = &skb_shinfo(skb)->frags[i];
  195. pci_unmap_page(mdev->pdev,
  196. (dma_addr_t) be64_to_cpu(data[i].addr),
  197. skb_frag_size(frag), PCI_DMA_TODEVICE);
  198. }
  199. }
  200. /* Stamp the freed descriptor */
  201. for (i = 0; i < tx_info->nr_txbb * TXBB_SIZE; i += STAMP_STRIDE) {
  202. *ptr = stamp;
  203. ptr += STAMP_DWORDS;
  204. }
  205. } else {
  206. if (!tx_info->inl) {
  207. if ((void *) data >= end) {
  208. data = ring->buf + ((void *)data - end);
  209. }
  210. if (tx_info->linear) {
  211. pci_unmap_single(mdev->pdev,
  212. (dma_addr_t) be64_to_cpu(data->addr),
  213. be32_to_cpu(data->byte_count),
  214. PCI_DMA_TODEVICE);
  215. ++data;
  216. }
  217. for (i = 0; i < frags; i++) {
  218. /* Check for wraparound before unmapping */
  219. if ((void *) data >= end)
  220. data = ring->buf;
  221. frag = &skb_shinfo(skb)->frags[i];
  222. pci_unmap_page(mdev->pdev,
  223. (dma_addr_t) be64_to_cpu(data->addr),
  224. skb_frag_size(frag), PCI_DMA_TODEVICE);
  225. ++data;
  226. }
  227. }
  228. /* Stamp the freed descriptor */
  229. for (i = 0; i < tx_info->nr_txbb * TXBB_SIZE; i += STAMP_STRIDE) {
  230. *ptr = stamp;
  231. ptr += STAMP_DWORDS;
  232. if ((void *) ptr >= end) {
  233. ptr = ring->buf;
  234. stamp ^= cpu_to_be32(0x80000000);
  235. }
  236. }
  237. }
  238. dev_kfree_skb_any(skb);
  239. return tx_info->nr_txbb;
  240. }
  241. int mlx4_en_free_tx_buf(struct net_device *dev, struct mlx4_en_tx_ring *ring)
  242. {
  243. struct mlx4_en_priv *priv = netdev_priv(dev);
  244. int cnt = 0;
  245. /* Skip last polled descriptor */
  246. ring->cons += ring->last_nr_txbb;
  247. en_dbg(DRV, priv, "Freeing Tx buf - cons:0x%x prod:0x%x\n",
  248. ring->cons, ring->prod);
  249. if ((u32) (ring->prod - ring->cons) > ring->size) {
  250. if (netif_msg_tx_err(priv))
  251. en_warn(priv, "Tx consumer passed producer!\n");
  252. return 0;
  253. }
  254. while (ring->cons != ring->prod) {
  255. ring->last_nr_txbb = mlx4_en_free_tx_desc(priv, ring,
  256. ring->cons & ring->size_mask,
  257. !!(ring->cons & ring->size));
  258. ring->cons += ring->last_nr_txbb;
  259. cnt++;
  260. }
  261. if (cnt)
  262. en_dbg(DRV, priv, "Freed %d uncompleted tx descriptors\n", cnt);
  263. return cnt;
  264. }
  265. static void mlx4_en_process_tx_cq(struct net_device *dev, struct mlx4_en_cq *cq)
  266. {
  267. struct mlx4_en_priv *priv = netdev_priv(dev);
  268. struct mlx4_cq *mcq = &cq->mcq;
  269. struct mlx4_en_tx_ring *ring = &priv->tx_ring[cq->ring];
  270. struct mlx4_cqe *cqe;
  271. u16 index;
  272. u16 new_index, ring_index;
  273. u32 txbbs_skipped = 0;
  274. u32 cons_index = mcq->cons_index;
  275. int size = cq->size;
  276. u32 size_mask = ring->size_mask;
  277. struct mlx4_cqe *buf = cq->buf;
  278. if (!priv->port_up)
  279. return;
  280. index = cons_index & size_mask;
  281. cqe = &buf[index];
  282. ring_index = ring->cons & size_mask;
  283. /* Process all completed CQEs */
  284. while (XNOR(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK,
  285. cons_index & size)) {
  286. /*
  287. * make sure we read the CQE after we read the
  288. * ownership bit
  289. */
  290. rmb();
  291. /* Skip over last polled CQE */
  292. new_index = be16_to_cpu(cqe->wqe_index) & size_mask;
  293. do {
  294. txbbs_skipped += ring->last_nr_txbb;
  295. ring_index = (ring_index + ring->last_nr_txbb) & size_mask;
  296. /* free next descriptor */
  297. ring->last_nr_txbb = mlx4_en_free_tx_desc(
  298. priv, ring, ring_index,
  299. !!((ring->cons + txbbs_skipped) &
  300. ring->size));
  301. } while (ring_index != new_index);
  302. ++cons_index;
  303. index = cons_index & size_mask;
  304. cqe = &buf[index];
  305. }
  306. /*
  307. * To prevent CQ overflow we first update CQ consumer and only then
  308. * the ring consumer.
  309. */
  310. mcq->cons_index = cons_index;
  311. mlx4_cq_set_ci(mcq);
  312. wmb();
  313. ring->cons += txbbs_skipped;
  314. /* Wakeup Tx queue if this ring stopped it */
  315. if (unlikely(ring->blocked)) {
  316. if ((u32) (ring->prod - ring->cons) <=
  317. ring->size - HEADROOM - MAX_DESC_TXBBS) {
  318. ring->blocked = 0;
  319. netif_tx_wake_queue(netdev_get_tx_queue(dev, cq->ring));
  320. priv->port_stats.wake_queue++;
  321. }
  322. }
  323. }
  324. void mlx4_en_tx_irq(struct mlx4_cq *mcq)
  325. {
  326. struct mlx4_en_cq *cq = container_of(mcq, struct mlx4_en_cq, mcq);
  327. struct mlx4_en_priv *priv = netdev_priv(cq->dev);
  328. struct mlx4_en_tx_ring *ring = &priv->tx_ring[cq->ring];
  329. if (!spin_trylock(&ring->comp_lock))
  330. return;
  331. mlx4_en_process_tx_cq(cq->dev, cq);
  332. mod_timer(&cq->timer, jiffies + 1);
  333. spin_unlock(&ring->comp_lock);
  334. }
  335. void mlx4_en_poll_tx_cq(unsigned long data)
  336. {
  337. struct mlx4_en_cq *cq = (struct mlx4_en_cq *) data;
  338. struct mlx4_en_priv *priv = netdev_priv(cq->dev);
  339. struct mlx4_en_tx_ring *ring = &priv->tx_ring[cq->ring];
  340. u32 inflight;
  341. INC_PERF_COUNTER(priv->pstats.tx_poll);
  342. if (!spin_trylock_irq(&ring->comp_lock)) {
  343. mod_timer(&cq->timer, jiffies + MLX4_EN_TX_POLL_TIMEOUT);
  344. return;
  345. }
  346. mlx4_en_process_tx_cq(cq->dev, cq);
  347. inflight = (u32) (ring->prod - ring->cons - ring->last_nr_txbb);
  348. /* If there are still packets in flight and the timer has not already
  349. * been scheduled by the Tx routine then schedule it here to guarantee
  350. * completion processing of these packets */
  351. if (inflight && priv->port_up)
  352. mod_timer(&cq->timer, jiffies + MLX4_EN_TX_POLL_TIMEOUT);
  353. spin_unlock_irq(&ring->comp_lock);
  354. }
  355. static struct mlx4_en_tx_desc *mlx4_en_bounce_to_desc(struct mlx4_en_priv *priv,
  356. struct mlx4_en_tx_ring *ring,
  357. u32 index,
  358. unsigned int desc_size)
  359. {
  360. u32 copy = (ring->size - index) * TXBB_SIZE;
  361. int i;
  362. for (i = desc_size - copy - 4; i >= 0; i -= 4) {
  363. if ((i & (TXBB_SIZE - 1)) == 0)
  364. wmb();
  365. *((u32 *) (ring->buf + i)) =
  366. *((u32 *) (ring->bounce_buf + copy + i));
  367. }
  368. for (i = copy - 4; i >= 4 ; i -= 4) {
  369. if ((i & (TXBB_SIZE - 1)) == 0)
  370. wmb();
  371. *((u32 *) (ring->buf + index * TXBB_SIZE + i)) =
  372. *((u32 *) (ring->bounce_buf + i));
  373. }
  374. /* Return real descriptor location */
  375. return ring->buf + index * TXBB_SIZE;
  376. }
  377. static inline void mlx4_en_xmit_poll(struct mlx4_en_priv *priv, int tx_ind)
  378. {
  379. struct mlx4_en_cq *cq = &priv->tx_cq[tx_ind];
  380. struct mlx4_en_tx_ring *ring = &priv->tx_ring[tx_ind];
  381. unsigned long flags;
  382. /* If we don't have a pending timer, set one up to catch our recent
  383. post in case the interface becomes idle */
  384. if (!timer_pending(&cq->timer))
  385. mod_timer(&cq->timer, jiffies + MLX4_EN_TX_POLL_TIMEOUT);
  386. /* Poll the CQ every mlx4_en_TX_MODER_POLL packets */
  387. if ((++ring->poll_cnt & (MLX4_EN_TX_POLL_MODER - 1)) == 0)
  388. if (spin_trylock_irqsave(&ring->comp_lock, flags)) {
  389. mlx4_en_process_tx_cq(priv->dev, cq);
  390. spin_unlock_irqrestore(&ring->comp_lock, flags);
  391. }
  392. }
  393. static int is_inline(struct sk_buff *skb, void **pfrag)
  394. {
  395. void *ptr;
  396. if (inline_thold && !skb_is_gso(skb) && skb->len <= inline_thold) {
  397. if (skb_shinfo(skb)->nr_frags == 1) {
  398. ptr = skb_frag_address_safe(&skb_shinfo(skb)->frags[0]);
  399. if (unlikely(!ptr))
  400. return 0;
  401. if (pfrag)
  402. *pfrag = ptr;
  403. return 1;
  404. } else if (unlikely(skb_shinfo(skb)->nr_frags))
  405. return 0;
  406. else
  407. return 1;
  408. }
  409. return 0;
  410. }
  411. static int inline_size(struct sk_buff *skb)
  412. {
  413. if (skb->len + CTRL_SIZE + sizeof(struct mlx4_wqe_inline_seg)
  414. <= MLX4_INLINE_ALIGN)
  415. return ALIGN(skb->len + CTRL_SIZE +
  416. sizeof(struct mlx4_wqe_inline_seg), 16);
  417. else
  418. return ALIGN(skb->len + CTRL_SIZE + 2 *
  419. sizeof(struct mlx4_wqe_inline_seg), 16);
  420. }
  421. static int get_real_size(struct sk_buff *skb, struct net_device *dev,
  422. int *lso_header_size)
  423. {
  424. struct mlx4_en_priv *priv = netdev_priv(dev);
  425. int real_size;
  426. if (skb_is_gso(skb)) {
  427. *lso_header_size = skb_transport_offset(skb) + tcp_hdrlen(skb);
  428. real_size = CTRL_SIZE + skb_shinfo(skb)->nr_frags * DS_SIZE +
  429. ALIGN(*lso_header_size + 4, DS_SIZE);
  430. if (unlikely(*lso_header_size != skb_headlen(skb))) {
  431. /* We add a segment for the skb linear buffer only if
  432. * it contains data */
  433. if (*lso_header_size < skb_headlen(skb))
  434. real_size += DS_SIZE;
  435. else {
  436. if (netif_msg_tx_err(priv))
  437. en_warn(priv, "Non-linear headers\n");
  438. return 0;
  439. }
  440. }
  441. } else {
  442. *lso_header_size = 0;
  443. if (!is_inline(skb, NULL))
  444. real_size = CTRL_SIZE + (skb_shinfo(skb)->nr_frags + 1) * DS_SIZE;
  445. else
  446. real_size = inline_size(skb);
  447. }
  448. return real_size;
  449. }
  450. static void build_inline_wqe(struct mlx4_en_tx_desc *tx_desc, struct sk_buff *skb,
  451. int real_size, u16 *vlan_tag, int tx_ind, void *fragptr)
  452. {
  453. struct mlx4_wqe_inline_seg *inl = &tx_desc->inl;
  454. int spc = MLX4_INLINE_ALIGN - CTRL_SIZE - sizeof *inl;
  455. if (skb->len <= spc) {
  456. inl->byte_count = cpu_to_be32(1 << 31 | skb->len);
  457. skb_copy_from_linear_data(skb, inl + 1, skb_headlen(skb));
  458. if (skb_shinfo(skb)->nr_frags)
  459. memcpy(((void *)(inl + 1)) + skb_headlen(skb), fragptr,
  460. skb_frag_size(&skb_shinfo(skb)->frags[0]));
  461. } else {
  462. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  463. if (skb_headlen(skb) <= spc) {
  464. skb_copy_from_linear_data(skb, inl + 1, skb_headlen(skb));
  465. if (skb_headlen(skb) < spc) {
  466. memcpy(((void *)(inl + 1)) + skb_headlen(skb),
  467. fragptr, spc - skb_headlen(skb));
  468. fragptr += spc - skb_headlen(skb);
  469. }
  470. inl = (void *) (inl + 1) + spc;
  471. memcpy(((void *)(inl + 1)), fragptr, skb->len - spc);
  472. } else {
  473. skb_copy_from_linear_data(skb, inl + 1, spc);
  474. inl = (void *) (inl + 1) + spc;
  475. skb_copy_from_linear_data_offset(skb, spc, inl + 1,
  476. skb_headlen(skb) - spc);
  477. if (skb_shinfo(skb)->nr_frags)
  478. memcpy(((void *)(inl + 1)) + skb_headlen(skb) - spc,
  479. fragptr, skb_frag_size(&skb_shinfo(skb)->frags[0]));
  480. }
  481. wmb();
  482. inl->byte_count = cpu_to_be32(1 << 31 | (skb->len - spc));
  483. }
  484. tx_desc->ctrl.vlan_tag = cpu_to_be16(*vlan_tag);
  485. tx_desc->ctrl.ins_vlan = MLX4_WQE_CTRL_INS_VLAN *
  486. (!!vlan_tx_tag_present(skb));
  487. tx_desc->ctrl.fence_size = (real_size / 16) & 0x3f;
  488. }
  489. u16 mlx4_en_select_queue(struct net_device *dev, struct sk_buff *skb)
  490. {
  491. struct mlx4_en_priv *priv = netdev_priv(dev);
  492. u16 vlan_tag = 0;
  493. /* If we support per priority flow control and the packet contains
  494. * a vlan tag, send the packet to the TX ring assigned to that priority
  495. */
  496. if (priv->prof->rx_ppp && vlan_tx_tag_present(skb)) {
  497. vlan_tag = vlan_tx_tag_get(skb);
  498. return MLX4_EN_NUM_TX_RINGS + (vlan_tag >> 13);
  499. }
  500. return skb_tx_hash(dev, skb);
  501. }
  502. static void mlx4_bf_copy(unsigned long *dst, unsigned long *src, unsigned bytecnt)
  503. {
  504. __iowrite64_copy(dst, src, bytecnt / 8);
  505. }
  506. netdev_tx_t mlx4_en_xmit(struct sk_buff *skb, struct net_device *dev)
  507. {
  508. struct mlx4_en_priv *priv = netdev_priv(dev);
  509. struct mlx4_en_dev *mdev = priv->mdev;
  510. struct mlx4_en_tx_ring *ring;
  511. struct mlx4_en_cq *cq;
  512. struct mlx4_en_tx_desc *tx_desc;
  513. struct mlx4_wqe_data_seg *data;
  514. struct skb_frag_struct *frag;
  515. struct mlx4_en_tx_info *tx_info;
  516. struct ethhdr *ethh;
  517. u64 mac;
  518. u32 mac_l, mac_h;
  519. int tx_ind = 0;
  520. int nr_txbb;
  521. int desc_size;
  522. int real_size;
  523. dma_addr_t dma;
  524. u32 index, bf_index;
  525. __be32 op_own;
  526. u16 vlan_tag = 0;
  527. int i;
  528. int lso_header_size;
  529. void *fragptr;
  530. bool bounce = false;
  531. if (!priv->port_up)
  532. goto tx_drop;
  533. real_size = get_real_size(skb, dev, &lso_header_size);
  534. if (unlikely(!real_size))
  535. goto tx_drop;
  536. /* Align descriptor to TXBB size */
  537. desc_size = ALIGN(real_size, TXBB_SIZE);
  538. nr_txbb = desc_size / TXBB_SIZE;
  539. if (unlikely(nr_txbb > MAX_DESC_TXBBS)) {
  540. if (netif_msg_tx_err(priv))
  541. en_warn(priv, "Oversized header or SG list\n");
  542. goto tx_drop;
  543. }
  544. tx_ind = skb->queue_mapping;
  545. ring = &priv->tx_ring[tx_ind];
  546. if (vlan_tx_tag_present(skb))
  547. vlan_tag = vlan_tx_tag_get(skb);
  548. /* Check available TXBBs And 2K spare for prefetch */
  549. if (unlikely(((int)(ring->prod - ring->cons)) >
  550. ring->size - HEADROOM - MAX_DESC_TXBBS)) {
  551. /* every full Tx ring stops queue */
  552. netif_tx_stop_queue(netdev_get_tx_queue(dev, tx_ind));
  553. ring->blocked = 1;
  554. priv->port_stats.queue_stopped++;
  555. /* Use interrupts to find out when queue opened */
  556. cq = &priv->tx_cq[tx_ind];
  557. mlx4_en_arm_cq(priv, cq);
  558. return NETDEV_TX_BUSY;
  559. }
  560. /* Track current inflight packets for performance analysis */
  561. AVG_PERF_COUNTER(priv->pstats.inflight_avg,
  562. (u32) (ring->prod - ring->cons - 1));
  563. /* Packet is good - grab an index and transmit it */
  564. index = ring->prod & ring->size_mask;
  565. bf_index = ring->prod;
  566. /* See if we have enough space for whole descriptor TXBB for setting
  567. * SW ownership on next descriptor; if not, use a bounce buffer. */
  568. if (likely(index + nr_txbb <= ring->size))
  569. tx_desc = ring->buf + index * TXBB_SIZE;
  570. else {
  571. tx_desc = (struct mlx4_en_tx_desc *) ring->bounce_buf;
  572. bounce = true;
  573. }
  574. /* Save skb in tx_info ring */
  575. tx_info = &ring->tx_info[index];
  576. tx_info->skb = skb;
  577. tx_info->nr_txbb = nr_txbb;
  578. /* Prepare ctrl segement apart opcode+ownership, which depends on
  579. * whether LSO is used */
  580. tx_desc->ctrl.vlan_tag = cpu_to_be16(vlan_tag);
  581. tx_desc->ctrl.ins_vlan = MLX4_WQE_CTRL_INS_VLAN *
  582. !!vlan_tx_tag_present(skb);
  583. tx_desc->ctrl.fence_size = (real_size / 16) & 0x3f;
  584. tx_desc->ctrl.srcrb_flags = priv->ctrl_flags;
  585. if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
  586. tx_desc->ctrl.srcrb_flags |= cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM |
  587. MLX4_WQE_CTRL_TCP_UDP_CSUM);
  588. ring->tx_csum++;
  589. }
  590. /* Copy dst mac address to wqe */
  591. skb_reset_mac_header(skb);
  592. ethh = eth_hdr(skb);
  593. if (ethh && ethh->h_dest) {
  594. mac = mlx4_en_mac_to_u64(ethh->h_dest);
  595. mac_h = (u32) ((mac & 0xffff00000000ULL) >> 16);
  596. mac_l = (u32) (mac & 0xffffffff);
  597. tx_desc->ctrl.srcrb_flags |= cpu_to_be32(mac_h);
  598. tx_desc->ctrl.imm = cpu_to_be32(mac_l);
  599. }
  600. /* Handle LSO (TSO) packets */
  601. if (lso_header_size) {
  602. /* Mark opcode as LSO */
  603. op_own = cpu_to_be32(MLX4_OPCODE_LSO | (1 << 6)) |
  604. ((ring->prod & ring->size) ?
  605. cpu_to_be32(MLX4_EN_BIT_DESC_OWN) : 0);
  606. /* Fill in the LSO prefix */
  607. tx_desc->lso.mss_hdr_size = cpu_to_be32(
  608. skb_shinfo(skb)->gso_size << 16 | lso_header_size);
  609. /* Copy headers;
  610. * note that we already verified that it is linear */
  611. memcpy(tx_desc->lso.header, skb->data, lso_header_size);
  612. data = ((void *) &tx_desc->lso +
  613. ALIGN(lso_header_size + 4, DS_SIZE));
  614. priv->port_stats.tso_packets++;
  615. i = ((skb->len - lso_header_size) / skb_shinfo(skb)->gso_size) +
  616. !!((skb->len - lso_header_size) % skb_shinfo(skb)->gso_size);
  617. ring->bytes += skb->len + (i - 1) * lso_header_size;
  618. ring->packets += i;
  619. } else {
  620. /* Normal (Non LSO) packet */
  621. op_own = cpu_to_be32(MLX4_OPCODE_SEND) |
  622. ((ring->prod & ring->size) ?
  623. cpu_to_be32(MLX4_EN_BIT_DESC_OWN) : 0);
  624. data = &tx_desc->data;
  625. ring->bytes += max(skb->len, (unsigned int) ETH_ZLEN);
  626. ring->packets++;
  627. }
  628. AVG_PERF_COUNTER(priv->pstats.tx_pktsz_avg, skb->len);
  629. /* valid only for none inline segments */
  630. tx_info->data_offset = (void *) data - (void *) tx_desc;
  631. tx_info->linear = (lso_header_size < skb_headlen(skb) && !is_inline(skb, NULL)) ? 1 : 0;
  632. data += skb_shinfo(skb)->nr_frags + tx_info->linear - 1;
  633. if (!is_inline(skb, &fragptr)) {
  634. /* Map fragments */
  635. for (i = skb_shinfo(skb)->nr_frags - 1; i >= 0; i--) {
  636. frag = &skb_shinfo(skb)->frags[i];
  637. dma = skb_frag_dma_map(&mdev->dev->pdev->dev, frag,
  638. 0, skb_frag_size(frag),
  639. DMA_TO_DEVICE);
  640. data->addr = cpu_to_be64(dma);
  641. data->lkey = cpu_to_be32(mdev->mr.key);
  642. wmb();
  643. data->byte_count = cpu_to_be32(skb_frag_size(frag));
  644. --data;
  645. }
  646. /* Map linear part */
  647. if (tx_info->linear) {
  648. dma = pci_map_single(mdev->dev->pdev, skb->data + lso_header_size,
  649. skb_headlen(skb) - lso_header_size, PCI_DMA_TODEVICE);
  650. data->addr = cpu_to_be64(dma);
  651. data->lkey = cpu_to_be32(mdev->mr.key);
  652. wmb();
  653. data->byte_count = cpu_to_be32(skb_headlen(skb) - lso_header_size);
  654. }
  655. tx_info->inl = 0;
  656. } else {
  657. build_inline_wqe(tx_desc, skb, real_size, &vlan_tag, tx_ind, fragptr);
  658. tx_info->inl = 1;
  659. }
  660. ring->prod += nr_txbb;
  661. /* If we used a bounce buffer then copy descriptor back into place */
  662. if (bounce)
  663. tx_desc = mlx4_en_bounce_to_desc(priv, ring, index, desc_size);
  664. /* Run destructor before passing skb to HW */
  665. if (likely(!skb_shared(skb)))
  666. skb_orphan(skb);
  667. if (ring->bf_enabled && desc_size <= MAX_BF && !bounce && !vlan_tag) {
  668. *(__be32 *) (&tx_desc->ctrl.vlan_tag) |= cpu_to_be32(ring->doorbell_qpn);
  669. op_own |= htonl((bf_index & 0xffff) << 8);
  670. /* Ensure new descirptor hits memory
  671. * before setting ownership of this descriptor to HW */
  672. wmb();
  673. tx_desc->ctrl.owner_opcode = op_own;
  674. wmb();
  675. mlx4_bf_copy(ring->bf.reg + ring->bf.offset, (unsigned long *) &tx_desc->ctrl,
  676. desc_size);
  677. wmb();
  678. ring->bf.offset ^= ring->bf.buf_size;
  679. } else {
  680. /* Ensure new descirptor hits memory
  681. * before setting ownership of this descriptor to HW */
  682. wmb();
  683. tx_desc->ctrl.owner_opcode = op_own;
  684. wmb();
  685. iowrite32be(ring->doorbell_qpn, ring->bf.uar->map + MLX4_SEND_DOORBELL);
  686. }
  687. /* Poll CQ here */
  688. mlx4_en_xmit_poll(priv, tx_ind);
  689. return NETDEV_TX_OK;
  690. tx_drop:
  691. dev_kfree_skb_any(skb);
  692. priv->stats.tx_dropped++;
  693. return NETDEV_TX_OK;
  694. }