chain_mode.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 txsize = priv->dma_tx_size;
  26. unsigned int entry = priv->cur_tx % txsize;
  27. struct dma_desc *desc = priv->dma_tx + entry;
  28. unsigned int nopaged_len = skb_headlen(skb);
  29. unsigned int bmax;
  30. unsigned int i = 1, len;
  31. if (priv->plat->enh_desc)
  32. bmax = BUF_SIZE_8KiB;
  33. else
  34. bmax = BUF_SIZE_2KiB;
  35. len = nopaged_len - bmax;
  36. desc->des2 = dma_map_single(priv->device, skb->data,
  37. bmax, DMA_TO_DEVICE);
  38. if (dma_mapping_error(priv->device, desc->des2))
  39. return -1;
  40. priv->tx_skbuff_dma[entry].buf = desc->des2;
  41. priv->hw->desc->prepare_tx_desc(desc, 1, bmax, csum, STMMAC_CHAIN_MODE);
  42. while (len != 0) {
  43. priv->tx_skbuff[entry] = NULL;
  44. entry = (++priv->cur_tx) % txsize;
  45. desc = priv->dma_tx + entry;
  46. if (len > bmax) {
  47. desc->des2 = dma_map_single(priv->device,
  48. (skb->data + bmax * i),
  49. bmax, DMA_TO_DEVICE);
  50. if (dma_mapping_error(priv->device, desc->des2))
  51. return -1;
  52. priv->tx_skbuff_dma[entry].buf = desc->des2;
  53. priv->hw->desc->prepare_tx_desc(desc, 0, bmax, csum,
  54. STMMAC_CHAIN_MODE);
  55. priv->hw->desc->set_tx_owner(desc);
  56. len -= bmax;
  57. i++;
  58. } else {
  59. desc->des2 = dma_map_single(priv->device,
  60. (skb->data + bmax * i), len,
  61. DMA_TO_DEVICE);
  62. if (dma_mapping_error(priv->device, desc->des2))
  63. return -1;
  64. priv->tx_skbuff_dma[entry].buf = desc->des2;
  65. priv->hw->desc->prepare_tx_desc(desc, 0, len, csum,
  66. STMMAC_CHAIN_MODE);
  67. priv->hw->desc->set_tx_owner(desc);
  68. len = 0;
  69. }
  70. }
  71. return entry;
  72. }
  73. static unsigned int stmmac_is_jumbo_frm(int len, int enh_desc)
  74. {
  75. unsigned int ret = 0;
  76. if ((enh_desc && (len > BUF_SIZE_8KiB)) ||
  77. (!enh_desc && (len > BUF_SIZE_2KiB))) {
  78. ret = 1;
  79. }
  80. return ret;
  81. }
  82. static void stmmac_init_dma_chain(void *des, dma_addr_t phy_addr,
  83. unsigned int size, unsigned int extend_desc)
  84. {
  85. /*
  86. * In chained mode the des3 points to the next element in the ring.
  87. * The latest element has to point to the head.
  88. */
  89. int i;
  90. dma_addr_t dma_phy = phy_addr;
  91. if (extend_desc) {
  92. struct dma_extended_desc *p = (struct dma_extended_desc *)des;
  93. for (i = 0; i < (size - 1); i++) {
  94. dma_phy += sizeof(struct dma_extended_desc);
  95. p->basic.des3 = (unsigned int)dma_phy;
  96. p++;
  97. }
  98. p->basic.des3 = (unsigned int)phy_addr;
  99. } else {
  100. struct dma_desc *p = (struct dma_desc *)des;
  101. for (i = 0; i < (size - 1); i++) {
  102. dma_phy += sizeof(struct dma_desc);
  103. p->des3 = (unsigned int)dma_phy;
  104. p++;
  105. }
  106. p->des3 = (unsigned int)phy_addr;
  107. }
  108. }
  109. static void stmmac_refill_desc3(void *priv_ptr, struct dma_desc *p)
  110. {
  111. struct stmmac_priv *priv = (struct stmmac_priv *)priv_ptr;
  112. if (priv->hwts_rx_en && !priv->extend_desc)
  113. /* NOTE: Device will overwrite des3 with timestamp value if
  114. * 1588-2002 time stamping is enabled, hence reinitialize it
  115. * to keep explicit chaining in the descriptor.
  116. */
  117. p->des3 = (unsigned int)(priv->dma_rx_phy +
  118. (((priv->dirty_rx) + 1) %
  119. priv->dma_rx_size) *
  120. sizeof(struct dma_desc));
  121. }
  122. static void stmmac_clean_desc3(void *priv_ptr, struct dma_desc *p)
  123. {
  124. struct stmmac_priv *priv = (struct stmmac_priv *)priv_ptr;
  125. if (priv->hw->desc->get_tx_ls(p) && !priv->extend_desc)
  126. /* NOTE: Device will overwrite des3 with timestamp value if
  127. * 1588-2002 time stamping is enabled, hence reinitialize it
  128. * to keep explicit chaining in the descriptor.
  129. */
  130. p->des3 = (unsigned int)(priv->dma_tx_phy +
  131. (((priv->dirty_tx + 1) %
  132. priv->dma_tx_size) *
  133. sizeof(struct dma_desc)));
  134. }
  135. const struct stmmac_mode_ops chain_mode_ops = {
  136. .init = stmmac_init_dma_chain,
  137. .is_jumbo_frm = stmmac_is_jumbo_frm,
  138. .jumbo_frm = stmmac_jumbo_frm,
  139. .refill_desc3 = stmmac_refill_desc3,
  140. .clean_desc3 = stmmac_clean_desc3,
  141. };