en_tx.c 28 KB

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