enh_desc.c 12 KB

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