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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * This file contains SM-HBA support for PMC-S driver
27  */
28 
29 #include <sys/scsi/adapters/pmcs/pmcs.h>
30 
31 
32 void
33 pmcs_smhba_add_hba_prop(pmcs_hw_t *pwp, data_type_t dt,
34     char *prop_name, void *prop_val)
35 {
36 	ASSERT(pwp != NULL);
37 
38 	switch (dt) {
39 	case DATA_TYPE_INT32:
40 		if (ddi_prop_update_int(DDI_DEV_T_NONE, pwp->dip,
41 		    prop_name, *(int *)prop_val)) {
42 			pmcs_prt(pwp, PMCS_PRT_DEBUG,
43 			    "%s: %s prop update failed", __func__, prop_name);
44 		}
45 		break;
46 	case DATA_TYPE_STRING:
47 		if (ddi_prop_update_string(DDI_DEV_T_NONE, pwp->dip,
48 		    prop_name, (char *)prop_val)) {
49 			pmcs_prt(pwp, PMCS_PRT_DEBUG,
50 			    "%s: %s prop update failed", __func__, prop_name);
51 		}
52 		break;
53 	default:
54 		pmcs_prt(pwp, PMCS_PRT_DEBUG, "%s: Unhandled datatype(%d) for "
55 		    "(%s). Skipping prop update.", __func__, dt, prop_name);
56 	}
57 }
58 
59 
60 void
61 pmcs_smhba_add_iport_prop(pmcs_iport_t *iport, data_type_t dt,
62     char *prop_name, void *prop_val)
63 {
64 	ASSERT(iport != NULL);
65 
66 	switch (dt) {
67 	case DATA_TYPE_INT32:
68 		if (ddi_prop_update_int(DDI_DEV_T_NONE, iport->dip,
69 		    prop_name, *(int *)prop_val)) {
70 			pmcs_prt(iport->pwp, PMCS_PRT_DEBUG,
71 			    "%s: %s prop update failed", __func__, prop_name);
72 		}
73 		break;
74 	case DATA_TYPE_STRING:
75 		if (ddi_prop_update_string(DDI_DEV_T_NONE, iport->dip,
76 		    prop_name, (char *)prop_val)) {
77 			pmcs_prt(iport->pwp, PMCS_PRT_DEBUG,
78 			    "%s: %s prop update failed", __func__, prop_name);
79 		}
80 		break;
81 	default:
82 		pmcs_prt(iport->pwp, PMCS_PRT_DEBUG, "%s: Unhandled "
83 		    "datatype(%d) for(%s). Skipping prop update.",
84 		    __func__, dt, prop_name);
85 	}
86 }
87 
88 
89 void
90 pmcs_smhba_add_tgt_prop(pmcs_xscsi_t *tgt, data_type_t dt,
91     char *prop_name, void *prop_val)
92 {
93 	ASSERT(tgt != NULL);
94 
95 	switch (dt) {
96 	case DATA_TYPE_INT32:
97 		if (ddi_prop_update_int(DDI_DEV_T_NONE, tgt->dip,
98 		    prop_name, *(int *)prop_val)) {
99 			pmcs_prt(tgt->pwp, PMCS_PRT_DEBUG,
100 			    "%s: %s prop update failed", __func__, prop_name);
101 		}
102 		break;
103 	case DATA_TYPE_STRING:
104 		if (ddi_prop_update_string(DDI_DEV_T_NONE, tgt->dip,
105 		    prop_name, (char *)prop_val)) {
106 			pmcs_prt(tgt->pwp, PMCS_PRT_DEBUG,
107 			    "%s: %s prop update failed", __func__, prop_name);
108 		}
109 		break;
110 	default:
111 		pmcs_prt(tgt->pwp, PMCS_PRT_DEBUG, "%s: Unhandled datatype(%d) "
112 		    "for (%s). Skipping prop update.", __func__, dt, prop_name);
113 	}
114 }
115 
116 /* ARGSUSED */
117 void
118 pmcs_smhba_set_scsi_device_props(pmcs_hw_t *pwp, pmcs_phy_t *pptr,
119     struct scsi_device *sd)
120 {
121 	char		*addr;
122 	int		ua_form = 1;
123 	uint64_t	wwn;
124 	pmcs_phy_t	*pphy;
125 
126 	pphy = pptr->parent;
127 
128 	if (pphy != NULL) {
129 		addr = kmem_zalloc(PMCS_MAX_UA_SIZE, KM_SLEEP);
130 		wwn = pmcs_barray2wwn(pphy->sas_address);
131 		(void) scsi_wwn_to_wwnstr(wwn, ua_form, addr);
132 
133 		if (pphy->dtype == SATA) {
134 			(void) scsi_device_prop_update_string(sd,
135 			    SCSI_DEVICE_PROP_PATH,
136 			    SCSI_ADDR_PROP_BRIDGE_PORT, addr);
137 		}
138 		if (pphy->dtype == EXPANDER) {
139 			(void) scsi_device_prop_update_string(sd,
140 			    SCSI_DEVICE_PROP_PATH,
141 			    SCSI_ADDR_PROP_ATTACHED_PORT, addr);
142 		}
143 		kmem_free(addr, PMCS_MAX_UA_SIZE);
144 	}
145 }
146 
147 void
148 pmcs_smhba_set_phy_props(pmcs_iport_t *iport)
149 {
150 	int		i;
151 	size_t		packed_size;
152 	char		*packed_data;
153 	pmcs_hw_t	*pwp = iport->pwp;
154 	pmcs_phy_t	*phy_ptr;
155 	nvlist_t	**phy_props;
156 	nvlist_t	*nvl;
157 
158 	mutex_enter(&iport->lock);
159 	if (iport->nphy == 0) {
160 		mutex_exit(&iport->lock);
161 		return;
162 	}
163 
164 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
165 		pmcs_prt(pwp, PMCS_PRT_DEBUG, "%s: nvlist_alloc() failed",
166 		    __func__);
167 	}
168 
169 	phy_props = kmem_zalloc(sizeof (nvlist_t *) * iport->nphy, KM_SLEEP);
170 
171 	for (phy_ptr = list_head(&iport->phys), i = 0;
172 	    phy_ptr != NULL;
173 	    phy_ptr = list_next(&iport->phys, phy_ptr), i++) {
174 		pmcs_lock_phy(phy_ptr);
175 
176 		(void) nvlist_alloc(&phy_props[i], NV_UNIQUE_NAME, 0);
177 
178 		(void) nvlist_add_uint8(phy_props[i], SAS_PHY_ID,
179 		    phy_ptr->phynum);
180 		(void) nvlist_add_int8(phy_props[i], SAS_NEG_LINK_RATE,
181 		    phy_ptr->link_rate);
182 		(void) nvlist_add_int8(phy_props[i], SAS_PROG_MIN_LINK_RATE,
183 		    phy_ptr->state.prog_min_rate);
184 		(void) nvlist_add_int8(phy_props[i], SAS_HW_MIN_LINK_RATE,
185 		    phy_ptr->state.hw_min_rate);
186 		(void) nvlist_add_int8(phy_props[i], SAS_PROG_MAX_LINK_RATE,
187 		    phy_ptr->state.prog_max_rate);
188 		(void) nvlist_add_int8(phy_props[i], SAS_HW_MAX_LINK_RATE,
189 		    phy_ptr->state.hw_max_rate);
190 
191 		pmcs_unlock_phy(phy_ptr);
192 	}
193 
194 	(void) nvlist_add_nvlist_array(nvl, SAS_PHY_INFO_NVL, phy_props,
195 	    iport->nphy);
196 
197 	(void) nvlist_size(nvl, &packed_size, NV_ENCODE_NATIVE);
198 	packed_data = kmem_zalloc(packed_size, KM_SLEEP);
199 	(void) nvlist_pack(nvl, &packed_data, &packed_size,
200 	    NV_ENCODE_NATIVE, 0);
201 
202 	(void) ddi_prop_update_byte_array(DDI_DEV_T_NONE, iport->dip,
203 	    SAS_PHY_INFO, (uchar_t *)packed_data, packed_size);
204 
205 	for (i = 0; i < iport->nphy && phy_props[i] != NULL; i++) {
206 		nvlist_free(phy_props[i]);
207 	}
208 	nvlist_free(nvl);
209 	kmem_free(phy_props, sizeof (nvlist_t *) * iport->nphy);
210 	mutex_exit(&iport->lock);
211 	kmem_free(packed_data, packed_size);
212 }
213 
214 /*
215  * Called with PHY lock held on phyp
216  */
217 void
218 pmcs_smhba_log_sysevent(pmcs_hw_t *pwp, char *subclass, char *etype,
219     pmcs_phy_t *phyp)
220 {
221 	nvlist_t	*attr_list;
222 	char		*pname;
223 	char		sas_addr[PMCS_MAX_UA_SIZE];
224 	uint8_t		phynum = 0;
225 	uint8_t		lrate = 0;
226 	uint64_t	wwn;
227 	int		ua_form = 0;
228 
229 	if (pwp->dip == NULL)
230 		return;
231 	if (phyp == NULL)
232 		return;
233 
234 	pname = kmem_zalloc(MAXPATHLEN, KM_NOSLEEP);
235 	if (pname == NULL)
236 		return;
237 
238 	if ((strcmp(subclass, ESC_SAS_PHY_EVENT) == 0) ||
239 	    (strcmp(subclass, ESC_SAS_HBA_PORT_BROADCAST) == 0)) {
240 		ASSERT(phyp != NULL);
241 		(void) strncpy(pname, phyp->path, strlen(phyp->path));
242 		phynum = phyp->phynum;
243 		wwn = pmcs_barray2wwn(phyp->sas_address);
244 		(void) scsi_wwn_to_wwnstr(wwn, ua_form, sas_addr);
245 		if (strcmp(etype, SAS_PHY_ONLINE) == 0) {
246 			lrate = phyp->link_rate;
247 		}
248 	}
249 	if (strcmp(subclass, ESC_SAS_HBA_PORT_BROADCAST) == 0) {
250 		(void) ddi_pathname(pwp->dip, pname);
251 	}
252 
253 	if (nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE, 0) != 0) {
254 		pmcs_prt(pwp, PMCS_PRT_DEBUG, "%s: Failed to post sysevent",
255 		    __func__);
256 		kmem_free(pname, MAXPATHLEN);
257 		return;
258 	}
259 
260 	if (nvlist_add_int32(attr_list, SAS_DRV_INST,
261 	    ddi_get_instance(pwp->dip)) != 0)
262 		goto fail;
263 
264 	if (nvlist_add_string(attr_list, SAS_PORT_ADDR, sas_addr) != 0)
265 		goto fail;
266 
267 	if (nvlist_add_string(attr_list, SAS_DEVFS_PATH, pname) != 0)
268 		goto fail;
269 
270 	if (nvlist_add_uint8(attr_list, SAS_PHY_ID, phynum) != 0)
271 		goto fail;
272 
273 	if (strcmp(etype, SAS_PHY_ONLINE) == 0) {
274 		if (nvlist_add_uint8(attr_list, SAS_LINK_RATE, lrate) != 0)
275 			goto fail;
276 	}
277 
278 	if (nvlist_add_string(attr_list, SAS_EVENT_TYPE, etype) != 0)
279 		goto fail;
280 
281 	(void) ddi_log_sysevent(pwp->dip, DDI_VENDOR_SUNW, EC_HBA, subclass,
282 	    attr_list, NULL, DDI_NOSLEEP);
283 
284 fail:
285 	kmem_free(pname, MAXPATHLEN);
286 	nvlist_free(attr_list);
287 }
288