summaryrefslogtreecommitdiff
path: root/src/linux_sysfs.c
blob: cd2713dbaf77f78f616e1aca235e9ef5be02f2da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
/*
 * (C) Copyright IBM Corporation 2006
 * All Rights Reserved.
 * Copyright 2012 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
 * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

/**
 * \file linux_sysfs.c
 * Access PCI subsystem using Linux's sysfs interface.  This interface is
 * available starting somewhere in the late 2.5.x kernel phase, and is the
 * preferred method on all 2.6.x kernels.
 *
 * \author Ian Romanick <idr@us.ibm.com>
 */

#define _GNU_SOURCE

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/mman.h>
#include <dirent.h>
#include <errno.h>
#include <limits.h>

#if defined(__i386__) || defined(__x86_64__) || defined(__arm__)
#include <sys/io.h>
#else
#define inb(x) -1
#define inw(x) -1
#define inl(x) -1
#define outb(x,y) do {} while (0)
#define outw(x,y) do {} while (0)
#define outl(x,y) do {} while (0)
#define iopl(x) -1
#endif

#ifdef HAVE_MTRR
#include <asm/mtrr.h>
#include <sys/ioctl.h>
#endif

#include "pciaccess.h"
#include "pciaccess_private.h"
#include "linux_devmem.h"

static const struct pci_system_methods linux_sysfs_methods;

#define SYS_BUS_PCI "/sys/bus/pci/devices"

static int
pci_device_linux_sysfs_read( struct pci_device * dev, void * data,
			     pciaddr_t offset, pciaddr_t size,
			     pciaddr_t * bytes_read );

static int populate_entries(struct pci_system * pci_sys);

/**
 * Attempt to access PCI subsystem using Linux's sysfs interface.
 */
_pci_hidden int
pci_system_linux_sysfs_create( void )
{
    int err = 0;
    struct stat st;


    /* If the directory "/sys/bus/pci/devices" exists, then the PCI subsystem
     * can be accessed using this interface.
     */

    if ( stat( SYS_BUS_PCI, & st ) == 0 ) {
	pci_sys = calloc( 1, sizeof( struct pci_system ) );
	if ( pci_sys != NULL ) {
	    pci_sys->methods = & linux_sysfs_methods;
#ifdef HAVE_MTRR
	    pci_sys->mtrr_fd = open("/proc/mtrr", O_WRONLY | O_CLOEXEC);
#endif
	    err = populate_entries(pci_sys);
	}
	else {
	    err = ENOMEM;
	}
    }
    else {
	err = errno;
    }

    return err;
}


/**
 * Filter out the names "." and ".." from the scanned sysfs entries, and
 * domains requiring 32-bits.
 *
 * \param d  Directory entry being processed by \c scandir.
 *
 * \return
 * Zero if the entry name matches either "." or "..", or the domain requires
 * 32 bits, non-zero otherwise.
 *
 * \sa scandir, populate_entries
 */
static int
scan_sys_pci_filter( const struct dirent * d )
{
    if (d->d_name[0] != '.') {
        unsigned dom = 0;

        sscanf(d->d_name, "%x:", &dom);
        if (dom > USHRT_MAX)
            return 0;
    }

    return !((strcmp( d->d_name, "." ) == 0)
	     || (strcmp( d->d_name, ".." ) == 0));
}


