|
@@ -0,0 +1,303 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
|
|
3
|
+ *
|
|
4
|
+ * This program is free software; you can redistribute it and/or
|
|
5
|
+ * modify it under the terms of the GNU General Public License as
|
|
6
|
+ * published by the Free Software Foundation; either version 2 of the
|
|
7
|
+ * License, or (at your option) any later version.
|
|
8
|
+ *
|
|
9
|
+ * This program is distributed in the hope that it will be useful, but
|
|
10
|
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
12
|
+ * General Public License for more details.
|
|
13
|
+ *
|
|
14
|
+ * You should have received a copy of the GNU General Public License
|
|
15
|
+ * along with this program; if not, write to the Free Software
|
|
16
|
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
17
|
+ * 02110-1301, USA.
|
|
18
|
+ *
|
|
19
|
+ * You can also choose to distribute this program under the terms of
|
|
20
|
+ * the Unmodified Binary Distribution Licence (as given in the file
|
|
21
|
+ * COPYING.UBDL), provided that you have satisfied its requirements.
|
|
22
|
+ */
|
|
23
|
+
|
|
24
|
+FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
|
|
25
|
+
|
|
26
|
+#include <string.h>
|
|
27
|
+#include <unistd.h>
|
|
28
|
+#include <errno.h>
|
|
29
|
+#include <ipxe/io.h>
|
|
30
|
+#include <ipxe/netdevice.h>
|
|
31
|
+#include <ipxe/ethernet.h>
|
|
32
|
+#include "intelvf.h"
|
|
33
|
+
|
|
34
|
+/** @file
|
|
35
|
+ *
|
|
36
|
+ * Intel 10/100/1000 virtual function network card driver
|
|
37
|
+ *
|
|
38
|
+ */
|
|
39
|
+
|
|
40
|
+/******************************************************************************
|
|
41
|
+ *
|
|
42
|
+ * Mailbox messages
|
|
43
|
+ *
|
|
44
|
+ ******************************************************************************
|
|
45
|
+ */
|
|
46
|
+
|
|
47
|
+/**
|
|
48
|
+ * Write message to mailbox
|
|
49
|
+ *
|
|
50
|
+ * @v intel Intel device
|
|
51
|
+ * @v msg Message
|
|
52
|
+ */
|
|
53
|
+static void intelvf_mbox_write ( struct intel_nic *intel,
|
|
54
|
+ const union intelvf_msg *msg ) {
|
|
55
|
+ unsigned int i;
|
|
56
|
+
|
|
57
|
+ /* Write message */
|
|
58
|
+ DBGC2 ( intel, "INTEL %p sending message", intel );
|
|
59
|
+ for ( i = 0 ; i < ( sizeof ( *msg ) / sizeof ( msg->dword[0] ) ) ; i++){
|
|
60
|
+ DBGC2 ( intel, "%c%08x", ( i ? ':' : ' ' ), msg->dword[i] );
|
|
61
|
+ writel ( msg->dword[i], ( intel->regs + intel->mbox.mem +
|
|
62
|
+ ( i * sizeof ( msg->dword[0] ) ) ) );
|
|
63
|
+ }
|
|
64
|
+ DBGC2 ( intel, "\n" );
|
|
65
|
+}
|
|
66
|
+
|
|
67
|
+/**
|
|
68
|
+ * Read message from mailbox
|
|
69
|
+ *
|
|
70
|
+ * @v intel Intel device
|
|
71
|
+ * @v msg Message
|
|
72
|
+ */
|
|
73
|
+static void intelvf_mbox_read ( struct intel_nic *intel,
|
|
74
|
+ union intelvf_msg *msg ) {
|
|
75
|
+ unsigned int i;
|
|
76
|
+
|
|
77
|
+ /* Read message */
|
|
78
|
+ DBGC2 ( intel, "INTEL %p received message", intel );
|
|
79
|
+ for ( i = 0 ; i < ( sizeof ( *msg ) / sizeof ( msg->dword[0] ) ) ; i++){
|
|
80
|
+ msg->dword[i] = readl ( intel->regs + intel->mbox.mem +
|
|
81
|
+ ( i * sizeof ( msg->dword[0] ) ) );
|
|
82
|
+ DBGC2 ( intel, "%c%08x", ( i ? ':' : ' ' ), msg->dword[i] );
|
|
83
|
+ }
|
|
84
|
+ DBGC2 ( intel, "\n" );
|
|
85
|
+}
|
|
86
|
+
|
|
87
|
+/**
|
|
88
|
+ * Poll mailbox
|
|
89
|
+ *
|
|
90
|
+ * @v intel Intel device
|
|
91
|
+ * @ret rc Return status code
|
|
92
|
+ *
|
|
93
|
+ * Note that polling the mailbox may fail if the underlying PF is
|
|
94
|
+ * reset.
|
|
95
|
+ */
|
|
96
|
+int intelvf_mbox_poll ( struct intel_nic *intel ) {
|
|
97
|
+ struct intel_mailbox *mbox = &intel->mbox;
|
|
98
|
+ union intelvf_msg msg;
|
|
99
|
+ uint32_t ctrl;
|
|
100
|
+
|
|
101
|
+ /* Get mailbox status */
|
|
102
|
+ ctrl = readl ( intel->regs + mbox->ctrl );
|
|
103
|
+
|
|
104
|
+ /* Fail if a reset is in progress */
|
|
105
|
+ if ( ctrl & INTELVF_MBCTRL_RSTI )
|
|
106
|
+ return -EPIPE;
|
|
107
|
+
|
|
108
|
+ /* Acknowledge (and ignore) any received messages */
|
|
109
|
+ if ( ctrl & INTELVF_MBCTRL_PFSTS ) {
|
|
110
|
+ intelvf_mbox_read ( intel, &msg );
|
|
111
|
+ writel ( INTELVF_MBCTRL_ACK, intel->regs + mbox->ctrl );
|
|
112
|
+ }
|
|
113
|
+
|
|
114
|
+ return 0;
|
|
115
|
+}
|
|
116
|
+
|
|
117
|
+/**
|
|
118
|
+ * Wait for PF reset to complete
|
|
119
|
+ *
|
|
120
|
+ * @v intel Intel device
|
|
121
|
+ * @ret rc Return status code
|
|
122
|
+ */
|
|
123
|
+int intelvf_mbox_wait ( struct intel_nic *intel ) {
|
|
124
|
+ unsigned int i;
|
|
125
|
+ int rc;
|
|
126
|
+
|
|
127
|
+ /* Wait until a poll completes successfully */
|
|
128
|
+ for ( i = 0 ; i < INTELVF_MBOX_MAX_WAIT_MS ; i++ ) {
|
|
129
|
+
|
|
130
|
+ /* Check for successful poll */
|
|
131
|
+ if ( ( rc = intelvf_mbox_poll ( intel ) ) == 0 )
|
|
132
|
+ return 0;
|
|
133
|
+
|
|
134
|
+ /* Delay */
|
|
135
|
+ mdelay ( 1 );
|
|
136
|
+ }
|
|
137
|
+
|
|
138
|
+ DBGC ( intel, "INTEL %p timed out waiting for reset\n", intel );
|
|
139
|
+ return -ETIMEDOUT;
|
|
140
|
+}
|
|
141
|
+
|
|
142
|
+/**
|
|
143
|
+ * Send/receive mailbox message
|
|
144
|
+ *
|
|
145
|
+ * @v intel Intel device
|
|
146
|
+ * @v msg Message buffer
|
|
147
|
+ * @ret rc Return status code
|
|
148
|
+ */
|
|
149
|
+static int intelvf_mbox_msg ( struct intel_nic *intel,
|
|
150
|
+ union intelvf_msg *msg ) {
|
|
151
|
+ struct intel_mailbox *mbox = &intel->mbox;
|
|
152
|
+ uint32_t ctrl;
|
|
153
|
+ uint32_t seen = 0;
|
|
154
|
+ unsigned int i;
|
|
155
|
+
|
|
156
|
+ /* Sanity check */
|
|
157
|
+ assert ( ! ( msg->hdr & INTELVF_MSG_RESPONSE ) );
|
|
158
|
+
|
|
159
|
+ /* Handle mailbox */
|
|
160
|
+ for ( i = 0 ; i < INTELVF_MBOX_MAX_WAIT_MS ; i++ ) {
|
|
161
|
+
|
|
162
|
+ /* Attempt to claim mailbox, if we have not yet sent
|
|
163
|
+ * our message.
|
|
164
|
+ */
|
|
165
|
+ if ( ! ( seen & INTELVF_MBCTRL_VFU ) )
|
|
166
|
+ writel ( INTELVF_MBCTRL_VFU, intel->regs + mbox->ctrl );
|
|
167
|
+
|
|
168
|
+ /* Get mailbox status and record observed flags */
|
|
169
|
+ ctrl = readl ( intel->regs + mbox->ctrl );
|
|
170
|
+ seen |= ctrl;
|
|
171
|
+
|
|
172
|
+ /* If a reset is in progress, clear VFU and abort */
|
|
173
|
+ if ( ctrl & INTELVF_MBCTRL_RSTI ) {
|
|
174
|
+ writel ( 0, intel->regs + mbox->ctrl );
|
|
175
|
+ return -EPIPE;
|
|
176
|
+ }
|
|
177
|
+
|
|
178
|
+ /* Write message to mailbox, if applicable. This
|
|
179
|
+ * potentially overwrites a message sent by the PF (if
|
|
180
|
+ * the PF has simultaneously released PFU (thus
|
|
181
|
+ * allowing our VFU) and asserted PFSTS), but that
|
|
182
|
+ * doesn't really matter since there are no
|
|
183
|
+ * unsolicited PF->VF messages that require the actual
|
|
184
|
+ * message content to be observed.
|
|
185
|
+ */
|
|
186
|
+ if ( ctrl & INTELVF_MBCTRL_VFU )
|
|
187
|
+ intelvf_mbox_write ( intel, msg );
|
|
188
|
+
|
|
189
|
+ /* Read message from mailbox, if applicable. */
|
|
190
|
+ if ( ( seen & INTELVF_MBCTRL_VFU ) &&
|
|
191
|
+ ( seen & INTELVF_MBCTRL_PFACK ) &&
|
|
192
|
+ ( ctrl & INTELVF_MBCTRL_PFSTS ) )
|
|
193
|
+ intelvf_mbox_read ( intel, msg );
|
|
194
|
+
|
|
195
|
+ /* Acknowledge received message (if applicable),
|
|
196
|
+ * release VFU lock, and send message (if applicable).
|
|
197
|
+ */
|
|
198
|
+ ctrl = ( ( ( ctrl & INTELVF_MBCTRL_PFSTS ) ?
|
|
199
|
+ INTELVF_MBCTRL_ACK : 0 ) |
|
|
200
|
+ ( ( ctrl & INTELVF_MBCTRL_VFU ) ?
|
|
201
|
+ INTELVF_MBCTRL_REQ : 0 ) );
|
|
202
|
+ writel ( ctrl, intel->regs + mbox->ctrl );
|
|
203
|
+
|
|
204
|
+ /* Exit successfully if we have received a response */
|
|
205
|
+ if ( msg->hdr & INTELVF_MSG_RESPONSE ) {
|
|
206
|
+
|
|
207
|
+ /* Sanity check */
|
|
208
|
+ assert ( seen & INTELVF_MBCTRL_VFU );
|
|
209
|
+ assert ( seen & INTELVF_MBCTRL_PFACK );
|
|
210
|
+ assert ( seen & INTELVF_MBCTRL_PFSTS );
|
|
211
|
+
|
|
212
|
+ return 0;
|
|
213
|
+ }
|
|
214
|
+
|
|
215
|
+ /* Delay */
|
|
216
|
+ mdelay ( 1 );
|
|
217
|
+ }
|
|
218
|
+
|
|
219
|
+ DBGC ( intel, "INTEL %p timed out waiting for mailbox (seen %08x)\n",
|
|
220
|
+ intel, seen );
|
|
221
|
+ return -ETIMEDOUT;
|
|
222
|
+}
|
|
223
|
+
|
|
224
|
+/**
|
|
225
|
+ * Send reset message and get initial MAC address
|
|
226
|
+ *
|
|
227
|
+ * @v intel Intel device
|
|
228
|
+ * @v hw_addr Hardware address to fill in, or NULL
|
|
229
|
+ * @ret rc Return status code
|
|
230
|
+ */
|
|
231
|
+int intelvf_mbox_reset ( struct intel_nic *intel, uint8_t *hw_addr ) {
|
|
232
|
+ union intelvf_msg msg;
|
|
233
|
+ int rc;
|
|
234
|
+
|
|
235
|
+ /* Send reset message */
|
|
236
|
+ memset ( &msg, 0, sizeof ( msg ) );
|
|
237
|
+ msg.hdr = INTELVF_MSG_TYPE_RESET;
|
|
238
|
+ if ( ( rc = intelvf_mbox_msg ( intel, &msg ) ) != 0 ) {
|
|
239
|
+ DBGC ( intel, "INTEL %p reset failed: %s\n",
|
|
240
|
+ intel, strerror ( rc ) );
|
|
241
|
+ return rc;
|
|
242
|
+ }
|
|
243
|
+
|
|
244
|
+ /* Check response */
|
|
245
|
+ if ( ( msg.hdr & INTELVF_MSG_TYPE_MASK ) != INTELVF_MSG_TYPE_RESET ) {
|
|
246
|
+ DBGC ( intel, "INTEL %p reset unexpected response:\n", intel );
|
|
247
|
+ DBGC_HDA ( intel, 0, &msg, sizeof ( msg ) );
|
|
248
|
+ return -EPROTO;
|
|
249
|
+ }
|
|
250
|
+
|
|
251
|
+ /* Fill in MAC address, if applicable */
|
|
252
|
+ if ( hw_addr ) {
|
|
253
|
+ if ( msg.hdr & INTELVF_MSG_ACK ) {
|
|
254
|
+ memcpy ( hw_addr, msg.mac.mac, sizeof ( msg.mac.mac ) );
|
|
255
|
+ DBGC ( intel, "INTEL %p reset assigned MAC address "
|
|
256
|
+ "%s\n", intel, eth_ntoa ( hw_addr ) );
|
|
257
|
+ } else {
|
|
258
|
+ eth_random_addr ( hw_addr );
|
|
259
|
+ DBGC ( intel, "INTEL %p reset generated MAC address "
|
|
260
|
+ "%s\n", intel, eth_ntoa ( hw_addr ) );
|
|
261
|
+ }
|
|
262
|
+ }
|
|
263
|
+
|
|
264
|
+ return 0;
|
|
265
|
+}
|
|
266
|
+
|
|
267
|
+/**
|
|
268
|
+ * Send set MAC address message
|
|
269
|
+ *
|
|
270
|
+ * @v intel Intel device
|
|
271
|
+ * @v ll_addr Link-layer address
|
|
272
|
+ * @ret rc Return status code
|
|
273
|
+ */
|
|
274
|
+int intelvf_mbox_set_mac ( struct intel_nic *intel, const uint8_t *ll_addr ) {
|
|
275
|
+ union intelvf_msg msg;
|
|
276
|
+ int rc;
|
|
277
|
+
|
|
278
|
+ /* Send set MAC address message */
|
|
279
|
+ memset ( &msg, 0, sizeof ( msg ) );
|
|
280
|
+ msg.hdr = INTELVF_MSG_TYPE_SET_MAC;
|
|
281
|
+ memcpy ( msg.mac.mac, ll_addr, sizeof ( msg.mac.mac ) );
|
|
282
|
+ if ( ( rc = intelvf_mbox_msg ( intel, &msg ) ) != 0 ) {
|
|
283
|
+ DBGC ( intel, "INTEL %p set MAC address failed: %s\n",
|
|
284
|
+ intel, strerror ( rc ) );
|
|
285
|
+ return rc;
|
|
286
|
+ }
|
|
287
|
+
|
|
288
|
+ /* Check response */
|
|
289
|
+ if ( ( msg.hdr & INTELVF_MSG_TYPE_MASK ) != INTELVF_MSG_TYPE_SET_MAC ) {
|
|
290
|
+ DBGC ( intel, "INTEL %p set MAC address unexpected response:\n",
|
|
291
|
+ intel );
|
|
292
|
+ DBGC_HDA ( intel, 0, &msg, sizeof ( msg ) );
|
|
293
|
+ return -EPROTO;
|
|
294
|
+ }
|
|
295
|
+
|
|
296
|
+ /* Check that we were allowed to set the MAC address */
|
|
297
|
+ if ( ! ( msg.hdr & INTELVF_MSG_ACK ) ) {
|
|
298
|
+ DBGC ( intel, "INTEL %p set MAC address refused\n", intel );
|
|
299
|
+ return -EPERM;
|
|
300
|
+ }
|
|
301
|
+
|
|
302
|
+ return 0;
|
|
303
|
+}
|