1 /* $NetBSD: ofw_network_subr.c,v 1.10 2021/01/27 02:31:35 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2021 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: ofw_network_subr.c,v 1.10 2021/01/27 02:31:35 thorpej Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/socket.h>
40 #include <sys/device.h>
41
42 #include <net/if.h>
43 #include <net/if_media.h>
44
45 #include <dev/ofw/openfirm.h>
46
47 static const struct device_compatible_entry media_compat[] = {
48 { .compat = "ethernet,10,rj45,half",
49 .value = IFM_ETHER | IFM_10_T },
50
51 { .compat = "ethernet,10,rj45,full",
52 .value = IFM_ETHER | IFM_10_T | IFM_FDX },
53
54 { .compat = "ethernet,10,aui,half",
55 .value = IFM_ETHER | IFM_10_5 },
56
57 { .compat = "ethernet,10,bnc,half",
58 .value = IFM_ETHER | IFM_10_2 },
59
60 { .compat = "ethernet,100,rj45,half",
61 .value = IFM_ETHER | IFM_100_TX },
62
63 { .compat = "ethernet,100,rj45,full",
64 .value = IFM_ETHER | IFM_100_TX | IFM_FDX },
65
66 DEVICE_COMPAT_EOL
67 };
68
69 /*
70 * int of_network_decode_media(phandle, nmediap, defmediap)
71 *
72 * This routine decodes the OFW properties `supported-network-types'
73 * and `chosen-network-type'.
74 *
75 * Arguments:
76 * phandle OFW phandle of device whos network media properties
77 * are to be decoded.
78 * nmediap Pointer to an integer which will be initialized
79 * with the number of returned media words.
80 * defmediap Pointer to an integer which will be initialized
81 * with the default network media.
82 *
83 * Return Values:
84 * An array of integers, allocated with malloc(), containing the
85 * decoded media values. The number of elements in the array will
86 * be stored in the location pointed to by the `nmediap' argument.
87 * The default media will be stored in the location pointed to by
88 * the `defmediap' argument.
89 *
90 * Side Effects:
91 * None.
92 */
93 int *
of_network_decode_media(int phandle,int * nmediap,int * defmediap)94 of_network_decode_media(int phandle, int *nmediap, int *defmediap)
95 {
96 const struct device_compatible_entry *dce;
97 int nmedia, len, *rv = NULL;
98 char *sl = NULL;
99 const char *cp;
100 size_t cursor;
101 unsigned int count;
102
103 len = OF_getproplen(phandle, "supported-network-types");
104 if (len <= 0)
105 return (NULL);
106
107 sl = malloc(len, M_TEMP, M_WAITOK);
108
109 /* `supported-network-types' should not change. */
110 if (OF_getprop(phandle, "supported-network-types", sl, len) != len)
111 goto bad;
112
113 count = strlist_count(sl, len);
114
115 if (count == 0)
116 goto bad;
117
118 /* Allocate the return value array. */
119 rv = malloc(count * sizeof(int), M_DEVBUF, M_WAITOK);
120
121 /* Parse each media string. */
122 for (nmedia = 0, cursor = 0;
123 (cp = strlist_next(sl, len, &cursor)) != NULL; ) {
124 dce = device_compatible_lookup(&cp, 1, media_compat);
125 if (dce != NULL) {
126 rv[nmedia++] = (int)dce->value;
127 }
128 }
129 /* Sanity... */
130 if (nmedia == 0)
131 goto bad;
132
133 free(sl, M_TEMP);
134 sl = NULL;
135
136 /*
137 * We now have the `supported-media-types' property decoded.
138 * Next step is to decode the `chosen-media-type' property,
139 * if it exists.
140 */
141 *defmediap = -1;
142 len = OF_getproplen(phandle, "chosen-network-type");
143 if (len <= 0) {
144 /* Property does not exist. */
145 *defmediap = -1;
146 goto done;
147 }
148
149 sl = malloc(len, M_TEMP, M_WAITOK);
150 if (OF_getprop(phandle, "chosen-network-type", sl, len) != len) {
151 /* Something went wrong... */
152 goto done;
153 }
154
155 cp = sl;
156 dce = device_compatible_lookup(&cp, 1, media_compat);
157 if (dce != NULL) {
158 *defmediap = (int)dce->value;
159 }
160
161 done:
162 if (sl != NULL)
163 free(sl, M_TEMP);
164 *nmediap = nmedia;
165 return (rv);
166
167 bad:
168 if (rv != NULL)
169 free(rv, M_DEVBUF);
170 if (sl != NULL)
171 free(sl, M_TEMP);
172 return (NULL);
173 }
174