int
populate_entries( struct pci_system * p )
{
    struct dirent ** devices = NULL;
    int n;
    int i;
    int err = 0;


    n = scandir( SYS_BUS_PCI, & devices, scan_sys_pci_filter, alphasort );
    if ( n > 0 ) {
	p->num_devices = n;
	p->devices = calloc( n, sizeof( struct pci_device_private ) );

	if (p->devices != NULL) {
	    for (i = 0 ; i < n ; i++) {
		uint8_t config[48];
		pciaddr_t bytes;
		unsigned dom, bus, dev, func;
		struct pci_device_private *device =
			(struct pci_device_private *) &p->devices[i];


		sscanf(devices[i]->d_name, "%04x:%02x:%02x.%1u",
		       & dom, & bus, & dev, & func);

		device->base.domain = dom;
		device->base.bus = bus;
		device->base.dev = dev;
		device->base.func = func;


		err = pci_device_linux_sysfs_read(& device->base, config, 0,
						  48, & bytes);
		if ((bytes == 48) && !err) {
		    device->base.vendor_id = (uint16_t)config[0]
			+ ((uint16_t)config[1] << 8);
		    device->base.device_id = (uint16_t)config[2]
			+ ((uint16_t)config[3] << 8);
		    device->base.device_class = (uint32_t)config[9]
			+ ((uint32_t)config[10] << 8)
			+ ((uint32_t)config[11] << 16);
		    device->base.revision = config[8];
		    device->base.subvendor_id = (uint16_t)config[44]
			+ ((uint16_t)config[45] << 8);
		    device->base.subdevice_id = (uint16_t)config[46]
			+ ((uint16_t)config[47] << 8);
		}

		if (err) {
		    break;
		}
	    }
	}
	else {
	    err = ENOMEM;
	}
    }

    for (i = 0; i < n; i++)
	free(devices[i]);
    free(devices);

    if (err) {
	free(p->devices);
	p->devices = NULL;
    }

    return err;
}


static int
pci_device_linux_sysfs_probe( struct pci_device * dev )
{
    char     name[256];
    uint8_t  config[256];
    char     resource[512];
    int fd;
    pciaddr_t bytes;
    unsigned i;
    int err;


    err = pci_device_linux_sysfs_read( dev, config, 0, 256, & bytes );
    if ( bytes >= 64 ) {
	struct pci_device_private *priv = (struct pci_device_private *) dev;

	dev->irq = config[60];
	priv->header_type = config[14];


	/* The PCI config registers can be used to obtain information
	 * about the memory and I/O regions for the device.  However,
	 * doing so requires some tricky parsing (to correctly handle
	 * 64-bit memory regions) and requires writing to the config
	 * registers.  Since we'd like to avoid having to deal with the
	 * parsing issues and non-root users can write to PCI config
	 * registers, we use a different file in the device's sysfs
	 * directory called "resource".
	 *
	 * The resource file contains all of the needed information in
	 * a format that is consistent across all platforms.  Each BAR
	 * and the expansion ROM have a single line of data containing
	 * 3, 64-bit hex values:  the first address in the region,
	 * the last address in the region, and the region's flags.
	 */
	snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/resource",
		  SYS_BUS_PCI,
		  dev->domain,
		  dev->bus,
		  dev->dev,
		  dev->func );
	fd = open( name, O_RDONLY | O_CLOEXEC);
	if ( fd != -1 ) {
	    char * next;
	    pciaddr_t  low_addr;
	    pciaddr_t  high_addr;
	    pciaddr_t  flags;


	    bytes = read( fd, resource, 512 );
	    resource[511] = '\0';

	    close( fd );

	    next = resource;
	    for ( i = 0 ; i < 6 ; i++ ) {

		dev->regions[i].base_addr = strtoull( next, & next, 16 );
		high_addr = strtoull( next, & next, 16 );
		flags = strtoull( next, & next, 16 );

		if ( dev->regions[i].base_addr != 0 ) {
		    dev->regions[i].size = (high_addr
					    - dev->regions[i].base_addr) + 1;

		    dev->regions[i].is_IO = (flags & 0x01);
		    dev->regions[i].is_64 = (flags & 0x04);
		    dev->regions[i].is_prefetchable = (flags & 0x08);
		}
	    }

	    low_addr = strtoull( next, & next, 16 );
	    high_addr = strtoull( next, & next, 16 );
	    flags = strtoull( next, & next, 16 );
	    if ( low_addr != 0 ) {
		priv->rom_base = low_addr;
		dev->rom_size = (high_addr - low_addr) + 1;
	    }
	}
    }

    return err;
}


