xref: /titanic_51/usr/src/common/devid/devid_smp.c (revision 5f661bbc7d40596c531df6eb04700d4deeb55e91)
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 /*
28  * These functions are used to encode SAS SMP address data into
29  * Solaris devid / guid values.
30  */
31 
32 #ifndef _KERNEL
33 #include <stdio.h>
34 #endif /* _KERNEL */
35 
36 #include <sys/inttypes.h>
37 #include <sys/types.h>
38 #include <sys/stropts.h>
39 #include <sys/debug.h>
40 #include <sys/isa_defs.h>
41 #include <sys/dditypes.h>
42 #include <sys/ddi_impldefs.h>
43 #include <sys/scsi/scsi.h>
44 #include <sys/scsi/generic/smp_frames.h>
45 #ifndef _KERNEL
46 #include <sys/libdevid.h>
47 #endif /* !_KERNEL */
48 #include "devid_impl.h"
49 
50 /*
51  * Typically the wwnstr makes a good devid, however in some cases the wwnstr
52  * comes form the location of a FRU in the chassis instead of from the identity
53  * of the FRU.  The table below provides vid/pid information for such cases.
54  * These vidpid strings are matched against smp_report_manufacturer_info_resp
55  * data. When a match occurs the srmir_vs_52 field, if non-zero, is used
56  * to form the devid.
57  */
58 char *vidpid_devid_from_srmir_vs_52[] = {
59 /*	"                  111111" */
60 /*	"012345670123456789012345" */
61 /*	"|-VID--||-----PID------|" */
62 	"SUN     GENESIS",
63 	NULL
64 };
65 
66 /*
67  *    Function: ddi_/devid_smp_encode
68  *
69  * Description: This routine finds and encodes a unique devid given the
70  *		SAS address of an SMP node.
71  *
72  *   Arguments: version - id encode algorithm version
73  *		driver_name - binding driver name (if ! known use NULL)
74  *		wwnstr - smp SAS address in wwnstr (unit-address) form.
75  *		srmir_buf - REPORT MANUFACTURER INFORMATION response.
76  *		srmir_len - amount of srmir_buf data.
77  *		devid - id returned
78  *
79  * Return Code: DEVID_SUCCESS - success
80  *		DEVID_FAILURE - failure
81  */
82 int
83 #ifdef _KERNEL
84 ddi_devid_smp_encode(
85 #else /* ! _KERNEL */
86 devid_smp_encode(
87 #endif /* _KERNEL */
88     int version,	/* IN */
89     char *driver_name,	/* IN */
90     char *wwnstr,	/* IN */
91     uchar_t *srmir_buf,	/* IN */
92     size_t srmir_len,	/* IN */
93     ddi_devid_t *devid)	/* OUT */
94 {
95 	uint64_t				wwn;
96 	ushort_t				raw_id_type;
97 	ushort_t				raw_id_len;
98 	impl_devid_t    			*i_devid;
99 	int					i_devid_len;
100 	int					i;
101 	smp_response_frame_t			*srs;
102 	smp_report_manufacturer_info_resp_t	*srmir;
103 	char					**vidpid;
104 	uint8_t					*vsp;
105 	uint64_t				s;
106 	char					sbuf[16 + 1];
107 	int					vlen, plen, slen;
108 	int					driver_name_len = 0;
109 
110 	DEVID_ASSERT(devid != NULL);
111 	*devid = NULL;
112 
113 	/* verify valid version */
114 	if (version > DEVID_SMP_ENCODE_VERSION_LATEST)
115 		return (DEVID_FAILURE);
116 
117 	if (wwnstr == NULL)
118 		return (DEVID_FAILURE);
119 
120 	/* convert wwnstr to binary */
121 	if (scsi_wwnstr_to_wwn(wwnstr, &wwn) != DDI_SUCCESS)
122 		return (DEVID_FAILURE);
123 
124 	if (srmir_buf &&
125 	    (srmir_len >= ((sizeof (*srs) - sizeof (srs->srf_data)) +
126 	    sizeof (*srmir)))) {
127 		srs = (smp_response_frame_t *)srmir_buf;
128 		srmir = (smp_report_manufacturer_info_resp_t *)srs->srf_data;
129 
130 		for (vidpid = vidpid_devid_from_srmir_vs_52; *vidpid; vidpid++)
131 			if (strncmp(srmir->srmir_vendor_identification,
132 			    *vidpid, strlen(*vidpid)) == 0)
133 				break;
134 
135 		/* no vid/pid match, use wwn for devid */
136 		if (*vidpid == NULL)
137 			goto usewwn;
138 
139 		/* extract the special vendor-specific 'devid serial number' */
140 		vsp = &srmir->srmir_vs_52[0];
141 		s = ((uint64_t)vsp[0] << 56) |
142 		    ((uint64_t)vsp[1] << 48) |
143 		    ((uint64_t)vsp[2] << 40) |
144 		    ((uint64_t)vsp[3] << 32) |
145 		    ((uint64_t)vsp[4] << 24) |
146 		    ((uint64_t)vsp[5] << 16) |
147 		    ((uint64_t)vsp[6] <<  8) |
148 		    ((uint64_t)vsp[7]);
149 
150 		/* discount zero value */
151 		if (s == 0)
152 			goto usewwn;
153 
154 		/* compute length (with trailing spaces removed) */
155 		vlen = scsi_ascii_inquiry_len(
156 		    srmir->srmir_vendor_identification,
157 		    sizeof (srmir->srmir_vendor_identification));
158 		plen = scsi_ascii_inquiry_len(
159 		    srmir->srmir_product_identification,
160 		    sizeof (srmir->srmir_product_identification));
161 		slen = snprintf(sbuf, sizeof (sbuf), "%016" PRIx64, s);
162 		if ((vlen <= 0) || (plen <= 0) || ((slen + 1) != sizeof (sbuf)))
163 			goto usewwn;
164 
165 		/* this is most like a devid formed from inquiry data */
166 		raw_id_type = DEVID_SCSI_SERIAL;
167 		raw_id_len = vlen + 1 + plen + 1 + slen;
168 
169 		i_devid_len = sizeof (*i_devid) +
170 		    raw_id_len - sizeof (i_devid->did_id);
171 		if ((i_devid = DEVID_MALLOC(i_devid_len)) == NULL)
172 			return (DEVID_FAILURE);
173 		bzero(i_devid, i_devid_len);
174 
175 		/* copy the vid to the beginning */
176 		bcopy(&srmir->srmir_vendor_identification,
177 		    &i_devid->did_id[0], vlen);
178 		i_devid->did_id[vlen] = '.';
179 
180 		/* copy the pid after the "vid." */
181 		bcopy(&srmir->srmir_product_identification,
182 		    &i_devid->did_id[vlen + 1], plen);
183 		i_devid->did_id[vlen + 1 + plen] = '.';
184 
185 		/* place the 'devid serial number' buffer the "vid.pid." */
186 		bcopy(sbuf, &i_devid->did_id[vlen + 1 + plen + 1], slen);
187 	} else {
188 usewwn:		raw_id_type = DEVID_SCSI3_WWN;
189 		raw_id_len = sizeof (wwn);
190 
191 		i_devid_len = sizeof (*i_devid) +
192 		    raw_id_len - sizeof (i_devid->did_id);
193 		if ((i_devid = DEVID_MALLOC(i_devid_len)) == NULL)
194 			return (DEVID_FAILURE);
195 		bzero(i_devid, i_devid_len);
196 
197 		/* binary devid stores wwn bytes in big-endian order */
198 		for (i = 0; i < sizeof (wwn); i++)
199 			i_devid->did_id[i] =
200 			    (wwn >> ((sizeof (wwn) * 8) -
201 			    ((i + 1) * 8))) & 0xFF;
202 
203 	}
204 
205 	i_devid->did_magic_hi = DEVID_MAGIC_MSB;
206 	i_devid->did_magic_lo = DEVID_MAGIC_LSB;
207 	i_devid->did_rev_hi = DEVID_REV_MSB;
208 	i_devid->did_rev_lo = DEVID_REV_LSB;
209 	DEVID_FORMTYPE(i_devid, raw_id_type);
210 	DEVID_FORMLEN(i_devid, raw_id_len);
211 
212 	/* fill in driver name hint */
213 	bzero(i_devid->did_driver, DEVID_HINT_SIZE);
214 	if (driver_name != NULL) {
215 		driver_name_len = strlen(driver_name);
216 		if (driver_name_len > DEVID_HINT_SIZE) {
217 			/* pick up last four characters of driver name */
218 			driver_name += driver_name_len - DEVID_HINT_SIZE;
219 			driver_name_len = DEVID_HINT_SIZE;
220 		}
221 		bcopy(driver_name, i_devid->did_driver, driver_name_len);
222 	}
223 
224 	/* return device id */
225 	*devid = (ddi_devid_t)i_devid;
226 	return (DEVID_SUCCESS);
227 }
228