en_tx.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  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 "mlx4_en.h"
  42. enum {
  43. MAX_INLINE = 104, /* 128 - 16 - 4 - 4 */
  44. MAX_BF = 256,
  45. };
  46. static int inline_thold __read_mostly = MAX_INLINE;
  47. module_param_named(inline_thold, inline_thold, int, 0444);
  48. MODULE_PARM_DESC(inline_thold, "threshold for using inline data");
  49. int mlx4_en_create_tx_ring(struct mlx4_en_priv *priv,
  50. struct mlx4_en_tx_ring *ring, int qpn, u32 size,
  51. u16 stride)
  52. {
  53. struct mlx4_en_dev *mdev = priv->mdev;
  54. int tmp;
  55. int err;
  56. ring->size = size;
  57. ring->size_mask = size - 1;
  58. ring->stride = stride;
  59. inline_thold = min(inline_thold, MAX_INLINE);
  60. spin_lock_init(&ring->comp_lock);
  61. tmp = size * sizeof(struct mlx4_en_tx_info);
  62. ring->tx_info = vmalloc(tmp);
  63. if (!ring->tx_info) {
  64. en_err(priv, "Failed allocating tx_info ring\n");
  65. return -ENOMEM;
  66. }
  67. en_dbg(DRV, priv, "Allocated tx_info ring at addr:%p size:%d\n",
  68. ring->tx_info, tmp);
  69. ring->bounce_buf = kmalloc(MAX_DESC_SIZE, GFP_KERNEL);
  70. if (!ring->bounce_buf) {
  71. en_err(priv, "Failed allocating bounce buffer\n");
  72. err = -ENOMEM;
  73. goto err_tx;
  74. }
  75. ring->buf_size = ALIGN(size * ring->stride, MLX4_EN_PAGE_SIZE);
  76. err = mlx4_alloc_hwq_res(mdev->dev, &ring->wqres, ring->buf_size,
  77. 2 * PAGE_SIZE);
  78. if (err) {
  79. en_err(priv, "Failed allocating hwq resources\n");
  80. goto err_bounce;
  81. }
  82. err = mlx4_en_map_buffer(&ring->wqres.buf);
  83. if (err) {
  84. en_err(priv, "Failed to map TX buffer\n");
  85. goto err_hwq_res;
  86. }
  87. ring->buf = ring->wqres.buf.direct.buf;
  88. en_dbg(DRV, priv, "Allocated TX ring (addr:%p) - buf:%p size:%d "
  89. "buf_size:%d dma:%llx\n", ring, ring->buf, ring->size,
  90. ring->buf_size, (unsigned long long) ring->wqres.buf.direct.map);
  91. ring->qpn = qpn;
  92. err = mlx4_qp_alloc(mdev->dev, ring->qpn, &ring->qp);
  93. if (err) {
  94. en_err(priv, "Failed allocating qp %d\n", ring->qpn);
  95. goto err_map;
  96. }
  97. ring->qp.event = mlx4_en_sqp_event;
  98. err = mlx4_bf_alloc(mdev->dev, &ring->bf);
  99. if (err) {
  100. en_dbg(DRV, priv, "working without blueflame (%d)", err);
  101. ring->bf.uar = &mdev->priv_uar;
  102. ring->bf.uar->map = mdev->uar_map;
  103. ring->bf_enabled = false;
  104. } else
  105. ring->bf_enabled = true;
  106. return 0;
  107. err_map:
  108. mlx4_en_unmap_buffer(&ring->wqres.buf);
  109. err_hwq_res:
  110. mlx4_free_hwq_res(mdev->dev, &ring->wqres, ring->buf_size);
  111. err_bounce:
  112. kfree(ring->bounce_buf);
  113. ring->bounce_buf = NULL;
  114. err_tx:
  115. vfree(ring->tx_info);
  116. ring->tx_info = NULL;
  117. return err;
  118. }
  119. void mlx4_en_destroy_tx_ring(struct mlx4_en_priv *priv,
  120. struct mlx4_en_tx_ring *ring)
  121. {
  122. struct mlx4_en_dev *mdev = priv->mdev;
  123. en_dbg(DRV, priv, "Destroying tx ring, qpn: %d\n", ring->qpn);
  124. if (ring->bf_enabled)
  125. mlx4_bf_free(mdev->dev, &ring->bf);
  126. mlx4_qp_remove(mdev->dev, &ring->qp);
  127. mlx4_qp_free(mdev->dev, &ring->qp);
  128. mlx4_qp_release_range(mdev->dev, ring->qpn, 1);
  129. mlx4_en_unmap_buffer(&ring->wqres.buf);
  130. mlx4_free_hwq_res(mdev->dev, &ring->wqres, ring->buf_size);
  131. kfree(ring->bounce_buf);
  132. ring->bounce_buf = NULL;
  133. vfree(ring->tx_info);
  134. ring->tx_info = NULL;
  135. }
  136. int mlx4_en_activate_tx_ring(struct mlx4_en_priv *priv,
  137. struct mlx4_en_tx_ring *ring,
  138. int cq)
  139. {
  140. struct mlx4_en_dev *mdev = priv->mdev;
  141. int err;
  142. ring->cqn = cq;
  143. ring->prod = 0;
  144. ring->cons = 0xffffffff;
  145. ring->last_nr_txbb = 1;
  146. ring->poll_cnt = 0;
  147. ring->blocked = 0;
  148. memset(ring->tx_info, 0, ring->size * sizeof(struct mlx4_en_tx_info));
  149. memset(ring->buf, 0, ring->buf_size);
  150. ring->qp_state = MLX4_QP_STATE_RST;
  151. ring->doorbell_qpn = swab32(ring->qp.qpn << 8);
  152. mlx4_en_fill_qp_context(priv, ring->size, ring->stride, 1, 0, ring->qpn,
  153. ring->cqn, &ring->context);
  154. if (ring->bf_enabled)
  155. ring->context.usr_page = cpu_to_be32(ring->bf.uar->index);
  156. err = mlx4_qp_to_ready(mdev->dev, &ring->wqres.mtt, &ring->context,
  157. &ring->qp, &ring->qp_state);
  158. return err;
  159. }
  160. void mlx4_en_deactivate_tx_ring(struct mlx4_en_priv *priv,
  161. struct mlx4_en_tx_ring *ring)
  162. {
  163. struct mlx4_en_dev *mdev = priv->mdev;
  164. mlx4_qp_modify(mdev->dev, NULL, ring->qp_state,
  165. MLX4_QP_STATE_RST, NULL, 0, 0, &ring->qp);
  166. }
  167. static u32 mlx4_en_free_tx_desc(struct mlx4_en_priv *priv,
  168. struct mlx4_en_tx_ring *ring,
  169. int index, u8 owner)
  170. {
  171. struct mlx4_en_dev *mdev = priv->mdev;
  172. struct mlx4_en_tx_info *tx_info = &ring->tx_info[index];
  173. struct mlx4_en_tx_desc *tx_desc = ring->buf + index * TXBB_SIZE;
  174. struct mlx4_wqe_data_seg *data = (void *) tx_desc + tx_info->data_offset;
  175. struct sk_buff *skb = tx_info->skb;
  176. struct skb_frag_struct *frag;
  177. void *end = ring->buf + ring->buf_size;
  178. int frags = skb_shinfo(skb)->nr_frags;
  179. int i;
  180. __be32 *ptr = (__be32 *)tx_desc;
  181. __be32 stamp = cpu_to_be32(STAMP_VAL | (!!owner << STAMP_SHIFT));
  182. /* Optimize the common case when there are no wraparounds */
  183. if (likely((void *) tx_desc + tx_info->nr_txbb * TXBB_SIZE <= end)) {
  184. if (!tx_info->inl) {
  185. if (tx_info->linear) {
  186. pci_unmap_single(mdev->pdev,
  187. (dma_addr_t) be64_to_cpu(data->addr),
  188. be32_to_cpu(data->byte_count),
  189. PCI_DMA_TODEVICE);
  190. ++data;
  191. }
  192. for (i = 0; i < frags; i++) {
  193. frag = &skb_shinfo(skb)->frags[i];
  194. pci_unmap_page(mdev->pdev,
  195. (dma_addr_t) be64_to_cpu(data[i].addr),
  196. frag->size, PCI_DMA_TODEVICE);
  197. }
  198. }
  199. /* Stamp the freed descriptor */
  200. for (i = 0; i < tx_info->nr_txbb * TXBB_SIZE; i += STAMP_STRIDE) {
  201. *ptr = stamp;
  202. ptr += STAMP_DWORDS;
  203. }
  204. } else {
  205. if (!tx_info->inl) {
  206. if ((void *) data >= end) {
  207. data = (struct mlx4_wqe_data_seg *)
  208. (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 = (struct mlx4_wqe_data_seg *) 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. frag->size, 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 = cq->buf;
  271. u16 index;
  272. u16 new_index;
  273. u32 txbbs_skipped = 0;
  274. u32 cq_last_sav;
  275. /* index always points to the first TXBB of the last polled descriptor */
  276. index = ring->cons & ring->size_mask;
  277. new_index = be16_to_cpu(cqe->wqe_index) & ring->size_mask;
  278. if (index == new_index)
  279. return;
  280. if (!priv->port_up)
  281. return;
  282. /*
  283. * We use a two-stage loop:
  284. * - the first samples the HW-updated CQE
  285. * - the second frees TXBBs until the last sample
  286. * This lets us amortize CQE cache misses, while still polling the CQ
  287. * until is quiescent.
  288. */
  289. cq_last_sav = mcq->cons_index;
  290. do {
  291. do {
  292. /* Skip over last polled CQE */
  293. index = (index + ring->last_nr_txbb) & ring->size_mask;
  294. txbbs_skipped += ring->last_nr_txbb;
  295. /* Poll next CQE */
  296. ring->last_nr_txbb = mlx4_en_free_tx_desc(
  297. priv, ring, index,
  298. !!((ring->cons + txbbs_skipped) &
  299. ring->size));
  300. ++mcq->cons_index;
  301. } while (index != new_index);
  302. new_index = be16_to_cpu(cqe->wqe_index) & ring->size_mask;
  303. } while (index != new_index);
  304. AVG_PERF_COUNTER(priv->pstats.tx_coal_avg,
  305. (u32) (mcq->cons_index - cq_last_sav));
  306. /*
  307. * To prevent CQ overflow we first update CQ consumer and only then
  308. * the ring consumer.
  309. */
  310. mlx4_cq_set_ci(mcq);
  311. wmb();
  312. ring->cons += txbbs_skipped;
  313. /* Wakeup Tx queue if this ring stopped it */
  314. if (unlikely(ring->blocked)) {
  315. if ((u32) (ring->prod - ring->cons) <=
  316. ring->size - HEADROOM - MAX_DESC_TXBBS) {
  317. ring->blocked = 0;
  318. netif_tx_wake_queue(netdev_get_tx_queue(dev, cq->ring));
  319. priv->port_stats.wake_queue++;
  320. }
  321. }
  322. }
  323. void mlx4_en_tx_irq(struct mlx4_cq *mcq)
  324. {
  325. struct mlx4_en_cq *cq = container_of(mcq, struct mlx4_en_cq, mcq);
  326. struct mlx4_en_priv *priv = netdev_priv(cq->dev);
  327. struct mlx4_en_tx_ring *ring = &priv->tx_ring[cq->ring];
  328. if (!spin_trylock(&ring->comp_lock))
  329. return;
  330. mlx4_en_process_tx_cq(cq->dev, cq);
  331. mod_timer(&cq->timer, jiffies + 1);
  332. spin_unlock(&ring->comp_lock);
  333. }
  334. void mlx4_en_poll_tx_cq(unsigned long data)
  335. {
  336. struct mlx4_en_cq *cq = (struct mlx4_en_cq *) data;
  337. struct mlx4_en_priv *priv = netdev_priv(cq->dev);
  338. struct mlx4_en_tx_ring *ring = &priv->tx_ring[cq->ring];
  339. u32 inflight;
  340. INC_PERF_COUNTER(priv->pstats.tx_poll);
  341. if (!spin_trylock_irq(&ring->comp_lock)) {
  342. mod_timer(&cq->timer, jiffies + MLX4_EN_TX_POLL_TIMEOUT);
  343. return;
  344. }
  345. mlx4_en_process_tx_cq(cq->dev, cq);
  346. inflight = (u32) (ring->prod - ring->cons - ring->last_nr_txbb);
  347. /* If there are still packets in flight and the timer has not already
  348. * been scheduled by the Tx routine then schedule it here to guarantee
  349. * completion processing of these packets */
  350. if (inflight && priv->port_up)
  351. mod_timer(&cq->timer, jiffies + MLX4_EN_TX_POLL_TIMEOUT);
  352. spin_unlock_irq(&ring->comp_lock);
  353. }
  354. static struct mlx4_en_tx_desc *mlx4_en_bounce_to_desc(struct mlx4_en_priv *priv,
  355. struct mlx4_en_tx_ring *ring,
  356. u32 index,
  357. unsigned int desc_size)
  358. {
  359. u32 copy = (ring->size - index) * TXBB_SIZE;
  360. int i;
  361. for (i = desc_size - copy - 4; i >= 0; i -= 4) {
  362. if ((i & (TXBB_SIZE - 1)) == 0)
  363. wmb();
  364. *((u32 *) (ring->buf + i)) =
  365. *((u32 *) (ring->bounce_buf + copy + i));
  366. }
  367. for (i = copy - 4; i >= 4 ; i -= 4) {
  368. if ((i & (TXBB_SIZE - 1)) == 0)
  369. wmb();
  370. *((u32 *) (ring->buf + index * TXBB_SIZE + i)) =
  371. *((u32 *) (ring->bounce_buf + i));
  372. }
  373. /* Return real descriptor location */
  374. return ring->buf + index * TXBB_SIZE;
  375. }
  376. static inline void mlx4_en_xmit_poll(struct mlx4_en_priv *priv, int tx_ind)
  377. {
  378. struct mlx4_en_cq *cq = &priv->tx_cq[tx_ind];
  379. struct mlx4_en_tx_ring *ring = &priv->tx_ring[tx_ind];
  380. unsigned long flags;
  381. /* If we don't have a pending timer, set one up to catch our recent
  382. post in case the interface becomes idle */
  383. if (!timer_pending(&cq->timer))
  384. mod_timer(&cq->timer, jiffies + MLX4_EN_TX_POLL_TIMEOUT);
  385. /* Poll the CQ every mlx4_en_TX_MODER_POLL packets */
  386. if ((++ring->poll_cnt & (MLX4_EN_TX_POLL_MODER - 1)) == 0)
  387. if (spin_trylock_irqsave(&ring->comp_lock, flags)) {
  388. mlx4_en_process_tx_cq(priv->dev, cq);
  389. spin_unlock_irqrestore(&ring->comp_lock, flags);
  390. }
  391. }
  392. static void *get_frag_ptr(struct sk_buff *skb)
  393. {
  394. struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0];
  395. struct page *page = frag->page;
  396. void *ptr;
  397. ptr = page_address(page);
  398. if (unlikely(!ptr))
  399. return NULL;
  400. return ptr + frag->page_offset;
  401. }
  402. static int is_inline(struct sk_buff *skb, void **pfrag)
  403. {
  404. void *ptr;
  405. if (inline_thold && !skb_is_gso(skb) && skb->len <= inline_thold) {
  406. if (skb_shinfo(skb)->nr_frags == 1) {
  407. ptr = get_frag_ptr(skb);
  408. if (unlikely(!ptr))
  409. return 0;
  410. if (pfrag)
  411. *pfrag = ptr;
  412. return 1;
  413. } else if (unlikely(skb_shinfo(skb)->nr_frags))
  414. return 0;
  415. else
  416. return 1;
  417. }
  418. return 0;
  419. }
  420. static int inline_size(struct sk_buff *skb)
  421. {
  422. if (skb->len + CTRL_SIZE + sizeof(struct mlx4_wqe_inline_seg)
  423. <= MLX4_INLINE_ALIGN)
  424. return ALIGN(skb->len + CTRL_SIZE +
  425. sizeof(struct mlx4_wqe_inline_seg), 16);
  426. else
  427. return ALIGN(skb->len + CTRL_SIZE + 2 *
  428. sizeof(struct mlx4_wqe_inline_seg), 16);
  429. }
  430. static int get_real_size(struct sk_buff *skb, struct net_device *dev,
  431. int *lso_header_size)
  432. {
  433. struct mlx4_en_priv *priv = netdev_priv(dev);
  434. int real_size;
  435. if (skb_is_gso(skb)) {
  436. *lso_header_size = skb_transport_offset(skb) + tcp_hdrlen(skb);
  437. real_size = CTRL_SIZE + skb_shinfo(skb)->nr_frags * DS_SIZE +
  438. ALIGN(*lso_header_size + 4, DS_SIZE);
  439. if (unlikely(*lso_header_size != skb_headlen(skb))) {
  440. /* We add a segment for the skb linear buffer only if
  441. * it contains data */
  442. if (*lso_header_size < skb_headlen(skb))
  443. real_size += DS_SIZE;
  444. else {
  445. if (netif_msg_tx_err(priv))
  446. en_warn(priv, "Non-linear headers\n");
  447. return 0;
  448. }
  449. }
  450. } else {
  451. *lso_header_size = 0;
  452. if (!is_inline(skb, NULL))
  453. real_size = CTRL_SIZE + (skb_shinfo(skb)->nr_frags + 1) * DS_SIZE;
  454. else
  455. real_size = inline_size(skb);
  456. }
  457. return real_size;
  458. }
  459. static void build_inline_wqe(struct mlx4_en_tx_desc *tx_desc, struct sk_buff *skb,
  460. int real_size, u16 *vlan_tag, int tx_ind, void *fragptr)
  461. {
  462. struct mlx4_wqe_inline_seg *inl = &tx_desc->inl;
  463. int spc = MLX4_INLINE_ALIGN - CTRL_SIZE - sizeof *inl;
  464. if (skb->len <= spc) {
  465. inl->byte_count = cpu_to_be32(1 << 31 | skb->len);
  466. skb_copy_from_linear_data(skb, inl + 1, skb_headlen(skb));
  467. if (skb_shinfo(skb)->nr_frags)
  468. memcpy(((void *)(inl + 1)) + skb_headlen(skb), fragptr,
  469. skb_shinfo(skb)->frags[0].size);
  470. } else {
  471. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  472. if (skb_headlen(skb) <= spc) {
  473. skb_copy_from_linear_data(skb, inl + 1, skb_headlen(skb));
  474. if (skb_headlen(skb) < spc) {
  475. memcpy(((void *)(inl + 1)) + skb_headlen(skb),
  476. fragptr, spc - skb_headlen(skb));
  477. fragptr += spc - skb_headlen(skb);
  478. }
  479. inl = (void *) (inl + 1) + spc;
  480. memcpy(((void *)(inl + 1)), fragptr, skb->len - spc);
  481. } else {
  482. skb_copy_from_linear_data(skb, inl + 1, spc);
  483. inl = (void *) (inl + 1) + spc;
  484. skb_copy_from_linear_data_offset(skb, spc, inl + 1,
  485. skb_headlen(skb) - spc);
  486. if (skb_shinfo(skb)->nr_frags)
  487. memcpy(((void *)(inl + 1)) + skb_headlen(skb) - spc,
  488. fragptr, skb_shinfo(skb)->frags[0].size);
  489. }
  490. wmb();
  491. inl->byte_count = cpu_to_be32(1 << 31 | (skb->len - spc));
  492. }
  493. tx_desc->ctrl.vlan_tag = cpu_to_be16(*vlan_tag);
  494. tx_desc->ctrl.ins_vlan = MLX4_WQE_CTRL_INS_VLAN * !!(*vlan_tag);
  495. tx_desc->ctrl.fence_size = (real_size / 16) & 0x3f;
  496. }
  497. u16 mlx4_en_select_queue(struct net_device *dev, struct sk_buff *skb)
  498. {
  499. struct mlx4_en_priv *priv = netdev_priv(dev);
  500. u16 vlan_tag = 0;
  501. /* If we support per priority flow control and the packet contains
  502. * a vlan tag, send the packet to the TX ring assigned to that priority
  503. */
  504. if (priv->prof->rx_ppp && vlan_tx_tag_present(skb)) {
  505. vlan_tag = vlan_tx_tag_get(skb);
  506. return MLX4_EN_NUM_TX_RINGS + (vlan_tag >> 13);
  507. }
  508. return skb_tx_hash(dev, skb);
  509. }
  510. static void mlx4_bf_copy(unsigned long *dst, unsigned long *src, unsigned bytecnt)
  511. {
  512. __iowrite64_copy(dst, src, bytecnt / 8);
  513. }
  514. netdev_tx_t mlx4_en_xmit(struct sk_buff *skb, struct net_device *dev)
  515. {
  516. struct mlx4_en_priv *priv = netdev_priv(dev);
  517. struct mlx4_en_dev *mdev = priv->mdev;
  518. struct mlx4_en_tx_ring *ring;
  519. struct mlx4_en_cq *cq;
  520. struct mlx4_en_tx_desc *tx_desc;
  521. struct mlx4_wqe_data_seg *data;
  522. struct skb_frag_struct *frag;
  523. struct mlx4_en_tx_info *tx_info;
  524. struct ethhdr *ethh;
  525. u64 mac;
  526. u32 mac_l, mac_h;
  527. int tx_ind = 0;
  528. int nr_txbb;
  529. int desc_size;
  530. int real_size;
  531. dma_addr_t dma;
  532. u32 index, bf_index;
  533. __be32 op_own;
  534. u16 vlan_tag = 0;
  535. int i;
  536. int lso_header_size;
  537. void *fragptr;
  538. bool bounce = false;
  539. if (!priv->port_up)
  540. goto tx_drop;
  541. real_size = get_real_size(skb, dev, &lso_header_size);
  542. if (unlikely(!real_size))
  543. goto tx_drop;
  544. /* Align descriptor to TXBB size */
  545. desc_size = ALIGN(real_size, TXBB_SIZE);
  546. nr_txbb = desc_size / TXBB_SIZE;
  547. if (unlikely(nr_txbb > MAX_DESC_TXBBS)) {
  548. if (netif_msg_tx_err(priv))
  549. en_warn(priv, "Oversized header or SG list\n");
  550. goto tx_drop;
  551. }
  552. tx_ind = skb->queue_mapping;
  553. ring = &priv->tx_ring[tx_ind];
  554. if (vlan_tx_tag_present(skb))
  555. vlan_tag = vlan_tx_tag_get(skb);
  556. /* Check available TXBBs And 2K spare for prefetch */
  557. if (unlikely(((int)(ring->prod - ring->cons)) >
  558. ring->size - HEADROOM - MAX_DESC_TXBBS)) {
  559. /* every full Tx ring stops queue */
  560. netif_tx_stop_queue(netdev_get_tx_queue(dev, tx_ind));
  561. ring->blocked = 1;
  562. priv->port_stats.queue_stopped++;
  563. /* Use interrupts to find out when queue opened */
  564. cq = &priv->tx_cq[tx_ind];
  565. mlx4_en_arm_cq(priv, cq);
  566. return NETDEV_TX_BUSY;
  567. }
  568. /* Track current inflight packets for performance analysis */
  569. AVG_PERF_COUNTER(priv->pstats.inflight_avg,
  570. (u32) (ring->prod - ring->cons - 1));
  571. /* Packet is good - grab an index and transmit it */
  572. index = ring->prod & ring->size_mask;
  573. bf_index = ring->prod;
  574. /* See if we have enough space for whole descriptor TXBB for setting
  575. * SW ownership on next descriptor; if not, use a bounce buffer. */
  576. if (likely(index + nr_txbb <= ring->size))
  577. tx_desc = ring->buf + index * TXBB_SIZE;
  578. else {
  579. tx_desc = (struct mlx4_en_tx_desc *) ring->bounce_buf;
  580. bounce = true;
  581. }
  582. /* Save skb in tx_info ring */
  583. tx_info = &ring->tx_info[index];
  584. tx_info->skb = skb;
  585. tx_info->nr_txbb = nr_txbb;
  586. /* Prepare ctrl segement apart opcode+ownership, which depends on
  587. * whether LSO is used */
  588. tx_desc->ctrl.vlan_tag = cpu_to_be16(vlan_tag);
  589. tx_desc->ctrl.ins_vlan = MLX4_WQE_CTRL_INS_VLAN * !!vlan_tag;
  590. tx_desc->ctrl.fence_size = (real_size / 16) & 0x3f;
  591. tx_desc->ctrl.srcrb_flags = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE |
  592. MLX4_WQE_CTRL_SOLICITED);
  593. if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
  594. tx_desc->ctrl.srcrb_flags |= cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM |
  595. MLX4_WQE_CTRL_TCP_UDP_CSUM);
  596. priv->port_stats.tx_chksum_offload++;
  597. }
  598. if (unlikely(priv->validate_loopback)) {
  599. /* Copy dst mac address to wqe */
  600. skb_reset_mac_header(skb);
  601. ethh = eth_hdr(skb);
  602. if (ethh && ethh->h_dest) {
  603. mac = mlx4_en_mac_to_u64(ethh->h_dest);
  604. mac_h = (u32) ((mac & 0xffff00000000ULL) >> 16);
  605. mac_l = (u32) (mac & 0xffffffff);
  606. tx_desc->ctrl.srcrb_flags |= cpu_to_be32(mac_h);
  607. tx_desc->ctrl.imm = cpu_to_be32(mac_l);
  608. }
  609. }
  610. /* Handle LSO (TSO) packets */
  611. if (lso_header_size) {
  612. /* Mark opcode as LSO */
  613. op_own = cpu_to_be32(MLX4_OPCODE_LSO | (1 << 6)) |
  614. ((ring->prod & ring->size) ?
  615. cpu_to_be32(MLX4_EN_BIT_DESC_OWN) : 0);
  616. /* Fill in the LSO prefix */
  617. tx_desc->lso.mss_hdr_size = cpu_to_be32(
  618. skb_shinfo(skb)->gso_size << 16 | lso_header_size);
  619. /* Copy headers;
  620. * note that we already verified that it is linear */
  621. memcpy(tx_desc->lso.header, skb->data, lso_header_size);
  622. data = ((void *) &tx_desc->lso +
  623. ALIGN(lso_header_size + 4, DS_SIZE));
  624. priv->port_stats.tso_packets++;
  625. i = ((skb->len - lso_header_size) / skb_shinfo(skb)->gso_size) +
  626. !!((skb->len - lso_header_size) % skb_shinfo(skb)->gso_size);
  627. ring->bytes += skb->len + (i - 1) * lso_header_size;
  628. ring->packets += i;
  629. } else {
  630. /* Normal (Non LSO) packet */
  631. op_own = cpu_to_be32(MLX4_OPCODE_SEND) |
  632. ((ring->prod & ring->size) ?
  633. cpu_to_be32(MLX4_EN_BIT_DESC_OWN) : 0);
  634. data = &tx_desc->data;
  635. ring->bytes += max(skb->len, (unsigned int) ETH_ZLEN);
  636. ring->packets++;
  637. }
  638. AVG_PERF_COUNTER(priv->pstats.tx_pktsz_avg, skb->len);
  639. /* valid only for none inline segments */
  640. tx_info->data_offset = (void *) data - (void *) tx_desc;
  641. tx_info->linear = (lso_header_size < skb_headlen(skb) && !is_inline(skb, NULL)) ? 1 : 0;
  642. data += skb_shinfo(skb)->nr_frags + tx_info->linear - 1;
  643. if (!is_inline(skb, &fragptr)) {
  644. /* Map fragments */
  645. for (i = skb_shinfo(skb)->nr_frags - 1; i >= 0; i--) {
  646. frag = &skb_shinfo(skb)->frags[i];
  647. dma = pci_map_page(mdev->dev->pdev, frag->page, frag->page_offset,
  648. frag->size, PCI_DMA_TODEVICE);
  649. data->addr = cpu_to_be64(dma);
  650. data->lkey = cpu_to_be32(mdev->mr.key);
  651. wmb();
  652. data->byte_count = cpu_to_be32(frag->size);
  653. --data;
  654. }
  655. /* Map linear part */
  656. if (tx_info->linear) {
  657. dma = pci_map_single(mdev->dev->pdev, skb->data + lso_header_size,
  658. skb_headlen(skb) - lso_header_size, PCI_DMA_TODEVICE);
  659. data->addr = cpu_to_be64(dma);
  660. data->lkey = cpu_to_be32(mdev->mr.key);
  661. wmb();
  662. data->byte_count = cpu_to_be32(skb_headlen(skb) - lso_header_size);
  663. }
  664. tx_info->inl = 0;
  665. } else {
  666. build_inline_wqe(tx_desc, skb, real_size, &vlan_tag, tx_ind, fragptr);
  667. tx_info->inl = 1;
  668. }
  669. ring->prod += nr_txbb;
  670. /* If we used a bounce buffer then copy descriptor back into place */
  671. if (bounce)
  672. tx_desc = mlx4_en_bounce_to_desc(priv, ring, index, desc_size);
  673. /* Run destructor before passing skb to HW */
  674. if (likely(!skb_shared(skb)))
  675. skb_orphan(skb);
  676. if (ring->bf_enabled && desc_size <= MAX_BF && !bounce && !vlan_tag) {
  677. *(u32 *) (&tx_desc->ctrl.vlan_tag) |= ring->doorbell_qpn;
  678. op_own |= htonl((bf_index & 0xffff) << 8);
  679. /* Ensure new descirptor hits memory
  680. * before setting ownership of this descriptor to HW */
  681. wmb();
  682. tx_desc->ctrl.owner_opcode = op_own;
  683. wmb();
  684. mlx4_bf_copy(ring->bf.reg + ring->bf.offset, (unsigned long *) &tx_desc->ctrl,
  685. desc_size);
  686. wmb();
  687. ring->bf.offset ^= ring->bf.buf_size;
  688. } else {
  689. /* Ensure new descirptor hits memory
  690. * before setting ownership of this descriptor to HW */
  691. wmb();
  692. tx_desc->ctrl.owner_opcode = op_own;
  693. wmb();
  694. writel(ring->doorbell_qpn, ring->bf.uar->map + MLX4_SEND_DOORBELL);
  695. }
  696. /* Poll CQ here */
  697. mlx4_en_xmit_poll(priv, tx_ind);
  698. return NETDEV_TX_OK;
  699. tx_drop:
  700. dev_kfree_skb_any(skb);
  701. priv->stats.tx_dropped++;
  702. return NETDEV_TX_OK;
  703. }