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/param.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
36 #include <sys/socket.h>
37
38 #include <net/ethernet.h>
39 #include <net/if.h>
40 #include <net/if_var.h>
41 #include <net/if_arp.h>
42 #include <net/if_dl.h>
43 #include <net/if_llc.h>
44 #include <net/if_media.h>
45 #include <net/if_types.h>
46
47 #include <net80211/ieee80211_var.h>
48 #include <net80211/ieee80211_radiotap.h>
49 #include <net80211/ieee80211_regdomain.h>
50 #include <net80211/ieee80211_phy.h>
51 #include <net80211/ieee80211_ratectl.h>
52
53 #include <dev/bhnd/bhnd.h>
54
55 #include <dev/bwn/if_bwnvar.h>
56
57 #include "if_bwn_phy_n_sprom.h"
58
59 #include "bhnd_nvram_map.h"
60
61
62 /* Core power NVRAM variables, indexed by D11 core unit number */
63 static const struct bwn_nphy_power_vars {
64 const char *itt2ga;
65 const char *itt5ga;
66 const char *maxp2ga;
67 const char *pa2ga;
68 const char *pa5ga;
69 } bwn_nphy_power_vars[BWN_NPHY_NUM_CORE_PWR] = {
70 #define BHND_POWER_NVAR(_idx) \
71 { BHND_NVAR_ITT2GA ## _idx, BHND_NVAR_ITT5GA ## _idx, \
72 BHND_NVAR_MAXP2GA ## _idx, BHND_NVAR_PA2GA ## _idx, \
73 BHND_NVAR_PA5GA ## _idx }
74 BHND_POWER_NVAR(0),
75 BHND_POWER_NVAR(1),
76 BHND_POWER_NVAR(2),
77 BHND_POWER_NVAR(3)
78 #undef BHND_POWER_NVAR
79 };
80
81 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)82 bwn_nphy_get_core_power_info_r11(struct bwn_softc *sc,
83 const struct bwn_nphy_power_vars *v, struct bwn_phy_n_core_pwr_info *c)
84 {
85 int16_t pa5ga[12];
86 int error;
87
88 /* BHND_NVAR_PA2GA[core] */
89 error = bhnd_nvram_getvar_array(sc->sc_dev, v->pa2ga, c->pa_2g,
90 sizeof(c->pa_2g), BHND_NVRAM_TYPE_INT16);
91 if (error)
92 return (error);
93
94 /*
95 * BHND_NVAR_PA5GA
96 *
97 * The NVRAM variable is defined as a single pa5ga[12] array; we have
98 * to split this into pa_5gl[4], pa_5g[4], and pa_5gh[4] for use
99 * by bwn(4);
100 */
101 _Static_assert(nitems(pa5ga) == nitems(c->pa_5g) + nitems(c->pa_5gh) +
102 nitems(c->pa_5gl), "cannot split pa5ga into pa_5gl/pa_5g/pa_5gh");
103
104 error = bhnd_nvram_getvar_array(sc->sc_dev, v->pa5ga, pa5ga,
105 sizeof(pa5ga), BHND_NVRAM_TYPE_INT16);
106 if (error)
107 return (error);
108
109 memcpy(c->pa_5gl, &pa5ga[0], sizeof(c->pa_5gl));
110 memcpy(c->pa_5g, &pa5ga[4], sizeof(c->pa_5g));
111 memcpy(c->pa_5gh, &pa5ga[8], sizeof(c->pa_5gh));
112 return (0);
113 }
114
115 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)116 bwn_nphy_get_core_power_info_r4_r10(struct bwn_softc *sc,
117 const struct bwn_nphy_power_vars *v, struct bwn_phy_n_core_pwr_info *c)
118 {
119 int error;
120
121 /* BHND_NVAR_ITT2GA[core] */
122 error = bhnd_nvram_getvar_uint8(sc->sc_dev, v->itt2ga, &c->itssi_2g);
123 if (error)
124 return (error);
125
126 /* BHND_NVAR_ITT5GA[core] */
127 error = bhnd_nvram_getvar_uint8(sc->sc_dev, v->itt5ga, &c->itssi_5g);
128 if (error)
129 return (error);
130
131 return (0);
132 }
133
134 /*
135 * siba_sprom_get_core_power_info()
136 *
137 * Referenced by:
138 * bwn_nphy_tx_power_ctl_setup()
139 * bwn_ppr_load_max_from_sprom()
140 */
141 int
bwn_nphy_get_core_power_info(struct bwn_mac * mac,int core,struct bwn_phy_n_core_pwr_info * c)142 bwn_nphy_get_core_power_info(struct bwn_mac *mac, int core,
143 struct bwn_phy_n_core_pwr_info *c)
144 {
145 struct bwn_softc *sc;
146 const struct bwn_nphy_power_vars *v;
147 uint8_t sromrev;
148 int error;
149
150 sc = mac->mac_sc;
151
152 if (core < 0 || core >= nitems(bwn_nphy_power_vars))
153 return (EINVAL);
154
155 sromrev = sc->sc_board_info.board_srom_rev;
156 if (sromrev < 4)
157 return (ENXIO);
158
159 v = &bwn_nphy_power_vars[core];
160
161 /* Any power variables not found in NVRAM (or returning a
162 * shorter array for a particular NVRAM revision) should be zero
163 * initialized */
164 memset(c, 0x0, sizeof(*c));
165
166 /* Populate SPROM revision-independent values */
167 error = bhnd_nvram_getvar_uint8(sc->sc_dev, v->maxp2ga, &c->maxpwr_2g);
168 if (error)
169 return (error);
170
171 /* Populate SPROM revision-specific values */
172 if (sromrev >= 4 && sromrev <= 10)
173 return (bwn_nphy_get_core_power_info_r4_r10(sc, v, c));
174 else
175 return (bwn_nphy_get_core_power_info_r11(sc, v, c));
176 }
177