en_tx.c 21 KB

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