]> mj.ucw.cz Git - pciutils.git/blob - lib/aix-device.c
Arguments now correspond to the format string
[pciutils.git] / lib / aix-device.c
1 /*
2  *      The PCI Library -- AIX /dev/pci[0-n] access
3  *
4  *      Copyright (c) 1999 Jari Kirma <kirma@cs.hut.fi>
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL.
7  */
8
9 /*
10  *      Read functionality of this driver is briefly tested, and seems
11  *      to supply basic information correctly, but I promise no more.
12  */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19
20 #include <sys/types.h>
21 #include <sys/mdio.h>
22
23 #include "internal.h"
24
25 #define AIX_LSDEV_CMD "/usr/sbin/lsdev -C -c bus -t pci\\* -S available -F name"
26 #define AIX_ODMGET_CMD \
27   "/usr/bin/odmget -q 'name=%s and attribute=bus_number' CuAt | \
28    /usr/bin/awk '$1 == \"value\" { print $3 }'"
29
30
31 /* AIX PCI bus device information */
32
33 typedef struct aix_pci_bus {
34     char *bus_name;
35     int   bus_number;
36     int   bus_fd;
37 } aix_pci_bus;
38
39 #define PCI_BUS_MAX 16          /* arbitrary choice */
40 static aix_pci_bus pci_buses[PCI_BUS_MAX];
41 static int pci_bus_count = 0;
42
43
44 /* Utility Routines */
45
46 static aix_pci_bus *
47 aix_find_bus(struct pci_access *a, int bus_number)
48 {
49   int i;
50
51   for (i = 0; i < pci_bus_count; i++)
52     {
53       if (pci_buses[i].bus_number == bus_number)
54         {
55           return &pci_buses[i];
56         }
57     }
58
59   a->error("aix_find_bus: bus number %d not found", bus_number);
60 }
61
62 static int
63 aix_bus_open(struct pci_access *a, int bus_number)
64 {
65   aix_pci_bus *bp = aix_find_bus(a, bus_number);
66
67   if (bp->bus_fd < 0)
68     {
69       char devbuf[256];
70       int mode = a->writeable ? O_RDWR : O_RDONLY;
71
72       snprintf(devbuf, sizeof (devbuf), "/dev/%s", bp->bus_name);
73       bp->bus_fd = open(devbuf, mode, 0);
74       if (bp->bus_fd < 0)
75         {
76           a->error("aix_open_bus: %s open failed", devbuf);
77         }
78     }
79
80   return bp->bus_fd;
81 }
82
83 static int
84 aix_bus_number(char *name)
85 {
86   int bus_number;
87   FILE *odmget_pipe;
88   char command[256];
89   char buf[256];
90   char *bp;
91   char *ep;
92
93   snprintf(command, sizeof (command), AIX_ODMGET_CMD, name);
94   odmget_pipe = popen(command, "r");
95   if (odmget_pipe == NULL)
96     {
97       /* popen failed */
98       return -1;
99     }
100
101   if (fgets(buf, sizeof (buf) - 1, odmget_pipe) != NULL)
102     {
103       bp = buf + 1;     /* skip leading double quote */
104       bus_number = strtol(bp, &ep, 0);
105       if (bp == ep)
106         {
107           /* strtol failed */
108           bus_number = -1;
109         }
110     }
111   else
112     {
113       /* first PCI bus_number is not recorded in ODM CuAt; default to 0 */
114       bus_number = 0;
115     }
116
117   (void) pclose(odmget_pipe);
118
119   return bus_number;
120 }
121
122
123 /* Method entries */
124
125 static void
126 aix_config(struct pci_access *a)
127 {
128   a->method_params[PCI_ACCESS_AIX_DEVICE] = NULL;
129 }
130
131 static int
132 aix_detect(struct pci_access *a)
133 {
134   int len;
135   int mode = a->writeable ? W_OK : R_OK;
136   char *command = AIX_LSDEV_CMD;
137   FILE *lsdev_pipe;
138   char buf[256];
139   char *name;
140
141   lsdev_pipe = popen(command, "r");
142   if (lsdev_pipe == NULL)
143     {
144       a->error("aix_config: popen(\"%s\") failed", command);
145     }
146
147   while (fgets(buf, sizeof (buf) - 1, lsdev_pipe) != NULL)
148     {
149       len = strlen(buf);
150       while (buf[len-1] == '\n' || buf[len-1] == '\r')
151           len--;
152       buf[len] = '\0';                          /* clobber the newline */
153
154       name = (char *) pci_malloc(a, len + 1);
155       strcpy(name, buf);
156       pci_buses[pci_bus_count].bus_name = name;
157       pci_buses[pci_bus_count].bus_number = 0;
158       pci_buses[pci_bus_count].bus_fd = -1;
159       if (!pci_bus_count)
160           a->debug("...using %s", name);
161       else
162           a->debug(", %s", name);
163       pci_bus_count++;
164       if (pci_bus_count >= PCI_BUS_MAX)
165           break;
166     }
167
168   (void) pclose(lsdev_pipe);
169
170   return pci_bus_count;
171 }
172
173 static void
174 aix_init(struct pci_access *a)
175 {
176   char *name;
177   int i;
178
179   for (i = 0; i < pci_bus_count; i++)
180     {
181       name = pci_buses[i].bus_name;
182       pci_buses[i].bus_number = aix_bus_number(name);
183     }
184 }
185
186 static void
187 aix_cleanup(struct pci_access *a)
188 {
189   aix_pci_bus *bp;
190
191   while (pci_bus_count-- > 0)
192     {
193       bp = &pci_buses[pci_bus_count];
194       (void) free(bp->bus_name);
195       if (bp->bus_fd >= 0)
196         {
197           (void) close(bp->bus_fd);
198           bp->bus_fd = -1;
199         }
200     }
201 }
202
203 void
204 aix_scan(struct pci_access *a)
205 {
206   int i;
207   int bus_number;
208   byte busmap[256];
209
210   bzero(busmap, sizeof(busmap));
211   for (i = 0; i < pci_bus_count; i++)
212     {
213       bus_number = pci_buses[i].bus_number;
214       if (!busmap[bus_number])
215         {
216           pci_generic_scan_bus(a, busmap, bus_number);
217         }
218     }
219 }
220
221 static int
222 aix_read(struct pci_dev *d, int pos, byte *buf, int len)
223 {
224   struct mdio mdio;
225   int fd;
226
227   if (pos + len > 256)
228     return 0;
229
230   fd = aix_bus_open(d->access, d->bus);
231   mdio.md_addr = (ulong) pos;
232   mdio.md_size = len;
233   mdio.md_incr = MV_BYTE;
234   mdio.md_data = (char *) buf;
235   mdio.md_sla = PCI_DEVFN(d->dev, d->func);
236
237   if (ioctl(fd, MIOPCFGET, &mdio) < 0)
238     d->access->error("aix_read: ioctl(MIOPCFGET) failed");
239   
240   return 1;
241 }
242
243 static int
244 aix_write(struct pci_dev *d, int pos, byte *buf, int len)
245 {
246   struct mdio mdio;
247   int fd;
248
249   if (pos + len > 256)
250     return 0;
251
252   fd = aix_bus_open(d->access, d->bus);
253   mdio.md_addr = (ulong) pos;
254   mdio.md_size = len;
255   mdio.md_incr = MV_BYTE;
256   mdio.md_data = (char *) buf;
257   mdio.md_sla = PCI_DEVFN(d->dev, d->func);
258
259   if (ioctl(fd, MIOPCFPUT, &mdio) < 0)
260     {
261       d->access->error("aix_write: ioctl(MIOPCFPUT) failed");
262     }
263
264   return 1;
265 }
266
267 struct pci_methods pm_aix_device = {
268   "AIX-device",
269   aix_config,
270   aix_detect,
271   aix_init,
272   aix_cleanup,
273   aix_scan,
274   pci_generic_fill_info,
275   aix_read,
276   aix_write,
277   NULL,                                 /* dev_init */
278   NULL                                  /* dev_cleanup */
279 };