1/*-
2 * Copyright (c) 2013 Adrian Chadd <adrian@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer,
10 *    without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 *    redistribution must be conditioned upon including a substantially
14 *    similar Disclaimer requirement for further binary redistribution.
15 *
16 * NO WARRANTY
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
28 *
29 * $FreeBSD: stable/11/sys/dev/ath/if_ath_btcoex.c 332303 2018-04-08 20:50:16Z emaste $
30 */
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: stable/11/sys/dev/ath/if_ath_btcoex.c 332303 2018-04-08 20:50:16Z emaste $");
33
34/*
35 * This implements some very basic bluetooth coexistence methods for
36 * the ath(4) hardware.
37 */
38#include "opt_ath.h"
39#include "opt_inet.h"
40#include "opt_wlan.h"
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/sysctl.h>
45#include <sys/kernel.h>
46#include <sys/lock.h>
47#include <sys/malloc.h>
48#include <sys/mutex.h>
49#include <sys/errno.h>
50#include <machine/bus.h>
51#include <machine/resource.h>
52
53#include <sys/bus.h>
54
55#include <sys/socket.h>
56
57#include <net/if.h>
58#include <net/if_var.h>
59#include <net/if_media.h>
60#include <net/if_arp.h>
61#include <net/ethernet.h>		/* XXX for ether_sprintf */
62
63#include <net80211/ieee80211_var.h>
64
65#include <net/bpf.h>
66
67#ifdef INET
68#include <netinet/in.h>
69#include <netinet/if_ether.h>
70#endif
71
72#include <dev/ath/if_athvar.h>
73#include <dev/ath/if_ath_btcoex.h>
74#include <dev/ath/if_ath_btcoex_mci.h>
75
76MALLOC_DECLARE(M_ATHDEV);
77
78/*
79 * Initial AR9285 / (WB195) bluetooth coexistence settings,
80 * just for experimentation.
81 *
82 * Return 0 for OK; errno for error.
83 *
84 * XXX TODO: There needs to be a PCIe workaround to disable ASPM if
85 * bluetooth coexistence is enabled.
86 */
87static int
88ath_btcoex_cfg_wb195(struct ath_softc *sc)
89{
90	HAL_BT_COEX_INFO btinfo;
91	HAL_BT_COEX_CONFIG btconfig;
92	struct ath_hal *ah = sc->sc_ah;
93
94	if (! ath_hal_btcoex_supported(ah))
95		return (EINVAL);
96
97	bzero(&btinfo, sizeof(btinfo));
98	bzero(&btconfig, sizeof(btconfig));
99
100	device_printf(sc->sc_dev, "Enabling WB195 BTCOEX\n");
101
102	btinfo.bt_module = HAL_BT_MODULE_JANUS;
103	btinfo.bt_coex_config = HAL_BT_COEX_CFG_3WIRE;
104	/*
105	 * These are the three GPIO pins hooked up between the AR9285 and
106	 * the AR3011.
107	 */
108	btinfo.bt_gpio_bt_active = 6;
109	btinfo.bt_gpio_bt_priority = 7;
110	btinfo.bt_gpio_wlan_active = 5;
111	btinfo.bt_active_polarity = 1;	/* XXX not used */
112	btinfo.bt_single_ant = 1;	/* 1 antenna on ar9285 ? */
113	btinfo.bt_isolation = 0;	/* in dB, not used */
114
115	ath_hal_btcoex_set_info(ah, &btinfo);
116
117	btconfig.bt_time_extend = 0;
118	btconfig.bt_txstate_extend = 1;	/* true */
119	btconfig.bt_txframe_extend = 1;	/* true */
120	btconfig.bt_mode = HAL_BT_COEX_MODE_SLOTTED;
121	btconfig.bt_quiet_collision = 1;	/* true */
122	btconfig.bt_rxclear_polarity = 1;	/* true */
123	btconfig.bt_priority_time = 2;
124	btconfig.bt_first_slot_time = 5;
125	btconfig.bt_hold_rxclear = 1;	/* true */
126
127	ath_hal_btcoex_set_config(ah, &btconfig);
128
129	/*
130	 * Enable antenna diversity.
131	 */
132	ath_hal_btcoex_set_parameter(ah, HAL_BT_COEX_ANTENNA_DIVERSITY, 1);
133
134	return (0);
135}
136
137/*
138 * Initial AR9485 / (WB225) bluetooth coexistence settings,
139 * just for experimentation.
140 *
141 * Return 0 for OK; errno for error.
142 */
143static int
144ath_btcoex_cfg_wb225(struct ath_softc *sc)
145{
146	HAL_BT_COEX_INFO btinfo;
147	HAL_BT_COEX_CONFIG btconfig;
148	struct ath_hal *ah = sc->sc_ah;
149
150	if (! ath_hal_btcoex_supported(ah))
151		return (EINVAL);
152
153	bzero(&btinfo, sizeof(btinfo));
154	bzero(&btconfig, sizeof(btconfig));
155
156	device_printf(sc->sc_dev, "Enabling WB225 BTCOEX\n");
157
158	btinfo.bt_module = HAL_BT_MODULE_JANUS;	/* XXX not used? */
159	btinfo.bt_coex_config = HAL_BT_COEX_CFG_3WIRE;
160	/*
161	 * These are the three GPIO pins hooked up between the AR9485 and
162	 * the bluetooth module.
163	 */
164	btinfo.bt_gpio_bt_active = 4;
165	btinfo.bt_gpio_bt_priority = 8;
166	btinfo.bt_gpio_wlan_active = 5;
167
168	btinfo.bt_active_polarity = 1;	/* XXX not used */
169	btinfo.bt_single_ant = 1;	/* 1 antenna on ar9285 ? */
170	btinfo.bt_isolation = 0;	/* in dB, not used */
171
172	ath_hal_btcoex_set_info(ah, &btinfo);
173
174	btconfig.bt_time_extend = 0;
175	btconfig.bt_txstate_extend = 1;	/* true */
176	btconfig.bt_txframe_extend = 1;	/* true */
177	btconfig.bt_mode = HAL_BT_COEX_MODE_SLOTTED;
178	btconfig.bt_quiet_collision = 1;	/* true */
179	btconfig.bt_rxclear_polarity = 1;	/* true */
180	btconfig.bt_priority_time = 2;
181	btconfig.bt_first_slot_time = 5;
182	btconfig.bt_hold_rxclear = 1;	/* true */
183
184	ath_hal_btcoex_set_config(ah, &btconfig);
185
186	/*
187	 * Enable antenna diversity.
188	 */
189	ath_hal_btcoex_set_parameter(ah, HAL_BT_COEX_ANTENNA_DIVERSITY, 1);
190
191	return (0);
192}
193
194static int
195ath_btcoex_cfg_mci(struct ath_softc *sc, uint32_t mci_cfg, int do_btdiv)
196{
197	HAL_BT_COEX_INFO btinfo;
198	HAL_BT_COEX_CONFIG btconfig;
199	struct ath_hal *ah = sc->sc_ah;
200
201	if (! ath_hal_btcoex_supported(ah))
202		return (EINVAL);
203
204	bzero(&btinfo, sizeof(btinfo));
205	bzero(&btconfig, sizeof(btconfig));
206
207	sc->sc_ah->ah_config.ath_hal_mci_config = mci_cfg;
208
209	if (ath_btcoex_mci_attach(sc) != 0) {
210		device_printf(sc->sc_dev, "Failed to setup btcoex\n");
211		return (EINVAL);
212	}
213
214	btinfo.bt_module = HAL_BT_MODULE_JANUS;	/* XXX not used? */
215	btinfo.bt_coex_config = HAL_BT_COEX_CFG_MCI;
216
217	/*
218	 * MCI uses a completely different interface to speak
219	 * to the bluetooth module - it's a command based
220	 * thing over a serial line, rather than
221	 * state pins to/from the bluetooth module.
222	 *
223	 * So, the GPIO configuration, polarity, etc
224	 * doesn't matter on MCI devices; it's just
225	 * completely ignored by the HAL.
226	 */
227	btinfo.bt_gpio_bt_active = 4;
228	btinfo.bt_gpio_bt_priority = 8;
229	btinfo.bt_gpio_wlan_active = 5;
230
231	btinfo.bt_active_polarity = 1;	/* XXX not used */
232	btinfo.bt_single_ant = 0;	/* 2 antenna on WB335 */
233	btinfo.bt_isolation = 0;	/* in dB, not used */
234
235	ath_hal_btcoex_set_info(ah, &btinfo);
236
237	btconfig.bt_time_extend = 0;
238	btconfig.bt_txstate_extend = 1;	/* true */
239	btconfig.bt_txframe_extend = 1;	/* true */
240	btconfig.bt_mode = HAL_BT_COEX_MODE_SLOTTED;
241	btconfig.bt_quiet_collision = 1;	/* true */
242	btconfig.bt_rxclear_polarity = 1;	/* true */
243	btconfig.bt_priority_time = 2;
244	btconfig.bt_first_slot_time = 5;
245	btconfig.bt_hold_rxclear = 1;	/* true */
246
247	ath_hal_btcoex_set_config(ah, &btconfig);
248
249	/* Enable */
250	ath_hal_btcoex_enable(sc->sc_ah);
251
252	/* Stomp */
253	ath_hal_btcoex_set_weights(ah, HAL_BT_COEX_STOMP_NONE);
254
255	/*
256	 * Enable antenna diversity.
257	 */
258	ath_hal_btcoex_set_parameter(ah, HAL_BT_COEX_ANTENNA_DIVERSITY,
259	    do_btdiv);
260
261	return (0);
262}
263
264/*
265 * Initial AR9462 / (WB222) bluetooth coexistence settings.
266 *
267 * Return 0 for OK; errno for error.
268 */
269static int
270ath_btcoex_cfg_wb222(struct ath_softc *sc)
271{
272
273	device_printf(sc->sc_dev, "Enabling WB222 BTCOEX\n");
274	/* XXX from ath9k */
275	return (ath_btcoex_cfg_mci(sc, 0x2201, 1));
276}
277
278/*
279 * Initial QCA9565 / (WB335B) bluetooth coexistence settings.
280 *
281 * Return 0 for OK; errno for error.
282 */
283static int
284ath_btcoex_cfg_wb335b(struct ath_softc *sc)
285{
286	uint32_t flags;
287	int do_btdiv = 0;
288
289	/* ath9k default */
290	flags = 0xa4c1;
291
292	/* 1-ant and 2-ant AR9565 */
293	/*
294	 * XXX TODO: ensure these actually make it down to the
295	 * HAL correctly!
296	 */
297	if (sc->sc_pci_devinfo & ATH_PCI_AR9565_1ANT) {
298		flags &= ~ATH_MCI_CONFIG_ANT_ARCH;
299		flags |= ATH_MCI_ANT_ARCH_1_ANT_PA_LNA_SHARED <<
300		    ATH_MCI_CONFIG_ANT_ARCH_S;
301	} else if (sc->sc_pci_devinfo & ATH_PCI_AR9565_2ANT) {
302		flags &= ~ATH_MCI_CONFIG_ANT_ARCH;
303		flags |= ATH_MCI_ANT_ARCH_2_ANT_PA_LNA_NON_SHARED <<
304		    ATH_MCI_CONFIG_ANT_ARCH_S;
305	}
306
307	if (sc->sc_pci_devinfo & ATH_PCI_BT_ANT_DIV) {
308		do_btdiv = 1;
309	}
310
311	device_printf(sc->sc_dev, "Enabling WB335 BTCOEX\n");
312	/* XXX from ath9k */
313	return (ath_btcoex_cfg_mci(sc, flags, do_btdiv));
314}
315
316#if 0
317/*
318 * When using bluetooth coexistence, ASPM needs to be disabled
319 * otherwise the sleeping interferes with the bluetooth (USB)
320 * operation and the MAC sleep/wakeup hardware.
321 *
322 * The PCIe powersave routine also needs to not be called
323 * by the driver during suspend/resume, else things will get
324 * a little odd.  Check Linux ath9k for more details.
325 */
326static int
327ath_btcoex_aspm_wb195(struct ath_softc *sc)
328{
329
330	/* XXX TODO: clear device ASPM L0S and L1 */
331	/* XXX TODO: clear _parent_ ASPM L0S and L1 */
332}
333#endif
334
335/*
336 * Methods which are required
337 */
338
339/*
340 * Attach btcoex to the given interface
341 */
342int
343ath_btcoex_attach(struct ath_softc *sc)
344{
345	int ret;
346	struct ath_hal *ah = sc->sc_ah;
347	const char *profname;
348
349	/*
350	 * No chipset bluetooth coexistence? Then do nothing.
351	 */
352	if (! ath_hal_btcoex_supported(ah))
353		return (0);
354
355	/*
356	 * Look at the hints to determine which bluetooth
357	 * profile to configure.
358	 */
359	ret = resource_string_value(device_get_name(sc->sc_dev),
360	    device_get_unit(sc->sc_dev),
361	    "btcoex_profile",
362	    &profname);
363	if (ret != 0) {
364		/* nothing to do */
365		return (0);
366	}
367
368	if (strncmp(profname, "wb195", 5) == 0) {
369		ret = ath_btcoex_cfg_wb195(sc);
370	} else if (strncmp(profname, "wb222", 5) == 0) {
371		ret = ath_btcoex_cfg_wb222(sc);
372	} else if (strncmp(profname, "wb225", 5) == 0) {
373		ret = ath_btcoex_cfg_wb225(sc);
374	} else if (strncmp(profname, "wb335", 5) == 0) {
375		ret = ath_btcoex_cfg_wb335b(sc);
376	} else {
377		return (0);
378	}
379
380	/*
381	 * Propagate up failure from the actual attach phase.
382	 */
383	if (ret != 0)
384		return (ret);
385
386	return (0);
387}
388
389/*
390 * Detach btcoex from the given interface
391 */
392int
393ath_btcoex_detach(struct ath_softc *sc)
394{
395	if (sc->sc_btcoex_mci) {
396		ath_btcoex_mci_detach(sc);
397	}
398
399	return (0);
400}
401
402/*
403 * Configure or disable bluetooth coexistence on the given channel.
404 *
405 * For AR9285/AR9287/AR9485, we'll never see a 5GHz channel, so we just
406 * assume bluetooth coexistence is always on.
407 *
408 * For AR9462, we may see a 5GHz channel; bluetooth coexistence should
409 * not be enabled on those channels.
410 */
411int
412ath_btcoex_enable(struct ath_softc *sc, const struct ieee80211_channel *chan)
413{
414	if (sc->sc_btcoex_mci) {
415		ath_btcoex_mci_enable(sc, chan);
416	}
417
418	return (0);
419}
420
421/*
422 * Handle ioctl requests from the diagnostic interface.
423 *
424 * The initial part of this code resembles ath_ioctl_diag();
425 * it's likely a good idea to reduce duplication between
426 * these two routines.
427 */
428int
429ath_btcoex_ioctl(struct ath_softc *sc, struct ath_diag *ad)
430{
431	unsigned int id = ad->ad_id & ATH_DIAG_ID;
432	void *indata = NULL;
433	void *outdata = NULL;
434	u_int32_t insize = ad->ad_in_size;
435	u_int32_t outsize = ad->ad_out_size;
436	int error = 0;
437//	int val;
438
439	if (ad->ad_id & ATH_DIAG_IN) {
440		/*
441		 * Copy in data.
442		 */
443		indata = malloc(insize, M_TEMP, M_NOWAIT);
444		if (indata == NULL) {
445			error = ENOMEM;
446			goto bad;
447		}
448		error = copyin(ad->ad_in_data, indata, insize);
449		if (error)
450			goto bad;
451	}
452	if (ad->ad_id & ATH_DIAG_DYN) {
453		/*
454		 * Allocate a buffer for the results (otherwise the HAL
455		 * returns a pointer to a buffer where we can read the
456		 * results).  Note that we depend on the HAL leaving this
457		 * pointer for us to use below in reclaiming the buffer;
458		 * may want to be more defensive.
459		 */
460		outdata = malloc(outsize, M_TEMP, M_NOWAIT | M_ZERO);
461		if (outdata == NULL) {
462			error = ENOMEM;
463			goto bad;
464		}
465	}
466	switch (id) {
467		default:
468			error = EINVAL;
469			goto bad;
470	}
471	if (outsize < ad->ad_out_size)
472		ad->ad_out_size = outsize;
473	if (outdata && copyout(outdata, ad->ad_out_data, ad->ad_out_size))
474		error = EFAULT;
475bad:
476	if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
477		free(indata, M_TEMP);
478	if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
479		free(outdata, M_TEMP);
480	return (error);
481}
482
483