OSDN Git Service

linux_sysfs.c: Include <limits.h> for PATH_MAX
[android-x86/external-libpciaccess.git] / src / solx_devfs.c
1 /*
2  * (C) Copyright IBM Corporation 2006
3  * Copyright (c) 2007, 2009, 2011, 2012, 2013 Oracle and/or its affiliates.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * on the rights to use, copy, modify, merge, publish, distribute, sub
10  * license, and/or sell copies of the Software, and to permit persons to whom
11  * the Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
20  * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 /*
26  * Solaris devfs interfaces
27  */
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <stdlib.h>
33 #include <strings.h>
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <sys/types.h>
37 #include <fcntl.h>
38 #include <sys/mman.h>
39 #include <errno.h>
40 #include <sys/pci.h>
41 #include <libdevinfo.h>
42 #include "pci_tools.h"
43
44 #ifdef __x86
45 # include <sys/sysi86.h>
46 # include <sys/psw.h>
47 #endif
48
49 #include "pciaccess.h"
50 #include "pciaccess_private.h"
51
52 /* #define DEBUG */
53
54 #define INITIAL_NUM_DEVICES     256
55 #define CELL_NUMS_1275  (sizeof(pci_regspec_t) / sizeof(uint_t))
56
57 typedef struct i_devnode {
58     uint8_t bus;
59     uint8_t dev;
60     uint8_t func;
61     di_node_t node;
62 } i_devnode_t;
63
64 typedef struct nexus {
65     int fd;
66     int first_bus;
67     int last_bus;
68     int domain;
69     char *path;                 /* for errors/debugging; fd is all we need */
70     char *dev_path;
71     struct nexus *next;
72 } nexus_t;
73
74 typedef struct probe_info {
75     volatile size_t num_allocated_elems;
76     volatile size_t num_devices;
77     struct pci_device_private * volatile devices;
78 } probe_info_t;
79
80 typedef struct probe_args {
81     probe_info_t *pinfo;
82     nexus_t *nexus;
83     int ret;
84 } probe_args_t;
85
86 typedef struct property_info {
87     const char *name;
88     int value;
89 } property_info_t;
90
91 static nexus_t *nexus_list = NULL;
92 #if !defined(__sparc)
93 static int xsvc_fd = -1;
94 #endif
95
96 #ifdef __sparc
97 static di_prom_handle_t di_phdl;
98 static size_t  nexus_count = 0;
99 #endif
100
101 /*
102  * Read config space in native processor endianness.  Endian-neutral
103  * processing can then take place.  On big endian machines, MSB and LSB
104  * of little endian data end up switched if read as little endian.
105  * They are in correct order if read as big endian.
106  */
107 #if defined(__sparc)
108 # define NATIVE_ENDIAN  PCITOOL_ACC_ATTR_ENDN_BIG
109 #elif defined(__x86)
110 # define NATIVE_ENDIAN  PCITOOL_ACC_ATTR_ENDN_LTL
111 #else
112 # error "ISA is neither __sparc nor __x86"
113 #endif
114
115 #ifdef __sparc
116 #define MAPPING_DEV_PATH(dev)    (((struct pci_device_private *) dev)->device_string)
117 #endif
118
119 static nexus_t *
120 find_nexus_for_bus( int domain, int bus )
121 {
122     nexus_t *nexus;
123
124     for (nexus = nexus_list ; nexus != NULL ; nexus = nexus->next) {
125         if ((domain == nexus->domain) &&
126             (bus >= nexus->first_bus) && (bus <= nexus->last_bus)) {
127             return nexus;
128         }
129     }
130     return NULL;
131 }
132
133 /*
134  * Release all the resources
135  * Solaris version
136  */
137 static void
138 pci_system_solx_devfs_destroy( void )
139 {
140     /*
141      * The memory allocated for pci_sys & devices in create routines
142      * will be freed in pci_system_cleanup.
143      * Need to free system-specific allocations here.
144      */
145     nexus_t *nexus, *next;
146
147     for (nexus = nexus_list ; nexus != NULL ; nexus = next) {
148         next = nexus->next;
149         close(nexus->fd);
150         free(nexus->path);
151         free(nexus->dev_path);
152         free(nexus);
153     }
154     nexus_list = NULL;
155
156 #ifdef __sparc
157     if (di_phdl != DI_PROM_HANDLE_NIL)
158         (void) di_prom_fini(di_phdl);
159 #else
160     if (xsvc_fd >= 0) {
161         close(xsvc_fd);
162         xsvc_fd = -1;
163     }
164 #endif
165 }
166
167
168 #ifdef __sparc
169 /*
170  * Release resources per device
171  */
172 static void
173 pci_system_solx_devfs_destroy_device( struct pci_device *dev )
174 {
175    if (MAPPING_DEV_PATH(dev))
176         di_devfs_path_free((char *) MAPPING_DEV_PATH(dev));
177 }
178 #endif
179
180
181 static int
182 probe_device_node(di_node_t node, void *arg)
183 {
184     int *retbuf = NULL;
185     int len = 0, i;
186     struct pci_device   *pci_base;
187     probe_info_t *pinfo = ((probe_args_t *)arg)->pinfo;
188     nexus_t *nexus = ((probe_args_t *)arg)->nexus;
189     property_info_t property_list[] = {
190         { "class-code", 0 },
191         { "device-id", 0 },
192         { "vendor-id", 0 },
193         { "revision-id", 0},
194         { "subsystem-vendor-id", 0},
195         { "subsystem-id", 0},
196     };
197 #define NUM_PROPERTIES          sizeof(property_list)/sizeof(property_info_t)
198
199     len = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "reg", &retbuf);
200
201 #ifdef __sparc
202     if ((len <= 0) && di_phdl)
203         len = di_prom_prop_lookup_ints(di_phdl, node, "reg", &retbuf);
204 #endif
205
206     /* Exclude usb devices */
207     if (len < 5) {
208         return DI_WALK_CONTINUE;
209     }
210
211     pci_base = &pinfo->devices[pinfo->num_devices].base;
212
213     pci_base->domain = nexus->domain;
214     pci_base->bus = PCI_REG_BUS_G(retbuf[0]);
215     pci_base->dev = PCI_REG_DEV_G(retbuf[0]);
216     pci_base->func  = PCI_REG_FUNC_G(retbuf[0]);
217
218     /* Get property values */
219     for (i = 0; i < NUM_PROPERTIES; i++) {
220         len = di_prop_lookup_ints(DDI_DEV_T_ANY, node,
221                 property_list[i].name, &retbuf);
222 #ifdef __sparc
223         if ((len <= 0) && di_phdl)
224             len = di_prom_prop_lookup_ints(di_phdl, node,
225                 property_list[i].name, &retbuf);
226 #endif
227
228         if (len > 0)
229             property_list[i].value = retbuf[0];
230         else {
231             /* a device must have property "class-code", "device-id", "vendor-id" */
232             if (i < 3)
233                 return DI_WALK_CONTINUE;
234 #ifdef DEBUG
235             fprintf(stderr, "cannot get property \"%s\" for nexus = %s :\n",
236                 property_list[i].name, nexus->path);
237             fprintf(stderr, "   domain = %x, busno = %x, devno = %x, funcno = %x\n",
238                 pci_base->domain, pci_base->bus, pci_base->dev, pci_base->func);
239 #endif
240         }
241     }
242
243     if ((property_list[1].value == 0) && (property_list[2].value == 0))
244         return DI_WALK_CONTINUE;
245
246     pci_base->device_class = property_list[0].value;
247     pci_base->device_id = property_list[1].value;
248     pci_base->vendor_id = property_list[2].value;
249     pci_base->revision = property_list[3].value;
250     pci_base->subvendor_id = property_list[4].value;
251     pci_base->subdevice_id = property_list[5].value;
252
253 #ifdef DEBUG
254     fprintf(stderr,
255             "nexus = %s, domain = %x, busno = %x, devno = %x, funcno = %x\n",
256             nexus->path, pci_base->domain, pci_base->bus, pci_base->dev, pci_base->func);
257 #endif
258
259     pinfo->num_devices++;
260     if (pinfo->num_devices == pinfo->num_allocated_elems) {
261         struct pci_device_private *new_devs;
262         size_t new_num_elems = pinfo->num_allocated_elems * 2;
263
264         new_devs = realloc(pinfo->devices,
265         new_num_elems * sizeof (struct pci_device_private));
266         if (new_devs == NULL) {
267             (void) fprintf(stderr,
268                    "Error allocating memory for PCI devices:"
269                    " %s\n discarding additional devices\n",
270                    strerror(errno));
271             ((probe_args_t *)arg)->ret = 1;
272             return (DI_WALK_TERMINATE);
273         }
274         (void) memset(&new_devs[pinfo->num_devices], 0,
275                 pinfo->num_allocated_elems *
276                 sizeof (struct pci_device_private));
277         pinfo->num_allocated_elems = new_num_elems;
278         pinfo->devices = new_devs;
279     }
280
281     return (DI_WALK_CONTINUE);
282 }
283 /*
284  * This function is called from di_walk_minor() when any PROBE is processed
285  */
286 static int
287 probe_nexus_node(di_node_t di_node, di_minor_t minor, void *arg)
288 {
289     probe_info_t *pinfo = (probe_info_t *)arg;
290     char *nexus_name, *nexus_dev_path;
291     nexus_t *nexus;
292     int fd;
293     char nexus_path[MAXPATHLEN];
294
295     di_prop_t prop;
296     char *strings;
297     int *ints;
298     int numval;
299     int pci_node = 0;
300     int first_bus = 0, last_bus = PCI_REG_BUS_G(PCI_REG_BUS_M);
301     int domain = 0;
302     di_node_t rnode =  DI_NODE_NIL;
303 #ifdef __sparc
304     int bus_range_found = 0;
305     int device_type_found = 0;
306     di_prom_prop_t prom_prop;
307 #endif
308
309
310 #ifdef DEBUG
311     nexus_name = di_devfs_minor_path(minor);
312     fprintf(stderr, "-- device name: %s\n", nexus_name);
313     di_devfs_path_free(nexus_name);
314 #endif
315
316     for (prop = di_prop_next(di_node, NULL); prop != NULL;
317          prop = di_prop_next(di_node, prop)) {
318
319         const char *prop_name = di_prop_name(prop);
320
321 #ifdef DEBUG
322         fprintf(stderr, "   property: %s\n", prop_name);
323 #endif
324
325         if (strcmp(prop_name, "device_type") == 0) {
326             numval = di_prop_strings(prop, &strings);
327             if (numval == 1) {
328                 if (strncmp(strings, "pci", 3) != 0)
329                     /* not a PCI node, bail */
330                     return (DI_WALK_CONTINUE);
331                 else {
332                     pci_node = 1;
333 #ifdef __sparc
334                     device_type_found =  1;
335 #endif
336                 }
337             }
338         }
339         else if (strcmp(prop_name, "class-code") == 0) {
340             /* not a root bus node, bail */
341             return (DI_WALK_CONTINUE);
342         }
343         else if (strcmp(prop_name, "bus-range") == 0) {
344             numval = di_prop_ints(prop, &ints);
345             if (numval == 2) {
346                 first_bus = ints[0];
347                 last_bus = ints[1];
348 #ifdef __sparc
349                 bus_range_found = 1;
350 #endif
351             }
352         }
353 #ifdef __sparc
354         domain = nexus_count;
355 #else
356         else if (strcmp(prop_name, "pciseg") == 0) {
357             numval = di_prop_ints(prop, &ints);
358             if (numval == 1) {
359                 domain = ints[0];
360             }
361         }
362 #endif
363     }
364
365 #ifdef __sparc
366     if ((!device_type_found) && di_phdl) {
367         numval = di_prom_prop_lookup_strings(di_phdl, di_node,
368             "device_type", &strings);
369         if (numval == 1) {
370             if (strncmp(strings, "pci", 3) != 0)
371                 return (DI_WALK_CONTINUE);
372             else
373                 pci_node = 1;
374         }
375     }
376
377     if ((!bus_range_found) && di_phdl) {
378         numval = di_prom_prop_lookup_ints(di_phdl, di_node,
379             "bus-range", &ints);
380         if (numval == 2) {
381             first_bus = ints[0];
382             last_bus = ints[1];
383         }
384     }
385 #endif
386
387     if (pci_node != 1)
388         return (DI_WALK_CONTINUE);
389
390     /* we have a PCI root bus node. */
391     nexus = calloc(1, sizeof(nexus_t));
392     if (nexus == NULL) {
393         (void) fprintf(stderr, "Error allocating memory for nexus: %s\n",
394                        strerror(errno));
395         return (DI_WALK_TERMINATE);
396     }
397     nexus->first_bus = first_bus;
398     nexus->last_bus = last_bus;
399     nexus->domain = domain;
400
401 #ifdef __sparc
402     nexus_count++;
403 #endif
404
405     nexus_name = di_devfs_minor_path(minor);
406     if (nexus_name == NULL) {
407         (void) fprintf(stderr, "Error getting nexus path: %s\n",
408                        strerror(errno));
409         free(nexus);
410         return (DI_WALK_CONTINUE);
411     }
412
413     snprintf(nexus_path, sizeof(nexus_path), "/devices%s", nexus_name);
414     di_devfs_path_free(nexus_name);
415
416 #ifdef DEBUG
417     fprintf(stderr, "nexus = %s, bus-range = %d - %d\n",
418             nexus_path, first_bus, last_bus);
419 #endif
420
421     if ((fd = open(nexus_path, O_RDWR | O_CLOEXEC)) >= 0) {
422         probe_args_t args;
423
424         nexus->fd = fd;
425         nexus->path = strdup(nexus_path);
426         nexus_dev_path = di_devfs_path(di_node);
427         nexus->dev_path = strdup(nexus_dev_path);
428         di_devfs_path_free(nexus_dev_path);
429
430         if ((rnode = di_init(nexus->dev_path, DINFOCPYALL)) == DI_NODE_NIL) {
431             (void) fprintf(stderr, "di_init failed: %s\n", strerror(errno));
432             close(nexus->fd);
433             free(nexus->path);
434             free(nexus->dev_path);
435             free(nexus);
436             return (DI_WALK_TERMINATE);
437         }
438
439         /* Walk through devices under the rnode */
440         args.pinfo = pinfo;
441         args.nexus = nexus;
442         args.ret = 0;
443
444         (void) di_walk_node(rnode, DI_WALK_CLDFIRST, (void *)&args, probe_device_node);
445         if (args.ret) {
446             close(nexus->fd);
447             free(nexus->path);
448             free(nexus->dev_path);
449             free(nexus);
450             di_fini(rnode);
451             return (DI_WALK_TERMINATE);
452         }
453
454         nexus->next = nexus_list;
455         nexus_list = nexus;
456     } else {
457         (void) fprintf(stderr, "Error opening %s: %s\n",
458                        nexus_path, strerror(errno));
459         free(nexus);
460     }
461
462     if (rnode != DI_NODE_NIL) {
463         di_fini(rnode);
464     }
465
466     return DI_WALK_CONTINUE;
467 }
468
469 static int
470 find_target_node(di_node_t node, void *arg)
471 {
472     int *regbuf = NULL;
473     int len = 0;
474     uint32_t busno, funcno, devno;
475     i_devnode_t *devnode = (i_devnode_t *)arg;
476
477     /*
478      * Test the property functions, only for testing
479      */
480     /*
481     void *prop = DI_PROP_NIL;
482
483     (void) fprintf(stderr, "start of node 0x%x\n", node->nodeid);
484     while ((prop = di_prop_hw_next(node, prop)) != DI_PROP_NIL) {
485         int i;
486         (void) fprintf(stderr, "name=%s: ", di_prop_name(prop));
487         len = 0;
488         if (!strcmp(di_prop_name(prop), "reg")) {
489             len = di_prop_ints(prop, &regbuf);
490         }
491         for (i = 0; i < len; i++) {
492             fprintf(stderr, "0x%0x.", regbuf[i]);
493         }
494         fprintf(stderr, "\n");
495     }
496     (void) fprintf(stderr, "end of node 0x%x\n", node->nodeid);
497     */
498
499     len = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "reg", &regbuf);
500
501 #ifdef __sparc
502     if ((len <= 0) && di_phdl)
503         len = di_prom_prop_lookup_ints(di_phdl, node, "reg", &regbuf);
504 #endif
505
506     if (len <= 0) {
507 #ifdef DEBUG
508         fprintf(stderr, "error = %x\n", errno);
509         fprintf(stderr, "can not find assigned-address\n");
510 #endif
511         return (DI_WALK_CONTINUE);
512     }
513
514     busno = PCI_REG_BUS_G(regbuf[0]);
515     devno = PCI_REG_DEV_G(regbuf[0]);
516     funcno = PCI_REG_FUNC_G(regbuf[0]);
517
518     if ((busno == devnode->bus) &&
519         (devno == devnode->dev) &&
520         (funcno == devnode->func)) {
521         devnode->node = node;
522
523         return (DI_WALK_TERMINATE);
524     }
525
526     return (DI_WALK_CONTINUE);
527 }
528
529 /*
530  * Solaris version
531  */
532 static int
533 pci_device_solx_devfs_probe( struct pci_device * dev )
534 {
535     int err = 0;
536     di_node_t rnode = DI_NODE_NIL;
537     i_devnode_t args = { 0, 0, 0, DI_NODE_NIL };
538     int *regbuf;
539     pci_regspec_t *reg;
540     int i;
541     int len = 0;
542     uint ent = 0;
543     struct pci_device_private *priv =
544         (struct pci_device_private *) dev;
545     nexus_t *nexus;
546
547     if ( (nexus = find_nexus_for_bus(dev->domain, dev->bus)) == NULL )
548         return ENODEV;
549
550     pci_device_cfg_read_u8(dev, &priv->header_type, PCI_CONF_HEADER);
551
552     pci_device_cfg_read_u8(dev, (uint8_t *)&dev->irq, PCI_CONF_ILINE);
553
554     /*
555      * starting to find if it is MEM/MEM64/IO
556      * using libdevinfo
557      */
558     if ((rnode = di_init(nexus->dev_path, DINFOCPYALL)) == DI_NODE_NIL) {
559         err = errno;
560         (void) fprintf(stderr, "di_init failed: %s\n", strerror(errno));
561     } else {
562         args.bus = dev->bus;
563         args.dev = dev->dev;
564         args.func = dev->func;
565         (void) di_walk_node(rnode, DI_WALK_CLDFIRST,
566                 (void *)&args, find_target_node);
567     }
568
569     if (args.node != DI_NODE_NIL) {
570         int *prop;
571 #ifdef __sparc
572         di_minor_t minor;
573 #endif
574
575         priv->is_primary = 0;
576
577 #ifdef __sparc
578         if (minor = di_minor_next(args.node, DI_MINOR_NIL))
579             MAPPING_DEV_PATH(dev) = di_devfs_minor_path (minor);
580         else
581             MAPPING_DEV_PATH(dev) = NULL;
582 #endif
583
584         if (di_prop_lookup_ints(DDI_DEV_T_ANY, args.node,
585                                 "primary-controller", &prop) >= 1) {
586             if (prop[0])
587                 priv->is_primary = 1;
588         }
589
590         /*
591          * It will succeed for sure, because it was
592          * successfully called in find_target_node
593          */
594         len = di_prop_lookup_ints(DDI_DEV_T_ANY, args.node,
595                                   "assigned-addresses",
596                                   &regbuf);
597
598 #ifdef __sparc
599         if ((len <= 0) && di_phdl) {
600             len = di_prom_prop_lookup_ints(di_phdl, args.node,
601                                 "assigned-addresses", &regbuf);
602         }
603 #endif
604     }
605
606     if (len <= 0)
607         goto cleanup;
608
609     /*
610      * Each BAR address get its own region slot in sequence.
611      * 32 bit BAR:
612      * BAR 0x10 -> slot0, BAR 0x14 -> slot1...
613      * 64 bit BAR:
614      * BAR 0x10 -> slot0, BAR 0x18 -> slot2...,
615      * slot1 is part of BAR 0x10
616      * Linux give two region slot for 64 bit address.
617      */
618     for (i = 0; i < len; i = i + (int)CELL_NUMS_1275) {
619
620         reg = (pci_regspec_t *)&regbuf[i];
621         ent = reg->pci_phys_hi & 0xff;
622
623         if (ent > PCI_CONF_ROM) {
624             fprintf(stderr, "error ent = %d\n", ent);
625             break;
626         }
627         /*
628          * G35 broken in BAR0
629          */
630         if (ent < PCI_CONF_BASE0) {
631             /*
632              * VGA resource here and ignore it
633              */
634             break;
635         } else if (ent == PCI_CONF_ROM) {
636             priv->rom_base = reg->pci_phys_low |
637                 ((uint64_t)reg->pci_phys_mid << 32);
638             dev->rom_size = reg->pci_size_low;
639         } else {
640             ent = (ent - PCI_CONF_BASE0) >> 2;
641             /*
642              * non relocatable resource is excluded
643              * such like 0xa0000, 0x3b0. If it is met,
644              * the loop is broken;
645              */
646             if (!PCI_REG_REG_G(reg->pci_phys_hi))
647                 break;
648
649             if (reg->pci_phys_hi & PCI_PREFETCH_B) {
650                 dev->regions[ent].is_prefetchable = 1;
651             }
652
653
654             dev->regions[ent].base_addr = reg->pci_phys_low |
655                 ((uint64_t)reg->pci_phys_mid << 32);
656             dev->regions[ent].size = reg->pci_size_low |
657                 ((uint64_t)reg->pci_size_hi << 32);
658
659             switch (reg->pci_phys_hi & PCI_REG_ADDR_M) {
660                 case PCI_ADDR_IO:
661                     dev->regions[ent].is_IO = 1;
662                     break;
663                 case PCI_ADDR_MEM32:
664                     break;
665                 case PCI_ADDR_MEM64:
666                     dev->regions[ent].is_64 = 1;
667                     /*
668                      * Skip one slot for 64 bit address
669                      */
670                     break;
671             }
672         }
673     }
674
675   cleanup:
676     if (rnode != DI_NODE_NIL) {
677         di_fini(rnode);
678     }
679     return (err);
680 }
681
682 /**
683  * Map a memory region for a device using /dev/xsvc (x86) or fb device (sparc)
684  *
685  * \param dev   Device whose memory region is to be mapped.
686  * \param map   Parameters of the mapping that is to be created.
687  *
688  * \return
689  * Zero on success or an \c errno value on failure.
690  */
691 static int
692 pci_device_solx_devfs_map_range(struct pci_device *dev,
693                                 struct pci_device_mapping *map)
694 {
695     const int prot = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
696                         ? (PROT_READ | PROT_WRITE) : PROT_READ;
697     int err = 0;
698
699     const char *map_dev;
700     int         map_fd;
701
702 #ifdef __sparc
703     char        map_dev_buf[128];
704
705     if (MAPPING_DEV_PATH(dev)) {
706         snprintf(map_dev_buf, sizeof (map_dev_buf), "%s%s",
707                  "/devices", MAPPING_DEV_PATH(dev));
708         map_dev = map_dev_buf;
709     }
710     else
711         map_dev = "/dev/fb0";
712
713     map_fd = -1;
714 #else
715     /*
716      * Still uses xsvc to do the user space mapping on x86/x64,
717      * caches open fd across multiple calls.
718      */
719     map_dev = "/dev/xsvc";
720     map_fd = xsvc_fd;
721 #endif
722
723     if (map_fd < 0) {
724         if ((map_fd = open(map_dev, O_RDWR | O_CLOEXEC)) < 0) {
725             err = errno;
726             (void) fprintf(stderr, "can not open %s: %s\n", map_dev,
727                            strerror(errno));
728             return err;
729         }
730 #ifndef __sparc
731         xsvc_fd = map_fd;
732 #endif
733     }
734
735     map->memory = mmap(NULL, map->size, prot, MAP_SHARED, map_fd, map->base);
736     if (map->memory == MAP_FAILED) {
737         err = errno;
738
739         (void) fprintf(stderr, "map rom region =%llx failed: %s\n",
740                        (unsigned long long) map->base, strerror(errno));
741     }
742
743 #ifdef __sparc
744     close (map_fd);
745 #endif
746
747     return err;
748 }
749
750 /*
751  * Solaris version: read the VGA ROM data
752  */
753 static int
754 pci_device_solx_devfs_read_rom( struct pci_device * dev, void * buffer )
755 {
756     int err;
757     struct pci_device_mapping prom = {
758         .base = 0xC0000,
759         .size = 0x10000,
760         .flags = 0
761     };
762     struct pci_device_private *priv =
763         (struct pci_device_private *) dev;
764
765     if (priv->rom_base) {
766         prom.base = priv->rom_base;
767         prom.size = dev->rom_size;
768     }
769
770     err = pci_device_solx_devfs_map_range(dev, &prom);
771     if (err == 0) {
772         (void) bcopy(prom.memory, buffer, dev->rom_size);
773
774         if (munmap(prom.memory, prom.size) == -1) {
775             err = errno;
776         }
777     }
778     return err;
779 }
780
781 /*
782  * solaris version: Read the configurations space of the devices
783  */
784 static int
785 pci_device_solx_devfs_read( struct pci_device * dev, void * data,
786                              pciaddr_t offset, pciaddr_t size,
787                              pciaddr_t * bytes_read )
788 {
789     pcitool_reg_t cfg_prg;
790     int err = 0;
791     unsigned int i = 0;
792     nexus_t *nexus;
793
794     nexus = find_nexus_for_bus(dev->domain, dev->bus);
795
796     *bytes_read = 0;
797
798     if ( nexus == NULL ) {
799         return ENODEV;
800     }
801
802     cfg_prg.offset = offset;
803     cfg_prg.acc_attr = PCITOOL_ACC_ATTR_SIZE_1 + NATIVE_ENDIAN;
804     cfg_prg.bus_no = dev->bus;
805     cfg_prg.dev_no = dev->dev;
806     cfg_prg.func_no = dev->func;
807     cfg_prg.barnum = 0;
808     cfg_prg.user_version = PCITOOL_USER_VERSION;
809
810     for (i = 0; i < size; i += PCITOOL_ACC_ATTR_SIZE(PCITOOL_ACC_ATTR_SIZE_1))
811     {
812         cfg_prg.offset = offset + i;
813
814         if ((err = ioctl(nexus->fd, PCITOOL_DEVICE_GET_REG, &cfg_prg)) != 0) {
815             fprintf(stderr, "read bdf<%s,%x,%x,%x,%llx> config space failure\n",
816                     nexus->path,
817                     cfg_prg.bus_no,
818                     cfg_prg.dev_no,
819                     cfg_prg.func_no,
820                     (unsigned long long) cfg_prg.offset);
821             fprintf(stderr, "Failure cause = %x\n", err);
822             break;
823         }
824
825         ((uint8_t *)data)[i] = (uint8_t)cfg_prg.data;
826         /*
827          * DWORDS Offset or bytes Offset ??
828          */
829     }
830     *bytes_read = i;
831
832     return (err);
833 }
834
835 /*
836  * Solaris version
837  */
838 static int
839 pci_device_solx_devfs_write( struct pci_device * dev, const void * data,
840                              pciaddr_t offset, pciaddr_t size,
841                              pciaddr_t * bytes_written )
842 {
843     pcitool_reg_t cfg_prg;
844     int err = 0;
845     int cmd;
846     nexus_t *nexus;
847
848     nexus = find_nexus_for_bus(dev->domain, dev->bus);
849
850     if ( bytes_written != NULL ) {
851         *bytes_written = 0;
852     }
853
854     if ( nexus == NULL ) {
855         return ENODEV;
856     }
857
858     cfg_prg.offset = offset;
859     switch (size) {
860         case 1:
861             cfg_prg.acc_attr = PCITOOL_ACC_ATTR_SIZE_1 + NATIVE_ENDIAN;
862             cfg_prg.data = *((const uint8_t *)data);
863             break;
864         case 2:
865             cfg_prg.acc_attr = PCITOOL_ACC_ATTR_SIZE_2 + NATIVE_ENDIAN;
866             cfg_prg.data = *((const uint16_t *)data);
867             break;
868         case 4:
869             cfg_prg.acc_attr = PCITOOL_ACC_ATTR_SIZE_4 + NATIVE_ENDIAN;
870             cfg_prg.data = *((const uint32_t *)data);
871             break;
872         case 8:
873             cfg_prg.acc_attr = PCITOOL_ACC_ATTR_SIZE_8 + NATIVE_ENDIAN;
874             cfg_prg.data = *((const uint64_t *)data);
875             break;
876         default:
877             return EINVAL;
878     }
879     cfg_prg.bus_no = dev->bus;
880     cfg_prg.dev_no = dev->dev;
881     cfg_prg.func_no = dev->func;
882     cfg_prg.barnum = 0;
883     cfg_prg.user_version = PCITOOL_USER_VERSION;
884
885     /*
886      * Check if this device is bridge device.
887      * If it is, it is also a nexus node???
888      * It seems that there is no explicit
889      * PCI nexus device for X86, so not applicable
890      * from pcitool_bus_reg_ops in pci_tools.c
891      */
892     cmd = PCITOOL_DEVICE_SET_REG;
893
894     if ((err = ioctl(nexus->fd, cmd, &cfg_prg)) != 0) {
895         return (err);
896     }
897     *bytes_written = size;
898
899     return (err);
900 }
901
902 static int pci_device_solx_devfs_boot_vga(struct pci_device *dev)
903 {
904     struct pci_device_private *priv =
905         (struct pci_device_private *) dev;
906
907     return (priv->is_primary);
908
909 }
910
911 static struct pci_io_handle *
912 pci_device_solx_devfs_open_legacy_io(struct pci_io_handle *ret,
913                                      struct pci_device *dev,
914                                      pciaddr_t base, pciaddr_t size)
915 {
916 #ifdef __x86
917     if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) == 0) {
918         ret->base = base;
919         ret->size = size;
920         ret->is_legacy = 1;
921         return ret;
922     }
923 #endif
924     return NULL;
925 }
926
927 static uint32_t
928 pci_device_solx_devfs_read32(struct pci_io_handle *handle, uint32_t reg)
929 {
930 #ifdef __x86
931     uint16_t port = (uint16_t) (handle->base + reg);
932     uint32_t ret;
933     __asm__ __volatile__("inl %1,%0":"=a"(ret):"d"(port));
934     return ret;
935 #else
936     return *(uint32_t *)((uintptr_t)handle->memory + reg);
937 #endif
938 }
939
940 static uint16_t
941 pci_device_solx_devfs_read16(struct pci_io_handle *handle, uint32_t reg)
942 {
943 #ifdef __x86
944     uint16_t port = (uint16_t) (handle->base + reg);
945     uint16_t ret;
946     __asm__ __volatile__("inw %1,%0":"=a"(ret):"d"(port));
947     return ret;
948 #else
949     return *(uint16_t *)((uintptr_t)handle->memory + reg);
950 #endif
951 }
952
953 static uint8_t
954 pci_device_solx_devfs_read8(struct pci_io_handle *handle, uint32_t reg)
955 {
956 #ifdef __x86
957     uint16_t port = (uint16_t) (handle->base + reg);
958     uint8_t ret;
959     __asm__ __volatile__("inb %1,%0":"=a"(ret):"d"(port));
960     return ret;
961 #else
962     return *(uint8_t *)((uintptr_t)handle->memory + reg);
963 #endif
964 }
965
966 static void
967 pci_device_solx_devfs_write32(struct pci_io_handle *handle, uint32_t reg,
968     uint32_t data)
969 {
970 #ifdef __x86
971       uint16_t port = (uint16_t) (handle->base + reg);
972       __asm__ __volatile__("outl %0,%1"::"a"(data), "d"(port));
973 #else
974       *(uint16_t *)((uintptr_t)handle->memory + reg) = data;
975 #endif
976 }
977
978 static void
979 pci_device_solx_devfs_write16(struct pci_io_handle *handle, uint32_t reg,
980     uint16_t data)
981 {
982 #ifdef __x86
983       uint16_t port = (uint16_t) (handle->base + reg);
984       __asm__ __volatile__("outw %0,%1"::"a"(data), "d"(port));
985 #else
986     *(uint8_t *)((uintptr_t)handle->memory + reg) = data;
987 #endif
988 }
989
990 static void
991 pci_device_solx_devfs_write8(struct pci_io_handle *handle, uint32_t reg,
992     uint8_t data)
993 {
994 #ifdef __x86
995       uint16_t port = (uint16_t) (handle->base + reg);
996       __asm__ __volatile__("outb %0,%1"::"a"(data), "d"(port));
997 #else
998       *(uint32_t *)((uintptr_t)handle->memory + reg) = data;
999 #endif
1000 }
1001
1002 static int
1003 pci_device_solx_devfs_map_legacy(struct pci_device *dev, pciaddr_t base,
1004                                  pciaddr_t size, unsigned map_flags,
1005                                  void **addr)
1006 {
1007     int err;
1008     struct pci_device_mapping map = {
1009         .base = base,
1010         .size = size,
1011         .flags = map_flags,
1012     };
1013
1014     err = pci_device_solx_devfs_map_range(dev, &map);
1015     if (err == 0)
1016         *addr = map.memory;
1017     return err;
1018 }
1019
1020 static int
1021 pci_device_solx_devfs_unmap_legacy(struct pci_device *dev,
1022                                    void *addr, pciaddr_t size)
1023 {
1024     struct pci_device_mapping map = {
1025         .memory = addr,
1026         .size = size,
1027     };
1028
1029     return pci_device_generic_unmap_range(dev, &map);
1030 }
1031
1032 static const struct pci_system_methods solx_devfs_methods = {
1033     .destroy = pci_system_solx_devfs_destroy,
1034 #ifdef __sparc
1035     .destroy_device = pci_system_solx_devfs_destroy_device,
1036 #else
1037     .destroy_device = NULL,
1038 #endif
1039     .read_rom = pci_device_solx_devfs_read_rom,
1040     .probe = pci_device_solx_devfs_probe,
1041     .map_range = pci_device_solx_devfs_map_range,
1042     .unmap_range = pci_device_generic_unmap_range,
1043
1044     .read = pci_device_solx_devfs_read,
1045     .write = pci_device_solx_devfs_write,
1046
1047     .fill_capabilities = pci_fill_capabilities_generic,
1048     .boot_vga = pci_device_solx_devfs_boot_vga,
1049
1050     .open_legacy_io = pci_device_solx_devfs_open_legacy_io,
1051     .read32 = pci_device_solx_devfs_read32,
1052     .read16 = pci_device_solx_devfs_read16,
1053     .read8 = pci_device_solx_devfs_read8,
1054     .write32 = pci_device_solx_devfs_write32,
1055     .write16 = pci_device_solx_devfs_write16,
1056     .write8 = pci_device_solx_devfs_write8,
1057     .map_legacy = pci_device_solx_devfs_map_legacy,
1058     .unmap_legacy = pci_device_solx_devfs_unmap_legacy,
1059 };
1060
1061 /*
1062  * Attempt to access PCI subsystem using Solaris's devfs interface.
1063  * Solaris version
1064  */
1065 _pci_hidden int
1066 pci_system_solx_devfs_create( void )
1067 {
1068     int err = 0;
1069     di_node_t di_node;
1070     probe_info_t pinfo;
1071     struct pci_device_private *devices;
1072
1073     if (nexus_list != NULL) {
1074         return 0;
1075     }
1076
1077     if ((di_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
1078         err = errno;
1079         (void) fprintf(stderr, "di_init() failed: %s\n",
1080                        strerror(errno));
1081         return (err);
1082     }
1083
1084     if ((devices = calloc(INITIAL_NUM_DEVICES,
1085                         sizeof (struct pci_device_private))) == NULL) {
1086         err = errno;
1087         di_fini(di_node);
1088         return (err);
1089     }
1090
1091 #ifdef __sparc
1092     if ((di_phdl = di_prom_init()) == DI_PROM_HANDLE_NIL)
1093         (void) fprintf(stderr, "di_prom_init failed: %s\n", strerror(errno));
1094 #endif
1095
1096     pinfo.num_allocated_elems = INITIAL_NUM_DEVICES;
1097     pinfo.num_devices = 0;
1098     pinfo.devices = devices;
1099 #ifdef __sparc
1100     nexus_count = 0;
1101 #endif
1102     (void) di_walk_minor(di_node, DDI_NT_REGACC, 0, &pinfo, probe_nexus_node);
1103
1104     di_fini(di_node);
1105
1106     if ((pci_sys = calloc(1, sizeof (struct pci_system))) == NULL) {
1107         err = errno;
1108         free(devices);
1109         return (err);
1110     }
1111
1112     pci_sys->methods = &solx_devfs_methods;
1113     pci_sys->devices = pinfo.devices;
1114     pci_sys->num_devices = pinfo.num_devices;
1115
1116     return (err);
1117 }