norm_desc.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*******************************************************************************
  2. This contains the functions to handle the normal descriptors.
  3. Copyright (C) 2007-2009 STMicroelectronics Ltd
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms and conditions of the GNU General Public License,
  6. version 2, as published by the Free Software Foundation.
  7. This program is distributed in the hope it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  14. The full GNU General Public License is included in this distribution in
  15. the file called "COPYING".
  16. Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
  17. *******************************************************************************/
  18. #include <linux/stmmac.h>
  19. #include "common.h"
  20. #include "descs_com.h"
  21. static int ndesc_get_tx_status(void *data, struct stmmac_extra_stats *x,
  22. struct dma_desc *p, void __iomem *ioaddr)
  23. {
  24. struct net_device_stats *stats = (struct net_device_stats *)data;
  25. unsigned int tdes0 = p->des0;
  26. unsigned int tdes1 = p->des1;
  27. int ret = tx_done;
  28. /* Get tx owner first */
  29. if (unlikely(tdes0 & TDES0_OWN))
  30. return tx_dma_own;
  31. /* Verify tx error by looking at the last segment. */
  32. if (likely(!(tdes1 & TDES1_LAST_SEGMENT)))
  33. return tx_not_ls;
  34. if (unlikely(tdes0 & TDES0_ERROR_SUMMARY)) {
  35. if (unlikely(tdes0 & TDES0_UNDERFLOW_ERROR)) {
  36. x->tx_underflow++;
  37. stats->tx_fifo_errors++;
  38. }
  39. if (unlikely(tdes0 & TDES0_NO_CARRIER)) {
  40. x->tx_carrier++;
  41. stats->tx_carrier_errors++;
  42. }
  43. if (unlikely(tdes0 & TDES0_LOSS_CARRIER)) {
  44. x->tx_losscarrier++;
  45. stats->tx_carrier_errors++;
  46. }
  47. if (unlikely((tdes0 & TDES0_EXCESSIVE_DEFERRAL) ||
  48. (tdes0 & TDES0_EXCESSIVE_COLLISIONS) ||
  49. (tdes0 & TDES0_LATE_COLLISION))) {
  50. unsigned int collisions;
  51. collisions = (tdes0 & TDES0_COLLISION_COUNT_MASK) >> 3;
  52. stats->collisions += collisions;
  53. }
  54. ret = tx_err;
  55. }
  56. if (tdes0 & TDES0_VLAN_FRAME)
  57. x->tx_vlan++;
  58. if (unlikely(tdes0 & TDES0_DEFERRED))
  59. x->tx_deferred++;
  60. return ret;
  61. }
  62. static int ndesc_get_tx_len(struct dma_desc *p)
  63. {
  64. return (p->des1 & RDES1_BUFFER1_SIZE_MASK);
  65. }
  66. /* This function verifies if each incoming frame has some errors
  67. * and, if required, updates the multicast statistics.
  68. * In case of success, it returns good_frame because the GMAC device
  69. * is supposed to be able to compute the csum in HW. */
  70. static int ndesc_get_rx_status(void *data, struct stmmac_extra_stats *x,
  71. struct dma_desc *p)
  72. {
  73. int ret = good_frame;
  74. unsigned int rdes0 = p->des0;
  75. struct net_device_stats *stats = (struct net_device_stats *)data;
  76. if (unlikely(rdes0 & RDES0_OWN))
  77. return dma_own;
  78. if (unlikely(!(rdes0 & RDES0_LAST_DESCRIPTOR))) {
  79. pr_warn("%s: Oversized frame spanned multiple buffers\n",
  80. __func__);
  81. stats->rx_length_errors++;
  82. return discard_frame;
  83. }
  84. if (unlikely(rdes0 & RDES0_ERROR_SUMMARY)) {
  85. if (unlikely(rdes0 & RDES0_DESCRIPTOR_ERROR))
  86. x->rx_desc++;
  87. if (unlikely(rdes0 & RDES0_SA_FILTER_FAIL))
  88. x->sa_filter_fail++;
  89. if (unlikely(rdes0 & RDES0_OVERFLOW_ERROR))
  90. x->overflow_error++;
  91. if (unlikely(rdes0 & RDES0_IPC_CSUM_ERROR))
  92. x->ipc_csum_error++;
  93. if (unlikely(rdes0 & RDES0_COLLISION)) {
  94. x->rx_collision++;
  95. stats->collisions++;
  96. }
  97. if (unlikely(rdes0 & RDES0_CRC_ERROR)) {
  98. x->rx_crc++;
  99. stats->rx_crc_errors++;
  100. }
  101. ret = discard_frame;
  102. }
  103. if (unlikely(rdes0 & RDES0_DRIBBLING))
  104. x->dribbling_bit++;
  105. if (unlikely(rdes0 & RDES0_LENGTH_ERROR)) {
  106. x->rx_length++;
  107. ret = discard_frame;
  108. }
  109. if (unlikely(rdes0 & RDES0_MII_ERROR)) {
  110. x->rx_mii++;
  111. ret = discard_frame;
  112. }
  113. #ifdef STMMAC_VLAN_TAG_USED
  114. if (rdes0 & RDES0_VLAN_TAG)
  115. x->vlan_tag++;
  116. #endif
  117. return ret;
  118. }
  119. static void ndesc_init_rx_desc(struct dma_desc *p, int disable_rx_ic, int mode,
  120. int end)
  121. {
  122. p->des0 |= RDES0_OWN;
  123. p->des1 |= (BUF_SIZE_2KiB - 1) & RDES1_BUFFER1_SIZE_MASK;
  124. if (mode == STMMAC_CHAIN_MODE)
  125. ndesc_rx_set_on_chain(p, end);
  126. else
  127. ndesc_rx_set_on_ring(p, end);
  128. if (disable_rx_ic)
  129. p->des1 |= RDES1_DISABLE_IC;
  130. }
  131. static void ndesc_init_tx_desc(struct dma_desc *p, int mode, int end)
  132. {
  133. p->des0 &= ~TDES0_OWN;
  134. if (mode == STMMAC_CHAIN_MODE)
  135. ndesc_tx_set_on_chain(p);
  136. else
  137. ndesc_end_tx_desc_on_ring(p, end);
  138. }
  139. static int ndesc_get_tx_owner(struct dma_desc *p)
  140. {
  141. return (p->des0 & TDES0_OWN) >> 31;
  142. }
  143. static void ndesc_set_tx_owner(struct dma_desc *p)
  144. {
  145. p->des0 |= TDES0_OWN;
  146. }
  147. static void ndesc_set_rx_owner(struct dma_desc *p)
  148. {
  149. p->des0 |= RDES0_OWN;
  150. }
  151. static int ndesc_get_tx_ls(struct dma_desc *p)
  152. {
  153. return (p->des1 & TDES1_LAST_SEGMENT) >> 30;
  154. }
  155. static void ndesc_release_tx_desc(struct dma_desc *p, int mode)
  156. {
  157. int ter = (p->des1 & TDES1_END_RING) >> 25;
  158. memset(p, 0, offsetof(struct dma_desc, des2));
  159. if (mode == STMMAC_CHAIN_MODE)
  160. ndesc_tx_set_on_chain(p);
  161. else
  162. ndesc_end_tx_desc_on_ring(p, ter);
  163. }
  164. static void ndesc_prepare_tx_desc(struct dma_desc *p, int is_fs, int len,
  165. bool csum_flag, int mode, bool tx_own,
  166. bool ls)
  167. {
  168. unsigned int tdes1 = p->des1;
  169. if (is_fs)
  170. tdes1 |= TDES1_FIRST_SEGMENT;
  171. else
  172. tdes1 &= ~TDES1_FIRST_SEGMENT;
  173. if (likely(csum_flag))
  174. tdes1 |= (TX_CIC_FULL) << TDES1_CHECKSUM_INSERTION_SHIFT;
  175. else
  176. tdes1 &= ~(TX_CIC_FULL << TDES1_CHECKSUM_INSERTION_SHIFT);
  177. if (ls)
  178. tdes1 |= TDES1_LAST_SEGMENT;
  179. p->des1 = tdes1;
  180. if (mode == STMMAC_CHAIN_MODE)
  181. norm_set_tx_desc_len_on_chain(p, len);
  182. else
  183. norm_set_tx_desc_len_on_ring(p, len);
  184. if (tx_own)
  185. p->des0 |= TDES0_OWN;
  186. }
  187. static void ndesc_set_tx_ic(struct dma_desc *p)
  188. {
  189. p->des1 |= TDES1_INTERRUPT;
  190. }
  191. static int ndesc_get_rx_frame_len(struct dma_desc *p, int rx_coe_type)
  192. {
  193. unsigned int csum = 0;
  194. /* The type-1 checksum offload engines append the checksum at
  195. * the end of frame and the two bytes of checksum are added in
  196. * the length.
  197. * Adjust for that in the framelen for type-1 checksum offload
  198. * engines
  199. */
  200. if (rx_coe_type == STMMAC_RX_COE_TYPE1)
  201. csum = 2;
  202. return (((p->des0 & RDES0_FRAME_LEN_MASK) >> RDES0_FRAME_LEN_SHIFT) -
  203. csum);
  204. }
  205. static void ndesc_enable_tx_timestamp(struct dma_desc *p)
  206. {
  207. p->des1 |= TDES1_TIME_STAMP_ENABLE;
  208. }
  209. static int ndesc_get_tx_timestamp_status(struct dma_desc *p)
  210. {
  211. return (p->des0 & TDES0_TIME_STAMP_STATUS) >> 17;
  212. }
  213. static u64 ndesc_get_timestamp(void *desc, u32 ats)
  214. {
  215. struct dma_desc *p = (struct dma_desc *)desc;
  216. u64 ns;
  217. ns = p->des2;
  218. /* convert high/sec time stamp value to nanosecond */
  219. ns += p->des3 * 1000000000ULL;
  220. return ns;
  221. }
  222. static int ndesc_get_rx_timestamp_status(void *desc, u32 ats)
  223. {
  224. struct dma_desc *p = (struct dma_desc *)desc;
  225. if ((p->des2 == 0xffffffff) && (p->des3 == 0xffffffff))
  226. /* timestamp is corrupted, hence don't store it */
  227. return 0;
  228. else
  229. return 1;
  230. }
  231. static void ndesc_display_ring(void *head, unsigned int size, bool rx)
  232. {
  233. struct dma_desc *p = (struct dma_desc *)head;
  234. int i;
  235. pr_info("%s descriptor ring:\n", rx ? "RX" : "TX");
  236. for (i = 0; i < size; i++) {
  237. u64 x;
  238. x = *(u64 *)p;
  239. pr_info("%d [0x%x]: 0x%x 0x%x 0x%x 0x%x",
  240. i, (unsigned int)virt_to_phys(p),
  241. (unsigned int)x, (unsigned int)(x >> 32),
  242. p->des2, p->des3);
  243. p++;
  244. }
  245. pr_info("\n");
  246. }
  247. const struct stmmac_desc_ops ndesc_ops = {
  248. .tx_status = ndesc_get_tx_status,
  249. .rx_status = ndesc_get_rx_status,
  250. .get_tx_len = ndesc_get_tx_len,
  251. .init_rx_desc = ndesc_init_rx_desc,
  252. .init_tx_desc = ndesc_init_tx_desc,
  253. .get_tx_owner = ndesc_get_tx_owner,
  254. .release_tx_desc = ndesc_release_tx_desc,
  255. .prepare_tx_desc = ndesc_prepare_tx_desc,
  256. .set_tx_ic = ndesc_set_tx_ic,
  257. .get_tx_ls = ndesc_get_tx_ls,
  258. .set_tx_owner = ndesc_set_tx_owner,
  259. .set_rx_owner = ndesc_set_rx_owner,
  260. .get_rx_frame_len = ndesc_get_rx_frame_len,
  261. .enable_tx_timestamp = ndesc_enable_tx_timestamp,
  262. .get_tx_timestamp_status = ndesc_get_tx_timestamp_status,
  263. .get_timestamp = ndesc_get_timestamp,
  264. .get_rx_timestamp_status = ndesc_get_rx_timestamp_status,
  265. .display_ring = ndesc_display_ring,
  266. };