|
@@ -0,0 +1,185 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (C) 2013 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
|
+
|
|
20
|
+FILE_LICENCE ( GPL2_OR_LATER );
|
|
21
|
+
|
|
22
|
+#include <stdio.h>
|
|
23
|
+#include <errno.h>
|
|
24
|
+#include <byteswap.h>
|
|
25
|
+#include <linux_api.h>
|
|
26
|
+#include <ipxe/linux.h>
|
|
27
|
+#include <ipxe/pci.h>
|
|
28
|
+
|
|
29
|
+/** @file
|
|
30
|
+ *
|
|
31
|
+ * iPXE PCI API for Linux
|
|
32
|
+ *
|
|
33
|
+ */
|
|
34
|
+
|
|
35
|
+/**
|
|
36
|
+ * Open PCI configuration space
|
|
37
|
+ *
|
|
38
|
+ * @v pci PCI device
|
|
39
|
+ * @v flags Access mode flags
|
|
40
|
+ * @v where Address within configuration space
|
|
41
|
+ * @ret fd File handle, or negative error
|
|
42
|
+ */
|
|
43
|
+static int linux_pci_open ( struct pci_device *pci, int flags,
|
|
44
|
+ unsigned long where ) {
|
|
45
|
+ char filename[ 22 /* "/proc/bus/pci/xx/xx.x" + NUL */ ];
|
|
46
|
+ int fd;
|
|
47
|
+ int rc;
|
|
48
|
+
|
|
49
|
+ /* Construct filename */
|
|
50
|
+ snprintf ( filename, sizeof ( filename ), "/proc/bus/pci/%02x/%02x.%x",
|
|
51
|
+ PCI_BUS ( pci->busdevfn ), PCI_SLOT ( pci->busdevfn ),
|
|
52
|
+ PCI_FUNC ( pci->busdevfn ) );
|
|
53
|
+
|
|
54
|
+ /* Open file */
|
|
55
|
+ fd = linux_open ( filename, flags );
|
|
56
|
+ if ( fd < 0 ) {
|
|
57
|
+ DBGC ( pci, "PCI could not open %s: %s\n", filename,
|
|
58
|
+ linux_strerror ( linux_errno ) );
|
|
59
|
+ rc = -ELINUX ( linux_errno );
|
|
60
|
+ goto err_open;
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ /* Seek to location */
|
|
64
|
+ if ( linux_lseek ( fd, where, SEEK_SET ) < 0 ) {
|
|
65
|
+ DBGC ( pci, "PCI could not seek to %s offset %#02lx: %s\n",
|
|
66
|
+ filename, where, linux_strerror ( linux_errno ) );
|
|
67
|
+ rc = -ELINUX ( linux_errno );
|
|
68
|
+ goto err_seek;
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+ return fd;
|
|
72
|
+
|
|
73
|
+ err_seek:
|
|
74
|
+ linux_close ( fd );
|
|
75
|
+ err_open:
|
|
76
|
+ return rc;
|
|
77
|
+}
|
|
78
|
+
|
|
79
|
+/**
|
|
80
|
+ * Read from PCI configuration space
|
|
81
|
+ *
|
|
82
|
+ * @v pci PCI device
|
|
83
|
+ * @v where Address within configuration space
|
|
84
|
+ * @v value Data buffer
|
|
85
|
+ * @v len Length to read
|
|
86
|
+ * @ret rc Return status code
|
|
87
|
+ */
|
|
88
|
+int linux_pci_read ( struct pci_device *pci, unsigned long where,
|
|
89
|
+ unsigned long *value, size_t len ) {
|
|
90
|
+ uint32_t tmp = 0;
|
|
91
|
+ int fd;
|
|
92
|
+ int check_len;
|
|
93
|
+ int rc;
|
|
94
|
+
|
|
95
|
+ /* Return "missing device" in case of error */
|
|
96
|
+ *value = -1UL;
|
|
97
|
+
|
|
98
|
+ /* Open configuration space */
|
|
99
|
+ fd = linux_pci_open ( pci, O_RDONLY, where );
|
|
100
|
+ if ( fd < 0 ) {
|
|
101
|
+ rc = fd;
|
|
102
|
+ goto err_open;
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+ /* Read value */
|
|
106
|
+ check_len = linux_read ( fd, &tmp, len );
|
|
107
|
+ if ( check_len < 0 ) {
|
|
108
|
+ DBGC ( pci, "PCI could not read from " PCI_FMT " %#02lx+%#zx: "
|
|
109
|
+ "%s\n", PCI_ARGS ( pci ), where, len,
|
|
110
|
+ linux_strerror ( linux_errno ) );
|
|
111
|
+ rc = -ELINUX ( linux_errno );
|
|
112
|
+ goto err_read;
|
|
113
|
+ }
|
|
114
|
+ if ( ( size_t ) check_len != len ) {
|
|
115
|
+ DBGC ( pci, "PCI read only %#x bytes from " PCI_FMT
|
|
116
|
+ " %#02lx+%#zx\n", check_len, PCI_ARGS ( pci ),
|
|
117
|
+ where, len );
|
|
118
|
+ rc = -EIO;
|
|
119
|
+ goto err_read;
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ /* Return value */
|
|
123
|
+ *value = le32_to_cpu ( tmp );
|
|
124
|
+
|
|
125
|
+ /* Success */
|
|
126
|
+ rc = 0;
|
|
127
|
+
|
|
128
|
+ err_read:
|
|
129
|
+ linux_close ( fd );
|
|
130
|
+ err_open:
|
|
131
|
+ return rc;
|
|
132
|
+}
|
|
133
|
+
|
|
134
|
+/**
|
|
135
|
+ * Write to PCI configuration space
|
|
136
|
+ *
|
|
137
|
+ * @v pci PCI device
|
|
138
|
+ * @v where Address within configuration space
|
|
139
|
+ * @v value Value to write
|
|
140
|
+ * @v len Length of value
|
|
141
|
+ * @ret rc Return status code
|
|
142
|
+ */
|
|
143
|
+int linux_pci_write ( struct pci_device *pci, unsigned long where,
|
|
144
|
+ unsigned long value, size_t len ) {
|
|
145
|
+ uint32_t tmp;
|
|
146
|
+ int fd;
|
|
147
|
+ int check_len;
|
|
148
|
+ int rc;
|
|
149
|
+
|
|
150
|
+ /* Open configuration space */
|
|
151
|
+ fd = linux_pci_open ( pci, O_WRONLY, where );
|
|
152
|
+ if ( fd < 0 ) {
|
|
153
|
+ rc = fd;
|
|
154
|
+ goto err_open;
|
|
155
|
+ }
|
|
156
|
+
|
|
157
|
+ /* Prepare value for writing */
|
|
158
|
+ tmp = cpu_to_le32 ( value );
|
|
159
|
+ assert ( len <= sizeof ( tmp ) );
|
|
160
|
+
|
|
161
|
+ /* Write value */
|
|
162
|
+ check_len = linux_write ( fd, &tmp, len );
|
|
163
|
+ if ( check_len < 0 ) {
|
|
164
|
+ DBGC ( pci, "PCI could not write to " PCI_FMT " %#02lx+%#zx: "
|
|
165
|
+ "%s\n", PCI_ARGS ( pci ), where, len,
|
|
166
|
+ linux_strerror ( linux_errno ) );
|
|
167
|
+ rc = -ELINUX ( linux_errno );
|
|
168
|
+ goto err_write;
|
|
169
|
+ }
|
|
170
|
+ if ( ( size_t ) check_len != len ) {
|
|
171
|
+ DBGC ( pci, "PCI wrote only %#x bytes to " PCI_FMT
|
|
172
|
+ " %#02lx+%#zx\n", check_len, PCI_ARGS ( pci ),
|
|
173
|
+ where, len );
|
|
174
|
+ rc = -EIO;
|
|
175
|
+ goto err_write;
|
|
176
|
+ }
|
|
177
|
+
|
|
178
|
+ /* Success */
|
|
179
|
+ rc = 0;
|
|
180
|
+
|
|
181
|
+ err_write:
|
|
182
|
+ linux_close ( fd );
|
|
183
|
+ err_open:
|
|
184
|
+ return rc;
|
|
185
|
+}
|