xref: /libpciaccess/src/linux_sysfs.c (revision c65aa763)
1 /*
2  * (C) Copyright IBM Corporation 2006
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * on the rights to use, copy, modify, merge, publish, distribute, sub
9  * license, and/or sell copies of the Software, and to permit persons to whom
10  * the Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
19  * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 /**
26  * \file linux_sysfs.c
27  * Access PCI subsystem using Linux's sysfs interface.  This interface is
28  * available starting somewhere in the late 2.5.x kernel phase, and is the
29  * prefered method on all 2.6.x kernels.
30  *
31  * \author Ian Romanick <[email protected]>
32  */
33 
34 #define _GNU_SOURCE
35 
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdio.h>
39 #include <unistd.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <fcntl.h>
43 #include <sys/mman.h>
44 #include <dirent.h>
45 #include <errno.h>
46 
47 #include "pciaccess.h"
48 #include "pciaccess_private.h"
49 
50 static int pci_device_linux_sysfs_read_rom( struct pci_device * dev,
51     void * buffer );
52 
53 static int pci_device_linux_sysfs_probe( struct pci_device * dev );
54 
55 static int pci_device_linux_sysfs_map_region( struct pci_device * dev,
56     unsigned region, int write_enable );
57 
58 static int pci_device_linux_sysfs_unmap_region( struct pci_device * dev,
59     unsigned region );
60 
61 static int pci_device_linux_sysfs_read( struct pci_device * dev, void * data,
62     pciaddr_t offset, pciaddr_t size, pciaddr_t * bytes_read );
63 
64 static int pci_device_linux_sysfs_write( struct pci_device * dev,
65     const void * data, pciaddr_t offset, pciaddr_t size,
66     pciaddr_t * bytes_wrtten );
67 
68 static const struct pci_system_methods linux_sysfs_methods = {
69     .destroy = NULL,
70     .destroy_device = NULL,
71     .read_rom = pci_device_linux_sysfs_read_rom,
72     .probe = pci_device_linux_sysfs_probe,
73     .map = pci_device_linux_sysfs_map_region,
74     .unmap = pci_device_linux_sysfs_unmap_region,
75 
76     .read = pci_device_linux_sysfs_read,
77     .write = pci_device_linux_sysfs_write,
78 
79     .fill_capabilities = pci_fill_capabilities_generic
80 };
81 
82 #define SYS_BUS_PCI "/sys/bus/pci/devices"
83 
84 
85 static void populate_entries( struct pci_system * pci_sys );
86 
87 
88 /**
89  * Attempt to access PCI subsystem using Linux's sysfs interface.
90  */
91 int
92 pci_system_linux_sysfs_create( void )
93 {
94     int err = 0;
95     struct stat st;
96 
97 
98     /* If the directory "/sys/bus/pci/devices" exists, then the PCI subsystem
99      * can be accessed using this interface.
100      */
101 
102     if ( stat( SYS_BUS_PCI, & st ) == 0 ) {
103 	pci_sys = calloc( 1, sizeof( struct pci_system ) );
104 	if ( pci_sys != NULL ) {
105 	    pci_sys->methods = & linux_sysfs_methods;
106 	    populate_entries( pci_sys );
107 	}
108 	else {
109 	    err = ENOMEM;
110 	}
111     }
112     else {
113 	err = errno;
114     }
115 
116     return err;
117 }
118 
119 
120 /**
121  * Filter out the names "." and ".." from the scanned sysfs entries.
122  *
123  * \param d  Directory entry being processed by \c scandir.
124  *
125  * \return
126  * Zero if the entry name matches either "." or "..", non-zero otherwise.
127  *
128  * \sa scandir, populate_entries
129  */
130 static int
131 scan_sys_pci_filter( const struct dirent * d )
132 {
133     return !((strcmp( d->d_name, "." ) == 0)
134 	     || (strcmp( d->d_name, ".." ) == 0));
135 }
136 
137 
138 void
139 populate_entries( struct pci_system * p )
140 {
141     struct dirent ** devices;
142     int n;
143     int i;
144 
145 
146     n = scandir( SYS_BUS_PCI, & devices, scan_sys_pci_filter, alphasort );
147     if ( n > 0 ) {
148 	p->num_devices = n;
149 	p->devices = calloc( n, sizeof( struct pci_device_private ) );
150 
151 
152 	for ( i = 0 ; i < n ; i++ ) {
153 	    unsigned dom, bus, dev, func;
154 
155 
156 	    sscanf( devices[ i ]->d_name, "%04x:%02x:%02x.%1u",
157 		    & dom, & bus, & dev, & func );
158 
159 	    p->devices[ i ].base.domain = dom;
160 	    p->devices[ i ].base.bus = bus;
161 	    p->devices[ i ].base.dev = dev;
162 	    p->devices[ i ].base.func = func;
163 	}
164     }
165 }
166 
167 
168 static int
169 pci_device_linux_sysfs_probe( struct pci_device * dev )
170 {
171     char     name[256];
172     uint8_t  config[256];
173     char     resource[512];
174     int fd;
175     pciaddr_t bytes;
176     unsigned i;
177     int err;
178 
179 
180     err = pci_device_linux_sysfs_read( dev, config, 0, 256, & bytes );
181     if ( bytes >= 64 ) {
182 	struct pci_device_private *priv = (struct pci_device_private *) dev;
183 
184 	dev->vendor_id = (uint16_t)config[0] + ((uint16_t)config[1] << 8);
185 	dev->device_id = (uint16_t)config[2] + ((uint16_t)config[3] << 8);
186 	dev->device_class = (uint32_t)config[9] + ((uint32_t)config[10] << 8)
187 	  + ((uint32_t)config[11] << 16);
188 	dev->revision = config[8];
189 	dev->subvendor_id = (uint16_t)config[44] + ((uint16_t)config[45] << 8);
190 	dev->subdevice_id = (uint16_t)config[46] + ((uint16_t)config[47] << 8);
191 	dev->irq = config[60];
192 
193 	priv->header_type = config[14];
194 
195 
196 	/* The PCI config registers can be used to obtain information
197 	 * about the memory and I/O regions for the device.  However,
198 	 * doing so requires some tricky parsing (to correctly handle
199 	 * 64-bit memory regions) and requires writing to the config
200 	 * registers.  Since we'd like to avoid having to deal with the
201 	 * parsing issues and non-root users can write to PCI config
202 	 * registers, we use a different file in the device's sysfs
203 	 * directory called "resource".
204 	 *
205 	 * The resource file contains all of the needed information in
206 	 * a format that is consistent across all platforms.  Each BAR
207 	 * and the expansion ROM have a single line of data containing
208 	 * 3, 64-bit hex values:  the first address in the region,
209 	 * the last address in the region, and the region's flags.
210 	 */
211 	snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/resource",
212 		  SYS_BUS_PCI,
213 		  dev->domain,
214 		  dev->bus,
215 		  dev->dev,
216 		  dev->func );
217 	fd = open( name, O_RDONLY );
218 	if ( fd != -1 ) {
219 	    char * next;
220 	    pciaddr_t  low_addr;
221 	    pciaddr_t  high_addr;
222 	    pciaddr_t  flags;
223 
224 
225 	    bytes = read( fd, resource, 512 );
226 	    resource[511] = '\0';
227 
228 	    close( fd );
229 
230 	    next = resource;
231 	    for ( i = 0 ; i < 6 ; i++ ) {
232 
233 		dev->regions[i].base_addr = strtoull( next, & next, 16 );
234 		high_addr = strtoull( next, & next, 16 );
235 		flags = strtoull( next, & next, 16 );
236 
237 		if ( dev->regions[i].base_addr != 0 ) {
238 		    dev->regions[i].size = (high_addr
239 					    - dev->regions[i].base_addr) + 1;
240 
241 		    dev->regions[i].is_IO = (flags & 0x01);
242 		    dev->regions[i].is_64 = (flags & 0x04);
243 		    dev->regions[i].is_prefetchable = (flags & 0x08);
244 		}
245 	    }
246 
247 	    low_addr = strtoull( next, & next, 16 );
248 	    high_addr = strtoull( next, & next, 16 );
249 	    flags = strtoull( next, & next, 16 );
250 	    if ( low_addr != 0 ) {
251 		dev->rom_size = (high_addr - low_addr) + 1;
252 	    }
253 	}
254     }
255 
256     return err;
257 }
258 
259 
260 static int
261 pci_device_linux_sysfs_read_rom( struct pci_device * dev, void * buffer )
262 {
263     char name[256];
264     int fd;
265     struct stat  st;
266     int err = 0;
267     size_t total_bytes;
268 
269 
270     snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/rom",
271 	      SYS_BUS_PCI,
272 	      dev->domain,
273 	      dev->bus,
274 	      dev->dev,
275 	      dev->func );
276 
277     fd = open( name, O_RDWR );
278     if ( fd == -1 ) {
279 	return errno;
280     }
281 
282 
283     if ( fstat( fd, & st ) == -1 ) {
284 	close( fd );
285 	return errno;
286     }
287 
288 
289     /* This is a quirky thing on Linux.  Even though the ROM and the file
290      * for the ROM in sysfs are read-only, the string "1" must be written to
291      * the file to enable the ROM.  After the data has been read, "0" must be
292      * written to the file to disable the ROM.
293      */
294     write( fd, "1", 1 );
295     lseek( fd, 0, SEEK_SET );
296 
297     for ( total_bytes = 0 ; total_bytes < st.st_size ; /* empty */ ) {
298 	const int bytes = read( fd, (char *) buffer + total_bytes,
299 				st.st_size - total_bytes );
300 	if ( bytes == -1 ) {
301 	    err = errno;
302 	    break;
303 	}
304 	else if ( bytes == 0 ) {
305 	    break;
306 	}
307 
308 	total_bytes += bytes;
309     }
310 
311 
312     lseek( fd, 0, SEEK_SET );
313     write( fd, "0", 1 );
314 
315     close( fd );
316     return err;
317 }
318 
319 
320 static int
321 pci_device_linux_sysfs_read( struct pci_device * dev, void * data,
322 			     pciaddr_t offset, pciaddr_t size,
323 			     pciaddr_t * bytes_read )
324 {
325     char name[256];
326     pciaddr_t temp_size = size;
327     int err = 0;
328     int fd;
329 
330 
331     if ( bytes_read != NULL ) {
332 	*bytes_read = 0;
333     }
334 
335     /* Each device has a directory under sysfs.  Within that directory there
336      * is a file named "config".  This file used to access the PCI config
337      * space.  It is used here to obtain most of the information about the
338      * device.
339      */
340     snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/config",
341 	      SYS_BUS_PCI,
342 	      dev->domain,
343 	      dev->bus,
344 	      dev->dev,
345 	      dev->func );
346 
347     fd = open( name, O_RDONLY );
348     if ( fd == -1 ) {
349 	return errno;
350     }
351 
352 
353     while ( temp_size > 0 ) {
354 	const ssize_t bytes = pread64( fd, data, temp_size, offset );
355 
356 	/* If zero bytes were read, then we assume it's the end of the
357 	 * config file.
358 	 */
359 	if ( bytes <= 0 ) {
360 	    err = errno;
361 	    break;
362 	}
363 
364 	temp_size -= bytes;
365 	offset += bytes;
366 	data += bytes;
367     }
368 
369     if ( bytes_read != NULL ) {
370 	*bytes_read = size - temp_size;
371     }
372 
373     close( fd );
374     return err;
375 }
376 
377 
378 static int
379 pci_device_linux_sysfs_write( struct pci_device * dev, const void * data,
380 			     pciaddr_t offset, pciaddr_t size,
381 			     pciaddr_t * bytes_written )
382 {
383     char name[256];
384     pciaddr_t temp_size = size;
385     int err = 0;
386     int fd;
387 
388 
389     if ( bytes_written != NULL ) {
390 	*bytes_written = 0;
391     }
392 
393     /* Each device has a directory under sysfs.  Within that directory there
394      * is a file named "config".  This file used to access the PCI config
395      * space.  It is used here to obtain most of the information about the
396      * device.
397      */
398     snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/config",
399 	      SYS_BUS_PCI,
400 	      dev->domain,
401 	      dev->bus,
402 	      dev->dev,
403 	      dev->func );
404 
405     fd = open( name, O_WRONLY );
406     if ( fd == -1 ) {
407 	return errno;
408     }
409 
410 
411     while ( temp_size > 0 ) {
412 	const ssize_t bytes = pwrite64( fd, data, temp_size, offset );
413 
414 	/* If zero bytes were written, then we assume it's the end of the
415 	 * config file.
416 	 */
417 	if ( bytes <= 0 ) {
418 	    err = errno;
419 	    break;
420 	}
421 
422 	temp_size -= bytes;
423 	offset += bytes;
424 	data += bytes;
425     }
426 
427     if ( bytes_written != NULL ) {
428 	*bytes_written = size - temp_size;
429     }
430 
431     close( fd );
432     return err;
433 }
434 
435 
436 /**
437  * Map a memory region for a device using the Linux sysfs interface.
438  *
439  * \param dev          Device whose memory region is to be mapped.
440  * \param region       Region, on the range [0, 5], that is to be mapped.
441  * \param write_enable Map for writing (non-zero).
442  *
443  * \return
444  * Zero on success or an \c errno value on failure.
445  *
446  * \sa pci_device_map_region, pci_device_linux_sysfs_unmap_region
447  *
448  * \todo
449  * Some older 2.6.x kernels don't implement the resourceN files.  On those
450  * systems /dev/mem must be used.  On these systems it is also possible that
451  * \c mmap64 may need to be used.
452  */
453 static int
454 pci_device_linux_sysfs_map_region( struct pci_device * dev, unsigned region,
455 				   int write_enable )
456 {
457     char name[256];
458     int fd;
459     int err = 0;
460     const int prot = (write_enable) ? (PROT_READ | PROT_WRITE) : PROT_READ;
461 
462 
463     snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/resource%u",
464 	      SYS_BUS_PCI,
465 	      dev->domain,
466 	      dev->bus,
467 	      dev->dev,
468 	      dev->func,
469 	      region );
470 
471     fd = open( name, (write_enable) ? O_RDWR : O_RDONLY );
472     if ( fd == -1 ) {
473 	return errno;
474     }
475 
476 
477     dev->regions[ region ].memory = mmap( NULL, dev->regions[ region ].size,
478 					  prot, MAP_SHARED, fd, 0 );
479     if ( dev->regions[ region ].memory == MAP_FAILED ) {
480 	err = errno;
481 	dev->regions[ region ].memory = NULL;
482     }
483 
484     close( fd );
485     return err;
486 }
487 
488 
489 /**
490  * Unmap the specified region using the Linux sysfs interface.
491  *
492  * \param dev          Device whose memory region is to be mapped.
493  * \param region       Region, on the range [0, 5], that is to be mapped.
494  *
495  * \return
496  * Zero on success or an \c errno value on failure.
497  *
498  * \sa pci_device_unmap_region, pci_device_linux_sysfs_map_region
499  *
500  * \todo
501  * Some older 2.6.x kernels don't implement the resourceN files.  On those
502  * systems /dev/mem must be used.  On these systems it is also possible that
503  * \c mmap64 may need to be used.
504  */
505 static int
506 pci_device_linux_sysfs_unmap_region( struct pci_device * dev, unsigned region )
507 {
508     int err = 0;
509 
510     if ( munmap( dev->regions[ region ].memory, dev->regions[ region ].size )
511 	 == -1 ) {
512 	err = errno;
513     }
514 
515     dev->regions[ region ].memory = NULL;
516 
517     return err;
518 }
519