blob: 3adc13f6ffef08c809865382681352fe6c5a5ee7 [file] [log] [blame]
Michael Buesche4d6b792007-09-18 15:39:42 -04001/*
2
3 Broadcom B43 wireless driver
4
5 Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>
Stefano Brivio1f21ad22007-11-06 22:49:20 +01006 Copyright (c) 2005 Stefano Brivio <stefano.brivio@polimi.it>
Michael Büscheb032b92011-07-04 20:50:05 +02007 Copyright (c) 2005-2009 Michael Buesch <m@bues.ch>
Michael Buesche4d6b792007-09-18 15:39:42 -04008 Copyright (c) 2005 Danny van Dyk <kugelfang@gentoo.org>
9 Copyright (c) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
Rafał Miłecki108f4f32011-09-03 21:01:02 +020010 Copyright (c) 2010-2011 Rafał Miłecki <zajec5@gmail.com>
Michael Buesche4d6b792007-09-18 15:39:42 -040011
Albert Herranz3dbba8e2009-09-10 19:34:49 +020012 SDIO support
13 Copyright (c) 2009 Albert Herranz <albert_herranz@yahoo.es>
14
Michael Buesche4d6b792007-09-18 15:39:42 -040015 Some parts of the code in this file are derived from the ipw2200
16 driver Copyright(c) 2003 - 2004 Intel Corporation.
17
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
22
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with this program; see the file COPYING. If not, write to
30 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
31 Boston, MA 02110-1301, USA.
32
33*/
34
35#include <linux/delay.h>
36#include <linux/init.h>
Paul Gortmakerac5c24e92011-08-30 14:18:44 -040037#include <linux/module.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040038#include <linux/if_arp.h>
39#include <linux/etherdevice.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040040#include <linux/firmware.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040041#include <linux/workqueue.h>
42#include <linux/skbuff.h>
Andrew Morton96cf49a2008-02-04 22:27:19 -080043#include <linux/io.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040044#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040046#include <asm/unaligned.h>
47
48#include "b43.h"
49#include "main.h"
50#include "debugfs.h"
Michael Bueschef1a6282008-08-27 18:53:02 +020051#include "phy_common.h"
52#include "phy_g.h"
Michael Buesch3d0da752008-08-30 02:27:19 +020053#include "phy_n.h"
Michael Buesche4d6b792007-09-18 15:39:42 -040054#include "dma.h"
Michael Buesch5100d5a2008-03-29 21:01:16 +010055#include "pio.h"
Michael Buesche4d6b792007-09-18 15:39:42 -040056#include "sysfs.h"
57#include "xmit.h"
Michael Buesche4d6b792007-09-18 15:39:42 -040058#include "lo.h"
59#include "pcmcia.h"
Albert Herranz3dbba8e2009-09-10 19:34:49 +020060#include "sdio.h"
61#include <linux/mmc/sdio_func.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040062
63MODULE_DESCRIPTION("Broadcom B43 wireless driver");
64MODULE_AUTHOR("Martin Langer");
65MODULE_AUTHOR("Stefano Brivio");
66MODULE_AUTHOR("Michael Buesch");
Gábor Stefanik0136e512009-08-28 22:32:17 +020067MODULE_AUTHOR("Gábor Stefanik");
Rafał Miłecki108f4f32011-09-03 21:01:02 +020068MODULE_AUTHOR("Rafał Miłecki");
Michael Buesche4d6b792007-09-18 15:39:42 -040069MODULE_LICENSE("GPL");
70
Tim Gardner6021e082010-01-07 11:10:38 -070071MODULE_FIRMWARE("b43/ucode11.fw");
72MODULE_FIRMWARE("b43/ucode13.fw");
73MODULE_FIRMWARE("b43/ucode14.fw");
74MODULE_FIRMWARE("b43/ucode15.fw");
Rafał Miłeckif6158392011-04-19 22:49:29 +020075MODULE_FIRMWARE("b43/ucode16_mimo.fw");
Tim Gardner6021e082010-01-07 11:10:38 -070076MODULE_FIRMWARE("b43/ucode5.fw");
77MODULE_FIRMWARE("b43/ucode9.fw");
Michael Buesche4d6b792007-09-18 15:39:42 -040078
79static int modparam_bad_frames_preempt;
80module_param_named(bad_frames_preempt, modparam_bad_frames_preempt, int, 0444);
81MODULE_PARM_DESC(bad_frames_preempt,
82 "enable(1) / disable(0) Bad Frames Preemption");
83
Michael Buesche4d6b792007-09-18 15:39:42 -040084static char modparam_fwpostfix[16];
85module_param_string(fwpostfix, modparam_fwpostfix, 16, 0444);
86MODULE_PARM_DESC(fwpostfix, "Postfix for the .fw files to load.");
87
Michael Buesche4d6b792007-09-18 15:39:42 -040088static int modparam_hwpctl;
89module_param_named(hwpctl, modparam_hwpctl, int, 0444);
90MODULE_PARM_DESC(hwpctl, "Enable hardware-side power control (default off)");
91
92static int modparam_nohwcrypt;
93module_param_named(nohwcrypt, modparam_nohwcrypt, int, 0444);
94MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
95
gregor kowski035d0242009-08-19 22:35:45 +020096static int modparam_hwtkip;
97module_param_named(hwtkip, modparam_hwtkip, int, 0444);
98MODULE_PARM_DESC(hwtkip, "Enable hardware tkip.");
99
Michael Buesch403a3a12009-06-08 21:04:57 +0200100static int modparam_qos = 1;
101module_param_named(qos, modparam_qos, int, 0444);
Michael Buesche6f5b932008-03-05 21:18:49 +0100102MODULE_PARM_DESC(qos, "Enable QOS support (default on)");
103
Michael Buesch1855ba72008-04-18 20:51:41 +0200104static int modparam_btcoex = 1;
105module_param_named(btcoex, modparam_btcoex, int, 0444);
Gábor Stefanikc71dbd32009-08-28 22:34:21 +0200106MODULE_PARM_DESC(btcoex, "Enable Bluetooth coexistence (default on)");
Michael Buesch1855ba72008-04-18 20:51:41 +0200107
Michael Buesch060210f2009-01-25 15:49:59 +0100108int b43_modparam_verbose = B43_VERBOSITY_DEFAULT;
109module_param_named(verbose, b43_modparam_verbose, int, 0644);
110MODULE_PARM_DESC(verbose, "Log message verbosity: 0=error, 1=warn, 2=info(default), 3=debug");
111
Rafał Miłeckidf766262011-08-16 12:14:07 +0200112static int b43_modparam_pio = 0;
Linus Torvalds9e3bd912010-02-26 10:34:27 -0800113module_param_named(pio, b43_modparam_pio, int, 0644);
114MODULE_PARM_DESC(pio, "Use PIO accesses by default: 0=DMA, 1=PIO");
Michael Buesche6f5b932008-03-05 21:18:49 +0100115
Rafał Miłecki89604002013-06-26 09:55:54 +0200116static int modparam_allhwsupport = !IS_ENABLED(CONFIG_BRCMSMAC);
117module_param_named(allhwsupport, modparam_allhwsupport, int, 0444);
118MODULE_PARM_DESC(allhwsupport, "Enable support for all hardware (even it if overlaps with the brcmsmac driver)");
119
Rafał Miłecki3c65ab62011-06-02 09:56:04 +0200120#ifdef CONFIG_B43_BCMA
121static const struct bcma_device_id b43_bcma_tbl[] = {
Hauke Mehrtensc027ed42011-07-23 13:57:34 +0200122 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x11, BCMA_ANY_CLASS),
Rafał Miłecki3c65ab62011-06-02 09:56:04 +0200123 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x17, BCMA_ANY_CLASS),
124 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x18, BCMA_ANY_CLASS),
Rafał Miłecki15be8e82014-07-01 16:33:57 +0200125 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x1C, BCMA_ANY_CLASS),
Rafał Miłecki3c65ab62011-06-02 09:56:04 +0200126 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x1D, BCMA_ANY_CLASS),
Rafał Miłecki15be8e82014-07-01 16:33:57 +0200127 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x1E, BCMA_ANY_CLASS),
128 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x28, BCMA_ANY_CLASS),
129 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x2A, BCMA_ANY_CLASS),
Rafał Miłecki3c65ab62011-06-02 09:56:04 +0200130 BCMA_CORETABLE_END
131};
132MODULE_DEVICE_TABLE(bcma, b43_bcma_tbl);
133#endif
134
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +0200135#ifdef CONFIG_B43_SSB
Michael Buesche4d6b792007-09-18 15:39:42 -0400136static const struct ssb_device_id b43_ssb_tbl[] = {
137 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 5),
138 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 6),
139 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 7),
140 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 9),
141 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 10),
Michael Bueschd5c71e42008-01-04 17:06:29 +0100142 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 11),
Rafał Miłecki003d6d22010-01-15 12:10:53 +0100143 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 12),
Larry Finger013978b2007-11-26 10:29:47 -0600144 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 13),
Michael Buesch6b1c7c62008-12-25 00:39:28 +0100145 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 15),
Johannes Berg92d61282008-12-24 12:44:09 +0100146 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 16),
Michael Buesche4d6b792007-09-18 15:39:42 -0400147 SSB_DEVTABLE_END
148};
Michael Buesche4d6b792007-09-18 15:39:42 -0400149MODULE_DEVICE_TABLE(ssb, b43_ssb_tbl);
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +0200150#endif
Michael Buesche4d6b792007-09-18 15:39:42 -0400151
152/* Channel and ratetables are shared for all devices.
153 * They can't be const, because ieee80211 puts some precalculated
154 * data in there. This data is the same for all devices, so we don't
155 * get concurrency issues */
156#define RATETAB_ENT(_rateid, _flags) \
Johannes Berg8318d782008-01-24 19:38:38 +0100157 { \
158 .bitrate = B43_RATE_TO_BASE100KBPS(_rateid), \
159 .hw_value = (_rateid), \
160 .flags = (_flags), \
Michael Buesche4d6b792007-09-18 15:39:42 -0400161 }
Johannes Berg8318d782008-01-24 19:38:38 +0100162
163/*
164 * NOTE: When changing this, sync with xmit.c's
165 * b43_plcp_get_bitrate_idx_* functions!
166 */
Michael Buesche4d6b792007-09-18 15:39:42 -0400167static struct ieee80211_rate __b43_ratetable[] = {
Johannes Berg8318d782008-01-24 19:38:38 +0100168 RATETAB_ENT(B43_CCK_RATE_1MB, 0),
169 RATETAB_ENT(B43_CCK_RATE_2MB, IEEE80211_RATE_SHORT_PREAMBLE),
170 RATETAB_ENT(B43_CCK_RATE_5MB, IEEE80211_RATE_SHORT_PREAMBLE),
171 RATETAB_ENT(B43_CCK_RATE_11MB, IEEE80211_RATE_SHORT_PREAMBLE),
172 RATETAB_ENT(B43_OFDM_RATE_6MB, 0),
173 RATETAB_ENT(B43_OFDM_RATE_9MB, 0),
174 RATETAB_ENT(B43_OFDM_RATE_12MB, 0),
175 RATETAB_ENT(B43_OFDM_RATE_18MB, 0),
176 RATETAB_ENT(B43_OFDM_RATE_24MB, 0),
177 RATETAB_ENT(B43_OFDM_RATE_36MB, 0),
178 RATETAB_ENT(B43_OFDM_RATE_48MB, 0),
179 RATETAB_ENT(B43_OFDM_RATE_54MB, 0),
Michael Buesche4d6b792007-09-18 15:39:42 -0400180};
181
182#define b43_a_ratetable (__b43_ratetable + 4)
183#define b43_a_ratetable_size 8
184#define b43_b_ratetable (__b43_ratetable + 0)
185#define b43_b_ratetable_size 4
186#define b43_g_ratetable (__b43_ratetable + 0)
187#define b43_g_ratetable_size 12
188
Rafał Miłeckie9cdcb72014-05-21 08:44:19 +0200189#define CHAN2G(_channel, _freq, _flags) { \
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100190 .band = IEEE80211_BAND_2GHZ, \
191 .center_freq = (_freq), \
192 .hw_value = (_channel), \
193 .flags = (_flags), \
194 .max_antenna_gain = 0, \
195 .max_power = 30, \
196}
Michael Buesch96c755a2008-01-06 00:09:46 +0100197static struct ieee80211_channel b43_2ghz_chantable[] = {
Rafał Miłeckie9cdcb72014-05-21 08:44:19 +0200198 CHAN2G(1, 2412, 0),
199 CHAN2G(2, 2417, 0),
200 CHAN2G(3, 2422, 0),
201 CHAN2G(4, 2427, 0),
202 CHAN2G(5, 2432, 0),
203 CHAN2G(6, 2437, 0),
204 CHAN2G(7, 2442, 0),
205 CHAN2G(8, 2447, 0),
206 CHAN2G(9, 2452, 0),
207 CHAN2G(10, 2457, 0),
208 CHAN2G(11, 2462, 0),
209 CHAN2G(12, 2467, 0),
210 CHAN2G(13, 2472, 0),
211 CHAN2G(14, 2484, 0),
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100212};
Rafał Miłecki3695b932014-07-08 15:11:10 +0200213
214/* No support for the last 3 channels (12, 13, 14) */
215#define b43_2ghz_chantable_limited_size 11
Rafał Miłeckie9cdcb72014-05-21 08:44:19 +0200216#undef CHAN2G
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100217
Rafał Miłecki91211732014-05-21 08:44:20 +0200218#define CHAN4G(_channel, _flags) { \
219 .band = IEEE80211_BAND_5GHZ, \
220 .center_freq = 4000 + (5 * (_channel)), \
221 .hw_value = (_channel), \
222 .flags = (_flags), \
223 .max_antenna_gain = 0, \
224 .max_power = 30, \
225}
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100226#define CHAN5G(_channel, _flags) { \
227 .band = IEEE80211_BAND_5GHZ, \
228 .center_freq = 5000 + (5 * (_channel)), \
229 .hw_value = (_channel), \
230 .flags = (_flags), \
231 .max_antenna_gain = 0, \
232 .max_power = 30, \
233}
234static struct ieee80211_channel b43_5ghz_nphy_chantable[] = {
Rafał Miłecki91211732014-05-21 08:44:20 +0200235 CHAN4G(184, 0), CHAN4G(186, 0),
236 CHAN4G(188, 0), CHAN4G(190, 0),
237 CHAN4G(192, 0), CHAN4G(194, 0),
238 CHAN4G(196, 0), CHAN4G(198, 0),
239 CHAN4G(200, 0), CHAN4G(202, 0),
240 CHAN4G(204, 0), CHAN4G(206, 0),
241 CHAN4G(208, 0), CHAN4G(210, 0),
242 CHAN4G(212, 0), CHAN4G(214, 0),
243 CHAN4G(216, 0), CHAN4G(218, 0),
244 CHAN4G(220, 0), CHAN4G(222, 0),
245 CHAN4G(224, 0), CHAN4G(226, 0),
246 CHAN4G(228, 0),
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100247 CHAN5G(32, 0), CHAN5G(34, 0),
248 CHAN5G(36, 0), CHAN5G(38, 0),
249 CHAN5G(40, 0), CHAN5G(42, 0),
250 CHAN5G(44, 0), CHAN5G(46, 0),
251 CHAN5G(48, 0), CHAN5G(50, 0),
252 CHAN5G(52, 0), CHAN5G(54, 0),
253 CHAN5G(56, 0), CHAN5G(58, 0),
254 CHAN5G(60, 0), CHAN5G(62, 0),
255 CHAN5G(64, 0), CHAN5G(66, 0),
256 CHAN5G(68, 0), CHAN5G(70, 0),
257 CHAN5G(72, 0), CHAN5G(74, 0),
258 CHAN5G(76, 0), CHAN5G(78, 0),
259 CHAN5G(80, 0), CHAN5G(82, 0),
260 CHAN5G(84, 0), CHAN5G(86, 0),
261 CHAN5G(88, 0), CHAN5G(90, 0),
262 CHAN5G(92, 0), CHAN5G(94, 0),
263 CHAN5G(96, 0), CHAN5G(98, 0),
264 CHAN5G(100, 0), CHAN5G(102, 0),
265 CHAN5G(104, 0), CHAN5G(106, 0),
266 CHAN5G(108, 0), CHAN5G(110, 0),
267 CHAN5G(112, 0), CHAN5G(114, 0),
268 CHAN5G(116, 0), CHAN5G(118, 0),
269 CHAN5G(120, 0), CHAN5G(122, 0),
270 CHAN5G(124, 0), CHAN5G(126, 0),
271 CHAN5G(128, 0), CHAN5G(130, 0),
272 CHAN5G(132, 0), CHAN5G(134, 0),
273 CHAN5G(136, 0), CHAN5G(138, 0),
274 CHAN5G(140, 0), CHAN5G(142, 0),
275 CHAN5G(144, 0), CHAN5G(145, 0),
276 CHAN5G(146, 0), CHAN5G(147, 0),
277 CHAN5G(148, 0), CHAN5G(149, 0),
278 CHAN5G(150, 0), CHAN5G(151, 0),
279 CHAN5G(152, 0), CHAN5G(153, 0),
280 CHAN5G(154, 0), CHAN5G(155, 0),
281 CHAN5G(156, 0), CHAN5G(157, 0),
282 CHAN5G(158, 0), CHAN5G(159, 0),
283 CHAN5G(160, 0), CHAN5G(161, 0),
284 CHAN5G(162, 0), CHAN5G(163, 0),
285 CHAN5G(164, 0), CHAN5G(165, 0),
286 CHAN5G(166, 0), CHAN5G(168, 0),
287 CHAN5G(170, 0), CHAN5G(172, 0),
288 CHAN5G(174, 0), CHAN5G(176, 0),
289 CHAN5G(178, 0), CHAN5G(180, 0),
Rafał Miłecki91211732014-05-21 08:44:20 +0200290 CHAN5G(182, 0),
Michael Buesche4d6b792007-09-18 15:39:42 -0400291};
292
Rafał Miłeckib453fda62014-07-23 18:54:49 +0200293static struct ieee80211_channel b43_5ghz_nphy_chantable_limited[] = {
294 CHAN5G(36, 0), CHAN5G(40, 0),
295 CHAN5G(44, 0), CHAN5G(48, 0),
296 CHAN5G(149, 0), CHAN5G(153, 0),
297 CHAN5G(157, 0), CHAN5G(161, 0),
298 CHAN5G(165, 0),
299};
300
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100301static struct ieee80211_channel b43_5ghz_aphy_chantable[] = {
302 CHAN5G(34, 0), CHAN5G(36, 0),
303 CHAN5G(38, 0), CHAN5G(40, 0),
304 CHAN5G(42, 0), CHAN5G(44, 0),
305 CHAN5G(46, 0), CHAN5G(48, 0),
306 CHAN5G(52, 0), CHAN5G(56, 0),
307 CHAN5G(60, 0), CHAN5G(64, 0),
308 CHAN5G(100, 0), CHAN5G(104, 0),
309 CHAN5G(108, 0), CHAN5G(112, 0),
310 CHAN5G(116, 0), CHAN5G(120, 0),
311 CHAN5G(124, 0), CHAN5G(128, 0),
312 CHAN5G(132, 0), CHAN5G(136, 0),
313 CHAN5G(140, 0), CHAN5G(149, 0),
314 CHAN5G(153, 0), CHAN5G(157, 0),
315 CHAN5G(161, 0), CHAN5G(165, 0),
316 CHAN5G(184, 0), CHAN5G(188, 0),
317 CHAN5G(192, 0), CHAN5G(196, 0),
318 CHAN5G(200, 0), CHAN5G(204, 0),
319 CHAN5G(208, 0), CHAN5G(212, 0),
320 CHAN5G(216, 0),
321};
Rafał Miłecki91211732014-05-21 08:44:20 +0200322#undef CHAN4G
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100323#undef CHAN5G
324
325static struct ieee80211_supported_band b43_band_5GHz_nphy = {
326 .band = IEEE80211_BAND_5GHZ,
327 .channels = b43_5ghz_nphy_chantable,
328 .n_channels = ARRAY_SIZE(b43_5ghz_nphy_chantable),
329 .bitrates = b43_a_ratetable,
330 .n_bitrates = b43_a_ratetable_size,
Michael Buesche4d6b792007-09-18 15:39:42 -0400331};
Johannes Berg8318d782008-01-24 19:38:38 +0100332
Rafał Miłeckib453fda62014-07-23 18:54:49 +0200333static struct ieee80211_supported_band b43_band_5GHz_nphy_limited = {
334 .band = IEEE80211_BAND_5GHZ,
335 .channels = b43_5ghz_nphy_chantable_limited,
336 .n_channels = ARRAY_SIZE(b43_5ghz_nphy_chantable_limited),
337 .bitrates = b43_a_ratetable,
338 .n_bitrates = b43_a_ratetable_size,
339};
340
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100341static struct ieee80211_supported_band b43_band_5GHz_aphy = {
342 .band = IEEE80211_BAND_5GHZ,
343 .channels = b43_5ghz_aphy_chantable,
344 .n_channels = ARRAY_SIZE(b43_5ghz_aphy_chantable),
345 .bitrates = b43_a_ratetable,
346 .n_bitrates = b43_a_ratetable_size,
Johannes Berg8318d782008-01-24 19:38:38 +0100347};
Michael Buesche4d6b792007-09-18 15:39:42 -0400348
Johannes Berg8318d782008-01-24 19:38:38 +0100349static struct ieee80211_supported_band b43_band_2GHz = {
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100350 .band = IEEE80211_BAND_2GHZ,
351 .channels = b43_2ghz_chantable,
352 .n_channels = ARRAY_SIZE(b43_2ghz_chantable),
353 .bitrates = b43_g_ratetable,
354 .n_bitrates = b43_g_ratetable_size,
Johannes Berg8318d782008-01-24 19:38:38 +0100355};
356
Rafał Miłecki3695b932014-07-08 15:11:10 +0200357static struct ieee80211_supported_band b43_band_2ghz_limited = {
358 .band = IEEE80211_BAND_2GHZ,
359 .channels = b43_2ghz_chantable,
360 .n_channels = b43_2ghz_chantable_limited_size,
361 .bitrates = b43_g_ratetable,
362 .n_bitrates = b43_g_ratetable_size,
363};
364
Michael Buesche4d6b792007-09-18 15:39:42 -0400365static void b43_wireless_core_exit(struct b43_wldev *dev);
366static int b43_wireless_core_init(struct b43_wldev *dev);
Michael Buesch36dbd952009-09-04 22:51:29 +0200367static struct b43_wldev * b43_wireless_core_stop(struct b43_wldev *dev);
Michael Buesche4d6b792007-09-18 15:39:42 -0400368static int b43_wireless_core_start(struct b43_wldev *dev);
Felix Fietkau2a190322011-08-10 13:50:30 -0600369static void b43_op_bss_info_changed(struct ieee80211_hw *hw,
370 struct ieee80211_vif *vif,
371 struct ieee80211_bss_conf *conf,
372 u32 changed);
Michael Buesche4d6b792007-09-18 15:39:42 -0400373
374static int b43_ratelimit(struct b43_wl *wl)
375{
376 if (!wl || !wl->current_dev)
377 return 1;
378 if (b43_status(wl->current_dev) < B43_STAT_STARTED)
379 return 1;
380 /* We are up and running.
381 * Ratelimit the messages to avoid DoS over the net. */
382 return net_ratelimit();
383}
384
385void b43info(struct b43_wl *wl, const char *fmt, ...)
386{
Joe Perches5b736d42010-11-09 16:35:18 -0800387 struct va_format vaf;
Michael Buesche4d6b792007-09-18 15:39:42 -0400388 va_list args;
389
Michael Buesch060210f2009-01-25 15:49:59 +0100390 if (b43_modparam_verbose < B43_VERBOSITY_INFO)
391 return;
Michael Buesche4d6b792007-09-18 15:39:42 -0400392 if (!b43_ratelimit(wl))
393 return;
Joe Perches5b736d42010-11-09 16:35:18 -0800394
Michael Buesche4d6b792007-09-18 15:39:42 -0400395 va_start(args, fmt);
Joe Perches5b736d42010-11-09 16:35:18 -0800396
397 vaf.fmt = fmt;
398 vaf.va = &args;
399
400 printk(KERN_INFO "b43-%s: %pV",
401 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
402
Michael Buesche4d6b792007-09-18 15:39:42 -0400403 va_end(args);
404}
405
406void b43err(struct b43_wl *wl, const char *fmt, ...)
407{
Joe Perches5b736d42010-11-09 16:35:18 -0800408 struct va_format vaf;
Michael Buesche4d6b792007-09-18 15:39:42 -0400409 va_list args;
410
Michael Buesch060210f2009-01-25 15:49:59 +0100411 if (b43_modparam_verbose < B43_VERBOSITY_ERROR)
412 return;
Michael Buesche4d6b792007-09-18 15:39:42 -0400413 if (!b43_ratelimit(wl))
414 return;
Joe Perches5b736d42010-11-09 16:35:18 -0800415
Michael Buesche4d6b792007-09-18 15:39:42 -0400416 va_start(args, fmt);
Joe Perches5b736d42010-11-09 16:35:18 -0800417
418 vaf.fmt = fmt;
419 vaf.va = &args;
420
421 printk(KERN_ERR "b43-%s ERROR: %pV",
422 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
423
Michael Buesche4d6b792007-09-18 15:39:42 -0400424 va_end(args);
425}
426
427void b43warn(struct b43_wl *wl, const char *fmt, ...)
428{
Joe Perches5b736d42010-11-09 16:35:18 -0800429 struct va_format vaf;
Michael Buesche4d6b792007-09-18 15:39:42 -0400430 va_list args;
431
Michael Buesch060210f2009-01-25 15:49:59 +0100432 if (b43_modparam_verbose < B43_VERBOSITY_WARN)
433 return;
Michael Buesche4d6b792007-09-18 15:39:42 -0400434 if (!b43_ratelimit(wl))
435 return;
Joe Perches5b736d42010-11-09 16:35:18 -0800436
Michael Buesche4d6b792007-09-18 15:39:42 -0400437 va_start(args, fmt);
Joe Perches5b736d42010-11-09 16:35:18 -0800438
439 vaf.fmt = fmt;
440 vaf.va = &args;
441
442 printk(KERN_WARNING "b43-%s warning: %pV",
443 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
444
Michael Buesche4d6b792007-09-18 15:39:42 -0400445 va_end(args);
446}
447
Michael Buesche4d6b792007-09-18 15:39:42 -0400448void b43dbg(struct b43_wl *wl, const char *fmt, ...)
449{
Joe Perches5b736d42010-11-09 16:35:18 -0800450 struct va_format vaf;
Michael Buesche4d6b792007-09-18 15:39:42 -0400451 va_list args;
452
Michael Buesch060210f2009-01-25 15:49:59 +0100453 if (b43_modparam_verbose < B43_VERBOSITY_DEBUG)
454 return;
Joe Perches5b736d42010-11-09 16:35:18 -0800455
Michael Buesche4d6b792007-09-18 15:39:42 -0400456 va_start(args, fmt);
Joe Perches5b736d42010-11-09 16:35:18 -0800457
458 vaf.fmt = fmt;
459 vaf.va = &args;
460
461 printk(KERN_DEBUG "b43-%s debug: %pV",
462 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
463
Michael Buesche4d6b792007-09-18 15:39:42 -0400464 va_end(args);
465}
Michael Buesche4d6b792007-09-18 15:39:42 -0400466
467static void b43_ram_write(struct b43_wldev *dev, u16 offset, u32 val)
468{
469 u32 macctl;
470
471 B43_WARN_ON(offset % 4 != 0);
472
473 macctl = b43_read32(dev, B43_MMIO_MACCTL);
474 if (macctl & B43_MACCTL_BE)
475 val = swab32(val);
476
477 b43_write32(dev, B43_MMIO_RAM_CONTROL, offset);
478 mmiowb();
479 b43_write32(dev, B43_MMIO_RAM_DATA, val);
480}
481
Michael Buesch280d0e12007-12-26 18:26:17 +0100482static inline void b43_shm_control_word(struct b43_wldev *dev,
483 u16 routing, u16 offset)
Michael Buesche4d6b792007-09-18 15:39:42 -0400484{
485 u32 control;
486
487 /* "offset" is the WORD offset. */
Michael Buesche4d6b792007-09-18 15:39:42 -0400488 control = routing;
489 control <<= 16;
490 control |= offset;
491 b43_write32(dev, B43_MMIO_SHM_CONTROL, control);
492}
493
Michael Buesch69eddc82009-09-04 22:57:26 +0200494u32 b43_shm_read32(struct b43_wldev *dev, u16 routing, u16 offset)
Michael Buesche4d6b792007-09-18 15:39:42 -0400495{
496 u32 ret;
497
498 if (routing == B43_SHM_SHARED) {
499 B43_WARN_ON(offset & 0x0001);
500 if (offset & 0x0003) {
501 /* Unaligned access */
502 b43_shm_control_word(dev, routing, offset >> 2);
503 ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
Michael Buesche4d6b792007-09-18 15:39:42 -0400504 b43_shm_control_word(dev, routing, (offset >> 2) + 1);
Michael Bueschf62ae6c2009-07-31 20:51:41 +0200505 ret |= ((u32)b43_read16(dev, B43_MMIO_SHM_DATA)) << 16;
Michael Buesche4d6b792007-09-18 15:39:42 -0400506
Michael Buesch280d0e12007-12-26 18:26:17 +0100507 goto out;
Michael Buesche4d6b792007-09-18 15:39:42 -0400508 }
509 offset >>= 2;
510 }
511 b43_shm_control_word(dev, routing, offset);
512 ret = b43_read32(dev, B43_MMIO_SHM_DATA);
Michael Buesch280d0e12007-12-26 18:26:17 +0100513out:
Michael Buesch6bbc3212008-06-19 19:33:51 +0200514 return ret;
515}
516
Michael Buesch69eddc82009-09-04 22:57:26 +0200517u16 b43_shm_read16(struct b43_wldev *dev, u16 routing, u16 offset)
Michael Buesche4d6b792007-09-18 15:39:42 -0400518{
519 u16 ret;
520
521 if (routing == B43_SHM_SHARED) {
522 B43_WARN_ON(offset & 0x0001);
523 if (offset & 0x0003) {
524 /* Unaligned access */
525 b43_shm_control_word(dev, routing, offset >> 2);
526 ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
527
Michael Buesch280d0e12007-12-26 18:26:17 +0100528 goto out;
Michael Buesche4d6b792007-09-18 15:39:42 -0400529 }
530 offset >>= 2;
531 }
532 b43_shm_control_word(dev, routing, offset);
533 ret = b43_read16(dev, B43_MMIO_SHM_DATA);
Michael Buesch280d0e12007-12-26 18:26:17 +0100534out:
Michael Buesch6bbc3212008-06-19 19:33:51 +0200535 return ret;
536}
537
Michael Buesch69eddc82009-09-04 22:57:26 +0200538void b43_shm_write32(struct b43_wldev *dev, u16 routing, u16 offset, u32 value)
Michael Buesche4d6b792007-09-18 15:39:42 -0400539{
540 if (routing == B43_SHM_SHARED) {
541 B43_WARN_ON(offset & 0x0001);
542 if (offset & 0x0003) {
543 /* Unaligned access */
544 b43_shm_control_word(dev, routing, offset >> 2);
Michael Buesche4d6b792007-09-18 15:39:42 -0400545 b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED,
Michael Bueschf62ae6c2009-07-31 20:51:41 +0200546 value & 0xFFFF);
Michael Buesche4d6b792007-09-18 15:39:42 -0400547 b43_shm_control_word(dev, routing, (offset >> 2) + 1);
Michael Bueschf62ae6c2009-07-31 20:51:41 +0200548 b43_write16(dev, B43_MMIO_SHM_DATA,
549 (value >> 16) & 0xFFFF);
Michael Buesch6bbc3212008-06-19 19:33:51 +0200550 return;
Michael Buesche4d6b792007-09-18 15:39:42 -0400551 }
552 offset >>= 2;
553 }
554 b43_shm_control_word(dev, routing, offset);
Michael Buesche4d6b792007-09-18 15:39:42 -0400555 b43_write32(dev, B43_MMIO_SHM_DATA, value);
Michael Buesch6bbc3212008-06-19 19:33:51 +0200556}
557
Michael Buesch69eddc82009-09-04 22:57:26 +0200558void b43_shm_write16(struct b43_wldev *dev, u16 routing, u16 offset, u16 value)
Michael Buesch6bbc3212008-06-19 19:33:51 +0200559{
560 if (routing == B43_SHM_SHARED) {
561 B43_WARN_ON(offset & 0x0001);
562 if (offset & 0x0003) {
563 /* Unaligned access */
564 b43_shm_control_word(dev, routing, offset >> 2);
565 b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED, value);
566 return;
567 }
568 offset >>= 2;
569 }
570 b43_shm_control_word(dev, routing, offset);
571 b43_write16(dev, B43_MMIO_SHM_DATA, value);
572}
573
Michael Buesche4d6b792007-09-18 15:39:42 -0400574/* Read HostFlags */
John Daiker99da1852009-02-24 02:16:42 -0800575u64 b43_hf_read(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -0400576{
Michael Buesch35f0d352008-02-13 14:31:08 +0100577 u64 ret;
Michael Buesche4d6b792007-09-18 15:39:42 -0400578
Rafał Miłecki6e6a2cd2012-07-25 16:58:38 +0200579 ret = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF3);
Michael Buesche4d6b792007-09-18 15:39:42 -0400580 ret <<= 16;
Rafał Miłecki6e6a2cd2012-07-25 16:58:38 +0200581 ret |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF2);
Michael Buesch35f0d352008-02-13 14:31:08 +0100582 ret <<= 16;
Rafał Miłecki6e6a2cd2012-07-25 16:58:38 +0200583 ret |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF1);
Michael Buesche4d6b792007-09-18 15:39:42 -0400584
585 return ret;
586}
587
588/* Write HostFlags */
Michael Buesch35f0d352008-02-13 14:31:08 +0100589void b43_hf_write(struct b43_wldev *dev, u64 value)
Michael Buesche4d6b792007-09-18 15:39:42 -0400590{
Michael Buesch35f0d352008-02-13 14:31:08 +0100591 u16 lo, mi, hi;
592
593 lo = (value & 0x00000000FFFFULL);
594 mi = (value & 0x0000FFFF0000ULL) >> 16;
595 hi = (value & 0xFFFF00000000ULL) >> 32;
Rafał Miłecki6e6a2cd2012-07-25 16:58:38 +0200596 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF1, lo);
597 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF2, mi);
598 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF3, hi);
Michael Buesche4d6b792007-09-18 15:39:42 -0400599}
600
Michael Buesch403a3a12009-06-08 21:04:57 +0200601/* Read the firmware capabilities bitmask (Opensource firmware only) */
602static u16 b43_fwcapa_read(struct b43_wldev *dev)
603{
604 B43_WARN_ON(!dev->fw.opensource);
605 return b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_FWCAPA);
606}
607
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100608void b43_tsf_read(struct b43_wldev *dev, u64 *tsf)
Michael Buesche4d6b792007-09-18 15:39:42 -0400609{
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100610 u32 low, high;
Michael Buesche4d6b792007-09-18 15:39:42 -0400611
Rafał Miłecki21d889d2011-05-18 02:06:38 +0200612 B43_WARN_ON(dev->dev->core_rev < 3);
Michael Buesche4d6b792007-09-18 15:39:42 -0400613
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100614 /* The hardware guarantees us an atomic read, if we
615 * read the low register first. */
616 low = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_LOW);
617 high = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_HIGH);
Michael Buesche4d6b792007-09-18 15:39:42 -0400618
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100619 *tsf = high;
620 *tsf <<= 32;
621 *tsf |= low;
Michael Buesche4d6b792007-09-18 15:39:42 -0400622}
623
624static void b43_time_lock(struct b43_wldev *dev)
625{
Rafał Miłecki50566352012-01-02 19:31:21 +0100626 b43_maskset32(dev, B43_MMIO_MACCTL, ~0, B43_MACCTL_TBTTHOLD);
Michael Buesche4d6b792007-09-18 15:39:42 -0400627 /* Commit the write */
628 b43_read32(dev, B43_MMIO_MACCTL);
629}
630
631static void b43_time_unlock(struct b43_wldev *dev)
632{
Rafał Miłecki50566352012-01-02 19:31:21 +0100633 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_TBTTHOLD, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -0400634 /* Commit the write */
635 b43_read32(dev, B43_MMIO_MACCTL);
636}
637
638static void b43_tsf_write_locked(struct b43_wldev *dev, u64 tsf)
639{
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100640 u32 low, high;
Michael Buesche4d6b792007-09-18 15:39:42 -0400641
Rafał Miłecki21d889d2011-05-18 02:06:38 +0200642 B43_WARN_ON(dev->dev->core_rev < 3);
Michael Buesche4d6b792007-09-18 15:39:42 -0400643
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100644 low = tsf;
645 high = (tsf >> 32);
646 /* The hardware guarantees us an atomic write, if we
647 * write the low register first. */
648 b43_write32(dev, B43_MMIO_REV3PLUS_TSF_LOW, low);
649 mmiowb();
650 b43_write32(dev, B43_MMIO_REV3PLUS_TSF_HIGH, high);
651 mmiowb();
Michael Buesche4d6b792007-09-18 15:39:42 -0400652}
653
654void b43_tsf_write(struct b43_wldev *dev, u64 tsf)
655{
656 b43_time_lock(dev);
657 b43_tsf_write_locked(dev, tsf);
658 b43_time_unlock(dev);
659}
660
661static
John Daiker99da1852009-02-24 02:16:42 -0800662void b43_macfilter_set(struct b43_wldev *dev, u16 offset, const u8 *mac)
Michael Buesche4d6b792007-09-18 15:39:42 -0400663{
664 static const u8 zero_addr[ETH_ALEN] = { 0 };
665 u16 data;
666
667 if (!mac)
668 mac = zero_addr;
669
670 offset |= 0x0020;
671 b43_write16(dev, B43_MMIO_MACFILTER_CONTROL, offset);
672
673 data = mac[0];
674 data |= mac[1] << 8;
675 b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
676 data = mac[2];
677 data |= mac[3] << 8;
678 b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
679 data = mac[4];
680 data |= mac[5] << 8;
681 b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
682}
683
684static void b43_write_mac_bssid_templates(struct b43_wldev *dev)
685{
686 const u8 *mac;
687 const u8 *bssid;
688 u8 mac_bssid[ETH_ALEN * 2];
689 int i;
690 u32 tmp;
691
692 bssid = dev->wl->bssid;
693 mac = dev->wl->mac_addr;
694
695 b43_macfilter_set(dev, B43_MACFILTER_BSSID, bssid);
696
697 memcpy(mac_bssid, mac, ETH_ALEN);
698 memcpy(mac_bssid + ETH_ALEN, bssid, ETH_ALEN);
699
700 /* Write our MAC address and BSSID to template ram */
701 for (i = 0; i < ARRAY_SIZE(mac_bssid); i += sizeof(u32)) {
702 tmp = (u32) (mac_bssid[i + 0]);
703 tmp |= (u32) (mac_bssid[i + 1]) << 8;
704 tmp |= (u32) (mac_bssid[i + 2]) << 16;
705 tmp |= (u32) (mac_bssid[i + 3]) << 24;
706 b43_ram_write(dev, 0x20 + i, tmp);
707 }
708}
709
Johannes Berg4150c572007-09-17 01:29:23 -0400710static void b43_upload_card_macaddress(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -0400711{
Michael Buesche4d6b792007-09-18 15:39:42 -0400712 b43_write_mac_bssid_templates(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400713 b43_macfilter_set(dev, B43_MACFILTER_SELF, dev->wl->mac_addr);
Michael Buesche4d6b792007-09-18 15:39:42 -0400714}
715
716static void b43_set_slot_time(struct b43_wldev *dev, u16 slot_time)
717{
718 /* slot_time is in usec. */
Larry Fingerb6c3f5b2010-02-02 10:08:19 -0600719 /* This test used to exit for all but a G PHY. */
720 if (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ)
Michael Buesche4d6b792007-09-18 15:39:42 -0400721 return;
Larry Fingerb6c3f5b2010-02-02 10:08:19 -0600722 b43_write16(dev, B43_MMIO_IFSSLOT, 510 + slot_time);
723 /* Shared memory location 0x0010 is the slot time and should be
724 * set to slot_time; however, this register is initially 0 and changing
725 * the value adversely affects the transmit rate for BCM4311
726 * devices. Until this behavior is unterstood, delete this step
727 *
728 * b43_shm_write16(dev, B43_SHM_SHARED, 0x0010, slot_time);
729 */
Michael Buesche4d6b792007-09-18 15:39:42 -0400730}
731
732static void b43_short_slot_timing_enable(struct b43_wldev *dev)
733{
734 b43_set_slot_time(dev, 9);
Michael Buesche4d6b792007-09-18 15:39:42 -0400735}
736
737static void b43_short_slot_timing_disable(struct b43_wldev *dev)
738{
739 b43_set_slot_time(dev, 20);
Michael Buesche4d6b792007-09-18 15:39:42 -0400740}
741
Michael Buesche4d6b792007-09-18 15:39:42 -0400742/* DummyTransmission function, as documented on
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200743 * http://bcm-v4.sipsolutions.net/802.11/DummyTransmission
Michael Buesche4d6b792007-09-18 15:39:42 -0400744 */
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200745void b43_dummy_transmission(struct b43_wldev *dev, bool ofdm, bool pa_on)
Michael Buesche4d6b792007-09-18 15:39:42 -0400746{
747 struct b43_phy *phy = &dev->phy;
748 unsigned int i, max_loop;
749 u16 value;
750 u32 buffer[5] = {
751 0x00000000,
752 0x00D40000,
753 0x00000000,
754 0x01000000,
755 0x00000000,
756 };
757
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200758 if (ofdm) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400759 max_loop = 0x1E;
760 buffer[0] = 0x000201CC;
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200761 } else {
Michael Buesche4d6b792007-09-18 15:39:42 -0400762 max_loop = 0xFA;
763 buffer[0] = 0x000B846E;
Michael Buesche4d6b792007-09-18 15:39:42 -0400764 }
765
766 for (i = 0; i < 5; i++)
767 b43_ram_write(dev, i * 4, buffer[i]);
768
Rafał Miłecki7955d872011-09-21 21:44:13 +0200769 b43_write16(dev, B43_MMIO_XMTSEL, 0x0000);
770
Rafał Miłecki21d889d2011-05-18 02:06:38 +0200771 if (dev->dev->core_rev < 11)
Rafał Miłecki7955d872011-09-21 21:44:13 +0200772 b43_write16(dev, B43_MMIO_WEPCTL, 0x0000);
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200773 else
Rafał Miłecki7955d872011-09-21 21:44:13 +0200774 b43_write16(dev, B43_MMIO_WEPCTL, 0x0100);
775
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200776 value = (ofdm ? 0x41 : 0x40);
Rafał Miłecki7955d872011-09-21 21:44:13 +0200777 b43_write16(dev, B43_MMIO_TXE0_PHYCTL, value);
Rafał Miłecki93dbd822011-09-21 21:44:14 +0200778 if (phy->type == B43_PHYTYPE_N || phy->type == B43_PHYTYPE_LP ||
779 phy->type == B43_PHYTYPE_LCN)
Rafał Miłecki7955d872011-09-21 21:44:13 +0200780 b43_write16(dev, B43_MMIO_TXE0_PHYCTL1, 0x1A02);
781
782 b43_write16(dev, B43_MMIO_TXE0_WM_0, 0x0000);
783 b43_write16(dev, B43_MMIO_TXE0_WM_1, 0x0000);
784
785 b43_write16(dev, B43_MMIO_XMTTPLATETXPTR, 0x0000);
786 b43_write16(dev, B43_MMIO_XMTTXCNT, 0x0014);
787 b43_write16(dev, B43_MMIO_XMTSEL, 0x0826);
788 b43_write16(dev, B43_MMIO_TXE0_CTL, 0x0000);
Rafał Miłecki93dbd822011-09-21 21:44:14 +0200789
790 if (!pa_on && phy->type == B43_PHYTYPE_N)
791 ; /*b43_nphy_pa_override(dev, false) */
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200792
793 switch (phy->type) {
794 case B43_PHYTYPE_N:
Rafał Miłecki93dbd822011-09-21 21:44:14 +0200795 case B43_PHYTYPE_LCN:
Rafał Miłecki7955d872011-09-21 21:44:13 +0200796 b43_write16(dev, B43_MMIO_TXE0_AUX, 0x00D0);
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200797 break;
798 case B43_PHYTYPE_LP:
Rafał Miłecki7955d872011-09-21 21:44:13 +0200799 b43_write16(dev, B43_MMIO_TXE0_AUX, 0x0050);
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200800 break;
801 default:
Rafał Miłecki7955d872011-09-21 21:44:13 +0200802 b43_write16(dev, B43_MMIO_TXE0_AUX, 0x0030);
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200803 }
Rafał Miłecki93dbd822011-09-21 21:44:14 +0200804 b43_read16(dev, B43_MMIO_TXE0_AUX);
Michael Buesche4d6b792007-09-18 15:39:42 -0400805
806 if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
807 b43_radio_write16(dev, 0x0051, 0x0017);
808 for (i = 0x00; i < max_loop; i++) {
Rafał Miłecki7955d872011-09-21 21:44:13 +0200809 value = b43_read16(dev, B43_MMIO_TXE0_STATUS);
Michael Buesche4d6b792007-09-18 15:39:42 -0400810 if (value & 0x0080)
811 break;
812 udelay(10);
813 }
814 for (i = 0x00; i < 0x0A; i++) {
Rafał Miłecki7955d872011-09-21 21:44:13 +0200815 value = b43_read16(dev, B43_MMIO_TXE0_STATUS);
Michael Buesche4d6b792007-09-18 15:39:42 -0400816 if (value & 0x0400)
817 break;
818 udelay(10);
819 }
Larry Finger1d280dd2008-09-29 14:19:29 -0500820 for (i = 0x00; i < 0x19; i++) {
Rafał Miłecki7955d872011-09-21 21:44:13 +0200821 value = b43_read16(dev, B43_MMIO_IFSSTAT);
Michael Buesche4d6b792007-09-18 15:39:42 -0400822 if (!(value & 0x0100))
823 break;
824 udelay(10);
825 }
826 if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
827 b43_radio_write16(dev, 0x0051, 0x0037);
828}
829
830static void key_write(struct b43_wldev *dev,
John Daiker99da1852009-02-24 02:16:42 -0800831 u8 index, u8 algorithm, const u8 *key)
Michael Buesche4d6b792007-09-18 15:39:42 -0400832{
833 unsigned int i;
834 u32 offset;
835 u16 value;
836 u16 kidx;
837
838 /* Key index/algo block */
839 kidx = b43_kidx_to_fw(dev, index);
840 value = ((kidx << 4) | algorithm);
841 b43_shm_write16(dev, B43_SHM_SHARED,
842 B43_SHM_SH_KEYIDXBLOCK + (kidx * 2), value);
843
844 /* Write the key to the Key Table Pointer offset */
845 offset = dev->ktp + (index * B43_SEC_KEYSIZE);
846 for (i = 0; i < B43_SEC_KEYSIZE; i += 2) {
847 value = key[i];
848 value |= (u16) (key[i + 1]) << 8;
849 b43_shm_write16(dev, B43_SHM_SHARED, offset + i, value);
850 }
851}
852
John Daiker99da1852009-02-24 02:16:42 -0800853static void keymac_write(struct b43_wldev *dev, u8 index, const u8 *addr)
Michael Buesche4d6b792007-09-18 15:39:42 -0400854{
855 u32 addrtmp[2] = { 0, 0, };
Michael Buesch66d2d082009-08-06 10:36:50 +0200856 u8 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
Michael Buesche4d6b792007-09-18 15:39:42 -0400857
858 if (b43_new_kidx_api(dev))
Michael Buesch66d2d082009-08-06 10:36:50 +0200859 pairwise_keys_start = B43_NR_GROUP_KEYS;
Michael Buesche4d6b792007-09-18 15:39:42 -0400860
Michael Buesch66d2d082009-08-06 10:36:50 +0200861 B43_WARN_ON(index < pairwise_keys_start);
862 /* We have four default TX keys and possibly four default RX keys.
Michael Buesche4d6b792007-09-18 15:39:42 -0400863 * Physical mac 0 is mapped to physical key 4 or 8, depending
864 * on the firmware version.
865 * So we must adjust the index here.
866 */
Michael Buesch66d2d082009-08-06 10:36:50 +0200867 index -= pairwise_keys_start;
868 B43_WARN_ON(index >= B43_NR_PAIRWISE_KEYS);
Michael Buesche4d6b792007-09-18 15:39:42 -0400869
870 if (addr) {
871 addrtmp[0] = addr[0];
872 addrtmp[0] |= ((u32) (addr[1]) << 8);
873 addrtmp[0] |= ((u32) (addr[2]) << 16);
874 addrtmp[0] |= ((u32) (addr[3]) << 24);
875 addrtmp[1] = addr[4];
876 addrtmp[1] |= ((u32) (addr[5]) << 8);
877 }
878
Michael Buesch66d2d082009-08-06 10:36:50 +0200879 /* Receive match transmitter address (RCMTA) mechanism */
880 b43_shm_write32(dev, B43_SHM_RCMTA,
881 (index * 2) + 0, addrtmp[0]);
882 b43_shm_write16(dev, B43_SHM_RCMTA,
883 (index * 2) + 1, addrtmp[1]);
Michael Buesche4d6b792007-09-18 15:39:42 -0400884}
885
gregor kowski035d0242009-08-19 22:35:45 +0200886/* The ucode will use phase1 key with TEK key to decrypt rx packets.
887 * When a packet is received, the iv32 is checked.
888 * - if it doesn't the packet is returned without modification (and software
889 * decryption can be done). That's what happen when iv16 wrap.
890 * - if it does, the rc4 key is computed, and decryption is tried.
891 * Either it will success and B43_RX_MAC_DEC is returned,
892 * either it fails and B43_RX_MAC_DEC|B43_RX_MAC_DECERR is returned
893 * and the packet is not usable (it got modified by the ucode).
894 * So in order to never have B43_RX_MAC_DECERR, we should provide
895 * a iv32 and phase1key that match. Because we drop packets in case of
896 * B43_RX_MAC_DECERR, if we have a correct iv32 but a wrong phase1key, all
897 * packets will be lost without higher layer knowing (ie no resync possible
898 * until next wrap).
899 *
900 * NOTE : this should support 50 key like RCMTA because
901 * (B43_SHM_SH_KEYIDXBLOCK - B43_SHM_SH_TKIPTSCTTAK)/14 = 50
902 */
903static void rx_tkip_phase1_write(struct b43_wldev *dev, u8 index, u32 iv32,
904 u16 *phase1key)
905{
906 unsigned int i;
907 u32 offset;
908 u8 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
909
910 if (!modparam_hwtkip)
911 return;
912
913 if (b43_new_kidx_api(dev))
914 pairwise_keys_start = B43_NR_GROUP_KEYS;
915
916 B43_WARN_ON(index < pairwise_keys_start);
917 /* We have four default TX keys and possibly four default RX keys.
918 * Physical mac 0 is mapped to physical key 4 or 8, depending
919 * on the firmware version.
920 * So we must adjust the index here.
921 */
922 index -= pairwise_keys_start;
923 B43_WARN_ON(index >= B43_NR_PAIRWISE_KEYS);
924
925 if (b43_debug(dev, B43_DBG_KEYS)) {
926 b43dbg(dev->wl, "rx_tkip_phase1_write : idx 0x%x, iv32 0x%x\n",
927 index, iv32);
928 }
929 /* Write the key to the RX tkip shared mem */
930 offset = B43_SHM_SH_TKIPTSCTTAK + index * (10 + 4);
931 for (i = 0; i < 10; i += 2) {
932 b43_shm_write16(dev, B43_SHM_SHARED, offset + i,
933 phase1key ? phase1key[i / 2] : 0);
934 }
935 b43_shm_write16(dev, B43_SHM_SHARED, offset + i, iv32);
936 b43_shm_write16(dev, B43_SHM_SHARED, offset + i + 2, iv32 >> 16);
937}
938
939static void b43_op_update_tkip_key(struct ieee80211_hw *hw,
Johannes Bergb3fbdcf2010-01-21 11:40:47 +0100940 struct ieee80211_vif *vif,
941 struct ieee80211_key_conf *keyconf,
942 struct ieee80211_sta *sta,
943 u32 iv32, u16 *phase1key)
gregor kowski035d0242009-08-19 22:35:45 +0200944{
945 struct b43_wl *wl = hw_to_b43_wl(hw);
946 struct b43_wldev *dev;
947 int index = keyconf->hw_key_idx;
948
949 if (B43_WARN_ON(!modparam_hwtkip))
950 return;
951
Michael Buesch96869a32010-01-24 13:13:32 +0100952 /* This is only called from the RX path through mac80211, where
953 * our mutex is already locked. */
954 B43_WARN_ON(!mutex_is_locked(&wl->mutex));
gregor kowski035d0242009-08-19 22:35:45 +0200955 dev = wl->current_dev;
Michael Buesch96869a32010-01-24 13:13:32 +0100956 B43_WARN_ON(!dev || b43_status(dev) < B43_STAT_INITIALIZED);
gregor kowski035d0242009-08-19 22:35:45 +0200957
958 keymac_write(dev, index, NULL); /* First zero out mac to avoid race */
959
960 rx_tkip_phase1_write(dev, index, iv32, phase1key);
Johannes Bergb3fbdcf2010-01-21 11:40:47 +0100961 /* only pairwise TKIP keys are supported right now */
962 if (WARN_ON(!sta))
Michael Buesch96869a32010-01-24 13:13:32 +0100963 return;
Johannes Bergb3fbdcf2010-01-21 11:40:47 +0100964 keymac_write(dev, index, sta->addr);
gregor kowski035d0242009-08-19 22:35:45 +0200965}
966
Michael Buesche4d6b792007-09-18 15:39:42 -0400967static void do_key_write(struct b43_wldev *dev,
968 u8 index, u8 algorithm,
John Daiker99da1852009-02-24 02:16:42 -0800969 const u8 *key, size_t key_len, const u8 *mac_addr)
Michael Buesche4d6b792007-09-18 15:39:42 -0400970{
971 u8 buf[B43_SEC_KEYSIZE] = { 0, };
Michael Buesch66d2d082009-08-06 10:36:50 +0200972 u8 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
Michael Buesche4d6b792007-09-18 15:39:42 -0400973
974 if (b43_new_kidx_api(dev))
Michael Buesch66d2d082009-08-06 10:36:50 +0200975 pairwise_keys_start = B43_NR_GROUP_KEYS;
Michael Buesche4d6b792007-09-18 15:39:42 -0400976
Michael Buesch66d2d082009-08-06 10:36:50 +0200977 B43_WARN_ON(index >= ARRAY_SIZE(dev->key));
Michael Buesche4d6b792007-09-18 15:39:42 -0400978 B43_WARN_ON(key_len > B43_SEC_KEYSIZE);
979
Michael Buesch66d2d082009-08-06 10:36:50 +0200980 if (index >= pairwise_keys_start)
Michael Buesche4d6b792007-09-18 15:39:42 -0400981 keymac_write(dev, index, NULL); /* First zero out mac. */
gregor kowski035d0242009-08-19 22:35:45 +0200982 if (algorithm == B43_SEC_ALGO_TKIP) {
983 /*
984 * We should provide an initial iv32, phase1key pair.
985 * We could start with iv32=0 and compute the corresponding
986 * phase1key, but this means calling ieee80211_get_tkip_key
987 * with a fake skb (or export other tkip function).
988 * Because we are lazy we hope iv32 won't start with
989 * 0xffffffff and let's b43_op_update_tkip_key provide a
990 * correct pair.
991 */
992 rx_tkip_phase1_write(dev, index, 0xffffffff, (u16*)buf);
993 } else if (index >= pairwise_keys_start) /* clear it */
994 rx_tkip_phase1_write(dev, index, 0, NULL);
Michael Buesche4d6b792007-09-18 15:39:42 -0400995 if (key)
996 memcpy(buf, key, key_len);
997 key_write(dev, index, algorithm, buf);
Michael Buesch66d2d082009-08-06 10:36:50 +0200998 if (index >= pairwise_keys_start)
Michael Buesche4d6b792007-09-18 15:39:42 -0400999 keymac_write(dev, index, mac_addr);
1000
1001 dev->key[index].algorithm = algorithm;
1002}
1003
1004static int b43_key_write(struct b43_wldev *dev,
1005 int index, u8 algorithm,
John Daiker99da1852009-02-24 02:16:42 -08001006 const u8 *key, size_t key_len,
1007 const u8 *mac_addr,
Michael Buesche4d6b792007-09-18 15:39:42 -04001008 struct ieee80211_key_conf *keyconf)
1009{
1010 int i;
Michael Buesch66d2d082009-08-06 10:36:50 +02001011 int pairwise_keys_start;
Michael Buesche4d6b792007-09-18 15:39:42 -04001012
gregor kowski035d0242009-08-19 22:35:45 +02001013 /* For ALG_TKIP the key is encoded as a 256-bit (32 byte) data block:
1014 * - Temporal Encryption Key (128 bits)
1015 * - Temporal Authenticator Tx MIC Key (64 bits)
1016 * - Temporal Authenticator Rx MIC Key (64 bits)
1017 *
1018 * Hardware only store TEK
1019 */
1020 if (algorithm == B43_SEC_ALGO_TKIP && key_len == 32)
1021 key_len = 16;
Michael Buesche4d6b792007-09-18 15:39:42 -04001022 if (key_len > B43_SEC_KEYSIZE)
1023 return -EINVAL;
Michael Buesch66d2d082009-08-06 10:36:50 +02001024 for (i = 0; i < ARRAY_SIZE(dev->key); i++) {
Michael Buesche4d6b792007-09-18 15:39:42 -04001025 /* Check that we don't already have this key. */
1026 B43_WARN_ON(dev->key[i].keyconf == keyconf);
1027 }
1028 if (index < 0) {
Michael Buesche808e582008-12-19 21:30:52 +01001029 /* Pairwise key. Get an empty slot for the key. */
Michael Buesche4d6b792007-09-18 15:39:42 -04001030 if (b43_new_kidx_api(dev))
Michael Buesch66d2d082009-08-06 10:36:50 +02001031 pairwise_keys_start = B43_NR_GROUP_KEYS;
Michael Buesche4d6b792007-09-18 15:39:42 -04001032 else
Michael Buesch66d2d082009-08-06 10:36:50 +02001033 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
1034 for (i = pairwise_keys_start;
1035 i < pairwise_keys_start + B43_NR_PAIRWISE_KEYS;
1036 i++) {
1037 B43_WARN_ON(i >= ARRAY_SIZE(dev->key));
Michael Buesche4d6b792007-09-18 15:39:42 -04001038 if (!dev->key[i].keyconf) {
1039 /* found empty */
1040 index = i;
1041 break;
1042 }
1043 }
1044 if (index < 0) {
Michael Buesche808e582008-12-19 21:30:52 +01001045 b43warn(dev->wl, "Out of hardware key memory\n");
Michael Buesche4d6b792007-09-18 15:39:42 -04001046 return -ENOSPC;
1047 }
1048 } else
1049 B43_WARN_ON(index > 3);
1050
1051 do_key_write(dev, index, algorithm, key, key_len, mac_addr);
1052 if ((index <= 3) && !b43_new_kidx_api(dev)) {
1053 /* Default RX key */
1054 B43_WARN_ON(mac_addr);
1055 do_key_write(dev, index + 4, algorithm, key, key_len, NULL);
1056 }
1057 keyconf->hw_key_idx = index;
1058 dev->key[index].keyconf = keyconf;
1059
1060 return 0;
1061}
1062
1063static int b43_key_clear(struct b43_wldev *dev, int index)
1064{
Michael Buesch66d2d082009-08-06 10:36:50 +02001065 if (B43_WARN_ON((index < 0) || (index >= ARRAY_SIZE(dev->key))))
Michael Buesche4d6b792007-09-18 15:39:42 -04001066 return -EINVAL;
1067 do_key_write(dev, index, B43_SEC_ALGO_NONE,
1068 NULL, B43_SEC_KEYSIZE, NULL);
1069 if ((index <= 3) && !b43_new_kidx_api(dev)) {
1070 do_key_write(dev, index + 4, B43_SEC_ALGO_NONE,
1071 NULL, B43_SEC_KEYSIZE, NULL);
1072 }
1073 dev->key[index].keyconf = NULL;
1074
1075 return 0;
1076}
1077
1078static void b43_clear_keys(struct b43_wldev *dev)
1079{
Michael Buesch66d2d082009-08-06 10:36:50 +02001080 int i, count;
Michael Buesche4d6b792007-09-18 15:39:42 -04001081
Michael Buesch66d2d082009-08-06 10:36:50 +02001082 if (b43_new_kidx_api(dev))
1083 count = B43_NR_GROUP_KEYS + B43_NR_PAIRWISE_KEYS;
1084 else
1085 count = B43_NR_GROUP_KEYS * 2 + B43_NR_PAIRWISE_KEYS;
1086 for (i = 0; i < count; i++)
Michael Buesche4d6b792007-09-18 15:39:42 -04001087 b43_key_clear(dev, i);
1088}
1089
Michael Buesch9cf7f242008-12-19 20:24:30 +01001090static void b43_dump_keymemory(struct b43_wldev *dev)
1091{
Michael Buesch66d2d082009-08-06 10:36:50 +02001092 unsigned int i, index, count, offset, pairwise_keys_start;
Michael Buesch9cf7f242008-12-19 20:24:30 +01001093 u8 mac[ETH_ALEN];
1094 u16 algo;
1095 u32 rcmta0;
1096 u16 rcmta1;
1097 u64 hf;
1098 struct b43_key *key;
1099
1100 if (!b43_debug(dev, B43_DBG_KEYS))
1101 return;
1102
1103 hf = b43_hf_read(dev);
1104 b43dbg(dev->wl, "Hardware key memory dump: USEDEFKEYS=%u\n",
1105 !!(hf & B43_HF_USEDEFKEYS));
Michael Buesch66d2d082009-08-06 10:36:50 +02001106 if (b43_new_kidx_api(dev)) {
1107 pairwise_keys_start = B43_NR_GROUP_KEYS;
1108 count = B43_NR_GROUP_KEYS + B43_NR_PAIRWISE_KEYS;
1109 } else {
1110 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
1111 count = B43_NR_GROUP_KEYS * 2 + B43_NR_PAIRWISE_KEYS;
1112 }
1113 for (index = 0; index < count; index++) {
Michael Buesch9cf7f242008-12-19 20:24:30 +01001114 key = &(dev->key[index]);
1115 printk(KERN_DEBUG "Key slot %02u: %s",
1116 index, (key->keyconf == NULL) ? " " : "*");
1117 offset = dev->ktp + (index * B43_SEC_KEYSIZE);
1118 for (i = 0; i < B43_SEC_KEYSIZE; i += 2) {
1119 u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, offset + i);
1120 printk("%02X%02X", (tmp & 0xFF), ((tmp >> 8) & 0xFF));
1121 }
1122
1123 algo = b43_shm_read16(dev, B43_SHM_SHARED,
1124 B43_SHM_SH_KEYIDXBLOCK + (index * 2));
1125 printk(" Algo: %04X/%02X", algo, key->algorithm);
1126
Michael Buesch66d2d082009-08-06 10:36:50 +02001127 if (index >= pairwise_keys_start) {
gregor kowski035d0242009-08-19 22:35:45 +02001128 if (key->algorithm == B43_SEC_ALGO_TKIP) {
1129 printk(" TKIP: ");
1130 offset = B43_SHM_SH_TKIPTSCTTAK + (index - 4) * (10 + 4);
1131 for (i = 0; i < 14; i += 2) {
1132 u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, offset + i);
1133 printk("%02X%02X", (tmp & 0xFF), ((tmp >> 8) & 0xFF));
1134 }
1135 }
Michael Buesch9cf7f242008-12-19 20:24:30 +01001136 rcmta0 = b43_shm_read32(dev, B43_SHM_RCMTA,
Michael Buesch66d2d082009-08-06 10:36:50 +02001137 ((index - pairwise_keys_start) * 2) + 0);
Michael Buesch9cf7f242008-12-19 20:24:30 +01001138 rcmta1 = b43_shm_read16(dev, B43_SHM_RCMTA,
Michael Buesch66d2d082009-08-06 10:36:50 +02001139 ((index - pairwise_keys_start) * 2) + 1);
Michael Buesch9cf7f242008-12-19 20:24:30 +01001140 *((__le32 *)(&mac[0])) = cpu_to_le32(rcmta0);
1141 *((__le16 *)(&mac[4])) = cpu_to_le16(rcmta1);
Johannes Berge91d8332009-07-15 17:21:41 +02001142 printk(" MAC: %pM", mac);
Michael Buesch9cf7f242008-12-19 20:24:30 +01001143 } else
1144 printk(" DEFAULT KEY");
1145 printk("\n");
1146 }
1147}
1148
Michael Buesche4d6b792007-09-18 15:39:42 -04001149void b43_power_saving_ctl_bits(struct b43_wldev *dev, unsigned int ps_flags)
1150{
1151 u32 macctl;
1152 u16 ucstat;
1153 bool hwps;
1154 bool awake;
1155 int i;
1156
1157 B43_WARN_ON((ps_flags & B43_PS_ENABLED) &&
1158 (ps_flags & B43_PS_DISABLED));
1159 B43_WARN_ON((ps_flags & B43_PS_AWAKE) && (ps_flags & B43_PS_ASLEEP));
1160
1161 if (ps_flags & B43_PS_ENABLED) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00001162 hwps = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001163 } else if (ps_flags & B43_PS_DISABLED) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00001164 hwps = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04001165 } else {
1166 //TODO: If powersave is not off and FIXME is not set and we are not in adhoc
1167 // and thus is not an AP and we are associated, set bit 25
1168 }
1169 if (ps_flags & B43_PS_AWAKE) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00001170 awake = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001171 } else if (ps_flags & B43_PS_ASLEEP) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00001172 awake = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04001173 } else {
1174 //TODO: If the device is awake or this is an AP, or we are scanning, or FIXME,
1175 // or we are associated, or FIXME, or the latest PS-Poll packet sent was
1176 // successful, set bit26
1177 }
1178
1179/* FIXME: For now we force awake-on and hwps-off */
Rusty Russell3db1cd52011-12-19 13:56:45 +00001180 hwps = false;
1181 awake = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001182
1183 macctl = b43_read32(dev, B43_MMIO_MACCTL);
1184 if (hwps)
1185 macctl |= B43_MACCTL_HWPS;
1186 else
1187 macctl &= ~B43_MACCTL_HWPS;
1188 if (awake)
1189 macctl |= B43_MACCTL_AWAKE;
1190 else
1191 macctl &= ~B43_MACCTL_AWAKE;
1192 b43_write32(dev, B43_MMIO_MACCTL, macctl);
1193 /* Commit write */
1194 b43_read32(dev, B43_MMIO_MACCTL);
Rafał Miłecki21d889d2011-05-18 02:06:38 +02001195 if (awake && dev->dev->core_rev >= 5) {
Michael Buesche4d6b792007-09-18 15:39:42 -04001196 /* Wait for the microcode to wake up. */
1197 for (i = 0; i < 100; i++) {
1198 ucstat = b43_shm_read16(dev, B43_SHM_SHARED,
1199 B43_SHM_SH_UCODESTAT);
1200 if (ucstat != B43_SHM_SH_UCODESTAT_SLEEP)
1201 break;
1202 udelay(10);
1203 }
1204 }
1205}
1206
Rafał Miłecki737f6572014-09-12 18:37:26 +02001207/* http://bcm-v4.sipsolutions.net/802.11/PHY/BmacCorePllReset */
1208void b43_wireless_core_phy_pll_reset(struct b43_wldev *dev)
1209{
1210 struct bcma_drv_cc *bcma_cc __maybe_unused;
1211 struct ssb_chipcommon *ssb_cc __maybe_unused;
1212
1213 switch (dev->dev->bus_type) {
1214#ifdef CONFIG_B43_BCMA
1215 case B43_BUS_BCMA:
1216 bcma_cc = &dev->dev->bdev->bus->drv_cc;
1217
1218 bcma_cc_write32(bcma_cc, BCMA_CC_CHIPCTL_ADDR, 0);
1219 bcma_cc_mask32(bcma_cc, BCMA_CC_CHIPCTL_DATA, ~0x4);
1220 bcma_cc_set32(bcma_cc, BCMA_CC_CHIPCTL_DATA, 0x4);
1221 bcma_cc_mask32(bcma_cc, BCMA_CC_CHIPCTL_DATA, ~0x4);
1222 break;
1223#endif
1224#ifdef CONFIG_B43_SSB
1225 case B43_BUS_SSB:
1226 ssb_cc = &dev->dev->sdev->bus->chipco;
1227
1228 chipco_write32(ssb_cc, SSB_CHIPCO_CHIPCTL_ADDR, 0);
1229 chipco_mask32(ssb_cc, SSB_CHIPCO_CHIPCTL_DATA, ~0x4);
1230 chipco_set32(ssb_cc, SSB_CHIPCO_CHIPCTL_DATA, 0x4);
1231 chipco_mask32(ssb_cc, SSB_CHIPCO_CHIPCTL_DATA, ~0x4);
1232 break;
1233#endif
1234 }
1235}
1236
Rafał Miłecki42c9a452011-07-06 15:45:27 +02001237#ifdef CONFIG_B43_BCMA
Rafał Miłecki49173592011-07-17 01:06:06 +02001238static void b43_bcma_phy_reset(struct b43_wldev *dev)
1239{
1240 u32 flags;
1241
1242 /* Put PHY into reset */
1243 flags = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
1244 flags |= B43_BCMA_IOCTL_PHY_RESET;
1245 flags |= B43_BCMA_IOCTL_PHY_BW_20MHZ; /* Make 20 MHz def */
1246 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, flags);
1247 udelay(2);
1248
Rafał Miłecki50c1b592014-05-17 23:24:55 +02001249 b43_phy_take_out_of_reset(dev);
Rafał Miłecki49173592011-07-17 01:06:06 +02001250}
1251
Rafał Miłecki42c9a452011-07-06 15:45:27 +02001252static void b43_bcma_wireless_core_reset(struct b43_wldev *dev, bool gmode)
1253{
Rafał Miłecki88cceab2013-02-26 10:07:57 +01001254 u32 req = B43_BCMA_CLKCTLST_80211_PLL_REQ |
1255 B43_BCMA_CLKCTLST_PHY_PLL_REQ;
1256 u32 status = B43_BCMA_CLKCTLST_80211_PLL_ST |
1257 B43_BCMA_CLKCTLST_PHY_PLL_ST;
Rafał Miłecki6b9e03e2014-04-22 13:54:35 +02001258 u32 flags;
Rafał Miłecki88cceab2013-02-26 10:07:57 +01001259
Rafał Miłecki6b9e03e2014-04-22 13:54:35 +02001260 flags = B43_BCMA_IOCTL_PHY_CLKEN;
1261 if (gmode)
1262 flags |= B43_BCMA_IOCTL_GMODE;
1263 b43_device_enable(dev, flags);
1264
Rafał Miłecki49173592011-07-17 01:06:06 +02001265 bcma_core_set_clockmode(dev->dev->bdev, BCMA_CLKMODE_FAST);
1266 b43_bcma_phy_reset(dev);
Rafał Miłecki88cceab2013-02-26 10:07:57 +01001267 bcma_core_pll_ctl(dev->dev->bdev, req, status, true);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02001268}
1269#endif
1270
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02001271#ifdef CONFIG_B43_SSB
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001272static void b43_ssb_wireless_core_reset(struct b43_wldev *dev, bool gmode)
Michael Buesche4d6b792007-09-18 15:39:42 -04001273{
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001274 u32 flags = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04001275
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001276 if (gmode)
1277 flags |= B43_TMSLOW_GMODE;
Michael Buesche4d6b792007-09-18 15:39:42 -04001278 flags |= B43_TMSLOW_PHYCLKEN;
1279 flags |= B43_TMSLOW_PHYRESET;
Rafał Miłecki42ab1352010-12-09 20:56:01 +01001280 if (dev->phy.type == B43_PHYTYPE_N)
1281 flags |= B43_TMSLOW_PHY_BANDWIDTH_20MHZ; /* Make 20 MHz def */
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02001282 b43_device_enable(dev, flags);
Michael Buesche4d6b792007-09-18 15:39:42 -04001283 msleep(2); /* Wait for the PLL to turn on. */
1284
Rafał Miłecki50c1b592014-05-17 23:24:55 +02001285 b43_phy_take_out_of_reset(dev);
Rafał Miłecki14952982011-05-17 18:57:28 +02001286}
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02001287#endif
Rafał Miłecki14952982011-05-17 18:57:28 +02001288
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001289void b43_wireless_core_reset(struct b43_wldev *dev, bool gmode)
Rafał Miłecki14952982011-05-17 18:57:28 +02001290{
1291 u32 macctl;
1292
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02001293 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02001294#ifdef CONFIG_B43_BCMA
1295 case B43_BUS_BCMA:
1296 b43_bcma_wireless_core_reset(dev, gmode);
1297 break;
1298#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02001299#ifdef CONFIG_B43_SSB
1300 case B43_BUS_SSB:
1301 b43_ssb_wireless_core_reset(dev, gmode);
1302 break;
1303#endif
1304 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001305
Michael Bueschfb111372008-09-02 13:00:34 +02001306 /* Turn Analog ON, but only if we already know the PHY-type.
1307 * This protects against very early setup where we don't know the
1308 * PHY-type, yet. wireless_core_reset will be called once again later,
1309 * when we know the PHY-type. */
1310 if (dev->phy.ops)
Michael Bueschcb24f572008-09-03 12:12:20 +02001311 dev->phy.ops->switch_analog(dev, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04001312
1313 macctl = b43_read32(dev, B43_MMIO_MACCTL);
1314 macctl &= ~B43_MACCTL_GMODE;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001315 if (gmode)
Michael Buesche4d6b792007-09-18 15:39:42 -04001316 macctl |= B43_MACCTL_GMODE;
1317 macctl |= B43_MACCTL_IHR_ENABLED;
1318 b43_write32(dev, B43_MMIO_MACCTL, macctl);
1319}
1320
1321static void handle_irq_transmit_status(struct b43_wldev *dev)
1322{
1323 u32 v0, v1;
1324 u16 tmp;
1325 struct b43_txstatus stat;
1326
1327 while (1) {
1328 v0 = b43_read32(dev, B43_MMIO_XMITSTAT_0);
1329 if (!(v0 & 0x00000001))
1330 break;
1331 v1 = b43_read32(dev, B43_MMIO_XMITSTAT_1);
1332
1333 stat.cookie = (v0 >> 16);
1334 stat.seq = (v1 & 0x0000FFFF);
1335 stat.phy_stat = ((v1 & 0x00FF0000) >> 16);
1336 tmp = (v0 & 0x0000FFFF);
1337 stat.frame_count = ((tmp & 0xF000) >> 12);
1338 stat.rts_count = ((tmp & 0x0F00) >> 8);
1339 stat.supp_reason = ((tmp & 0x001C) >> 2);
1340 stat.pm_indicated = !!(tmp & 0x0080);
1341 stat.intermediate = !!(tmp & 0x0040);
1342 stat.for_ampdu = !!(tmp & 0x0020);
1343 stat.acked = !!(tmp & 0x0002);
1344
1345 b43_handle_txstatus(dev, &stat);
1346 }
1347}
1348
1349static void drain_txstatus_queue(struct b43_wldev *dev)
1350{
1351 u32 dummy;
1352
Rafał Miłecki21d889d2011-05-18 02:06:38 +02001353 if (dev->dev->core_rev < 5)
Michael Buesche4d6b792007-09-18 15:39:42 -04001354 return;
1355 /* Read all entries from the microcode TXstatus FIFO
1356 * and throw them away.
1357 */
1358 while (1) {
1359 dummy = b43_read32(dev, B43_MMIO_XMITSTAT_0);
1360 if (!(dummy & 0x00000001))
1361 break;
1362 dummy = b43_read32(dev, B43_MMIO_XMITSTAT_1);
1363 }
1364}
1365
1366static u32 b43_jssi_read(struct b43_wldev *dev)
1367{
1368 u32 val = 0;
1369
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001370 val = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_JSSI1);
Michael Buesche4d6b792007-09-18 15:39:42 -04001371 val <<= 16;
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001372 val |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_JSSI0);
Michael Buesche4d6b792007-09-18 15:39:42 -04001373
1374 return val;
1375}
1376
1377static void b43_jssi_write(struct b43_wldev *dev, u32 jssi)
1378{
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001379 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_JSSI0,
1380 (jssi & 0x0000FFFF));
1381 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_JSSI1,
1382 (jssi & 0xFFFF0000) >> 16);
Michael Buesche4d6b792007-09-18 15:39:42 -04001383}
1384
1385static void b43_generate_noise_sample(struct b43_wldev *dev)
1386{
1387 b43_jssi_write(dev, 0x7F7F7F7F);
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01001388 b43_write32(dev, B43_MMIO_MACCMD,
1389 b43_read32(dev, B43_MMIO_MACCMD) | B43_MACCMD_BGNOISE);
Michael Buesche4d6b792007-09-18 15:39:42 -04001390}
1391
1392static void b43_calculate_link_quality(struct b43_wldev *dev)
1393{
1394 /* Top half of Link Quality calculation. */
1395
Michael Bueschef1a6282008-08-27 18:53:02 +02001396 if (dev->phy.type != B43_PHYTYPE_G)
1397 return;
Michael Buesche4d6b792007-09-18 15:39:42 -04001398 if (dev->noisecalc.calculation_running)
1399 return;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001400 dev->noisecalc.calculation_running = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001401 dev->noisecalc.nr_samples = 0;
1402
1403 b43_generate_noise_sample(dev);
1404}
1405
1406static void handle_irq_noise(struct b43_wldev *dev)
1407{
Michael Bueschef1a6282008-08-27 18:53:02 +02001408 struct b43_phy_g *phy = dev->phy.g;
Michael Buesche4d6b792007-09-18 15:39:42 -04001409 u16 tmp;
1410 u8 noise[4];
1411 u8 i, j;
1412 s32 average;
1413
1414 /* Bottom half of Link Quality calculation. */
1415
Michael Bueschef1a6282008-08-27 18:53:02 +02001416 if (dev->phy.type != B43_PHYTYPE_G)
1417 return;
1418
Michael Buesch98a3b2f2008-06-12 12:36:29 +02001419 /* Possible race condition: It might be possible that the user
1420 * changed to a different channel in the meantime since we
1421 * started the calculation. We ignore that fact, since it's
1422 * not really that much of a problem. The background noise is
1423 * an estimation only anyway. Slightly wrong results will get damped
1424 * by the averaging of the 8 sample rounds. Additionally the
1425 * value is shortlived. So it will be replaced by the next noise
1426 * calculation round soon. */
1427
Michael Buesche4d6b792007-09-18 15:39:42 -04001428 B43_WARN_ON(!dev->noisecalc.calculation_running);
Michael Buesch1a094042007-09-20 11:13:40 -07001429 *((__le32 *)noise) = cpu_to_le32(b43_jssi_read(dev));
Michael Buesche4d6b792007-09-18 15:39:42 -04001430 if (noise[0] == 0x7F || noise[1] == 0x7F ||
1431 noise[2] == 0x7F || noise[3] == 0x7F)
1432 goto generate_new;
1433
1434 /* Get the noise samples. */
1435 B43_WARN_ON(dev->noisecalc.nr_samples >= 8);
1436 i = dev->noisecalc.nr_samples;
Harvey Harrisoncdbf0842008-05-02 13:47:48 -07001437 noise[0] = clamp_val(noise[0], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1438 noise[1] = clamp_val(noise[1], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1439 noise[2] = clamp_val(noise[2], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1440 noise[3] = clamp_val(noise[3], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04001441 dev->noisecalc.samples[i][0] = phy->nrssi_lt[noise[0]];
1442 dev->noisecalc.samples[i][1] = phy->nrssi_lt[noise[1]];
1443 dev->noisecalc.samples[i][2] = phy->nrssi_lt[noise[2]];
1444 dev->noisecalc.samples[i][3] = phy->nrssi_lt[noise[3]];
1445 dev->noisecalc.nr_samples++;
1446 if (dev->noisecalc.nr_samples == 8) {
1447 /* Calculate the Link Quality by the noise samples. */
1448 average = 0;
1449 for (i = 0; i < 8; i++) {
1450 for (j = 0; j < 4; j++)
1451 average += dev->noisecalc.samples[i][j];
1452 }
1453 average /= (8 * 4);
1454 average *= 125;
1455 average += 64;
1456 average /= 128;
1457 tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x40C);
1458 tmp = (tmp / 128) & 0x1F;
1459 if (tmp >= 8)
1460 average += 2;
1461 else
1462 average -= 25;
1463 if (tmp == 8)
1464 average -= 72;
1465 else
1466 average -= 48;
1467
1468 dev->stats.link_noise = average;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001469 dev->noisecalc.calculation_running = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04001470 return;
1471 }
Michael Buesch98a3b2f2008-06-12 12:36:29 +02001472generate_new:
Michael Buesche4d6b792007-09-18 15:39:42 -04001473 b43_generate_noise_sample(dev);
1474}
1475
1476static void handle_irq_tbtt_indication(struct b43_wldev *dev)
1477{
Johannes Berg05c914f2008-09-11 00:01:58 +02001478 if (b43_is_mode(dev->wl, NL80211_IFTYPE_AP)) {
Michael Buesche4d6b792007-09-18 15:39:42 -04001479 ///TODO: PS TBTT
1480 } else {
1481 if (1 /*FIXME: the last PSpoll frame was sent successfully */ )
1482 b43_power_saving_ctl_bits(dev, 0);
1483 }
Johannes Berg05c914f2008-09-11 00:01:58 +02001484 if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC))
Rusty Russell3db1cd52011-12-19 13:56:45 +00001485 dev->dfq_valid = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001486}
1487
1488static void handle_irq_atim_end(struct b43_wldev *dev)
1489{
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01001490 if (dev->dfq_valid) {
1491 b43_write32(dev, B43_MMIO_MACCMD,
1492 b43_read32(dev, B43_MMIO_MACCMD)
1493 | B43_MACCMD_DFQ_VALID);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001494 dev->dfq_valid = false;
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01001495 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001496}
1497
1498static void handle_irq_pmq(struct b43_wldev *dev)
1499{
1500 u32 tmp;
1501
1502 //TODO: AP mode.
1503
1504 while (1) {
1505 tmp = b43_read32(dev, B43_MMIO_PS_STATUS);
1506 if (!(tmp & 0x00000008))
1507 break;
1508 }
1509 /* 16bit write is odd, but correct. */
1510 b43_write16(dev, B43_MMIO_PS_STATUS, 0x0002);
1511}
1512
1513static void b43_write_template_common(struct b43_wldev *dev,
John Daiker99da1852009-02-24 02:16:42 -08001514 const u8 *data, u16 size,
Michael Buesche4d6b792007-09-18 15:39:42 -04001515 u16 ram_offset,
1516 u16 shm_size_offset, u8 rate)
1517{
1518 u32 i, tmp;
1519 struct b43_plcp_hdr4 plcp;
1520
1521 plcp.data = 0;
1522 b43_generate_plcp_hdr(&plcp, size + FCS_LEN, rate);
1523 b43_ram_write(dev, ram_offset, le32_to_cpu(plcp.data));
1524 ram_offset += sizeof(u32);
1525 /* The PLCP is 6 bytes long, but we only wrote 4 bytes, yet.
1526 * So leave the first two bytes of the next write blank.
1527 */
1528 tmp = (u32) (data[0]) << 16;
1529 tmp |= (u32) (data[1]) << 24;
1530 b43_ram_write(dev, ram_offset, tmp);
1531 ram_offset += sizeof(u32);
1532 for (i = 2; i < size; i += sizeof(u32)) {
1533 tmp = (u32) (data[i + 0]);
1534 if (i + 1 < size)
1535 tmp |= (u32) (data[i + 1]) << 8;
1536 if (i + 2 < size)
1537 tmp |= (u32) (data[i + 2]) << 16;
1538 if (i + 3 < size)
1539 tmp |= (u32) (data[i + 3]) << 24;
1540 b43_ram_write(dev, ram_offset + i - 2, tmp);
1541 }
1542 b43_shm_write16(dev, B43_SHM_SHARED, shm_size_offset,
1543 size + sizeof(struct b43_plcp_hdr6));
1544}
1545
Michael Buesch5042c502008-04-05 15:05:00 +02001546/* Check if the use of the antenna that ieee80211 told us to
1547 * use is possible. This will fall back to DEFAULT.
1548 * "antenna_nr" is the antenna identifier we got from ieee80211. */
1549u8 b43_ieee80211_antenna_sanitize(struct b43_wldev *dev,
1550 u8 antenna_nr)
1551{
1552 u8 antenna_mask;
1553
1554 if (antenna_nr == 0) {
1555 /* Zero means "use default antenna". That's always OK. */
1556 return 0;
1557 }
1558
1559 /* Get the mask of available antennas. */
1560 if (dev->phy.gmode)
Rafał Miłecki05814832011-05-18 02:06:39 +02001561 antenna_mask = dev->dev->bus_sprom->ant_available_bg;
Michael Buesch5042c502008-04-05 15:05:00 +02001562 else
Rafał Miłecki05814832011-05-18 02:06:39 +02001563 antenna_mask = dev->dev->bus_sprom->ant_available_a;
Michael Buesch5042c502008-04-05 15:05:00 +02001564
1565 if (!(antenna_mask & (1 << (antenna_nr - 1)))) {
1566 /* This antenna is not available. Fall back to default. */
1567 return 0;
1568 }
1569
1570 return antenna_nr;
1571}
1572
Michael Buesch5042c502008-04-05 15:05:00 +02001573/* Convert a b43 antenna number value to the PHY TX control value. */
1574static u16 b43_antenna_to_phyctl(int antenna)
1575{
1576 switch (antenna) {
1577 case B43_ANTENNA0:
1578 return B43_TXH_PHY_ANT0;
1579 case B43_ANTENNA1:
1580 return B43_TXH_PHY_ANT1;
1581 case B43_ANTENNA2:
1582 return B43_TXH_PHY_ANT2;
1583 case B43_ANTENNA3:
1584 return B43_TXH_PHY_ANT3;
Gábor Stefanik64e368b2009-08-27 22:49:49 +02001585 case B43_ANTENNA_AUTO0:
1586 case B43_ANTENNA_AUTO1:
Michael Buesch5042c502008-04-05 15:05:00 +02001587 return B43_TXH_PHY_ANT01AUTO;
1588 }
1589 B43_WARN_ON(1);
1590 return 0;
1591}
1592
Michael Buesche4d6b792007-09-18 15:39:42 -04001593static void b43_write_beacon_template(struct b43_wldev *dev,
1594 u16 ram_offset,
Michael Buesch5042c502008-04-05 15:05:00 +02001595 u16 shm_size_offset)
Michael Buesche4d6b792007-09-18 15:39:42 -04001596{
Michael Buesch47f76ca2007-12-27 22:15:11 +01001597 unsigned int i, len, variable_len;
Michael Buesche66fee62007-12-26 17:47:10 +01001598 const struct ieee80211_mgmt *bcn;
1599 const u8 *ie;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001600 bool tim_found = false;
Michael Buesch5042c502008-04-05 15:05:00 +02001601 unsigned int rate;
1602 u16 ctl;
1603 int antenna;
Johannes Berge039fa42008-05-15 12:55:29 +02001604 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(dev->wl->current_beacon);
Michael Buesche4d6b792007-09-18 15:39:42 -04001605
Michael Buesche66fee62007-12-26 17:47:10 +01001606 bcn = (const struct ieee80211_mgmt *)(dev->wl->current_beacon->data);
Silvan Jegenc8e49552014-02-25 18:12:52 +01001607 len = min_t(size_t, dev->wl->current_beacon->len,
Michael Buesche4d6b792007-09-18 15:39:42 -04001608 0x200 - sizeof(struct b43_plcp_hdr6));
Johannes Berge039fa42008-05-15 12:55:29 +02001609 rate = ieee80211_get_tx_rate(dev->wl->hw, info)->hw_value;
Michael Buesche66fee62007-12-26 17:47:10 +01001610
1611 b43_write_template_common(dev, (const u8 *)bcn,
Michael Buesche4d6b792007-09-18 15:39:42 -04001612 len, ram_offset, shm_size_offset, rate);
Michael Buesche66fee62007-12-26 17:47:10 +01001613
Michael Buesch5042c502008-04-05 15:05:00 +02001614 /* Write the PHY TX control parameters. */
Johannes Berg0f4ac382008-10-09 12:18:04 +02001615 antenna = B43_ANTENNA_DEFAULT;
Michael Buesch5042c502008-04-05 15:05:00 +02001616 antenna = b43_antenna_to_phyctl(antenna);
1617 ctl = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL);
1618 /* We can't send beacons with short preamble. Would get PHY errors. */
1619 ctl &= ~B43_TXH_PHY_SHORTPRMBL;
1620 ctl &= ~B43_TXH_PHY_ANT;
1621 ctl &= ~B43_TXH_PHY_ENC;
1622 ctl |= antenna;
1623 if (b43_is_cck_rate(rate))
1624 ctl |= B43_TXH_PHY_ENC_CCK;
1625 else
1626 ctl |= B43_TXH_PHY_ENC_OFDM;
1627 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, ctl);
1628
Michael Buesche66fee62007-12-26 17:47:10 +01001629 /* Find the position of the TIM and the DTIM_period value
1630 * and write them to SHM. */
1631 ie = bcn->u.beacon.variable;
Michael Buesch47f76ca2007-12-27 22:15:11 +01001632 variable_len = len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
1633 for (i = 0; i < variable_len - 2; ) {
Michael Buesche66fee62007-12-26 17:47:10 +01001634 uint8_t ie_id, ie_len;
1635
1636 ie_id = ie[i];
1637 ie_len = ie[i + 1];
1638 if (ie_id == 5) {
1639 u16 tim_position;
1640 u16 dtim_period;
1641 /* This is the TIM Information Element */
1642
1643 /* Check whether the ie_len is in the beacon data range. */
Michael Buesch47f76ca2007-12-27 22:15:11 +01001644 if (variable_len < ie_len + 2 + i)
Michael Buesche66fee62007-12-26 17:47:10 +01001645 break;
1646 /* A valid TIM is at least 4 bytes long. */
1647 if (ie_len < 4)
1648 break;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001649 tim_found = true;
Michael Buesche66fee62007-12-26 17:47:10 +01001650
1651 tim_position = sizeof(struct b43_plcp_hdr6);
1652 tim_position += offsetof(struct ieee80211_mgmt, u.beacon.variable);
1653 tim_position += i;
1654
1655 dtim_period = ie[i + 3];
1656
1657 b43_shm_write16(dev, B43_SHM_SHARED,
1658 B43_SHM_SH_TIMBPOS, tim_position);
1659 b43_shm_write16(dev, B43_SHM_SHARED,
1660 B43_SHM_SH_DTIMPER, dtim_period);
1661 break;
1662 }
1663 i += ie_len + 2;
1664 }
1665 if (!tim_found) {
Johannes Berg04dea132008-05-20 12:10:49 +02001666 /*
1667 * If ucode wants to modify TIM do it behind the beacon, this
1668 * will happen, for example, when doing mesh networking.
1669 */
1670 b43_shm_write16(dev, B43_SHM_SHARED,
1671 B43_SHM_SH_TIMBPOS,
1672 len + sizeof(struct b43_plcp_hdr6));
1673 b43_shm_write16(dev, B43_SHM_SHARED,
1674 B43_SHM_SH_DTIMPER, 0);
1675 }
1676 b43dbg(dev->wl, "Updated beacon template at 0x%x\n", ram_offset);
Michael Buesche4d6b792007-09-18 15:39:42 -04001677}
1678
Michael Buesch6b4bec02008-05-20 12:16:28 +02001679static void b43_upload_beacon0(struct b43_wldev *dev)
1680{
1681 struct b43_wl *wl = dev->wl;
1682
1683 if (wl->beacon0_uploaded)
1684 return;
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001685 b43_write_beacon_template(dev, B43_SHM_SH_BT_BASE0, B43_SHM_SH_BTL0);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001686 wl->beacon0_uploaded = true;
Michael Buesch6b4bec02008-05-20 12:16:28 +02001687}
1688
1689static void b43_upload_beacon1(struct b43_wldev *dev)
1690{
1691 struct b43_wl *wl = dev->wl;
1692
1693 if (wl->beacon1_uploaded)
1694 return;
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001695 b43_write_beacon_template(dev, B43_SHM_SH_BT_BASE1, B43_SHM_SH_BTL1);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001696 wl->beacon1_uploaded = true;
Michael Buesch6b4bec02008-05-20 12:16:28 +02001697}
1698
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001699static void handle_irq_beacon(struct b43_wldev *dev)
1700{
1701 struct b43_wl *wl = dev->wl;
1702 u32 cmd, beacon0_valid, beacon1_valid;
1703
Johannes Berg05c914f2008-09-11 00:01:58 +02001704 if (!b43_is_mode(wl, NL80211_IFTYPE_AP) &&
Manual Munz8c235162011-09-18 18:24:03 -05001705 !b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT) &&
1706 !b43_is_mode(wl, NL80211_IFTYPE_ADHOC))
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001707 return;
1708
1709 /* This is the bottom half of the asynchronous beacon update. */
1710
1711 /* Ignore interrupt in the future. */
Michael Buesch13790722009-04-08 21:26:27 +02001712 dev->irq_mask &= ~B43_IRQ_BEACON;
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001713
1714 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1715 beacon0_valid = (cmd & B43_MACCMD_BEACON0_VALID);
1716 beacon1_valid = (cmd & B43_MACCMD_BEACON1_VALID);
1717
1718 /* Schedule interrupt manually, if busy. */
1719 if (beacon0_valid && beacon1_valid) {
1720 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_BEACON);
Michael Buesch13790722009-04-08 21:26:27 +02001721 dev->irq_mask |= B43_IRQ_BEACON;
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001722 return;
1723 }
1724
Michael Buesch6b4bec02008-05-20 12:16:28 +02001725 if (unlikely(wl->beacon_templates_virgin)) {
1726 /* We never uploaded a beacon before.
1727 * Upload both templates now, but only mark one valid. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00001728 wl->beacon_templates_virgin = false;
Michael Buesch6b4bec02008-05-20 12:16:28 +02001729 b43_upload_beacon0(dev);
1730 b43_upload_beacon1(dev);
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001731 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1732 cmd |= B43_MACCMD_BEACON0_VALID;
1733 b43_write32(dev, B43_MMIO_MACCMD, cmd);
Michael Buesch6b4bec02008-05-20 12:16:28 +02001734 } else {
1735 if (!beacon0_valid) {
1736 b43_upload_beacon0(dev);
1737 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1738 cmd |= B43_MACCMD_BEACON0_VALID;
1739 b43_write32(dev, B43_MMIO_MACCMD, cmd);
1740 } else if (!beacon1_valid) {
1741 b43_upload_beacon1(dev);
1742 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1743 cmd |= B43_MACCMD_BEACON1_VALID;
1744 b43_write32(dev, B43_MMIO_MACCMD, cmd);
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001745 }
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001746 }
1747}
1748
Michael Buesch36dbd952009-09-04 22:51:29 +02001749static void b43_do_beacon_update_trigger_work(struct b43_wldev *dev)
1750{
1751 u32 old_irq_mask = dev->irq_mask;
1752
1753 /* update beacon right away or defer to irq */
1754 handle_irq_beacon(dev);
1755 if (old_irq_mask != dev->irq_mask) {
1756 /* The handler updated the IRQ mask. */
1757 B43_WARN_ON(!dev->irq_mask);
1758 if (b43_read32(dev, B43_MMIO_GEN_IRQ_MASK)) {
1759 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
1760 } else {
1761 /* Device interrupts are currently disabled. That means
1762 * we just ran the hardirq handler and scheduled the
1763 * IRQ thread. The thread will write the IRQ mask when
1764 * it finished, so there's nothing to do here. Writing
1765 * the mask _here_ would incorrectly re-enable IRQs. */
1766 }
1767 }
1768}
1769
Michael Buescha82d9922008-04-04 21:40:06 +02001770static void b43_beacon_update_trigger_work(struct work_struct *work)
1771{
1772 struct b43_wl *wl = container_of(work, struct b43_wl,
1773 beacon_update_trigger);
1774 struct b43_wldev *dev;
1775
1776 mutex_lock(&wl->mutex);
1777 dev = wl->current_dev;
1778 if (likely(dev && (b43_status(dev) >= B43_STAT_INITIALIZED))) {
Rafał Miłecki505fb012011-05-19 15:11:27 +02001779 if (b43_bus_host_is_sdio(dev->dev)) {
Michael Buesch36dbd952009-09-04 22:51:29 +02001780 /* wl->mutex is enough. */
1781 b43_do_beacon_update_trigger_work(dev);
1782 mmiowb();
1783 } else {
1784 spin_lock_irq(&wl->hardirq_lock);
1785 b43_do_beacon_update_trigger_work(dev);
1786 mmiowb();
1787 spin_unlock_irq(&wl->hardirq_lock);
1788 }
Michael Buescha82d9922008-04-04 21:40:06 +02001789 }
1790 mutex_unlock(&wl->mutex);
1791}
1792
Michael Bueschd4df6f1a2007-12-26 18:04:14 +01001793/* Asynchronously update the packet templates in template RAM.
Michael Buesch36dbd952009-09-04 22:51:29 +02001794 * Locking: Requires wl->mutex to be locked. */
Johannes Berg9d139c82008-07-09 14:40:37 +02001795static void b43_update_templates(struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04001796{
Johannes Berg9d139c82008-07-09 14:40:37 +02001797 struct sk_buff *beacon;
1798
Michael Buesche66fee62007-12-26 17:47:10 +01001799 /* This is the top half of the ansynchronous beacon update.
1800 * The bottom half is the beacon IRQ.
1801 * Beacon update must be asynchronous to avoid sending an
1802 * invalid beacon. This can happen for example, if the firmware
1803 * transmits a beacon while we are updating it. */
Michael Buesche4d6b792007-09-18 15:39:42 -04001804
Johannes Berg9d139c82008-07-09 14:40:37 +02001805 /* We could modify the existing beacon and set the aid bit in
1806 * the TIM field, but that would probably require resizing and
1807 * moving of data within the beacon template.
1808 * Simply request a new beacon and let mac80211 do the hard work. */
1809 beacon = ieee80211_beacon_get(wl->hw, wl->vif);
1810 if (unlikely(!beacon))
1811 return;
1812
Michael Buesche66fee62007-12-26 17:47:10 +01001813 if (wl->current_beacon)
1814 dev_kfree_skb_any(wl->current_beacon);
1815 wl->current_beacon = beacon;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001816 wl->beacon0_uploaded = false;
1817 wl->beacon1_uploaded = false;
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001818 ieee80211_queue_work(wl->hw, &wl->beacon_update_trigger);
Michael Buesche4d6b792007-09-18 15:39:42 -04001819}
1820
Michael Buesche4d6b792007-09-18 15:39:42 -04001821static void b43_set_beacon_int(struct b43_wldev *dev, u16 beacon_int)
1822{
1823 b43_time_lock(dev);
Rafał Miłecki21d889d2011-05-18 02:06:38 +02001824 if (dev->dev->core_rev >= 3) {
Michael Buescha82d9922008-04-04 21:40:06 +02001825 b43_write32(dev, B43_MMIO_TSF_CFP_REP, (beacon_int << 16));
1826 b43_write32(dev, B43_MMIO_TSF_CFP_START, (beacon_int << 10));
Michael Buesche4d6b792007-09-18 15:39:42 -04001827 } else {
1828 b43_write16(dev, 0x606, (beacon_int >> 6));
1829 b43_write16(dev, 0x610, beacon_int);
1830 }
1831 b43_time_unlock(dev);
Michael Buescha82d9922008-04-04 21:40:06 +02001832 b43dbg(dev->wl, "Set beacon interval to %u\n", beacon_int);
Michael Buesche4d6b792007-09-18 15:39:42 -04001833}
1834
Michael Bueschafa83e22008-05-19 23:51:37 +02001835static void b43_handle_firmware_panic(struct b43_wldev *dev)
1836{
1837 u16 reason;
1838
1839 /* Read the register that contains the reason code for the panic. */
1840 reason = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_FWPANIC_REASON_REG);
1841 b43err(dev->wl, "Whoopsy, firmware panic! Reason: %u\n", reason);
1842
1843 switch (reason) {
1844 default:
1845 b43dbg(dev->wl, "The panic reason is unknown.\n");
1846 /* fallthrough */
1847 case B43_FWPANIC_DIE:
1848 /* Do not restart the controller or firmware.
1849 * The device is nonfunctional from now on.
1850 * Restarting would result in this panic to trigger again,
1851 * so we avoid that recursion. */
1852 break;
1853 case B43_FWPANIC_RESTART:
1854 b43_controller_restart(dev, "Microcode panic");
1855 break;
1856 }
1857}
1858
Michael Buesche4d6b792007-09-18 15:39:42 -04001859static void handle_irq_ucode_debug(struct b43_wldev *dev)
1860{
Michael Buesche48b0ee2008-05-17 22:44:35 +02001861 unsigned int i, cnt;
Michael Buesch53c06852008-05-20 00:24:36 +02001862 u16 reason, marker_id, marker_line;
Michael Buesche48b0ee2008-05-17 22:44:35 +02001863 __le16 *buf;
1864
1865 /* The proprietary firmware doesn't have this IRQ. */
1866 if (!dev->fw.opensource)
1867 return;
1868
Michael Bueschafa83e22008-05-19 23:51:37 +02001869 /* Read the register that contains the reason code for this IRQ. */
1870 reason = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_DEBUGIRQ_REASON_REG);
1871
Michael Buesche48b0ee2008-05-17 22:44:35 +02001872 switch (reason) {
1873 case B43_DEBUGIRQ_PANIC:
Michael Bueschafa83e22008-05-19 23:51:37 +02001874 b43_handle_firmware_panic(dev);
Michael Buesche48b0ee2008-05-17 22:44:35 +02001875 break;
1876 case B43_DEBUGIRQ_DUMP_SHM:
1877 if (!B43_DEBUG)
1878 break; /* Only with driver debugging enabled. */
1879 buf = kmalloc(4096, GFP_ATOMIC);
1880 if (!buf) {
1881 b43dbg(dev->wl, "SHM-dump: Failed to allocate memory\n");
1882 goto out;
1883 }
1884 for (i = 0; i < 4096; i += 2) {
1885 u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, i);
1886 buf[i / 2] = cpu_to_le16(tmp);
1887 }
1888 b43info(dev->wl, "Shared memory dump:\n");
1889 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET,
1890 16, 2, buf, 4096, 1);
1891 kfree(buf);
1892 break;
1893 case B43_DEBUGIRQ_DUMP_REGS:
1894 if (!B43_DEBUG)
1895 break; /* Only with driver debugging enabled. */
1896 b43info(dev->wl, "Microcode register dump:\n");
1897 for (i = 0, cnt = 0; i < 64; i++) {
1898 u16 tmp = b43_shm_read16(dev, B43_SHM_SCRATCH, i);
1899 if (cnt == 0)
1900 printk(KERN_INFO);
1901 printk("r%02u: 0x%04X ", i, tmp);
1902 cnt++;
1903 if (cnt == 6) {
1904 printk("\n");
1905 cnt = 0;
1906 }
1907 }
1908 printk("\n");
1909 break;
Michael Buesch53c06852008-05-20 00:24:36 +02001910 case B43_DEBUGIRQ_MARKER:
1911 if (!B43_DEBUG)
1912 break; /* Only with driver debugging enabled. */
1913 marker_id = b43_shm_read16(dev, B43_SHM_SCRATCH,
1914 B43_MARKER_ID_REG);
1915 marker_line = b43_shm_read16(dev, B43_SHM_SCRATCH,
1916 B43_MARKER_LINE_REG);
1917 b43info(dev->wl, "The firmware just executed the MARKER(%u) "
1918 "at line number %u\n",
1919 marker_id, marker_line);
1920 break;
Michael Buesche48b0ee2008-05-17 22:44:35 +02001921 default:
1922 b43dbg(dev->wl, "Debug-IRQ triggered for unknown reason: %u\n",
1923 reason);
1924 }
1925out:
Michael Bueschafa83e22008-05-19 23:51:37 +02001926 /* Acknowledge the debug-IRQ, so the firmware can continue. */
1927 b43_shm_write16(dev, B43_SHM_SCRATCH,
1928 B43_DEBUGIRQ_REASON_REG, B43_DEBUGIRQ_ACK);
Michael Buesche4d6b792007-09-18 15:39:42 -04001929}
1930
Michael Buesch36dbd952009-09-04 22:51:29 +02001931static void b43_do_interrupt_thread(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04001932{
1933 u32 reason;
1934 u32 dma_reason[ARRAY_SIZE(dev->dma_reason)];
1935 u32 merged_dma_reason = 0;
Michael Buesch21954c32007-09-27 15:31:40 +02001936 int i;
Michael Buesche4d6b792007-09-18 15:39:42 -04001937
Michael Buesch36dbd952009-09-04 22:51:29 +02001938 if (unlikely(b43_status(dev) != B43_STAT_STARTED))
1939 return;
Michael Buesche4d6b792007-09-18 15:39:42 -04001940
1941 reason = dev->irq_reason;
1942 for (i = 0; i < ARRAY_SIZE(dma_reason); i++) {
1943 dma_reason[i] = dev->dma_reason[i];
1944 merged_dma_reason |= dma_reason[i];
1945 }
1946
1947 if (unlikely(reason & B43_IRQ_MAC_TXERR))
1948 b43err(dev->wl, "MAC transmission error\n");
1949
Stefano Brivio00e0b8c2007-11-25 11:10:33 +01001950 if (unlikely(reason & B43_IRQ_PHY_TXERR)) {
Michael Buesche4d6b792007-09-18 15:39:42 -04001951 b43err(dev->wl, "PHY transmission error\n");
Stefano Brivio00e0b8c2007-11-25 11:10:33 +01001952 rmb();
1953 if (unlikely(atomic_dec_and_test(&dev->phy.txerr_cnt))) {
1954 atomic_set(&dev->phy.txerr_cnt,
1955 B43_PHY_TX_BADNESS_LIMIT);
1956 b43err(dev->wl, "Too many PHY TX errors, "
1957 "restarting the controller\n");
1958 b43_controller_restart(dev, "PHY TX errors");
1959 }
1960 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001961
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02001962 if (unlikely(merged_dma_reason & (B43_DMAIRQ_FATALMASK))) {
1963 b43err(dev->wl,
1964 "Fatal DMA error: 0x%08X, 0x%08X, 0x%08X, 0x%08X, 0x%08X, 0x%08X\n",
1965 dma_reason[0], dma_reason[1],
1966 dma_reason[2], dma_reason[3],
1967 dma_reason[4], dma_reason[5]);
1968 b43err(dev->wl, "This device does not support DMA "
Larry Fingerbb64d952010-06-19 08:29:08 -05001969 "on your system. It will now be switched to PIO.\n");
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02001970 /* Fall back to PIO transfers if we get fatal DMA errors! */
1971 dev->use_pio = true;
1972 b43_controller_restart(dev, "DMA error");
1973 return;
Michael Buesche4d6b792007-09-18 15:39:42 -04001974 }
1975
1976 if (unlikely(reason & B43_IRQ_UCODE_DEBUG))
1977 handle_irq_ucode_debug(dev);
1978 if (reason & B43_IRQ_TBTT_INDI)
1979 handle_irq_tbtt_indication(dev);
1980 if (reason & B43_IRQ_ATIM_END)
1981 handle_irq_atim_end(dev);
1982 if (reason & B43_IRQ_BEACON)
1983 handle_irq_beacon(dev);
1984 if (reason & B43_IRQ_PMQ)
1985 handle_irq_pmq(dev);
Michael Buesch21954c32007-09-27 15:31:40 +02001986 if (reason & B43_IRQ_TXFIFO_FLUSH_OK)
1987 ;/* TODO */
1988 if (reason & B43_IRQ_NOISESAMPLE_OK)
Michael Buesche4d6b792007-09-18 15:39:42 -04001989 handle_irq_noise(dev);
1990
1991 /* Check the DMA reason registers for received data. */
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02001992 if (dma_reason[0] & B43_DMAIRQ_RDESC_UFLOW) {
1993 if (B43_DEBUG)
1994 b43warn(dev->wl, "RX descriptor underrun\n");
1995 b43_dma_handle_rx_overflow(dev->dma.rx_ring);
1996 }
Michael Buesch5100d5a2008-03-29 21:01:16 +01001997 if (dma_reason[0] & B43_DMAIRQ_RX_DONE) {
1998 if (b43_using_pio_transfers(dev))
1999 b43_pio_rx(dev->pio.rx_queue);
2000 else
2001 b43_dma_rx(dev->dma.rx_ring);
2002 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002003 B43_WARN_ON(dma_reason[1] & B43_DMAIRQ_RX_DONE);
2004 B43_WARN_ON(dma_reason[2] & B43_DMAIRQ_RX_DONE);
Michael Bueschb27faf82008-03-06 16:32:46 +01002005 B43_WARN_ON(dma_reason[3] & B43_DMAIRQ_RX_DONE);
Michael Buesche4d6b792007-09-18 15:39:42 -04002006 B43_WARN_ON(dma_reason[4] & B43_DMAIRQ_RX_DONE);
2007 B43_WARN_ON(dma_reason[5] & B43_DMAIRQ_RX_DONE);
2008
Michael Buesch21954c32007-09-27 15:31:40 +02002009 if (reason & B43_IRQ_TX_OK)
Michael Buesche4d6b792007-09-18 15:39:42 -04002010 handle_irq_transmit_status(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002011
Michael Buesch36dbd952009-09-04 22:51:29 +02002012 /* Re-enable interrupts on the device by restoring the current interrupt mask. */
Michael Buesch13790722009-04-08 21:26:27 +02002013 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
Michael Buesch990b86f2009-09-12 00:48:03 +02002014
2015#if B43_DEBUG
2016 if (b43_debug(dev, B43_DBG_VERBOSESTATS)) {
2017 dev->irq_count++;
2018 for (i = 0; i < ARRAY_SIZE(dev->irq_bit_count); i++) {
2019 if (reason & (1 << i))
2020 dev->irq_bit_count[i]++;
2021 }
2022 }
2023#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04002024}
2025
Michael Buesch36dbd952009-09-04 22:51:29 +02002026/* Interrupt thread handler. Handles device interrupts in thread context. */
2027static irqreturn_t b43_interrupt_thread_handler(int irq, void *dev_id)
Michael Buesche4d6b792007-09-18 15:39:42 -04002028{
Michael Buesche4d6b792007-09-18 15:39:42 -04002029 struct b43_wldev *dev = dev_id;
Michael Buesch36dbd952009-09-04 22:51:29 +02002030
2031 mutex_lock(&dev->wl->mutex);
2032 b43_do_interrupt_thread(dev);
2033 mmiowb();
2034 mutex_unlock(&dev->wl->mutex);
2035
2036 return IRQ_HANDLED;
2037}
2038
2039static irqreturn_t b43_do_interrupt(struct b43_wldev *dev)
2040{
Michael Buesche4d6b792007-09-18 15:39:42 -04002041 u32 reason;
2042
Michael Buesch36dbd952009-09-04 22:51:29 +02002043 /* This code runs under wl->hardirq_lock, but _only_ on non-SDIO busses.
2044 * On SDIO, this runs under wl->mutex. */
Michael Buesche4d6b792007-09-18 15:39:42 -04002045
Michael Buesche4d6b792007-09-18 15:39:42 -04002046 reason = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2047 if (reason == 0xffffffff) /* shared IRQ */
Michael Buesch36dbd952009-09-04 22:51:29 +02002048 return IRQ_NONE;
Michael Buesch13790722009-04-08 21:26:27 +02002049 reason &= dev->irq_mask;
Michael Buesche4d6b792007-09-18 15:39:42 -04002050 if (!reason)
Sebastian Andrzej Siewiorcae56142011-07-07 21:58:10 +02002051 return IRQ_NONE;
Michael Buesche4d6b792007-09-18 15:39:42 -04002052
2053 dev->dma_reason[0] = b43_read32(dev, B43_MMIO_DMA0_REASON)
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02002054 & 0x0001FC00;
Michael Buesche4d6b792007-09-18 15:39:42 -04002055 dev->dma_reason[1] = b43_read32(dev, B43_MMIO_DMA1_REASON)
2056 & 0x0000DC00;
2057 dev->dma_reason[2] = b43_read32(dev, B43_MMIO_DMA2_REASON)
2058 & 0x0000DC00;
2059 dev->dma_reason[3] = b43_read32(dev, B43_MMIO_DMA3_REASON)
2060 & 0x0001DC00;
2061 dev->dma_reason[4] = b43_read32(dev, B43_MMIO_DMA4_REASON)
2062 & 0x0000DC00;
Michael Buesch13790722009-04-08 21:26:27 +02002063/* Unused ring
Michael Buesche4d6b792007-09-18 15:39:42 -04002064 dev->dma_reason[5] = b43_read32(dev, B43_MMIO_DMA5_REASON)
2065 & 0x0000DC00;
Michael Buesch13790722009-04-08 21:26:27 +02002066*/
Michael Buesche4d6b792007-09-18 15:39:42 -04002067
Michael Buesch36dbd952009-09-04 22:51:29 +02002068 /* ACK the interrupt. */
2069 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, reason);
2070 b43_write32(dev, B43_MMIO_DMA0_REASON, dev->dma_reason[0]);
2071 b43_write32(dev, B43_MMIO_DMA1_REASON, dev->dma_reason[1]);
2072 b43_write32(dev, B43_MMIO_DMA2_REASON, dev->dma_reason[2]);
2073 b43_write32(dev, B43_MMIO_DMA3_REASON, dev->dma_reason[3]);
2074 b43_write32(dev, B43_MMIO_DMA4_REASON, dev->dma_reason[4]);
2075/* Unused ring
2076 b43_write32(dev, B43_MMIO_DMA5_REASON, dev->dma_reason[5]);
2077*/
2078
2079 /* Disable IRQs on the device. The IRQ thread handler will re-enable them. */
Michael Buesch13790722009-04-08 21:26:27 +02002080 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
Michael Buesch36dbd952009-09-04 22:51:29 +02002081 /* Save the reason bitmasks for the IRQ thread handler. */
Michael Buesche4d6b792007-09-18 15:39:42 -04002082 dev->irq_reason = reason;
Michael Buesch36dbd952009-09-04 22:51:29 +02002083
2084 return IRQ_WAKE_THREAD;
2085}
2086
2087/* Interrupt handler top-half. This runs with interrupts disabled. */
2088static irqreturn_t b43_interrupt_handler(int irq, void *dev_id)
2089{
2090 struct b43_wldev *dev = dev_id;
2091 irqreturn_t ret;
2092
2093 if (unlikely(b43_status(dev) < B43_STAT_STARTED))
2094 return IRQ_NONE;
2095
2096 spin_lock(&dev->wl->hardirq_lock);
2097 ret = b43_do_interrupt(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002098 mmiowb();
Michael Buesch36dbd952009-09-04 22:51:29 +02002099 spin_unlock(&dev->wl->hardirq_lock);
Michael Buesche4d6b792007-09-18 15:39:42 -04002100
2101 return ret;
2102}
2103
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002104/* SDIO interrupt handler. This runs in process context. */
2105static void b43_sdio_interrupt_handler(struct b43_wldev *dev)
2106{
2107 struct b43_wl *wl = dev->wl;
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002108 irqreturn_t ret;
2109
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002110 mutex_lock(&wl->mutex);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002111
2112 ret = b43_do_interrupt(dev);
2113 if (ret == IRQ_WAKE_THREAD)
2114 b43_do_interrupt_thread(dev);
2115
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002116 mutex_unlock(&wl->mutex);
2117}
2118
Michael Buesch1a9f5092009-01-23 21:21:51 +01002119void b43_do_release_fw(struct b43_firmware_file *fw)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002120{
2121 release_firmware(fw->data);
2122 fw->data = NULL;
2123 fw->filename = NULL;
2124}
2125
Michael Buesche4d6b792007-09-18 15:39:42 -04002126static void b43_release_firmware(struct b43_wldev *dev)
2127{
Larry Finger0673eff2014-01-12 15:11:38 -06002128 complete(&dev->fw_load_complete);
Michael Buesch1a9f5092009-01-23 21:21:51 +01002129 b43_do_release_fw(&dev->fw.ucode);
2130 b43_do_release_fw(&dev->fw.pcm);
2131 b43_do_release_fw(&dev->fw.initvals);
2132 b43_do_release_fw(&dev->fw.initvals_band);
Michael Buesche4d6b792007-09-18 15:39:42 -04002133}
2134
Michael Buescheb189d8b2008-01-28 14:47:41 -08002135static void b43_print_fw_helptext(struct b43_wl *wl, bool error)
Michael Buesche4d6b792007-09-18 15:39:42 -04002136{
Hannes Ederfc68ed42009-02-14 11:50:06 +00002137 const char text[] =
2138 "You must go to " \
2139 "http://wireless.kernel.org/en/users/Drivers/b43#devicefirmware " \
2140 "and download the correct firmware for this driver version. " \
2141 "Please carefully read all instructions on this website.\n";
Michael Buescheb189d8b2008-01-28 14:47:41 -08002142
Michael Buescheb189d8b2008-01-28 14:47:41 -08002143 if (error)
2144 b43err(wl, text);
2145 else
2146 b43warn(wl, text);
Michael Buesche4d6b792007-09-18 15:39:42 -04002147}
2148
Larry Finger5e20a4b2012-12-20 15:55:01 -06002149static void b43_fw_cb(const struct firmware *firmware, void *context)
2150{
2151 struct b43_request_fw_context *ctx = context;
2152
2153 ctx->blob = firmware;
Larry Finger0673eff2014-01-12 15:11:38 -06002154 complete(&ctx->dev->fw_load_complete);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002155}
2156
Michael Buesch1a9f5092009-01-23 21:21:51 +01002157int b43_do_request_fw(struct b43_request_fw_context *ctx,
2158 const char *name,
Larry Finger5e20a4b2012-12-20 15:55:01 -06002159 struct b43_firmware_file *fw, bool async)
Michael Buesche4d6b792007-09-18 15:39:42 -04002160{
Michael Buesche4d6b792007-09-18 15:39:42 -04002161 struct b43_fw_header *hdr;
2162 u32 size;
2163 int err;
2164
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002165 if (!name) {
2166 /* Don't fetch anything. Free possibly cached firmware. */
Michael Buesch1a9f5092009-01-23 21:21:51 +01002167 /* FIXME: We should probably keep it anyway, to save some headache
2168 * on suspend/resume with multiband devices. */
2169 b43_do_release_fw(fw);
Michael Buesche4d6b792007-09-18 15:39:42 -04002170 return 0;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002171 }
2172 if (fw->filename) {
Michael Buesch1a9f5092009-01-23 21:21:51 +01002173 if ((fw->type == ctx->req_type) &&
2174 (strcmp(fw->filename, name) == 0))
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002175 return 0; /* Already have this fw. */
2176 /* Free the cached firmware first. */
Michael Buesch1a9f5092009-01-23 21:21:51 +01002177 /* FIXME: We should probably do this later after we successfully
2178 * got the new fw. This could reduce headache with multiband devices.
2179 * We could also redesign this to cache the firmware for all possible
2180 * bands all the time. */
2181 b43_do_release_fw(fw);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002182 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002183
Michael Buesch1a9f5092009-01-23 21:21:51 +01002184 switch (ctx->req_type) {
2185 case B43_FWTYPE_PROPRIETARY:
2186 snprintf(ctx->fwname, sizeof(ctx->fwname),
2187 "b43%s/%s.fw",
2188 modparam_fwpostfix, name);
2189 break;
2190 case B43_FWTYPE_OPENSOURCE:
2191 snprintf(ctx->fwname, sizeof(ctx->fwname),
2192 "b43-open%s/%s.fw",
2193 modparam_fwpostfix, name);
2194 break;
2195 default:
2196 B43_WARN_ON(1);
2197 return -ENOSYS;
2198 }
Larry Finger5e20a4b2012-12-20 15:55:01 -06002199 if (async) {
2200 /* do this part asynchronously */
Larry Finger0673eff2014-01-12 15:11:38 -06002201 init_completion(&ctx->dev->fw_load_complete);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002202 err = request_firmware_nowait(THIS_MODULE, 1, ctx->fwname,
2203 ctx->dev->dev->dev, GFP_KERNEL,
2204 ctx, b43_fw_cb);
2205 if (err < 0) {
2206 pr_err("Unable to load firmware\n");
2207 return err;
2208 }
Larry Finger0673eff2014-01-12 15:11:38 -06002209 wait_for_completion(&ctx->dev->fw_load_complete);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002210 if (ctx->blob)
2211 goto fw_ready;
2212 /* On some ARM systems, the async request will fail, but the next sync
Larry Finger0673eff2014-01-12 15:11:38 -06002213 * request works. For this reason, we fall through here
Larry Finger5e20a4b2012-12-20 15:55:01 -06002214 */
2215 }
2216 err = request_firmware(&ctx->blob, ctx->fwname,
2217 ctx->dev->dev->dev);
Michael Buesch68217832008-05-17 23:43:57 +02002218 if (err == -ENOENT) {
Michael Buesch1a9f5092009-01-23 21:21:51 +01002219 snprintf(ctx->errors[ctx->req_type],
2220 sizeof(ctx->errors[ctx->req_type]),
Larry Finger5e20a4b2012-12-20 15:55:01 -06002221 "Firmware file \"%s\" not found\n",
2222 ctx->fwname);
Michael Buesch68217832008-05-17 23:43:57 +02002223 return err;
2224 } else if (err) {
Michael Buesch1a9f5092009-01-23 21:21:51 +01002225 snprintf(ctx->errors[ctx->req_type],
2226 sizeof(ctx->errors[ctx->req_type]),
2227 "Firmware file \"%s\" request failed (err=%d)\n",
2228 ctx->fwname, err);
Michael Buesche4d6b792007-09-18 15:39:42 -04002229 return err;
2230 }
Larry Finger5e20a4b2012-12-20 15:55:01 -06002231fw_ready:
2232 if (ctx->blob->size < sizeof(struct b43_fw_header))
Michael Buesche4d6b792007-09-18 15:39:42 -04002233 goto err_format;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002234 hdr = (struct b43_fw_header *)(ctx->blob->data);
Michael Buesche4d6b792007-09-18 15:39:42 -04002235 switch (hdr->type) {
2236 case B43_FW_TYPE_UCODE:
2237 case B43_FW_TYPE_PCM:
2238 size = be32_to_cpu(hdr->size);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002239 if (size != ctx->blob->size - sizeof(struct b43_fw_header))
Michael Buesche4d6b792007-09-18 15:39:42 -04002240 goto err_format;
2241 /* fallthrough */
2242 case B43_FW_TYPE_IV:
2243 if (hdr->ver != 1)
2244 goto err_format;
2245 break;
2246 default:
2247 goto err_format;
2248 }
2249
Larry Finger5e20a4b2012-12-20 15:55:01 -06002250 fw->data = ctx->blob;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002251 fw->filename = name;
Michael Buesch1a9f5092009-01-23 21:21:51 +01002252 fw->type = ctx->req_type;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002253
2254 return 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04002255
2256err_format:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002257 snprintf(ctx->errors[ctx->req_type],
2258 sizeof(ctx->errors[ctx->req_type]),
2259 "Firmware file \"%s\" format error.\n", ctx->fwname);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002260 release_firmware(ctx->blob);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002261
Michael Buesche4d6b792007-09-18 15:39:42 -04002262 return -EPROTO;
2263}
2264
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002265/* http://bcm-v4.sipsolutions.net/802.11/Init/Firmware */
Michael Buesch1a9f5092009-01-23 21:21:51 +01002266static int b43_try_request_fw(struct b43_request_fw_context *ctx)
Michael Buesche4d6b792007-09-18 15:39:42 -04002267{
Michael Buesch1a9f5092009-01-23 21:21:51 +01002268 struct b43_wldev *dev = ctx->dev;
2269 struct b43_firmware *fw = &ctx->dev->fw;
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002270 struct b43_phy *phy = &dev->phy;
Rafał Miłecki21d889d2011-05-18 02:06:38 +02002271 const u8 rev = ctx->dev->dev->core_rev;
Michael Buesche4d6b792007-09-18 15:39:42 -04002272 const char *filename;
Michael Buesche4d6b792007-09-18 15:39:42 -04002273 int err;
2274
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002275 /* Get microcode */
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002276 filename = NULL;
2277 switch (rev) {
2278 case 42:
2279 if (phy->type == B43_PHYTYPE_AC)
2280 filename = "ucode42";
2281 break;
Rafał Miłecki15be8e82014-07-01 16:33:57 +02002282 case 40:
2283 if (phy->type == B43_PHYTYPE_AC)
2284 filename = "ucode40";
2285 break;
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002286 case 33:
2287 if (phy->type == B43_PHYTYPE_LCN40)
2288 filename = "ucode33_lcn40";
2289 break;
2290 case 30:
2291 if (phy->type == B43_PHYTYPE_N)
2292 filename = "ucode30_mimo";
2293 break;
2294 case 29:
2295 if (phy->type == B43_PHYTYPE_HT)
2296 filename = "ucode29_mimo";
2297 break;
2298 case 26:
2299 if (phy->type == B43_PHYTYPE_HT)
2300 filename = "ucode26_mimo";
2301 break;
2302 case 28:
2303 case 25:
2304 if (phy->type == B43_PHYTYPE_N)
2305 filename = "ucode25_mimo";
2306 else if (phy->type == B43_PHYTYPE_LCN)
2307 filename = "ucode25_lcn";
2308 break;
2309 case 24:
2310 if (phy->type == B43_PHYTYPE_LCN)
2311 filename = "ucode24_lcn";
2312 break;
2313 case 23:
2314 if (phy->type == B43_PHYTYPE_N)
2315 filename = "ucode16_mimo";
2316 break;
2317 case 16 ... 19:
2318 if (phy->type == B43_PHYTYPE_N)
2319 filename = "ucode16_mimo";
2320 else if (phy->type == B43_PHYTYPE_LP)
2321 filename = "ucode16_lp";
2322 break;
2323 case 15:
Gábor Stefanik759b9732009-08-14 14:39:53 +02002324 filename = "ucode15";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002325 break;
2326 case 14:
2327 filename = "ucode14";
2328 break;
2329 case 13:
2330 filename = "ucode13";
2331 break;
2332 case 11 ... 12:
2333 filename = "ucode11";
2334 break;
2335 case 5 ... 10:
2336 filename = "ucode5";
2337 break;
Rafał Miłecki6ff1e5c2011-07-06 17:41:55 +02002338 }
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002339 if (!filename)
2340 goto err_no_ucode;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002341 err = b43_do_request_fw(ctx, filename, &fw->ucode, true);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002342 if (err)
2343 goto err_load;
2344
2345 /* Get PCM code */
2346 if ((rev >= 5) && (rev <= 10))
2347 filename = "pcm5";
2348 else if (rev >= 11)
2349 filename = NULL;
2350 else
2351 goto err_no_pcm;
Rusty Russell3db1cd52011-12-19 13:56:45 +00002352 fw->pcm_request_failed = false;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002353 err = b43_do_request_fw(ctx, filename, &fw->pcm, false);
Michael Buesch68217832008-05-17 23:43:57 +02002354 if (err == -ENOENT) {
2355 /* We did not find a PCM file? Not fatal, but
2356 * core rev <= 10 must do without hwcrypto then. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00002357 fw->pcm_request_failed = true;
Michael Buesch68217832008-05-17 23:43:57 +02002358 } else if (err)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002359 goto err_load;
2360
2361 /* Get initvals */
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002362 filename = NULL;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002363 switch (dev->phy.type) {
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002364 case B43_PHYTYPE_G:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002365 if (rev == 13)
Larry.Finger@lwfinger.nete9304882008-05-15 14:07:36 -05002366 filename = "b0g0initvals13";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002367 else if (rev >= 5 && rev <= 10)
2368 filename = "b0g0initvals5";
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002369 break;
2370 case B43_PHYTYPE_N:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002371 if (rev == 30)
2372 filename = "n16initvals30";
2373 else if (rev == 28 || rev == 25)
2374 filename = "n0initvals25";
2375 else if (rev == 24)
2376 filename = "n0initvals24";
2377 else if (rev == 23)
2378 filename = "n0initvals16"; /* What about n0initvals22? */
2379 else if (rev >= 16 && rev <= 18)
Rafał Miłeckie41596a2010-12-21 11:50:19 +01002380 filename = "n0initvals16";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002381 else if (rev >= 11 && rev <= 12)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002382 filename = "n0initvals11";
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002383 break;
Gábor Stefanik759b9732009-08-14 14:39:53 +02002384 case B43_PHYTYPE_LP:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002385 if (rev >= 16 && rev <= 18)
2386 filename = "lp0initvals16";
2387 else if (rev == 15)
2388 filename = "lp0initvals15";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002389 else if (rev == 14)
2390 filename = "lp0initvals14";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002391 else if (rev == 13)
2392 filename = "lp0initvals13";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002393 break;
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002394 case B43_PHYTYPE_HT:
2395 if (rev == 29)
2396 filename = "ht0initvals29";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002397 else if (rev == 26)
2398 filename = "ht0initvals26";
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002399 break;
2400 case B43_PHYTYPE_LCN:
2401 if (rev == 24)
2402 filename = "lcn0initvals24";
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002403 break;
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002404 case B43_PHYTYPE_LCN40:
2405 if (rev == 33)
2406 filename = "lcn400initvals33";
2407 break;
2408 case B43_PHYTYPE_AC:
2409 if (rev == 42)
2410 filename = "ac1initvals42";
Rafał Miłecki15be8e82014-07-01 16:33:57 +02002411 else if (rev == 40)
2412 filename = "ac0initvals40";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002413 break;
Michael Buesche4d6b792007-09-18 15:39:42 -04002414 }
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002415 if (!filename)
2416 goto err_no_initvals;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002417 err = b43_do_request_fw(ctx, filename, &fw->initvals, false);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002418 if (err)
2419 goto err_load;
2420
2421 /* Get bandswitch initvals */
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002422 filename = NULL;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002423 switch (dev->phy.type) {
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002424 case B43_PHYTYPE_G:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002425 if (rev == 13)
2426 filename = "b0g0bsinitvals13";
2427 else if (rev >= 5 && rev <= 10)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002428 filename = "b0g0bsinitvals5";
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002429 break;
2430 case B43_PHYTYPE_N:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002431 if (rev == 30)
2432 filename = "n16bsinitvals30";
2433 else if (rev == 28 || rev == 25)
2434 filename = "n0bsinitvals25";
2435 else if (rev == 24)
2436 filename = "n0bsinitvals24";
2437 else if (rev == 23)
2438 filename = "n0bsinitvals16"; /* What about n0bsinitvals22? */
2439 else if (rev >= 16 && rev <= 18)
Rafał Miłeckie41596a2010-12-21 11:50:19 +01002440 filename = "n0bsinitvals16";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002441 else if (rev >= 11 && rev <= 12)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002442 filename = "n0bsinitvals11";
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002443 break;
Gábor Stefanik759b9732009-08-14 14:39:53 +02002444 case B43_PHYTYPE_LP:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002445 if (rev >= 16 && rev <= 18)
2446 filename = "lp0bsinitvals16";
2447 else if (rev == 15)
2448 filename = "lp0bsinitvals15";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002449 else if (rev == 14)
2450 filename = "lp0bsinitvals14";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002451 else if (rev == 13)
2452 filename = "lp0bsinitvals13";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002453 break;
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002454 case B43_PHYTYPE_HT:
2455 if (rev == 29)
2456 filename = "ht0bsinitvals29";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002457 else if (rev == 26)
2458 filename = "ht0bsinitvals26";
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002459 break;
2460 case B43_PHYTYPE_LCN:
2461 if (rev == 24)
2462 filename = "lcn0bsinitvals24";
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002463 break;
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002464 case B43_PHYTYPE_LCN40:
2465 if (rev == 33)
2466 filename = "lcn400bsinitvals33";
2467 break;
2468 case B43_PHYTYPE_AC:
2469 if (rev == 42)
2470 filename = "ac1bsinitvals42";
Rafał Miłecki15be8e82014-07-01 16:33:57 +02002471 else if (rev == 40)
2472 filename = "ac0bsinitvals40";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002473 break;
Michael Buesche4d6b792007-09-18 15:39:42 -04002474 }
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002475 if (!filename)
2476 goto err_no_initvals;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002477 err = b43_do_request_fw(ctx, filename, &fw->initvals_band, false);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002478 if (err)
2479 goto err_load;
Michael Buesche4d6b792007-09-18 15:39:42 -04002480
Johannes Berg097b0e12012-07-17 17:12:29 +02002481 fw->opensource = (ctx->req_type == B43_FWTYPE_OPENSOURCE);
2482
Michael Buesche4d6b792007-09-18 15:39:42 -04002483 return 0;
2484
Michael Buesche4d6b792007-09-18 15:39:42 -04002485err_no_ucode:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002486 err = ctx->fatal_failure = -EOPNOTSUPP;
2487 b43err(dev->wl, "The driver does not know which firmware (ucode) "
2488 "is required for your device (wl-core rev %u)\n", rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002489 goto error;
2490
2491err_no_pcm:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002492 err = ctx->fatal_failure = -EOPNOTSUPP;
2493 b43err(dev->wl, "The driver does not know which firmware (PCM) "
2494 "is required for your device (wl-core rev %u)\n", rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002495 goto error;
2496
2497err_no_initvals:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002498 err = ctx->fatal_failure = -EOPNOTSUPP;
2499 b43err(dev->wl, "The driver does not know which firmware (initvals) "
2500 "is required for your device (wl-core rev %u)\n", rev);
2501 goto error;
2502
2503err_load:
2504 /* We failed to load this firmware image. The error message
2505 * already is in ctx->errors. Return and let our caller decide
2506 * what to do. */
Michael Buesche4d6b792007-09-18 15:39:42 -04002507 goto error;
2508
2509error:
2510 b43_release_firmware(dev);
2511 return err;
2512}
2513
Larry Finger6b6fa582012-03-08 22:27:46 -06002514static int b43_one_core_attach(struct b43_bus_dev *dev, struct b43_wl *wl);
2515static void b43_one_core_detach(struct b43_bus_dev *dev);
Larry Finger09164042014-01-12 15:11:37 -06002516static int b43_rng_init(struct b43_wl *wl);
Larry Finger6b6fa582012-03-08 22:27:46 -06002517
2518static void b43_request_firmware(struct work_struct *work)
Michael Buesch1a9f5092009-01-23 21:21:51 +01002519{
Larry Finger6b6fa582012-03-08 22:27:46 -06002520 struct b43_wl *wl = container_of(work,
2521 struct b43_wl, firmware_load);
2522 struct b43_wldev *dev = wl->current_dev;
Michael Buesch1a9f5092009-01-23 21:21:51 +01002523 struct b43_request_fw_context *ctx;
2524 unsigned int i;
2525 int err;
2526 const char *errmsg;
2527
2528 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2529 if (!ctx)
Larry Finger6b6fa582012-03-08 22:27:46 -06002530 return;
Michael Buesch1a9f5092009-01-23 21:21:51 +01002531 ctx->dev = dev;
2532
2533 ctx->req_type = B43_FWTYPE_PROPRIETARY;
2534 err = b43_try_request_fw(ctx);
2535 if (!err)
Larry Finger6b6fa582012-03-08 22:27:46 -06002536 goto start_ieee80211; /* Successfully loaded it. */
2537 /* Was fw version known? */
2538 if (ctx->fatal_failure)
Michael Buesch1a9f5092009-01-23 21:21:51 +01002539 goto out;
2540
Larry Finger6b6fa582012-03-08 22:27:46 -06002541 /* proprietary fw not found, try open source */
Michael Buesch1a9f5092009-01-23 21:21:51 +01002542 ctx->req_type = B43_FWTYPE_OPENSOURCE;
2543 err = b43_try_request_fw(ctx);
2544 if (!err)
Larry Finger6b6fa582012-03-08 22:27:46 -06002545 goto start_ieee80211; /* Successfully loaded it. */
2546 if(ctx->fatal_failure)
Michael Buesch1a9f5092009-01-23 21:21:51 +01002547 goto out;
2548
2549 /* Could not find a usable firmware. Print the errors. */
2550 for (i = 0; i < B43_NR_FWTYPES; i++) {
2551 errmsg = ctx->errors[i];
2552 if (strlen(errmsg))
Kees Cooke0e29b62013-05-10 14:48:21 -07002553 b43err(dev->wl, "%s", errmsg);
Michael Buesch1a9f5092009-01-23 21:21:51 +01002554 }
2555 b43_print_fw_helptext(dev->wl, 1);
Larry Finger6b6fa582012-03-08 22:27:46 -06002556 goto out;
2557
2558start_ieee80211:
Johannes Berg097b0e12012-07-17 17:12:29 +02002559 wl->hw->queues = B43_QOS_QUEUE_NUM;
2560 if (!modparam_qos || dev->fw.opensource)
2561 wl->hw->queues = 1;
2562
Larry Finger6b6fa582012-03-08 22:27:46 -06002563 err = ieee80211_register_hw(wl->hw);
2564 if (err)
2565 goto err_one_core_detach;
Oleksij Rempele64add22012-06-05 20:39:32 +02002566 wl->hw_registred = true;
Larry Finger6b6fa582012-03-08 22:27:46 -06002567 b43_leds_register(wl->current_dev);
Larry Finger09164042014-01-12 15:11:37 -06002568
2569 /* Register HW RNG driver */
2570 b43_rng_init(wl);
2571
Larry Finger6b6fa582012-03-08 22:27:46 -06002572 goto out;
2573
2574err_one_core_detach:
2575 b43_one_core_detach(dev->dev);
Michael Buesch1a9f5092009-01-23 21:21:51 +01002576
2577out:
2578 kfree(ctx);
Michael Buesch1a9f5092009-01-23 21:21:51 +01002579}
2580
Michael Buesche4d6b792007-09-18 15:39:42 -04002581static int b43_upload_microcode(struct b43_wldev *dev)
2582{
John W. Linville652caa52010-07-29 13:27:28 -04002583 struct wiphy *wiphy = dev->wl->hw->wiphy;
Michael Buesche4d6b792007-09-18 15:39:42 -04002584 const size_t hdr_len = sizeof(struct b43_fw_header);
2585 const __be32 *data;
2586 unsigned int i, len;
2587 u16 fwrev, fwpatch, fwdate, fwtime;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002588 u32 tmp, macctl;
Michael Buesche4d6b792007-09-18 15:39:42 -04002589 int err = 0;
2590
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002591 /* Jump the microcode PSM to offset 0 */
2592 macctl = b43_read32(dev, B43_MMIO_MACCTL);
2593 B43_WARN_ON(macctl & B43_MACCTL_PSM_RUN);
2594 macctl |= B43_MACCTL_PSM_JMP0;
2595 b43_write32(dev, B43_MMIO_MACCTL, macctl);
2596 /* Zero out all microcode PSM registers and shared memory. */
2597 for (i = 0; i < 64; i++)
2598 b43_shm_write16(dev, B43_SHM_SCRATCH, i, 0);
2599 for (i = 0; i < 4096; i += 2)
2600 b43_shm_write16(dev, B43_SHM_SHARED, i, 0);
2601
Michael Buesche4d6b792007-09-18 15:39:42 -04002602 /* Upload Microcode. */
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002603 data = (__be32 *) (dev->fw.ucode.data->data + hdr_len);
2604 len = (dev->fw.ucode.data->size - hdr_len) / sizeof(__be32);
Michael Buesche4d6b792007-09-18 15:39:42 -04002605 b43_shm_control_word(dev, B43_SHM_UCODE | B43_SHM_AUTOINC_W, 0x0000);
2606 for (i = 0; i < len; i++) {
2607 b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
2608 udelay(10);
2609 }
2610
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002611 if (dev->fw.pcm.data) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002612 /* Upload PCM data. */
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002613 data = (__be32 *) (dev->fw.pcm.data->data + hdr_len);
2614 len = (dev->fw.pcm.data->size - hdr_len) / sizeof(__be32);
Michael Buesche4d6b792007-09-18 15:39:42 -04002615 b43_shm_control_word(dev, B43_SHM_HW, 0x01EA);
2616 b43_write32(dev, B43_MMIO_SHM_DATA, 0x00004000);
2617 /* No need for autoinc bit in SHM_HW */
2618 b43_shm_control_word(dev, B43_SHM_HW, 0x01EB);
2619 for (i = 0; i < len; i++) {
2620 b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
2621 udelay(10);
2622 }
2623 }
2624
2625 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_ALL);
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002626
2627 /* Start the microcode PSM */
Rafał Miłecki50566352012-01-02 19:31:21 +01002628 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_PSM_JMP0,
2629 B43_MACCTL_PSM_RUN);
Michael Buesche4d6b792007-09-18 15:39:42 -04002630
2631 /* Wait for the microcode to load and respond */
2632 i = 0;
2633 while (1) {
2634 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2635 if (tmp == B43_IRQ_MAC_SUSPENDED)
2636 break;
2637 i++;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002638 if (i >= 20) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002639 b43err(dev->wl, "Microcode not responding\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002640 b43_print_fw_helptext(dev->wl, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002641 err = -ENODEV;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002642 goto error;
Michael Buesche4d6b792007-09-18 15:39:42 -04002643 }
Michael Buesche175e992009-09-11 18:31:32 +02002644 msleep(50);
Michael Buesche4d6b792007-09-18 15:39:42 -04002645 }
2646 b43_read32(dev, B43_MMIO_GEN_IRQ_REASON); /* dummy read */
2647
2648 /* Get and check the revisions. */
2649 fwrev = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEREV);
2650 fwpatch = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEPATCH);
2651 fwdate = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEDATE);
2652 fwtime = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODETIME);
2653
2654 if (fwrev <= 0x128) {
2655 b43err(dev->wl, "YOUR FIRMWARE IS TOO OLD. Firmware from "
2656 "binary drivers older than version 4.x is unsupported. "
2657 "You must upgrade your firmware files.\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002658 b43_print_fw_helptext(dev->wl, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002659 err = -EOPNOTSUPP;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002660 goto error;
Michael Buesche4d6b792007-09-18 15:39:42 -04002661 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002662 dev->fw.rev = fwrev;
2663 dev->fw.patch = fwpatch;
Rafał Miłecki5d852902011-08-11 15:07:16 +02002664 if (dev->fw.rev >= 598)
2665 dev->fw.hdr_format = B43_FW_HDR_598;
2666 else if (dev->fw.rev >= 410)
Rafał Miłeckiefe02492011-08-11 15:07:15 +02002667 dev->fw.hdr_format = B43_FW_HDR_410;
2668 else
2669 dev->fw.hdr_format = B43_FW_HDR_351;
Johannes Berg097b0e12012-07-17 17:12:29 +02002670 WARN_ON(dev->fw.opensource != (fwdate == 0xFFFF));
Michael Buesche48b0ee2008-05-17 22:44:35 +02002671
Johannes Berg097b0e12012-07-17 17:12:29 +02002672 dev->qos_enabled = dev->wl->hw->queues > 1;
Michael Buesch403a3a12009-06-08 21:04:57 +02002673 /* Default to firmware/hardware crypto acceleration. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00002674 dev->hwcrypto_enabled = true;
Michael Buesch403a3a12009-06-08 21:04:57 +02002675
Michael Buesche48b0ee2008-05-17 22:44:35 +02002676 if (dev->fw.opensource) {
Michael Buesch403a3a12009-06-08 21:04:57 +02002677 u16 fwcapa;
2678
Michael Buesche48b0ee2008-05-17 22:44:35 +02002679 /* Patchlevel info is encoded in the "time" field. */
2680 dev->fw.patch = fwtime;
Michael Buesch403a3a12009-06-08 21:04:57 +02002681 b43info(dev->wl, "Loading OpenSource firmware version %u.%u\n",
2682 dev->fw.rev, dev->fw.patch);
2683
2684 fwcapa = b43_fwcapa_read(dev);
2685 if (!(fwcapa & B43_FWCAPA_HWCRYPTO) || dev->fw.pcm_request_failed) {
2686 b43info(dev->wl, "Hardware crypto acceleration not supported by firmware\n");
2687 /* Disable hardware crypto and fall back to software crypto. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00002688 dev->hwcrypto_enabled = false;
Michael Buesch403a3a12009-06-08 21:04:57 +02002689 }
Johannes Berg097b0e12012-07-17 17:12:29 +02002690 /* adding QoS support should use an offline discovery mechanism */
2691 WARN(fwcapa & B43_FWCAPA_QOS, "QoS in OpenFW not supported\n");
Michael Buesche48b0ee2008-05-17 22:44:35 +02002692 } else {
2693 b43info(dev->wl, "Loading firmware version %u.%u "
2694 "(20%.2i-%.2i-%.2i %.2i:%.2i:%.2i)\n",
2695 fwrev, fwpatch,
2696 (fwdate >> 12) & 0xF, (fwdate >> 8) & 0xF, fwdate & 0xFF,
2697 (fwtime >> 11) & 0x1F, (fwtime >> 5) & 0x3F, fwtime & 0x1F);
Michael Buesch68217832008-05-17 23:43:57 +02002698 if (dev->fw.pcm_request_failed) {
2699 b43warn(dev->wl, "No \"pcm5.fw\" firmware file found. "
2700 "Hardware accelerated cryptography is disabled.\n");
2701 b43_print_fw_helptext(dev->wl, 0);
2702 }
Michael Buesche48b0ee2008-05-17 22:44:35 +02002703 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002704
John W. Linville652caa52010-07-29 13:27:28 -04002705 snprintf(wiphy->fw_version, sizeof(wiphy->fw_version), "%u.%u",
2706 dev->fw.rev, dev->fw.patch);
Rafał Miłecki21d889d2011-05-18 02:06:38 +02002707 wiphy->hw_version = dev->dev->core_id;
John W. Linville652caa52010-07-29 13:27:28 -04002708
Rafał Miłeckiefe02492011-08-11 15:07:15 +02002709 if (dev->fw.hdr_format == B43_FW_HDR_351) {
Michael Bueschc5572892008-12-27 18:26:39 +01002710 /* We're over the deadline, but we keep support for old fw
2711 * until it turns out to be in major conflict with something new. */
Michael Buescheb189d8b2008-01-28 14:47:41 -08002712 b43warn(dev->wl, "You are using an old firmware image. "
Michael Bueschc5572892008-12-27 18:26:39 +01002713 "Support for old firmware will be removed soon "
2714 "(official deadline was July 2008).\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002715 b43_print_fw_helptext(dev->wl, 0);
2716 }
2717
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002718 return 0;
2719
2720error:
Rafał Miłecki50566352012-01-02 19:31:21 +01002721 /* Stop the microcode PSM. */
2722 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_PSM_RUN,
2723 B43_MACCTL_PSM_JMP0);
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002724
Michael Buesche4d6b792007-09-18 15:39:42 -04002725 return err;
2726}
2727
2728static int b43_write_initvals(struct b43_wldev *dev,
2729 const struct b43_iv *ivals,
2730 size_t count,
2731 size_t array_size)
2732{
2733 const struct b43_iv *iv;
2734 u16 offset;
2735 size_t i;
2736 bool bit32;
2737
2738 BUILD_BUG_ON(sizeof(struct b43_iv) != 6);
2739 iv = ivals;
2740 for (i = 0; i < count; i++) {
2741 if (array_size < sizeof(iv->offset_size))
2742 goto err_format;
2743 array_size -= sizeof(iv->offset_size);
2744 offset = be16_to_cpu(iv->offset_size);
2745 bit32 = !!(offset & B43_IV_32BIT);
2746 offset &= B43_IV_OFFSET_MASK;
2747 if (offset >= 0x1000)
2748 goto err_format;
2749 if (bit32) {
2750 u32 value;
2751
2752 if (array_size < sizeof(iv->data.d32))
2753 goto err_format;
2754 array_size -= sizeof(iv->data.d32);
2755
Harvey Harrison533dd1b2008-04-29 01:03:36 -07002756 value = get_unaligned_be32(&iv->data.d32);
Michael Buesche4d6b792007-09-18 15:39:42 -04002757 b43_write32(dev, offset, value);
2758
2759 iv = (const struct b43_iv *)((const uint8_t *)iv +
2760 sizeof(__be16) +
2761 sizeof(__be32));
2762 } else {
2763 u16 value;
2764
2765 if (array_size < sizeof(iv->data.d16))
2766 goto err_format;
2767 array_size -= sizeof(iv->data.d16);
2768
2769 value = be16_to_cpu(iv->data.d16);
2770 b43_write16(dev, offset, value);
2771
2772 iv = (const struct b43_iv *)((const uint8_t *)iv +
2773 sizeof(__be16) +
2774 sizeof(__be16));
2775 }
2776 }
2777 if (array_size)
2778 goto err_format;
2779
2780 return 0;
2781
2782err_format:
2783 b43err(dev->wl, "Initial Values Firmware file-format error.\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002784 b43_print_fw_helptext(dev->wl, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002785
2786 return -EPROTO;
2787}
2788
2789static int b43_upload_initvals(struct b43_wldev *dev)
2790{
2791 const size_t hdr_len = sizeof(struct b43_fw_header);
2792 const struct b43_fw_header *hdr;
2793 struct b43_firmware *fw = &dev->fw;
2794 const struct b43_iv *ivals;
2795 size_t count;
Michael Buesche4d6b792007-09-18 15:39:42 -04002796
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002797 hdr = (const struct b43_fw_header *)(fw->initvals.data->data);
2798 ivals = (const struct b43_iv *)(fw->initvals.data->data + hdr_len);
Michael Buesche4d6b792007-09-18 15:39:42 -04002799 count = be32_to_cpu(hdr->size);
Rafał Miłecki0f684232014-05-17 23:24:53 +02002800 return b43_write_initvals(dev, ivals, count,
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002801 fw->initvals.data->size - hdr_len);
Rafał Miłecki0f684232014-05-17 23:24:53 +02002802}
Michael Buesche4d6b792007-09-18 15:39:42 -04002803
Rafał Miłecki0f684232014-05-17 23:24:53 +02002804static int b43_upload_initvals_band(struct b43_wldev *dev)
2805{
2806 const size_t hdr_len = sizeof(struct b43_fw_header);
2807 const struct b43_fw_header *hdr;
2808 struct b43_firmware *fw = &dev->fw;
2809 const struct b43_iv *ivals;
2810 size_t count;
2811
2812 if (!fw->initvals_band.data)
2813 return 0;
2814
2815 hdr = (const struct b43_fw_header *)(fw->initvals_band.data->data);
2816 ivals = (const struct b43_iv *)(fw->initvals_band.data->data + hdr_len);
2817 count = be32_to_cpu(hdr->size);
2818 return b43_write_initvals(dev, ivals, count,
2819 fw->initvals_band.data->size - hdr_len);
Michael Buesche4d6b792007-09-18 15:39:42 -04002820}
2821
2822/* Initialize the GPIOs
2823 * http://bcm-specs.sipsolutions.net/GPIO
2824 */
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002825
2826#ifdef CONFIG_B43_SSB
Rafał Miłeckic4a2a0812011-05-17 18:57:27 +02002827static struct ssb_device *b43_ssb_gpio_dev(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04002828{
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02002829 struct ssb_bus *bus = dev->dev->sdev->bus;
Rafał Miłeckic4a2a0812011-05-17 18:57:27 +02002830
2831#ifdef CONFIG_SSB_DRIVER_PCICORE
2832 return (bus->chipco.dev ? bus->chipco.dev : bus->pcicore.dev);
2833#else
2834 return bus->chipco.dev;
2835#endif
2836}
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002837#endif
Rafał Miłeckic4a2a0812011-05-17 18:57:27 +02002838
Michael Buesche4d6b792007-09-18 15:39:42 -04002839static int b43_gpio_init(struct b43_wldev *dev)
2840{
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002841#ifdef CONFIG_B43_SSB
Rafał Miłeckic4a2a0812011-05-17 18:57:27 +02002842 struct ssb_device *gpiodev;
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002843#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04002844 u32 mask, set;
2845
Rafał Miłecki50566352012-01-02 19:31:21 +01002846 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_GPOUTSMSK, 0);
2847 b43_maskset16(dev, B43_MMIO_GPIO_MASK, ~0, 0xF);
Michael Buesche4d6b792007-09-18 15:39:42 -04002848
2849 mask = 0x0000001F;
2850 set = 0x0000000F;
Rafał Miłeckic244e082011-05-18 02:06:41 +02002851 if (dev->dev->chip_id == 0x4301) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002852 mask |= 0x0060;
2853 set |= 0x0060;
Rafał Miłecki828afd22012-07-23 22:57:01 +02002854 } else if (dev->dev->chip_id == 0x5354) {
2855 /* Don't allow overtaking buttons GPIOs */
2856 set &= 0x2; /* 0x2 is LED GPIO on BCM5354 */
Michael Buesche4d6b792007-09-18 15:39:42 -04002857 }
Rafał Miłecki828afd22012-07-23 22:57:01 +02002858
Michael Buesche4d6b792007-09-18 15:39:42 -04002859 if (0 /* FIXME: conditional unknown */ ) {
2860 b43_write16(dev, B43_MMIO_GPIO_MASK,
2861 b43_read16(dev, B43_MMIO_GPIO_MASK)
2862 | 0x0100);
Rafał Miłecki828afd22012-07-23 22:57:01 +02002863 /* BT Coexistance Input */
2864 mask |= 0x0080;
2865 set |= 0x0080;
2866 /* BT Coexistance Out */
2867 mask |= 0x0100;
2868 set |= 0x0100;
Michael Buesche4d6b792007-09-18 15:39:42 -04002869 }
Rafał Miłecki05814832011-05-18 02:06:39 +02002870 if (dev->dev->bus_sprom->boardflags_lo & B43_BFL_PACTRL) {
Rafał Miłecki828afd22012-07-23 22:57:01 +02002871 /* PA is controlled by gpio 9, let ucode handle it */
Michael Buesche4d6b792007-09-18 15:39:42 -04002872 b43_write16(dev, B43_MMIO_GPIO_MASK,
2873 b43_read16(dev, B43_MMIO_GPIO_MASK)
2874 | 0x0200);
2875 mask |= 0x0200;
2876 set |= 0x0200;
2877 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002878
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002879 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002880#ifdef CONFIG_B43_BCMA
2881 case B43_BUS_BCMA:
Hauke Mehrtens0a64bae2013-03-21 16:19:45 +01002882 bcma_chipco_gpio_control(&dev->dev->bdev->bus->drv_cc, mask, set);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002883 break;
2884#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002885#ifdef CONFIG_B43_SSB
2886 case B43_BUS_SSB:
2887 gpiodev = b43_ssb_gpio_dev(dev);
2888 if (gpiodev)
2889 ssb_write32(gpiodev, B43_GPIO_CONTROL,
2890 (ssb_read32(gpiodev, B43_GPIO_CONTROL)
Rafał Miłecki828afd22012-07-23 22:57:01 +02002891 & ~mask) | set);
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002892 break;
2893#endif
2894 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002895
2896 return 0;
2897}
2898
2899/* Turn off all GPIO stuff. Call this on module unload, for example. */
2900static void b43_gpio_cleanup(struct b43_wldev *dev)
2901{
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002902#ifdef CONFIG_B43_SSB
Rafał Miłeckic4a2a0812011-05-17 18:57:27 +02002903 struct ssb_device *gpiodev;
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002904#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04002905
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002906 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002907#ifdef CONFIG_B43_BCMA
2908 case B43_BUS_BCMA:
Hauke Mehrtens0a64bae2013-03-21 16:19:45 +01002909 bcma_chipco_gpio_control(&dev->dev->bdev->bus->drv_cc, ~0, 0);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002910 break;
2911#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002912#ifdef CONFIG_B43_SSB
2913 case B43_BUS_SSB:
2914 gpiodev = b43_ssb_gpio_dev(dev);
2915 if (gpiodev)
2916 ssb_write32(gpiodev, B43_GPIO_CONTROL, 0);
2917 break;
2918#endif
2919 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002920}
2921
2922/* http://bcm-specs.sipsolutions.net/EnableMac */
Michael Bueschf5eda472008-04-20 16:03:32 +02002923void b43_mac_enable(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04002924{
Michael Buesch923fd702008-06-20 18:02:08 +02002925 if (b43_debug(dev, B43_DBG_FIRMWARE)) {
2926 u16 fwstate;
2927
2928 fwstate = b43_shm_read16(dev, B43_SHM_SHARED,
2929 B43_SHM_SH_UCODESTAT);
2930 if ((fwstate != B43_SHM_SH_UCODESTAT_SUSP) &&
2931 (fwstate != B43_SHM_SH_UCODESTAT_SLEEP)) {
2932 b43err(dev->wl, "b43_mac_enable(): The firmware "
2933 "should be suspended, but current state is %u\n",
2934 fwstate);
2935 }
2936 }
2937
Michael Buesche4d6b792007-09-18 15:39:42 -04002938 dev->mac_suspended--;
2939 B43_WARN_ON(dev->mac_suspended < 0);
2940 if (dev->mac_suspended == 0) {
Rafał Miłecki50566352012-01-02 19:31:21 +01002941 b43_maskset32(dev, B43_MMIO_MACCTL, ~0, B43_MACCTL_ENABLED);
Michael Buesche4d6b792007-09-18 15:39:42 -04002942 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON,
2943 B43_IRQ_MAC_SUSPENDED);
2944 /* Commit writes */
2945 b43_read32(dev, B43_MMIO_MACCTL);
2946 b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2947 b43_power_saving_ctl_bits(dev, 0);
2948 }
2949}
2950
2951/* http://bcm-specs.sipsolutions.net/SuspendMAC */
Michael Bueschf5eda472008-04-20 16:03:32 +02002952void b43_mac_suspend(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04002953{
2954 int i;
2955 u32 tmp;
2956
Michael Buesch05b64b32007-09-28 16:19:03 +02002957 might_sleep();
Michael Buesche4d6b792007-09-18 15:39:42 -04002958 B43_WARN_ON(dev->mac_suspended < 0);
Michael Buesch05b64b32007-09-28 16:19:03 +02002959
Michael Buesche4d6b792007-09-18 15:39:42 -04002960 if (dev->mac_suspended == 0) {
2961 b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
Rafał Miłecki50566352012-01-02 19:31:21 +01002962 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_ENABLED, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04002963 /* force pci to flush the write */
2964 b43_read32(dev, B43_MMIO_MACCTL);
Michael Bueschba380012008-04-15 21:13:36 +02002965 for (i = 35; i; i--) {
2966 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2967 if (tmp & B43_IRQ_MAC_SUSPENDED)
2968 goto out;
2969 udelay(10);
2970 }
2971 /* Hm, it seems this will take some time. Use msleep(). */
Michael Buesch05b64b32007-09-28 16:19:03 +02002972 for (i = 40; i; i--) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002973 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2974 if (tmp & B43_IRQ_MAC_SUSPENDED)
2975 goto out;
Michael Buesch05b64b32007-09-28 16:19:03 +02002976 msleep(1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002977 }
2978 b43err(dev->wl, "MAC suspend failed\n");
2979 }
Michael Buesch05b64b32007-09-28 16:19:03 +02002980out:
Michael Buesche4d6b792007-09-18 15:39:42 -04002981 dev->mac_suspended++;
2982}
2983
Rafał Miłecki858a1652011-05-10 16:05:33 +02002984/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/MacPhyClkSet */
2985void b43_mac_phy_clock_set(struct b43_wldev *dev, bool on)
2986{
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002987 u32 tmp;
2988
2989 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002990#ifdef CONFIG_B43_BCMA
2991 case B43_BUS_BCMA:
Rafał Miłecki36677872011-07-16 18:27:55 +02002992 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002993 if (on)
2994 tmp |= B43_BCMA_IOCTL_MACPHYCLKEN;
2995 else
2996 tmp &= ~B43_BCMA_IOCTL_MACPHYCLKEN;
Rafał Miłecki36677872011-07-16 18:27:55 +02002997 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002998 break;
2999#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02003000#ifdef CONFIG_B43_SSB
3001 case B43_BUS_SSB:
3002 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
3003 if (on)
3004 tmp |= B43_TMSLOW_MACPHYCLKEN;
3005 else
3006 tmp &= ~B43_TMSLOW_MACPHYCLKEN;
3007 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
3008 break;
3009#endif
3010 }
Rafał Miłecki858a1652011-05-10 16:05:33 +02003011}
3012
Rafał Miłeckic2cb2c42014-07-17 19:31:05 +02003013/* brcms_b_switch_macfreq */
3014void b43_mac_switch_freq(struct b43_wldev *dev, u8 spurmode)
3015{
3016 u16 chip_id = dev->dev->chip_id;
3017
Rafał Miłeckibc944502014-09-10 09:07:13 +02003018 if (chip_id == BCMA_CHIP_ID_BCM4331) {
3019 switch (spurmode) {
3020 case 2: /* 168 Mhz: 2^26/168 = 0x61862 */
3021 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0x1862);
3022 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0x6);
3023 break;
3024 case 1: /* 164 Mhz: 2^26/164 = 0x63e70 */
3025 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0x3e70);
3026 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0x6);
3027 break;
3028 default: /* 160 Mhz: 2^26/160 = 0x66666 */
3029 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0x6666);
3030 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0x6);
3031 break;
3032 }
3033 } else if (chip_id == BCMA_CHIP_ID_BCM43131 ||
Rafał Miłeckia67d19d2014-07-24 15:29:18 +02003034 chip_id == BCMA_CHIP_ID_BCM43217 ||
Rafał Miłeckic2cb2c42014-07-17 19:31:05 +02003035 chip_id == BCMA_CHIP_ID_BCM43222 ||
3036 chip_id == BCMA_CHIP_ID_BCM43224 ||
3037 chip_id == BCMA_CHIP_ID_BCM43225 ||
3038 chip_id == BCMA_CHIP_ID_BCM43227 ||
3039 chip_id == BCMA_CHIP_ID_BCM43228) {
3040 switch (spurmode) {
3041 case 2: /* 126 Mhz */
3042 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0x2082);
3043 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0x8);
3044 break;
3045 case 1: /* 123 Mhz */
3046 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0x5341);
3047 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0x8);
3048 break;
3049 default: /* 120 Mhz */
3050 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0x8889);
3051 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0x8);
3052 break;
3053 }
3054 } else if (dev->phy.type == B43_PHYTYPE_LCN) {
3055 switch (spurmode) {
3056 case 1: /* 82 Mhz */
3057 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0x7CE0);
3058 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0xC);
3059 break;
3060 default: /* 80 Mhz */
3061 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0xCCCD);
3062 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0xC);
3063 break;
3064 }
3065 }
3066}
3067
Michael Buesche4d6b792007-09-18 15:39:42 -04003068static void b43_adjust_opmode(struct b43_wldev *dev)
3069{
3070 struct b43_wl *wl = dev->wl;
3071 u32 ctl;
3072 u16 cfp_pretbtt;
3073
3074 ctl = b43_read32(dev, B43_MMIO_MACCTL);
3075 /* Reset status to STA infrastructure mode. */
3076 ctl &= ~B43_MACCTL_AP;
3077 ctl &= ~B43_MACCTL_KEEP_CTL;
3078 ctl &= ~B43_MACCTL_KEEP_BADPLCP;
3079 ctl &= ~B43_MACCTL_KEEP_BAD;
3080 ctl &= ~B43_MACCTL_PROMISC;
Johannes Berg4150c572007-09-17 01:29:23 -04003081 ctl &= ~B43_MACCTL_BEACPROMISC;
Michael Buesche4d6b792007-09-18 15:39:42 -04003082 ctl |= B43_MACCTL_INFRA;
3083
Johannes Berg05c914f2008-09-11 00:01:58 +02003084 if (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
3085 b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT))
Johannes Berg4150c572007-09-17 01:29:23 -04003086 ctl |= B43_MACCTL_AP;
Johannes Berg05c914f2008-09-11 00:01:58 +02003087 else if (b43_is_mode(wl, NL80211_IFTYPE_ADHOC))
Johannes Berg4150c572007-09-17 01:29:23 -04003088 ctl &= ~B43_MACCTL_INFRA;
3089
3090 if (wl->filter_flags & FIF_CONTROL)
Michael Buesche4d6b792007-09-18 15:39:42 -04003091 ctl |= B43_MACCTL_KEEP_CTL;
Johannes Berg4150c572007-09-17 01:29:23 -04003092 if (wl->filter_flags & FIF_FCSFAIL)
3093 ctl |= B43_MACCTL_KEEP_BAD;
3094 if (wl->filter_flags & FIF_PLCPFAIL)
3095 ctl |= B43_MACCTL_KEEP_BADPLCP;
3096 if (wl->filter_flags & FIF_PROMISC_IN_BSS)
Michael Buesche4d6b792007-09-18 15:39:42 -04003097 ctl |= B43_MACCTL_PROMISC;
Johannes Berg4150c572007-09-17 01:29:23 -04003098 if (wl->filter_flags & FIF_BCN_PRBRESP_PROMISC)
3099 ctl |= B43_MACCTL_BEACPROMISC;
3100
Michael Buesche4d6b792007-09-18 15:39:42 -04003101 /* Workaround: On old hardware the HW-MAC-address-filter
3102 * doesn't work properly, so always run promisc in filter
3103 * it in software. */
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003104 if (dev->dev->core_rev <= 4)
Michael Buesche4d6b792007-09-18 15:39:42 -04003105 ctl |= B43_MACCTL_PROMISC;
3106
3107 b43_write32(dev, B43_MMIO_MACCTL, ctl);
3108
3109 cfp_pretbtt = 2;
3110 if ((ctl & B43_MACCTL_INFRA) && !(ctl & B43_MACCTL_AP)) {
Rafał Miłeckic244e082011-05-18 02:06:41 +02003111 if (dev->dev->chip_id == 0x4306 &&
3112 dev->dev->chip_rev == 3)
Michael Buesche4d6b792007-09-18 15:39:42 -04003113 cfp_pretbtt = 100;
3114 else
3115 cfp_pretbtt = 50;
3116 }
3117 b43_write16(dev, 0x612, cfp_pretbtt);
Michael Buesch09ebe2f2009-09-12 00:52:48 +02003118
3119 /* FIXME: We don't currently implement the PMQ mechanism,
3120 * so always disable it. If we want to implement PMQ,
3121 * we need to enable it here (clear DISCPMQ) in AP mode.
3122 */
Rafał Miłecki50566352012-01-02 19:31:21 +01003123 if (0 /* ctl & B43_MACCTL_AP */)
3124 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_DISCPMQ, 0);
3125 else
3126 b43_maskset32(dev, B43_MMIO_MACCTL, ~0, B43_MACCTL_DISCPMQ);
Michael Buesche4d6b792007-09-18 15:39:42 -04003127}
3128
3129static void b43_rate_memory_write(struct b43_wldev *dev, u16 rate, int is_ofdm)
3130{
3131 u16 offset;
3132
3133 if (is_ofdm) {
3134 offset = 0x480;
3135 offset += (b43_plcp_get_ratecode_ofdm(rate) & 0x000F) * 2;
3136 } else {
3137 offset = 0x4C0;
3138 offset += (b43_plcp_get_ratecode_cck(rate) & 0x000F) * 2;
3139 }
3140 b43_shm_write16(dev, B43_SHM_SHARED, offset + 0x20,
3141 b43_shm_read16(dev, B43_SHM_SHARED, offset));
3142}
3143
3144static void b43_rate_memory_init(struct b43_wldev *dev)
3145{
3146 switch (dev->phy.type) {
3147 case B43_PHYTYPE_A:
3148 case B43_PHYTYPE_G:
Michael Buesch53a6e232008-01-13 21:23:44 +01003149 case B43_PHYTYPE_N:
Gábor Stefanik9d86a2d2009-08-14 14:54:46 +02003150 case B43_PHYTYPE_LP:
Rafał Miłecki6a461c22011-08-12 00:03:25 +02003151 case B43_PHYTYPE_HT:
Rafał Miłecki0b4ff452011-08-31 23:36:16 +02003152 case B43_PHYTYPE_LCN:
Michael Buesche4d6b792007-09-18 15:39:42 -04003153 b43_rate_memory_write(dev, B43_OFDM_RATE_6MB, 1);
3154 b43_rate_memory_write(dev, B43_OFDM_RATE_12MB, 1);
3155 b43_rate_memory_write(dev, B43_OFDM_RATE_18MB, 1);
3156 b43_rate_memory_write(dev, B43_OFDM_RATE_24MB, 1);
3157 b43_rate_memory_write(dev, B43_OFDM_RATE_36MB, 1);
3158 b43_rate_memory_write(dev, B43_OFDM_RATE_48MB, 1);
3159 b43_rate_memory_write(dev, B43_OFDM_RATE_54MB, 1);
3160 if (dev->phy.type == B43_PHYTYPE_A)
3161 break;
3162 /* fallthrough */
3163 case B43_PHYTYPE_B:
3164 b43_rate_memory_write(dev, B43_CCK_RATE_1MB, 0);
3165 b43_rate_memory_write(dev, B43_CCK_RATE_2MB, 0);
3166 b43_rate_memory_write(dev, B43_CCK_RATE_5MB, 0);
3167 b43_rate_memory_write(dev, B43_CCK_RATE_11MB, 0);
3168 break;
3169 default:
3170 B43_WARN_ON(1);
3171 }
3172}
3173
Michael Buesch5042c502008-04-05 15:05:00 +02003174/* Set the default values for the PHY TX Control Words. */
3175static void b43_set_phytxctl_defaults(struct b43_wldev *dev)
3176{
3177 u16 ctl = 0;
3178
3179 ctl |= B43_TXH_PHY_ENC_CCK;
3180 ctl |= B43_TXH_PHY_ANT01AUTO;
3181 ctl |= B43_TXH_PHY_TXPWR;
3182
3183 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, ctl);
3184 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, ctl);
3185 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, ctl);
3186}
3187
Michael Buesche4d6b792007-09-18 15:39:42 -04003188/* Set the TX-Antenna for management frames sent by firmware. */
3189static void b43_mgmtframe_txantenna(struct b43_wldev *dev, int antenna)
3190{
Michael Buesch5042c502008-04-05 15:05:00 +02003191 u16 ant;
Michael Buesche4d6b792007-09-18 15:39:42 -04003192 u16 tmp;
3193
Michael Buesch5042c502008-04-05 15:05:00 +02003194 ant = b43_antenna_to_phyctl(antenna);
Michael Buesche4d6b792007-09-18 15:39:42 -04003195
Michael Buesche4d6b792007-09-18 15:39:42 -04003196 /* For ACK/CTS */
3197 tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL);
Michael Buescheb189d8b2008-01-28 14:47:41 -08003198 tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
Michael Buesche4d6b792007-09-18 15:39:42 -04003199 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, tmp);
3200 /* For Probe Resposes */
3201 tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL);
Michael Buescheb189d8b2008-01-28 14:47:41 -08003202 tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
Michael Buesche4d6b792007-09-18 15:39:42 -04003203 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, tmp);
3204}
3205
3206/* This is the opposite of b43_chip_init() */
3207static void b43_chip_exit(struct b43_wldev *dev)
3208{
Michael Bueschfb111372008-09-02 13:00:34 +02003209 b43_phy_exit(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003210 b43_gpio_cleanup(dev);
3211 /* firmware is released later */
3212}
3213
3214/* Initialize the chip
3215 * http://bcm-specs.sipsolutions.net/ChipInit
3216 */
3217static int b43_chip_init(struct b43_wldev *dev)
3218{
3219 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +02003220 int err;
Rafał Miłecki858a1652011-05-10 16:05:33 +02003221 u32 macctl;
Michael Buesche4d6b792007-09-18 15:39:42 -04003222 u16 value16;
3223
Michael Buesch1f7d87b2008-01-22 20:23:34 +01003224 /* Initialize the MAC control */
3225 macctl = B43_MACCTL_IHR_ENABLED | B43_MACCTL_SHM_ENABLED;
3226 if (dev->phy.gmode)
3227 macctl |= B43_MACCTL_GMODE;
3228 macctl |= B43_MACCTL_INFRA;
3229 b43_write32(dev, B43_MMIO_MACCTL, macctl);
Michael Buesche4d6b792007-09-18 15:39:42 -04003230
Michael Buesche4d6b792007-09-18 15:39:42 -04003231 err = b43_upload_microcode(dev);
3232 if (err)
3233 goto out; /* firmware is released later */
3234
3235 err = b43_gpio_init(dev);
3236 if (err)
3237 goto out; /* firmware is released later */
Michael Buesch21954c32007-09-27 15:31:40 +02003238
Michael Buesche4d6b792007-09-18 15:39:42 -04003239 err = b43_upload_initvals(dev);
3240 if (err)
Larry Finger1a8d12272007-12-14 13:59:11 +01003241 goto err_gpio_clean;
Michael Buesche4d6b792007-09-18 15:39:42 -04003242
Rafał Miłecki0f684232014-05-17 23:24:53 +02003243 err = b43_upload_initvals_band(dev);
3244 if (err)
3245 goto err_gpio_clean;
3246
Michael Buesch0b7dcd92008-09-03 12:31:54 +02003247 /* Turn the Analog on and initialize the PHY. */
3248 phy->ops->switch_analog(dev, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04003249 err = b43_phy_init(dev);
3250 if (err)
Michael Bueschef1a6282008-08-27 18:53:02 +02003251 goto err_gpio_clean;
Michael Buesche4d6b792007-09-18 15:39:42 -04003252
Michael Bueschef1a6282008-08-27 18:53:02 +02003253 /* Disable Interference Mitigation. */
3254 if (phy->ops->interf_mitigation)
3255 phy->ops->interf_mitigation(dev, B43_INTERFMODE_NONE);
Michael Buesche4d6b792007-09-18 15:39:42 -04003256
Michael Bueschef1a6282008-08-27 18:53:02 +02003257 /* Select the antennae */
3258 if (phy->ops->set_rx_antenna)
3259 phy->ops->set_rx_antenna(dev, B43_ANTENNA_DEFAULT);
Michael Buesche4d6b792007-09-18 15:39:42 -04003260 b43_mgmtframe_txantenna(dev, B43_ANTENNA_DEFAULT);
3261
3262 if (phy->type == B43_PHYTYPE_B) {
3263 value16 = b43_read16(dev, 0x005E);
3264 value16 |= 0x0004;
3265 b43_write16(dev, 0x005E, value16);
3266 }
3267 b43_write32(dev, 0x0100, 0x01000000);
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003268 if (dev->dev->core_rev < 5)
Michael Buesche4d6b792007-09-18 15:39:42 -04003269 b43_write32(dev, 0x010C, 0x01000000);
3270
Rafał Miłecki50566352012-01-02 19:31:21 +01003271 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_INFRA, 0);
3272 b43_maskset32(dev, B43_MMIO_MACCTL, ~0, B43_MACCTL_INFRA);
Michael Buesche4d6b792007-09-18 15:39:42 -04003273
Michael Buesche4d6b792007-09-18 15:39:42 -04003274 /* Probe Response Timeout value */
3275 /* FIXME: Default to 0, has to be set by ioctl probably... :-/ */
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01003276 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRMAXTIME, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04003277
3278 /* Initially set the wireless operation mode. */
3279 b43_adjust_opmode(dev);
3280
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003281 if (dev->dev->core_rev < 3) {
Michael Buesche4d6b792007-09-18 15:39:42 -04003282 b43_write16(dev, 0x060E, 0x0000);
3283 b43_write16(dev, 0x0610, 0x8000);
3284 b43_write16(dev, 0x0604, 0x0000);
3285 b43_write16(dev, 0x0606, 0x0200);
3286 } else {
3287 b43_write32(dev, 0x0188, 0x80000000);
3288 b43_write32(dev, 0x018C, 0x02000000);
3289 }
3290 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, 0x00004000);
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02003291 b43_write32(dev, B43_MMIO_DMA0_IRQ_MASK, 0x0001FC00);
Michael Buesche4d6b792007-09-18 15:39:42 -04003292 b43_write32(dev, B43_MMIO_DMA1_IRQ_MASK, 0x0000DC00);
3293 b43_write32(dev, B43_MMIO_DMA2_IRQ_MASK, 0x0000DC00);
3294 b43_write32(dev, B43_MMIO_DMA3_IRQ_MASK, 0x0001DC00);
3295 b43_write32(dev, B43_MMIO_DMA4_IRQ_MASK, 0x0000DC00);
3296 b43_write32(dev, B43_MMIO_DMA5_IRQ_MASK, 0x0000DC00);
3297
Rafał Miłecki858a1652011-05-10 16:05:33 +02003298 b43_mac_phy_clock_set(dev, true);
Michael Buesche4d6b792007-09-18 15:39:42 -04003299
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02003300 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02003301#ifdef CONFIG_B43_BCMA
3302 case B43_BUS_BCMA:
3303 /* FIXME: 0xE74 is quite common, but should be read from CC */
3304 b43_write16(dev, B43_MMIO_POWERUP_DELAY, 0xE74);
3305 break;
3306#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02003307#ifdef CONFIG_B43_SSB
3308 case B43_BUS_SSB:
3309 b43_write16(dev, B43_MMIO_POWERUP_DELAY,
3310 dev->dev->sdev->bus->chipco.fast_pwrup_delay);
3311 break;
3312#endif
3313 }
Michael Buesche4d6b792007-09-18 15:39:42 -04003314
3315 err = 0;
3316 b43dbg(dev->wl, "Chip initialized\n");
Michael Buesch21954c32007-09-27 15:31:40 +02003317out:
Michael Buesche4d6b792007-09-18 15:39:42 -04003318 return err;
3319
Larry Finger1a8d12272007-12-14 13:59:11 +01003320err_gpio_clean:
Michael Buesche4d6b792007-09-18 15:39:42 -04003321 b43_gpio_cleanup(dev);
Michael Buesch21954c32007-09-27 15:31:40 +02003322 return err;
Michael Buesche4d6b792007-09-18 15:39:42 -04003323}
3324
Michael Buesche4d6b792007-09-18 15:39:42 -04003325static void b43_periodic_every60sec(struct b43_wldev *dev)
3326{
Michael Bueschef1a6282008-08-27 18:53:02 +02003327 const struct b43_phy_operations *ops = dev->phy.ops;
Michael Buesche4d6b792007-09-18 15:39:42 -04003328
Michael Bueschef1a6282008-08-27 18:53:02 +02003329 if (ops->pwork_60sec)
3330 ops->pwork_60sec(dev);
Michael Buesch18c8ade2008-08-28 19:33:40 +02003331
3332 /* Force check the TX power emission now. */
3333 b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME);
Michael Buesche4d6b792007-09-18 15:39:42 -04003334}
3335
3336static void b43_periodic_every30sec(struct b43_wldev *dev)
3337{
3338 /* Update device statistics. */
3339 b43_calculate_link_quality(dev);
3340}
3341
3342static void b43_periodic_every15sec(struct b43_wldev *dev)
3343{
3344 struct b43_phy *phy = &dev->phy;
Michael Buesch9b839a72008-06-20 17:44:02 +02003345 u16 wdr;
3346
3347 if (dev->fw.opensource) {
3348 /* Check if the firmware is still alive.
3349 * It will reset the watchdog counter to 0 in its idle loop. */
3350 wdr = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_WATCHDOG_REG);
3351 if (unlikely(wdr)) {
3352 b43err(dev->wl, "Firmware watchdog: The firmware died!\n");
3353 b43_controller_restart(dev, "Firmware watchdog");
3354 return;
3355 } else {
3356 b43_shm_write16(dev, B43_SHM_SCRATCH,
3357 B43_WATCHDOG_REG, 1);
3358 }
3359 }
Michael Buesche4d6b792007-09-18 15:39:42 -04003360
Michael Bueschef1a6282008-08-27 18:53:02 +02003361 if (phy->ops->pwork_15sec)
3362 phy->ops->pwork_15sec(dev);
3363
Stefano Brivio00e0b8c2007-11-25 11:10:33 +01003364 atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
3365 wmb();
Michael Buesch990b86f2009-09-12 00:48:03 +02003366
3367#if B43_DEBUG
3368 if (b43_debug(dev, B43_DBG_VERBOSESTATS)) {
3369 unsigned int i;
3370
3371 b43dbg(dev->wl, "Stats: %7u IRQs/sec, %7u TX/sec, %7u RX/sec\n",
3372 dev->irq_count / 15,
3373 dev->tx_count / 15,
3374 dev->rx_count / 15);
3375 dev->irq_count = 0;
3376 dev->tx_count = 0;
3377 dev->rx_count = 0;
3378 for (i = 0; i < ARRAY_SIZE(dev->irq_bit_count); i++) {
3379 if (dev->irq_bit_count[i]) {
3380 b43dbg(dev->wl, "Stats: %7u IRQ-%02u/sec (0x%08X)\n",
3381 dev->irq_bit_count[i] / 15, i, (1 << i));
3382 dev->irq_bit_count[i] = 0;
3383 }
3384 }
3385 }
3386#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04003387}
3388
Michael Buesche4d6b792007-09-18 15:39:42 -04003389static void do_periodic_work(struct b43_wldev *dev)
3390{
3391 unsigned int state;
3392
3393 state = dev->periodic_state;
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003394 if (state % 4 == 0)
Michael Buesche4d6b792007-09-18 15:39:42 -04003395 b43_periodic_every60sec(dev);
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003396 if (state % 2 == 0)
Michael Buesche4d6b792007-09-18 15:39:42 -04003397 b43_periodic_every30sec(dev);
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003398 b43_periodic_every15sec(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003399}
3400
Michael Buesch05b64b32007-09-28 16:19:03 +02003401/* Periodic work locking policy:
3402 * The whole periodic work handler is protected by
3403 * wl->mutex. If another lock is needed somewhere in the
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02003404 * pwork callchain, it's acquired in-place, where it's needed.
Michael Buesche4d6b792007-09-18 15:39:42 -04003405 */
Michael Buesche4d6b792007-09-18 15:39:42 -04003406static void b43_periodic_work_handler(struct work_struct *work)
3407{
Michael Buesch05b64b32007-09-28 16:19:03 +02003408 struct b43_wldev *dev = container_of(work, struct b43_wldev,
3409 periodic_work.work);
3410 struct b43_wl *wl = dev->wl;
3411 unsigned long delay;
Michael Buesche4d6b792007-09-18 15:39:42 -04003412
Michael Buesch05b64b32007-09-28 16:19:03 +02003413 mutex_lock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003414
3415 if (unlikely(b43_status(dev) != B43_STAT_STARTED))
3416 goto out;
3417 if (b43_debug(dev, B43_DBG_PWORK_STOP))
3418 goto out_requeue;
3419
Michael Buesch05b64b32007-09-28 16:19:03 +02003420 do_periodic_work(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003421
Michael Buesche4d6b792007-09-18 15:39:42 -04003422 dev->periodic_state++;
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003423out_requeue:
Michael Buesche4d6b792007-09-18 15:39:42 -04003424 if (b43_debug(dev, B43_DBG_PWORK_FAST))
3425 delay = msecs_to_jiffies(50);
3426 else
Anton Blanchard82cd6822007-10-15 00:42:23 -05003427 delay = round_jiffies_relative(HZ * 15);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003428 ieee80211_queue_delayed_work(wl->hw, &dev->periodic_work, delay);
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003429out:
Michael Buesch05b64b32007-09-28 16:19:03 +02003430 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003431}
3432
3433static void b43_periodic_tasks_setup(struct b43_wldev *dev)
3434{
3435 struct delayed_work *work = &dev->periodic_work;
3436
3437 dev->periodic_state = 0;
3438 INIT_DELAYED_WORK(work, b43_periodic_work_handler);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003439 ieee80211_queue_delayed_work(dev->wl->hw, work, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04003440}
3441
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003442/* Check if communication with the device works correctly. */
Michael Buesche4d6b792007-09-18 15:39:42 -04003443static int b43_validate_chipaccess(struct b43_wldev *dev)
3444{
Michael Bueschf62ae6c2009-07-31 20:51:41 +02003445 u32 v, backup0, backup4;
Michael Buesche4d6b792007-09-18 15:39:42 -04003446
Michael Bueschf62ae6c2009-07-31 20:51:41 +02003447 backup0 = b43_shm_read32(dev, B43_SHM_SHARED, 0);
3448 backup4 = b43_shm_read32(dev, B43_SHM_SHARED, 4);
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003449
3450 /* Check for read/write and endianness problems. */
Michael Buesche4d6b792007-09-18 15:39:42 -04003451 b43_shm_write32(dev, B43_SHM_SHARED, 0, 0x55AAAA55);
3452 if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0x55AAAA55)
3453 goto error;
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003454 b43_shm_write32(dev, B43_SHM_SHARED, 0, 0xAA5555AA);
3455 if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0xAA5555AA)
Michael Buesche4d6b792007-09-18 15:39:42 -04003456 goto error;
3457
Michael Bueschf62ae6c2009-07-31 20:51:41 +02003458 /* Check if unaligned 32bit SHM_SHARED access works properly.
3459 * However, don't bail out on failure, because it's noncritical. */
3460 b43_shm_write16(dev, B43_SHM_SHARED, 0, 0x1122);
3461 b43_shm_write16(dev, B43_SHM_SHARED, 2, 0x3344);
3462 b43_shm_write16(dev, B43_SHM_SHARED, 4, 0x5566);
3463 b43_shm_write16(dev, B43_SHM_SHARED, 6, 0x7788);
3464 if (b43_shm_read32(dev, B43_SHM_SHARED, 2) != 0x55663344)
3465 b43warn(dev->wl, "Unaligned 32bit SHM read access is broken\n");
3466 b43_shm_write32(dev, B43_SHM_SHARED, 2, 0xAABBCCDD);
3467 if (b43_shm_read16(dev, B43_SHM_SHARED, 0) != 0x1122 ||
3468 b43_shm_read16(dev, B43_SHM_SHARED, 2) != 0xCCDD ||
3469 b43_shm_read16(dev, B43_SHM_SHARED, 4) != 0xAABB ||
3470 b43_shm_read16(dev, B43_SHM_SHARED, 6) != 0x7788)
3471 b43warn(dev->wl, "Unaligned 32bit SHM write access is broken\n");
3472
3473 b43_shm_write32(dev, B43_SHM_SHARED, 0, backup0);
3474 b43_shm_write32(dev, B43_SHM_SHARED, 4, backup4);
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003475
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003476 if ((dev->dev->core_rev >= 3) && (dev->dev->core_rev <= 10)) {
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003477 /* The 32bit register shadows the two 16bit registers
3478 * with update sideeffects. Validate this. */
3479 b43_write16(dev, B43_MMIO_TSF_CFP_START, 0xAAAA);
3480 b43_write32(dev, B43_MMIO_TSF_CFP_START, 0xCCCCBBBB);
3481 if (b43_read16(dev, B43_MMIO_TSF_CFP_START_LOW) != 0xBBBB)
3482 goto error;
3483 if (b43_read16(dev, B43_MMIO_TSF_CFP_START_HIGH) != 0xCCCC)
3484 goto error;
3485 }
3486 b43_write32(dev, B43_MMIO_TSF_CFP_START, 0);
3487
3488 v = b43_read32(dev, B43_MMIO_MACCTL);
3489 v |= B43_MACCTL_GMODE;
3490 if (v != (B43_MACCTL_GMODE | B43_MACCTL_IHR_ENABLED))
Michael Buesche4d6b792007-09-18 15:39:42 -04003491 goto error;
3492
3493 return 0;
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003494error:
Michael Buesche4d6b792007-09-18 15:39:42 -04003495 b43err(dev->wl, "Failed to validate the chipaccess\n");
3496 return -ENODEV;
3497}
3498
3499static void b43_security_init(struct b43_wldev *dev)
3500{
Michael Buesche4d6b792007-09-18 15:39:42 -04003501 dev->ktp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_KTP);
3502 /* KTP is a word address, but we address SHM bytewise.
3503 * So multiply by two.
3504 */
3505 dev->ktp *= 2;
Michael Buesch66d2d082009-08-06 10:36:50 +02003506 /* Number of RCMTA address slots */
3507 b43_write16(dev, B43_MMIO_RCMTA_COUNT, B43_NR_PAIRWISE_KEYS);
3508 /* Clear the key memory. */
Michael Buesche4d6b792007-09-18 15:39:42 -04003509 b43_clear_keys(dev);
3510}
3511
Michael Buesch616de352009-03-29 13:19:31 +02003512#ifdef CONFIG_B43_HWRNG
John Daiker99da1852009-02-24 02:16:42 -08003513static int b43_rng_read(struct hwrng *rng, u32 *data)
Michael Buesche4d6b792007-09-18 15:39:42 -04003514{
3515 struct b43_wl *wl = (struct b43_wl *)rng->priv;
Michael Buescha78b3bb2009-09-11 21:44:05 +02003516 struct b43_wldev *dev;
3517 int count = -ENODEV;
Michael Buesche4d6b792007-09-18 15:39:42 -04003518
Michael Buescha78b3bb2009-09-11 21:44:05 +02003519 mutex_lock(&wl->mutex);
3520 dev = wl->current_dev;
3521 if (likely(dev && b43_status(dev) >= B43_STAT_INITIALIZED)) {
3522 *data = b43_read16(dev, B43_MMIO_RNG);
3523 count = sizeof(u16);
3524 }
3525 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003526
Michael Buescha78b3bb2009-09-11 21:44:05 +02003527 return count;
Michael Buesche4d6b792007-09-18 15:39:42 -04003528}
Michael Buesch616de352009-03-29 13:19:31 +02003529#endif /* CONFIG_B43_HWRNG */
Michael Buesche4d6b792007-09-18 15:39:42 -04003530
Rafael J. Wysockib844eba2008-03-23 20:28:24 +01003531static void b43_rng_exit(struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04003532{
Michael Buesch616de352009-03-29 13:19:31 +02003533#ifdef CONFIG_B43_HWRNG
Michael Buesche4d6b792007-09-18 15:39:42 -04003534 if (wl->rng_initialized)
Rafael J. Wysockib844eba2008-03-23 20:28:24 +01003535 hwrng_unregister(&wl->rng);
Michael Buesch616de352009-03-29 13:19:31 +02003536#endif /* CONFIG_B43_HWRNG */
Michael Buesche4d6b792007-09-18 15:39:42 -04003537}
3538
3539static int b43_rng_init(struct b43_wl *wl)
3540{
Michael Buesch616de352009-03-29 13:19:31 +02003541 int err = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003542
Michael Buesch616de352009-03-29 13:19:31 +02003543#ifdef CONFIG_B43_HWRNG
Michael Buesche4d6b792007-09-18 15:39:42 -04003544 snprintf(wl->rng_name, ARRAY_SIZE(wl->rng_name),
3545 "%s_%s", KBUILD_MODNAME, wiphy_name(wl->hw->wiphy));
3546 wl->rng.name = wl->rng_name;
3547 wl->rng.data_read = b43_rng_read;
3548 wl->rng.priv = (unsigned long)wl;
Rusty Russell3db1cd52011-12-19 13:56:45 +00003549 wl->rng_initialized = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04003550 err = hwrng_register(&wl->rng);
3551 if (err) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00003552 wl->rng_initialized = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04003553 b43err(wl, "Failed to register the random "
3554 "number generator (%d)\n", err);
3555 }
Michael Buesch616de352009-03-29 13:19:31 +02003556#endif /* CONFIG_B43_HWRNG */
Michael Buesche4d6b792007-09-18 15:39:42 -04003557
3558 return err;
3559}
3560
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003561static void b43_tx_work(struct work_struct *work)
Michael Buesche4d6b792007-09-18 15:39:42 -04003562{
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003563 struct b43_wl *wl = container_of(work, struct b43_wl, tx_work);
3564 struct b43_wldev *dev;
3565 struct sk_buff *skb;
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003566 int queue_num;
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003567 int err = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003568
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003569 mutex_lock(&wl->mutex);
3570 dev = wl->current_dev;
3571 if (unlikely(!dev || b43_status(dev) < B43_STAT_STARTED)) {
3572 mutex_unlock(&wl->mutex);
3573 return;
Michael Buesch5100d5a2008-03-29 21:01:16 +01003574 }
Michael Buesch21a75d72008-04-25 19:29:08 +02003575
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003576 for (queue_num = 0; queue_num < B43_QOS_QUEUE_NUM; queue_num++) {
3577 while (skb_queue_len(&wl->tx_queue[queue_num])) {
3578 skb = skb_dequeue(&wl->tx_queue[queue_num]);
3579 if (b43_using_pio_transfers(dev))
3580 err = b43_pio_tx(dev, skb);
3581 else
3582 err = b43_dma_tx(dev, skb);
3583 if (err == -ENOSPC) {
3584 wl->tx_queue_stopped[queue_num] = 1;
3585 ieee80211_stop_queue(wl->hw, queue_num);
3586 skb_queue_head(&wl->tx_queue[queue_num], skb);
3587 break;
3588 }
3589 if (unlikely(err))
Felix Fietkau78f18df2012-12-10 17:40:21 +01003590 ieee80211_free_txskb(wl->hw, skb);
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003591 err = 0;
3592 }
Michael Buesch21a75d72008-04-25 19:29:08 +02003593
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003594 if (!err)
3595 wl->tx_queue_stopped[queue_num] = 0;
Michael Buesch21a75d72008-04-25 19:29:08 +02003596 }
3597
Michael Buesch990b86f2009-09-12 00:48:03 +02003598#if B43_DEBUG
3599 dev->tx_count++;
3600#endif
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003601 mutex_unlock(&wl->mutex);
3602}
Michael Buesch21a75d72008-04-25 19:29:08 +02003603
Johannes Berg7bb45682011-02-24 14:42:06 +01003604static void b43_op_tx(struct ieee80211_hw *hw,
Thomas Huehn36323f82012-07-23 21:33:42 +02003605 struct ieee80211_tx_control *control,
3606 struct sk_buff *skb)
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003607{
3608 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Bueschc9e8eae2008-06-15 15:17:29 +02003609
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003610 if (unlikely(skb->len < 2 + 2 + 6)) {
3611 /* Too short, this can't be a valid frame. */
Felix Fietkau78f18df2012-12-10 17:40:21 +01003612 ieee80211_free_txskb(hw, skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01003613 return;
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003614 }
3615 B43_WARN_ON(skb_shinfo(skb)->nr_frags);
3616
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003617 skb_queue_tail(&wl->tx_queue[skb->queue_mapping], skb);
3618 if (!wl->tx_queue_stopped[skb->queue_mapping]) {
3619 ieee80211_queue_work(wl->hw, &wl->tx_work);
3620 } else {
3621 ieee80211_stop_queue(wl->hw, skb->queue_mapping);
3622 }
Michael Buesche4d6b792007-09-18 15:39:42 -04003623}
3624
Michael Buesche6f5b932008-03-05 21:18:49 +01003625static void b43_qos_params_upload(struct b43_wldev *dev,
3626 const struct ieee80211_tx_queue_params *p,
3627 u16 shm_offset)
3628{
3629 u16 params[B43_NR_QOSPARAMS];
Johannes Berg0b576642008-07-15 02:08:24 -07003630 int bslots, tmp;
Michael Buesche6f5b932008-03-05 21:18:49 +01003631 unsigned int i;
3632
Michael Bueschb0544eb2009-09-06 15:42:45 +02003633 if (!dev->qos_enabled)
3634 return;
3635
Johannes Berg0b576642008-07-15 02:08:24 -07003636 bslots = b43_read16(dev, B43_MMIO_RNG) & p->cw_min;
Michael Buesche6f5b932008-03-05 21:18:49 +01003637
3638 memset(&params, 0, sizeof(params));
3639
3640 params[B43_QOSPARAM_TXOP] = p->txop * 32;
Johannes Berg0b576642008-07-15 02:08:24 -07003641 params[B43_QOSPARAM_CWMIN] = p->cw_min;
3642 params[B43_QOSPARAM_CWMAX] = p->cw_max;
3643 params[B43_QOSPARAM_CWCUR] = p->cw_min;
3644 params[B43_QOSPARAM_AIFS] = p->aifs;
Michael Buesche6f5b932008-03-05 21:18:49 +01003645 params[B43_QOSPARAM_BSLOTS] = bslots;
Johannes Berg0b576642008-07-15 02:08:24 -07003646 params[B43_QOSPARAM_REGGAP] = bslots + p->aifs;
Michael Buesche6f5b932008-03-05 21:18:49 +01003647
3648 for (i = 0; i < ARRAY_SIZE(params); i++) {
3649 if (i == B43_QOSPARAM_STATUS) {
3650 tmp = b43_shm_read16(dev, B43_SHM_SHARED,
3651 shm_offset + (i * 2));
3652 /* Mark the parameters as updated. */
3653 tmp |= 0x100;
3654 b43_shm_write16(dev, B43_SHM_SHARED,
3655 shm_offset + (i * 2),
3656 tmp);
3657 } else {
3658 b43_shm_write16(dev, B43_SHM_SHARED,
3659 shm_offset + (i * 2),
3660 params[i]);
3661 }
3662 }
3663}
3664
Michael Bueschc40c1122008-09-06 16:21:47 +02003665/* Mapping of mac80211 queue numbers to b43 QoS SHM offsets. */
3666static const u16 b43_qos_shm_offsets[] = {
3667 /* [mac80211-queue-nr] = SHM_OFFSET, */
3668 [0] = B43_QOS_VOICE,
3669 [1] = B43_QOS_VIDEO,
3670 [2] = B43_QOS_BESTEFFORT,
3671 [3] = B43_QOS_BACKGROUND,
3672};
3673
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003674/* Update all QOS parameters in hardware. */
3675static void b43_qos_upload_all(struct b43_wldev *dev)
Michael Buesche6f5b932008-03-05 21:18:49 +01003676{
3677 struct b43_wl *wl = dev->wl;
3678 struct b43_qos_params *params;
Michael Buesche6f5b932008-03-05 21:18:49 +01003679 unsigned int i;
3680
Michael Bueschb0544eb2009-09-06 15:42:45 +02003681 if (!dev->qos_enabled)
3682 return;
3683
Michael Bueschc40c1122008-09-06 16:21:47 +02003684 BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
3685 ARRAY_SIZE(wl->qos_params));
Michael Buesche6f5b932008-03-05 21:18:49 +01003686
3687 b43_mac_suspend(dev);
Michael Buesche6f5b932008-03-05 21:18:49 +01003688 for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
3689 params = &(wl->qos_params[i]);
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003690 b43_qos_params_upload(dev, &(params->p),
3691 b43_qos_shm_offsets[i]);
Michael Buesche6f5b932008-03-05 21:18:49 +01003692 }
Michael Buesche6f5b932008-03-05 21:18:49 +01003693 b43_mac_enable(dev);
3694}
3695
3696static void b43_qos_clear(struct b43_wl *wl)
3697{
3698 struct b43_qos_params *params;
3699 unsigned int i;
3700
Michael Bueschc40c1122008-09-06 16:21:47 +02003701 /* Initialize QoS parameters to sane defaults. */
3702
3703 BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
3704 ARRAY_SIZE(wl->qos_params));
3705
Michael Buesche6f5b932008-03-05 21:18:49 +01003706 for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
3707 params = &(wl->qos_params[i]);
3708
Michael Bueschc40c1122008-09-06 16:21:47 +02003709 switch (b43_qos_shm_offsets[i]) {
3710 case B43_QOS_VOICE:
3711 params->p.txop = 0;
3712 params->p.aifs = 2;
3713 params->p.cw_min = 0x0001;
3714 params->p.cw_max = 0x0001;
3715 break;
3716 case B43_QOS_VIDEO:
3717 params->p.txop = 0;
3718 params->p.aifs = 2;
3719 params->p.cw_min = 0x0001;
3720 params->p.cw_max = 0x0001;
3721 break;
3722 case B43_QOS_BESTEFFORT:
3723 params->p.txop = 0;
3724 params->p.aifs = 3;
3725 params->p.cw_min = 0x0001;
3726 params->p.cw_max = 0x03FF;
3727 break;
3728 case B43_QOS_BACKGROUND:
3729 params->p.txop = 0;
3730 params->p.aifs = 7;
3731 params->p.cw_min = 0x0001;
3732 params->p.cw_max = 0x03FF;
3733 break;
3734 default:
3735 B43_WARN_ON(1);
3736 }
Michael Buesche6f5b932008-03-05 21:18:49 +01003737 }
3738}
3739
3740/* Initialize the core's QOS capabilities */
3741static void b43_qos_init(struct b43_wldev *dev)
3742{
Michael Bueschb0544eb2009-09-06 15:42:45 +02003743 if (!dev->qos_enabled) {
3744 /* Disable QOS support. */
3745 b43_hf_write(dev, b43_hf_read(dev) & ~B43_HF_EDCF);
3746 b43_write16(dev, B43_MMIO_IFSCTL,
3747 b43_read16(dev, B43_MMIO_IFSCTL)
3748 & ~B43_MMIO_IFSCTL_USE_EDCF);
3749 b43dbg(dev->wl, "QoS disabled\n");
3750 return;
3751 }
3752
Michael Buesche6f5b932008-03-05 21:18:49 +01003753 /* Upload the current QOS parameters. */
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003754 b43_qos_upload_all(dev);
Michael Buesche6f5b932008-03-05 21:18:49 +01003755
3756 /* Enable QOS support. */
3757 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_EDCF);
3758 b43_write16(dev, B43_MMIO_IFSCTL,
3759 b43_read16(dev, B43_MMIO_IFSCTL)
3760 | B43_MMIO_IFSCTL_USE_EDCF);
Michael Bueschb0544eb2009-09-06 15:42:45 +02003761 b43dbg(dev->wl, "QoS enabled\n");
Michael Buesche6f5b932008-03-05 21:18:49 +01003762}
3763
Eliad Peller8a3a3c82011-10-02 10:15:52 +02003764static int b43_op_conf_tx(struct ieee80211_hw *hw,
3765 struct ieee80211_vif *vif, u16 _queue,
Michael Buesch40faacc2007-10-28 16:29:32 +01003766 const struct ieee80211_tx_queue_params *params)
Michael Buesche4d6b792007-09-18 15:39:42 -04003767{
Michael Buesche6f5b932008-03-05 21:18:49 +01003768 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003769 struct b43_wldev *dev;
Michael Buesche6f5b932008-03-05 21:18:49 +01003770 unsigned int queue = (unsigned int)_queue;
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003771 int err = -ENODEV;
Michael Buesche6f5b932008-03-05 21:18:49 +01003772
3773 if (queue >= ARRAY_SIZE(wl->qos_params)) {
3774 /* Queue not available or don't support setting
3775 * params on this queue. Return success to not
3776 * confuse mac80211. */
3777 return 0;
3778 }
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003779 BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
3780 ARRAY_SIZE(wl->qos_params));
Michael Buesche6f5b932008-03-05 21:18:49 +01003781
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003782 mutex_lock(&wl->mutex);
3783 dev = wl->current_dev;
3784 if (unlikely(!dev || (b43_status(dev) < B43_STAT_INITIALIZED)))
3785 goto out_unlock;
Michael Buesche6f5b932008-03-05 21:18:49 +01003786
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003787 memcpy(&(wl->qos_params[queue].p), params, sizeof(*params));
3788 b43_mac_suspend(dev);
3789 b43_qos_params_upload(dev, &(wl->qos_params[queue].p),
3790 b43_qos_shm_offsets[queue]);
3791 b43_mac_enable(dev);
3792 err = 0;
Michael Buesche6f5b932008-03-05 21:18:49 +01003793
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003794out_unlock:
3795 mutex_unlock(&wl->mutex);
3796
3797 return err;
Michael Buesche4d6b792007-09-18 15:39:42 -04003798}
3799
Michael Buesch40faacc2007-10-28 16:29:32 +01003800static int b43_op_get_stats(struct ieee80211_hw *hw,
3801 struct ieee80211_low_level_stats *stats)
Michael Buesche4d6b792007-09-18 15:39:42 -04003802{
3803 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04003804
Michael Buesch36dbd952009-09-04 22:51:29 +02003805 mutex_lock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003806 memcpy(stats, &wl->ieee_stats, sizeof(*stats));
Michael Buesch36dbd952009-09-04 22:51:29 +02003807 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003808
3809 return 0;
3810}
3811
Eliad Peller37a41b42011-09-21 14:06:11 +03003812static u64 b43_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003813{
3814 struct b43_wl *wl = hw_to_b43_wl(hw);
3815 struct b43_wldev *dev;
3816 u64 tsf;
3817
3818 mutex_lock(&wl->mutex);
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003819 dev = wl->current_dev;
3820
3821 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED))
3822 b43_tsf_read(dev, &tsf);
3823 else
3824 tsf = 0;
3825
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003826 mutex_unlock(&wl->mutex);
3827
3828 return tsf;
3829}
3830
Eliad Peller37a41b42011-09-21 14:06:11 +03003831static void b43_op_set_tsf(struct ieee80211_hw *hw,
3832 struct ieee80211_vif *vif, u64 tsf)
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003833{
3834 struct b43_wl *wl = hw_to_b43_wl(hw);
3835 struct b43_wldev *dev;
3836
3837 mutex_lock(&wl->mutex);
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003838 dev = wl->current_dev;
3839
3840 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED))
3841 b43_tsf_write(dev, tsf);
3842
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003843 mutex_unlock(&wl->mutex);
3844}
3845
John Daiker99da1852009-02-24 02:16:42 -08003846static const char *band_to_string(enum ieee80211_band band)
Michael Buesche4d6b792007-09-18 15:39:42 -04003847{
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003848 switch (band) {
3849 case IEEE80211_BAND_5GHZ:
3850 return "5";
3851 case IEEE80211_BAND_2GHZ:
3852 return "2.4";
3853 default:
3854 break;
3855 }
3856 B43_WARN_ON(1);
3857 return "";
3858}
3859
3860/* Expects wl->mutex locked */
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003861static int b43_switch_band(struct b43_wldev *dev,
3862 struct ieee80211_channel *chan)
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003863{
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003864 struct b43_phy *phy = &dev->phy;
3865 bool gmode;
3866 u32 tmp;
Michael Buesche4d6b792007-09-18 15:39:42 -04003867
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003868 switch (chan->band) {
3869 case IEEE80211_BAND_5GHZ:
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003870 gmode = false;
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003871 break;
3872 case IEEE80211_BAND_2GHZ:
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003873 gmode = true;
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003874 break;
3875 default:
3876 B43_WARN_ON(1);
3877 return -EINVAL;
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003878 }
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003879
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003880 if (!((gmode && phy->supports_2ghz) ||
3881 (!gmode && phy->supports_5ghz))) {
3882 b43err(dev->wl, "This device doesn't support %s-GHz band\n",
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003883 band_to_string(chan->band));
3884 return -ENODEV;
Michael Buesche4d6b792007-09-18 15:39:42 -04003885 }
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003886
3887 if (!!phy->gmode == !!gmode) {
Michael Buesche4d6b792007-09-18 15:39:42 -04003888 /* This device is already running. */
3889 return 0;
3890 }
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003891
3892 b43dbg(dev->wl, "Switching to %s GHz band\n",
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003893 band_to_string(chan->band));
Michael Buesche4d6b792007-09-18 15:39:42 -04003894
Rafał Miłecki6fe55142014-05-27 22:07:33 +02003895 /* Some new devices don't need disabling radio for band switching */
3896 if (!(phy->type == B43_PHYTYPE_N && phy->rev >= 3))
3897 b43_software_rfkill(dev, true);
Michael Buesche4d6b792007-09-18 15:39:42 -04003898
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003899 phy->gmode = gmode;
3900 b43_phy_put_into_reset(dev);
3901 switch (dev->dev->bus_type) {
3902#ifdef CONFIG_B43_BCMA
3903 case B43_BUS_BCMA:
3904 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
3905 if (gmode)
3906 tmp |= B43_BCMA_IOCTL_GMODE;
3907 else
3908 tmp &= ~B43_BCMA_IOCTL_GMODE;
3909 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
3910 break;
3911#endif
3912#ifdef CONFIG_B43_SSB
3913 case B43_BUS_SSB:
3914 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
3915 if (gmode)
3916 tmp |= B43_TMSLOW_GMODE;
3917 else
3918 tmp &= ~B43_TMSLOW_GMODE;
3919 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
3920 break;
3921#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04003922 }
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003923 b43_phy_take_out_of_reset(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003924
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003925 b43_upload_initvals_band(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003926
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003927 b43_phy_init(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003928
3929 return 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003930}
3931
Johannes Berg9124b072008-10-14 19:17:54 +02003932/* Write the short and long frame retry limit values. */
3933static void b43_set_retry_limits(struct b43_wldev *dev,
3934 unsigned int short_retry,
3935 unsigned int long_retry)
3936{
3937 /* The retry limit is a 4-bit counter. Enforce this to avoid overflowing
3938 * the chip-internal counter. */
3939 short_retry = min(short_retry, (unsigned int)0xF);
3940 long_retry = min(long_retry, (unsigned int)0xF);
3941
3942 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_SRLIMIT,
3943 short_retry);
3944 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_LRLIMIT,
3945 long_retry);
3946}
3947
Johannes Berge8975582008-10-09 12:18:51 +02003948static int b43_op_config(struct ieee80211_hw *hw, u32 changed)
Michael Buesche4d6b792007-09-18 15:39:42 -04003949{
3950 struct b43_wl *wl = hw_to_b43_wl(hw);
Rafał Miłecki53256512014-05-31 20:49:34 +02003951 struct b43_wldev *dev = wl->current_dev;
3952 struct b43_phy *phy = &dev->phy;
Johannes Berge8975582008-10-09 12:18:51 +02003953 struct ieee80211_conf *conf = &hw->conf;
Michael Buesch9db1f6d2007-12-22 21:54:20 +01003954 int antenna;
Michael Buesche4d6b792007-09-18 15:39:42 -04003955 int err = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003956
Michael Buesche4d6b792007-09-18 15:39:42 -04003957 mutex_lock(&wl->mutex);
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003958 b43_mac_suspend(dev);
3959
Rafał Miłecki8c79e5e2014-05-31 20:49:35 +02003960 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
Rafał Miłeckiea42e712014-05-31 20:49:38 +02003961 phy->chandef = &conf->chandef;
Rafał Miłeckif9471e92014-05-31 20:49:37 +02003962 phy->channel = conf->chandef.chan->hw_value;
Felix Fietkau2a190322011-08-10 13:50:30 -06003963
Rafał Miłecki8c79e5e2014-05-31 20:49:35 +02003964 /* Switch the band (if necessary). */
3965 err = b43_switch_band(dev, conf->chandef.chan);
3966 if (err)
3967 goto out_mac_enable;
3968
3969 /* Switch to the requested channel.
3970 * The firmware takes care of races with the TX handler.
3971 */
Rafał Miłeckif9471e92014-05-31 20:49:37 +02003972 b43_switch_channel(dev, phy->channel);
Rafał Miłecki8c79e5e2014-05-31 20:49:35 +02003973 }
Rafał Miłeckiaa4c7b22010-01-22 01:53:12 +01003974
Johannes Berg9124b072008-10-14 19:17:54 +02003975 if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
3976 b43_set_retry_limits(dev, conf->short_frame_max_tx_count,
3977 conf->long_frame_max_tx_count);
3978 changed &= ~IEEE80211_CONF_CHANGE_RETRY_LIMITS;
3979 if (!changed)
Michael Bueschd10d0e52008-12-18 22:13:39 +01003980 goto out_mac_enable;
Michael Buesche4d6b792007-09-18 15:39:42 -04003981
Johannes Berg0869aea02009-10-28 10:03:35 +01003982 dev->wl->radiotap_enabled = !!(conf->flags & IEEE80211_CONF_MONITOR);
Johannes Bergd42ce842007-11-23 14:50:51 +01003983
Michael Buesche4d6b792007-09-18 15:39:42 -04003984 /* Adjust the desired TX power level. */
3985 if (conf->power_level != 0) {
Michael Buesch18c8ade2008-08-28 19:33:40 +02003986 if (conf->power_level != phy->desired_txpower) {
3987 phy->desired_txpower = conf->power_level;
3988 b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME |
3989 B43_TXPWR_IGNORE_TSSI);
Michael Buesche4d6b792007-09-18 15:39:42 -04003990 }
3991 }
3992
3993 /* Antennas for RX and management frame TX. */
Johannes Berg0f4ac382008-10-09 12:18:04 +02003994 antenna = B43_ANTENNA_DEFAULT;
Michael Buesch9db1f6d2007-12-22 21:54:20 +01003995 b43_mgmtframe_txantenna(dev, antenna);
Johannes Berg0f4ac382008-10-09 12:18:04 +02003996 antenna = B43_ANTENNA_DEFAULT;
Michael Bueschef1a6282008-08-27 18:53:02 +02003997 if (phy->ops->set_rx_antenna)
3998 phy->ops->set_rx_antenna(dev, antenna);
Michael Buesche4d6b792007-09-18 15:39:42 -04003999
Larry Fingerfd4973c2009-06-20 12:58:11 -05004000 if (wl->radio_enabled != phy->radio_on) {
4001 if (wl->radio_enabled) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004002 b43_software_rfkill(dev, false);
Michael Bueschfda9abc2007-09-20 22:14:18 +02004003 b43info(dev->wl, "Radio turned on by software\n");
4004 if (!dev->radio_hw_enable) {
4005 b43info(dev->wl, "The hardware RF-kill button "
4006 "still turns the radio physically off. "
4007 "Press the button to turn it on.\n");
4008 }
4009 } else {
Johannes Berg19d337d2009-06-02 13:01:37 +02004010 b43_software_rfkill(dev, true);
Michael Bueschfda9abc2007-09-20 22:14:18 +02004011 b43info(dev->wl, "Radio turned off by software\n");
4012 }
4013 }
4014
Michael Bueschd10d0e52008-12-18 22:13:39 +01004015out_mac_enable:
4016 b43_mac_enable(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004017 mutex_unlock(&wl->mutex);
4018
4019 return err;
4020}
4021
Johannes Berg881d9482009-01-21 15:13:48 +01004022static void b43_update_basic_rates(struct b43_wldev *dev, u32 brates)
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004023{
4024 struct ieee80211_supported_band *sband =
4025 dev->wl->hw->wiphy->bands[b43_current_band(dev->wl)];
4026 struct ieee80211_rate *rate;
4027 int i;
4028 u16 basic, direct, offset, basic_offset, rateptr;
4029
4030 for (i = 0; i < sband->n_bitrates; i++) {
4031 rate = &sband->bitrates[i];
4032
4033 if (b43_is_cck_rate(rate->hw_value)) {
4034 direct = B43_SHM_SH_CCKDIRECT;
4035 basic = B43_SHM_SH_CCKBASIC;
4036 offset = b43_plcp_get_ratecode_cck(rate->hw_value);
4037 offset &= 0xF;
4038 } else {
4039 direct = B43_SHM_SH_OFDMDIRECT;
4040 basic = B43_SHM_SH_OFDMBASIC;
4041 offset = b43_plcp_get_ratecode_ofdm(rate->hw_value);
4042 offset &= 0xF;
4043 }
4044
4045 rate = ieee80211_get_response_rate(sband, brates, rate->bitrate);
4046
4047 if (b43_is_cck_rate(rate->hw_value)) {
4048 basic_offset = b43_plcp_get_ratecode_cck(rate->hw_value);
4049 basic_offset &= 0xF;
4050 } else {
4051 basic_offset = b43_plcp_get_ratecode_ofdm(rate->hw_value);
4052 basic_offset &= 0xF;
4053 }
4054
4055 /*
4056 * Get the pointer that we need to point to
4057 * from the direct map
4058 */
4059 rateptr = b43_shm_read16(dev, B43_SHM_SHARED,
4060 direct + 2 * basic_offset);
4061 /* and write it to the basic map */
4062 b43_shm_write16(dev, B43_SHM_SHARED, basic + 2 * offset,
4063 rateptr);
4064 }
4065}
4066
4067static void b43_op_bss_info_changed(struct ieee80211_hw *hw,
4068 struct ieee80211_vif *vif,
4069 struct ieee80211_bss_conf *conf,
4070 u32 changed)
4071{
4072 struct b43_wl *wl = hw_to_b43_wl(hw);
4073 struct b43_wldev *dev;
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004074
4075 mutex_lock(&wl->mutex);
4076
4077 dev = wl->current_dev;
Michael Bueschd10d0e52008-12-18 22:13:39 +01004078 if (!dev || b43_status(dev) < B43_STAT_STARTED)
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004079 goto out_unlock_mutex;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02004080
4081 B43_WARN_ON(wl->vif != vif);
4082
4083 if (changed & BSS_CHANGED_BSSID) {
Johannes Berg2d0ddec2009-04-23 16:13:26 +02004084 if (conf->bssid)
4085 memcpy(wl->bssid, conf->bssid, ETH_ALEN);
4086 else
4087 memset(wl->bssid, 0, ETH_ALEN);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02004088 }
4089
Johannes Berg3f0d8432009-05-18 10:53:18 +02004090 if (b43_status(dev) >= B43_STAT_INITIALIZED) {
4091 if (changed & BSS_CHANGED_BEACON &&
4092 (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
4093 b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT) ||
4094 b43_is_mode(wl, NL80211_IFTYPE_ADHOC)))
4095 b43_update_templates(wl);
4096
4097 if (changed & BSS_CHANGED_BSSID)
4098 b43_write_mac_bssid_templates(dev);
4099 }
Johannes Berg3f0d8432009-05-18 10:53:18 +02004100
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004101 b43_mac_suspend(dev);
4102
Johannes Berg57c4d7b2009-04-23 16:10:04 +02004103 /* Update templates for AP/mesh mode. */
4104 if (changed & BSS_CHANGED_BEACON_INT &&
4105 (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
4106 b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT) ||
Felix Fietkau2a190322011-08-10 13:50:30 -06004107 b43_is_mode(wl, NL80211_IFTYPE_ADHOC)) &&
4108 conf->beacon_int)
Johannes Berg57c4d7b2009-04-23 16:10:04 +02004109 b43_set_beacon_int(dev, conf->beacon_int);
4110
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004111 if (changed & BSS_CHANGED_BASIC_RATES)
4112 b43_update_basic_rates(dev, conf->basic_rates);
4113
4114 if (changed & BSS_CHANGED_ERP_SLOT) {
4115 if (conf->use_short_slot)
4116 b43_short_slot_timing_enable(dev);
4117 else
4118 b43_short_slot_timing_disable(dev);
4119 }
4120
4121 b43_mac_enable(dev);
Michael Bueschd10d0e52008-12-18 22:13:39 +01004122out_unlock_mutex:
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004123 mutex_unlock(&wl->mutex);
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004124}
4125
Michael Buesch40faacc2007-10-28 16:29:32 +01004126static int b43_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01004127 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
4128 struct ieee80211_key_conf *key)
Michael Buesche4d6b792007-09-18 15:39:42 -04004129{
4130 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004131 struct b43_wldev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004132 u8 algorithm;
4133 u8 index;
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004134 int err;
Michael Buesch060210f2009-01-25 15:49:59 +01004135 static const u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
Michael Buesche4d6b792007-09-18 15:39:42 -04004136
4137 if (modparam_nohwcrypt)
4138 return -ENOSPC; /* User disabled HW-crypto */
4139
Antonio Quartulli78f9c852012-04-01 00:35:40 +03004140 if ((vif->type == NL80211_IFTYPE_ADHOC ||
4141 vif->type == NL80211_IFTYPE_MESH_POINT) &&
4142 (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
4143 key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
4144 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
4145 /*
4146 * For now, disable hw crypto for the RSN IBSS group keys. This
4147 * could be optimized in the future, but until that gets
4148 * implemented, use of software crypto for group addressed
4149 * frames is a acceptable to allow RSN IBSS to be used.
4150 */
4151 return -EOPNOTSUPP;
4152 }
4153
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004154 mutex_lock(&wl->mutex);
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004155
4156 dev = wl->current_dev;
4157 err = -ENODEV;
4158 if (!dev || b43_status(dev) < B43_STAT_INITIALIZED)
4159 goto out_unlock;
4160
Michael Buesch403a3a12009-06-08 21:04:57 +02004161 if (dev->fw.pcm_request_failed || !dev->hwcrypto_enabled) {
Michael Buesch68217832008-05-17 23:43:57 +02004162 /* We don't have firmware for the crypto engine.
4163 * Must use software-crypto. */
4164 err = -EOPNOTSUPP;
4165 goto out_unlock;
4166 }
4167
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004168 err = -EINVAL;
Johannes Berg97359d12010-08-10 09:46:38 +02004169 switch (key->cipher) {
4170 case WLAN_CIPHER_SUITE_WEP40:
4171 algorithm = B43_SEC_ALGO_WEP40;
Michael Buesche4d6b792007-09-18 15:39:42 -04004172 break;
Johannes Berg97359d12010-08-10 09:46:38 +02004173 case WLAN_CIPHER_SUITE_WEP104:
4174 algorithm = B43_SEC_ALGO_WEP104;
4175 break;
4176 case WLAN_CIPHER_SUITE_TKIP:
Michael Buesche4d6b792007-09-18 15:39:42 -04004177 algorithm = B43_SEC_ALGO_TKIP;
4178 break;
Johannes Berg97359d12010-08-10 09:46:38 +02004179 case WLAN_CIPHER_SUITE_CCMP:
Michael Buesche4d6b792007-09-18 15:39:42 -04004180 algorithm = B43_SEC_ALGO_AES;
4181 break;
4182 default:
4183 B43_WARN_ON(1);
Michael Buesche4d6b792007-09-18 15:39:42 -04004184 goto out_unlock;
4185 }
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004186 index = (u8) (key->keyidx);
4187 if (index > 3)
4188 goto out_unlock;
Michael Buesche4d6b792007-09-18 15:39:42 -04004189
4190 switch (cmd) {
4191 case SET_KEY:
gregor kowski035d0242009-08-19 22:35:45 +02004192 if (algorithm == B43_SEC_ALGO_TKIP &&
4193 (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
4194 !modparam_hwtkip)) {
4195 /* We support only pairwise key */
Michael Buesche4d6b792007-09-18 15:39:42 -04004196 err = -EOPNOTSUPP;
4197 goto out_unlock;
4198 }
4199
Michael Buesche808e582008-12-19 21:30:52 +01004200 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
Johannes Bergdc822b52008-12-29 12:55:09 +01004201 if (WARN_ON(!sta)) {
4202 err = -EOPNOTSUPP;
4203 goto out_unlock;
4204 }
Michael Buesche808e582008-12-19 21:30:52 +01004205 /* Pairwise key with an assigned MAC address. */
Michael Buesche4d6b792007-09-18 15:39:42 -04004206 err = b43_key_write(dev, -1, algorithm,
Johannes Bergdc822b52008-12-29 12:55:09 +01004207 key->key, key->keylen,
4208 sta->addr, key);
Michael Buesche808e582008-12-19 21:30:52 +01004209 } else {
4210 /* Group key */
4211 err = b43_key_write(dev, index, algorithm,
4212 key->key, key->keylen, NULL, key);
Michael Buesche4d6b792007-09-18 15:39:42 -04004213 }
4214 if (err)
4215 goto out_unlock;
4216
4217 if (algorithm == B43_SEC_ALGO_WEP40 ||
4218 algorithm == B43_SEC_ALGO_WEP104) {
4219 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_USEDEFKEYS);
4220 } else {
4221 b43_hf_write(dev,
4222 b43_hf_read(dev) & ~B43_HF_USEDEFKEYS);
4223 }
4224 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
gregor kowski035d0242009-08-19 22:35:45 +02004225 if (algorithm == B43_SEC_ALGO_TKIP)
4226 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
Michael Buesche4d6b792007-09-18 15:39:42 -04004227 break;
4228 case DISABLE_KEY: {
4229 err = b43_key_clear(dev, key->hw_key_idx);
4230 if (err)
4231 goto out_unlock;
4232 break;
4233 }
4234 default:
4235 B43_WARN_ON(1);
4236 }
Michael Buesch9cf7f242008-12-19 20:24:30 +01004237
Michael Buesche4d6b792007-09-18 15:39:42 -04004238out_unlock:
Michael Buesche4d6b792007-09-18 15:39:42 -04004239 if (!err) {
4240 b43dbg(wl, "%s hardware based encryption for keyidx: %d, "
Johannes Berge1749612008-10-27 15:59:26 -07004241 "mac: %pM\n",
Michael Buesche4d6b792007-09-18 15:39:42 -04004242 cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
Larry Fingera1d882102009-01-14 11:15:25 -06004243 sta ? sta->addr : bcast_addr);
Michael Buesch9cf7f242008-12-19 20:24:30 +01004244 b43_dump_keymemory(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004245 }
Michael Buesch9cf7f242008-12-19 20:24:30 +01004246 mutex_unlock(&wl->mutex);
4247
Michael Buesche4d6b792007-09-18 15:39:42 -04004248 return err;
4249}
4250
Michael Buesch40faacc2007-10-28 16:29:32 +01004251static void b43_op_configure_filter(struct ieee80211_hw *hw,
4252 unsigned int changed, unsigned int *fflags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02004253 u64 multicast)
Michael Buesche4d6b792007-09-18 15:39:42 -04004254{
4255 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesch36dbd952009-09-04 22:51:29 +02004256 struct b43_wldev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004257
Michael Buesch36dbd952009-09-04 22:51:29 +02004258 mutex_lock(&wl->mutex);
4259 dev = wl->current_dev;
Johannes Berg4150c572007-09-17 01:29:23 -04004260 if (!dev) {
4261 *fflags = 0;
Michael Buesch36dbd952009-09-04 22:51:29 +02004262 goto out_unlock;
Michael Buesche4d6b792007-09-18 15:39:42 -04004263 }
Johannes Berg4150c572007-09-17 01:29:23 -04004264
Johannes Berg4150c572007-09-17 01:29:23 -04004265 *fflags &= FIF_PROMISC_IN_BSS |
4266 FIF_ALLMULTI |
4267 FIF_FCSFAIL |
4268 FIF_PLCPFAIL |
4269 FIF_CONTROL |
4270 FIF_OTHER_BSS |
4271 FIF_BCN_PRBRESP_PROMISC;
4272
4273 changed &= FIF_PROMISC_IN_BSS |
4274 FIF_ALLMULTI |
4275 FIF_FCSFAIL |
4276 FIF_PLCPFAIL |
4277 FIF_CONTROL |
4278 FIF_OTHER_BSS |
4279 FIF_BCN_PRBRESP_PROMISC;
4280
4281 wl->filter_flags = *fflags;
4282
4283 if (changed && b43_status(dev) >= B43_STAT_INITIALIZED)
4284 b43_adjust_opmode(dev);
Michael Buesch36dbd952009-09-04 22:51:29 +02004285
4286out_unlock:
4287 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04004288}
4289
Michael Buesch36dbd952009-09-04 22:51:29 +02004290/* Locking: wl->mutex
4291 * Returns the current dev. This might be different from the passed in dev,
4292 * because the core might be gone away while we unlocked the mutex. */
4293static struct b43_wldev * b43_wireless_core_stop(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04004294{
Larry Finger9a53bf52011-08-27 15:53:42 -05004295 struct b43_wl *wl;
Michael Buesch36dbd952009-09-04 22:51:29 +02004296 struct b43_wldev *orig_dev;
Michael Buesch49d965c2009-10-03 00:57:58 +02004297 u32 mask;
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01004298 int queue_num;
Michael Buesche4d6b792007-09-18 15:39:42 -04004299
Larry Finger9a53bf52011-08-27 15:53:42 -05004300 if (!dev)
4301 return NULL;
4302 wl = dev->wl;
Michael Buesch36dbd952009-09-04 22:51:29 +02004303redo:
4304 if (!dev || b43_status(dev) < B43_STAT_STARTED)
4305 return dev;
Stefano Brivioa19d12d2007-11-07 18:16:11 +01004306
Michael Bueschf5d40ee2009-09-04 22:53:18 +02004307 /* Cancel work. Unlock to avoid deadlocks. */
Michael Buesche4d6b792007-09-18 15:39:42 -04004308 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04004309 cancel_delayed_work_sync(&dev->periodic_work);
Michael Bueschf5d40ee2009-09-04 22:53:18 +02004310 cancel_work_sync(&wl->tx_work);
Michael Buesche4d6b792007-09-18 15:39:42 -04004311 mutex_lock(&wl->mutex);
Michael Buesch36dbd952009-09-04 22:51:29 +02004312 dev = wl->current_dev;
4313 if (!dev || b43_status(dev) < B43_STAT_STARTED) {
4314 /* Whoops, aliens ate up the device while we were unlocked. */
4315 return dev;
4316 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004317
Michael Buesch36dbd952009-09-04 22:51:29 +02004318 /* Disable interrupts on the device. */
4319 b43_set_status(dev, B43_STAT_INITIALIZED);
Rafał Miłecki505fb012011-05-19 15:11:27 +02004320 if (b43_bus_host_is_sdio(dev->dev)) {
Michael Buesch36dbd952009-09-04 22:51:29 +02004321 /* wl->mutex is locked. That is enough. */
4322 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
4323 b43_read32(dev, B43_MMIO_GEN_IRQ_MASK); /* Flush */
4324 } else {
4325 spin_lock_irq(&wl->hardirq_lock);
4326 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
4327 b43_read32(dev, B43_MMIO_GEN_IRQ_MASK); /* Flush */
4328 spin_unlock_irq(&wl->hardirq_lock);
4329 }
Michael Buesch176e9f62009-09-11 23:04:04 +02004330 /* Synchronize and free the interrupt handlers. Unlock to avoid deadlocks. */
Michael Buesch36dbd952009-09-04 22:51:29 +02004331 orig_dev = dev;
4332 mutex_unlock(&wl->mutex);
Rafał Miłecki505fb012011-05-19 15:11:27 +02004333 if (b43_bus_host_is_sdio(dev->dev)) {
Michael Buesch176e9f62009-09-11 23:04:04 +02004334 b43_sdio_free_irq(dev);
4335 } else {
Rafał Miłeckia18c7152011-05-18 02:06:40 +02004336 synchronize_irq(dev->dev->irq);
4337 free_irq(dev->dev->irq, dev);
Michael Buesch176e9f62009-09-11 23:04:04 +02004338 }
Michael Buesch36dbd952009-09-04 22:51:29 +02004339 mutex_lock(&wl->mutex);
4340 dev = wl->current_dev;
4341 if (!dev)
4342 return dev;
4343 if (dev != orig_dev) {
4344 if (b43_status(dev) >= B43_STAT_STARTED)
4345 goto redo;
4346 return dev;
4347 }
Michael Buesch49d965c2009-10-03 00:57:58 +02004348 mask = b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
4349 B43_WARN_ON(mask != 0xFFFFFFFF && mask);
Michael Buesch36dbd952009-09-04 22:51:29 +02004350
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01004351 /* Drain all TX queues. */
4352 for (queue_num = 0; queue_num < B43_QOS_QUEUE_NUM; queue_num++) {
Felix Fietkau78f18df2012-12-10 17:40:21 +01004353 while (skb_queue_len(&wl->tx_queue[queue_num])) {
4354 struct sk_buff *skb;
4355
4356 skb = skb_dequeue(&wl->tx_queue[queue_num]);
4357 ieee80211_free_txskb(wl->hw, skb);
4358 }
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01004359 }
Michael Bueschf5d40ee2009-09-04 22:53:18 +02004360
Michael Buesche4d6b792007-09-18 15:39:42 -04004361 b43_mac_suspend(dev);
Michael Buescha78b3bb2009-09-11 21:44:05 +02004362 b43_leds_exit(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004363 b43dbg(wl, "Wireless interface stopped\n");
Michael Buesch36dbd952009-09-04 22:51:29 +02004364
4365 return dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004366}
4367
4368/* Locking: wl->mutex */
4369static int b43_wireless_core_start(struct b43_wldev *dev)
4370{
4371 int err;
4372
4373 B43_WARN_ON(b43_status(dev) != B43_STAT_INITIALIZED);
4374
4375 drain_txstatus_queue(dev);
Rafał Miłecki505fb012011-05-19 15:11:27 +02004376 if (b43_bus_host_is_sdio(dev->dev)) {
Albert Herranz3dbba8e2009-09-10 19:34:49 +02004377 err = b43_sdio_request_irq(dev, b43_sdio_interrupt_handler);
4378 if (err) {
4379 b43err(dev->wl, "Cannot request SDIO IRQ\n");
4380 goto out;
4381 }
4382 } else {
Rafał Miłeckia18c7152011-05-18 02:06:40 +02004383 err = request_threaded_irq(dev->dev->irq, b43_interrupt_handler,
Albert Herranz3dbba8e2009-09-10 19:34:49 +02004384 b43_interrupt_thread_handler,
4385 IRQF_SHARED, KBUILD_MODNAME, dev);
4386 if (err) {
Rafał Miłeckidedb1eb2011-05-14 00:04:38 +02004387 b43err(dev->wl, "Cannot request IRQ-%d\n",
Rafał Miłeckia18c7152011-05-18 02:06:40 +02004388 dev->dev->irq);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02004389 goto out;
4390 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004391 }
4392
4393 /* We are ready to run. */
Larry Finger0866b032010-02-03 13:33:44 -06004394 ieee80211_wake_queues(dev->wl->hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04004395 b43_set_status(dev, B43_STAT_STARTED);
4396
4397 /* Start data flow (TX/RX). */
4398 b43_mac_enable(dev);
Michael Buesch13790722009-04-08 21:26:27 +02004399 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
Michael Buesche4d6b792007-09-18 15:39:42 -04004400
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004401 /* Start maintenance work */
Michael Buesche4d6b792007-09-18 15:39:42 -04004402 b43_periodic_tasks_setup(dev);
4403
Michael Buescha78b3bb2009-09-11 21:44:05 +02004404 b43_leds_init(dev);
4405
Michael Buesche4d6b792007-09-18 15:39:42 -04004406 b43dbg(dev->wl, "Wireless interface started\n");
Michael Buescha78b3bb2009-09-11 21:44:05 +02004407out:
Michael Buesche4d6b792007-09-18 15:39:42 -04004408 return err;
4409}
4410
Rafał Miłecki2fdf8c52012-07-26 08:16:01 +02004411static char *b43_phy_name(struct b43_wldev *dev, u8 phy_type)
4412{
4413 switch (phy_type) {
4414 case B43_PHYTYPE_A:
4415 return "A";
4416 case B43_PHYTYPE_B:
4417 return "B";
4418 case B43_PHYTYPE_G:
4419 return "G";
4420 case B43_PHYTYPE_N:
4421 return "N";
4422 case B43_PHYTYPE_LP:
4423 return "LP";
4424 case B43_PHYTYPE_SSLPN:
4425 return "SSLPN";
4426 case B43_PHYTYPE_HT:
4427 return "HT";
4428 case B43_PHYTYPE_LCN:
4429 return "LCN";
4430 case B43_PHYTYPE_LCNXN:
4431 return "LCNXN";
4432 case B43_PHYTYPE_LCN40:
4433 return "LCN40";
4434 case B43_PHYTYPE_AC:
4435 return "AC";
4436 }
4437 return "UNKNOWN";
4438}
4439
Michael Buesche4d6b792007-09-18 15:39:42 -04004440/* Get PHY and RADIO versioning numbers */
4441static int b43_phy_versioning(struct b43_wldev *dev)
4442{
4443 struct b43_phy *phy = &dev->phy;
Rafał Miłeckife5e499f2014-07-04 09:21:56 +02004444 const u8 core_rev = dev->dev->core_rev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004445 u32 tmp;
4446 u8 analog_type;
4447 u8 phy_type;
4448 u8 phy_rev;
4449 u16 radio_manuf;
Rafał Miłecki16e75452014-07-20 12:57:45 +02004450 u16 radio_id;
Michael Buesche4d6b792007-09-18 15:39:42 -04004451 u16 radio_rev;
Rafał Miłecki16e75452014-07-20 12:57:45 +02004452 u8 radio_ver;
Michael Buesche4d6b792007-09-18 15:39:42 -04004453 int unsupported = 0;
4454
4455 /* Get PHY versioning */
4456 tmp = b43_read16(dev, B43_MMIO_PHY_VER);
4457 analog_type = (tmp & B43_PHYVER_ANALOG) >> B43_PHYVER_ANALOG_SHIFT;
4458 phy_type = (tmp & B43_PHYVER_TYPE) >> B43_PHYVER_TYPE_SHIFT;
4459 phy_rev = (tmp & B43_PHYVER_VERSION);
Rafał Miłeckib49c3ca2014-06-29 21:46:45 +02004460
4461 /* LCNXN is continuation of N which run out of revisions */
4462 if (phy_type == B43_PHYTYPE_LCNXN) {
4463 phy_type = B43_PHYTYPE_N;
4464 phy_rev += 16;
4465 }
4466
Michael Buesche4d6b792007-09-18 15:39:42 -04004467 switch (phy_type) {
Rafał Miłecki418378f2014-06-20 17:22:01 +02004468#ifdef CONFIG_B43_PHY_G
Michael Buesche4d6b792007-09-18 15:39:42 -04004469 case B43_PHYTYPE_G:
Larry Finger013978b2007-11-26 10:29:47 -06004470 if (phy_rev > 9)
Michael Buesche4d6b792007-09-18 15:39:42 -04004471 unsupported = 1;
4472 break;
Rafał Miłecki418378f2014-06-20 17:22:01 +02004473#endif
Rafał Miłecki692d2c02010-12-07 21:56:00 +01004474#ifdef CONFIG_B43_PHY_N
Michael Bueschd5c71e42008-01-04 17:06:29 +01004475 case B43_PHYTYPE_N:
Rafał Miłecki40c68f22014-07-08 15:11:07 +02004476 if (phy_rev >= 19)
Michael Bueschd5c71e42008-01-04 17:06:29 +01004477 unsupported = 1;
4478 break;
4479#endif
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004480#ifdef CONFIG_B43_PHY_LP
4481 case B43_PHYTYPE_LP:
Gábor Stefanik9d86a2d2009-08-14 14:54:46 +02004482 if (phy_rev > 2)
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004483 unsupported = 1;
4484 break;
4485#endif
Rafał Miłeckid7520b12011-06-13 16:20:06 +02004486#ifdef CONFIG_B43_PHY_HT
4487 case B43_PHYTYPE_HT:
4488 if (phy_rev > 1)
4489 unsupported = 1;
4490 break;
4491#endif
Rafał Miłecki1d738e62011-07-07 15:25:27 +02004492#ifdef CONFIG_B43_PHY_LCN
4493 case B43_PHYTYPE_LCN:
4494 if (phy_rev > 1)
4495 unsupported = 1;
4496 break;
4497#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04004498 default:
4499 unsupported = 1;
Joe Perches6403eab2011-06-03 11:51:20 +00004500 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004501 if (unsupported) {
Rafał Miłecki2fdf8c52012-07-26 08:16:01 +02004502 b43err(dev->wl, "FOUND UNSUPPORTED PHY (Analog %u, Type %d (%s), Revision %u)\n",
4503 analog_type, phy_type, b43_phy_name(dev, phy_type),
4504 phy_rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004505 return -EOPNOTSUPP;
4506 }
Rafał Miłecki2fdf8c52012-07-26 08:16:01 +02004507 b43info(dev->wl, "Found PHY: Analog %u, Type %d (%s), Revision %u\n",
4508 analog_type, phy_type, b43_phy_name(dev, phy_type), phy_rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004509
4510 /* Get RADIO versioning */
Rafał Miłeckife5e499f2014-07-04 09:21:56 +02004511 if (core_rev == 40 || core_rev == 42) {
4512 radio_manuf = 0x17F;
4513
Rafał Miłecki25c15562014-08-07 07:45:37 +02004514 b43_write16f(dev, B43_MMIO_RADIO24_CONTROL, 0);
Rafał Miłeckife5e499f2014-07-04 09:21:56 +02004515 radio_rev = b43_read16(dev, B43_MMIO_RADIO24_DATA);
4516
Rafał Miłecki25c15562014-08-07 07:45:37 +02004517 b43_write16f(dev, B43_MMIO_RADIO24_CONTROL, 1);
Rafał Miłecki16e75452014-07-20 12:57:45 +02004518 radio_id = b43_read16(dev, B43_MMIO_RADIO24_DATA);
4519
4520 radio_ver = 0; /* Is there version somewhere? */
Rafał Miłeckife5e499f2014-07-04 09:21:56 +02004521 } else if (core_rev >= 24) {
Rafał Miłecki544e5d82011-07-06 20:27:25 +02004522 u16 radio24[3];
4523
4524 for (tmp = 0; tmp < 3; tmp++) {
Rafał Miłecki25c15562014-08-07 07:45:37 +02004525 b43_write16f(dev, B43_MMIO_RADIO24_CONTROL, tmp);
Rafał Miłecki544e5d82011-07-06 20:27:25 +02004526 radio24[tmp] = b43_read16(dev, B43_MMIO_RADIO24_DATA);
4527 }
4528
Rafał Miłecki544e5d82011-07-06 20:27:25 +02004529 radio_manuf = 0x17F;
Rafał Miłecki16e75452014-07-20 12:57:45 +02004530 radio_id = (radio24[2] << 8) | radio24[1];
Rafał Miłecki544e5d82011-07-06 20:27:25 +02004531 radio_rev = (radio24[0] & 0xF);
Rafał Miłecki16e75452014-07-20 12:57:45 +02004532 radio_ver = (radio24[0] & 0xF0) >> 4;
Michael Buesche4d6b792007-09-18 15:39:42 -04004533 } else {
Rafał Miłecki3fd48502011-07-06 20:27:24 +02004534 if (dev->dev->chip_id == 0x4317) {
4535 if (dev->dev->chip_rev == 0)
4536 tmp = 0x3205017F;
4537 else if (dev->dev->chip_rev == 1)
4538 tmp = 0x4205017F;
4539 else
4540 tmp = 0x5205017F;
4541 } else {
Rafał Miłecki25c15562014-08-07 07:45:37 +02004542 b43_write16f(dev, B43_MMIO_RADIO_CONTROL,
4543 B43_RADIOCTL_ID);
Rafał Miłecki3fd48502011-07-06 20:27:24 +02004544 tmp = b43_read16(dev, B43_MMIO_RADIO_DATA_LOW);
Rafał Miłecki25c15562014-08-07 07:45:37 +02004545 b43_write16f(dev, B43_MMIO_RADIO_CONTROL,
4546 B43_RADIOCTL_ID);
4547 tmp |= b43_read16(dev, B43_MMIO_RADIO_DATA_HIGH) << 16;
Rafał Miłecki3fd48502011-07-06 20:27:24 +02004548 }
4549 radio_manuf = (tmp & 0x00000FFF);
Rafał Miłecki16e75452014-07-20 12:57:45 +02004550 radio_id = (tmp & 0x0FFFF000) >> 12;
Rafał Miłecki3fd48502011-07-06 20:27:24 +02004551 radio_rev = (tmp & 0xF0000000) >> 28;
Rafał Miłecki16e75452014-07-20 12:57:45 +02004552 radio_ver = 0; /* Probably not available on old hw */
Michael Buesche4d6b792007-09-18 15:39:42 -04004553 }
Rafał Miłecki3fd48502011-07-06 20:27:24 +02004554
Michael Buesch96c755a2008-01-06 00:09:46 +01004555 if (radio_manuf != 0x17F /* Broadcom */)
4556 unsupported = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -04004557 switch (phy_type) {
4558 case B43_PHYTYPE_A:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004559 if (radio_id != 0x2060)
Michael Buesche4d6b792007-09-18 15:39:42 -04004560 unsupported = 1;
4561 if (radio_rev != 1)
4562 unsupported = 1;
4563 if (radio_manuf != 0x17F)
4564 unsupported = 1;
4565 break;
4566 case B43_PHYTYPE_B:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004567 if ((radio_id & 0xFFF0) != 0x2050)
Michael Buesche4d6b792007-09-18 15:39:42 -04004568 unsupported = 1;
4569 break;
4570 case B43_PHYTYPE_G:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004571 if (radio_id != 0x2050)
Michael Buesche4d6b792007-09-18 15:39:42 -04004572 unsupported = 1;
4573 break;
Michael Buesch96c755a2008-01-06 00:09:46 +01004574 case B43_PHYTYPE_N:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004575 if (radio_id != 0x2055 && radio_id != 0x2056 &&
4576 radio_id != 0x2057)
Rafał Miłecki3695b932014-07-08 15:11:10 +02004577 unsupported = 1;
Rafał Miłecki16e75452014-07-20 12:57:45 +02004578 if (radio_id == 0x2057 &&
Rafał Miłeckic11082f2014-07-19 12:52:47 +02004579 !(radio_rev == 9 || radio_rev == 14))
Michael Buesch96c755a2008-01-06 00:09:46 +01004580 unsupported = 1;
4581 break;
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004582 case B43_PHYTYPE_LP:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004583 if (radio_id != 0x2062 && radio_id != 0x2063)
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004584 unsupported = 1;
4585 break;
Rafał Miłeckid7520b12011-06-13 16:20:06 +02004586 case B43_PHYTYPE_HT:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004587 if (radio_id != 0x2059)
Rafał Miłeckid7520b12011-06-13 16:20:06 +02004588 unsupported = 1;
4589 break;
Rafał Miłecki1d738e62011-07-07 15:25:27 +02004590 case B43_PHYTYPE_LCN:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004591 if (radio_id != 0x2064)
Rafał Miłecki1d738e62011-07-07 15:25:27 +02004592 unsupported = 1;
4593 break;
Michael Buesche4d6b792007-09-18 15:39:42 -04004594 default:
4595 B43_WARN_ON(1);
4596 }
4597 if (unsupported) {
Rafał Miłecki88d825b2014-07-02 19:07:43 +02004598 b43err(dev->wl,
Rafał Miłecki16e75452014-07-20 12:57:45 +02004599 "FOUND UNSUPPORTED RADIO (Manuf 0x%X, ID 0x%X, Revision %u, Version %u)\n",
4600 radio_manuf, radio_id, radio_rev, radio_ver);
Michael Buesche4d6b792007-09-18 15:39:42 -04004601 return -EOPNOTSUPP;
4602 }
Rafał Miłecki16e75452014-07-20 12:57:45 +02004603 b43info(dev->wl,
4604 "Found Radio: Manuf 0x%X, ID 0x%X, Revision %u, Version %u\n",
4605 radio_manuf, radio_id, radio_rev, radio_ver);
Michael Buesche4d6b792007-09-18 15:39:42 -04004606
Rafał Miłecki16e75452014-07-20 12:57:45 +02004607 /* FIXME: b43 treats "id" as "ver" and ignores the real "ver" */
Michael Buesche4d6b792007-09-18 15:39:42 -04004608 phy->radio_manuf = radio_manuf;
Rafał Miłecki16e75452014-07-20 12:57:45 +02004609 phy->radio_ver = radio_id;
Michael Buesche4d6b792007-09-18 15:39:42 -04004610 phy->radio_rev = radio_rev;
4611
4612 phy->analog = analog_type;
4613 phy->type = phy_type;
4614 phy->rev = phy_rev;
4615
4616 return 0;
4617}
4618
4619static void setup_struct_phy_for_init(struct b43_wldev *dev,
4620 struct b43_phy *phy)
4621{
Michael Buesche4d6b792007-09-18 15:39:42 -04004622 phy->hardware_power_control = !!modparam_hwpctl;
Michael Buesch18c8ade2008-08-28 19:33:40 +02004623 phy->next_txpwr_check_time = jiffies;
Michael Buesch8ed7fc42007-12-09 22:34:59 +01004624 /* PHY TX errors counter. */
4625 atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
Michael Buesch591f3dc2009-03-31 12:27:32 +02004626
4627#if B43_DEBUG
Rusty Russell3db1cd52011-12-19 13:56:45 +00004628 phy->phy_locked = false;
4629 phy->radio_locked = false;
Michael Buesch591f3dc2009-03-31 12:27:32 +02004630#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04004631}
4632
4633static void setup_struct_wldev_for_init(struct b43_wldev *dev)
4634{
Rusty Russell3db1cd52011-12-19 13:56:45 +00004635 dev->dfq_valid = false;
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01004636
Michael Buesch6a724d62007-09-20 22:12:58 +02004637 /* Assume the radio is enabled. If it's not enabled, the state will
4638 * immediately get fixed on the first periodic work run. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00004639 dev->radio_hw_enable = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04004640
4641 /* Stats */
4642 memset(&dev->stats, 0, sizeof(dev->stats));
4643
4644 setup_struct_phy_for_init(dev, &dev->phy);
4645
4646 /* IRQ related flags */
4647 dev->irq_reason = 0;
4648 memset(dev->dma_reason, 0, sizeof(dev->dma_reason));
Michael Buesch13790722009-04-08 21:26:27 +02004649 dev->irq_mask = B43_IRQ_MASKTEMPLATE;
Michael Buesch3e3ccb32009-03-19 19:27:21 +01004650 if (b43_modparam_verbose < B43_VERBOSITY_DEBUG)
Michael Buesch13790722009-04-08 21:26:27 +02004651 dev->irq_mask &= ~B43_IRQ_PHY_TXERR;
Michael Buesche4d6b792007-09-18 15:39:42 -04004652
4653 dev->mac_suspended = 1;
4654
4655 /* Noise calculation context */
4656 memset(&dev->noisecalc, 0, sizeof(dev->noisecalc));
4657}
4658
4659static void b43_bluetooth_coext_enable(struct b43_wldev *dev)
4660{
Rafał Miłecki05814832011-05-18 02:06:39 +02004661 struct ssb_sprom *sprom = dev->dev->bus_sprom;
Michael Buescha259d6a2008-04-18 21:06:37 +02004662 u64 hf;
Michael Buesche4d6b792007-09-18 15:39:42 -04004663
Michael Buesch1855ba72008-04-18 20:51:41 +02004664 if (!modparam_btcoex)
4665 return;
Larry Finger95de2842007-11-09 16:57:18 -06004666 if (!(sprom->boardflags_lo & B43_BFL_BTCOEXIST))
Michael Buesche4d6b792007-09-18 15:39:42 -04004667 return;
4668 if (dev->phy.type != B43_PHYTYPE_B && !dev->phy.gmode)
4669 return;
4670
4671 hf = b43_hf_read(dev);
Larry Finger95de2842007-11-09 16:57:18 -06004672 if (sprom->boardflags_lo & B43_BFL_BTCMOD)
Michael Buesche4d6b792007-09-18 15:39:42 -04004673 hf |= B43_HF_BTCOEXALT;
4674 else
4675 hf |= B43_HF_BTCOEX;
4676 b43_hf_write(dev, hf);
Michael Buesche4d6b792007-09-18 15:39:42 -04004677}
4678
4679static void b43_bluetooth_coext_disable(struct b43_wldev *dev)
Michael Buesch1855ba72008-04-18 20:51:41 +02004680{
4681 if (!modparam_btcoex)
4682 return;
4683 //TODO
Michael Buesche4d6b792007-09-18 15:39:42 -04004684}
4685
4686static void b43_imcfglo_timeouts_workaround(struct b43_wldev *dev)
4687{
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004688 struct ssb_bus *bus;
Michael Buesche4d6b792007-09-18 15:39:42 -04004689 u32 tmp;
4690
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02004691#ifdef CONFIG_B43_SSB
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004692 if (dev->dev->bus_type != B43_BUS_SSB)
4693 return;
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02004694#else
4695 return;
4696#endif
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004697
4698 bus = dev->dev->sdev->bus;
4699
Rafał Miłecki0fd82ea2011-05-11 02:10:59 +02004700 if ((bus->chip_id == 0x4311 && bus->chip_rev == 2) ||
4701 (bus->chip_id == 0x4312)) {
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004702 tmp = ssb_read32(dev->dev->sdev, SSB_IMCFGLO);
Rafał Miłecki0fd82ea2011-05-11 02:10:59 +02004703 tmp &= ~SSB_IMCFGLO_REQTO;
4704 tmp &= ~SSB_IMCFGLO_SERTO;
4705 tmp |= 0x3;
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004706 ssb_write32(dev->dev->sdev, SSB_IMCFGLO, tmp);
Rafał Miłecki0fd82ea2011-05-11 02:10:59 +02004707 ssb_commit_settings(bus);
Michael Buesche4d6b792007-09-18 15:39:42 -04004708 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004709}
4710
Michael Bueschd59f7202008-04-03 18:56:19 +02004711static void b43_set_synth_pu_delay(struct b43_wldev *dev, bool idle)
4712{
4713 u16 pu_delay;
4714
4715 /* The time value is in microseconds. */
4716 if (dev->phy.type == B43_PHYTYPE_A)
4717 pu_delay = 3700;
4718 else
4719 pu_delay = 1050;
Johannes Berg05c914f2008-09-11 00:01:58 +02004720 if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC) || idle)
Michael Bueschd59f7202008-04-03 18:56:19 +02004721 pu_delay = 500;
4722 if ((dev->phy.radio_ver == 0x2050) && (dev->phy.radio_rev == 8))
4723 pu_delay = max(pu_delay, (u16)2400);
4724
4725 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SPUWKUP, pu_delay);
4726}
4727
4728/* Set the TSF CFP pre-TargetBeaconTransmissionTime. */
4729static void b43_set_pretbtt(struct b43_wldev *dev)
4730{
4731 u16 pretbtt;
4732
4733 /* The time value is in microseconds. */
Johannes Berg05c914f2008-09-11 00:01:58 +02004734 if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC)) {
Michael Bueschd59f7202008-04-03 18:56:19 +02004735 pretbtt = 2;
4736 } else {
4737 if (dev->phy.type == B43_PHYTYPE_A)
4738 pretbtt = 120;
4739 else
4740 pretbtt = 250;
4741 }
4742 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRETBTT, pretbtt);
4743 b43_write16(dev, B43_MMIO_TSF_CFP_PRETBTT, pretbtt);
4744}
4745
Michael Buesche4d6b792007-09-18 15:39:42 -04004746/* Shutdown a wireless core */
4747/* Locking: wl->mutex */
4748static void b43_wireless_core_exit(struct b43_wldev *dev)
4749{
Michael Buesch36dbd952009-09-04 22:51:29 +02004750 B43_WARN_ON(dev && b43_status(dev) > B43_STAT_INITIALIZED);
4751 if (!dev || b43_status(dev) != B43_STAT_INITIALIZED)
Michael Buesche4d6b792007-09-18 15:39:42 -04004752 return;
John W. Linville84c164a2010-08-06 15:31:45 -04004753
Michael Buesche4d6b792007-09-18 15:39:42 -04004754 b43_set_status(dev, B43_STAT_UNINIT);
4755
Michael Buesch1f7d87b2008-01-22 20:23:34 +01004756 /* Stop the microcode PSM. */
Rafał Miłecki50566352012-01-02 19:31:21 +01004757 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_PSM_RUN,
4758 B43_MACCTL_PSM_JMP0);
Michael Buesch1f7d87b2008-01-22 20:23:34 +01004759
Hauke Mehrtens50023002013-08-24 00:32:34 +02004760 switch (dev->dev->bus_type) {
4761#ifdef CONFIG_B43_BCMA
4762 case B43_BUS_BCMA:
4763 bcma_core_pci_down(dev->dev->bdev->bus);
4764 break;
4765#endif
4766#ifdef CONFIG_B43_SSB
4767 case B43_BUS_SSB:
4768 /* TODO */
4769 break;
4770#endif
4771 }
4772
Michael Buesche4d6b792007-09-18 15:39:42 -04004773 b43_dma_free(dev);
Michael Buesch5100d5a2008-03-29 21:01:16 +01004774 b43_pio_free(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004775 b43_chip_exit(dev);
Michael Bueschcb24f572008-09-03 12:12:20 +02004776 dev->phy.ops->switch_analog(dev, 0);
Michael Buesche66fee62007-12-26 17:47:10 +01004777 if (dev->wl->current_beacon) {
4778 dev_kfree_skb_any(dev->wl->current_beacon);
4779 dev->wl->current_beacon = NULL;
4780 }
4781
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004782 b43_device_disable(dev, 0);
4783 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004784}
4785
4786/* Initialize a wireless core */
4787static int b43_wireless_core_init(struct b43_wldev *dev)
4788{
Rafał Miłecki05814832011-05-18 02:06:39 +02004789 struct ssb_sprom *sprom = dev->dev->bus_sprom;
Michael Buesche4d6b792007-09-18 15:39:42 -04004790 struct b43_phy *phy = &dev->phy;
4791 int err;
Michael Buescha259d6a2008-04-18 21:06:37 +02004792 u64 hf;
Michael Buesche4d6b792007-09-18 15:39:42 -04004793
4794 B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
4795
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004796 err = b43_bus_powerup(dev, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04004797 if (err)
4798 goto out;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02004799 if (!b43_device_is_enabled(dev))
4800 b43_wireless_core_reset(dev, phy->gmode);
Michael Buesche4d6b792007-09-18 15:39:42 -04004801
Michael Bueschfb111372008-09-02 13:00:34 +02004802 /* Reset all data structures. */
Michael Buesche4d6b792007-09-18 15:39:42 -04004803 setup_struct_wldev_for_init(dev);
Michael Bueschfb111372008-09-02 13:00:34 +02004804 phy->ops->prepare_structs(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004805
4806 /* Enable IRQ routing to this device. */
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02004807 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02004808#ifdef CONFIG_B43_BCMA
4809 case B43_BUS_BCMA:
Hauke Mehrtensdfae7142012-09-29 20:40:18 +02004810 bcma_core_pci_irq_ctl(&dev->dev->bdev->bus->drv_pci[0],
Rafał Miłecki42c9a452011-07-06 15:45:27 +02004811 dev->dev->bdev, true);
Hauke Mehrtens50023002013-08-24 00:32:34 +02004812 bcma_core_pci_up(dev->dev->bdev->bus);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02004813 break;
4814#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02004815#ifdef CONFIG_B43_SSB
4816 case B43_BUS_SSB:
4817 ssb_pcicore_dev_irqvecs_enable(&dev->dev->sdev->bus->pcicore,
4818 dev->dev->sdev);
4819 break;
4820#endif
4821 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004822
4823 b43_imcfglo_timeouts_workaround(dev);
4824 b43_bluetooth_coext_disable(dev);
Michael Bueschfb111372008-09-02 13:00:34 +02004825 if (phy->ops->prepare_hardware) {
4826 err = phy->ops->prepare_hardware(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +02004827 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02004828 goto err_busdown;
Michael Bueschef1a6282008-08-27 18:53:02 +02004829 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004830 err = b43_chip_init(dev);
4831 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02004832 goto err_busdown;
Michael Buesche4d6b792007-09-18 15:39:42 -04004833 b43_shm_write16(dev, B43_SHM_SHARED,
Rafał Miłecki21d889d2011-05-18 02:06:38 +02004834 B43_SHM_SH_WLCOREREV, dev->dev->core_rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004835 hf = b43_hf_read(dev);
4836 if (phy->type == B43_PHYTYPE_G) {
4837 hf |= B43_HF_SYMW;
4838 if (phy->rev == 1)
4839 hf |= B43_HF_GDCW;
Larry Finger95de2842007-11-09 16:57:18 -06004840 if (sprom->boardflags_lo & B43_BFL_PACTRL)
Michael Buesche4d6b792007-09-18 15:39:42 -04004841 hf |= B43_HF_OFDMPABOOST;
Michael Buesch969d15c2009-02-20 14:27:15 +01004842 }
4843 if (phy->radio_ver == 0x2050) {
4844 if (phy->radio_rev == 6)
4845 hf |= B43_HF_4318TSSI;
4846 if (phy->radio_rev < 6)
4847 hf |= B43_HF_VCORECALC;
Michael Buesche4d6b792007-09-18 15:39:42 -04004848 }
Michael Buesch1cc8f472009-02-20 14:47:56 +01004849 if (sprom->boardflags_lo & B43_BFL_XTAL_NOSLOW)
4850 hf |= B43_HF_DSCRQ; /* Disable slowclock requests from ucode. */
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02004851#if defined(CONFIG_B43_SSB) && defined(CONFIG_SSB_DRIVER_PCICORE)
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02004852 if (dev->dev->bus_type == B43_BUS_SSB &&
4853 dev->dev->sdev->bus->bustype == SSB_BUSTYPE_PCI &&
4854 dev->dev->sdev->bus->pcicore.dev->id.revision <= 10)
Michael Buesch88219052009-02-20 14:58:59 +01004855 hf |= B43_HF_PCISCW; /* PCI slow clock workaround. */
Michael Buesch1a777332009-03-04 16:41:10 +01004856#endif
Michael Buesch25d3ef52009-02-20 15:39:21 +01004857 hf &= ~B43_HF_SKCFPUP;
Michael Buesche4d6b792007-09-18 15:39:42 -04004858 b43_hf_write(dev, hf);
4859
Michael Buesch74cfdba2007-10-28 16:19:44 +01004860 b43_set_retry_limits(dev, B43_DEFAULT_SHORT_RETRY_LIMIT,
4861 B43_DEFAULT_LONG_RETRY_LIMIT);
Michael Buesche4d6b792007-09-18 15:39:42 -04004862 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SFFBLIM, 3);
4863 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_LFFBLIM, 2);
4864
4865 /* Disable sending probe responses from firmware.
4866 * Setting the MaxTime to one usec will always trigger
4867 * a timeout, so we never send any probe resp.
4868 * A timeout of zero is infinite. */
4869 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRMAXTIME, 1);
4870
4871 b43_rate_memory_init(dev);
Michael Buesch5042c502008-04-05 15:05:00 +02004872 b43_set_phytxctl_defaults(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004873
4874 /* Minimum Contention Window */
Daniel Nguc5a079f2010-03-23 00:52:44 +13004875 if (phy->type == B43_PHYTYPE_B)
Michael Buesche4d6b792007-09-18 15:39:42 -04004876 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0x1F);
Daniel Nguc5a079f2010-03-23 00:52:44 +13004877 else
Michael Buesche4d6b792007-09-18 15:39:42 -04004878 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0xF);
Michael Buesche4d6b792007-09-18 15:39:42 -04004879 /* Maximum Contention Window */
4880 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MAXCONT, 0x3FF);
4881
Rafał Miłecki505fb012011-05-19 15:11:27 +02004882 if (b43_bus_host_is_pcmcia(dev->dev) ||
Rafał Miłeckicbe1e822011-08-16 21:44:21 +02004883 b43_bus_host_is_sdio(dev->dev)) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00004884 dev->__using_pio_transfers = true;
Rafał Miłeckicbe1e822011-08-16 21:44:21 +02004885 err = b43_pio_init(dev);
4886 } else if (dev->use_pio) {
4887 b43warn(dev->wl, "Forced PIO by use_pio module parameter. "
4888 "This should not be needed and will result in lower "
4889 "performance.\n");
Rusty Russell3db1cd52011-12-19 13:56:45 +00004890 dev->__using_pio_transfers = true;
Michael Buesch5100d5a2008-03-29 21:01:16 +01004891 err = b43_pio_init(dev);
4892 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00004893 dev->__using_pio_transfers = false;
Michael Buesch5100d5a2008-03-29 21:01:16 +01004894 err = b43_dma_init(dev);
4895 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004896 if (err)
4897 goto err_chip_exit;
Michael Buesch03b29772007-12-26 14:41:30 +01004898 b43_qos_init(dev);
Michael Bueschd59f7202008-04-03 18:56:19 +02004899 b43_set_synth_pu_delay(dev, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04004900 b43_bluetooth_coext_enable(dev);
4901
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004902 b43_bus_powerup(dev, !(sprom->boardflags_lo & B43_BFL_XTAL_NOSLOW));
Johannes Berg4150c572007-09-17 01:29:23 -04004903 b43_upload_card_macaddress(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004904 b43_security_init(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004905
Michael Buesch5ab95492009-09-10 20:31:46 +02004906 ieee80211_wake_queues(dev->wl->hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04004907
4908 b43_set_status(dev, B43_STAT_INITIALIZED);
4909
Larry Finger1a8d12272007-12-14 13:59:11 +01004910out:
Michael Buesche4d6b792007-09-18 15:39:42 -04004911 return err;
4912
Michael Bueschef1a6282008-08-27 18:53:02 +02004913err_chip_exit:
Michael Buesche4d6b792007-09-18 15:39:42 -04004914 b43_chip_exit(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +02004915err_busdown:
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004916 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004917 B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
4918 return err;
4919}
4920
Michael Buesch40faacc2007-10-28 16:29:32 +01004921static int b43_op_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01004922 struct ieee80211_vif *vif)
Michael Buesche4d6b792007-09-18 15:39:42 -04004923{
4924 struct b43_wl *wl = hw_to_b43_wl(hw);
4925 struct b43_wldev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004926 int err = -EOPNOTSUPP;
Johannes Berg4150c572007-09-17 01:29:23 -04004927
4928 /* TODO: allow WDS/AP devices to coexist */
4929
Johannes Berg1ed32e42009-12-23 13:15:45 +01004930 if (vif->type != NL80211_IFTYPE_AP &&
4931 vif->type != NL80211_IFTYPE_MESH_POINT &&
4932 vif->type != NL80211_IFTYPE_STATION &&
4933 vif->type != NL80211_IFTYPE_WDS &&
4934 vif->type != NL80211_IFTYPE_ADHOC)
Johannes Berg4150c572007-09-17 01:29:23 -04004935 return -EOPNOTSUPP;
Michael Buesche4d6b792007-09-18 15:39:42 -04004936
4937 mutex_lock(&wl->mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04004938 if (wl->operating)
Michael Buesche4d6b792007-09-18 15:39:42 -04004939 goto out_mutex_unlock;
4940
Johannes Berg1ed32e42009-12-23 13:15:45 +01004941 b43dbg(wl, "Adding Interface type %d\n", vif->type);
Michael Buesche4d6b792007-09-18 15:39:42 -04004942
4943 dev = wl->current_dev;
Rusty Russell3db1cd52011-12-19 13:56:45 +00004944 wl->operating = true;
Johannes Berg1ed32e42009-12-23 13:15:45 +01004945 wl->vif = vif;
4946 wl->if_type = vif->type;
4947 memcpy(wl->mac_addr, vif->addr, ETH_ALEN);
Michael Buesche4d6b792007-09-18 15:39:42 -04004948
Michael Buesche4d6b792007-09-18 15:39:42 -04004949 b43_adjust_opmode(dev);
Michael Bueschd59f7202008-04-03 18:56:19 +02004950 b43_set_pretbtt(dev);
4951 b43_set_synth_pu_delay(dev, 0);
Johannes Berg4150c572007-09-17 01:29:23 -04004952 b43_upload_card_macaddress(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004953
4954 err = 0;
Johannes Berg4150c572007-09-17 01:29:23 -04004955 out_mutex_unlock:
Michael Buesche4d6b792007-09-18 15:39:42 -04004956 mutex_unlock(&wl->mutex);
4957
Felix Fietkau2a190322011-08-10 13:50:30 -06004958 if (err == 0)
4959 b43_op_bss_info_changed(hw, vif, &vif->bss_conf, ~0);
4960
Michael Buesche4d6b792007-09-18 15:39:42 -04004961 return err;
4962}
4963
Michael Buesch40faacc2007-10-28 16:29:32 +01004964static void b43_op_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01004965 struct ieee80211_vif *vif)
Michael Buesche4d6b792007-09-18 15:39:42 -04004966{
4967 struct b43_wl *wl = hw_to_b43_wl(hw);
Johannes Berg4150c572007-09-17 01:29:23 -04004968 struct b43_wldev *dev = wl->current_dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004969
Johannes Berg1ed32e42009-12-23 13:15:45 +01004970 b43dbg(wl, "Removing Interface type %d\n", vif->type);
Michael Buesche4d6b792007-09-18 15:39:42 -04004971
4972 mutex_lock(&wl->mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04004973
4974 B43_WARN_ON(!wl->operating);
Johannes Berg1ed32e42009-12-23 13:15:45 +01004975 B43_WARN_ON(wl->vif != vif);
Johannes Berg32bfd352007-12-19 01:31:26 +01004976 wl->vif = NULL;
Johannes Berg4150c572007-09-17 01:29:23 -04004977
Rusty Russell3db1cd52011-12-19 13:56:45 +00004978 wl->operating = false;
Johannes Berg4150c572007-09-17 01:29:23 -04004979
Johannes Berg4150c572007-09-17 01:29:23 -04004980 b43_adjust_opmode(dev);
4981 memset(wl->mac_addr, 0, ETH_ALEN);
4982 b43_upload_card_macaddress(dev);
Johannes Berg4150c572007-09-17 01:29:23 -04004983
4984 mutex_unlock(&wl->mutex);
4985}
4986
Michael Buesch40faacc2007-10-28 16:29:32 +01004987static int b43_op_start(struct ieee80211_hw *hw)
Johannes Berg4150c572007-09-17 01:29:23 -04004988{
4989 struct b43_wl *wl = hw_to_b43_wl(hw);
4990 struct b43_wldev *dev = wl->current_dev;
4991 int did_init = 0;
WANG Cong923403b2007-10-16 14:29:38 -07004992 int err = 0;
Johannes Berg4150c572007-09-17 01:29:23 -04004993
Michael Buesch7be1bb62008-01-23 21:10:56 +01004994 /* Kill all old instance specific information to make sure
4995 * the card won't use it in the short timeframe between start
4996 * and mac80211 reconfiguring it. */
4997 memset(wl->bssid, 0, ETH_ALEN);
4998 memset(wl->mac_addr, 0, ETH_ALEN);
4999 wl->filter_flags = 0;
Rusty Russell3db1cd52011-12-19 13:56:45 +00005000 wl->radiotap_enabled = false;
Michael Buesche6f5b932008-03-05 21:18:49 +01005001 b43_qos_clear(wl);
Rusty Russell3db1cd52011-12-19 13:56:45 +00005002 wl->beacon0_uploaded = false;
5003 wl->beacon1_uploaded = false;
5004 wl->beacon_templates_virgin = true;
5005 wl->radio_enabled = true;
Michael Buesch7be1bb62008-01-23 21:10:56 +01005006
Johannes Berg4150c572007-09-17 01:29:23 -04005007 mutex_lock(&wl->mutex);
5008
5009 if (b43_status(dev) < B43_STAT_INITIALIZED) {
5010 err = b43_wireless_core_init(dev);
Johannes Bergf41f3f32009-06-07 12:30:34 -05005011 if (err)
Johannes Berg4150c572007-09-17 01:29:23 -04005012 goto out_mutex_unlock;
5013 did_init = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -04005014 }
5015
Johannes Berg4150c572007-09-17 01:29:23 -04005016 if (b43_status(dev) < B43_STAT_STARTED) {
5017 err = b43_wireless_core_start(dev);
5018 if (err) {
5019 if (did_init)
5020 b43_wireless_core_exit(dev);
5021 goto out_mutex_unlock;
5022 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005023 }
Johannes Berg4150c572007-09-17 01:29:23 -04005024
Johannes Bergf41f3f32009-06-07 12:30:34 -05005025 /* XXX: only do if device doesn't support rfkill irq */
5026 wiphy_rfkill_start_polling(hw->wiphy);
5027
Johannes Berg4150c572007-09-17 01:29:23 -04005028 out_mutex_unlock:
5029 mutex_unlock(&wl->mutex);
5030
Seth Forsheedbdedbd2012-04-25 17:28:00 -05005031 /*
5032 * Configuration may have been overwritten during initialization.
5033 * Reload the configuration, but only if initialization was
5034 * successful. Reloading the configuration after a failed init
5035 * may hang the system.
5036 */
5037 if (!err)
5038 b43_op_config(hw, ~0);
Felix Fietkau2a190322011-08-10 13:50:30 -06005039
Johannes Berg4150c572007-09-17 01:29:23 -04005040 return err;
5041}
5042
Michael Buesch40faacc2007-10-28 16:29:32 +01005043static void b43_op_stop(struct ieee80211_hw *hw)
Johannes Berg4150c572007-09-17 01:29:23 -04005044{
5045 struct b43_wl *wl = hw_to_b43_wl(hw);
5046 struct b43_wldev *dev = wl->current_dev;
5047
Michael Buescha82d9922008-04-04 21:40:06 +02005048 cancel_work_sync(&(wl->beacon_update_trigger));
Larry Finger1a8d12272007-12-14 13:59:11 +01005049
Guennadi Liakhovetskiccde8a42012-01-06 12:58:16 +01005050 if (!dev)
5051 goto out;
5052
Johannes Berg4150c572007-09-17 01:29:23 -04005053 mutex_lock(&wl->mutex);
Michael Buesch36dbd952009-09-04 22:51:29 +02005054 if (b43_status(dev) >= B43_STAT_STARTED) {
5055 dev = b43_wireless_core_stop(dev);
5056 if (!dev)
5057 goto out_unlock;
5058 }
Johannes Berg4150c572007-09-17 01:29:23 -04005059 b43_wireless_core_exit(dev);
Rusty Russell3db1cd52011-12-19 13:56:45 +00005060 wl->radio_enabled = false;
Michael Buesch36dbd952009-09-04 22:51:29 +02005061
5062out_unlock:
Michael Buesche4d6b792007-09-18 15:39:42 -04005063 mutex_unlock(&wl->mutex);
Guennadi Liakhovetskiccde8a42012-01-06 12:58:16 +01005064out:
Michael Buesch18c8ade2008-08-28 19:33:40 +02005065 cancel_work_sync(&(wl->txpower_adjust_work));
Michael Buesche4d6b792007-09-18 15:39:42 -04005066}
5067
Johannes Berg17741cd2008-09-11 00:02:02 +02005068static int b43_op_beacon_set_tim(struct ieee80211_hw *hw,
5069 struct ieee80211_sta *sta, bool set)
Michael Buesche66fee62007-12-26 17:47:10 +01005070{
5071 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesche66fee62007-12-26 17:47:10 +01005072
Felix Fietkau8f611282009-11-07 18:37:37 +01005073 /* FIXME: add locking */
Johannes Berg9d139c82008-07-09 14:40:37 +02005074 b43_update_templates(wl);
Michael Buesche66fee62007-12-26 17:47:10 +01005075
5076 return 0;
5077}
5078
Johannes Berg38968d02008-02-25 16:27:50 +01005079static void b43_op_sta_notify(struct ieee80211_hw *hw,
5080 struct ieee80211_vif *vif,
5081 enum sta_notify_cmd notify_cmd,
Johannes Berg17741cd2008-09-11 00:02:02 +02005082 struct ieee80211_sta *sta)
Johannes Berg38968d02008-02-25 16:27:50 +01005083{
5084 struct b43_wl *wl = hw_to_b43_wl(hw);
5085
5086 B43_WARN_ON(!vif || wl->vif != vif);
5087}
5088
Michael Buesch25d3ef52009-02-20 15:39:21 +01005089static void b43_op_sw_scan_start_notifier(struct ieee80211_hw *hw)
5090{
5091 struct b43_wl *wl = hw_to_b43_wl(hw);
5092 struct b43_wldev *dev;
5093
5094 mutex_lock(&wl->mutex);
5095 dev = wl->current_dev;
5096 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED)) {
5097 /* Disable CFP update during scan on other channels. */
5098 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_SKCFPUP);
5099 }
5100 mutex_unlock(&wl->mutex);
5101}
5102
5103static void b43_op_sw_scan_complete_notifier(struct ieee80211_hw *hw)
5104{
5105 struct b43_wl *wl = hw_to_b43_wl(hw);
5106 struct b43_wldev *dev;
5107
5108 mutex_lock(&wl->mutex);
5109 dev = wl->current_dev;
5110 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED)) {
5111 /* Re-enable CFP update. */
5112 b43_hf_write(dev, b43_hf_read(dev) & ~B43_HF_SKCFPUP);
5113 }
5114 mutex_unlock(&wl->mutex);
5115}
5116
John W. Linville354b4f02010-04-29 15:56:06 -04005117static int b43_op_get_survey(struct ieee80211_hw *hw, int idx,
5118 struct survey_info *survey)
5119{
5120 struct b43_wl *wl = hw_to_b43_wl(hw);
5121 struct b43_wldev *dev = wl->current_dev;
5122 struct ieee80211_conf *conf = &hw->conf;
5123
5124 if (idx != 0)
5125 return -ENOENT;
5126
Karl Beldan675a0b02013-03-25 16:26:57 +01005127 survey->channel = conf->chandef.chan;
John W. Linville354b4f02010-04-29 15:56:06 -04005128 survey->filled = SURVEY_INFO_NOISE_DBM;
5129 survey->noise = dev->stats.link_noise;
5130
5131 return 0;
5132}
5133
Michael Buesche4d6b792007-09-18 15:39:42 -04005134static const struct ieee80211_ops b43_hw_ops = {
Michael Buesch40faacc2007-10-28 16:29:32 +01005135 .tx = b43_op_tx,
5136 .conf_tx = b43_op_conf_tx,
5137 .add_interface = b43_op_add_interface,
5138 .remove_interface = b43_op_remove_interface,
5139 .config = b43_op_config,
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01005140 .bss_info_changed = b43_op_bss_info_changed,
Michael Buesch40faacc2007-10-28 16:29:32 +01005141 .configure_filter = b43_op_configure_filter,
5142 .set_key = b43_op_set_key,
gregor kowski035d0242009-08-19 22:35:45 +02005143 .update_tkip_key = b43_op_update_tkip_key,
Michael Buesch40faacc2007-10-28 16:29:32 +01005144 .get_stats = b43_op_get_stats,
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01005145 .get_tsf = b43_op_get_tsf,
5146 .set_tsf = b43_op_set_tsf,
Michael Buesch40faacc2007-10-28 16:29:32 +01005147 .start = b43_op_start,
5148 .stop = b43_op_stop,
Michael Buesche66fee62007-12-26 17:47:10 +01005149 .set_tim = b43_op_beacon_set_tim,
Johannes Berg38968d02008-02-25 16:27:50 +01005150 .sta_notify = b43_op_sta_notify,
Michael Buesch25d3ef52009-02-20 15:39:21 +01005151 .sw_scan_start = b43_op_sw_scan_start_notifier,
5152 .sw_scan_complete = b43_op_sw_scan_complete_notifier,
John W. Linville354b4f02010-04-29 15:56:06 -04005153 .get_survey = b43_op_get_survey,
Johannes Bergf41f3f32009-06-07 12:30:34 -05005154 .rfkill_poll = b43_rfkill_poll,
Michael Buesche4d6b792007-09-18 15:39:42 -04005155};
5156
5157/* Hard-reset the chip. Do not call this directly.
5158 * Use b43_controller_restart()
5159 */
5160static void b43_chip_reset(struct work_struct *work)
5161{
5162 struct b43_wldev *dev =
5163 container_of(work, struct b43_wldev, restart_work);
5164 struct b43_wl *wl = dev->wl;
5165 int err = 0;
5166 int prev_status;
5167
5168 mutex_lock(&wl->mutex);
5169
5170 prev_status = b43_status(dev);
5171 /* Bring the device down... */
Michael Buesch36dbd952009-09-04 22:51:29 +02005172 if (prev_status >= B43_STAT_STARTED) {
5173 dev = b43_wireless_core_stop(dev);
5174 if (!dev) {
5175 err = -ENODEV;
5176 goto out;
5177 }
5178 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005179 if (prev_status >= B43_STAT_INITIALIZED)
5180 b43_wireless_core_exit(dev);
5181
5182 /* ...and up again. */
5183 if (prev_status >= B43_STAT_INITIALIZED) {
5184 err = b43_wireless_core_init(dev);
5185 if (err)
5186 goto out;
5187 }
5188 if (prev_status >= B43_STAT_STARTED) {
5189 err = b43_wireless_core_start(dev);
5190 if (err) {
5191 b43_wireless_core_exit(dev);
5192 goto out;
5193 }
5194 }
Michael Buesch3bf0a322008-05-22 16:32:16 +02005195out:
5196 if (err)
5197 wl->current_dev = NULL; /* Failed to init the dev. */
Michael Buesche4d6b792007-09-18 15:39:42 -04005198 mutex_unlock(&wl->mutex);
Felix Fietkau2a190322011-08-10 13:50:30 -06005199
5200 if (err) {
Michael Buesche4d6b792007-09-18 15:39:42 -04005201 b43err(wl, "Controller restart FAILED\n");
Felix Fietkau2a190322011-08-10 13:50:30 -06005202 return;
5203 }
5204
5205 /* reload configuration */
5206 b43_op_config(wl->hw, ~0);
5207 if (wl->vif)
5208 b43_op_bss_info_changed(wl->hw, wl->vif, &wl->vif->bss_conf, ~0);
5209
5210 b43info(wl, "Controller restarted\n");
Michael Buesche4d6b792007-09-18 15:39:42 -04005211}
5212
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005213static int b43_setup_bands(struct b43_wldev *dev,
Michael Buesch96c755a2008-01-06 00:09:46 +01005214 bool have_2ghz_phy, bool have_5ghz_phy)
Michael Buesche4d6b792007-09-18 15:39:42 -04005215{
5216 struct ieee80211_hw *hw = dev->wl->hw;
Rafał Miłecki3695b932014-07-08 15:11:10 +02005217 struct b43_phy *phy = &dev->phy;
5218 bool limited_2g;
Rafał Miłeckib453fda62014-07-23 18:54:49 +02005219 bool limited_5g;
Rafał Miłecki3695b932014-07-08 15:11:10 +02005220
5221 /* We don't support all 2 GHz channels on some devices */
Rafał Miłeckic11082f2014-07-19 12:52:47 +02005222 limited_2g = phy->radio_ver == 0x2057 &&
5223 (phy->radio_rev == 9 || phy->radio_rev == 14);
Rafał Miłeckib453fda62014-07-23 18:54:49 +02005224 limited_5g = phy->radio_ver == 0x2057 &&
5225 phy->radio_rev == 9;
Michael Buesche4d6b792007-09-18 15:39:42 -04005226
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005227 if (have_2ghz_phy)
Rafał Miłecki3695b932014-07-08 15:11:10 +02005228 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = limited_2g ?
5229 &b43_band_2ghz_limited : &b43_band_2GHz;
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005230 if (dev->phy.type == B43_PHYTYPE_N) {
5231 if (have_5ghz_phy)
Rafał Miłeckib453fda62014-07-23 18:54:49 +02005232 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = limited_5g ?
5233 &b43_band_5GHz_nphy_limited :
5234 &b43_band_5GHz_nphy;
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005235 } else {
5236 if (have_5ghz_phy)
5237 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &b43_band_5GHz_aphy;
5238 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005239
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005240 dev->phy.supports_2ghz = have_2ghz_phy;
5241 dev->phy.supports_5ghz = have_5ghz_phy;
Michael Buesche4d6b792007-09-18 15:39:42 -04005242
5243 return 0;
5244}
5245
5246static void b43_wireless_core_detach(struct b43_wldev *dev)
5247{
5248 /* We release firmware that late to not be required to re-request
5249 * is all the time when we reinit the core. */
5250 b43_release_firmware(dev);
Michael Bueschfb111372008-09-02 13:00:34 +02005251 b43_phy_free(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005252}
5253
Rafał Miłecki075ca602014-05-19 23:18:54 +02005254static void b43_supported_bands(struct b43_wldev *dev, bool *have_2ghz_phy,
5255 bool *have_5ghz_phy)
5256{
5257 u16 dev_id = 0;
5258
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005259#ifdef CONFIG_B43_BCMA
5260 if (dev->dev->bus_type == B43_BUS_BCMA &&
5261 dev->dev->bdev->bus->hosttype == BCMA_HOSTTYPE_PCI)
5262 dev_id = dev->dev->bdev->bus->host_pci->device;
5263#endif
Rafał Miłecki075ca602014-05-19 23:18:54 +02005264#ifdef CONFIG_B43_SSB
5265 if (dev->dev->bus_type == B43_BUS_SSB &&
5266 dev->dev->sdev->bus->bustype == SSB_BUSTYPE_PCI)
5267 dev_id = dev->dev->sdev->bus->host_pci->device;
5268#endif
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005269 /* Override with SPROM value if available */
5270 if (dev->dev->bus_sprom->dev_id)
5271 dev_id = dev->dev->bus_sprom->dev_id;
Rafał Miłecki075ca602014-05-19 23:18:54 +02005272
5273 /* Note: below IDs can be "virtual" (not maching e.g. real PCI ID) */
5274 switch (dev_id) {
5275 case 0x4324: /* BCM4306 */
5276 case 0x4312: /* BCM4311 */
5277 case 0x4319: /* BCM4318 */
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005278 case 0x4328: /* BCM4321 */
5279 case 0x432b: /* BCM4322 */
5280 case 0x4350: /* BCM43222 */
5281 case 0x4353: /* BCM43224 */
5282 case 0x0576: /* BCM43224 */
5283 case 0x435f: /* BCM6362 */
5284 case 0x4331: /* BCM4331 */
5285 case 0x4359: /* BCM43228 */
5286 case 0x43a0: /* BCM4360 */
5287 case 0x43b1: /* BCM4352 */
Rafał Miłecki075ca602014-05-19 23:18:54 +02005288 /* Dual band devices */
5289 *have_2ghz_phy = true;
5290 *have_5ghz_phy = true;
5291 return;
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005292 case 0x4321: /* BCM4306 */
5293 case 0x4313: /* BCM4311 */
5294 case 0x431a: /* BCM4318 */
5295 case 0x432a: /* BCM4321 */
5296 case 0x432d: /* BCM4322 */
5297 case 0x4352: /* BCM43222 */
5298 case 0x4333: /* BCM4331 */
5299 case 0x43a2: /* BCM4360 */
5300 case 0x43b3: /* BCM4352 */
5301 /* 5 GHz only devices */
5302 *have_2ghz_phy = false;
5303 *have_5ghz_phy = true;
5304 return;
Rafał Miłecki075ca602014-05-19 23:18:54 +02005305 }
5306
5307 /* As a fallback, try to guess using PHY type */
5308 switch (dev->phy.type) {
5309 case B43_PHYTYPE_A:
5310 *have_2ghz_phy = false;
5311 *have_5ghz_phy = true;
5312 return;
5313 case B43_PHYTYPE_G:
5314 case B43_PHYTYPE_N:
5315 case B43_PHYTYPE_LP:
5316 case B43_PHYTYPE_HT:
5317 case B43_PHYTYPE_LCN:
5318 *have_2ghz_phy = true;
5319 *have_5ghz_phy = false;
5320 return;
5321 }
5322
5323 B43_WARN_ON(1);
5324}
5325
Michael Buesche4d6b792007-09-18 15:39:42 -04005326static int b43_wireless_core_attach(struct b43_wldev *dev)
5327{
5328 struct b43_wl *wl = dev->wl;
Rafał Miłecki09951ad2014-05-27 22:07:31 +02005329 struct b43_phy *phy = &dev->phy;
Michael Buesche4d6b792007-09-18 15:39:42 -04005330 int err;
Rafał Miłecki40c62262011-07-18 02:01:30 +02005331 u32 tmp;
Rusty Russell3db1cd52011-12-19 13:56:45 +00005332 bool have_2ghz_phy = false, have_5ghz_phy = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04005333
5334 /* Do NOT do any device initialization here.
5335 * Do it in wireless_core_init() instead.
5336 * This function is for gathering basic information about the HW, only.
5337 * Also some structs may be set up here. But most likely you want to have
5338 * that in core_init(), too.
5339 */
5340
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02005341 err = b43_bus_powerup(dev, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04005342 if (err) {
5343 b43err(wl, "Bus powerup failed\n");
5344 goto out;
5345 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005346
Rafał Miłecki09951ad2014-05-27 22:07:31 +02005347 phy->do_full_init = true;
5348
Rafał Miłecki075ca602014-05-19 23:18:54 +02005349 /* Try to guess supported bands for the first init needs */
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02005350 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02005351#ifdef CONFIG_B43_BCMA
5352 case B43_BUS_BCMA:
Rafał Miłecki40c62262011-07-18 02:01:30 +02005353 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOST);
5354 have_2ghz_phy = !!(tmp & B43_BCMA_IOST_2G_PHY);
5355 have_5ghz_phy = !!(tmp & B43_BCMA_IOST_5G_PHY);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02005356 break;
5357#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02005358#ifdef CONFIG_B43_SSB
5359 case B43_BUS_SSB:
5360 if (dev->dev->core_rev >= 5) {
Rafał Miłecki40c62262011-07-18 02:01:30 +02005361 tmp = ssb_read32(dev->dev->sdev, SSB_TMSHIGH);
5362 have_2ghz_phy = !!(tmp & B43_TMSHIGH_HAVE_2GHZ_PHY);
5363 have_5ghz_phy = !!(tmp & B43_TMSHIGH_HAVE_5GHZ_PHY);
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02005364 } else
5365 B43_WARN_ON(1);
5366 break;
5367#endif
5368 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005369
Michael Buesch96c755a2008-01-06 00:09:46 +01005370 dev->phy.gmode = have_2ghz_phy;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02005371 b43_wireless_core_reset(dev, dev->phy.gmode);
Michael Buesche4d6b792007-09-18 15:39:42 -04005372
Rafał Miłecki075ca602014-05-19 23:18:54 +02005373 /* Get the PHY type. */
Michael Buesche4d6b792007-09-18 15:39:42 -04005374 err = b43_phy_versioning(dev);
5375 if (err)
Michael Buesch21954c32007-09-27 15:31:40 +02005376 goto err_powerdown;
Rafał Miłecki075ca602014-05-19 23:18:54 +02005377
5378 /* Get real info about supported bands */
5379 b43_supported_bands(dev, &have_2ghz_phy, &have_5ghz_phy);
5380
5381 /* We don't support 5 GHz on some PHYs yet */
Rafał Miłecki72fcd3d2014-07-08 21:00:19 +02005382 if (have_5ghz_phy) {
5383 switch (dev->phy.type) {
5384 case B43_PHYTYPE_A:
5385 case B43_PHYTYPE_G:
Rafał Miłecki72fcd3d2014-07-08 21:00:19 +02005386 case B43_PHYTYPE_LP:
5387 case B43_PHYTYPE_HT:
5388 b43warn(wl, "5 GHz band is unsupported on this PHY\n");
5389 have_5ghz_phy = false;
5390 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005391 }
Rafał Miłecki075ca602014-05-19 23:18:54 +02005392
5393 if (!have_2ghz_phy && !have_5ghz_phy) {
5394 b43err(wl, "b43 can't support any band on this device\n");
Michael Buesch96c755a2008-01-06 00:09:46 +01005395 err = -EOPNOTSUPP;
5396 goto err_powerdown;
5397 }
Michael Buesch2e35af12008-04-27 19:06:18 +02005398
Michael Bueschfb111372008-09-02 13:00:34 +02005399 err = b43_phy_allocate(dev);
5400 if (err)
5401 goto err_powerdown;
5402
Michael Buesch96c755a2008-01-06 00:09:46 +01005403 dev->phy.gmode = have_2ghz_phy;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02005404 b43_wireless_core_reset(dev, dev->phy.gmode);
Michael Buesche4d6b792007-09-18 15:39:42 -04005405
5406 err = b43_validate_chipaccess(dev);
5407 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02005408 goto err_phy_free;
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005409 err = b43_setup_bands(dev, have_2ghz_phy, have_5ghz_phy);
Michael Buesche4d6b792007-09-18 15:39:42 -04005410 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02005411 goto err_phy_free;
Michael Buesche4d6b792007-09-18 15:39:42 -04005412
5413 /* Now set some default "current_dev" */
5414 if (!wl->current_dev)
5415 wl->current_dev = dev;
5416 INIT_WORK(&dev->restart_work, b43_chip_reset);
5417
Michael Bueschcb24f572008-09-03 12:12:20 +02005418 dev->phy.ops->switch_analog(dev, 0);
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02005419 b43_device_disable(dev, 0);
5420 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005421
5422out:
5423 return err;
5424
Michael Bueschfb111372008-09-02 13:00:34 +02005425err_phy_free:
5426 b43_phy_free(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005427err_powerdown:
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02005428 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005429 return err;
5430}
5431
Rafał Miłecki482f0532011-05-18 02:06:36 +02005432static void b43_one_core_detach(struct b43_bus_dev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04005433{
5434 struct b43_wldev *wldev;
5435 struct b43_wl *wl;
5436
Michael Buesch3bf0a322008-05-22 16:32:16 +02005437 /* Do not cancel ieee80211-workqueue based work here.
5438 * See comment in b43_remove(). */
5439
Rafał Miłecki74abacb2011-07-06 15:45:28 +02005440 wldev = b43_bus_get_wldev(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005441 wl = wldev->wl;
Michael Buesche4d6b792007-09-18 15:39:42 -04005442 b43_debugfs_remove_device(wldev);
5443 b43_wireless_core_detach(wldev);
5444 list_del(&wldev->list);
Rafał Miłecki74abacb2011-07-06 15:45:28 +02005445 b43_bus_set_wldev(dev, NULL);
Michael Buesche4d6b792007-09-18 15:39:42 -04005446 kfree(wldev);
5447}
5448
Rafał Miłecki482f0532011-05-18 02:06:36 +02005449static int b43_one_core_attach(struct b43_bus_dev *dev, struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04005450{
5451 struct b43_wldev *wldev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005452 int err = -ENOMEM;
5453
Michael Buesche4d6b792007-09-18 15:39:42 -04005454 wldev = kzalloc(sizeof(*wldev), GFP_KERNEL);
5455 if (!wldev)
5456 goto out;
5457
Linus Torvalds9e3bd912010-02-26 10:34:27 -08005458 wldev->use_pio = b43_modparam_pio;
Rafał Miłecki482f0532011-05-18 02:06:36 +02005459 wldev->dev = dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005460 wldev->wl = wl;
5461 b43_set_status(wldev, B43_STAT_UNINIT);
5462 wldev->bad_frames_preempt = modparam_bad_frames_preempt;
Michael Buesche4d6b792007-09-18 15:39:42 -04005463 INIT_LIST_HEAD(&wldev->list);
5464
5465 err = b43_wireless_core_attach(wldev);
5466 if (err)
5467 goto err_kfree_wldev;
5468
Rafał Miłecki74abacb2011-07-06 15:45:28 +02005469 b43_bus_set_wldev(dev, wldev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005470 b43_debugfs_add_device(wldev);
5471
5472 out:
5473 return err;
5474
5475 err_kfree_wldev:
5476 kfree(wldev);
5477 return err;
5478}
5479
Michael Buesch9fc38452008-04-19 16:53:00 +02005480#define IS_PDEV(pdev, _vendor, _device, _subvendor, _subdevice) ( \
5481 (pdev->vendor == PCI_VENDOR_ID_##_vendor) && \
5482 (pdev->device == _device) && \
5483 (pdev->subsystem_vendor == PCI_VENDOR_ID_##_subvendor) && \
5484 (pdev->subsystem_device == _subdevice) )
5485
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02005486#ifdef CONFIG_B43_SSB
Michael Buesche4d6b792007-09-18 15:39:42 -04005487static void b43_sprom_fixup(struct ssb_bus *bus)
5488{
Michael Buesch1855ba72008-04-18 20:51:41 +02005489 struct pci_dev *pdev;
5490
Michael Buesche4d6b792007-09-18 15:39:42 -04005491 /* boardflags workarounds */
5492 if (bus->boardinfo.vendor == SSB_BOARDVENDOR_DELL &&
Hauke Mehrtens5a20ef32012-04-29 02:04:06 +02005493 bus->chip_id == 0x4301 && bus->sprom.board_rev == 0x74)
Larry Finger95de2842007-11-09 16:57:18 -06005494 bus->sprom.boardflags_lo |= B43_BFL_BTCOEXIST;
Michael Buesche4d6b792007-09-18 15:39:42 -04005495 if (bus->boardinfo.vendor == PCI_VENDOR_ID_APPLE &&
Hauke Mehrtens5a20ef32012-04-29 02:04:06 +02005496 bus->boardinfo.type == 0x4E && bus->sprom.board_rev > 0x40)
Larry Finger95de2842007-11-09 16:57:18 -06005497 bus->sprom.boardflags_lo |= B43_BFL_PACTRL;
Michael Buesch1855ba72008-04-18 20:51:41 +02005498 if (bus->bustype == SSB_BUSTYPE_PCI) {
5499 pdev = bus->host_pci;
Michael Buesch9fc38452008-04-19 16:53:00 +02005500 if (IS_PDEV(pdev, BROADCOM, 0x4318, ASUSTEK, 0x100F) ||
Larry Finger430cd472008-08-14 18:57:11 -05005501 IS_PDEV(pdev, BROADCOM, 0x4320, DELL, 0x0003) ||
Larry Finger570bdfb2008-09-26 08:23:00 -05005502 IS_PDEV(pdev, BROADCOM, 0x4320, HP, 0x12f8) ||
Michael Buesch9fc38452008-04-19 16:53:00 +02005503 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0015) ||
Larry Fingera58d4522008-08-10 10:19:33 -05005504 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0014) ||
Larry Finger3bb91bf2008-09-19 14:47:38 -05005505 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0013) ||
5506 IS_PDEV(pdev, BROADCOM, 0x4320, MOTOROLA, 0x7010))
Michael Buesch1855ba72008-04-18 20:51:41 +02005507 bus->sprom.boardflags_lo &= ~B43_BFL_BTCOEXIST;
5508 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005509}
5510
Rafał Miłecki482f0532011-05-18 02:06:36 +02005511static void b43_wireless_exit(struct b43_bus_dev *dev, struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04005512{
5513 struct ieee80211_hw *hw = wl->hw;
5514
Rafał Miłecki482f0532011-05-18 02:06:36 +02005515 ssb_set_devtypedata(dev->sdev, NULL);
Michael Buesche4d6b792007-09-18 15:39:42 -04005516 ieee80211_free_hw(hw);
5517}
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02005518#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04005519
Rafał Miłeckid1507052011-07-05 23:54:07 +02005520static struct b43_wl *b43_wireless_init(struct b43_bus_dev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04005521{
Rafał Miłeckid1507052011-07-05 23:54:07 +02005522 struct ssb_sprom *sprom = dev->bus_sprom;
Michael Buesche4d6b792007-09-18 15:39:42 -04005523 struct ieee80211_hw *hw;
5524 struct b43_wl *wl;
Rafał Miłecki2729df22011-07-18 22:45:58 +02005525 char chip_name[6];
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01005526 int queue_num;
Michael Buesche4d6b792007-09-18 15:39:42 -04005527
5528 hw = ieee80211_alloc_hw(sizeof(*wl), &b43_hw_ops);
5529 if (!hw) {
5530 b43err(NULL, "Could not allocate ieee80211 device\n");
Rafał Miłecki0355a342011-05-17 14:00:01 +02005531 return ERR_PTR(-ENOMEM);
Michael Buesche4d6b792007-09-18 15:39:42 -04005532 }
Michael Buesch403a3a12009-06-08 21:04:57 +02005533 wl = hw_to_b43_wl(hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04005534
5535 /* fill hw info */
Johannes Berg605a0bd2008-07-15 10:10:01 +02005536 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
John W. Linvillef5c044e2010-04-30 15:37:00 -04005537 IEEE80211_HW_SIGNAL_DBM;
Bruno Randolf566bfe52008-05-08 19:15:40 +02005538
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07005539 hw->wiphy->interface_modes =
5540 BIT(NL80211_IFTYPE_AP) |
5541 BIT(NL80211_IFTYPE_MESH_POINT) |
5542 BIT(NL80211_IFTYPE_STATION) |
5543 BIT(NL80211_IFTYPE_WDS) |
5544 BIT(NL80211_IFTYPE_ADHOC);
5545
Antonio Quartulli78f9c852012-04-01 00:35:40 +03005546 hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
5547
Oleksij Rempele64add22012-06-05 20:39:32 +02005548 wl->hw_registred = false;
Johannes Berge6a98542008-10-21 12:40:02 +02005549 hw->max_rates = 2;
Michael Buesche4d6b792007-09-18 15:39:42 -04005550 SET_IEEE80211_DEV(hw, dev->dev);
Larry Finger95de2842007-11-09 16:57:18 -06005551 if (is_valid_ether_addr(sprom->et1mac))
5552 SET_IEEE80211_PERM_ADDR(hw, sprom->et1mac);
Michael Buesche4d6b792007-09-18 15:39:42 -04005553 else
Larry Finger95de2842007-11-09 16:57:18 -06005554 SET_IEEE80211_PERM_ADDR(hw, sprom->il0mac);
Michael Buesche4d6b792007-09-18 15:39:42 -04005555
Michael Buesch403a3a12009-06-08 21:04:57 +02005556 /* Initialize struct b43_wl */
Michael Buesche4d6b792007-09-18 15:39:42 -04005557 wl->hw = hw;
Michael Buesche4d6b792007-09-18 15:39:42 -04005558 mutex_init(&wl->mutex);
Michael Buesch36dbd952009-09-04 22:51:29 +02005559 spin_lock_init(&wl->hardirq_lock);
Michael Buescha82d9922008-04-04 21:40:06 +02005560 INIT_WORK(&wl->beacon_update_trigger, b43_beacon_update_trigger_work);
Michael Buesch18c8ade2008-08-28 19:33:40 +02005561 INIT_WORK(&wl->txpower_adjust_work, b43_phy_txpower_adjust_work);
Michael Bueschf5d40ee2009-09-04 22:53:18 +02005562 INIT_WORK(&wl->tx_work, b43_tx_work);
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01005563
5564 /* Initialize queues and flags. */
5565 for (queue_num = 0; queue_num < B43_QOS_QUEUE_NUM; queue_num++) {
5566 skb_queue_head_init(&wl->tx_queue[queue_num]);
5567 wl->tx_queue_stopped[queue_num] = 0;
5568 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005569
Rafał Miłecki2729df22011-07-18 22:45:58 +02005570 snprintf(chip_name, ARRAY_SIZE(chip_name),
5571 (dev->chip_id > 0x9999) ? "%d" : "%04X", dev->chip_id);
5572 b43info(wl, "Broadcom %s WLAN found (core revision %u)\n", chip_name,
5573 dev->core_rev);
Rafał Miłecki0355a342011-05-17 14:00:01 +02005574 return wl;
Michael Buesche4d6b792007-09-18 15:39:42 -04005575}
5576
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005577#ifdef CONFIG_B43_BCMA
5578static int b43_bcma_probe(struct bcma_device *core)
Michael Buesche4d6b792007-09-18 15:39:42 -04005579{
Rafał Miłecki397915c2011-07-06 19:03:46 +02005580 struct b43_bus_dev *dev;
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005581 struct b43_wl *wl;
5582 int err;
Rafał Miłecki397915c2011-07-06 19:03:46 +02005583
Rafał Miłecki89604002013-06-26 09:55:54 +02005584 if (!modparam_allhwsupport &&
5585 (core->id.rev == 0x17 || core->id.rev == 0x18)) {
5586 pr_err("Support for cores revisions 0x17 and 0x18 disabled by module param allhwsupport=0. Try b43.allhwsupport=1\n");
5587 return -ENOTSUPP;
5588 }
5589
Rafał Miłecki397915c2011-07-06 19:03:46 +02005590 dev = b43_bus_dev_bcma_init(core);
5591 if (!dev)
5592 return -ENODEV;
5593
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005594 wl = b43_wireless_init(dev);
5595 if (IS_ERR(wl)) {
5596 err = PTR_ERR(wl);
5597 goto bcma_out;
5598 }
5599
5600 err = b43_one_core_attach(dev, wl);
5601 if (err)
5602 goto bcma_err_wireless_exit;
5603
Larry Finger6b6fa582012-03-08 22:27:46 -06005604 /* setup and start work to load firmware */
5605 INIT_WORK(&wl->firmware_load, b43_request_firmware);
5606 schedule_work(&wl->firmware_load);
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005607
5608bcma_out:
5609 return err;
5610
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005611bcma_err_wireless_exit:
5612 ieee80211_free_hw(wl->hw);
5613 return err;
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005614}
5615
5616static void b43_bcma_remove(struct bcma_device *core)
5617{
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005618 struct b43_wldev *wldev = bcma_get_drvdata(core);
5619 struct b43_wl *wl = wldev->wl;
5620
5621 /* We must cancel any work here before unregistering from ieee80211,
5622 * as the ieee80211 unreg will destroy the workqueue. */
5623 cancel_work_sync(&wldev->restart_work);
Larry Finger63a02ce2013-02-25 06:09:24 +00005624 cancel_work_sync(&wl->firmware_load);
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005625
Oleksij Rempele64add22012-06-05 20:39:32 +02005626 B43_WARN_ON(!wl);
Larry Fingerf89ff642012-10-24 08:57:16 -05005627 if (!wldev->fw.ucode.data)
5628 return; /* NULL if firmware never loaded */
Oleksij Rempele64add22012-06-05 20:39:32 +02005629 if (wl->current_dev == wldev && wl->hw_registred) {
Oleksij Rempele64add22012-06-05 20:39:32 +02005630 b43_leds_stop(wldev);
5631 ieee80211_unregister_hw(wl->hw);
5632 }
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005633
5634 b43_one_core_detach(wldev->dev);
5635
Larry Finger09164042014-01-12 15:11:37 -06005636 /* Unregister HW RNG driver */
5637 b43_rng_exit(wl);
5638
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005639 b43_leds_unregister(wl);
5640
5641 ieee80211_free_hw(wl->hw);
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005642}
5643
5644static struct bcma_driver b43_bcma_driver = {
5645 .name = KBUILD_MODNAME,
5646 .id_table = b43_bcma_tbl,
5647 .probe = b43_bcma_probe,
5648 .remove = b43_bcma_remove,
5649};
5650#endif
5651
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005652#ifdef CONFIG_B43_SSB
Rafał Miłeckiaa63418a2011-05-18 02:06:35 +02005653static
5654int b43_ssb_probe(struct ssb_device *sdev, const struct ssb_device_id *id)
Michael Buesche4d6b792007-09-18 15:39:42 -04005655{
Rafał Miłecki482f0532011-05-18 02:06:36 +02005656 struct b43_bus_dev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005657 struct b43_wl *wl;
5658 int err;
Michael Buesche4d6b792007-09-18 15:39:42 -04005659
Rafał Miłecki482f0532011-05-18 02:06:36 +02005660 dev = b43_bus_dev_ssb_init(sdev);
Dan Carpenter5b49b352011-06-09 10:09:34 +03005661 if (!dev)
5662 return -ENOMEM;
Rafał Miłecki482f0532011-05-18 02:06:36 +02005663
Rafał Miłeckiaa63418a2011-05-18 02:06:35 +02005664 wl = ssb_get_devtypedata(sdev);
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005665 if (wl) {
5666 b43err(NULL, "Dual-core devices are not supported\n");
5667 err = -ENOTSUPP;
5668 goto err_ssb_kfree_dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005669 }
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005670
5671 b43_sprom_fixup(sdev->bus);
5672
5673 wl = b43_wireless_init(dev);
5674 if (IS_ERR(wl)) {
5675 err = PTR_ERR(wl);
5676 goto err_ssb_kfree_dev;
5677 }
5678 ssb_set_devtypedata(sdev, wl);
5679 B43_WARN_ON(ssb_get_devtypedata(sdev) != wl);
5680
Michael Buesche4d6b792007-09-18 15:39:42 -04005681 err = b43_one_core_attach(dev, wl);
5682 if (err)
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005683 goto err_ssb_wireless_exit;
Michael Buesche4d6b792007-09-18 15:39:42 -04005684
Larry Finger6b6fa582012-03-08 22:27:46 -06005685 /* setup and start work to load firmware */
5686 INIT_WORK(&wl->firmware_load, b43_request_firmware);
5687 schedule_work(&wl->firmware_load);
Michael Buesche4d6b792007-09-18 15:39:42 -04005688
Michael Buesche4d6b792007-09-18 15:39:42 -04005689 return err;
5690
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005691err_ssb_wireless_exit:
5692 b43_wireless_exit(dev, wl);
5693err_ssb_kfree_dev:
5694 kfree(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005695 return err;
5696}
5697
Rafał Miłeckiaa63418a2011-05-18 02:06:35 +02005698static void b43_ssb_remove(struct ssb_device *sdev)
Michael Buesche4d6b792007-09-18 15:39:42 -04005699{
Rafał Miłeckiaa63418a2011-05-18 02:06:35 +02005700 struct b43_wl *wl = ssb_get_devtypedata(sdev);
5701 struct b43_wldev *wldev = ssb_get_drvdata(sdev);
Pavel Roskine61b52d2011-07-22 18:07:13 -04005702 struct b43_bus_dev *dev = wldev->dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005703
Michael Buesch3bf0a322008-05-22 16:32:16 +02005704 /* We must cancel any work here before unregistering from ieee80211,
5705 * as the ieee80211 unreg will destroy the workqueue. */
5706 cancel_work_sync(&wldev->restart_work);
Larry Finger63a02ce2013-02-25 06:09:24 +00005707 cancel_work_sync(&wl->firmware_load);
Michael Buesch3bf0a322008-05-22 16:32:16 +02005708
Michael Buesche4d6b792007-09-18 15:39:42 -04005709 B43_WARN_ON(!wl);
Larry Fingerf89ff642012-10-24 08:57:16 -05005710 if (!wldev->fw.ucode.data)
5711 return; /* NULL if firmware never loaded */
Oleksij Rempele64add22012-06-05 20:39:32 +02005712 if (wl->current_dev == wldev && wl->hw_registred) {
Albert Herranz82905ac2009-09-16 00:26:19 +02005713 b43_leds_stop(wldev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005714 ieee80211_unregister_hw(wl->hw);
Michael Buesch403a3a12009-06-08 21:04:57 +02005715 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005716
Pavel Roskine61b52d2011-07-22 18:07:13 -04005717 b43_one_core_detach(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005718
Larry Finger09164042014-01-12 15:11:37 -06005719 /* Unregister HW RNG driver */
5720 b43_rng_exit(wl);
5721
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02005722 b43_leds_unregister(wl);
5723 b43_wireless_exit(dev, wl);
Michael Buesche4d6b792007-09-18 15:39:42 -04005724}
5725
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005726static struct ssb_driver b43_ssb_driver = {
5727 .name = KBUILD_MODNAME,
5728 .id_table = b43_ssb_tbl,
5729 .probe = b43_ssb_probe,
5730 .remove = b43_ssb_remove,
5731};
5732#endif /* CONFIG_B43_SSB */
5733
Michael Buesche4d6b792007-09-18 15:39:42 -04005734/* Perform a hardware reset. This can be called from any context. */
5735void b43_controller_restart(struct b43_wldev *dev, const char *reason)
5736{
5737 /* Must avoid requeueing, if we are in shutdown. */
5738 if (b43_status(dev) < B43_STAT_INITIALIZED)
5739 return;
5740 b43info(dev->wl, "Controller RESET (%s) ...\n", reason);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04005741 ieee80211_queue_work(dev->wl->hw, &dev->restart_work);
Michael Buesche4d6b792007-09-18 15:39:42 -04005742}
5743
Michael Buesch26bc7832008-02-09 00:18:35 +01005744static void b43_print_driverinfo(void)
5745{
5746 const char *feat_pci = "", *feat_pcmcia = "", *feat_nphy = "",
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005747 *feat_leds = "", *feat_sdio = "";
Michael Buesch26bc7832008-02-09 00:18:35 +01005748
5749#ifdef CONFIG_B43_PCI_AUTOSELECT
5750 feat_pci = "P";
5751#endif
5752#ifdef CONFIG_B43_PCMCIA
5753 feat_pcmcia = "M";
5754#endif
Rafał Miłecki692d2c02010-12-07 21:56:00 +01005755#ifdef CONFIG_B43_PHY_N
Michael Buesch26bc7832008-02-09 00:18:35 +01005756 feat_nphy = "N";
5757#endif
5758#ifdef CONFIG_B43_LEDS
5759 feat_leds = "L";
5760#endif
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005761#ifdef CONFIG_B43_SDIO
5762 feat_sdio = "S";
5763#endif
Michael Buesch26bc7832008-02-09 00:18:35 +01005764 printk(KERN_INFO "Broadcom 43xx driver loaded "
Michael Büsch8b0be902011-08-21 17:24:47 +02005765 "[ Features: %s%s%s%s%s ]\n",
Michael Buesch26bc7832008-02-09 00:18:35 +01005766 feat_pci, feat_pcmcia, feat_nphy,
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005767 feat_leds, feat_sdio);
Michael Buesch26bc7832008-02-09 00:18:35 +01005768}
5769
Michael Buesche4d6b792007-09-18 15:39:42 -04005770static int __init b43_init(void)
5771{
5772 int err;
5773
5774 b43_debugfs_init();
5775 err = b43_pcmcia_init();
5776 if (err)
5777 goto err_dfs_exit;
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005778 err = b43_sdio_init();
Michael Buesche4d6b792007-09-18 15:39:42 -04005779 if (err)
5780 goto err_pcmcia_exit;
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005781#ifdef CONFIG_B43_BCMA
5782 err = bcma_driver_register(&b43_bcma_driver);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005783 if (err)
5784 goto err_sdio_exit;
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005785#endif
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005786#ifdef CONFIG_B43_SSB
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005787 err = ssb_driver_register(&b43_ssb_driver);
5788 if (err)
5789 goto err_bcma_driver_exit;
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005790#endif
Michael Buesch26bc7832008-02-09 00:18:35 +01005791 b43_print_driverinfo();
Michael Buesche4d6b792007-09-18 15:39:42 -04005792
5793 return err;
5794
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005795#ifdef CONFIG_B43_SSB
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005796err_bcma_driver_exit:
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005797#endif
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005798#ifdef CONFIG_B43_BCMA
5799 bcma_driver_unregister(&b43_bcma_driver);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005800err_sdio_exit:
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005801#endif
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005802 b43_sdio_exit();
Michael Buesche4d6b792007-09-18 15:39:42 -04005803err_pcmcia_exit:
5804 b43_pcmcia_exit();
5805err_dfs_exit:
5806 b43_debugfs_exit();
5807 return err;
5808}
5809
5810static void __exit b43_exit(void)
5811{
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005812#ifdef CONFIG_B43_SSB
Michael Buesche4d6b792007-09-18 15:39:42 -04005813 ssb_driver_unregister(&b43_ssb_driver);
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005814#endif
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005815#ifdef CONFIG_B43_BCMA
5816 bcma_driver_unregister(&b43_bcma_driver);
5817#endif
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005818 b43_sdio_exit();
Michael Buesche4d6b792007-09-18 15:39:42 -04005819 b43_pcmcia_exit();
5820 b43_debugfs_exit();
5821}
5822
5823module_init(b43_init)
5824module_exit(b43_exit)