2 * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
25 #include <linux_api.h>
26 #include <ipxe/linux.h>
31 * iPXE PCI API for Linux
36 * Open PCI configuration space
39 * @v flags Access mode flags
40 * @v where Address within configuration space
41 * @ret fd File handle, or negative error
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 */ ];
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 ) );
55 fd = linux_open ( filename, flags );
57 DBGC ( pci, "PCI could not open %s: %s\n", filename,
58 linux_strerror ( linux_errno ) );
59 rc = -ELINUX ( linux_errno );
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 );
80 * Read from PCI configuration space
83 * @v where Address within configuration space
84 * @v value Data buffer
85 * @v len Length to read
86 * @ret rc Return status code
88 int linux_pci_read ( struct pci_device *pci, unsigned long where,
89 unsigned long *value, size_t len ) {
95 /* Return "missing device" in case of error */
98 /* Open configuration space */
99 fd = linux_pci_open ( pci, O_RDONLY, where );
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 );
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 ),
123 *value = le32_to_cpu ( tmp );
135 * Write to PCI configuration space
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
143 int linux_pci_write ( struct pci_device *pci, unsigned long where,
144 unsigned long value, size_t len ) {
150 /* Open configuration space */
151 fd = linux_pci_open ( pci, O_WRONLY, where );
157 /* Prepare value for writing */
158 tmp = cpu_to_le32 ( value );
159 assert ( len <= sizeof ( tmp ) );
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 );
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 ),