enh_desc.c 11.3 KB
Newer Older
1 2 3
/*******************************************************************************
  This contains the functions to handle the enhanced descriptors.

4
  Copyright (C) 2007-2014  STMicroelectronics Ltd
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

  This program is free software; you can redistribute it and/or modify it
  under the terms and conditions of the GNU General Public License,
  version 2, as published by the Free Software Foundation.

  This program is distributed in the hope it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  more details.

  You should have received a copy of the GNU General Public License along with
  this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.

  The full GNU General Public License is included in this distribution in
  the file called "COPYING".

  Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
*******************************************************************************/

25
#include <linux/stmmac.h>
26
#include "common.h"
27
#include "descs_com.h"
28 29

static int enh_desc_get_tx_status(void *data, struct stmmac_extra_stats *x,
30
				  struct dma_desc *p, void __iomem *ioaddr)
31 32
{
	struct net_device_stats *stats = (struct net_device_stats *)data;
33 34
	unsigned int tdes0 = p->des0;
	int ret = 0;
35

36 37
	if (unlikely(tdes0 & ETDES0_ERROR_SUMMARY)) {
		if (unlikely(tdes0 & ETDES0_JABBER_TIMEOUT))
38 39
			x->tx_jabber++;

40
		if (unlikely(tdes0 & ETDES0_FRAME_FLUSHED)) {
41
			x->tx_frame_flushed++;
42
			dwmac_dma_flush_tx_fifo(ioaddr);
43 44
		}

45
		if (unlikely(tdes0 & ETDES0_LOSS_CARRIER)) {
46 47 48
			x->tx_losscarrier++;
			stats->tx_carrier_errors++;
		}
49
		if (unlikely(tdes0 & ETDES0_NO_CARRIER)) {
50 51 52
			x->tx_carrier++;
			stats->tx_carrier_errors++;
		}
53 54 55 56
		if (unlikely((tdes0 & ETDES0_LATE_COLLISION) ||
			     (tdes0 & ETDES0_EXCESSIVE_COLLISIONS)))
			stats->collisions +=
				(tdes0 & ETDES0_COLLISION_COUNT_MASK) >> 3;
57

58
		if (unlikely(tdes0 & ETDES0_EXCESSIVE_DEFERRAL))
59 60
			x->tx_deferred++;

61
		if (unlikely(tdes0 & ETDES0_UNDERFLOW_ERROR)) {
62
			dwmac_dma_flush_tx_fifo(ioaddr);
63 64 65
			x->tx_underflow++;
		}

66
		if (unlikely(tdes0 & ETDES0_IP_HEADER_ERROR))
67 68
			x->tx_ip_header_error++;

69
		if (unlikely(tdes0 & ETDES0_PAYLOAD_ERROR)) {
70
			x->tx_payload_error++;
71
			dwmac_dma_flush_tx_fifo(ioaddr);
72 73 74 75 76
		}

		ret = -1;
	}

77
	if (unlikely(tdes0 & ETDES0_DEFERRED))
78
		x->tx_deferred++;
79

80
#ifdef STMMAC_VLAN_TAG_USED
81
	if (tdes0 & ETDES0_VLAN_FRAME)
82 83 84 85 86 87 88 89
		x->tx_vlan++;
#endif

	return ret;
}

static int enh_desc_get_tx_len(struct dma_desc *p)
{
90
	return (p->des1 & ETDES1_BUFFER1_SIZE_MASK);
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
}

static int enh_desc_coe_rdes0(int ipc_err, int type, int payload_err)
{
	int ret = good_frame;
	u32 status = (type << 2 | ipc_err << 1 | payload_err) & 0x7;

	/* bits 5 7 0 | Frame status
	 * ----------------------------------------------------------
	 *      0 0 0 | IEEE 802.3 Type frame (length < 1536 octects)
	 *      1 0 0 | IPv4/6 No CSUM errorS.
	 *      1 0 1 | IPv4/6 CSUM PAYLOAD error
	 *      1 1 0 | IPv4/6 CSUM IP HR error
	 *      1 1 1 | IPv4/6 IP PAYLOAD AND HEADER errorS
	 *      0 0 1 | IPv4/6 unsupported IP PAYLOAD
	 *      0 1 1 | COE bypassed.. no IPv4/6 frame
	 *      0 1 0 | Reserved.
	 */
109
	if (status == 0x0)
110
		ret = llc_snap;
111
	else if (status == 0x4)
112
		ret = good_frame;
113
	else if (status == 0x5)
114
		ret = csum_none;
115
	else if (status == 0x6)
116
		ret = csum_none;
117
	else if (status == 0x7)
118
		ret = csum_none;
119
	else if (status == 0x1)
120
		ret = discard_frame;
121
	else if (status == 0x3)
122 123 124 125
		ret = discard_frame;
	return ret;
}

