xref: /openbsd-src/sys/dev/pci/pci_subr.c (revision f9914ca2e573ae8994044d4cf63dd23ddcd0619c)
1 /*	$OpenBSD: pci_subr.c,v 1.16 2004/10/23 16:06:07 brad Exp $	*/
2 /*	$NetBSD: pci_subr.c,v 1.19 1996/10/13 01:38:29 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1995, 1996 Christopher G. Demetriou.  All rights reserved.
6  * Copyright (c) 1994 Charles Hannum.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by Charles Hannum.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * PCI autoconfiguration support functions.
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/device.h>
41 
42 #include <dev/pci/pcireg.h>
43 #include <dev/pci/pcivar.h>
44 #ifdef PCIVERBOSE
45 #include <dev/pci/pcidevs.h>
46 #endif
47 
48 /*
49  * Descriptions of known PCI classes and subclasses.
50  *
51  * Subclasses are described in the same way as classes, but have a
52  * NULL subclass pointer.
53  */
54 struct pci_class {
55 	const char	*name;
56 	int		val;		/* as wide as pci_{,sub}class_t */
57 	const struct pci_class *subclasses;
58 };
59 
60 const struct pci_class pci_subclass_prehistoric[] = {
61 	{ "miscellaneous",	PCI_SUBCLASS_PREHISTORIC_MISC,		},
62 	{ "VGA",		PCI_SUBCLASS_PREHISTORIC_VGA,		},
63 	{ 0 }
64 };
65 
66 const struct pci_class pci_subclass_mass_storage[] = {
67 	{ "SCSI",		PCI_SUBCLASS_MASS_STORAGE_SCSI,		},
68 	{ "IDE",		PCI_SUBCLASS_MASS_STORAGE_IDE,		},
69 	{ "floppy",		PCI_SUBCLASS_MASS_STORAGE_FLOPPY,	},
70 	{ "IPI",		PCI_SUBCLASS_MASS_STORAGE_IPI,		},
71 	{ "RAID",		PCI_SUBCLASS_MASS_STORAGE_RAID,		},
72 	{ "ATA",		PCI_SUBCLASS_MASS_STORAGE_ATA,		},
73 	{ "SATA",		PCI_SUBCLASS_MASS_STORAGE_SATA,		},
74 	{ "miscellaneous",	PCI_SUBCLASS_MASS_STORAGE_MISC,		},
75 	{ 0 },
76 };
77 
78 const struct pci_class pci_subclass_network[] = {
79 	{ "ethernet",		PCI_SUBCLASS_NETWORK_ETHERNET,		},
80 	{ "token ring",		PCI_SUBCLASS_NETWORK_TOKENRING,		},
81 	{ "FDDI",		PCI_SUBCLASS_NETWORK_FDDI,		},
82 	{ "ATM",		PCI_SUBCLASS_NETWORK_ATM,		},
83 	{ "ISDN",		PCI_SUBCLASS_NETWORK_ISDN,		},
84 	{ "WorldFip",		PCI_SUBCLASS_NETWORK_WORLDFIP,		},
85 	{ "PCMIG Multi Computing", PCI_SUBCLASS_NETWORK_PCIMGMULTICOMP,	},
86 	{ "miscellaneous",	PCI_SUBCLASS_NETWORK_MISC,		},
87 	{ 0 },
88 };
89 
90 const struct pci_class pci_subclass_display[] = {
91 	{ "VGA",		PCI_SUBCLASS_DISPLAY_VGA,		},
92 	{ "XGA",		PCI_SUBCLASS_DISPLAY_XGA,		},
93 	{ "3D",			PCI_SUBCLASS_DISPLAY_3D,		},
94 	{ "miscellaneous",	PCI_SUBCLASS_DISPLAY_MISC,		},
95 	{ 0 },
96 };
97 
98 const struct pci_class pci_subclass_multimedia[] = {
99 	{ "video",		PCI_SUBCLASS_MULTIMEDIA_VIDEO,		},
100 	{ "audio",		PCI_SUBCLASS_MULTIMEDIA_AUDIO,		},
101 	{ "telephony",		PCI_SUBCLASS_MULTIMEDIA_TELEPHONY,	},
102 	{ "miscellaneous",	PCI_SUBCLASS_MULTIMEDIA_MISC,		},
103 	{ 0 },
104 };
105 
106 const struct pci_class pci_subclass_memory[] = {
107 	{ "RAM",		PCI_SUBCLASS_MEMORY_RAM,		},
108 	{ "flash",		PCI_SUBCLASS_MEMORY_FLASH,		},
109 	{ "miscellaneous",	PCI_SUBCLASS_MEMORY_MISC,		},
110 	{ 0 },
111 };
112 
113 const struct pci_class pci_subclass_bridge[] = {
114 	{ "host",		PCI_SUBCLASS_BRIDGE_HOST,		},
115 	{ "ISA",		PCI_SUBCLASS_BRIDGE_ISA,		},
116 	{ "EISA",		PCI_SUBCLASS_BRIDGE_EISA,		},
117 	{ "MicroChannel",	PCI_SUBCLASS_BRIDGE_MC,			},
118 	{ "PCI",		PCI_SUBCLASS_BRIDGE_PCI,		},
119 	{ "PCMCIA",		PCI_SUBCLASS_BRIDGE_PCMCIA,		},
120 	{ "NuBus",		PCI_SUBCLASS_BRIDGE_NUBUS,		},
121 	{ "CardBus",		PCI_SUBCLASS_BRIDGE_CARDBUS,		},
122 	{ "RACEway",		PCI_SUBCLASS_BRIDGE_RACEWAY,		},
123 	{ "Semi-transparent PCI", PCI_SUBCLASS_BRIDGE_STPCI,		},
124 	{ "InfiniBand",		PCI_SUBCLASS_BRIDGE_INFINIBAND,		},
125 	{ "miscellaneous",	PCI_SUBCLASS_BRIDGE_MISC,		},
126 	{ 0 },
127 };
128 
129 const struct pci_class pci_subclass_communications[] = {
130 	{ "serial",		PCI_SUBCLASS_COMMUNICATIONS_SERIAL,	},
131 	{ "parallel",		PCI_SUBCLASS_COMMUNICATIONS_PARALLEL,	},
132 	{ "multi-port serial",	PCI_SUBCLASS_COMMUNICATIONS_MPSERIAL,	},
133 	{ "modem",		PCI_SUBCLASS_COMMUNICATIONS_MODEM,	},
134 	{ "GPIB",		PCI_SUBCLASS_COMMUNICATIONS_GPIB,	},
135 	{ "smartcard",		PCI_SUBCLASS_COMMUNICATIONS_SMARTCARD,	},
136 	{ "miscellaneous",	PCI_SUBCLASS_COMMUNICATIONS_MISC,	},
137 	{ 0 },
138 };
139 
140 const struct pci_class pci_subclass_system[] = {
141 	{ "interrupt",		PCI_SUBCLASS_SYSTEM_PIC,		},
142 	{ "8237 DMA",		PCI_SUBCLASS_SYSTEM_DMA,		},
143 	{ "8254 timer",		PCI_SUBCLASS_SYSTEM_TIMER,		},
144 	{ "RTC",		PCI_SUBCLASS_SYSTEM_RTC,		},
145 	{ "PCI Hot-Plug",	PCI_SUBCLASS_SYSTEM_PCIHOTPLUG,		},
146 	{ "miscellaneous",	PCI_SUBCLASS_SYSTEM_MISC,		},
147 	{ 0 },
148 };
149 
150 const struct pci_class pci_subclass_input[] = {
151 	{ "keyboard",		PCI_SUBCLASS_INPUT_KEYBOARD,		},
152 	{ "digitizer",		PCI_SUBCLASS_INPUT_DIGITIZER,		},
153 	{ "mouse",		PCI_SUBCLASS_INPUT_MOUSE,		},
154 	{ "scanner",		PCI_SUBCLASS_INPUT_SCANNER,		},
155 	{ "game port",		PCI_SUBCLASS_INPUT_GAMEPORT,		},
156 	{ "miscellaneous",	PCI_SUBCLASS_INPUT_MISC,		},
157 	{ 0 },
158 };
159 
160 const struct pci_class pci_subclass_dock[] = {
161 	{ "generic",		PCI_SUBCLASS_DOCK_GENERIC,		},
162 	{ "miscellaneous",	PCI_SUBCLASS_DOCK_MISC,			},
163 	{ 0 },
164 };
165 
166 const struct pci_class pci_subclass_processor[] = {
167 	{ "386",		PCI_SUBCLASS_PROCESSOR_386,		},
168 	{ "486",		PCI_SUBCLASS_PROCESSOR_486,		},
169 	{ "Pentium",		PCI_SUBCLASS_PROCESSOR_PENTIUM,		},
170 	{ "Alpha",		PCI_SUBCLASS_PROCESSOR_ALPHA,		},
171 	{ "PowerPC",		PCI_SUBCLASS_PROCESSOR_POWERPC,		},
172 	{ "MIPS",		PCI_SUBCLASS_PROCESSOR_MIPS,		},
173 	{ "Co-processor",	PCI_SUBCLASS_PROCESSOR_COPROC,		},
174 	{ 0 },
175 };
176 
177 const struct pci_class pci_subclass_serialbus[] = {
178 	{ "Firewire",		PCI_SUBCLASS_SERIALBUS_FIREWIRE,	},
179 	{ "ACCESS.bus",		PCI_SUBCLASS_SERIALBUS_ACCESS,		},
180 	{ "SSA",		PCI_SUBCLASS_SERIALBUS_SSA,		},
181 	{ "USB",		PCI_SUBCLASS_SERIALBUS_USB,		},
182 	/* XXX Fiber Channel/_FIBRECHANNEL */
183 	{ "Fiber Channel",	PCI_SUBCLASS_SERIALBUS_FIBER,		},
184 	{ "SMBus",		PCI_SUBCLASS_SERIALBUS_SMBUS,		},
185 	{ "InfiniBand",		PCI_SUBCLASS_SERIALBUS_INFINIBAND,	},
186 	{ "IPMI",		PCI_SUBCLASS_SERIALBUS_IPMI,		},
187 	{ "SERCOS",		PCI_SUBCLASS_SERIALBUS_SERCOS,		},
188 	{ "CANbus",		PCI_SUBCLASS_SERIALBUS_CANBUS,		},
189 	{ 0 },
190 };
191 
192 const struct pci_class pci_subclass_wireless[] = {
193 	{ "IrDA",		PCI_SUBCLASS_WIRELESS_IRDA,		},
194 	{ "Consumer IR",	PCI_SUBCLASS_WIRELESS_CONSUMERIR,	},
195 	{ "RF",			PCI_SUBCLASS_WIRELESS_RF,		},
196 	{ "bluetooth",		PCI_SUBCLASS_WIRELESS_BLUETOOTH,	},
197 	{ "broadband",		PCI_SUBCLASS_WIRELESS_BROADBAND,	},
198 	{ "802.11a (5 GHz)",	PCI_SUBCLASS_WIRELESS_802_11A,		},
199 	{ "802.11b (2.4 GHz)",	PCI_SUBCLASS_WIRELESS_802_11B,		},
200 	{ "miscellaneous",	PCI_SUBCLASS_WIRELESS_MISC,		},
201 	{ 0 },
202 };
203 
204 const struct pci_class pci_subclass_i2o[] = {
205 	{ "standard",		PCI_SUBCLASS_I2O_STANDARD,		},
206 	{ 0 },
207 };
208 
209 const struct pci_class pci_subclass_satcom[] = {
210 	{ "TV",			PCI_SUBCLASS_SATCOM_TV,			},
211 	{ "audio",		PCI_SUBCLASS_SATCOM_AUDIO,		},
212 	{ "voice",		PCI_SUBCLASS_SATCOM_VOICE,		},
213 	{ "data",		PCI_SUBCLASS_SATCOM_DATA,		},
214 	{ 0 },
215 };
216 
217 const struct pci_class pci_subclass_crypto[] = {
218 	{ "network/computing",	PCI_SUBCLASS_CRYPTO_NETCOMP,		},
219 	{ "entertainment",	PCI_SUBCLASS_CRYPTO_ENTERTAINMENT,	},
220 	{ "miscellaneous",	PCI_SUBCLASS_CRYPTO_MISC,		},
221 	{ 0 },
222 };
223 
224 const struct pci_class pci_subclass_dasp[] = {
225 	{ "DPIO",		PCI_SUBCLASS_DASP_DPIO,			},
226 	{ "Time and Frequency",	PCI_SUBCLASS_DASP_TIMEFREQ,		},
227 	{ "synchronization",	PCI_SUBCLASS_DASP_SYNC,			},
228 	{ "management",		PCI_SUBCLASS_DASP_MGMT,			},
229 	{ "miscellaneous",	PCI_SUBCLASS_DASP_MISC,			},
230 	{ 0 },
231 };
232 
233 const struct pci_class pci_class[] = {
234 	{ "prehistoric",	PCI_CLASS_PREHISTORIC,
235 	    pci_subclass_prehistoric,				},
236 	{ "mass storage",	PCI_CLASS_MASS_STORAGE,
237 	    pci_subclass_mass_storage,				},
238 	{ "network",		PCI_CLASS_NETWORK,
239 	    pci_subclass_network,				},
240 	{ "display",		PCI_CLASS_DISPLAY,
241 	    pci_subclass_display,				},
242 	{ "multimedia",		PCI_CLASS_MULTIMEDIA,
243 	    pci_subclass_multimedia,				},
244 	{ "memory",		PCI_CLASS_MEMORY,
245 	    pci_subclass_memory,				},
246 	{ "bridge",		PCI_CLASS_BRIDGE,
247 	    pci_subclass_bridge,				},
248 	{ "communications",	PCI_CLASS_COMMUNICATIONS,
249 	    pci_subclass_communications,			},
250 	{ "system",		PCI_CLASS_SYSTEM,
251 	    pci_subclass_system,				},
252 	{ "input",		PCI_CLASS_INPUT,
253 	    pci_subclass_input,					},
254 	{ "dock",		PCI_CLASS_DOCK,
255 	    pci_subclass_dock,					},
256 	{ "processor",		PCI_CLASS_PROCESSOR,
257 	    pci_subclass_processor,				},
258 	{ "serial bus",		PCI_CLASS_SERIALBUS,
259 	    pci_subclass_serialbus,				},
260 	{ "wireless",		PCI_CLASS_WIRELESS,
261 	    pci_subclass_wireless,				},
262 	{ "I2O",		PCI_CLASS_I2O,
263 	    pci_subclass_i2o,					},
264 	{ "satellite comm",	PCI_CLASS_SATCOM,
265 	    pci_subclass_satcom,				},
266 	{ "crypto",		PCI_CLASS_CRYPTO,
267 	    pci_subclass_crypto,				},
268 	{ "DASP",		PCI_CLASS_DASP,
269 	    pci_subclass_dasp,					},
270 	{ "undefined",		PCI_CLASS_UNDEFINED,
271 	    0,							},
272 	{ 0 },
273 };
274 
275 #ifdef PCIVERBOSE
276 /*
277  * Descriptions of known vendors and devices ("products").
278  */
279 struct pci_known_vendor {
280 	pci_vendor_id_t		vendor;
281 	const char		*vendorname;
282 };
283 
284 struct pci_known_product {
285 	pci_vendor_id_t		vendor;
286 	pci_product_id_t	product;
287 	const char		*productname;
288 };
289 
290 #include <dev/pci/pcidevs_data.h>
291 #endif /* PCIVERBOSE */
292 
293 const char *
294 pci_findvendor(pcireg_t id_reg)
295 {
296 #ifdef PCIVERBOSE
297 	pci_vendor_id_t vendor = PCI_VENDOR(id_reg);
298 	const struct pci_known_vendor *kdp;
299 
300 	kdp = pci_known_vendors;
301         while (kdp->vendorname != NULL) {	/* all have vendor name */
302                 if (kdp->vendor == vendor)
303                         break;
304 		kdp++;
305 	}
306         return (kdp->vendorname);
307 #else
308 	return (NULL);
309 #endif
310 }
311 
312 void
313 pci_devinfo(pcireg_t id_reg, pcireg_t class_reg, int showclass, char *cp,
314 	    size_t cp_max)
315 {
316 	pci_vendor_id_t vendor;
317 	pci_product_id_t product;
318 	pci_class_t class;
319 	pci_subclass_t subclass;
320 	pci_interface_t interface;
321 	pci_revision_t revision;
322 	const char *vendor_namep = NULL, *product_namep = NULL;
323 	const struct pci_class *classp, *subclassp;
324 	size_t cp_len = 0;
325 #ifdef PCIVERBOSE
326 	const struct pci_known_vendor *pkv;
327 	const struct pci_known_product *pkp;
328 	const char *unmatched = "unknown ";
329 #else
330 	const char *unmatched = "";
331 #endif
332 
333 	vendor = PCI_VENDOR(id_reg);
334 	product = PCI_PRODUCT(id_reg);
335 
336 	class = PCI_CLASS(class_reg);
337 	subclass = PCI_SUBCLASS(class_reg);
338 	interface = PCI_INTERFACE(class_reg);
339 	revision = PCI_REVISION(class_reg);
340 
341 #ifdef PCIVERBOSE
342 	pkv = pci_known_vendors;
343 	while (pkv->vendorname != NULL) {	/* all have vendor name */
344 		if (pkv->vendor == vendor) {
345 			vendor_namep = pkv->vendorname;
346 			break;
347 		}
348 		pkv++;
349 	}
350 	if (vendor_namep) {
351 		pkp = pci_known_products;
352 		while (pkp->productname != NULL) {	/* all have product name */
353 			if (pkp->vendor == vendor && pkp->product == product) {
354 				product_namep = pkp->productname;
355 				break;
356 			}
357 			pkp++;
358 		}
359 	}
360 #endif /* PCIVERBOSE */
361 
362 	classp = pci_class;
363 	while (classp->name != NULL) {
364 		if (class == classp->val)
365 			break;
366 		classp++;
367 	}
368 
369 	subclassp = (classp->name != NULL) ? classp->subclasses : NULL;
370 	while (subclassp && subclassp->name != NULL) {
371 		if (subclass == subclassp->val)
372 			break;
373 		subclassp++;
374 	}
375 
376 	if (vendor_namep == NULL)
377 		snprintf(cp, cp_max, "%svendor 0x%04x product 0x%04x",
378 		    unmatched, vendor, product);
379 	else if (product_namep != NULL)
380 		snprintf(cp, cp_max, "\"%s %s\"", vendor_namep, product_namep);
381 	else
382 		snprintf(cp, cp_max, "vendor \"%s\", unknown product 0x%x",
383 		    vendor_namep, product);
384 	if (showclass && product_namep == NULL) {
385 		strlcat(cp, " (", cp_max);
386 		cp_len = strlen(cp);
387 		if (classp->name == NULL)
388 			snprintf(cp + cp_len, cp_max - cp_len,
389 			    "unknown class 0x%02x, subclass 0x%02x",
390 			    class, subclass);
391 		else if (subclassp == NULL || subclassp->name == NULL)
392 			snprintf(cp + cp_len, cp_max - cp_len,
393 			    "class %s unknown subclass 0x%02x", classp->name,
394 			    subclass);
395 		else
396 			snprintf(cp + cp_len, cp_max - cp_len,
397 			    "class %s subclass %s", classp->name,
398 			    subclassp->name);
399 #if 0 /* not very useful */
400 		cp_len = strlen(cp);
401 		snprintf(cp + cp_len, cp_max - cp_len,
402 		    ", interface 0x%02x", interface);
403 #endif
404 		cp_len = strlen(cp);
405 		snprintf(cp + cp_len, cp_max - cp_len,
406 		    ", rev 0x%02x)", revision);
407 	} else {
408 		cp_len = strlen(cp);
409 		snprintf(cp + cp_len, cp_max - cp_len, " rev 0x%02x",
410 		    revision);
411 	}
412 }
413