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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
29 #include <linux_api.h>
30 #include <ipxe/linux.h>
35 * iPXE PCI API for Linux
40 * Open PCI configuration space
43 * @v flags Access mode flags
44 * @v where Address within configuration space
45 * @ret fd File handle, or negative error
47 static int linux_pci_open ( struct pci_device *pci, int flags,
48 unsigned long where ) {
49 char filename[ 22 /* "/proc/bus/pci/xx/xx.x" + NUL */ ];
53 /* Construct filename */
54 snprintf ( filename, sizeof ( filename ), "/proc/bus/pci/%02x/%02x.%x",
55 PCI_BUS ( pci->busdevfn ), PCI_SLOT ( pci->busdevfn ),
56 PCI_FUNC ( pci->busdevfn ) );
59 fd = linux_open ( filename, flags );
61 DBGC ( pci, "PCI could not open %s: %s\n", filename,
62 linux_strerror ( linux_errno ) );
63 rc = -ELINUX ( linux_errno );
67 /* Seek to location */
68 if ( linux_lseek ( fd, where, SEEK_SET ) < 0 ) {
69 DBGC ( pci, "PCI could not seek to %s offset %#02lx: %s\n",
70 filename, where, linux_strerror ( linux_errno ) );
71 rc = -ELINUX ( linux_errno );
84 * Read from PCI configuration space
87 * @v where Address within configuration space
88 * @v value Data buffer
89 * @v len Length to read
90 * @ret rc Return status code
92 int linux_pci_read ( struct pci_device *pci, unsigned long where,
93 unsigned long *value, size_t len ) {
99 /* Return "missing device" in case of error */
102 /* Open configuration space */
103 fd = linux_pci_open ( pci, O_RDONLY, where );
110 check_len = linux_read ( fd, &tmp, len );
111 if ( check_len < 0 ) {
112 DBGC ( pci, "PCI could not read from " PCI_FMT " %#02lx+%#zx: "
113 "%s\n", PCI_ARGS ( pci ), where, len,
114 linux_strerror ( linux_errno ) );
115 rc = -ELINUX ( linux_errno );
118 if ( ( size_t ) check_len != len ) {
119 DBGC ( pci, "PCI read only %#x bytes from " PCI_FMT
120 " %#02lx+%#zx\n", check_len, PCI_ARGS ( pci ),
127 *value = le32_to_cpu ( tmp );
139 * Write to PCI configuration space
142 * @v where Address within configuration space
143 * @v value Value to write
144 * @v len Length of value
145 * @ret rc Return status code
147 int linux_pci_write ( struct pci_device *pci, unsigned long where,
148 unsigned long value, size_t len ) {
154 /* Open configuration space */
155 fd = linux_pci_open ( pci, O_WRONLY, where );
161 /* Prepare value for writing */
162 tmp = cpu_to_le32 ( value );
163 assert ( len <= sizeof ( tmp ) );
166 check_len = linux_write ( fd, &tmp, len );
167 if ( check_len < 0 ) {
168 DBGC ( pci, "PCI could not write to " PCI_FMT " %#02lx+%#zx: "
169 "%s\n", PCI_ARGS ( pci ), where, len,
170 linux_strerror ( linux_errno ) );
171 rc = -ELINUX ( linux_errno );
174 if ( ( size_t ) check_len != len ) {
175 DBGC ( pci, "PCI wrote only %#x bytes to " PCI_FMT
176 " %#02lx+%#zx\n", check_len, PCI_ARGS ( pci ),