static int
pci_device_linux_sysfs_read_rom( struct pci_device * dev, void * buffer )
{
    char name[256];
    int fd;
    struct stat  st;
    int err = 0;
    size_t rom_size;
    size_t total_bytes;


    snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/rom",
	      SYS_BUS_PCI,
	      dev->domain,
	      dev->bus,
	      dev->dev,
	      dev->func );

    fd = open( name, O_RDWR | O_CLOEXEC);
    if ( fd == -1 ) {
#ifdef LINUX_ROM
	/* If reading the ROM using sysfs fails, fall back to the old
	 * /dev/mem based interface.
	 * disable this for newer kernels using configure
	 */
	return pci_device_linux_devmem_read_rom(dev, buffer);
#else
	return errno;
#endif
    }


    if ( fstat( fd, & st ) == -1 ) {
	close( fd );
	return errno;
    }

    rom_size = st.st_size;
    if ( rom_size == 0 )
	rom_size = 0x10000;

    /* This is a quirky thing on Linux.  Even though the ROM and the file
     * for the ROM in sysfs are read-only, the string "1" must be written to
     * the file to enable the ROM.  After the data has been read, "0" must be
     * written to the file to disable the ROM.
     */
    write( fd, "1", 1 );
    lseek( fd, 0, SEEK_SET );

    for ( total_bytes = 0 ; total_bytes < rom_size ; /* empty */ ) {
	const int bytes = read( fd, (char *) buffer + total_bytes,
				rom_size - total_bytes );
	if ( bytes == -1 ) {
	    err = errno;
	    break;
	}
	else if ( bytes == 0 ) {
	    break;
	}

	total_bytes += bytes;
    }


    lseek( fd, 0, SEEK_SET );
    write( fd, "0", 1 );

    close( fd );
    return err;
}


static int
pci_device_linux_sysfs_read( struct pci_device * dev, void * data,
			     pciaddr_t offset, pciaddr_t size,
			     pciaddr_t * bytes_read )
{
    char name[256];
    pciaddr_t temp_size = size;
    int err = 0;
    int fd;
    char *data_bytes = data;

    if ( bytes_read != NULL ) {
	*bytes_read = 0;
    }

    /* Each device has a directory under sysfs.  Within that directory there
     * is a file named "config".  This file used to access the PCI config
     * space.  It is used here to obtain most of the information about the
     * device.
     */
    snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/config",
	      SYS_BUS_PCI,
	      dev->domain,
	      dev->bus,
	      dev->dev,
	      dev->func );

    fd = open( name, O_RDONLY | O_CLOEXEC);
    if ( fd == -1 ) {
	return errno;
    }


    while ( temp_size > 0 ) {
	const ssize_t bytes = pread64( fd, data_bytes, temp_size, offset );

	/* If zero bytes were read, then we assume it's the end of the
	 * config file.
	 */
	if (bytes == 0)
	    break;
	if ( bytes < 0 ) {
	    err = errno;
	    break;
	}

	temp_size -= bytes;
	offset += bytes;
	data_bytes += bytes;
    }

    if ( bytes_read != NULL ) {
	*bytes_read = size - temp_size;
    }

    close( fd );
    return err;
}


static int
pci_device_linux_sysfs_write( struct pci_device * dev, const void * data,
			     pciaddr_t offset, pciaddr_t size,
			     pciaddr_t * bytes_written )
{
    char name[256];
    pciaddr_t temp_size = size;
    int err = 0;
    int fd;
    const char *data_bytes = data;

    if ( bytes_written != NULL ) {
	*bytes_written = 0;
    }

    /* Each device has a directory under sysfs.  Within that directory there
     * is a file named "config".  This file used to access the PCI config
     * space.  It is used here to obtain most of the information about the
     * device.
     */
    snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/config",
	      SYS_BUS_PCI,
	      dev->domain,
	      dev->bus,
	      dev->dev,
	      dev->func );

    fd = open( name, O_WRONLY | O_CLOEXEC);
    if ( fd == -1 ) {
	return errno;
    }


    while ( temp_size > 0 ) {
	const ssize_t bytes = pwrite64( fd, data_bytes, temp_size, offset );

	/* If zero bytes were written, then we assume it's the end of the
	 * config file.
	 */
	if ( bytes == 0 )
	    break;
	if ( bytes < 0 ) {
	    err = errno;
	    break;
	}

	temp_size -= bytes;
	offset += bytes;
	data_bytes += bytes;
    }

    if ( bytes_written != NULL ) {
	*bytes_written = size - temp_size;
    }

    close( fd );
    return err;
}

