en_tx.c 32 KB

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