PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/net/ethernet/stmicro/stmmac/enh_desc.c

https://github.com/mturquette/linux
C | 454 lines | 337 code | 64 blank | 53 comment | 108 complexity | 9997c7451eff49700039c2d4ee6c7923 MD5 | raw file
  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_SYNC)
  127. x->rx_msg_type_sync++;
  128. else if (message_type == RDES_EXT_FOLLOW_UP)
  129. x->rx_msg_type_follow_up++;
  130. else if (message_type == RDES_EXT_DELAY_REQ)
  131. x->rx_msg_type_delay_req++;
  132. else if (message_type == RDES_EXT_DELAY_RESP)
  133. x->rx_msg_type_delay_resp++;
  134. else if (message_type == RDES_EXT_PDELAY_REQ)
  135. x->rx_msg_type_pdelay_req++;
  136. else if (message_type == RDES_EXT_PDELAY_RESP)
  137. x->rx_msg_type_pdelay_resp++;
  138. else if (message_type == RDES_EXT_PDELAY_FOLLOW_UP)
  139. x->rx_msg_type_pdelay_follow_up++;
  140. else
  141. x->rx_msg_type_ext_no_ptp++;
  142. if (rdes4 & ERDES4_PTP_FRAME_TYPE)
  143. x->ptp_frame_type++;
  144. if (rdes4 & ERDES4_PTP_VER)
  145. x->ptp_ver++;
  146. if (rdes4 & ERDES4_TIMESTAMP_DROPPED)
  147. x->timestamp_dropped++;
  148. if (rdes4 & ERDES4_AV_PKT_RCVD)
  149. x->av_pkt_rcvd++;
  150. if (rdes4 & ERDES4_AV_TAGGED_PKT_RCVD)
  151. x->av_tagged_pkt_rcvd++;
  152. if ((rdes4 & ERDES4_VLAN_TAG_PRI_VAL_MASK) >> 18)
  153. x->vlan_tag_priority_val++;
  154. if (rdes4 & ERDES4_L3_FILTER_MATCH)
  155. x->l3_filter_match++;
  156. if (rdes4 & ERDES4_L4_FILTER_MATCH)
  157. x->l4_filter_match++;
  158. if ((rdes4 & ERDES4_L3_L4_FILT_NO_MATCH_MASK) >> 26)
  159. x->l3_l4_filter_no_match++;
  160. }
  161. }
  162. static int enh_desc_get_rx_status(void *data, struct stmmac_extra_stats *x,
  163. struct dma_desc *p)
  164. {
  165. struct net_device_stats *stats = (struct net_device_stats *)data;
  166. unsigned int rdes0 = p->des0;
  167. int ret = good_frame;
  168. if (unlikely(rdes0 & RDES0_OWN))
  169. return dma_own;
  170. if (unlikely(rdes0 & RDES0_ERROR_SUMMARY)) {
  171. if (unlikely(rdes0 & RDES0_DESCRIPTOR_ERROR)) {
  172. x->rx_desc++;
  173. stats->rx_length_errors++;
  174. }
  175. if (unlikely(rdes0 & RDES0_OVERFLOW_ERROR))
  176. x->rx_gmac_overflow++;
  177. if (unlikely(rdes0 & RDES0_IPC_CSUM_ERROR))
  178. pr_err("\tIPC Csum Error/Giant frame\n");
  179. if (unlikely(rdes0 & RDES0_COLLISION))
  180. stats->collisions++;
  181. if (unlikely(rdes0 & RDES0_RECEIVE_WATCHDOG))
  182. x->rx_watchdog++;
  183. if (unlikely(rdes0 & RDES0_MII_ERROR)) /* GMII */
  184. x->rx_mii++;
  185. if (unlikely(rdes0 & RDES0_CRC_ERROR)) {
  186. x->rx_crc++;
  187. stats->rx_crc_errors++;
  188. }
  189. ret = discard_frame;
  190. }
  191. /* After a payload csum error, the ES bit is set.
  192. * It doesn't match with the information reported into the databook.
  193. * At any rate, we need to understand if the CSUM hw computation is ok
  194. * and report this info to the upper layers. */
  195. ret = enh_desc_coe_rdes0(!!(rdes0 & RDES0_IPC_CSUM_ERROR),
  196. !!(rdes0 & RDES0_FRAME_TYPE),
  197. !!(rdes0 & ERDES0_RX_MAC_ADDR));
  198. if (unlikely(rdes0 & RDES0_DRIBBLING))
  199. x->dribbling_bit++;
  200. if (unlikely(rdes0 & RDES0_SA_FILTER_FAIL)) {
  201. x->sa_rx_filter_fail++;
  202. ret = discard_frame;
  203. }
  204. if (unlikely(rdes0 & RDES0_DA_FILTER_FAIL)) {
  205. x->da_rx_filter_fail++;
  206. ret = discard_frame;
  207. }
  208. if (unlikely(rdes0 & RDES0_LENGTH_ERROR)) {
  209. x->rx_length++;
  210. ret = discard_frame;
  211. }
  212. #ifdef STMMAC_VLAN_TAG_USED
  213. if (rdes0 & RDES0_VLAN_TAG)
  214. x->rx_vlan++;
  215. #endif
  216. return ret;
  217. }
  218. static void enh_desc_init_rx_desc(struct dma_desc *p, int disable_rx_ic,
  219. int mode, int end)
  220. {
  221. p->des0 |= RDES0_OWN;
  222. p->des1 |= ((BUF_SIZE_8KiB - 1) & ERDES1_BUFFER1_SIZE_MASK);
  223. if (mode == STMMAC_CHAIN_MODE)
  224. ehn_desc_rx_set_on_chain(p);
  225. else
  226. ehn_desc_rx_set_on_ring(p, end);
  227. if (disable_rx_ic)
  228. p->des1 |= ERDES1_DISABLE_IC;
  229. }
  230. static void enh_desc_init_tx_desc(struct dma_desc *p, int mode, int end)
  231. {
  232. p->des0 &= ~ETDES0_OWN;
  233. if (mode == STMMAC_CHAIN_MODE)
  234. enh_desc_end_tx_desc_on_chain(p);
  235. else
  236. enh_desc_end_tx_desc_on_ring(p, end);
  237. }
  238. static int enh_desc_get_tx_owner(struct dma_desc *p)
  239. {
  240. return (p->des0 & ETDES0_OWN) >> 31;
  241. }
  242. static void enh_desc_set_tx_owner(struct dma_desc *p)
  243. {
  244. p->des0 |= ETDES0_OWN;
  245. }
  246. static void enh_desc_set_rx_owner(struct dma_desc *p)
  247. {
  248. p->des0 |= RDES0_OWN;
  249. }
  250. static int enh_desc_get_tx_ls(struct dma_desc *p)
  251. {
  252. return (p->des0 & ETDES0_LAST_SEGMENT) >> 29;
  253. }
  254. static void enh_desc_release_tx_desc(struct dma_desc *p, int mode)
  255. {
  256. int ter = (p->des0 & ETDES0_END_RING) >> 21;
  257. memset(p, 0, offsetof(struct dma_desc, des2));
  258. if (mode == STMMAC_CHAIN_MODE)
  259. enh_desc_end_tx_desc_on_chain(p);
  260. else
  261. enh_desc_end_tx_desc_on_ring(p, ter);
  262. }
  263. static void enh_desc_prepare_tx_desc(struct dma_desc *p, int is_fs, int len,
  264. bool csum_flag, int mode, bool tx_own,
  265. bool ls)
  266. {
  267. unsigned int tdes0 = p->des0;
  268. if (mode == STMMAC_CHAIN_MODE)
  269. enh_set_tx_desc_len_on_chain(p, len);
  270. else
  271. enh_set_tx_desc_len_on_ring(p, len);
  272. if (is_fs)
  273. tdes0 |= ETDES0_FIRST_SEGMENT;
  274. else
  275. tdes0 &= ~ETDES0_FIRST_SEGMENT;
  276. if (likely(csum_flag))
  277. tdes0 |= (TX_CIC_FULL << ETDES0_CHECKSUM_INSERTION_SHIFT);
  278. else
  279. tdes0 &= ~(TX_CIC_FULL << ETDES0_CHECKSUM_INSERTION_SHIFT);
  280. if (ls)
  281. tdes0 |= ETDES0_LAST_SEGMENT;
  282. /* Finally set the OWN bit. Later the DMA will start! */
  283. if (tx_own)
  284. tdes0 |= ETDES0_OWN;
  285. if (is_fs & tx_own)
  286. /* When the own bit, for the first frame, has to be set, all
  287. * descriptors for the same frame has to be set before, to
  288. * avoid race condition.
  289. */
  290. wmb();
  291. p->des0 = tdes0;
  292. }
  293. static void enh_desc_set_tx_ic(struct dma_desc *p)
  294. {
  295. p->des0 |= ETDES0_INTERRUPT;
  296. }
  297. static int enh_desc_get_rx_frame_len(struct dma_desc *p, int rx_coe_type)
  298. {
  299. unsigned int csum = 0;
  300. /* The type-1 checksum offload engines append the checksum at
  301. * the end of frame and the two bytes of checksum are added in
  302. * the length.
  303. * Adjust for that in the framelen for type-1 checksum offload
  304. * engines.
  305. */
  306. if (rx_coe_type == STMMAC_RX_COE_TYPE1)
  307. csum = 2;
  308. return (((p->des0 & RDES0_FRAME_LEN_MASK) >> RDES0_FRAME_LEN_SHIFT) -
  309. csum);
  310. }
  311. static void enh_desc_enable_tx_timestamp(struct dma_desc *p)
  312. {
  313. p->des0 |= ETDES0_TIME_STAMP_ENABLE;
  314. }
  315. static int enh_desc_get_tx_timestamp_status(struct dma_desc *p)
  316. {
  317. return (p->des0 & ETDES0_TIME_STAMP_STATUS) >> 17;
  318. }
  319. static u64 enh_desc_get_timestamp(void *desc, u32 ats)
  320. {
  321. u64 ns;
  322. if (ats) {
  323. struct dma_extended_desc *p = (struct dma_extended_desc *)desc;
  324. ns = p->des6;
  325. /* convert high/sec time stamp value to nanosecond */
  326. ns += p->des7 * 1000000000ULL;
  327. } else {
  328. struct dma_desc *p = (struct dma_desc *)desc;
  329. ns = p->des2;
  330. ns += p->des3 * 1000000000ULL;
  331. }
  332. return ns;
  333. }
  334. static int enh_desc_get_rx_timestamp_status(void *desc, u32 ats)
  335. {
  336. if (ats) {
  337. struct dma_extended_desc *p = (struct dma_extended_desc *)desc;
  338. return (p->basic.des0 & RDES0_IPC_CSUM_ERROR) >> 7;
  339. } else {
  340. struct dma_desc *p = (struct dma_desc *)desc;
  341. if ((p->des2 == 0xffffffff) && (p->des3 == 0xffffffff))
  342. /* timestamp is corrupted, hence don't store it */
  343. return 0;
  344. else
  345. return 1;
  346. }
  347. }
  348. static void enh_desc_display_ring(void *head, unsigned int size, bool rx)
  349. {
  350. struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
  351. int i;
  352. pr_info("Extended %s descriptor ring:\n", rx ? "RX" : "TX");
  353. for (i = 0; i < size; i++) {
  354. u64 x;
  355. x = *(u64 *)ep;
  356. pr_info("%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
  357. i, (unsigned int)virt_to_phys(ep),
  358. (unsigned int)x, (unsigned int)(x >> 32),
  359. ep->basic.des2, ep->basic.des3);
  360. ep++;
  361. }
  362. pr_info("\n");
  363. }
  364. const struct stmmac_desc_ops enh_desc_ops = {
  365. .tx_status = enh_desc_get_tx_status,
  366. .rx_status = enh_desc_get_rx_status,
  367. .get_tx_len = enh_desc_get_tx_len,
  368. .init_rx_desc = enh_desc_init_rx_desc,
  369. .init_tx_desc = enh_desc_init_tx_desc,
  370. .get_tx_owner = enh_desc_get_tx_owner,
  371. .release_tx_desc = enh_desc_release_tx_desc,
  372. .prepare_tx_desc = enh_desc_prepare_tx_desc,
  373. .set_tx_ic = enh_desc_set_tx_ic,
  374. .get_tx_ls = enh_desc_get_tx_ls,
  375. .set_tx_owner = enh_desc_set_tx_owner,
  376. .set_rx_owner = enh_desc_set_rx_owner,
  377. .get_rx_frame_len = enh_desc_get_rx_frame_len,
  378. .rx_extended_status = enh_desc_get_ext_status,
  379. .enable_tx_timestamp = enh_desc_enable_tx_timestamp,
  380. .get_tx_timestamp_status = enh_desc_get_tx_timestamp_status,
  381. .get_timestamp = enh_desc_get_timestamp,
  382. .get_rx_timestamp_status = enh_desc_get_rx_timestamp_status,
  383. .display_ring = enh_desc_display_ring,
  384. };