en_tx.c 28 KB

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