126 127 128
static void enh_desc_get_ext_status(void *data, struct stmmac_extra_stats *x,
				    struct dma_extended_desc *p)
{
129 130 131 132 133 134 135
	unsigned int rdes0 = p->basic.des0;
	unsigned int rdes4 = p->des4;

	if (unlikely(rdes0 & ERDES0_RX_MAC_ADDR)) {
		int message_type = (rdes4 & ERDES4_MSG_TYPE_MASK) >> 8;

		if (rdes4 & ERDES4_IP_HDR_ERR)
136
			x->ip_hdr_err++;
137
		if (rdes4 & ERDES4_IP_PAYLOAD_ERR)
138
			x->ip_payload_err++;
139
		if (rdes4 & ERDES4_IP_CSUM_BYPASSED)
140
			x->ip_csum_bypassed++;
141
		if (rdes4 & ERDES4_IPV4_PKT_RCVD)
142
			x->ipv4_pkt_rcvd++;
143
		if (rdes4 & ERDES4_IPV6_PKT_RCVD)
144
			x->ipv6_pkt_rcvd++;
145
		if (message_type == RDES_EXT_SYNC)
146
			x->rx_msg_type_sync++;
147
		else if (message_type == RDES_EXT_FOLLOW_UP)
148
			x->rx_msg_type_follow_up++;
149
		else if (message_type == RDES_EXT_DELAY_REQ)
150
			x->rx_msg_type_delay_req++;
151
		else if (message_type == RDES_EXT_DELAY_RESP)
152
			x->rx_msg_type_delay_resp++;
153
		else if (message_type == RDES_EXT_PDELAY_REQ)
154
			x->rx_msg_type_pdelay_req++;
155
		else if (message_type == RDES_EXT_PDELAY_RESP)
156
			x->rx_msg_type_pdelay_resp++;
157
		else if (message_type == RDES_EXT_PDELAY_FOLLOW_UP)
158 159 160
			x->rx_msg_type_pdelay_follow_up++;
		else
			x->rx_msg_type_ext_no_ptp++;
161
		if (rdes4 & ERDES4_PTP_FRAME_TYPE)
162
			x->ptp_frame_type++;
163
		if (rdes4 & ERDES4_PTP_VER)
164
			x->ptp_ver++;
165
		if (rdes4 & ERDES4_TIMESTAMP_DROPPED)
166
			x->timestamp_dropped++;
167
		if (rdes4 & ERDES4_AV_PKT_RCVD)
168
			x->av_pkt_rcvd++;
169
		if (rdes4 & ERDES4_AV_TAGGED_PKT_RCVD)
170
			x->av_tagged_pkt_rcvd++;
171
		if ((rdes4 & ERDES4_VLAN_TAG_PRI_VAL_MASK) >> 18)
172
			x->vlan_tag_priority_val++;
173
		if (rdes4 & ERDES4_L3_FILTER_MATCH)
174
			x->l3_filter_match++;
175
		if (rdes4 & ERDES4_L4_FILTER_MATCH)
176
			x->l4_filter_match++;
177
		if ((rdes4 & ERDES4_L3_L4_FILT_NO_MATCH_MASK) >> 26)
178 179 180 181
			x->l3_l4_filter_no_match++;
	}
}

182 183 184 185
static int enh_desc_get_rx_status(void *data, struct stmmac_extra_stats *x,
				  struct dma_desc *p)
{
	struct net_device_stats *stats = (struct net_device_stats *)data;
186 187
	unsigned int rdes0 = p->des0;
	int ret = good_frame;
188

189 190 191
	if (unlikely(rdes0 & RDES0_OWN))
		return dma_own;

192 193
	if (unlikely(rdes0 & RDES0_ERROR_SUMMARY)) {
		if (unlikely(rdes0 & RDES0_DESCRIPTOR_ERROR)) {
194 195 196
			x->rx_desc++;
			stats->rx_length_errors++;
		}
197
		if (unlikely(rdes0 & RDES0_OVERFLOW_ERROR))
198 199
			x->rx_gmac_overflow++;

200
		if (unlikely(rdes0 & RDES0_IPC_CSUM_ERROR))
201
			pr_err("\tIPC Csum Error/Giant frame\n");
202

203
		if (unlikely(rdes0 & RDES0_COLLISION))
204
			stats->collisions++;
205
		if (unlikely(rdes0 & RDES0_RECEIVE_WATCHDOG))
206
			x->rx_watchdog++;
207

208
		if (unlikely(rdes0 & RDES0_MII_ERROR))	/* GMII */
209
			x->rx_mii++;
210

211
		if (unlikely(rdes0 & RDES0_CRC_ERROR)) {
212 213 214 215 216 217 218 219 220 221
			x->rx_crc++;
			stats->rx_crc_errors++;
		}
		ret = discard_frame;
	}

