summaryrefslogtreecommitdiffstats
path: root/qemu/roms/ipxe/src/drivers/net/intelvf.c
blob: ac6fea745457863544edf66581380f369bf2adc2 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
 * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that 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 Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <ipxe/io.h>
#include <ipxe/netdevice.h>
#include <ipxe/ethernet.h>
#include "intelvf.h"

/** @file
 *
 * Intel 10/100/1000 virtual function network card driver
 *
 */

/******************************************************************************
 *
 * Mailbox messages
 *
 ******************************************************************************
 */

/**
 * Write message to mailbox
 *
 * @v intel		Intel device
 * @v msg		Message
 */
static void intelvf_mbox_write ( struct intel_nic *intel,
				 const union intelvf_msg *msg ) {
	unsigned int i;

	/* Write message */
	DBGC2 ( intel, "INTEL %p sending message", intel );
	for ( i = 0 ; i < ( sizeof ( *msg ) / sizeof ( msg->dword[0] ) ) ; i++){
		DBGC2 ( intel, "%c%08x", ( i ? ':' : ' ' ), msg->dword[i] );
		writel ( msg->dword[i], ( intel->regs + intel->mbox.mem +
					  ( i * sizeof ( msg->dword[0] ) ) ) );
	}
	DBGC2 ( intel, "\n" );
}

/**
 * Read message from mailbox
 *
 * @v intel		Intel device
 * @v msg		Message
 */
static void intelvf_mbox_read ( struct intel_nic *intel,
				union intelvf_msg *msg ) {
	unsigned int i;

	/* Read message */
	DBGC2 ( intel, "INTEL %p received message", intel );
	for ( i = 0 ; i < ( sizeof ( *msg ) / sizeof ( msg->dword[0] ) ) ; i++){
		msg->dword[i] = readl ( intel->regs + intel->mbox.mem +
					( i * sizeof ( msg->dword[0] ) ) );
		DBGC2 ( intel, "%c%08x", ( i ? ':' : ' ' ), msg->dword[i] );
	}
	DBGC2 ( intel, "\n" );
}

/**
 * Poll mailbox
 *
 * @v intel		Intel device
 * @ret rc		Return status code
 *
 * Note that polling the mailbox may fail if the underlying PF is
 * reset.
 */
int intelvf_mbox_poll ( struct intel_nic *intel ) {
	struct intel_mailbox *mbox = &intel->mbox;
	union intelvf_msg msg;
	uint32_t ctrl;

	/* Get mailbox status */
	ctrl = readl ( intel->regs + mbox->ctrl );

	/* Fail if a reset is in progress */
	if ( ctrl & INTELVF_MBCTRL_RSTI )
		return -EPIPE;

	/* Acknowledge (and ignore) any received messages */
	if ( ctrl & INTELVF_MBCTRL_PFSTS ) {
		intelvf_mbox_read ( intel, &msg );
		writel ( INTELVF_MBCTRL_ACK, intel->regs + mbox->ctrl );
	}

	return 0;
}

/**
 * Wait for PF reset to complete
 *
 * @v intel		Intel device
 * @ret rc		Return status code
 */
int intelvf_mbox_wait ( struct intel_nic *intel ) {
	unsigned int i;
	int rc;

	/* Wait until a poll completes successfully */
	for ( i = 0 ; i < INTELVF_MBOX_MAX_WAIT_MS ; i++ ) {

		/* Check for successful poll */
		if ( ( rc = intelvf_mbox_poll ( intel ) ) == 0 )
			return 0;

		/* Delay */
		mdelay ( 1 );
	}

	DBGC ( intel, "INTEL %p timed out waiting for reset\n", intel );
	return -ETIMEDOUT;
}

/**
 * Send/receive mailbox message
 *
 * @v intel		Intel device
 * @v msg		Message buffer
 * @ret rc		Return status code
 */
int intelvf_mbox_msg ( struct intel_nic *intel, union intelvf_msg *msg ) {
	struct intel_mailbox *mbox = &intel->mbox;
	uint32_t ctrl;
	uint32_t seen = 0;
	unsigned int i;

	/* Sanity check */
	assert ( ! ( msg->hdr & INTELVF_MSG_RESPONSE ) );

	/* Handle mailbox */
	for ( i = 0 ; i < INTELVF_MBOX_MAX_WAIT_MS ; i++ ) {

		/* Attempt to claim mailbox, if we have not yet sent
		 * our message.
		 */
		if ( ! ( seen & INTELVF_MBCTRL_VFU ) )
			writel ( INTELVF_MBCTRL_VFU, intel->regs + mbox->ctrl );

		/* Get mailbox status and record observed flags */
		ctrl = readl ( intel->regs + mbox->ctrl );
		seen |= ctrl;

		/* If a reset is in progress, clear VFU and abort */
		if ( ctrl & INTELVF_MBCTRL_RSTI ) {
			writel ( 0, intel->regs + mbox->ctrl );
			return -EPIPE;
		}

		/* Write message to mailbox, if applicable.  This
		 * potentially overwrites a message sent by the PF (if
		 * the PF has simultaneously released PFU (thus
		 * allowing our VFU) and asserted PFSTS), but that
		 * doesn't really matter since there are no
		 * unsolicited PF->VF messages that require the actual
		 * message content to be observed.
		 */
		if ( ctrl & INTELVF_MBCTRL_VFU )
			intelvf_mbox_write ( intel, msg );

		/* Read message from mailbox, if applicable. */
		if ( ( seen & INTELVF_MBCTRL_VFU ) &&
		     ( seen & INTELVF_MBCTRL_PFACK ) &&
		     ( ctrl & INTELVF_MBCTRL_PFSTS ) )
			intelvf_mbox_read ( intel, msg );

		/* Acknowledge received message (if applicable),
		 * release VFU lock, and send message (if applicable).
		 */
		ctrl = ( ( ( ctrl & INTELVF_MBCTRL_PFSTS ) ?
			   INTELVF_MBCTRL_ACK : 0 ) |
			 ( ( ctrl & INTELVF_MBCTRL_VFU ) ?
			   INTELVF_MBCTRL_REQ : 0 ) );
		writel ( ctrl, intel->regs + mbox->ctrl );

		/* Exit successfully if we have received a response */
		if ( msg->hdr & INTELVF_MSG_RESPONSE ) {

			/* Sanity check */
			assert ( seen & INTELVF_MBCTRL_VFU );
			assert ( seen & INTELVF_MBCTRL_PFACK );
			assert ( seen & INTELVF_MBCTRL_PFSTS );

			return 0;
		}

		/* Delay */
		mdelay ( 1 );
	}

	DBGC ( intel, "INTEL %p timed out waiting for mailbox (seen %08x)\n",
	       intel, seen );
	return -ETIMEDOUT;
}

