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/*
23 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#include    <sun_sas.h>
28
29/*
30 * Discover an HBA node with  matching path.
31 * The di_node_t argument should be the root of the device tree.
32 * This routine assumes the locks have been taken
33 */
34static int
35find_matching_hba(di_node_t node, void *arg)
36{
37	int *propData, rval;
38	walkarg_t *wa = (walkarg_t *)arg;
39	char	*devpath, fulldevpath[MAXPATHLEN];
40
41	/* Skip stub(instance -1) nodes */
42	if (IS_STUB_NODE(node)) {
43		return (DI_WALK_CONTINUE);
44	}
45
46	rval = di_prop_lookup_ints(DDI_DEV_T_ANY, node,
47	    "sm-hba-supported", &propData);
48	if (rval < 0) {
49		return (DI_WALK_CONTINUE);
50	} else {
51		if ((devpath = di_devfs_path(node)) == NULL) {
52			/* still continue to see if there is matching one. */
53			return (DI_WALK_CONTINUE);
54		}
55		(void) snprintf(fulldevpath, MAXPATHLEN, "%s%s", DEVICES_DIR,
56		    devpath);
57
58		if ((strstr(fulldevpath, wa->devpath)) != NULL) {
59				*wa->flag = B_TRUE;
60			/* Found a node. No need to walk any more. */
61			di_devfs_path_free(devpath);
62			return (DI_WALK_TERMINATE);
63		}
64		di_devfs_path_free(devpath);
65	}
66
67	return (DI_WALK_CONTINUE);
68}
69
70/*
71 * Refreshes information about an HBA
72 *
73 * Note: This routine holds the locks in write mode
74 *       during most of the processing, and as such, will cause
75 *	 all other threads to block on entry into the library
76 *	 until the refresh is complete.  An optimization would be
77 *       to put fine-grain locking in for the open_handle structures.
78 */
79void
80Sun_sasRefreshAdapterConfiguration()
81{
82	const char		    ROUTINE[] =
83	    "Sun_sasRefreshAdapterConfiguration";
84	struct sun_sas_hba	    *hba_ptr;
85	di_node_t		    root;
86	hrtime_t		    start;
87	hrtime_t		    end;
88	double			    duration;
89	walkarg_t		    wa;
90
91	/*
92	 * We're going to be tweaking a lot of global lists, so write
93	 * lock them.
94	 */
95	lock(&all_hbas_lock);
96	lock(&open_handles_lock);
97
98	start = gethrtime();
99	/* Grab device tree */
100	if ((root = di_init("/", DINFOCACHE)) == DI_NODE_NIL) {
101		log(LOG_DEBUG, ROUTINE,
102		    "Unable to load device tree for reason \"%s\"",
103		    strerror(errno));
104		unlock(&open_handles_lock);
105		unlock(&all_hbas_lock);
106		return;
107	}
108
109	end = gethrtime();
110	duration = end - start;
111	duration /= HR_SECOND;
112	log(LOG_DEBUG, ROUTINE, "Device tree init took "
113	    "%.6f seconds", duration);
114
115	for (hba_ptr = global_hba_head; hba_ptr != NULL;
116	    hba_ptr = hba_ptr->next) {
117		wa.devpath = hba_ptr->device_path;
118		wa.flag = (boolean_t *)calloc(1, sizeof (boolean_t));
119		*wa.flag = B_FALSE;
120
121		if (di_walk_node(root, DI_WALK_SIBFIRST, &wa,
122		    find_matching_hba) != 0) {
123			log(LOG_DEBUG, ROUTINE, "di_walk_node failed.");
124			unlock(&open_handles_lock);
125			unlock(&all_hbas_lock);
126			S_FREE(wa.flag);
127			di_fini(root);
128			return;
129		}
130
131		if (*wa.flag == B_FALSE) {
132			/*
133			 * Keep the HBA as it is including open handles
134			 * per the SM-HBA standards. Just mark it as invalid.
135			 */
136			log(LOG_DEBUG, ROUTINE, "No matching HBA found. %s",
137			    hba_ptr->device_path);
138			hba_ptr->invalid = B_TRUE;
139		}
140		S_FREE(wa.flag);
141	}
142
143	/*
144	 * Now we marked missing HBA(s). Redisoover hbas to refresh
145	 * or add any new adapter to the hba list.
146	 * Simply call devtree_get_all_hbas().
147	 */
148	if (devtree_get_all_hbas(root) != HBA_STATUS_OK) {
149		log(LOG_DEBUG, ROUTINE, "devtree_get_all_hbas failed.");
150	}
151	di_fini(root);
152
153	unlock(&open_handles_lock);
154	unlock(&all_hbas_lock);
155}
156