xref: /titanic_51/usr/src/cmd/biosdev/biosdev.c (revision 56dd2b4519ecc7832e0656be6ddafdd86c164b25)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <sys/param.h>
33 #include <errno.h>
34 #include <sys/types.h>
35 #include <dirent.h>
36 #include <libdevinfo.h>
37 #include <fcntl.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <sys/pci.h>
41 #include <sys/biosdisk.h>
42 
43 
44 /*
45  * structure used for searching device tree for a node matching
46  * pci bus/dev/fn
47  */
48 typedef struct pcibdf {
49 	int busnum;
50 	int devnum;
51 	int funcnum;
52 	di_node_t	di_node;
53 } pcibdf_t;
54 
55 /*
56  * structure used for searching device tree for a node matching
57  * USB serial number.
58  */
59 typedef struct {
60 	uint64_t serialno;
61 	di_node_t	node;
62 } usbser_t;
63 
64 /*
65  * structure for holding the mapping info
66  */
67 typedef struct {
68 	int disklist_index;	/* index to disk_list of the mapped path */
69 	int matchcount;		/* number of matches per this device number */
70 } mapinfo_t;
71 
72 #define	DEVFS_PREFIX "/devices"
73 #define	DISKS_LIST_INCR		20	/* increment for resizing disk_list */
74 
75 #define	BIOSPROPNAME_TMPL	"biosdev-0x%x"
76 #define	BIOSPROPNAME_TMPL_LEN	13
77 #define	BIOSDEV_NUM		8
78 #define	STARTING_DRVNUM		0x80
79 
80 /*
81  * array to hold mappings. Element at index X corresponds to BIOS device
82  * number 0x80 + X
83  */
84 static mapinfo_t mapinfo[BIOSDEV_NUM];
85 
86 /*
87  * Cache copy of kernel device tree snapshot root handle, includes devices
88  * that are detached
89  */
90 static di_node_t root_node = DI_NODE_NIL;
91 
92 /*
93  * kernel device tree snapshot with currently attached devices. Detached
94  * devices are not included.
95  */
96 static di_node_t root_allnode = DI_NODE_NIL;
97 
98 /*
99  * handle to retrieve prom properties
100  */
101 
102 static di_prom_handle_t prom_hdl = DI_PROM_HANDLE_NIL;
103 
104 static char **disk_list = NULL;	/* array of physical device pathnames */
105 static int disk_list_len = 0;		/* length of disk_list */
106 static int disk_list_valid = 0;	/* number of valid entries in disk_list */
107 
108 static int debug = 0;			/* used for enabling debug output */
109 
110 
111 /* Local function prototypes */
112 static void new_disk_list_entry(di_node_t node);
113 static int i_disktype(di_node_t node, di_minor_t minor, void *arg);
114 static void build_disk_list();
115 static int search_disklist_match_path(char *path);
116 static void free_disks();
117 static void cleanup_and_exit(int);
118 
119 static int match_edd(biosdev_data_t *bd);
120 static int match_first_block(biosdev_data_t *bd);
121 
122 static di_node_t search_tree_match_pcibdf(di_node_t node, int bus, int dev,
123     int fn);
124 static int i_match_pcibdf(di_node_t node, void *arg);
125 
126 static di_node_t search_tree_match_usbserialno(di_node_t node,
127     uint64_t serialno);
128 static int i_match_usbserialno(di_node_t node, void *arg);
129 
130 static di_node_t search_children_match_busaddr(di_node_t node,
131     char *matchbusaddr);
132 
133 
134 
135 static void
136 new_disk_list_entry(di_node_t node)
137 {
138 	size_t	newsize;
139 	char **newlist;
140 	int newlen;
141 	char *devfspath;
142 
143 	if (disk_list_valid >= disk_list_len)	{
144 		/* valid should never really be larger than len */
145 		/* if they are equal we need to init or realloc */
146 		newlen = disk_list_len + DISKS_LIST_INCR;
147 		newsize = newlen * sizeof (*disk_list);
148 
149 		newlist = (char **)realloc(disk_list, newsize);
150 		if (newlist == NULL) {
151 			(void) printf("realloc failed to resize disk table\n");
152 			cleanup_and_exit(1);
153 		}
154 		disk_list = newlist;
155 		disk_list_len = newlen;
156 	}
157 
158 	devfspath = di_devfs_path(node);
159 	disk_list[disk_list_valid] = devfspath;
160 	if (debug)
161 		(void) printf("adding %s\n", devfspath);
162 	disk_list_valid++;
163 }
164 
165 /* ARGSUSED */
166 static int
167 i_disktype(di_node_t node, di_minor_t minor, void *arg)
168 {
169 	char *minortype;
170 
171 	if (di_minor_spectype(minor) == S_IFCHR) {
172 		minortype = di_minor_nodetype(minor);
173 
174 		/* exclude CD's */
175 		if (strncmp(minortype, DDI_NT_CD, sizeof (DDI_NT_CD) - 1) != 0)
176 			/* only take p0 raw device */
177 			if (strcmp(di_minor_name(minor), "q,raw") == 0)
178 				new_disk_list_entry(node);
179 	}
180 	return (DI_WALK_CONTINUE);
181 }
182 
183 static void
184 build_disk_list()
185 {
186 	int ret;
187 	ret = di_walk_minor(root_node, DDI_NT_BLOCK, 0, NULL,
188 	    i_disktype);
189 	if (ret != 0) {
190 		(void) fprintf(stderr, "di_walk_minor failed errno %d\n",
191 		    errno);
192 		cleanup_and_exit(1);
193 	}
194 }
195 
196 static void
197 free_disks()
198 {
199 	int i;
200 
201 	if (disk_list) {
202 		for (i = 0; i < disk_list_valid; i++)
203 			di_devfs_path_free(disk_list[i]);
204 
205 		free(disk_list);
206 	}
207 }
208 
209 static int
210 i_match_pcibdf(di_node_t node, void *arg)
211 {
212 	pcibdf_t *pbp;
213 	int len;
214 	uint32_t	regval;
215 	uint32_t	busnum, funcnum, devicenum;
216 	char *devtype;
217 	uint32_t *regbuf = NULL;
218 	di_node_t	parentnode;
219 
220 	pbp = (pcibdf_t *)arg;
221 
222 	parentnode = di_parent_node(node);
223 
224 	len = di_prop_lookup_strings(DDI_DEV_T_ANY, parentnode,
225 	    "device_type", (char **)&devtype);
226 
227 	if ((len <= 0) ||
228 	    ((strcmp(devtype, "pci") != 0) && (strcmp(devtype, "pciex") != 0)))
229 		return (DI_WALK_CONTINUE);
230 
231 	len = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "reg",
232 	    (int **)&regbuf);
233 
234 	if (len <= 0) {
235 		/* Try PROM property */
236 		len = di_prom_prop_lookup_ints(prom_hdl, node, "reg",
237 		    (int **)&regbuf);
238 	}
239 
240 
241 	if (len > 0) {
242 		regval = regbuf[0];
243 
244 		busnum = PCI_REG_BUS_G(regval);
245 		devicenum = PCI_REG_DEV_G(regval);
246 		funcnum = PCI_REG_FUNC_G(regval);
247 
248 		if ((busnum == pbp->busnum) &&
249 		    (devicenum == pbp->devnum) &&
250 		    (funcnum == pbp->funcnum)) {
251 			/* found it */
252 			pbp->di_node = node;
253 			return (DI_WALK_TERMINATE);
254 		}
255 	}
256 
257 	return (DI_WALK_CONTINUE);
258 }
259 
260 static di_node_t
261 search_tree_match_pcibdf(di_node_t node, int bus, int dev, int fn)
262 {
263 	pcibdf_t pb;
264 	pb.busnum = bus;
265 	pb.devnum = dev;
266 	pb.funcnum = fn;
267 	pb.di_node = DI_NODE_NIL;
268 
269 	(void) di_walk_node(node, DI_WALK_CLDFIRST, &pb, i_match_pcibdf);
270 	return (pb.di_node);
271 
272 }
273 
274 static int
275 i_match_usbserialno(di_node_t node, void *arg)
276 {
277 	int len;
278 	char *serialp;
279 	usbser_t *usbsp;
280 
281 	usbsp = (usbser_t *)arg;
282 
283 	len = di_prop_lookup_bytes(DDI_DEV_T_ANY, node, "usb-serialno",
284 	    (uchar_t **)&serialp);
285 
286 	if ((len > 0) && (strncmp((char *)&usbsp->serialno, serialp,
287 	    sizeof (uint64_t)) == 0)) {
288 		usbsp->node = node;
289 		return (DI_WALK_TERMINATE);
290 	}
291 	return (DI_WALK_CONTINUE);
292 }
293 
294 static di_node_t
295 search_tree_match_usbserialno(di_node_t node, uint64_t serialno)
296 {
297 
298 	usbser_t usbs;
299 
300 	usbs.serialno = serialno;
301 	usbs.node = DI_NODE_NIL;
302 
303 	(void) di_walk_node(node, DI_WALK_CLDFIRST, &usbs, i_match_usbserialno);
304 	return (usbs.node);
305 }
306 
307 /*
308  * returns the index to the disklist to the disk with matching path
309  */
310 static int
311 search_disklist_match_path(char *path)
312 {
313 	int i;
314 	for (i = 0; i < disk_list_valid; i++)
315 		if (strcmp(disk_list[i], path) == 0) {
316 			return (i);
317 		}
318 	return (-1);
319 }
320 
321 /*
322  * Find first child of 'node' whose unit address is 'matchbusaddr'
323  */
324 static di_node_t
325 search_children_match_busaddr(di_node_t node, char *matchbusaddr)
326 {
327 	di_node_t cnode;
328 	char *busaddr;
329 
330 	if (matchbusaddr == NULL)
331 		return (DI_NODE_NIL);
332 
333 
334 	for (cnode = di_child_node(node); cnode != DI_NODE_NIL;
335 	    cnode = di_sibling_node(cnode)) {
336 		busaddr = di_bus_addr(cnode);
337 		if (busaddr == NULL)
338 			continue;
339 		if (strncmp(busaddr, matchbusaddr, MAXNAMELEN) == 0)
340 			break;
341 	}
342 	return (cnode);
343 }
344 
345 /*
346  * Construct a physical device pathname from EDD and verify the
347  * path exists. Return the index of in disk_list for the mapped
348  * path on success, -1 on failure.
349  */
350 static int
351 match_edd(biosdev_data_t *bdata)
352 {
353 	di_node_t node, cnode = DI_NODE_NIL;
354 	char *devfspath = NULL;
355 	fn48_t *bd;
356 	int index;
357 	char busaddrbuf[MAXNAMELEN];
358 
359 	if (!bdata->edd_valid) {
360 		if (debug)
361 			(void) printf("edd not valid\n");
362 		return (-1);
363 	}
364 
365 	bd = &bdata->fn48_dev_params;
366 
367 	if (bd->magic != 0xBEDD || bd->pathinfo_len == 0) {
368 		/* EDD extensions for devicepath not present */
369 		if (debug)
370 			(void) printf("magic not valid %x pathinfolen %d\n",
371 			    bd->magic, bd->pathinfo_len);
372 		return (-1);
373 	}
374 
375 	/* we handle only PCI scsi, ata or sata for now */
376 	if (strncmp(bd->bustype, "PCI", 3) != 0) {
377 		if (debug)
378 			(void) printf("was not pci %s\n", bd->bustype);
379 		return (-1);
380 	}
381 	if (debug)
382 		(void) printf("match_edd bdf %d %d %d\n",
383 		    bd->interfacepath.pci.bus,
384 		    bd->interfacepath.pci.device,
385 		    bd->interfacepath.pci.function);
386 
387 	/* look into devinfo tree and find a node with matching pci b/d/f */
388 	node = search_tree_match_pcibdf(root_node, bd->interfacepath.pci.bus,
389 	    bd->interfacepath.pci.device, bd->interfacepath.pci.function);
390 
391 	if (node == DI_NODE_NIL) {
392 		if (debug)
393 			(void) printf(" could not find a node in tree "
394 			    "matching bdf\n");
395 		return (-1);
396 	}
397 
398 	if (debug) {
399 		int i;
400 		(void) printf("interface type ");
401 		for (i = 0; i < 8; i++)
402 			(void) printf("%c", bd->interface_type[i]);
403 		(void) printf(" pci channel %x target %x\n",
404 		    bd->interfacepath.pci.channel,
405 		    bd->devicepath.scsi.target);
406 	}
407 
408 	if (strncmp(bd->interface_type, "SCSI", 4) == 0) {
409 
410 		(void) snprintf(busaddrbuf, MAXNAMELEN, "%x,%x",
411 		    bd->devicepath.scsi.target, bd->devicepath.scsi.lun_lo);
412 
413 		cnode = search_children_match_busaddr(node, busaddrbuf);
414 
415 	} else if ((strncmp(bd->interface_type, "ATAPI", 5) == 0) ||
416 	    (strncmp(bd->interface_type, "ATA", 3) == 0) ||
417 	    (strncmp(bd->interface_type, "SATA", 4) == 0)) {
418 
419 		if (strncmp(di_node_name(node), "pci-ide", 7) == 0) {
420 			/*
421 			 * Legacy using pci-ide
422 			 * the child should be ide@<x>, where x is
423 			 * the channel number
424 			 */
425 			(void) snprintf(busaddrbuf, MAXNAMELEN, "%d",
426 			    bd->interfacepath.pci.channel);
427 
428 			if ((cnode = search_children_match_busaddr(node,
429 			    busaddrbuf)) != DI_NODE_NIL) {
430 
431 				(void) snprintf(busaddrbuf, MAXNAMELEN, "%x,0",
432 				    bd->devicepath.ata.chan);
433 				cnode = search_children_match_busaddr(cnode,
434 				    busaddrbuf);
435 
436 				if (cnode == DI_NODE_NIL)
437 					if (debug)
438 						(void) printf("Interface %s "
439 						    "using pci-ide no "
440 						    "grandchild at %s\n",
441 						    bd->interface_type,
442 						    busaddrbuf);
443 			} else {
444 				if (debug)
445 					(void) printf("Interface %s using "
446 					    "pci-ide, with no child at %s\n",
447 					    bd->interface_type, busaddrbuf);
448 			}
449 		} else {
450 			if (strncmp(bd->interface_type, "SATA", 4) == 0) {
451 				/*
452 				 * The current EDD (EDD-2) spec does not
453 				 * address port number. This is work in
454 				 * progress.
455 				 * Interprete the first field of device path
456 				 * as port number. Needs to be revisited
457 				 * with port multiplier support.
458 				 */
459 				(void) snprintf(busaddrbuf, MAXNAMELEN, "%x,0",
460 				    bd->devicepath.ata.chan);
461 
462 				cnode = search_children_match_busaddr(node,
463 				    busaddrbuf);
464 			} else {
465 				if (debug)
466 					(void) printf("Interface %s, not using"
467 					    " pci-ide\n", bd->interface_type);
468 			}
469 		}
470 
471 	} else if (strncmp(bd->interface_type, "USB", 3) == 0) {
472 		cnode = search_tree_match_usbserialno(node,
473 		    bd->devicepath.usb.usb_serial_id);
474 	} else {
475 		if (debug)
476 			(void) printf("sorry not supported interface %s\n",
477 			    bd->interface_type);
478 	}
479 
480 	if (cnode != DI_NODE_NIL) {
481 		devfspath = di_devfs_path(cnode);
482 		index = search_disklist_match_path(devfspath);
483 		di_devfs_path_free(devfspath);
484 		if (index >= 0)
485 			return (index);
486 	}
487 
488 	return (-1);
489 }
490 
491 /*
492  * For each disk in list of disks, compare the first block with the
493  * one from bdd. On the first match, return the index of path in
494  * disk_list. If none matched return -1.
495  */
496 static int
497 match_first_block(biosdev_data_t *bd)
498 {
499 
500 	char diskpath[MAXPATHLEN];
501 	int fd;
502 	char buf[512];
503 	ssize_t	num_read;
504 	int i;
505 
506 	if (!bd->first_block_valid)
507 		return (-1);
508 
509 	for (i = 0; i < disk_list_valid; i++) {
510 		(void) snprintf(diskpath, MAXPATHLEN, "%s/%s:q,raw",
511 		    DEVFS_PREFIX, disk_list[i]);
512 		fd = open(diskpath, O_RDONLY);
513 		if (fd  < 0) {
514 			(void) fprintf(stderr, "opening %s failed errno %d\n",
515 			    diskpath, errno);
516 			continue;
517 		}
518 		num_read = read(fd, buf, 512);
519 		if (num_read != 512) {
520 			(void) printf("read only %d bytes from %s\n", num_read,
521 			    diskpath);
522 			continue;
523 		}
524 
525 		if (memcmp(buf, bd->first_block, 512) == 0)	 {
526 			/* found it */
527 			return (i);
528 		}
529 	}
530 	return (-1);
531 }
532 
533 
534 static void
535 cleanup_and_exit(int exitcode)
536 {
537 
538 	free_disks();
539 
540 	if (root_node != DI_NODE_NIL)
541 		di_fini(root_node);
542 
543 	if (root_allnode != DI_NODE_NIL)
544 		di_fini(root_allnode);
545 
546 	if (prom_hdl != DI_PROM_HANDLE_NIL)
547 		di_prom_fini(prom_hdl);
548 	exit(exitcode);
549 
550 }
551 
552 
553 int
554 main(int argc, char *argv[])
555 {
556 	biosdev_data_t		*biosdata;
557 	int i, c, j;
558 	int matchedindex = -1;
559 	char biospropname[BIOSPROPNAME_TMPL_LEN];
560 	int totalmatches = 0;
561 	biosdev_data_t *biosdataarray[BIOSDEV_NUM];
562 
563 
564 	while ((c = getopt(argc, argv, "d")) != -1)  {
565 		switch (c) {
566 		case 'd':
567 			debug = 1;
568 			break;
569 		default:
570 			(void) printf("unknown option %c\n", c);
571 			exit(1);
572 		}
573 	}
574 
575 	if ((prom_hdl = di_prom_init()) == DI_PROM_HANDLE_NIL) {
576 		(void) fprintf(stderr, "di_prom_init failed\n");
577 		cleanup_and_exit(1);
578 	}
579 
580 	if ((root_node = di_init("/", DINFOCACHE)) == DI_NODE_NIL) {
581 		(void) fprintf(stderr, "di_init failed\n");
582 		cleanup_and_exit(1);
583 	}
584 
585 	if ((root_allnode = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
586 		(void) fprintf(stderr, "di_init failed\n");
587 		cleanup_and_exit(1);
588 	}
589 
590 	(void) memset(mapinfo, 0, sizeof (mapinfo));
591 
592 	/* get a list of all disks in the system */
593 	build_disk_list();
594 
595 	/*  Get property values that were created at boot up time */
596 	for (i = 0; i < BIOSDEV_NUM; i++) {
597 
598 		(void) snprintf((char *)biospropname, BIOSPROPNAME_TMPL_LEN,
599 		    BIOSPROPNAME_TMPL, i + STARTING_DRVNUM);
600 		if (di_prop_lookup_bytes(DDI_DEV_T_ANY, root_allnode,
601 		    biospropname, (uchar_t **)&biosdataarray[i]) <= 0)
602 			biosdataarray[i] = NULL;
603 	}
604 
605 	/* Try to match based on device/interface path info from BIOS */
606 	for (i = 0; i < BIOSDEV_NUM; i++) {
607 
608 		if ((biosdata = biosdataarray[i]) == NULL)
609 			continue;
610 		if (debug)
611 			(void) printf("matching edd 0x%x\n",
612 			    i + STARTING_DRVNUM);
613 
614 		matchedindex = match_edd(biosdata);
615 
616 		if (matchedindex != -1) {
617 			if (debug) {
618 				(void) printf("matched by edd\n");
619 				(void) printf("0x%x %s\n", i + STARTING_DRVNUM,
620 				    disk_list[matchedindex]);
621 			}
622 
623 			mapinfo[i].disklist_index = matchedindex;
624 			mapinfo[i].matchcount++;
625 
626 			for (j = 0; j < i; j++) {
627 				if (mapinfo[j].matchcount > 0 &&
628 				    mapinfo[j].disklist_index == matchedindex) {
629 					mapinfo[j].matchcount++;
630 					mapinfo[i].matchcount++;
631 				}
632 			}
633 
634 		} else
635 			if (debug)
636 				(void) printf("No matches by edd\n");
637 	}
638 
639 	/*
640 	 * Go through the list and ignore any found matches that are dups.
641 	 * This is to workaround issues with BIOSes that do not implement
642 	 * providing interface/device path info correctly.
643 	 */
644 
645 	for (i = 0; i < BIOSDEV_NUM; i++) {
646 		if (mapinfo[i].matchcount > 1) {
647 			if (debug)
648 				(void) printf("Ignoring dup match_edd\n(count "
649 				    "%d): 0x%x %s\n", mapinfo[i].matchcount,
650 				    i + STARTING_DRVNUM,
651 				    disk_list[mapinfo[i].disklist_index]);
652 
653 			mapinfo[i].matchcount = 0;
654 			mapinfo[i].disklist_index = 0;
655 		}
656 	}
657 
658 
659 	/*
660 	 * For each bios dev number that we do not have exactly one match
661 	 * already, try to match based on first block
662 	 */
663 	for (i = 0; i < BIOSDEV_NUM; i++) {
664 		if (mapinfo[i].matchcount == 1)
665 			continue;
666 
667 		if ((biosdata = biosdataarray[i]) == NULL)
668 			continue;
669 
670 		if (debug)
671 			(void) printf("matching first block 0x%x\n",
672 			    i + STARTING_DRVNUM);
673 
674 		matchedindex = match_first_block(biosdata);
675 		if (matchedindex != -1) {
676 			if (debug) {
677 				(void) printf("matched by first block\n");
678 				(void) printf("0x%x %s\n", i + STARTING_DRVNUM,
679 				    disk_list[matchedindex]);
680 			}
681 
682 			mapinfo[i].disklist_index = matchedindex;
683 			mapinfo[i].matchcount++;
684 
685 			for (j = 0; j < i; j++) {
686 				if (mapinfo[j].matchcount > 0 &&
687 				    mapinfo[j].disklist_index == matchedindex) {
688 					mapinfo[j].matchcount++;
689 					mapinfo[i].matchcount++;
690 				}
691 			}
692 		} else
693 			if (debug) {
694 				(void) printf(" No matches by first block\n");
695 				(void) fprintf(stderr, "Could not match 0x%x\n",
696 				    i + STARTING_DRVNUM);
697 			}
698 	}
699 
700 
701 	for (i = 0; i < BIOSDEV_NUM; i++) {
702 		if (mapinfo[i].matchcount == 1) {
703 			(void) printf("0x%x %s\n", i + STARTING_DRVNUM,
704 			    disk_list[mapinfo[i].disklist_index]);
705 			totalmatches++;
706 		} else if (debug && mapinfo[i].matchcount > 1) {
707 			(void) printf("0x%x %s matchcount %d\n",
708 			    i + STARTING_DRVNUM,
709 			    disk_list[mapinfo[i].disklist_index],
710 			    mapinfo[i].matchcount);
711 		}
712 	}
713 
714 	if (totalmatches == 0) {
715 		(void) fprintf(stderr, "biosdev: Could not match any!!\n");
716 		cleanup_and_exit(1);
717 	}
718 
719 	cleanup_and_exit(0);
720 	/* NOTREACHED */
721 	return (0);
722 }
723