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