static int
pci_device_linux_sysfs_map_range_wc(struct pci_device *dev,
				    struct pci_device_mapping *map)
{
    char name[256];
    int fd;
    const int prot = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
        ? (PROT_READ | PROT_WRITE) : PROT_READ;
    const int open_flags = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
        ? O_RDWR : O_RDONLY;
    const off_t offset = map->base - dev->regions[map->region].base_addr;

    snprintf(name, 255, "%s/%04x:%02x:%02x.%1u/resource%u_wc",
	     SYS_BUS_PCI,
	     dev->domain,
	     dev->bus,
	     dev->dev,
	     dev->func,
	     map->region);
    fd = open(name, open_flags | O_CLOEXEC);
    if (fd == -1)
	    return errno;

    map->memory = mmap(NULL, map->size, prot, MAP_SHARED, fd, offset);
    if (map->memory == MAP_FAILED) {
        map->memory = NULL;
	close(fd);
	return errno;
    }

    close(fd);

    return 0;
}

/**
 * Map a memory region for a device using the Linux sysfs interface.
 *
 * \param dev   Device whose memory region is to be mapped.
 * \param map   Parameters of the mapping that is to be created.
 *
 * \return
 * Zero on success or an \c errno value on failure.
 *
 * \sa pci_device_map_rrange, pci_device_linux_sysfs_unmap_range
 *
 * \todo
 * Some older 2.6.x kernels don't implement the resourceN files.  On those
 * systems /dev/mem must be used.  On these systems it is also possible that
 * \c mmap64 may need to be used.
 */
static int
pci_device_linux_sysfs_map_range(struct pci_device *dev,
                                 struct pci_device_mapping *map)
{
    char name[256];
    int fd;
    int err = 0;
    const int prot = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
        ? (PROT_READ | PROT_WRITE) : PROT_READ;
    const int open_flags = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
        ? O_RDWR : O_RDONLY;
    const off_t offset = map->base - dev->regions[map->region].base_addr;
#ifdef HAVE_MTRR
    struct mtrr_sentry sentry = {
	.base = map->base,
        .size = map->size,
	.type = MTRR_TYPE_UNCACHABLE
    };
#endif

    /* For WC mappings, try sysfs resourceN_wc file first */
    if ((map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) &&
	!pci_device_linux_sysfs_map_range_wc(dev, map))
	    return 0;

    snprintf(name, 255, "%s/%04x:%02x:%02x.%1u/resource%u",
             SYS_BUS_PCI,
             dev->domain,
             dev->bus,
             dev->dev,
             dev->func,
             map->region);

    fd = open(name, open_flags | O_CLOEXEC);
    if (fd == -1) {
        return errno;
    }


