chain_mode.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*******************************************************************************
  2. Specialised functions for managing Chained mode
  3. Copyright(C) 2011 STMicroelectronics Ltd
  4. It defines all the functions used to handle the normal/enhanced
  5. descriptors in case of the DMA is configured to work in chained or
  6. in ring mode.
  7. This program is free software; you can redistribute it and/or modify it
  8. under the terms and conditions of the GNU General Public License,
  9. version 2, as published by the Free Software Foundation.
  10. This program is distributed in the hope it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. more details.
  14. You should have received a copy of the GNU General Public License along with
  15. this program; if not, write to the Free Software Foundation, Inc.,
  16. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. The full GNU General Public License is included in this distribution in
  18. the file called "COPYING".
  19. Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
  20. *******************************************************************************/
  21. #include "stmmac.h"
  22. static int stmmac_jumbo_frm(void *p, struct sk_buff *skb, int csum)
  23. {
  24. struct stmmac_priv *priv = (struct stmmac_priv *)p;
  25. unsigned int entry = priv->cur_tx;
  26. struct dma_desc *desc = priv->dma_tx + entry;
  27. unsigned int nopaged_len = skb_headlen(skb);
  28. unsigned int bmax;
  29. unsigned int i = 1, len;
  30. if (priv->plat->enh_desc)
  31. bmax = BUF_SIZE_8KiB;
  32. else
  33. bmax = BUF_SIZE_2KiB;
  34. len = nopaged_len - bmax;
  35. desc->des2 = dma_map_single(priv->device, skb->data,
  36. bmax, DMA_TO_DEVICE);
  37. if (dma_mapping_error(priv->device, desc->des2))
  38. return -1;
  39. priv->tx_skbuff_dma[entry].buf = desc->des2;
  40. priv->tx_skbuff_dma[entry].len = bmax;
  41. /* do not close the descriptor and do not set own bit */
  42. priv->hw->desc->prepare_tx_desc(desc, 1, bmax, csum, STMMAC_CHAIN_MODE,
  43. 0, false);
  44. while (len != 0) {
  45. priv->tx_skbuff[entry] = NULL;
  46. entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
  47. desc = priv->dma_tx + entry;
  48. if (len > bmax) {
  49. desc->des2 = dma_map_single(priv->device,
  50. (skb->data + bmax * i),
  51. bmax, DMA_TO_DEVICE);
  52. if (dma_mapping_error(priv->device, desc->des2))
  53. return -1;
  54. priv->tx_skbuff_dma[entry].buf = desc->des2;
  55. priv->tx_skbuff_dma[entry].len = bmax;
  56. priv->hw->desc->prepare_tx_desc(desc, 0, bmax, csum,
  57. STMMAC_CHAIN_MODE, 1,
  58. false);
  59. len -= bmax;
  60. i++;
  61. } else {
  62. desc->des2 = dma_map_single(priv->device,
  63. (skb->data + bmax * i), len,
  64. DMA_TO_DEVICE);
  65. if (dma_mapping_error(priv->device, desc->des2))
  66. return -1;
  67. priv->tx_skbuff_dma[entry].buf = desc->des2;
  68. priv->tx_skbuff_dma[entry].len = len;
  69. /* last descriptor can be set now */
  70. priv->hw->desc->prepare_tx_desc(desc, 0, len, csum,
  71. STMMAC_CHAIN_MODE, 1,
  72. true);
  73. len = 0;
  74. }
  75. }
  76. priv->cur_tx = entry;
  77. return entry;
  78. }
  79. static unsigned int stmmac_is_jumbo_frm(int len, int enh_desc)
  80. {
  81. unsigned int ret = 0;
  82. if ((enh_desc && (len > BUF_SIZE_8KiB)) ||
  83. (!enh_desc && (len > BUF_SIZE_2KiB))) {
  84. ret = 1;
  85. }
  86. return ret;
  87. }
  88. static void stmmac_init_dma_chain(void *des, dma_addr_t phy_addr,
  89. unsigned int size, unsigned int extend_desc)
  90. {
  91. /*
  92. * In chained mode the des3 points to the next element in the ring.
  93. * The latest element has to point to the head.
  94. */
  95. int i;
  96. dma_addr_t dma_phy = phy_addr;
  97. if (extend_desc) {
  98. struct dma_extended_desc *p = (struct dma_extended_desc *)des;
  99. for (i = 0; i < (size - 1); i++) {
  100. dma_phy += sizeof(struct dma_extended_desc);
  101. p->basic.des3 = (unsigned int)dma_phy;
  102. p++;
  103. }
  104. p->basic.des3 = (unsigned int)phy_addr;
  105. } else {
  106. struct dma_desc *p = (struct dma_desc *)des;
  107. for (i = 0; i < (size - 1); i++) {
  108. dma_phy += sizeof(struct dma_desc);
  109. p->des3 = (unsigned int)dma_phy;
  110. p++;
  111. }
  112. p->des3 = (unsigned int)phy_addr;
  113. }
  114. }
  115. static void stmmac_refill_desc3(void *priv_ptr, struct dma_desc *p)
  116. {
  117. struct stmmac_priv *priv = (struct stmmac_priv *)priv_ptr;
  118. if (priv->hwts_rx_en && !priv->extend_desc)
  119. /* NOTE: Device will overwrite des3 with timestamp value if
  120. * 1588-2002 time stamping is enabled, hence reinitialize it
  121. * to keep explicit chaining in the descriptor.
  122. */
  123. p->des3 = (unsigned int)(priv->dma_rx_phy +
  124. (((priv->dirty_rx) + 1) %
  125. DMA_RX_SIZE) *
  126. sizeof(struct dma_desc));
  127. }
  128. static void stmmac_clean_desc3(void *priv_ptr, struct dma_desc *p)
  129. {
  130. struct stmmac_priv *priv = (struct stmmac_priv *)priv_ptr;
  131. unsigned int entry = priv->dirty_tx;
  132. if (priv->tx_skbuff_dma[entry].last_segment && !priv->extend_desc &&
  133. priv->hwts_tx_en)
  134. /* NOTE: Device will overwrite des3 with timestamp value if
  135. * 1588-2002 time stamping is enabled, hence reinitialize it
  136. * to keep explicit chaining in the descriptor.
  137. */
  138. p->des3 = (unsigned int)((priv->dma_tx_phy +
  139. ((priv->dirty_tx + 1) % DMA_TX_SIZE))
  140. * sizeof(struct dma_desc));
  141. }
  142. const struct stmmac_mode_ops chain_mode_ops = {
  143. .init = stmmac_init_dma_chain,
  144. .is_jumbo_frm = stmmac_is_jumbo_frm,
  145. .jumbo_frm = stmmac_jumbo_frm,
  146. .refill_desc3 = stmmac_refill_desc3,
  147. .clean_desc3 = stmmac_clean_desc3,
  148. };