These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / interface / linux / linux_pci.c
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  * 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 <stdio.h>
27 #include <errno.h>
28 #include <byteswap.h>
29 #include <linux_api.h>
30 #include <ipxe/linux.h>
31 #include <ipxe/pci.h>
32
33 /** @file
34  *
35  * iPXE PCI API for Linux
36  *
37  */
38
39 /**
40  * Open PCI configuration space
41  *
42  * @v pci               PCI device
43  * @v flags             Access mode flags
44  * @v where             Address within configuration space
45  * @ret fd              File handle, or negative error
46  */
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 */ ];
50         int fd;
51         int rc;
52
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 ) );
57
58         /* Open file */
59         fd = linux_open ( filename, flags );
60         if ( fd < 0 ) {
61                 DBGC ( pci, "PCI could not open %s: %s\n", filename,
62                        linux_strerror ( linux_errno ) );
63                 rc = -ELINUX ( linux_errno );
64                 goto err_open;
65         }
66
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 );
72                 goto err_seek;
73         }
74
75         return fd;
76
77  err_seek:
78         linux_close ( fd );
79  err_open:
80         return rc;
81 }
82
83 /**
84  * Read from PCI configuration space
85  *
86  * @v pci               PCI device
87  * @v where             Address within configuration space
88  * @v value             Data buffer
89  * @v len               Length to read
90  * @ret rc              Return status code
91  */
92 int linux_pci_read ( struct pci_device *pci, unsigned long where,
93                      unsigned long *value, size_t len ) {
94         uint32_t tmp = 0;
95         int fd;
96         int check_len;
97         int rc;
98
99         /* Return "missing device" in case of error */
100         *value = -1UL;
101
102         /* Open configuration space */
103         fd = linux_pci_open ( pci, O_RDONLY, where );
104         if ( fd < 0 ) {
105                 rc = fd;
106                 goto err_open;
107         }
108
109         /* Read value */
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 );
116                 goto err_read;
117         }
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 ),
121                        where, len );
122                 rc = -EIO;
123                 goto err_read;
124         }
125
126         /* Return value */
127         *value = le32_to_cpu ( tmp );
128
129         /* Success */
130         rc = 0;
131
132  err_read:
133         linux_close ( fd );
134  err_open:
135         return rc;
136 }
137
138 /**
139  * Write to PCI configuration space
140  *
141  * @v pci               PCI device
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
146  */
147 int linux_pci_write ( struct pci_device *pci, unsigned long where,
148                       unsigned long value, size_t len ) {
149         uint32_t tmp;
150         int fd;
151         int check_len;
152         int rc;
153
154         /* Open configuration space */
155         fd = linux_pci_open ( pci, O_WRONLY, where );
156         if ( fd < 0 ) {
157                 rc = fd;
158                 goto err_open;
159         }
160
161         /* Prepare value for writing */
162         tmp = cpu_to_le32 ( value );
163         assert ( len <= sizeof ( tmp ) );
164
165         /* Write value */
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 );
172                 goto err_write;
173         }
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 ),
177                        where, len );
178                 rc = -EIO;
179                 goto err_write;
180         }
181
182         /* Success */
183         rc = 0;
184
185  err_write:
186         linux_close ( fd );
187  err_open:
188         return rc;
189 }