/**
 * Send reset message and get initial MAC address
 *
 * @v intel		Intel device
 * @v hw_addr		Hardware address to fill in, or NULL
 * @ret rc		Return status code
 */
int intelvf_mbox_reset ( struct intel_nic *intel, uint8_t *hw_addr ) {
	union intelvf_msg msg;
	int rc;

	/* Send reset message */
	memset ( &msg, 0, sizeof ( msg ) );
	msg.hdr = INTELVF_MSG_TYPE_RESET;
	if ( ( rc = intelvf_mbox_msg ( intel, &msg ) ) != 0 ) {
		DBGC ( intel, "INTEL %p reset failed: %s\n",
		       intel, strerror ( rc ) );
		return rc;
	}

	/* Check response */
	if ( ( msg.hdr & INTELVF_MSG_TYPE_MASK ) != INTELVF_MSG_TYPE_RESET ) {
		DBGC ( intel, "INTEL %p reset unexpected response:\n", intel );
		DBGC_HDA ( intel, 0, &msg, sizeof ( msg ) );
		return -EPROTO;
	}

	/* Fill in MAC address, if applicable */
	if ( hw_addr ) {
		if ( msg.hdr & INTELVF_MSG_ACK ) {
			memcpy ( hw_addr, msg.mac.mac, sizeof ( msg.mac.mac ) );
			DBGC ( intel, "INTEL %p reset assigned MAC address "
			       "%s\n", intel, eth_ntoa ( hw_addr ) );
		} else {
			eth_random_addr ( hw_addr );
			DBGC ( intel, "INTEL %p reset generated MAC address "
			       "%s\n", intel, eth_ntoa ( hw_addr ) );
		}
	}

	return 0;
}

/**
 * Send set MAC address message
 *
 * @v intel		Intel device
 * @v ll_addr		Link-layer address
 * @ret rc		Return status code
 */
int intelvf_mbox_set_mac ( struct intel_nic *intel, const uint8_t *ll_addr ) {
	union intelvf_msg msg;
	int rc;

	/* Send set MAC address message */
	memset ( &msg, 0, sizeof ( msg ) );
	msg.hdr = INTELVF_MSG_TYPE_SET_MAC;
	memcpy ( msg.mac.mac, ll_addr, sizeof ( msg.mac.mac ) );
	if ( ( rc = intelvf_mbox_msg ( intel, &msg ) ) != 0 ) {
		DBGC ( intel, "INTEL %p set MAC address failed: %s\n",
		       intel, strerror ( rc ) );
		return rc;
	}

	/* Check response */
	if ( ( msg.hdr & INTELVF_MSG_TYPE_MASK ) != INTELVF_MSG_TYPE_SET_MAC ) {
		DBGC ( intel, "INTEL %p set MAC address unexpected response:\n",
		       intel );
		DBGC_HDA ( intel, 0, &msg, sizeof ( msg ) );
		return -EPROTO;
	}

	/* Check that we were allowed to set the MAC address */
	if ( ! ( msg.hdr & INTELVF_MSG_ACK ) ) {
		DBGC ( intel, "INTEL %p set MAC address refused\n", intel );
		return -EPERM;
	}

	return 0;
}

/**
 * Send set MTU message
 *
 * @v intel		Intel device
 * @v mtu		Maximum packet size
 * @ret rc		Return status code
 */
int intelvf_mbox_set_mtu ( struct intel_nic *intel, size_t mtu ) {
	union intelvf_msg msg;
	int rc;

	/* Send set MTU message */
	memset ( &msg, 0, sizeof ( msg ) );
	msg.hdr = INTELVF_MSG_TYPE_SET_MTU;
	msg.mtu.mtu = mtu;
	if ( ( rc = intelvf_mbox_msg ( intel, &msg ) ) != 0 ) {
		DBGC ( intel, "INTEL %p set MTU failed: %s\n",
		       intel, strerror ( rc ) );
		return rc;
	}

	/* Check response */
	if ( ( msg.hdr & INTELVF_MSG_TYPE_MASK ) != INTELVF_MSG_TYPE_SET_MTU ) {
		DBGC ( intel, "INTEL %p set MTU unexpected response:\n",
		       intel );
		DBGC_HDA ( intel, 0, &msg, sizeof ( msg ) );
		return -EPROTO;
	}

	/* Check that we were allowed to set the MTU */
	if ( ! ( msg.hdr & INTELVF_MSG_ACK ) ) {
		DBGC ( intel, "INTEL %p set MTU refused\n", intel );
		return -EPERM;
	}

	return 0;
}