1ceaec73dSDamien Bergamini /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
484ca7670SDamien Bergamini * Copyright (c) 2004-2006
5ceaec73dSDamien Bergamini * Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
688d3b172SAndrew Thompson * Copyright (c) 2006 Sam Leffler, Errno Consulting
788d3b172SAndrew Thompson * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
8ceaec73dSDamien Bergamini *
9ceaec73dSDamien Bergamini * Redistribution and use in source and binary forms, with or without
10ceaec73dSDamien Bergamini * modification, are permitted provided that the following conditions
11ceaec73dSDamien Bergamini * are met:
12ceaec73dSDamien Bergamini * 1. Redistributions of source code must retain the above copyright
13ceaec73dSDamien Bergamini * notice unmodified, this list of conditions, and the following
14ceaec73dSDamien Bergamini * disclaimer.
15ceaec73dSDamien Bergamini * 2. Redistributions in binary form must reproduce the above copyright
16ceaec73dSDamien Bergamini * notice, this list of conditions and the following disclaimer in the
17ceaec73dSDamien Bergamini * documentation and/or other materials provided with the distribution.
18ceaec73dSDamien Bergamini *
19ceaec73dSDamien Bergamini * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20ceaec73dSDamien Bergamini * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21ceaec73dSDamien Bergamini * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22ceaec73dSDamien Bergamini * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23ceaec73dSDamien Bergamini * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24ceaec73dSDamien Bergamini * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25ceaec73dSDamien Bergamini * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26ceaec73dSDamien Bergamini * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27ceaec73dSDamien Bergamini * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28ceaec73dSDamien Bergamini * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29ceaec73dSDamien Bergamini * SUCH DAMAGE.
30ceaec73dSDamien Bergamini */
31ceaec73dSDamien Bergamini
32ceaec73dSDamien Bergamini #include <sys/cdefs.h>
33ceaec73dSDamien Bergamini /*-
34ceaec73dSDamien Bergamini * Intel(R) PRO/Wireless 2100 MiniPCI driver
35ceaec73dSDamien Bergamini * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
36ceaec73dSDamien Bergamini */
37ceaec73dSDamien Bergamini
38ceaec73dSDamien Bergamini #include <sys/param.h>
39ceaec73dSDamien Bergamini #include <sys/sysctl.h>
40ceaec73dSDamien Bergamini #include <sys/sockio.h>
41ceaec73dSDamien Bergamini #include <sys/mbuf.h>
42ceaec73dSDamien Bergamini #include <sys/kernel.h>
43ceaec73dSDamien Bergamini #include <sys/socket.h>
44ceaec73dSDamien Bergamini #include <sys/systm.h>
45ceaec73dSDamien Bergamini #include <sys/malloc.h>
4684ca7670SDamien Bergamini #include <sys/queue.h>
4784ca7670SDamien Bergamini #include <sys/taskqueue.h>
48ceaec73dSDamien Bergamini #include <sys/module.h>
49ceaec73dSDamien Bergamini #include <sys/bus.h>
50ceaec73dSDamien Bergamini #include <sys/endian.h>
5184ca7670SDamien Bergamini #include <sys/linker.h>
5284ca7670SDamien Bergamini #include <sys/firmware.h>
53ceaec73dSDamien Bergamini
54ceaec73dSDamien Bergamini #include <machine/bus.h>
55ceaec73dSDamien Bergamini #include <machine/resource.h>
56ceaec73dSDamien Bergamini #include <sys/rman.h>
57ceaec73dSDamien Bergamini
58ceaec73dSDamien Bergamini #include <dev/pci/pcireg.h>
59ceaec73dSDamien Bergamini #include <dev/pci/pcivar.h>
60ceaec73dSDamien Bergamini
61ceaec73dSDamien Bergamini #include <net/bpf.h>
62ceaec73dSDamien Bergamini #include <net/if.h>
6376039bc8SGleb Smirnoff #include <net/if_var.h>
64ceaec73dSDamien Bergamini #include <net/if_arp.h>
65ceaec73dSDamien Bergamini #include <net/ethernet.h>
66ceaec73dSDamien Bergamini #include <net/if_dl.h>
67ceaec73dSDamien Bergamini #include <net/if_media.h>
68ceaec73dSDamien Bergamini #include <net/if_types.h>
69ceaec73dSDamien Bergamini
7084ca7670SDamien Bergamini #include <net80211/ieee80211_var.h>
7184ca7670SDamien Bergamini #include <net80211/ieee80211_radiotap.h>
7284ca7670SDamien Bergamini
73ceaec73dSDamien Bergamini #include <netinet/in.h>
74ceaec73dSDamien Bergamini #include <netinet/in_systm.h>
75ceaec73dSDamien Bergamini #include <netinet/in_var.h>
76ceaec73dSDamien Bergamini #include <netinet/ip.h>
77ceaec73dSDamien Bergamini #include <netinet/if_ether.h>
78ceaec73dSDamien Bergamini
79ceaec73dSDamien Bergamini #include <dev/ipw/if_ipwreg.h>
80ceaec73dSDamien Bergamini #include <dev/ipw/if_ipwvar.h>
81ceaec73dSDamien Bergamini
8288d3b172SAndrew Thompson #define IPW_DEBUG
83ceaec73dSDamien Bergamini #ifdef IPW_DEBUG
84ceaec73dSDamien Bergamini #define DPRINTF(x) do { if (ipw_debug > 0) printf x; } while (0)
85ceaec73dSDamien Bergamini #define DPRINTFN(n, x) do { if (ipw_debug >= (n)) printf x; } while (0)
86ceaec73dSDamien Bergamini int ipw_debug = 0;
87ceaec73dSDamien Bergamini SYSCTL_INT(_debug, OID_AUTO, ipw, CTLFLAG_RW, &ipw_debug, 0, "ipw debug level");
88ceaec73dSDamien Bergamini #else
89ceaec73dSDamien Bergamini #define DPRINTF(x)
90ceaec73dSDamien Bergamini #define DPRINTFN(n, x)
91ceaec73dSDamien Bergamini #endif
92ceaec73dSDamien Bergamini
93ceaec73dSDamien Bergamini MODULE_DEPEND(ipw, pci, 1, 1, 1);
94ceaec73dSDamien Bergamini MODULE_DEPEND(ipw, wlan, 1, 1, 1);
9584ca7670SDamien Bergamini MODULE_DEPEND(ipw, firmware, 1, 1, 1);
96ceaec73dSDamien Bergamini
97ceaec73dSDamien Bergamini struct ipw_ident {
98ceaec73dSDamien Bergamini uint16_t vendor;
99ceaec73dSDamien Bergamini uint16_t device;
100ceaec73dSDamien Bergamini const char *name;
101ceaec73dSDamien Bergamini };
102ceaec73dSDamien Bergamini
103ceaec73dSDamien Bergamini static const struct ipw_ident ipw_ident_table[] = {
104ceaec73dSDamien Bergamini { 0x8086, 0x1043, "Intel(R) PRO/Wireless 2100 MiniPCI" },
105ceaec73dSDamien Bergamini
106ceaec73dSDamien Bergamini { 0, 0, NULL }
107ceaec73dSDamien Bergamini };
108ceaec73dSDamien Bergamini
109b032f27cSSam Leffler static struct ieee80211vap *ipw_vap_create(struct ieee80211com *,
110fcd9500fSBernhard Schmidt const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
111fcd9500fSBernhard Schmidt const uint8_t [IEEE80211_ADDR_LEN],
112fcd9500fSBernhard Schmidt const uint8_t [IEEE80211_ADDR_LEN]);
113b032f27cSSam Leffler static void ipw_vap_delete(struct ieee80211vap *);
114ceaec73dSDamien Bergamini static int ipw_dma_alloc(struct ipw_softc *);
115ceaec73dSDamien Bergamini static void ipw_release(struct ipw_softc *);
116ec22a3a2SJustin Hibbits static void ipw_media_status(if_t, struct ifmediareq *);
117b032f27cSSam Leffler static int ipw_newstate(struct ieee80211vap *, enum ieee80211_state, int);
118ceaec73dSDamien Bergamini static uint16_t ipw_read_prom_word(struct ipw_softc *, uint8_t);
1190587ad6bSAndriy Voskoboinyk static uint16_t ipw_read_chanmask(struct ipw_softc *);
12088d3b172SAndrew Thompson static void ipw_rx_cmd_intr(struct ipw_softc *, struct ipw_soft_buf *);
12188d3b172SAndrew Thompson static void ipw_rx_newstate_intr(struct ipw_softc *, struct ipw_soft_buf *);
12288d3b172SAndrew Thompson static void ipw_rx_data_intr(struct ipw_softc *, struct ipw_status *,
123ceaec73dSDamien Bergamini struct ipw_soft_bd *, struct ipw_soft_buf *);
124ceaec73dSDamien Bergamini static void ipw_rx_intr(struct ipw_softc *);
125ceaec73dSDamien Bergamini static void ipw_release_sbd(struct ipw_softc *, struct ipw_soft_bd *);
126ceaec73dSDamien Bergamini static void ipw_tx_intr(struct ipw_softc *);
127ceaec73dSDamien Bergamini static void ipw_intr(void *);
128ceaec73dSDamien Bergamini static void ipw_dma_map_addr(void *, bus_dma_segment_t *, int, int);
12988d3b172SAndrew Thompson static const char * ipw_cmdname(int);
130ceaec73dSDamien Bergamini static int ipw_cmd(struct ipw_softc *, uint32_t, void *, uint32_t);
1317a79cebfSGleb Smirnoff static int ipw_tx_start(struct ipw_softc *, struct mbuf *,
132ceaec73dSDamien Bergamini struct ieee80211_node *);
133b032f27cSSam Leffler static int ipw_raw_xmit(struct ieee80211_node *, struct mbuf *,
134b032f27cSSam Leffler const struct ieee80211_bpf_params *);
1357a79cebfSGleb Smirnoff static int ipw_transmit(struct ieee80211com *, struct mbuf *);
1367a79cebfSGleb Smirnoff static void ipw_start(struct ipw_softc *);
13788d3b172SAndrew Thompson static void ipw_watchdog(void *);
1387a79cebfSGleb Smirnoff static void ipw_parent(struct ieee80211com *);
139ceaec73dSDamien Bergamini static void ipw_stop_master(struct ipw_softc *);
14088d3b172SAndrew Thompson static int ipw_enable(struct ipw_softc *);
14188d3b172SAndrew Thompson static int ipw_disable(struct ipw_softc *);
142ceaec73dSDamien Bergamini static int ipw_reset(struct ipw_softc *);
14384ca7670SDamien Bergamini static int ipw_load_ucode(struct ipw_softc *, const char *, int);
14484ca7670SDamien Bergamini static int ipw_load_firmware(struct ipw_softc *, const char *, int);
145ceaec73dSDamien Bergamini static int ipw_config(struct ipw_softc *);
1465efea30fSAndrew Thompson static void ipw_assoc(struct ieee80211com *, struct ieee80211vap *);
1475efea30fSAndrew Thompson static void ipw_disassoc(struct ieee80211com *, struct ieee80211vap *);
14884ca7670SDamien Bergamini static void ipw_init_task(void *, int);
149ceaec73dSDamien Bergamini static void ipw_init(void *);
150b032f27cSSam Leffler static void ipw_init_locked(struct ipw_softc *);
151ceaec73dSDamien Bergamini static void ipw_stop(void *);
15288d3b172SAndrew Thompson static void ipw_stop_locked(struct ipw_softc *);
153ceaec73dSDamien Bergamini static int ipw_sysctl_stats(SYSCTL_HANDLER_ARGS);
154ceaec73dSDamien Bergamini static int ipw_sysctl_radio(SYSCTL_HANDLER_ARGS);
155ceaec73dSDamien Bergamini static uint32_t ipw_read_table1(struct ipw_softc *, uint32_t);
156ceaec73dSDamien Bergamini static void ipw_write_table1(struct ipw_softc *, uint32_t, uint32_t);
15788d3b172SAndrew Thompson #if 0
158ceaec73dSDamien Bergamini static int ipw_read_table2(struct ipw_softc *, uint32_t, void *,
159ceaec73dSDamien Bergamini uint32_t *);
160ceaec73dSDamien Bergamini static void ipw_read_mem_1(struct ipw_softc *, bus_size_t, uint8_t *,
161ceaec73dSDamien Bergamini bus_size_t);
16288d3b172SAndrew Thompson #endif
16384ca7670SDamien Bergamini static void ipw_write_mem_1(struct ipw_softc *, bus_size_t,
16484ca7670SDamien Bergamini const uint8_t *, bus_size_t);
16588d3b172SAndrew Thompson static int ipw_scan(struct ipw_softc *);
16688d3b172SAndrew Thompson static void ipw_scan_start(struct ieee80211com *);
16788d3b172SAndrew Thompson static void ipw_scan_end(struct ieee80211com *);
1680587ad6bSAndriy Voskoboinyk static void ipw_getradiocaps(struct ieee80211com *, int, int *,
1690587ad6bSAndriy Voskoboinyk struct ieee80211_channel[]);
17088d3b172SAndrew Thompson static void ipw_set_channel(struct ieee80211com *);
171b032f27cSSam Leffler static void ipw_scan_curchan(struct ieee80211_scan_state *,
172b032f27cSSam Leffler unsigned long maxdwell);
173b032f27cSSam Leffler static void ipw_scan_mindwell(struct ieee80211_scan_state *);
174ceaec73dSDamien Bergamini
175ceaec73dSDamien Bergamini static int ipw_probe(device_t);
176ceaec73dSDamien Bergamini static int ipw_attach(device_t);
177ceaec73dSDamien Bergamini static int ipw_detach(device_t);
178ceaec73dSDamien Bergamini static int ipw_shutdown(device_t);
179ceaec73dSDamien Bergamini static int ipw_suspend(device_t);
180ceaec73dSDamien Bergamini static int ipw_resume(device_t);
181ceaec73dSDamien Bergamini
182ceaec73dSDamien Bergamini static device_method_t ipw_methods[] = {
183ceaec73dSDamien Bergamini /* Device interface */
184ceaec73dSDamien Bergamini DEVMETHOD(device_probe, ipw_probe),
185ceaec73dSDamien Bergamini DEVMETHOD(device_attach, ipw_attach),
186ceaec73dSDamien Bergamini DEVMETHOD(device_detach, ipw_detach),
187ceaec73dSDamien Bergamini DEVMETHOD(device_shutdown, ipw_shutdown),
188ceaec73dSDamien Bergamini DEVMETHOD(device_suspend, ipw_suspend),
189ceaec73dSDamien Bergamini DEVMETHOD(device_resume, ipw_resume),
190ceaec73dSDamien Bergamini
19166168a04SMarius Strobl DEVMETHOD_END
192ceaec73dSDamien Bergamini };
193ceaec73dSDamien Bergamini
194ceaec73dSDamien Bergamini static driver_t ipw_driver = {
195ceaec73dSDamien Bergamini "ipw",
196ceaec73dSDamien Bergamini ipw_methods,
197ceaec73dSDamien Bergamini sizeof (struct ipw_softc)
198ceaec73dSDamien Bergamini };
199ceaec73dSDamien Bergamini
20064afbe6aSJohn Baldwin DRIVER_MODULE(ipw, pci, ipw_driver, NULL, NULL);
201f489d4c3SWarner Losh MODULE_PNP_INFO("U16:vendor;U16:device;D:#", pci, ipw, ipw_ident_table,
202329e817fSWarner Losh nitems(ipw_ident_table) - 1);
203ceaec73dSDamien Bergamini
204129145a4SBernhard Schmidt MODULE_VERSION(ipw, 1);
205129145a4SBernhard Schmidt
206ceaec73dSDamien Bergamini static int
ipw_probe(device_t dev)207ceaec73dSDamien Bergamini ipw_probe(device_t dev)
208ceaec73dSDamien Bergamini {
209ceaec73dSDamien Bergamini const struct ipw_ident *ident;
210ceaec73dSDamien Bergamini
211ceaec73dSDamien Bergamini for (ident = ipw_ident_table; ident->name != NULL; ident++) {
212ceaec73dSDamien Bergamini if (pci_get_vendor(dev) == ident->vendor &&
213ceaec73dSDamien Bergamini pci_get_device(dev) == ident->device) {
214ceaec73dSDamien Bergamini device_set_desc(dev, ident->name);
21566168a04SMarius Strobl return (BUS_PROBE_DEFAULT);
216ceaec73dSDamien Bergamini }
217ceaec73dSDamien Bergamini }
218ceaec73dSDamien Bergamini return ENXIO;
219ceaec73dSDamien Bergamini }
220ceaec73dSDamien Bergamini
221ceaec73dSDamien Bergamini /* Base Address Register */
222ceaec73dSDamien Bergamini static int
ipw_attach(device_t dev)223ceaec73dSDamien Bergamini ipw_attach(device_t dev)
224ceaec73dSDamien Bergamini {
225ceaec73dSDamien Bergamini struct ipw_softc *sc = device_get_softc(dev);
2267a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
227ceaec73dSDamien Bergamini uint16_t val;
228ceaec73dSDamien Bergamini int error, i;
229ceaec73dSDamien Bergamini
230ceaec73dSDamien Bergamini sc->sc_dev = dev;
231ceaec73dSDamien Bergamini
232ceaec73dSDamien Bergamini mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
233ceaec73dSDamien Bergamini MTX_DEF | MTX_RECURSE);
2347a79cebfSGleb Smirnoff mbufq_init(&sc->sc_snd, ifqmaxlen);
23584ca7670SDamien Bergamini TASK_INIT(&sc->sc_init_task, 0, ipw_init_task, sc);
23688d3b172SAndrew Thompson callout_init_mtx(&sc->sc_wdtimer, &sc->sc_mtx, 0);
23784ca7670SDamien Bergamini
2380e22d2c5SDamien Bergamini pci_write_config(dev, 0x41, 0, 1);
2390e22d2c5SDamien Bergamini
240ceaec73dSDamien Bergamini /* enable bus-mastering */
241ceaec73dSDamien Bergamini pci_enable_busmaster(dev);
242ceaec73dSDamien Bergamini
24366168a04SMarius Strobl i = PCIR_BAR(0);
24466168a04SMarius Strobl sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &i, RF_ACTIVE);
245ceaec73dSDamien Bergamini if (sc->mem == NULL) {
246ceaec73dSDamien Bergamini device_printf(dev, "could not allocate memory resource\n");
247ceaec73dSDamien Bergamini goto fail;
248ceaec73dSDamien Bergamini }
249ceaec73dSDamien Bergamini
250ceaec73dSDamien Bergamini sc->sc_st = rman_get_bustag(sc->mem);
251ceaec73dSDamien Bergamini sc->sc_sh = rman_get_bushandle(sc->mem);
252ceaec73dSDamien Bergamini
25366168a04SMarius Strobl i = 0;
25466168a04SMarius Strobl sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i,
255ceaec73dSDamien Bergamini RF_ACTIVE | RF_SHAREABLE);
256ceaec73dSDamien Bergamini if (sc->irq == NULL) {
257ceaec73dSDamien Bergamini device_printf(dev, "could not allocate interrupt resource\n");
258b032f27cSSam Leffler goto fail1;
259ceaec73dSDamien Bergamini }
260ceaec73dSDamien Bergamini
261ceaec73dSDamien Bergamini if (ipw_reset(sc) != 0) {
262ceaec73dSDamien Bergamini device_printf(dev, "could not reset adapter\n");
263b032f27cSSam Leffler goto fail2;
264ceaec73dSDamien Bergamini }
265ceaec73dSDamien Bergamini
266ceaec73dSDamien Bergamini if (ipw_dma_alloc(sc) != 0) {
267ceaec73dSDamien Bergamini device_printf(dev, "could not allocate DMA resources\n");
268b032f27cSSam Leffler goto fail2;
269ceaec73dSDamien Bergamini }
27009137879SDamien Bergamini
27159686fe9SGleb Smirnoff ic->ic_softc = sc;
272c8550c02SGleb Smirnoff ic->ic_name = device_get_nameunit(dev);
273ceaec73dSDamien Bergamini ic->ic_opmode = IEEE80211_M_STA;
274b032f27cSSam Leffler ic->ic_phytype = IEEE80211_T_DS;
275ceaec73dSDamien Bergamini
276ceaec73dSDamien Bergamini /* set device capabilities */
277c43feedeSSam Leffler ic->ic_caps =
278c43feedeSSam Leffler IEEE80211_C_STA /* station mode supported */
279c43feedeSSam Leffler | IEEE80211_C_IBSS /* IBSS mode supported */
28088d3b172SAndrew Thompson | IEEE80211_C_MONITOR /* monitor mode supported */
28188d3b172SAndrew Thompson | IEEE80211_C_PMGT /* power save supported */
28288d3b172SAndrew Thompson | IEEE80211_C_SHPREAMBLE /* short preamble supported */
28388d3b172SAndrew Thompson | IEEE80211_C_WPA /* 802.11i supported */
28488d3b172SAndrew Thompson ;
285ceaec73dSDamien Bergamini
286ceaec73dSDamien Bergamini /* read MAC address from EEPROM */
287ceaec73dSDamien Bergamini val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 0);
2887a79cebfSGleb Smirnoff ic->ic_macaddr[0] = val >> 8;
2897a79cebfSGleb Smirnoff ic->ic_macaddr[1] = val & 0xff;
290ceaec73dSDamien Bergamini val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 1);
2917a79cebfSGleb Smirnoff ic->ic_macaddr[2] = val >> 8;
2927a79cebfSGleb Smirnoff ic->ic_macaddr[3] = val & 0xff;
293ceaec73dSDamien Bergamini val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 2);
2947a79cebfSGleb Smirnoff ic->ic_macaddr[4] = val >> 8;
2957a79cebfSGleb Smirnoff ic->ic_macaddr[5] = val & 0xff;
296ceaec73dSDamien Bergamini
2970587ad6bSAndriy Voskoboinyk sc->chanmask = ipw_read_chanmask(sc);
2980587ad6bSAndriy Voskoboinyk ipw_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
2990587ad6bSAndriy Voskoboinyk ic->ic_channels);
300ceaec73dSDamien Bergamini
301ceaec73dSDamien Bergamini /* check support for radio transmitter switch in EEPROM */
302ceaec73dSDamien Bergamini if (!(ipw_read_prom_word(sc, IPW_EEPROM_RADIO) & 8))
303ceaec73dSDamien Bergamini sc->flags |= IPW_FLAG_HAS_RADIO_SWITCH;
304ceaec73dSDamien Bergamini
3057a79cebfSGleb Smirnoff ieee80211_ifattach(ic);
30688d3b172SAndrew Thompson ic->ic_scan_start = ipw_scan_start;
30788d3b172SAndrew Thompson ic->ic_scan_end = ipw_scan_end;
3080587ad6bSAndriy Voskoboinyk ic->ic_getradiocaps = ipw_getradiocaps;
30988d3b172SAndrew Thompson ic->ic_set_channel = ipw_set_channel;
31088d3b172SAndrew Thompson ic->ic_scan_curchan = ipw_scan_curchan;
31188d3b172SAndrew Thompson ic->ic_scan_mindwell = ipw_scan_mindwell;
312b032f27cSSam Leffler ic->ic_raw_xmit = ipw_raw_xmit;
313b032f27cSSam Leffler ic->ic_vap_create = ipw_vap_create;
314b032f27cSSam Leffler ic->ic_vap_delete = ipw_vap_delete;
3157a79cebfSGleb Smirnoff ic->ic_transmit = ipw_transmit;
3167a79cebfSGleb Smirnoff ic->ic_parent = ipw_parent;
317b032f27cSSam Leffler
3185463c4a4SSam Leffler ieee80211_radiotap_attach(ic,
3195463c4a4SSam Leffler &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
3205463c4a4SSam Leffler IPW_TX_RADIOTAP_PRESENT,
3215463c4a4SSam Leffler &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
3225463c4a4SSam Leffler IPW_RX_RADIOTAP_PRESENT);
323ceaec73dSDamien Bergamini
324ceaec73dSDamien Bergamini /*
325ceaec73dSDamien Bergamini * Add a few sysctl knobs.
326ceaec73dSDamien Bergamini */
327ceaec73dSDamien Bergamini SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
328ceaec73dSDamien Bergamini SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "radio",
3297029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
3307029da5cSPawel Biernacki ipw_sysctl_radio, "I",
331ceaec73dSDamien Bergamini "radio transmitter switch state (0=off, 1=on)");
332ceaec73dSDamien Bergamini
333ceaec73dSDamien Bergamini SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
334ceaec73dSDamien Bergamini SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "stats",
3357029da5cSPawel Biernacki CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
3367029da5cSPawel Biernacki ipw_sysctl_stats, "S", "statistics");
337ceaec73dSDamien Bergamini
338ceaec73dSDamien Bergamini /*
339ceaec73dSDamien Bergamini * Hook our interrupt after all initialization is complete.
340ceaec73dSDamien Bergamini */
341ceaec73dSDamien Bergamini error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
342ef544f63SPaolo Pisati NULL, ipw_intr, sc, &sc->sc_ih);
343ceaec73dSDamien Bergamini if (error != 0) {
344ceaec73dSDamien Bergamini device_printf(dev, "could not set up interrupt\n");
3457a79cebfSGleb Smirnoff goto fail3;
346ceaec73dSDamien Bergamini }
347ceaec73dSDamien Bergamini
348ceaec73dSDamien Bergamini if (bootverbose)
349ceaec73dSDamien Bergamini ieee80211_announce(ic);
350ceaec73dSDamien Bergamini
351ceaec73dSDamien Bergamini return 0;
352b032f27cSSam Leffler fail3:
353b032f27cSSam Leffler ipw_release(sc);
354b032f27cSSam Leffler fail2:
35566168a04SMarius Strobl bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq), sc->irq);
356b032f27cSSam Leffler fail1:
35766168a04SMarius Strobl bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->mem),
35866168a04SMarius Strobl sc->mem);
359b032f27cSSam Leffler fail:
360b032f27cSSam Leffler mtx_destroy(&sc->sc_mtx);
361ceaec73dSDamien Bergamini return ENXIO;
362ceaec73dSDamien Bergamini }
363ceaec73dSDamien Bergamini
364ceaec73dSDamien Bergamini static int
ipw_detach(device_t dev)365ceaec73dSDamien Bergamini ipw_detach(device_t dev)
366ceaec73dSDamien Bergamini {
367ceaec73dSDamien Bergamini struct ipw_softc *sc = device_get_softc(dev);
3687a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
369ceaec73dSDamien Bergamini
37066168a04SMarius Strobl bus_teardown_intr(dev, sc->irq, sc->sc_ih);
37166168a04SMarius Strobl
372b569d213SAndrew Thompson ieee80211_draintask(ic, &sc->sc_init_task);
373ceaec73dSDamien Bergamini ipw_stop(sc);
374b032f27cSSam Leffler
375b032f27cSSam Leffler ieee80211_ifdetach(ic);
376b032f27cSSam Leffler
37788d3b172SAndrew Thompson callout_drain(&sc->sc_wdtimer);
3787a79cebfSGleb Smirnoff mbufq_drain(&sc->sc_snd);
379ceaec73dSDamien Bergamini
380ceaec73dSDamien Bergamini ipw_release(sc);
381ceaec73dSDamien Bergamini
38266168a04SMarius Strobl bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq), sc->irq);
383ceaec73dSDamien Bergamini
38466168a04SMarius Strobl bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->mem),
38566168a04SMarius Strobl sc->mem);
38684ca7670SDamien Bergamini
387cbe2bd55SIan Dowse if (sc->sc_firmware != NULL) {
388cbe2bd55SIan Dowse firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
389cbe2bd55SIan Dowse sc->sc_firmware = NULL;
390cbe2bd55SIan Dowse }
391cbe2bd55SIan Dowse
392ceaec73dSDamien Bergamini mtx_destroy(&sc->sc_mtx);
393ceaec73dSDamien Bergamini
394ceaec73dSDamien Bergamini return 0;
395ceaec73dSDamien Bergamini }
396ceaec73dSDamien Bergamini
397b032f27cSSam Leffler static struct ieee80211vap *
ipw_vap_create(struct ieee80211com * ic,const char name[IFNAMSIZ],int unit,enum ieee80211_opmode opmode,int flags,const uint8_t bssid[IEEE80211_ADDR_LEN],const uint8_t mac[IEEE80211_ADDR_LEN])398fcd9500fSBernhard Schmidt ipw_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
399fcd9500fSBernhard Schmidt enum ieee80211_opmode opmode, int flags,
400b032f27cSSam Leffler const uint8_t bssid[IEEE80211_ADDR_LEN],
401b032f27cSSam Leffler const uint8_t mac[IEEE80211_ADDR_LEN])
402b032f27cSSam Leffler {
4032cb71df1SAdrian Chadd struct ipw_softc *sc = ic->ic_softc;
404b032f27cSSam Leffler struct ipw_vap *ivp;
405b032f27cSSam Leffler struct ieee80211vap *vap;
406b032f27cSSam Leffler const struct firmware *fp;
407b032f27cSSam Leffler const struct ipw_firmware_hdr *hdr;
408b032f27cSSam Leffler const char *imagename;
409b032f27cSSam Leffler
410b032f27cSSam Leffler if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
411b032f27cSSam Leffler return NULL;
412b032f27cSSam Leffler
413b032f27cSSam Leffler switch (opmode) {
414b032f27cSSam Leffler case IEEE80211_M_STA:
415b032f27cSSam Leffler imagename = "ipw_bss";
416b032f27cSSam Leffler break;
417b032f27cSSam Leffler case IEEE80211_M_IBSS:
418b032f27cSSam Leffler imagename = "ipw_ibss";
419b032f27cSSam Leffler break;
420b032f27cSSam Leffler case IEEE80211_M_MONITOR:
421b032f27cSSam Leffler imagename = "ipw_monitor";
422b032f27cSSam Leffler break;
423b032f27cSSam Leffler default:
424b032f27cSSam Leffler return NULL;
425b032f27cSSam Leffler }
426b032f27cSSam Leffler
427b032f27cSSam Leffler /*
428b032f27cSSam Leffler * Load firmware image using the firmware(9) subsystem. Doing
429b032f27cSSam Leffler * this unlocked is ok since we're single-threaded by the
430b032f27cSSam Leffler * 802.11 layer.
431b032f27cSSam Leffler */
432b032f27cSSam Leffler if (sc->sc_firmware == NULL ||
433b032f27cSSam Leffler strcmp(sc->sc_firmware->name, imagename) != 0) {
434b032f27cSSam Leffler if (sc->sc_firmware != NULL)
435b032f27cSSam Leffler firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
436b032f27cSSam Leffler sc->sc_firmware = firmware_get(imagename);
437b032f27cSSam Leffler }
438b032f27cSSam Leffler if (sc->sc_firmware == NULL) {
439b032f27cSSam Leffler device_printf(sc->sc_dev,
440b032f27cSSam Leffler "could not load firmware image '%s'\n", imagename);
441b032f27cSSam Leffler return NULL;
442b032f27cSSam Leffler }
443b032f27cSSam Leffler fp = sc->sc_firmware;
444b032f27cSSam Leffler if (fp->datasize < sizeof *hdr) {
445b032f27cSSam Leffler device_printf(sc->sc_dev,
446b032f27cSSam Leffler "firmware image too short %zu\n", fp->datasize);
447b032f27cSSam Leffler firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
448b032f27cSSam Leffler sc->sc_firmware = NULL;
449b032f27cSSam Leffler return NULL;
450b032f27cSSam Leffler }
451b032f27cSSam Leffler hdr = (const struct ipw_firmware_hdr *)fp->data;
452b032f27cSSam Leffler if (fp->datasize < sizeof *hdr + le32toh(hdr->mainsz) +
453b032f27cSSam Leffler le32toh(hdr->ucodesz)) {
454b032f27cSSam Leffler device_printf(sc->sc_dev,
455b032f27cSSam Leffler "firmware image too short %zu\n", fp->datasize);
456b032f27cSSam Leffler firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
457b032f27cSSam Leffler sc->sc_firmware = NULL;
458b032f27cSSam Leffler return NULL;
459b032f27cSSam Leffler }
460b032f27cSSam Leffler
4617a79cebfSGleb Smirnoff ivp = malloc(sizeof(struct ipw_vap), M_80211_VAP, M_WAITOK | M_ZERO);
462b032f27cSSam Leffler vap = &ivp->vap;
463b032f27cSSam Leffler
4647a79cebfSGleb Smirnoff ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
465b032f27cSSam Leffler /* override with driver methods */
466b032f27cSSam Leffler ivp->newstate = vap->iv_newstate;
467b032f27cSSam Leffler vap->iv_newstate = ipw_newstate;
468b032f27cSSam Leffler
469b032f27cSSam Leffler /* complete setup */
4707a79cebfSGleb Smirnoff ieee80211_vap_attach(vap, ieee80211_media_change, ipw_media_status,
4717a79cebfSGleb Smirnoff mac);
472b032f27cSSam Leffler ic->ic_opmode = opmode;
473b032f27cSSam Leffler return vap;
474b032f27cSSam Leffler }
475b032f27cSSam Leffler
476b032f27cSSam Leffler static void
ipw_vap_delete(struct ieee80211vap * vap)477b032f27cSSam Leffler ipw_vap_delete(struct ieee80211vap *vap)
478b032f27cSSam Leffler {
479b032f27cSSam Leffler struct ipw_vap *ivp = IPW_VAP(vap);
480b032f27cSSam Leffler
481b032f27cSSam Leffler ieee80211_vap_detach(vap);
482b032f27cSSam Leffler free(ivp, M_80211_VAP);
483b032f27cSSam Leffler }
484b032f27cSSam Leffler
485ceaec73dSDamien Bergamini static int
ipw_dma_alloc(struct ipw_softc * sc)486ceaec73dSDamien Bergamini ipw_dma_alloc(struct ipw_softc *sc)
487ceaec73dSDamien Bergamini {
488ceaec73dSDamien Bergamini struct ipw_soft_bd *sbd;
489ceaec73dSDamien Bergamini struct ipw_soft_hdr *shdr;
490ceaec73dSDamien Bergamini struct ipw_soft_buf *sbuf;
491ceaec73dSDamien Bergamini bus_addr_t physaddr;
492ceaec73dSDamien Bergamini int error, i;
493ceaec73dSDamien Bergamini
494ceaec73dSDamien Bergamini /*
49562ce43ccSScott Long * Allocate parent DMA tag for subsequent allocations.
49662ce43ccSScott Long */
49762ce43ccSScott Long error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
49862ce43ccSScott Long BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
49962ce43ccSScott Long BUS_SPACE_MAXSIZE_32BIT, BUS_SPACE_UNRESTRICTED,
50062ce43ccSScott Long BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL, &sc->parent_dmat);
50162ce43ccSScott Long if (error != 0) {
50262ce43ccSScott Long device_printf(sc->sc_dev, "could not create parent DMA tag\n");
50362ce43ccSScott Long goto fail;
50462ce43ccSScott Long }
50562ce43ccSScott Long
50662ce43ccSScott Long /*
507ceaec73dSDamien Bergamini * Allocate and map tx ring.
508ceaec73dSDamien Bergamini */
50962ce43ccSScott Long error = bus_dma_tag_create(sc->parent_dmat, 4, 0, BUS_SPACE_MAXADDR_32BIT,
510ceaec73dSDamien Bergamini BUS_SPACE_MAXADDR, NULL, NULL, IPW_TBD_SZ, 1, IPW_TBD_SZ, 0, NULL,
511ceaec73dSDamien Bergamini NULL, &sc->tbd_dmat);
512ceaec73dSDamien Bergamini if (error != 0) {
513ceaec73dSDamien Bergamini device_printf(sc->sc_dev, "could not create tx ring DMA tag\n");
514ceaec73dSDamien Bergamini goto fail;
515ceaec73dSDamien Bergamini }
516ceaec73dSDamien Bergamini
517ceaec73dSDamien Bergamini error = bus_dmamem_alloc(sc->tbd_dmat, (void **)&sc->tbd_list,
518ceaec73dSDamien Bergamini BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->tbd_map);
519ceaec73dSDamien Bergamini if (error != 0) {
520ceaec73dSDamien Bergamini device_printf(sc->sc_dev,
521ceaec73dSDamien Bergamini "could not allocate tx ring DMA memory\n");
522ceaec73dSDamien Bergamini goto fail;
523ceaec73dSDamien Bergamini }
524ceaec73dSDamien Bergamini
525ceaec73dSDamien Bergamini error = bus_dmamap_load(sc->tbd_dmat, sc->tbd_map, sc->tbd_list,
526ceaec73dSDamien Bergamini IPW_TBD_SZ, ipw_dma_map_addr, &sc->tbd_phys, 0);
527ceaec73dSDamien Bergamini if (error != 0) {
528ceaec73dSDamien Bergamini device_printf(sc->sc_dev, "could not map tx ring DMA memory\n");
529ceaec73dSDamien Bergamini goto fail;
530ceaec73dSDamien Bergamini }
531ceaec73dSDamien Bergamini
532ceaec73dSDamien Bergamini /*
533ceaec73dSDamien Bergamini * Allocate and map rx ring.
534ceaec73dSDamien Bergamini */
53562ce43ccSScott Long error = bus_dma_tag_create(sc->parent_dmat, 4, 0, BUS_SPACE_MAXADDR_32BIT,
536ceaec73dSDamien Bergamini BUS_SPACE_MAXADDR, NULL, NULL, IPW_RBD_SZ, 1, IPW_RBD_SZ, 0, NULL,
537ceaec73dSDamien Bergamini NULL, &sc->rbd_dmat);
538ceaec73dSDamien Bergamini if (error != 0) {
539ceaec73dSDamien Bergamini device_printf(sc->sc_dev, "could not create rx ring DMA tag\n");
540ceaec73dSDamien Bergamini goto fail;
541ceaec73dSDamien Bergamini }
542ceaec73dSDamien Bergamini
543ceaec73dSDamien Bergamini error = bus_dmamem_alloc(sc->rbd_dmat, (void **)&sc->rbd_list,
544ceaec73dSDamien Bergamini BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->rbd_map);
545ceaec73dSDamien Bergamini if (error != 0) {
546ceaec73dSDamien Bergamini device_printf(sc->sc_dev,
547ceaec73dSDamien Bergamini "could not allocate rx ring DMA memory\n");
548ceaec73dSDamien Bergamini goto fail;
549ceaec73dSDamien Bergamini }
550ceaec73dSDamien Bergamini
551ceaec73dSDamien Bergamini error = bus_dmamap_load(sc->rbd_dmat, sc->rbd_map, sc->rbd_list,
552ceaec73dSDamien Bergamini IPW_RBD_SZ, ipw_dma_map_addr, &sc->rbd_phys, 0);
553ceaec73dSDamien Bergamini if (error != 0) {
554ceaec73dSDamien Bergamini device_printf(sc->sc_dev, "could not map rx ring DMA memory\n");
555ceaec73dSDamien Bergamini goto fail;
556ceaec73dSDamien Bergamini }
557ceaec73dSDamien Bergamini
558ceaec73dSDamien Bergamini /*
559ceaec73dSDamien Bergamini * Allocate and map status ring.
560ceaec73dSDamien Bergamini */
56162ce43ccSScott Long error = bus_dma_tag_create(sc->parent_dmat, 4, 0, BUS_SPACE_MAXADDR_32BIT,
562ceaec73dSDamien Bergamini BUS_SPACE_MAXADDR, NULL, NULL, IPW_STATUS_SZ, 1, IPW_STATUS_SZ, 0,
563ceaec73dSDamien Bergamini NULL, NULL, &sc->status_dmat);
564ceaec73dSDamien Bergamini if (error != 0) {
565ceaec73dSDamien Bergamini device_printf(sc->sc_dev,
566ceaec73dSDamien Bergamini "could not create status ring DMA tag\n");
567ceaec73dSDamien Bergamini goto fail;
568ceaec73dSDamien Bergamini }
569ceaec73dSDamien Bergamini
570ceaec73dSDamien Bergamini error = bus_dmamem_alloc(sc->status_dmat, (void **)&sc->status_list,
571ceaec73dSDamien Bergamini BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->status_map);
572ceaec73dSDamien Bergamini if (error != 0) {
573ceaec73dSDamien Bergamini device_printf(sc->sc_dev,
574ceaec73dSDamien Bergamini "could not allocate status ring DMA memory\n");
575ceaec73dSDamien Bergamini goto fail;
576ceaec73dSDamien Bergamini }
577ceaec73dSDamien Bergamini
578ceaec73dSDamien Bergamini error = bus_dmamap_load(sc->status_dmat, sc->status_map,
579ceaec73dSDamien Bergamini sc->status_list, IPW_STATUS_SZ, ipw_dma_map_addr, &sc->status_phys,
580ceaec73dSDamien Bergamini 0);
581ceaec73dSDamien Bergamini if (error != 0) {
582ceaec73dSDamien Bergamini device_printf(sc->sc_dev,
583ceaec73dSDamien Bergamini "could not map status ring DMA memory\n");
584ceaec73dSDamien Bergamini goto fail;
585ceaec73dSDamien Bergamini }
586ceaec73dSDamien Bergamini
587ceaec73dSDamien Bergamini /*
588ceaec73dSDamien Bergamini * Allocate command DMA map.
589ceaec73dSDamien Bergamini */
59062ce43ccSScott Long error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
591ceaec73dSDamien Bergamini BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_cmd), 1,
592ceaec73dSDamien Bergamini sizeof (struct ipw_cmd), 0, NULL, NULL, &sc->cmd_dmat);
593ceaec73dSDamien Bergamini if (error != 0) {
594ceaec73dSDamien Bergamini device_printf(sc->sc_dev, "could not create command DMA tag\n");
595ceaec73dSDamien Bergamini goto fail;
596ceaec73dSDamien Bergamini }
597ceaec73dSDamien Bergamini
598ceaec73dSDamien Bergamini error = bus_dmamap_create(sc->cmd_dmat, 0, &sc->cmd_map);
599ceaec73dSDamien Bergamini if (error != 0) {
600ceaec73dSDamien Bergamini device_printf(sc->sc_dev,
601ceaec73dSDamien Bergamini "could not create command DMA map\n");
602ceaec73dSDamien Bergamini goto fail;
603ceaec73dSDamien Bergamini }
604ceaec73dSDamien Bergamini
605ceaec73dSDamien Bergamini /*
606ceaec73dSDamien Bergamini * Allocate headers DMA maps.
607ceaec73dSDamien Bergamini */
60862ce43ccSScott Long error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
609ceaec73dSDamien Bergamini BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_hdr), 1,
610ceaec73dSDamien Bergamini sizeof (struct ipw_hdr), 0, NULL, NULL, &sc->hdr_dmat);
611ceaec73dSDamien Bergamini if (error != 0) {
612ceaec73dSDamien Bergamini device_printf(sc->sc_dev, "could not create header DMA tag\n");
613ceaec73dSDamien Bergamini goto fail;
614ceaec73dSDamien Bergamini }
615ceaec73dSDamien Bergamini
616ceaec73dSDamien Bergamini SLIST_INIT(&sc->free_shdr);
617ceaec73dSDamien Bergamini for (i = 0; i < IPW_NDATA; i++) {
618ceaec73dSDamien Bergamini shdr = &sc->shdr_list[i];
619ceaec73dSDamien Bergamini error = bus_dmamap_create(sc->hdr_dmat, 0, &shdr->map);
620ceaec73dSDamien Bergamini if (error != 0) {
621ceaec73dSDamien Bergamini device_printf(sc->sc_dev,
622ceaec73dSDamien Bergamini "could not create header DMA map\n");
623ceaec73dSDamien Bergamini goto fail;
624ceaec73dSDamien Bergamini }
625ceaec73dSDamien Bergamini SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
626ceaec73dSDamien Bergamini }
627ceaec73dSDamien Bergamini
628ceaec73dSDamien Bergamini /*
629ceaec73dSDamien Bergamini * Allocate tx buffers DMA maps.
630ceaec73dSDamien Bergamini */
63162ce43ccSScott Long error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
632ceaec73dSDamien Bergamini BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, IPW_MAX_NSEG, MCLBYTES, 0,
633ceaec73dSDamien Bergamini NULL, NULL, &sc->txbuf_dmat);
634ceaec73dSDamien Bergamini if (error != 0) {
635ceaec73dSDamien Bergamini device_printf(sc->sc_dev, "could not create tx DMA tag\n");
636ceaec73dSDamien Bergamini goto fail;
637ceaec73dSDamien Bergamini }
638ceaec73dSDamien Bergamini
639ceaec73dSDamien Bergamini SLIST_INIT(&sc->free_sbuf);
640ceaec73dSDamien Bergamini for (i = 0; i < IPW_NDATA; i++) {
641ceaec73dSDamien Bergamini sbuf = &sc->tx_sbuf_list[i];
642ceaec73dSDamien Bergamini error = bus_dmamap_create(sc->txbuf_dmat, 0, &sbuf->map);
643ceaec73dSDamien Bergamini if (error != 0) {
644ceaec73dSDamien Bergamini device_printf(sc->sc_dev,
645ceaec73dSDamien Bergamini "could not create tx DMA map\n");
646ceaec73dSDamien Bergamini goto fail;
647ceaec73dSDamien Bergamini }
648ceaec73dSDamien Bergamini SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
649ceaec73dSDamien Bergamini }
650ceaec73dSDamien Bergamini
651ceaec73dSDamien Bergamini /*
652ceaec73dSDamien Bergamini * Initialize tx ring.
653ceaec73dSDamien Bergamini */
654ceaec73dSDamien Bergamini for (i = 0; i < IPW_NTBD; i++) {
655ceaec73dSDamien Bergamini sbd = &sc->stbd_list[i];
656ceaec73dSDamien Bergamini sbd->bd = &sc->tbd_list[i];
657ceaec73dSDamien Bergamini sbd->type = IPW_SBD_TYPE_NOASSOC;
658ceaec73dSDamien Bergamini }
659ceaec73dSDamien Bergamini
660ceaec73dSDamien Bergamini /*
661ceaec73dSDamien Bergamini * Pre-allocate rx buffers and DMA maps.
662ceaec73dSDamien Bergamini */
66362ce43ccSScott Long error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
66469e2ffe6SDamien Bergamini BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0, NULL,
66569e2ffe6SDamien Bergamini NULL, &sc->rxbuf_dmat);
666ceaec73dSDamien Bergamini if (error != 0) {
667ceaec73dSDamien Bergamini device_printf(sc->sc_dev, "could not create rx DMA tag\n");
668ceaec73dSDamien Bergamini goto fail;
669ceaec73dSDamien Bergamini }
670ceaec73dSDamien Bergamini
671ceaec73dSDamien Bergamini for (i = 0; i < IPW_NRBD; i++) {
672ceaec73dSDamien Bergamini sbd = &sc->srbd_list[i];
673ceaec73dSDamien Bergamini sbuf = &sc->rx_sbuf_list[i];
674ceaec73dSDamien Bergamini sbd->bd = &sc->rbd_list[i];
675ceaec73dSDamien Bergamini
676c6499eccSGleb Smirnoff sbuf->m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
677ceaec73dSDamien Bergamini if (sbuf->m == NULL) {
678ceaec73dSDamien Bergamini device_printf(sc->sc_dev,
679ceaec73dSDamien Bergamini "could not allocate rx mbuf\n");
680ceaec73dSDamien Bergamini error = ENOMEM;
681ceaec73dSDamien Bergamini goto fail;
682ceaec73dSDamien Bergamini }
683ceaec73dSDamien Bergamini
684ceaec73dSDamien Bergamini error = bus_dmamap_create(sc->rxbuf_dmat, 0, &sbuf->map);
685ceaec73dSDamien Bergamini if (error != 0) {
686ceaec73dSDamien Bergamini device_printf(sc->sc_dev,
687ceaec73dSDamien Bergamini "could not create rx DMA map\n");
688ceaec73dSDamien Bergamini goto fail;
689ceaec73dSDamien Bergamini }
690ceaec73dSDamien Bergamini
691ceaec73dSDamien Bergamini error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
692ceaec73dSDamien Bergamini mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
693ceaec73dSDamien Bergamini &physaddr, 0);
694ceaec73dSDamien Bergamini if (error != 0) {
695ceaec73dSDamien Bergamini device_printf(sc->sc_dev,
696ceaec73dSDamien Bergamini "could not map rx DMA memory\n");
697ceaec73dSDamien Bergamini goto fail;
698ceaec73dSDamien Bergamini }
699ceaec73dSDamien Bergamini
700ceaec73dSDamien Bergamini sbd->type = IPW_SBD_TYPE_DATA;
701ceaec73dSDamien Bergamini sbd->priv = sbuf;
702ceaec73dSDamien Bergamini sbd->bd->physaddr = htole32(physaddr);
703ceaec73dSDamien Bergamini sbd->bd->len = htole32(MCLBYTES);
704ceaec73dSDamien Bergamini }
705ceaec73dSDamien Bergamini
706ceaec73dSDamien Bergamini bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
707ceaec73dSDamien Bergamini
708ceaec73dSDamien Bergamini return 0;
709ceaec73dSDamien Bergamini
710ceaec73dSDamien Bergamini fail: ipw_release(sc);
711ceaec73dSDamien Bergamini return error;
712ceaec73dSDamien Bergamini }
713ceaec73dSDamien Bergamini
714ceaec73dSDamien Bergamini static void
ipw_release(struct ipw_softc * sc)715ceaec73dSDamien Bergamini ipw_release(struct ipw_softc *sc)
716ceaec73dSDamien Bergamini {
717ceaec73dSDamien Bergamini struct ipw_soft_buf *sbuf;
718ceaec73dSDamien Bergamini int i;
719ceaec73dSDamien Bergamini
72062ce43ccSScott Long if (sc->parent_dmat != NULL) {
72162ce43ccSScott Long bus_dma_tag_destroy(sc->parent_dmat);
72262ce43ccSScott Long }
72362ce43ccSScott Long
724ceaec73dSDamien Bergamini if (sc->tbd_dmat != NULL) {
725ceaec73dSDamien Bergamini bus_dmamap_unload(sc->tbd_dmat, sc->tbd_map);
72619de5f4eSGleb Smirnoff bus_dmamem_free(sc->tbd_dmat, sc->tbd_list, sc->tbd_map);
727ceaec73dSDamien Bergamini bus_dma_tag_destroy(sc->tbd_dmat);
728ceaec73dSDamien Bergamini }
729ceaec73dSDamien Bergamini
730ceaec73dSDamien Bergamini if (sc->rbd_dmat != NULL) {
731ceaec73dSDamien Bergamini if (sc->rbd_list != NULL) {
732ceaec73dSDamien Bergamini bus_dmamap_unload(sc->rbd_dmat, sc->rbd_map);
733ceaec73dSDamien Bergamini bus_dmamem_free(sc->rbd_dmat, sc->rbd_list,
734ceaec73dSDamien Bergamini sc->rbd_map);
735ceaec73dSDamien Bergamini }
736ceaec73dSDamien Bergamini bus_dma_tag_destroy(sc->rbd_dmat);
737ceaec73dSDamien Bergamini }
738ceaec73dSDamien Bergamini
739ceaec73dSDamien Bergamini if (sc->status_dmat != NULL) {
740ceaec73dSDamien Bergamini if (sc->status_list != NULL) {
741ceaec73dSDamien Bergamini bus_dmamap_unload(sc->status_dmat, sc->status_map);
742ceaec73dSDamien Bergamini bus_dmamem_free(sc->status_dmat, sc->status_list,
743ceaec73dSDamien Bergamini sc->status_map);
744ceaec73dSDamien Bergamini }
745ceaec73dSDamien Bergamini bus_dma_tag_destroy(sc->status_dmat);
746ceaec73dSDamien Bergamini }
747ceaec73dSDamien Bergamini
748ceaec73dSDamien Bergamini for (i = 0; i < IPW_NTBD; i++)
749ceaec73dSDamien Bergamini ipw_release_sbd(sc, &sc->stbd_list[i]);
750ceaec73dSDamien Bergamini
751ceaec73dSDamien Bergamini if (sc->cmd_dmat != NULL) {
752ceaec73dSDamien Bergamini bus_dmamap_destroy(sc->cmd_dmat, sc->cmd_map);
753ceaec73dSDamien Bergamini bus_dma_tag_destroy(sc->cmd_dmat);
754ceaec73dSDamien Bergamini }
755ceaec73dSDamien Bergamini
756ceaec73dSDamien Bergamini if (sc->hdr_dmat != NULL) {
757ceaec73dSDamien Bergamini for (i = 0; i < IPW_NDATA; i++)
758ceaec73dSDamien Bergamini bus_dmamap_destroy(sc->hdr_dmat, sc->shdr_list[i].map);
759ceaec73dSDamien Bergamini bus_dma_tag_destroy(sc->hdr_dmat);
760ceaec73dSDamien Bergamini }
761ceaec73dSDamien Bergamini
762ceaec73dSDamien Bergamini if (sc->txbuf_dmat != NULL) {
763ceaec73dSDamien Bergamini for (i = 0; i < IPW_NDATA; i++) {
764ceaec73dSDamien Bergamini bus_dmamap_destroy(sc->txbuf_dmat,
765ceaec73dSDamien Bergamini sc->tx_sbuf_list[i].map);
766ceaec73dSDamien Bergamini }
767ceaec73dSDamien Bergamini bus_dma_tag_destroy(sc->txbuf_dmat);
768ceaec73dSDamien Bergamini }
769ceaec73dSDamien Bergamini
770ceaec73dSDamien Bergamini if (sc->rxbuf_dmat != NULL) {
771ceaec73dSDamien Bergamini for (i = 0; i < IPW_NRBD; i++) {
772ceaec73dSDamien Bergamini sbuf = &sc->rx_sbuf_list[i];
773ceaec73dSDamien Bergamini if (sbuf->m != NULL) {
774ceaec73dSDamien Bergamini bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map,
775ceaec73dSDamien Bergamini BUS_DMASYNC_POSTREAD);
776ceaec73dSDamien Bergamini bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
777ceaec73dSDamien Bergamini m_freem(sbuf->m);
778ceaec73dSDamien Bergamini }
779ceaec73dSDamien Bergamini bus_dmamap_destroy(sc->rxbuf_dmat, sbuf->map);
780ceaec73dSDamien Bergamini }
781ceaec73dSDamien Bergamini bus_dma_tag_destroy(sc->rxbuf_dmat);
782ceaec73dSDamien Bergamini }
783ceaec73dSDamien Bergamini }
784ceaec73dSDamien Bergamini
785ceaec73dSDamien Bergamini static int
ipw_shutdown(device_t dev)786ceaec73dSDamien Bergamini ipw_shutdown(device_t dev)
787ceaec73dSDamien Bergamini {
788ceaec73dSDamien Bergamini struct ipw_softc *sc = device_get_softc(dev);
789ceaec73dSDamien Bergamini
790ceaec73dSDamien Bergamini ipw_stop(sc);
791ceaec73dSDamien Bergamini
792ceaec73dSDamien Bergamini return 0;
793ceaec73dSDamien Bergamini }
794ceaec73dSDamien Bergamini
795ceaec73dSDamien Bergamini static int
ipw_suspend(device_t dev)796ceaec73dSDamien Bergamini ipw_suspend(device_t dev)
797ceaec73dSDamien Bergamini {
798ceaec73dSDamien Bergamini struct ipw_softc *sc = device_get_softc(dev);
7997a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
800ceaec73dSDamien Bergamini
801ada977b1SBernhard Schmidt ieee80211_suspend_all(ic);
802ceaec73dSDamien Bergamini return 0;
803ceaec73dSDamien Bergamini }
804ceaec73dSDamien Bergamini
805ceaec73dSDamien Bergamini static int
ipw_resume(device_t dev)806ceaec73dSDamien Bergamini ipw_resume(device_t dev)
807ceaec73dSDamien Bergamini {
808ceaec73dSDamien Bergamini struct ipw_softc *sc = device_get_softc(dev);
8097a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
810ceaec73dSDamien Bergamini
8110e22d2c5SDamien Bergamini pci_write_config(dev, 0x41, 0, 1);
8120e22d2c5SDamien Bergamini
813ada977b1SBernhard Schmidt ieee80211_resume_all(ic);
814ceaec73dSDamien Bergamini return 0;
815ceaec73dSDamien Bergamini }
816ceaec73dSDamien Bergamini
817ceaec73dSDamien Bergamini static int
ipw_cvtrate(int ipwrate)81888d3b172SAndrew Thompson ipw_cvtrate(int ipwrate)
81988d3b172SAndrew Thompson {
82088d3b172SAndrew Thompson switch (ipwrate) {
82188d3b172SAndrew Thompson case IPW_RATE_DS1: return 2;
82288d3b172SAndrew Thompson case IPW_RATE_DS2: return 4;
82388d3b172SAndrew Thompson case IPW_RATE_DS5: return 11;
82488d3b172SAndrew Thompson case IPW_RATE_DS11: return 22;
82588d3b172SAndrew Thompson }
826ceaec73dSDamien Bergamini return 0;
827ceaec73dSDamien Bergamini }
828ceaec73dSDamien Bergamini
829ceaec73dSDamien Bergamini /*
83084ca7670SDamien Bergamini * The firmware automatically adapts the transmit speed. We report its current
83184ca7670SDamien Bergamini * value here.
832ceaec73dSDamien Bergamini */
833ceaec73dSDamien Bergamini static void
ipw_media_status(if_t ifp,struct ifmediareq * imr)834ec22a3a2SJustin Hibbits ipw_media_status(if_t ifp, struct ifmediareq *imr)
835ceaec73dSDamien Bergamini {
836ec22a3a2SJustin Hibbits struct ieee80211vap *vap = if_getsoftc(ifp);
837b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic;
8382cb71df1SAdrian Chadd struct ipw_softc *sc = ic->ic_softc;
839ceaec73dSDamien Bergamini
840ceaec73dSDamien Bergamini /* read current transmission rate from adapter */
841*70674500SAdrian Chadd ieee80211_node_set_txrate_dot11rate(vap->iv_bss,
842*70674500SAdrian Chadd ipw_cvtrate(ipw_read_table1(sc, IPW_INFO_CURRENT_TX_RATE) & 0xf));
843b032f27cSSam Leffler ieee80211_media_status(ifp, imr);
844ceaec73dSDamien Bergamini }
845ceaec73dSDamien Bergamini
846ceaec73dSDamien Bergamini static int
ipw_newstate(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)847b032f27cSSam Leffler ipw_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
848ceaec73dSDamien Bergamini {
849b032f27cSSam Leffler struct ipw_vap *ivp = IPW_VAP(vap);
850b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic;
8512cb71df1SAdrian Chadd struct ipw_softc *sc = ic->ic_softc;
8525efea30fSAndrew Thompson enum ieee80211_state ostate;
85388d3b172SAndrew Thompson
85488d3b172SAndrew Thompson DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
855b032f27cSSam Leffler ieee80211_state_name[vap->iv_state],
85688d3b172SAndrew Thompson ieee80211_state_name[nstate], sc->flags));
857ceaec73dSDamien Bergamini
8585efea30fSAndrew Thompson ostate = vap->iv_state;
8595efea30fSAndrew Thompson IEEE80211_UNLOCK(ic);
8605efea30fSAndrew Thompson
861ceaec73dSDamien Bergamini switch (nstate) {
862ceaec73dSDamien Bergamini case IEEE80211_S_RUN:
86388d3b172SAndrew Thompson if (ic->ic_opmode == IEEE80211_M_IBSS) {
86488d3b172SAndrew Thompson /*
86588d3b172SAndrew Thompson * XXX when joining an ibss network we are called
86688d3b172SAndrew Thompson * with a SCAN -> RUN transition on scan complete.
867d02ae91aSBernhard Schmidt * Use that to call ipw_assoc. On completing the
868d02ae91aSBernhard Schmidt * join we are then called again with an AUTH -> RUN
869d02ae91aSBernhard Schmidt * transition and we want to do nothing. This is
870d02ae91aSBernhard Schmidt * all totally bogus and needs to be redone.
87188d3b172SAndrew Thompson */
8725efea30fSAndrew Thompson if (ostate == IEEE80211_S_SCAN)
8735efea30fSAndrew Thompson ipw_assoc(ic, vap);
87488d3b172SAndrew Thompson }
875ceaec73dSDamien Bergamini break;
876ceaec73dSDamien Bergamini
877ceaec73dSDamien Bergamini case IEEE80211_S_INIT:
87888d3b172SAndrew Thompson if (sc->flags & IPW_FLAG_ASSOCIATED)
8795efea30fSAndrew Thompson ipw_disassoc(ic, vap);
88088d3b172SAndrew Thompson break;
88188d3b172SAndrew Thompson
882ceaec73dSDamien Bergamini case IEEE80211_S_AUTH:
883557c25b4SBernhard Schmidt /*
884557c25b4SBernhard Schmidt * Move to ASSOC state after the ipw_assoc() call. Firmware
885557c25b4SBernhard Schmidt * takes care of authentication, after the call we'll receive
886557c25b4SBernhard Schmidt * only an assoc response which would otherwise be discared
887557c25b4SBernhard Schmidt * if we are still in AUTH state.
888557c25b4SBernhard Schmidt */
889557c25b4SBernhard Schmidt nstate = IEEE80211_S_ASSOC;
8905efea30fSAndrew Thompson ipw_assoc(ic, vap);
8915efea30fSAndrew Thompson break;
89288d3b172SAndrew Thompson
893ceaec73dSDamien Bergamini case IEEE80211_S_ASSOC:
89488d3b172SAndrew Thompson /*
895d02ae91aSBernhard Schmidt * If we are not transitioning from AUTH then resend the
89688d3b172SAndrew Thompson * association request.
89788d3b172SAndrew Thompson */
8985efea30fSAndrew Thompson if (ostate != IEEE80211_S_AUTH)
8995efea30fSAndrew Thompson ipw_assoc(ic, vap);
90088d3b172SAndrew Thompson break;
90188d3b172SAndrew Thompson
90214fb6b8fSSam Leffler default:
903ceaec73dSDamien Bergamini break;
904ceaec73dSDamien Bergamini }
9055efea30fSAndrew Thompson IEEE80211_LOCK(ic);
906b032f27cSSam Leffler return ivp->newstate(vap, nstate, arg);
907ceaec73dSDamien Bergamini }
908ceaec73dSDamien Bergamini
909ceaec73dSDamien Bergamini /*
910ceaec73dSDamien Bergamini * Read 16 bits at address 'addr' from the serial EEPROM.
911ceaec73dSDamien Bergamini */
912ceaec73dSDamien Bergamini static uint16_t
ipw_read_prom_word(struct ipw_softc * sc,uint8_t addr)913ceaec73dSDamien Bergamini ipw_read_prom_word(struct ipw_softc *sc, uint8_t addr)
914ceaec73dSDamien Bergamini {
915ceaec73dSDamien Bergamini uint32_t tmp;
916ceaec73dSDamien Bergamini uint16_t val;
917ceaec73dSDamien Bergamini int n;
918ceaec73dSDamien Bergamini
919ceaec73dSDamien Bergamini /* clock C once before the first command */
920ceaec73dSDamien Bergamini IPW_EEPROM_CTL(sc, 0);
921ceaec73dSDamien Bergamini IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
922ceaec73dSDamien Bergamini IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
923ceaec73dSDamien Bergamini IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
924ceaec73dSDamien Bergamini
925ceaec73dSDamien Bergamini /* write start bit (1) */
926ceaec73dSDamien Bergamini IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
927ceaec73dSDamien Bergamini IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
928ceaec73dSDamien Bergamini
929ceaec73dSDamien Bergamini /* write READ opcode (10) */
930ceaec73dSDamien Bergamini IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
931ceaec73dSDamien Bergamini IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
932ceaec73dSDamien Bergamini IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
933ceaec73dSDamien Bergamini IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
934ceaec73dSDamien Bergamini
935ceaec73dSDamien Bergamini /* write address A7-A0 */
936ceaec73dSDamien Bergamini for (n = 7; n >= 0; n--) {
937ceaec73dSDamien Bergamini IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
938ceaec73dSDamien Bergamini (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D));
939ceaec73dSDamien Bergamini IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
940ceaec73dSDamien Bergamini (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D) | IPW_EEPROM_C);
941ceaec73dSDamien Bergamini }
942ceaec73dSDamien Bergamini
943ceaec73dSDamien Bergamini IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
944ceaec73dSDamien Bergamini
945ceaec73dSDamien Bergamini /* read data Q15-Q0 */
946ceaec73dSDamien Bergamini val = 0;
947ceaec73dSDamien Bergamini for (n = 15; n >= 0; n--) {
948ceaec73dSDamien Bergamini IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
949ceaec73dSDamien Bergamini IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
950ceaec73dSDamien Bergamini tmp = MEM_READ_4(sc, IPW_MEM_EEPROM_CTL);
951ceaec73dSDamien Bergamini val |= ((tmp & IPW_EEPROM_Q) >> IPW_EEPROM_SHIFT_Q) << n;
952ceaec73dSDamien Bergamini }
953ceaec73dSDamien Bergamini
954ceaec73dSDamien Bergamini IPW_EEPROM_CTL(sc, 0);
955ceaec73dSDamien Bergamini
956ceaec73dSDamien Bergamini /* clear Chip Select and clock C */
957ceaec73dSDamien Bergamini IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
958ceaec73dSDamien Bergamini IPW_EEPROM_CTL(sc, 0);
959ceaec73dSDamien Bergamini IPW_EEPROM_CTL(sc, IPW_EEPROM_C);
960ceaec73dSDamien Bergamini
961ceaec73dSDamien Bergamini return le16toh(val);
962ceaec73dSDamien Bergamini }
963ceaec73dSDamien Bergamini
9640587ad6bSAndriy Voskoboinyk static uint16_t
ipw_read_chanmask(struct ipw_softc * sc)9650587ad6bSAndriy Voskoboinyk ipw_read_chanmask(struct ipw_softc *sc)
9660587ad6bSAndriy Voskoboinyk {
9670587ad6bSAndriy Voskoboinyk uint16_t val;
9680587ad6bSAndriy Voskoboinyk
9690587ad6bSAndriy Voskoboinyk /* set supported .11b channels (read from EEPROM) */
9700587ad6bSAndriy Voskoboinyk if ((val = ipw_read_prom_word(sc, IPW_EEPROM_CHANNEL_LIST)) == 0)
9710587ad6bSAndriy Voskoboinyk val = 0x7ff; /* default to channels 1-11 */
9720587ad6bSAndriy Voskoboinyk val <<= 1;
9730587ad6bSAndriy Voskoboinyk
9740587ad6bSAndriy Voskoboinyk return (val);
9750587ad6bSAndriy Voskoboinyk }
9760587ad6bSAndriy Voskoboinyk
977ceaec73dSDamien Bergamini static void
ipw_rx_cmd_intr(struct ipw_softc * sc,struct ipw_soft_buf * sbuf)97888d3b172SAndrew Thompson ipw_rx_cmd_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
979ceaec73dSDamien Bergamini {
980ceaec73dSDamien Bergamini struct ipw_cmd *cmd;
981ceaec73dSDamien Bergamini
982ceaec73dSDamien Bergamini bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
983ceaec73dSDamien Bergamini
984ceaec73dSDamien Bergamini cmd = mtod(sbuf->m, struct ipw_cmd *);
985ceaec73dSDamien Bergamini
98688d3b172SAndrew Thompson DPRINTFN(9, ("cmd ack'ed %s(%u, %u, %u, %u, %u)\n",
98788d3b172SAndrew Thompson ipw_cmdname(le32toh(cmd->type)), le32toh(cmd->type),
988ceaec73dSDamien Bergamini le32toh(cmd->subtype), le32toh(cmd->seq), le32toh(cmd->len),
989ceaec73dSDamien Bergamini le32toh(cmd->status)));
990ceaec73dSDamien Bergamini
99188d3b172SAndrew Thompson sc->flags &= ~IPW_FLAG_BUSY;
992ceaec73dSDamien Bergamini wakeup(sc);
993ceaec73dSDamien Bergamini }
994ceaec73dSDamien Bergamini
995ceaec73dSDamien Bergamini static void
ipw_rx_newstate_intr(struct ipw_softc * sc,struct ipw_soft_buf * sbuf)99688d3b172SAndrew Thompson ipw_rx_newstate_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
997ceaec73dSDamien Bergamini {
998b032f27cSSam Leffler #define IEEESTATE(vap) ieee80211_state_name[vap->iv_state]
9997a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
1000b032f27cSSam Leffler struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1001ceaec73dSDamien Bergamini uint32_t state;
1002ceaec73dSDamien Bergamini
1003ceaec73dSDamien Bergamini bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1004ceaec73dSDamien Bergamini
1005ceaec73dSDamien Bergamini state = le32toh(*mtod(sbuf->m, uint32_t *));
1006ceaec73dSDamien Bergamini
1007ceaec73dSDamien Bergamini switch (state) {
1008ceaec73dSDamien Bergamini case IPW_STATE_ASSOCIATED:
100988d3b172SAndrew Thompson DPRINTFN(2, ("Association succeeded (%s flags 0x%x)\n",
1010b032f27cSSam Leffler IEEESTATE(vap), sc->flags));
101188d3b172SAndrew Thompson /* XXX suppress state change in case the fw auto-associates */
1012b032f27cSSam Leffler if ((sc->flags & IPW_FLAG_ASSOCIATING) == 0) {
1013b032f27cSSam Leffler DPRINTF(("Unexpected association (%s, flags 0x%x)\n",
1014b032f27cSSam Leffler IEEESTATE(vap), sc->flags));
1015b032f27cSSam Leffler break;
1016b032f27cSSam Leffler }
1017b032f27cSSam Leffler sc->flags &= ~IPW_FLAG_ASSOCIATING;
1018b032f27cSSam Leffler sc->flags |= IPW_FLAG_ASSOCIATED;
1019ceaec73dSDamien Bergamini break;
1020ceaec73dSDamien Bergamini
1021ceaec73dSDamien Bergamini case IPW_STATE_SCANNING:
102288d3b172SAndrew Thompson DPRINTFN(3, ("Scanning (%s flags 0x%x)\n",
1023b032f27cSSam Leffler IEEESTATE(vap), sc->flags));
102488d3b172SAndrew Thompson /*
102588d3b172SAndrew Thompson * NB: Check driver state for association on assoc
102688d3b172SAndrew Thompson * loss as the firmware will immediately start to
102788d3b172SAndrew Thompson * scan and we would treat it as a beacon miss if
102888d3b172SAndrew Thompson * we checked the 802.11 layer state.
102988d3b172SAndrew Thompson */
1030b032f27cSSam Leffler if (sc->flags & IPW_FLAG_ASSOCIATED) {
103182ff07c0SBernhard Schmidt IPW_UNLOCK(sc);
1032b032f27cSSam Leffler /* XXX probably need to issue disassoc to fw */
10335efea30fSAndrew Thompson ieee80211_beacon_miss(ic);
103482ff07c0SBernhard Schmidt IPW_LOCK(sc);
1035b032f27cSSam Leffler }
1036ceaec73dSDamien Bergamini break;
1037ceaec73dSDamien Bergamini
1038ceaec73dSDamien Bergamini case IPW_STATE_SCAN_COMPLETE:
103988d3b172SAndrew Thompson /*
104088d3b172SAndrew Thompson * XXX For some reason scan requests generate scan
104188d3b172SAndrew Thompson * started + scan done events before any traffic is
104288d3b172SAndrew Thompson * received (e.g. probe response frames). We work
104388d3b172SAndrew Thompson * around this by marking the HACK flag and skipping
104488d3b172SAndrew Thompson * the first scan complete event.
104588d3b172SAndrew Thompson */
1046b032f27cSSam Leffler DPRINTFN(3, ("Scan complete (%s flags 0x%x)\n",
1047b032f27cSSam Leffler IEEESTATE(vap), sc->flags));
104888d3b172SAndrew Thompson if (sc->flags & IPW_FLAG_HACK) {
104988d3b172SAndrew Thompson sc->flags &= ~IPW_FLAG_HACK;
105088d3b172SAndrew Thompson break;
105188d3b172SAndrew Thompson }
105288d3b172SAndrew Thompson if (sc->flags & IPW_FLAG_SCANNING) {
105382ff07c0SBernhard Schmidt IPW_UNLOCK(sc);
10545efea30fSAndrew Thompson ieee80211_scan_done(vap);
105582ff07c0SBernhard Schmidt IPW_LOCK(sc);
105688d3b172SAndrew Thompson sc->flags &= ~IPW_FLAG_SCANNING;
105788d3b172SAndrew Thompson sc->sc_scan_timer = 0;
105888d3b172SAndrew Thompson }
1059ceaec73dSDamien Bergamini break;
1060ceaec73dSDamien Bergamini
1061ceaec73dSDamien Bergamini case IPW_STATE_ASSOCIATION_LOST:
106288d3b172SAndrew Thompson DPRINTFN(2, ("Association lost (%s flags 0x%x)\n",
1063b032f27cSSam Leffler IEEESTATE(vap), sc->flags));
1064b032f27cSSam Leffler sc->flags &= ~(IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
106582ff07c0SBernhard Schmidt if (vap->iv_state == IEEE80211_S_RUN) {
106682ff07c0SBernhard Schmidt IPW_UNLOCK(sc);
10675efea30fSAndrew Thompson ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
106882ff07c0SBernhard Schmidt IPW_LOCK(sc);
106982ff07c0SBernhard Schmidt }
107088d3b172SAndrew Thompson break;
107188d3b172SAndrew Thompson
107288d3b172SAndrew Thompson case IPW_STATE_DISABLED:
1073b032f27cSSam Leffler /* XXX? is this right? */
1074b032f27cSSam Leffler sc->flags &= ~(IPW_FLAG_HACK | IPW_FLAG_SCANNING |
1075b032f27cSSam Leffler IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
107688d3b172SAndrew Thompson DPRINTFN(2, ("Firmware disabled (%s flags 0x%x)\n",
1077b032f27cSSam Leffler IEEESTATE(vap), sc->flags));
1078ceaec73dSDamien Bergamini break;
1079ceaec73dSDamien Bergamini
1080ceaec73dSDamien Bergamini case IPW_STATE_RADIO_DISABLED:
1081b032f27cSSam Leffler device_printf(sc->sc_dev, "radio turned off\n");
1082b032f27cSSam Leffler ieee80211_notify_radio(ic, 0);
108388d3b172SAndrew Thompson ipw_stop_locked(sc);
1084b032f27cSSam Leffler /* XXX start polling thread to detect radio on */
108588d3b172SAndrew Thompson break;
108688d3b172SAndrew Thompson
108788d3b172SAndrew Thompson default:
108888d3b172SAndrew Thompson DPRINTFN(2, ("%s: unhandled state %u %s flags 0x%x\n",
1089b032f27cSSam Leffler __func__, state, IEEESTATE(vap), sc->flags));
1090ceaec73dSDamien Bergamini break;
1091ceaec73dSDamien Bergamini }
109288d3b172SAndrew Thompson #undef IEEESTATE
109388d3b172SAndrew Thompson }
109488d3b172SAndrew Thompson
109588d3b172SAndrew Thompson /*
109688d3b172SAndrew Thompson * Set driver state for current channel.
109788d3b172SAndrew Thompson */
109888d3b172SAndrew Thompson static void
ipw_setcurchan(struct ipw_softc * sc,struct ieee80211_channel * chan)109988d3b172SAndrew Thompson ipw_setcurchan(struct ipw_softc *sc, struct ieee80211_channel *chan)
110088d3b172SAndrew Thompson {
11017a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
110288d3b172SAndrew Thompson
110388d3b172SAndrew Thompson ic->ic_curchan = chan;
11045463c4a4SSam Leffler ieee80211_radiotap_chan_change(ic);
1105ceaec73dSDamien Bergamini }
1106ceaec73dSDamien Bergamini
1107ceaec73dSDamien Bergamini /*
1108ceaec73dSDamien Bergamini * XXX: Hack to set the current channel to the value advertised in beacons or
1109ceaec73dSDamien Bergamini * probe responses. Only used during AP detection.
1110ceaec73dSDamien Bergamini */
1111ceaec73dSDamien Bergamini static void
ipw_fix_channel(struct ipw_softc * sc,struct mbuf * m)111288d3b172SAndrew Thompson ipw_fix_channel(struct ipw_softc *sc, struct mbuf *m)
1113ceaec73dSDamien Bergamini {
11147a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
111588d3b172SAndrew Thompson struct ieee80211_channel *c;
1116ceaec73dSDamien Bergamini struct ieee80211_frame *wh;
1117ceaec73dSDamien Bergamini uint8_t subtype;
1118ceaec73dSDamien Bergamini uint8_t *frm, *efrm;
1119ceaec73dSDamien Bergamini
1120ceaec73dSDamien Bergamini wh = mtod(m, struct ieee80211_frame *);
1121ceaec73dSDamien Bergamini
1122c249cc38SAdrian Chadd if (!IEEE80211_IS_MGMT(wh))
1123ceaec73dSDamien Bergamini return;
1124ceaec73dSDamien Bergamini
1125ceaec73dSDamien Bergamini subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1126ceaec73dSDamien Bergamini
1127ceaec73dSDamien Bergamini if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
1128ceaec73dSDamien Bergamini subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1129ceaec73dSDamien Bergamini return;
1130ceaec73dSDamien Bergamini
1131b032f27cSSam Leffler /* XXX use ieee80211_parse_beacon */
1132ceaec73dSDamien Bergamini frm = (uint8_t *)(wh + 1);
1133ceaec73dSDamien Bergamini efrm = mtod(m, uint8_t *) + m->m_len;
1134ceaec73dSDamien Bergamini
1135ceaec73dSDamien Bergamini frm += 12; /* skip tstamp, bintval and capinfo fields */
1136ceaec73dSDamien Bergamini while (frm < efrm) {
1137ceaec73dSDamien Bergamini if (*frm == IEEE80211_ELEMID_DSPARMS)
1138ceaec73dSDamien Bergamini #if IEEE80211_CHAN_MAX < 255
1139ceaec73dSDamien Bergamini if (frm[2] <= IEEE80211_CHAN_MAX)
1140ceaec73dSDamien Bergamini #endif
114188d3b172SAndrew Thompson {
114288d3b172SAndrew Thompson DPRINTF(("Fixing channel to %d\n", frm[2]));
114388d3b172SAndrew Thompson c = ieee80211_find_channel(ic,
114468e8e04eSSam Leffler ieee80211_ieee2mhz(frm[2], 0),
114588d3b172SAndrew Thompson IEEE80211_CHAN_B);
114688d3b172SAndrew Thompson if (c == NULL)
114788d3b172SAndrew Thompson c = &ic->ic_channels[0];
114888d3b172SAndrew Thompson ipw_setcurchan(sc, c);
114988d3b172SAndrew Thompson }
1150ceaec73dSDamien Bergamini
1151ceaec73dSDamien Bergamini frm += frm[1] + 2;
1152ceaec73dSDamien Bergamini }
1153ceaec73dSDamien Bergamini }
1154ceaec73dSDamien Bergamini
1155ceaec73dSDamien Bergamini static void
ipw_rx_data_intr(struct ipw_softc * sc,struct ipw_status * status,struct ipw_soft_bd * sbd,struct ipw_soft_buf * sbuf)115688d3b172SAndrew Thompson ipw_rx_data_intr(struct ipw_softc *sc, struct ipw_status *status,
1157ceaec73dSDamien Bergamini struct ipw_soft_bd *sbd, struct ipw_soft_buf *sbuf)
1158ceaec73dSDamien Bergamini {
11597a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
11604c4102ecSDamien Bergamini struct mbuf *mnew, *m;
1161ceaec73dSDamien Bergamini struct ieee80211_node *ni;
1162ceaec73dSDamien Bergamini bus_addr_t physaddr;
1163ceaec73dSDamien Bergamini int error;
11645463c4a4SSam Leffler int8_t rssi, nf;
1165ceaec73dSDamien Bergamini
11664c4102ecSDamien Bergamini DPRINTFN(5, ("received frame len=%u, rssi=%u\n", le32toh(status->len),
11674c4102ecSDamien Bergamini status->rssi));
11684c4102ecSDamien Bergamini
116909137879SDamien Bergamini if (le32toh(status->len) < sizeof (struct ieee80211_frame_min) ||
117009137879SDamien Bergamini le32toh(status->len) > MCLBYTES)
117109137879SDamien Bergamini return;
117209137879SDamien Bergamini
11734c4102ecSDamien Bergamini /*
11744c4102ecSDamien Bergamini * Try to allocate a new mbuf for this ring element and load it before
11754c4102ecSDamien Bergamini * processing the current mbuf. If the ring element cannot be loaded,
11764c4102ecSDamien Bergamini * drop the received packet and reuse the old mbuf. In the unlikely
11774c4102ecSDamien Bergamini * case that the old mbuf can't be reloaded either, explicitly panic.
11784c4102ecSDamien Bergamini */
1179c6499eccSGleb Smirnoff mnew = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
11804c4102ecSDamien Bergamini if (mnew == NULL) {
11817a79cebfSGleb Smirnoff counter_u64_add(ic->ic_ierrors, 1);
11824c4102ecSDamien Bergamini return;
11834c4102ecSDamien Bergamini }
11844c4102ecSDamien Bergamini
1185ceaec73dSDamien Bergamini bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1186ceaec73dSDamien Bergamini bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
1187ceaec73dSDamien Bergamini
11884c4102ecSDamien Bergamini error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map, mtod(mnew, void *),
11894c4102ecSDamien Bergamini MCLBYTES, ipw_dma_map_addr, &physaddr, 0);
11904c4102ecSDamien Bergamini if (error != 0) {
11914c4102ecSDamien Bergamini m_freem(mnew);
11924c4102ecSDamien Bergamini
11934c4102ecSDamien Bergamini /* try to reload the old mbuf */
11944c4102ecSDamien Bergamini error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
11954c4102ecSDamien Bergamini mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
11964c4102ecSDamien Bergamini &physaddr, 0);
11974c4102ecSDamien Bergamini if (error != 0) {
11984c4102ecSDamien Bergamini /* very unlikely that it will fail... */
11994c4102ecSDamien Bergamini panic("%s: could not load old rx mbuf",
12004c4102ecSDamien Bergamini device_get_name(sc->sc_dev));
12014c4102ecSDamien Bergamini }
12027a79cebfSGleb Smirnoff counter_u64_add(ic->ic_ierrors, 1);
12034c4102ecSDamien Bergamini return;
12044c4102ecSDamien Bergamini }
12054c4102ecSDamien Bergamini
12064c4102ecSDamien Bergamini /*
12074c4102ecSDamien Bergamini * New mbuf successfully loaded, update Rx ring and continue
12084c4102ecSDamien Bergamini * processing.
12094c4102ecSDamien Bergamini */
1210ceaec73dSDamien Bergamini m = sbuf->m;
12114c4102ecSDamien Bergamini sbuf->m = mnew;
12124c4102ecSDamien Bergamini sbd->bd->physaddr = htole32(physaddr);
1213ceaec73dSDamien Bergamini m->m_pkthdr.len = m->m_len = le32toh(status->len);
1214ceaec73dSDamien Bergamini
12155463c4a4SSam Leffler rssi = status->rssi + IPW_RSSI_TO_DBM;
12165463c4a4SSam Leffler nf = -95;
12175463c4a4SSam Leffler if (ieee80211_radiotap_active(ic)) {
1218ceaec73dSDamien Bergamini struct ipw_rx_radiotap_header *tap = &sc->sc_rxtap;
1219ceaec73dSDamien Bergamini
1220ceaec73dSDamien Bergamini tap->wr_flags = 0;
12215463c4a4SSam Leffler tap->wr_antsignal = rssi;
12225463c4a4SSam Leffler tap->wr_antnoise = nf;
1223ceaec73dSDamien Bergamini }
1224ceaec73dSDamien Bergamini
122588d3b172SAndrew Thompson if (sc->flags & IPW_FLAG_SCANNING)
122688d3b172SAndrew Thompson ipw_fix_channel(sc, m);
1227ceaec73dSDamien Bergamini
122888d3b172SAndrew Thompson IPW_UNLOCK(sc);
1229b032f27cSSam Leffler ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1230b032f27cSSam Leffler if (ni != NULL) {
1231892d2e9dSBernhard Schmidt (void) ieee80211_input(ni, m, rssi - nf, nf);
1232ceaec73dSDamien Bergamini ieee80211_free_node(ni);
1233b032f27cSSam Leffler } else
1234892d2e9dSBernhard Schmidt (void) ieee80211_input_all(ic, m, rssi - nf, nf);
123588d3b172SAndrew Thompson IPW_LOCK(sc);
1236ceaec73dSDamien Bergamini
1237ceaec73dSDamien Bergamini bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1238ceaec73dSDamien Bergamini }
1239ceaec73dSDamien Bergamini
1240ceaec73dSDamien Bergamini static void
ipw_rx_intr(struct ipw_softc * sc)1241ceaec73dSDamien Bergamini ipw_rx_intr(struct ipw_softc *sc)
1242ceaec73dSDamien Bergamini {
1243ceaec73dSDamien Bergamini struct ipw_status *status;
1244ceaec73dSDamien Bergamini struct ipw_soft_bd *sbd;
1245ceaec73dSDamien Bergamini struct ipw_soft_buf *sbuf;
1246ceaec73dSDamien Bergamini uint32_t r, i;
1247ceaec73dSDamien Bergamini
1248ceaec73dSDamien Bergamini if (!(sc->flags & IPW_FLAG_FW_INITED))
1249ceaec73dSDamien Bergamini return;
1250ceaec73dSDamien Bergamini
1251ceaec73dSDamien Bergamini r = CSR_READ_4(sc, IPW_CSR_RX_READ);
1252ceaec73dSDamien Bergamini
1253ceaec73dSDamien Bergamini bus_dmamap_sync(sc->status_dmat, sc->status_map, BUS_DMASYNC_POSTREAD);
1254ceaec73dSDamien Bergamini
1255ceaec73dSDamien Bergamini for (i = (sc->rxcur + 1) % IPW_NRBD; i != r; i = (i + 1) % IPW_NRBD) {
1256ceaec73dSDamien Bergamini status = &sc->status_list[i];
1257ceaec73dSDamien Bergamini sbd = &sc->srbd_list[i];
1258ceaec73dSDamien Bergamini sbuf = sbd->priv;
1259ceaec73dSDamien Bergamini
1260ceaec73dSDamien Bergamini switch (le16toh(status->code) & 0xf) {
1261ceaec73dSDamien Bergamini case IPW_STATUS_CODE_COMMAND:
126288d3b172SAndrew Thompson ipw_rx_cmd_intr(sc, sbuf);
1263ceaec73dSDamien Bergamini break;
1264ceaec73dSDamien Bergamini
1265ceaec73dSDamien Bergamini case IPW_STATUS_CODE_NEWSTATE:
126688d3b172SAndrew Thompson ipw_rx_newstate_intr(sc, sbuf);
1267ceaec73dSDamien Bergamini break;
1268ceaec73dSDamien Bergamini
1269ceaec73dSDamien Bergamini case IPW_STATUS_CODE_DATA_802_3:
1270ceaec73dSDamien Bergamini case IPW_STATUS_CODE_DATA_802_11:
127188d3b172SAndrew Thompson ipw_rx_data_intr(sc, status, sbd, sbuf);
1272ceaec73dSDamien Bergamini break;
1273ceaec73dSDamien Bergamini
1274ceaec73dSDamien Bergamini case IPW_STATUS_CODE_NOTIFICATION:
127588d3b172SAndrew Thompson DPRINTFN(2, ("notification status, len %u flags 0x%x\n",
127688d3b172SAndrew Thompson le32toh(status->len), status->flags));
1277b032f27cSSam Leffler /* XXX maybe drive state machine AUTH->ASSOC? */
1278ceaec73dSDamien Bergamini break;
1279ceaec73dSDamien Bergamini
1280ceaec73dSDamien Bergamini default:
128188d3b172SAndrew Thompson device_printf(sc->sc_dev, "unexpected status code %u\n",
1282ceaec73dSDamien Bergamini le16toh(status->code));
1283ceaec73dSDamien Bergamini }
1284ceaec73dSDamien Bergamini
1285ceaec73dSDamien Bergamini /* firmware was killed, stop processing received frames */
1286ceaec73dSDamien Bergamini if (!(sc->flags & IPW_FLAG_FW_INITED))
1287ceaec73dSDamien Bergamini return;
1288ceaec73dSDamien Bergamini
1289ceaec73dSDamien Bergamini sbd->bd->flags = 0;
1290ceaec73dSDamien Bergamini }
1291ceaec73dSDamien Bergamini
1292ceaec73dSDamien Bergamini bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1293ceaec73dSDamien Bergamini
1294ceaec73dSDamien Bergamini /* kick the firmware */
1295ceaec73dSDamien Bergamini sc->rxcur = (r == 0) ? IPW_NRBD - 1 : r - 1;
1296ceaec73dSDamien Bergamini CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
1297ceaec73dSDamien Bergamini }
1298ceaec73dSDamien Bergamini
1299ceaec73dSDamien Bergamini static void
ipw_release_sbd(struct ipw_softc * sc,struct ipw_soft_bd * sbd)1300ceaec73dSDamien Bergamini ipw_release_sbd(struct ipw_softc *sc, struct ipw_soft_bd *sbd)
1301ceaec73dSDamien Bergamini {
1302ceaec73dSDamien Bergamini struct ipw_soft_hdr *shdr;
1303ceaec73dSDamien Bergamini struct ipw_soft_buf *sbuf;
1304ceaec73dSDamien Bergamini
1305ceaec73dSDamien Bergamini switch (sbd->type) {
1306ceaec73dSDamien Bergamini case IPW_SBD_TYPE_COMMAND:
1307ceaec73dSDamien Bergamini bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map,
1308ceaec73dSDamien Bergamini BUS_DMASYNC_POSTWRITE);
1309ceaec73dSDamien Bergamini bus_dmamap_unload(sc->cmd_dmat, sc->cmd_map);
1310ceaec73dSDamien Bergamini break;
1311ceaec73dSDamien Bergamini
1312ceaec73dSDamien Bergamini case IPW_SBD_TYPE_HEADER:
1313ceaec73dSDamien Bergamini shdr = sbd->priv;
1314ceaec73dSDamien Bergamini bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_POSTWRITE);
1315ceaec73dSDamien Bergamini bus_dmamap_unload(sc->hdr_dmat, shdr->map);
1316ceaec73dSDamien Bergamini SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
1317ceaec73dSDamien Bergamini break;
1318ceaec73dSDamien Bergamini
1319ceaec73dSDamien Bergamini case IPW_SBD_TYPE_DATA:
1320ceaec73dSDamien Bergamini sbuf = sbd->priv;
1321ceaec73dSDamien Bergamini bus_dmamap_sync(sc->txbuf_dmat, sbuf->map,
1322ceaec73dSDamien Bergamini BUS_DMASYNC_POSTWRITE);
1323ceaec73dSDamien Bergamini bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1324ceaec73dSDamien Bergamini SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
1325ceaec73dSDamien Bergamini
13269b1a2971SAndriy Voskoboinyk ieee80211_tx_complete(sbuf->ni, sbuf->m, 0/*XXX*/);
1327ceaec73dSDamien Bergamini
1328ceaec73dSDamien Bergamini sc->sc_tx_timer = 0;
1329ceaec73dSDamien Bergamini break;
1330ceaec73dSDamien Bergamini }
1331ceaec73dSDamien Bergamini
1332ceaec73dSDamien Bergamini sbd->type = IPW_SBD_TYPE_NOASSOC;
1333ceaec73dSDamien Bergamini }
1334ceaec73dSDamien Bergamini
1335ceaec73dSDamien Bergamini static void
ipw_tx_intr(struct ipw_softc * sc)1336ceaec73dSDamien Bergamini ipw_tx_intr(struct ipw_softc *sc)
1337ceaec73dSDamien Bergamini {
1338ceaec73dSDamien Bergamini struct ipw_soft_bd *sbd;
1339ceaec73dSDamien Bergamini uint32_t r, i;
1340ceaec73dSDamien Bergamini
1341ceaec73dSDamien Bergamini if (!(sc->flags & IPW_FLAG_FW_INITED))
1342ceaec73dSDamien Bergamini return;
1343ceaec73dSDamien Bergamini
1344ceaec73dSDamien Bergamini r = CSR_READ_4(sc, IPW_CSR_TX_READ);
1345ceaec73dSDamien Bergamini
1346ceaec73dSDamien Bergamini for (i = (sc->txold + 1) % IPW_NTBD; i != r; i = (i + 1) % IPW_NTBD) {
1347ceaec73dSDamien Bergamini sbd = &sc->stbd_list[i];
1348ceaec73dSDamien Bergamini ipw_release_sbd(sc, sbd);
1349ceaec73dSDamien Bergamini sc->txfree++;
1350ceaec73dSDamien Bergamini }
1351ceaec73dSDamien Bergamini
1352ceaec73dSDamien Bergamini /* remember what the firmware has processed */
1353ceaec73dSDamien Bergamini sc->txold = (r == 0) ? IPW_NTBD - 1 : r - 1;
1354ceaec73dSDamien Bergamini
13557a79cebfSGleb Smirnoff ipw_start(sc);
1356ceaec73dSDamien Bergamini }
1357ceaec73dSDamien Bergamini
1358ceaec73dSDamien Bergamini static void
ipw_fatal_error_intr(struct ipw_softc * sc)13595efea30fSAndrew Thompson ipw_fatal_error_intr(struct ipw_softc *sc)
13605efea30fSAndrew Thompson {
13617a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
1362e1d2045eSAndrew Thompson struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
13635efea30fSAndrew Thompson
13645efea30fSAndrew Thompson device_printf(sc->sc_dev, "firmware error\n");
136582ff07c0SBernhard Schmidt if (vap != NULL) {
136682ff07c0SBernhard Schmidt IPW_UNLOCK(sc);
1367e1d2045eSAndrew Thompson ieee80211_cancel_scan(vap);
136882ff07c0SBernhard Schmidt IPW_LOCK(sc);
136982ff07c0SBernhard Schmidt }
13705efea30fSAndrew Thompson ieee80211_runtask(ic, &sc->sc_init_task);
13715efea30fSAndrew Thompson }
13725efea30fSAndrew Thompson
13735efea30fSAndrew Thompson static void
ipw_intr(void * arg)1374ceaec73dSDamien Bergamini ipw_intr(void *arg)
1375ceaec73dSDamien Bergamini {
1376ceaec73dSDamien Bergamini struct ipw_softc *sc = arg;
1377ceaec73dSDamien Bergamini uint32_t r;
1378ceaec73dSDamien Bergamini
137988d3b172SAndrew Thompson IPW_LOCK(sc);
1380ceaec73dSDamien Bergamini
13815efea30fSAndrew Thompson r = CSR_READ_4(sc, IPW_CSR_INTR);
13825efea30fSAndrew Thompson if (r == 0 || r == 0xffffffff)
13835efea30fSAndrew Thompson goto done;
1384ceaec73dSDamien Bergamini
1385ceaec73dSDamien Bergamini /* disable interrupts */
1386ceaec73dSDamien Bergamini CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1387ceaec73dSDamien Bergamini
138884ca7670SDamien Bergamini /* acknowledge all interrupts */
138984ca7670SDamien Bergamini CSR_WRITE_4(sc, IPW_CSR_INTR, r);
139084ca7670SDamien Bergamini
1391ceaec73dSDamien Bergamini if (r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)) {
13925efea30fSAndrew Thompson ipw_fatal_error_intr(sc);
13935efea30fSAndrew Thompson goto done;
1394ceaec73dSDamien Bergamini }
1395ceaec73dSDamien Bergamini
139684ca7670SDamien Bergamini if (r & IPW_INTR_FW_INIT_DONE)
1397ceaec73dSDamien Bergamini wakeup(sc);
1398ceaec73dSDamien Bergamini
1399ceaec73dSDamien Bergamini if (r & IPW_INTR_RX_TRANSFER)
1400ceaec73dSDamien Bergamini ipw_rx_intr(sc);
1401ceaec73dSDamien Bergamini
1402ceaec73dSDamien Bergamini if (r & IPW_INTR_TX_TRANSFER)
1403ceaec73dSDamien Bergamini ipw_tx_intr(sc);
1404ceaec73dSDamien Bergamini
1405ceaec73dSDamien Bergamini /* re-enable interrupts */
1406ceaec73dSDamien Bergamini CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
14075efea30fSAndrew Thompson done:
140888d3b172SAndrew Thompson IPW_UNLOCK(sc);
1409ceaec73dSDamien Bergamini }
1410ceaec73dSDamien Bergamini
1411ceaec73dSDamien Bergamini static void
ipw_dma_map_addr(void * arg,bus_dma_segment_t * segs,int nseg,int error)1412ceaec73dSDamien Bergamini ipw_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1413ceaec73dSDamien Bergamini {
1414ceaec73dSDamien Bergamini if (error != 0)
1415ceaec73dSDamien Bergamini return;
1416ceaec73dSDamien Bergamini
1417ceaec73dSDamien Bergamini KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
1418ceaec73dSDamien Bergamini
1419ceaec73dSDamien Bergamini *(bus_addr_t *)arg = segs[0].ds_addr;
1420ceaec73dSDamien Bergamini }
1421ceaec73dSDamien Bergamini
142288d3b172SAndrew Thompson static const char *
ipw_cmdname(int cmd)142388d3b172SAndrew Thompson ipw_cmdname(int cmd)
142488d3b172SAndrew Thompson {
142588d3b172SAndrew Thompson static const struct {
142688d3b172SAndrew Thompson int cmd;
142788d3b172SAndrew Thompson const char *name;
142888d3b172SAndrew Thompson } cmds[] = {
142988d3b172SAndrew Thompson { IPW_CMD_ADD_MULTICAST, "ADD_MULTICAST" },
143088d3b172SAndrew Thompson { IPW_CMD_BROADCAST_SCAN, "BROADCAST_SCAN" },
143188d3b172SAndrew Thompson { IPW_CMD_DISABLE, "DISABLE" },
143288d3b172SAndrew Thompson { IPW_CMD_DISABLE_PHY, "DISABLE_PHY" },
143388d3b172SAndrew Thompson { IPW_CMD_ENABLE, "ENABLE" },
143488d3b172SAndrew Thompson { IPW_CMD_PREPARE_POWER_DOWN, "PREPARE_POWER_DOWN" },
143588d3b172SAndrew Thompson { IPW_CMD_SET_BASIC_TX_RATES, "SET_BASIC_TX_RATES" },
143688d3b172SAndrew Thompson { IPW_CMD_SET_BEACON_INTERVAL, "SET_BEACON_INTERVAL" },
143788d3b172SAndrew Thompson { IPW_CMD_SET_CHANNEL, "SET_CHANNEL" },
143888d3b172SAndrew Thompson { IPW_CMD_SET_CONFIGURATION, "SET_CONFIGURATION" },
143988d3b172SAndrew Thompson { IPW_CMD_SET_DESIRED_BSSID, "SET_DESIRED_BSSID" },
144088d3b172SAndrew Thompson { IPW_CMD_SET_ESSID, "SET_ESSID" },
144188d3b172SAndrew Thompson { IPW_CMD_SET_FRAG_THRESHOLD, "SET_FRAG_THRESHOLD" },
144288d3b172SAndrew Thompson { IPW_CMD_SET_MAC_ADDRESS, "SET_MAC_ADDRESS" },
144388d3b172SAndrew Thompson { IPW_CMD_SET_MANDATORY_BSSID, "SET_MANDATORY_BSSID" },
144488d3b172SAndrew Thompson { IPW_CMD_SET_MODE, "SET_MODE" },
144588d3b172SAndrew Thompson { IPW_CMD_SET_MSDU_TX_RATES, "SET_MSDU_TX_RATES" },
144688d3b172SAndrew Thompson { IPW_CMD_SET_POWER_MODE, "SET_POWER_MODE" },
144788d3b172SAndrew Thompson { IPW_CMD_SET_RTS_THRESHOLD, "SET_RTS_THRESHOLD" },
144888d3b172SAndrew Thompson { IPW_CMD_SET_SCAN_OPTIONS, "SET_SCAN_OPTIONS" },
144988d3b172SAndrew Thompson { IPW_CMD_SET_SECURITY_INFO, "SET_SECURITY_INFO" },
145088d3b172SAndrew Thompson { IPW_CMD_SET_TX_POWER_INDEX, "SET_TX_POWER_INDEX" },
145188d3b172SAndrew Thompson { IPW_CMD_SET_TX_RATES, "SET_TX_RATES" },
145288d3b172SAndrew Thompson { IPW_CMD_SET_WEP_FLAGS, "SET_WEP_FLAGS" },
145388d3b172SAndrew Thompson { IPW_CMD_SET_WEP_KEY, "SET_WEP_KEY" },
145488d3b172SAndrew Thompson { IPW_CMD_SET_WEP_KEY_INDEX, "SET_WEP_KEY_INDEX" },
145588d3b172SAndrew Thompson { IPW_CMD_SET_WPA_IE, "SET_WPA_IE" },
145688d3b172SAndrew Thompson
145788d3b172SAndrew Thompson };
145888d3b172SAndrew Thompson static char buf[12];
145988d3b172SAndrew Thompson int i;
146088d3b172SAndrew Thompson
1461d6166defSAdrian Chadd for (i = 0; i < nitems(cmds); i++)
146288d3b172SAndrew Thompson if (cmds[i].cmd == cmd)
146388d3b172SAndrew Thompson return cmds[i].name;
146488d3b172SAndrew Thompson snprintf(buf, sizeof(buf), "%u", cmd);
146588d3b172SAndrew Thompson return buf;
146688d3b172SAndrew Thompson }
146788d3b172SAndrew Thompson
1468ceaec73dSDamien Bergamini /*
1469ceaec73dSDamien Bergamini * Send a command to the firmware and wait for the acknowledgement.
1470ceaec73dSDamien Bergamini */
1471ceaec73dSDamien Bergamini static int
ipw_cmd(struct ipw_softc * sc,uint32_t type,void * data,uint32_t len)1472ceaec73dSDamien Bergamini ipw_cmd(struct ipw_softc *sc, uint32_t type, void *data, uint32_t len)
1473ceaec73dSDamien Bergamini {
1474ceaec73dSDamien Bergamini struct ipw_soft_bd *sbd;
1475ceaec73dSDamien Bergamini bus_addr_t physaddr;
1476ceaec73dSDamien Bergamini int error;
1477ceaec73dSDamien Bergamini
1478b032f27cSSam Leffler IPW_LOCK_ASSERT(sc);
1479b032f27cSSam Leffler
148088d3b172SAndrew Thompson if (sc->flags & IPW_FLAG_BUSY) {
148188d3b172SAndrew Thompson device_printf(sc->sc_dev, "%s: %s not sent, busy\n",
148288d3b172SAndrew Thompson __func__, ipw_cmdname(type));
148388d3b172SAndrew Thompson return EAGAIN;
148488d3b172SAndrew Thompson }
148588d3b172SAndrew Thompson sc->flags |= IPW_FLAG_BUSY;
148688d3b172SAndrew Thompson
1487ceaec73dSDamien Bergamini sbd = &sc->stbd_list[sc->txcur];
1488ceaec73dSDamien Bergamini
1489ceaec73dSDamien Bergamini error = bus_dmamap_load(sc->cmd_dmat, sc->cmd_map, &sc->cmd,
1490ceaec73dSDamien Bergamini sizeof (struct ipw_cmd), ipw_dma_map_addr, &physaddr, 0);
1491ceaec73dSDamien Bergamini if (error != 0) {
1492ceaec73dSDamien Bergamini device_printf(sc->sc_dev, "could not map command DMA memory\n");
149388d3b172SAndrew Thompson sc->flags &= ~IPW_FLAG_BUSY;
1494ceaec73dSDamien Bergamini return error;
1495ceaec73dSDamien Bergamini }
1496ceaec73dSDamien Bergamini
1497ceaec73dSDamien Bergamini sc->cmd.type = htole32(type);
1498ceaec73dSDamien Bergamini sc->cmd.subtype = 0;
1499ceaec73dSDamien Bergamini sc->cmd.len = htole32(len);
1500ceaec73dSDamien Bergamini sc->cmd.seq = 0;
15013afda804SDamien Bergamini memcpy(sc->cmd.data, data, len);
1502ceaec73dSDamien Bergamini
1503ceaec73dSDamien Bergamini sbd->type = IPW_SBD_TYPE_COMMAND;
1504ceaec73dSDamien Bergamini sbd->bd->physaddr = htole32(physaddr);
1505ceaec73dSDamien Bergamini sbd->bd->len = htole32(sizeof (struct ipw_cmd));
1506ceaec73dSDamien Bergamini sbd->bd->nfrag = 1;
1507ceaec73dSDamien Bergamini sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_COMMAND |
1508ceaec73dSDamien Bergamini IPW_BD_FLAG_TX_LAST_FRAGMENT;
1509ceaec73dSDamien Bergamini
1510ceaec73dSDamien Bergamini bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map, BUS_DMASYNC_PREWRITE);
1511ceaec73dSDamien Bergamini bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1512ceaec73dSDamien Bergamini
151388d3b172SAndrew Thompson #ifdef IPW_DEBUG
151488d3b172SAndrew Thompson if (ipw_debug >= 4) {
151588d3b172SAndrew Thompson printf("sending %s(%u, %u, %u, %u)", ipw_cmdname(type), type,
151688d3b172SAndrew Thompson 0, 0, len);
151788d3b172SAndrew Thompson /* Print the data buffer in the higher debug level */
151888d3b172SAndrew Thompson if (ipw_debug >= 9 && len > 0) {
151988d3b172SAndrew Thompson printf(" data: 0x");
152088d3b172SAndrew Thompson for (int i = 1; i <= len; i++)
152199771434SWojciech A. Koszek printf("%1D", (u_char *)data + len - i, "");
152288d3b172SAndrew Thompson }
152388d3b172SAndrew Thompson printf("\n");
152488d3b172SAndrew Thompson }
152588d3b172SAndrew Thompson #endif
1526ceaec73dSDamien Bergamini
1527ceaec73dSDamien Bergamini /* kick firmware */
1528ceaec73dSDamien Bergamini sc->txfree--;
1529ceaec73dSDamien Bergamini sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1530ceaec73dSDamien Bergamini CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1531ceaec73dSDamien Bergamini
1532ceaec73dSDamien Bergamini /* wait at most one second for command to complete */
153388d3b172SAndrew Thompson error = msleep(sc, &sc->sc_mtx, 0, "ipwcmd", hz);
153488d3b172SAndrew Thompson if (error != 0) {
153588d3b172SAndrew Thompson device_printf(sc->sc_dev, "%s: %s failed, timeout (error %u)\n",
153688d3b172SAndrew Thompson __func__, ipw_cmdname(type), error);
153788d3b172SAndrew Thompson sc->flags &= ~IPW_FLAG_BUSY;
153888d3b172SAndrew Thompson return (error);
153988d3b172SAndrew Thompson }
154088d3b172SAndrew Thompson return (0);
1541ceaec73dSDamien Bergamini }
1542ceaec73dSDamien Bergamini
1543ceaec73dSDamien Bergamini static int
ipw_tx_start(struct ipw_softc * sc,struct mbuf * m0,struct ieee80211_node * ni)15447a79cebfSGleb Smirnoff ipw_tx_start(struct ipw_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1545ceaec73dSDamien Bergamini {
15467a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
15475463c4a4SSam Leffler struct ieee80211vap *vap = ni->ni_vap;
1548ceaec73dSDamien Bergamini struct ieee80211_frame *wh;
1549ceaec73dSDamien Bergamini struct ipw_soft_bd *sbd;
1550ceaec73dSDamien Bergamini struct ipw_soft_hdr *shdr;
1551ceaec73dSDamien Bergamini struct ipw_soft_buf *sbuf;
1552ceaec73dSDamien Bergamini struct ieee80211_key *k;
1553ceaec73dSDamien Bergamini struct mbuf *mnew;
1554ceaec73dSDamien Bergamini bus_dma_segment_t segs[IPW_MAX_NSEG];
1555ceaec73dSDamien Bergamini bus_addr_t physaddr;
1556ceaec73dSDamien Bergamini int nsegs, error, i;
1557ceaec73dSDamien Bergamini
1558ceaec73dSDamien Bergamini wh = mtod(m0, struct ieee80211_frame *);
1559ceaec73dSDamien Bergamini
15605945b5f5SKevin Lo if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1561b032f27cSSam Leffler k = ieee80211_crypto_encap(ni, m0);
156282145663SSam Leffler if (k == NULL) {
156382145663SSam Leffler m_freem(m0);
1564ceaec73dSDamien Bergamini return ENOBUFS;
156582145663SSam Leffler }
1566ceaec73dSDamien Bergamini /* packet header may have moved, reset our local pointer */
1567ceaec73dSDamien Bergamini wh = mtod(m0, struct ieee80211_frame *);
1568ceaec73dSDamien Bergamini }
1569ceaec73dSDamien Bergamini
15705463c4a4SSam Leffler if (ieee80211_radiotap_active_vap(vap)) {
1571ceaec73dSDamien Bergamini struct ipw_tx_radiotap_header *tap = &sc->sc_txtap;
1572ceaec73dSDamien Bergamini
1573ceaec73dSDamien Bergamini tap->wt_flags = 0;
1574ceaec73dSDamien Bergamini
15755463c4a4SSam Leffler ieee80211_radiotap_tx(vap, m0);
1576ceaec73dSDamien Bergamini }
1577ceaec73dSDamien Bergamini
1578ceaec73dSDamien Bergamini shdr = SLIST_FIRST(&sc->free_shdr);
1579ceaec73dSDamien Bergamini sbuf = SLIST_FIRST(&sc->free_sbuf);
1580ceaec73dSDamien Bergamini KASSERT(shdr != NULL && sbuf != NULL, ("empty sw hdr/buf pool"));
1581ceaec73dSDamien Bergamini
1582ceaec73dSDamien Bergamini shdr->hdr.type = htole32(IPW_HDR_TYPE_SEND);
1583ceaec73dSDamien Bergamini shdr->hdr.subtype = 0;
15845945b5f5SKevin Lo shdr->hdr.encrypted = (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) ? 1 : 0;
1585ceaec73dSDamien Bergamini shdr->hdr.encrypt = 0;
1586ceaec73dSDamien Bergamini shdr->hdr.keyidx = 0;
1587ceaec73dSDamien Bergamini shdr->hdr.keysz = 0;
1588ceaec73dSDamien Bergamini shdr->hdr.fragmentsz = 0;
1589ceaec73dSDamien Bergamini IEEE80211_ADDR_COPY(shdr->hdr.src_addr, wh->i_addr2);
1590ceaec73dSDamien Bergamini if (ic->ic_opmode == IEEE80211_M_STA)
1591ceaec73dSDamien Bergamini IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr3);
1592ceaec73dSDamien Bergamini else
1593ceaec73dSDamien Bergamini IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr1);
1594ceaec73dSDamien Bergamini
1595ceaec73dSDamien Bergamini /* trim IEEE802.11 header */
1596ceaec73dSDamien Bergamini m_adj(m0, sizeof (struct ieee80211_frame));
1597ceaec73dSDamien Bergamini
1598ceaec73dSDamien Bergamini error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0, segs,
1599ceaec73dSDamien Bergamini &nsegs, 0);
1600ceaec73dSDamien Bergamini if (error != 0 && error != EFBIG) {
1601ceaec73dSDamien Bergamini device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1602ceaec73dSDamien Bergamini error);
1603ceaec73dSDamien Bergamini m_freem(m0);
1604ceaec73dSDamien Bergamini return error;
1605ceaec73dSDamien Bergamini }
1606ceaec73dSDamien Bergamini if (error != 0) {
1607c6499eccSGleb Smirnoff mnew = m_defrag(m0, M_NOWAIT);
1608ceaec73dSDamien Bergamini if (mnew == NULL) {
1609ceaec73dSDamien Bergamini device_printf(sc->sc_dev,
1610ceaec73dSDamien Bergamini "could not defragment mbuf\n");
1611ceaec73dSDamien Bergamini m_freem(m0);
1612ceaec73dSDamien Bergamini return ENOBUFS;
1613ceaec73dSDamien Bergamini }
1614ceaec73dSDamien Bergamini m0 = mnew;
1615ceaec73dSDamien Bergamini
1616ceaec73dSDamien Bergamini error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0,
1617ceaec73dSDamien Bergamini segs, &nsegs, 0);
1618ceaec73dSDamien Bergamini if (error != 0) {
1619ceaec73dSDamien Bergamini device_printf(sc->sc_dev,
1620ceaec73dSDamien Bergamini "could not map mbuf (error %d)\n", error);
1621ceaec73dSDamien Bergamini m_freem(m0);
1622ceaec73dSDamien Bergamini return error;
1623ceaec73dSDamien Bergamini }
1624ceaec73dSDamien Bergamini }
1625ceaec73dSDamien Bergamini
1626ceaec73dSDamien Bergamini error = bus_dmamap_load(sc->hdr_dmat, shdr->map, &shdr->hdr,
1627ceaec73dSDamien Bergamini sizeof (struct ipw_hdr), ipw_dma_map_addr, &physaddr, 0);
1628ceaec73dSDamien Bergamini if (error != 0) {
1629ceaec73dSDamien Bergamini device_printf(sc->sc_dev, "could not map header DMA memory\n");
1630ceaec73dSDamien Bergamini bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1631ceaec73dSDamien Bergamini m_freem(m0);
1632ceaec73dSDamien Bergamini return error;
1633ceaec73dSDamien Bergamini }
1634ceaec73dSDamien Bergamini
1635ceaec73dSDamien Bergamini SLIST_REMOVE_HEAD(&sc->free_sbuf, next);
1636ceaec73dSDamien Bergamini SLIST_REMOVE_HEAD(&sc->free_shdr, next);
1637ceaec73dSDamien Bergamini
1638ceaec73dSDamien Bergamini sbd = &sc->stbd_list[sc->txcur];
1639ceaec73dSDamien Bergamini sbd->type = IPW_SBD_TYPE_HEADER;
1640ceaec73dSDamien Bergamini sbd->priv = shdr;
1641ceaec73dSDamien Bergamini sbd->bd->physaddr = htole32(physaddr);
1642ceaec73dSDamien Bergamini sbd->bd->len = htole32(sizeof (struct ipw_hdr));
1643ceaec73dSDamien Bergamini sbd->bd->nfrag = 1 + nsegs;
1644ceaec73dSDamien Bergamini sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3 |
1645ceaec73dSDamien Bergamini IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1646ceaec73dSDamien Bergamini
1647ceaec73dSDamien Bergamini DPRINTFN(5, ("sending tx hdr (%u, %u, %u, %u, %6D, %6D)\n",
1648ceaec73dSDamien Bergamini shdr->hdr.type, shdr->hdr.subtype, shdr->hdr.encrypted,
1649ceaec73dSDamien Bergamini shdr->hdr.encrypt, shdr->hdr.src_addr, ":", shdr->hdr.dst_addr,
1650ceaec73dSDamien Bergamini ":"));
1651ceaec73dSDamien Bergamini
1652ceaec73dSDamien Bergamini sc->txfree--;
1653ceaec73dSDamien Bergamini sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1654ceaec73dSDamien Bergamini
1655ceaec73dSDamien Bergamini sbuf->m = m0;
1656ceaec73dSDamien Bergamini sbuf->ni = ni;
1657ceaec73dSDamien Bergamini
1658ceaec73dSDamien Bergamini for (i = 0; i < nsegs; i++) {
1659ceaec73dSDamien Bergamini sbd = &sc->stbd_list[sc->txcur];
1660ceaec73dSDamien Bergamini
1661ceaec73dSDamien Bergamini sbd->bd->physaddr = htole32(segs[i].ds_addr);
1662ceaec73dSDamien Bergamini sbd->bd->len = htole32(segs[i].ds_len);
1663ceaec73dSDamien Bergamini sbd->bd->nfrag = 0;
1664ceaec73dSDamien Bergamini sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3;
1665ceaec73dSDamien Bergamini if (i == nsegs - 1) {
1666ceaec73dSDamien Bergamini sbd->type = IPW_SBD_TYPE_DATA;
1667ceaec73dSDamien Bergamini sbd->priv = sbuf;
1668ceaec73dSDamien Bergamini sbd->bd->flags |= IPW_BD_FLAG_TX_LAST_FRAGMENT;
1669ceaec73dSDamien Bergamini } else {
1670ceaec73dSDamien Bergamini sbd->type = IPW_SBD_TYPE_NOASSOC;
1671ceaec73dSDamien Bergamini sbd->bd->flags |= IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1672ceaec73dSDamien Bergamini }
1673ceaec73dSDamien Bergamini
1674d4e60294SAndrew Thompson DPRINTFN(5, ("sending fragment (%d)\n", i));
1675ceaec73dSDamien Bergamini
1676ceaec73dSDamien Bergamini sc->txfree--;
1677ceaec73dSDamien Bergamini sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1678ceaec73dSDamien Bergamini }
1679ceaec73dSDamien Bergamini
1680ceaec73dSDamien Bergamini bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_PREWRITE);
1681ceaec73dSDamien Bergamini bus_dmamap_sync(sc->txbuf_dmat, sbuf->map, BUS_DMASYNC_PREWRITE);
1682ceaec73dSDamien Bergamini bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1683ceaec73dSDamien Bergamini
1684ceaec73dSDamien Bergamini /* kick firmware */
1685ceaec73dSDamien Bergamini CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1686ceaec73dSDamien Bergamini
1687ceaec73dSDamien Bergamini return 0;
1688ceaec73dSDamien Bergamini }
1689ceaec73dSDamien Bergamini
1690b032f27cSSam Leffler static int
ipw_raw_xmit(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_bpf_params * params)1691b032f27cSSam Leffler ipw_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1692b032f27cSSam Leffler const struct ieee80211_bpf_params *params)
1693b032f27cSSam Leffler {
1694b032f27cSSam Leffler /* no support; just discard */
1695b032f27cSSam Leffler m_freem(m);
1696b032f27cSSam Leffler ieee80211_free_node(ni);
1697b032f27cSSam Leffler return 0;
1698b032f27cSSam Leffler }
1699b032f27cSSam Leffler
17007a79cebfSGleb Smirnoff static int
ipw_transmit(struct ieee80211com * ic,struct mbuf * m)17017a79cebfSGleb Smirnoff ipw_transmit(struct ieee80211com *ic, struct mbuf *m)
1702ceaec73dSDamien Bergamini {
17037a79cebfSGleb Smirnoff struct ipw_softc *sc = ic->ic_softc;
17047a79cebfSGleb Smirnoff int error;
170588d3b172SAndrew Thompson
170688d3b172SAndrew Thompson IPW_LOCK(sc);
17077a79cebfSGleb Smirnoff if ((sc->flags & IPW_FLAG_RUNNING) == 0) {
170888d3b172SAndrew Thompson IPW_UNLOCK(sc);
17097a79cebfSGleb Smirnoff return (ENXIO);
17107a79cebfSGleb Smirnoff }
17117a79cebfSGleb Smirnoff error = mbufq_enqueue(&sc->sc_snd, m);
17127a79cebfSGleb Smirnoff if (error) {
17137a79cebfSGleb Smirnoff IPW_UNLOCK(sc);
17147a79cebfSGleb Smirnoff return (error);
17157a79cebfSGleb Smirnoff }
17167a79cebfSGleb Smirnoff ipw_start(sc);
17177a79cebfSGleb Smirnoff IPW_UNLOCK(sc);
17187a79cebfSGleb Smirnoff return (0);
171988d3b172SAndrew Thompson }
172088d3b172SAndrew Thompson
172188d3b172SAndrew Thompson static void
ipw_start(struct ipw_softc * sc)17227a79cebfSGleb Smirnoff ipw_start(struct ipw_softc *sc)
172388d3b172SAndrew Thompson {
1724ceaec73dSDamien Bergamini struct ieee80211_node *ni;
1725b032f27cSSam Leffler struct mbuf *m;
1726ceaec73dSDamien Bergamini
172788d3b172SAndrew Thompson IPW_LOCK_ASSERT(sc);
1728ceaec73dSDamien Bergamini
1729daa70021SGleb Smirnoff while (sc->txfree >= 1 + IPW_MAX_NSEG &&
17307a79cebfSGleb Smirnoff (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
1731b032f27cSSam Leffler ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
17327a79cebfSGleb Smirnoff if (ipw_tx_start(sc, m, ni) != 0) {
17337a79cebfSGleb Smirnoff if_inc_counter(ni->ni_vap->iv_ifp,
17347a79cebfSGleb Smirnoff IFCOUNTER_OERRORS, 1);
1735ceaec73dSDamien Bergamini ieee80211_free_node(ni);
1736ceaec73dSDamien Bergamini break;
1737ceaec73dSDamien Bergamini }
1738ceaec73dSDamien Bergamini /* start watchdog timer */
1739ceaec73dSDamien Bergamini sc->sc_tx_timer = 5;
1740ceaec73dSDamien Bergamini }
1741ceaec73dSDamien Bergamini }
1742ceaec73dSDamien Bergamini
1743ceaec73dSDamien Bergamini static void
ipw_watchdog(void * arg)174488d3b172SAndrew Thompson ipw_watchdog(void *arg)
1745ceaec73dSDamien Bergamini {
174688d3b172SAndrew Thompson struct ipw_softc *sc = arg;
17477a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
1748ceaec73dSDamien Bergamini
174988d3b172SAndrew Thompson IPW_LOCK_ASSERT(sc);
1750ceaec73dSDamien Bergamini
1751ceaec73dSDamien Bergamini if (sc->sc_tx_timer > 0) {
1752ceaec73dSDamien Bergamini if (--sc->sc_tx_timer == 0) {
17537a79cebfSGleb Smirnoff device_printf(sc->sc_dev, "device timeout\n");
17547a79cebfSGleb Smirnoff counter_u64_add(ic->ic_oerrors, 1);
1755b032f27cSSam Leffler taskqueue_enqueue(taskqueue_swi, &sc->sc_init_task);
1756ceaec73dSDamien Bergamini }
1757ceaec73dSDamien Bergamini }
175888d3b172SAndrew Thompson if (sc->sc_scan_timer > 0) {
175988d3b172SAndrew Thompson if (--sc->sc_scan_timer == 0) {
176088d3b172SAndrew Thompson DPRINTFN(3, ("Scan timeout\n"));
176188d3b172SAndrew Thompson /* End the scan */
176288d3b172SAndrew Thompson if (sc->flags & IPW_FLAG_SCANNING) {
176382ff07c0SBernhard Schmidt IPW_UNLOCK(sc);
1764b032f27cSSam Leffler ieee80211_scan_done(TAILQ_FIRST(&ic->ic_vaps));
176582ff07c0SBernhard Schmidt IPW_LOCK(sc);
176688d3b172SAndrew Thompson sc->flags &= ~IPW_FLAG_SCANNING;
176788d3b172SAndrew Thompson }
176888d3b172SAndrew Thompson }
176988d3b172SAndrew Thompson }
17707a79cebfSGleb Smirnoff if (sc->flags & IPW_FLAG_RUNNING)
177188d3b172SAndrew Thompson callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
1772ceaec73dSDamien Bergamini }
1773ceaec73dSDamien Bergamini
17747a79cebfSGleb Smirnoff static void
ipw_parent(struct ieee80211com * ic)17757a79cebfSGleb Smirnoff ipw_parent(struct ieee80211com *ic)
1776ceaec73dSDamien Bergamini {
17777a79cebfSGleb Smirnoff struct ipw_softc *sc = ic->ic_softc;
17787a79cebfSGleb Smirnoff int startall = 0;
1779ceaec73dSDamien Bergamini
178031a8c1edSAndrew Thompson IPW_LOCK(sc);
17817a79cebfSGleb Smirnoff if (ic->ic_nrunning > 0) {
17827a79cebfSGleb Smirnoff if (!(sc->flags & IPW_FLAG_RUNNING)) {
1783b032f27cSSam Leffler ipw_init_locked(sc);
1784b032f27cSSam Leffler startall = 1;
1785b032f27cSSam Leffler }
17867a79cebfSGleb Smirnoff } else if (sc->flags & IPW_FLAG_RUNNING)
178788d3b172SAndrew Thompson ipw_stop_locked(sc);
178888d3b172SAndrew Thompson IPW_UNLOCK(sc);
1789b032f27cSSam Leffler if (startall)
1790b032f27cSSam Leffler ieee80211_start_all(ic);
1791ceaec73dSDamien Bergamini }
1792ceaec73dSDamien Bergamini
1793ceaec73dSDamien Bergamini static void
ipw_stop_master(struct ipw_softc * sc)1794ceaec73dSDamien Bergamini ipw_stop_master(struct ipw_softc *sc)
1795ceaec73dSDamien Bergamini {
179684ca7670SDamien Bergamini uint32_t tmp;
1797ceaec73dSDamien Bergamini int ntries;
1798ceaec73dSDamien Bergamini
1799ceaec73dSDamien Bergamini /* disable interrupts */
1800ceaec73dSDamien Bergamini CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1801ceaec73dSDamien Bergamini
1802ceaec73dSDamien Bergamini CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_STOP_MASTER);
1803ceaec73dSDamien Bergamini for (ntries = 0; ntries < 50; ntries++) {
1804ceaec73dSDamien Bergamini if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_MASTER_DISABLED)
1805ceaec73dSDamien Bergamini break;
1806ceaec73dSDamien Bergamini DELAY(10);
1807ceaec73dSDamien Bergamini }
1808ceaec73dSDamien Bergamini if (ntries == 50)
1809ceaec73dSDamien Bergamini device_printf(sc->sc_dev, "timeout waiting for master\n");
1810ceaec73dSDamien Bergamini
181184ca7670SDamien Bergamini tmp = CSR_READ_4(sc, IPW_CSR_RST);
181284ca7670SDamien Bergamini CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_PRINCETON_RESET);
1813ceaec73dSDamien Bergamini
181488d3b172SAndrew Thompson /* Clear all flags except the following */
181588d3b172SAndrew Thompson sc->flags &= IPW_FLAG_HAS_RADIO_SWITCH;
1816ceaec73dSDamien Bergamini }
1817ceaec73dSDamien Bergamini
1818ceaec73dSDamien Bergamini static int
ipw_reset(struct ipw_softc * sc)1819ceaec73dSDamien Bergamini ipw_reset(struct ipw_softc *sc)
1820ceaec73dSDamien Bergamini {
182184ca7670SDamien Bergamini uint32_t tmp;
1822ceaec73dSDamien Bergamini int ntries;
1823ceaec73dSDamien Bergamini
1824ceaec73dSDamien Bergamini ipw_stop_master(sc);
1825ceaec73dSDamien Bergamini
1826ceaec73dSDamien Bergamini /* move adapter to D0 state */
182784ca7670SDamien Bergamini tmp = CSR_READ_4(sc, IPW_CSR_CTL);
182884ca7670SDamien Bergamini CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1829ceaec73dSDamien Bergamini
1830ceaec73dSDamien Bergamini /* wait for clock stabilization */
1831ceaec73dSDamien Bergamini for (ntries = 0; ntries < 1000; ntries++) {
1832ceaec73dSDamien Bergamini if (CSR_READ_4(sc, IPW_CSR_CTL) & IPW_CTL_CLOCK_READY)
1833ceaec73dSDamien Bergamini break;
1834ceaec73dSDamien Bergamini DELAY(200);
1835ceaec73dSDamien Bergamini }
1836ceaec73dSDamien Bergamini if (ntries == 1000)
1837ceaec73dSDamien Bergamini return EIO;
1838ceaec73dSDamien Bergamini
183984ca7670SDamien Bergamini tmp = CSR_READ_4(sc, IPW_CSR_RST);
184084ca7670SDamien Bergamini CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_SW_RESET);
1841ceaec73dSDamien Bergamini
1842ceaec73dSDamien Bergamini DELAY(10);
1843ceaec73dSDamien Bergamini
184484ca7670SDamien Bergamini tmp = CSR_READ_4(sc, IPW_CSR_CTL);
184584ca7670SDamien Bergamini CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1846ceaec73dSDamien Bergamini
1847ceaec73dSDamien Bergamini return 0;
1848ceaec73dSDamien Bergamini }
1849ceaec73dSDamien Bergamini
185088d3b172SAndrew Thompson static int
ipw_waitfordisable(struct ipw_softc * sc,int waitfor)185188d3b172SAndrew Thompson ipw_waitfordisable(struct ipw_softc *sc, int waitfor)
185288d3b172SAndrew Thompson {
185388d3b172SAndrew Thompson int ms = hz < 1000 ? 1 : hz/10;
185488d3b172SAndrew Thompson int i, error;
185588d3b172SAndrew Thompson
185688d3b172SAndrew Thompson for (i = 0; i < 100; i++) {
185788d3b172SAndrew Thompson if (ipw_read_table1(sc, IPW_INFO_CARD_DISABLED) == waitfor)
185888d3b172SAndrew Thompson return 0;
185988d3b172SAndrew Thompson error = msleep(sc, &sc->sc_mtx, PCATCH, __func__, ms);
186088d3b172SAndrew Thompson if (error == 0 || error != EWOULDBLOCK)
186188d3b172SAndrew Thompson return 0;
186288d3b172SAndrew Thompson }
186388d3b172SAndrew Thompson DPRINTF(("%s: timeout waiting for %s\n",
186488d3b172SAndrew Thompson __func__, waitfor ? "disable" : "enable"));
186588d3b172SAndrew Thompson return ETIMEDOUT;
186688d3b172SAndrew Thompson }
186788d3b172SAndrew Thompson
186888d3b172SAndrew Thompson static int
ipw_enable(struct ipw_softc * sc)186988d3b172SAndrew Thompson ipw_enable(struct ipw_softc *sc)
187088d3b172SAndrew Thompson {
187188d3b172SAndrew Thompson int error;
187288d3b172SAndrew Thompson
187388d3b172SAndrew Thompson if ((sc->flags & IPW_FLAG_ENABLED) == 0) {
187488d3b172SAndrew Thompson DPRINTF(("Enable adapter\n"));
187588d3b172SAndrew Thompson error = ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
187688d3b172SAndrew Thompson if (error != 0)
187788d3b172SAndrew Thompson return error;
187888d3b172SAndrew Thompson error = ipw_waitfordisable(sc, 0);
187988d3b172SAndrew Thompson if (error != 0)
188088d3b172SAndrew Thompson return error;
188188d3b172SAndrew Thompson sc->flags |= IPW_FLAG_ENABLED;
188288d3b172SAndrew Thompson }
188388d3b172SAndrew Thompson return 0;
188488d3b172SAndrew Thompson }
188588d3b172SAndrew Thompson
188688d3b172SAndrew Thompson static int
ipw_disable(struct ipw_softc * sc)188788d3b172SAndrew Thompson ipw_disable(struct ipw_softc *sc)
188888d3b172SAndrew Thompson {
188988d3b172SAndrew Thompson int error;
189088d3b172SAndrew Thompson
189188d3b172SAndrew Thompson if (sc->flags & IPW_FLAG_ENABLED) {
189288d3b172SAndrew Thompson DPRINTF(("Disable adapter\n"));
189388d3b172SAndrew Thompson error = ipw_cmd(sc, IPW_CMD_DISABLE, NULL, 0);
189488d3b172SAndrew Thompson if (error != 0)
189588d3b172SAndrew Thompson return error;
189688d3b172SAndrew Thompson error = ipw_waitfordisable(sc, 1);
189788d3b172SAndrew Thompson if (error != 0)
189888d3b172SAndrew Thompson return error;
189988d3b172SAndrew Thompson sc->flags &= ~IPW_FLAG_ENABLED;
190088d3b172SAndrew Thompson }
190188d3b172SAndrew Thompson return 0;
190288d3b172SAndrew Thompson }
190388d3b172SAndrew Thompson
1904ceaec73dSDamien Bergamini /*
1905ceaec73dSDamien Bergamini * Upload the microcode to the device.
1906ceaec73dSDamien Bergamini */
1907ceaec73dSDamien Bergamini static int
ipw_load_ucode(struct ipw_softc * sc,const char * uc,int size)190884ca7670SDamien Bergamini ipw_load_ucode(struct ipw_softc *sc, const char *uc, int size)
1909ceaec73dSDamien Bergamini {
1910ceaec73dSDamien Bergamini int ntries;
1911ceaec73dSDamien Bergamini
1912ceaec73dSDamien Bergamini MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
1913ceaec73dSDamien Bergamini CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1914ceaec73dSDamien Bergamini
1915ceaec73dSDamien Bergamini MEM_WRITE_2(sc, 0x220000, 0x0703);
1916ceaec73dSDamien Bergamini MEM_WRITE_2(sc, 0x220000, 0x0707);
1917ceaec73dSDamien Bergamini
1918ceaec73dSDamien Bergamini MEM_WRITE_1(sc, 0x210014, 0x72);
1919ceaec73dSDamien Bergamini MEM_WRITE_1(sc, 0x210014, 0x72);
1920ceaec73dSDamien Bergamini
1921ceaec73dSDamien Bergamini MEM_WRITE_1(sc, 0x210000, 0x40);
1922ceaec73dSDamien Bergamini MEM_WRITE_1(sc, 0x210000, 0x00);
1923ceaec73dSDamien Bergamini MEM_WRITE_1(sc, 0x210000, 0x40);
1924ceaec73dSDamien Bergamini
1925ceaec73dSDamien Bergamini MEM_WRITE_MULTI_1(sc, 0x210010, uc, size);
1926ceaec73dSDamien Bergamini
1927ceaec73dSDamien Bergamini MEM_WRITE_1(sc, 0x210000, 0x00);
1928ceaec73dSDamien Bergamini MEM_WRITE_1(sc, 0x210000, 0x00);
1929ceaec73dSDamien Bergamini MEM_WRITE_1(sc, 0x210000, 0x80);
1930ceaec73dSDamien Bergamini
1931ceaec73dSDamien Bergamini MEM_WRITE_2(sc, 0x220000, 0x0703);
1932ceaec73dSDamien Bergamini MEM_WRITE_2(sc, 0x220000, 0x0707);
1933ceaec73dSDamien Bergamini
1934ceaec73dSDamien Bergamini MEM_WRITE_1(sc, 0x210014, 0x72);
1935ceaec73dSDamien Bergamini MEM_WRITE_1(sc, 0x210014, 0x72);
1936ceaec73dSDamien Bergamini
1937ceaec73dSDamien Bergamini MEM_WRITE_1(sc, 0x210000, 0x00);
1938ceaec73dSDamien Bergamini MEM_WRITE_1(sc, 0x210000, 0x80);
1939ceaec73dSDamien Bergamini
1940ceaec73dSDamien Bergamini for (ntries = 0; ntries < 10; ntries++) {
1941ceaec73dSDamien Bergamini if (MEM_READ_1(sc, 0x210000) & 1)
1942ceaec73dSDamien Bergamini break;
1943ceaec73dSDamien Bergamini DELAY(10);
1944ceaec73dSDamien Bergamini }
1945ceaec73dSDamien Bergamini if (ntries == 10) {
1946ceaec73dSDamien Bergamini device_printf(sc->sc_dev,
1947ceaec73dSDamien Bergamini "timeout waiting for ucode to initialize\n");
1948ceaec73dSDamien Bergamini return EIO;
1949ceaec73dSDamien Bergamini }
1950ceaec73dSDamien Bergamini
1951ceaec73dSDamien Bergamini MEM_WRITE_4(sc, 0x3000e0, 0);
1952ceaec73dSDamien Bergamini
1953ceaec73dSDamien Bergamini return 0;
1954ceaec73dSDamien Bergamini }
1955ceaec73dSDamien Bergamini
1956ceaec73dSDamien Bergamini /* set of macros to handle unaligned little endian data in firmware image */
1957ceaec73dSDamien Bergamini #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
1958ceaec73dSDamien Bergamini #define GETLE16(p) ((p)[0] | (p)[1] << 8)
1959ceaec73dSDamien Bergamini static int
ipw_load_firmware(struct ipw_softc * sc,const char * fw,int size)196084ca7670SDamien Bergamini ipw_load_firmware(struct ipw_softc *sc, const char *fw, int size)
1961ceaec73dSDamien Bergamini {
196284ca7670SDamien Bergamini const uint8_t *p, *end;
196384ca7670SDamien Bergamini uint32_t tmp, dst;
1964ceaec73dSDamien Bergamini uint16_t len;
1965ceaec73dSDamien Bergamini int error;
1966ceaec73dSDamien Bergamini
1967ceaec73dSDamien Bergamini p = fw;
1968ceaec73dSDamien Bergamini end = fw + size;
1969ceaec73dSDamien Bergamini while (p < end) {
1970ceaec73dSDamien Bergamini dst = GETLE32(p); p += 4;
1971ceaec73dSDamien Bergamini len = GETLE16(p); p += 2;
1972ceaec73dSDamien Bergamini
1973ceaec73dSDamien Bergamini ipw_write_mem_1(sc, dst, p, len);
1974ceaec73dSDamien Bergamini p += len;
1975ceaec73dSDamien Bergamini }
1976ceaec73dSDamien Bergamini
1977ceaec73dSDamien Bergamini CSR_WRITE_4(sc, IPW_CSR_IO, IPW_IO_GPIO1_ENABLE | IPW_IO_GPIO3_MASK |
1978ceaec73dSDamien Bergamini IPW_IO_LED_OFF);
1979ceaec73dSDamien Bergamini
1980ceaec73dSDamien Bergamini /* enable interrupts */
1981ceaec73dSDamien Bergamini CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1982ceaec73dSDamien Bergamini
1983ceaec73dSDamien Bergamini /* kick the firmware */
1984ceaec73dSDamien Bergamini CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1985ceaec73dSDamien Bergamini
198684ca7670SDamien Bergamini tmp = CSR_READ_4(sc, IPW_CSR_CTL);
198784ca7670SDamien Bergamini CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_ALLOW_STANDBY);
1988ceaec73dSDamien Bergamini
1989ceaec73dSDamien Bergamini /* wait at most one second for firmware initialization to complete */
1990ceaec73dSDamien Bergamini if ((error = msleep(sc, &sc->sc_mtx, 0, "ipwinit", hz)) != 0) {
1991ceaec73dSDamien Bergamini device_printf(sc->sc_dev, "timeout waiting for firmware "
1992ceaec73dSDamien Bergamini "initialization to complete\n");
1993ceaec73dSDamien Bergamini return error;
1994ceaec73dSDamien Bergamini }
1995ceaec73dSDamien Bergamini
199684ca7670SDamien Bergamini tmp = CSR_READ_4(sc, IPW_CSR_IO);
199784ca7670SDamien Bergamini CSR_WRITE_4(sc, IPW_CSR_IO, tmp | IPW_IO_GPIO1_MASK |
199884ca7670SDamien Bergamini IPW_IO_GPIO3_MASK);
1999ceaec73dSDamien Bergamini
2000ceaec73dSDamien Bergamini return 0;
2001ceaec73dSDamien Bergamini }
2002ceaec73dSDamien Bergamini
2003ceaec73dSDamien Bergamini static int
ipw_setwepkeys(struct ipw_softc * sc)200488d3b172SAndrew Thompson ipw_setwepkeys(struct ipw_softc *sc)
200588d3b172SAndrew Thompson {
20067a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
2007b032f27cSSam Leffler struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
200888d3b172SAndrew Thompson struct ipw_wep_key wepkey;
200988d3b172SAndrew Thompson struct ieee80211_key *wk;
201088d3b172SAndrew Thompson int error, i;
201188d3b172SAndrew Thompson
201288d3b172SAndrew Thompson for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2013b032f27cSSam Leffler wk = &vap->iv_nw_keys[i];
201488d3b172SAndrew Thompson
201588d3b172SAndrew Thompson if (wk->wk_cipher == NULL ||
201688d3b172SAndrew Thompson wk->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP)
201788d3b172SAndrew Thompson continue;
201888d3b172SAndrew Thompson
201988d3b172SAndrew Thompson wepkey.idx = i;
202088d3b172SAndrew Thompson wepkey.len = wk->wk_keylen;
202188d3b172SAndrew Thompson memset(wepkey.key, 0, sizeof wepkey.key);
202288d3b172SAndrew Thompson memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
202388d3b172SAndrew Thompson DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx,
202488d3b172SAndrew Thompson wepkey.len));
202588d3b172SAndrew Thompson error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY, &wepkey,
202688d3b172SAndrew Thompson sizeof wepkey);
202788d3b172SAndrew Thompson if (error != 0)
202888d3b172SAndrew Thompson return error;
202988d3b172SAndrew Thompson }
203088d3b172SAndrew Thompson return 0;
203188d3b172SAndrew Thompson }
203288d3b172SAndrew Thompson
203388d3b172SAndrew Thompson static int
ipw_setwpaie(struct ipw_softc * sc,const void * ie,int ielen)203488d3b172SAndrew Thompson ipw_setwpaie(struct ipw_softc *sc, const void *ie, int ielen)
203588d3b172SAndrew Thompson {
203688d3b172SAndrew Thompson struct ipw_wpa_ie wpaie;
203788d3b172SAndrew Thompson
203888d3b172SAndrew Thompson memset(&wpaie, 0, sizeof(wpaie));
203988d3b172SAndrew Thompson wpaie.len = htole32(ielen);
204088d3b172SAndrew Thompson /* XXX verify length */
204188d3b172SAndrew Thompson memcpy(&wpaie.ie, ie, ielen);
204288d3b172SAndrew Thompson DPRINTF(("Setting WPA IE\n"));
204388d3b172SAndrew Thompson return ipw_cmd(sc, IPW_CMD_SET_WPA_IE, &wpaie, sizeof(wpaie));
204488d3b172SAndrew Thompson }
204588d3b172SAndrew Thompson
204688d3b172SAndrew Thompson static int
ipw_setbssid(struct ipw_softc * sc,uint8_t * bssid)204788d3b172SAndrew Thompson ipw_setbssid(struct ipw_softc *sc, uint8_t *bssid)
204888d3b172SAndrew Thompson {
204988d3b172SAndrew Thompson static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
205088d3b172SAndrew Thompson
205188d3b172SAndrew Thompson if (bssid == NULL || bcmp(bssid, zerobssid, IEEE80211_ADDR_LEN) == 0) {
205288d3b172SAndrew Thompson DPRINTF(("Setting mandatory BSSID to null\n"));
205388d3b172SAndrew Thompson return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, NULL, 0);
205488d3b172SAndrew Thompson } else {
205588d3b172SAndrew Thompson DPRINTF(("Setting mandatory BSSID to %6D\n", bssid, ":"));
205688d3b172SAndrew Thompson return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID,
205788d3b172SAndrew Thompson bssid, IEEE80211_ADDR_LEN);
205888d3b172SAndrew Thompson }
205988d3b172SAndrew Thompson }
206088d3b172SAndrew Thompson
206188d3b172SAndrew Thompson static int
ipw_setssid(struct ipw_softc * sc,void * ssid,size_t ssidlen)206288d3b172SAndrew Thompson ipw_setssid(struct ipw_softc *sc, void *ssid, size_t ssidlen)
206388d3b172SAndrew Thompson {
206488d3b172SAndrew Thompson if (ssidlen == 0) {
206588d3b172SAndrew Thompson /*
206688d3b172SAndrew Thompson * A bug in the firmware breaks the ``don't associate''
206788d3b172SAndrew Thompson * bit in the scan options command. To compensate for
206888d3b172SAndrew Thompson * this install a bogus ssid when no ssid is specified
206988d3b172SAndrew Thompson * so the firmware won't try to associate.
207088d3b172SAndrew Thompson */
207188d3b172SAndrew Thompson DPRINTF(("Setting bogus ESSID to WAR firmware bug\n"));
207288d3b172SAndrew Thompson return ipw_cmd(sc, IPW_CMD_SET_ESSID,
207388d3b172SAndrew Thompson "\x18\x19\x20\x21\x22\x23\x24\x25\x26\x27"
207488d3b172SAndrew Thompson "\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31"
207588d3b172SAndrew Thompson "\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b"
207688d3b172SAndrew Thompson "\x3c\x3d", IEEE80211_NWID_LEN);
207788d3b172SAndrew Thompson } else {
207888d3b172SAndrew Thompson #ifdef IPW_DEBUG
207988d3b172SAndrew Thompson if (ipw_debug > 0) {
208088d3b172SAndrew Thompson printf("Setting ESSID to ");
208188d3b172SAndrew Thompson ieee80211_print_essid(ssid, ssidlen);
208288d3b172SAndrew Thompson printf("\n");
208388d3b172SAndrew Thompson }
208488d3b172SAndrew Thompson #endif
208588d3b172SAndrew Thompson return ipw_cmd(sc, IPW_CMD_SET_ESSID, ssid, ssidlen);
208688d3b172SAndrew Thompson }
208788d3b172SAndrew Thompson }
208888d3b172SAndrew Thompson
208988d3b172SAndrew Thompson static int
ipw_setscanopts(struct ipw_softc * sc,uint32_t chanmask,uint32_t flags)209088d3b172SAndrew Thompson ipw_setscanopts(struct ipw_softc *sc, uint32_t chanmask, uint32_t flags)
209188d3b172SAndrew Thompson {
209288d3b172SAndrew Thompson struct ipw_scan_options opts;
209388d3b172SAndrew Thompson
209488d3b172SAndrew Thompson DPRINTF(("Scan options: mask 0x%x flags 0x%x\n", chanmask, flags));
209588d3b172SAndrew Thompson opts.channels = htole32(chanmask);
209688d3b172SAndrew Thompson opts.flags = htole32(flags);
209788d3b172SAndrew Thompson return ipw_cmd(sc, IPW_CMD_SET_SCAN_OPTIONS, &opts, sizeof(opts));
209888d3b172SAndrew Thompson }
209988d3b172SAndrew Thompson
210088d3b172SAndrew Thompson static int
ipw_scan(struct ipw_softc * sc)210188d3b172SAndrew Thompson ipw_scan(struct ipw_softc *sc)
210288d3b172SAndrew Thompson {
210388d3b172SAndrew Thompson uint32_t params;
210488d3b172SAndrew Thompson int error;
210588d3b172SAndrew Thompson
210688d3b172SAndrew Thompson DPRINTF(("%s: flags 0x%x\n", __func__, sc->flags));
210788d3b172SAndrew Thompson
210888d3b172SAndrew Thompson if (sc->flags & IPW_FLAG_SCANNING)
210988d3b172SAndrew Thompson return (EBUSY);
211088d3b172SAndrew Thompson sc->flags |= IPW_FLAG_SCANNING | IPW_FLAG_HACK;
211188d3b172SAndrew Thompson
211288d3b172SAndrew Thompson /* NB: IPW_SCAN_DO_NOT_ASSOCIATE does not work (we set it anyway) */
211388d3b172SAndrew Thompson error = ipw_setscanopts(sc, 0x3fff, IPW_SCAN_DO_NOT_ASSOCIATE);
211488d3b172SAndrew Thompson if (error != 0)
211588d3b172SAndrew Thompson goto done;
211688d3b172SAndrew Thompson
211788d3b172SAndrew Thompson /*
211888d3b172SAndrew Thompson * Setup null/bogus ssid so firmware doesn't use any previous
211988d3b172SAndrew Thompson * ssid to try and associate. This is because the ``don't
212088d3b172SAndrew Thompson * associate'' option bit is broken (sigh).
212188d3b172SAndrew Thompson */
212288d3b172SAndrew Thompson error = ipw_setssid(sc, NULL, 0);
212388d3b172SAndrew Thompson if (error != 0)
212488d3b172SAndrew Thompson goto done;
212588d3b172SAndrew Thompson
212688d3b172SAndrew Thompson /*
212788d3b172SAndrew Thompson * NB: the adapter may be disabled on association lost;
212888d3b172SAndrew Thompson * if so just re-enable it to kick off scanning.
212988d3b172SAndrew Thompson */
213088d3b172SAndrew Thompson DPRINTF(("Starting scan\n"));
213188d3b172SAndrew Thompson sc->sc_scan_timer = 3;
213288d3b172SAndrew Thompson if (sc->flags & IPW_FLAG_ENABLED) {
213388d3b172SAndrew Thompson params = 0; /* XXX? */
213488d3b172SAndrew Thompson error = ipw_cmd(sc, IPW_CMD_BROADCAST_SCAN,
213588d3b172SAndrew Thompson ¶ms, sizeof(params));
213688d3b172SAndrew Thompson } else
213788d3b172SAndrew Thompson error = ipw_enable(sc);
213888d3b172SAndrew Thompson done:
213988d3b172SAndrew Thompson if (error != 0) {
214088d3b172SAndrew Thompson DPRINTF(("Scan failed\n"));
214188d3b172SAndrew Thompson sc->flags &= ~(IPW_FLAG_SCANNING | IPW_FLAG_HACK);
214288d3b172SAndrew Thompson }
214388d3b172SAndrew Thompson return (error);
214488d3b172SAndrew Thompson }
214588d3b172SAndrew Thompson
214688d3b172SAndrew Thompson static int
ipw_setchannel(struct ipw_softc * sc,struct ieee80211_channel * chan)214788d3b172SAndrew Thompson ipw_setchannel(struct ipw_softc *sc, struct ieee80211_channel *chan)
214888d3b172SAndrew Thompson {
21497a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
215088d3b172SAndrew Thompson uint32_t data;
215188d3b172SAndrew Thompson int error;
215288d3b172SAndrew Thompson
215388d3b172SAndrew Thompson data = htole32(ieee80211_chan2ieee(ic, chan));
215488d3b172SAndrew Thompson DPRINTF(("Setting channel to %u\n", le32toh(data)));
215588d3b172SAndrew Thompson error = ipw_cmd(sc, IPW_CMD_SET_CHANNEL, &data, sizeof data);
215688d3b172SAndrew Thompson if (error == 0)
215788d3b172SAndrew Thompson ipw_setcurchan(sc, chan);
215888d3b172SAndrew Thompson return error;
215988d3b172SAndrew Thompson }
216088d3b172SAndrew Thompson
2161b032f27cSSam Leffler static void
ipw_assoc(struct ieee80211com * ic,struct ieee80211vap * vap)21625efea30fSAndrew Thompson ipw_assoc(struct ieee80211com *ic, struct ieee80211vap *vap)
2163b032f27cSSam Leffler {
21642cb71df1SAdrian Chadd struct ipw_softc *sc = ic->ic_softc;
2165b032f27cSSam Leffler struct ieee80211_node *ni = vap->iv_bss;
2166b032f27cSSam Leffler struct ipw_security security;
2167b032f27cSSam Leffler uint32_t data;
2168b032f27cSSam Leffler int error;
2169b032f27cSSam Leffler
2170b032f27cSSam Leffler IPW_LOCK(sc);
2171b032f27cSSam Leffler error = ipw_disable(sc);
2172b032f27cSSam Leffler if (error != 0)
2173b032f27cSSam Leffler goto done;
2174b032f27cSSam Leffler
2175b032f27cSSam Leffler memset(&security, 0, sizeof security);
2176b032f27cSSam Leffler security.authmode = (ni->ni_authmode == IEEE80211_AUTH_SHARED) ?
2177b032f27cSSam Leffler IPW_AUTH_SHARED : IPW_AUTH_OPEN;
2178b032f27cSSam Leffler security.ciphers = htole32(IPW_CIPHER_NONE);
2179b032f27cSSam Leffler DPRINTF(("Setting authmode to %u\n", security.authmode));
2180b032f27cSSam Leffler error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFO, &security,
2181b032f27cSSam Leffler sizeof security);
2182b032f27cSSam Leffler if (error != 0)
2183b032f27cSSam Leffler goto done;
2184b032f27cSSam Leffler
2185b032f27cSSam Leffler data = htole32(vap->iv_rtsthreshold);
2186b032f27cSSam Leffler DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2187b032f27cSSam Leffler error = ipw_cmd(sc, IPW_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
2188b032f27cSSam Leffler if (error != 0)
2189b032f27cSSam Leffler goto done;
2190b032f27cSSam Leffler
2191b032f27cSSam Leffler data = htole32(vap->iv_fragthreshold);
2192b032f27cSSam Leffler DPRINTF(("Setting frag threshold to %u\n", le32toh(data)));
2193b032f27cSSam Leffler error = ipw_cmd(sc, IPW_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
2194b032f27cSSam Leffler if (error != 0)
2195b032f27cSSam Leffler goto done;
2196b032f27cSSam Leffler
2197b032f27cSSam Leffler if (vap->iv_flags & IEEE80211_F_PRIVACY) {
2198b032f27cSSam Leffler error = ipw_setwepkeys(sc);
2199b032f27cSSam Leffler if (error != 0)
2200b032f27cSSam Leffler goto done;
2201b032f27cSSam Leffler
2202b032f27cSSam Leffler if (vap->iv_def_txkey != IEEE80211_KEYIX_NONE) {
2203b032f27cSSam Leffler data = htole32(vap->iv_def_txkey);
2204b032f27cSSam Leffler DPRINTF(("Setting wep tx key index to %u\n",
2205b032f27cSSam Leffler le32toh(data)));
2206b032f27cSSam Leffler error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY_INDEX, &data,
2207b032f27cSSam Leffler sizeof data);
2208b032f27cSSam Leffler if (error != 0)
2209b032f27cSSam Leffler goto done;
2210b032f27cSSam Leffler }
2211b032f27cSSam Leffler }
2212b032f27cSSam Leffler
2213b032f27cSSam Leffler data = htole32((vap->iv_flags & IEEE80211_F_PRIVACY) ? IPW_WEPON : 0);
2214b032f27cSSam Leffler DPRINTF(("Setting wep flags to 0x%x\n", le32toh(data)));
2215b032f27cSSam Leffler error = ipw_cmd(sc, IPW_CMD_SET_WEP_FLAGS, &data, sizeof data);
2216b032f27cSSam Leffler if (error != 0)
2217b032f27cSSam Leffler goto done;
2218b032f27cSSam Leffler
2219b032f27cSSam Leffler error = ipw_setssid(sc, ni->ni_essid, ni->ni_esslen);
2220b032f27cSSam Leffler if (error != 0)
2221b032f27cSSam Leffler goto done;
2222b032f27cSSam Leffler
2223b032f27cSSam Leffler error = ipw_setbssid(sc, ni->ni_bssid);
2224b032f27cSSam Leffler if (error != 0)
2225b032f27cSSam Leffler goto done;
2226b032f27cSSam Leffler
22276e4c30c9SBernhard Schmidt if (vap->iv_appie_wpa != NULL) {
22286e4c30c9SBernhard Schmidt struct ieee80211_appie *ie = vap->iv_appie_wpa;
2229b032f27cSSam Leffler error = ipw_setwpaie(sc, ie->ie_data, ie->ie_len);
2230b032f27cSSam Leffler if (error != 0)
2231b032f27cSSam Leffler goto done;
2232b032f27cSSam Leffler }
2233b032f27cSSam Leffler if (ic->ic_opmode == IEEE80211_M_IBSS) {
2234b032f27cSSam Leffler error = ipw_setchannel(sc, ni->ni_chan);
2235b032f27cSSam Leffler if (error != 0)
2236b032f27cSSam Leffler goto done;
2237b032f27cSSam Leffler }
2238b032f27cSSam Leffler
2239b032f27cSSam Leffler /* lock scan to ap's channel and enable associate */
2240b032f27cSSam Leffler error = ipw_setscanopts(sc,
2241b032f27cSSam Leffler 1<<(ieee80211_chan2ieee(ic, ni->ni_chan)-1), 0);
2242b032f27cSSam Leffler if (error != 0)
2243b032f27cSSam Leffler goto done;
2244b032f27cSSam Leffler
2245b032f27cSSam Leffler error = ipw_enable(sc); /* finally, enable adapter */
2246b032f27cSSam Leffler if (error == 0)
2247b032f27cSSam Leffler sc->flags |= IPW_FLAG_ASSOCIATING;
2248b032f27cSSam Leffler done:
2249b032f27cSSam Leffler IPW_UNLOCK(sc);
2250b032f27cSSam Leffler }
2251b032f27cSSam Leffler
2252b032f27cSSam Leffler static void
ipw_disassoc(struct ieee80211com * ic,struct ieee80211vap * vap)22535efea30fSAndrew Thompson ipw_disassoc(struct ieee80211com *ic, struct ieee80211vap *vap)
2254b032f27cSSam Leffler {
2255b032f27cSSam Leffler struct ieee80211_node *ni = vap->iv_bss;
22562cb71df1SAdrian Chadd struct ipw_softc *sc = ic->ic_softc;
2257b032f27cSSam Leffler
2258b032f27cSSam Leffler IPW_LOCK(sc);
2259b032f27cSSam Leffler DPRINTF(("Disassociate from %6D\n", ni->ni_bssid, ":"));
2260b032f27cSSam Leffler /*
2261b032f27cSSam Leffler * NB: don't try to do this if ipw_stop_master has
2262b032f27cSSam Leffler * shutdown the firmware and disabled interrupts.
2263b032f27cSSam Leffler */
2264b032f27cSSam Leffler if (sc->flags & IPW_FLAG_FW_INITED) {
2265b032f27cSSam Leffler sc->flags &= ~IPW_FLAG_ASSOCIATED;
2266b032f27cSSam Leffler /*
2267b032f27cSSam Leffler * NB: firmware currently ignores bssid parameter, but
2268b032f27cSSam Leffler * supply it in case this changes (follow linux driver).
2269b032f27cSSam Leffler */
2270b032f27cSSam Leffler (void) ipw_cmd(sc, IPW_CMD_DISASSOCIATE,
2271b032f27cSSam Leffler ni->ni_bssid, IEEE80211_ADDR_LEN);
2272b032f27cSSam Leffler }
2273b032f27cSSam Leffler IPW_UNLOCK(sc);
2274b032f27cSSam Leffler }
2275b032f27cSSam Leffler
2276b032f27cSSam Leffler /*
2277b032f27cSSam Leffler * Handler for sc_init_task. This is a simple wrapper around ipw_init().
2278b032f27cSSam Leffler * It is called on firmware panics or on watchdog timeouts.
2279b032f27cSSam Leffler */
2280b032f27cSSam Leffler static void
ipw_init_task(void * context,int pending)2281b032f27cSSam Leffler ipw_init_task(void *context, int pending)
2282b032f27cSSam Leffler {
2283b032f27cSSam Leffler ipw_init(context);
2284b032f27cSSam Leffler }
2285b032f27cSSam Leffler
2286b032f27cSSam Leffler static void
ipw_init(void * priv)2287b032f27cSSam Leffler ipw_init(void *priv)
2288b032f27cSSam Leffler {
2289b032f27cSSam Leffler struct ipw_softc *sc = priv;
22907a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
2291b032f27cSSam Leffler
2292b032f27cSSam Leffler IPW_LOCK(sc);
2293b032f27cSSam Leffler ipw_init_locked(sc);
2294b032f27cSSam Leffler IPW_UNLOCK(sc);
2295b032f27cSSam Leffler
22967a79cebfSGleb Smirnoff if (sc->flags & IPW_FLAG_RUNNING)
229777197f9cSAndrew Thompson ieee80211_start_all(ic); /* start all vap's */
2298b032f27cSSam Leffler }
2299b032f27cSSam Leffler
2300b032f27cSSam Leffler static void
ipw_init_locked(struct ipw_softc * sc)2301b032f27cSSam Leffler ipw_init_locked(struct ipw_softc *sc)
2302b032f27cSSam Leffler {
23037a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
2304b032f27cSSam Leffler struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2305b032f27cSSam Leffler const struct firmware *fp;
2306b032f27cSSam Leffler const struct ipw_firmware_hdr *hdr;
2307b032f27cSSam Leffler const char *fw;
2308b032f27cSSam Leffler
2309b032f27cSSam Leffler IPW_LOCK_ASSERT(sc);
2310b032f27cSSam Leffler
2311b032f27cSSam Leffler DPRINTF(("%s: state %s flags 0x%x\n", __func__,
2312b032f27cSSam Leffler ieee80211_state_name[vap->iv_state], sc->flags));
2313b032f27cSSam Leffler
2314b032f27cSSam Leffler /*
2315b032f27cSSam Leffler * Avoid re-entrant calls. We need to release the mutex in ipw_init()
2316b032f27cSSam Leffler * when loading the firmware and we don't want to be called during this
2317b032f27cSSam Leffler * operation.
2318b032f27cSSam Leffler */
2319b032f27cSSam Leffler if (sc->flags & IPW_FLAG_INIT_LOCKED)
2320b032f27cSSam Leffler return;
2321b032f27cSSam Leffler sc->flags |= IPW_FLAG_INIT_LOCKED;
2322b032f27cSSam Leffler
2323b032f27cSSam Leffler ipw_stop_locked(sc);
2324b032f27cSSam Leffler
2325b032f27cSSam Leffler if (ipw_reset(sc) != 0) {
2326b032f27cSSam Leffler device_printf(sc->sc_dev, "could not reset adapter\n");
2327b032f27cSSam Leffler goto fail;
2328b032f27cSSam Leffler }
2329b032f27cSSam Leffler
2330b032f27cSSam Leffler if (sc->sc_firmware == NULL) {
2331b032f27cSSam Leffler device_printf(sc->sc_dev, "no firmware\n");
2332b032f27cSSam Leffler goto fail;
2333b032f27cSSam Leffler }
2334b032f27cSSam Leffler /* NB: consistency already checked on load */
2335b032f27cSSam Leffler fp = sc->sc_firmware;
2336b032f27cSSam Leffler hdr = (const struct ipw_firmware_hdr *)fp->data;
2337b032f27cSSam Leffler
2338b032f27cSSam Leffler DPRINTF(("Loading firmware image '%s'\n", fp->name));
2339b032f27cSSam Leffler fw = (const char *)fp->data + sizeof *hdr + le32toh(hdr->mainsz);
2340b032f27cSSam Leffler if (ipw_load_ucode(sc, fw, le32toh(hdr->ucodesz)) != 0) {
2341b032f27cSSam Leffler device_printf(sc->sc_dev, "could not load microcode\n");
2342b032f27cSSam Leffler goto fail;
2343b032f27cSSam Leffler }
2344b032f27cSSam Leffler
2345b032f27cSSam Leffler ipw_stop_master(sc);
2346b032f27cSSam Leffler
2347b032f27cSSam Leffler /*
2348b032f27cSSam Leffler * Setup tx, rx and status rings.
2349b032f27cSSam Leffler */
2350b032f27cSSam Leffler sc->txold = IPW_NTBD - 1;
2351b032f27cSSam Leffler sc->txcur = 0;
2352b032f27cSSam Leffler sc->txfree = IPW_NTBD - 2;
2353b032f27cSSam Leffler sc->rxcur = IPW_NRBD - 1;
2354b032f27cSSam Leffler
2355b032f27cSSam Leffler CSR_WRITE_4(sc, IPW_CSR_TX_BASE, sc->tbd_phys);
2356b032f27cSSam Leffler CSR_WRITE_4(sc, IPW_CSR_TX_SIZE, IPW_NTBD);
2357b032f27cSSam Leffler CSR_WRITE_4(sc, IPW_CSR_TX_READ, 0);
2358b032f27cSSam Leffler CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
2359b032f27cSSam Leffler
2360b032f27cSSam Leffler CSR_WRITE_4(sc, IPW_CSR_RX_BASE, sc->rbd_phys);
2361b032f27cSSam Leffler CSR_WRITE_4(sc, IPW_CSR_RX_SIZE, IPW_NRBD);
2362b032f27cSSam Leffler CSR_WRITE_4(sc, IPW_CSR_RX_READ, 0);
2363b032f27cSSam Leffler CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
2364b032f27cSSam Leffler
2365b032f27cSSam Leffler CSR_WRITE_4(sc, IPW_CSR_STATUS_BASE, sc->status_phys);
2366b032f27cSSam Leffler
2367b032f27cSSam Leffler fw = (const char *)fp->data + sizeof *hdr;
2368b032f27cSSam Leffler if (ipw_load_firmware(sc, fw, le32toh(hdr->mainsz)) != 0) {
2369b032f27cSSam Leffler device_printf(sc->sc_dev, "could not load firmware\n");
2370b032f27cSSam Leffler goto fail;
2371b032f27cSSam Leffler }
2372b032f27cSSam Leffler
2373b032f27cSSam Leffler sc->flags |= IPW_FLAG_FW_INITED;
2374b032f27cSSam Leffler
2375b032f27cSSam Leffler /* retrieve information tables base addresses */
2376b032f27cSSam Leffler sc->table1_base = CSR_READ_4(sc, IPW_CSR_TABLE1_BASE);
2377b032f27cSSam Leffler sc->table2_base = CSR_READ_4(sc, IPW_CSR_TABLE2_BASE);
2378b032f27cSSam Leffler
2379b032f27cSSam Leffler ipw_write_table1(sc, IPW_INFO_LOCK, 0);
2380b032f27cSSam Leffler
2381b032f27cSSam Leffler if (ipw_config(sc) != 0) {
2382b032f27cSSam Leffler device_printf(sc->sc_dev, "device configuration failed\n");
2383b032f27cSSam Leffler goto fail;
2384b032f27cSSam Leffler }
2385b032f27cSSam Leffler
2386b032f27cSSam Leffler callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
23877a79cebfSGleb Smirnoff sc->flags |= IPW_FLAG_RUNNING;
2388b032f27cSSam Leffler sc->flags &= ~IPW_FLAG_INIT_LOCKED;
2389b032f27cSSam Leffler return;
2390b032f27cSSam Leffler
2391b032f27cSSam Leffler fail:
2392b032f27cSSam Leffler ipw_stop_locked(sc);
2393b032f27cSSam Leffler sc->flags &= ~IPW_FLAG_INIT_LOCKED;
2394b032f27cSSam Leffler }
2395b032f27cSSam Leffler
239688d3b172SAndrew Thompson static int
ipw_config(struct ipw_softc * sc)2397ceaec73dSDamien Bergamini ipw_config(struct ipw_softc *sc)
2398ceaec73dSDamien Bergamini {
23997a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
2400ceaec73dSDamien Bergamini struct ipw_configuration config;
2401ceaec73dSDamien Bergamini uint32_t data;
240288d3b172SAndrew Thompson int error;
240388d3b172SAndrew Thompson
240488d3b172SAndrew Thompson error = ipw_disable(sc);
240588d3b172SAndrew Thompson if (error != 0)
240688d3b172SAndrew Thompson return error;
2407ceaec73dSDamien Bergamini
2408ceaec73dSDamien Bergamini switch (ic->ic_opmode) {
2409ceaec73dSDamien Bergamini case IEEE80211_M_STA:
2410ceaec73dSDamien Bergamini case IEEE80211_M_HOSTAP:
241168e8e04eSSam Leffler case IEEE80211_M_WDS: /* XXX */
2412ceaec73dSDamien Bergamini data = htole32(IPW_MODE_BSS);
2413ceaec73dSDamien Bergamini break;
2414ceaec73dSDamien Bergamini case IEEE80211_M_IBSS:
2415ceaec73dSDamien Bergamini case IEEE80211_M_AHDEMO:
2416ceaec73dSDamien Bergamini data = htole32(IPW_MODE_IBSS);
2417ceaec73dSDamien Bergamini break;
2418ceaec73dSDamien Bergamini case IEEE80211_M_MONITOR:
2419ceaec73dSDamien Bergamini data = htole32(IPW_MODE_MONITOR);
2420ceaec73dSDamien Bergamini break;
2421820e6a1fSRui Paulo default:
2422820e6a1fSRui Paulo device_printf(sc->sc_dev, "unknown opmode %d\n", ic->ic_opmode);
2423820e6a1fSRui Paulo return EINVAL;
2424ceaec73dSDamien Bergamini }
2425ceaec73dSDamien Bergamini DPRINTF(("Setting mode to %u\n", le32toh(data)));
2426ceaec73dSDamien Bergamini error = ipw_cmd(sc, IPW_CMD_SET_MODE, &data, sizeof data);
2427ceaec73dSDamien Bergamini if (error != 0)
2428ceaec73dSDamien Bergamini return error;
2429ceaec73dSDamien Bergamini
2430ceaec73dSDamien Bergamini if (ic->ic_opmode == IEEE80211_M_IBSS ||
2431ceaec73dSDamien Bergamini ic->ic_opmode == IEEE80211_M_MONITOR) {
243288d3b172SAndrew Thompson error = ipw_setchannel(sc, ic->ic_curchan);
2433ceaec73dSDamien Bergamini if (error != 0)
2434ceaec73dSDamien Bergamini return error;
2435ceaec73dSDamien Bergamini }
2436ceaec73dSDamien Bergamini
243788d3b172SAndrew Thompson if (ic->ic_opmode == IEEE80211_M_MONITOR)
243888d3b172SAndrew Thompson return ipw_enable(sc);
2439ceaec73dSDamien Bergamini
2440ceaec73dSDamien Bergamini config.flags = htole32(IPW_CFG_BSS_MASK | IPW_CFG_IBSS_MASK |
2441ceaec73dSDamien Bergamini IPW_CFG_PREAMBLE_AUTO | IPW_CFG_802_1x_ENABLE);
2442ceaec73dSDamien Bergamini if (ic->ic_opmode == IEEE80211_M_IBSS)
2443ceaec73dSDamien Bergamini config.flags |= htole32(IPW_CFG_IBSS_AUTO_START);
24447a79cebfSGleb Smirnoff if (ic->ic_promisc > 0)
2445ceaec73dSDamien Bergamini config.flags |= htole32(IPW_CFG_PROMISCUOUS);
2446ceaec73dSDamien Bergamini config.bss_chan = htole32(0x3fff); /* channels 1-14 */
2447ceaec73dSDamien Bergamini config.ibss_chan = htole32(0x7ff); /* channels 1-11 */
2448ceaec73dSDamien Bergamini DPRINTF(("Setting configuration to 0x%x\n", le32toh(config.flags)));
2449ceaec73dSDamien Bergamini error = ipw_cmd(sc, IPW_CMD_SET_CONFIGURATION, &config, sizeof config);
2450ceaec73dSDamien Bergamini if (error != 0)
2451ceaec73dSDamien Bergamini return error;
2452ceaec73dSDamien Bergamini
2453227901e1SBernhard Schmidt data = htole32(0xf); /* 1, 2, 5.5, 11 */
2454ceaec73dSDamien Bergamini DPRINTF(("Setting basic tx rates to 0x%x\n", le32toh(data)));
2455ceaec73dSDamien Bergamini error = ipw_cmd(sc, IPW_CMD_SET_BASIC_TX_RATES, &data, sizeof data);
2456ceaec73dSDamien Bergamini if (error != 0)
2457ceaec73dSDamien Bergamini return error;
2458ceaec73dSDamien Bergamini
2459227901e1SBernhard Schmidt /* Use the same rate set */
246088d3b172SAndrew Thompson DPRINTF(("Setting msdu tx rates to 0x%x\n", le32toh(data)));
246188d3b172SAndrew Thompson error = ipw_cmd(sc, IPW_CMD_SET_MSDU_TX_RATES, &data, sizeof data);
246288d3b172SAndrew Thompson if (error != 0)
246388d3b172SAndrew Thompson return error;
246488d3b172SAndrew Thompson
2465227901e1SBernhard Schmidt /* Use the same rate set */
2466ceaec73dSDamien Bergamini DPRINTF(("Setting tx rates to 0x%x\n", le32toh(data)));
2467ceaec73dSDamien Bergamini error = ipw_cmd(sc, IPW_CMD_SET_TX_RATES, &data, sizeof data);
2468ceaec73dSDamien Bergamini if (error != 0)
2469ceaec73dSDamien Bergamini return error;
2470ceaec73dSDamien Bergamini
2471ceaec73dSDamien Bergamini data = htole32(IPW_POWER_MODE_CAM);
2472ceaec73dSDamien Bergamini DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2473ceaec73dSDamien Bergamini error = ipw_cmd(sc, IPW_CMD_SET_POWER_MODE, &data, sizeof data);
2474ceaec73dSDamien Bergamini if (error != 0)
2475ceaec73dSDamien Bergamini return error;
2476ceaec73dSDamien Bergamini
2477ceaec73dSDamien Bergamini if (ic->ic_opmode == IEEE80211_M_IBSS) {
2478ceaec73dSDamien Bergamini data = htole32(32); /* default value */
2479ceaec73dSDamien Bergamini DPRINTF(("Setting tx power index to %u\n", le32toh(data)));
2480ceaec73dSDamien Bergamini error = ipw_cmd(sc, IPW_CMD_SET_TX_POWER_INDEX, &data,
2481ceaec73dSDamien Bergamini sizeof data);
2482ceaec73dSDamien Bergamini if (error != 0)
2483ceaec73dSDamien Bergamini return error;
2484ceaec73dSDamien Bergamini }
2485ceaec73dSDamien Bergamini
2486b032f27cSSam Leffler return 0;
2487ceaec73dSDamien Bergamini }
2488ceaec73dSDamien Bergamini
2489ceaec73dSDamien Bergamini static void
ipw_stop(void * priv)2490ceaec73dSDamien Bergamini ipw_stop(void *priv)
2491ceaec73dSDamien Bergamini {
2492ceaec73dSDamien Bergamini struct ipw_softc *sc = priv;
249388d3b172SAndrew Thompson
249488d3b172SAndrew Thompson IPW_LOCK(sc);
249588d3b172SAndrew Thompson ipw_stop_locked(sc);
249688d3b172SAndrew Thompson IPW_UNLOCK(sc);
249788d3b172SAndrew Thompson }
249888d3b172SAndrew Thompson
249988d3b172SAndrew Thompson static void
ipw_stop_locked(struct ipw_softc * sc)250088d3b172SAndrew Thompson ipw_stop_locked(struct ipw_softc *sc)
250188d3b172SAndrew Thompson {
2502ceaec73dSDamien Bergamini int i;
2503ceaec73dSDamien Bergamini
250488d3b172SAndrew Thompson IPW_LOCK_ASSERT(sc);
250584ca7670SDamien Bergamini
250688d3b172SAndrew Thompson callout_stop(&sc->sc_wdtimer);
2507ceaec73dSDamien Bergamini ipw_stop_master(sc);
2508ceaec73dSDamien Bergamini
2509ceaec73dSDamien Bergamini CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET);
2510ceaec73dSDamien Bergamini
2511ceaec73dSDamien Bergamini /*
2512ceaec73dSDamien Bergamini * Release tx buffers.
2513ceaec73dSDamien Bergamini */
2514ceaec73dSDamien Bergamini for (i = 0; i < IPW_NTBD; i++)
2515ceaec73dSDamien Bergamini ipw_release_sbd(sc, &sc->stbd_list[i]);
2516ceaec73dSDamien Bergamini
2517ceaec73dSDamien Bergamini sc->sc_tx_timer = 0;
25187a79cebfSGleb Smirnoff sc->flags &= ~IPW_FLAG_RUNNING;
2519ceaec73dSDamien Bergamini }
2520ceaec73dSDamien Bergamini
2521ceaec73dSDamien Bergamini static int
ipw_sysctl_stats(SYSCTL_HANDLER_ARGS)2522ceaec73dSDamien Bergamini ipw_sysctl_stats(SYSCTL_HANDLER_ARGS)
2523ceaec73dSDamien Bergamini {
2524ceaec73dSDamien Bergamini struct ipw_softc *sc = arg1;
2525ceaec73dSDamien Bergamini uint32_t i, size, buf[256];
2526ceaec73dSDamien Bergamini
25273afda804SDamien Bergamini memset(buf, 0, sizeof buf);
25286c3c35e4SPhilip Paeps
25296c3c35e4SPhilip Paeps if (!(sc->flags & IPW_FLAG_FW_INITED))
2530ceaec73dSDamien Bergamini return SYSCTL_OUT(req, buf, sizeof buf);
2531ceaec73dSDamien Bergamini
2532ceaec73dSDamien Bergamini CSR_WRITE_4(sc, IPW_CSR_AUTOINC_ADDR, sc->table1_base);
2533ceaec73dSDamien Bergamini
2534ceaec73dSDamien Bergamini size = min(CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA), 256);
2535ceaec73dSDamien Bergamini for (i = 1; i < size; i++)
2536ceaec73dSDamien Bergamini buf[i] = MEM_READ_4(sc, CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA));
2537ceaec73dSDamien Bergamini
25386c3c35e4SPhilip Paeps return SYSCTL_OUT(req, buf, size);
2539ceaec73dSDamien Bergamini }
2540ceaec73dSDamien Bergamini
2541ceaec73dSDamien Bergamini static int
ipw_sysctl_radio(SYSCTL_HANDLER_ARGS)2542ceaec73dSDamien Bergamini ipw_sysctl_radio(SYSCTL_HANDLER_ARGS)
2543ceaec73dSDamien Bergamini {
2544ceaec73dSDamien Bergamini struct ipw_softc *sc = arg1;
2545ceaec73dSDamien Bergamini int val;
2546ceaec73dSDamien Bergamini
2547ceaec73dSDamien Bergamini val = !((sc->flags & IPW_FLAG_HAS_RADIO_SWITCH) &&
2548ceaec73dSDamien Bergamini (CSR_READ_4(sc, IPW_CSR_IO) & IPW_IO_RADIO_DISABLED));
2549ceaec73dSDamien Bergamini
2550ceaec73dSDamien Bergamini return SYSCTL_OUT(req, &val, sizeof val);
2551ceaec73dSDamien Bergamini }
2552ceaec73dSDamien Bergamini
2553ceaec73dSDamien Bergamini static uint32_t
ipw_read_table1(struct ipw_softc * sc,uint32_t off)2554ceaec73dSDamien Bergamini ipw_read_table1(struct ipw_softc *sc, uint32_t off)
2555ceaec73dSDamien Bergamini {
2556ceaec73dSDamien Bergamini return MEM_READ_4(sc, MEM_READ_4(sc, sc->table1_base + off));
2557ceaec73dSDamien Bergamini }
2558ceaec73dSDamien Bergamini
2559ceaec73dSDamien Bergamini static void
ipw_write_table1(struct ipw_softc * sc,uint32_t off,uint32_t info)2560ceaec73dSDamien Bergamini ipw_write_table1(struct ipw_softc *sc, uint32_t off, uint32_t info)
2561ceaec73dSDamien Bergamini {
2562ceaec73dSDamien Bergamini MEM_WRITE_4(sc, MEM_READ_4(sc, sc->table1_base + off), info);
2563ceaec73dSDamien Bergamini }
2564ceaec73dSDamien Bergamini
256588d3b172SAndrew Thompson #if 0
2566ceaec73dSDamien Bergamini static int
2567ceaec73dSDamien Bergamini ipw_read_table2(struct ipw_softc *sc, uint32_t off, void *buf, uint32_t *len)
2568ceaec73dSDamien Bergamini {
2569ceaec73dSDamien Bergamini uint32_t addr, info;
2570ceaec73dSDamien Bergamini uint16_t count, size;
2571ceaec73dSDamien Bergamini uint32_t total;
2572ceaec73dSDamien Bergamini
2573ceaec73dSDamien Bergamini /* addr[4] + count[2] + size[2] */
2574ceaec73dSDamien Bergamini addr = MEM_READ_4(sc, sc->table2_base + off);
2575ceaec73dSDamien Bergamini info = MEM_READ_4(sc, sc->table2_base + off + 4);
2576ceaec73dSDamien Bergamini
2577ceaec73dSDamien Bergamini count = info >> 16;
2578ceaec73dSDamien Bergamini size = info & 0xffff;
2579ceaec73dSDamien Bergamini total = count * size;
2580ceaec73dSDamien Bergamini
2581ceaec73dSDamien Bergamini if (total > *len) {
2582ceaec73dSDamien Bergamini *len = total;
2583ceaec73dSDamien Bergamini return EINVAL;
2584ceaec73dSDamien Bergamini }
2585ceaec73dSDamien Bergamini
2586ceaec73dSDamien Bergamini *len = total;
2587ceaec73dSDamien Bergamini ipw_read_mem_1(sc, addr, buf, total);
2588ceaec73dSDamien Bergamini
2589ceaec73dSDamien Bergamini return 0;
2590ceaec73dSDamien Bergamini }
2591ceaec73dSDamien Bergamini
2592ceaec73dSDamien Bergamini static void
2593ceaec73dSDamien Bergamini ipw_read_mem_1(struct ipw_softc *sc, bus_size_t offset, uint8_t *datap,
2594ceaec73dSDamien Bergamini bus_size_t count)
2595ceaec73dSDamien Bergamini {
2596ceaec73dSDamien Bergamini for (; count > 0; offset++, datap++, count--) {
2597ceaec73dSDamien Bergamini CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2598ceaec73dSDamien Bergamini *datap = CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3));
2599ceaec73dSDamien Bergamini }
2600ceaec73dSDamien Bergamini }
260188d3b172SAndrew Thompson #endif
2602ceaec73dSDamien Bergamini
2603ceaec73dSDamien Bergamini static void
ipw_write_mem_1(struct ipw_softc * sc,bus_size_t offset,const uint8_t * datap,bus_size_t count)260484ca7670SDamien Bergamini ipw_write_mem_1(struct ipw_softc *sc, bus_size_t offset, const uint8_t *datap,
2605ceaec73dSDamien Bergamini bus_size_t count)
2606ceaec73dSDamien Bergamini {
2607ceaec73dSDamien Bergamini for (; count > 0; offset++, datap++, count--) {
2608ceaec73dSDamien Bergamini CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2609ceaec73dSDamien Bergamini CSR_WRITE_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3), *datap);
2610ceaec73dSDamien Bergamini }
2611ceaec73dSDamien Bergamini }
261288d3b172SAndrew Thompson
261388d3b172SAndrew Thompson static void
ipw_scan_start(struct ieee80211com * ic)261488d3b172SAndrew Thompson ipw_scan_start(struct ieee80211com *ic)
261588d3b172SAndrew Thompson {
26162cb71df1SAdrian Chadd struct ipw_softc *sc = ic->ic_softc;
261788d3b172SAndrew Thompson
261888d3b172SAndrew Thompson IPW_LOCK(sc);
26195efea30fSAndrew Thompson ipw_scan(sc);
262088d3b172SAndrew Thompson IPW_UNLOCK(sc);
262188d3b172SAndrew Thompson }
262288d3b172SAndrew Thompson
262388d3b172SAndrew Thompson static void
ipw_getradiocaps(struct ieee80211com * ic,int maxchans,int * nchans,struct ieee80211_channel chans[])26240587ad6bSAndriy Voskoboinyk ipw_getradiocaps(struct ieee80211com *ic,
26250587ad6bSAndriy Voskoboinyk int maxchans, int *nchans, struct ieee80211_channel chans[])
26260587ad6bSAndriy Voskoboinyk {
26270587ad6bSAndriy Voskoboinyk struct ipw_softc *sc = ic->ic_softc;
26280587ad6bSAndriy Voskoboinyk uint8_t bands[IEEE80211_MODE_BYTES];
26290587ad6bSAndriy Voskoboinyk int i;
26300587ad6bSAndriy Voskoboinyk
26310587ad6bSAndriy Voskoboinyk memset(bands, 0, sizeof(bands));
26320587ad6bSAndriy Voskoboinyk setbit(bands, IEEE80211_MODE_11B);
26330587ad6bSAndriy Voskoboinyk
26340587ad6bSAndriy Voskoboinyk for (i = 1; i < 16; i++) {
26350587ad6bSAndriy Voskoboinyk if (sc->chanmask & (1 << i)) {
26360587ad6bSAndriy Voskoboinyk ieee80211_add_channel(chans, maxchans, nchans,
26370587ad6bSAndriy Voskoboinyk i, 0, 0, 0, bands);
26380587ad6bSAndriy Voskoboinyk }
26390587ad6bSAndriy Voskoboinyk }
26400587ad6bSAndriy Voskoboinyk
26410587ad6bSAndriy Voskoboinyk }
26420587ad6bSAndriy Voskoboinyk
26430587ad6bSAndriy Voskoboinyk static void
ipw_set_channel(struct ieee80211com * ic)264488d3b172SAndrew Thompson ipw_set_channel(struct ieee80211com *ic)
264588d3b172SAndrew Thompson {
26462cb71df1SAdrian Chadd struct ipw_softc *sc = ic->ic_softc;
264788d3b172SAndrew Thompson
264888d3b172SAndrew Thompson IPW_LOCK(sc);
264988d3b172SAndrew Thompson if (ic->ic_opmode == IEEE80211_M_MONITOR) {
265088d3b172SAndrew Thompson ipw_disable(sc);
265188d3b172SAndrew Thompson ipw_setchannel(sc, ic->ic_curchan);
265288d3b172SAndrew Thompson ipw_enable(sc);
265388d3b172SAndrew Thompson }
265488d3b172SAndrew Thompson IPW_UNLOCK(sc);
265588d3b172SAndrew Thompson }
265688d3b172SAndrew Thompson
265788d3b172SAndrew Thompson static void
ipw_scan_curchan(struct ieee80211_scan_state * ss,unsigned long maxdwell)2658b032f27cSSam Leffler ipw_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
265988d3b172SAndrew Thompson {
266088d3b172SAndrew Thompson /* NB: all channels are scanned at once */
266188d3b172SAndrew Thompson }
266288d3b172SAndrew Thompson
266388d3b172SAndrew Thompson static void
ipw_scan_mindwell(struct ieee80211_scan_state * ss)2664b032f27cSSam Leffler ipw_scan_mindwell(struct ieee80211_scan_state *ss)
266588d3b172SAndrew Thompson {
266688d3b172SAndrew Thompson /* NB: don't try to abort scan; wait for firmware to finish */
266788d3b172SAndrew Thompson }
266888d3b172SAndrew Thompson
266988d3b172SAndrew Thompson static void
ipw_scan_end(struct ieee80211com * ic)267088d3b172SAndrew Thompson ipw_scan_end(struct ieee80211com *ic)
267188d3b172SAndrew Thompson {
26722cb71df1SAdrian Chadd struct ipw_softc *sc = ic->ic_softc;
267388d3b172SAndrew Thompson
267488d3b172SAndrew Thompson IPW_LOCK(sc);
267588d3b172SAndrew Thompson sc->flags &= ~IPW_FLAG_SCANNING;
267688d3b172SAndrew Thompson IPW_UNLOCK(sc);
267788d3b172SAndrew Thompson }
2678