enh_desc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*******************************************************************************
  2. This contains the functions to handle the enhanced descriptors.
  3. Copyright (C) 2007-2014 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. The full GNU General Public License is included in this distribution in
  12. the file called "COPYING".
  13. Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
  14. *******************************************************************************/
  15. #include <linux/stmmac.h>
  16. #include "common.h"
  17. #include "descs_com.h"
  18. static int enh_desc_get_tx_status(void *data, struct stmmac_extra_stats *x,
  19. struct dma_desc *p, void __iomem *ioaddr)
  20. {
  21. struct net_device_stats *stats = (struct net_device_stats *)data;
  22. unsigned int tdes0 = le32_to_cpu(p->des0);
  23. int ret = tx_done;
  24. /* Get tx owner first */
  25. if (unlikely(tdes0 & ETDES0_OWN))
  26. return tx_dma_own;
  27. /* Verify tx error by looking at the last segment. */
  28. if (likely(!(tdes0 & ETDES0_LAST_SEGMENT)))
  29. return tx_not_ls;
  30. if (unlikely(tdes0 & ETDES0_ERROR_SUMMARY)) {
  31. if (unlikely(tdes0 & ETDES0_JABBER_TIMEOUT))
  32. x->tx_jabber++;
  33. if (unlikely(tdes0 & ETDES0_FRAME_FLUSHED)) {
  34. x->tx_frame_flushed++;
  35. dwmac_dma_flush_tx_fifo(ioaddr);
  36. }
  37. if (unlikely(tdes0 & ETDES0_LOSS_CARRIER)) {
  38. x->tx_losscarrier++;
  39. stats->tx_carrier_errors++;
  40. }
  41. if (unlikely(tdes0 & ETDES0_NO_CARRIER)) {
  42. x->tx_carrier++;
  43. stats->tx_carrier_errors++;
  44. }
  45. if (unlikely((tdes0 & ETDES0_LATE_COLLISION) ||
  46. (tdes0 & ETDES0_EXCESSIVE_COLLISIONS)))
  47. stats->collisions +=
  48. (tdes0 & ETDES0_COLLISION_COUNT_MASK) >> 3;
  49. if (unlikely(tdes0 & ETDES0_EXCESSIVE_DEFERRAL))
  50. x->tx_deferred++;
  51. if (unlikely(tdes0 & ETDES0_UNDERFLOW_ERROR)) {
  52. dwmac_dma_flush_tx_fifo(ioaddr);
  53. x->tx_underflow++;
  54. }
  55. if (unlikely(tdes0 & ETDES0_IP_HEADER_ERROR))
  56. x->tx_ip_header_error++;
  57. if (unlikely(tdes0 & ETDES0_PAYLOAD_ERROR)) {
  58. x->tx_payload_error++;
  59. dwmac_dma_flush_tx_fifo(ioaddr);
  60. }
  61. ret = tx_err;
  62. }
  63. if (unlikely(tdes0 & ETDES0_DEFERRED))
  64. x->tx_deferred++;
  65. #ifdef STMMAC_VLAN_TAG_USED
  66. if (tdes0 & ETDES0_VLAN_FRAME)
  67. x->tx_vlan++;
  68. #endif
  69. return ret;
  70. }
  71. static int enh_desc_get_tx_len(struct dma_desc *p)
  72. {
  73. return (le32_to_cpu(p->des1) & ETDES1_BUFFER1_SIZE_MASK);
  74. }
  75. static int enh_desc_coe_rdes0(int ipc_err, int type, int payload_err)
  76. {
  77. int ret = good_frame;
  78. u32 status = (type << 2 | ipc_err << 1 | payload_err) & 0x7;
  79. /* bits 5 7 0 | Frame status
  80. * ----------------------------------------------------------
  81. * 0 0 0 | IEEE 802.3 Type frame (length < 1536 octects)
  82. * 1 0 0 | IPv4/6 No CSUM errorS.
  83. * 1 0 1 | IPv4/6 CSUM PAYLOAD error
  84. * 1 1 0 | IPv4/6 CSUM IP HR error
  85. * 1 1 1 | IPv4/6 IP PAYLOAD AND HEADER errorS
  86. * 0 0 1 | IPv4/6 unsupported IP PAYLOAD
  87. * 0 1 1 | COE bypassed.. no IPv4/6 frame
  88. * 0 1 0 | Reserved.
  89. */
  90. if (status == 0x0)
  91. ret = llc_snap;
  92. else if (status == 0x4)
  93. ret = good_frame;
  94. else if (status == 0x5)
  95. ret = csum_none;
  96. else if (status == 0x6)
  97. ret = csum_none;
  98. else if (status == 0x7)
  99. ret = csum_none;
  100. else if (status == 0x1)
  101. ret = discard_frame;
  102. else if (status == 0x3)
  103. ret = discard_frame;
  104. return ret;
  105. }
  106. static void enh_desc_get_ext_status(void *data, struct stmmac_extra_stats *x,
  107. struct dma_extended_desc *p)
  108. {
  109. unsigned int rdes0 = le32_to_cpu(p->basic.des0);
  110. unsigned int rdes4 = le32_to_cpu(p->des4);
  111. if (unlikely(rdes0 & ERDES0_RX_MAC_ADDR)) {
  112. int message_type = (rdes4 & ERDES4_MSG_TYPE_MASK) >> 8;
  113. if (rdes4 & ERDES4_IP_HDR_ERR)
  114. x->ip_hdr_err++;
  115. if (rdes4 & ERDES4_IP_PAYLOAD_ERR)
  116. x->ip_payload_err++;
  117. if (rdes4 & ERDES4_IP_CSUM_BYPASSED)
  118. x->ip_csum_bypassed++;
  119. if (rdes4 & ERDES4_IPV4_PKT_RCVD)
  120. x->ipv4_pkt_rcvd++;
  121. if (rdes4 & ERDES4_IPV6_PKT_RCVD)
  122. x->ipv6_pkt_rcvd++;
  123. if (message_type == RDES_EXT_NO_PTP)
  124. x->no_ptp_rx_msg_type_ext++;
  125. else if (message_type == RDES_EXT_SYNC)
  126. x->ptp_rx_msg_type_sync++;
  127. else if (message_type == RDES_EXT_FOLLOW_UP)
  128. x->ptp_rx_msg_type_follow_up++;
  129. else if (message_type == RDES_EXT_DELAY_REQ)
  130. x->ptp_rx_msg_type_delay_req++;
  131. else if (message_type == RDES_EXT_DELAY_RESP)
  132. x->ptp_rx_msg_type_delay_resp++;
  133. else if (message_type == RDES_EXT_PDELAY_REQ)
  134. x->ptp_rx_msg_type_pdelay_req++;
  135. else if (message_type == RDES_EXT_PDELAY_RESP)
  136. x->ptp_rx_msg_type_pdelay_resp++;
  137. else if (message_type == RDES_EXT_PDELAY_FOLLOW_UP)
  138. x->ptp_rx_msg_type_pdelay_follow_up++;
  139. else if (message_type == RDES_PTP_ANNOUNCE)
  140. x->ptp_rx_msg_type_announce++;
  141. else if (message_type == RDES_PTP_MANAGEMENT)
  142. x->ptp_rx_msg_type_management++;
  143. else if (message_type == RDES_PTP_PKT_RESERVED_TYPE)
  144. x->ptp_rx_msg_pkt_reserved_type++;
  145. if (rdes4 & ERDES4_PTP_FRAME_TYPE)
  146. x->ptp_frame_type++;
  147. if (rdes4 & ERDES4_PTP_VER)
  148. x->ptp_ver++;
  149. if (rdes4 & ERDES4_TIMESTAMP_DROPPED)
  150. x->timestamp_dropped++;
  151. if (rdes4 & ERDES4_AV_PKT_RCVD)
  152. x->av_pkt_rcvd++;
  153. if (rdes4 & ERDES4_AV_TAGGED_PKT_RCVD)
  154. x->av_tagged_pkt_rcvd++;
  155. if ((rdes4 & ERDES4_VLAN_TAG_PRI_VAL_MASK) >> 18)
  156. x->vlan_tag_priority_val++;
  157. if (rdes4 & ERDES4_L3_FILTER_MATCH)
  158. x->l3_filter_match++;
  159. if (rdes4 & ERDES4_L4_FILTER_MATCH)
  160. x->l4_filter_match++;
  161. if ((rdes4 & ERDES4_L3_L4_FILT_NO_MATCH_MASK) >> 26)
  162. x->l3_l4_filter_no_match++;
  163. }
  164. }
  165. static int enh_desc_get_rx_status(void *data, struct stmmac_extra_stats *x,
  166. struct dma_desc *p)
  167. {
  168. struct net_device_stats *stats = (struct net_device_stats *)data;
  169. unsigned int rdes0 = le32_to_cpu(p->des0);
  170. int ret = good_frame;
  171. if (unlikely(rdes0 & RDES0_OWN))
  172. return dma_own;
  173. if (unlikely(!(rdes0 & RDES0_LAST_DESCRIPTOR))) {
  174. stats->rx_length_errors++;
  175. return discard_frame;
  176. }
  177. if (unlikely(rdes0 & RDES0_ERROR_SUMMARY)) {
  178. if (unlikely(rdes0 & RDES0_DESCRIPTOR_ERROR)) {
  179. x->rx_desc++;
  180. stats->rx_length_errors++;
  181. }
  182. if (unlikely(rdes0 & RDES0_OVERFLOW_ERROR))
  183. x->rx_gmac_overflow++;
  184. if (unlikely(rdes0 & RDES0_IPC_CSUM_ERROR))
  185. pr_err("\tIPC Csum Error/Giant frame\n");
  186. if (unlikely(rdes0 & RDES0_COLLISION))
  187. stats->collisions++;
  188. if (unlikely(rdes0 & RDES0_RECEIVE_WATCHDOG))
  189. x->rx_watchdog++;
  190. if (unlikely(rdes0 & RDES0_MII_ERROR)) /* GMII */
  191. x->rx_mii++;
  192. if (unlikely(rdes0 & RDES0_CRC_ERROR)) {
  193. x->rx_crc_errors++;
  194. stats->rx_crc_errors++;
  195. }
  196. ret = discard_frame;
  197. }
  198. /* After a payload csum error, the ES bit is set.
  199. * It doesn't match with the information reported into the databook.
  200. * At any rate, we need to understand if the CSUM hw computation is ok
  201. * and report this info to the upper layers. */
  202. if (likely(ret == good_frame))
  203. ret = enh_desc_coe_rdes0(!!(rdes0 & RDES0_IPC_CSUM_ERROR),
  204. !!(rdes0 & RDES0_FRAME_TYPE),
  205. !!(rdes0 & ERDES0_RX_MAC_ADDR));
  206. if (unlikely(rdes0 & RDES0_DRIBBLING))
  207. x->dribbling_bit++;
  208. if (unlikely(rdes0 & RDES0_SA_FILTER_FAIL)) {
  209. x->sa_rx_filter_fail++;
  210. ret = discard_frame;
  211. }
  212. if (unlikely(rdes0 & RDES0_DA_FILTER_FAIL)) {
  213. x->da_rx_filter_fail++;
  214. ret = discard_frame;
  215. }
  216. if (unlikely(rdes0 & RDES0_LENGTH_ERROR)) {
  217. x->rx_length++;
  218. ret = discard_frame;
  219. }
  220. #ifdef STMMAC_VLAN_TAG_USED
  221. if (rdes0 & RDES0_VLAN_TAG)
  222. x->rx_vlan++;
  223. #endif
  224. return ret;
  225. }
  226. static void enh_desc_init_rx_desc(struct dma_desc *p, int disable_rx_ic,
  227. int mode, int end, int bfsize)
  228. {
  229. int bfsize1;
  230. p->des0 |= cpu_to_le32(RDES0_OWN);
  231. bfsize1 = min(bfsize, BUF_SIZE_8KiB);
  232. p->des1 |= cpu_to_le32(bfsize1 & ERDES1_BUFFER1_SIZE_MASK);
  233. if (mode == STMMAC_CHAIN_MODE)
  234. ehn_desc_rx_set_on_chain(p);
  235. else
  236. ehn_desc_rx_set_on_ring(p, end, bfsize);
  237. if (disable_rx_ic)
  238. p->des1 |= cpu_to_le32(ERDES1_DISABLE_IC);
  239. }
  240. static void enh_desc_init_tx_desc(struct dma_desc *p, int mode, int end)
  241. {
  242. p->des0 &= cpu_to_le32(~ETDES0_OWN);
  243. if (mode == STMMAC_CHAIN_MODE)
  244. enh_desc_end_tx_desc_on_chain(p);
  245. else
  246. enh_desc_end_tx_desc_on_ring(p, end);
  247. }
  248. static int enh_desc_get_tx_owner(struct dma_desc *p)
  249. {
  250. return (le32_to_cpu(p->des0) & ETDES0_OWN) >> 31;
  251. }
  252. static void enh_desc_set_tx_owner(struct dma_desc *p)
  253. {
  254. p->des0 |= cpu_to_le32(ETDES0_OWN);
  255. }
  256. static void enh_desc_set_rx_owner(struct dma_desc *p, int disable_rx_ic)
  257. {
  258. p->des0 |= cpu_to_le32(RDES0_OWN);
  259. }
  260. static int enh_desc_get_tx_ls(struct dma_desc *p)
  261. {
  262. return (le32_to_cpu(p->des0) & ETDES0_LAST_SEGMENT) >> 29;
  263. }
  264. static void enh_desc_release_tx_desc(struct dma_desc *p, int mode)
  265. {
  266. int ter = (le32_to_cpu(p->des0) & ETDES0_END_RING) >> 21;
  267. memset(p, 0, offsetof(struct dma_desc, des2));
  268. if (mode == STMMAC_CHAIN_MODE)
  269. enh_desc_end_tx_desc_on_chain(p);
  270. else
  271. enh_desc_end_tx_desc_on_ring(p, ter);
  272. }
  273. static void enh_desc_prepare_tx_desc(struct dma_desc *p, int is_fs, int len,
  274. bool csum_flag, int mode, bool tx_own,
  275. bool ls, unsigned int tot_pkt_len)
  276. {
  277. unsigned int tdes0 = le32_to_cpu(p->des0);
  278. if (mode == STMMAC_CHAIN_MODE)
  279. enh_set_tx_desc_len_on_chain(p, len);
  280. else
  281. enh_set_tx_desc_len_on_ring(p, len);
  282. if (is_fs)
  283. tdes0 |= ETDES0_FIRST_SEGMENT;
  284. else
  285. tdes0 &= ~ETDES0_FIRST_SEGMENT;
  286. if (likely(csum_flag))
  287. tdes0 |= (TX_CIC_FULL << ETDES0_CHECKSUM_INSERTION_SHIFT);
  288. else
  289. tdes0 &= ~(TX_CIC_FULL << ETDES0_CHECKSUM_INSERTION_SHIFT);
  290. if (ls)
  291. tdes0 |= ETDES0_LAST_SEGMENT;
  292. /* Finally set the OWN bit. Later the DMA will start! */
  293. if (tx_own)
  294. tdes0 |= ETDES0_OWN;
  295. if (is_fs && tx_own)
  296. /* When the own bit, for the first frame, has to be set, all
  297. * descriptors for the same frame has to be set before, to
  298. * avoid race condition.
  299. */
  300. dma_wmb();
  301. p->des0 = cpu_to_le32(tdes0);
  302. }
  303. static void enh_desc_set_tx_ic(struct dma_desc *p)
  304. {
  305. p->des0 |= cpu_to_le32(ETDES0_INTERRUPT);
  306. }
  307. static int enh_desc_get_rx_frame_len(struct dma_desc *p, int rx_coe_type)
  308. {
  309. unsigned int csum = 0;
  310. /* The type-1 checksum offload engines append the checksum at
  311. * the end of frame and the two bytes of checksum are added in
  312. * the length.
  313. * Adjust for that in the framelen for type-1 checksum offload
  314. * engines.
  315. */
  316. if (rx_coe_type == STMMAC_RX_COE_TYPE1)
  317. csum = 2;
  318. return (((le32_to_cpu(p->des0) & RDES0_FRAME_LEN_MASK)
  319. >> RDES0_FRAME_LEN_SHIFT) - csum);
  320. }
  321. static void enh_desc_enable_tx_timestamp(struct dma_desc *p)
  322. {
  323. p->des0 |= cpu_to_le32(ETDES0_TIME_STAMP_ENABLE);
  324. }
  325. static int enh_desc_get_tx_timestamp_status(struct dma_desc *p)
  326. {
  327. return (le32_to_cpu(p->des0) & ETDES0_TIME_STAMP_STATUS) >> 17;
  328. }
  329. static void enh_desc_get_timestamp(void *desc, u32 ats, u64 *ts)
  330. {
  331. u64 ns;
  332. if (ats) {
  333. struct dma_extended_desc *p = (struct dma_extended_desc *)desc;
  334. ns = le32_to_cpu(p->des6);
  335. /* convert high/sec time stamp value to nanosecond */
  336. ns += le32_to_cpu(p->des7) * 1000000000ULL;
  337. } else {
  338. struct dma_desc *p = (struct dma_desc *)desc;
  339. ns = le32_to_cpu(p->des2);
  340. ns += le32_to_cpu(p->des3) * 1000000000ULL;
  341. }
  342. *ts = ns;
  343. }
  344. static int enh_desc_get_rx_timestamp_status(void *desc, void *next_desc,
  345. u32 ats)
  346. {
  347. if (ats) {
  348. struct dma_extended_desc *p = (struct dma_extended_desc *)desc;
  349. return (le32_to_cpu(p->basic.des0) & RDES0_IPC_CSUM_ERROR) >> 7;
  350. } else {
  351. struct dma_desc *p = (struct dma_desc *)desc;
  352. if ((le32_to_cpu(p->des2) == 0xffffffff) &&
  353. (le32_to_cpu(p->des3) == 0xffffffff))
  354. /* timestamp is corrupted, hence don't store it */
  355. return 0;
  356. else
  357. return 1;
  358. }
  359. }
  360. static void enh_desc_display_ring(void *head, unsigned int size, bool rx)
  361. {
  362. struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
  363. int i;
  364. pr_info("Extended %s descriptor ring:\n", rx ? "RX" : "TX");
  365. for (i = 0; i < size; i++) {
  366. u64 x;
  367. x = *(u64 *)ep;
  368. pr_info("%03d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
  369. i, (unsigned int)virt_to_phys(ep),
  370. (unsigned int)x, (unsigned int)(x >> 32),
  371. ep->basic.des2, ep->basic.des3);
  372. ep++;
  373. }
  374. pr_info("\n");
  375. }
  376. static void enh_desc_get_addr(struct dma_desc *p, unsigned int *addr)
  377. {
  378. *addr = le32_to_cpu(p->des2);
  379. }
  380. static void enh_desc_set_addr(struct dma_desc *p, dma_addr_t addr)
  381. {
  382. p->des2 = cpu_to_le32(addr);
  383. }
  384. static void enh_desc_clear(struct dma_desc *p)
  385. {
  386. p->des2 = 0;
  387. }
  388. const struct stmmac_desc_ops enh_desc_ops = {
  389. .tx_status = enh_desc_get_tx_status,
  390. .rx_status = enh_desc_get_rx_status,
  391. .get_tx_len = enh_desc_get_tx_len,
  392. .init_rx_desc = enh_desc_init_rx_desc,
  393. .init_tx_desc = enh_desc_init_tx_desc,
  394. .get_tx_owner = enh_desc_get_tx_owner,
  395. .release_tx_desc = enh_desc_release_tx_desc,
  396. .prepare_tx_desc = enh_desc_prepare_tx_desc,
  397. .set_tx_ic = enh_desc_set_tx_ic,
  398. .get_tx_ls = enh_desc_get_tx_ls,
  399. .set_tx_owner = enh_desc_set_tx_owner,
  400. .set_rx_owner = enh_desc_set_rx_owner,
  401. .get_rx_frame_len = enh_desc_get_rx_frame_len,
  402. .rx_extended_status = enh_desc_get_ext_status,
  403. .enable_tx_timestamp = enh_desc_enable_tx_timestamp,
  404. .get_tx_timestamp_status = enh_desc_get_tx_timestamp_status,
  405. .get_timestamp = enh_desc_get_timestamp,
  406. .get_rx_timestamp_status = enh_desc_get_rx_timestamp_status,
  407. .display_ring = enh_desc_display_ring,
  408. .get_addr = enh_desc_get_addr,
  409. .set_addr = enh_desc_set_addr,
  410. .clear = enh_desc_clear,
  411. };