en_tx.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  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/prefetch.h>
  40. #include <linux/vmalloc.h>
  41. #include <linux/tcp.h>
  42. #include <linux/ip.h>
  43. #include <linux/ipv6.h>
  44. #include <linux/moduleparam.h>
  45. #include "mlx4_en.h"
  46. int mlx4_en_create_tx_ring(struct mlx4_en_priv *priv,
  47. struct mlx4_en_tx_ring **pring, u32 size,
  48. u16 stride, int node, int queue_index)
  49. {
  50. struct mlx4_en_dev *mdev = priv->mdev;
  51. struct mlx4_en_tx_ring *ring;
  52. int tmp;
  53. int err;
  54. ring = kzalloc_node(sizeof(*ring), GFP_KERNEL, node);
  55. if (!ring) {
  56. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  57. if (!ring) {
  58. en_err(priv, "Failed allocating TX ring\n");
  59. return -ENOMEM;
  60. }
  61. }
  62. ring->size = size;
  63. ring->size_mask = size - 1;
  64. ring->sp_stride = stride;
  65. ring->full_size = ring->size - HEADROOM - MAX_DESC_TXBBS;
  66. tmp = size * sizeof(struct mlx4_en_tx_info);
  67. ring->tx_info = kmalloc_node(tmp, GFP_KERNEL | __GFP_NOWARN, node);
  68. if (!ring->tx_info) {
  69. ring->tx_info = vmalloc(tmp);
  70. if (!ring->tx_info) {
  71. err = -ENOMEM;
  72. goto err_ring;
  73. }
  74. }
  75. en_dbg(DRV, priv, "Allocated tx_info ring at addr:%p size:%d\n",
  76. ring->tx_info, tmp);
  77. ring->bounce_buf = kmalloc_node(MAX_DESC_SIZE, GFP_KERNEL, node);
  78. if (!ring->bounce_buf) {
  79. ring->bounce_buf = kmalloc(MAX_DESC_SIZE, GFP_KERNEL);
  80. if (!ring->bounce_buf) {
  81. err = -ENOMEM;
  82. goto err_info;
  83. }
  84. }
  85. ring->buf_size = ALIGN(size * ring->sp_stride, MLX4_EN_PAGE_SIZE);
  86. /* Allocate HW buffers on provided NUMA node */
  87. set_dev_node(&mdev->dev->persist->pdev->dev, node);
  88. err = mlx4_alloc_hwq_res(mdev->dev, &ring->sp_wqres, ring->buf_size);
  89. set_dev_node(&mdev->dev->persist->pdev->dev, mdev->dev->numa_node);
  90. if (err) {
  91. en_err(priv, "Failed allocating hwq resources\n");
  92. goto err_bounce;
  93. }
  94. ring->buf = ring->sp_wqres.buf.direct.buf;
  95. en_dbg(DRV, priv, "Allocated TX ring (addr:%p) - buf:%p size:%d buf_size:%d dma:%llx\n",
  96. ring, ring->buf, ring->size, ring->buf_size,
  97. (unsigned long long) ring->sp_wqres.buf.direct.map);
  98. err = mlx4_qp_reserve_range(mdev->dev, 1, 1, &ring->qpn,
  99. MLX4_RESERVE_ETH_BF_QP);
  100. if (err) {
  101. en_err(priv, "failed reserving qp for TX ring\n");
  102. goto err_hwq_res;
  103. }
  104. err = mlx4_qp_alloc(mdev->dev, ring->qpn, &ring->sp_qp, GFP_KERNEL);
  105. if (err) {
  106. en_err(priv, "Failed allocating qp %d\n", ring->qpn);
  107. goto err_reserve;
  108. }
  109. ring->sp_qp.event = mlx4_en_sqp_event;
  110. err = mlx4_bf_alloc(mdev->dev, &ring->bf, node);
  111. if (err) {
  112. en_dbg(DRV, priv, "working without blueflame (%d)\n", err);
  113. ring->bf.uar = &mdev->priv_uar;
  114. ring->bf.uar->map = mdev->uar_map;
  115. ring->bf_enabled = false;
  116. ring->bf_alloced = false;
  117. priv->pflags &= ~MLX4_EN_PRIV_FLAGS_BLUEFLAME;
  118. } else {
  119. ring->bf_alloced = true;
  120. ring->bf_enabled = !!(priv->pflags &
  121. MLX4_EN_PRIV_FLAGS_BLUEFLAME);
  122. }
  123. ring->hwtstamp_tx_type = priv->hwtstamp_config.tx_type;
  124. ring->queue_index = queue_index;
  125. if (queue_index < priv->num_tx_rings_p_up)
  126. cpumask_set_cpu(cpumask_local_spread(queue_index,
  127. priv->mdev->dev->numa_node),
  128. &ring->sp_affinity_mask);
  129. *pring = ring;
  130. return 0;
  131. err_reserve:
  132. mlx4_qp_release_range(mdev->dev, ring->qpn, 1);
  133. err_hwq_res:
  134. mlx4_free_hwq_res(mdev->dev, &ring->sp_wqres, ring->buf_size);
  135. err_bounce:
  136. kfree(ring->bounce_buf);
  137. ring->bounce_buf = NULL;
  138. err_info:
  139. kvfree(ring->tx_info);
  140. ring->tx_info = NULL;
  141. err_ring:
  142. kfree(ring);
  143. *pring = NULL;
  144. return err;
  145. }
  146. void mlx4_en_destroy_tx_ring(struct mlx4_en_priv *priv,
  147. struct mlx4_en_tx_ring **pring)
  148. {
  149. struct mlx4_en_dev *mdev = priv->mdev;
  150. struct mlx4_en_tx_ring *ring = *pring;
  151. en_dbg(DRV, priv, "Destroying tx ring, qpn: %d\n", ring->qpn);
  152. if (ring->bf_alloced)
  153. mlx4_bf_free(mdev->dev, &ring->bf);
  154. mlx4_qp_remove(mdev->dev, &ring->sp_qp);
  155. mlx4_qp_free(mdev->dev, &ring->sp_qp);
  156. mlx4_qp_release_range(priv->mdev->dev, ring->qpn, 1);
  157. mlx4_free_hwq_res(mdev->dev, &ring->sp_wqres, ring->buf_size);
  158. kfree(ring->bounce_buf);
  159. ring->bounce_buf = NULL;
  160. kvfree(ring->tx_info);
  161. ring->tx_info = NULL;
  162. kfree(ring);
  163. *pring = NULL;
  164. }
  165. int mlx4_en_activate_tx_ring(struct mlx4_en_priv *priv,
  166. struct mlx4_en_tx_ring *ring,
  167. int cq, int user_prio)
  168. {
  169. struct mlx4_en_dev *mdev = priv->mdev;
  170. int err;
  171. ring->sp_cqn = cq;
  172. ring->prod = 0;
  173. ring->cons = 0xffffffff;
  174. ring->last_nr_txbb = 1;
  175. memset(ring->tx_info, 0, ring->size * sizeof(struct mlx4_en_tx_info));
  176. memset(ring->buf, 0, ring->buf_size);
  177. ring->free_tx_desc = mlx4_en_free_tx_desc;
  178. ring->sp_qp_state = MLX4_QP_STATE_RST;
  179. ring->doorbell_qpn = cpu_to_be32(ring->sp_qp.qpn << 8);
  180. ring->mr_key = cpu_to_be32(mdev->mr.key);
  181. mlx4_en_fill_qp_context(priv, ring->size, ring->sp_stride, 1, 0, ring->qpn,
  182. ring->sp_cqn, user_prio, &ring->sp_context);
  183. if (ring->bf_alloced)
  184. ring->sp_context.usr_page =
  185. cpu_to_be32(mlx4_to_hw_uar_index(mdev->dev,
  186. ring->bf.uar->index));
  187. err = mlx4_qp_to_ready(mdev->dev, &ring->sp_wqres.mtt, &ring->sp_context,
  188. &ring->sp_qp, &ring->sp_qp_state);
  189. if (!cpumask_empty(&ring->sp_affinity_mask))
  190. netif_set_xps_queue(priv->dev, &ring->sp_affinity_mask,
  191. ring->queue_index);
  192. return err;
  193. }
  194. void mlx4_en_deactivate_tx_ring(struct mlx4_en_priv *priv,
  195. struct mlx4_en_tx_ring *ring)
  196. {
  197. struct mlx4_en_dev *mdev = priv->mdev;
  198. mlx4_qp_modify(mdev->dev, NULL, ring->sp_qp_state,
  199. MLX4_QP_STATE_RST, NULL, 0, 0, &ring->sp_qp);
  200. }
  201. static inline bool mlx4_en_is_tx_ring_full(struct mlx4_en_tx_ring *ring)
  202. {
  203. return ring->prod - ring->cons > ring->full_size;
  204. }
  205. static void mlx4_en_stamp_wqe(struct mlx4_en_priv *priv,
  206. struct mlx4_en_tx_ring *ring, int index,
  207. u8 owner)
  208. {
  209. __be32 stamp = cpu_to_be32(STAMP_VAL | (!!owner << STAMP_SHIFT));
  210. struct mlx4_en_tx_desc *tx_desc = ring->buf + index * TXBB_SIZE;
  211. struct mlx4_en_tx_info *tx_info = &ring->tx_info[index];
  212. void *end = ring->buf + ring->buf_size;
  213. __be32 *ptr = (__be32 *)tx_desc;
  214. int i;
  215. /* Optimize the common case when there are no wraparounds */
  216. if (likely((void *)tx_desc + tx_info->nr_txbb * TXBB_SIZE <= end)) {
  217. /* Stamp the freed descriptor */
  218. for (i = 0; i < tx_info->nr_txbb * TXBB_SIZE;
  219. i += STAMP_STRIDE) {
  220. *ptr = stamp;
  221. ptr += STAMP_DWORDS;
  222. }
  223. } else {
  224. /* Stamp the freed descriptor */
  225. for (i = 0; i < tx_info->nr_txbb * TXBB_SIZE;
  226. i += STAMP_STRIDE) {
  227. *ptr = stamp;
  228. ptr += STAMP_DWORDS;
  229. if ((void *)ptr >= end) {
  230. ptr = ring->buf;
  231. stamp ^= cpu_to_be32(0x80000000);
  232. }
  233. }
  234. }
  235. }
  236. u32 mlx4_en_free_tx_desc(struct mlx4_en_priv *priv,
  237. struct mlx4_en_tx_ring *ring,
  238. int index, u8 owner, u64 timestamp,
  239. int napi_mode)
  240. {
  241. struct mlx4_en_tx_info *tx_info = &ring->tx_info[index];
  242. struct mlx4_en_tx_desc *tx_desc = ring->buf + index * TXBB_SIZE;
  243. struct mlx4_wqe_data_seg *data = (void *) tx_desc + tx_info->data_offset;
  244. void *end = ring->buf + ring->buf_size;
  245. struct sk_buff *skb = tx_info->skb;
  246. int nr_maps = tx_info->nr_maps;
  247. int i;
  248. /* We do not touch skb here, so prefetch skb->users location
  249. * to speedup consume_skb()
  250. */
  251. prefetchw(&skb->users);
  252. if (unlikely(timestamp)) {
  253. struct skb_shared_hwtstamps hwts;
  254. mlx4_en_fill_hwtstamps(priv->mdev, &hwts, timestamp);
  255. skb_tstamp_tx(skb, &hwts);
  256. }
  257. /* Optimize the common case when there are no wraparounds */
  258. if (likely((void *) tx_desc + tx_info->nr_txbb * TXBB_SIZE <= end)) {
  259. if (!tx_info->inl) {
  260. if (tx_info->linear)
  261. dma_unmap_single(priv->ddev,
  262. tx_info->map0_dma,
  263. tx_info->map0_byte_count,
  264. PCI_DMA_TODEVICE);
  265. else
  266. dma_unmap_page(priv->ddev,
  267. tx_info->map0_dma,
  268. tx_info->map0_byte_count,
  269. PCI_DMA_TODEVICE);
  270. for (i = 1; i < nr_maps; i++) {
  271. data++;
  272. dma_unmap_page(priv->ddev,
  273. (dma_addr_t)be64_to_cpu(data->addr),
  274. be32_to_cpu(data->byte_count),
  275. PCI_DMA_TODEVICE);
  276. }
  277. }
  278. } else {
  279. if (!tx_info->inl) {
  280. if ((void *) data >= end) {
  281. data = ring->buf + ((void *)data - end);
  282. }
  283. if (tx_info->linear)
  284. dma_unmap_single(priv->ddev,
  285. tx_info->map0_dma,
  286. tx_info->map0_byte_count,
  287. PCI_DMA_TODEVICE);
  288. else
  289. dma_unmap_page(priv->ddev,
  290. tx_info->map0_dma,
  291. tx_info->map0_byte_count,
  292. PCI_DMA_TODEVICE);
  293. for (i = 1; i < nr_maps; i++) {
  294. data++;
  295. /* Check for wraparound before unmapping */
  296. if ((void *) data >= end)
  297. data = ring->buf;
  298. dma_unmap_page(priv->ddev,
  299. (dma_addr_t)be64_to_cpu(data->addr),
  300. be32_to_cpu(data->byte_count),
  301. PCI_DMA_TODEVICE);
  302. }
  303. }
  304. }
  305. napi_consume_skb(skb, napi_mode);
  306. return tx_info->nr_txbb;
  307. }
  308. u32 mlx4_en_recycle_tx_desc(struct mlx4_en_priv *priv,
  309. struct mlx4_en_tx_ring *ring,
  310. int index, u8 owner, u64 timestamp,
  311. int napi_mode)
  312. {
  313. struct mlx4_en_tx_info *tx_info = &ring->tx_info[index];
  314. struct mlx4_en_rx_alloc frame = {
  315. .page = tx_info->page,
  316. .dma = tx_info->map0_dma,
  317. .page_offset = XDP_PACKET_HEADROOM,
  318. .page_size = PAGE_SIZE,
  319. };
  320. if (!mlx4_en_rx_recycle(ring->recycle_ring, &frame)) {
  321. dma_unmap_page(priv->ddev, tx_info->map0_dma,
  322. PAGE_SIZE, priv->frag_info[0].dma_dir);
  323. put_page(tx_info->page);
  324. }
  325. return tx_info->nr_txbb;
  326. }
  327. int mlx4_en_free_tx_buf(struct net_device *dev, struct mlx4_en_tx_ring *ring)
  328. {
  329. struct mlx4_en_priv *priv = netdev_priv(dev);
  330. int cnt = 0;
  331. /* Skip last polled descriptor */
  332. ring->cons += ring->last_nr_txbb;
  333. en_dbg(DRV, priv, "Freeing Tx buf - cons:0x%x prod:0x%x\n",
  334. ring->cons, ring->prod);
  335. if ((u32) (ring->prod - ring->cons) > ring->size) {
  336. if (netif_msg_tx_err(priv))
  337. en_warn(priv, "Tx consumer passed producer!\n");
  338. return 0;
  339. }
  340. while (ring->cons != ring->prod) {
  341. ring->last_nr_txbb = ring->free_tx_desc(priv, ring,
  342. ring->cons & ring->size_mask,
  343. !!(ring->cons & ring->size), 0,
  344. 0 /* Non-NAPI caller */);
  345. ring->cons += ring->last_nr_txbb;
  346. cnt++;
  347. }
  348. if (ring->tx_queue)
  349. netdev_tx_reset_queue(ring->tx_queue);
  350. if (cnt)
  351. en_dbg(DRV, priv, "Freed %d uncompleted tx descriptors\n", cnt);
  352. return cnt;
  353. }
  354. static bool mlx4_en_process_tx_cq(struct net_device *dev,
  355. struct mlx4_en_cq *cq, int napi_budget)
  356. {
  357. struct mlx4_en_priv *priv = netdev_priv(dev);
  358. struct mlx4_cq *mcq = &cq->mcq;
  359. struct mlx4_en_tx_ring *ring = priv->tx_ring[cq->type][cq->ring];
  360. struct mlx4_cqe *cqe;
  361. u16 index;
  362. u16 new_index, ring_index, stamp_index;
  363. u32 txbbs_skipped = 0;
  364. u32 txbbs_stamp = 0;
  365. u32 cons_index = mcq->cons_index;
  366. int size = cq->size;
  367. u32 size_mask = ring->size_mask;
  368. struct mlx4_cqe *buf = cq->buf;
  369. u32 packets = 0;
  370. u32 bytes = 0;
  371. int factor = priv->cqe_factor;
  372. int done = 0;
  373. int budget = priv->tx_work_limit;
  374. u32 last_nr_txbb;
  375. u32 ring_cons;
  376. if (!priv->port_up)
  377. return true;
  378. netdev_txq_bql_complete_prefetchw(ring->tx_queue);
  379. index = cons_index & size_mask;
  380. cqe = mlx4_en_get_cqe(buf, index, priv->cqe_size) + factor;
  381. last_nr_txbb = ACCESS_ONCE(ring->last_nr_txbb);
  382. ring_cons = ACCESS_ONCE(ring->cons);
  383. ring_index = ring_cons & size_mask;
  384. stamp_index = ring_index;
  385. /* Process all completed CQEs */
  386. while (XNOR(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK,
  387. cons_index & size) && (done < budget)) {
  388. /*
  389. * make sure we read the CQE after we read the
  390. * ownership bit
  391. */
  392. dma_rmb();
  393. if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
  394. MLX4_CQE_OPCODE_ERROR)) {
  395. struct mlx4_err_cqe *cqe_err = (struct mlx4_err_cqe *)cqe;
  396. en_err(priv, "CQE error - vendor syndrome: 0x%x syndrome: 0x%x\n",
  397. cqe_err->vendor_err_syndrome,
  398. cqe_err->syndrome);
  399. }
  400. /* Skip over last polled CQE */
  401. new_index = be16_to_cpu(cqe->wqe_index) & size_mask;
  402. do {
  403. u64 timestamp = 0;
  404. txbbs_skipped += last_nr_txbb;
  405. ring_index = (ring_index + last_nr_txbb) & size_mask;
  406. if (unlikely(ring->tx_info[ring_index].ts_requested))
  407. timestamp = mlx4_en_get_cqe_ts(cqe);
  408. /* free next descriptor */
  409. last_nr_txbb = ring->free_tx_desc(
  410. priv, ring, ring_index,
  411. !!((ring_cons + txbbs_skipped) &
  412. ring->size), timestamp, napi_budget);
  413. mlx4_en_stamp_wqe(priv, ring, stamp_index,
  414. !!((ring_cons + txbbs_stamp) &
  415. ring->size));
  416. stamp_index = ring_index;
  417. txbbs_stamp = txbbs_skipped;
  418. packets++;
  419. bytes += ring->tx_info[ring_index].nr_bytes;
  420. } while ((++done < budget) && (ring_index != new_index));
  421. ++cons_index;
  422. index = cons_index & size_mask;
  423. cqe = mlx4_en_get_cqe(buf, index, priv->cqe_size) + factor;
  424. }
  425. /*
  426. * To prevent CQ overflow we first update CQ consumer and only then
  427. * the ring consumer.
  428. */
  429. mcq->cons_index = cons_index;
  430. mlx4_cq_set_ci(mcq);
  431. wmb();
  432. /* we want to dirty this cache line once */
  433. ACCESS_ONCE(ring->last_nr_txbb) = last_nr_txbb;
  434. ACCESS_ONCE(ring->cons) = ring_cons + txbbs_skipped;
  435. if (ring->free_tx_desc == mlx4_en_recycle_tx_desc)
  436. return done < budget;
  437. netdev_tx_completed_queue(ring->tx_queue, packets, bytes);
  438. /* Wakeup Tx queue if this stopped, and ring is not full.
  439. */
  440. if (netif_tx_queue_stopped(ring->tx_queue) &&
  441. !mlx4_en_is_tx_ring_full(ring)) {
  442. netif_tx_wake_queue(ring->tx_queue);
  443. ring->wake_queue++;
  444. }
  445. return done < budget;
  446. }
  447. void mlx4_en_tx_irq(struct mlx4_cq *mcq)
  448. {
  449. struct mlx4_en_cq *cq = container_of(mcq, struct mlx4_en_cq, mcq);
  450. struct mlx4_en_priv *priv = netdev_priv(cq->dev);
  451. if (likely(priv->port_up))
  452. napi_schedule_irqoff(&cq->napi);
  453. else
  454. mlx4_en_arm_cq(priv, cq);
  455. }
  456. /* TX CQ polling - called by NAPI */
  457. int mlx4_en_poll_tx_cq(struct napi_struct *napi, int budget)
  458. {
  459. struct mlx4_en_cq *cq = container_of(napi, struct mlx4_en_cq, napi);
  460. struct net_device *dev = cq->dev;
  461. struct mlx4_en_priv *priv = netdev_priv(dev);
  462. int clean_complete;
  463. clean_complete = mlx4_en_process_tx_cq(dev, cq, budget);
  464. if (!clean_complete)
  465. return budget;
  466. napi_complete(napi);
  467. mlx4_en_arm_cq(priv, cq);
  468. return 0;
  469. }
  470. static struct mlx4_en_tx_desc *mlx4_en_bounce_to_desc(struct mlx4_en_priv *priv,
  471. struct mlx4_en_tx_ring *ring,
  472. u32 index,
  473. unsigned int desc_size)
  474. {
  475. u32 copy = (ring->size - index) * TXBB_SIZE;
  476. int i;
  477. for (i = desc_size - copy - 4; i >= 0; i -= 4) {
  478. if ((i & (TXBB_SIZE - 1)) == 0)
  479. wmb();
  480. *((u32 *) (ring->buf + i)) =
  481. *((u32 *) (ring->bounce_buf + copy + i));
  482. }
  483. for (i = copy - 4; i >= 4 ; i -= 4) {
  484. if ((i & (TXBB_SIZE - 1)) == 0)
  485. wmb();
  486. *((u32 *) (ring->buf + index * TXBB_SIZE + i)) =
  487. *((u32 *) (ring->bounce_buf + i));
  488. }
  489. /* Return real descriptor location */
  490. return ring->buf + index * TXBB_SIZE;
  491. }
  492. /* Decide if skb can be inlined in tx descriptor to avoid dma mapping
  493. *
  494. * It seems strange we do not simply use skb_copy_bits().
  495. * This would allow to inline all skbs iff skb->len <= inline_thold
  496. *
  497. * Note that caller already checked skb was not a gso packet
  498. */
  499. static bool is_inline(int inline_thold, const struct sk_buff *skb,
  500. const struct skb_shared_info *shinfo,
  501. void **pfrag)
  502. {
  503. void *ptr;
  504. if (skb->len > inline_thold || !inline_thold)
  505. return false;
  506. if (shinfo->nr_frags == 1) {
  507. ptr = skb_frag_address_safe(&shinfo->frags[0]);
  508. if (unlikely(!ptr))
  509. return false;
  510. *pfrag = ptr;
  511. return true;
  512. }
  513. if (shinfo->nr_frags)
  514. return false;
  515. return true;
  516. }
  517. static int inline_size(const struct sk_buff *skb)
  518. {
  519. if (skb->len + CTRL_SIZE + sizeof(struct mlx4_wqe_inline_seg)
  520. <= MLX4_INLINE_ALIGN)
  521. return ALIGN(skb->len + CTRL_SIZE +
  522. sizeof(struct mlx4_wqe_inline_seg), 16);
  523. else
  524. return ALIGN(skb->len + CTRL_SIZE + 2 *
  525. sizeof(struct mlx4_wqe_inline_seg), 16);
  526. }
  527. static int get_real_size(const struct sk_buff *skb,
  528. const struct skb_shared_info *shinfo,
  529. struct net_device *dev,
  530. int *lso_header_size,
  531. bool *inline_ok,
  532. void **pfrag)
  533. {
  534. struct mlx4_en_priv *priv = netdev_priv(dev);
  535. int real_size;
  536. if (shinfo->gso_size) {
  537. *inline_ok = false;
  538. if (skb->encapsulation)
  539. *lso_header_size = (skb_inner_transport_header(skb) - skb->data) + inner_tcp_hdrlen(skb);
  540. else
  541. *lso_header_size = skb_transport_offset(skb) + tcp_hdrlen(skb);
  542. real_size = CTRL_SIZE + shinfo->nr_frags * DS_SIZE +
  543. ALIGN(*lso_header_size + 4, DS_SIZE);
  544. if (unlikely(*lso_header_size != skb_headlen(skb))) {
  545. /* We add a segment for the skb linear buffer only if
  546. * it contains data */
  547. if (*lso_header_size < skb_headlen(skb))
  548. real_size += DS_SIZE;
  549. else {
  550. if (netif_msg_tx_err(priv))
  551. en_warn(priv, "Non-linear headers\n");
  552. return 0;
  553. }
  554. }
  555. } else {
  556. *lso_header_size = 0;
  557. *inline_ok = is_inline(priv->prof->inline_thold, skb,
  558. shinfo, pfrag);
  559. if (*inline_ok)
  560. real_size = inline_size(skb);
  561. else
  562. real_size = CTRL_SIZE +
  563. (shinfo->nr_frags + 1) * DS_SIZE;
  564. }
  565. return real_size;
  566. }
  567. static void build_inline_wqe(struct mlx4_en_tx_desc *tx_desc,
  568. const struct sk_buff *skb,
  569. const struct skb_shared_info *shinfo,
  570. void *fragptr)
  571. {
  572. struct mlx4_wqe_inline_seg *inl = &tx_desc->inl;
  573. int spc = MLX4_INLINE_ALIGN - CTRL_SIZE - sizeof *inl;
  574. unsigned int hlen = skb_headlen(skb);
  575. if (skb->len <= spc) {
  576. if (likely(skb->len >= MIN_PKT_LEN)) {
  577. inl->byte_count = cpu_to_be32(1 << 31 | skb->len);
  578. } else {
  579. inl->byte_count = cpu_to_be32(1 << 31 | MIN_PKT_LEN);
  580. memset(((void *)(inl + 1)) + skb->len, 0,
  581. MIN_PKT_LEN - skb->len);
  582. }
  583. skb_copy_from_linear_data(skb, inl + 1, hlen);
  584. if (shinfo->nr_frags)
  585. memcpy(((void *)(inl + 1)) + hlen, fragptr,
  586. skb_frag_size(&shinfo->frags[0]));
  587. } else {
  588. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  589. if (hlen <= spc) {
  590. skb_copy_from_linear_data(skb, inl + 1, hlen);
  591. if (hlen < spc) {
  592. memcpy(((void *)(inl + 1)) + hlen,
  593. fragptr, spc - hlen);
  594. fragptr += spc - hlen;
  595. }
  596. inl = (void *) (inl + 1) + spc;
  597. memcpy(((void *)(inl + 1)), fragptr, skb->len - spc);
  598. } else {
  599. skb_copy_from_linear_data(skb, inl + 1, spc);
  600. inl = (void *) (inl + 1) + spc;
  601. skb_copy_from_linear_data_offset(skb, spc, inl + 1,
  602. hlen - spc);
  603. if (shinfo->nr_frags)
  604. memcpy(((void *)(inl + 1)) + hlen - spc,
  605. fragptr,
  606. skb_frag_size(&shinfo->frags[0]));
  607. }
  608. dma_wmb();
  609. inl->byte_count = cpu_to_be32(1 << 31 | (skb->len - spc));
  610. }
  611. }
  612. u16 mlx4_en_select_queue(struct net_device *dev, struct sk_buff *skb,
  613. void *accel_priv, select_queue_fallback_t fallback)
  614. {
  615. struct mlx4_en_priv *priv = netdev_priv(dev);
  616. u16 rings_p_up = priv->num_tx_rings_p_up;
  617. u8 up = 0;
  618. if (netdev_get_num_tc(dev))
  619. return skb_tx_hash(dev, skb);
  620. if (skb_vlan_tag_present(skb))
  621. up = skb_vlan_tag_get(skb) >> VLAN_PRIO_SHIFT;
  622. return fallback(dev, skb) % rings_p_up + up * rings_p_up;
  623. }
  624. static void mlx4_bf_copy(void __iomem *dst, const void *src,
  625. unsigned int bytecnt)
  626. {
  627. __iowrite64_copy(dst, src, bytecnt / 8);
  628. }
  629. void mlx4_en_xmit_doorbell(struct mlx4_en_tx_ring *ring)
  630. {
  631. wmb();
  632. /* Since there is no iowrite*_native() that writes the
  633. * value as is, without byteswapping - using the one
  634. * the doesn't do byteswapping in the relevant arch
  635. * endianness.
  636. */
  637. #if defined(__LITTLE_ENDIAN)
  638. iowrite32(
  639. #else
  640. iowrite32be(
  641. #endif
  642. ring->doorbell_qpn,
  643. ring->bf.uar->map + MLX4_SEND_DOORBELL);
  644. }
  645. static void mlx4_en_tx_write_desc(struct mlx4_en_tx_ring *ring,
  646. struct mlx4_en_tx_desc *tx_desc,
  647. union mlx4_wqe_qpn_vlan qpn_vlan,
  648. int desc_size, int bf_index,
  649. __be32 op_own, bool bf_ok,
  650. bool send_doorbell)
  651. {
  652. tx_desc->ctrl.qpn_vlan = qpn_vlan;
  653. if (bf_ok) {
  654. op_own |= htonl((bf_index & 0xffff) << 8);
  655. /* Ensure new descriptor hits memory
  656. * before setting ownership of this descriptor to HW
  657. */
  658. dma_wmb();
  659. tx_desc->ctrl.owner_opcode = op_own;
  660. wmb();
  661. mlx4_bf_copy(ring->bf.reg + ring->bf.offset, &tx_desc->ctrl,
  662. desc_size);
  663. wmb();
  664. ring->bf.offset ^= ring->bf.buf_size;
  665. } else {
  666. /* Ensure new descriptor hits memory
  667. * before setting ownership of this descriptor to HW
  668. */
  669. dma_wmb();
  670. tx_desc->ctrl.owner_opcode = op_own;
  671. if (send_doorbell)
  672. mlx4_en_xmit_doorbell(ring);
  673. else
  674. ring->xmit_more++;
  675. }
  676. }
  677. netdev_tx_t mlx4_en_xmit(struct sk_buff *skb, struct net_device *dev)
  678. {
  679. struct skb_shared_info *shinfo = skb_shinfo(skb);
  680. struct mlx4_en_priv *priv = netdev_priv(dev);
  681. union mlx4_wqe_qpn_vlan qpn_vlan = {};
  682. struct device *ddev = priv->ddev;
  683. struct mlx4_en_tx_ring *ring;
  684. struct mlx4_en_tx_desc *tx_desc;
  685. struct mlx4_wqe_data_seg *data;
  686. struct mlx4_en_tx_info *tx_info;
  687. int tx_ind = 0;
  688. int nr_txbb;
  689. int desc_size;
  690. int real_size;
  691. u32 index, bf_index;
  692. __be32 op_own;
  693. u16 vlan_proto = 0;
  694. int i_frag;
  695. int lso_header_size;
  696. void *fragptr = NULL;
  697. bool bounce = false;
  698. bool send_doorbell;
  699. bool stop_queue;
  700. bool inline_ok;
  701. u32 ring_cons;
  702. bool bf_ok;
  703. tx_ind = skb_get_queue_mapping(skb);
  704. ring = priv->tx_ring[TX][tx_ind];
  705. if (!priv->port_up)
  706. goto tx_drop;
  707. /* fetch ring->cons far ahead before needing it to avoid stall */
  708. ring_cons = ACCESS_ONCE(ring->cons);
  709. real_size = get_real_size(skb, shinfo, dev, &lso_header_size,
  710. &inline_ok, &fragptr);
  711. if (unlikely(!real_size))
  712. goto tx_drop_count;
  713. /* Align descriptor to TXBB size */
  714. desc_size = ALIGN(real_size, TXBB_SIZE);
  715. nr_txbb = desc_size / TXBB_SIZE;
  716. if (unlikely(nr_txbb > MAX_DESC_TXBBS)) {
  717. if (netif_msg_tx_err(priv))
  718. en_warn(priv, "Oversized header or SG list\n");
  719. goto tx_drop_count;
  720. }
  721. bf_ok = ring->bf_enabled;
  722. if (skb_vlan_tag_present(skb)) {
  723. qpn_vlan.vlan_tag = cpu_to_be16(skb_vlan_tag_get(skb));
  724. vlan_proto = be16_to_cpu(skb->vlan_proto);
  725. if (vlan_proto == ETH_P_8021AD)
  726. qpn_vlan.ins_vlan = MLX4_WQE_CTRL_INS_SVLAN;
  727. else if (vlan_proto == ETH_P_8021Q)
  728. qpn_vlan.ins_vlan = MLX4_WQE_CTRL_INS_CVLAN;
  729. else
  730. qpn_vlan.ins_vlan = 0;
  731. bf_ok = false;
  732. }
  733. netdev_txq_bql_enqueue_prefetchw(ring->tx_queue);
  734. /* Track current inflight packets for performance analysis */
  735. AVG_PERF_COUNTER(priv->pstats.inflight_avg,
  736. (u32)(ring->prod - ring_cons - 1));
  737. /* Packet is good - grab an index and transmit it */
  738. index = ring->prod & ring->size_mask;
  739. bf_index = ring->prod;
  740. /* See if we have enough space for whole descriptor TXBB for setting
  741. * SW ownership on next descriptor; if not, use a bounce buffer. */
  742. if (likely(index + nr_txbb <= ring->size))
  743. tx_desc = ring->buf + index * TXBB_SIZE;
  744. else {
  745. tx_desc = (struct mlx4_en_tx_desc *) ring->bounce_buf;
  746. bounce = true;
  747. bf_ok = false;
  748. }
  749. /* Save skb in tx_info ring */
  750. tx_info = &ring->tx_info[index];
  751. tx_info->skb = skb;
  752. tx_info->nr_txbb = nr_txbb;
  753. data = &tx_desc->data;
  754. if (lso_header_size)
  755. data = ((void *)&tx_desc->lso + ALIGN(lso_header_size + 4,
  756. DS_SIZE));
  757. /* valid only for none inline segments */
  758. tx_info->data_offset = (void *)data - (void *)tx_desc;
  759. tx_info->inl = inline_ok;
  760. tx_info->linear = (lso_header_size < skb_headlen(skb) &&
  761. !inline_ok) ? 1 : 0;
  762. tx_info->nr_maps = shinfo->nr_frags + tx_info->linear;
  763. data += tx_info->nr_maps - 1;
  764. if (!tx_info->inl) {
  765. dma_addr_t dma = 0;
  766. u32 byte_count = 0;
  767. /* Map fragments if any */
  768. for (i_frag = shinfo->nr_frags - 1; i_frag >= 0; i_frag--) {
  769. const struct skb_frag_struct *frag;
  770. frag = &shinfo->frags[i_frag];
  771. byte_count = skb_frag_size(frag);
  772. dma = skb_frag_dma_map(ddev, frag,
  773. 0, byte_count,
  774. DMA_TO_DEVICE);
  775. if (dma_mapping_error(ddev, dma))
  776. goto tx_drop_unmap;
  777. data->addr = cpu_to_be64(dma);
  778. data->lkey = ring->mr_key;
  779. dma_wmb();
  780. data->byte_count = cpu_to_be32(byte_count);
  781. --data;
  782. }
  783. /* Map linear part if needed */
  784. if (tx_info->linear) {
  785. byte_count = skb_headlen(skb) - lso_header_size;
  786. dma = dma_map_single(ddev, skb->data +
  787. lso_header_size, byte_count,
  788. PCI_DMA_TODEVICE);
  789. if (dma_mapping_error(ddev, dma))
  790. goto tx_drop_unmap;
  791. data->addr = cpu_to_be64(dma);
  792. data->lkey = ring->mr_key;
  793. dma_wmb();
  794. data->byte_count = cpu_to_be32(byte_count);
  795. }
  796. /* tx completion can avoid cache line miss for common cases */
  797. tx_info->map0_dma = dma;
  798. tx_info->map0_byte_count = byte_count;
  799. }
  800. /*
  801. * For timestamping add flag to skb_shinfo and
  802. * set flag for further reference
  803. */
  804. tx_info->ts_requested = 0;
  805. if (unlikely(ring->hwtstamp_tx_type == HWTSTAMP_TX_ON &&
  806. shinfo->tx_flags & SKBTX_HW_TSTAMP)) {
  807. shinfo->tx_flags |= SKBTX_IN_PROGRESS;
  808. tx_info->ts_requested = 1;
  809. }
  810. /* Prepare ctrl segement apart opcode+ownership, which depends on
  811. * whether LSO is used */
  812. tx_desc->ctrl.srcrb_flags = priv->ctrl_flags;
  813. if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
  814. if (!skb->encapsulation)
  815. tx_desc->ctrl.srcrb_flags |= cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM |
  816. MLX4_WQE_CTRL_TCP_UDP_CSUM);
  817. else
  818. tx_desc->ctrl.srcrb_flags |= cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM);
  819. ring->tx_csum++;
  820. }
  821. if (priv->flags & MLX4_EN_FLAG_ENABLE_HW_LOOPBACK) {
  822. struct ethhdr *ethh;
  823. /* Copy dst mac address to wqe. This allows loopback in eSwitch,
  824. * so that VFs and PF can communicate with each other
  825. */
  826. ethh = (struct ethhdr *)skb->data;
  827. tx_desc->ctrl.srcrb_flags16[0] = get_unaligned((__be16 *)ethh->h_dest);
  828. tx_desc->ctrl.imm = get_unaligned((__be32 *)(ethh->h_dest + 2));
  829. }
  830. /* Handle LSO (TSO) packets */
  831. if (lso_header_size) {
  832. int i;
  833. /* Mark opcode as LSO */
  834. op_own = cpu_to_be32(MLX4_OPCODE_LSO | (1 << 6)) |
  835. ((ring->prod & ring->size) ?
  836. cpu_to_be32(MLX4_EN_BIT_DESC_OWN) : 0);
  837. /* Fill in the LSO prefix */
  838. tx_desc->lso.mss_hdr_size = cpu_to_be32(
  839. shinfo->gso_size << 16 | lso_header_size);
  840. /* Copy headers;
  841. * note that we already verified that it is linear */
  842. memcpy(tx_desc->lso.header, skb->data, lso_header_size);
  843. ring->tso_packets++;
  844. i = ((skb->len - lso_header_size) / shinfo->gso_size) +
  845. !!((skb->len - lso_header_size) % shinfo->gso_size);
  846. tx_info->nr_bytes = skb->len + (i - 1) * lso_header_size;
  847. ring->packets += i;
  848. } else {
  849. /* Normal (Non LSO) packet */
  850. op_own = cpu_to_be32(MLX4_OPCODE_SEND) |
  851. ((ring->prod & ring->size) ?
  852. cpu_to_be32(MLX4_EN_BIT_DESC_OWN) : 0);
  853. tx_info->nr_bytes = max_t(unsigned int, skb->len, ETH_ZLEN);
  854. ring->packets++;
  855. }
  856. ring->bytes += tx_info->nr_bytes;
  857. netdev_tx_sent_queue(ring->tx_queue, tx_info->nr_bytes);
  858. AVG_PERF_COUNTER(priv->pstats.tx_pktsz_avg, skb->len);
  859. if (tx_info->inl)
  860. build_inline_wqe(tx_desc, skb, shinfo, fragptr);
  861. if (skb->encapsulation) {
  862. union {
  863. struct iphdr *v4;
  864. struct ipv6hdr *v6;
  865. unsigned char *hdr;
  866. } ip;
  867. u8 proto;
  868. ip.hdr = skb_inner_network_header(skb);
  869. proto = (ip.v4->version == 4) ? ip.v4->protocol :
  870. ip.v6->nexthdr;
  871. if (proto == IPPROTO_TCP || proto == IPPROTO_UDP)
  872. op_own |= cpu_to_be32(MLX4_WQE_CTRL_IIP | MLX4_WQE_CTRL_ILP);
  873. else
  874. op_own |= cpu_to_be32(MLX4_WQE_CTRL_IIP);
  875. }
  876. ring->prod += nr_txbb;
  877. /* If we used a bounce buffer then copy descriptor back into place */
  878. if (unlikely(bounce))
  879. tx_desc = mlx4_en_bounce_to_desc(priv, ring, index, desc_size);
  880. skb_tx_timestamp(skb);
  881. /* Check available TXBBs And 2K spare for prefetch */
  882. stop_queue = mlx4_en_is_tx_ring_full(ring);
  883. if (unlikely(stop_queue)) {
  884. netif_tx_stop_queue(ring->tx_queue);
  885. ring->queue_stopped++;
  886. }
  887. send_doorbell = !skb->xmit_more || netif_xmit_stopped(ring->tx_queue);
  888. real_size = (real_size / 16) & 0x3f;
  889. bf_ok &= desc_size <= MAX_BF && send_doorbell;
  890. if (bf_ok)
  891. qpn_vlan.bf_qpn = ring->doorbell_qpn | cpu_to_be32(real_size);
  892. else
  893. qpn_vlan.fence_size = real_size;
  894. mlx4_en_tx_write_desc(ring, tx_desc, qpn_vlan, desc_size, bf_index,
  895. op_own, bf_ok, send_doorbell);
  896. if (unlikely(stop_queue)) {
  897. /* If queue was emptied after the if (stop_queue) , and before
  898. * the netif_tx_stop_queue() - need to wake the queue,
  899. * or else it will remain stopped forever.
  900. * Need a memory barrier to make sure ring->cons was not
  901. * updated before queue was stopped.
  902. */
  903. smp_rmb();
  904. ring_cons = ACCESS_ONCE(ring->cons);
  905. if (unlikely(!mlx4_en_is_tx_ring_full(ring))) {
  906. netif_tx_wake_queue(ring->tx_queue);
  907. ring->wake_queue++;
  908. }
  909. }
  910. return NETDEV_TX_OK;
  911. tx_drop_unmap:
  912. en_err(priv, "DMA mapping error\n");
  913. while (++i_frag < shinfo->nr_frags) {
  914. ++data;
  915. dma_unmap_page(ddev, (dma_addr_t) be64_to_cpu(data->addr),
  916. be32_to_cpu(data->byte_count),
  917. PCI_DMA_TODEVICE);
  918. }
  919. tx_drop_count:
  920. ring->tx_dropped++;
  921. tx_drop:
  922. dev_kfree_skb_any(skb);
  923. return NETDEV_TX_OK;
  924. }
  925. netdev_tx_t mlx4_en_xmit_frame(struct mlx4_en_rx_ring *rx_ring,
  926. struct mlx4_en_rx_alloc *frame,
  927. struct net_device *dev, unsigned int length,
  928. int tx_ind, int *doorbell_pending)
  929. {
  930. struct mlx4_en_priv *priv = netdev_priv(dev);
  931. union mlx4_wqe_qpn_vlan qpn_vlan = {};
  932. struct mlx4_en_tx_ring *ring;
  933. struct mlx4_en_tx_desc *tx_desc;
  934. struct mlx4_wqe_data_seg *data;
  935. struct mlx4_en_tx_info *tx_info;
  936. int index, bf_index;
  937. bool send_doorbell;
  938. int nr_txbb = 1;
  939. bool stop_queue;
  940. dma_addr_t dma;
  941. int real_size;
  942. __be32 op_own;
  943. u32 ring_cons;
  944. bool bf_ok;
  945. BUILD_BUG_ON_MSG(ALIGN(CTRL_SIZE + DS_SIZE, TXBB_SIZE) != TXBB_SIZE,
  946. "mlx4_en_xmit_frame requires minimum size tx desc");
  947. ring = priv->tx_ring[TX_XDP][tx_ind];
  948. if (!priv->port_up)
  949. goto tx_drop;
  950. if (mlx4_en_is_tx_ring_full(ring))
  951. goto tx_drop_count;
  952. /* fetch ring->cons far ahead before needing it to avoid stall */
  953. ring_cons = READ_ONCE(ring->cons);
  954. index = ring->prod & ring->size_mask;
  955. tx_info = &ring->tx_info[index];
  956. bf_ok = ring->bf_enabled;
  957. /* Track current inflight packets for performance analysis */
  958. AVG_PERF_COUNTER(priv->pstats.inflight_avg,
  959. (u32)(ring->prod - ring_cons - 1));
  960. bf_index = ring->prod;
  961. tx_desc = ring->buf + index * TXBB_SIZE;
  962. data = &tx_desc->data;
  963. dma = frame->dma;
  964. tx_info->page = frame->page;
  965. frame->page = NULL;
  966. tx_info->map0_dma = dma;
  967. tx_info->map0_byte_count = PAGE_SIZE;
  968. tx_info->nr_txbb = nr_txbb;
  969. tx_info->nr_bytes = max_t(unsigned int, length, ETH_ZLEN);
  970. tx_info->data_offset = (void *)data - (void *)tx_desc;
  971. tx_info->ts_requested = 0;
  972. tx_info->nr_maps = 1;
  973. tx_info->linear = 1;
  974. tx_info->inl = 0;
  975. dma_sync_single_range_for_device(priv->ddev, dma, frame->page_offset,
  976. length, PCI_DMA_TODEVICE);
  977. data->addr = cpu_to_be64(dma + frame->page_offset);
  978. data->lkey = ring->mr_key;
  979. dma_wmb();
  980. data->byte_count = cpu_to_be32(length);
  981. /* tx completion can avoid cache line miss for common cases */
  982. tx_desc->ctrl.srcrb_flags = priv->ctrl_flags;
  983. op_own = cpu_to_be32(MLX4_OPCODE_SEND) |
  984. ((ring->prod & ring->size) ?
  985. cpu_to_be32(MLX4_EN_BIT_DESC_OWN) : 0);
  986. rx_ring->xdp_tx++;
  987. AVG_PERF_COUNTER(priv->pstats.tx_pktsz_avg, length);
  988. ring->prod += nr_txbb;
  989. stop_queue = mlx4_en_is_tx_ring_full(ring);
  990. send_doorbell = stop_queue ||
  991. *doorbell_pending > MLX4_EN_DOORBELL_BUDGET;
  992. bf_ok &= send_doorbell;
  993. real_size = ((CTRL_SIZE + nr_txbb * DS_SIZE) / 16) & 0x3f;
  994. if (bf_ok)
  995. qpn_vlan.bf_qpn = ring->doorbell_qpn | cpu_to_be32(real_size);
  996. else
  997. qpn_vlan.fence_size = real_size;
  998. mlx4_en_tx_write_desc(ring, tx_desc, qpn_vlan, TXBB_SIZE, bf_index,
  999. op_own, bf_ok, send_doorbell);
  1000. *doorbell_pending = send_doorbell ? 0 : *doorbell_pending + 1;
  1001. return NETDEV_TX_OK;
  1002. tx_drop_count:
  1003. rx_ring->xdp_tx_full++;
  1004. tx_drop:
  1005. return NETDEV_TX_BUSY;
  1006. }