1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2016 Landon Fuller <landonf@FreeBSD.org>
5 * Copyright (c) 2017 The FreeBSD Foundation
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer,
13 * without modification.
14 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
15 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
16 * redistribution must be conditioned upon including a substantially
17 * similar Disclaimer requirement for further binary redistribution.
18 *
19 * NO WARRANTY
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
23 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
24 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
25 * OR 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
28 * IN 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
30 * THE POSSIBILITY OF SUCH DAMAGES.
31 */
32
33 #include <sys/cdefs.h>
34 #include <sys/param.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
37 #include <sys/socket.h>
38
39 #include <net/ethernet.h>
40 #include <net/if.h>
41 #include <net/if_var.h>
42 #include <net/if_arp.h>
43 #include <net/if_dl.h>
44 #include <net/if_llc.h>
45 #include <net/if_media.h>
46 #include <net/if_types.h>
47
48 #include <net80211/ieee80211_var.h>
49 #include <net80211/ieee80211_radiotap.h>
50 #include <net80211/ieee80211_regdomain.h>
51 #include <net80211/ieee80211_phy.h>
52 #include <net80211/ieee80211_ratectl.h>
53
54 #include <dev/bhnd/bhnd.h>
55
56 #include <dev/bwn/if_bwnvar.h>
57
58 #include "if_bwn_phy_n_sprom.h"
59
60 #include "bhnd_nvram_map.h"
61
62
63 /* Core power NVRAM variables, indexed by D11 core unit number */
64 static const struct bwn_nphy_power_vars {
65 const char *itt2ga;
66 const char *itt5ga;
67 const char *maxp2ga;
68 const char *pa2ga;
69 const char *pa5ga;
70 } bwn_nphy_power_vars[BWN_NPHY_NUM_CORE_PWR] = {
71 #define BHND_POWER_NVAR(_idx) \
72 { BHND_NVAR_ITT2GA ## _idx, BHND_NVAR_ITT5GA ## _idx, \
73 BHND_NVAR_MAXP2GA ## _idx, BHND_NVAR_PA2GA ## _idx, \
74 BHND_NVAR_PA5GA ## _idx }
75 BHND_POWER_NVAR(0),
76 BHND_POWER_NVAR(1),
77 BHND_POWER_NVAR(2),
78 BHND_POWER_NVAR(3)
79 #undef BHND_POWER_NVAR
80 };
81
82 static int
bwn_nphy_get_core_power_info_r11(struct bwn_softc * sc,const struct bwn_nphy_power_vars * v,struct bwn_phy_n_core_pwr_info * c)83 bwn_nphy_get_core_power_info_r11(struct bwn_softc *sc,
84 const struct bwn_nphy_power_vars *v, struct bwn_phy_n_core_pwr_info *c)
85 {
86 int16_t pa5ga[12];
87 int error;
88
89 /* BHND_NVAR_PA2GA[core] */
90 error = bhnd_nvram_getvar_array(sc->sc_dev, v->pa2ga, c->pa_2g,
91 sizeof(c->pa_2g), BHND_NVRAM_TYPE_INT16);
92 if (error)
93 return (error);
94
95 /*
96 * BHND_NVAR_PA5GA
97 *
98 * The NVRAM variable is defined as a single pa5ga[12] array; we have
99 * to split this into pa_5gl[4], pa_5g[4], and pa_5gh[4] for use
100 * by bwn(4);
101 */
102 _Static_assert(nitems(pa5ga) == nitems(c->pa_5g) + nitems(c->pa_5gh) +
103 nitems(c->pa_5gl), "cannot split pa5ga into pa_5gl/pa_5g/pa_5gh");
104
105 error = bhnd_nvram_getvar_array(sc->sc_dev, v->pa5ga, pa5ga,
106 sizeof(pa5ga), BHND_NVRAM_TYPE_INT16);
107 if (error)
108 return (error);
109
110 memcpy(c->pa_5gl, &pa5ga[0], sizeof(c->pa_5gl));
111 memcpy(c->pa_5g, &pa5ga[4], sizeof(c->pa_5g));
112 memcpy(c->pa_5gh, &pa5ga[8], sizeof(c->pa_5gh));
113 return (0);
114 }
115
116 static int
bwn_nphy_get_core_power_info_r4_r10(struct bwn_softc * sc,const struct bwn_nphy_power_vars * v,struct bwn_phy_n_core_pwr_info * c)117 bwn_nphy_get_core_power_info_r4_r10(struct bwn_softc *sc,
118 const struct bwn_nphy_power_vars *v, struct bwn_phy_n_core_pwr_info *c)
119 {
120 int error;
121
122 /* BHND_NVAR_ITT2GA[core] */
123 error = bhnd_nvram_getvar_uint8(sc->sc_dev, v->itt2ga, &c->itssi_2g);
124 if (error)
125 return (error);
126
127 /* BHND_NVAR_ITT5GA[core] */
128 error = bhnd_nvram_getvar_uint8(sc->sc_dev, v->itt5ga, &c->itssi_5g);
129 if (error)
130 return (error);
131
132 return (0);
133 }
134
135 /*
136 * siba_sprom_get_core_power_info()
137 *
138 * Referenced by:
139 * bwn_nphy_tx_power_ctl_setup()
140 * bwn_ppr_load_max_from_sprom()
141 */
142 int
bwn_nphy_get_core_power_info(struct bwn_mac * mac,int core,struct bwn_phy_n_core_pwr_info * c)143 bwn_nphy_get_core_power_info(struct bwn_mac *mac, int core,
144 struct bwn_phy_n_core_pwr_info *c)
145 {
146 struct bwn_softc *sc;
147 const struct bwn_nphy_power_vars *v;
148 uint8_t sromrev;
149 int error;
150
151 sc = mac->mac_sc;
152
153 if (core < 0 || core >= nitems(bwn_nphy_power_vars))
154 return (EINVAL);
155
156 sromrev = sc->sc_board_info.board_srom_rev;
157 if (sromrev < 4)
158 return (ENXIO);
159
160 v = &bwn_nphy_power_vars[core];
161
162 /* Any power variables not found in NVRAM (or returning a
163 * shorter array for a particular NVRAM revision) should be zero
164 * initialized */
165 memset(c, 0x0, sizeof(*c));
166
167 /* Populate SPROM revision-independent values */
168 error = bhnd_nvram_getvar_uint8(sc->sc_dev, v->maxp2ga, &c->maxpwr_2g);
169 if (error)
170 return (error);
171
172 /* Populate SPROM revision-specific values */
173 if (sromrev >= 4 && sromrev <= 10)
174 return (bwn_nphy_get_core_power_info_r4_r10(sc, v, c));
175 else
176 return (bwn_nphy_get_core_power_info_r11(sc, v, c));
177 }
178