en_tx.c 23 KB

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