xref: /libpciaccess/src/solx_devfs.c (revision cfae4096)
1206e2921Sedward shu /*
2110cdac9SAlan Coopersmith  * (C) Copyright IBM Corporation 2006
3a18460b3SJohn Martin  * Copyright (c) 2007, 2009, 2011, Oracle and/or its affiliates.
4206e2921Sedward shu  * All Rights Reserved.
5206e2921Sedward shu  *
6206e2921Sedward shu  * Permission is hereby granted, free of charge, to any person obtaining a
7206e2921Sedward shu  * copy of this software and associated documentation files (the "Software"),
8206e2921Sedward shu  * to deal in the Software without restriction, including without limitation
9206e2921Sedward shu  * on the rights to use, copy, modify, merge, publish, distribute, sub
10206e2921Sedward shu  * license, and/or sell copies of the Software, and to permit persons to whom
11206e2921Sedward shu  * the Software is furnished to do so, subject to the following conditions:
12206e2921Sedward shu  *
13206e2921Sedward shu  * The above copyright notice and this permission notice (including the next
14206e2921Sedward shu  * paragraph) shall be included in all copies or substantial portions of the
15206e2921Sedward shu  * Software.
16206e2921Sedward shu  *
17206e2921Sedward shu  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18206e2921Sedward shu  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19206e2921Sedward shu  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
20206e2921Sedward shu  * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21206e2921Sedward shu  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22206e2921Sedward shu  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23206e2921Sedward shu  * DEALINGS IN THE SOFTWARE.
24206e2921Sedward shu  */
25110cdac9SAlan Coopersmith /*
26206e2921Sedward shu  * Solaris devfs interfaces
27206e2921Sedward shu  */
28206e2921Sedward shu 
29206e2921Sedward shu #include <stdlib.h>
30206e2921Sedward shu #include <strings.h>
31206e2921Sedward shu #include <stdio.h>
32206e2921Sedward shu #include <unistd.h>
33206e2921Sedward shu #include <sys/types.h>
34206e2921Sedward shu #include <fcntl.h>
35206e2921Sedward shu #include <sys/mman.h>
36206e2921Sedward shu #include <errno.h>
37206e2921Sedward shu #include <sys/pci.h>
38206e2921Sedward shu #include <libdevinfo.h>
39206e2921Sedward shu #include "pci_tools.h"
40206e2921Sedward shu 
41206e2921Sedward shu #include "pciaccess.h"
42206e2921Sedward shu #include "pciaccess_private.h"
43206e2921Sedward shu 
445bf4b32cSAlan Coopersmith /* #define DEBUG */
455bf4b32cSAlan Coopersmith 
46a18460b3SJohn Martin #define	INITIAL_NUM_DEVICES	256
47206e2921Sedward shu #define	CELL_NUMS_1275	(sizeof(pci_regspec_t) / sizeof(uint_t))
489a5565c7SAlan Coopersmith 
49206e2921Sedward shu typedef union {
50206e2921Sedward shu     uint8_t bytes[16 * sizeof (uint32_t)];
51206e2921Sedward shu     uint32_t dwords[16];
52206e2921Sedward shu } pci_conf_hdr_t;
53206e2921Sedward shu 
54206e2921Sedward shu typedef struct i_devnode {
55206e2921Sedward shu     uint8_t bus;
56206e2921Sedward shu     uint8_t dev;
57206e2921Sedward shu     uint8_t func;
58206e2921Sedward shu     di_node_t node;
59206e2921Sedward shu } i_devnode_t;
60206e2921Sedward shu 
61de97e7e4SAlan Coopersmith typedef struct nexus {
62de97e7e4SAlan Coopersmith     int fd;
635bf4b32cSAlan Coopersmith     int first_bus;
645bf4b32cSAlan Coopersmith     int last_bus;
65a18460b3SJohn Martin     int domain;
66c56da48eSAlan Coopersmith     char *path;			/* for errors/debugging; fd is all we need */
67a18460b3SJohn Martin     char *dev_path;
68de97e7e4SAlan Coopersmith     struct nexus *next;
69*cfae4096SHenry Zhao #ifdef __sparc
70*cfae4096SHenry Zhao     struct pci_device **devlist;
71*cfae4096SHenry Zhao     volatile size_t num_allocated_elems;
72*cfae4096SHenry Zhao     volatile size_t num_devices;
73*cfae4096SHenry Zhao #endif
74de97e7e4SAlan Coopersmith } nexus_t;
75de97e7e4SAlan Coopersmith 
76a18460b3SJohn Martin typedef struct probe_info {
77a18460b3SJohn Martin     volatile size_t num_allocated_elems;
78a18460b3SJohn Martin     volatile size_t num_devices;
79a18460b3SJohn Martin     struct pci_device_private * volatile devices;
80a18460b3SJohn Martin } probe_info_t;
81a18460b3SJohn Martin 
82de97e7e4SAlan Coopersmith static nexus_t *nexus_list = NULL;
83*cfae4096SHenry Zhao #if !defined(__sparc)
84206e2921Sedward shu static int xsvc_fd = -1;
85*cfae4096SHenry Zhao #endif
86*cfae4096SHenry Zhao 
87*cfae4096SHenry Zhao #ifdef __sparc
88*cfae4096SHenry Zhao static di_prom_handle_t di_phdl;
89*cfae4096SHenry Zhao #endif
909a5565c7SAlan Coopersmith 
91206e2921Sedward shu /*
92206e2921Sedward shu  * Read config space in native processor endianness.  Endian-neutral
93206e2921Sedward shu  * processing can then take place.  On big endian machines, MSB and LSB
94206e2921Sedward shu  * of little endian data end up switched if read as little endian.
95206e2921Sedward shu  * They are in correct order if read as big endian.
96206e2921Sedward shu  */
97206e2921Sedward shu #if defined(__sparc)
98206e2921Sedward shu # define NATIVE_ENDIAN	PCITOOL_ACC_ATTR_ENDN_BIG
99206e2921Sedward shu #elif defined(__x86)
100206e2921Sedward shu # define NATIVE_ENDIAN	PCITOOL_ACC_ATTR_ENDN_LTL
101206e2921Sedward shu #else
102206e2921Sedward shu # error "ISA is neither __sparc nor __x86"
103206e2921Sedward shu #endif
104206e2921Sedward shu 
105*cfae4096SHenry Zhao #ifdef __sparc
106*cfae4096SHenry Zhao #define MAPPING_DEV_PATH(dev)	 (((struct pci_device_private *) dev)->device_string)
107*cfae4096SHenry Zhao #endif
108*cfae4096SHenry Zhao 
109206e2921Sedward shu /*
110206e2921Sedward shu  * Identify problematic southbridges.  These have device id 0x5249 and
111206e2921Sedward shu  * vendor id 0x10b9.  Check for revision ID 0 and class code 060400 as well.
112206e2921Sedward shu  * Values are little endian, so they are reversed for SPARC.
113206e2921Sedward shu  *
114206e2921Sedward shu  * Check for these southbridges on all architectures, as the issue is a
115206e2921Sedward shu  * southbridge issue, independent of processor.
116206e2921Sedward shu  *
117206e2921Sedward shu  * If one of these is found during probing, skip probing other devs/funcs on
118206e2921Sedward shu  * the rest of the bus, since the southbridge and all devs underneath will
119206e2921Sedward shu  * otherwise disappear.
120206e2921Sedward shu  */
121206e2921Sedward shu #if (NATIVE_ENDIAN == PCITOOL_ACC_ATTR_ENDN_BIG)
122206e2921Sedward shu # define U45_SB_DEVID_VID	0xb9104952
123206e2921Sedward shu # define U45_SB_CLASS_RID	0x00000406
124206e2921Sedward shu #else
125206e2921Sedward shu # define U45_SB_DEVID_VID	0x524910b9
126206e2921Sedward shu # define U45_SB_CLASS_RID	0x06040000
127206e2921Sedward shu #endif
128206e2921Sedward shu 
12963983e23SAlan Coopersmith static int pci_device_solx_devfs_map_range(struct pci_device *dev,
13063983e23SAlan Coopersmith     struct pci_device_mapping *map);
131206e2921Sedward shu 
132206e2921Sedward shu static int pci_device_solx_devfs_read_rom( struct pci_device * dev,
133206e2921Sedward shu     void * buffer );
134206e2921Sedward shu 
135206e2921Sedward shu static int pci_device_solx_devfs_probe( struct pci_device * dev );
136206e2921Sedward shu 
137206e2921Sedward shu static int pci_device_solx_devfs_read( struct pci_device * dev, void * data,
138206e2921Sedward shu     pciaddr_t offset, pciaddr_t size, pciaddr_t * bytes_read );
139206e2921Sedward shu 
140206e2921Sedward shu static int pci_device_solx_devfs_write( struct pci_device * dev,
141206e2921Sedward shu     const void * data, pciaddr_t offset, pciaddr_t size,
142d43d21c8SAlan Coopersmith     pciaddr_t * bytes_written );
143206e2921Sedward shu 
144a18460b3SJohn Martin static int probe_dev(nexus_t *nexus, pcitool_reg_t *prg_p, probe_info_t *pinfo);
145206e2921Sedward shu 
146a18460b3SJohn Martin static int do_probe(nexus_t *nexus, probe_info_t *pinfo);
147de97e7e4SAlan Coopersmith 
1489a5565c7SAlan Coopersmith static int probe_nexus_node(di_node_t di_node, di_minor_t minor, void *arg);
149206e2921Sedward shu 
1509a5565c7SAlan Coopersmith static void pci_system_solx_devfs_destroy( void );
151206e2921Sedward shu 
1529a5565c7SAlan Coopersmith static int get_config_header(int fd, uint8_t bus_no, uint8_t dev_no,
1539a5565c7SAlan Coopersmith 			     uint8_t func_no, pci_conf_hdr_t *config_hdr_p);
154206e2921Sedward shu 
1559a5565c7SAlan Coopersmith int pci_system_solx_devfs_create( void );
156206e2921Sedward shu 
157206e2921Sedward shu static const struct pci_system_methods solx_devfs_methods = {
158206e2921Sedward shu     .destroy = pci_system_solx_devfs_destroy,
159206e2921Sedward shu     .destroy_device = NULL,
160206e2921Sedward shu     .read_rom = pci_device_solx_devfs_read_rom,
161206e2921Sedward shu     .probe = pci_device_solx_devfs_probe,
16208ff9f7fSIan Romanick     .map_range = pci_device_solx_devfs_map_range,
16308ff9f7fSIan Romanick     .unmap_range = pci_device_generic_unmap_range,
164206e2921Sedward shu 
165206e2921Sedward shu     .read = pci_device_solx_devfs_read,
166206e2921Sedward shu     .write = pci_device_solx_devfs_write,
167206e2921Sedward shu 
168206e2921Sedward shu     .fill_capabilities = pci_fill_capabilities_generic
169206e2921Sedward shu };
170206e2921Sedward shu 
171*cfae4096SHenry Zhao #ifdef __sparc
172*cfae4096SHenry Zhao static nexus_t *
find_nexus_for_dev(struct pci_device * dev)173*cfae4096SHenry Zhao find_nexus_for_dev(struct pci_device *dev)
174*cfae4096SHenry Zhao {
175*cfae4096SHenry Zhao     nexus_t *nexus;
176*cfae4096SHenry Zhao     int i;
177*cfae4096SHenry Zhao 
178*cfae4096SHenry Zhao     for (nexus = nexus_list ; nexus != NULL ; nexus = nexus->next) {
179*cfae4096SHenry Zhao 	for (i = 0; i < nexus->num_devices; i++) {
180*cfae4096SHenry Zhao 	    if (nexus->devlist[i] == dev)
181*cfae4096SHenry Zhao 		return nexus;
182*cfae4096SHenry Zhao 	}
183*cfae4096SHenry Zhao     }
184*cfae4096SHenry Zhao     return NULL;
185*cfae4096SHenry Zhao }
186*cfae4096SHenry Zhao #else
187de97e7e4SAlan Coopersmith static nexus_t *
find_nexus_for_bus(int domain,int bus)188a18460b3SJohn Martin find_nexus_for_bus( int domain, int bus )
189de97e7e4SAlan Coopersmith {
190de97e7e4SAlan Coopersmith     nexus_t *nexus;
191de97e7e4SAlan Coopersmith 
192de97e7e4SAlan Coopersmith     for (nexus = nexus_list ; nexus != NULL ; nexus = nexus->next) {
193a18460b3SJohn Martin 	if ((domain == nexus->domain) &&
194a18460b3SJohn Martin 	    (bus >= nexus->first_bus) && (bus <= nexus->last_bus)) {
195de97e7e4SAlan Coopersmith 	    return nexus;
196de97e7e4SAlan Coopersmith 	}
197de97e7e4SAlan Coopersmith     }
198de97e7e4SAlan Coopersmith     return NULL;
199de97e7e4SAlan Coopersmith }
200*cfae4096SHenry Zhao #endif
201de97e7e4SAlan Coopersmith 
202f537fc50SAlan Coopersmith #define GET_CONFIG_VAL_8(offset) (config_hdr.bytes[offset])
203de97e7e4SAlan Coopersmith #define GET_CONFIG_VAL_16(offset) \
204f537fc50SAlan Coopersmith     (uint16_t) (GET_CONFIG_VAL_8(offset) + (GET_CONFIG_VAL_8(offset+1) << 8))
205de97e7e4SAlan Coopersmith #define GET_CONFIG_VAL_32(offset) \
206f537fc50SAlan Coopersmith     (uint32_t) (GET_CONFIG_VAL_8(offset) + 		\
207f537fc50SAlan Coopersmith 		(GET_CONFIG_VAL_8(offset+1) << 8) +	\
208f537fc50SAlan Coopersmith 		(GET_CONFIG_VAL_8(offset+2) << 16) +	\
209f537fc50SAlan Coopersmith 		(GET_CONFIG_VAL_8(offset+3) << 24))
210de97e7e4SAlan Coopersmith 
211206e2921Sedward shu /*
212d43d21c8SAlan Coopersmith  * Release all the resources
213206e2921Sedward shu  * Solaris version
214206e2921Sedward shu  */
215206e2921Sedward shu static void
pci_system_solx_devfs_destroy(void)216206e2921Sedward shu pci_system_solx_devfs_destroy( void )
217206e2921Sedward shu {
218206e2921Sedward shu     /*
219f537fc50SAlan Coopersmith      * The memory allocated for pci_sys & devices in create routines
220f537fc50SAlan Coopersmith      * will be freed in pci_system_cleanup.
221f537fc50SAlan Coopersmith      * Need to free system-specific allocations here.
222206e2921Sedward shu      */
223de97e7e4SAlan Coopersmith     nexus_t *nexus, *next;
2249a5565c7SAlan Coopersmith 
225de97e7e4SAlan Coopersmith     for (nexus = nexus_list ; nexus != NULL ; nexus = next) {
226de97e7e4SAlan Coopersmith 	next = nexus->next;
227de97e7e4SAlan Coopersmith 	close(nexus->fd);
2285bf4b32cSAlan Coopersmith 	free(nexus->path);
229a18460b3SJohn Martin 	free(nexus->dev_path);
230*cfae4096SHenry Zhao #ifdef __sparc
231*cfae4096SHenry Zhao 	{
232*cfae4096SHenry Zhao 	    struct pci_device *dev;
233*cfae4096SHenry Zhao 	    int i;
234*cfae4096SHenry Zhao 
235*cfae4096SHenry Zhao 	    for (i = 0; i < nexus->num_devices; i++) {
236*cfae4096SHenry Zhao 		dev = nexus->devlist[i];
237*cfae4096SHenry Zhao 		if (MAPPING_DEV_PATH(dev))
238*cfae4096SHenry Zhao 		    di_devfs_path_free((char *) MAPPING_DEV_PATH(dev));
239*cfae4096SHenry Zhao 	    }
240*cfae4096SHenry Zhao 	}
241*cfae4096SHenry Zhao 	free(nexus->devlist);
242*cfae4096SHenry Zhao #endif
243de97e7e4SAlan Coopersmith 	free(nexus);
244206e2921Sedward shu     }
245de97e7e4SAlan Coopersmith     nexus_list = NULL;
246206e2921Sedward shu 
247*cfae4096SHenry Zhao #ifdef __sparc
248*cfae4096SHenry Zhao     if (di_phdl != DI_PROM_HANDLE_NIL)
249*cfae4096SHenry Zhao 	(void) di_prom_fini(di_phdl);
250*cfae4096SHenry Zhao #else
251206e2921Sedward shu     if (xsvc_fd >= 0) {
252206e2921Sedward shu 	close(xsvc_fd);
253206e2921Sedward shu 	xsvc_fd = -1;
254206e2921Sedward shu     }
255*cfae4096SHenry Zhao #endif
256206e2921Sedward shu }
257de97e7e4SAlan Coopersmith 
258206e2921Sedward shu /*
259206e2921Sedward shu  * Attempt to access PCI subsystem using Solaris's devfs interface.
260206e2921Sedward shu  * Solaris version
261206e2921Sedward shu  */
262f537fc50SAlan Coopersmith _pci_hidden int
pci_system_solx_devfs_create(void)263206e2921Sedward shu pci_system_solx_devfs_create( void )
264206e2921Sedward shu {
265206e2921Sedward shu     int err = 0;
266de97e7e4SAlan Coopersmith     di_node_t di_node;
267a18460b3SJohn Martin     probe_info_t pinfo;
268a18460b3SJohn Martin     struct pci_device_private *devices;
269206e2921Sedward shu 
270de97e7e4SAlan Coopersmith     if (nexus_list != NULL) {
271de97e7e4SAlan Coopersmith 	return 0;
272de97e7e4SAlan Coopersmith     }
273206e2921Sedward shu 
274f537fc50SAlan Coopersmith     if ((di_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
275de97e7e4SAlan Coopersmith 	err = errno;
2769a5565c7SAlan Coopersmith 	(void) fprintf(stderr, "di_init() failed: %s\n",
277de97e7e4SAlan Coopersmith 		       strerror(errno));
278a18460b3SJohn Martin 	return (err);
279206e2921Sedward shu     }
280206e2921Sedward shu 
281a18460b3SJohn Martin     if ((devices = calloc(INITIAL_NUM_DEVICES,
282a18460b3SJohn Martin 			sizeof (struct pci_device_private))) == NULL) {
283a18460b3SJohn Martin 	err = errno;
284a18460b3SJohn Martin 	di_fini(di_node);
285a18460b3SJohn Martin 	return (err);
286de97e7e4SAlan Coopersmith     }
287a18460b3SJohn Martin 
288*cfae4096SHenry Zhao #ifdef __sparc
289*cfae4096SHenry Zhao     if ((di_phdl = di_prom_init()) == DI_PROM_HANDLE_NIL)
290*cfae4096SHenry Zhao 	(void) fprintf(stderr, "di_prom_init failed: %s\n", strerror(errno));
291*cfae4096SHenry Zhao #endif
292*cfae4096SHenry Zhao 
293a18460b3SJohn Martin     pinfo.num_allocated_elems = INITIAL_NUM_DEVICES;
294a18460b3SJohn Martin     pinfo.num_devices = 0;
295a18460b3SJohn Martin     pinfo.devices = devices;
296a18460b3SJohn Martin     (void) di_walk_minor(di_node, DDI_NT_REGACC, 0, &pinfo, probe_nexus_node);
297*cfae4096SHenry Zhao 
298a18460b3SJohn Martin     di_fini(di_node);
299a18460b3SJohn Martin 
300a18460b3SJohn Martin     if ((pci_sys = calloc(1, sizeof (struct pci_system))) == NULL) {
301a18460b3SJohn Martin 	err = errno;
302a18460b3SJohn Martin 	free(devices);
303a18460b3SJohn Martin 	return (err);
304206e2921Sedward shu     }
305*cfae4096SHenry Zhao 
306a18460b3SJohn Martin     pci_sys->methods = &solx_devfs_methods;
307a18460b3SJohn Martin     pci_sys->devices = pinfo.devices;
308a18460b3SJohn Martin     pci_sys->num_devices = pinfo.num_devices;
309206e2921Sedward shu 
310206e2921Sedward shu     return (err);
311206e2921Sedward shu }
312206e2921Sedward shu 
313206e2921Sedward shu /*
314206e2921Sedward shu  * Retrieve first 16 dwords of device's config header, except for the first
315206e2921Sedward shu  * dword.  First 16 dwords are defined by the PCI specification.
316206e2921Sedward shu  */
317206e2921Sedward shu static int
get_config_header(int fd,uint8_t bus_no,uint8_t dev_no,uint8_t func_no,pci_conf_hdr_t * config_hdr_p)318206e2921Sedward shu get_config_header(int fd, uint8_t bus_no, uint8_t dev_no, uint8_t func_no,
319206e2921Sedward shu 		  pci_conf_hdr_t *config_hdr_p)
320206e2921Sedward shu {
321206e2921Sedward shu     pcitool_reg_t cfg_prg;
322206e2921Sedward shu     int i;
323206e2921Sedward shu     int rval = 0;
324206e2921Sedward shu 
325206e2921Sedward shu     /* Prepare a local pcitool_reg_t so as to not disturb the caller's. */
326206e2921Sedward shu     cfg_prg.offset = 0;
327206e2921Sedward shu     cfg_prg.acc_attr = PCITOOL_ACC_ATTR_SIZE_4 + NATIVE_ENDIAN;
328206e2921Sedward shu     cfg_prg.bus_no = bus_no;
329206e2921Sedward shu     cfg_prg.dev_no = dev_no;
330206e2921Sedward shu     cfg_prg.func_no = func_no;
331206e2921Sedward shu     cfg_prg.barnum = 0;
332206e2921Sedward shu     cfg_prg.user_version = PCITOOL_USER_VERSION;
333206e2921Sedward shu 
334206e2921Sedward shu     /* Get dwords 1-15 of config space. They must be read as uint32_t. */
335206e2921Sedward shu     for (i = 1; i < (sizeof (pci_conf_hdr_t) / sizeof (uint32_t)); i++) {
336206e2921Sedward shu 	cfg_prg.offset += sizeof (uint32_t);
3379a5565c7SAlan Coopersmith 	if ((rval = ioctl(fd, PCITOOL_DEVICE_GET_REG, &cfg_prg)) != 0) {
338206e2921Sedward shu 	    break;
339206e2921Sedward shu 	}
340206e2921Sedward shu 	config_hdr_p->dwords[i] = (uint32_t)cfg_prg.data;
341206e2921Sedward shu     }
342206e2921Sedward shu 
343206e2921Sedward shu     return (rval);
344206e2921Sedward shu }
345206e2921Sedward shu 
346206e2921Sedward shu 
347206e2921Sedward shu /*
348206e2921Sedward shu  * Probe device's functions.  Modifies many fields in the prg_p.
349206e2921Sedward shu  */
350206e2921Sedward shu static int
probe_dev(nexus_t * nexus,pcitool_reg_t * prg_p,probe_info_t * pinfo)351a18460b3SJohn Martin probe_dev(nexus_t *nexus, pcitool_reg_t *prg_p, probe_info_t *pinfo)
352206e2921Sedward shu {
353206e2921Sedward shu     pci_conf_hdr_t	config_hdr;
354206e2921Sedward shu     boolean_t		multi_function_device;
355206e2921Sedward shu     int8_t		func;
356206e2921Sedward shu     int8_t		first_func = 0;
357206e2921Sedward shu     int8_t		last_func = PCI_REG_FUNC_M >> PCI_REG_FUNC_SHIFT;
358206e2921Sedward shu     int			rval = 0;
359de97e7e4SAlan Coopersmith     struct pci_device *	pci_base;
360206e2921Sedward shu 
361206e2921Sedward shu     /*
362206e2921Sedward shu      * Loop through at least func=first_func.  Continue looping through
363206e2921Sedward shu      * functions if there are no errors and the device is a multi-function
364206e2921Sedward shu      * device.
365206e2921Sedward shu      *
366206e2921Sedward shu      * (Note, if first_func == 0, header will show whether multifunction
367206e2921Sedward shu      * device and set multi_function_device.  If first_func != 0, then we
368206e2921Sedward shu      * will force the loop as the user wants a specific function to be
369206e2921Sedward shu      * checked.
370206e2921Sedward shu      */
371206e2921Sedward shu     for (func = first_func, multi_function_device = B_FALSE;
372206e2921Sedward shu 	 ((func <= last_func) &&
373206e2921Sedward shu 	  ((func == first_func) || (multi_function_device)));
374206e2921Sedward shu 	 func++) {
375206e2921Sedward shu 	prg_p->func_no = func;
376206e2921Sedward shu 
377206e2921Sedward shu 	/*
378206e2921Sedward shu 	 * Four things can happen here:
379206e2921Sedward shu 	 *
380206e2921Sedward shu 	 * 1) ioctl comes back as EFAULT and prg_p->status is
3819a5565c7SAlan Coopersmith 	 *    PCITOOL_INVALID_ADDRESS.  There is no device at this location.
382206e2921Sedward shu 	 *
383206e2921Sedward shu 	 * 2) ioctl comes back successful and the data comes back as
384206e2921Sedward shu 	 *    zero.  Config space is mapped but no device responded.
385206e2921Sedward shu 	 *
386206e2921Sedward shu 	 * 3) ioctl comes back successful and the data comes back as
387206e2921Sedward shu 	 *    non-zero.  We've found a device.
388206e2921Sedward shu 	 *
389206e2921Sedward shu 	 * 4) Some other error occurs in an ioctl.
390206e2921Sedward shu 	 */
391206e2921Sedward shu 
392206e2921Sedward shu 	prg_p->status = PCITOOL_SUCCESS;
393206e2921Sedward shu 	prg_p->offset = 0;
394206e2921Sedward shu 	prg_p->data = 0;
395206e2921Sedward shu 	prg_p->user_version = PCITOOL_USER_VERSION;
3969a5565c7SAlan Coopersmith 
397f537fc50SAlan Coopersmith 	errno = 0;
398de97e7e4SAlan Coopersmith 	if (((rval = ioctl(nexus->fd, PCITOOL_DEVICE_GET_REG, prg_p)) != 0) ||
399206e2921Sedward shu 	    (prg_p->data == 0xffffffff)) {
400206e2921Sedward shu 
401206e2921Sedward shu 	    /*
402206e2921Sedward shu 	     * Accept errno == EINVAL along with status of
403206e2921Sedward shu 	     * PCITOOL_OUT_OF_RANGE because some systems
404206e2921Sedward shu 	     * don't implement the full range of config space.
405206e2921Sedward shu 	     * Leave the loop quietly in this case.
406206e2921Sedward shu 	     */
407206e2921Sedward shu 	    if ((errno == EINVAL) ||
408206e2921Sedward shu 		(prg_p->status == PCITOOL_OUT_OF_RANGE)) {
409206e2921Sedward shu 		break;
410206e2921Sedward shu 	    }
411206e2921Sedward shu 
412206e2921Sedward shu 	    /*
413206e2921Sedward shu 	     * Exit silently with ENXIO as this means that there are
414206e2921Sedward shu 	     * no devices under the pci root nexus.
415206e2921Sedward shu 	     */
416206e2921Sedward shu 	    else if ((errno == ENXIO) &&
417206e2921Sedward shu 		     (prg_p->status == PCITOOL_IO_ERROR)) {
418206e2921Sedward shu 		break;
419206e2921Sedward shu 	    }
420206e2921Sedward shu 
421206e2921Sedward shu 	    /*
422206e2921Sedward shu 	     * Expect errno == EFAULT along with status of
423206e2921Sedward shu 	     * PCITOOL_INVALID_ADDRESS because there won't be
424206e2921Sedward shu 	     * devices at each stop.  Quit on any other error.
425206e2921Sedward shu 	     */
426206e2921Sedward shu 	    else if (((errno != EFAULT) ||
427206e2921Sedward shu 		      (prg_p->status != PCITOOL_INVALID_ADDRESS)) &&
428206e2921Sedward shu 		     (prg_p->data != 0xffffffff)) {
429*cfae4096SHenry Zhao #ifdef __sparc
430*cfae4096SHenry Zhao /* on sparc, devices can be enumerated discontiguously. Do not quit */
431*cfae4096SHenry Zhao 		rval = 0;
432*cfae4096SHenry Zhao #endif
433206e2921Sedward shu 		break;
4349a5565c7SAlan Coopersmith 	    }
435206e2921Sedward shu 
436206e2921Sedward shu 	    /*
437206e2921Sedward shu 	     * If no function at this location,
438206e2921Sedward shu 	     * just advance to the next function.
439206e2921Sedward shu 	     */
4409a5565c7SAlan Coopersmith 	    else {
441206e2921Sedward shu 		rval = 0;
442206e2921Sedward shu 	    }
443206e2921Sedward shu 
444206e2921Sedward shu 	    /*
445206e2921Sedward shu 	     * Data came back as 0.
446d43d21c8SAlan Coopersmith 	     * Treat as unresponsive device and check next device.
447206e2921Sedward shu 	     */
448206e2921Sedward shu 	} else if (prg_p->data == 0) {
449206e2921Sedward shu 	    rval = 0;
450206e2921Sedward shu 	    break;	/* Func loop. */
451206e2921Sedward shu 
452206e2921Sedward shu 	    /* Found something. */
453206e2921Sedward shu 	} else {
454206e2921Sedward shu 	    config_hdr.dwords[0] = (uint32_t)prg_p->data;
455206e2921Sedward shu 
456206e2921Sedward shu 	    /* Get the rest of the PCI header. */
457de97e7e4SAlan Coopersmith 	    if ((rval = get_config_header(nexus->fd, prg_p->bus_no,
4589a5565c7SAlan Coopersmith 					  prg_p->dev_no, prg_p->func_no,
4599a5565c7SAlan Coopersmith 					  &config_hdr)) != 0) {
460206e2921Sedward shu 		break;
461206e2921Sedward shu 	    }
462206e2921Sedward shu 
463206e2921Sedward shu 	    /*
464206e2921Sedward shu 	     * Special case for the type of Southbridge found on
465206e2921Sedward shu 	     * Ultra-45 and other sun4u fire workstations.
466206e2921Sedward shu 	     */
467206e2921Sedward shu 	    if ((config_hdr.dwords[0] == U45_SB_DEVID_VID) &&
468206e2921Sedward shu 		(config_hdr.dwords[2] == U45_SB_CLASS_RID)) {
469206e2921Sedward shu 		rval = ECANCELED;
470206e2921Sedward shu 		break;
471206e2921Sedward shu 	    }
472206e2921Sedward shu 
473206e2921Sedward shu 	    /*
474d43d21c8SAlan Coopersmith 	     * Found one device with bus number, device number and
475206e2921Sedward shu 	     * function number.
476206e2921Sedward shu 	     */
477206e2921Sedward shu 
478a18460b3SJohn Martin 	    pci_base = &pinfo->devices[pinfo->num_devices].base;
479de97e7e4SAlan Coopersmith 
480a18460b3SJohn Martin 	    pci_base->domain = nexus->domain;
481de97e7e4SAlan Coopersmith 	    pci_base->bus = prg_p->bus_no;
482de97e7e4SAlan Coopersmith 	    pci_base->dev = prg_p->dev_no;
483de97e7e4SAlan Coopersmith 	    pci_base->func = func;
4849a5565c7SAlan Coopersmith 
485206e2921Sedward shu 	    /*
486206e2921Sedward shu 	     * for the format of device_class, see struct pci_device;
487206e2921Sedward shu 	     */
4889a5565c7SAlan Coopersmith 
489de97e7e4SAlan Coopersmith 	    pci_base->device_class =
490de97e7e4SAlan Coopersmith 		(GET_CONFIG_VAL_8(PCI_CONF_BASCLASS) << 16) |
491de97e7e4SAlan Coopersmith 		(GET_CONFIG_VAL_8(PCI_CONF_SUBCLASS) << 8) |
492de97e7e4SAlan Coopersmith 		GET_CONFIG_VAL_8(PCI_CONF_PROGCLASS);
493de97e7e4SAlan Coopersmith 
4949a5565c7SAlan Coopersmith 	    pci_base->revision		= GET_CONFIG_VAL_8(PCI_CONF_REVID);
4959a5565c7SAlan Coopersmith 	    pci_base->vendor_id		= GET_CONFIG_VAL_16(PCI_CONF_VENID);
4969a5565c7SAlan Coopersmith 	    pci_base->device_id		= GET_CONFIG_VAL_16(PCI_CONF_DEVID);
4979a5565c7SAlan Coopersmith 	    pci_base->subvendor_id 	= GET_CONFIG_VAL_16(PCI_CONF_SUBVENID);
4989a5565c7SAlan Coopersmith 	    pci_base->subdevice_id 	= GET_CONFIG_VAL_16(PCI_CONF_SUBSYSID);
499*cfae4096SHenry Zhao 	    pci_base->irq		= GET_CONFIG_VAL_8(PCI_CONF_ILINE);
5009a5565c7SAlan Coopersmith 
501a18460b3SJohn Martin 	    pinfo->devices[pinfo->num_devices].header_type
5029a5565c7SAlan Coopersmith 					= GET_CONFIG_VAL_8(PCI_CONF_HEADER);
5039a5565c7SAlan Coopersmith 
5045bf4b32cSAlan Coopersmith #ifdef DEBUG
5055bf4b32cSAlan Coopersmith 	    fprintf(stderr,
5065bf4b32cSAlan Coopersmith 		    "nexus = %s, busno = %x, devno = %x, funcno = %x\n",
5075bf4b32cSAlan Coopersmith 		    nexus->path, prg_p->bus_no, prg_p->dev_no, func);
508206e2921Sedward shu #endif
509206e2921Sedward shu 
510a18460b3SJohn Martin 	    pinfo->num_devices++;
511a18460b3SJohn Martin 	    if (pinfo->num_devices == pinfo->num_allocated_elems) {
512a18460b3SJohn Martin 		struct pci_device_private *new_devs;
513a18460b3SJohn Martin 		size_t new_num_elems = pinfo->num_allocated_elems * 2;
514a18460b3SJohn Martin 
515a18460b3SJohn Martin 		new_devs = realloc(pinfo->devices,
516a18460b3SJohn Martin 			new_num_elems * sizeof (struct pci_device_private));
517a18460b3SJohn Martin 		if (new_devs == NULL) {
5185bf4b32cSAlan Coopersmith 		    (void) fprintf(stderr,
519803bf3aaSAlan Coopersmith 			           "Error allocating memory for PCI devices:"
520803bf3aaSAlan Coopersmith 				   " %s\n discarding additional devices\n",
521803bf3aaSAlan Coopersmith 				   strerror(errno));
522a18460b3SJohn Martin 		    return (rval);
5235bf4b32cSAlan Coopersmith 		}
524a18460b3SJohn Martin 		(void) memset(&new_devs[pinfo->num_devices], 0,
525a18460b3SJohn Martin 			pinfo->num_allocated_elems *
526a18460b3SJohn Martin 			sizeof (struct pci_device_private));
527a18460b3SJohn Martin 		pinfo->num_allocated_elems = new_num_elems;
528a18460b3SJohn Martin 		pinfo->devices = new_devs;
529a18460b3SJohn Martin 	    }
530206e2921Sedward shu 
531*cfae4096SHenry Zhao #ifdef __sparc
532*cfae4096SHenry Zhao 	    nexus->devlist[nexus->num_devices++] = pci_base;
533*cfae4096SHenry Zhao 
534*cfae4096SHenry Zhao 	    if (nexus->num_devices == nexus->num_allocated_elems) {
535*cfae4096SHenry Zhao 		struct pci_device **new_devs;
536*cfae4096SHenry Zhao 		size_t new_num_elems = nexus->num_allocated_elems * 2;
537*cfae4096SHenry Zhao 
538*cfae4096SHenry Zhao 		new_devs = realloc(nexus->devlist,
539*cfae4096SHenry Zhao 			new_num_elems * sizeof (struct pci_device *));
540*cfae4096SHenry Zhao 		if (new_devs == NULL)
541*cfae4096SHenry Zhao 		    return (rval);
542*cfae4096SHenry Zhao 		(void) memset(&new_devs[nexus->num_devices], 0,
543*cfae4096SHenry Zhao 			nexus->num_allocated_elems *
544*cfae4096SHenry Zhao 			sizeof (struct pci_device *));
545*cfae4096SHenry Zhao 		nexus->num_allocated_elems = new_num_elems;
546*cfae4096SHenry Zhao 		nexus->devlist = new_devs;
547*cfae4096SHenry Zhao 	    }
548*cfae4096SHenry Zhao #endif
549*cfae4096SHenry Zhao 
550206e2921Sedward shu 	    /*
551d43d21c8SAlan Coopersmith 	     * Accommodate devices which state their
552206e2921Sedward shu 	     * multi-functionality only in their function 0 config
553206e2921Sedward shu 	     * space.  Note multi-functionality throughout probing
554206e2921Sedward shu 	     * of all of this device's functions.
555206e2921Sedward shu 	     */
5569a5565c7SAlan Coopersmith 	    if (config_hdr.bytes[PCI_CONF_HEADER] & PCI_HEADER_MULTI) {
557206e2921Sedward shu 		multi_function_device = B_TRUE;
558206e2921Sedward shu 	    }
559206e2921Sedward shu 	}
560206e2921Sedward shu     }
561206e2921Sedward shu 
562206e2921Sedward shu     return (rval);
563206e2921Sedward shu }
564206e2921Sedward shu 
565206e2921Sedward shu /*
566de97e7e4SAlan Coopersmith  * This function is called from di_walk_minor() when any PROBE is processed
567de97e7e4SAlan Coopersmith  */
568de97e7e4SAlan Coopersmith static int
probe_nexus_node(di_node_t di_node,di_minor_t minor,void * arg)569de97e7e4SAlan Coopersmith probe_nexus_node(di_node_t di_node, di_minor_t minor, void *arg)
570de97e7e4SAlan Coopersmith {
571a18460b3SJohn Martin     probe_info_t *pinfo = (probe_info_t *)arg;
572a18460b3SJohn Martin     char *nexus_name, *nexus_dev_path;
573de97e7e4SAlan Coopersmith     nexus_t *nexus;
574de97e7e4SAlan Coopersmith     int fd;
575de97e7e4SAlan Coopersmith     char nexus_path[MAXPATHLEN];
576de97e7e4SAlan Coopersmith 
5775bf4b32cSAlan Coopersmith     di_prop_t prop;
578c56da48eSAlan Coopersmith     char *strings;
5795bf4b32cSAlan Coopersmith     int *ints;
5805bf4b32cSAlan Coopersmith     int numval;
5815bf4b32cSAlan Coopersmith     int pci_node = 0;
5825bf4b32cSAlan Coopersmith     int first_bus = 0, last_bus = PCI_REG_BUS_G(PCI_REG_BUS_M);
583a18460b3SJohn Martin     int domain = 0;
584*cfae4096SHenry Zhao #ifdef __sparc
585*cfae4096SHenry Zhao     int bus_range_found = 0;
586*cfae4096SHenry Zhao     int device_type_found = 0;
587*cfae4096SHenry Zhao     di_prom_prop_t prom_prop;
588*cfae4096SHenry Zhao #endif
589*cfae4096SHenry Zhao 
5905bf4b32cSAlan Coopersmith 
5915bf4b32cSAlan Coopersmith #ifdef DEBUG
5925bf4b32cSAlan Coopersmith     nexus_name = di_devfs_minor_path(minor);
5935bf4b32cSAlan Coopersmith     fprintf(stderr, "-- device name: %s\n", nexus_name);
5945bf4b32cSAlan Coopersmith #endif
5955bf4b32cSAlan Coopersmith 
5965bf4b32cSAlan Coopersmith     for (prop = di_prop_next(di_node, NULL); prop != NULL;
5975bf4b32cSAlan Coopersmith 	 prop = di_prop_next(di_node, prop)) {
5985bf4b32cSAlan Coopersmith 
5995bf4b32cSAlan Coopersmith 	const char *prop_name = di_prop_name(prop);
6005bf4b32cSAlan Coopersmith 
6015bf4b32cSAlan Coopersmith #ifdef DEBUG
6025bf4b32cSAlan Coopersmith 	fprintf(stderr, "   property: %s\n", prop_name);
6035bf4b32cSAlan Coopersmith #endif
6045bf4b32cSAlan Coopersmith 
6055bf4b32cSAlan Coopersmith 	if (strcmp(prop_name, "device_type") == 0) {
6065bf4b32cSAlan Coopersmith 	    numval = di_prop_strings(prop, &strings);
607*cfae4096SHenry Zhao 	    if (numval == 1) {
608*cfae4096SHenry Zhao 		if (strncmp(strings, "pci", 3) != 0)
6095bf4b32cSAlan Coopersmith 		    /* not a PCI node, bail */
6105bf4b32cSAlan Coopersmith 		    return (DI_WALK_CONTINUE);
611*cfae4096SHenry Zhao 		else {
6125bf4b32cSAlan Coopersmith 		    pci_node = 1;
613*cfae4096SHenry Zhao #ifdef __sparc
614*cfae4096SHenry Zhao 		    device_type_found =  1;
615*cfae4096SHenry Zhao #endif
616*cfae4096SHenry Zhao 		}
617*cfae4096SHenry Zhao 	    }
6185bf4b32cSAlan Coopersmith 	}
6195bf4b32cSAlan Coopersmith 	else if (strcmp(prop_name, "class-code") == 0) {
6205bf4b32cSAlan Coopersmith 	    /* not a root bus node, bail */
6215bf4b32cSAlan Coopersmith 	    return (DI_WALK_CONTINUE);
6225bf4b32cSAlan Coopersmith 	}
6235bf4b32cSAlan Coopersmith 	else if (strcmp(prop_name, "bus-range") == 0) {
6245bf4b32cSAlan Coopersmith 	    numval = di_prop_ints(prop, &ints);
6255bf4b32cSAlan Coopersmith 	    if (numval == 2) {
6265bf4b32cSAlan Coopersmith 		first_bus = ints[0];
6275bf4b32cSAlan Coopersmith 		last_bus = ints[1];
628*cfae4096SHenry Zhao #ifdef __sparc
629*cfae4096SHenry Zhao 		bus_range_found = 1;
630*cfae4096SHenry Zhao #endif
6315bf4b32cSAlan Coopersmith 	    }
6325bf4b32cSAlan Coopersmith 	}
633a18460b3SJohn Martin 	else if (strcmp(prop_name, "pciseg") == 0) {
634a18460b3SJohn Martin 	    numval = di_prop_ints(prop, &ints);
635a18460b3SJohn Martin 	    if (numval == 1) {
636a18460b3SJohn Martin 		domain = ints[0];
637a18460b3SJohn Martin 	    }
638a18460b3SJohn Martin 	}
6395bf4b32cSAlan Coopersmith     }
6405bf4b32cSAlan Coopersmith 
641*cfae4096SHenry Zhao #ifdef __sparc
642*cfae4096SHenry Zhao     if ((!device_type_found) && di_phdl) {
643*cfae4096SHenry Zhao 	numval = di_prom_prop_lookup_strings(di_phdl, di_node,
644*cfae4096SHenry Zhao 	    "device_type", &strings);
645*cfae4096SHenry Zhao 	if (numval == 1) {
646*cfae4096SHenry Zhao 	    if (strncmp(strings, "pci", 3) != 0)
647*cfae4096SHenry Zhao 		return (DI_WALK_CONTINUE);
648*cfae4096SHenry Zhao 	    else
649*cfae4096SHenry Zhao 		pci_node = 1;
650*cfae4096SHenry Zhao 	}
651*cfae4096SHenry Zhao     }
652*cfae4096SHenry Zhao 
653*cfae4096SHenry Zhao     if ((!bus_range_found) && di_phdl) {
654*cfae4096SHenry Zhao 	numval = di_prom_prop_lookup_ints(di_phdl, di_node,
655*cfae4096SHenry Zhao 	    "bus-range", &ints);
656*cfae4096SHenry Zhao 	if (numval == 2) {
657*cfae4096SHenry Zhao 	    first_bus = ints[0];
658*cfae4096SHenry Zhao 	    last_bus = ints[1];
659*cfae4096SHenry Zhao 	}
660*cfae4096SHenry Zhao     }
661*cfae4096SHenry Zhao #endif
662*cfae4096SHenry Zhao 
6635bf4b32cSAlan Coopersmith     if (pci_node != 1)
6645bf4b32cSAlan Coopersmith 	return (DI_WALK_CONTINUE);
6655bf4b32cSAlan Coopersmith 
6665bf4b32cSAlan Coopersmith     /* we have a PCI root bus node. */
667de97e7e4SAlan Coopersmith     nexus = calloc(1, sizeof(nexus_t));
668de97e7e4SAlan Coopersmith     if (nexus == NULL) {
6699a5565c7SAlan Coopersmith 	(void) fprintf(stderr, "Error allocating memory for nexus: %s\n",
670de97e7e4SAlan Coopersmith 		       strerror(errno));
6715bf4b32cSAlan Coopersmith 	return (DI_WALK_TERMINATE);
672de97e7e4SAlan Coopersmith     }
6735bf4b32cSAlan Coopersmith     nexus->first_bus = first_bus;
6745bf4b32cSAlan Coopersmith     nexus->last_bus = last_bus;
675a18460b3SJohn Martin     nexus->domain = domain;
676de97e7e4SAlan Coopersmith 
677*cfae4096SHenry Zhao #ifdef __sparc
678*cfae4096SHenry Zhao     if ((nexus->devlist = calloc(INITIAL_NUM_DEVICES,
679*cfae4096SHenry Zhao 			sizeof (struct pci_device *))) == NULL) {
680*cfae4096SHenry Zhao 	(void) fprintf(stderr, "Error allocating memory for nexus devlist: %s\n",
681*cfae4096SHenry Zhao                        strerror(errno));
682*cfae4096SHenry Zhao 	free (nexus);
683*cfae4096SHenry Zhao 	return (DI_WALK_TERMINATE);
684*cfae4096SHenry Zhao     }
685*cfae4096SHenry Zhao     nexus->num_allocated_elems = INITIAL_NUM_DEVICES;
686*cfae4096SHenry Zhao     nexus->num_devices = 0;
687*cfae4096SHenry Zhao #endif
688*cfae4096SHenry Zhao 
689de97e7e4SAlan Coopersmith     nexus_name = di_devfs_minor_path(minor);
690de97e7e4SAlan Coopersmith     if (nexus_name == NULL) {
691de97e7e4SAlan Coopersmith 	(void) fprintf(stderr, "Error getting nexus path: %s\n",
692de97e7e4SAlan Coopersmith 		       strerror(errno));
693de97e7e4SAlan Coopersmith 	free(nexus);
694de97e7e4SAlan Coopersmith 	return (DI_WALK_CONTINUE);
695de97e7e4SAlan Coopersmith     }
696de97e7e4SAlan Coopersmith 
697de97e7e4SAlan Coopersmith     snprintf(nexus_path, sizeof(nexus_path), "/devices%s", nexus_name);
698f537fc50SAlan Coopersmith     di_devfs_path_free(nexus_name);
6995bf4b32cSAlan Coopersmith 
7005bf4b32cSAlan Coopersmith #ifdef DEBUG
7015bf4b32cSAlan Coopersmith     fprintf(stderr, "nexus = %s, bus-range = %d - %d\n",
7025bf4b32cSAlan Coopersmith 	    nexus_path, first_bus, last_bus);
7035bf4b32cSAlan Coopersmith #endif
704de97e7e4SAlan Coopersmith 
705de97e7e4SAlan Coopersmith     if ((fd = open(nexus_path, O_RDWR)) >= 0) {
706de97e7e4SAlan Coopersmith 	nexus->fd = fd;
707c56da48eSAlan Coopersmith 	nexus->path = strdup(nexus_path);
708a18460b3SJohn Martin 	nexus_dev_path = di_devfs_path(di_node);
709a18460b3SJohn Martin 	nexus->dev_path = strdup(nexus_dev_path);
710a18460b3SJohn Martin 	di_devfs_path_free(nexus_dev_path);
711a18460b3SJohn Martin 	if ((do_probe(nexus, pinfo) != 0) && (errno != ENXIO)) {
712de97e7e4SAlan Coopersmith 	    (void) fprintf(stderr, "Error probing node %s: %s\n",
713de97e7e4SAlan Coopersmith 			   nexus_path, strerror(errno));
714de97e7e4SAlan Coopersmith 	    (void) close(fd);
7155bf4b32cSAlan Coopersmith 	    free(nexus->path);
716a18460b3SJohn Martin 	    free(nexus->dev_path);
717de97e7e4SAlan Coopersmith 	    free(nexus);
718de97e7e4SAlan Coopersmith 	} else {
719de97e7e4SAlan Coopersmith 	    nexus->next = nexus_list;
720de97e7e4SAlan Coopersmith 	    nexus_list = nexus;
721de97e7e4SAlan Coopersmith 	}
722de97e7e4SAlan Coopersmith     } else {
723de97e7e4SAlan Coopersmith 	(void) fprintf(stderr, "Error opening %s: %s\n",
724de97e7e4SAlan Coopersmith 		       nexus_path, strerror(errno));
725de97e7e4SAlan Coopersmith 	free(nexus);
726de97e7e4SAlan Coopersmith     }
727de97e7e4SAlan Coopersmith 
728de97e7e4SAlan Coopersmith     return DI_WALK_CONTINUE;
729de97e7e4SAlan Coopersmith }
730de97e7e4SAlan Coopersmith 
731de97e7e4SAlan Coopersmith 
732de97e7e4SAlan Coopersmith /*
733206e2921Sedward shu  * Solaris version
734206e2921Sedward shu  * Probe a given nexus config space for devices.
735206e2921Sedward shu  *
736206e2921Sedward shu  * fd is the file descriptor of the nexus.
737206e2921Sedward shu  * input_args contains commandline options as specified by the user.
738206e2921Sedward shu  */
739206e2921Sedward shu static int
do_probe(nexus_t * nexus,probe_info_t * pinfo)740a18460b3SJohn Martin do_probe(nexus_t *nexus, probe_info_t *pinfo)
741206e2921Sedward shu {
742206e2921Sedward shu     pcitool_reg_t prg;
743206e2921Sedward shu     uint32_t bus;
744206e2921Sedward shu     uint8_t dev;
7455bf4b32cSAlan Coopersmith     uint32_t last_bus = nexus->last_bus;
746206e2921Sedward shu     uint8_t last_dev = PCI_REG_DEV_M >> PCI_REG_DEV_SHIFT;
7475bf4b32cSAlan Coopersmith     uint8_t first_bus = nexus->first_bus;
748206e2921Sedward shu     uint8_t first_dev = 0;
749206e2921Sedward shu     int rval = 0;
750206e2921Sedward shu 
751206e2921Sedward shu     prg.barnum = 0;	/* Config space. */
752206e2921Sedward shu 
753206e2921Sedward shu     /* Must read in 4-byte quantities. */
754206e2921Sedward shu     prg.acc_attr = PCITOOL_ACC_ATTR_SIZE_4 + NATIVE_ENDIAN;
755206e2921Sedward shu 
756206e2921Sedward shu     prg.data = 0;
757206e2921Sedward shu 
758206e2921Sedward shu     /*
759206e2921Sedward shu      * Loop through all valid bus / dev / func combinations to check for
760206e2921Sedward shu      * all devices, with the following exceptions:
761206e2921Sedward shu      *
762206e2921Sedward shu      * When nothing is found at function 0 of a bus / dev combination, skip
763206e2921Sedward shu      * the other functions of that bus / dev combination.
764206e2921Sedward shu      *
765206e2921Sedward shu      * When a found device's function 0 is probed and it is determined that
766206e2921Sedward shu      * it is not a multifunction device, skip probing of that device's
767206e2921Sedward shu      * other functions.
768206e2921Sedward shu      */
769206e2921Sedward shu     for (bus = first_bus; ((bus <= last_bus) && (rval == 0)); bus++) {
770206e2921Sedward shu 	prg.bus_no = (uint8_t)bus;
7719a5565c7SAlan Coopersmith 
7729a5565c7SAlan Coopersmith 	for (dev = first_dev; ((dev <= last_dev) && (rval == 0)); dev++) {
773206e2921Sedward shu 	    prg.dev_no = dev;
774a18460b3SJohn Martin 	    rval = probe_dev(nexus, &prg, pinfo);
775206e2921Sedward shu 	}
776206e2921Sedward shu 
777206e2921Sedward shu 	/*
778206e2921Sedward shu 	 * Ultra-45 southbridge workaround:
779206e2921Sedward shu 	 * ECANCELED tells to skip to the next bus.
780206e2921Sedward shu 	 */
781206e2921Sedward shu 	if (rval == ECANCELED) {
782206e2921Sedward shu 	    rval = 0;
783206e2921Sedward shu 	}
784206e2921Sedward shu     }
785206e2921Sedward shu 
786206e2921Sedward shu     return (rval);
787206e2921Sedward shu }
788206e2921Sedward shu 
789206e2921Sedward shu static int
find_target_node(di_node_t node,void * arg)790206e2921Sedward shu find_target_node(di_node_t node, void *arg)
791206e2921Sedward shu {
792206e2921Sedward shu     int *regbuf = NULL;
793206e2921Sedward shu     int len = 0;
794206e2921Sedward shu     uint32_t busno, funcno, devno;
7957f08a1e1SAlan Coopersmith     i_devnode_t *devnode = (i_devnode_t *)arg;
796206e2921Sedward shu 
797206e2921Sedward shu     /*
798d43d21c8SAlan Coopersmith      * Test the property functions, only for testing
799206e2921Sedward shu      */
800206e2921Sedward shu     /*
8017f08a1e1SAlan Coopersmith     void *prop = DI_PROP_NIL;
8027f08a1e1SAlan Coopersmith 
803206e2921Sedward shu     (void) fprintf(stderr, "start of node 0x%x\n", node->nodeid);
804206e2921Sedward shu     while ((prop = di_prop_hw_next(node, prop)) != DI_PROP_NIL) {
8057f08a1e1SAlan Coopersmith 	int i;
806206e2921Sedward shu 	(void) fprintf(stderr, "name=%s: ", di_prop_name(prop));
807206e2921Sedward shu 	len = 0;
808206e2921Sedward shu 	if (!strcmp(di_prop_name(prop), "reg")) {
809206e2921Sedward shu 	    len = di_prop_ints(prop, &regbuf);
810206e2921Sedward shu 	}
811206e2921Sedward shu 	for (i = 0; i < len; i++) {
812206e2921Sedward shu 	    fprintf(stderr, "0x%0x.", regbuf[i]);
813206e2921Sedward shu 	}
814206e2921Sedward shu 	fprintf(stderr, "\n");
815206e2921Sedward shu     }
816206e2921Sedward shu     (void) fprintf(stderr, "end of node 0x%x\n", node->nodeid);
817206e2921Sedward shu     */
818206e2921Sedward shu 
8199a5565c7SAlan Coopersmith     len = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "reg", &regbuf);
820206e2921Sedward shu 
821*cfae4096SHenry Zhao #ifdef __sparc
822*cfae4096SHenry Zhao     if ((len <= 0) && di_phdl)
823*cfae4096SHenry Zhao 	len = di_prom_prop_lookup_ints(di_phdl, node, "reg", &regbuf);
824*cfae4096SHenry Zhao #endif
825*cfae4096SHenry Zhao 
826206e2921Sedward shu     if (len <= 0) {
8275bf4b32cSAlan Coopersmith #ifdef DEBUG
828206e2921Sedward shu 	fprintf(stderr, "error = %x\n", errno);
829206e2921Sedward shu 	fprintf(stderr, "can not find assigned-address\n");
830206e2921Sedward shu #endif
831206e2921Sedward shu 	return (DI_WALK_CONTINUE);
832206e2921Sedward shu     }
8339a5565c7SAlan Coopersmith 
834206e2921Sedward shu     busno = PCI_REG_BUS_G(regbuf[0]);
835206e2921Sedward shu     devno = PCI_REG_DEV_G(regbuf[0]);
836206e2921Sedward shu     funcno = PCI_REG_FUNC_G(regbuf[0]);
837206e2921Sedward shu 
838206e2921Sedward shu     if ((busno == devnode->bus) &&
839206e2921Sedward shu 	(devno == devnode->dev) &&
840206e2921Sedward shu 	(funcno == devnode->func)) {
841206e2921Sedward shu 	devnode->node = node;
842206e2921Sedward shu 
843206e2921Sedward shu 	return (DI_WALK_TERMINATE);
844206e2921Sedward shu     }
845206e2921Sedward shu 
846206e2921Sedward shu     return (DI_WALK_CONTINUE);
847206e2921Sedward shu }
848206e2921Sedward shu 
849206e2921Sedward shu /*
850206e2921Sedward shu  * Solaris version
851206e2921Sedward shu  */
852206e2921Sedward shu static int
pci_device_solx_devfs_probe(struct pci_device * dev)853206e2921Sedward shu pci_device_solx_devfs_probe( struct pci_device * dev )
854206e2921Sedward shu {
855*cfae4096SHenry Zhao     int err = 0;
856be748a7bSAlan Coopersmith     di_node_t rnode = DI_NODE_NIL;
857be748a7bSAlan Coopersmith     i_devnode_t args = { 0, 0, 0, DI_NODE_NIL };
858206e2921Sedward shu     int *regbuf;
859206e2921Sedward shu     pci_regspec_t *reg;
860206e2921Sedward shu     int i;
861206e2921Sedward shu     int len = 0;
862f6084593Sedward shu     uint ent = 0;
863a18460b3SJohn Martin     nexus_t *nexus;
864a18460b3SJohn Martin 
865*cfae4096SHenry Zhao #ifdef __sparc
866*cfae4096SHenry Zhao     if ( (nexus = find_nexus_for_dev(dev)) == NULL )
867*cfae4096SHenry Zhao #else
868a18460b3SJohn Martin     if ( (nexus = find_nexus_for_bus(dev->domain, dev->bus)) == NULL )
869*cfae4096SHenry Zhao #endif
870a18460b3SJohn Martin 	return ENODEV;
871206e2921Sedward shu 
872206e2921Sedward shu     /*
873206e2921Sedward shu      * starting to find if it is MEM/MEM64/IO
874206e2921Sedward shu      * using libdevinfo
875206e2921Sedward shu      */
876a18460b3SJohn Martin     if ((rnode = di_init(nexus->dev_path, DINFOCPYALL)) == DI_NODE_NIL) {
877206e2921Sedward shu 	err = errno;
878f537fc50SAlan Coopersmith 	(void) fprintf(stderr, "di_init failed: %s\n", strerror(errno));
879206e2921Sedward shu     } else {
880206e2921Sedward shu 	args.bus = dev->bus;
881206e2921Sedward shu 	args.dev = dev->dev;
882206e2921Sedward shu 	args.func = dev->func;
883206e2921Sedward shu 	(void) di_walk_node(rnode, DI_WALK_CLDFIRST,
884206e2921Sedward shu 		(void *)&args, find_target_node);
885206e2921Sedward shu     }
886*cfae4096SHenry Zhao 
887206e2921Sedward shu     if (args.node != DI_NODE_NIL) {
888*cfae4096SHenry Zhao #ifdef __sparc
889*cfae4096SHenry Zhao 	di_minor_t minor;
890*cfae4096SHenry Zhao #endif
891*cfae4096SHenry Zhao 
892*cfae4096SHenry Zhao #ifdef __sparc
893*cfae4096SHenry Zhao 	if (minor = di_minor_next(args.node, DI_MINOR_NIL))
894*cfae4096SHenry Zhao 	    MAPPING_DEV_PATH(dev) = di_devfs_minor_path (minor);
895*cfae4096SHenry Zhao 	else
896*cfae4096SHenry Zhao 	    MAPPING_DEV_PATH(dev) = NULL;
897*cfae4096SHenry Zhao #endif
898*cfae4096SHenry Zhao 
899206e2921Sedward shu 	/*
900f537fc50SAlan Coopersmith 	 * It will succeed for sure, because it was
901206e2921Sedward shu 	 * successfully called in find_target_node
902206e2921Sedward shu 	 */
903206e2921Sedward shu 	len = di_prop_lookup_ints(DDI_DEV_T_ANY, args.node,
904206e2921Sedward shu 				  "assigned-addresses",
905206e2921Sedward shu 				  &regbuf);
906206e2921Sedward shu 
907*cfae4096SHenry Zhao #ifdef __sparc
908*cfae4096SHenry Zhao 	if ((len <= 0) && di_phdl) {
909*cfae4096SHenry Zhao 	    len = di_prom_prop_lookup_ints(di_phdl, args.node,
910*cfae4096SHenry Zhao 				"assigned-addresses", &regbuf);
911*cfae4096SHenry Zhao 	}
912*cfae4096SHenry Zhao #endif
913206e2921Sedward shu     }
914206e2921Sedward shu 
915206e2921Sedward shu     if (len <= 0)
916be748a7bSAlan Coopersmith 	goto cleanup;
917206e2921Sedward shu 
918206e2921Sedward shu 
919206e2921Sedward shu     /*
920206e2921Sedward shu      * how to find the size of rom???
921206e2921Sedward shu      * if the device has expansion rom,
922206e2921Sedward shu      * it must be listed in the last
923206e2921Sedward shu      * cells because solaris find probe
924206e2921Sedward shu      * the base address from offset 0x10
925206e2921Sedward shu      * to 0x30h. So only check the last
926206e2921Sedward shu      * item.
927206e2921Sedward shu      */
928206e2921Sedward shu     reg = (pci_regspec_t *)&regbuf[len - CELL_NUMS_1275];
9299a5565c7SAlan Coopersmith     if (PCI_REG_REG_G(reg->pci_phys_hi) == PCI_CONF_ROM) {
930206e2921Sedward shu 	/*
931206e2921Sedward shu 	 * rom can only be 32 bits
932206e2921Sedward shu 	 */
933206e2921Sedward shu 	dev->rom_size = reg->pci_size_low;
934206e2921Sedward shu 	len = len - CELL_NUMS_1275;
935206e2921Sedward shu     }
936206e2921Sedward shu     else {
937206e2921Sedward shu 	/*
938206e2921Sedward shu 	 * size default to 64K and base address
939206e2921Sedward shu 	 * default to 0xC0000
940206e2921Sedward shu 	 */
941206e2921Sedward shu 	dev->rom_size = 0x10000;
942206e2921Sedward shu     }
943206e2921Sedward shu 
944206e2921Sedward shu     /*
945f6084593Sedward shu      * Solaris has its own BAR index.
946f6084593Sedward shu      * Linux give two region slot for 64 bit address.
947206e2921Sedward shu      */
948206e2921Sedward shu     for (i = 0; i < len; i = i + CELL_NUMS_1275) {
949206e2921Sedward shu 
950206e2921Sedward shu 	reg = (pci_regspec_t *)&regbuf[i];
951f6084593Sedward shu 	ent = reg->pci_phys_hi & 0xff;
952f6084593Sedward shu 	/*
953f6084593Sedward shu 	 * G35 broken in BAR0
954f6084593Sedward shu 	 */
955f6084593Sedward shu 	ent = (ent - PCI_CONF_BASE0) >> 2;
956f6084593Sedward shu 	if (ent >= 6) {
957f6084593Sedward shu 	    fprintf(stderr, "error ent = %d\n", ent);
958f6084593Sedward shu 	    break;
959f6084593Sedward shu 	}
960206e2921Sedward shu 
961206e2921Sedward shu 	/*
962206e2921Sedward shu 	 * non relocatable resource is excluded
963206e2921Sedward shu 	 * such like 0xa0000, 0x3b0. If it is met,
964206e2921Sedward shu 	 * the loop is broken;
965206e2921Sedward shu 	 */
966206e2921Sedward shu 	if (!PCI_REG_REG_G(reg->pci_phys_hi))
967206e2921Sedward shu 	    break;
968206e2921Sedward shu 
969206e2921Sedward shu 	if (reg->pci_phys_hi & PCI_PREFETCH_B) {
970206e2921Sedward shu 	    dev->regions[ent].is_prefetchable = 1;
971206e2921Sedward shu 	}
972206e2921Sedward shu 
9739a5565c7SAlan Coopersmith 
974206e2921Sedward shu 	/*
975206e2921Sedward shu 	 * We split the shift count 32 into two 16 to
976206e2921Sedward shu 	 * avoid the complaining of the compiler
977206e2921Sedward shu 	 */
978206e2921Sedward shu 	dev->regions[ent].base_addr = reg->pci_phys_low +
979206e2921Sedward shu 	    ((reg->pci_phys_mid << 16) << 16);
980206e2921Sedward shu 	dev->regions[ent].size = reg->pci_size_low +
981206e2921Sedward shu 	    ((reg->pci_size_hi << 16) << 16);
982f6084593Sedward shu 
983f6084593Sedward shu 	switch (reg->pci_phys_hi & PCI_REG_ADDR_M) {
984f6084593Sedward shu 	    case PCI_ADDR_IO:
985f6084593Sedward shu 		dev->regions[ent].is_IO = 1;
986f6084593Sedward shu 		break;
987f6084593Sedward shu 	    case PCI_ADDR_MEM32:
988f6084593Sedward shu 		break;
989f6084593Sedward shu 	    case PCI_ADDR_MEM64:
990f6084593Sedward shu 		dev->regions[ent].is_64 = 1;
991f6084593Sedward shu 		/*
992f6084593Sedward shu 		 * Skip one slot for 64 bit address
993f6084593Sedward shu 		 */
994f6084593Sedward shu 		break;
995f6084593Sedward shu 	}
996206e2921Sedward shu     }
997206e2921Sedward shu 
998be748a7bSAlan Coopersmith   cleanup:
999be748a7bSAlan Coopersmith     if (rnode != DI_NODE_NIL) {
1000be748a7bSAlan Coopersmith 	di_fini(rnode);
1001be748a7bSAlan Coopersmith     }
1002206e2921Sedward shu     return (err);
1003206e2921Sedward shu }
1004206e2921Sedward shu 
1005206e2921Sedward shu /*
1006f537fc50SAlan Coopersmith  * Solaris version: read the VGA ROM data
1007206e2921Sedward shu  */
1008206e2921Sedward shu static int
pci_device_solx_devfs_read_rom(struct pci_device * dev,void * buffer)1009206e2921Sedward shu pci_device_solx_devfs_read_rom( struct pci_device * dev, void * buffer )
1010206e2921Sedward shu {
1011f537fc50SAlan Coopersmith     int err;
1012f537fc50SAlan Coopersmith     struct pci_device_mapping prom = {
1013f537fc50SAlan Coopersmith 	.base = 0xC0000,
1014f537fc50SAlan Coopersmith 	.size = dev->rom_size,
1015f537fc50SAlan Coopersmith 	.flags = 0
1016f537fc50SAlan Coopersmith     };
1017206e2921Sedward shu 
1018f537fc50SAlan Coopersmith     err = pci_device_solx_devfs_map_range(dev, &prom);
1019f537fc50SAlan Coopersmith     if (err == 0) {
1020f537fc50SAlan Coopersmith 	(void) bcopy(prom.memory, buffer, dev->rom_size);
1021206e2921Sedward shu 
1022f537fc50SAlan Coopersmith 	if (munmap(prom.memory, dev->rom_size) == -1) {
1023f537fc50SAlan Coopersmith 	    err = errno;
1024206e2921Sedward shu 	}
1025206e2921Sedward shu     }
1026f537fc50SAlan Coopersmith     return err;
1027206e2921Sedward shu }
1028206e2921Sedward shu 
1029206e2921Sedward shu /*
1030206e2921Sedward shu  * solaris version: Read the configurations space of the devices
1031206e2921Sedward shu  */
1032206e2921Sedward shu static int
pci_device_solx_devfs_read(struct pci_device * dev,void * data,pciaddr_t offset,pciaddr_t size,pciaddr_t * bytes_read)1033206e2921Sedward shu pci_device_solx_devfs_read( struct pci_device * dev, void * data,
1034206e2921Sedward shu 			     pciaddr_t offset, pciaddr_t size,
1035206e2921Sedward shu 			     pciaddr_t * bytes_read )
1036206e2921Sedward shu {
1037206e2921Sedward shu     pcitool_reg_t cfg_prg;
1038206e2921Sedward shu     int err = 0;
1039206e2921Sedward shu     int i = 0;
1040*cfae4096SHenry Zhao     nexus_t *nexus;
1041*cfae4096SHenry Zhao 
1042*cfae4096SHenry Zhao #ifdef __sparc
1043*cfae4096SHenry Zhao     nexus = find_nexus_for_dev(dev);
1044*cfae4096SHenry Zhao #else
1045*cfae4096SHenry Zhao     nexus = find_nexus_for_bus(dev->domain, dev->bus);
1046*cfae4096SHenry Zhao #endif
1047de97e7e4SAlan Coopersmith 
1048de97e7e4SAlan Coopersmith     *bytes_read = 0;
1049de97e7e4SAlan Coopersmith 
1050de97e7e4SAlan Coopersmith     if ( nexus == NULL ) {
1051de97e7e4SAlan Coopersmith 	return ENODEV;
1052de97e7e4SAlan Coopersmith     }
1053206e2921Sedward shu 
1054206e2921Sedward shu     cfg_prg.offset = offset;
1055206e2921Sedward shu     cfg_prg.acc_attr = PCITOOL_ACC_ATTR_SIZE_1 + NATIVE_ENDIAN;
1056206e2921Sedward shu     cfg_prg.bus_no = dev->bus;
1057206e2921Sedward shu     cfg_prg.dev_no = dev->dev;
1058206e2921Sedward shu     cfg_prg.func_no = dev->func;
1059206e2921Sedward shu     cfg_prg.barnum = 0;
1060206e2921Sedward shu     cfg_prg.user_version = PCITOOL_USER_VERSION;
1061206e2921Sedward shu 
10629a5565c7SAlan Coopersmith     for (i = 0; i < size; i += PCITOOL_ACC_ATTR_SIZE(PCITOOL_ACC_ATTR_SIZE_1))
10639a5565c7SAlan Coopersmith     {
1064206e2921Sedward shu 	cfg_prg.offset = offset + i;
10659a5565c7SAlan Coopersmith 
10669a5565c7SAlan Coopersmith 	if ((err = ioctl(nexus->fd, PCITOOL_DEVICE_GET_REG, &cfg_prg)) != 0) {
10675bf4b32cSAlan Coopersmith 	    fprintf(stderr, "read bdf<%s,%x,%x,%x,%llx> config space failure\n",
10685bf4b32cSAlan Coopersmith 		    nexus->path,
1069206e2921Sedward shu 		    cfg_prg.bus_no,
1070206e2921Sedward shu 		    cfg_prg.dev_no,
1071206e2921Sedward shu 		    cfg_prg.func_no,
1072206e2921Sedward shu 		    cfg_prg.offset);
1073206e2921Sedward shu 	    fprintf(stderr, "Failure cause = %x\n", err);
1074206e2921Sedward shu 	    break;
1075206e2921Sedward shu 	}
1076206e2921Sedward shu 
1077206e2921Sedward shu 	((uint8_t *)data)[i] = (uint8_t)cfg_prg.data;
1078206e2921Sedward shu 	/*
1079206e2921Sedward shu 	 * DWORDS Offset or bytes Offset ??
1080206e2921Sedward shu 	 */
1081206e2921Sedward shu     }
1082206e2921Sedward shu     *bytes_read = i;
1083206e2921Sedward shu 
1084206e2921Sedward shu     return (err);
1085206e2921Sedward shu }
1086206e2921Sedward shu 
1087206e2921Sedward shu /*
1088206e2921Sedward shu  * Solaris version
1089206e2921Sedward shu  */
1090206e2921Sedward shu static int
pci_device_solx_devfs_write(struct pci_device * dev,const void * data,pciaddr_t offset,pciaddr_t size,pciaddr_t * bytes_written)1091206e2921Sedward shu pci_device_solx_devfs_write( struct pci_device * dev, const void * data,
1092206e2921Sedward shu 			     pciaddr_t offset, pciaddr_t size,
1093206e2921Sedward shu 			     pciaddr_t * bytes_written )
1094206e2921Sedward shu {
1095206e2921Sedward shu     pcitool_reg_t cfg_prg;
1096206e2921Sedward shu     int err = 0;
1097206e2921Sedward shu     int cmd;
1098*cfae4096SHenry Zhao     nexus_t *nexus;
1099*cfae4096SHenry Zhao 
1100*cfae4096SHenry Zhao #ifdef __sparc
1101*cfae4096SHenry Zhao     nexus = find_nexus_for_dev(dev);
1102*cfae4096SHenry Zhao #else
1103*cfae4096SHenry Zhao     nexus = find_nexus_for_bus(dev->domain, dev->bus);
1104*cfae4096SHenry Zhao #endif
1105206e2921Sedward shu 
1106206e2921Sedward shu     if ( bytes_written != NULL ) {
1107206e2921Sedward shu 	*bytes_written = 0;
1108206e2921Sedward shu     }
1109206e2921Sedward shu 
1110de97e7e4SAlan Coopersmith     if ( nexus == NULL ) {
1111de97e7e4SAlan Coopersmith 	return ENODEV;
1112de97e7e4SAlan Coopersmith     }
1113de97e7e4SAlan Coopersmith 
1114206e2921Sedward shu     cfg_prg.offset = offset;
1115206e2921Sedward shu     switch (size) {
1116206e2921Sedward shu         case 1:
1117206e2921Sedward shu 	    cfg_prg.acc_attr = PCITOOL_ACC_ATTR_SIZE_1 + NATIVE_ENDIAN;
1118*cfae4096SHenry Zhao 	    cfg_prg.data = *((uint8_t *)data);
1119206e2921Sedward shu 	    break;
1120206e2921Sedward shu         case 2:
1121206e2921Sedward shu 	    cfg_prg.acc_attr = PCITOOL_ACC_ATTR_SIZE_2 + NATIVE_ENDIAN;
1122*cfae4096SHenry Zhao 	    cfg_prg.data = *((uint16_t *)data);
1123206e2921Sedward shu 	    break;
1124206e2921Sedward shu         case 4:
1125206e2921Sedward shu 	    cfg_prg.acc_attr = PCITOOL_ACC_ATTR_SIZE_4 + NATIVE_ENDIAN;
1126*cfae4096SHenry Zhao 	    cfg_prg.data = *((uint32_t *)data);
1127206e2921Sedward shu 	    break;
1128206e2921Sedward shu         case 8:
1129206e2921Sedward shu 	    cfg_prg.acc_attr = PCITOOL_ACC_ATTR_SIZE_8 + NATIVE_ENDIAN;
1130*cfae4096SHenry Zhao 	    cfg_prg.data = *((uint64_t *)data);
1131206e2921Sedward shu 	    break;
1132206e2921Sedward shu         default:
1133f537fc50SAlan Coopersmith 	    return EINVAL;
1134206e2921Sedward shu     }
1135206e2921Sedward shu     cfg_prg.bus_no = dev->bus;
1136206e2921Sedward shu     cfg_prg.dev_no = dev->dev;
1137206e2921Sedward shu     cfg_prg.func_no = dev->func;
1138206e2921Sedward shu     cfg_prg.barnum = 0;
1139206e2921Sedward shu     cfg_prg.user_version = PCITOOL_USER_VERSION;
11409a5565c7SAlan Coopersmith 
1141206e2921Sedward shu     /*
1142206e2921Sedward shu      * Check if this device is bridge device.
1143206e2921Sedward shu      * If it is, it is also a nexus node???
1144206e2921Sedward shu      * It seems that there is no explicit
1145206e2921Sedward shu      * PCI nexus device for X86, so not applicable
1146206e2921Sedward shu      * from pcitool_bus_reg_ops in pci_tools.c
1147206e2921Sedward shu      */
1148206e2921Sedward shu     cmd = PCITOOL_DEVICE_SET_REG;
1149206e2921Sedward shu 
1150de97e7e4SAlan Coopersmith     if ((err = ioctl(nexus->fd, cmd, &cfg_prg)) != 0) {
1151206e2921Sedward shu 	return (err);
1152206e2921Sedward shu     }
1153206e2921Sedward shu     *bytes_written = size;
1154206e2921Sedward shu 
1155206e2921Sedward shu     return (err);
1156206e2921Sedward shu }
1157206e2921Sedward shu 
1158206e2921Sedward shu 
115908ff9f7fSIan Romanick /**
116008ff9f7fSIan Romanick  * Map a memory region for a device using /dev/xsvc.
116108ff9f7fSIan Romanick  *
116208ff9f7fSIan Romanick  * \param dev   Device whose memory region is to be mapped.
116308ff9f7fSIan Romanick  * \param map   Parameters of the mapping that is to be created.
116408ff9f7fSIan Romanick  *
116508ff9f7fSIan Romanick  * \return
116608ff9f7fSIan Romanick  * Zero on success or an \c errno value on failure.
1167206e2921Sedward shu  */
1168206e2921Sedward shu static int
pci_device_solx_devfs_map_range(struct pci_device * dev,struct pci_device_mapping * map)116908ff9f7fSIan Romanick pci_device_solx_devfs_map_range(struct pci_device *dev,
117008ff9f7fSIan Romanick 				struct pci_device_mapping *map)
1171206e2921Sedward shu {
117208ff9f7fSIan Romanick     const int prot = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
117308ff9f7fSIan Romanick 			? (PROT_READ | PROT_WRITE) : PROT_READ;
11742ac461b2SAlan Coopersmith     int err = 0;
117508ff9f7fSIan Romanick 
1176*cfae4096SHenry Zhao #ifdef __sparc
1177*cfae4096SHenry Zhao     char	map_dev[128];
1178*cfae4096SHenry Zhao     int		map_fd;
1179*cfae4096SHenry Zhao 
1180*cfae4096SHenry Zhao     if (MAPPING_DEV_PATH(dev))
1181*cfae4096SHenry Zhao 	snprintf(map_dev, sizeof (map_dev), "%s%s", "/devices", MAPPING_DEV_PATH(dev));
1182*cfae4096SHenry Zhao     else
1183*cfae4096SHenry Zhao 	strcpy (map_dev, "/dev/fb0");
1184*cfae4096SHenry Zhao 
1185*cfae4096SHenry Zhao     if ((map_fd = open(map_dev, O_RDWR)) < 0) {
1186*cfae4096SHenry Zhao 	err = errno;
1187*cfae4096SHenry Zhao 	(void) fprintf(stderr, "can not open %s: %s\n", map_dev,
1188*cfae4096SHenry Zhao 			   strerror(errno));
1189*cfae4096SHenry Zhao 	return err;
1190*cfae4096SHenry Zhao     }
1191*cfae4096SHenry Zhao 
1192*cfae4096SHenry Zhao     map->memory = mmap(NULL, map->size, prot, MAP_SHARED, map_fd, map->base);
1193*cfae4096SHenry Zhao #else
1194f537fc50SAlan Coopersmith     /*
1195f537fc50SAlan Coopersmith      * Still used xsvc to do the user space mapping
1196f537fc50SAlan Coopersmith      */
1197206e2921Sedward shu     if (xsvc_fd < 0) {
1198206e2921Sedward shu 	if ((xsvc_fd = open("/dev/xsvc", O_RDWR)) < 0) {
1199f537fc50SAlan Coopersmith 	    err = errno;
1200f537fc50SAlan Coopersmith 	    (void) fprintf(stderr, "can not open /dev/xsvc: %s\n",
1201f537fc50SAlan Coopersmith 			   strerror(errno));
1202f537fc50SAlan Coopersmith 	    return err;
1203206e2921Sedward shu 	}
1204206e2921Sedward shu     }
1205206e2921Sedward shu 
12069a5565c7SAlan Coopersmith     map->memory = mmap(NULL, map->size, prot, MAP_SHARED, xsvc_fd, map->base);
1207*cfae4096SHenry Zhao #endif
1208*cfae4096SHenry Zhao 
120908ff9f7fSIan Romanick     if (map->memory == MAP_FAILED) {
121008ff9f7fSIan Romanick 	err = errno;
1211206e2921Sedward shu 
1212f537fc50SAlan Coopersmith 	(void) fprintf(stderr, "map rom region =%llx failed: %s\n",
1213f537fc50SAlan Coopersmith 		       map->base, strerror(errno));
1214206e2921Sedward shu     }
1215206e2921Sedward shu 
1216*cfae4096SHenry Zhao #ifdef __sparc
1217*cfae4096SHenry Zhao     close (map_fd);
1218*cfae4096SHenry Zhao #endif
1219*cfae4096SHenry Zhao 
122008ff9f7fSIan Romanick     return err;
1221206e2921Sedward shu }
1222