    map->memory = mmap(NULL, map->size, prot, MAP_SHARED, fd, offset);
    if (map->memory == MAP_FAILED) {
        map->memory = NULL;
	close(fd);
	return errno;
    }

#ifdef HAVE_MTRR
    if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) != 0) {
        sentry.type = MTRR_TYPE_WRBACK;
    } else if ((map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) != 0) {
        sentry.type = MTRR_TYPE_WRCOMB;
    }

    if (pci_sys->mtrr_fd != -1 && sentry.type != MTRR_TYPE_UNCACHABLE) {
	if (ioctl(pci_sys->mtrr_fd, MTRRIOC_ADD_ENTRY, &sentry) < 0) {
	    /* FIXME: Should we report an error in this case?
	     */
	    fprintf(stderr, "error setting MTRR "
		    "(base = 0x%016" PRIx64 ", size = 0x%08x, type = %u) %s (%d)\n",
		    (pciaddr_t)sentry.base, sentry.size, sentry.type,
		    strerror(errno), errno);
/*            err = errno;*/
	}
	/* KLUDGE ALERT -- rewrite the PTEs to turn off the CD and WT bits */
	mprotect (map->memory, map->size, PROT_NONE);
	err = mprotect (map->memory, map->size, PROT_READ|PROT_WRITE);

	if (err != 0) {
	    fprintf(stderr, "mprotect(PROT_READ | PROT_WRITE) failed: %s\n",
		    strerror(errno));
	    fprintf(stderr, "remapping without mprotect performance kludge.\n");

	    munmap(map->memory, map->size);
	    map->memory = mmap(NULL, map->size, prot, MAP_SHARED, fd, offset);
	    if (map->memory == MAP_FAILED) {
		map->memory = NULL;
		close(fd);
		return errno;
	    }
	}
    }
#endif

    close(fd);

    return 0;
}

/**
 * Unmap a memory region for a device using the Linux sysfs interface.
 *
 * \param dev   Device whose memory region is to be unmapped.
 * \param map   Parameters of the mapping that is to be destroyed.
 *
 * \return
 * Zero on success or an \c errno value on failure.
 *
 * \sa pci_device_map_rrange, pci_device_linux_sysfs_map_range
 *
 * \todo
 * Some older 2.6.x kernels don't implement the resourceN files.  On those
 * systems /dev/mem must be used.  On these systems it is also possible that
 * \c mmap64 may need to be used.
 */
static int
pci_device_linux_sysfs_unmap_range(struct pci_device *dev,
				   struct pci_device_mapping *map)
{
    int err = 0;
#ifdef HAVE_MTRR
    struct mtrr_sentry sentry = {
	.base = map->base,
        .size = map->size,
	.type = MTRR_TYPE_UNCACHABLE
    };
#endif

    err = pci_device_generic_unmap_range (dev, map);
    if (err)
	return err;

#ifdef HAVE_MTRR
    if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) != 0) {
        sentry.type = MTRR_TYPE_WRBACK;
    } else if ((map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) != 0) {
        sentry.type = MTRR_TYPE_WRCOMB;
    }

    if (pci_sys->mtrr_fd != -1 && sentry.type != MTRR_TYPE_UNCACHABLE) {
	if (ioctl(pci_sys->mtrr_fd, MTRRIOC_DEL_ENTRY, &sentry) < 0) {
	    /* FIXME: Should we report an error in this case?
	     */
	    fprintf(stderr, "error setting MTRR "
		    "(base = 0x%016" PRIx64 ", size = 0x%08x, type = %u) %s (%d)\n",
		    (pciaddr_t)sentry.base, sentry.size, sentry.type,
		    strerror(errno), errno);
/*            err = errno;*/
	}
    }
#endif

    return err;
}

static void pci_device_linux_sysfs_enable(struct pci_device *dev)
{
    char name[256];
    int fd;

    snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/enable",
	      SYS_BUS_PCI,
	      dev->domain,
	      dev->bus,
	      dev->dev,
	      dev->func );

    fd = open( name, O_RDWR | O_CLOEXEC);
    if (fd == -1)
       return;

    write( fd, "1", 1 );
    close(fd);
}

static int pci_device_linux_sysfs_boot_vga(struct pci_device *dev)
{
    char name[256];
    char reply[3];
    int fd, bytes_read;
    int ret = 0;

    snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/boot_vga",
	      SYS_BUS_PCI,
	      dev->domain,
	      dev->bus,
	      dev->dev,
	      dev->func );

    fd = open( name, O_RDONLY | O_CLOEXEC);
    if (fd == -1)
       return 0;

    bytes_read = read(fd, reply, 1);
    if (bytes_read != 1)
	goto out;
    if (reply[0] == '1')
	ret = 1;
