en_tx.c 22 KB

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