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