	/* After a payload csum error, the ES bit is set.
	 * It doesn't match with the information reported into the databook.
	 * At any rate, we need to understand if the CSUM hw computation is ok
	 * and report this info to the upper layers. */
222 223 224
	ret = enh_desc_coe_rdes0(!!(rdes0 & RDES0_IPC_CSUM_ERROR),
				 !!(rdes0 & RDES0_FRAME_TYPE),
				 !!(rdes0 & ERDES0_RX_MAC_ADDR));
225

226
	if (unlikely(rdes0 & RDES0_DRIBBLING))
227
		x->dribbling_bit++;
228

229
	if (unlikely(rdes0 & RDES0_SA_FILTER_FAIL)) {
230 231 232
		x->sa_rx_filter_fail++;
		ret = discard_frame;
	}
233
	if (unlikely(rdes0 & RDES0_DA_FILTER_FAIL)) {
234 235 236
		x->da_rx_filter_fail++;
		ret = discard_frame;
	}
237
	if (unlikely(rdes0 & RDES0_LENGTH_ERROR)) {
238 239 240 241
		x->rx_length++;
		ret = discard_frame;
	}
#ifdef STMMAC_VLAN_TAG_USED
242
	if (rdes0 & RDES0_VLAN_TAG)
243 244
		x->rx_vlan++;
#endif
245

246 247 248
	return ret;
}

249 250
static void enh_desc_init_rx_desc(struct dma_desc *p, int disable_rx_ic,
				  int mode, int end)
251
{
252 253
	p->des0 |= RDES0_OWN;
	p->des1 |= ((BUF_SIZE_8KiB - 1) & ERDES1_BUFFER1_SIZE_MASK);
254

255
	if (mode == STMMAC_CHAIN_MODE)
256
		ehn_desc_rx_set_on_chain(p);
257 258
	else
		ehn_desc_rx_set_on_ring(p, end);
259

260
	if (disable_rx_ic)
261
		p->des1 |= ERDES1_DISABLE_IC;
262 263
}

264
static void enh_desc_init_tx_desc(struct dma_desc *p, int mode, int end)
265
{
266
	p->des0 &= ~ETDES0_OWN;
267
	if (mode == STMMAC_CHAIN_MODE)
268
		enh_desc_end_tx_desc_on_chain(p);
269
	else
270
		enh_desc_end_tx_desc_on_ring(p, end);
271 272 273 274
}

static int enh_desc_get_tx_owner(struct dma_desc *p)
{
275
	return (p->des0 & ETDES0_OWN) >> 31;
276 277 278 279
}

static void enh_desc_set_tx_owner(struct dma_desc *p)
{
280
	p->des0 |= ETDES0_OWN;
281 282 283 284
}

static void enh_desc_set_rx_owner(struct dma_desc *p)
{
285
	p->des0 |= RDES0_OWN;
286 287 288 289
}

static int enh_desc_get_tx_ls(struct dma_desc *p)
{
290
	return (p->des0 & ETDES0_LAST_SEGMENT) >> 29;
291 292
}

293
static void enh_desc_release_tx_desc(struct dma_desc *p, int mode)
294
{
295
	int ter = (p->des0 & ETDES0_END_RING) >> 21;
296

297
	memset(p, 0, offsetof(struct dma_desc, des2));
298
	if (mode == STMMAC_CHAIN_MODE)
299
		enh_desc_end_tx_desc_on_chain(p);
300 301
	else
		enh_desc_end_tx_desc_on_ring(p, ter);
302 303 304
}

static void enh_desc_prepare_tx_desc(struct dma_desc *p, int is_fs, int len,
305 306
				     bool csum_flag, int mode, bool tx_own,
				     bool ls_ic)