out:
    close(fd);
    return ret;
}

static int pci_device_linux_sysfs_has_kernel_driver(struct pci_device *dev)
{
    char name[256];
    struct stat dummy;
    int ret;

    snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/driver",
	      SYS_BUS_PCI,
	      dev->domain,
	      dev->bus,
	      dev->dev,
	      dev->func );

    ret = stat(name, &dummy);
    if (ret < 0)
	return 0;
    return 1;
}

static struct pci_io_handle *
pci_device_linux_sysfs_open_device_io(struct pci_io_handle *ret,
				      struct pci_device *dev, int bar,
				      pciaddr_t base, pciaddr_t size)
{
    char name[PATH_MAX];

    snprintf(name, PATH_MAX, "%s/%04x:%02x:%02x.%1u/resource%d",
	     SYS_BUS_PCI, dev->domain, dev->bus, dev->dev, dev->func, bar);

    ret->fd = open(name, O_RDWR | O_CLOEXEC);

    if (ret->fd < 0)
	return NULL;

    ret->base = base;
    ret->size = size;
    ret->is_legacy = 0;

    return ret;
}

static struct pci_io_handle *
pci_device_linux_sysfs_open_legacy_io(struct pci_io_handle *ret,
				      struct pci_device *dev, pciaddr_t base,
				      pciaddr_t size)
{
    char name[PATH_MAX];

    /* First check if there's a legacy io method for the device */
    while (dev) {
	snprintf(name, PATH_MAX, "/sys/class/pci_bus/%04x:%02x/legacy_io",
		 dev->domain, dev->bus);

	ret->fd = open(name, O_RDWR | O_CLOEXEC);
	if (ret->fd >= 0)
	    break;

	dev = pci_device_get_parent_bridge(dev);
    }

    /*
     * You would think you'd want to use /dev/port here.  Don't make that
     * mistake, /dev/port only does byte-wide i/o cycles which means it
     * doesn't work.  If you think this is stupid, well, you're right.
     */

    /* If we've no other choice, iopl */
    if (ret->fd < 0) {
	if (iopl(3))
	    return NULL;
    }

    ret->base = base;
    ret->size = size;
    ret->is_legacy = 1;

    return ret;
}

static void
pci_device_linux_sysfs_close_io(struct pci_device *dev,
				struct pci_io_handle *handle)
{
    if (handle->fd > -1)
	close(handle->fd);
}

static uint32_t
pci_device_linux_sysfs_read32(struct pci_io_handle *handle, uint32_t port)
{
    uint32_t ret;

    if (handle->fd > -1) {
	if (handle->is_legacy)
	    pread(handle->fd, &ret, 4, port + handle->base);
	else
	    pread(handle->fd, &ret, 4, port);
    } else {
	ret = inl(port + handle->base);
    }
	
    return ret;
}

static uint16_t
pci_device_linux_sysfs_read16(struct pci_io_handle *handle, uint32_t port)
{
    uint16_t ret;

    if (handle->fd > -1) {
	if (handle->is_legacy)
	    pread(handle->fd, &ret, 2, port + handle->base);
	else
	    pread(handle->fd, &ret, 2, port);
    } else {
	ret = inw(port + handle->base);
    }

    return ret;
}

static uint8_t
pci_device_linux_sysfs_read8(struct pci_io_handle *handle, uint32_t port)
{
    uint8_t ret;

    if (handle->fd > -1) {
	if (handle->is_legacy)
	    pread(handle->fd, &ret, 1, port + handle->base);
	else
	    pread(handle->fd, &ret, 1, port);
    } else {
	ret = inb(port + handle->base);
    }

    return ret;
}

static void
pci_device_linux_sysfs_write32(struct pci_io_handle *handle, uint32_t port,
			       uint32_t data)
{
    if (handle->fd > -1) {
	if (handle->is_legacy)
	    pwrite(handle->fd, &data, 4, port + handle->base);
	else
	    pwrite(handle->fd, &data, 4, port);
    } else {
	outl(data, port + handle->base);
    }
}

