1*01632a17Sthorpej /* $NetBSD: ofw_network_subr.c,v 1.10 2021/01/27 02:31:35 thorpej Exp $ */
2daf40e1bSthorpej
3daf40e1bSthorpej /*-
47bd34e60Sthorpej * Copyright (c) 1998, 2021 The NetBSD Foundation, Inc.
5daf40e1bSthorpej * All rights reserved.
6daf40e1bSthorpej *
7daf40e1bSthorpej * This code is derived from software contributed to The NetBSD Foundation
8daf40e1bSthorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9daf40e1bSthorpej * NASA Ames Research Center.
10daf40e1bSthorpej *
11daf40e1bSthorpej * Redistribution and use in source and binary forms, with or without
12daf40e1bSthorpej * modification, are permitted provided that the following conditions
13daf40e1bSthorpej * are met:
14daf40e1bSthorpej * 1. Redistributions of source code must retain the above copyright
15daf40e1bSthorpej * notice, this list of conditions and the following disclaimer.
16daf40e1bSthorpej * 2. Redistributions in binary form must reproduce the above copyright
17daf40e1bSthorpej * notice, this list of conditions and the following disclaimer in the
18daf40e1bSthorpej * documentation and/or other materials provided with the distribution.
19daf40e1bSthorpej *
20daf40e1bSthorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21daf40e1bSthorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22daf40e1bSthorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23daf40e1bSthorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24daf40e1bSthorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25daf40e1bSthorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26daf40e1bSthorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27daf40e1bSthorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28daf40e1bSthorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29daf40e1bSthorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30daf40e1bSthorpej * POSSIBILITY OF SUCH DAMAGE.
31daf40e1bSthorpej */
32daf40e1bSthorpej
33ab5d9d2bSlukem #include <sys/cdefs.h>
34*01632a17Sthorpej __KERNEL_RCSID(0, "$NetBSD: ofw_network_subr.c,v 1.10 2021/01/27 02:31:35 thorpej Exp $");
35ab5d9d2bSlukem
36daf40e1bSthorpej #include <sys/param.h>
37daf40e1bSthorpej #include <sys/systm.h>
38daf40e1bSthorpej #include <sys/malloc.h>
39daf40e1bSthorpej #include <sys/socket.h>
407bd34e60Sthorpej #include <sys/device.h>
41daf40e1bSthorpej
42daf40e1bSthorpej #include <net/if.h>
43daf40e1bSthorpej #include <net/if_media.h>
44daf40e1bSthorpej
45daf40e1bSthorpej #include <dev/ofw/openfirm.h>
46daf40e1bSthorpej
477bd34e60Sthorpej static const struct device_compatible_entry media_compat[] = {
487bd34e60Sthorpej { .compat = "ethernet,10,rj45,half",
497bd34e60Sthorpej .value = IFM_ETHER | IFM_10_T },
50daf40e1bSthorpej
517bd34e60Sthorpej { .compat = "ethernet,10,rj45,full",
527bd34e60Sthorpej .value = IFM_ETHER | IFM_10_T | IFM_FDX },
537bd34e60Sthorpej
547bd34e60Sthorpej { .compat = "ethernet,10,aui,half",
557bd34e60Sthorpej .value = IFM_ETHER | IFM_10_5 },
567bd34e60Sthorpej
577bd34e60Sthorpej { .compat = "ethernet,10,bnc,half",
587bd34e60Sthorpej .value = IFM_ETHER | IFM_10_2 },
597bd34e60Sthorpej
607bd34e60Sthorpej { .compat = "ethernet,100,rj45,half",
617bd34e60Sthorpej .value = IFM_ETHER | IFM_100_TX },
627bd34e60Sthorpej
637bd34e60Sthorpej { .compat = "ethernet,100,rj45,full",
647bd34e60Sthorpej .value = IFM_ETHER | IFM_100_TX | IFM_FDX },
657bd34e60Sthorpej
66*01632a17Sthorpej DEVICE_COMPAT_EOL
67daf40e1bSthorpej };
68daf40e1bSthorpej
69daf40e1bSthorpej /*
70daf40e1bSthorpej * int of_network_decode_media(phandle, nmediap, defmediap)
71daf40e1bSthorpej *
72daf40e1bSthorpej * This routine decodes the OFW properties `supported-network-types'
73daf40e1bSthorpej * and `chosen-network-type'.
74daf40e1bSthorpej *
75daf40e1bSthorpej * Arguments:
76daf40e1bSthorpej * phandle OFW phandle of device whos network media properties
77daf40e1bSthorpej * are to be decoded.
78daf40e1bSthorpej * nmediap Pointer to an integer which will be initialized
79daf40e1bSthorpej * with the number of returned media words.
80daf40e1bSthorpej * defmediap Pointer to an integer which will be initialized
81daf40e1bSthorpej * with the default network media.
82daf40e1bSthorpej *
83daf40e1bSthorpej * Return Values:
84daf40e1bSthorpej * An array of integers, allocated with malloc(), containing the
85daf40e1bSthorpej * decoded media values. The number of elements in the array will
86daf40e1bSthorpej * be stored in the location pointed to by the `nmediap' argument.
87daf40e1bSthorpej * The default media will be stored in the location pointed to by
88daf40e1bSthorpej * the `defmediap' argument.
89daf40e1bSthorpej *
90daf40e1bSthorpej * Side Effects:
91daf40e1bSthorpej * None.
92daf40e1bSthorpej */
93daf40e1bSthorpej int *
of_network_decode_media(int phandle,int * nmediap,int * defmediap)9482357f6dSdsl of_network_decode_media(int phandle, int *nmediap, int *defmediap)
95daf40e1bSthorpej {
967bd34e60Sthorpej const struct device_compatible_entry *dce;
977bd34e60Sthorpej int nmedia, len, *rv = NULL;
987bd34e60Sthorpej char *sl = NULL;
997bd34e60Sthorpej const char *cp;
1007bd34e60Sthorpej size_t cursor;
1017bd34e60Sthorpej unsigned int count;
102daf40e1bSthorpej
103daf40e1bSthorpej len = OF_getproplen(phandle, "supported-network-types");
104daf40e1bSthorpej if (len <= 0)
105daf40e1bSthorpej return (NULL);
106daf40e1bSthorpej
1077bd34e60Sthorpej sl = malloc(len, M_TEMP, M_WAITOK);
108daf40e1bSthorpej
109daf40e1bSthorpej /* `supported-network-types' should not change. */
1107bd34e60Sthorpej if (OF_getprop(phandle, "supported-network-types", sl, len) != len)
111daf40e1bSthorpej goto bad;
112daf40e1bSthorpej
1137bd34e60Sthorpej count = strlist_count(sl, len);
114daf40e1bSthorpej
115daf40e1bSthorpej if (count == 0)
116daf40e1bSthorpej goto bad;
117daf40e1bSthorpej
118daf40e1bSthorpej /* Allocate the return value array. */
119daf40e1bSthorpej rv = malloc(count * sizeof(int), M_DEVBUF, M_WAITOK);
120daf40e1bSthorpej
1217bd34e60Sthorpej /* Parse each media string. */
1227bd34e60Sthorpej for (nmedia = 0, cursor = 0;
1237bd34e60Sthorpej (cp = strlist_next(sl, len, &cursor)) != NULL; ) {
1247bd34e60Sthorpej dce = device_compatible_lookup(&cp, 1, media_compat);
1257bd34e60Sthorpej if (dce != NULL) {
1267bd34e60Sthorpej rv[nmedia++] = (int)dce->value;
127daf40e1bSthorpej }
128daf40e1bSthorpej }
129daf40e1bSthorpej /* Sanity... */
1307bd34e60Sthorpej if (nmedia == 0)
131daf40e1bSthorpej goto bad;
132daf40e1bSthorpej
1337bd34e60Sthorpej free(sl, M_TEMP);
1347bd34e60Sthorpej sl = NULL;
1357bd34e60Sthorpej
136daf40e1bSthorpej /*
137daf40e1bSthorpej * We now have the `supported-media-types' property decoded.
138daf40e1bSthorpej * Next step is to decode the `chosen-media-type' property,
139daf40e1bSthorpej * if it exists.
140daf40e1bSthorpej */
1417bd34e60Sthorpej *defmediap = -1;
142daf40e1bSthorpej len = OF_getproplen(phandle, "chosen-network-type");
143daf40e1bSthorpej if (len <= 0) {
144daf40e1bSthorpej /* Property does not exist. */
145daf40e1bSthorpej *defmediap = -1;
146daf40e1bSthorpej goto done;
147daf40e1bSthorpej }
148daf40e1bSthorpej
1497bd34e60Sthorpej sl = malloc(len, M_TEMP, M_WAITOK);
1507bd34e60Sthorpej if (OF_getprop(phandle, "chosen-network-type", sl, len) != len) {
151daf40e1bSthorpej /* Something went wrong... */
152daf40e1bSthorpej goto done;
153daf40e1bSthorpej }
154daf40e1bSthorpej
1557bd34e60Sthorpej cp = sl;
1567bd34e60Sthorpej dce = device_compatible_lookup(&cp, 1, media_compat);
1577bd34e60Sthorpej if (dce != NULL) {
1587bd34e60Sthorpej *defmediap = (int)dce->value;
1597bd34e60Sthorpej }
160daf40e1bSthorpej
161daf40e1bSthorpej done:
1627bd34e60Sthorpej if (sl != NULL)
1637bd34e60Sthorpej free(sl, M_TEMP);
1647bd34e60Sthorpej *nmediap = nmedia;
165daf40e1bSthorpej return (rv);
166daf40e1bSthorpej
167daf40e1bSthorpej bad:
168daf40e1bSthorpej if (rv != NULL)
169daf40e1bSthorpej free(rv, M_DEVBUF);
1707bd34e60Sthorpej if (sl != NULL)
1717bd34e60Sthorpej free(sl, M_TEMP);
172daf40e1bSthorpej return (NULL);
173daf40e1bSthorpej }
174