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 2006 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25
26/*
27 * Sun4v Platform specific functions.
28 *
29 * 	called when :
30 *      machine_type ==  montoya
31 *
32 */
33
34#pragma ident	"%Z%%M%	%I%	%E% SMI"
35
36#include <stdio.h>
37#include <stdlib.h>
38#include <unistd.h>
39#include <kstat.h>
40#include <fcntl.h>
41#include <string.h>
42#include <assert.h>
43#include <libintl.h>
44#include <note.h>
45#include <sys/systeminfo.h>
46#include <sys/openpromio.h>
47#include <sys/sysmacros.h>
48#include <picl.h>
49#include "picldefs.h"
50#include <pdevinfo.h>
51#include <display.h>
52#include <display_sun4v.h>
53#include <libprtdiag.h>
54#include "montoya.h"
55
56#if !defined(TEXT_DOMAIN)
57#define	TEXT_DOMAIN	"SYS_TEST"
58#endif
59
60/*
61 * these functions will overlay the symbol table of libprtdiag
62 * at runtime
63 */
64void sun4v_display_pci(picl_nodehdl_t plafh);
65void sun4v_display_diaginfo(int flag, Prom_node *root, picl_nodehdl_t plafh);
66
67
68/* local functions */
69static void sun4v_display_hw_revisions(Prom_node *root, picl_nodehdl_t plafh);
70static int montoya_pci_callback(picl_nodehdl_t pcih, void *args);
71static int montoya_get_first_compatible_value(picl_nodehdl_t nodeh,
72    char **outbuf);
73static int64_t montoya_get_int_propval(picl_nodehdl_t modh, char *prop_name,
74    int *ret);
75
76static void
77get_bus_type(char *path, struct io_card *card)
78{
79	(void) strcpy(card->bus_type, "PCIE");
80}
81
82static void
83get_slot_number(char *path, struct io_card *card)
84{
85	if (strncmp(path, SATA_DISK_PATH, strlen(SATA_DISK_PATH)) == 0) {
86		(void) strcpy(card->slot_str, RTM);
87		card->slot = 0;
88	} else if (strncmp(path, AMC_PATH, strlen(AMC_PATH)) == 0) {
89		(void) strcpy(card->slot_str, MOTHERBOARD);
90		card->slot = 0;
91	} else {
92		(void) strcpy(card->slot_str, MOTHERBOARD);
93		card->slot = -1;
94	}
95}
96
97static int
98montoya_get_network_instance(char *path)
99{
100
101	if (strncmp(path, NETWORK_1_PATH, strlen(NETWORK_1_PATH)) == 0) {
102		return (1);
103	} else if (strncmp(path, NETWORK_0_PATH, strlen(NETWORK_0_PATH)) == 0) {
104		return (0);
105	} else if (strncmp(path, NETWORK_3_PATH, strlen(NETWORK_3_PATH)) == 0) {
106		return (3);
107	} else if (strncmp(path, NETWORK_2_PATH, strlen(NETWORK_2_PATH)) == 0) {
108		return (2);
109	} else {
110		return (-1);
111	}
112}
113
114/*
115 * add all io devices under pci in io list
116 */
117/* ARGSUSED */
118static int
119montoya_pci_callback(picl_nodehdl_t pcih, void *args)
120{
121	int		err = PICL_SUCCESS;
122	picl_nodehdl_t	nodeh;
123	char		path[MAXSTRLEN];
124	char		parent_path[MAXSTRLEN];
125	char		piclclass[PICL_CLASSNAMELEN_MAX];
126	char		name[MAXSTRLEN];
127	char		model[MAXSTRLEN];
128	char		*compatible;
129	char		binding_name[MAXSTRLEN];
130	struct io_card	pci_card;
131	int32_t		instance;
132
133	err = picl_get_propval_by_name(pcih, PICL_PROP_DEVFS_PATH, parent_path,
134	    sizeof (parent_path));
135	if (err != PICL_SUCCESS) {
136		return (err);
137	}
138
139	/* Walk through the children */
140
141	err = picl_get_propval_by_name(pcih, PICL_PROP_CHILD, &nodeh,
142	    sizeof (picl_nodehdl_t));
143
144	while (err == PICL_SUCCESS) {
145		err = picl_get_propval_by_name(nodeh, PICL_PROP_CLASSNAME,
146		    piclclass, sizeof (piclclass));
147		if (err !=  PICL_SUCCESS)
148			return (err);
149
150		if (strcmp(piclclass, PCIEX) == 0) {
151			err = picl_get_propval_by_name(nodeh, PICL_PROP_PEER,
152			    &nodeh, sizeof (picl_nodehdl_t));
153			continue;
154		}
155
156		if (strcmp(piclclass, PICL_CLASS_PCI) == 0) {
157			err = picl_get_propval_by_name(nodeh, PICL_PROP_CHILD,
158			    &nodeh, sizeof (picl_nodehdl_t));
159			continue;
160		}
161
162		err = picl_get_propval_by_name(nodeh, PICL_PROP_DEVFS_PATH,
163		    path, sizeof (path));
164		if (err != PICL_SUCCESS) {
165			return (err);
166		}
167
168		(void) strlcpy(pci_card.notes, path, sizeof (pci_card.notes));
169
170		get_bus_type(parent_path, &pci_card);
171
172		get_slot_number(parent_path, &pci_card);
173
174		err = picl_get_propval_by_name(nodeh, PICL_PROP_NAME, &name,
175		    sizeof (name));
176		if (err == PICL_PROPNOTFOUND)
177			(void) strcpy(name, "");
178		else if (err != PICL_SUCCESS)
179			return (err);
180
181		/* Figure NAC name */
182		if ((strcmp(name, NETWORK) == 0 ||
183			strcmp(name, ETHERNET) == 0) &&
184			(strcmp(pci_card.slot_str, MOTHERBOARD) == 0)) {
185			instance = montoya_get_network_instance(path);
186
187			(void) snprintf(pci_card.status,
188			    sizeof (pci_card.status), "%s/%s%d", MOTHERBOARD,
189			    "NET", instance);
190		} else {
191			if (pci_card.slot != -1) {
192				(void) snprintf(pci_card.status,
193				    sizeof (pci_card.status), "%s/%s%d",
194				    IOBOARD, pci_card.bus_type, pci_card.slot);
195			} else {
196				(void) snprintf(pci_card.status,
197				    sizeof (pci_card.status),
198				"%s/%s", MOTHERBOARD, pci_card.bus_type);
199			}
200		}
201
202		/*
203		 * Get the name of this card. If binding_name is found,
204		 * name will be <nodename>-<binding_name>
205		 */
206
207		err = picl_get_propval_by_name(nodeh, PICL_PROP_BINDING_NAME,
208		    &binding_name, sizeof (binding_name));
209		if (err == PICL_PROPNOTFOUND) {
210			/*
211			 * if compatible prop is found, name will be
212			 * <nodename>-<compatible>
213			 */
214			err = montoya_get_first_compatible_value(nodeh,
215			    &compatible);
216			if (err == PICL_SUCCESS) {
217				(void) strlcat(name, "-", MAXSTRLEN);
218				(void) strlcat(name, compatible, MAXSTRLEN);
219				free(compatible);
220			} else if (err != PICL_PROPNOTFOUND) {
221				return (err);
222			}
223		} else if (err != PICL_SUCCESS) {
224			return (err);
225		} else if (strcmp(name, binding_name) != 0) {
226			(void) strlcat(name, "-", MAXSTRLEN);
227			(void) strlcat(name, binding_name, MAXSTRLEN);
228		}
229
230		(void) strlcpy(pci_card.name, name, sizeof (pci_card.name));
231
232		/* Get the model of this card */
233
234		err = picl_get_propval_by_name(nodeh, OBP_PROP_MODEL,
235		    &model, sizeof (model));
236		if (err == PICL_PROPNOTFOUND)
237			(void) strcpy(model, "");
238		else if (err != PICL_SUCCESS)
239			return (err);
240		(void) strlcpy(pci_card.model, model, sizeof (pci_card.model));
241
242		/* Print NAC name */
243		log_printf("%-11s", pci_card.status);
244		/* Print IO Type */
245		log_printf("%6s", pci_card.bus_type);
246		/* Print Slot # */
247		log_printf("%5s", pci_card.slot_str);
248		/* Print Parent Path */
249		log_printf("%46.45s", pci_card.notes);
250		/* Printf Card Name */
251		if (strlen(pci_card.name) > 24)
252			log_printf("%25.24s+", pci_card.name);
253		else
254			log_printf("%26s", pci_card.name);
255		/* Print Card Model */
256		if (strlen(pci_card.model) > 10)
257			log_printf("%10.9s+", pci_card.model);
258		else
259			log_printf("%10s", pci_card.model);
260		log_printf("\n");
261
262		err = picl_get_propval_by_name(nodeh, PICL_PROP_PEER, &nodeh,
263		    sizeof (picl_nodehdl_t));
264
265	}
266
267	return (PICL_WALK_CONTINUE);
268}
269
270/*
271 * display_pci
272 * Display all the PCI IO cards on this board.
273 */
274void
275sun4v_display_pci(picl_nodehdl_t plafh)
276{
277	char    platbuf[MAXSTRLEN];
278	char	*fmt = "%-11s %-7s %-4s %-46s %-25s %-8s";
279	static int banner = FALSE; /* Have we printed the column headings? */
280
281	if (banner == FALSE) {
282		log_printf("\n", 0);
283		log_printf("=========================", 0);
284		log_printf(dgettext(TEXT_DOMAIN, " IO Configuration "), 0);
285		log_printf("=========================", 0);
286		log_printf("\n", 0);
287		log_printf("\n", 0);
288		log_printf(fmt, "", "IO", "", "", "", "", 0);
289		log_printf("\n", 0);
290		log_printf(fmt, "Location", "Type", "Slot", "Path",
291		    "Name", "Model", 0);
292		log_printf("\n");
293		log_printf(fmt, "-----------", "-----", "----",
294		    "---------------------------------------------",
295		    "-------------------------", "---------", 0);
296		log_printf("\n");
297		banner = TRUE;
298	}
299
300	/* Get platform name, if that fails, use montoya name by default */
301	if (sysinfo(SI_PLATFORM, platbuf, sizeof (platbuf)) == -1) {
302		(void) strcpy(platbuf, MONTOYA_PLATFORM);
303	}
304
305	(void) picl_walk_tree_by_class(plafh, PCIEX, PCIEX,
306				montoya_pci_callback);
307}
308
309/* ARGSUSED */
310void
311sun4v_display_diaginfo(int flag, Prom_node *root, picl_nodehdl_t plafh)
312{
313	/* NOTE(ARGUNUSED(kstats)) */
314	/*
315	 * Now display the last powerfail time and the fatal hardware
316	 * reset information. We do this under a couple of conditions.
317	 * First if the user asks for it. The second is if the user
318	 * told us to do logging, and we found a system failure.
319	 */
320	if (flag) {
321		/*
322		 * display time of latest powerfail. Not all systems
323		 * have this capability. For those that do not, this
324		 * is just a no-op.
325		 */
326		disp_powerfail(root);
327
328		/* platform_disp_prom_version(tree); */
329		sun4v_display_hw_revisions(root, plafh);
330	}
331}
332
333/*
334 * local functions
335 */
336/*
337 * add all io devices under pci in io list
338 */
339/* ARGSUSED */
340static int
341montoya_hw_rev_callback(picl_nodehdl_t pcih, void *args)
342{
343	int		err = PICL_SUCCESS;
344	char		path[MAXSTRLEN] = "";
345	char		device_path[MAXSTRLEN];
346	char		NAC[MAXSTRLEN];
347	char		*compatible;
348	int32_t		revision;
349	int		device_found;
350
351	device_found = 0;
352
353	err = picl_get_propval_by_name(pcih, PICL_PROP_DEVFS_PATH, path,
354	    sizeof (path));
355	if (err != PICL_SUCCESS) {
356		return (err);
357	}
358
359
360	if ((strncmp(path, NETWORK_0_PATH, strlen(NETWORK_0_PATH)) == 0) ||
361	    (strncmp(path, NETWORK_1_PATH, strlen(NETWORK_1_PATH)) == 0)) {
362		device_found = 1;
363		(void) snprintf(NAC, sizeof (NAC), "%s/%s%d",
364					MOTHERBOARD, OPHIR, 0);
365		revision = montoya_get_int_propval(pcih, OBP_PROP_REVISION_ID,
366		    &err);
367	}
368
369	if ((strncmp(path, NETWORK_2_PATH, strlen(NETWORK_2_PATH)) == 0) ||
370	    (strncmp(path, NETWORK_3_PATH, strlen(NETWORK_3_PATH)) == 0)) {
371		device_found = 1;
372		(void) snprintf(NAC, sizeof (NAC), "%s/%s%d",
373				MOTHERBOARD, OPHIR, 1);
374		revision = montoya_get_int_propval(pcih, OBP_PROP_REVISION_ID,
375		    &err);
376	}
377
378	if ((strcmp(path, FIRE_PATHB) == 0) ||
379	    (strcmp(path, FIRE_PATHA) == 0)) {
380		device_found = 1;
381		(void) snprintf(NAC, sizeof (NAC), "%s/%s", MOTHERBOARD,
382		    PCI_BRIDGE);
383		revision = montoya_get_int_propval(pcih, OBP_PROP_VERSION_NUM,
384		    &err);
385	}
386
387	if (strcmp(path, SWITCH_PATH) == 0) {
388		device_found = 1;
389		(void) snprintf(NAC, sizeof (NAC), "%s/%s",
390			MOTHERBOARD, SWITCH_A);
391		revision = montoya_get_int_propval(pcih,
392			OBP_PROP_REVISION_ID, &err);
393	}
394
395	if (strcmp(path, SATA_DISK_PATH) == 0) {
396		device_found = 1;
397		(void) snprintf(NAC, sizeof (NAC), "%s/%s", RTM, SATA_HBA);
398		revision = montoya_get_int_propval(pcih, OBP_PROP_REVISION_ID,
399		    &err);
400	}
401
402	if (strcmp(path, RTM_PCI_PATH) == 0) {
403		device_found = 1;
404		(void) snprintf(NAC, sizeof (NAC), "%s/%s", RTM, AMC);
405		revision = montoya_get_int_propval(pcih, OBP_PROP_REVISION_ID,
406		    &err);
407	}
408
409	if (device_found == 1) {
410		(void) strcpy(device_path, path);
411		err = montoya_get_first_compatible_value(pcih, &compatible);
412
413		/* Print NAC name */
414		log_printf("%-20s", NAC);
415		/* Print Device Path */
416		if (strlen(device_path) > 38)
417			log_printf("%38.37s+", device_path);
418		else
419			log_printf("%39s", device_path);
420
421		/* Print Compatible # */
422		if (err == PICL_SUCCESS) {
423			log_printf("%31s", compatible);
424			free(compatible);
425		} else
426			log_printf("%31s", " ");
427
428		/* Print Revision */
429		log_printf("%6d", revision);
430		log_printf("\n");
431	}
432
433	return (PICL_WALK_CONTINUE);
434}
435
436/*ARGSUSED*/
437static void
438sun4v_display_hw_revisions(Prom_node *root, picl_nodehdl_t plafh)
439{
440	Prom_node	*pnode;
441	char		*value;
442	char 		platbuf[MAXSTRLEN];
443	char	*fmt = "%-20s %-40s %-30s %-9s";
444
445	log_printf(dgettext(TEXT_DOMAIN, "\n"
446		"========================= HW Revisions "
447		"=======================================\n\n"));
448
449	log_printf(dgettext(TEXT_DOMAIN,
450		"System PROM revisions:\n"
451		"----------------------\n"));
452
453	pnode = dev_find_node(root, "openprom");
454	if (pnode != NULL) {
455	    value = (char *)get_prop_val(find_prop(pnode, "version"));
456	    log_printf(value);
457	}
458
459	log_printf(dgettext(TEXT_DOMAIN, "\n\n"
460		"IO ASIC revisions:\n"
461			    "------------------\n"));
462	log_printf(fmt, "Location", "Path", "Device", "Revision\n", 0);
463	log_printf(fmt, "--------------------",
464	    "----------------------------------------",
465	    "------------------------------",
466	    "---------\n", 0);
467
468	/* Get platform name, if that fails, use montoya name by default */
469	if (sysinfo(SI_PLATFORM, platbuf, sizeof (platbuf)) == -1) {
470		(void) strcpy(platbuf, MONTOYA_PLATFORM);
471	}
472
473	(void) picl_walk_tree_by_class(plafh, PCIEX,
474		    PCIEX, montoya_hw_rev_callback);
475	(void) picl_walk_tree_by_class(plafh, PCI,
476		    PCI, montoya_hw_rev_callback);
477	(void) picl_walk_tree_by_class(plafh, NETWORK,
478		    NETWORK, montoya_hw_rev_callback);
479	(void) picl_walk_tree_by_class(plafh, SCSI2, SCSI2,
480		    montoya_hw_rev_callback);
481}
482
483/*
484 * return the first compatible value
485 */
486static int
487montoya_get_first_compatible_value(picl_nodehdl_t nodeh, char **outbuf)
488{
489	int		err;
490	picl_prophdl_t	proph;
491	picl_propinfo_t	pinfo;
492	picl_prophdl_t	tblh;
493	picl_prophdl_t	rowproph;
494	char		*pval;
495
496	err = picl_get_propinfo_by_name(nodeh, OBP_PROP_COMPATIBLE,
497	    &pinfo, &proph);
498	if (err != PICL_SUCCESS)
499	    return (err);
500
501	if (pinfo.type == PICL_PTYPE_CHARSTRING) {
502		pval = malloc(pinfo.size);
503		if (pval == NULL)
504			return (PICL_FAILURE);
505		err = picl_get_propval(proph, pval, pinfo.size);
506		if (err != PICL_SUCCESS) {
507			free(pval);
508			return (err);
509		}
510		*outbuf = pval;
511		return (PICL_SUCCESS);
512	}
513
514	if (pinfo.type != PICL_PTYPE_TABLE)
515		return (PICL_FAILURE);
516
517	/* get first string from table */
518	err = picl_get_propval(proph, &tblh, pinfo.size);
519	if (err != PICL_SUCCESS)
520		return (err);
521
522	err = picl_get_next_by_row(tblh, &rowproph);
523	if (err != PICL_SUCCESS)
524		return (err);
525
526	err = picl_get_propinfo(rowproph, &pinfo);
527	if (err != PICL_SUCCESS)
528	    return (err);
529
530	pval = malloc(pinfo.size);
531	if (pval == NULL)
532		return (PICL_FAILURE);
533
534	err = picl_get_propval(rowproph, pval, pinfo.size);
535	if (err != PICL_SUCCESS) {
536		free(pval);
537		return (err);
538	}
539
540	*outbuf = pval;
541	return (PICL_SUCCESS);
542}
543
544static int64_t
545montoya_get_int_propval(picl_nodehdl_t modh, char *prop_name, int *ret)
546{
547	int		err;
548	picl_prophdl_t	proph;
549	picl_propinfo_t	pinfo;
550	int8_t		int8v;
551	int16_t		int16v;
552	int32_t		int32v;
553	int64_t		int64v;
554
555	err = picl_get_propinfo_by_name(modh, prop_name, &pinfo, &proph);
556	if (err != PICL_SUCCESS) {
557		*ret = err;
558		return (0);
559	}
560
561	/*
562	 * If it is not an int, uint or byte array prop, return failure
563	 */
564	if ((pinfo.type != PICL_PTYPE_INT) &&
565		(pinfo.type != PICL_PTYPE_UNSIGNED_INT) &&
566		(pinfo.type != PICL_PTYPE_BYTEARRAY)) {
567		*ret = PICL_FAILURE;
568		return (0);
569	}
570
571	switch (pinfo.size) {
572	case sizeof (int8_t):
573		err = picl_get_propval(proph, &int8v, sizeof (int8v));
574		*ret = err;
575		return (int8v);
576	case sizeof (int16_t):
577		err = picl_get_propval(proph, &int16v, sizeof (int16v));
578		*ret = err;
579		return (int16v);
580	case sizeof (int32_t):
581		err = picl_get_propval(proph, &int32v, sizeof (int32v));
582		*ret = err;
583		return (int32v);
584	case sizeof (int64_t):
585		err = picl_get_propval(proph, &int64v, sizeof (int64v));
586		*ret = err;
587		return (int64v);
588	default:	/* not supported size */
589		*ret = PICL_FAILURE;
590		return (0);
591	}
592}
593