static void
pci_device_linux_sysfs_write16(struct pci_io_handle *handle, uint32_t port,
			       uint16_t data)
{
    if (handle->fd > -1) {
	if (handle->is_legacy)
	    pwrite(handle->fd, &data, 2, port + handle->base);
	else
	    pwrite(handle->fd, &data, 2, port);
    } else {
	outw(data, port + handle->base);
    }
}

static void
pci_device_linux_sysfs_write8(struct pci_io_handle *handle, uint32_t port,
			      uint8_t data)
{
    if (handle->fd > -1) {
	if (handle->is_legacy)
	    pwrite(handle->fd, &data, 1, port + handle->base);
	else
	    pwrite(handle->fd, &data, 1, port);
    } else {
	outb(data, port + handle->base);
    }
}

static int
pci_device_linux_sysfs_map_legacy(struct pci_device *dev, pciaddr_t base,
				  pciaddr_t size, unsigned map_flags, void **addr)
{
    char name[PATH_MAX];
    int flags = O_RDONLY;
    int prot = PROT_READ;
    int fd;
    int ret=0;

    if (map_flags & PCI_DEV_MAP_FLAG_WRITABLE) {
	flags = O_RDWR; /* O_RDWR != O_WRONLY | O_RDONLY */;
	prot |= PROT_WRITE;
    }

    /* First check if there's a legacy memory method for the device */
    while (dev) {
	snprintf(name, PATH_MAX, "/sys/class/pci_bus/%04x:%02x/legacy_mem",
		 dev->domain, dev->bus);

	fd = open(name, flags | O_CLOEXEC);
	if (fd >= 0)
	    break;

	dev = pci_device_get_parent_bridge(dev);
    }

    /* If not, /dev/mem is the best we can do */
    if (!dev)
	fd = open("/dev/mem", flags | O_CLOEXEC);

    if (fd < 0)
	return errno;

    *addr = mmap(NULL, size, prot, MAP_SHARED, fd, base);
    if (*addr == MAP_FAILED) {
	ret = errno;
    }

    close(fd);
    return ret;
}

static int
pci_device_linux_sysfs_unmap_legacy(struct pci_device *dev, void *addr, pciaddr_t size)
{
    return munmap(addr, size);
}


static void
pci_system_linux_destroy(void)
{
#ifdef HAVE_MTRR
	if (pci_sys->mtrr_fd != -1)
		close(pci_sys->mtrr_fd);
#endif
}

static const struct pci_system_methods linux_sysfs_methods = {
    .destroy = pci_system_linux_destroy,
    .destroy_device = NULL,
    .read_rom = pci_device_linux_sysfs_read_rom,
    .probe = pci_device_linux_sysfs_probe,
    .map_range = pci_device_linux_sysfs_map_range,
    .unmap_range = pci_device_linux_sysfs_unmap_range,

    .read = pci_device_linux_sysfs_read,
    .write = pci_device_linux_sysfs_write,

    .fill_capabilities = pci_fill_capabilities_generic,
    .enable = pci_device_linux_sysfs_enable,
    .boot_vga = pci_device_linux_sysfs_boot_vga,
    .has_kernel_driver = pci_device_linux_sysfs_has_kernel_driver,

    .open_device_io = pci_device_linux_sysfs_open_device_io,
    .open_legacy_io = pci_device_linux_sysfs_open_legacy_io,
    .close_io = pci_device_linux_sysfs_close_io,
    .read32 = pci_device_linux_sysfs_read32,
    .read16 = pci_device_linux_sysfs_read16,
    .read8 = pci_device_linux_sysfs_read8,
    .write32 = pci_device_linux_sysfs_write32,
    .write16 = pci_device_linux_sysfs_write16,
    .write8 = pci_device_linux_sysfs_write8,

    .map_legacy = pci_device_linux_sysfs_map_legacy,
    .unmap_legacy = pci_device_linux_sysfs_unmap_legacy,
};