[infiniband] Do not use GRH for local paths
[ipxe.git] / src / interface / efi / efi_pci.c
1 /*
2 * Copyright (C) 2008 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 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 <stdlib.h>
27 #include <errno.h>
28 #include <ipxe/pci.h>
29 #include <ipxe/efi/efi.h>
30 #include <ipxe/efi/efi_pci.h>
31 #include <ipxe/efi/efi_driver.h>
32 #include <ipxe/efi/Protocol/PciIo.h>
33 #include <ipxe/efi/Protocol/PciRootBridgeIo.h>
34
35 /** @file
36 *
37 * iPXE PCI I/O API for EFI
38 *
39 */
40
41 /* Disambiguate the various error causes */
42 #define EINFO_EEFI_PCI \
43 __einfo_uniqify ( EINFO_EPLATFORM, 0x01, \
44 "Could not open PCI I/O protocol" )
45 #define EINFO_EEFI_PCI_NOT_PCI \
46 __einfo_platformify ( EINFO_EEFI_PCI, EFI_UNSUPPORTED, \
47 "Not a PCI device" )
48 #define EEFI_PCI_NOT_PCI __einfo_error ( EINFO_EEFI_PCI_NOT_PCI )
49 #define EINFO_EEFI_PCI_IN_USE \
50 __einfo_platformify ( EINFO_EEFI_PCI, EFI_ACCESS_DENIED, \
51 "PCI device already has a driver" )
52 #define EEFI_PCI_IN_USE __einfo_error ( EINFO_EEFI_PCI_IN_USE )
53 #define EEFI_PCI( efirc ) \
54 EPLATFORM ( EINFO_EEFI_PCI, efirc, \
55 EEFI_PCI_NOT_PCI, EEFI_PCI_IN_USE )
56
57 /******************************************************************************
58 *
59 * iPXE PCI API
60 *
61 ******************************************************************************
62 */
63
64 /** PCI root bridge I/O protocol */
65 static EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *efipci;
66 EFI_REQUEST_PROTOCOL ( EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL, &efipci );
67
68 static unsigned long efipci_address ( struct pci_device *pci,
69 unsigned long location ) {
70 return EFI_PCI_ADDRESS ( PCI_BUS ( pci->busdevfn ),
71 PCI_SLOT ( pci->busdevfn ),
72 PCI_FUNC ( pci->busdevfn ),
73 EFIPCI_OFFSET ( location ) );
74 }
75
76 int efipci_read ( struct pci_device *pci, unsigned long location,
77 void *value ) {
78 EFI_STATUS efirc;
79 int rc;
80
81 if ( ! efipci )
82 return -ENOTSUP;
83
84 if ( ( efirc = efipci->Pci.Read ( efipci, EFIPCI_WIDTH ( location ),
85 efipci_address ( pci, location ), 1,
86 value ) ) != 0 ) {
87 rc = -EEFI ( efirc );
88 DBG ( "EFIPCI config read from " PCI_FMT " offset %02lx "
89 "failed: %s\n", PCI_ARGS ( pci ),
90 EFIPCI_OFFSET ( location ), strerror ( rc ) );
91 return -EIO;
92 }
93
94 return 0;
95 }
96
97 int efipci_write ( struct pci_device *pci, unsigned long location,
98 unsigned long value ) {
99 EFI_STATUS efirc;
100 int rc;
101
102 if ( ! efipci )
103 return -ENOTSUP;
104
105 if ( ( efirc = efipci->Pci.Write ( efipci, EFIPCI_WIDTH ( location ),
106 efipci_address ( pci, location ), 1,
107 &value ) ) != 0 ) {
108 rc = -EEFI ( efirc );
109 DBG ( "EFIPCI config write to " PCI_FMT " offset %02lx "
110 "failed: %s\n", PCI_ARGS ( pci ),
111 EFIPCI_OFFSET ( location ), strerror ( rc ) );
112 return -EIO;
113 }
114
115 return 0;
116 }
117
118 PROVIDE_PCIAPI_INLINE ( efi, pci_num_bus );
119 PROVIDE_PCIAPI_INLINE ( efi, pci_read_config_byte );
120 PROVIDE_PCIAPI_INLINE ( efi, pci_read_config_word );
121 PROVIDE_PCIAPI_INLINE ( efi, pci_read_config_dword );
122 PROVIDE_PCIAPI_INLINE ( efi, pci_write_config_byte );
123 PROVIDE_PCIAPI_INLINE ( efi, pci_write_config_word );
124 PROVIDE_PCIAPI_INLINE ( efi, pci_write_config_dword );
125
126 /******************************************************************************
127 *
128 * EFI PCI device instantiation
129 *
130 ******************************************************************************
131 */
132
133 /**
134 * Open EFI PCI device
135 *
136 * @v device EFI device handle
137 * @v attributes Protocol opening attributes
138 * @v pci PCI device to fill in
139 * @ret rc Return status code
140 */
141 int efipci_open ( EFI_HANDLE device, UINT32 attributes,
142 struct pci_device *pci ) {
143 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
144 union {
145 EFI_PCI_IO_PROTOCOL *pci_io;
146 void *interface;
147 } pci_io;
148 UINTN pci_segment, pci_bus, pci_dev, pci_fn;
149 EFI_STATUS efirc;
150 int rc;
151
152 /* See if device is a PCI device */
153 if ( ( efirc = bs->OpenProtocol ( device, &efi_pci_io_protocol_guid,
154 &pci_io.interface, efi_image_handle,
155 device, attributes ) ) != 0 ) {
156 rc = -EEFI_PCI ( efirc );
157 DBGCP ( device, "EFIPCI %s cannot open PCI protocols: %s\n",
158 efi_handle_name ( device ), strerror ( rc ) );
159 goto err_open_protocol;
160 }
161
162 /* Get PCI bus:dev.fn address */
163 if ( ( efirc = pci_io.pci_io->GetLocation ( pci_io.pci_io, &pci_segment,
164 &pci_bus, &pci_dev,
165 &pci_fn ) ) != 0 ) {
166 rc = -EEFI ( efirc );
167 DBGC ( device, "EFIPCI %s could not get PCI location: %s\n",
168 efi_handle_name ( device ), strerror ( rc ) );
169 goto err_get_location;
170 }
171 DBGC2 ( device, "EFIPCI %s is PCI %04lx:%02lx:%02lx.%lx\n",
172 efi_handle_name ( device ), ( ( unsigned long ) pci_segment ),
173 ( ( unsigned long ) pci_bus ), ( ( unsigned long ) pci_dev ),
174 ( ( unsigned long ) pci_fn ) );
175
176 /* Try to enable I/O cycles, memory cycles, and bus mastering.
177 * Some platforms will 'helpfully' report errors if these bits
178 * can't be enabled (for example, if the card doesn't actually
179 * support I/O cycles). Work around any such platforms by
180 * enabling bits individually and simply ignoring any errors.
181 */
182 pci_io.pci_io->Attributes ( pci_io.pci_io,
183 EfiPciIoAttributeOperationEnable,
184 EFI_PCI_IO_ATTRIBUTE_IO, NULL );
185 pci_io.pci_io->Attributes ( pci_io.pci_io,
186 EfiPciIoAttributeOperationEnable,
187 EFI_PCI_IO_ATTRIBUTE_MEMORY, NULL );
188 pci_io.pci_io->Attributes ( pci_io.pci_io,
189 EfiPciIoAttributeOperationEnable,
190 EFI_PCI_IO_ATTRIBUTE_BUS_MASTER, NULL );
191
192 /* Populate PCI device */
193 pci_init ( pci, PCI_BUSDEVFN ( pci_bus, pci_dev, pci_fn ) );
194 if ( ( rc = pci_read_config ( pci ) ) != 0 ) {
195 DBGC ( device, "EFIPCI %s cannot read PCI configuration: %s\n",
196 efi_handle_name ( device ), strerror ( rc ) );
197 goto err_pci_read_config;
198 }
199
200 return 0;
201
202 err_pci_read_config:
203 err_get_location:
204 bs->CloseProtocol ( device, &efi_pci_io_protocol_guid,
205 efi_image_handle, device );
206 err_open_protocol:
207 return rc;
208 }
209
210 /**
211 * Close EFI PCI device
212 *
213 * @v device EFI device handle
214 */
215 void efipci_close ( EFI_HANDLE device ) {
216 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
217
218 bs->CloseProtocol ( device, &efi_pci_io_protocol_guid,
219 efi_image_handle, device );
220 }
221
222 /**
223 * Get EFI PCI device information
224 *
225 * @v device EFI device handle
226 * @v pci PCI device to fill in
227 * @ret rc Return status code
228 */
229 int efipci_info ( EFI_HANDLE device, struct pci_device *pci ) {
230 int rc;
231
232 /* Open PCI device, if possible */
233 if ( ( rc = efipci_open ( device, EFI_OPEN_PROTOCOL_GET_PROTOCOL,
234 pci ) ) != 0 )
235 return rc;
236
237 /* Close PCI device */
238 efipci_close ( device );
239
240 return 0;
241 }
242
243 /******************************************************************************
244 *
245 * EFI PCI driver
246 *
247 ******************************************************************************
248 */
249
250 /**
251 * Check to see if driver supports a device
252 *
253 * @v device EFI device handle
254 * @ret rc Return status code
255 */
256 static int efipci_supported ( EFI_HANDLE device ) {
257 struct pci_device pci;
258 int rc;
259
260 /* Get PCI device information */
261 if ( ( rc = efipci_info ( device, &pci ) ) != 0 )
262 return rc;
263
264 /* Look for a driver */
265 if ( ( rc = pci_find_driver ( &pci ) ) != 0 ) {
266 DBGCP ( device, "EFIPCI %s has no driver\n",
267 efi_handle_name ( device ) );
268 return rc;
269 }
270 DBGC ( device, "EFIPCI %s has driver \"%s\"\n",
271 efi_handle_name ( device ), pci.id->name );
272
273 return 0;
274 }
275
276 /**
277 * Attach driver to device
278 *
279 * @v efidev EFI device
280 * @ret rc Return status code
281 */
282 static int efipci_start ( struct efi_device *efidev ) {
283 EFI_HANDLE device = efidev->device;
284 struct pci_device *pci;
285 int rc;
286
287 /* Allocate PCI device */
288 pci = zalloc ( sizeof ( *pci ) );
289 if ( ! pci ) {
290 rc = -ENOMEM;
291 goto err_alloc;
292 }
293
294 /* Open PCI device */
295 if ( ( rc = efipci_open ( device, ( EFI_OPEN_PROTOCOL_BY_DRIVER |
296 EFI_OPEN_PROTOCOL_EXCLUSIVE ),
297 pci ) ) != 0 ) {
298 DBGC ( device, "EFIPCI %s could not open PCI device: %s\n",
299 efi_handle_name ( device ), strerror ( rc ) );
300 DBGC_EFI_OPENERS ( device, device, &efi_pci_io_protocol_guid );
301 goto err_open;
302 }
303
304 /* Find driver */
305 if ( ( rc = pci_find_driver ( pci ) ) != 0 ) {
306 DBGC ( device, "EFIPCI %s has no driver\n",
307 efi_handle_name ( device ) );
308 goto err_find_driver;
309 }
310
311 /* Mark PCI device as a child of the EFI device */
312 pci->dev.parent = &efidev->dev;
313 list_add ( &pci->dev.siblings, &efidev->dev.children );
314
315 /* Probe driver */
316 if ( ( rc = pci_probe ( pci ) ) != 0 ) {
317 DBGC ( device, "EFIPCI %s could not probe driver \"%s\": %s\n",
318 efi_handle_name ( device ), pci->id->name,
319 strerror ( rc ) );
320 goto err_probe;
321 }
322 DBGC ( device, "EFIPCI %s using driver \"%s\"\n",
323 efi_handle_name ( device ), pci->id->name );
324
325 efidev_set_drvdata ( efidev, pci );
326 return 0;
327
328 pci_remove ( pci );
329 err_probe:
330 list_del ( &pci->dev.siblings );
331 err_find_driver:
332 efipci_close ( device );
333 err_open:
334 free ( pci );
335 err_alloc:
336 return rc;
337 }
338
339 /**
340 * Detach driver from device
341 *
342 * @v efidev EFI device
343 */
344 static void efipci_stop ( struct efi_device *efidev ) {
345 struct pci_device *pci = efidev_get_drvdata ( efidev );
346 EFI_HANDLE device = efidev->device;
347
348 pci_remove ( pci );
349 list_del ( &pci->dev.siblings );
350 efipci_close ( device );
351 free ( pci );
352 }
353
354 /** EFI PCI driver */
355 struct efi_driver efipci_driver __efi_driver ( EFI_DRIVER_NORMAL ) = {
356 .name = "PCI",
357 .supported = efipci_supported,
358 .start = efipci_start,
359 .stop = efipci_stop,
360 };