17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
26981fe1b1SJohn Levon
27981fe1b1SJohn Levon /*
28981fe1b1SJohn Levon * Copyright (c) 2018, Joyent, Inc.
29981fe1b1SJohn Levon */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
337c478bd9Sstevel@tonic-gate #include <sys/debug.h>
347c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
357c478bd9Sstevel@tonic-gate #include <sys/dditypes.h>
367c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
377c478bd9Sstevel@tonic-gate #include "devid_impl.h"
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate static int devid_str_decode_id(char *devidstr, ddi_devid_t *devidp,
407c478bd9Sstevel@tonic-gate char **minor_namep, impl_devid_t *id);
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate * Validate device id.
457c478bd9Sstevel@tonic-gate */
467c478bd9Sstevel@tonic-gate int
477c478bd9Sstevel@tonic-gate #ifdef _KERNEL
ddi_devid_valid(ddi_devid_t devid)487c478bd9Sstevel@tonic-gate ddi_devid_valid(ddi_devid_t devid)
497c478bd9Sstevel@tonic-gate #else /* !_KERNEL */
507c478bd9Sstevel@tonic-gate devid_valid(ddi_devid_t devid)
517c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate impl_devid_t *id = (impl_devid_t *)devid;
547c478bd9Sstevel@tonic-gate ushort_t type;
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate DEVID_ASSERT(devid != NULL);
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate if (id->did_magic_hi != DEVID_MAGIC_MSB)
597c478bd9Sstevel@tonic-gate return (DEVID_RET_INVALID);
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate if (id->did_magic_lo != DEVID_MAGIC_LSB)
627c478bd9Sstevel@tonic-gate return (DEVID_RET_INVALID);
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate if (id->did_rev_hi != DEVID_REV_MSB)
657c478bd9Sstevel@tonic-gate return (DEVID_RET_INVALID);
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate if (id->did_rev_lo != DEVID_REV_LSB)
687c478bd9Sstevel@tonic-gate return (DEVID_RET_INVALID);
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate type = DEVID_GETTYPE(id);
717c478bd9Sstevel@tonic-gate if ((type == DEVID_NONE) || (type > DEVID_MAXTYPE))
727c478bd9Sstevel@tonic-gate return (DEVID_RET_INVALID);
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate return (DEVID_RET_VALID);
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate * Return the sizeof a device id. If called with NULL devid it returns
797c478bd9Sstevel@tonic-gate * the amount of space needed to determine the size.
807c478bd9Sstevel@tonic-gate */
817c478bd9Sstevel@tonic-gate size_t
827c478bd9Sstevel@tonic-gate #ifdef _KERNEL
ddi_devid_sizeof(ddi_devid_t devid)837c478bd9Sstevel@tonic-gate ddi_devid_sizeof(ddi_devid_t devid)
847c478bd9Sstevel@tonic-gate #else /* !_KERNEL */
857c478bd9Sstevel@tonic-gate devid_sizeof(ddi_devid_t devid)
867c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate impl_devid_t *id = (impl_devid_t *)devid;
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate if (id == NULL)
917c478bd9Sstevel@tonic-gate return (sizeof (*id) - sizeof (id->did_id));
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate DEVID_ASSERT(DEVID_FUNC(devid_valid)(devid) == DEVID_RET_VALID);
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate return (sizeof (*id) + DEVID_GETLEN(id) - sizeof (id->did_id));
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate * Compare two device id's.
1007c478bd9Sstevel@tonic-gate * -1 - less than
1017c478bd9Sstevel@tonic-gate * 0 - equal
1027c478bd9Sstevel@tonic-gate * 1 - greater than
1037c478bd9Sstevel@tonic-gate */
1047c478bd9Sstevel@tonic-gate int
1057c478bd9Sstevel@tonic-gate #ifdef _KERNEL
ddi_devid_compare(ddi_devid_t id1,ddi_devid_t id2)1067c478bd9Sstevel@tonic-gate ddi_devid_compare(ddi_devid_t id1, ddi_devid_t id2)
1077c478bd9Sstevel@tonic-gate #else /* !_KERNEL */
1087c478bd9Sstevel@tonic-gate devid_compare(ddi_devid_t id1, ddi_devid_t id2)
1097c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate int rval;
1127c478bd9Sstevel@tonic-gate impl_devid_t *i_id1 = (impl_devid_t *)id1;
1137c478bd9Sstevel@tonic-gate impl_devid_t *i_id2 = (impl_devid_t *)id2;
1147c478bd9Sstevel@tonic-gate ushort_t i_id1_type;
1157c478bd9Sstevel@tonic-gate ushort_t i_id2_type;
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate DEVID_ASSERT((id1 != NULL) && (id2 != NULL));
1187c478bd9Sstevel@tonic-gate DEVID_ASSERT(DEVID_FUNC(devid_valid)(id1) == DEVID_RET_VALID);
1197c478bd9Sstevel@tonic-gate DEVID_ASSERT(DEVID_FUNC(devid_valid)(id2) == DEVID_RET_VALID);
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate /* magic and revision comparison */
1227c478bd9Sstevel@tonic-gate if ((rval = bcmp(id1, id2, 4)) != 0) {
1237c478bd9Sstevel@tonic-gate return (rval);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate /* get current devid types */
1277c478bd9Sstevel@tonic-gate i_id1_type = DEVID_GETTYPE(i_id1);
1287c478bd9Sstevel@tonic-gate i_id2_type = DEVID_GETTYPE(i_id2);
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate /*
1317c478bd9Sstevel@tonic-gate * Originaly all page83 devids used DEVID_SCSI3_WWN.
1327c478bd9Sstevel@tonic-gate * To avoid a possible uniqueness issue each type of page83
1337c478bd9Sstevel@tonic-gate * encoding supported is represented as a separate
1347c478bd9Sstevel@tonic-gate * devid type. If comparing DEVID_SCSI3_WWN against
1357c478bd9Sstevel@tonic-gate * one of the new page83 encodings we assume that no
1367c478bd9Sstevel@tonic-gate * uniqueness issue exists (since we had apparently been
1377c478bd9Sstevel@tonic-gate * running with the old DEVID_SCSI3_WWN encoding without
1387c478bd9Sstevel@tonic-gate * a problem).
1397c478bd9Sstevel@tonic-gate */
1407c478bd9Sstevel@tonic-gate if ((i_id1_type == DEVID_SCSI3_WWN) ||
1417c478bd9Sstevel@tonic-gate (i_id2_type == DEVID_SCSI3_WWN)) {
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate * Atleast one devid is using old scsi
1447c478bd9Sstevel@tonic-gate * encode algorithm. Force devid types
1457c478bd9Sstevel@tonic-gate * to same scheme for comparison.
1467c478bd9Sstevel@tonic-gate */
1477c478bd9Sstevel@tonic-gate if (IS_DEVID_SCSI3_VPD_TYPE(i_id1_type)) {
1487c478bd9Sstevel@tonic-gate i_id1_type = DEVID_SCSI3_WWN;
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate if (IS_DEVID_SCSI3_VPD_TYPE(i_id2_type)) {
1517c478bd9Sstevel@tonic-gate i_id2_type = DEVID_SCSI3_WWN;
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate /* type comparison */
1567c478bd9Sstevel@tonic-gate if (i_id1_type != i_id2_type) {
1577c478bd9Sstevel@tonic-gate return ((i_id1_type < i_id2_type) ? -1 : 1);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate /* length comparison */
1617c478bd9Sstevel@tonic-gate if (DEVID_GETLEN(i_id1) != DEVID_GETLEN(i_id2)) {
1627c478bd9Sstevel@tonic-gate return (DEVID_GETLEN(i_id1) < DEVID_GETLEN(i_id2) ? -1 : 1);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate /* id comparison */
1667c478bd9Sstevel@tonic-gate rval = bcmp(i_id1->did_id, i_id2->did_id, DEVID_GETLEN(i_id1));
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate return (rval);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate * Free a Device Id
1737c478bd9Sstevel@tonic-gate */
1747c478bd9Sstevel@tonic-gate void
1757c478bd9Sstevel@tonic-gate #ifdef _KERNEL
ddi_devid_free(ddi_devid_t devid)1767c478bd9Sstevel@tonic-gate ddi_devid_free(ddi_devid_t devid)
1777c478bd9Sstevel@tonic-gate #else /* !_KERNEL */
1787c478bd9Sstevel@tonic-gate devid_free(ddi_devid_t devid)
1797c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate DEVID_ASSERT(devid != NULL);
1827c478bd9Sstevel@tonic-gate DEVID_FREE(devid, DEVID_FUNC(devid_sizeof)(devid));
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate * Encode a device id into a string. See ddi_impldefs.h for details.
1877c478bd9Sstevel@tonic-gate */
1887c478bd9Sstevel@tonic-gate char *
1897c478bd9Sstevel@tonic-gate #ifdef _KERNEL
ddi_devid_str_encode(ddi_devid_t devid,char * minor_name)1907c478bd9Sstevel@tonic-gate ddi_devid_str_encode(ddi_devid_t devid, char *minor_name)
1917c478bd9Sstevel@tonic-gate #else /* !_KERNEL */
1927c478bd9Sstevel@tonic-gate devid_str_encode(ddi_devid_t devid, char *minor_name)
1937c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate impl_devid_t *id = (impl_devid_t *)devid;
1967c478bd9Sstevel@tonic-gate size_t driver_len, devid_len, slen;
1977c478bd9Sstevel@tonic-gate char *sbuf, *dsp, *dp, ta;
1987c478bd9Sstevel@tonic-gate int i, n, ascii;
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate /* "id0" is the encoded representation of a NULL device id */
2017c478bd9Sstevel@tonic-gate if (devid == NULL) {
2027c478bd9Sstevel@tonic-gate if ((sbuf = DEVID_MALLOC(4)) == NULL)
2037c478bd9Sstevel@tonic-gate return (NULL);
2047c478bd9Sstevel@tonic-gate *(sbuf+0) = DEVID_MAGIC_MSB;
2057c478bd9Sstevel@tonic-gate *(sbuf+1) = DEVID_MAGIC_LSB;
2067c478bd9Sstevel@tonic-gate *(sbuf+2) = '0';
2077c478bd9Sstevel@tonic-gate *(sbuf+3) = 0;
2087c478bd9Sstevel@tonic-gate return (sbuf);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate /* verify input */
2127c478bd9Sstevel@tonic-gate if (DEVID_FUNC(devid_valid)(devid) != DEVID_RET_VALID)
2137c478bd9Sstevel@tonic-gate return (NULL);
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate /* scan the driver hint to see how long the hint is */
2167c478bd9Sstevel@tonic-gate for (driver_len = 0; driver_len < DEVID_HINT_SIZE; driver_len++)
2177c478bd9Sstevel@tonic-gate if (id->did_driver[driver_len] == '\0')
2187c478bd9Sstevel@tonic-gate break;
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate /* scan the contained did_id to see if it meets ascii requirements */
2217c478bd9Sstevel@tonic-gate devid_len = DEVID_GETLEN(id);
2227c478bd9Sstevel@tonic-gate for (ascii = 1, i = 0; i < devid_len; i++)
2237c478bd9Sstevel@tonic-gate if (!DEVID_IDBYTE_ISASCII(id->did_id[i])) {
2247c478bd9Sstevel@tonic-gate ascii = 0;
2257c478bd9Sstevel@tonic-gate break;
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate
2287c478bd9Sstevel@tonic-gate /* some types should always go hex even if they look ascii */
2297c478bd9Sstevel@tonic-gate if (DEVID_TYPE_BIN_FORCEHEX(id->did_type_lo))
2307c478bd9Sstevel@tonic-gate ascii = 0;
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate /* set the length of the resulting string */
2337c478bd9Sstevel@tonic-gate slen = 2 + 1; /* <magic><rev> "id1" */
2347c478bd9Sstevel@tonic-gate slen += 1 + driver_len + 1 + 1; /* ",<driver>@<type>" */
2357c478bd9Sstevel@tonic-gate slen += ascii ? devid_len : (devid_len * 2); /* did_id field */
2367c478bd9Sstevel@tonic-gate if (minor_name) {
2377c478bd9Sstevel@tonic-gate slen += 1; /* '/' */
2387c478bd9Sstevel@tonic-gate slen += strlen(minor_name); /* len of minor_name */
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate slen += 1; /* NULL */
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate /* allocate string */
2437c478bd9Sstevel@tonic-gate if ((sbuf = DEVID_MALLOC(slen)) == NULL)
2447c478bd9Sstevel@tonic-gate return (NULL);
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate /* perform encode of id to hex string */
2477c478bd9Sstevel@tonic-gate dsp = sbuf;
2487c478bd9Sstevel@tonic-gate *dsp++ = id->did_magic_hi;
2497c478bd9Sstevel@tonic-gate *dsp++ = id->did_magic_lo;
2507c478bd9Sstevel@tonic-gate *dsp++ = DEVID_REV_BINTOASCII(id->did_rev_lo);
2517c478bd9Sstevel@tonic-gate *dsp++ = ',';
2527c478bd9Sstevel@tonic-gate for (i = 0; i < driver_len; i++)
2537c478bd9Sstevel@tonic-gate *dsp++ = id->did_driver[i];
2547c478bd9Sstevel@tonic-gate *dsp++ = '@';
2557c478bd9Sstevel@tonic-gate ta = DEVID_TYPE_BINTOASCII(id->did_type_lo);
2567c478bd9Sstevel@tonic-gate if (ascii)
2577c478bd9Sstevel@tonic-gate ta = DEVID_TYPE_SETASCII(ta);
2587c478bd9Sstevel@tonic-gate *dsp++ = ta;
2597c478bd9Sstevel@tonic-gate for (i = 0, dp = &id->did_id[0]; i < devid_len; i++, dp++) {
2607c478bd9Sstevel@tonic-gate if (ascii) {
2617c478bd9Sstevel@tonic-gate if (*dp == ' ')
2627c478bd9Sstevel@tonic-gate *dsp++ = '_';
2637c478bd9Sstevel@tonic-gate else if (*dp == 0x00)
2647c478bd9Sstevel@tonic-gate *dsp++ = '~';
2657c478bd9Sstevel@tonic-gate else
2667c478bd9Sstevel@tonic-gate *dsp++ = *dp;
2677c478bd9Sstevel@tonic-gate } else {
2687c478bd9Sstevel@tonic-gate n = ((*dp) >> 4) & 0xF;
2697c478bd9Sstevel@tonic-gate *dsp++ = (n < 10) ? (n + '0') : (n + ('a' - 10));
2707c478bd9Sstevel@tonic-gate n = (*dp) & 0xF;
2717c478bd9Sstevel@tonic-gate *dsp++ = (n < 10) ? (n + '0') : (n + ('a' - 10));
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate if (minor_name) {
2767c478bd9Sstevel@tonic-gate *dsp++ = '/';
2777c478bd9Sstevel@tonic-gate (void) strcpy(dsp, minor_name);
2787c478bd9Sstevel@tonic-gate } else
2797c478bd9Sstevel@tonic-gate *dsp++ = 0;
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate /* ensure that (strlen + 1) is correct length for free */
2827c478bd9Sstevel@tonic-gate DEVID_ASSERT((strlen(sbuf) + 1) == slen);
2837c478bd9Sstevel@tonic-gate return (sbuf);
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate
2867c478bd9Sstevel@tonic-gate /* free the string returned by devid_str_encode */
2877c478bd9Sstevel@tonic-gate void
2887c478bd9Sstevel@tonic-gate #ifdef _KERNEL
ddi_devid_str_free(char * devidstr)2897c478bd9Sstevel@tonic-gate ddi_devid_str_free(char *devidstr)
2907c478bd9Sstevel@tonic-gate #else /* !_KERNEL */
2917c478bd9Sstevel@tonic-gate devid_str_free(char *devidstr)
2927c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
2937c478bd9Sstevel@tonic-gate {
2947c478bd9Sstevel@tonic-gate DEVID_FREE(devidstr, strlen(devidstr) + 1);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate
2977c478bd9Sstevel@tonic-gate /*
2987c478bd9Sstevel@tonic-gate * given the string representation of a device id returned by calling
2997c478bd9Sstevel@tonic-gate * devid_str_encode (passed in as devidstr), return pointers to the
3007c478bd9Sstevel@tonic-gate * broken out devid and minor_name as requested. Devidstr remains
3017c478bd9Sstevel@tonic-gate * allocated and unmodified. The devid returned in *devidp should be freed by
3027c478bd9Sstevel@tonic-gate * calling devid_free. The minor_name returned in minor_namep should
3037c478bd9Sstevel@tonic-gate * be freed by calling devid_str_free(minor_namep).
3047c478bd9Sstevel@tonic-gate *
3057c478bd9Sstevel@tonic-gate * See ddi_impldefs.h for format details.
3067c478bd9Sstevel@tonic-gate */
3077c478bd9Sstevel@tonic-gate int
3087c478bd9Sstevel@tonic-gate #ifdef _KERNEL
ddi_devid_str_decode(char * devidstr,ddi_devid_t * devidp,char ** minor_namep)3097c478bd9Sstevel@tonic-gate ddi_devid_str_decode(
3107c478bd9Sstevel@tonic-gate #else /* !_KERNEL */
3117c478bd9Sstevel@tonic-gate devid_str_decode(
3127c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
3137c478bd9Sstevel@tonic-gate char *devidstr, ddi_devid_t *devidp, char **minor_namep)
3147c478bd9Sstevel@tonic-gate {
3157c478bd9Sstevel@tonic-gate return (devid_str_decode_id(devidstr, devidp, minor_namep, NULL));
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate
3187c478bd9Sstevel@tonic-gate /* implementation for (ddi_)devid_str_decode */
3197c478bd9Sstevel@tonic-gate static int
devid_str_decode_id(char * devidstr,ddi_devid_t * devidp,char ** minor_namep,impl_devid_t * id)3207c478bd9Sstevel@tonic-gate devid_str_decode_id(char *devidstr, ddi_devid_t *devidp,
3217c478bd9Sstevel@tonic-gate char **minor_namep, impl_devid_t *id)
3227c478bd9Sstevel@tonic-gate {
3237c478bd9Sstevel@tonic-gate char *str, *msp, *dsp, *dp, ta;
3247c478bd9Sstevel@tonic-gate int slen, devid_len, ascii, i, n, c, pre_alloc = FALSE;
3257c478bd9Sstevel@tonic-gate unsigned short id_len, type; /* for hibyte/lobyte */
3267c478bd9Sstevel@tonic-gate
327*c6f039c7SToomas Soome devid_len = 0;
3287c478bd9Sstevel@tonic-gate if (devidp != NULL)
3297c478bd9Sstevel@tonic-gate *devidp = NULL;
3307c478bd9Sstevel@tonic-gate if (minor_namep != NULL)
3317c478bd9Sstevel@tonic-gate *minor_namep = NULL;
3327c478bd9Sstevel@tonic-gate if (id != NULL)
3337c478bd9Sstevel@tonic-gate pre_alloc = TRUE;
3347c478bd9Sstevel@tonic-gate
3357c478bd9Sstevel@tonic-gate if (devidstr == NULL)
3367c478bd9Sstevel@tonic-gate return (DEVID_FAILURE);
3377c478bd9Sstevel@tonic-gate
3387c478bd9Sstevel@tonic-gate /* the string must atleast contain the ascii two byte header */
3397c478bd9Sstevel@tonic-gate slen = strlen(devidstr);
3407c478bd9Sstevel@tonic-gate if ((slen < 3) || (devidstr[0] != DEVID_MAGIC_MSB) ||
3417c478bd9Sstevel@tonic-gate (devidstr[1] != DEVID_MAGIC_LSB))
3427c478bd9Sstevel@tonic-gate return (DEVID_FAILURE);
3437c478bd9Sstevel@tonic-gate
3447c478bd9Sstevel@tonic-gate /* "id0" is the encoded representation of a NULL device id */
3457c478bd9Sstevel@tonic-gate if ((devidstr[2] == '0') && (slen == 3))
3467c478bd9Sstevel@tonic-gate return (DEVID_SUCCESS);
3477c478bd9Sstevel@tonic-gate
3487c478bd9Sstevel@tonic-gate /* "id1,@S0" is the shortest possible, reject if shorter */
3497c478bd9Sstevel@tonic-gate if (slen < 7)
3507c478bd9Sstevel@tonic-gate return (DEVID_FAILURE);
3517c478bd9Sstevel@tonic-gate
3527c478bd9Sstevel@tonic-gate /* find the optional minor name, start after ',' */
3537c478bd9Sstevel@tonic-gate if ((msp = strchr(&devidstr[4], '/')) != NULL)
3547c478bd9Sstevel@tonic-gate msp++;
3557c478bd9Sstevel@tonic-gate
3567c478bd9Sstevel@tonic-gate /* skip devid processing if we are not asked to return it */
3577c478bd9Sstevel@tonic-gate if (devidp) {
3587c478bd9Sstevel@tonic-gate /* find the required '@' separator */
3597c478bd9Sstevel@tonic-gate if ((str = strchr(devidstr, '@')) == NULL)
3607c478bd9Sstevel@tonic-gate return (DEVID_FAILURE);
3617c478bd9Sstevel@tonic-gate str++; /* skip '@' */
3627c478bd9Sstevel@tonic-gate
3637c478bd9Sstevel@tonic-gate /* pick up <type> after the '@' and verify */
3647c478bd9Sstevel@tonic-gate ta = *str++;
3657c478bd9Sstevel@tonic-gate ascii = DEVID_TYPE_ISASCII(ta);
3667c478bd9Sstevel@tonic-gate type = DEVID_TYPE_ASCIITOBIN(ta);
3677c478bd9Sstevel@tonic-gate if (type > DEVID_MAXTYPE)
3687c478bd9Sstevel@tonic-gate return (DEVID_FAILURE);
3697c478bd9Sstevel@tonic-gate
3707c478bd9Sstevel@tonic-gate /* determine length of id->did_id field */
3717c478bd9Sstevel@tonic-gate if (msp == NULL)
3727c478bd9Sstevel@tonic-gate id_len = strlen(str);
3737c478bd9Sstevel@tonic-gate else
3747c478bd9Sstevel@tonic-gate id_len = msp - str - 1;
3757c478bd9Sstevel@tonic-gate
3767c478bd9Sstevel@tonic-gate /* account for encoding: with hex, binary is half the size */
3777c478bd9Sstevel@tonic-gate if (!ascii) {
3787c478bd9Sstevel@tonic-gate /* hex id field must be even length */
3797c478bd9Sstevel@tonic-gate if (id_len & 1)
3807c478bd9Sstevel@tonic-gate return (DEVID_FAILURE);
3817c478bd9Sstevel@tonic-gate id_len /= 2;
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate
3847c478bd9Sstevel@tonic-gate /* add in size of the binary devid header */
3857c478bd9Sstevel@tonic-gate devid_len = id_len + sizeof (*id) - sizeof (id->did_id);
3867c478bd9Sstevel@tonic-gate
3877c478bd9Sstevel@tonic-gate /*
3887c478bd9Sstevel@tonic-gate * Allocate space for devid if we are asked to decode it
3897c478bd9Sstevel@tonic-gate * decode it and space wasn't pre-allocated.
3907c478bd9Sstevel@tonic-gate */
3917c478bd9Sstevel@tonic-gate if (pre_alloc == FALSE) {
3927c478bd9Sstevel@tonic-gate if ((id = (impl_devid_t *)DEVID_MALLOC(
3937c478bd9Sstevel@tonic-gate devid_len)) == NULL)
3947c478bd9Sstevel@tonic-gate return (DEVID_FAILURE);
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate
3977c478bd9Sstevel@tonic-gate /* decode header portion of the string into the binary devid */
3987c478bd9Sstevel@tonic-gate dsp = devidstr;
3997c478bd9Sstevel@tonic-gate id->did_magic_hi = *dsp++; /* <magic> "id" */
4007c478bd9Sstevel@tonic-gate id->did_magic_lo = *dsp++;
4017c478bd9Sstevel@tonic-gate id->did_rev_hi = 0;
4027c478bd9Sstevel@tonic-gate id->did_rev_lo =
4037c478bd9Sstevel@tonic-gate DEVID_REV_ASCIITOBIN(*dsp); /* <rev> "1" */
4047c478bd9Sstevel@tonic-gate dsp++; /* skip "1" */
4057c478bd9Sstevel@tonic-gate dsp++; /* skip "," */
4067c478bd9Sstevel@tonic-gate for (i = 0; i < DEVID_HINT_SIZE; i++) { /* <driver>@ */
4077c478bd9Sstevel@tonic-gate if (*dsp == '@')
4087c478bd9Sstevel@tonic-gate break;
4097c478bd9Sstevel@tonic-gate id->did_driver[i] = *dsp++;
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate for (; i < DEVID_HINT_SIZE; i++)
4127c478bd9Sstevel@tonic-gate id->did_driver[i] = 0;
4137c478bd9Sstevel@tonic-gate
4147c478bd9Sstevel@tonic-gate /* we must now be at the '@' */
4157c478bd9Sstevel@tonic-gate if (*dsp != '@')
4167c478bd9Sstevel@tonic-gate goto efree;
4177c478bd9Sstevel@tonic-gate
4187c478bd9Sstevel@tonic-gate /* set the type and length */
4197c478bd9Sstevel@tonic-gate DEVID_FORMTYPE(id, type);
4207c478bd9Sstevel@tonic-gate DEVID_FORMLEN(id, id_len);
4217c478bd9Sstevel@tonic-gate
4227c478bd9Sstevel@tonic-gate /* decode devid portion of string into the binary */
4237c478bd9Sstevel@tonic-gate for (i = 0, dsp = str, dp = &id->did_id[0];
4247c478bd9Sstevel@tonic-gate i < id_len; i++, dp++) {
4257c478bd9Sstevel@tonic-gate if (ascii) {
4267c478bd9Sstevel@tonic-gate if (*dsp == '_')
4277c478bd9Sstevel@tonic-gate *dp = ' ';
4287c478bd9Sstevel@tonic-gate else if (*dsp == '~')
4297c478bd9Sstevel@tonic-gate *dp = 0x00;
4307c478bd9Sstevel@tonic-gate else
4317c478bd9Sstevel@tonic-gate *dp = *dsp;
4327c478bd9Sstevel@tonic-gate dsp++;
4337c478bd9Sstevel@tonic-gate } else {
4347c478bd9Sstevel@tonic-gate c = *dsp++;
4357c478bd9Sstevel@tonic-gate if (c >= '0' && c <= '9')
4367c478bd9Sstevel@tonic-gate n = (c - '0') & 0xFF;
4377c478bd9Sstevel@tonic-gate else if (c >= 'a' && c <= 'f')
4387c478bd9Sstevel@tonic-gate n = (c - ('a' - 10)) & 0xFF;
4397c478bd9Sstevel@tonic-gate else
4407c478bd9Sstevel@tonic-gate goto efree;
4417c478bd9Sstevel@tonic-gate n <<= 4;
4427c478bd9Sstevel@tonic-gate c = *dsp++;
4437c478bd9Sstevel@tonic-gate if (c >= '0' && c <= '9')
4447c478bd9Sstevel@tonic-gate n |= (c - '0') & 0xFF;
4457c478bd9Sstevel@tonic-gate else if (c >= 'a' && c <= 'f')
4467c478bd9Sstevel@tonic-gate n |= (c - ('a' - 10)) & 0xFF;
4477c478bd9Sstevel@tonic-gate else
4487c478bd9Sstevel@tonic-gate goto efree;
4497c478bd9Sstevel@tonic-gate *dp = n;
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate
4537c478bd9Sstevel@tonic-gate /* verify result */
4547c478bd9Sstevel@tonic-gate if (DEVID_FUNC(devid_valid)((ddi_devid_t)id) != DEVID_RET_VALID)
4557c478bd9Sstevel@tonic-gate goto efree;
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate
4587c478bd9Sstevel@tonic-gate /* duplicate minor_name if we are asked to decode it */
4597c478bd9Sstevel@tonic-gate if (minor_namep && msp) {
4607c478bd9Sstevel@tonic-gate if ((*minor_namep = DEVID_MALLOC(strlen(msp) + 1)) == NULL)
4617c478bd9Sstevel@tonic-gate goto efree;
4627c478bd9Sstevel@tonic-gate (void) strcpy(*minor_namep, msp);
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate
4657c478bd9Sstevel@tonic-gate /* return pointer to binary */
4667c478bd9Sstevel@tonic-gate if (devidp)
4677c478bd9Sstevel@tonic-gate *devidp = (ddi_devid_t)id;
4687c478bd9Sstevel@tonic-gate return (DEVID_SUCCESS);
4697c478bd9Sstevel@tonic-gate
4707c478bd9Sstevel@tonic-gate efree:
4717c478bd9Sstevel@tonic-gate if ((pre_alloc == FALSE) && (id))
4727c478bd9Sstevel@tonic-gate DEVID_FREE(id, devid_len);
4737c478bd9Sstevel@tonic-gate return (DEVID_FAILURE);
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate
4767c478bd9Sstevel@tonic-gate
4777c478bd9Sstevel@tonic-gate /*
4787c478bd9Sstevel@tonic-gate * Compare two device id's in string form
4797c478bd9Sstevel@tonic-gate * -1 - id1 less than id2
4807c478bd9Sstevel@tonic-gate * 0 - equal
4817c478bd9Sstevel@tonic-gate * 1 - id1 greater than id2
4827c478bd9Sstevel@tonic-gate */
4837c478bd9Sstevel@tonic-gate int
4847c478bd9Sstevel@tonic-gate #ifdef _KERNEL
ddi_devid_str_compare(char * id1_str,char * id2_str)4857c478bd9Sstevel@tonic-gate ddi_devid_str_compare(char *id1_str, char *id2_str)
4867c478bd9Sstevel@tonic-gate #else /* !_KERNEL */
4877c478bd9Sstevel@tonic-gate devid_str_compare(char *id1_str, char *id2_str)
4887c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
4897c478bd9Sstevel@tonic-gate {
4907c478bd9Sstevel@tonic-gate int rval = DEVID_FAILURE;
4917c478bd9Sstevel@tonic-gate ddi_devid_t devid1;
4927c478bd9Sstevel@tonic-gate ddi_devid_t devid2;
4937c478bd9Sstevel@tonic-gate #ifdef _KERNEL
4947c478bd9Sstevel@tonic-gate /* kernel use static protected by lock. */
4957c478bd9Sstevel@tonic-gate static kmutex_t id_lock;
4967c478bd9Sstevel@tonic-gate static uchar_t id1[sizeof (impl_devid_t) + MAXPATHLEN];
4977c478bd9Sstevel@tonic-gate static uchar_t id2[sizeof (impl_devid_t) + MAXPATHLEN];
4987c478bd9Sstevel@tonic-gate #else /* !_KERNEL */
4997c478bd9Sstevel@tonic-gate /* userland place on stack, since malloc might fail */
5007c478bd9Sstevel@tonic-gate uchar_t id1[sizeof (impl_devid_t) + MAXPATHLEN];
5017c478bd9Sstevel@tonic-gate uchar_t id2[sizeof (impl_devid_t) + MAXPATHLEN];
5027c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
5037c478bd9Sstevel@tonic-gate
5047c478bd9Sstevel@tonic-gate #ifdef _KERNEL
5057c478bd9Sstevel@tonic-gate mutex_enter(&id_lock);
5067c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
5077c478bd9Sstevel@tonic-gate
5087c478bd9Sstevel@tonic-gate /*
5097c478bd9Sstevel@tonic-gate * encode string form of devid
5107c478bd9Sstevel@tonic-gate */
5117c478bd9Sstevel@tonic-gate if ((devid_str_decode_id(id1_str, &devid1, NULL, (impl_devid_t *)id1) ==
5127c478bd9Sstevel@tonic-gate DEVID_SUCCESS) &&
5137c478bd9Sstevel@tonic-gate (devid_str_decode_id(id2_str, &devid2, NULL, (impl_devid_t *)id2) ==
5147c478bd9Sstevel@tonic-gate DEVID_SUCCESS)) {
5157c478bd9Sstevel@tonic-gate rval = DEVID_FUNC(devid_compare)(devid1, devid2);
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate
5187c478bd9Sstevel@tonic-gate #ifdef _KERNEL
5197c478bd9Sstevel@tonic-gate mutex_exit(&id_lock);
5207c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
5217c478bd9Sstevel@tonic-gate
5227c478bd9Sstevel@tonic-gate return (rval);
5237c478bd9Sstevel@tonic-gate }
524