aboutsummaryrefslogtreecommitdiff
path: root/drivers/r8169.c
blob: 1a563abeea3f984fb6523625284717c8eb6d90b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <stdio.h>
#include <endian.h>
#include <unistd.h>
/* Our */
#include <drivers/r8169.h>
#include <drivers/driver_ops.h>
#include <mm_api.h>

typedef unsigned long dma_addr_t;

static void print_packet(unsigned char *buffer)
{
	int i;
	printf("%02x:%02x:%02x:%02x:%02x:%02x -> %02x:%02x:%02x:%02x:%02x:%02x [%04x]:",
		buffer[6], buffer[7], buffer[8], buffer[9], buffer[10], buffer[11],
		buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5],
		be16toh(*((__u16*)(&buffer[12])))
	);

	for (i = 14; i < 32; i++) {
		printf("%02x", buffer[i]);
	}
}

static inline void rtl8169_mark_to_asic(struct RxDesc *desc, __u32 rx_buf_sz)
{
	__u32 eor = le32_to_cpu(desc->opts1) & RingEnd;

	/* Force memory writes to complete before releasing descriptor */
	dma_wmb();

	desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
}

static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
	__u32 rx_buf_sz)
{
	desc->addr = cpu_to_le64(mapping);
	rtl8169_mark_to_asic(desc, rx_buf_sz);
}

static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
{
	desc->opts1 |= cpu_to_le32(RingEnd);
}

static int rtl8169_rx_fill(int device, void *rx_data, struct iomem data,
			   char *rx_buff[])
{
	int i;
	struct RxDesc *r8169_rxring = (struct RxDesc *) rx_data;

	for (i = 0; i < NUM_RX_DESC; i++) {
		rtl8169_map_to_asic(&r8169_rxring[i], data.iova + i * 2048, 2048);
		rx_buff[i] = (char *)(data.vaddr + i * 2048);
	}
	rtl8169_mark_as_last_descriptor(&r8169_rxring[NUM_RX_DESC - 1]);

	return 0;
}

void r8169_recv(void *rxring, char *rx_buff[])
{
	int i = 0;
	struct RxDesc *r8169_rxring = (struct RxDesc *)rxring;

	while (1) {
		if (i >= NUM_RX_DESC)
			i = 0;
		for (; i < NUM_RX_DESC; i++) {
			__u32 status;

			status = le32_to_cpu(r8169_rxring[i].opts1) & ~0; /// either  ~(RxBOVF | RxFOVF) or ~0;

			if (status & DescOwn) {
				usleep(100*1000);
				break;
			}
			/* This barrier is needed to keep us from reading
			* any other fields out of the Rx descriptor until
			* we know the status of DescOwn
			*/
			dma_rmb();

			if (unlikely(status & RxRES)) {
				printf("Rx ERROR. status = %08x\n",status);
				/*
				dev->stats.rx_errors++;
				if (status & (RxRWT | RxRUNT))
					dev->stats.rx_length_errors++;
				if (status & RxCRC)
					dev->stats.rx_crc_errors++;
				if (status & RxFOVF) {
					rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
					dev->stats.rx_fifo_errors++;
				}
				*/
				if ((status & (RxRUNT | RxCRC)) &&
					!(status & (RxRWT | RxFOVF))
					/* && 	(dev->features & NETIF_F_RXALL) */
					)
					goto process_pkt;
			} else {
				//dma_addr_t addr;
				int pkt_size;
process_pkt:
				//addr = le64_to_cpu(r8169_rxring[i].addr);
				if (1) // likely(!(dev->features & NETIF_F_RXFCS)))
					pkt_size = (status & 0x00003fff) - 4;
				else
					pkt_size = status & 0x00003fff;

				/*
				* The driver does not support incoming fragmented
				* frames. They are seen as a symptom of over-mtu
				* sized frames.
				*/
				/*
				if (unlikely(rtl8169_fragmented_frame(status))) {
					dev->stats.rx_dropped++;
					dev->stats.rx_length_errors++;
					goto release_descriptor;
				}

				skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
					tp, pkt_size, addr);
				if (!skb) {
					dev->stats.rx_dropped++;
					goto release_descriptor;
				}

				rtl8169_rx_csum(skb, status);
				skb_put(skb, pkt_size);
				skb->protocol = eth_type_trans(skb, dev);

				rtl8169_rx_vlan_tag(desc, skb);

				if (skb->pkt_type == PACKET_MULTICAST)
					dev->stats.multicast++;

				napi_gro_receive(&tp->napi, skb);

				__u64_stats_update_begin(&tp->rx_stats.syncp);
				tp->rx_stats.packets++;
				tp->rx_stats.bytes += pkt_size;
				__u64_stats_update_end(&tp->rx_stats.syncp);
				*/
				printf("desc[%03d]: size= %5d ", i, pkt_size);
				print_packet((unsigned char *)rx_buff[i]);
				printf("\n");
			}
			/* release_descriptor: */
			r8169_rxring[i].opts2 = 0;
			rtl8169_mark_to_asic(&r8169_rxring[i], 2048);
		}
	}
}

/* FIXME make this staic once we have loadbale module support */
const struct driver_ops r8169_ops = {
	/* endianess for vendor/id? */
	.vendor = 0x10ec,
	.device = 0x8168,
	.vfio_quirks = NULL,
	.rx_fill = rtl8169_rx_fill,
	.tx_fill= NULL,
	.recv = r8169_recv,
	.xmit = NULL,
};