2 * The PCI Library -- AIX /dev/pci[0-n] access
4 * Copyright (c) 1999 Jari Kirma <kirma@cs.hut.fi>
6 * Can be freely distributed and used under the terms of the GNU GPL v2+.
8 * SPDX-License-Identifier: GPL-2.0-or-later
12 * Read functionality of this driver is briefly tested, and seems
13 * to supply basic information correctly, but I promise no more.
22 #include <sys/types.h>
27 #define AIX_LSDEV_CMD "/usr/sbin/lsdev -C -c bus -t pci\\* -S available -F name"
28 #define AIX_ODMGET_CMD \
29 "/usr/bin/odmget -q 'name=%s and attribute=bus_number' CuAt | \
30 /usr/bin/awk '$1 == \"value\" { print $3 }'"
33 /* AIX PCI bus device information */
35 typedef struct aix_pci_bus {
41 #define PCI_BUS_MAX 16 /* arbitrary choice */
42 static aix_pci_bus pci_buses[PCI_BUS_MAX];
43 static int pci_bus_count = 0;
46 /* Utility Routines */
49 aix_find_bus(struct pci_access *a, int bus_number)
53 for (i = 0; i < pci_bus_count; i++)
55 if (pci_buses[i].bus_number == bus_number)
61 a->error("aix_find_bus: bus number %d not found", bus_number);
65 aix_bus_open(struct pci_dev *d)
67 struct pci_access *a = d->access;
68 aix_pci_bus *bp = aix_find_bus(a, d->bus);
73 int mode = a->writeable ? O_RDWR : O_RDONLY;
75 snprintf(devbuf, sizeof (devbuf), "/dev/%s", bp->bus_name);
76 bp->bus_fd = open(devbuf, mode, 0);
78 a->error("aix_open_bus: %s open failed", devbuf);
85 aix_bus_number(char *name)
94 snprintf(command, sizeof (command), AIX_ODMGET_CMD, name);
95 odmget_pipe = popen(command, "r");
96 if (odmget_pipe == NULL)
102 if (fgets(buf, sizeof (buf) - 1, odmget_pipe) != NULL)
104 bp = buf + 1; /* skip leading double quote */
105 bus_number = strtol(bp, &ep, 0);
114 /* first PCI bus_number is not recorded in ODM CuAt; default to 0 */
118 (void) pclose(odmget_pipe);
127 aix_detect(struct pci_access *a)
130 int mode = a->writeable ? W_OK : R_OK;
131 char *command = AIX_LSDEV_CMD;
136 lsdev_pipe = popen(command, "r");
137 if (lsdev_pipe == NULL)
139 a->error("aix_config: popen(\"%s\") failed", command);
142 while (fgets(buf, sizeof (buf) - 1, lsdev_pipe) != NULL)
145 while (buf[len-1] == '\n' || buf[len-1] == '\r')
147 buf[len] = '\0'; /* clobber the newline */
149 name = (char *) pci_malloc(a, len + 1);
151 pci_buses[pci_bus_count].bus_name = name;
152 pci_buses[pci_bus_count].bus_number = 0;
153 pci_buses[pci_bus_count].bus_fd = -1;
155 a->debug("...using %s", name);
157 a->debug(", %s", name);
159 if (pci_bus_count >= PCI_BUS_MAX)
163 (void) pclose(lsdev_pipe);
165 return pci_bus_count;
169 aix_init(struct pci_access *a)
174 for (i = 0; i < pci_bus_count; i++)
176 name = pci_buses[i].bus_name;
177 pci_buses[i].bus_number = aix_bus_number(name);
182 aix_cleanup(struct pci_access *a)
186 while (pci_bus_count-- > 0)
188 bp = &pci_buses[pci_bus_count];
189 (void) free(bp->bus_name);
192 (void) close(bp->bus_fd);
199 aix_scan(struct pci_access *a)
205 memset(busmap, 0, sizeof(busmap));
206 for (i = 0; i < pci_bus_count; i++)
208 bus_number = pci_buses[i].bus_number;
209 if (!busmap[bus_number])
211 pci_generic_scan_bus(a, busmap, 0, bus_number);
217 aix_read(struct pci_dev *d, int pos, byte *buf, int len)
222 if (d->domain || pos + len > 256)
225 fd = aix_bus_open(d);
226 mdio.md_addr = (ulong) pos;
228 mdio.md_incr = MV_BYTE;
229 mdio.md_data = (char *) buf;
230 mdio.md_sla = PCI_DEVFN(d->dev, d->func);
232 if (ioctl(fd, MIOPCFGET, &mdio) < 0)
233 d->access->error("aix_read: ioctl(MIOPCFGET) failed");
239 aix_write(struct pci_dev *d, int pos, byte *buf, int len)
244 if (d->domain || pos + len > 256)
247 fd = aix_bus_open(d);
248 mdio.md_addr = (ulong) pos;
250 mdio.md_incr = MV_BYTE;
251 mdio.md_data = (char *) buf;
252 mdio.md_sla = PCI_DEVFN(d->dev, d->func);
254 if (ioctl(fd, MIOPCFPUT, &mdio) < 0)
256 d->access->error("aix_write: ioctl(MIOPCFPUT) failed");
262 struct pci_methods pm_aix_device = {
263 .name = "aix-device",
264 .help = "AIX /dev/pci[0-n]",
265 .detect = aix_detect,
267 .cleanup = aix_cleanup,
269 .fill_info = pci_generic_fill_info,