blob: da0d9e6087cab6a5aab422ff688b19c5c87bba84 [file] [log] [blame]
Thomas Gleixnerca47d342019-05-19 15:51:50 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Larry Finger75388ac2007-09-25 16:46:54 -07002/*
3 *
4 * Broadcom B43legacy wireless driver
5 *
6 * Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>
Stefano Brivio6fff1c62008-02-09 07:20:43 +01007 * Copyright (c) 2005-2008 Stefano Brivio <stefano.brivio@polimi.it>
Michael Büscheb032b92011-07-04 20:50:05 +02008 * Copyright (c) 2005, 2006 Michael Buesch <m@bues.ch>
Larry Finger75388ac2007-09-25 16:46:54 -07009 * Copyright (c) 2005 Danny van Dyk <kugelfang@gentoo.org>
10 * Copyright (c) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
11 * Copyright (c) 2007 Larry Finger <Larry.Finger@lwfinger.net>
12 *
13 * Some parts of the code in this file are derived from the ipw2200
14 * driver Copyright(c) 2003 - 2004 Intel Corporation.
15
Larry Finger75388ac2007-09-25 16:46:54 -070016 */
17
18#include <linux/delay.h>
19#include <linux/init.h>
Paul Gortmakerac5c24e92011-08-30 14:18:44 -040020#include <linux/module.h>
Larry Finger75388ac2007-09-25 16:46:54 -070021#include <linux/if_arp.h>
22#include <linux/etherdevice.h>
Larry Finger75388ac2007-09-25 16:46:54 -070023#include <linux/firmware.h>
Larry Finger75388ac2007-09-25 16:46:54 -070024#include <linux/workqueue.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010025#include <linux/sched/signal.h>
Larry Finger75388ac2007-09-25 16:46:54 -070026#include <linux/skbuff.h>
27#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Larry Finger75388ac2007-09-25 16:46:54 -070029#include <net/dst.h>
30#include <asm/unaligned.h>
31
32#include "b43legacy.h"
33#include "main.h"
34#include "debugfs.h"
35#include "phy.h"
36#include "dma.h"
37#include "pio.h"
38#include "sysfs.h"
39#include "xmit.h"
40#include "radio.h"
41
42
43MODULE_DESCRIPTION("Broadcom B43legacy wireless driver");
44MODULE_AUTHOR("Martin Langer");
45MODULE_AUTHOR("Stefano Brivio");
46MODULE_AUTHOR("Michael Buesch");
47MODULE_LICENSE("GPL");
48
Tim Gardnerc2f4f522010-01-12 12:58:17 -060049MODULE_FIRMWARE("b43legacy/ucode2.fw");
50MODULE_FIRMWARE("b43legacy/ucode4.fw");
Stefano Brivio1a1c3602008-02-12 03:09:00 +010051
Larry Finger75388ac2007-09-25 16:46:54 -070052#if defined(CONFIG_B43LEGACY_DMA) && defined(CONFIG_B43LEGACY_PIO)
53static int modparam_pio;
54module_param_named(pio, modparam_pio, int, 0444);
55MODULE_PARM_DESC(pio, "enable(1) / disable(0) PIO mode");
56#elif defined(CONFIG_B43LEGACY_DMA)
57# define modparam_pio 0
58#elif defined(CONFIG_B43LEGACY_PIO)
59# define modparam_pio 1
60#endif
61
62static int modparam_bad_frames_preempt;
63module_param_named(bad_frames_preempt, modparam_bad_frames_preempt, int, 0444);
64MODULE_PARM_DESC(bad_frames_preempt, "enable(1) / disable(0) Bad Frames"
65 " Preemption");
66
Larry Finger75388ac2007-09-25 16:46:54 -070067static char modparam_fwpostfix[16];
68module_param_string(fwpostfix, modparam_fwpostfix, 16, 0444);
69MODULE_PARM_DESC(fwpostfix, "Postfix for the firmware files to load.");
70
Larry Finger75388ac2007-09-25 16:46:54 -070071/* The following table supports BCM4301, BCM4303 and BCM4306/2 devices. */
72static const struct ssb_device_id b43legacy_ssb_tbl[] = {
73 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 2),
74 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 4),
Joe Perches673e2ba2015-02-10 13:19:24 -080075 {},
Larry Finger75388ac2007-09-25 16:46:54 -070076};
77MODULE_DEVICE_TABLE(ssb, b43legacy_ssb_tbl);
78
79
80/* Channel and ratetables are shared for all devices.
81 * They can't be const, because ieee80211 puts some precalculated
82 * data in there. This data is the same for all devices, so we don't
83 * get concurrency issues */
84#define RATETAB_ENT(_rateid, _flags) \
Johannes Berg8318d782008-01-24 19:38:38 +010085 { \
86 .bitrate = B43legacy_RATE_TO_100KBPS(_rateid), \
87 .hw_value = (_rateid), \
88 .flags = (_flags), \
Larry Finger75388ac2007-09-25 16:46:54 -070089 }
Johannes Berg8318d782008-01-24 19:38:38 +010090/*
91 * NOTE: When changing this, sync with xmit.c's
92 * b43legacy_plcp_get_bitrate_idx_* functions!
93 */
Larry Finger75388ac2007-09-25 16:46:54 -070094static struct ieee80211_rate __b43legacy_ratetable[] = {
Johannes Berg8318d782008-01-24 19:38:38 +010095 RATETAB_ENT(B43legacy_CCK_RATE_1MB, 0),
96 RATETAB_ENT(B43legacy_CCK_RATE_2MB, IEEE80211_RATE_SHORT_PREAMBLE),
97 RATETAB_ENT(B43legacy_CCK_RATE_5MB, IEEE80211_RATE_SHORT_PREAMBLE),
98 RATETAB_ENT(B43legacy_CCK_RATE_11MB, IEEE80211_RATE_SHORT_PREAMBLE),
99 RATETAB_ENT(B43legacy_OFDM_RATE_6MB, 0),
100 RATETAB_ENT(B43legacy_OFDM_RATE_9MB, 0),
101 RATETAB_ENT(B43legacy_OFDM_RATE_12MB, 0),
102 RATETAB_ENT(B43legacy_OFDM_RATE_18MB, 0),
103 RATETAB_ENT(B43legacy_OFDM_RATE_24MB, 0),
104 RATETAB_ENT(B43legacy_OFDM_RATE_36MB, 0),
105 RATETAB_ENT(B43legacy_OFDM_RATE_48MB, 0),
106 RATETAB_ENT(B43legacy_OFDM_RATE_54MB, 0),
Larry Finger75388ac2007-09-25 16:46:54 -0700107};
Larry Finger75388ac2007-09-25 16:46:54 -0700108#define b43legacy_b_ratetable (__b43legacy_ratetable + 0)
109#define b43legacy_b_ratetable_size 4
110#define b43legacy_g_ratetable (__b43legacy_ratetable + 0)
111#define b43legacy_g_ratetable_size 12
112
113#define CHANTAB_ENT(_chanid, _freq) \
114 { \
Johannes Berg8318d782008-01-24 19:38:38 +0100115 .center_freq = (_freq), \
116 .hw_value = (_chanid), \
Larry Finger75388ac2007-09-25 16:46:54 -0700117 }
118static struct ieee80211_channel b43legacy_bg_chantable[] = {
119 CHANTAB_ENT(1, 2412),
120 CHANTAB_ENT(2, 2417),
121 CHANTAB_ENT(3, 2422),
122 CHANTAB_ENT(4, 2427),
123 CHANTAB_ENT(5, 2432),
124 CHANTAB_ENT(6, 2437),
125 CHANTAB_ENT(7, 2442),
126 CHANTAB_ENT(8, 2447),
127 CHANTAB_ENT(9, 2452),
128 CHANTAB_ENT(10, 2457),
129 CHANTAB_ENT(11, 2462),
130 CHANTAB_ENT(12, 2467),
131 CHANTAB_ENT(13, 2472),
132 CHANTAB_ENT(14, 2484),
133};
Johannes Berg8318d782008-01-24 19:38:38 +0100134
135static struct ieee80211_supported_band b43legacy_band_2GHz_BPHY = {
136 .channels = b43legacy_bg_chantable,
137 .n_channels = ARRAY_SIZE(b43legacy_bg_chantable),
138 .bitrates = b43legacy_b_ratetable,
139 .n_bitrates = b43legacy_b_ratetable_size,
140};
141
142static struct ieee80211_supported_band b43legacy_band_2GHz_GPHY = {
143 .channels = b43legacy_bg_chantable,
144 .n_channels = ARRAY_SIZE(b43legacy_bg_chantable),
145 .bitrates = b43legacy_g_ratetable,
146 .n_bitrates = b43legacy_g_ratetable_size,
147};
Larry Finger75388ac2007-09-25 16:46:54 -0700148
149static void b43legacy_wireless_core_exit(struct b43legacy_wldev *dev);
150static int b43legacy_wireless_core_init(struct b43legacy_wldev *dev);
151static void b43legacy_wireless_core_stop(struct b43legacy_wldev *dev);
152static int b43legacy_wireless_core_start(struct b43legacy_wldev *dev);
153
154
155static int b43legacy_ratelimit(struct b43legacy_wl *wl)
156{
157 if (!wl || !wl->current_dev)
158 return 1;
159 if (b43legacy_status(wl->current_dev) < B43legacy_STAT_STARTED)
160 return 1;
161 /* We are up and running.
162 * Ratelimit the messages to avoid DoS over the net. */
163 return net_ratelimit();
164}
165
166void b43legacyinfo(struct b43legacy_wl *wl, const char *fmt, ...)
167{
Joe Perches0e67d6cb2010-11-09 16:35:19 -0800168 struct va_format vaf;
Larry Finger75388ac2007-09-25 16:46:54 -0700169 va_list args;
170
171 if (!b43legacy_ratelimit(wl))
172 return;
Joe Perches0e67d6cb2010-11-09 16:35:19 -0800173
Larry Finger75388ac2007-09-25 16:46:54 -0700174 va_start(args, fmt);
Joe Perches0e67d6cb2010-11-09 16:35:19 -0800175
176 vaf.fmt = fmt;
177 vaf.va = &args;
178
179 printk(KERN_INFO "b43legacy-%s: %pV",
180 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
181
Larry Finger75388ac2007-09-25 16:46:54 -0700182 va_end(args);
183}
184
185void b43legacyerr(struct b43legacy_wl *wl, const char *fmt, ...)
186{
Joe Perches0e67d6cb2010-11-09 16:35:19 -0800187 struct va_format vaf;
Larry Finger75388ac2007-09-25 16:46:54 -0700188 va_list args;
189
190 if (!b43legacy_ratelimit(wl))
191 return;
Joe Perches0e67d6cb2010-11-09 16:35:19 -0800192
Larry Finger75388ac2007-09-25 16:46:54 -0700193 va_start(args, fmt);
Joe Perches0e67d6cb2010-11-09 16:35:19 -0800194
195 vaf.fmt = fmt;
196 vaf.va = &args;
197
198 printk(KERN_ERR "b43legacy-%s ERROR: %pV",
199 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
200
Larry Finger75388ac2007-09-25 16:46:54 -0700201 va_end(args);
202}
203
204void b43legacywarn(struct b43legacy_wl *wl, const char *fmt, ...)
205{
Joe Perches0e67d6cb2010-11-09 16:35:19 -0800206 struct va_format vaf;
Larry Finger75388ac2007-09-25 16:46:54 -0700207 va_list args;
208
209 if (!b43legacy_ratelimit(wl))
210 return;
Joe Perches0e67d6cb2010-11-09 16:35:19 -0800211
Larry Finger75388ac2007-09-25 16:46:54 -0700212 va_start(args, fmt);
Joe Perches0e67d6cb2010-11-09 16:35:19 -0800213
214 vaf.fmt = fmt;
215 vaf.va = &args;
216
217 printk(KERN_WARNING "b43legacy-%s warning: %pV",
218 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
219
Larry Finger75388ac2007-09-25 16:46:54 -0700220 va_end(args);
221}
222
223#if B43legacy_DEBUG
224void b43legacydbg(struct b43legacy_wl *wl, const char *fmt, ...)
225{
Joe Perches0e67d6cb2010-11-09 16:35:19 -0800226 struct va_format vaf;
Larry Finger75388ac2007-09-25 16:46:54 -0700227 va_list args;
228
229 va_start(args, fmt);
Joe Perches0e67d6cb2010-11-09 16:35:19 -0800230
231 vaf.fmt = fmt;
232 vaf.va = &args;
233
234 printk(KERN_DEBUG "b43legacy-%s debug: %pV",
235 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
236
Larry Finger75388ac2007-09-25 16:46:54 -0700237 va_end(args);
238}
239#endif /* DEBUG */
240
241static void b43legacy_ram_write(struct b43legacy_wldev *dev, u16 offset,
242 u32 val)
243{
244 u32 status;
245
246 B43legacy_WARN_ON(offset % 4 != 0);
247
Stefano Brivioe78c9d22008-01-23 14:48:50 +0100248 status = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
249 if (status & B43legacy_MACCTL_BE)
Larry Finger75388ac2007-09-25 16:46:54 -0700250 val = swab32(val);
251
252 b43legacy_write32(dev, B43legacy_MMIO_RAM_CONTROL, offset);
Larry Finger75388ac2007-09-25 16:46:54 -0700253 b43legacy_write32(dev, B43legacy_MMIO_RAM_DATA, val);
254}
255
256static inline
257void b43legacy_shm_control_word(struct b43legacy_wldev *dev,
258 u16 routing, u16 offset)
259{
260 u32 control;
261
262 /* "offset" is the WORD offset. */
263
264 control = routing;
265 control <<= 16;
266 control |= offset;
267 b43legacy_write32(dev, B43legacy_MMIO_SHM_CONTROL, control);
268}
269
270u32 b43legacy_shm_read32(struct b43legacy_wldev *dev,
271 u16 routing, u16 offset)
272{
273 u32 ret;
274
275 if (routing == B43legacy_SHM_SHARED) {
276 B43legacy_WARN_ON((offset & 0x0001) != 0);
277 if (offset & 0x0003) {
278 /* Unaligned access */
279 b43legacy_shm_control_word(dev, routing, offset >> 2);
280 ret = b43legacy_read16(dev,
281 B43legacy_MMIO_SHM_DATA_UNALIGNED);
282 ret <<= 16;
283 b43legacy_shm_control_word(dev, routing,
284 (offset >> 2) + 1);
285 ret |= b43legacy_read16(dev, B43legacy_MMIO_SHM_DATA);
286
287 return ret;
288 }
289 offset >>= 2;
290 }
291 b43legacy_shm_control_word(dev, routing, offset);
292 ret = b43legacy_read32(dev, B43legacy_MMIO_SHM_DATA);
293
294 return ret;
295}
296
297u16 b43legacy_shm_read16(struct b43legacy_wldev *dev,
298 u16 routing, u16 offset)
299{
300 u16 ret;
301
302 if (routing == B43legacy_SHM_SHARED) {
303 B43legacy_WARN_ON((offset & 0x0001) != 0);
304 if (offset & 0x0003) {
305 /* Unaligned access */
306 b43legacy_shm_control_word(dev, routing, offset >> 2);
307 ret = b43legacy_read16(dev,
308 B43legacy_MMIO_SHM_DATA_UNALIGNED);
309
310 return ret;
311 }
312 offset >>= 2;
313 }
314 b43legacy_shm_control_word(dev, routing, offset);
315 ret = b43legacy_read16(dev, B43legacy_MMIO_SHM_DATA);
316
317 return ret;
318}
319
320void b43legacy_shm_write32(struct b43legacy_wldev *dev,
321 u16 routing, u16 offset,
322 u32 value)
323{
324 if (routing == B43legacy_SHM_SHARED) {
325 B43legacy_WARN_ON((offset & 0x0001) != 0);
326 if (offset & 0x0003) {
327 /* Unaligned access */
328 b43legacy_shm_control_word(dev, routing, offset >> 2);
Larry Finger75388ac2007-09-25 16:46:54 -0700329 b43legacy_write16(dev,
330 B43legacy_MMIO_SHM_DATA_UNALIGNED,
331 (value >> 16) & 0xffff);
Larry Finger75388ac2007-09-25 16:46:54 -0700332 b43legacy_shm_control_word(dev, routing,
333 (offset >> 2) + 1);
Larry Finger75388ac2007-09-25 16:46:54 -0700334 b43legacy_write16(dev, B43legacy_MMIO_SHM_DATA,
335 value & 0xffff);
336 return;
337 }
338 offset >>= 2;
339 }
340 b43legacy_shm_control_word(dev, routing, offset);
Larry Finger75388ac2007-09-25 16:46:54 -0700341 b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA, value);
342}
343
344void b43legacy_shm_write16(struct b43legacy_wldev *dev, u16 routing, u16 offset,
345 u16 value)
346{
347 if (routing == B43legacy_SHM_SHARED) {
348 B43legacy_WARN_ON((offset & 0x0001) != 0);
349 if (offset & 0x0003) {
350 /* Unaligned access */
351 b43legacy_shm_control_word(dev, routing, offset >> 2);
Larry Finger75388ac2007-09-25 16:46:54 -0700352 b43legacy_write16(dev,
353 B43legacy_MMIO_SHM_DATA_UNALIGNED,
354 value);
355 return;
356 }
357 offset >>= 2;
358 }
359 b43legacy_shm_control_word(dev, routing, offset);
Larry Finger75388ac2007-09-25 16:46:54 -0700360 b43legacy_write16(dev, B43legacy_MMIO_SHM_DATA, value);
361}
362
363/* Read HostFlags */
364u32 b43legacy_hf_read(struct b43legacy_wldev *dev)
365{
366 u32 ret;
367
368 ret = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
369 B43legacy_SHM_SH_HOSTFHI);
370 ret <<= 16;
371 ret |= b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
372 B43legacy_SHM_SH_HOSTFLO);
373
374 return ret;
375}
376
377/* Write HostFlags */
378void b43legacy_hf_write(struct b43legacy_wldev *dev, u32 value)
379{
380 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
381 B43legacy_SHM_SH_HOSTFLO,
382 (value & 0x0000FFFF));
383 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
384 B43legacy_SHM_SH_HOSTFHI,
385 ((value & 0xFFFF0000) >> 16));
386}
387
388void b43legacy_tsf_read(struct b43legacy_wldev *dev, u64 *tsf)
389{
390 /* We need to be careful. As we read the TSF from multiple
391 * registers, we should take care of register overflows.
392 * In theory, the whole tsf read process should be atomic.
393 * We try to be atomic here, by restaring the read process,
394 * if any of the high registers changed (overflew).
395 */
396 if (dev->dev->id.revision >= 3) {
397 u32 low;
398 u32 high;
399 u32 high2;
400
401 do {
402 high = b43legacy_read32(dev,
403 B43legacy_MMIO_REV3PLUS_TSF_HIGH);
404 low = b43legacy_read32(dev,
405 B43legacy_MMIO_REV3PLUS_TSF_LOW);
406 high2 = b43legacy_read32(dev,
407 B43legacy_MMIO_REV3PLUS_TSF_HIGH);
408 } while (unlikely(high != high2));
409
410 *tsf = high;
411 *tsf <<= 32;
412 *tsf |= low;
413 } else {
414 u64 tmp;
415 u16 v0;
416 u16 v1;
417 u16 v2;
418 u16 v3;
419 u16 test1;
420 u16 test2;
421 u16 test3;
422
423 do {
424 v3 = b43legacy_read16(dev, B43legacy_MMIO_TSF_3);
425 v2 = b43legacy_read16(dev, B43legacy_MMIO_TSF_2);
426 v1 = b43legacy_read16(dev, B43legacy_MMIO_TSF_1);
427 v0 = b43legacy_read16(dev, B43legacy_MMIO_TSF_0);
428
429 test3 = b43legacy_read16(dev, B43legacy_MMIO_TSF_3);
430 test2 = b43legacy_read16(dev, B43legacy_MMIO_TSF_2);
431 test1 = b43legacy_read16(dev, B43legacy_MMIO_TSF_1);
432 } while (v3 != test3 || v2 != test2 || v1 != test1);
433
434 *tsf = v3;
435 *tsf <<= 48;
436 tmp = v2;
437 tmp <<= 32;
438 *tsf |= tmp;
439 tmp = v1;
440 tmp <<= 16;
441 *tsf |= tmp;
442 *tsf |= v0;
443 }
444}
445
446static void b43legacy_time_lock(struct b43legacy_wldev *dev)
447{
448 u32 status;
449
Stefano Brivioe78c9d22008-01-23 14:48:50 +0100450 status = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
451 status |= B43legacy_MACCTL_TBTTHOLD;
452 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, status);
Larry Finger75388ac2007-09-25 16:46:54 -0700453}
454
455static void b43legacy_time_unlock(struct b43legacy_wldev *dev)
456{
457 u32 status;
458
Stefano Brivioe78c9d22008-01-23 14:48:50 +0100459 status = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
460 status &= ~B43legacy_MACCTL_TBTTHOLD;
461 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, status);
Larry Finger75388ac2007-09-25 16:46:54 -0700462}
463
464static void b43legacy_tsf_write_locked(struct b43legacy_wldev *dev, u64 tsf)
465{
466 /* Be careful with the in-progress timer.
467 * First zero out the low register, so we have a full
468 * register-overflow duration to complete the operation.
469 */
470 if (dev->dev->id.revision >= 3) {
471 u32 lo = (tsf & 0x00000000FFFFFFFFULL);
472 u32 hi = (tsf & 0xFFFFFFFF00000000ULL) >> 32;
473
474 b43legacy_write32(dev, B43legacy_MMIO_REV3PLUS_TSF_LOW, 0);
Larry Finger75388ac2007-09-25 16:46:54 -0700475 b43legacy_write32(dev, B43legacy_MMIO_REV3PLUS_TSF_HIGH,
476 hi);
Larry Finger75388ac2007-09-25 16:46:54 -0700477 b43legacy_write32(dev, B43legacy_MMIO_REV3PLUS_TSF_LOW,
478 lo);
479 } else {
480 u16 v0 = (tsf & 0x000000000000FFFFULL);
481 u16 v1 = (tsf & 0x00000000FFFF0000ULL) >> 16;
482 u16 v2 = (tsf & 0x0000FFFF00000000ULL) >> 32;
483 u16 v3 = (tsf & 0xFFFF000000000000ULL) >> 48;
484
485 b43legacy_write16(dev, B43legacy_MMIO_TSF_0, 0);
Larry Finger75388ac2007-09-25 16:46:54 -0700486 b43legacy_write16(dev, B43legacy_MMIO_TSF_3, v3);
Larry Finger75388ac2007-09-25 16:46:54 -0700487 b43legacy_write16(dev, B43legacy_MMIO_TSF_2, v2);
Larry Finger75388ac2007-09-25 16:46:54 -0700488 b43legacy_write16(dev, B43legacy_MMIO_TSF_1, v1);
Larry Finger75388ac2007-09-25 16:46:54 -0700489 b43legacy_write16(dev, B43legacy_MMIO_TSF_0, v0);
490 }
491}
492
493void b43legacy_tsf_write(struct b43legacy_wldev *dev, u64 tsf)
494{
495 b43legacy_time_lock(dev);
496 b43legacy_tsf_write_locked(dev, tsf);
497 b43legacy_time_unlock(dev);
498}
499
500static
501void b43legacy_macfilter_set(struct b43legacy_wldev *dev,
502 u16 offset, const u8 *mac)
503{
504 static const u8 zero_addr[ETH_ALEN] = { 0 };
505 u16 data;
506
507 if (!mac)
508 mac = zero_addr;
509
510 offset |= 0x0020;
511 b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_CONTROL, offset);
512
513 data = mac[0];
514 data |= mac[1] << 8;
515 b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_DATA, data);
516 data = mac[2];
517 data |= mac[3] << 8;
518 b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_DATA, data);
519 data = mac[4];
520 data |= mac[5] << 8;
521 b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_DATA, data);
522}
523
524static void b43legacy_write_mac_bssid_templates(struct b43legacy_wldev *dev)
525{
526 static const u8 zero_addr[ETH_ALEN] = { 0 };
527 const u8 *mac = dev->wl->mac_addr;
528 const u8 *bssid = dev->wl->bssid;
529 u8 mac_bssid[ETH_ALEN * 2];
530 int i;
531 u32 tmp;
532
533 if (!bssid)
534 bssid = zero_addr;
535 if (!mac)
536 mac = zero_addr;
537
538 b43legacy_macfilter_set(dev, B43legacy_MACFILTER_BSSID, bssid);
539
540 memcpy(mac_bssid, mac, ETH_ALEN);
541 memcpy(mac_bssid + ETH_ALEN, bssid, ETH_ALEN);
542
543 /* Write our MAC address and BSSID to template ram */
544 for (i = 0; i < ARRAY_SIZE(mac_bssid); i += sizeof(u32)) {
545 tmp = (u32)(mac_bssid[i + 0]);
546 tmp |= (u32)(mac_bssid[i + 1]) << 8;
547 tmp |= (u32)(mac_bssid[i + 2]) << 16;
548 tmp |= (u32)(mac_bssid[i + 3]) << 24;
549 b43legacy_ram_write(dev, 0x20 + i, tmp);
550 b43legacy_ram_write(dev, 0x78 + i, tmp);
551 b43legacy_ram_write(dev, 0x478 + i, tmp);
552 }
553}
554
Johannes Berg4150c572007-09-17 01:29:23 -0400555static void b43legacy_upload_card_macaddress(struct b43legacy_wldev *dev)
Larry Finger75388ac2007-09-25 16:46:54 -0700556{
Larry Finger75388ac2007-09-25 16:46:54 -0700557 b43legacy_write_mac_bssid_templates(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400558 b43legacy_macfilter_set(dev, B43legacy_MACFILTER_SELF,
559 dev->wl->mac_addr);
Larry Finger75388ac2007-09-25 16:46:54 -0700560}
561
562static void b43legacy_set_slot_time(struct b43legacy_wldev *dev,
563 u16 slot_time)
564{
565 /* slot_time is in usec. */
566 if (dev->phy.type != B43legacy_PHYTYPE_G)
567 return;
568 b43legacy_write16(dev, 0x684, 510 + slot_time);
569 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x0010,
570 slot_time);
571}
572
573static void b43legacy_short_slot_timing_enable(struct b43legacy_wldev *dev)
574{
575 b43legacy_set_slot_time(dev, 9);
Larry Finger75388ac2007-09-25 16:46:54 -0700576}
577
578static void b43legacy_short_slot_timing_disable(struct b43legacy_wldev *dev)
579{
580 b43legacy_set_slot_time(dev, 20);
Larry Finger75388ac2007-09-25 16:46:54 -0700581}
582
Larry Finger75388ac2007-09-25 16:46:54 -0700583/* Synchronize IRQ top- and bottom-half.
584 * IRQs must be masked before calling this.
585 * This must not be called with the irq_lock held.
586 */
587static void b43legacy_synchronize_irq(struct b43legacy_wldev *dev)
588{
589 synchronize_irq(dev->dev->irq);
590 tasklet_kill(&dev->isr_tasklet);
591}
592
593/* DummyTransmission function, as documented on
Alexander A. Klimov140c6022020-07-19 13:11:24 +0200594 * https://bcm-specs.sipsolutions.net/DummyTransmission
Larry Finger75388ac2007-09-25 16:46:54 -0700595 */
596void b43legacy_dummy_transmission(struct b43legacy_wldev *dev)
597{
598 struct b43legacy_phy *phy = &dev->phy;
599 unsigned int i;
600 unsigned int max_loop;
601 u16 value;
602 u32 buffer[5] = {
603 0x00000000,
604 0x00D40000,
605 0x00000000,
606 0x01000000,
607 0x00000000,
608 };
609
610 switch (phy->type) {
611 case B43legacy_PHYTYPE_B:
612 case B43legacy_PHYTYPE_G:
613 max_loop = 0xFA;
614 buffer[0] = 0x000B846E;
615 break;
616 default:
617 B43legacy_BUG_ON(1);
618 return;
619 }
620
621 for (i = 0; i < 5; i++)
622 b43legacy_ram_write(dev, i * 4, buffer[i]);
623
624 /* dummy read follows */
Stefano Brivioe78c9d22008-01-23 14:48:50 +0100625 b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
Larry Finger75388ac2007-09-25 16:46:54 -0700626
627 b43legacy_write16(dev, 0x0568, 0x0000);
628 b43legacy_write16(dev, 0x07C0, 0x0000);
629 b43legacy_write16(dev, 0x050C, 0x0000);
630 b43legacy_write16(dev, 0x0508, 0x0000);
631 b43legacy_write16(dev, 0x050A, 0x0000);
632 b43legacy_write16(dev, 0x054C, 0x0000);
633 b43legacy_write16(dev, 0x056A, 0x0014);
634 b43legacy_write16(dev, 0x0568, 0x0826);
635 b43legacy_write16(dev, 0x0500, 0x0000);
636 b43legacy_write16(dev, 0x0502, 0x0030);
637
638 if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
639 b43legacy_radio_write16(dev, 0x0051, 0x0017);
640 for (i = 0x00; i < max_loop; i++) {
641 value = b43legacy_read16(dev, 0x050E);
642 if (value & 0x0080)
643 break;
644 udelay(10);
645 }
646 for (i = 0x00; i < 0x0A; i++) {
647 value = b43legacy_read16(dev, 0x050E);
648 if (value & 0x0400)
649 break;
650 udelay(10);
651 }
652 for (i = 0x00; i < 0x0A; i++) {
653 value = b43legacy_read16(dev, 0x0690);
654 if (!(value & 0x0100))
655 break;
656 udelay(10);
657 }
658 if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
659 b43legacy_radio_write16(dev, 0x0051, 0x0037);
660}
661
662/* Turn the Analog ON/OFF */
663static void b43legacy_switch_analog(struct b43legacy_wldev *dev, int on)
664{
665 b43legacy_write16(dev, B43legacy_MMIO_PHY0, on ? 0 : 0xF4);
666}
667
668void b43legacy_wireless_core_reset(struct b43legacy_wldev *dev, u32 flags)
669{
670 u32 tmslow;
671 u32 macctl;
672
673 flags |= B43legacy_TMSLOW_PHYCLKEN;
674 flags |= B43legacy_TMSLOW_PHYRESET;
675 ssb_device_enable(dev->dev, flags);
676 msleep(2); /* Wait for the PLL to turn on. */
677
678 /* Now take the PHY out of Reset again */
679 tmslow = ssb_read32(dev->dev, SSB_TMSLOW);
680 tmslow |= SSB_TMSLOW_FGC;
681 tmslow &= ~B43legacy_TMSLOW_PHYRESET;
682 ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
683 ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
684 msleep(1);
685 tmslow &= ~SSB_TMSLOW_FGC;
686 ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
687 ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
688 msleep(1);
689
690 /* Turn Analog ON */
691 b43legacy_switch_analog(dev, 1);
692
693 macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
694 macctl &= ~B43legacy_MACCTL_GMODE;
695 if (flags & B43legacy_TMSLOW_GMODE) {
696 macctl |= B43legacy_MACCTL_GMODE;
Rusty Russell3db1cd52011-12-19 13:56:45 +0000697 dev->phy.gmode = true;
Larry Finger75388ac2007-09-25 16:46:54 -0700698 } else
Rusty Russell3db1cd52011-12-19 13:56:45 +0000699 dev->phy.gmode = false;
Larry Finger75388ac2007-09-25 16:46:54 -0700700 macctl |= B43legacy_MACCTL_IHR_ENABLED;
701 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
702}
703
704static void handle_irq_transmit_status(struct b43legacy_wldev *dev)
705{
706 u32 v0;
707 u32 v1;
708 u16 tmp;
709 struct b43legacy_txstatus stat;
710
711 while (1) {
712 v0 = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_0);
713 if (!(v0 & 0x00000001))
714 break;
715 v1 = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_1);
716
717 stat.cookie = (v0 >> 16);
718 stat.seq = (v1 & 0x0000FFFF);
719 stat.phy_stat = ((v1 & 0x00FF0000) >> 16);
720 tmp = (v0 & 0x0000FFFF);
721 stat.frame_count = ((tmp & 0xF000) >> 12);
722 stat.rts_count = ((tmp & 0x0F00) >> 8);
723 stat.supp_reason = ((tmp & 0x001C) >> 2);
724 stat.pm_indicated = !!(tmp & 0x0080);
725 stat.intermediate = !!(tmp & 0x0040);
726 stat.for_ampdu = !!(tmp & 0x0020);
727 stat.acked = !!(tmp & 0x0002);
728
729 b43legacy_handle_txstatus(dev, &stat);
730 }
731}
732
733static void drain_txstatus_queue(struct b43legacy_wldev *dev)
734{
735 u32 dummy;
736
737 if (dev->dev->id.revision < 5)
738 return;
739 /* Read all entries from the microcode TXstatus FIFO
740 * and throw them away.
741 */
742 while (1) {
743 dummy = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_0);
744 if (!(dummy & 0x00000001))
745 break;
746 dummy = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_1);
747 }
748}
749
750static u32 b43legacy_jssi_read(struct b43legacy_wldev *dev)
751{
752 u32 val = 0;
753
754 val = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED, 0x40A);
755 val <<= 16;
756 val |= b43legacy_shm_read16(dev, B43legacy_SHM_SHARED, 0x408);
757
758 return val;
759}
760
761static void b43legacy_jssi_write(struct b43legacy_wldev *dev, u32 jssi)
762{
763 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x408,
764 (jssi & 0x0000FFFF));
765 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x40A,
766 (jssi & 0xFFFF0000) >> 16);
767}
768
769static void b43legacy_generate_noise_sample(struct b43legacy_wldev *dev)
770{
771 b43legacy_jssi_write(dev, 0x7F7F7F7F);
Stefano Brivioe78c9d22008-01-23 14:48:50 +0100772 b43legacy_write32(dev, B43legacy_MMIO_MACCMD,
Stefano Brivioeed0fd22008-02-08 06:31:10 +0100773 b43legacy_read32(dev, B43legacy_MMIO_MACCMD)
774 | B43legacy_MACCMD_BGNOISE);
Larry Finger75388ac2007-09-25 16:46:54 -0700775 B43legacy_WARN_ON(dev->noisecalc.channel_at_start !=
776 dev->phy.channel);
777}
778
779static void b43legacy_calculate_link_quality(struct b43legacy_wldev *dev)
780{
781 /* Top half of Link Quality calculation. */
782
783 if (dev->noisecalc.calculation_running)
784 return;
785 dev->noisecalc.channel_at_start = dev->phy.channel;
Rusty Russell3db1cd52011-12-19 13:56:45 +0000786 dev->noisecalc.calculation_running = true;
Larry Finger75388ac2007-09-25 16:46:54 -0700787 dev->noisecalc.nr_samples = 0;
788
789 b43legacy_generate_noise_sample(dev);
790}
791
792static void handle_irq_noise(struct b43legacy_wldev *dev)
793{
794 struct b43legacy_phy *phy = &dev->phy;
795 u16 tmp;
796 u8 noise[4];
797 u8 i;
798 u8 j;
799 s32 average;
800
801 /* Bottom half of Link Quality calculation. */
802
803 B43legacy_WARN_ON(!dev->noisecalc.calculation_running);
804 if (dev->noisecalc.channel_at_start != phy->channel)
805 goto drop_calculation;
806 *((__le32 *)noise) = cpu_to_le32(b43legacy_jssi_read(dev));
807 if (noise[0] == 0x7F || noise[1] == 0x7F ||
808 noise[2] == 0x7F || noise[3] == 0x7F)
809 goto generate_new;
810
811 /* Get the noise samples. */
812 B43legacy_WARN_ON(dev->noisecalc.nr_samples >= 8);
813 i = dev->noisecalc.nr_samples;
Harvey Harrisonca216142008-05-02 13:47:49 -0700814 noise[0] = clamp_val(noise[0], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
815 noise[1] = clamp_val(noise[1], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
816 noise[2] = clamp_val(noise[2], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
817 noise[3] = clamp_val(noise[3], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
Larry Finger75388ac2007-09-25 16:46:54 -0700818 dev->noisecalc.samples[i][0] = phy->nrssi_lt[noise[0]];
819 dev->noisecalc.samples[i][1] = phy->nrssi_lt[noise[1]];
820 dev->noisecalc.samples[i][2] = phy->nrssi_lt[noise[2]];
821 dev->noisecalc.samples[i][3] = phy->nrssi_lt[noise[3]];
822 dev->noisecalc.nr_samples++;
823 if (dev->noisecalc.nr_samples == 8) {
824 /* Calculate the Link Quality by the noise samples. */
825 average = 0;
826 for (i = 0; i < 8; i++) {
827 for (j = 0; j < 4; j++)
828 average += dev->noisecalc.samples[i][j];
829 }
830 average /= (8 * 4);
831 average *= 125;
832 average += 64;
833 average /= 128;
834 tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
835 0x40C);
836 tmp = (tmp / 128) & 0x1F;
837 if (tmp >= 8)
838 average += 2;
839 else
840 average -= 25;
841 if (tmp == 8)
842 average -= 72;
843 else
844 average -= 48;
845
846 dev->stats.link_noise = average;
847drop_calculation:
Rusty Russell3db1cd52011-12-19 13:56:45 +0000848 dev->noisecalc.calculation_running = false;
Larry Finger75388ac2007-09-25 16:46:54 -0700849 return;
850 }
851generate_new:
852 b43legacy_generate_noise_sample(dev);
853}
854
855static void handle_irq_tbtt_indication(struct b43legacy_wldev *dev)
856{
Johannes Berg05c914f2008-09-11 00:01:58 +0200857 if (b43legacy_is_mode(dev->wl, NL80211_IFTYPE_AP)) {
Larry Finger75388ac2007-09-25 16:46:54 -0700858 /* TODO: PS TBTT */
859 } else {
860 if (1/*FIXME: the last PSpoll frame was sent successfully */)
861 b43legacy_power_saving_ctl_bits(dev, -1, -1);
862 }
Johannes Berg05c914f2008-09-11 00:01:58 +0200863 if (b43legacy_is_mode(dev->wl, NL80211_IFTYPE_ADHOC))
Rusty Russell3db1cd52011-12-19 13:56:45 +0000864 dev->dfq_valid = true;
Larry Finger75388ac2007-09-25 16:46:54 -0700865}
866
867static void handle_irq_atim_end(struct b43legacy_wldev *dev)
868{
Stefano Brivioeed0fd22008-02-08 06:31:10 +0100869 if (dev->dfq_valid) {
870 b43legacy_write32(dev, B43legacy_MMIO_MACCMD,
871 b43legacy_read32(dev, B43legacy_MMIO_MACCMD)
872 | B43legacy_MACCMD_DFQ_VALID);
Rusty Russell3db1cd52011-12-19 13:56:45 +0000873 dev->dfq_valid = false;
Stefano Brivioeed0fd22008-02-08 06:31:10 +0100874 }
Larry Finger75388ac2007-09-25 16:46:54 -0700875}
876
877static void handle_irq_pmq(struct b43legacy_wldev *dev)
878{
879 u32 tmp;
880
881 /* TODO: AP mode. */
882
883 while (1) {
884 tmp = b43legacy_read32(dev, B43legacy_MMIO_PS_STATUS);
885 if (!(tmp & 0x00000008))
886 break;
887 }
888 /* 16bit write is odd, but correct. */
889 b43legacy_write16(dev, B43legacy_MMIO_PS_STATUS, 0x0002);
890}
891
892static void b43legacy_write_template_common(struct b43legacy_wldev *dev,
893 const u8 *data, u16 size,
894 u16 ram_offset,
895 u16 shm_size_offset, u8 rate)
896{
897 u32 i;
898 u32 tmp;
899 struct b43legacy_plcp_hdr4 plcp;
900
901 plcp.data = 0;
902 b43legacy_generate_plcp_hdr(&plcp, size + FCS_LEN, rate);
903 b43legacy_ram_write(dev, ram_offset, le32_to_cpu(plcp.data));
904 ram_offset += sizeof(u32);
905 /* The PLCP is 6 bytes long, but we only wrote 4 bytes, yet.
906 * So leave the first two bytes of the next write blank.
907 */
908 tmp = (u32)(data[0]) << 16;
909 tmp |= (u32)(data[1]) << 24;
910 b43legacy_ram_write(dev, ram_offset, tmp);
911 ram_offset += sizeof(u32);
912 for (i = 2; i < size; i += sizeof(u32)) {
913 tmp = (u32)(data[i + 0]);
914 if (i + 1 < size)
915 tmp |= (u32)(data[i + 1]) << 8;
916 if (i + 2 < size)
917 tmp |= (u32)(data[i + 2]) << 16;
918 if (i + 3 < size)
919 tmp |= (u32)(data[i + 3]) << 24;
920 b43legacy_ram_write(dev, ram_offset + i - 2, tmp);
921 }
922 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_size_offset,
923 size + sizeof(struct b43legacy_plcp_hdr6));
924}
925
Larry Finger2d1f96d2009-04-11 11:26:01 -0500926/* Convert a b43legacy antenna number value to the PHY TX control value. */
927static u16 b43legacy_antenna_to_phyctl(int antenna)
928{
929 switch (antenna) {
930 case B43legacy_ANTENNA0:
931 return B43legacy_TX4_PHY_ANT0;
932 case B43legacy_ANTENNA1:
933 return B43legacy_TX4_PHY_ANT1;
934 }
935 return B43legacy_TX4_PHY_ANTLAST;
936}
937
Larry Finger75388ac2007-09-25 16:46:54 -0700938static void b43legacy_write_beacon_template(struct b43legacy_wldev *dev,
939 u16 ram_offset,
Larry Finger2d1f96d2009-04-11 11:26:01 -0500940 u16 shm_size_offset)
Larry Finger75388ac2007-09-25 16:46:54 -0700941{
Larry Finger75388ac2007-09-25 16:46:54 -0700942
Stefano Brivioa2971702008-02-08 06:31:25 +0100943 unsigned int i, len, variable_len;
944 const struct ieee80211_mgmt *bcn;
945 const u8 *ie;
Rusty Russell3db1cd52011-12-19 13:56:45 +0000946 bool tim_found = false;
Larry Finger2d1f96d2009-04-11 11:26:01 -0500947 unsigned int rate;
948 u16 ctl;
949 int antenna;
950 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(dev->wl->current_beacon);
Stefano Brivioa2971702008-02-08 06:31:25 +0100951
952 bcn = (const struct ieee80211_mgmt *)(dev->wl->current_beacon->data);
Silvan Jegenc8e49552014-02-25 18:12:52 +0100953 len = min_t(size_t, dev->wl->current_beacon->len,
Larry Finger75388ac2007-09-25 16:46:54 -0700954 0x200 - sizeof(struct b43legacy_plcp_hdr6));
Larry Finger2d1f96d2009-04-11 11:26:01 -0500955 rate = ieee80211_get_tx_rate(dev->wl->hw, info)->hw_value;
Stefano Brivioa2971702008-02-08 06:31:25 +0100956
957 b43legacy_write_template_common(dev, (const u8 *)bcn, len, ram_offset,
Larry Finger75388ac2007-09-25 16:46:54 -0700958 shm_size_offset, rate);
Stefano Brivioa2971702008-02-08 06:31:25 +0100959
Larry Finger2d1f96d2009-04-11 11:26:01 -0500960 /* Write the PHY TX control parameters. */
961 antenna = B43legacy_ANTENNA_DEFAULT;
962 antenna = b43legacy_antenna_to_phyctl(antenna);
963 ctl = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
964 B43legacy_SHM_SH_BEACPHYCTL);
965 /* We can't send beacons with short preamble. Would get PHY errors. */
966 ctl &= ~B43legacy_TX4_PHY_SHORTPRMBL;
967 ctl &= ~B43legacy_TX4_PHY_ANT;
968 ctl &= ~B43legacy_TX4_PHY_ENC;
969 ctl |= antenna;
970 ctl |= B43legacy_TX4_PHY_ENC_CCK;
971 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
972 B43legacy_SHM_SH_BEACPHYCTL, ctl);
973
Stefano Brivioa2971702008-02-08 06:31:25 +0100974 /* Find the position of the TIM and the DTIM_period value
975 * and write them to SHM. */
976 ie = bcn->u.beacon.variable;
977 variable_len = len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
978 for (i = 0; i < variable_len - 2; ) {
979 uint8_t ie_id, ie_len;
980
981 ie_id = ie[i];
982 ie_len = ie[i + 1];
983 if (ie_id == 5) {
984 u16 tim_position;
985 u16 dtim_period;
986 /* This is the TIM Information Element */
987
988 /* Check whether the ie_len is in the beacon data range. */
989 if (variable_len < ie_len + 2 + i)
990 break;
991 /* A valid TIM is at least 4 bytes long. */
992 if (ie_len < 4)
993 break;
Rusty Russell3db1cd52011-12-19 13:56:45 +0000994 tim_found = true;
Stefano Brivioa2971702008-02-08 06:31:25 +0100995
996 tim_position = sizeof(struct b43legacy_plcp_hdr6);
997 tim_position += offsetof(struct ieee80211_mgmt,
998 u.beacon.variable);
999 tim_position += i;
1000
1001 dtim_period = ie[i + 3];
1002
1003 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
1004 B43legacy_SHM_SH_TIMPOS, tim_position);
1005 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
1006 B43legacy_SHM_SH_DTIMP, dtim_period);
1007 break;
1008 }
1009 i += ie_len + 2;
1010 }
1011 if (!tim_found) {
1012 b43legacywarn(dev->wl, "Did not find a valid TIM IE in the "
1013 "beacon template packet. AP or IBSS operation "
1014 "may be broken.\n");
Larry Finger7858e072009-04-11 11:25:24 -05001015 } else
1016 b43legacydbg(dev->wl, "Updated beacon template\n");
Larry Finger75388ac2007-09-25 16:46:54 -07001017}
1018
1019static void b43legacy_write_probe_resp_plcp(struct b43legacy_wldev *dev,
1020 u16 shm_offset, u16 size,
Johannes Berg8318d782008-01-24 19:38:38 +01001021 struct ieee80211_rate *rate)
Larry Finger75388ac2007-09-25 16:46:54 -07001022{
1023 struct b43legacy_plcp_hdr4 plcp;
1024 u32 tmp;
1025 __le16 dur;
1026
1027 plcp.data = 0;
Larry Finger2d1f96d2009-04-11 11:26:01 -05001028 b43legacy_generate_plcp_hdr(&plcp, size + FCS_LEN, rate->hw_value);
Larry Finger75388ac2007-09-25 16:46:54 -07001029 dur = ieee80211_generic_frame_duration(dev->wl->hw,
Johannes Berg32bfd352007-12-19 01:31:26 +01001030 dev->wl->vif,
Johannes Berg57fbcce2016-04-12 15:56:15 +02001031 NL80211_BAND_2GHZ,
Larry Finger75388ac2007-09-25 16:46:54 -07001032 size,
Johannes Berg8318d782008-01-24 19:38:38 +01001033 rate);
Larry Finger75388ac2007-09-25 16:46:54 -07001034 /* Write PLCP in two parts and timing for packet transfer */
1035 tmp = le32_to_cpu(plcp.data);
1036 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_offset,
1037 tmp & 0xFFFF);
1038 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_offset + 2,
1039 tmp >> 16);
1040 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_offset + 6,
1041 le16_to_cpu(dur));
1042}
1043
1044/* Instead of using custom probe response template, this function
1045 * just patches custom beacon template by:
1046 * 1) Changing packet type
1047 * 2) Patching duration field
1048 * 3) Stripping TIM
1049 */
Stefano Brivioa2971702008-02-08 06:31:25 +01001050static const u8 *b43legacy_generate_probe_resp(struct b43legacy_wldev *dev,
1051 u16 *dest_size,
1052 struct ieee80211_rate *rate)
Larry Finger75388ac2007-09-25 16:46:54 -07001053{
1054 const u8 *src_data;
1055 u8 *dest_data;
Stefano Brivioa2971702008-02-08 06:31:25 +01001056 u16 src_size, elem_size, src_pos, dest_pos;
Larry Finger75388ac2007-09-25 16:46:54 -07001057 __le16 dur;
1058 struct ieee80211_hdr *hdr;
Stefano Brivioa2971702008-02-08 06:31:25 +01001059 size_t ie_start;
Larry Finger75388ac2007-09-25 16:46:54 -07001060
Stefano Brivioa2971702008-02-08 06:31:25 +01001061 src_size = dev->wl->current_beacon->len;
1062 src_data = (const u8 *)dev->wl->current_beacon->data;
Larry Finger75388ac2007-09-25 16:46:54 -07001063
Stefano Brivioa2971702008-02-08 06:31:25 +01001064 /* Get the start offset of the variable IEs in the packet. */
1065 ie_start = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
1066 B43legacy_WARN_ON(ie_start != offsetof(struct ieee80211_mgmt,
1067 u.beacon.variable));
1068
Stefano Brivio4688be32008-02-08 06:31:39 +01001069 if (B43legacy_WARN_ON(src_size < ie_start))
Larry Finger75388ac2007-09-25 16:46:54 -07001070 return NULL;
Larry Finger75388ac2007-09-25 16:46:54 -07001071
1072 dest_data = kmalloc(src_size, GFP_ATOMIC);
1073 if (unlikely(!dest_data))
1074 return NULL;
1075
Stefano Brivioa2971702008-02-08 06:31:25 +01001076 /* Copy the static data and all Information Elements, except the TIM. */
1077 memcpy(dest_data, src_data, ie_start);
1078 src_pos = ie_start;
1079 dest_pos = ie_start;
1080 for ( ; src_pos < src_size - 2; src_pos += elem_size) {
Larry Finger75388ac2007-09-25 16:46:54 -07001081 elem_size = src_data[src_pos + 1] + 2;
Stefano Brivioa2971702008-02-08 06:31:25 +01001082 if (src_data[src_pos] == 5) {
1083 /* This is the TIM. */
1084 continue;
Larry Finger75388ac2007-09-25 16:46:54 -07001085 }
Stefano Brivioa2971702008-02-08 06:31:25 +01001086 memcpy(dest_data + dest_pos, src_data + src_pos, elem_size);
1087 dest_pos += elem_size;
Larry Finger75388ac2007-09-25 16:46:54 -07001088 }
1089 *dest_size = dest_pos;
1090 hdr = (struct ieee80211_hdr *)dest_data;
1091
1092 /* Set the frame control. */
1093 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1094 IEEE80211_STYPE_PROBE_RESP);
1095 dur = ieee80211_generic_frame_duration(dev->wl->hw,
Johannes Berg32bfd352007-12-19 01:31:26 +01001096 dev->wl->vif,
Johannes Berg57fbcce2016-04-12 15:56:15 +02001097 NL80211_BAND_2GHZ,
Larry Finger75388ac2007-09-25 16:46:54 -07001098 *dest_size,
Johannes Berg8318d782008-01-24 19:38:38 +01001099 rate);
Larry Finger75388ac2007-09-25 16:46:54 -07001100 hdr->duration_id = dur;
1101
1102 return dest_data;
1103}
1104
1105static void b43legacy_write_probe_resp_template(struct b43legacy_wldev *dev,
1106 u16 ram_offset,
Johannes Berg8318d782008-01-24 19:38:38 +01001107 u16 shm_size_offset,
1108 struct ieee80211_rate *rate)
Larry Finger75388ac2007-09-25 16:46:54 -07001109{
Stefano Brivioa2971702008-02-08 06:31:25 +01001110 const u8 *probe_resp_data;
Larry Finger75388ac2007-09-25 16:46:54 -07001111 u16 size;
1112
Stefano Brivioa2971702008-02-08 06:31:25 +01001113 size = dev->wl->current_beacon->len;
Larry Finger75388ac2007-09-25 16:46:54 -07001114 probe_resp_data = b43legacy_generate_probe_resp(dev, &size, rate);
1115 if (unlikely(!probe_resp_data))
1116 return;
1117
1118 /* Looks like PLCP headers plus packet timings are stored for
1119 * all possible basic rates
1120 */
1121 b43legacy_write_probe_resp_plcp(dev, 0x31A, size,
Johannes Berg8318d782008-01-24 19:38:38 +01001122 &b43legacy_b_ratetable[0]);
Larry Finger75388ac2007-09-25 16:46:54 -07001123 b43legacy_write_probe_resp_plcp(dev, 0x32C, size,
Johannes Berg8318d782008-01-24 19:38:38 +01001124 &b43legacy_b_ratetable[1]);
Larry Finger75388ac2007-09-25 16:46:54 -07001125 b43legacy_write_probe_resp_plcp(dev, 0x33E, size,
Johannes Berg8318d782008-01-24 19:38:38 +01001126 &b43legacy_b_ratetable[2]);
Larry Finger75388ac2007-09-25 16:46:54 -07001127 b43legacy_write_probe_resp_plcp(dev, 0x350, size,
Johannes Berg8318d782008-01-24 19:38:38 +01001128 &b43legacy_b_ratetable[3]);
Larry Finger75388ac2007-09-25 16:46:54 -07001129
Silvan Jegenc8e49552014-02-25 18:12:52 +01001130 size = min_t(size_t, size,
Larry Finger75388ac2007-09-25 16:46:54 -07001131 0x200 - sizeof(struct b43legacy_plcp_hdr6));
1132 b43legacy_write_template_common(dev, probe_resp_data,
1133 size, ram_offset,
Larry Finger2d1f96d2009-04-11 11:26:01 -05001134 shm_size_offset, rate->hw_value);
Larry Finger75388ac2007-09-25 16:46:54 -07001135 kfree(probe_resp_data);
1136}
1137
Larry Finger2d1f96d2009-04-11 11:26:01 -05001138static void b43legacy_upload_beacon0(struct b43legacy_wldev *dev)
1139{
1140 struct b43legacy_wl *wl = dev->wl;
1141
1142 if (wl->beacon0_uploaded)
1143 return;
1144 b43legacy_write_beacon_template(dev, 0x68, 0x18);
1145 /* FIXME: Probe resp upload doesn't really belong here,
1146 * but we don't use that feature anyway. */
1147 b43legacy_write_probe_resp_template(dev, 0x268, 0x4A,
1148 &__b43legacy_ratetable[3]);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001149 wl->beacon0_uploaded = true;
Larry Finger2d1f96d2009-04-11 11:26:01 -05001150}
1151
1152static void b43legacy_upload_beacon1(struct b43legacy_wldev *dev)
1153{
1154 struct b43legacy_wl *wl = dev->wl;
1155
1156 if (wl->beacon1_uploaded)
1157 return;
1158 b43legacy_write_beacon_template(dev, 0x468, 0x1A);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001159 wl->beacon1_uploaded = true;
Larry Finger2d1f96d2009-04-11 11:26:01 -05001160}
1161
1162static void handle_irq_beacon(struct b43legacy_wldev *dev)
1163{
1164 struct b43legacy_wl *wl = dev->wl;
1165 u32 cmd, beacon0_valid, beacon1_valid;
1166
1167 if (!b43legacy_is_mode(wl, NL80211_IFTYPE_AP))
1168 return;
1169
1170 /* This is the bottom half of the asynchronous beacon update. */
1171
1172 /* Ignore interrupt in the future. */
Stefano Brivio44710bb2009-05-15 15:39:20 -05001173 dev->irq_mask &= ~B43legacy_IRQ_BEACON;
Larry Finger2d1f96d2009-04-11 11:26:01 -05001174
1175 cmd = b43legacy_read32(dev, B43legacy_MMIO_MACCMD);
1176 beacon0_valid = (cmd & B43legacy_MACCMD_BEACON0_VALID);
1177 beacon1_valid = (cmd & B43legacy_MACCMD_BEACON1_VALID);
1178
1179 /* Schedule interrupt manually, if busy. */
1180 if (beacon0_valid && beacon1_valid) {
1181 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON, B43legacy_IRQ_BEACON);
Stefano Brivio44710bb2009-05-15 15:39:20 -05001182 dev->irq_mask |= B43legacy_IRQ_BEACON;
Larry Finger2d1f96d2009-04-11 11:26:01 -05001183 return;
1184 }
1185
1186 if (unlikely(wl->beacon_templates_virgin)) {
1187 /* We never uploaded a beacon before.
1188 * Upload both templates now, but only mark one valid. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00001189 wl->beacon_templates_virgin = false;
Larry Finger2d1f96d2009-04-11 11:26:01 -05001190 b43legacy_upload_beacon0(dev);
1191 b43legacy_upload_beacon1(dev);
1192 cmd = b43legacy_read32(dev, B43legacy_MMIO_MACCMD);
1193 cmd |= B43legacy_MACCMD_BEACON0_VALID;
1194 b43legacy_write32(dev, B43legacy_MMIO_MACCMD, cmd);
1195 } else {
1196 if (!beacon0_valid) {
1197 b43legacy_upload_beacon0(dev);
1198 cmd = b43legacy_read32(dev, B43legacy_MMIO_MACCMD);
1199 cmd |= B43legacy_MACCMD_BEACON0_VALID;
1200 b43legacy_write32(dev, B43legacy_MMIO_MACCMD, cmd);
1201 } else if (!beacon1_valid) {
1202 b43legacy_upload_beacon1(dev);
1203 cmd = b43legacy_read32(dev, B43legacy_MMIO_MACCMD);
1204 cmd |= B43legacy_MACCMD_BEACON1_VALID;
1205 b43legacy_write32(dev, B43legacy_MMIO_MACCMD, cmd);
1206 }
1207 }
1208}
1209
Larry Finger7858e072009-04-11 11:25:24 -05001210static void b43legacy_beacon_update_trigger_work(struct work_struct *work)
1211{
1212 struct b43legacy_wl *wl = container_of(work, struct b43legacy_wl,
1213 beacon_update_trigger);
1214 struct b43legacy_wldev *dev;
1215
1216 mutex_lock(&wl->mutex);
1217 dev = wl->current_dev;
1218 if (likely(dev && (b43legacy_status(dev) >= B43legacy_STAT_INITIALIZED))) {
Larry Finger7858e072009-04-11 11:25:24 -05001219 spin_lock_irq(&wl->irq_lock);
Stefano Brivio44710bb2009-05-15 15:39:20 -05001220 /* Update beacon right away or defer to IRQ. */
Larry Finger2d1f96d2009-04-11 11:26:01 -05001221 handle_irq_beacon(dev);
1222 /* The handler might have updated the IRQ mask. */
1223 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK,
Stefano Brivio44710bb2009-05-15 15:39:20 -05001224 dev->irq_mask);
Larry Finger7858e072009-04-11 11:25:24 -05001225 spin_unlock_irq(&wl->irq_lock);
1226 }
1227 mutex_unlock(&wl->mutex);
1228}
1229
Stefano Brivioa2971702008-02-08 06:31:25 +01001230/* Asynchronously update the packet templates in template RAM.
1231 * Locking: Requires wl->irq_lock to be locked. */
Johannes Berg9d139c82008-07-09 14:40:37 +02001232static void b43legacy_update_templates(struct b43legacy_wl *wl)
Larry Finger75388ac2007-09-25 16:46:54 -07001233{
Johannes Berg9d139c82008-07-09 14:40:37 +02001234 struct sk_buff *beacon;
Stefano Brivioa2971702008-02-08 06:31:25 +01001235 /* This is the top half of the ansynchronous beacon update. The bottom
1236 * half is the beacon IRQ. Beacon update must be asynchronous to avoid
1237 * sending an invalid beacon. This can happen for example, if the
1238 * firmware transmits a beacon while we are updating it. */
Larry Finger75388ac2007-09-25 16:46:54 -07001239
Johannes Berg9d139c82008-07-09 14:40:37 +02001240 /* We could modify the existing beacon and set the aid bit in the TIM
1241 * field, but that would probably require resizing and moving of data
1242 * within the beacon template. Simply request a new beacon and let
1243 * mac80211 do the hard work. */
1244 beacon = ieee80211_beacon_get(wl->hw, wl->vif);
1245 if (unlikely(!beacon))
1246 return;
1247
Stefano Brivioa2971702008-02-08 06:31:25 +01001248 if (wl->current_beacon)
1249 dev_kfree_skb_any(wl->current_beacon);
1250 wl->current_beacon = beacon;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001251 wl->beacon0_uploaded = false;
1252 wl->beacon1_uploaded = false;
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001253 ieee80211_queue_work(wl->hw, &wl->beacon_update_trigger);
Larry Finger75388ac2007-09-25 16:46:54 -07001254}
1255
Larry Finger75388ac2007-09-25 16:46:54 -07001256static void b43legacy_set_beacon_int(struct b43legacy_wldev *dev,
1257 u16 beacon_int)
1258{
1259 b43legacy_time_lock(dev);
Larry Finger7858e072009-04-11 11:25:24 -05001260 if (dev->dev->id.revision >= 3) {
1261 b43legacy_write32(dev, B43legacy_MMIO_TSF_CFP_REP,
1262 (beacon_int << 16));
1263 b43legacy_write32(dev, B43legacy_MMIO_TSF_CFP_START,
1264 (beacon_int << 10));
1265 } else {
Larry Finger75388ac2007-09-25 16:46:54 -07001266 b43legacy_write16(dev, 0x606, (beacon_int >> 6));
1267 b43legacy_write16(dev, 0x610, beacon_int);
1268 }
1269 b43legacy_time_unlock(dev);
Larry Finger7858e072009-04-11 11:25:24 -05001270 b43legacydbg(dev->wl, "Set beacon interval to %u\n", beacon_int);
Larry Finger75388ac2007-09-25 16:46:54 -07001271}
1272
Larry Finger75388ac2007-09-25 16:46:54 -07001273static void handle_irq_ucode_debug(struct b43legacy_wldev *dev)
1274{
1275}
1276
1277/* Interrupt handler bottom-half */
Phong Tran475eec12019-11-27 00:55:26 +07001278static void b43legacy_interrupt_tasklet(unsigned long data)
Larry Finger75388ac2007-09-25 16:46:54 -07001279{
Phong Tran475eec12019-11-27 00:55:26 +07001280 struct b43legacy_wldev *dev = (struct b43legacy_wldev *)data;
Larry Finger75388ac2007-09-25 16:46:54 -07001281 u32 reason;
1282 u32 dma_reason[ARRAY_SIZE(dev->dma_reason)];
1283 u32 merged_dma_reason = 0;
1284 int i;
Larry Finger75388ac2007-09-25 16:46:54 -07001285 unsigned long flags;
1286
1287 spin_lock_irqsave(&dev->wl->irq_lock, flags);
1288
1289 B43legacy_WARN_ON(b43legacy_status(dev) <
1290 B43legacy_STAT_INITIALIZED);
1291
1292 reason = dev->irq_reason;
1293 for (i = 0; i < ARRAY_SIZE(dma_reason); i++) {
1294 dma_reason[i] = dev->dma_reason[i];
1295 merged_dma_reason |= dma_reason[i];
1296 }
1297
1298 if (unlikely(reason & B43legacy_IRQ_MAC_TXERR))
1299 b43legacyerr(dev->wl, "MAC transmission error\n");
1300
Stefano Brivioa293ee92007-11-24 23:35:25 +01001301 if (unlikely(reason & B43legacy_IRQ_PHY_TXERR)) {
Larry Finger75388ac2007-09-25 16:46:54 -07001302 b43legacyerr(dev->wl, "PHY transmission error\n");
Stefano Brivioa293ee92007-11-24 23:35:25 +01001303 rmb();
1304 if (unlikely(atomic_dec_and_test(&dev->phy.txerr_cnt))) {
1305 b43legacyerr(dev->wl, "Too many PHY TX errors, "
1306 "restarting the controller\n");
1307 b43legacy_controller_restart(dev, "PHY TX errors");
1308 }
1309 }
Larry Finger75388ac2007-09-25 16:46:54 -07001310
1311 if (unlikely(merged_dma_reason & (B43legacy_DMAIRQ_FATALMASK |
1312 B43legacy_DMAIRQ_NONFATALMASK))) {
1313 if (merged_dma_reason & B43legacy_DMAIRQ_FATALMASK) {
1314 b43legacyerr(dev->wl, "Fatal DMA error: "
1315 "0x%08X, 0x%08X, 0x%08X, "
1316 "0x%08X, 0x%08X, 0x%08X\n",
1317 dma_reason[0], dma_reason[1],
1318 dma_reason[2], dma_reason[3],
1319 dma_reason[4], dma_reason[5]);
1320 b43legacy_controller_restart(dev, "DMA error");
Larry Finger75388ac2007-09-25 16:46:54 -07001321 spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
1322 return;
1323 }
1324 if (merged_dma_reason & B43legacy_DMAIRQ_NONFATALMASK)
1325 b43legacyerr(dev->wl, "DMA error: "
1326 "0x%08X, 0x%08X, 0x%08X, "
1327 "0x%08X, 0x%08X, 0x%08X\n",
1328 dma_reason[0], dma_reason[1],
1329 dma_reason[2], dma_reason[3],
1330 dma_reason[4], dma_reason[5]);
1331 }
1332
1333 if (unlikely(reason & B43legacy_IRQ_UCODE_DEBUG))
1334 handle_irq_ucode_debug(dev);
1335 if (reason & B43legacy_IRQ_TBTT_INDI)
1336 handle_irq_tbtt_indication(dev);
1337 if (reason & B43legacy_IRQ_ATIM_END)
1338 handle_irq_atim_end(dev);
1339 if (reason & B43legacy_IRQ_BEACON)
1340 handle_irq_beacon(dev);
1341 if (reason & B43legacy_IRQ_PMQ)
1342 handle_irq_pmq(dev);
1343 if (reason & B43legacy_IRQ_TXFIFO_FLUSH_OK)
1344 ;/*TODO*/
1345 if (reason & B43legacy_IRQ_NOISESAMPLE_OK)
1346 handle_irq_noise(dev);
1347
1348 /* Check the DMA reason registers for received data. */
1349 if (dma_reason[0] & B43legacy_DMAIRQ_RX_DONE) {
1350 if (b43legacy_using_pio(dev))
1351 b43legacy_pio_rx(dev->pio.queue0);
1352 else
1353 b43legacy_dma_rx(dev->dma.rx_ring0);
Larry Finger75388ac2007-09-25 16:46:54 -07001354 }
1355 B43legacy_WARN_ON(dma_reason[1] & B43legacy_DMAIRQ_RX_DONE);
1356 B43legacy_WARN_ON(dma_reason[2] & B43legacy_DMAIRQ_RX_DONE);
1357 if (dma_reason[3] & B43legacy_DMAIRQ_RX_DONE) {
1358 if (b43legacy_using_pio(dev))
1359 b43legacy_pio_rx(dev->pio.queue3);
1360 else
1361 b43legacy_dma_rx(dev->dma.rx_ring3);
Larry Finger75388ac2007-09-25 16:46:54 -07001362 }
1363 B43legacy_WARN_ON(dma_reason[4] & B43legacy_DMAIRQ_RX_DONE);
1364 B43legacy_WARN_ON(dma_reason[5] & B43legacy_DMAIRQ_RX_DONE);
1365
Larry Fingerba48f7b2007-10-12 23:04:51 -05001366 if (reason & B43legacy_IRQ_TX_OK)
Larry Finger75388ac2007-09-25 16:46:54 -07001367 handle_irq_transmit_status(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07001368
Stefano Brivio44710bb2009-05-15 15:39:20 -05001369 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, dev->irq_mask);
Larry Finger75388ac2007-09-25 16:46:54 -07001370 spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
1371}
1372
1373static void pio_irq_workaround(struct b43legacy_wldev *dev,
1374 u16 base, int queueidx)
1375{
1376 u16 rxctl;
1377
1378 rxctl = b43legacy_read16(dev, base + B43legacy_PIO_RXCTL);
1379 if (rxctl & B43legacy_PIO_RXCTL_DATAAVAILABLE)
1380 dev->dma_reason[queueidx] |= B43legacy_DMAIRQ_RX_DONE;
1381 else
1382 dev->dma_reason[queueidx] &= ~B43legacy_DMAIRQ_RX_DONE;
1383}
1384
1385static void b43legacy_interrupt_ack(struct b43legacy_wldev *dev, u32 reason)
1386{
1387 if (b43legacy_using_pio(dev) &&
1388 (dev->dev->id.revision < 3) &&
1389 (!(reason & B43legacy_IRQ_PIO_WORKAROUND))) {
1390 /* Apply a PIO specific workaround to the dma_reasons */
1391 pio_irq_workaround(dev, B43legacy_MMIO_PIO1_BASE, 0);
1392 pio_irq_workaround(dev, B43legacy_MMIO_PIO2_BASE, 1);
1393 pio_irq_workaround(dev, B43legacy_MMIO_PIO3_BASE, 2);
1394 pio_irq_workaround(dev, B43legacy_MMIO_PIO4_BASE, 3);
1395 }
1396
1397 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON, reason);
1398
1399 b43legacy_write32(dev, B43legacy_MMIO_DMA0_REASON,
1400 dev->dma_reason[0]);
1401 b43legacy_write32(dev, B43legacy_MMIO_DMA1_REASON,
1402 dev->dma_reason[1]);
1403 b43legacy_write32(dev, B43legacy_MMIO_DMA2_REASON,
1404 dev->dma_reason[2]);
1405 b43legacy_write32(dev, B43legacy_MMIO_DMA3_REASON,
1406 dev->dma_reason[3]);
1407 b43legacy_write32(dev, B43legacy_MMIO_DMA4_REASON,
1408 dev->dma_reason[4]);
1409 b43legacy_write32(dev, B43legacy_MMIO_DMA5_REASON,
1410 dev->dma_reason[5]);
1411}
1412
1413/* Interrupt handler top-half */
1414static irqreturn_t b43legacy_interrupt_handler(int irq, void *dev_id)
1415{
1416 irqreturn_t ret = IRQ_NONE;
1417 struct b43legacy_wldev *dev = dev_id;
1418 u32 reason;
1419
Stefano Brivio44710bb2009-05-15 15:39:20 -05001420 B43legacy_WARN_ON(!dev);
Larry Finger75388ac2007-09-25 16:46:54 -07001421
1422 spin_lock(&dev->wl->irq_lock);
1423
Stefano Brivio44710bb2009-05-15 15:39:20 -05001424 if (unlikely(b43legacy_status(dev) < B43legacy_STAT_STARTED))
1425 /* This can only happen on shared IRQ lines. */
Larry Finger75388ac2007-09-25 16:46:54 -07001426 goto out;
1427 reason = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
1428 if (reason == 0xffffffff) /* shared IRQ */
1429 goto out;
1430 ret = IRQ_HANDLED;
Stefano Brivio44710bb2009-05-15 15:39:20 -05001431 reason &= dev->irq_mask;
Larry Finger75388ac2007-09-25 16:46:54 -07001432 if (!reason)
1433 goto out;
1434
1435 dev->dma_reason[0] = b43legacy_read32(dev,
1436 B43legacy_MMIO_DMA0_REASON)
1437 & 0x0001DC00;
1438 dev->dma_reason[1] = b43legacy_read32(dev,
1439 B43legacy_MMIO_DMA1_REASON)
1440 & 0x0000DC00;
1441 dev->dma_reason[2] = b43legacy_read32(dev,
1442 B43legacy_MMIO_DMA2_REASON)
1443 & 0x0000DC00;
1444 dev->dma_reason[3] = b43legacy_read32(dev,
1445 B43legacy_MMIO_DMA3_REASON)
1446 & 0x0001DC00;
1447 dev->dma_reason[4] = b43legacy_read32(dev,
1448 B43legacy_MMIO_DMA4_REASON)
1449 & 0x0000DC00;
1450 dev->dma_reason[5] = b43legacy_read32(dev,
1451 B43legacy_MMIO_DMA5_REASON)
1452 & 0x0000DC00;
1453
1454 b43legacy_interrupt_ack(dev, reason);
Stefano Brivio44710bb2009-05-15 15:39:20 -05001455 /* Disable all IRQs. They are enabled again in the bottom half. */
1456 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, 0);
1457 /* Save the reason code and call our bottom half. */
Larry Finger75388ac2007-09-25 16:46:54 -07001458 dev->irq_reason = reason;
1459 tasklet_schedule(&dev->isr_tasklet);
1460out:
Larry Finger75388ac2007-09-25 16:46:54 -07001461 spin_unlock(&dev->wl->irq_lock);
1462
1463 return ret;
1464}
1465
1466static void b43legacy_release_firmware(struct b43legacy_wldev *dev)
1467{
1468 release_firmware(dev->fw.ucode);
1469 dev->fw.ucode = NULL;
1470 release_firmware(dev->fw.pcm);
1471 dev->fw.pcm = NULL;
1472 release_firmware(dev->fw.initvals);
1473 dev->fw.initvals = NULL;
1474 release_firmware(dev->fw.initvals_band);
1475 dev->fw.initvals_band = NULL;
1476}
1477
1478static void b43legacy_print_fw_helptext(struct b43legacy_wl *wl)
1479{
Flavio Suligoi8bd41472020-06-05 17:41:08 +02001480 b43legacyerr(wl, "You must go to https://wireless.wiki.kernel.org/en/"
1481 "users/Drivers/b43#devicefirmware "
Larry Finger75388ac2007-09-25 16:46:54 -07001482 "and download the correct firmware (version 3).\n");
1483}
1484
Larry Finger576d28a2012-12-06 21:55:16 -06001485static void b43legacy_fw_cb(const struct firmware *firmware, void *context)
1486{
1487 struct b43legacy_wldev *dev = context;
1488
1489 dev->fwp = firmware;
1490 complete(&dev->fw_load_complete);
1491}
1492
Larry Finger75388ac2007-09-25 16:46:54 -07001493static int do_request_fw(struct b43legacy_wldev *dev,
1494 const char *name,
Larry Finger576d28a2012-12-06 21:55:16 -06001495 const struct firmware **fw, bool async)
Larry Finger75388ac2007-09-25 16:46:54 -07001496{
1497 char path[sizeof(modparam_fwpostfix) + 32];
1498 struct b43legacy_fw_header *hdr;
1499 u32 size;
1500 int err;
1501
1502 if (!name)
1503 return 0;
1504
1505 snprintf(path, ARRAY_SIZE(path),
1506 "b43legacy%s/%s.fw",
1507 modparam_fwpostfix, name);
Larry Finger576d28a2012-12-06 21:55:16 -06001508 b43legacyinfo(dev->wl, "Loading firmware %s\n", path);
1509 if (async) {
1510 init_completion(&dev->fw_load_complete);
1511 err = request_firmware_nowait(THIS_MODULE, 1, path,
1512 dev->dev->dev, GFP_KERNEL,
1513 dev, b43legacy_fw_cb);
1514 if (err) {
1515 b43legacyerr(dev->wl, "Unable to load firmware\n");
1516 return err;
1517 }
1518 /* stall here until fw ready */
1519 wait_for_completion(&dev->fw_load_complete);
1520 if (!dev->fwp)
1521 err = -EINVAL;
1522 *fw = dev->fwp;
1523 } else {
1524 err = request_firmware(fw, path, dev->dev->dev);
1525 }
Larry Finger75388ac2007-09-25 16:46:54 -07001526 if (err) {
1527 b43legacyerr(dev->wl, "Firmware file \"%s\" not found "
1528 "or load failed.\n", path);
1529 return err;
1530 }
1531 if ((*fw)->size < sizeof(struct b43legacy_fw_header))
1532 goto err_format;
1533 hdr = (struct b43legacy_fw_header *)((*fw)->data);
1534 switch (hdr->type) {
1535 case B43legacy_FW_TYPE_UCODE:
1536 case B43legacy_FW_TYPE_PCM:
1537 size = be32_to_cpu(hdr->size);
1538 if (size != (*fw)->size - sizeof(struct b43legacy_fw_header))
1539 goto err_format;
1540 /* fallthrough */
1541 case B43legacy_FW_TYPE_IV:
1542 if (hdr->ver != 1)
1543 goto err_format;
1544 break;
1545 default:
1546 goto err_format;
1547 }
1548
1549 return err;
1550
1551err_format:
1552 b43legacyerr(dev->wl, "Firmware file \"%s\" format error.\n", path);
1553 return -EPROTO;
1554}
1555
Larry Fingera3ea2c72012-03-08 22:25:57 -06001556static int b43legacy_one_core_attach(struct ssb_device *dev,
1557 struct b43legacy_wl *wl);
1558static void b43legacy_one_core_detach(struct ssb_device *dev);
1559
1560static void b43legacy_request_firmware(struct work_struct *work)
Larry Finger75388ac2007-09-25 16:46:54 -07001561{
Larry Fingera3ea2c72012-03-08 22:25:57 -06001562 struct b43legacy_wl *wl = container_of(work,
1563 struct b43legacy_wl, firmware_load);
1564 struct b43legacy_wldev *dev = wl->current_dev;
Larry Finger75388ac2007-09-25 16:46:54 -07001565 struct b43legacy_firmware *fw = &dev->fw;
1566 const u8 rev = dev->dev->id.revision;
1567 const char *filename;
Larry Finger75388ac2007-09-25 16:46:54 -07001568 int err;
1569
Larry Finger75388ac2007-09-25 16:46:54 -07001570 if (!fw->ucode) {
1571 if (rev == 2)
1572 filename = "ucode2";
1573 else if (rev == 4)
1574 filename = "ucode4";
1575 else
1576 filename = "ucode5";
Larry Finger576d28a2012-12-06 21:55:16 -06001577 err = do_request_fw(dev, filename, &fw->ucode, true);
Larry Finger75388ac2007-09-25 16:46:54 -07001578 if (err)
1579 goto err_load;
1580 }
1581 if (!fw->pcm) {
1582 if (rev < 5)
1583 filename = "pcm4";
1584 else
1585 filename = "pcm5";
Larry Finger576d28a2012-12-06 21:55:16 -06001586 err = do_request_fw(dev, filename, &fw->pcm, false);
Larry Finger75388ac2007-09-25 16:46:54 -07001587 if (err)
1588 goto err_load;
1589 }
1590 if (!fw->initvals) {
1591 switch (dev->phy.type) {
Stefano Brivio385f8482008-04-06 17:10:53 +02001592 case B43legacy_PHYTYPE_B:
Larry Finger75388ac2007-09-25 16:46:54 -07001593 case B43legacy_PHYTYPE_G:
1594 if ((rev >= 5) && (rev <= 10))
1595 filename = "b0g0initvals5";
1596 else if (rev == 2 || rev == 4)
1597 filename = "b0g0initvals2";
1598 else
1599 goto err_no_initvals;
1600 break;
1601 default:
1602 goto err_no_initvals;
1603 }
Larry Finger576d28a2012-12-06 21:55:16 -06001604 err = do_request_fw(dev, filename, &fw->initvals, false);
Larry Finger75388ac2007-09-25 16:46:54 -07001605 if (err)
1606 goto err_load;
1607 }
1608 if (!fw->initvals_band) {
1609 switch (dev->phy.type) {
Stefano Brivio385f8482008-04-06 17:10:53 +02001610 case B43legacy_PHYTYPE_B:
Larry Finger75388ac2007-09-25 16:46:54 -07001611 case B43legacy_PHYTYPE_G:
1612 if ((rev >= 5) && (rev <= 10))
1613 filename = "b0g0bsinitvals5";
1614 else if (rev >= 11)
1615 filename = NULL;
1616 else if (rev == 2 || rev == 4)
1617 filename = NULL;
1618 else
1619 goto err_no_initvals;
1620 break;
1621 default:
1622 goto err_no_initvals;
1623 }
Larry Finger576d28a2012-12-06 21:55:16 -06001624 err = do_request_fw(dev, filename, &fw->initvals_band, false);
Larry Finger75388ac2007-09-25 16:46:54 -07001625 if (err)
1626 goto err_load;
1627 }
Larry Fingera3ea2c72012-03-08 22:25:57 -06001628 err = ieee80211_register_hw(wl->hw);
1629 if (err)
1630 goto err_one_core_detach;
1631 return;
Larry Finger75388ac2007-09-25 16:46:54 -07001632
Larry Fingera3ea2c72012-03-08 22:25:57 -06001633err_one_core_detach:
1634 b43legacy_one_core_detach(dev->dev);
1635 goto error;
Larry Finger75388ac2007-09-25 16:46:54 -07001636
1637err_load:
1638 b43legacy_print_fw_helptext(dev->wl);
1639 goto error;
1640
1641err_no_initvals:
1642 err = -ENODEV;
1643 b43legacyerr(dev->wl, "No Initial Values firmware file for PHY %u, "
1644 "core rev %u\n", dev->phy.type, rev);
1645 goto error;
1646
1647error:
1648 b43legacy_release_firmware(dev);
Larry Fingera3ea2c72012-03-08 22:25:57 -06001649 return;
Larry Finger75388ac2007-09-25 16:46:54 -07001650}
1651
1652static int b43legacy_upload_microcode(struct b43legacy_wldev *dev)
1653{
John W. Linvillebcf3c7c2010-07-29 13:30:15 -04001654 struct wiphy *wiphy = dev->wl->hw->wiphy;
Larry Finger75388ac2007-09-25 16:46:54 -07001655 const size_t hdr_len = sizeof(struct b43legacy_fw_header);
1656 const __be32 *data;
1657 unsigned int i;
1658 unsigned int len;
1659 u16 fwrev;
1660 u16 fwpatch;
1661 u16 fwdate;
1662 u16 fwtime;
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001663 u32 tmp, macctl;
Larry Finger75388ac2007-09-25 16:46:54 -07001664 int err = 0;
1665
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001666 /* Jump the microcode PSM to offset 0 */
1667 macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
1668 B43legacy_WARN_ON(macctl & B43legacy_MACCTL_PSM_RUN);
1669 macctl |= B43legacy_MACCTL_PSM_JMP0;
1670 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
1671 /* Zero out all microcode PSM registers and shared memory. */
1672 for (i = 0; i < 64; i++)
1673 b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS, i, 0);
1674 for (i = 0; i < 4096; i += 2)
1675 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, i, 0);
1676
Larry Finger75388ac2007-09-25 16:46:54 -07001677 /* Upload Microcode. */
1678 data = (__be32 *) (dev->fw.ucode->data + hdr_len);
1679 len = (dev->fw.ucode->size - hdr_len) / sizeof(__be32);
1680 b43legacy_shm_control_word(dev,
1681 B43legacy_SHM_UCODE |
1682 B43legacy_SHM_AUTOINC_W,
1683 0x0000);
1684 for (i = 0; i < len; i++) {
1685 b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA,
1686 be32_to_cpu(data[i]));
1687 udelay(10);
1688 }
1689
1690 if (dev->fw.pcm) {
1691 /* Upload PCM data. */
1692 data = (__be32 *) (dev->fw.pcm->data + hdr_len);
1693 len = (dev->fw.pcm->size - hdr_len) / sizeof(__be32);
1694 b43legacy_shm_control_word(dev, B43legacy_SHM_HW, 0x01EA);
1695 b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA, 0x00004000);
1696 /* No need for autoinc bit in SHM_HW */
1697 b43legacy_shm_control_word(dev, B43legacy_SHM_HW, 0x01EB);
1698 for (i = 0; i < len; i++) {
1699 b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA,
1700 be32_to_cpu(data[i]));
1701 udelay(10);
1702 }
1703 }
1704
1705 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON,
1706 B43legacy_IRQ_ALL);
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001707
1708 /* Start the microcode PSM */
1709 macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
1710 macctl &= ~B43legacy_MACCTL_PSM_JMP0;
1711 macctl |= B43legacy_MACCTL_PSM_RUN;
1712 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
Larry Finger75388ac2007-09-25 16:46:54 -07001713
1714 /* Wait for the microcode to load and respond */
1715 i = 0;
1716 while (1) {
1717 tmp = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
1718 if (tmp == B43legacy_IRQ_MAC_SUSPENDED)
1719 break;
1720 i++;
1721 if (i >= B43legacy_IRQWAIT_MAX_RETRIES) {
1722 b43legacyerr(dev->wl, "Microcode not responding\n");
1723 b43legacy_print_fw_helptext(dev->wl);
1724 err = -ENODEV;
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001725 goto error;
Larry Finger75388ac2007-09-25 16:46:54 -07001726 }
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001727 msleep_interruptible(50);
1728 if (signal_pending(current)) {
1729 err = -EINTR;
1730 goto error;
1731 }
Larry Finger75388ac2007-09-25 16:46:54 -07001732 }
1733 /* dummy read follows */
1734 b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
1735
1736 /* Get and check the revisions. */
1737 fwrev = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
1738 B43legacy_SHM_SH_UCODEREV);
1739 fwpatch = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
1740 B43legacy_SHM_SH_UCODEPATCH);
1741 fwdate = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
1742 B43legacy_SHM_SH_UCODEDATE);
1743 fwtime = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
1744 B43legacy_SHM_SH_UCODETIME);
1745
1746 if (fwrev > 0x128) {
1747 b43legacyerr(dev->wl, "YOU ARE TRYING TO LOAD V4 FIRMWARE."
1748 " Only firmware from binary drivers version 3.x"
1749 " is supported. You must change your firmware"
1750 " files.\n");
1751 b43legacy_print_fw_helptext(dev->wl);
Larry Finger75388ac2007-09-25 16:46:54 -07001752 err = -EOPNOTSUPP;
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001753 goto error;
Larry Finger75388ac2007-09-25 16:46:54 -07001754 }
Stefano Briviocfbc35b2008-02-12 03:09:10 +01001755 b43legacyinfo(dev->wl, "Loading firmware version 0x%X, patch level %u "
1756 "(20%.2i-%.2i-%.2i %.2i:%.2i:%.2i)\n", fwrev, fwpatch,
1757 (fwdate >> 12) & 0xF, (fwdate >> 8) & 0xF, fwdate & 0xFF,
1758 (fwtime >> 11) & 0x1F, (fwtime >> 5) & 0x3F,
1759 fwtime & 0x1F);
Larry Finger75388ac2007-09-25 16:46:54 -07001760
1761 dev->fw.rev = fwrev;
1762 dev->fw.patch = fwpatch;
1763
John W. Linvillebcf3c7c2010-07-29 13:30:15 -04001764 snprintf(wiphy->fw_version, sizeof(wiphy->fw_version), "%u.%u",
1765 dev->fw.rev, dev->fw.patch);
1766 wiphy->hw_version = dev->dev->id.coreid;
1767
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001768 return 0;
1769
1770error:
1771 macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
1772 macctl &= ~B43legacy_MACCTL_PSM_RUN;
1773 macctl |= B43legacy_MACCTL_PSM_JMP0;
1774 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
1775
Larry Finger75388ac2007-09-25 16:46:54 -07001776 return err;
1777}
1778
1779static int b43legacy_write_initvals(struct b43legacy_wldev *dev,
1780 const struct b43legacy_iv *ivals,
1781 size_t count,
1782 size_t array_size)
1783{
1784 const struct b43legacy_iv *iv;
1785 u16 offset;
1786 size_t i;
1787 bool bit32;
1788
1789 BUILD_BUG_ON(sizeof(struct b43legacy_iv) != 6);
1790 iv = ivals;
1791 for (i = 0; i < count; i++) {
1792 if (array_size < sizeof(iv->offset_size))
1793 goto err_format;
1794 array_size -= sizeof(iv->offset_size);
1795 offset = be16_to_cpu(iv->offset_size);
1796 bit32 = !!(offset & B43legacy_IV_32BIT);
1797 offset &= B43legacy_IV_OFFSET_MASK;
1798 if (offset >= 0x1000)
1799 goto err_format;
1800 if (bit32) {
1801 u32 value;
1802
1803 if (array_size < sizeof(iv->data.d32))
1804 goto err_format;
1805 array_size -= sizeof(iv->data.d32);
1806
Harvey Harrison533dd1b2008-04-29 01:03:36 -07001807 value = get_unaligned_be32(&iv->data.d32);
Larry Finger75388ac2007-09-25 16:46:54 -07001808 b43legacy_write32(dev, offset, value);
1809
1810 iv = (const struct b43legacy_iv *)((const uint8_t *)iv +
1811 sizeof(__be16) +
1812 sizeof(__be32));
1813 } else {
1814 u16 value;
1815
1816 if (array_size < sizeof(iv->data.d16))
1817 goto err_format;
1818 array_size -= sizeof(iv->data.d16);
1819
1820 value = be16_to_cpu(iv->data.d16);
1821 b43legacy_write16(dev, offset, value);
1822
1823 iv = (const struct b43legacy_iv *)((const uint8_t *)iv +
1824 sizeof(__be16) +
1825 sizeof(__be16));
1826 }
1827 }
1828 if (array_size)
1829 goto err_format;
1830
1831 return 0;
1832
1833err_format:
1834 b43legacyerr(dev->wl, "Initial Values Firmware file-format error.\n");
1835 b43legacy_print_fw_helptext(dev->wl);
1836
1837 return -EPROTO;
1838}
1839
1840static int b43legacy_upload_initvals(struct b43legacy_wldev *dev)
1841{
1842 const size_t hdr_len = sizeof(struct b43legacy_fw_header);
1843 const struct b43legacy_fw_header *hdr;
1844 struct b43legacy_firmware *fw = &dev->fw;
1845 const struct b43legacy_iv *ivals;
1846 size_t count;
1847 int err;
1848
1849 hdr = (const struct b43legacy_fw_header *)(fw->initvals->data);
1850 ivals = (const struct b43legacy_iv *)(fw->initvals->data + hdr_len);
1851 count = be32_to_cpu(hdr->size);
1852 err = b43legacy_write_initvals(dev, ivals, count,
1853 fw->initvals->size - hdr_len);
1854 if (err)
1855 goto out;
1856 if (fw->initvals_band) {
1857 hdr = (const struct b43legacy_fw_header *)
1858 (fw->initvals_band->data);
1859 ivals = (const struct b43legacy_iv *)(fw->initvals_band->data
1860 + hdr_len);
1861 count = be32_to_cpu(hdr->size);
1862 err = b43legacy_write_initvals(dev, ivals, count,
1863 fw->initvals_band->size - hdr_len);
1864 if (err)
1865 goto out;
1866 }
1867out:
1868
1869 return err;
1870}
1871
1872/* Initialize the GPIOs
Alexander A. Klimov140c6022020-07-19 13:11:24 +02001873 * https://bcm-specs.sipsolutions.net/GPIO
Larry Finger75388ac2007-09-25 16:46:54 -07001874 */
1875static int b43legacy_gpio_init(struct b43legacy_wldev *dev)
1876{
1877 struct ssb_bus *bus = dev->dev->bus;
1878 struct ssb_device *gpiodev, *pcidev = NULL;
1879 u32 mask;
1880 u32 set;
1881
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001882 b43legacy_write32(dev, B43legacy_MMIO_MACCTL,
Larry Finger75388ac2007-09-25 16:46:54 -07001883 b43legacy_read32(dev,
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001884 B43legacy_MMIO_MACCTL)
Larry Finger75388ac2007-09-25 16:46:54 -07001885 & 0xFFFF3FFF);
1886
Larry Finger75388ac2007-09-25 16:46:54 -07001887 b43legacy_write16(dev, B43legacy_MMIO_GPIO_MASK,
1888 b43legacy_read16(dev,
1889 B43legacy_MMIO_GPIO_MASK)
1890 | 0x000F);
1891
1892 mask = 0x0000001F;
1893 set = 0x0000000F;
1894 if (dev->dev->bus->chip_id == 0x4301) {
1895 mask |= 0x0060;
1896 set |= 0x0060;
1897 }
Larry Finger7797aa32007-11-09 16:57:34 -06001898 if (dev->dev->bus->sprom.boardflags_lo & B43legacy_BFL_PACTRL) {
Larry Finger75388ac2007-09-25 16:46:54 -07001899 b43legacy_write16(dev, B43legacy_MMIO_GPIO_MASK,
1900 b43legacy_read16(dev,
1901 B43legacy_MMIO_GPIO_MASK)
1902 | 0x0200);
1903 mask |= 0x0200;
1904 set |= 0x0200;
1905 }
1906 if (dev->dev->id.revision >= 2)
1907 mask |= 0x0010; /* FIXME: This is redundant. */
1908
1909#ifdef CONFIG_SSB_DRIVER_PCICORE
1910 pcidev = bus->pcicore.dev;
1911#endif
1912 gpiodev = bus->chipco.dev ? : pcidev;
1913 if (!gpiodev)
1914 return 0;
1915 ssb_write32(gpiodev, B43legacy_GPIO_CONTROL,
1916 (ssb_read32(gpiodev, B43legacy_GPIO_CONTROL)
Rafał Miłecki85ce5ae2012-08-08 19:10:16 +02001917 & ~mask) | set);
Larry Finger75388ac2007-09-25 16:46:54 -07001918
1919 return 0;
1920}
1921
1922/* Turn off all GPIO stuff. Call this on module unload, for example. */
1923static void b43legacy_gpio_cleanup(struct b43legacy_wldev *dev)
1924{
1925 struct ssb_bus *bus = dev->dev->bus;
1926 struct ssb_device *gpiodev, *pcidev = NULL;
1927
1928#ifdef CONFIG_SSB_DRIVER_PCICORE
1929 pcidev = bus->pcicore.dev;
1930#endif
1931 gpiodev = bus->chipco.dev ? : pcidev;
1932 if (!gpiodev)
1933 return;
1934 ssb_write32(gpiodev, B43legacy_GPIO_CONTROL, 0);
1935}
1936
1937/* http://bcm-specs.sipsolutions.net/EnableMac */
1938void b43legacy_mac_enable(struct b43legacy_wldev *dev)
1939{
1940 dev->mac_suspended--;
1941 B43legacy_WARN_ON(dev->mac_suspended < 0);
Larry Fingerf34eb692007-10-11 00:05:57 -05001942 B43legacy_WARN_ON(irqs_disabled());
Larry Finger75388ac2007-09-25 16:46:54 -07001943 if (dev->mac_suspended == 0) {
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001944 b43legacy_write32(dev, B43legacy_MMIO_MACCTL,
Larry Finger75388ac2007-09-25 16:46:54 -07001945 b43legacy_read32(dev,
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001946 B43legacy_MMIO_MACCTL)
1947 | B43legacy_MACCTL_ENABLED);
Larry Finger75388ac2007-09-25 16:46:54 -07001948 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON,
1949 B43legacy_IRQ_MAC_SUSPENDED);
1950 /* the next two are dummy reads */
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001951 b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
Larry Finger75388ac2007-09-25 16:46:54 -07001952 b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
1953 b43legacy_power_saving_ctl_bits(dev, -1, -1);
Larry Fingerf34eb692007-10-11 00:05:57 -05001954
1955 /* Re-enable IRQs. */
1956 spin_lock_irq(&dev->wl->irq_lock);
Stefano Brivio44710bb2009-05-15 15:39:20 -05001957 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK,
1958 dev->irq_mask);
Larry Fingerf34eb692007-10-11 00:05:57 -05001959 spin_unlock_irq(&dev->wl->irq_lock);
Larry Finger75388ac2007-09-25 16:46:54 -07001960 }
1961}
1962
Alexander A. Klimov140c6022020-07-19 13:11:24 +02001963/* https://bcm-specs.sipsolutions.net/SuspendMAC */
Larry Finger75388ac2007-09-25 16:46:54 -07001964void b43legacy_mac_suspend(struct b43legacy_wldev *dev)
1965{
1966 int i;
1967 u32 tmp;
1968
Larry Fingerf34eb692007-10-11 00:05:57 -05001969 might_sleep();
1970 B43legacy_WARN_ON(irqs_disabled());
Larry Finger75388ac2007-09-25 16:46:54 -07001971 B43legacy_WARN_ON(dev->mac_suspended < 0);
Larry Fingerf34eb692007-10-11 00:05:57 -05001972
Larry Finger75388ac2007-09-25 16:46:54 -07001973 if (dev->mac_suspended == 0) {
Larry Fingerf34eb692007-10-11 00:05:57 -05001974 /* Mask IRQs before suspending MAC. Otherwise
1975 * the MAC stays busy and won't suspend. */
1976 spin_lock_irq(&dev->wl->irq_lock);
Stefano Brivio44710bb2009-05-15 15:39:20 -05001977 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, 0);
Larry Fingerf34eb692007-10-11 00:05:57 -05001978 spin_unlock_irq(&dev->wl->irq_lock);
1979 b43legacy_synchronize_irq(dev);
Larry Fingerf34eb692007-10-11 00:05:57 -05001980
Larry Finger75388ac2007-09-25 16:46:54 -07001981 b43legacy_power_saving_ctl_bits(dev, -1, 1);
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001982 b43legacy_write32(dev, B43legacy_MMIO_MACCTL,
Larry Finger75388ac2007-09-25 16:46:54 -07001983 b43legacy_read32(dev,
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001984 B43legacy_MMIO_MACCTL)
1985 & ~B43legacy_MACCTL_ENABLED);
Larry Finger75388ac2007-09-25 16:46:54 -07001986 b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
Larry Fingerf34eb692007-10-11 00:05:57 -05001987 for (i = 40; i; i--) {
Larry Finger75388ac2007-09-25 16:46:54 -07001988 tmp = b43legacy_read32(dev,
1989 B43legacy_MMIO_GEN_IRQ_REASON);
1990 if (tmp & B43legacy_IRQ_MAC_SUSPENDED)
1991 goto out;
Larry Fingerf34eb692007-10-11 00:05:57 -05001992 msleep(1);
Larry Finger75388ac2007-09-25 16:46:54 -07001993 }
1994 b43legacyerr(dev->wl, "MAC suspend failed\n");
1995 }
1996out:
1997 dev->mac_suspended++;
1998}
1999
2000static void b43legacy_adjust_opmode(struct b43legacy_wldev *dev)
2001{
2002 struct b43legacy_wl *wl = dev->wl;
2003 u32 ctl;
2004 u16 cfp_pretbtt;
2005
2006 ctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
2007 /* Reset status to STA infrastructure mode. */
2008 ctl &= ~B43legacy_MACCTL_AP;
2009 ctl &= ~B43legacy_MACCTL_KEEP_CTL;
2010 ctl &= ~B43legacy_MACCTL_KEEP_BADPLCP;
2011 ctl &= ~B43legacy_MACCTL_KEEP_BAD;
2012 ctl &= ~B43legacy_MACCTL_PROMISC;
Johannes Berg4150c572007-09-17 01:29:23 -04002013 ctl &= ~B43legacy_MACCTL_BEACPROMISC;
Larry Finger75388ac2007-09-25 16:46:54 -07002014 ctl |= B43legacy_MACCTL_INFRA;
2015
Johannes Berg05c914f2008-09-11 00:01:58 +02002016 if (b43legacy_is_mode(wl, NL80211_IFTYPE_AP))
Johannes Berg4150c572007-09-17 01:29:23 -04002017 ctl |= B43legacy_MACCTL_AP;
Johannes Berg05c914f2008-09-11 00:01:58 +02002018 else if (b43legacy_is_mode(wl, NL80211_IFTYPE_ADHOC))
Johannes Berg4150c572007-09-17 01:29:23 -04002019 ctl &= ~B43legacy_MACCTL_INFRA;
2020
2021 if (wl->filter_flags & FIF_CONTROL)
Larry Finger75388ac2007-09-25 16:46:54 -07002022 ctl |= B43legacy_MACCTL_KEEP_CTL;
Johannes Berg4150c572007-09-17 01:29:23 -04002023 if (wl->filter_flags & FIF_FCSFAIL)
2024 ctl |= B43legacy_MACCTL_KEEP_BAD;
2025 if (wl->filter_flags & FIF_PLCPFAIL)
2026 ctl |= B43legacy_MACCTL_KEEP_BADPLCP;
Johannes Berg4150c572007-09-17 01:29:23 -04002027 if (wl->filter_flags & FIF_BCN_PRBRESP_PROMISC)
2028 ctl |= B43legacy_MACCTL_BEACPROMISC;
2029
Larry Finger75388ac2007-09-25 16:46:54 -07002030 /* Workaround: On old hardware the HW-MAC-address-filter
2031 * doesn't work properly, so always run promisc in filter
2032 * it in software. */
2033 if (dev->dev->id.revision <= 4)
2034 ctl |= B43legacy_MACCTL_PROMISC;
2035
2036 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, ctl);
2037
2038 cfp_pretbtt = 2;
2039 if ((ctl & B43legacy_MACCTL_INFRA) &&
2040 !(ctl & B43legacy_MACCTL_AP)) {
2041 if (dev->dev->bus->chip_id == 0x4306 &&
2042 dev->dev->bus->chip_rev == 3)
2043 cfp_pretbtt = 100;
2044 else
2045 cfp_pretbtt = 50;
2046 }
2047 b43legacy_write16(dev, 0x612, cfp_pretbtt);
2048}
2049
2050static void b43legacy_rate_memory_write(struct b43legacy_wldev *dev,
2051 u16 rate,
2052 int is_ofdm)
2053{
2054 u16 offset;
2055
2056 if (is_ofdm) {
2057 offset = 0x480;
2058 offset += (b43legacy_plcp_get_ratecode_ofdm(rate) & 0x000F) * 2;
2059 } else {
2060 offset = 0x4C0;
2061 offset += (b43legacy_plcp_get_ratecode_cck(rate) & 0x000F) * 2;
2062 }
2063 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, offset + 0x20,
2064 b43legacy_shm_read16(dev,
2065 B43legacy_SHM_SHARED, offset));
2066}
2067
2068static void b43legacy_rate_memory_init(struct b43legacy_wldev *dev)
2069{
2070 switch (dev->phy.type) {
2071 case B43legacy_PHYTYPE_G:
2072 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_6MB, 1);
2073 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_12MB, 1);
2074 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_18MB, 1);
2075 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_24MB, 1);
2076 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_36MB, 1);
2077 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_48MB, 1);
2078 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_54MB, 1);
2079 /* fallthrough */
2080 case B43legacy_PHYTYPE_B:
2081 b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_1MB, 0);
2082 b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_2MB, 0);
2083 b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_5MB, 0);
2084 b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_11MB, 0);
2085 break;
2086 default:
2087 B43legacy_BUG_ON(1);
2088 }
2089}
2090
2091/* Set the TX-Antenna for management frames sent by firmware. */
2092static void b43legacy_mgmtframe_txantenna(struct b43legacy_wldev *dev,
2093 int antenna)
2094{
2095 u16 ant = 0;
2096 u16 tmp;
2097
2098 switch (antenna) {
2099 case B43legacy_ANTENNA0:
2100 ant |= B43legacy_TX4_PHY_ANT0;
2101 break;
2102 case B43legacy_ANTENNA1:
2103 ant |= B43legacy_TX4_PHY_ANT1;
2104 break;
2105 case B43legacy_ANTENNA_AUTO:
2106 ant |= B43legacy_TX4_PHY_ANTLAST;
2107 break;
2108 default:
2109 B43legacy_BUG_ON(1);
2110 }
2111
2112 /* FIXME We also need to set the other flags of the PHY control
2113 * field somewhere. */
2114
2115 /* For Beacons */
2116 tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
2117 B43legacy_SHM_SH_BEACPHYCTL);
2118 tmp = (tmp & ~B43legacy_TX4_PHY_ANT) | ant;
2119 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
2120 B43legacy_SHM_SH_BEACPHYCTL, tmp);
2121 /* For ACK/CTS */
2122 tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
2123 B43legacy_SHM_SH_ACKCTSPHYCTL);
2124 tmp = (tmp & ~B43legacy_TX4_PHY_ANT) | ant;
2125 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
2126 B43legacy_SHM_SH_ACKCTSPHYCTL, tmp);
2127 /* For Probe Resposes */
2128 tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
2129 B43legacy_SHM_SH_PRPHYCTL);
2130 tmp = (tmp & ~B43legacy_TX4_PHY_ANT) | ant;
2131 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
2132 B43legacy_SHM_SH_PRPHYCTL, tmp);
2133}
2134
2135/* This is the opposite of b43legacy_chip_init() */
2136static void b43legacy_chip_exit(struct b43legacy_wldev *dev)
2137{
Larry Finger93bb7f32007-10-10 22:44:22 -05002138 b43legacy_radio_turn_off(dev, 1);
Larry Finger75388ac2007-09-25 16:46:54 -07002139 b43legacy_gpio_cleanup(dev);
2140 /* firmware is released later */
2141}
2142
2143/* Initialize the chip
Alexander A. Klimov140c6022020-07-19 13:11:24 +02002144 * https://bcm-specs.sipsolutions.net/ChipInit
Larry Finger75388ac2007-09-25 16:46:54 -07002145 */
2146static int b43legacy_chip_init(struct b43legacy_wldev *dev)
2147{
2148 struct b43legacy_phy *phy = &dev->phy;
2149 int err;
2150 int tmp;
Stefano Brivioe78c9d22008-01-23 14:48:50 +01002151 u32 value32, macctl;
Larry Finger75388ac2007-09-25 16:46:54 -07002152 u16 value16;
2153
Stefano Brivioe78c9d22008-01-23 14:48:50 +01002154 /* Initialize the MAC control */
2155 macctl = B43legacy_MACCTL_IHR_ENABLED | B43legacy_MACCTL_SHM_ENABLED;
2156 if (dev->phy.gmode)
2157 macctl |= B43legacy_MACCTL_GMODE;
2158 macctl |= B43legacy_MACCTL_INFRA;
2159 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
Larry Finger75388ac2007-09-25 16:46:54 -07002160
Larry Finger75388ac2007-09-25 16:46:54 -07002161 err = b43legacy_upload_microcode(dev);
2162 if (err)
2163 goto out; /* firmware is released later */
2164
2165 err = b43legacy_gpio_init(dev);
2166 if (err)
2167 goto out; /* firmware is released later */
Larry Fingerba48f7b2007-10-12 23:04:51 -05002168
Larry Finger75388ac2007-09-25 16:46:54 -07002169 err = b43legacy_upload_initvals(dev);
2170 if (err)
Larry Finger4ad36d72007-12-16 19:21:06 +01002171 goto err_gpio_clean;
Larry Finger75388ac2007-09-25 16:46:54 -07002172 b43legacy_radio_turn_on(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07002173
2174 b43legacy_write16(dev, 0x03E6, 0x0000);
2175 err = b43legacy_phy_init(dev);
2176 if (err)
2177 goto err_radio_off;
2178
2179 /* Select initial Interference Mitigation. */
2180 tmp = phy->interfmode;
2181 phy->interfmode = B43legacy_INTERFMODE_NONE;
2182 b43legacy_radio_set_interference_mitigation(dev, tmp);
2183
2184 b43legacy_phy_set_antenna_diversity(dev);
2185 b43legacy_mgmtframe_txantenna(dev, B43legacy_ANTENNA_DEFAULT);
2186
2187 if (phy->type == B43legacy_PHYTYPE_B) {
2188 value16 = b43legacy_read16(dev, 0x005E);
2189 value16 |= 0x0004;
2190 b43legacy_write16(dev, 0x005E, value16);
2191 }
2192 b43legacy_write32(dev, 0x0100, 0x01000000);
2193 if (dev->dev->id.revision < 5)
2194 b43legacy_write32(dev, 0x010C, 0x01000000);
2195
Stefano Brivioe78c9d22008-01-23 14:48:50 +01002196 value32 = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
2197 value32 &= ~B43legacy_MACCTL_INFRA;
2198 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, value32);
2199 value32 = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
2200 value32 |= B43legacy_MACCTL_INFRA;
2201 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, value32);
Larry Finger75388ac2007-09-25 16:46:54 -07002202
Larry Finger75388ac2007-09-25 16:46:54 -07002203 if (b43legacy_using_pio(dev)) {
2204 b43legacy_write32(dev, 0x0210, 0x00000100);
2205 b43legacy_write32(dev, 0x0230, 0x00000100);
2206 b43legacy_write32(dev, 0x0250, 0x00000100);
2207 b43legacy_write32(dev, 0x0270, 0x00000100);
2208 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x0034,
2209 0x0000);
2210 }
2211
2212 /* Probe Response Timeout value */
2213 /* FIXME: Default to 0, has to be set by ioctl probably... :-/ */
2214 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x0074, 0x0000);
2215
2216 /* Initially set the wireless operation mode. */
2217 b43legacy_adjust_opmode(dev);
2218
2219 if (dev->dev->id.revision < 3) {
2220 b43legacy_write16(dev, 0x060E, 0x0000);
2221 b43legacy_write16(dev, 0x0610, 0x8000);
2222 b43legacy_write16(dev, 0x0604, 0x0000);
2223 b43legacy_write16(dev, 0x0606, 0x0200);
2224 } else {
2225 b43legacy_write32(dev, 0x0188, 0x80000000);
2226 b43legacy_write32(dev, 0x018C, 0x02000000);
2227 }
2228 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON, 0x00004000);
2229 b43legacy_write32(dev, B43legacy_MMIO_DMA0_IRQ_MASK, 0x0001DC00);
2230 b43legacy_write32(dev, B43legacy_MMIO_DMA1_IRQ_MASK, 0x0000DC00);
2231 b43legacy_write32(dev, B43legacy_MMIO_DMA2_IRQ_MASK, 0x0000DC00);
2232 b43legacy_write32(dev, B43legacy_MMIO_DMA3_IRQ_MASK, 0x0001DC00);
2233 b43legacy_write32(dev, B43legacy_MMIO_DMA4_IRQ_MASK, 0x0000DC00);
2234 b43legacy_write32(dev, B43legacy_MMIO_DMA5_IRQ_MASK, 0x0000DC00);
2235
2236 value32 = ssb_read32(dev->dev, SSB_TMSLOW);
Rafał Miłeckia7ffab32011-05-10 15:53:16 +02002237 value32 |= B43legacy_TMSLOW_MACPHYCLKEN;
Larry Finger75388ac2007-09-25 16:46:54 -07002238 ssb_write32(dev->dev, SSB_TMSLOW, value32);
2239
2240 b43legacy_write16(dev, B43legacy_MMIO_POWERUP_DELAY,
2241 dev->dev->bus->chipco.fast_pwrup_delay);
2242
Stefano Brivioa293ee92007-11-24 23:35:25 +01002243 /* PHY TX errors counter. */
2244 atomic_set(&phy->txerr_cnt, B43legacy_PHY_TX_BADNESS_LIMIT);
2245
Larry Finger75388ac2007-09-25 16:46:54 -07002246 B43legacy_WARN_ON(err != 0);
2247 b43legacydbg(dev->wl, "Chip initialized\n");
2248out:
2249 return err;
2250
2251err_radio_off:
Larry Finger93bb7f32007-10-10 22:44:22 -05002252 b43legacy_radio_turn_off(dev, 1);
Larry Finger4ad36d72007-12-16 19:21:06 +01002253err_gpio_clean:
Larry Finger75388ac2007-09-25 16:46:54 -07002254 b43legacy_gpio_cleanup(dev);
2255 goto out;
2256}
2257
2258static void b43legacy_periodic_every120sec(struct b43legacy_wldev *dev)
2259{
2260 struct b43legacy_phy *phy = &dev->phy;
2261
2262 if (phy->type != B43legacy_PHYTYPE_G || phy->rev < 2)
2263 return;
2264
2265 b43legacy_mac_suspend(dev);
2266 b43legacy_phy_lo_g_measure(dev);
2267 b43legacy_mac_enable(dev);
2268}
2269
2270static void b43legacy_periodic_every60sec(struct b43legacy_wldev *dev)
2271{
2272 b43legacy_phy_lo_mark_all_unused(dev);
Larry Finger7797aa32007-11-09 16:57:34 -06002273 if (dev->dev->bus->sprom.boardflags_lo & B43legacy_BFL_RSSI) {
Larry Finger75388ac2007-09-25 16:46:54 -07002274 b43legacy_mac_suspend(dev);
2275 b43legacy_calc_nrssi_slope(dev);
2276 b43legacy_mac_enable(dev);
2277 }
2278}
2279
2280static void b43legacy_periodic_every30sec(struct b43legacy_wldev *dev)
2281{
2282 /* Update device statistics. */
2283 b43legacy_calculate_link_quality(dev);
2284}
2285
2286static void b43legacy_periodic_every15sec(struct b43legacy_wldev *dev)
2287{
2288 b43legacy_phy_xmitpower(dev); /* FIXME: unless scanning? */
Stefano Brivioa293ee92007-11-24 23:35:25 +01002289
2290 atomic_set(&dev->phy.txerr_cnt, B43legacy_PHY_TX_BADNESS_LIMIT);
2291 wmb();
Larry Finger75388ac2007-09-25 16:46:54 -07002292}
2293
Larry Finger75388ac2007-09-25 16:46:54 -07002294static void do_periodic_work(struct b43legacy_wldev *dev)
2295{
2296 unsigned int state;
2297
2298 state = dev->periodic_state;
Larry Finger6be50832007-10-10 22:48:17 -05002299 if (state % 8 == 0)
Larry Finger75388ac2007-09-25 16:46:54 -07002300 b43legacy_periodic_every120sec(dev);
Larry Finger6be50832007-10-10 22:48:17 -05002301 if (state % 4 == 0)
Larry Finger75388ac2007-09-25 16:46:54 -07002302 b43legacy_periodic_every60sec(dev);
Larry Finger6be50832007-10-10 22:48:17 -05002303 if (state % 2 == 0)
Larry Finger75388ac2007-09-25 16:46:54 -07002304 b43legacy_periodic_every30sec(dev);
Larry Finger6be50832007-10-10 22:48:17 -05002305 b43legacy_periodic_every15sec(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07002306}
2307
Larry Fingerf34eb692007-10-11 00:05:57 -05002308/* Periodic work locking policy:
2309 * The whole periodic work handler is protected by
2310 * wl->mutex. If another lock is needed somewhere in the
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02002311 * pwork callchain, it's acquired in-place, where it's needed.
Larry Finger75388ac2007-09-25 16:46:54 -07002312 */
Larry Finger75388ac2007-09-25 16:46:54 -07002313static void b43legacy_periodic_work_handler(struct work_struct *work)
2314{
Larry Fingerf34eb692007-10-11 00:05:57 -05002315 struct b43legacy_wldev *dev = container_of(work, struct b43legacy_wldev,
2316 periodic_work.work);
2317 struct b43legacy_wl *wl = dev->wl;
Larry Finger75388ac2007-09-25 16:46:54 -07002318 unsigned long delay;
Larry Finger75388ac2007-09-25 16:46:54 -07002319
Larry Fingerf34eb692007-10-11 00:05:57 -05002320 mutex_lock(&wl->mutex);
Larry Finger75388ac2007-09-25 16:46:54 -07002321
2322 if (unlikely(b43legacy_status(dev) != B43legacy_STAT_STARTED))
2323 goto out;
2324 if (b43legacy_debug(dev, B43legacy_DBG_PWORK_STOP))
2325 goto out_requeue;
2326
Larry Fingerf34eb692007-10-11 00:05:57 -05002327 do_periodic_work(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07002328
Larry Finger75388ac2007-09-25 16:46:54 -07002329 dev->periodic_state++;
2330out_requeue:
2331 if (b43legacy_debug(dev, B43legacy_DBG_PWORK_FAST))
2332 delay = msecs_to_jiffies(50);
2333 else
Larry Finger6be50832007-10-10 22:48:17 -05002334 delay = round_jiffies_relative(HZ * 15);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04002335 ieee80211_queue_delayed_work(wl->hw, &dev->periodic_work, delay);
Larry Finger75388ac2007-09-25 16:46:54 -07002336out:
Larry Fingerf34eb692007-10-11 00:05:57 -05002337 mutex_unlock(&wl->mutex);
Larry Finger75388ac2007-09-25 16:46:54 -07002338}
2339
2340static void b43legacy_periodic_tasks_setup(struct b43legacy_wldev *dev)
2341{
2342 struct delayed_work *work = &dev->periodic_work;
2343
2344 dev->periodic_state = 0;
2345 INIT_DELAYED_WORK(work, b43legacy_periodic_work_handler);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04002346 ieee80211_queue_delayed_work(dev->wl->hw, work, 0);
Larry Finger75388ac2007-09-25 16:46:54 -07002347}
2348
2349/* Validate access to the chip (SHM) */
2350static int b43legacy_validate_chipaccess(struct b43legacy_wldev *dev)
2351{
2352 u32 value;
2353 u32 shm_backup;
2354
2355 shm_backup = b43legacy_shm_read32(dev, B43legacy_SHM_SHARED, 0);
2356 b43legacy_shm_write32(dev, B43legacy_SHM_SHARED, 0, 0xAA5555AA);
2357 if (b43legacy_shm_read32(dev, B43legacy_SHM_SHARED, 0) !=
2358 0xAA5555AA)
2359 goto error;
2360 b43legacy_shm_write32(dev, B43legacy_SHM_SHARED, 0, 0x55AAAA55);
2361 if (b43legacy_shm_read32(dev, B43legacy_SHM_SHARED, 0) !=
2362 0x55AAAA55)
2363 goto error;
2364 b43legacy_shm_write32(dev, B43legacy_SHM_SHARED, 0, shm_backup);
2365
2366 value = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
2367 if ((value | B43legacy_MACCTL_GMODE) !=
2368 (B43legacy_MACCTL_GMODE | B43legacy_MACCTL_IHR_ENABLED))
2369 goto error;
2370
2371 value = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
2372 if (value)
2373 goto error;
2374
2375 return 0;
2376error:
2377 b43legacyerr(dev->wl, "Failed to validate the chipaccess\n");
2378 return -ENODEV;
2379}
2380
2381static void b43legacy_security_init(struct b43legacy_wldev *dev)
2382{
2383 dev->max_nr_keys = (dev->dev->id.revision >= 5) ? 58 : 20;
2384 B43legacy_WARN_ON(dev->max_nr_keys > ARRAY_SIZE(dev->key));
2385 dev->ktp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
2386 0x0056);
2387 /* KTP is a word address, but we address SHM bytewise.
2388 * So multiply by two.
2389 */
2390 dev->ktp *= 2;
2391 if (dev->dev->id.revision >= 5)
2392 /* Number of RCMTA address slots */
2393 b43legacy_write16(dev, B43legacy_MMIO_RCMTA_COUNT,
2394 dev->max_nr_keys - 8);
2395}
2396
Larry Finger910cfee2009-04-01 10:42:36 -05002397#ifdef CONFIG_B43LEGACY_HWRNG
Larry Finger75388ac2007-09-25 16:46:54 -07002398static int b43legacy_rng_read(struct hwrng *rng, u32 *data)
2399{
2400 struct b43legacy_wl *wl = (struct b43legacy_wl *)rng->priv;
2401 unsigned long flags;
2402
2403 /* Don't take wl->mutex here, as it could deadlock with
2404 * hwrng internal locking. It's not needed to take
2405 * wl->mutex here, anyway. */
2406
2407 spin_lock_irqsave(&wl->irq_lock, flags);
2408 *data = b43legacy_read16(wl->current_dev, B43legacy_MMIO_RNG);
2409 spin_unlock_irqrestore(&wl->irq_lock, flags);
2410
2411 return (sizeof(u16));
2412}
Larry Finger910cfee2009-04-01 10:42:36 -05002413#endif
Larry Finger75388ac2007-09-25 16:46:54 -07002414
2415static void b43legacy_rng_exit(struct b43legacy_wl *wl)
2416{
Larry Finger910cfee2009-04-01 10:42:36 -05002417#ifdef CONFIG_B43LEGACY_HWRNG
Larry Finger75388ac2007-09-25 16:46:54 -07002418 if (wl->rng_initialized)
2419 hwrng_unregister(&wl->rng);
Larry Finger910cfee2009-04-01 10:42:36 -05002420#endif
Larry Finger75388ac2007-09-25 16:46:54 -07002421}
2422
2423static int b43legacy_rng_init(struct b43legacy_wl *wl)
2424{
Larry Finger910cfee2009-04-01 10:42:36 -05002425 int err = 0;
Larry Finger75388ac2007-09-25 16:46:54 -07002426
Larry Finger910cfee2009-04-01 10:42:36 -05002427#ifdef CONFIG_B43LEGACY_HWRNG
Larry Finger75388ac2007-09-25 16:46:54 -07002428 snprintf(wl->rng_name, ARRAY_SIZE(wl->rng_name),
2429 "%s_%s", KBUILD_MODNAME, wiphy_name(wl->hw->wiphy));
2430 wl->rng.name = wl->rng_name;
2431 wl->rng.data_read = b43legacy_rng_read;
2432 wl->rng.priv = (unsigned long)wl;
2433 wl->rng_initialized = 1;
2434 err = hwrng_register(&wl->rng);
2435 if (err) {
2436 wl->rng_initialized = 0;
2437 b43legacyerr(wl, "Failed to register the random "
2438 "number generator (%d)\n", err);
2439 }
2440
Larry Finger910cfee2009-04-01 10:42:36 -05002441#endif
Larry Finger75388ac2007-09-25 16:46:54 -07002442 return err;
2443}
2444
Larry Finger5d07a3d2011-12-21 18:47:59 -06002445static void b43legacy_tx_work(struct work_struct *work)
2446{
2447 struct b43legacy_wl *wl = container_of(work, struct b43legacy_wl,
2448 tx_work);
2449 struct b43legacy_wldev *dev;
2450 struct sk_buff *skb;
2451 int queue_num;
2452 int err = 0;
2453
2454 mutex_lock(&wl->mutex);
2455 dev = wl->current_dev;
2456 if (unlikely(!dev || b43legacy_status(dev) < B43legacy_STAT_STARTED)) {
2457 mutex_unlock(&wl->mutex);
2458 return;
2459 }
2460
2461 for (queue_num = 0; queue_num < B43legacy_QOS_QUEUE_NUM; queue_num++) {
2462 while (skb_queue_len(&wl->tx_queue[queue_num])) {
2463 skb = skb_dequeue(&wl->tx_queue[queue_num]);
2464 if (b43legacy_using_pio(dev))
2465 err = b43legacy_pio_tx(dev, skb);
2466 else
2467 err = b43legacy_dma_tx(dev, skb);
2468 if (err == -ENOSPC) {
2469 wl->tx_queue_stopped[queue_num] = 1;
2470 ieee80211_stop_queue(wl->hw, queue_num);
2471 skb_queue_head(&wl->tx_queue[queue_num], skb);
2472 break;
2473 }
2474 if (unlikely(err))
2475 dev_kfree_skb(skb); /* Drop it */
2476 err = 0;
2477 }
2478
2479 if (!err)
2480 wl->tx_queue_stopped[queue_num] = 0;
2481 }
2482
2483 mutex_unlock(&wl->mutex);
2484}
2485
Johannes Berg7bb45682011-02-24 14:42:06 +01002486static void b43legacy_op_tx(struct ieee80211_hw *hw,
Thomas Huehn36323f82012-07-23 21:33:42 +02002487 struct ieee80211_tx_control *control,
Johannes Berg7bb45682011-02-24 14:42:06 +01002488 struct sk_buff *skb)
Larry Finger75388ac2007-09-25 16:46:54 -07002489{
2490 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
Larry Finger75388ac2007-09-25 16:46:54 -07002491
Larry Finger5d07a3d2011-12-21 18:47:59 -06002492 if (unlikely(skb->len < 2 + 2 + 6)) {
2493 /* Too short, this can't be a valid frame. */
Michael Buesch664f2002008-06-15 15:27:49 +02002494 dev_kfree_skb_any(skb);
Larry Finger5d07a3d2011-12-21 18:47:59 -06002495 return;
Michael Buesch664f2002008-06-15 15:27:49 +02002496 }
Larry Finger5d07a3d2011-12-21 18:47:59 -06002497 B43legacy_WARN_ON(skb_shinfo(skb)->nr_frags);
2498
2499 skb_queue_tail(&wl->tx_queue[skb->queue_mapping], skb);
2500 if (!wl->tx_queue_stopped[skb->queue_mapping])
2501 ieee80211_queue_work(wl->hw, &wl->tx_work);
2502 else
2503 ieee80211_stop_queue(wl->hw, skb->queue_mapping);
Larry Finger75388ac2007-09-25 16:46:54 -07002504}
2505
Eliad Peller8a3a3c82011-10-02 10:15:52 +02002506static int b43legacy_op_conf_tx(struct ieee80211_hw *hw,
2507 struct ieee80211_vif *vif, u16 queue,
Stefano Brivio33a3dc932007-11-06 22:48:25 +01002508 const struct ieee80211_tx_queue_params *params)
Larry Finger75388ac2007-09-25 16:46:54 -07002509{
2510 return 0;
2511}
2512
Stefano Brivio33a3dc932007-11-06 22:48:25 +01002513static int b43legacy_op_get_stats(struct ieee80211_hw *hw,
2514 struct ieee80211_low_level_stats *stats)
Larry Finger75388ac2007-09-25 16:46:54 -07002515{
2516 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
2517 unsigned long flags;
2518
2519 spin_lock_irqsave(&wl->irq_lock, flags);
2520 memcpy(stats, &wl->ieee_stats, sizeof(*stats));
2521 spin_unlock_irqrestore(&wl->irq_lock, flags);
2522
2523 return 0;
2524}
2525
2526static const char *phymode_to_string(unsigned int phymode)
2527{
2528 switch (phymode) {
2529 case B43legacy_PHYMODE_B:
2530 return "B";
2531 case B43legacy_PHYMODE_G:
2532 return "G";
2533 default:
2534 B43legacy_BUG_ON(1);
2535 }
2536 return "";
2537}
2538
2539static int find_wldev_for_phymode(struct b43legacy_wl *wl,
2540 unsigned int phymode,
2541 struct b43legacy_wldev **dev,
2542 bool *gmode)
2543{
2544 struct b43legacy_wldev *d;
2545
2546 list_for_each_entry(d, &wl->devlist, list) {
2547 if (d->phy.possible_phymodes & phymode) {
2548 /* Ok, this device supports the PHY-mode.
2549 * Set the gmode bit. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00002550 *gmode = true;
Larry Finger75388ac2007-09-25 16:46:54 -07002551 *dev = d;
2552
2553 return 0;
2554 }
2555 }
2556
2557 return -ESRCH;
2558}
2559
2560static void b43legacy_put_phy_into_reset(struct b43legacy_wldev *dev)
2561{
2562 struct ssb_device *sdev = dev->dev;
2563 u32 tmslow;
2564
2565 tmslow = ssb_read32(sdev, SSB_TMSLOW);
2566 tmslow &= ~B43legacy_TMSLOW_GMODE;
2567 tmslow |= B43legacy_TMSLOW_PHYRESET;
2568 tmslow |= SSB_TMSLOW_FGC;
2569 ssb_write32(sdev, SSB_TMSLOW, tmslow);
2570 msleep(1);
2571
2572 tmslow = ssb_read32(sdev, SSB_TMSLOW);
2573 tmslow &= ~SSB_TMSLOW_FGC;
2574 tmslow |= B43legacy_TMSLOW_PHYRESET;
2575 ssb_write32(sdev, SSB_TMSLOW, tmslow);
2576 msleep(1);
2577}
2578
2579/* Expects wl->mutex locked */
2580static int b43legacy_switch_phymode(struct b43legacy_wl *wl,
2581 unsigned int new_mode)
2582{
John W. Linville08cb7e02009-01-12 14:43:18 -05002583 struct b43legacy_wldev *uninitialized_var(up_dev);
Larry Finger75388ac2007-09-25 16:46:54 -07002584 struct b43legacy_wldev *down_dev;
2585 int err;
Rusty Russell3db1cd52011-12-19 13:56:45 +00002586 bool gmode = false;
Larry Finger75388ac2007-09-25 16:46:54 -07002587 int prev_status;
2588
2589 err = find_wldev_for_phymode(wl, new_mode, &up_dev, &gmode);
2590 if (err) {
2591 b43legacyerr(wl, "Could not find a device for %s-PHY mode\n",
2592 phymode_to_string(new_mode));
2593 return err;
2594 }
2595 if ((up_dev == wl->current_dev) &&
2596 (!!wl->current_dev->phy.gmode == !!gmode))
2597 /* This device is already running. */
2598 return 0;
2599 b43legacydbg(wl, "Reconfiguring PHYmode to %s-PHY\n",
2600 phymode_to_string(new_mode));
2601 down_dev = wl->current_dev;
2602
2603 prev_status = b43legacy_status(down_dev);
2604 /* Shutdown the currently running core. */
2605 if (prev_status >= B43legacy_STAT_STARTED)
2606 b43legacy_wireless_core_stop(down_dev);
2607 if (prev_status >= B43legacy_STAT_INITIALIZED)
2608 b43legacy_wireless_core_exit(down_dev);
2609
2610 if (down_dev != up_dev)
2611 /* We switch to a different core, so we put PHY into
2612 * RESET on the old core. */
2613 b43legacy_put_phy_into_reset(down_dev);
2614
2615 /* Now start the new core. */
2616 up_dev->phy.gmode = gmode;
2617 if (prev_status >= B43legacy_STAT_INITIALIZED) {
2618 err = b43legacy_wireless_core_init(up_dev);
2619 if (err) {
2620 b43legacyerr(wl, "Fatal: Could not initialize device"
2621 " for newly selected %s-PHY mode\n",
2622 phymode_to_string(new_mode));
2623 goto init_failure;
2624 }
2625 }
2626 if (prev_status >= B43legacy_STAT_STARTED) {
2627 err = b43legacy_wireless_core_start(up_dev);
2628 if (err) {
Anatol Pomozov02b7d832012-06-23 15:54:34 -07002629 b43legacyerr(wl, "Fatal: Could not start device for "
Larry Finger75388ac2007-09-25 16:46:54 -07002630 "newly selected %s-PHY mode\n",
2631 phymode_to_string(new_mode));
2632 b43legacy_wireless_core_exit(up_dev);
2633 goto init_failure;
2634 }
2635 }
2636 B43legacy_WARN_ON(b43legacy_status(up_dev) != prev_status);
2637
2638 b43legacy_shm_write32(up_dev, B43legacy_SHM_SHARED, 0x003E, 0);
2639
2640 wl->current_dev = up_dev;
2641
2642 return 0;
2643init_failure:
2644 /* Whoops, failed to init the new core. No core is operating now. */
2645 wl->current_dev = NULL;
2646 return err;
2647}
2648
Johannes Berg9124b072008-10-14 19:17:54 +02002649/* Write the short and long frame retry limit values. */
2650static void b43legacy_set_retry_limits(struct b43legacy_wldev *dev,
2651 unsigned int short_retry,
2652 unsigned int long_retry)
2653{
2654 /* The retry limit is a 4-bit counter. Enforce this to avoid overflowing
2655 * the chip-internal counter. */
2656 short_retry = min(short_retry, (unsigned int)0xF);
2657 long_retry = min(long_retry, (unsigned int)0xF);
2658
2659 b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS, 0x0006, short_retry);
2660 b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS, 0x0007, long_retry);
2661}
2662
Stefano Brivio33a3dc932007-11-06 22:48:25 +01002663static int b43legacy_op_dev_config(struct ieee80211_hw *hw,
Johannes Berge8975582008-10-09 12:18:51 +02002664 u32 changed)
Larry Finger75388ac2007-09-25 16:46:54 -07002665{
2666 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
2667 struct b43legacy_wldev *dev;
2668 struct b43legacy_phy *phy;
Johannes Berge8975582008-10-09 12:18:51 +02002669 struct ieee80211_conf *conf = &hw->conf;
Larry Finger75388ac2007-09-25 16:46:54 -07002670 unsigned long flags;
2671 unsigned int new_phymode = 0xFFFF;
2672 int antenna_tx;
Larry Finger75388ac2007-09-25 16:46:54 -07002673 int err = 0;
Larry Finger75388ac2007-09-25 16:46:54 -07002674
Johannes Berg0f4ac382008-10-09 12:18:04 +02002675 antenna_tx = B43legacy_ANTENNA_DEFAULT;
Larry Finger75388ac2007-09-25 16:46:54 -07002676
2677 mutex_lock(&wl->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01002678 dev = wl->current_dev;
2679 phy = &dev->phy;
Larry Finger75388ac2007-09-25 16:46:54 -07002680
Johannes Berg9124b072008-10-14 19:17:54 +02002681 if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
2682 b43legacy_set_retry_limits(dev,
2683 conf->short_frame_max_tx_count,
2684 conf->long_frame_max_tx_count);
2685 changed &= ~IEEE80211_CONF_CHANGE_RETRY_LIMITS;
2686 if (!changed)
2687 goto out_unlock_mutex;
2688
Larry Finger75388ac2007-09-25 16:46:54 -07002689 /* Switch the PHY mode (if necessary). */
Karl Beldan675a0b02013-03-25 16:26:57 +01002690 switch (conf->chandef.chan->band) {
Johannes Berg57fbcce2016-04-12 15:56:15 +02002691 case NL80211_BAND_2GHZ:
Johannes Berg8318d782008-01-24 19:38:38 +01002692 if (phy->type == B43legacy_PHYTYPE_B)
2693 new_phymode = B43legacy_PHYMODE_B;
2694 else
2695 new_phymode = B43legacy_PHYMODE_G;
Larry Finger75388ac2007-09-25 16:46:54 -07002696 break;
2697 default:
2698 B43legacy_WARN_ON(1);
2699 }
2700 err = b43legacy_switch_phymode(wl, new_phymode);
2701 if (err)
2702 goto out_unlock_mutex;
Larry Finger75388ac2007-09-25 16:46:54 -07002703
2704 /* Disable IRQs while reconfiguring the device.
2705 * This makes it possible to drop the spinlock throughout
2706 * the reconfiguration process. */
2707 spin_lock_irqsave(&wl->irq_lock, flags);
2708 if (b43legacy_status(dev) < B43legacy_STAT_STARTED) {
2709 spin_unlock_irqrestore(&wl->irq_lock, flags);
2710 goto out_unlock_mutex;
2711 }
Stefano Brivio44710bb2009-05-15 15:39:20 -05002712 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, 0);
Larry Finger75388ac2007-09-25 16:46:54 -07002713 spin_unlock_irqrestore(&wl->irq_lock, flags);
2714 b43legacy_synchronize_irq(dev);
2715
2716 /* Switch to the requested channel.
2717 * The firmware takes care of races with the TX handler. */
Karl Beldan675a0b02013-03-25 16:26:57 +01002718 if (conf->chandef.chan->hw_value != phy->channel)
2719 b43legacy_radio_selectchannel(dev, conf->chandef.chan->hw_value,
2720 0);
Larry Finger75388ac2007-09-25 16:46:54 -07002721
Johannes Berg0869aea02009-10-28 10:03:35 +01002722 dev->wl->radiotap_enabled = !!(conf->flags & IEEE80211_CONF_MONITOR);
Johannes Berg5be3bda2007-11-24 21:11:09 +01002723
Larry Finger75388ac2007-09-25 16:46:54 -07002724 /* Adjust the desired TX power level. */
2725 if (conf->power_level != 0) {
2726 if (conf->power_level != phy->power_level) {
2727 phy->power_level = conf->power_level;
2728 b43legacy_phy_xmitpower(dev);
2729 }
2730 }
2731
2732 /* Antennas for RX and management frame TX. */
2733 b43legacy_mgmtframe_txantenna(dev, antenna_tx);
2734
Larry Fingerfd4973c2009-06-20 12:58:11 -05002735 if (wl->radio_enabled != phy->radio_on) {
2736 if (wl->radio_enabled) {
Larry Finger42a91742007-09-20 21:11:02 -05002737 b43legacy_radio_turn_on(dev);
2738 b43legacyinfo(dev->wl, "Radio turned on by software\n");
2739 if (!dev->radio_hw_enable)
2740 b43legacyinfo(dev->wl, "The hardware RF-kill"
2741 " button still turns the radio"
2742 " physically off. Press the"
2743 " button to turn it on.\n");
2744 } else {
Larry Finger93bb7f32007-10-10 22:44:22 -05002745 b43legacy_radio_turn_off(dev, 0);
Larry Finger42a91742007-09-20 21:11:02 -05002746 b43legacyinfo(dev->wl, "Radio turned off by"
2747 " software\n");
2748 }
2749 }
2750
Larry Finger75388ac2007-09-25 16:46:54 -07002751 spin_lock_irqsave(&wl->irq_lock, flags);
Stefano Brivio44710bb2009-05-15 15:39:20 -05002752 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, dev->irq_mask);
Larry Finger75388ac2007-09-25 16:46:54 -07002753 spin_unlock_irqrestore(&wl->irq_lock, flags);
2754out_unlock_mutex:
2755 mutex_unlock(&wl->mutex);
2756
2757 return err;
2758}
2759
Johannes Berg881d9482009-01-21 15:13:48 +01002760static void b43legacy_update_basic_rates(struct b43legacy_wldev *dev, u32 brates)
Johannes Berg7f3704e2008-11-06 15:18:11 +01002761{
2762 struct ieee80211_supported_band *sband =
Johannes Berg57fbcce2016-04-12 15:56:15 +02002763 dev->wl->hw->wiphy->bands[NL80211_BAND_2GHZ];
Johannes Berg7f3704e2008-11-06 15:18:11 +01002764 struct ieee80211_rate *rate;
2765 int i;
2766 u16 basic, direct, offset, basic_offset, rateptr;
2767
2768 for (i = 0; i < sband->n_bitrates; i++) {
2769 rate = &sband->bitrates[i];
2770
2771 if (b43legacy_is_cck_rate(rate->hw_value)) {
2772 direct = B43legacy_SHM_SH_CCKDIRECT;
2773 basic = B43legacy_SHM_SH_CCKBASIC;
2774 offset = b43legacy_plcp_get_ratecode_cck(rate->hw_value);
2775 offset &= 0xF;
2776 } else {
2777 direct = B43legacy_SHM_SH_OFDMDIRECT;
2778 basic = B43legacy_SHM_SH_OFDMBASIC;
2779 offset = b43legacy_plcp_get_ratecode_ofdm(rate->hw_value);
2780 offset &= 0xF;
2781 }
2782
2783 rate = ieee80211_get_response_rate(sband, brates, rate->bitrate);
2784
2785 if (b43legacy_is_cck_rate(rate->hw_value)) {
2786 basic_offset = b43legacy_plcp_get_ratecode_cck(rate->hw_value);
2787 basic_offset &= 0xF;
2788 } else {
2789 basic_offset = b43legacy_plcp_get_ratecode_ofdm(rate->hw_value);
2790 basic_offset &= 0xF;
2791 }
2792
2793 /*
2794 * Get the pointer that we need to point to
2795 * from the direct map
2796 */
2797 rateptr = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
2798 direct + 2 * basic_offset);
2799 /* and write it to the basic map */
2800 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
2801 basic + 2 * offset, rateptr);
2802 }
2803}
2804
2805static void b43legacy_op_bss_info_changed(struct ieee80211_hw *hw,
2806 struct ieee80211_vif *vif,
2807 struct ieee80211_bss_conf *conf,
2808 u32 changed)
2809{
2810 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
2811 struct b43legacy_wldev *dev;
Johannes Berg7f3704e2008-11-06 15:18:11 +01002812 unsigned long flags;
Johannes Berg7f3704e2008-11-06 15:18:11 +01002813
2814 mutex_lock(&wl->mutex);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02002815 B43legacy_WARN_ON(wl->vif != vif);
Johannes Berg7f3704e2008-11-06 15:18:11 +01002816
2817 dev = wl->current_dev;
Johannes Berg7f3704e2008-11-06 15:18:11 +01002818
2819 /* Disable IRQs while reconfiguring the device.
2820 * This makes it possible to drop the spinlock throughout
2821 * the reconfiguration process. */
2822 spin_lock_irqsave(&wl->irq_lock, flags);
2823 if (b43legacy_status(dev) < B43legacy_STAT_STARTED) {
2824 spin_unlock_irqrestore(&wl->irq_lock, flags);
2825 goto out_unlock_mutex;
2826 }
Stefano Brivio44710bb2009-05-15 15:39:20 -05002827 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, 0);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02002828
2829 if (changed & BSS_CHANGED_BSSID) {
Johannes Berg2d0ddec2009-04-23 16:13:26 +02002830 b43legacy_synchronize_irq(dev);
2831
2832 if (conf->bssid)
2833 memcpy(wl->bssid, conf->bssid, ETH_ALEN);
2834 else
Joe Perches93803b32015-03-02 19:54:49 -08002835 eth_zero_addr(wl->bssid);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02002836 }
Johannes Berg7f3704e2008-11-06 15:18:11 +01002837
Johannes Berg3f0d8432009-05-18 10:53:18 +02002838 if (b43legacy_status(dev) >= B43legacy_STAT_INITIALIZED) {
2839 if (changed & BSS_CHANGED_BEACON &&
2840 (b43legacy_is_mode(wl, NL80211_IFTYPE_AP) ||
2841 b43legacy_is_mode(wl, NL80211_IFTYPE_ADHOC)))
2842 b43legacy_update_templates(wl);
2843
2844 if (changed & BSS_CHANGED_BSSID)
2845 b43legacy_write_mac_bssid_templates(dev);
2846 }
2847 spin_unlock_irqrestore(&wl->irq_lock, flags);
2848
Johannes Berg7f3704e2008-11-06 15:18:11 +01002849 b43legacy_mac_suspend(dev);
2850
Johannes Berg57c4d7b2009-04-23 16:10:04 +02002851 if (changed & BSS_CHANGED_BEACON_INT &&
2852 (b43legacy_is_mode(wl, NL80211_IFTYPE_AP) ||
2853 b43legacy_is_mode(wl, NL80211_IFTYPE_ADHOC)))
2854 b43legacy_set_beacon_int(dev, conf->beacon_int);
2855
Johannes Berg7f3704e2008-11-06 15:18:11 +01002856 if (changed & BSS_CHANGED_BASIC_RATES)
2857 b43legacy_update_basic_rates(dev, conf->basic_rates);
2858
2859 if (changed & BSS_CHANGED_ERP_SLOT) {
2860 if (conf->use_short_slot)
2861 b43legacy_short_slot_timing_enable(dev);
2862 else
2863 b43legacy_short_slot_timing_disable(dev);
2864 }
2865
2866 b43legacy_mac_enable(dev);
2867
2868 spin_lock_irqsave(&wl->irq_lock, flags);
Stefano Brivio44710bb2009-05-15 15:39:20 -05002869 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, dev->irq_mask);
Johannes Berg7f3704e2008-11-06 15:18:11 +01002870 /* XXX: why? */
Johannes Berg7f3704e2008-11-06 15:18:11 +01002871 spin_unlock_irqrestore(&wl->irq_lock, flags);
2872 out_unlock_mutex:
2873 mutex_unlock(&wl->mutex);
Johannes Berg7f3704e2008-11-06 15:18:11 +01002874}
2875
Stefano Brivio33a3dc932007-11-06 22:48:25 +01002876static void b43legacy_op_configure_filter(struct ieee80211_hw *hw,
2877 unsigned int changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02002878 unsigned int *fflags,u64 multicast)
Larry Finger75388ac2007-09-25 16:46:54 -07002879{
2880 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
2881 struct b43legacy_wldev *dev = wl->current_dev;
2882 unsigned long flags;
2883
Johannes Berg4150c572007-09-17 01:29:23 -04002884 if (!dev) {
2885 *fflags = 0;
Larry Finger75388ac2007-09-25 16:46:54 -07002886 return;
Larry Finger75388ac2007-09-25 16:46:54 -07002887 }
Johannes Berg4150c572007-09-17 01:29:23 -04002888
2889 spin_lock_irqsave(&wl->irq_lock, flags);
Johannes Bergdf140462015-04-22 14:40:58 +02002890 *fflags &= FIF_ALLMULTI |
Johannes Berg4150c572007-09-17 01:29:23 -04002891 FIF_FCSFAIL |
2892 FIF_PLCPFAIL |
2893 FIF_CONTROL |
2894 FIF_OTHER_BSS |
2895 FIF_BCN_PRBRESP_PROMISC;
2896
Johannes Bergdf140462015-04-22 14:40:58 +02002897 changed &= FIF_ALLMULTI |
Johannes Berg4150c572007-09-17 01:29:23 -04002898 FIF_FCSFAIL |
2899 FIF_PLCPFAIL |
2900 FIF_CONTROL |
2901 FIF_OTHER_BSS |
2902 FIF_BCN_PRBRESP_PROMISC;
2903
2904 wl->filter_flags = *fflags;
2905
2906 if (changed && b43legacy_status(dev) >= B43legacy_STAT_INITIALIZED)
2907 b43legacy_adjust_opmode(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07002908 spin_unlock_irqrestore(&wl->irq_lock, flags);
2909}
2910
Larry Finger75388ac2007-09-25 16:46:54 -07002911/* Locking: wl->mutex */
2912static void b43legacy_wireless_core_stop(struct b43legacy_wldev *dev)
2913{
2914 struct b43legacy_wl *wl = dev->wl;
2915 unsigned long flags;
Larry Finger5d07a3d2011-12-21 18:47:59 -06002916 int queue_num;
Larry Finger75388ac2007-09-25 16:46:54 -07002917
2918 if (b43legacy_status(dev) < B43legacy_STAT_STARTED)
2919 return;
Stefano Brivio440cb582007-11-07 18:33:37 +01002920
2921 /* Disable and sync interrupts. We must do this before than
2922 * setting the status to INITIALIZED, as the interrupt handler
2923 * won't care about IRQs then. */
2924 spin_lock_irqsave(&wl->irq_lock, flags);
Stefano Brivio44710bb2009-05-15 15:39:20 -05002925 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, 0);
Stefano Brivio440cb582007-11-07 18:33:37 +01002926 b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_MASK); /* flush */
2927 spin_unlock_irqrestore(&wl->irq_lock, flags);
2928 b43legacy_synchronize_irq(dev);
2929
Larry Finger75388ac2007-09-25 16:46:54 -07002930 b43legacy_set_status(dev, B43legacy_STAT_INITIALIZED);
2931
2932 mutex_unlock(&wl->mutex);
2933 /* Must unlock as it would otherwise deadlock. No races here.
2934 * Cancel the possibly running self-rearming periodic work. */
2935 cancel_delayed_work_sync(&dev->periodic_work);
Larry Finger5d07a3d2011-12-21 18:47:59 -06002936 cancel_work_sync(&wl->tx_work);
Larry Finger75388ac2007-09-25 16:46:54 -07002937 mutex_lock(&wl->mutex);
2938
Larry Finger5d07a3d2011-12-21 18:47:59 -06002939 /* Drain all TX queues. */
2940 for (queue_num = 0; queue_num < B43legacy_QOS_QUEUE_NUM; queue_num++) {
2941 while (skb_queue_len(&wl->tx_queue[queue_num]))
2942 dev_kfree_skb(skb_dequeue(&wl->tx_queue[queue_num]));
2943 }
Larry Finger75388ac2007-09-25 16:46:54 -07002944
Larry Finger5d07a3d2011-12-21 18:47:59 -06002945b43legacy_mac_suspend(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07002946 free_irq(dev->dev->irq, dev);
2947 b43legacydbg(wl, "Wireless interface stopped\n");
2948}
2949
2950/* Locking: wl->mutex */
2951static int b43legacy_wireless_core_start(struct b43legacy_wldev *dev)
2952{
2953 int err;
2954
2955 B43legacy_WARN_ON(b43legacy_status(dev) != B43legacy_STAT_INITIALIZED);
2956
2957 drain_txstatus_queue(dev);
2958 err = request_irq(dev->dev->irq, b43legacy_interrupt_handler,
2959 IRQF_SHARED, KBUILD_MODNAME, dev);
2960 if (err) {
2961 b43legacyerr(dev->wl, "Cannot request IRQ-%d\n",
2962 dev->dev->irq);
2963 goto out;
2964 }
2965 /* We are ready to run. */
Larry Finger0866b032010-02-03 13:33:44 -06002966 ieee80211_wake_queues(dev->wl->hw);
Larry Finger75388ac2007-09-25 16:46:54 -07002967 b43legacy_set_status(dev, B43legacy_STAT_STARTED);
2968
2969 /* Start data flow (TX/RX) */
2970 b43legacy_mac_enable(dev);
Stefano Brivio44710bb2009-05-15 15:39:20 -05002971 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, dev->irq_mask);
Larry Finger75388ac2007-09-25 16:46:54 -07002972
2973 /* Start maintenance work */
2974 b43legacy_periodic_tasks_setup(dev);
2975
2976 b43legacydbg(dev->wl, "Wireless interface started\n");
2977out:
2978 return err;
2979}
2980
2981/* Get PHY and RADIO versioning numbers */
2982static int b43legacy_phy_versioning(struct b43legacy_wldev *dev)
2983{
2984 struct b43legacy_phy *phy = &dev->phy;
2985 u32 tmp;
2986 u8 analog_type;
2987 u8 phy_type;
2988 u8 phy_rev;
2989 u16 radio_manuf;
2990 u16 radio_ver;
2991 u16 radio_rev;
2992 int unsupported = 0;
2993
2994 /* Get PHY versioning */
2995 tmp = b43legacy_read16(dev, B43legacy_MMIO_PHY_VER);
2996 analog_type = (tmp & B43legacy_PHYVER_ANALOG)
2997 >> B43legacy_PHYVER_ANALOG_SHIFT;
2998 phy_type = (tmp & B43legacy_PHYVER_TYPE) >> B43legacy_PHYVER_TYPE_SHIFT;
2999 phy_rev = (tmp & B43legacy_PHYVER_VERSION);
3000 switch (phy_type) {
3001 case B43legacy_PHYTYPE_B:
3002 if (phy_rev != 2 && phy_rev != 4
3003 && phy_rev != 6 && phy_rev != 7)
3004 unsupported = 1;
3005 break;
3006 case B43legacy_PHYTYPE_G:
3007 if (phy_rev > 8)
3008 unsupported = 1;
3009 break;
3010 default:
3011 unsupported = 1;
Joe Perches6403eab2011-06-03 11:51:20 +00003012 }
Larry Finger75388ac2007-09-25 16:46:54 -07003013 if (unsupported) {
3014 b43legacyerr(dev->wl, "FOUND UNSUPPORTED PHY "
3015 "(Analog %u, Type %u, Revision %u)\n",
3016 analog_type, phy_type, phy_rev);
3017 return -EOPNOTSUPP;
3018 }
3019 b43legacydbg(dev->wl, "Found PHY: Analog %u, Type %u, Revision %u\n",
3020 analog_type, phy_type, phy_rev);
3021
3022
3023 /* Get RADIO versioning */
3024 if (dev->dev->bus->chip_id == 0x4317) {
3025 if (dev->dev->bus->chip_rev == 0)
3026 tmp = 0x3205017F;
3027 else if (dev->dev->bus->chip_rev == 1)
3028 tmp = 0x4205017F;
3029 else
3030 tmp = 0x5205017F;
3031 } else {
3032 b43legacy_write16(dev, B43legacy_MMIO_RADIO_CONTROL,
3033 B43legacy_RADIOCTL_ID);
3034 tmp = b43legacy_read16(dev, B43legacy_MMIO_RADIO_DATA_HIGH);
3035 tmp <<= 16;
3036 b43legacy_write16(dev, B43legacy_MMIO_RADIO_CONTROL,
3037 B43legacy_RADIOCTL_ID);
3038 tmp |= b43legacy_read16(dev, B43legacy_MMIO_RADIO_DATA_LOW);
3039 }
3040 radio_manuf = (tmp & 0x00000FFF);
3041 radio_ver = (tmp & 0x0FFFF000) >> 12;
3042 radio_rev = (tmp & 0xF0000000) >> 28;
3043 switch (phy_type) {
3044 case B43legacy_PHYTYPE_B:
3045 if ((radio_ver & 0xFFF0) != 0x2050)
3046 unsupported = 1;
3047 break;
3048 case B43legacy_PHYTYPE_G:
3049 if (radio_ver != 0x2050)
3050 unsupported = 1;
3051 break;
3052 default:
3053 B43legacy_BUG_ON(1);
3054 }
3055 if (unsupported) {
3056 b43legacyerr(dev->wl, "FOUND UNSUPPORTED RADIO "
3057 "(Manuf 0x%X, Version 0x%X, Revision %u)\n",
3058 radio_manuf, radio_ver, radio_rev);
3059 return -EOPNOTSUPP;
3060 }
3061 b43legacydbg(dev->wl, "Found Radio: Manuf 0x%X, Version 0x%X,"
3062 " Revision %u\n", radio_manuf, radio_ver, radio_rev);
3063
3064
3065 phy->radio_manuf = radio_manuf;
3066 phy->radio_ver = radio_ver;
3067 phy->radio_rev = radio_rev;
3068
3069 phy->analog = analog_type;
3070 phy->type = phy_type;
3071 phy->rev = phy_rev;
3072
3073 return 0;
3074}
3075
3076static void setup_struct_phy_for_init(struct b43legacy_wldev *dev,
3077 struct b43legacy_phy *phy)
3078{
3079 struct b43legacy_lopair *lo;
3080 int i;
3081
3082 memset(phy->minlowsig, 0xFF, sizeof(phy->minlowsig));
3083 memset(phy->minlowsigpos, 0, sizeof(phy->minlowsigpos));
3084
Larry Finger1065de12007-09-20 20:10:07 -05003085 /* Assume the radio is enabled. If it's not enabled, the state will
3086 * immediately get fixed on the first periodic work run. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00003087 dev->radio_hw_enable = true;
Larry Finger75388ac2007-09-25 16:46:54 -07003088
3089 phy->savedpctlreg = 0xFFFF;
Rusty Russell3db1cd52011-12-19 13:56:45 +00003090 phy->aci_enable = false;
3091 phy->aci_wlan_automatic = false;
3092 phy->aci_hw_rssi = false;
Larry Finger75388ac2007-09-25 16:46:54 -07003093
3094 lo = phy->_lo_pairs;
3095 if (lo)
3096 memset(lo, 0, sizeof(struct b43legacy_lopair) *
3097 B43legacy_LO_COUNT);
3098 phy->max_lb_gain = 0;
3099 phy->trsw_rx_gain = 0;
3100
3101 /* Set default attenuation values. */
3102 phy->bbatt = b43legacy_default_baseband_attenuation(dev);
3103 phy->rfatt = b43legacy_default_radio_attenuation(dev);
3104 phy->txctl1 = b43legacy_default_txctl1(dev);
3105 phy->txpwr_offset = 0;
3106
3107 /* NRSSI */
3108 phy->nrssislope = 0;
3109 for (i = 0; i < ARRAY_SIZE(phy->nrssi); i++)
3110 phy->nrssi[i] = -1000;
3111 for (i = 0; i < ARRAY_SIZE(phy->nrssi_lt); i++)
3112 phy->nrssi_lt[i] = i;
3113
3114 phy->lofcal = 0xFFFF;
3115 phy->initval = 0xFFFF;
3116
Larry Finger75388ac2007-09-25 16:46:54 -07003117 phy->interfmode = B43legacy_INTERFMODE_NONE;
3118 phy->channel = 0xFF;
3119}
3120
3121static void setup_struct_wldev_for_init(struct b43legacy_wldev *dev)
3122{
3123 /* Flags */
Rusty Russell3db1cd52011-12-19 13:56:45 +00003124 dev->dfq_valid = false;
Larry Finger75388ac2007-09-25 16:46:54 -07003125
3126 /* Stats */
3127 memset(&dev->stats, 0, sizeof(dev->stats));
3128
3129 setup_struct_phy_for_init(dev, &dev->phy);
3130
3131 /* IRQ related flags */
3132 dev->irq_reason = 0;
3133 memset(dev->dma_reason, 0, sizeof(dev->dma_reason));
Stefano Brivio44710bb2009-05-15 15:39:20 -05003134 dev->irq_mask = B43legacy_IRQ_MASKTEMPLATE;
Larry Finger75388ac2007-09-25 16:46:54 -07003135
3136 dev->mac_suspended = 1;
3137
3138 /* Noise calculation context */
3139 memset(&dev->noisecalc, 0, sizeof(dev->noisecalc));
3140}
3141
Stefano Brivio3e2c40e2008-04-14 00:57:03 +02003142static void b43legacy_set_synth_pu_delay(struct b43legacy_wldev *dev,
3143 bool idle) {
3144 u16 pu_delay = 1050;
3145
Johannes Berg05c914f2008-09-11 00:01:58 +02003146 if (b43legacy_is_mode(dev->wl, NL80211_IFTYPE_ADHOC) || idle)
Stefano Brivio3e2c40e2008-04-14 00:57:03 +02003147 pu_delay = 500;
3148 if ((dev->phy.radio_ver == 0x2050) && (dev->phy.radio_rev == 8))
3149 pu_delay = max(pu_delay, (u16)2400);
3150
3151 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
3152 B43legacy_SHM_SH_SPUWKUP, pu_delay);
3153}
3154
3155/* Set the TSF CFP pre-TargetBeaconTransmissionTime. */
3156static void b43legacy_set_pretbtt(struct b43legacy_wldev *dev)
3157{
3158 u16 pretbtt;
3159
3160 /* The time value is in microseconds. */
Johannes Berg05c914f2008-09-11 00:01:58 +02003161 if (b43legacy_is_mode(dev->wl, NL80211_IFTYPE_ADHOC))
Stefano Brivio3e2c40e2008-04-14 00:57:03 +02003162 pretbtt = 2;
3163 else
3164 pretbtt = 250;
3165 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
3166 B43legacy_SHM_SH_PRETBTT, pretbtt);
3167 b43legacy_write16(dev, B43legacy_MMIO_TSF_CFP_PRETBTT, pretbtt);
3168}
3169
Larry Finger75388ac2007-09-25 16:46:54 -07003170/* Shutdown a wireless core */
3171/* Locking: wl->mutex */
3172static void b43legacy_wireless_core_exit(struct b43legacy_wldev *dev)
3173{
Larry Finger75388ac2007-09-25 16:46:54 -07003174 struct b43legacy_phy *phy = &dev->phy;
Stefano Brivioe78c9d22008-01-23 14:48:50 +01003175 u32 macctl;
Larry Finger75388ac2007-09-25 16:46:54 -07003176
3177 B43legacy_WARN_ON(b43legacy_status(dev) > B43legacy_STAT_INITIALIZED);
3178 if (b43legacy_status(dev) != B43legacy_STAT_INITIALIZED)
3179 return;
3180 b43legacy_set_status(dev, B43legacy_STAT_UNINIT);
3181
Stefano Brivioe78c9d22008-01-23 14:48:50 +01003182 /* Stop the microcode PSM. */
3183 macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
3184 macctl &= ~B43legacy_MACCTL_PSM_RUN;
3185 macctl |= B43legacy_MACCTL_PSM_JMP0;
3186 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
3187
Larry Finger4ad36d72007-12-16 19:21:06 +01003188 b43legacy_leds_exit(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07003189 b43legacy_rng_exit(dev->wl);
3190 b43legacy_pio_free(dev);
3191 b43legacy_dma_free(dev);
3192 b43legacy_chip_exit(dev);
Larry Finger93bb7f32007-10-10 22:44:22 -05003193 b43legacy_radio_turn_off(dev, 1);
Larry Finger75388ac2007-09-25 16:46:54 -07003194 b43legacy_switch_analog(dev, 0);
3195 if (phy->dyn_tssi_tbl)
3196 kfree(phy->tssi2dbm);
3197 kfree(phy->lo_control);
3198 phy->lo_control = NULL;
Stefano Brivioa2971702008-02-08 06:31:25 +01003199 if (dev->wl->current_beacon) {
3200 dev_kfree_skb_any(dev->wl->current_beacon);
3201 dev->wl->current_beacon = NULL;
3202 }
3203
Larry Finger75388ac2007-09-25 16:46:54 -07003204 ssb_device_disable(dev->dev, 0);
3205 ssb_bus_may_powerdown(dev->dev->bus);
3206}
3207
3208static void prepare_phy_data_for_init(struct b43legacy_wldev *dev)
3209{
3210 struct b43legacy_phy *phy = &dev->phy;
3211 int i;
3212
3213 /* Set default attenuation values. */
3214 phy->bbatt = b43legacy_default_baseband_attenuation(dev);
3215 phy->rfatt = b43legacy_default_radio_attenuation(dev);
3216 phy->txctl1 = b43legacy_default_txctl1(dev);
3217 phy->txctl2 = 0xFFFF;
3218 phy->txpwr_offset = 0;
3219
3220 /* NRSSI */
3221 phy->nrssislope = 0;
3222 for (i = 0; i < ARRAY_SIZE(phy->nrssi); i++)
3223 phy->nrssi[i] = -1000;
3224 for (i = 0; i < ARRAY_SIZE(phy->nrssi_lt); i++)
3225 phy->nrssi_lt[i] = i;
3226
3227 phy->lofcal = 0xFFFF;
3228 phy->initval = 0xFFFF;
3229
Rusty Russell3db1cd52011-12-19 13:56:45 +00003230 phy->aci_enable = false;
3231 phy->aci_wlan_automatic = false;
3232 phy->aci_hw_rssi = false;
Larry Finger75388ac2007-09-25 16:46:54 -07003233
3234 phy->antenna_diversity = 0xFFFF;
3235 memset(phy->minlowsig, 0xFF, sizeof(phy->minlowsig));
3236 memset(phy->minlowsigpos, 0, sizeof(phy->minlowsigpos));
3237
3238 /* Flags */
3239 phy->calibrated = 0;
Larry Finger75388ac2007-09-25 16:46:54 -07003240
3241 if (phy->_lo_pairs)
3242 memset(phy->_lo_pairs, 0,
3243 sizeof(struct b43legacy_lopair) * B43legacy_LO_COUNT);
3244 memset(phy->loopback_gain, 0, sizeof(phy->loopback_gain));
3245}
3246
3247/* Initialize a wireless core */
3248static int b43legacy_wireless_core_init(struct b43legacy_wldev *dev)
3249{
3250 struct b43legacy_wl *wl = dev->wl;
3251 struct ssb_bus *bus = dev->dev->bus;
3252 struct b43legacy_phy *phy = &dev->phy;
3253 struct ssb_sprom *sprom = &dev->dev->bus->sprom;
3254 int err;
3255 u32 hf;
3256 u32 tmp;
3257
3258 B43legacy_WARN_ON(b43legacy_status(dev) != B43legacy_STAT_UNINIT);
3259
3260 err = ssb_bus_powerup(bus, 0);
3261 if (err)
3262 goto out;
3263 if (!ssb_device_is_enabled(dev->dev)) {
3264 tmp = phy->gmode ? B43legacy_TMSLOW_GMODE : 0;
3265 b43legacy_wireless_core_reset(dev, tmp);
3266 }
3267
3268 if ((phy->type == B43legacy_PHYTYPE_B) ||
3269 (phy->type == B43legacy_PHYTYPE_G)) {
Kees Cook6396bb22018-06-12 14:03:40 -07003270 phy->_lo_pairs = kcalloc(B43legacy_LO_COUNT,
3271 sizeof(struct b43legacy_lopair),
Larry Finger75388ac2007-09-25 16:46:54 -07003272 GFP_KERNEL);
3273 if (!phy->_lo_pairs)
3274 return -ENOMEM;
3275 }
3276 setup_struct_wldev_for_init(dev);
3277
3278 err = b43legacy_phy_init_tssi2dbm_table(dev);
3279 if (err)
3280 goto err_kfree_lo_control;
3281
3282 /* Enable IRQ routing to this device. */
3283 ssb_pcicore_dev_irqvecs_enable(&bus->pcicore, dev->dev);
3284
Larry Finger75388ac2007-09-25 16:46:54 -07003285 prepare_phy_data_for_init(dev);
3286 b43legacy_phy_calibrate(dev);
3287 err = b43legacy_chip_init(dev);
3288 if (err)
3289 goto err_kfree_tssitbl;
3290 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
3291 B43legacy_SHM_SH_WLCOREREV,
3292 dev->dev->id.revision);
3293 hf = b43legacy_hf_read(dev);
3294 if (phy->type == B43legacy_PHYTYPE_G) {
3295 hf |= B43legacy_HF_SYMW;
3296 if (phy->rev == 1)
3297 hf |= B43legacy_HF_GDCW;
Larry Finger7797aa32007-11-09 16:57:34 -06003298 if (sprom->boardflags_lo & B43legacy_BFL_PACTRL)
Larry Finger75388ac2007-09-25 16:46:54 -07003299 hf |= B43legacy_HF_OFDMPABOOST;
3300 } else if (phy->type == B43legacy_PHYTYPE_B) {
3301 hf |= B43legacy_HF_SYMW;
3302 if (phy->rev >= 2 && phy->radio_ver == 0x2050)
3303 hf &= ~B43legacy_HF_GDCW;
3304 }
3305 b43legacy_hf_write(dev, hf);
3306
Stefano Brivio0a6e1be2007-11-06 22:48:12 +01003307 b43legacy_set_retry_limits(dev,
3308 B43legacy_DEFAULT_SHORT_RETRY_LIMIT,
3309 B43legacy_DEFAULT_LONG_RETRY_LIMIT);
Larry Finger75388ac2007-09-25 16:46:54 -07003310
3311 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
3312 0x0044, 3);
3313 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
3314 0x0046, 2);
3315
3316 /* Disable sending probe responses from firmware.
3317 * Setting the MaxTime to one usec will always trigger
3318 * a timeout, so we never send any probe resp.
3319 * A timeout of zero is infinite. */
3320 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
3321 B43legacy_SHM_SH_PRMAXTIME, 1);
3322
3323 b43legacy_rate_memory_init(dev);
3324
3325 /* Minimum Contention Window */
3326 if (phy->type == B43legacy_PHYTYPE_B)
3327 b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS,
3328 0x0003, 31);
3329 else
3330 b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS,
3331 0x0003, 15);
3332 /* Maximum Contention Window */
3333 b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS,
3334 0x0004, 1023);
3335
3336 do {
3337 if (b43legacy_using_pio(dev))
3338 err = b43legacy_pio_init(dev);
3339 else {
3340 err = b43legacy_dma_init(dev);
3341 if (!err)
3342 b43legacy_qos_init(dev);
3343 }
3344 } while (err == -EAGAIN);
3345 if (err)
3346 goto err_chip_exit;
3347
Stefano Brivio3e2c40e2008-04-14 00:57:03 +02003348 b43legacy_set_synth_pu_delay(dev, 1);
Larry Finger75388ac2007-09-25 16:46:54 -07003349
3350 ssb_bus_powerup(bus, 1); /* Enable dynamic PCTL */
Johannes Berg4150c572007-09-17 01:29:23 -04003351 b43legacy_upload_card_macaddress(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07003352 b43legacy_security_init(dev);
3353 b43legacy_rng_init(wl);
3354
Larry Finger0866b032010-02-03 13:33:44 -06003355 ieee80211_wake_queues(dev->wl->hw);
Larry Finger75388ac2007-09-25 16:46:54 -07003356 b43legacy_set_status(dev, B43legacy_STAT_INITIALIZED);
3357
Larry Finger4ad36d72007-12-16 19:21:06 +01003358 b43legacy_leds_init(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07003359out:
3360 return err;
3361
3362err_chip_exit:
3363 b43legacy_chip_exit(dev);
3364err_kfree_tssitbl:
3365 if (phy->dyn_tssi_tbl)
3366 kfree(phy->tssi2dbm);
3367err_kfree_lo_control:
3368 kfree(phy->lo_control);
3369 phy->lo_control = NULL;
3370 ssb_bus_may_powerdown(bus);
3371 B43legacy_WARN_ON(b43legacy_status(dev) != B43legacy_STAT_UNINIT);
3372 return err;
3373}
3374
Stefano Brivio33a3dc932007-11-06 22:48:25 +01003375static int b43legacy_op_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01003376 struct ieee80211_vif *vif)
Larry Finger75388ac2007-09-25 16:46:54 -07003377{
3378 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
3379 struct b43legacy_wldev *dev;
3380 unsigned long flags;
3381 int err = -EOPNOTSUPP;
Johannes Berg4150c572007-09-17 01:29:23 -04003382
3383 /* TODO: allow WDS/AP devices to coexist */
3384
Johannes Berg1ed32e42009-12-23 13:15:45 +01003385 if (vif->type != NL80211_IFTYPE_AP &&
3386 vif->type != NL80211_IFTYPE_STATION &&
3387 vif->type != NL80211_IFTYPE_WDS &&
3388 vif->type != NL80211_IFTYPE_ADHOC)
Johannes Berg4150c572007-09-17 01:29:23 -04003389 return -EOPNOTSUPP;
Larry Finger75388ac2007-09-25 16:46:54 -07003390
3391 mutex_lock(&wl->mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04003392 if (wl->operating)
Larry Finger75388ac2007-09-25 16:46:54 -07003393 goto out_mutex_unlock;
3394
Johannes Berg1ed32e42009-12-23 13:15:45 +01003395 b43legacydbg(wl, "Adding Interface type %d\n", vif->type);
Larry Finger75388ac2007-09-25 16:46:54 -07003396
3397 dev = wl->current_dev;
Rusty Russell3db1cd52011-12-19 13:56:45 +00003398 wl->operating = true;
Johannes Berg1ed32e42009-12-23 13:15:45 +01003399 wl->vif = vif;
3400 wl->if_type = vif->type;
3401 memcpy(wl->mac_addr, vif->addr, ETH_ALEN);
Larry Finger75388ac2007-09-25 16:46:54 -07003402
3403 spin_lock_irqsave(&wl->irq_lock, flags);
Larry Finger75388ac2007-09-25 16:46:54 -07003404 b43legacy_adjust_opmode(dev);
Stefano Brivio3e2c40e2008-04-14 00:57:03 +02003405 b43legacy_set_pretbtt(dev);
3406 b43legacy_set_synth_pu_delay(dev, 0);
Johannes Berg4150c572007-09-17 01:29:23 -04003407 b43legacy_upload_card_macaddress(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07003408 spin_unlock_irqrestore(&wl->irq_lock, flags);
3409
3410 err = 0;
Johannes Berg4150c572007-09-17 01:29:23 -04003411 out_mutex_unlock:
Larry Finger75388ac2007-09-25 16:46:54 -07003412 mutex_unlock(&wl->mutex);
3413
3414 return err;
3415}
3416
Stefano Brivio33a3dc932007-11-06 22:48:25 +01003417static void b43legacy_op_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01003418 struct ieee80211_vif *vif)
Larry Finger75388ac2007-09-25 16:46:54 -07003419{
3420 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
Johannes Berg4150c572007-09-17 01:29:23 -04003421 struct b43legacy_wldev *dev = wl->current_dev;
Larry Finger75388ac2007-09-25 16:46:54 -07003422 unsigned long flags;
3423
Johannes Berg1ed32e42009-12-23 13:15:45 +01003424 b43legacydbg(wl, "Removing Interface type %d\n", vif->type);
Larry Finger75388ac2007-09-25 16:46:54 -07003425
3426 mutex_lock(&wl->mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04003427
3428 B43legacy_WARN_ON(!wl->operating);
Johannes Berg1ed32e42009-12-23 13:15:45 +01003429 B43legacy_WARN_ON(wl->vif != vif);
Johannes Berg32bfd352007-12-19 01:31:26 +01003430 wl->vif = NULL;
Johannes Berg4150c572007-09-17 01:29:23 -04003431
Rusty Russell3db1cd52011-12-19 13:56:45 +00003432 wl->operating = false;
Johannes Berg4150c572007-09-17 01:29:23 -04003433
3434 spin_lock_irqsave(&wl->irq_lock, flags);
3435 b43legacy_adjust_opmode(dev);
Joe Perches93803b32015-03-02 19:54:49 -08003436 eth_zero_addr(wl->mac_addr);
Johannes Berg4150c572007-09-17 01:29:23 -04003437 b43legacy_upload_card_macaddress(dev);
3438 spin_unlock_irqrestore(&wl->irq_lock, flags);
3439
3440 mutex_unlock(&wl->mutex);
3441}
3442
Stefano Brivio33a3dc932007-11-06 22:48:25 +01003443static int b43legacy_op_start(struct ieee80211_hw *hw)
Johannes Berg4150c572007-09-17 01:29:23 -04003444{
3445 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
3446 struct b43legacy_wldev *dev = wl->current_dev;
3447 int did_init = 0;
Larry Finger208eec82007-10-14 16:04:30 -05003448 int err = 0;
Larry Finger4ad36d72007-12-16 19:21:06 +01003449
Stefano Brivioada50732008-02-02 19:15:57 +01003450 /* Kill all old instance specific information to make sure
3451 * the card won't use it in the short timeframe between start
3452 * and mac80211 reconfiguring it. */
Joe Perches93803b32015-03-02 19:54:49 -08003453 eth_zero_addr(wl->bssid);
3454 eth_zero_addr(wl->mac_addr);
Stefano Brivioada50732008-02-02 19:15:57 +01003455 wl->filter_flags = 0;
Rusty Russell3db1cd52011-12-19 13:56:45 +00003456 wl->beacon0_uploaded = false;
3457 wl->beacon1_uploaded = false;
3458 wl->beacon_templates_virgin = true;
3459 wl->radio_enabled = true;
Stefano Brivioada50732008-02-02 19:15:57 +01003460
Johannes Berg4150c572007-09-17 01:29:23 -04003461 mutex_lock(&wl->mutex);
3462
3463 if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED) {
3464 err = b43legacy_wireless_core_init(dev);
Johannes Bergf41f3f32009-06-07 12:30:34 -05003465 if (err)
Johannes Berg4150c572007-09-17 01:29:23 -04003466 goto out_mutex_unlock;
3467 did_init = 1;
Larry Finger75388ac2007-09-25 16:46:54 -07003468 }
3469
Johannes Berg4150c572007-09-17 01:29:23 -04003470 if (b43legacy_status(dev) < B43legacy_STAT_STARTED) {
3471 err = b43legacy_wireless_core_start(dev);
3472 if (err) {
3473 if (did_init)
3474 b43legacy_wireless_core_exit(dev);
3475 goto out_mutex_unlock;
3476 }
Larry Finger75388ac2007-09-25 16:46:54 -07003477 }
Johannes Berg4150c572007-09-17 01:29:23 -04003478
Johannes Bergf41f3f32009-06-07 12:30:34 -05003479 wiphy_rfkill_start_polling(hw->wiphy);
3480
Johannes Berg4150c572007-09-17 01:29:23 -04003481out_mutex_unlock:
3482 mutex_unlock(&wl->mutex);
3483
3484 return err;
3485}
3486
Stefano Brivio33a3dc932007-11-06 22:48:25 +01003487static void b43legacy_op_stop(struct ieee80211_hw *hw)
Johannes Berg4150c572007-09-17 01:29:23 -04003488{
3489 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
3490 struct b43legacy_wldev *dev = wl->current_dev;
3491
Larry Finger7858e072009-04-11 11:25:24 -05003492 cancel_work_sync(&(wl->beacon_update_trigger));
Larry Finger4ad36d72007-12-16 19:21:06 +01003493
Johannes Berg4150c572007-09-17 01:29:23 -04003494 mutex_lock(&wl->mutex);
3495 if (b43legacy_status(dev) >= B43legacy_STAT_STARTED)
3496 b43legacy_wireless_core_stop(dev);
3497 b43legacy_wireless_core_exit(dev);
Rusty Russell3db1cd52011-12-19 13:56:45 +00003498 wl->radio_enabled = false;
Larry Finger75388ac2007-09-25 16:46:54 -07003499 mutex_unlock(&wl->mutex);
3500}
3501
Stefano Brivioa2971702008-02-08 06:31:25 +01003502static int b43legacy_op_beacon_set_tim(struct ieee80211_hw *hw,
Johannes Berg17741cd2008-09-11 00:02:02 +02003503 struct ieee80211_sta *sta, bool set)
Stefano Brivioa2971702008-02-08 06:31:25 +01003504{
3505 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
Stefano Brivioa2971702008-02-08 06:31:25 +01003506 unsigned long flags;
3507
3508 spin_lock_irqsave(&wl->irq_lock, flags);
Johannes Berg9d139c82008-07-09 14:40:37 +02003509 b43legacy_update_templates(wl);
Stefano Brivioa2971702008-02-08 06:31:25 +01003510 spin_unlock_irqrestore(&wl->irq_lock, flags);
3511
3512 return 0;
3513}
3514
John W. Linvillec7ab1a42010-04-29 15:56:25 -04003515static int b43legacy_op_get_survey(struct ieee80211_hw *hw, int idx,
3516 struct survey_info *survey)
3517{
3518 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
3519 struct b43legacy_wldev *dev = wl->current_dev;
3520 struct ieee80211_conf *conf = &hw->conf;
3521
3522 if (idx != 0)
3523 return -ENOENT;
3524
Karl Beldan675a0b02013-03-25 16:26:57 +01003525 survey->channel = conf->chandef.chan;
John W. Linvillec7ab1a42010-04-29 15:56:25 -04003526 survey->filled = SURVEY_INFO_NOISE_DBM;
3527 survey->noise = dev->stats.link_noise;
3528
3529 return 0;
3530}
3531
Larry Finger75388ac2007-09-25 16:46:54 -07003532static const struct ieee80211_ops b43legacy_hw_ops = {
Stefano Brivio33a3dc932007-11-06 22:48:25 +01003533 .tx = b43legacy_op_tx,
3534 .conf_tx = b43legacy_op_conf_tx,
3535 .add_interface = b43legacy_op_add_interface,
3536 .remove_interface = b43legacy_op_remove_interface,
3537 .config = b43legacy_op_dev_config,
Johannes Berg7f3704e2008-11-06 15:18:11 +01003538 .bss_info_changed = b43legacy_op_bss_info_changed,
Stefano Brivio33a3dc932007-11-06 22:48:25 +01003539 .configure_filter = b43legacy_op_configure_filter,
3540 .get_stats = b43legacy_op_get_stats,
Stefano Brivio33a3dc932007-11-06 22:48:25 +01003541 .start = b43legacy_op_start,
3542 .stop = b43legacy_op_stop,
Stefano Brivioa2971702008-02-08 06:31:25 +01003543 .set_tim = b43legacy_op_beacon_set_tim,
John W. Linvillec7ab1a42010-04-29 15:56:25 -04003544 .get_survey = b43legacy_op_get_survey,
Johannes Bergf41f3f32009-06-07 12:30:34 -05003545 .rfkill_poll = b43legacy_rfkill_poll,
Larry Finger75388ac2007-09-25 16:46:54 -07003546};
3547
3548/* Hard-reset the chip. Do not call this directly.
3549 * Use b43legacy_controller_restart()
3550 */
3551static void b43legacy_chip_reset(struct work_struct *work)
3552{
3553 struct b43legacy_wldev *dev =
3554 container_of(work, struct b43legacy_wldev, restart_work);
3555 struct b43legacy_wl *wl = dev->wl;
3556 int err = 0;
3557 int prev_status;
3558
3559 mutex_lock(&wl->mutex);
3560
3561 prev_status = b43legacy_status(dev);
3562 /* Bring the device down... */
3563 if (prev_status >= B43legacy_STAT_STARTED)
3564 b43legacy_wireless_core_stop(dev);
3565 if (prev_status >= B43legacy_STAT_INITIALIZED)
3566 b43legacy_wireless_core_exit(dev);
3567
3568 /* ...and up again. */
3569 if (prev_status >= B43legacy_STAT_INITIALIZED) {
3570 err = b43legacy_wireless_core_init(dev);
3571 if (err)
3572 goto out;
3573 }
3574 if (prev_status >= B43legacy_STAT_STARTED) {
3575 err = b43legacy_wireless_core_start(dev);
3576 if (err) {
3577 b43legacy_wireless_core_exit(dev);
3578 goto out;
3579 }
3580 }
3581out:
Michael Buesch48e6c512008-05-22 17:06:36 +02003582 if (err)
3583 wl->current_dev = NULL; /* Failed to init the dev. */
Larry Finger75388ac2007-09-25 16:46:54 -07003584 mutex_unlock(&wl->mutex);
3585 if (err)
3586 b43legacyerr(wl, "Controller restart FAILED\n");
3587 else
3588 b43legacyinfo(wl, "Controller restarted\n");
3589}
3590
3591static int b43legacy_setup_modes(struct b43legacy_wldev *dev,
3592 int have_bphy,
3593 int have_gphy)
3594{
3595 struct ieee80211_hw *hw = dev->wl->hw;
Larry Finger75388ac2007-09-25 16:46:54 -07003596 struct b43legacy_phy *phy = &dev->phy;
Larry Finger75388ac2007-09-25 16:46:54 -07003597
3598 phy->possible_phymodes = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003599 if (have_bphy) {
Johannes Berg57fbcce2016-04-12 15:56:15 +02003600 hw->wiphy->bands[NL80211_BAND_2GHZ] =
Johannes Berg8318d782008-01-24 19:38:38 +01003601 &b43legacy_band_2GHz_BPHY;
3602 phy->possible_phymodes |= B43legacy_PHYMODE_B;
3603 }
Larry Finger75388ac2007-09-25 16:46:54 -07003604
Johannes Berg8318d782008-01-24 19:38:38 +01003605 if (have_gphy) {
Johannes Berg57fbcce2016-04-12 15:56:15 +02003606 hw->wiphy->bands[NL80211_BAND_2GHZ] =
Johannes Berg8318d782008-01-24 19:38:38 +01003607 &b43legacy_band_2GHz_GPHY;
3608 phy->possible_phymodes |= B43legacy_PHYMODE_G;
Larry Finger75388ac2007-09-25 16:46:54 -07003609 }
3610
3611 return 0;
3612}
3613
3614static void b43legacy_wireless_core_detach(struct b43legacy_wldev *dev)
3615{
3616 /* We release firmware that late to not be required to re-request
3617 * is all the time when we reinit the core. */
3618 b43legacy_release_firmware(dev);
3619}
3620
3621static int b43legacy_wireless_core_attach(struct b43legacy_wldev *dev)
3622{
3623 struct b43legacy_wl *wl = dev->wl;
3624 struct ssb_bus *bus = dev->dev->bus;
Michael Buesch899110f2009-10-09 20:30:10 +02003625 struct pci_dev *pdev = (bus->bustype == SSB_BUSTYPE_PCI) ? bus->host_pci : NULL;
Larry Finger75388ac2007-09-25 16:46:54 -07003626 int err;
3627 int have_bphy = 0;
3628 int have_gphy = 0;
3629 u32 tmp;
3630
3631 /* Do NOT do any device initialization here.
3632 * Do it in wireless_core_init() instead.
3633 * This function is for gathering basic information about the HW, only.
3634 * Also some structs may be set up here. But most likely you want to
3635 * have that in core_init(), too.
3636 */
3637
3638 err = ssb_bus_powerup(bus, 0);
3639 if (err) {
3640 b43legacyerr(wl, "Bus powerup failed\n");
3641 goto out;
3642 }
3643 /* Get the PHY type. */
3644 if (dev->dev->id.revision >= 5) {
3645 u32 tmshigh;
3646
3647 tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
3648 have_gphy = !!(tmshigh & B43legacy_TMSHIGH_GPHY);
3649 if (!have_gphy)
3650 have_bphy = 1;
3651 } else if (dev->dev->id.revision == 4)
3652 have_gphy = 1;
3653 else
3654 have_bphy = 1;
3655
Larry Finger75388ac2007-09-25 16:46:54 -07003656 dev->phy.gmode = (have_gphy || have_bphy);
Rusty Russell3db1cd52011-12-19 13:56:45 +00003657 dev->phy.radio_on = true;
Larry Finger75388ac2007-09-25 16:46:54 -07003658 tmp = dev->phy.gmode ? B43legacy_TMSLOW_GMODE : 0;
3659 b43legacy_wireless_core_reset(dev, tmp);
3660
3661 err = b43legacy_phy_versioning(dev);
3662 if (err)
Larry Fingerba48f7b2007-10-12 23:04:51 -05003663 goto err_powerdown;
Larry Finger75388ac2007-09-25 16:46:54 -07003664 /* Check if this device supports multiband. */
3665 if (!pdev ||
3666 (pdev->device != 0x4312 &&
3667 pdev->device != 0x4319 &&
3668 pdev->device != 0x4324)) {
3669 /* No multiband support. */
3670 have_bphy = 0;
3671 have_gphy = 0;
3672 switch (dev->phy.type) {
3673 case B43legacy_PHYTYPE_B:
3674 have_bphy = 1;
3675 break;
3676 case B43legacy_PHYTYPE_G:
3677 have_gphy = 1;
3678 break;
3679 default:
3680 B43legacy_BUG_ON(1);
3681 }
3682 }
3683 dev->phy.gmode = (have_gphy || have_bphy);
3684 tmp = dev->phy.gmode ? B43legacy_TMSLOW_GMODE : 0;
3685 b43legacy_wireless_core_reset(dev, tmp);
3686
3687 err = b43legacy_validate_chipaccess(dev);
3688 if (err)
Larry Fingerba48f7b2007-10-12 23:04:51 -05003689 goto err_powerdown;
Larry Finger75388ac2007-09-25 16:46:54 -07003690 err = b43legacy_setup_modes(dev, have_bphy, have_gphy);
3691 if (err)
Larry Fingerba48f7b2007-10-12 23:04:51 -05003692 goto err_powerdown;
Larry Finger75388ac2007-09-25 16:46:54 -07003693
3694 /* Now set some default "current_dev" */
3695 if (!wl->current_dev)
3696 wl->current_dev = dev;
3697 INIT_WORK(&dev->restart_work, b43legacy_chip_reset);
3698
Larry Finger93bb7f32007-10-10 22:44:22 -05003699 b43legacy_radio_turn_off(dev, 1);
Larry Finger75388ac2007-09-25 16:46:54 -07003700 b43legacy_switch_analog(dev, 0);
3701 ssb_device_disable(dev->dev, 0);
3702 ssb_bus_may_powerdown(bus);
3703
3704out:
3705 return err;
3706
Larry Finger75388ac2007-09-25 16:46:54 -07003707err_powerdown:
3708 ssb_bus_may_powerdown(bus);
3709 return err;
3710}
3711
3712static void b43legacy_one_core_detach(struct ssb_device *dev)
3713{
3714 struct b43legacy_wldev *wldev;
3715 struct b43legacy_wl *wl;
3716
Michael Buesch48e6c512008-05-22 17:06:36 +02003717 /* Do not cancel ieee80211-workqueue based work here.
3718 * See comment in b43legacy_remove(). */
3719
Larry Finger75388ac2007-09-25 16:46:54 -07003720 wldev = ssb_get_drvdata(dev);
3721 wl = wldev->wl;
Larry Finger75388ac2007-09-25 16:46:54 -07003722 b43legacy_debugfs_remove_device(wldev);
3723 b43legacy_wireless_core_detach(wldev);
3724 list_del(&wldev->list);
3725 wl->nr_devs--;
3726 ssb_set_drvdata(dev, NULL);
3727 kfree(wldev);
3728}
3729
3730static int b43legacy_one_core_attach(struct ssb_device *dev,
3731 struct b43legacy_wl *wl)
3732{
3733 struct b43legacy_wldev *wldev;
Larry Finger75388ac2007-09-25 16:46:54 -07003734 int err = -ENOMEM;
3735
Larry Finger75388ac2007-09-25 16:46:54 -07003736 wldev = kzalloc(sizeof(*wldev), GFP_KERNEL);
3737 if (!wldev)
3738 goto out;
3739
3740 wldev->dev = dev;
3741 wldev->wl = wl;
3742 b43legacy_set_status(wldev, B43legacy_STAT_UNINIT);
3743 wldev->bad_frames_preempt = modparam_bad_frames_preempt;
3744 tasklet_init(&wldev->isr_tasklet,
Phong Tran475eec12019-11-27 00:55:26 +07003745 b43legacy_interrupt_tasklet,
Larry Finger75388ac2007-09-25 16:46:54 -07003746 (unsigned long)wldev);
3747 if (modparam_pio)
Rusty Russell3db1cd52011-12-19 13:56:45 +00003748 wldev->__using_pio = true;
Larry Finger75388ac2007-09-25 16:46:54 -07003749 INIT_LIST_HEAD(&wldev->list);
3750
3751 err = b43legacy_wireless_core_attach(wldev);
3752 if (err)
3753 goto err_kfree_wldev;
3754
3755 list_add(&wldev->list, &wl->devlist);
3756 wl->nr_devs++;
3757 ssb_set_drvdata(dev, wldev);
3758 b43legacy_debugfs_add_device(wldev);
3759out:
3760 return err;
3761
3762err_kfree_wldev:
3763 kfree(wldev);
3764 return err;
3765}
3766
3767static void b43legacy_sprom_fixup(struct ssb_bus *bus)
3768{
3769 /* boardflags workarounds */
3770 if (bus->boardinfo.vendor == PCI_VENDOR_ID_APPLE &&
3771 bus->boardinfo.type == 0x4E &&
Hauke Mehrtens5a20ef32012-04-29 02:04:06 +02003772 bus->sprom.board_rev > 0x40)
Larry Finger7797aa32007-11-09 16:57:34 -06003773 bus->sprom.boardflags_lo |= B43legacy_BFL_PACTRL;
Larry Finger75388ac2007-09-25 16:46:54 -07003774}
3775
3776static void b43legacy_wireless_exit(struct ssb_device *dev,
3777 struct b43legacy_wl *wl)
3778{
3779 struct ieee80211_hw *hw = wl->hw;
3780
3781 ssb_set_devtypedata(dev, NULL);
3782 ieee80211_free_hw(hw);
3783}
3784
3785static int b43legacy_wireless_init(struct ssb_device *dev)
3786{
3787 struct ssb_sprom *sprom = &dev->bus->sprom;
3788 struct ieee80211_hw *hw;
3789 struct b43legacy_wl *wl;
3790 int err = -ENOMEM;
Larry Finger5d07a3d2011-12-21 18:47:59 -06003791 int queue_num;
Larry Finger75388ac2007-09-25 16:46:54 -07003792
3793 b43legacy_sprom_fixup(dev->bus);
3794
3795 hw = ieee80211_alloc_hw(sizeof(*wl), &b43legacy_hw_ops);
3796 if (!hw) {
3797 b43legacyerr(NULL, "Could not allocate ieee80211 device\n");
3798 goto out;
3799 }
3800
3801 /* fill hw info */
Johannes Berg30686bf2015-06-02 21:39:54 +02003802 ieee80211_hw_set(hw, RX_INCLUDES_FCS);
3803 ieee80211_hw_set(hw, SIGNAL_DBM);
Larry Finger6a29d132020-05-26 10:59:09 -05003804 ieee80211_hw_set(hw, MFP_CAPABLE); /* Allow WPA3 in software */
Johannes Berg30686bf2015-06-02 21:39:54 +02003805
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07003806 hw->wiphy->interface_modes =
3807 BIT(NL80211_IFTYPE_AP) |
3808 BIT(NL80211_IFTYPE_STATION) |
Johannes Berg8f205422016-10-18 10:28:57 +02003809#ifdef CONFIG_WIRELESS_WDS
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07003810 BIT(NL80211_IFTYPE_WDS) |
Johannes Berg8f205422016-10-18 10:28:57 +02003811#endif
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07003812 BIT(NL80211_IFTYPE_ADHOC);
Larry Finger75388ac2007-09-25 16:46:54 -07003813 hw->queues = 1; /* FIXME: hardware has more queues */
Johannes Berge6a98542008-10-21 12:40:02 +02003814 hw->max_rates = 2;
Larry Finger75388ac2007-09-25 16:46:54 -07003815 SET_IEEE80211_DEV(hw, dev->dev);
Larry Finger7797aa32007-11-09 16:57:34 -06003816 if (is_valid_ether_addr(sprom->et1mac))
3817 SET_IEEE80211_PERM_ADDR(hw, sprom->et1mac);
Larry Finger75388ac2007-09-25 16:46:54 -07003818 else
Larry Finger7797aa32007-11-09 16:57:34 -06003819 SET_IEEE80211_PERM_ADDR(hw, sprom->il0mac);
Larry Finger75388ac2007-09-25 16:46:54 -07003820
Andrew Zaborowskiae44b502017-02-10 04:50:23 +01003821 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
3822
Larry Finger75388ac2007-09-25 16:46:54 -07003823 /* Get and initialize struct b43legacy_wl */
3824 wl = hw_to_b43legacy_wl(hw);
3825 memset(wl, 0, sizeof(*wl));
3826 wl->hw = hw;
3827 spin_lock_init(&wl->irq_lock);
3828 spin_lock_init(&wl->leds_lock);
3829 mutex_init(&wl->mutex);
3830 INIT_LIST_HEAD(&wl->devlist);
Larry Finger7858e072009-04-11 11:25:24 -05003831 INIT_WORK(&wl->beacon_update_trigger, b43legacy_beacon_update_trigger_work);
Larry Finger5d07a3d2011-12-21 18:47:59 -06003832 INIT_WORK(&wl->tx_work, b43legacy_tx_work);
3833
3834 /* Initialize queues and flags. */
3835 for (queue_num = 0; queue_num < B43legacy_QOS_QUEUE_NUM; queue_num++) {
3836 skb_queue_head_init(&wl->tx_queue[queue_num]);
3837 wl->tx_queue_stopped[queue_num] = 0;
3838 }
Larry Finger75388ac2007-09-25 16:46:54 -07003839
3840 ssb_set_devtypedata(dev, wl);
Pavel Roskinea7a03c2011-07-26 11:36:48 -04003841 b43legacyinfo(wl, "Broadcom %04X WLAN found (core revision %u)\n",
3842 dev->bus->chip_id, dev->id.revision);
Larry Finger75388ac2007-09-25 16:46:54 -07003843 err = 0;
3844out:
3845 return err;
3846}
3847
3848static int b43legacy_probe(struct ssb_device *dev,
3849 const struct ssb_device_id *id)
3850{
3851 struct b43legacy_wl *wl;
3852 int err;
3853 int first = 0;
3854
3855 wl = ssb_get_devtypedata(dev);
3856 if (!wl) {
3857 /* Probing the first core - setup common struct b43legacy_wl */
3858 first = 1;
3859 err = b43legacy_wireless_init(dev);
3860 if (err)
3861 goto out;
3862 wl = ssb_get_devtypedata(dev);
3863 B43legacy_WARN_ON(!wl);
3864 }
3865 err = b43legacy_one_core_attach(dev, wl);
3866 if (err)
3867 goto err_wireless_exit;
3868
Larry Fingera3ea2c72012-03-08 22:25:57 -06003869 /* setup and start work to load firmware */
3870 INIT_WORK(&wl->firmware_load, b43legacy_request_firmware);
3871 schedule_work(&wl->firmware_load);
Larry Finger75388ac2007-09-25 16:46:54 -07003872
3873out:
3874 return err;
3875
Larry Finger75388ac2007-09-25 16:46:54 -07003876err_wireless_exit:
3877 if (first)
3878 b43legacy_wireless_exit(dev, wl);
3879 return err;
3880}
3881
3882static void b43legacy_remove(struct ssb_device *dev)
3883{
3884 struct b43legacy_wl *wl = ssb_get_devtypedata(dev);
3885 struct b43legacy_wldev *wldev = ssb_get_drvdata(dev);
3886
Michael Buesch48e6c512008-05-22 17:06:36 +02003887 /* We must cancel any work here before unregistering from ieee80211,
3888 * as the ieee80211 unreg will destroy the workqueue. */
3889 cancel_work_sync(&wldev->restart_work);
Larry Fingera3ea2c72012-03-08 22:25:57 -06003890 cancel_work_sync(&wl->firmware_load);
Larry Finger45202862014-01-12 15:11:39 -06003891 complete(&wldev->fw_load_complete);
Michael Buesch48e6c512008-05-22 17:06:36 +02003892
Larry Finger75388ac2007-09-25 16:46:54 -07003893 B43legacy_WARN_ON(!wl);
Larry Finger2d838bb2012-09-26 12:32:02 -05003894 if (!wldev->fw.ucode)
3895 return; /* NULL if fw never loaded */
Larry Finger75388ac2007-09-25 16:46:54 -07003896 if (wl->current_dev == wldev)
3897 ieee80211_unregister_hw(wl->hw);
3898
3899 b43legacy_one_core_detach(dev);
3900
3901 if (list_empty(&wl->devlist))
3902 /* Last core on the chip unregistered.
3903 * We can destroy common struct b43legacy_wl.
3904 */
3905 b43legacy_wireless_exit(dev, wl);
3906}
3907
3908/* Perform a hardware reset. This can be called from any context. */
3909void b43legacy_controller_restart(struct b43legacy_wldev *dev,
3910 const char *reason)
3911{
3912 /* Must avoid requeueing, if we are in shutdown. */
3913 if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED)
3914 return;
3915 b43legacyinfo(dev->wl, "Controller RESET (%s) ...\n", reason);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003916 ieee80211_queue_work(dev->wl->hw, &dev->restart_work);
Larry Finger75388ac2007-09-25 16:46:54 -07003917}
3918
3919#ifdef CONFIG_PM
3920
3921static int b43legacy_suspend(struct ssb_device *dev, pm_message_t state)
3922{
3923 struct b43legacy_wldev *wldev = ssb_get_drvdata(dev);
3924 struct b43legacy_wl *wl = wldev->wl;
3925
3926 b43legacydbg(wl, "Suspending...\n");
3927
3928 mutex_lock(&wl->mutex);
3929 wldev->suspend_init_status = b43legacy_status(wldev);
3930 if (wldev->suspend_init_status >= B43legacy_STAT_STARTED)
3931 b43legacy_wireless_core_stop(wldev);
3932 if (wldev->suspend_init_status >= B43legacy_STAT_INITIALIZED)
3933 b43legacy_wireless_core_exit(wldev);
3934 mutex_unlock(&wl->mutex);
3935
3936 b43legacydbg(wl, "Device suspended.\n");
3937
3938 return 0;
3939}
3940
3941static int b43legacy_resume(struct ssb_device *dev)
3942{
3943 struct b43legacy_wldev *wldev = ssb_get_drvdata(dev);
3944 struct b43legacy_wl *wl = wldev->wl;
3945 int err = 0;
3946
3947 b43legacydbg(wl, "Resuming...\n");
3948
3949 mutex_lock(&wl->mutex);
3950 if (wldev->suspend_init_status >= B43legacy_STAT_INITIALIZED) {
3951 err = b43legacy_wireless_core_init(wldev);
3952 if (err) {
3953 b43legacyerr(wl, "Resume failed at core init\n");
3954 goto out;
3955 }
3956 }
3957 if (wldev->suspend_init_status >= B43legacy_STAT_STARTED) {
3958 err = b43legacy_wireless_core_start(wldev);
3959 if (err) {
3960 b43legacy_wireless_core_exit(wldev);
3961 b43legacyerr(wl, "Resume failed at core start\n");
3962 goto out;
3963 }
3964 }
Larry Finger75388ac2007-09-25 16:46:54 -07003965
3966 b43legacydbg(wl, "Device resumed.\n");
3967out:
Julia Lawall41048632008-07-21 11:29:34 +02003968 mutex_unlock(&wl->mutex);
Larry Finger75388ac2007-09-25 16:46:54 -07003969 return err;
3970}
3971
3972#else /* CONFIG_PM */
3973# define b43legacy_suspend NULL
3974# define b43legacy_resume NULL
3975#endif /* CONFIG_PM */
3976
3977static struct ssb_driver b43legacy_ssb_driver = {
3978 .name = KBUILD_MODNAME,
3979 .id_table = b43legacy_ssb_tbl,
3980 .probe = b43legacy_probe,
3981 .remove = b43legacy_remove,
3982 .suspend = b43legacy_suspend,
3983 .resume = b43legacy_resume,
3984};
3985
Stefano Brivio6fff1c62008-02-09 07:20:43 +01003986static void b43legacy_print_driverinfo(void)
3987{
Christoph Egger2f1f00f2010-01-13 14:36:24 +01003988 const char *feat_pci = "", *feat_leds = "",
Stefano Brivio6fff1c62008-02-09 07:20:43 +01003989 *feat_pio = "", *feat_dma = "";
3990
3991#ifdef CONFIG_B43LEGACY_PCI_AUTOSELECT
3992 feat_pci = "P";
3993#endif
3994#ifdef CONFIG_B43LEGACY_LEDS
3995 feat_leds = "L";
3996#endif
Stefano Brivio6fff1c62008-02-09 07:20:43 +01003997#ifdef CONFIG_B43LEGACY_PIO
3998 feat_pio = "I";
3999#endif
4000#ifdef CONFIG_B43LEGACY_DMA
4001 feat_dma = "D";
4002#endif
Michael Bueschc256e052008-03-04 20:31:13 +01004003 printk(KERN_INFO "Broadcom 43xx-legacy driver loaded "
Michael Büsch8b0be902011-08-21 17:24:47 +02004004 "[ Features: %s%s%s%s ]\n",
Christoph Egger2f1f00f2010-01-13 14:36:24 +01004005 feat_pci, feat_leds, feat_pio, feat_dma);
Stefano Brivio6fff1c62008-02-09 07:20:43 +01004006}
4007
Larry Finger75388ac2007-09-25 16:46:54 -07004008static int __init b43legacy_init(void)
4009{
4010 int err;
4011
4012 b43legacy_debugfs_init();
4013
4014 err = ssb_driver_register(&b43legacy_ssb_driver);
4015 if (err)
4016 goto err_dfs_exit;
4017
Stefano Brivio6fff1c62008-02-09 07:20:43 +01004018 b43legacy_print_driverinfo();
4019
Larry Finger75388ac2007-09-25 16:46:54 -07004020 return err;
4021
4022err_dfs_exit:
4023 b43legacy_debugfs_exit();
4024 return err;
4025}
4026
4027static void __exit b43legacy_exit(void)
4028{
4029 ssb_driver_unregister(&b43legacy_ssb_driver);
4030 b43legacy_debugfs_exit();
4031}
4032
4033module_init(b43legacy_init)
4034module_exit(b43legacy_exit)