307
{
308 309 310 311 312 313 314 315 316 317 318 319
	unsigned int tdes0 = p->des0;

	if (is_fs)
		tdes0 |= ETDES0_FIRST_SEGMENT;
	else
		tdes0 &= ~ETDES0_FIRST_SEGMENT;

	if (likely(csum_flag))
		tdes0 |= (TX_CIC_FULL << ETDES0_CHECKSUM_INSERTION_SHIFT);
	else
		tdes0 &= ~(TX_CIC_FULL << ETDES0_CHECKSUM_INSERTION_SHIFT);

320 321 322 323 324 325 326 327 328 329 330 331 332
	if (tx_own)
		tdes0 |= ETDES0_OWN;

	if (is_fs & tx_own)
		/* When the own bit, for the first frame, has to be set, all
		 * descriptors for the same frame has to be set before, to
		 * avoid race condition.
		 */
		wmb();

	if (ls_ic)
		tdes0 |= ETDES0_LAST_SEGMENT | ETDES0_INTERRUPT;

333
	p->des0 = tdes0;
334

335 336 337 338
	if (mode == STMMAC_CHAIN_MODE)
		enh_set_tx_desc_len_on_chain(p, len);
	else
		enh_set_tx_desc_len_on_ring(p, len);
339 340 341 342
}

static void enh_desc_clear_tx_ic(struct dma_desc *p)
{
343
	p->des0 &= ~ETDES0_INTERRUPT;
344 345
}

346
static int enh_desc_get_rx_frame_len(struct dma_desc *p, int rx_coe_type)
347
{
348
	unsigned int csum = 0;
349 350 351 352
	/* The type-1 checksum offload engines append the checksum at
	 * the end of frame and the two bytes of checksum are added in
	 * the length.
	 * Adjust for that in the framelen for type-1 checksum offload
353 354
	 * engines.
	 */
355
	if (rx_coe_type == STMMAC_RX_COE_TYPE1)
356 357 358 359
		csum = 2;

	return (((p->des0 & RDES0_FRAME_LEN_MASK) >> RDES0_FRAME_LEN_SHIFT) -
		csum);
360 361
}

362 363
static void enh_desc_enable_tx_timestamp(struct dma_desc *p)
{
364
	p->des0 |= ETDES0_TIME_STAMP_ENABLE;
365 366 367 368
}

static int enh_desc_get_tx_timestamp_status(struct dma_desc *p)
{
369
	return (p->des0 & ETDES0_TIME_STAMP_STATUS) >> 17;
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
}

static u64 enh_desc_get_timestamp(void *desc, u32 ats)
{
	u64 ns;

	if (ats) {
		struct dma_extended_desc *p = (struct dma_extended_desc *)desc;
		ns = p->des6;
		/* convert high/sec time stamp value to nanosecond */
		ns += p->des7 * 1000000000ULL;
	} else {
		struct dma_desc *p = (struct dma_desc *)desc;
		ns = p->des2;
		ns += p->des3 * 1000000000ULL;
	}

	return ns;
}

static int enh_desc_get_rx_timestamp_status(void *desc, u32 ats)
{
	if (ats) {
		struct dma_extended_desc *p = (struct dma_extended_desc *)desc;
394
		return (p->basic.des0 & RDES0_IPC_CSUM_ERROR) >> 7;
395 396 397 398 399 400 401 402 403 404
	} else {
		struct dma_desc *p = (struct dma_desc *)desc;
		if ((p->des2 == 0xffffffff) && (p->des3 == 0xffffffff))
			/* timestamp is corrupted, hence don't store it */
			return 0;
		else
			return 1;
	}
}

405
const struct stmmac_desc_ops enh_desc_ops = {
406 407 408 409 410 411 412 413 414 415 416 417 418
	.tx_status = enh_desc_get_tx_status,
	.rx_status = enh_desc_get_rx_status,
	.get_tx_len = enh_desc_get_tx_len,
	.init_rx_desc = enh_desc_init_rx_desc,
	.init_tx_desc = enh_desc_init_tx_desc,
	.get_tx_owner = enh_desc_get_tx_owner,
	.release_tx_desc = enh_desc_release_tx_desc,
	.prepare_tx_desc = enh_desc_prepare_tx_desc,
	.clear_tx_ic = enh_desc_clear_tx_ic,
	.get_tx_ls = enh_desc_get_tx_ls,
	.set_tx_owner = enh_desc_set_tx_owner,
	.set_rx_owner = enh_desc_set_rx_owner,
	.get_rx_frame_len = enh_desc_get_rx_frame_len,
419
	.rx_extended_status = enh_desc_get_ext_status,
420 421 422 423
	.enable_tx_timestamp = enh_desc_enable_tx_timestamp,
	.get_tx_timestamp_status = enh_desc_get_tx_timestamp_status,
	.get_timestamp = enh_desc_get_timestamp,
	.get_rx_timestamp_status = enh_desc_get_rx_timestamp_status,
424
};