aboutsummaryrefslogtreecommitdiffstats
path: root/gpxe/src/drivers/net/mlx_ipoib/mt23108_imp.c
blob: bb2383c5fa7f5fa6a27beeb18fee8d528740be31 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
typedef uint32_t __u32;
typedef uint16_t __u16;
typedef uint8_t __u8;

static int verbose_messages=0;
static int print_info=0;
static int fatal_condition=0;
static int fw_fatal;

#define tprintf(fmt, a...) \
		 do {    \
			if ( verbose_messages ) { \
				printf("%s:%d: " fmt "\n", __func__, __LINE__,  ##a); \
			} \
		 } \
		 while(0)

#define eprintf(fmt, a...) \
		 printf("%s:%d: " fmt "\n", __func__, __LINE__,  ##a)

static void cpu_to_be_buf(void *buf, int size)
{
	int dw_sz = size >> 2, i;

	for (i = 0; i < dw_sz; ++i) {
		((__u32 *) buf)[i] = cpu_to_be32(((__u32 *) buf)[i]);
	}
}

static void be_to_cpu_buf(void *buf, int size)
{
	int dw_sz = size >> 2, i;
	u32 *p = buf;

	for (i = 0; i < dw_sz; ++i) {
		p[i] = be32_to_cpu(p[i]);
	}
}

#include "cmdif_mt23108.c"
#include "cmdif_comm.c"
#include "ib_mt23108.c"
#include "ib_mad.c"
#include "ib_driver.c"
#include "ipoib.c"

static int probe_imp(struct pci_device *pci, struct nic *nic)
{
	int rc;

	if (0 && nic) {		/* just to supress warning */
		return 0;
	}

	fatal_condition= 0;
	fw_fatal= 0;

	tprintf("");
	rc = ipoib_init(pci);
	if (rc)
		return rc;

	tprintf("");

	return rc;
}

static int disable_imp(void)
{
	int rc;

	rc = ipoib_close(fw_fatal);

	return rc;
}

static int transmit_imp(const char *dest,	/* Destination */
			unsigned int type,	/* Type */
			const char *packet,	/* Packet */
			unsigned int size)
{				/* size */
	int rc;

	if (fatal_condition) {
		/* since the transmit function does not return a value
		   we return success but do nothing to suppress error messages */
		return 0;
	}

	rc = ipoib_send_packet(dest, type, packet, size);
	if (rc) {
		printf("*** ERROR IN SEND FLOW ***\n");
		printf("restarting Etherboot\n");
		sleep(1);
		longjmp(restart_etherboot, -1);
		/* we should not be here ... */
		return -1; 
	}

	return rc;
}

static void hd(void *where, int n)
{
	int i;

	while (n > 0) {
		printf("%X ", where);
		for (i = 0; i < ((n > 16) ? 16 : n); i++)
			printf(" %hhX", ((char *)where)[i]);
		printf("\n");
		n -= 16;
		where += 16;
	}
}

static int poll_imp(struct nic *nic, int retrieve, unsigned int *size_p)
{
	static char packet[2048];
	static char *last_packet_p = NULL;
	static unsigned long last_packet_size;
	char *packet_p;
	const int eth_header_len = 14;
	unsigned int packet_len;
	int is_bcast = 0;
	__u16 prot, *ptr;
	int rc;

	if (0 && nic) {		/* just to supress warning */
		return -1;
	}

	if (fatal_condition) {
		*size_p = 0;
		return 0;
	}

	if (poll_error_buf()) {
		fatal_condition= 1;
		fw_fatal= 1;
		printf("\n *** DEVICE FATAL ERROR ***\n");
		goto fatal_handling;
	}
	else if (drain_eq()) {
		fatal_condition= 1;
		printf("\n *** FATAL ERROR ***\n");
		goto fatal_handling;
	}


	if (retrieve) {
		/* we actually want to read the packet */
		if (last_packet_p) {
			eprintf("");
			/* there is already a packet that was previously read */
			memcpy(nic->packet, last_packet_p, last_packet_size);
			*size_p = last_packet_size;
			last_packet_p = NULL;
			return 0;
		}
		packet_p = nic->packet;
	} else {
		/* we don't want to read the packet,
		   just know if there is one. so we
		   read the packet to a local buffer and
		   we will return that buffer when the ip layer wants
		   another packet */
		if (last_packet_p) {
			/* there is already a packet that
			   was not consumend */
			eprintf("overflow receive packets");
			return -1;
		}
		packet_p = packet;
	}

	rc = ipoib_read_packet(&prot, packet_p + eth_header_len, &packet_len,
			       &is_bcast);
	if (rc) {
		printf("*** FATAL IN RECEIVE FLOW ****\n");
		goto fatal_handling;
	}

	if (packet_len == 0) {
		*size_p = 0;
		return 0;
	}

	if (is_bcast) {
		int i;
		for (i = 0; i < 6; ++i) {
			packet_p[i] = 0xff;
		}
	} else {
		packet_p[0] = MLX_ETH_BYTE0;
		packet_p[1] = MLX_ETH_BYTE1;
		packet_p[2] = MLX_ETH_BYTE2;
		packet_p[3] = 0;
		packet_p[4] = 0;
		packet_p[5] = 0;
	}

	memset(packet_p + 6, 0, 6);

	ptr = (__u16 *) (packet_p + 12);
	*ptr = htons(prot);

	if (!retrieve) {
		last_packet_p = packet;
		last_packet_size = packet_len + eth_header_len;
		*size_p = 0;
	}

	*size_p = packet_len + eth_header_len;
	tprintf("packet size=%d, prot=%x\n", *size_p, prot);
	if (0) {
		hd(nic->packet, 42);
	}

	return 0;

fatal_handling:
	printf("restarting Etherboot\n");
	sleep(1);
	longjmp(restart_etherboot, -1);
	/* we should not be here ... */
	return -1; 
	
}