en_tx.c 23 KB

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