dwmac4_descs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * This contains the functions to handle the descriptors for DesignWare databook
  3. * 4.xx.
  4. *
  5. * Copyright (C) 2015 STMicroelectronics Ltd
  6. *
  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. *
  11. * Author: Alexandre Torgue <alexandre.torgue@st.com>
  12. */
  13. #include <linux/stmmac.h>
  14. #include "common.h"
  15. #include "dwmac4_descs.h"
  16. static int dwmac4_wrback_get_tx_status(void *data, struct stmmac_extra_stats *x,
  17. struct dma_desc *p,
  18. void __iomem *ioaddr)
  19. {
  20. struct net_device_stats *stats = (struct net_device_stats *)data;
  21. unsigned int tdes3;
  22. int ret = tx_done;
  23. tdes3 = le32_to_cpu(p->des3);
  24. /* Get tx owner first */
  25. if (unlikely(tdes3 & TDES3_OWN))
  26. return tx_dma_own;
  27. /* Verify tx error by looking at the last segment. */
  28. if (likely(!(tdes3 & TDES3_LAST_DESCRIPTOR)))
  29. return tx_not_ls;
  30. if (unlikely(tdes3 & TDES3_ERROR_SUMMARY)) {
  31. if (unlikely(tdes3 & TDES3_JABBER_TIMEOUT))
  32. x->tx_jabber++;
  33. if (unlikely(tdes3 & TDES3_PACKET_FLUSHED))
  34. x->tx_frame_flushed++;
  35. if (unlikely(tdes3 & TDES3_LOSS_CARRIER)) {
  36. x->tx_losscarrier++;
  37. stats->tx_carrier_errors++;
  38. }
  39. if (unlikely(tdes3 & TDES3_NO_CARRIER)) {
  40. x->tx_carrier++;
  41. stats->tx_carrier_errors++;
  42. }
  43. if (unlikely((tdes3 & TDES3_LATE_COLLISION) ||
  44. (tdes3 & TDES3_EXCESSIVE_COLLISION)))
  45. stats->collisions +=
  46. (tdes3 & TDES3_COLLISION_COUNT_MASK)
  47. >> TDES3_COLLISION_COUNT_SHIFT;
  48. if (unlikely(tdes3 & TDES3_EXCESSIVE_DEFERRAL))
  49. x->tx_deferred++;
  50. if (unlikely(tdes3 & TDES3_UNDERFLOW_ERROR))
  51. x->tx_underflow++;
  52. if (unlikely(tdes3 & TDES3_IP_HDR_ERROR))
  53. x->tx_ip_header_error++;
  54. if (unlikely(tdes3 & TDES3_PAYLOAD_ERROR))
  55. x->tx_payload_error++;
  56. ret = tx_err;
  57. }
  58. if (unlikely(tdes3 & TDES3_DEFERRED))
  59. x->tx_deferred++;
  60. return ret;
  61. }
  62. static int dwmac4_wrback_get_rx_status(void *data, struct stmmac_extra_stats *x,
  63. struct dma_desc *p)
  64. {
  65. struct net_device_stats *stats = (struct net_device_stats *)data;
  66. unsigned int rdes1 = le32_to_cpu(p->des1);
  67. unsigned int rdes2 = le32_to_cpu(p->des2);
  68. unsigned int rdes3 = le32_to_cpu(p->des3);
  69. int message_type;
  70. int ret = good_frame;
  71. if (unlikely(rdes3 & RDES3_OWN))
  72. return dma_own;
  73. /* Verify rx error by looking at the last segment. */
  74. if (likely(!(rdes3 & RDES3_LAST_DESCRIPTOR)))
  75. return discard_frame;
  76. if (unlikely(rdes3 & RDES3_ERROR_SUMMARY)) {
  77. if (unlikely(rdes3 & RDES3_GIANT_PACKET))
  78. stats->rx_length_errors++;
  79. if (unlikely(rdes3 & RDES3_OVERFLOW_ERROR))
  80. x->rx_gmac_overflow++;
  81. if (unlikely(rdes3 & RDES3_RECEIVE_WATCHDOG))
  82. x->rx_watchdog++;
  83. if (unlikely(rdes3 & RDES3_RECEIVE_ERROR))
  84. x->rx_mii++;
  85. if (unlikely(rdes3 & RDES3_CRC_ERROR)) {
  86. x->rx_crc_errors++;
  87. stats->rx_crc_errors++;
  88. }
  89. if (unlikely(rdes3 & RDES3_DRIBBLE_ERROR))
  90. x->dribbling_bit++;
  91. ret = discard_frame;
  92. }
  93. message_type = (rdes1 & ERDES4_MSG_TYPE_MASK) >> 8;
  94. if (rdes1 & RDES1_IP_HDR_ERROR)
  95. x->ip_hdr_err++;
  96. if (rdes1 & RDES1_IP_CSUM_BYPASSED)
  97. x->ip_csum_bypassed++;
  98. if (rdes1 & RDES1_IPV4_HEADER)
  99. x->ipv4_pkt_rcvd++;
  100. if (rdes1 & RDES1_IPV6_HEADER)
  101. x->ipv6_pkt_rcvd++;
  102. if (message_type == RDES_EXT_NO_PTP)
  103. x->no_ptp_rx_msg_type_ext++;
  104. else if (message_type == RDES_EXT_SYNC)
  105. x->ptp_rx_msg_type_sync++;
  106. else if (message_type == RDES_EXT_FOLLOW_UP)
  107. x->ptp_rx_msg_type_follow_up++;
  108. else if (message_type == RDES_EXT_DELAY_REQ)
  109. x->ptp_rx_msg_type_delay_req++;
  110. else if (message_type == RDES_EXT_DELAY_RESP)
  111. x->ptp_rx_msg_type_delay_resp++;
  112. else if (message_type == RDES_EXT_PDELAY_REQ)
  113. x->ptp_rx_msg_type_pdelay_req++;
  114. else if (message_type == RDES_EXT_PDELAY_RESP)
  115. x->ptp_rx_msg_type_pdelay_resp++;
  116. else if (message_type == RDES_EXT_PDELAY_FOLLOW_UP)
  117. x->ptp_rx_msg_type_pdelay_follow_up++;
  118. else if (message_type == RDES_PTP_ANNOUNCE)
  119. x->ptp_rx_msg_type_announce++;
  120. else if (message_type == RDES_PTP_MANAGEMENT)
  121. x->ptp_rx_msg_type_management++;
  122. else if (message_type == RDES_PTP_PKT_RESERVED_TYPE)
  123. x->ptp_rx_msg_pkt_reserved_type++;
  124. if (rdes1 & RDES1_PTP_PACKET_TYPE)
  125. x->ptp_frame_type++;
  126. if (rdes1 & RDES1_PTP_VER)
  127. x->ptp_ver++;
  128. if (rdes1 & RDES1_TIMESTAMP_DROPPED)
  129. x->timestamp_dropped++;
  130. if (unlikely(rdes2 & RDES2_SA_FILTER_FAIL)) {
  131. x->sa_rx_filter_fail++;
  132. ret = discard_frame;
  133. }
  134. if (unlikely(rdes2 & RDES2_DA_FILTER_FAIL)) {
  135. x->da_rx_filter_fail++;
  136. ret = discard_frame;
  137. }
  138. if (rdes2 & RDES2_L3_FILTER_MATCH)
  139. x->l3_filter_match++;
  140. if (rdes2 & RDES2_L4_FILTER_MATCH)
  141. x->l4_filter_match++;
  142. if ((rdes2 & RDES2_L3_L4_FILT_NB_MATCH_MASK)
  143. >> RDES2_L3_L4_FILT_NB_MATCH_SHIFT)
  144. x->l3_l4_filter_no_match++;
  145. return ret;
  146. }
  147. static int dwmac4_rd_get_tx_len(struct dma_desc *p)
  148. {
  149. return (le32_to_cpu(p->des2) & TDES2_BUFFER1_SIZE_MASK);
  150. }
  151. static int dwmac4_get_tx_owner(struct dma_desc *p)
  152. {
  153. return (le32_to_cpu(p->des3) & TDES3_OWN) >> TDES3_OWN_SHIFT;
  154. }
  155. static void dwmac4_set_tx_owner(struct dma_desc *p)
  156. {
  157. p->des3 |= cpu_to_le32(TDES3_OWN);
  158. }
  159. static void dwmac4_set_rx_owner(struct dma_desc *p, int disable_rx_ic)
  160. {
  161. p->des3 = cpu_to_le32(RDES3_OWN | RDES3_BUFFER1_VALID_ADDR);
  162. if (!disable_rx_ic)
  163. p->des3 |= cpu_to_le32(RDES3_INT_ON_COMPLETION_EN);
  164. }
  165. static int dwmac4_get_tx_ls(struct dma_desc *p)
  166. {
  167. return (le32_to_cpu(p->des3) & TDES3_LAST_DESCRIPTOR)
  168. >> TDES3_LAST_DESCRIPTOR_SHIFT;
  169. }
  170. static int dwmac4_wrback_get_rx_frame_len(struct dma_desc *p, int rx_coe)
  171. {
  172. return (le32_to_cpu(p->des3) & RDES3_PACKET_SIZE_MASK);
  173. }
  174. static void dwmac4_rd_enable_tx_timestamp(struct dma_desc *p)
  175. {
  176. p->des2 |= cpu_to_le32(TDES2_TIMESTAMP_ENABLE);
  177. }
  178. static int dwmac4_wrback_get_tx_timestamp_status(struct dma_desc *p)
  179. {
  180. /* Context type from W/B descriptor must be zero */
  181. if (le32_to_cpu(p->des3) & TDES3_CONTEXT_TYPE)
  182. return 0;
  183. /* Tx Timestamp Status is 1 so des0 and des1'll have valid values */
  184. if (le32_to_cpu(p->des3) & TDES3_TIMESTAMP_STATUS)
  185. return 1;
  186. return 0;
  187. }
  188. static inline void dwmac4_get_timestamp(void *desc, u32 ats, u64 *ts)
  189. {
  190. struct dma_desc *p = (struct dma_desc *)desc;
  191. u64 ns;
  192. ns = le32_to_cpu(p->des0);
  193. /* convert high/sec time stamp value to nanosecond */
  194. ns += le32_to_cpu(p->des1) * 1000000000ULL;
  195. *ts = ns;
  196. }
  197. static int dwmac4_rx_check_timestamp(void *desc)
  198. {
  199. struct dma_desc *p = (struct dma_desc *)desc;
  200. unsigned int rdes0 = le32_to_cpu(p->des0);
  201. unsigned int rdes1 = le32_to_cpu(p->des1);
  202. unsigned int rdes3 = le32_to_cpu(p->des3);
  203. u32 own, ctxt;
  204. int ret = 1;
  205. own = rdes3 & RDES3_OWN;
  206. ctxt = ((rdes3 & RDES3_CONTEXT_DESCRIPTOR)
  207. >> RDES3_CONTEXT_DESCRIPTOR_SHIFT);
  208. if (likely(!own && ctxt)) {
  209. if ((rdes0 == 0xffffffff) && (rdes1 == 0xffffffff))
  210. /* Corrupted value */
  211. ret = -EINVAL;
  212. else
  213. /* A valid Timestamp is ready to be read */
  214. ret = 0;
  215. }
  216. /* Timestamp not ready */
  217. return ret;
  218. }
  219. static int dwmac4_wrback_get_rx_timestamp_status(void *desc, void *next_desc,
  220. u32 ats)
  221. {
  222. struct dma_desc *p = (struct dma_desc *)desc;
  223. int ret = -EINVAL;
  224. /* Get the status from normal w/b descriptor */
  225. if (likely(p->des3 & TDES3_RS1V)) {
  226. if (likely(le32_to_cpu(p->des1) & RDES1_TIMESTAMP_AVAILABLE)) {
  227. int i = 0;
  228. /* Check if timestamp is OK from context descriptor */
  229. do {
  230. ret = dwmac4_rx_check_timestamp(next_desc);
  231. if (ret < 0)
  232. goto exit;
  233. i++;
  234. } while ((ret == 1) && (i < 10));
  235. if (i == 10)
  236. ret = -EBUSY;
  237. }
  238. }
  239. exit:
  240. if (likely(ret == 0))
  241. return 1;
  242. return 0;
  243. }
  244. static void dwmac4_rd_init_rx_desc(struct dma_desc *p, int disable_rx_ic,
  245. int mode, int end, int bfsize)
  246. {
  247. dwmac4_set_rx_owner(p, disable_rx_ic);
  248. }
  249. static void dwmac4_rd_init_tx_desc(struct dma_desc *p, int mode, int end)
  250. {
  251. p->des0 = 0;
  252. p->des1 = 0;
  253. p->des2 = 0;
  254. p->des3 = 0;
  255. }
  256. static void dwmac4_rd_prepare_tx_desc(struct dma_desc *p, int is_fs, int len,
  257. bool csum_flag, int mode, bool tx_own,
  258. bool ls, unsigned int tot_pkt_len)
  259. {
  260. unsigned int tdes3 = le32_to_cpu(p->des3);
  261. p->des2 |= cpu_to_le32(len & TDES2_BUFFER1_SIZE_MASK);
  262. tdes3 |= tot_pkt_len & TDES3_PACKET_SIZE_MASK;
  263. if (is_fs)
  264. tdes3 |= TDES3_FIRST_DESCRIPTOR;
  265. else
  266. tdes3 &= ~TDES3_FIRST_DESCRIPTOR;
  267. if (likely(csum_flag))
  268. tdes3 |= (TX_CIC_FULL << TDES3_CHECKSUM_INSERTION_SHIFT);
  269. else
  270. tdes3 &= ~(TX_CIC_FULL << TDES3_CHECKSUM_INSERTION_SHIFT);
  271. if (ls)
  272. tdes3 |= TDES3_LAST_DESCRIPTOR;
  273. else
  274. tdes3 &= ~TDES3_LAST_DESCRIPTOR;
  275. /* Finally set the OWN bit. Later the DMA will start! */
  276. if (tx_own)
  277. tdes3 |= TDES3_OWN;
  278. if (is_fs && tx_own)
  279. /* When the own bit, for the first frame, has to be set, all
  280. * descriptors for the same frame has to be set before, to
  281. * avoid race condition.
  282. */
  283. dma_wmb();
  284. p->des3 = cpu_to_le32(tdes3);
  285. }
  286. static void dwmac4_rd_prepare_tso_tx_desc(struct dma_desc *p, int is_fs,
  287. int len1, int len2, bool tx_own,
  288. bool ls, unsigned int tcphdrlen,
  289. unsigned int tcppayloadlen)
  290. {
  291. unsigned int tdes3 = le32_to_cpu(p->des3);
  292. if (len1)
  293. p->des2 |= cpu_to_le32((len1 & TDES2_BUFFER1_SIZE_MASK));
  294. if (len2)
  295. p->des2 |= cpu_to_le32((len2 << TDES2_BUFFER2_SIZE_MASK_SHIFT)
  296. & TDES2_BUFFER2_SIZE_MASK);
  297. if (is_fs) {
  298. tdes3 |= TDES3_FIRST_DESCRIPTOR |
  299. TDES3_TCP_SEGMENTATION_ENABLE |
  300. ((tcphdrlen << TDES3_HDR_LEN_SHIFT) &
  301. TDES3_SLOT_NUMBER_MASK) |
  302. ((tcppayloadlen & TDES3_TCP_PKT_PAYLOAD_MASK));
  303. } else {
  304. tdes3 &= ~TDES3_FIRST_DESCRIPTOR;
  305. }
  306. if (ls)
  307. tdes3 |= TDES3_LAST_DESCRIPTOR;
  308. else
  309. tdes3 &= ~TDES3_LAST_DESCRIPTOR;
  310. /* Finally set the OWN bit. Later the DMA will start! */
  311. if (tx_own)
  312. tdes3 |= TDES3_OWN;
  313. if (is_fs && tx_own)
  314. /* When the own bit, for the first frame, has to be set, all
  315. * descriptors for the same frame has to be set before, to
  316. * avoid race condition.
  317. */
  318. dma_wmb();
  319. p->des3 = cpu_to_le32(tdes3);
  320. }
  321. static void dwmac4_release_tx_desc(struct dma_desc *p, int mode)
  322. {
  323. p->des0 = 0;
  324. p->des1 = 0;
  325. p->des2 = 0;
  326. p->des3 = 0;
  327. }
  328. static void dwmac4_rd_set_tx_ic(struct dma_desc *p)
  329. {
  330. p->des2 |= cpu_to_le32(TDES2_INTERRUPT_ON_COMPLETION);
  331. }
  332. static void dwmac4_display_ring(void *head, unsigned int size, bool rx)
  333. {
  334. struct dma_desc *p = (struct dma_desc *)head;
  335. int i;
  336. pr_info("%s descriptor ring:\n", rx ? "RX" : "TX");
  337. for (i = 0; i < size; i++) {
  338. pr_info("%03d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
  339. i, (unsigned int)virt_to_phys(p),
  340. le32_to_cpu(p->des0), le32_to_cpu(p->des1),
  341. le32_to_cpu(p->des2), le32_to_cpu(p->des3));
  342. p++;
  343. }
  344. }
  345. static void dwmac4_set_mss_ctxt(struct dma_desc *p, unsigned int mss)
  346. {
  347. p->des0 = 0;
  348. p->des1 = 0;
  349. p->des2 = cpu_to_le32(mss);
  350. p->des3 = cpu_to_le32(TDES3_CONTEXT_TYPE | TDES3_CTXT_TCMSSV);
  351. }
  352. static void dwmac4_get_addr(struct dma_desc *p, unsigned int *addr)
  353. {
  354. *addr = le32_to_cpu(p->des0);
  355. }
  356. static void dwmac4_set_addr(struct dma_desc *p, dma_addr_t addr)
  357. {
  358. p->des0 = cpu_to_le32(addr);
  359. p->des1 = 0;
  360. }
  361. static void dwmac4_clear(struct dma_desc *p)
  362. {
  363. p->des0 = 0;
  364. p->des1 = 0;
  365. p->des2 = 0;
  366. p->des3 = 0;
  367. }
  368. const struct stmmac_desc_ops dwmac4_desc_ops = {
  369. .tx_status = dwmac4_wrback_get_tx_status,
  370. .rx_status = dwmac4_wrback_get_rx_status,
  371. .get_tx_len = dwmac4_rd_get_tx_len,
  372. .get_tx_owner = dwmac4_get_tx_owner,
  373. .set_tx_owner = dwmac4_set_tx_owner,
  374. .set_rx_owner = dwmac4_set_rx_owner,
  375. .get_tx_ls = dwmac4_get_tx_ls,
  376. .get_rx_frame_len = dwmac4_wrback_get_rx_frame_len,
  377. .enable_tx_timestamp = dwmac4_rd_enable_tx_timestamp,
  378. .get_tx_timestamp_status = dwmac4_wrback_get_tx_timestamp_status,
  379. .get_rx_timestamp_status = dwmac4_wrback_get_rx_timestamp_status,
  380. .get_timestamp = dwmac4_get_timestamp,
  381. .set_tx_ic = dwmac4_rd_set_tx_ic,
  382. .prepare_tx_desc = dwmac4_rd_prepare_tx_desc,
  383. .prepare_tso_tx_desc = dwmac4_rd_prepare_tso_tx_desc,
  384. .release_tx_desc = dwmac4_release_tx_desc,
  385. .init_rx_desc = dwmac4_rd_init_rx_desc,
  386. .init_tx_desc = dwmac4_rd_init_tx_desc,
  387. .display_ring = dwmac4_display_ring,
  388. .set_mss = dwmac4_set_mss_ctxt,
  389. .get_addr = dwmac4_get_addr,
  390. .set_addr = dwmac4_set_addr,
  391. .clear = dwmac4_clear,
  392. };
  393. const struct stmmac_mode_ops dwmac4_ring_mode_ops = { };