en_tx.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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, int qpn, 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->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->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. ring->qpn = qpn;
  104. err = mlx4_qp_alloc(mdev->dev, ring->qpn, &ring->qp, GFP_KERNEL);
  105. if (err) {
  106. en_err(priv, "Failed allocating qp %d\n", ring->qpn);
  107. goto err_map;
  108. }
  109. ring->qp.event = mlx4_en_sqp_event;
  110. err = mlx4_bf_alloc(mdev->dev, &ring->bf, node);
  111. if (err) {
  112. en_dbg(DRV, priv, "working without blueflame (%d)\n", err);
  113. ring->bf.uar = &mdev->priv_uar;
  114. ring->bf.uar->map = mdev->uar_map;
  115. ring->bf_enabled = false;
  116. ring->bf_alloced = false;
  117. priv->pflags &= ~MLX4_EN_PRIV_FLAGS_BLUEFLAME;
  118. } else {
  119. ring->bf_alloced = true;
  120. ring->bf_enabled = !!(priv->pflags &
  121. MLX4_EN_PRIV_FLAGS_BLUEFLAME);
  122. }
  123. ring->hwtstamp_tx_type = priv->hwtstamp_config.tx_type;
  124. ring->queue_index = queue_index;
  125. if (queue_index < priv->num_tx_rings_p_up)
  126. cpumask_set_cpu_local_first(queue_index,
  127. priv->mdev->dev->numa_node,
  128. &ring->affinity_mask);
  129. *pring = ring;
  130. return 0;
  131. err_map:
  132. mlx4_en_unmap_buffer(&ring->wqres.buf);
  133. err_hwq_res:
  134. mlx4_free_hwq_res(mdev->dev, &ring->wqres, ring->buf_size);
  135. err_bounce:
  136. kfree(ring->bounce_buf);
  137. ring->bounce_buf = NULL;
  138. err_info:
  139. kvfree(ring->tx_info);
  140. ring->tx_info = NULL;
  141. err_ring:
  142. kfree(ring);
  143. *pring = NULL;
  144. return err;
  145. }
  146. void mlx4_en_destroy_tx_ring(struct mlx4_en_priv *priv,
  147. struct mlx4_en_tx_ring **pring)
  148. {
  149. struct mlx4_en_dev *mdev = priv->mdev;
  150. struct mlx4_en_tx_ring *ring = *pring;
  151. en_dbg(DRV, priv, "Destroying tx ring, qpn: %d\n", ring->qpn);
  152. if (ring->bf_alloced)
  153. mlx4_bf_free(mdev->dev, &ring->bf);
  154. mlx4_qp_remove(mdev->dev, &ring->qp);
  155. mlx4_qp_free(mdev->dev, &ring->qp);
  156. mlx4_en_unmap_buffer(&ring->wqres.buf);
  157. mlx4_free_hwq_res(mdev->dev, &ring->wqres, ring->buf_size);
  158. kfree(ring->bounce_buf);
  159. ring->bounce_buf = NULL;
  160. kvfree(ring->tx_info);
  161. ring->tx_info = NULL;
  162. kfree(ring);
  163. *pring = NULL;
  164. }
  165. int mlx4_en_activate_tx_ring(struct mlx4_en_priv *priv,
  166. struct mlx4_en_tx_ring *ring,
  167. int cq, int user_prio)
  168. {
  169. struct mlx4_en_dev *mdev = priv->mdev;
  170. int err;
  171. ring->cqn = cq;
  172. ring->prod = 0;
  173. ring->cons = 0xffffffff;
  174. ring->last_nr_txbb = 1;
  175. memset(ring->tx_info, 0, ring->size * sizeof(struct mlx4_en_tx_info));
  176. memset(ring->buf, 0, ring->buf_size);
  177. ring->qp_state = MLX4_QP_STATE_RST;
  178. ring->doorbell_qpn = cpu_to_be32(ring->qp.qpn << 8);
  179. ring->mr_key = cpu_to_be32(mdev->mr.key);
  180. mlx4_en_fill_qp_context(priv, ring->size, ring->stride, 1, 0, ring->qpn,
  181. ring->cqn, user_prio, &ring->context);
  182. if (ring->bf_alloced)
  183. ring->context.usr_page = cpu_to_be32(ring->bf.uar->index);
  184. err = mlx4_qp_to_ready(mdev->dev, &ring->wqres.mtt, &ring->context,
  185. &ring->qp, &ring->qp_state);
  186. if (!cpumask_empty(&ring->affinity_mask))
  187. netif_set_xps_queue(priv->dev, &ring->affinity_mask,
  188. ring->queue_index);
  189. return err;
  190. }
  191. void mlx4_en_deactivate_tx_ring(struct mlx4_en_priv *priv,
  192. struct mlx4_en_tx_ring *ring)
  193. {
  194. struct mlx4_en_dev *mdev = priv->mdev;
  195. mlx4_qp_modify(mdev->dev, NULL, ring->qp_state,
  196. MLX4_QP_STATE_RST, NULL, 0, 0, &ring->qp);
  197. }
  198. static inline bool mlx4_en_is_tx_ring_full(struct mlx4_en_tx_ring *ring)
  199. {
  200. return ring->prod - ring->cons > ring->full_size;
  201. }
  202. static void mlx4_en_stamp_wqe(struct mlx4_en_priv *priv,
  203. struct mlx4_en_tx_ring *ring, int index,
  204. u8 owner)
  205. {
  206. __be32 stamp = cpu_to_be32(STAMP_VAL | (!!owner << STAMP_SHIFT));
  207. struct mlx4_en_tx_desc *tx_desc = ring->buf + index * TXBB_SIZE;
  208. struct mlx4_en_tx_info *tx_info = &ring->tx_info[index];
  209. void *end = ring->buf + ring->buf_size;
  210. __be32 *ptr = (__be32 *)tx_desc;
  211. int i;
  212. /* Optimize the common case when there are no wraparounds */
  213. if (likely((void *)tx_desc + tx_info->nr_txbb * TXBB_SIZE <= end)) {
  214. /* Stamp the freed descriptor */
  215. for (i = 0; i < tx_info->nr_txbb * TXBB_SIZE;
  216. i += STAMP_STRIDE) {
  217. *ptr = stamp;
  218. ptr += STAMP_DWORDS;
  219. }
  220. } else {
  221. /* Stamp the freed descriptor */
  222. for (i = 0; i < tx_info->nr_txbb * TXBB_SIZE;
  223. i += STAMP_STRIDE) {
  224. *ptr = stamp;
  225. ptr += STAMP_DWORDS;
  226. if ((void *)ptr >= end) {
  227. ptr = ring->buf;
  228. stamp ^= cpu_to_be32(0x80000000);
  229. }
  230. }
  231. }
  232. }
  233. 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. /* Wakeup Tx queue if this stopped, and ring is not full.
  412. */
  413. if (netif_tx_queue_stopped(ring->tx_queue) &&
  414. !mlx4_en_is_tx_ring_full(ring)) {
  415. netif_tx_wake_queue(ring->tx_queue);
  416. ring->wake_queue++;
  417. }
  418. return done < budget;
  419. }
  420. void mlx4_en_tx_irq(struct mlx4_cq *mcq)
  421. {
  422. struct mlx4_en_cq *cq = container_of(mcq, struct mlx4_en_cq, mcq);
  423. struct mlx4_en_priv *priv = netdev_priv(cq->dev);
  424. if (priv->port_up)
  425. napi_schedule(&cq->napi);
  426. else
  427. mlx4_en_arm_cq(priv, cq);
  428. }
  429. /* TX CQ polling - called by NAPI */
  430. int mlx4_en_poll_tx_cq(struct napi_struct *napi, int budget)
  431. {
  432. struct mlx4_en_cq *cq = container_of(napi, struct mlx4_en_cq, napi);
  433. struct net_device *dev = cq->dev;
  434. struct mlx4_en_priv *priv = netdev_priv(dev);
  435. int clean_complete;
  436. clean_complete = mlx4_en_process_tx_cq(dev, cq);
  437. if (!clean_complete)
  438. return budget;
  439. napi_complete(napi);
  440. mlx4_en_arm_cq(priv, cq);
  441. return 0;
  442. }
  443. static struct mlx4_en_tx_desc *mlx4_en_bounce_to_desc(struct mlx4_en_priv *priv,
  444. struct mlx4_en_tx_ring *ring,
  445. u32 index,
  446. unsigned int desc_size)
  447. {
  448. u32 copy = (ring->size - index) * TXBB_SIZE;
  449. int i;
  450. for (i = desc_size - copy - 4; i >= 0; i -= 4) {
  451. if ((i & (TXBB_SIZE - 1)) == 0)
  452. wmb();
  453. *((u32 *) (ring->buf + i)) =
  454. *((u32 *) (ring->bounce_buf + copy + i));
  455. }
  456. for (i = copy - 4; i >= 4 ; i -= 4) {
  457. if ((i & (TXBB_SIZE - 1)) == 0)
  458. wmb();
  459. *((u32 *) (ring->buf + index * TXBB_SIZE + i)) =
  460. *((u32 *) (ring->bounce_buf + i));
  461. }
  462. /* Return real descriptor location */
  463. return ring->buf + index * TXBB_SIZE;
  464. }
  465. /* Decide if skb can be inlined in tx descriptor to avoid dma mapping
  466. *
  467. * It seems strange we do not simply use skb_copy_bits().
  468. * This would allow to inline all skbs iff skb->len <= inline_thold
  469. *
  470. * Note that caller already checked skb was not a gso packet
  471. */
  472. static bool is_inline(int inline_thold, const struct sk_buff *skb,
  473. const struct skb_shared_info *shinfo,
  474. void **pfrag)
  475. {
  476. void *ptr;
  477. if (skb->len > inline_thold || !inline_thold)
  478. return false;
  479. if (shinfo->nr_frags == 1) {
  480. ptr = skb_frag_address_safe(&shinfo->frags[0]);
  481. if (unlikely(!ptr))
  482. return false;
  483. *pfrag = ptr;
  484. return true;
  485. }
  486. if (shinfo->nr_frags)
  487. return false;
  488. return true;
  489. }
  490. static int inline_size(const struct sk_buff *skb)
  491. {
  492. if (skb->len + CTRL_SIZE + sizeof(struct mlx4_wqe_inline_seg)
  493. <= MLX4_INLINE_ALIGN)
  494. return ALIGN(skb->len + CTRL_SIZE +
  495. sizeof(struct mlx4_wqe_inline_seg), 16);
  496. else
  497. return ALIGN(skb->len + CTRL_SIZE + 2 *
  498. sizeof(struct mlx4_wqe_inline_seg), 16);
  499. }
  500. static int get_real_size(const struct sk_buff *skb,
  501. const struct skb_shared_info *shinfo,
  502. struct net_device *dev,
  503. int *lso_header_size,
  504. bool *inline_ok,
  505. void **pfrag)
  506. {
  507. struct mlx4_en_priv *priv = netdev_priv(dev);
  508. int real_size;
  509. if (shinfo->gso_size) {
  510. *inline_ok = false;
  511. if (skb->encapsulation)
  512. *lso_header_size = (skb_inner_transport_header(skb) - skb->data) + inner_tcp_hdrlen(skb);
  513. else
  514. *lso_header_size = skb_transport_offset(skb) + tcp_hdrlen(skb);
  515. real_size = CTRL_SIZE + shinfo->nr_frags * DS_SIZE +
  516. ALIGN(*lso_header_size + 4, DS_SIZE);
  517. if (unlikely(*lso_header_size != skb_headlen(skb))) {
  518. /* We add a segment for the skb linear buffer only if
  519. * it contains data */
  520. if (*lso_header_size < skb_headlen(skb))
  521. real_size += DS_SIZE;
  522. else {
  523. if (netif_msg_tx_err(priv))
  524. en_warn(priv, "Non-linear headers\n");
  525. return 0;
  526. }
  527. }
  528. } else {
  529. *lso_header_size = 0;
  530. *inline_ok = is_inline(priv->prof->inline_thold, skb,
  531. shinfo, pfrag);
  532. if (*inline_ok)
  533. real_size = inline_size(skb);
  534. else
  535. real_size = CTRL_SIZE +
  536. (shinfo->nr_frags + 1) * DS_SIZE;
  537. }
  538. return real_size;
  539. }
  540. static void build_inline_wqe(struct mlx4_en_tx_desc *tx_desc,
  541. const struct sk_buff *skb,
  542. const struct skb_shared_info *shinfo,
  543. int real_size, u16 *vlan_tag,
  544. int tx_ind, void *fragptr)
  545. {
  546. struct mlx4_wqe_inline_seg *inl = &tx_desc->inl;
  547. int spc = MLX4_INLINE_ALIGN - CTRL_SIZE - sizeof *inl;
  548. unsigned int hlen = skb_headlen(skb);
  549. if (skb->len <= spc) {
  550. if (likely(skb->len >= MIN_PKT_LEN)) {
  551. inl->byte_count = cpu_to_be32(1 << 31 | skb->len);
  552. } else {
  553. inl->byte_count = cpu_to_be32(1 << 31 | MIN_PKT_LEN);
  554. memset(((void *)(inl + 1)) + skb->len, 0,
  555. MIN_PKT_LEN - skb->len);
  556. }
  557. skb_copy_from_linear_data(skb, inl + 1, hlen);
  558. if (shinfo->nr_frags)
  559. memcpy(((void *)(inl + 1)) + hlen, fragptr,
  560. skb_frag_size(&shinfo->frags[0]));
  561. } else {
  562. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  563. if (hlen <= spc) {
  564. skb_copy_from_linear_data(skb, inl + 1, hlen);
  565. if (hlen < spc) {
  566. memcpy(((void *)(inl + 1)) + hlen,
  567. fragptr, spc - hlen);
  568. fragptr += spc - hlen;
  569. }
  570. inl = (void *) (inl + 1) + spc;
  571. memcpy(((void *)(inl + 1)), fragptr, skb->len - spc);
  572. } else {
  573. skb_copy_from_linear_data(skb, inl + 1, spc);
  574. inl = (void *) (inl + 1) + spc;
  575. skb_copy_from_linear_data_offset(skb, spc, inl + 1,
  576. hlen - spc);
  577. if (shinfo->nr_frags)
  578. memcpy(((void *)(inl + 1)) + hlen - spc,
  579. fragptr,
  580. skb_frag_size(&shinfo->frags[0]));
  581. }
  582. wmb();
  583. inl->byte_count = cpu_to_be32(1 << 31 | (skb->len - spc));
  584. }
  585. }
  586. u16 mlx4_en_select_queue(struct net_device *dev, struct sk_buff *skb,
  587. void *accel_priv, select_queue_fallback_t fallback)
  588. {
  589. struct mlx4_en_priv *priv = netdev_priv(dev);
  590. u16 rings_p_up = priv->num_tx_rings_p_up;
  591. u8 up = 0;
  592. if (dev->num_tc)
  593. return skb_tx_hash(dev, skb);
  594. if (vlan_tx_tag_present(skb))
  595. up = vlan_tx_tag_get(skb) >> VLAN_PRIO_SHIFT;
  596. return fallback(dev, skb) % rings_p_up + up * rings_p_up;
  597. }
  598. static void mlx4_bf_copy(void __iomem *dst, const void *src,
  599. unsigned int bytecnt)
  600. {
  601. __iowrite64_copy(dst, src, bytecnt / 8);
  602. }
  603. netdev_tx_t mlx4_en_xmit(struct sk_buff *skb, struct net_device *dev)
  604. {
  605. struct skb_shared_info *shinfo = skb_shinfo(skb);
  606. struct mlx4_en_priv *priv = netdev_priv(dev);
  607. struct device *ddev = priv->ddev;
  608. struct mlx4_en_tx_ring *ring;
  609. struct mlx4_en_tx_desc *tx_desc;
  610. struct mlx4_wqe_data_seg *data;
  611. struct mlx4_en_tx_info *tx_info;
  612. int tx_ind = 0;
  613. int nr_txbb;
  614. int desc_size;
  615. int real_size;
  616. u32 index, bf_index;
  617. __be32 op_own;
  618. u16 vlan_tag = 0;
  619. int i_frag;
  620. int lso_header_size;
  621. void *fragptr = NULL;
  622. bool bounce = false;
  623. bool send_doorbell;
  624. bool stop_queue;
  625. bool inline_ok;
  626. u32 ring_cons;
  627. if (!priv->port_up)
  628. goto tx_drop;
  629. tx_ind = skb_get_queue_mapping(skb);
  630. ring = priv->tx_ring[tx_ind];
  631. /* fetch ring->cons far ahead before needing it to avoid stall */
  632. ring_cons = ACCESS_ONCE(ring->cons);
  633. real_size = get_real_size(skb, shinfo, dev, &lso_header_size,
  634. &inline_ok, &fragptr);
  635. if (unlikely(!real_size))
  636. goto tx_drop;
  637. /* Align descriptor to TXBB size */
  638. desc_size = ALIGN(real_size, TXBB_SIZE);
  639. nr_txbb = desc_size / TXBB_SIZE;
  640. if (unlikely(nr_txbb > MAX_DESC_TXBBS)) {
  641. if (netif_msg_tx_err(priv))
  642. en_warn(priv, "Oversized header or SG list\n");
  643. goto tx_drop;
  644. }
  645. if (vlan_tx_tag_present(skb))
  646. vlan_tag = vlan_tx_tag_get(skb);
  647. netdev_txq_bql_enqueue_prefetchw(ring->tx_queue);
  648. /* Track current inflight packets for performance analysis */
  649. AVG_PERF_COUNTER(priv->pstats.inflight_avg,
  650. (u32)(ring->prod - ring_cons - 1));
  651. /* Packet is good - grab an index and transmit it */
  652. index = ring->prod & ring->size_mask;
  653. bf_index = ring->prod;
  654. /* See if we have enough space for whole descriptor TXBB for setting
  655. * SW ownership on next descriptor; if not, use a bounce buffer. */
  656. if (likely(index + nr_txbb <= ring->size))
  657. tx_desc = ring->buf + index * TXBB_SIZE;
  658. else {
  659. tx_desc = (struct mlx4_en_tx_desc *) ring->bounce_buf;
  660. bounce = true;
  661. }
  662. /* Save skb in tx_info ring */
  663. tx_info = &ring->tx_info[index];
  664. tx_info->skb = skb;
  665. tx_info->nr_txbb = nr_txbb;
  666. data = &tx_desc->data;
  667. if (lso_header_size)
  668. data = ((void *)&tx_desc->lso + ALIGN(lso_header_size + 4,
  669. DS_SIZE));
  670. /* valid only for none inline segments */
  671. tx_info->data_offset = (void *)data - (void *)tx_desc;
  672. tx_info->inl = inline_ok;
  673. tx_info->linear = (lso_header_size < skb_headlen(skb) &&
  674. !inline_ok) ? 1 : 0;
  675. tx_info->nr_maps = shinfo->nr_frags + tx_info->linear;
  676. data += tx_info->nr_maps - 1;
  677. if (!tx_info->inl) {
  678. dma_addr_t dma = 0;
  679. u32 byte_count = 0;
  680. /* Map fragments if any */
  681. for (i_frag = shinfo->nr_frags - 1; i_frag >= 0; i_frag--) {
  682. const struct skb_frag_struct *frag;
  683. frag = &shinfo->frags[i_frag];
  684. byte_count = skb_frag_size(frag);
  685. dma = skb_frag_dma_map(ddev, frag,
  686. 0, byte_count,
  687. DMA_TO_DEVICE);
  688. if (dma_mapping_error(ddev, dma))
  689. goto tx_drop_unmap;
  690. data->addr = cpu_to_be64(dma);
  691. data->lkey = ring->mr_key;
  692. wmb();
  693. data->byte_count = cpu_to_be32(byte_count);
  694. --data;
  695. }
  696. /* Map linear part if needed */
  697. if (tx_info->linear) {
  698. byte_count = skb_headlen(skb) - lso_header_size;
  699. dma = dma_map_single(ddev, skb->data +
  700. lso_header_size, byte_count,
  701. PCI_DMA_TODEVICE);
  702. if (dma_mapping_error(ddev, dma))
  703. goto tx_drop_unmap;
  704. data->addr = cpu_to_be64(dma);
  705. data->lkey = ring->mr_key;
  706. wmb();
  707. data->byte_count = cpu_to_be32(byte_count);
  708. }
  709. /* tx completion can avoid cache line miss for common cases */
  710. tx_info->map0_dma = dma;
  711. tx_info->map0_byte_count = byte_count;
  712. }
  713. /*
  714. * For timestamping add flag to skb_shinfo and
  715. * set flag for further reference
  716. */
  717. tx_info->ts_requested = 0;
  718. if (unlikely(ring->hwtstamp_tx_type == HWTSTAMP_TX_ON &&
  719. shinfo->tx_flags & SKBTX_HW_TSTAMP)) {
  720. shinfo->tx_flags |= SKBTX_IN_PROGRESS;
  721. tx_info->ts_requested = 1;
  722. }
  723. /* Prepare ctrl segement apart opcode+ownership, which depends on
  724. * whether LSO is used */
  725. tx_desc->ctrl.srcrb_flags = priv->ctrl_flags;
  726. if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
  727. if (!skb->encapsulation)
  728. tx_desc->ctrl.srcrb_flags |= cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM |
  729. MLX4_WQE_CTRL_TCP_UDP_CSUM);
  730. else
  731. tx_desc->ctrl.srcrb_flags |= cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM);
  732. ring->tx_csum++;
  733. }
  734. if (priv->flags & MLX4_EN_FLAG_ENABLE_HW_LOOPBACK) {
  735. struct ethhdr *ethh;
  736. /* Copy dst mac address to wqe. This allows loopback in eSwitch,
  737. * so that VFs and PF can communicate with each other
  738. */
  739. ethh = (struct ethhdr *)skb->data;
  740. tx_desc->ctrl.srcrb_flags16[0] = get_unaligned((__be16 *)ethh->h_dest);
  741. tx_desc->ctrl.imm = get_unaligned((__be32 *)(ethh->h_dest + 2));
  742. }
  743. /* Handle LSO (TSO) packets */
  744. if (lso_header_size) {
  745. int i;
  746. /* Mark opcode as LSO */
  747. op_own = cpu_to_be32(MLX4_OPCODE_LSO | (1 << 6)) |
  748. ((ring->prod & ring->size) ?
  749. cpu_to_be32(MLX4_EN_BIT_DESC_OWN) : 0);
  750. /* Fill in the LSO prefix */
  751. tx_desc->lso.mss_hdr_size = cpu_to_be32(
  752. shinfo->gso_size << 16 | lso_header_size);
  753. /* Copy headers;
  754. * note that we already verified that it is linear */
  755. memcpy(tx_desc->lso.header, skb->data, lso_header_size);
  756. ring->tso_packets++;
  757. i = ((skb->len - lso_header_size) / shinfo->gso_size) +
  758. !!((skb->len - lso_header_size) % shinfo->gso_size);
  759. tx_info->nr_bytes = skb->len + (i - 1) * lso_header_size;
  760. ring->packets += i;
  761. } else {
  762. /* Normal (Non LSO) packet */
  763. op_own = cpu_to_be32(MLX4_OPCODE_SEND) |
  764. ((ring->prod & ring->size) ?
  765. cpu_to_be32(MLX4_EN_BIT_DESC_OWN) : 0);
  766. tx_info->nr_bytes = max_t(unsigned int, skb->len, ETH_ZLEN);
  767. ring->packets++;
  768. }
  769. ring->bytes += tx_info->nr_bytes;
  770. netdev_tx_sent_queue(ring->tx_queue, tx_info->nr_bytes);
  771. AVG_PERF_COUNTER(priv->pstats.tx_pktsz_avg, skb->len);
  772. if (tx_info->inl)
  773. build_inline_wqe(tx_desc, skb, shinfo, real_size, &vlan_tag,
  774. tx_ind, fragptr);
  775. if (skb->encapsulation) {
  776. struct iphdr *ipv4 = (struct iphdr *)skb_inner_network_header(skb);
  777. if (ipv4->protocol == IPPROTO_TCP || ipv4->protocol == IPPROTO_UDP)
  778. op_own |= cpu_to_be32(MLX4_WQE_CTRL_IIP | MLX4_WQE_CTRL_ILP);
  779. else
  780. op_own |= cpu_to_be32(MLX4_WQE_CTRL_IIP);
  781. }
  782. ring->prod += nr_txbb;
  783. /* If we used a bounce buffer then copy descriptor back into place */
  784. if (unlikely(bounce))
  785. tx_desc = mlx4_en_bounce_to_desc(priv, ring, index, desc_size);
  786. skb_tx_timestamp(skb);
  787. /* Check available TXBBs And 2K spare for prefetch */
  788. stop_queue = mlx4_en_is_tx_ring_full(ring);
  789. if (unlikely(stop_queue)) {
  790. netif_tx_stop_queue(ring->tx_queue);
  791. ring->queue_stopped++;
  792. }
  793. send_doorbell = !skb->xmit_more || netif_xmit_stopped(ring->tx_queue);
  794. real_size = (real_size / 16) & 0x3f;
  795. if (ring->bf_enabled && desc_size <= MAX_BF && !bounce &&
  796. !vlan_tx_tag_present(skb) && send_doorbell) {
  797. tx_desc->ctrl.bf_qpn = ring->doorbell_qpn |
  798. cpu_to_be32(real_size);
  799. op_own |= htonl((bf_index & 0xffff) << 8);
  800. /* Ensure new descriptor hits memory
  801. * before setting ownership of this descriptor to HW
  802. */
  803. wmb();
  804. tx_desc->ctrl.owner_opcode = op_own;
  805. wmb();
  806. mlx4_bf_copy(ring->bf.reg + ring->bf.offset, &tx_desc->ctrl,
  807. desc_size);
  808. wmb();
  809. ring->bf.offset ^= ring->bf.buf_size;
  810. } else {
  811. tx_desc->ctrl.vlan_tag = cpu_to_be16(vlan_tag);
  812. tx_desc->ctrl.ins_vlan = MLX4_WQE_CTRL_INS_VLAN *
  813. !!vlan_tx_tag_present(skb);
  814. tx_desc->ctrl.fence_size = real_size;
  815. /* Ensure new descriptor hits memory
  816. * before setting ownership of this descriptor to HW
  817. */
  818. wmb();
  819. tx_desc->ctrl.owner_opcode = op_own;
  820. if (send_doorbell) {
  821. wmb();
  822. /* Since there is no iowrite*_native() that writes the
  823. * value as is, without byteswapping - using the one
  824. * the doesn't do byteswapping in the relevant arch
  825. * endianness.
  826. */
  827. #if defined(__LITTLE_ENDIAN)
  828. iowrite32(
  829. #else
  830. iowrite32be(
  831. #endif
  832. ring->doorbell_qpn,
  833. ring->bf.uar->map + MLX4_SEND_DOORBELL);
  834. } else {
  835. ring->xmit_more++;
  836. }
  837. }
  838. if (unlikely(stop_queue)) {
  839. /* If queue was emptied after the if (stop_queue) , and before
  840. * the netif_tx_stop_queue() - need to wake the queue,
  841. * or else it will remain stopped forever.
  842. * Need a memory barrier to make sure ring->cons was not
  843. * updated before queue was stopped.
  844. */
  845. smp_rmb();
  846. ring_cons = ACCESS_ONCE(ring->cons);
  847. if (unlikely(!mlx4_en_is_tx_ring_full(ring))) {
  848. netif_tx_wake_queue(ring->tx_queue);
  849. ring->wake_queue++;
  850. }
  851. }
  852. return NETDEV_TX_OK;
  853. tx_drop_unmap:
  854. en_err(priv, "DMA mapping error\n");
  855. while (++i_frag < shinfo->nr_frags) {
  856. ++data;
  857. dma_unmap_page(ddev, (dma_addr_t) be64_to_cpu(data->addr),
  858. be32_to_cpu(data->byte_count),
  859. PCI_DMA_TODEVICE);
  860. }
  861. tx_drop:
  862. dev_kfree_skb_any(skb);
  863. priv->stats.tx_dropped++;
  864. return NETDEV_TX_OK;
  865. }