2 * This contains the functions to handle the descriptors for DesignWare databook
5 * Copyright (C) 2015 STMicroelectronics Ltd
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.
11 * Author: Alexandre Torgue <alexandre.torgue@st.com>
14 #include <linux/stmmac.h>
16 #include "dwmac4_descs.h"
18 static int dwmac4_wrback_get_tx_status(void *data, struct stmmac_extra_stats *x,
22 struct net_device_stats *stats = (struct net_device_stats *)data;
28 /* Get tx owner first */
29 if (unlikely(tdes3 & TDES3_OWN))
32 /* Verify tx error by looking at the last segment. */
33 if (likely(!(tdes3 & TDES3_LAST_DESCRIPTOR)))
36 if (unlikely(tdes3 & TDES3_ERROR_SUMMARY)) {
37 if (unlikely(tdes3 & TDES3_JABBER_TIMEOUT))
39 if (unlikely(tdes3 & TDES3_PACKET_FLUSHED))
40 x->tx_frame_flushed++;
41 if (unlikely(tdes3 & TDES3_LOSS_CARRIER)) {
43 stats->tx_carrier_errors++;
45 if (unlikely(tdes3 & TDES3_NO_CARRIER)) {
47 stats->tx_carrier_errors++;
49 if (unlikely((tdes3 & TDES3_LATE_COLLISION) ||
50 (tdes3 & TDES3_EXCESSIVE_COLLISION)))
52 (tdes3 & TDES3_COLLISION_COUNT_MASK)
53 >> TDES3_COLLISION_COUNT_SHIFT;
55 if (unlikely(tdes3 & TDES3_EXCESSIVE_DEFERRAL))
58 if (unlikely(tdes3 & TDES3_UNDERFLOW_ERROR))
61 if (unlikely(tdes3 & TDES3_IP_HDR_ERROR))
62 x->tx_ip_header_error++;
64 if (unlikely(tdes3 & TDES3_PAYLOAD_ERROR))
65 x->tx_payload_error++;
70 if (unlikely(tdes3 & TDES3_DEFERRED))
76 static int dwmac4_wrback_get_rx_status(void *data, struct stmmac_extra_stats *x,
79 struct net_device_stats *stats = (struct net_device_stats *)data;
80 unsigned int rdes1 = p->des1;
81 unsigned int rdes2 = p->des2;
82 unsigned int rdes3 = p->des3;
86 if (unlikely(rdes3 & RDES3_OWN))
89 /* Verify rx error by looking at the last segment. */
90 if (likely(!(rdes3 & RDES3_LAST_DESCRIPTOR)))
93 if (unlikely(rdes3 & RDES3_ERROR_SUMMARY)) {
94 if (unlikely(rdes3 & RDES3_GIANT_PACKET))
95 stats->rx_length_errors++;
96 if (unlikely(rdes3 & RDES3_OVERFLOW_ERROR))
97 x->rx_gmac_overflow++;
99 if (unlikely(rdes3 & RDES3_RECEIVE_WATCHDOG))
102 if (unlikely(rdes3 & RDES3_RECEIVE_ERROR))
105 if (unlikely(rdes3 & RDES3_CRC_ERROR)) {
107 stats->rx_crc_errors++;
110 if (unlikely(rdes3 & RDES3_DRIBBLE_ERROR))
116 message_type = (rdes1 & ERDES4_MSG_TYPE_MASK) >> 8;
118 if (rdes1 & RDES1_IP_HDR_ERROR)
120 if (rdes1 & RDES1_IP_CSUM_BYPASSED)
121 x->ip_csum_bypassed++;
122 if (rdes1 & RDES1_IPV4_HEADER)
124 if (rdes1 & RDES1_IPV6_HEADER)
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++;
141 x->rx_msg_type_ext_no_ptp++;
143 if (rdes1 & RDES1_PTP_PACKET_TYPE)
145 if (rdes1 & RDES1_PTP_VER)
147 if (rdes1 & RDES1_TIMESTAMP_DROPPED)
148 x->timestamp_dropped++;
150 if (unlikely(rdes2 & RDES2_SA_FILTER_FAIL)) {
151 x->sa_rx_filter_fail++;
154 if (unlikely(rdes2 & RDES2_DA_FILTER_FAIL)) {
155 x->da_rx_filter_fail++;
159 if (rdes2 & RDES2_L3_FILTER_MATCH)
160 x->l3_filter_match++;
161 if (rdes2 & RDES2_L4_FILTER_MATCH)
162 x->l4_filter_match++;
163 if ((rdes2 & RDES2_L3_L4_FILT_NB_MATCH_MASK)
164 >> RDES2_L3_L4_FILT_NB_MATCH_SHIFT)
165 x->l3_l4_filter_no_match++;
170 static int dwmac4_rd_get_tx_len(struct dma_desc *p)
172 return (p->des2 & TDES2_BUFFER1_SIZE_MASK);
175 static int dwmac4_get_tx_owner(struct dma_desc *p)
177 return (p->des3 & TDES3_OWN) >> TDES3_OWN_SHIFT;
180 static void dwmac4_set_tx_owner(struct dma_desc *p)
182 p->des3 |= TDES3_OWN;
185 static void dwmac4_set_rx_owner(struct dma_desc *p)
187 p->des3 |= RDES3_OWN;
190 static int dwmac4_get_tx_ls(struct dma_desc *p)
192 return (p->des3 & TDES3_LAST_DESCRIPTOR) >> TDES3_LAST_DESCRIPTOR_SHIFT;
195 static int dwmac4_wrback_get_rx_frame_len(struct dma_desc *p, int rx_coe)
197 return (p->des3 & RDES3_PACKET_SIZE_MASK);
200 static void dwmac4_rd_enable_tx_timestamp(struct dma_desc *p)
202 p->des2 |= TDES2_TIMESTAMP_ENABLE;
205 static int dwmac4_wrback_get_tx_timestamp_status(struct dma_desc *p)
207 return (p->des3 & TDES3_TIMESTAMP_STATUS)
208 >> TDES3_TIMESTAMP_STATUS_SHIFT;
211 /* NOTE: For RX CTX bit has to be checked before
212 * HAVE a specific function for TX and another one for RX
214 static u64 dwmac4_wrback_get_timestamp(void *desc, u32 ats)
216 struct dma_desc *p = (struct dma_desc *)desc;
220 /* convert high/sec time stamp value to nanosecond */
221 ns += p->des1 * 1000000000ULL;
226 static int dwmac4_context_get_rx_timestamp_status(void *desc, u32 ats)
228 struct dma_desc *p = (struct dma_desc *)desc;
230 return (p->des1 & RDES1_TIMESTAMP_AVAILABLE)
231 >> RDES1_TIMESTAMP_AVAILABLE_SHIFT;
234 static void dwmac4_rd_init_rx_desc(struct dma_desc *p, int disable_rx_ic,
237 p->des3 = RDES3_OWN | RDES3_BUFFER1_VALID_ADDR;
240 p->des3 |= RDES3_INT_ON_COMPLETION_EN;
243 static void dwmac4_rd_init_tx_desc(struct dma_desc *p, int mode, int end)
251 static void dwmac4_rd_prepare_tx_desc(struct dma_desc *p, int is_fs, int len,
252 bool csum_flag, int mode, bool tx_own,
255 unsigned int tdes3 = p->des3;
257 p->des2 |= (len & TDES2_BUFFER1_SIZE_MASK);
260 tdes3 |= TDES3_FIRST_DESCRIPTOR;
262 tdes3 &= ~TDES3_FIRST_DESCRIPTOR;
264 if (likely(csum_flag))
265 tdes3 |= (TX_CIC_FULL << TDES3_CHECKSUM_INSERTION_SHIFT);
267 tdes3 &= ~(TX_CIC_FULL << TDES3_CHECKSUM_INSERTION_SHIFT);
270 tdes3 |= TDES3_LAST_DESCRIPTOR;
272 tdes3 &= ~TDES3_LAST_DESCRIPTOR;
274 /* Finally set the OWN bit. Later the DMA will start! */
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.
288 static void dwmac4_rd_prepare_tso_tx_desc(struct dma_desc *p, int is_fs,
289 int len1, int len2, bool tx_own,
290 bool ls, unsigned int tcphdrlen,
291 unsigned int tcppayloadlen)
293 unsigned int tdes3 = p->des3;
296 p->des2 |= (len1 & TDES2_BUFFER1_SIZE_MASK);
299 p->des2 |= (len2 << TDES2_BUFFER2_SIZE_MASK_SHIFT)
300 & TDES2_BUFFER2_SIZE_MASK;
303 tdes3 |= TDES3_FIRST_DESCRIPTOR |
304 TDES3_TCP_SEGMENTATION_ENABLE |
305 ((tcphdrlen << TDES3_HDR_LEN_SHIFT) &
306 TDES3_SLOT_NUMBER_MASK) |
307 ((tcppayloadlen & TDES3_TCP_PKT_PAYLOAD_MASK));
309 tdes3 &= ~TDES3_FIRST_DESCRIPTOR;
313 tdes3 |= TDES3_LAST_DESCRIPTOR;
315 tdes3 &= ~TDES3_LAST_DESCRIPTOR;
317 /* Finally set the OWN bit. Later the DMA will start! */
322 /* When the own bit, for the first frame, has to be set, all
323 * descriptors for the same frame has to be set before, to
324 * avoid race condition.
331 static void dwmac4_release_tx_desc(struct dma_desc *p, int mode)
337 static void dwmac4_rd_set_tx_ic(struct dma_desc *p)
339 p->des2 |= TDES2_INTERRUPT_ON_COMPLETION;
342 static void dwmac4_display_ring(void *head, unsigned int size, bool rx)
344 struct dma_desc *p = (struct dma_desc *)head;
347 pr_info("%s descriptor ring:\n", rx ? "RX" : "TX");
349 for (i = 0; i < size; i++) {
351 pr_info("%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
352 i, (unsigned int)virt_to_phys(p),
353 p->des0, p->des1, p->des2, p->des3);
358 static void dwmac4_set_mss_ctxt(struct dma_desc *p, unsigned int mss)
363 p->des3 = TDES3_CONTEXT_TYPE | TDES3_CTXT_TCMSSV;
366 const struct stmmac_desc_ops dwmac4_desc_ops = {
367 .tx_status = dwmac4_wrback_get_tx_status,
368 .rx_status = dwmac4_wrback_get_rx_status,
369 .get_tx_len = dwmac4_rd_get_tx_len,
370 .get_tx_owner = dwmac4_get_tx_owner,
371 .set_tx_owner = dwmac4_set_tx_owner,
372 .set_rx_owner = dwmac4_set_rx_owner,
373 .get_tx_ls = dwmac4_get_tx_ls,
374 .get_rx_frame_len = dwmac4_wrback_get_rx_frame_len,
375 .enable_tx_timestamp = dwmac4_rd_enable_tx_timestamp,
376 .get_tx_timestamp_status = dwmac4_wrback_get_tx_timestamp_status,
377 .get_timestamp = dwmac4_wrback_get_timestamp,
378 .get_rx_timestamp_status = dwmac4_context_get_rx_timestamp_status,
379 .set_tx_ic = dwmac4_rd_set_tx_ic,
380 .prepare_tx_desc = dwmac4_rd_prepare_tx_desc,
381 .prepare_tso_tx_desc = dwmac4_rd_prepare_tso_tx_desc,
382 .release_tx_desc = dwmac4_release_tx_desc,
383 .init_rx_desc = dwmac4_rd_init_rx_desc,
384 .init_tx_desc = dwmac4_rd_init_tx_desc,
385 .display_ring = dwmac4_display_ring,
386 .set_mss = dwmac4_set_mss_ctxt,
389 const struct stmmac_mode_ops dwmac4_ring_mode_ops = { };