en_tx.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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/vmalloc.h>
  40. #include <linux/tcp.h>
  41. #include <linux/moduleparam.h>
  42. #include "mlx4_en.h"
  43. enum {
  44. MAX_INLINE = 104, /* 128 - 16 - 4 - 4 */
  45. MAX_BF = 256,
  46. };
  47. static int inline_thold __read_mostly = MAX_INLINE;
  48. module_param_named(inline_thold, inline_thold, int, 0444);
  49. MODULE_PARM_DESC(inline_thold, "threshold for using inline data");
  50. int mlx4_en_create_tx_ring(struct mlx4_en_priv *priv,
  51. struct mlx4_en_tx_ring **pring, int qpn, u32 size,
  52. u16 stride, int node)
  53. {
  54. struct mlx4_en_dev *mdev = priv->mdev;
  55. struct mlx4_en_tx_ring *ring;
  56. int tmp;
  57. int err;
  58. ring = kzalloc_node(sizeof(*ring), GFP_KERNEL, node);
  59. if (!ring) {
  60. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  61. if (!ring) {
  62. en_err(priv, "Failed allocating TX ring\n");
  63. return -ENOMEM;
  64. }
  65. }
  66. ring->size = size;
  67. ring->size_mask = size - 1;
  68. ring->stride = stride;
  69. inline_thold = min(inline_thold, MAX_INLINE);
  70. tmp = size * sizeof(struct mlx4_en_tx_info);
  71. ring->tx_info = vmalloc_node(tmp, node);
  72. if (!ring->tx_info) {
  73. ring->tx_info = vmalloc(tmp);
  74. if (!ring->tx_info) {
  75. err = -ENOMEM;
  76. goto err_ring;
  77. }
  78. }
  79. en_dbg(DRV, priv, "Allocated tx_info ring at addr:%p size:%d\n",
  80. ring->tx_info, tmp);
  81. ring->bounce_buf = kmalloc_node(MAX_DESC_SIZE, GFP_KERNEL, node);
  82. if (!ring->bounce_buf) {
  83. ring->bounce_buf = kmalloc(MAX_DESC_SIZE, GFP_KERNEL);
  84. if (!ring->bounce_buf) {
  85. err = -ENOMEM;
  86. goto err_info;
  87. }
  88. }
  89. ring->buf_size = ALIGN(size * ring->stride, MLX4_EN_PAGE_SIZE);
  90. /* Allocate HW buffers on provided NUMA node */
  91. set_dev_node(&mdev->dev->pdev->dev, node);
  92. err = mlx4_alloc_hwq_res(mdev->dev, &ring->wqres, ring->buf_size,
  93. 2 * PAGE_SIZE);
  94. set_dev_node(&mdev->dev->pdev->dev, mdev->dev->numa_node);
  95. if (err) {
  96. en_err(priv, "Failed allocating hwq resources\n");
  97. goto err_bounce;
  98. }
  99. err = mlx4_en_map_buffer(&ring->wqres.buf);
  100. if (err) {
  101. en_err(priv, "Failed to map TX buffer\n");
  102. goto err_hwq_res;
  103. }
  104. ring->buf = ring->wqres.buf.direct.buf;
  105. en_dbg(DRV, priv, "Allocated TX ring (addr:%p) - buf:%p size:%d "
  106. "buf_size:%d dma:%llx\n", ring, ring->buf, ring->size,
  107. ring->buf_size, (unsigned long long) ring->wqres.buf.direct.map);
  108. ring->qpn = qpn;
  109. err = mlx4_qp_alloc(mdev->dev, ring->qpn, &ring->qp);
  110. if (err) {
  111. en_err(priv, "Failed allocating qp %d\n", ring->qpn);
  112. goto err_map;
  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)", err);
  118. ring->bf.uar = &mdev->priv_uar;
  119. ring->bf.uar->map = mdev->uar_map;
  120. ring->bf_enabled = false;
  121. } else
  122. ring->bf_enabled = true;
  123. ring->hwtstamp_tx_type = priv->hwtstamp_config.tx_type;
  124. *pring = ring;
  125. return 0;
  126. err_map:
  127. mlx4_en_unmap_buffer(&ring->wqres.buf);
  128. err_hwq_res:
  129. mlx4_free_hwq_res(mdev->dev, &ring->wqres, ring->buf_size);
  130. err_bounce:
  131. kfree(ring->bounce_buf);
  132. ring->bounce_buf = NULL;
  133. err_info:
  134. vfree(ring->tx_info);
  135. ring->tx_info = NULL;
  136. err_ring:
  137. kfree(ring);
  138. *pring = NULL;
  139. return err;
  140. }
  141. void mlx4_en_destroy_tx_ring(struct mlx4_en_priv *priv,
  142. struct mlx4_en_tx_ring **pring)
  143. {
  144. struct mlx4_en_dev *mdev = priv->mdev;
  145. struct mlx4_en_tx_ring *ring = *pring;
  146. en_dbg(DRV, priv, "Destroying tx ring, qpn: %d\n", ring->qpn);
  147. if (ring->bf_enabled)
  148. mlx4_bf_free(mdev->dev, &ring->bf);
  149. mlx4_qp_remove(mdev->dev, &ring->qp);
  150. mlx4_qp_free(mdev->dev, &ring->qp);
  151. mlx4_en_unmap_buffer(&ring->wqres.buf);
  152. mlx4_free_hwq_res(mdev->dev, &ring->wqres, ring->buf_size);
  153. kfree(ring->bounce_buf);
  154. ring->bounce_buf = NULL;
  155. vfree(ring->tx_info);
  156. ring->tx_info = NULL;
  157. kfree(ring);
  158. *pring = NULL;
  159. }
  160. int mlx4_en_activate_tx_ring(struct mlx4_en_priv *priv,
  161. struct mlx4_en_tx_ring *ring,
  162. int cq, int user_prio)
  163. {
  164. struct mlx4_en_dev *mdev = priv->mdev;
  165. int err;
  166. ring->cqn = cq;
  167. ring->prod = 0;
  168. ring->cons = 0xffffffff;
  169. ring->last_nr_txbb = 1;
  170. ring->poll_cnt = 0;
  171. memset(ring->tx_info, 0, ring->size * sizeof(struct mlx4_en_tx_info));
  172. memset(ring->buf, 0, ring->buf_size);
  173. ring->qp_state = MLX4_QP_STATE_RST;
  174. ring->doorbell_qpn = ring->qp.qpn << 8;
  175. mlx4_en_fill_qp_context(priv, ring->size, ring->stride, 1, 0, ring->qpn,
  176. ring->cqn, user_prio, &ring->context);
  177. if (ring->bf_enabled)
  178. ring->context.usr_page = cpu_to_be32(ring->bf.uar->index);
  179. err = mlx4_qp_to_ready(mdev->dev, &ring->wqres.mtt, &ring->context,
  180. &ring->qp, &ring->qp_state);
  181. return err;
  182. }
  183. void mlx4_en_deactivate_tx_ring(struct mlx4_en_priv *priv,
  184. struct mlx4_en_tx_ring *ring)
  185. {
  186. struct mlx4_en_dev *mdev = priv->mdev;
  187. mlx4_qp_modify(mdev->dev, NULL, ring->qp_state,
  188. MLX4_QP_STATE_RST, NULL, 0, 0, &ring->qp);
  189. }
  190. static void mlx4_en_stamp_wqe(struct mlx4_en_priv *priv,
  191. struct mlx4_en_tx_ring *ring, int index,
  192. u8 owner)
  193. {
  194. __be32 stamp = cpu_to_be32(STAMP_VAL | (!!owner << STAMP_SHIFT));
  195. struct mlx4_en_tx_desc *tx_desc = ring->buf + index * TXBB_SIZE;
  196. struct mlx4_en_tx_info *tx_info = &ring->tx_info[index];
  197. void *end = ring->buf + ring->buf_size;
  198. __be32 *ptr = (__be32 *)tx_desc;
  199. int i;
  200. /* Optimize the common case when there are no wraparounds */
  201. if (likely((void *)tx_desc + tx_info->nr_txbb * TXBB_SIZE <= end)) {
  202. /* Stamp the freed descriptor */
  203. for (i = 0; i < tx_info->nr_txbb * TXBB_SIZE;
  204. i += STAMP_STRIDE) {
  205. *ptr = stamp;
  206. ptr += STAMP_DWORDS;
  207. }
  208. } else {
  209. /* Stamp the freed descriptor */
  210. for (i = 0; i < tx_info->nr_txbb * TXBB_SIZE;
  211. i += STAMP_STRIDE) {
  212. *ptr = stamp;
  213. ptr += STAMP_DWORDS;
  214. if ((void *)ptr >= end) {
  215. ptr = ring->buf;
  216. stamp ^= cpu_to_be32(0x80000000);
  217. }
  218. }
  219. }
  220. }
  221. static u32 mlx4_en_free_tx_desc(struct mlx4_en_priv *priv,
  222. struct mlx4_en_tx_ring *ring,
  223. int index, u8 owner, u64 timestamp)
  224. {
  225. struct mlx4_en_dev *mdev = priv->mdev;
  226. struct mlx4_en_tx_info *tx_info = &ring->tx_info[index];
  227. struct mlx4_en_tx_desc *tx_desc = ring->buf + index * TXBB_SIZE;
  228. struct mlx4_wqe_data_seg *data = (void *) tx_desc + tx_info->data_offset;
  229. struct sk_buff *skb = tx_info->skb;
  230. struct skb_frag_struct *frag;
  231. void *end = ring->buf + ring->buf_size;
  232. int frags = skb_shinfo(skb)->nr_frags;
  233. int i;
  234. struct skb_shared_hwtstamps hwts;
  235. if (timestamp) {
  236. mlx4_en_fill_hwtstamps(mdev, &hwts, timestamp);
  237. skb_tstamp_tx(skb, &hwts);
  238. }
  239. /* Optimize the common case when there are no wraparounds */
  240. if (likely((void *) tx_desc + tx_info->nr_txbb * TXBB_SIZE <= end)) {
  241. if (!tx_info->inl) {
  242. if (tx_info->linear) {
  243. dma_unmap_single(priv->ddev,
  244. (dma_addr_t) be64_to_cpu(data->addr),
  245. be32_to_cpu(data->byte_count),
  246. PCI_DMA_TODEVICE);
  247. ++data;
  248. }
  249. for (i = 0; i < frags; i++) {
  250. frag = &skb_shinfo(skb)->frags[i];
  251. dma_unmap_page(priv->ddev,
  252. (dma_addr_t) be64_to_cpu(data[i].addr),
  253. skb_frag_size(frag), PCI_DMA_TODEVICE);
  254. }
  255. }
  256. } else {
  257. if (!tx_info->inl) {
  258. if ((void *) data >= end) {
  259. data = ring->buf + ((void *)data - end);
  260. }
  261. if (tx_info->linear) {
  262. dma_unmap_single(priv->ddev,
  263. (dma_addr_t) be64_to_cpu(data->addr),
  264. be32_to_cpu(data->byte_count),
  265. PCI_DMA_TODEVICE);
  266. ++data;
  267. }
  268. for (i = 0; i < frags; i++) {
  269. /* Check for wraparound before unmapping */
  270. if ((void *) data >= end)
  271. data = ring->buf;
  272. frag = &skb_shinfo(skb)->frags[i];
  273. dma_unmap_page(priv->ddev,
  274. (dma_addr_t) be64_to_cpu(data->addr),
  275. skb_frag_size(frag), PCI_DMA_TODEVICE);
  276. ++data;
  277. }
  278. }
  279. }
  280. dev_kfree_skb_any(skb);
  281. return tx_info->nr_txbb;
  282. }
  283. int mlx4_en_free_tx_buf(struct net_device *dev, struct mlx4_en_tx_ring *ring)
  284. {
  285. struct mlx4_en_priv *priv = netdev_priv(dev);
  286. int cnt = 0;
  287. /* Skip last polled descriptor */
  288. ring->cons += ring->last_nr_txbb;
  289. en_dbg(DRV, priv, "Freeing Tx buf - cons:0x%x prod:0x%x\n",
  290. ring->cons, ring->prod);
  291. if ((u32) (ring->prod - ring->cons) > ring->size) {
  292. if (netif_msg_tx_err(priv))
  293. en_warn(priv, "Tx consumer passed producer!\n");
  294. return 0;
  295. }
  296. while (ring->cons != ring->prod) {
  297. ring->last_nr_txbb = mlx4_en_free_tx_desc(priv, ring,
  298. ring->cons & ring->size_mask,
  299. !!(ring->cons & ring->size), 0);
  300. ring->cons += ring->last_nr_txbb;
  301. cnt++;
  302. }
  303. netdev_tx_reset_queue(ring->tx_queue);
  304. if (cnt)
  305. en_dbg(DRV, priv, "Freed %d uncompleted tx descriptors\n", cnt);
  306. return cnt;
  307. }
  308. static void mlx4_en_process_tx_cq(struct net_device *dev, struct mlx4_en_cq *cq)
  309. {
  310. struct mlx4_en_priv *priv = netdev_priv(dev);
  311. struct mlx4_cq *mcq = &cq->mcq;
  312. struct mlx4_en_tx_ring *ring = priv->tx_ring[cq->ring];
  313. struct mlx4_cqe *cqe;
  314. u16 index;
  315. u16 new_index, ring_index, stamp_index;
  316. u32 txbbs_skipped = 0;
  317. u32 txbbs_stamp = 0;
  318. u32 cons_index = mcq->cons_index;
  319. int size = cq->size;
  320. u32 size_mask = ring->size_mask;
  321. struct mlx4_cqe *buf = cq->buf;
  322. u32 packets = 0;
  323. u32 bytes = 0;
  324. int factor = priv->cqe_factor;
  325. u64 timestamp = 0;
  326. if (!priv->port_up)
  327. return;
  328. index = cons_index & size_mask;
  329. cqe = &buf[(index << factor) + factor];
  330. ring_index = ring->cons & size_mask;
  331. stamp_index = ring_index;
  332. /* Process all completed CQEs */
  333. while (XNOR(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK,
  334. cons_index & size)) {
  335. /*
  336. * make sure we read the CQE after we read the
  337. * ownership bit
  338. */
  339. rmb();
  340. if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
  341. MLX4_CQE_OPCODE_ERROR)) {
  342. struct mlx4_err_cqe *cqe_err = (struct mlx4_err_cqe *)cqe;
  343. en_err(priv, "CQE error - vendor syndrome: 0x%x syndrome: 0x%x\n",
  344. cqe_err->vendor_err_syndrome,
  345. cqe_err->syndrome);
  346. }
  347. /* Skip over last polled CQE */
  348. new_index = be16_to_cpu(cqe->wqe_index) & size_mask;
  349. do {
  350. txbbs_skipped += ring->last_nr_txbb;
  351. ring_index = (ring_index + ring->last_nr_txbb) & size_mask;
  352. if (ring->tx_info[ring_index].ts_requested)
  353. timestamp = mlx4_en_get_cqe_ts(cqe);
  354. /* free next descriptor */
  355. ring->last_nr_txbb = mlx4_en_free_tx_desc(
  356. priv, ring, ring_index,
  357. !!((ring->cons + txbbs_skipped) &
  358. ring->size), timestamp);
  359. mlx4_en_stamp_wqe(priv, ring, stamp_index,
  360. !!((ring->cons + txbbs_stamp) &
  361. ring->size));
  362. stamp_index = ring_index;
  363. txbbs_stamp = txbbs_skipped;
  364. packets++;
  365. bytes += ring->tx_info[ring_index].nr_bytes;
  366. } while (ring_index != new_index);
  367. ++cons_index;
  368. index = cons_index & size_mask;
  369. cqe = &buf[(index << factor) + factor];
  370. }
  371. /*
  372. * To prevent CQ overflow we first update CQ consumer and only then
  373. * the ring consumer.
  374. */
  375. mcq->cons_index = cons_index;
  376. mlx4_cq_set_ci(mcq);
  377. wmb();
  378. ring->cons += txbbs_skipped;
  379. netdev_tx_completed_queue(ring->tx_queue, packets, bytes);
  380. /*
  381. * Wakeup Tx queue if this stopped, and at least 1 packet
  382. * was completed
  383. */
  384. if (netif_tx_queue_stopped(ring->tx_queue) && txbbs_skipped > 0) {
  385. netif_tx_wake_queue(ring->tx_queue);
  386. priv->port_stats.wake_queue++;
  387. }
  388. }
  389. void mlx4_en_tx_irq(struct mlx4_cq *mcq)
  390. {
  391. struct mlx4_en_cq *cq = container_of(mcq, struct mlx4_en_cq, mcq);
  392. struct mlx4_en_priv *priv = netdev_priv(cq->dev);
  393. mlx4_en_process_tx_cq(cq->dev, cq);
  394. mlx4_en_arm_cq(priv, cq);
  395. }
  396. static struct mlx4_en_tx_desc *mlx4_en_bounce_to_desc(struct mlx4_en_priv *priv,
  397. struct mlx4_en_tx_ring *ring,
  398. u32 index,
  399. unsigned int desc_size)
  400. {
  401. u32 copy = (ring->size - index) * TXBB_SIZE;
  402. int i;
  403. for (i = desc_size - copy - 4; i >= 0; i -= 4) {
  404. if ((i & (TXBB_SIZE - 1)) == 0)
  405. wmb();
  406. *((u32 *) (ring->buf + i)) =
  407. *((u32 *) (ring->bounce_buf + copy + i));
  408. }
  409. for (i = copy - 4; i >= 4 ; i -= 4) {
  410. if ((i & (TXBB_SIZE - 1)) == 0)
  411. wmb();
  412. *((u32 *) (ring->buf + index * TXBB_SIZE + i)) =
  413. *((u32 *) (ring->bounce_buf + i));
  414. }
  415. /* Return real descriptor location */
  416. return ring->buf + index * TXBB_SIZE;
  417. }
  418. static int is_inline(struct sk_buff *skb, void **pfrag)
  419. {
  420. void *ptr;
  421. if (inline_thold && !skb_is_gso(skb) && skb->len <= inline_thold) {
  422. if (skb_shinfo(skb)->nr_frags == 1) {
  423. ptr = skb_frag_address_safe(&skb_shinfo(skb)->frags[0]);
  424. if (unlikely(!ptr))
  425. return 0;
  426. if (pfrag)
  427. *pfrag = ptr;
  428. return 1;
  429. } else if (unlikely(skb_shinfo(skb)->nr_frags))
  430. return 0;
  431. else
  432. return 1;
  433. }
  434. return 0;
  435. }
  436. static int inline_size(struct sk_buff *skb)
  437. {
  438. if (skb->len + CTRL_SIZE + sizeof(struct mlx4_wqe_inline_seg)
  439. <= MLX4_INLINE_ALIGN)
  440. return ALIGN(skb->len + CTRL_SIZE +
  441. sizeof(struct mlx4_wqe_inline_seg), 16);
  442. else
  443. return ALIGN(skb->len + CTRL_SIZE + 2 *
  444. sizeof(struct mlx4_wqe_inline_seg), 16);
  445. }
  446. static int get_real_size(struct sk_buff *skb, struct net_device *dev,
  447. int *lso_header_size)
  448. {
  449. struct mlx4_en_priv *priv = netdev_priv(dev);
  450. int real_size;
  451. if (skb_is_gso(skb)) {
  452. *lso_header_size = skb_transport_offset(skb) + tcp_hdrlen(skb);
  453. real_size = CTRL_SIZE + skb_shinfo(skb)->nr_frags * DS_SIZE +
  454. ALIGN(*lso_header_size + 4, DS_SIZE);
  455. if (unlikely(*lso_header_size != skb_headlen(skb))) {
  456. /* We add a segment for the skb linear buffer only if
  457. * it contains data */
  458. if (*lso_header_size < skb_headlen(skb))
  459. real_size += DS_SIZE;
  460. else {
  461. if (netif_msg_tx_err(priv))
  462. en_warn(priv, "Non-linear headers\n");
  463. return 0;
  464. }
  465. }
  466. } else {
  467. *lso_header_size = 0;
  468. if (!is_inline(skb, NULL))
  469. real_size = CTRL_SIZE + (skb_shinfo(skb)->nr_frags + 1) * DS_SIZE;
  470. else
  471. real_size = inline_size(skb);
  472. }
  473. return real_size;
  474. }
  475. static void build_inline_wqe(struct mlx4_en_tx_desc *tx_desc, struct sk_buff *skb,
  476. int real_size, u16 *vlan_tag, int tx_ind, void *fragptr)
  477. {
  478. struct mlx4_wqe_inline_seg *inl = &tx_desc->inl;
  479. int spc = MLX4_INLINE_ALIGN - CTRL_SIZE - sizeof *inl;
  480. if (skb->len <= spc) {
  481. inl->byte_count = cpu_to_be32(1 << 31 | skb->len);
  482. skb_copy_from_linear_data(skb, inl + 1, skb_headlen(skb));
  483. if (skb_shinfo(skb)->nr_frags)
  484. memcpy(((void *)(inl + 1)) + skb_headlen(skb), fragptr,
  485. skb_frag_size(&skb_shinfo(skb)->frags[0]));
  486. } else {
  487. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  488. if (skb_headlen(skb) <= spc) {
  489. skb_copy_from_linear_data(skb, inl + 1, skb_headlen(skb));
  490. if (skb_headlen(skb) < spc) {
  491. memcpy(((void *)(inl + 1)) + skb_headlen(skb),
  492. fragptr, spc - skb_headlen(skb));
  493. fragptr += spc - skb_headlen(skb);
  494. }
  495. inl = (void *) (inl + 1) + spc;
  496. memcpy(((void *)(inl + 1)), fragptr, skb->len - spc);
  497. } else {
  498. skb_copy_from_linear_data(skb, inl + 1, spc);
  499. inl = (void *) (inl + 1) + spc;
  500. skb_copy_from_linear_data_offset(skb, spc, inl + 1,
  501. skb_headlen(skb) - spc);
  502. if (skb_shinfo(skb)->nr_frags)
  503. memcpy(((void *)(inl + 1)) + skb_headlen(skb) - spc,
  504. fragptr, skb_frag_size(&skb_shinfo(skb)->frags[0]));
  505. }
  506. wmb();
  507. inl->byte_count = cpu_to_be32(1 << 31 | (skb->len - spc));
  508. }
  509. }
  510. u16 mlx4_en_select_queue(struct net_device *dev, struct sk_buff *skb)
  511. {
  512. struct mlx4_en_priv *priv = netdev_priv(dev);
  513. u16 rings_p_up = priv->num_tx_rings_p_up;
  514. u8 up = 0;
  515. if (dev->num_tc)
  516. return skb_tx_hash(dev, skb);
  517. if (vlan_tx_tag_present(skb))
  518. up = vlan_tx_tag_get(skb) >> VLAN_PRIO_SHIFT;
  519. return __netdev_pick_tx(dev, skb) % rings_p_up + up * rings_p_up;
  520. }
  521. static void mlx4_bf_copy(void __iomem *dst, unsigned long *src, unsigned bytecnt)
  522. {
  523. __iowrite64_copy(dst, src, bytecnt / 8);
  524. }
  525. netdev_tx_t mlx4_en_xmit(struct sk_buff *skb, struct net_device *dev)
  526. {
  527. struct mlx4_en_priv *priv = netdev_priv(dev);
  528. struct mlx4_en_dev *mdev = priv->mdev;
  529. struct device *ddev = priv->ddev;
  530. struct mlx4_en_tx_ring *ring;
  531. struct mlx4_en_tx_desc *tx_desc;
  532. struct mlx4_wqe_data_seg *data;
  533. struct mlx4_en_tx_info *tx_info;
  534. int tx_ind = 0;
  535. int nr_txbb;
  536. int desc_size;
  537. int real_size;
  538. u32 index, bf_index;
  539. __be32 op_own;
  540. u16 vlan_tag = 0;
  541. int i;
  542. int lso_header_size;
  543. void *fragptr;
  544. bool bounce = false;
  545. if (!priv->port_up)
  546. goto tx_drop;
  547. real_size = get_real_size(skb, dev, &lso_header_size);
  548. if (unlikely(!real_size))
  549. goto tx_drop;
  550. /* Align descriptor to TXBB size */
  551. desc_size = ALIGN(real_size, TXBB_SIZE);
  552. nr_txbb = desc_size / TXBB_SIZE;
  553. if (unlikely(nr_txbb > MAX_DESC_TXBBS)) {
  554. if (netif_msg_tx_err(priv))
  555. en_warn(priv, "Oversized header or SG list\n");
  556. goto tx_drop;
  557. }
  558. tx_ind = skb->queue_mapping;
  559. ring = priv->tx_ring[tx_ind];
  560. if (vlan_tx_tag_present(skb))
  561. vlan_tag = vlan_tx_tag_get(skb);
  562. /* Check available TXBBs And 2K spare for prefetch */
  563. if (unlikely(((int)(ring->prod - ring->cons)) >
  564. ring->size - HEADROOM - MAX_DESC_TXBBS)) {
  565. /* every full Tx ring stops queue */
  566. netif_tx_stop_queue(ring->tx_queue);
  567. priv->port_stats.queue_stopped++;
  568. /* If queue was emptied after the if, and before the
  569. * stop_queue - need to wake the queue, or else it will remain
  570. * stopped forever.
  571. * Need a memory barrier to make sure ring->cons was not
  572. * updated before queue was stopped.
  573. */
  574. wmb();
  575. if (unlikely(((int)(ring->prod - ring->cons)) <=
  576. ring->size - HEADROOM - MAX_DESC_TXBBS)) {
  577. netif_tx_wake_queue(ring->tx_queue);
  578. priv->port_stats.wake_queue++;
  579. } else {
  580. return NETDEV_TX_BUSY;
  581. }
  582. }
  583. /* Track current inflight packets for performance analysis */
  584. AVG_PERF_COUNTER(priv->pstats.inflight_avg,
  585. (u32) (ring->prod - ring->cons - 1));
  586. /* Packet is good - grab an index and transmit it */
  587. index = ring->prod & ring->size_mask;
  588. bf_index = ring->prod;
  589. /* See if we have enough space for whole descriptor TXBB for setting
  590. * SW ownership on next descriptor; if not, use a bounce buffer. */
  591. if (likely(index + nr_txbb <= ring->size))
  592. tx_desc = ring->buf + index * TXBB_SIZE;
  593. else {
  594. tx_desc = (struct mlx4_en_tx_desc *) ring->bounce_buf;
  595. bounce = true;
  596. }
  597. /* Save skb in tx_info ring */
  598. tx_info = &ring->tx_info[index];
  599. tx_info->skb = skb;
  600. tx_info->nr_txbb = nr_txbb;
  601. if (lso_header_size)
  602. data = ((void *)&tx_desc->lso + ALIGN(lso_header_size + 4,
  603. DS_SIZE));
  604. else
  605. data = &tx_desc->data;
  606. /* valid only for none inline segments */
  607. tx_info->data_offset = (void *)data - (void *)tx_desc;
  608. tx_info->linear = (lso_header_size < skb_headlen(skb) &&
  609. !is_inline(skb, NULL)) ? 1 : 0;
  610. data += skb_shinfo(skb)->nr_frags + tx_info->linear - 1;
  611. if (is_inline(skb, &fragptr)) {
  612. tx_info->inl = 1;
  613. } else {
  614. /* Map fragments */
  615. for (i = skb_shinfo(skb)->nr_frags - 1; i >= 0; i--) {
  616. struct skb_frag_struct *frag;
  617. dma_addr_t dma;
  618. frag = &skb_shinfo(skb)->frags[i];
  619. dma = skb_frag_dma_map(ddev, frag,
  620. 0, skb_frag_size(frag),
  621. DMA_TO_DEVICE);
  622. if (dma_mapping_error(ddev, dma))
  623. goto tx_drop_unmap;
  624. data->addr = cpu_to_be64(dma);
  625. data->lkey = cpu_to_be32(mdev->mr.key);
  626. wmb();
  627. data->byte_count = cpu_to_be32(skb_frag_size(frag));
  628. --data;
  629. }
  630. /* Map linear part */
  631. if (tx_info->linear) {
  632. u32 byte_count = skb_headlen(skb) - lso_header_size;
  633. dma_addr_t dma;
  634. dma = dma_map_single(ddev, skb->data +
  635. lso_header_size, byte_count,
  636. PCI_DMA_TODEVICE);
  637. if (dma_mapping_error(ddev, dma))
  638. goto tx_drop_unmap;
  639. data->addr = cpu_to_be64(dma);
  640. data->lkey = cpu_to_be32(mdev->mr.key);
  641. wmb();
  642. data->byte_count = cpu_to_be32(byte_count);
  643. }
  644. tx_info->inl = 0;
  645. }
  646. /*
  647. * For timestamping add flag to skb_shinfo and
  648. * set flag for further reference
  649. */
  650. if (ring->hwtstamp_tx_type == HWTSTAMP_TX_ON &&
  651. skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
  652. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  653. tx_info->ts_requested = 1;
  654. }
  655. /* Prepare ctrl segement apart opcode+ownership, which depends on
  656. * whether LSO is used */
  657. tx_desc->ctrl.vlan_tag = cpu_to_be16(vlan_tag);
  658. tx_desc->ctrl.ins_vlan = MLX4_WQE_CTRL_INS_VLAN *
  659. !!vlan_tx_tag_present(skb);
  660. tx_desc->ctrl.fence_size = (real_size / 16) & 0x3f;
  661. tx_desc->ctrl.srcrb_flags = priv->ctrl_flags;
  662. if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
  663. tx_desc->ctrl.srcrb_flags |= cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM |
  664. MLX4_WQE_CTRL_TCP_UDP_CSUM);
  665. ring->tx_csum++;
  666. }
  667. if (priv->flags & MLX4_EN_FLAG_ENABLE_HW_LOOPBACK) {
  668. struct ethhdr *ethh;
  669. /* Copy dst mac address to wqe. This allows loopback in eSwitch,
  670. * so that VFs and PF can communicate with each other
  671. */
  672. ethh = (struct ethhdr *)skb->data;
  673. tx_desc->ctrl.srcrb_flags16[0] = get_unaligned((__be16 *)ethh->h_dest);
  674. tx_desc->ctrl.imm = get_unaligned((__be32 *)(ethh->h_dest + 2));
  675. }
  676. /* Handle LSO (TSO) packets */
  677. if (lso_header_size) {
  678. /* Mark opcode as LSO */
  679. op_own = cpu_to_be32(MLX4_OPCODE_LSO | (1 << 6)) |
  680. ((ring->prod & ring->size) ?
  681. cpu_to_be32(MLX4_EN_BIT_DESC_OWN) : 0);
  682. /* Fill in the LSO prefix */
  683. tx_desc->lso.mss_hdr_size = cpu_to_be32(
  684. skb_shinfo(skb)->gso_size << 16 | lso_header_size);
  685. /* Copy headers;
  686. * note that we already verified that it is linear */
  687. memcpy(tx_desc->lso.header, skb->data, lso_header_size);
  688. priv->port_stats.tso_packets++;
  689. i = ((skb->len - lso_header_size) / skb_shinfo(skb)->gso_size) +
  690. !!((skb->len - lso_header_size) % skb_shinfo(skb)->gso_size);
  691. tx_info->nr_bytes = skb->len + (i - 1) * lso_header_size;
  692. ring->packets += i;
  693. } else {
  694. /* Normal (Non LSO) packet */
  695. op_own = cpu_to_be32(MLX4_OPCODE_SEND) |
  696. ((ring->prod & ring->size) ?
  697. cpu_to_be32(MLX4_EN_BIT_DESC_OWN) : 0);
  698. tx_info->nr_bytes = max_t(unsigned int, skb->len, ETH_ZLEN);
  699. ring->packets++;
  700. }
  701. ring->bytes += tx_info->nr_bytes;
  702. netdev_tx_sent_queue(ring->tx_queue, tx_info->nr_bytes);
  703. AVG_PERF_COUNTER(priv->pstats.tx_pktsz_avg, skb->len);
  704. if (tx_info->inl) {
  705. build_inline_wqe(tx_desc, skb, real_size, &vlan_tag, tx_ind, fragptr);
  706. tx_info->inl = 1;
  707. }
  708. ring->prod += nr_txbb;
  709. /* If we used a bounce buffer then copy descriptor back into place */
  710. if (bounce)
  711. tx_desc = mlx4_en_bounce_to_desc(priv, ring, index, desc_size);
  712. skb_tx_timestamp(skb);
  713. if (ring->bf_enabled && desc_size <= MAX_BF && !bounce && !vlan_tx_tag_present(skb)) {
  714. *(__be32 *) (&tx_desc->ctrl.vlan_tag) |= cpu_to_be32(ring->doorbell_qpn);
  715. op_own |= htonl((bf_index & 0xffff) << 8);
  716. /* Ensure new descirptor hits memory
  717. * before setting ownership of this descriptor to HW */
  718. wmb();
  719. tx_desc->ctrl.owner_opcode = op_own;
  720. wmb();
  721. mlx4_bf_copy(ring->bf.reg + ring->bf.offset, (unsigned long *) &tx_desc->ctrl,
  722. desc_size);
  723. wmb();
  724. ring->bf.offset ^= ring->bf.buf_size;
  725. } else {
  726. /* Ensure new descirptor hits memory
  727. * before setting ownership of this descriptor to HW */
  728. wmb();
  729. tx_desc->ctrl.owner_opcode = op_own;
  730. wmb();
  731. iowrite32be(ring->doorbell_qpn, ring->bf.uar->map + MLX4_SEND_DOORBELL);
  732. }
  733. return NETDEV_TX_OK;
  734. tx_drop_unmap:
  735. en_err(priv, "DMA mapping error\n");
  736. for (i++; i < skb_shinfo(skb)->nr_frags; i++) {
  737. data++;
  738. dma_unmap_page(ddev, (dma_addr_t) be64_to_cpu(data->addr),
  739. be32_to_cpu(data->byte_count),
  740. PCI_DMA_TODEVICE);
  741. }
  742. tx_drop:
  743. dev_kfree_skb_any(skb);
  744. priv->stats.tx_dropped++;
  745. return NETDEV_TX_OK;
  746. }