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 2008 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 ==  pelton
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 "pelton.h"
55
56#if !defined(TEXT_DOMAIN)
57#define	TEXT_DOMAIN	"SYS_TEST"
58#endif
59
60/* local functions */
61static int pelton_get_first_compatible_value(picl_nodehdl_t nodeh,
62    char **outbuf);
63static int64_t pelton_get_int_propval(picl_nodehdl_t modh, char *prop_name,
64    int *ret);
65
66static void
67get_bus_type(char *path, struct io_card *card)
68{
69	if (strncmp(path, PEL_PCIX_SLOT0, PCIX_COMP_NUM) == 0) {
70		(void) strcpy(card->bus_type, "PCIX");
71	} else if (strncmp(path, PEL_PCIX_SLOT1, PCIX_COMP_NUM) == 0) {
72		(void) strcpy(card->bus_type, "PCIX");
73	} else if (strncmp(path, PEL_PCIX_SLOT2, PCIX_COMP_NUM) == 0) {
74		(void) strcpy(card->bus_type, "PCIX");
75	} else if (strncmp(path, PEL_PCIX_ONBOARD0, PCIX_COMP_NUM) == 0) {
76		(void) strcpy(card->bus_type, "PCIX");
77	} else {
78		(void) strcpy(card->bus_type, "PCIE");
79	}
80}
81
82/*
83 * 'path' is the device-path of the *parent* of the IO card ('card').
84 *
85 * Compare the prefix of the string representing the device-path with
86 * the strings representing the platform's Slots.
87 *
88 * Note that string for PEL_PCIX_SLOT1 is a prefix of PEL_PCIX_SLOT2's
89 * string.  Since we want to match the longest possible substring of
90 * 'path', compare SLOT2 before comparing SLOT1.
91 */
92static void
93get_slot_number(char *path, struct io_card *card)
94{
95	if (strncmp(path, PEL_PCIE_SLOT0, PCIE_COMP_NUM) == 0) {
96		(void) strcpy(card->slot_str, "0");
97		card->slot = 0;
98	} else if (strncmp(path, PEL_PCIX_SLOT2, strlen(PEL_PCIX_SLOT2)) == 0) {
99		(void) strcpy(card->slot_str, "2");
100		card->slot = 2;
101	} else if (strncmp(path, PEL_PCIX_SLOT1, strlen(PEL_PCIX_SLOT1)) == 0) {
102		(void) strcpy(card->slot_str, "3");
103		card->slot = 3;
104	} else if (strncmp(path, PEL_PCIX_SLOT0, strlen(PEL_PCIX_SLOT0)) == 0) {
105		(void) strcpy(card->slot_str, "1");
106		card->slot = 1;
107	} else {
108		(void) strcpy(card->slot_str, IOBOARD);
109		card->slot = -1;
110	}
111}
112
113static int
114pelton_get_network_instance(char *path)
115{
116	if (strncmp(path, PEL_NETWORK_1_PATH,
117	    strlen(PEL_NETWORK_1_PATH)) == 0) {
118		return (1);
119	} else if (strncmp(path, PEL_NETWORK_3_PATH,
120	    strlen(PEL_NETWORK_3_PATH)) == 0) {
121		return (3);
122	} else if (strncmp(path, PEL_NETWORK_0_PATH,
123	    strlen(PEL_NETWORK_0_PATH)) == 0) {
124		return (0);
125	} else if (strncmp(path, PEL_NETWORK_2_PATH,
126	    strlen(PEL_NETWORK_2_PATH)) == 0) {
127		return (2);
128	} else {
129		return (-1);
130	}
131}
132/*
133 * add all io devices under pci in io list
134 */
135/* ARGSUSED */
136int
137pelton_pci_callback(picl_nodehdl_t pcih, void *args)
138{
139	int		err = PICL_SUCCESS;
140	picl_nodehdl_t	nodeh;
141	char		path[MAXSTRLEN];
142	char		parent_path[MAXSTRLEN];
143	char		piclclass[PICL_CLASSNAMELEN_MAX];
144	char		name[MAXSTRLEN];
145	char		model[MAXSTRLEN];
146	char		*compatible;
147	char		binding_name[MAXSTRLEN];
148	struct io_card	pci_card;
149	int32_t		instance;
150
151	err = picl_get_propval_by_name(pcih, PICL_PROP_DEVFS_PATH, parent_path,
152	    sizeof (parent_path));
153	if (err != PICL_SUCCESS) {
154		return (err);
155	}
156
157	/* Walk through the children */
158
159	err = picl_get_propval_by_name(pcih, PICL_PROP_CHILD, &nodeh,
160	    sizeof (picl_nodehdl_t));
161
162	while (err == PICL_SUCCESS) {
163		err = picl_get_propval_by_name(nodeh, PICL_PROP_CLASSNAME,
164		    piclclass, sizeof (piclclass));
165		if (err !=  PICL_SUCCESS)
166			return (err);
167
168		if ((strcmp(piclclass, PICL_CLASS_PCIEX) == 0) ||
169		    (strcmp(piclclass, PICL_CLASS_PCI) == 0)) {
170			err = picl_get_propval_by_name(nodeh, PICL_PROP_PEER,
171			    &nodeh, sizeof (picl_nodehdl_t));
172			continue;
173		}
174
175		err = picl_get_propval_by_name(nodeh, PICL_PROP_DEVFS_PATH,
176		    path, sizeof (path));
177		if (err != PICL_SUCCESS) {
178			return (err);
179		}
180
181		(void) strlcpy(pci_card.notes, path, sizeof (pci_card.notes));
182
183		get_bus_type(parent_path, &pci_card);
184
185		get_slot_number(parent_path, &pci_card);
186
187		err = picl_get_propval_by_name(nodeh, PICL_PROP_NAME, &name,
188		    sizeof (name));
189		if (err == PICL_PROPNOTFOUND)
190			(void) strcpy(name, "");
191		else if (err != PICL_SUCCESS)
192			return (err);
193
194
195		/* Figure NAC name */
196		if ((strcmp(name, NETWORK) == 0) &&
197		    (strcmp(pci_card.slot_str, IOBOARD) == 0)) {
198			instance = pelton_get_network_instance(path);
199
200			(void) snprintf(pci_card.status,
201			    sizeof (pci_card.status), "%s/%s%d", IOBOARD,
202			    "NET", instance);
203		} else {
204			if (pci_card.slot != -1) {
205				(void) snprintf(pci_card.status,
206				    sizeof (pci_card.status), "%s/%s%d",
207				    IOBOARD, pci_card.bus_type, pci_card.slot);
208			} else {
209				(void) snprintf(pci_card.status,
210				    sizeof (pci_card.status), "%s/%s", IOBOARD,
211				    pci_card.bus_type);
212			}
213		}
214
215		/*
216		 * Get the name of this card. Iif binding_name is found,
217		 * name will be <nodename>-<binding_name>
218		 */
219
220		err = picl_get_propval_by_name(nodeh, PICL_PROP_BINDING_NAME,
221		    &binding_name, sizeof (binding_name));
222		if (err == PICL_PROPNOTFOUND) {
223			/*
224			 * if compatible prop is found, name will be
225			 * <nodename>-<compatible>
226			 */
227			err = pelton_get_first_compatible_value(nodeh,
228			    &compatible);
229			if (err == PICL_SUCCESS) {
230				(void) strlcat(name, "-", MAXSTRLEN);
231				(void) strlcat(name, compatible, MAXSTRLEN);
232				free(compatible);
233			} else if (err != PICL_PROPNOTFOUND) {
234				return (err);
235			}
236		} else if (err != PICL_SUCCESS) {
237			return (err);
238		} else if (strcmp(name, binding_name) != 0) {
239			(void) strlcat(name, "-", MAXSTRLEN);
240			(void) strlcat(name, binding_name, MAXSTRLEN);
241		}
242
243		(void) strlcpy(pci_card.name, name, sizeof (pci_card.name));
244
245		/* Get the model of this card */
246
247		err = picl_get_propval_by_name(nodeh, OBP_PROP_MODEL,
248		    &model, sizeof (model));
249		if (err == PICL_PROPNOTFOUND)
250			(void) strcpy(model, "");
251		else if (err != PICL_SUCCESS)
252			return (err);
253		(void) strlcpy(pci_card.model, model, sizeof (pci_card.model));
254
255		/* Print NAC name */
256		log_printf("%-11s", pci_card.status);
257		/* Print IO Type */
258		log_printf("%6s", pci_card.bus_type);
259		/* Print Slot # */
260		log_printf("%5s", pci_card.slot_str);
261		/* Print Parent Path */
262		log_printf("%46.45s", pci_card.notes);
263		/* Printf Card Name */
264		if (strlen(pci_card.name) > 24)
265			log_printf("%25.24s+", pci_card.name);
266		else
267			log_printf("%26s", pci_card.name);
268		/* Print Card Model */
269		if (strlen(pci_card.model) > 10)
270			log_printf("%10.9s+", pci_card.model);
271		else
272			log_printf("%10s", pci_card.model);
273		log_printf("\n");
274
275		err = picl_get_propval_by_name(nodeh, PICL_PROP_PEER, &nodeh,
276		    sizeof (picl_nodehdl_t));
277
278	}
279
280	return (PICL_WALK_CONTINUE);
281}
282
283/*
284 * local functions
285 */
286/*
287 * add all io devices under pci in io list
288 */
289/* ARGSUSED */
290int
291pelton_hw_rev_callback(picl_nodehdl_t pcih, void *args)
292{
293	int		err = PICL_SUCCESS;
294	char		path[MAXSTRLEN] = "";
295	char		device_path[MAXSTRLEN];
296	char		NAC[MAXSTRLEN];
297	char		*compatible;
298	int32_t		revision;
299	int		device_found;
300
301	device_found = 0;
302
303	err = picl_get_propval_by_name(pcih, PICL_PROP_DEVFS_PATH, path,
304	    sizeof (path));
305	if (err != PICL_SUCCESS) {
306		return (err);
307	}
308
309	if ((strcmp(path, PEL_NETWORK_0_PATH) == 0) ||
310	    (strcmp(path, PEL_NETWORK_1_PATH) == 0)) {
311		device_found = 1;
312		(void) snprintf(NAC, sizeof (NAC), "%s/%s%d", IOBOARD, OPHIR,
313		    0);
314		revision = pelton_get_int_propval(pcih, OBP_PROP_REVISION_ID,
315		    &err);
316	}
317
318	if ((strcmp(path, PEL_NETWORK_2_PATH) == 0) ||
319	    (strcmp(path, PEL_NETWORK_3_PATH) == 0)) {
320		device_found = 1;
321		(void) snprintf(NAC, sizeof (NAC), "%s/%s%d", IOBOARD, OPHIR,
322		    1);
323		revision = pelton_get_int_propval(pcih, OBP_PROP_REVISION_ID,
324		    &err);
325	}
326
327	if ((strcmp(path, FIRE_PATH0) == 0) ||
328	    (strcmp(path, FIRE_PATH1) == 0)) {
329		device_found = 1;
330		(void) snprintf(NAC, sizeof (NAC), "%s/%s", IOBOARD,
331		    "IO-BRIDGE");
332		revision = pelton_get_int_propval(pcih, OBP_PROP_VERSION_NUM,
333		    &err);
334	}
335
336	if ((strcmp(path, PEL_PCIX_SLOT0) == 0) ||
337	    (strcmp(path, PEL_PCIX_SLOT1) == 0) ||
338	    (strcmp(path, PEL_PCIX_SLOT2) == 0) ||
339	    (strcmp(path, PEL_PCIX_ONBOARD0) == 0)) {
340		device_found = 1;
341		(void) snprintf(NAC, sizeof (NAC), "%s/%s", IOBOARD,
342		    PCI_BRIDGE);
343		revision = pelton_get_int_propval(pcih, OBP_PROP_REVISION_ID,
344		    &err);
345	}
346
347	if (strcmp(path, SWITCH_A_PATH) == 0) {
348		device_found = 1;
349		(void) snprintf(NAC, sizeof (NAC), "%s/%s", IOBOARD, SWITCH_A);
350		revision = pelton_get_int_propval(pcih, OBP_PROP_REVISION_ID,
351		    &err);
352	}
353
354	if (strcmp(path, SWITCH_B_PATH) == 0) {
355		device_found = 1;
356		(void) snprintf(NAC, sizeof (NAC), "%s/%s", IOBOARD, SWITCH_B);
357		revision = pelton_get_int_propval(pcih, OBP_PROP_REVISION_ID,
358		    &err);
359	}
360
361	if (strcmp(path, PEL_LSI_PATH) == 0) {
362		device_found = 1;
363		(void) snprintf(NAC, sizeof (NAC), "%s/%s", IOBOARD,
364		    PEL_SAS_HBA);
365		revision = pelton_get_int_propval(pcih, OBP_PROP_REVISION_ID,
366		    &err);
367	}
368	if (device_found == 1) {
369		(void) strcpy(device_path, path);
370		err = pelton_get_first_compatible_value(pcih, &compatible);
371
372		/* Print NAC name */
373		log_printf("%-20s", NAC);
374		/* Print Device Path */
375		if (strlen(device_path) > 38)
376			log_printf("%38.37s+", device_path);
377		else
378			log_printf("%39s", device_path);
379		/* Print Compatible # */
380		if (err == PICL_SUCCESS) {
381			log_printf("%31s", compatible);
382			free(compatible);
383		} else
384			log_printf("%31s", " ");
385		/* Print Revision */
386		log_printf("%6d", revision);
387		log_printf("\n");
388	}
389
390	return (PICL_WALK_CONTINUE);
391}
392
393/*
394 * return the first compatible value
395 */
396static int
397pelton_get_first_compatible_value(picl_nodehdl_t nodeh, char **outbuf)
398{
399	int		err;
400	picl_prophdl_t	proph;
401	picl_propinfo_t	pinfo;
402	picl_prophdl_t	tblh;
403	picl_prophdl_t	rowproph;
404	char		*pval;
405
406	err = picl_get_propinfo_by_name(nodeh, OBP_PROP_COMPATIBLE,
407	    &pinfo, &proph);
408	if (err != PICL_SUCCESS)
409		return (err);
410
411	if (pinfo.type == PICL_PTYPE_CHARSTRING) {
412		pval = malloc(pinfo.size);
413		if (pval == NULL)
414			return (PICL_FAILURE);
415		err = picl_get_propval(proph, pval, pinfo.size);
416		if (err != PICL_SUCCESS) {
417			free(pval);
418			return (err);
419		}
420		*outbuf = pval;
421		return (PICL_SUCCESS);
422	}
423
424	if (pinfo.type != PICL_PTYPE_TABLE)
425		return (PICL_FAILURE);
426
427	/* get first string from table */
428	err = picl_get_propval(proph, &tblh, pinfo.size);
429	if (err != PICL_SUCCESS)
430		return (err);
431
432	err = picl_get_next_by_row(tblh, &rowproph);
433	if (err != PICL_SUCCESS)
434		return (err);
435
436	err = picl_get_propinfo(rowproph, &pinfo);
437	if (err != PICL_SUCCESS)
438		return (err);
439
440	pval = malloc(pinfo.size);
441	if (pval == NULL)
442		return (PICL_FAILURE);
443
444	err = picl_get_propval(rowproph, pval, pinfo.size);
445	if (err != PICL_SUCCESS) {
446		free(pval);
447		return (err);
448	}
449
450	*outbuf = pval;
451	return (PICL_SUCCESS);
452}
453
454static int64_t
455pelton_get_int_propval(picl_nodehdl_t modh, char *prop_name, int *ret)
456{
457	int		err;
458	picl_prophdl_t	proph;
459	picl_propinfo_t	pinfo;
460	int8_t		int8v;
461	int16_t		int16v;
462	int32_t		int32v;
463	int64_t		int64v;
464
465	err = picl_get_propinfo_by_name(modh, prop_name, &pinfo, &proph);
466	if (err != PICL_SUCCESS) {
467		*ret = err;
468		return (0);
469	}
470
471	/*
472	 * If it is not an int, uint or byte array prop, return failure
473	 */
474	if ((pinfo.type != PICL_PTYPE_INT) &&
475	    (pinfo.type != PICL_PTYPE_UNSIGNED_INT) &&
476	    (pinfo.type != PICL_PTYPE_BYTEARRAY)) {
477		*ret = PICL_FAILURE;
478		return (0);
479	}
480
481	switch (pinfo.size) {
482	case sizeof (int8_t):
483		err = picl_get_propval(proph, &int8v, sizeof (int8v));
484		*ret = err;
485		return (int8v);
486	case sizeof (int16_t):
487		err = picl_get_propval(proph, &int16v, sizeof (int16v));
488		*ret = err;
489		return (int16v);
490	case sizeof (int32_t):
491		err = picl_get_propval(proph, &int32v, sizeof (int32v));
492		*ret = err;
493		return (int32v);
494	case sizeof (int64_t):
495		err = picl_get_propval(proph, &int64v, sizeof (int64v));
496		*ret = err;
497		return (int64v);
498	default:	/* not supported size */
499		*ret = PICL_FAILURE;
500		return (0);
501	}
502}
503