1 /*******************************************************************************
2 This contains the functions to handle the normal descriptors.
4 Copyright (C) 2007-2009 STMicroelectronics Ltd
6 This program is free software; you can redistribute it and/or modify it
7 under the terms and conditions of the GNU General Public License,
8 version 2, as published by the Free Software Foundation.
10 This program is distributed in the hope it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 The full GNU General Public License is included in this distribution in
16 the file called "COPYING".
18 Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
19 *******************************************************************************/
21 #include <linux/stmmac.h>
23 #include "descs_com.h"
25 static int ndesc_get_tx_status(void *data, struct stmmac_extra_stats *x,
26 struct dma_desc *p, void __iomem *ioaddr)
28 struct net_device_stats *stats = (struct net_device_stats *)data;
29 unsigned int tdes0 = le32_to_cpu(p->des0);
30 unsigned int tdes1 = le32_to_cpu(p->des1);
33 /* Get tx owner first */
34 if (unlikely(tdes0 & TDES0_OWN))
37 /* Verify tx error by looking at the last segment. */
38 if (likely(!(tdes1 & TDES1_LAST_SEGMENT)))
41 if (unlikely(tdes0 & TDES0_ERROR_SUMMARY)) {
42 if (unlikely(tdes0 & TDES0_UNDERFLOW_ERROR)) {
44 stats->tx_fifo_errors++;
46 if (unlikely(tdes0 & TDES0_NO_CARRIER)) {
48 stats->tx_carrier_errors++;
50 if (unlikely(tdes0 & TDES0_LOSS_CARRIER)) {
52 stats->tx_carrier_errors++;
54 if (unlikely((tdes0 & TDES0_EXCESSIVE_DEFERRAL) ||
55 (tdes0 & TDES0_EXCESSIVE_COLLISIONS) ||
56 (tdes0 & TDES0_LATE_COLLISION))) {
57 unsigned int collisions;
59 collisions = (tdes0 & TDES0_COLLISION_COUNT_MASK) >> 3;
60 stats->collisions += collisions;
65 if (tdes0 & TDES0_VLAN_FRAME)
68 if (unlikely(tdes0 & TDES0_DEFERRED))
74 static int ndesc_get_tx_len(struct dma_desc *p)
76 return (le32_to_cpu(p->des1) & RDES1_BUFFER1_SIZE_MASK);
79 /* This function verifies if each incoming frame has some errors
80 * and, if required, updates the multicast statistics.
81 * In case of success, it returns good_frame because the GMAC device
82 * is supposed to be able to compute the csum in HW. */
83 static int ndesc_get_rx_status(void *data, struct stmmac_extra_stats *x,
87 unsigned int rdes0 = le32_to_cpu(p->des0);
88 struct net_device_stats *stats = (struct net_device_stats *)data;
90 if (unlikely(rdes0 & RDES0_OWN))
93 if (unlikely(!(rdes0 & RDES0_LAST_DESCRIPTOR))) {
94 pr_warn("%s: Oversized frame spanned multiple buffers\n",
96 stats->rx_length_errors++;
100 if (unlikely(rdes0 & RDES0_ERROR_SUMMARY)) {
101 if (unlikely(rdes0 & RDES0_DESCRIPTOR_ERROR))
103 if (unlikely(rdes0 & RDES0_SA_FILTER_FAIL))
105 if (unlikely(rdes0 & RDES0_OVERFLOW_ERROR))
107 if (unlikely(rdes0 & RDES0_IPC_CSUM_ERROR))
109 if (unlikely(rdes0 & RDES0_COLLISION)) {
113 if (unlikely(rdes0 & RDES0_CRC_ERROR)) {
115 stats->rx_crc_errors++;
119 if (unlikely(rdes0 & RDES0_DRIBBLING))
122 if (unlikely(rdes0 & RDES0_LENGTH_ERROR)) {
126 if (unlikely(rdes0 & RDES0_MII_ERROR)) {
130 #ifdef STMMAC_VLAN_TAG_USED
131 if (rdes0 & RDES0_VLAN_TAG)
137 static void ndesc_init_rx_desc(struct dma_desc *p, int disable_rx_ic, int mode,
140 p->des0 |= cpu_to_le32(RDES0_OWN);
141 p->des1 |= cpu_to_le32((BUF_SIZE_2KiB - 1) & RDES1_BUFFER1_SIZE_MASK);
143 if (mode == STMMAC_CHAIN_MODE)
144 ndesc_rx_set_on_chain(p, end);
146 ndesc_rx_set_on_ring(p, end);
149 p->des1 |= cpu_to_le32(RDES1_DISABLE_IC);
152 static void ndesc_init_tx_desc(struct dma_desc *p, int mode, int end)
154 p->des0 &= cpu_to_le32(~TDES0_OWN);
155 if (mode == STMMAC_CHAIN_MODE)
156 ndesc_tx_set_on_chain(p);
158 ndesc_end_tx_desc_on_ring(p, end);
161 static int ndesc_get_tx_owner(struct dma_desc *p)
163 return (le32_to_cpu(p->des0) & TDES0_OWN) >> 31;
166 static void ndesc_set_tx_owner(struct dma_desc *p)
168 p->des0 |= cpu_to_le32(TDES0_OWN);
171 static void ndesc_set_rx_owner(struct dma_desc *p)
173 p->des0 |= cpu_to_le32(RDES0_OWN);
176 static int ndesc_get_tx_ls(struct dma_desc *p)
178 return (le32_to_cpu(p->des1) & TDES1_LAST_SEGMENT) >> 30;
181 static void ndesc_release_tx_desc(struct dma_desc *p, int mode)
183 int ter = (le32_to_cpu(p->des1) & TDES1_END_RING) >> 25;
185 memset(p, 0, offsetof(struct dma_desc, des2));
186 if (mode == STMMAC_CHAIN_MODE)
187 ndesc_tx_set_on_chain(p);
189 ndesc_end_tx_desc_on_ring(p, ter);
192 static void ndesc_prepare_tx_desc(struct dma_desc *p, int is_fs, int len,
193 bool csum_flag, int mode, bool tx_own,
194 bool ls, unsigned int tot_pkt_len)
196 unsigned int tdes1 = le32_to_cpu(p->des1);
199 tdes1 |= TDES1_FIRST_SEGMENT;
201 tdes1 &= ~TDES1_FIRST_SEGMENT;
203 if (likely(csum_flag))
204 tdes1 |= (TX_CIC_FULL) << TDES1_CHECKSUM_INSERTION_SHIFT;
206 tdes1 &= ~(TX_CIC_FULL << TDES1_CHECKSUM_INSERTION_SHIFT);
209 tdes1 |= TDES1_LAST_SEGMENT;
211 p->des1 = cpu_to_le32(tdes1);
213 if (mode == STMMAC_CHAIN_MODE)
214 norm_set_tx_desc_len_on_chain(p, len);
216 norm_set_tx_desc_len_on_ring(p, len);
219 p->des0 |= cpu_to_le32(TDES0_OWN);
222 static void ndesc_set_tx_ic(struct dma_desc *p)
224 p->des1 |= cpu_to_le32(TDES1_INTERRUPT);
227 static int ndesc_get_rx_frame_len(struct dma_desc *p, int rx_coe_type)
229 unsigned int csum = 0;
231 /* The type-1 checksum offload engines append the checksum at
232 * the end of frame and the two bytes of checksum are added in
234 * Adjust for that in the framelen for type-1 checksum offload
237 if (rx_coe_type == STMMAC_RX_COE_TYPE1)
240 return (((le32_to_cpu(p->des0) & RDES0_FRAME_LEN_MASK)
241 >> RDES0_FRAME_LEN_SHIFT) -
246 static void ndesc_enable_tx_timestamp(struct dma_desc *p)
248 p->des1 |= cpu_to_le32(TDES1_TIME_STAMP_ENABLE);
251 static int ndesc_get_tx_timestamp_status(struct dma_desc *p)
253 return (le32_to_cpu(p->des0) & TDES0_TIME_STAMP_STATUS) >> 17;
256 static u64 ndesc_get_timestamp(void *desc, u32 ats)
258 struct dma_desc *p = (struct dma_desc *)desc;
261 ns = le32_to_cpu(p->des2);
262 /* convert high/sec time stamp value to nanosecond */
263 ns += le32_to_cpu(p->des3) * 1000000000ULL;
268 static int ndesc_get_rx_timestamp_status(void *desc, void *next_desc, u32 ats)
270 struct dma_desc *p = (struct dma_desc *)desc;
272 if ((le32_to_cpu(p->des2) == 0xffffffff) &&
273 (le32_to_cpu(p->des3) == 0xffffffff))
274 /* timestamp is corrupted, hence don't store it */
280 static void ndesc_display_ring(void *head, unsigned int size, bool rx)
282 struct dma_desc *p = (struct dma_desc *)head;
285 pr_info("%s descriptor ring:\n", rx ? "RX" : "TX");
287 for (i = 0; i < size; i++) {
291 pr_info("%d [0x%x]: 0x%x 0x%x 0x%x 0x%x",
292 i, (unsigned int)virt_to_phys(p),
293 (unsigned int)x, (unsigned int)(x >> 32),
300 const struct stmmac_desc_ops ndesc_ops = {
301 .tx_status = ndesc_get_tx_status,
302 .rx_status = ndesc_get_rx_status,
303 .get_tx_len = ndesc_get_tx_len,
304 .init_rx_desc = ndesc_init_rx_desc,
305 .init_tx_desc = ndesc_init_tx_desc,
306 .get_tx_owner = ndesc_get_tx_owner,
307 .release_tx_desc = ndesc_release_tx_desc,
308 .prepare_tx_desc = ndesc_prepare_tx_desc,
309 .set_tx_ic = ndesc_set_tx_ic,
310 .get_tx_ls = ndesc_get_tx_ls,
311 .set_tx_owner = ndesc_set_tx_owner,
312 .set_rx_owner = ndesc_set_rx_owner,
313 .get_rx_frame_len = ndesc_get_rx_frame_len,
314 .enable_tx_timestamp = ndesc_enable_tx_timestamp,
315 .get_tx_timestamp_status = ndesc_get_tx_timestamp_status,
316 .get_timestamp = ndesc_get_timestamp,
317 .get_rx_timestamp_status = ndesc_get_rx_timestamp_status,
318 .display_ring = ndesc_display_ring,