en_tx.c 28 KB

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