nrelease - fix/improve livecd
[dragonfly.git] / sys / dev / netif / iwi / if_iwi.c
... / ...
CommitLineData
1/*-
2 * Copyright (c) 2004, 2005
3 * Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
4 * Copyright (c) 2005-2006 Sam Leffler, Errno Consulting
5 * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
12 * disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: head/sys/dev/iwi/if_iwi.c 298818 2016-04-29 22:14:11Z avos $
30 */
31
32/*-
33 * Intel(R) PRO/Wireless 2200BG/2225BG/2915ABG driver
34 * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
35 */
36
37#include <sys/param.h>
38#include <sys/sysctl.h>
39#include <sys/sockio.h>
40#include <sys/mbuf.h>
41#include <sys/kernel.h>
42#include <sys/socket.h>
43#include <sys/systm.h>
44#include <sys/malloc.h>
45#include <sys/lock.h>
46#include <sys/module.h>
47#include <sys/bus.h>
48#include <sys/endian.h>
49#include <sys/proc.h>
50#include <sys/mount.h>
51#include <sys/linker.h>
52#include <sys/firmware.h>
53#include <sys/taskqueue.h>
54#if defined(__DragonFly__)
55#include <sys/devfs.h>
56#endif
57
58#if !defined(__DragonFly__)
59#include <machine/bus.h>
60#include <machine/resource.h>
61#endif
62#include <sys/rman.h>
63
64#if defined(__DragonFly__)
65#include <bus/pci/pcivar.h>
66#include <bus/pci/pcireg.h>
67#else
68#include <dev/pci/pcireg.h>
69#include <dev/pci/pcivar.h>
70#endif
71
72#include <net/bpf.h>
73#include <net/if.h>
74#include <net/if_var.h>
75#include <net/if_arp.h>
76#include <net/ethernet.h>
77#include <net/if_dl.h>
78#include <net/if_media.h>
79#include <net/if_types.h>
80
81#if defined(__DragonFly__)
82#include <netproto/802_11/ieee80211_var.h>
83#include <netproto/802_11/ieee80211_radiotap.h>
84#include <netproto/802_11/ieee80211_input.h>
85#include <netproto/802_11/ieee80211_regdomain.h>
86#else
87#include <net80211/ieee80211_var.h>
88#include <net80211/ieee80211_radiotap.h>
89#include <net80211/ieee80211_input.h>
90#include <net80211/ieee80211_regdomain.h>
91#endif
92
93#include <netinet/in.h>
94#include <netinet/in_systm.h>
95#include <netinet/in_var.h>
96#include <netinet/ip.h>
97#include <netinet/if_ether.h>
98
99#if defined(__DragonFly__)
100#include "if_iwireg.h"
101#include "if_iwivar.h"
102#else
103#include <dev/iwi/if_iwireg.h>
104#include <dev/iwi/if_iwivar.h>
105#endif
106
107#define IWI_DEBUG
108#ifdef IWI_DEBUG
109#define DPRINTF(x) do { if (iwi_debug > 0) kprintf x; } while (0)
110#define DPRINTFN(n, x) do { if (iwi_debug >= (n)) kprintf x; } while (0)
111int iwi_debug = 0;
112SYSCTL_INT(_debug, OID_AUTO, iwi, CTLFLAG_RW, &iwi_debug, 0, "iwi debug level");
113
114static const char *iwi_fw_states[] = {
115 "IDLE", /* IWI_FW_IDLE */
116 "LOADING", /* IWI_FW_LOADING */
117 "ASSOCIATING", /* IWI_FW_ASSOCIATING */
118 "DISASSOCIATING", /* IWI_FW_DISASSOCIATING */
119 "SCANNING", /* IWI_FW_SCANNING */
120};
121#else
122#define DPRINTF(x)
123#define DPRINTFN(n, x)
124#endif
125
126MODULE_DEPEND(iwi, pci, 1, 1, 1);
127MODULE_DEPEND(iwi, wlan, 1, 1, 1);
128MODULE_DEPEND(iwi, firmware, 1, 1, 1);
129
130enum {
131 IWI_LED_TX,
132 IWI_LED_RX,
133 IWI_LED_POLL,
134};
135
136struct iwi_ident {
137 uint16_t vendor;
138 uint16_t device;
139 const char *name;
140};
141
142static const struct iwi_ident iwi_ident_table[] = {
143 { 0x8086, 0x4220, "Intel(R) PRO/Wireless 2200BG" },
144 { 0x8086, 0x4221, "Intel(R) PRO/Wireless 2225BG" },
145 { 0x8086, 0x4223, "Intel(R) PRO/Wireless 2915ABG" },
146 { 0x8086, 0x4224, "Intel(R) PRO/Wireless 2915ABG" },
147
148 { 0, 0, NULL }
149};
150
151static struct ieee80211vap *iwi_vap_create(struct ieee80211com *,
152 const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
153 const uint8_t [IEEE80211_ADDR_LEN],
154 const uint8_t [IEEE80211_ADDR_LEN]);
155static void iwi_vap_delete(struct ieee80211vap *);
156static void iwi_dma_map_addr(void *, bus_dma_segment_t *, int, int);
157static int iwi_alloc_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *,
158 int);
159static void iwi_reset_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
160static void iwi_free_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
161static int iwi_alloc_tx_ring(struct iwi_softc *, struct iwi_tx_ring *,
162 int, bus_addr_t, bus_addr_t);
163static void iwi_reset_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
164static void iwi_free_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
165static int iwi_alloc_rx_ring(struct iwi_softc *, struct iwi_rx_ring *,
166 int);
167static void iwi_reset_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
168static void iwi_free_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
169static struct ieee80211_node *iwi_node_alloc(struct ieee80211vap *,
170 const uint8_t [IEEE80211_ADDR_LEN]);
171static void iwi_node_free(struct ieee80211_node *);
172static void iwi_media_status(struct ifnet *, struct ifmediareq *);
173static int iwi_newstate(struct ieee80211vap *, enum ieee80211_state, int);
174static void iwi_wme_init(struct iwi_softc *);
175static int iwi_wme_setparams(struct iwi_softc *);
176static int iwi_wme_update(struct ieee80211com *);
177static uint16_t iwi_read_prom_word(struct iwi_softc *, uint8_t);
178static void iwi_frame_intr(struct iwi_softc *, struct iwi_rx_data *, int,
179 struct iwi_frame *);
180static void iwi_notification_intr(struct iwi_softc *, struct iwi_notif *);
181static void iwi_rx_intr(struct iwi_softc *);
182static void iwi_tx_intr(struct iwi_softc *, struct iwi_tx_ring *);
183static void iwi_intr(void *);
184static int iwi_cmd(struct iwi_softc *, uint8_t, void *, uint8_t);
185static void iwi_write_ibssnode(struct iwi_softc *, const u_int8_t [], int);
186static int iwi_tx_start(struct iwi_softc *, struct mbuf *,
187 struct ieee80211_node *, int);
188static int iwi_raw_xmit(struct ieee80211_node *, struct mbuf *,
189 const struct ieee80211_bpf_params *);
190static void iwi_start(struct iwi_softc *);
191static int iwi_transmit(struct ieee80211com *, struct mbuf *);
192static void iwi_watchdog(void *);
193static void iwi_parent(struct ieee80211com *);
194static void iwi_stop_master(struct iwi_softc *);
195static int iwi_reset(struct iwi_softc *);
196static int iwi_load_ucode(struct iwi_softc *, const struct iwi_fw *);
197static int iwi_load_firmware(struct iwi_softc *, const struct iwi_fw *);
198static void iwi_release_fw_dma(struct iwi_softc *sc);
199static int iwi_config(struct iwi_softc *);
200static int iwi_get_firmware(struct iwi_softc *, enum ieee80211_opmode);
201static void iwi_put_firmware(struct iwi_softc *);
202static void iwi_monitor_scan(void *, int);
203static int iwi_scanchan(struct iwi_softc *, unsigned long, int);
204static void iwi_scan_start(struct ieee80211com *);
205static void iwi_scan_end(struct ieee80211com *);
206static void iwi_set_channel(struct ieee80211com *);
207static void iwi_scan_curchan(struct ieee80211_scan_state *, unsigned long maxdwell);
208static void iwi_scan_mindwell(struct ieee80211_scan_state *);
209static int iwi_auth_and_assoc(struct iwi_softc *, struct ieee80211vap *);
210static void iwi_disassoc(void *, int);
211static int iwi_disassociate(struct iwi_softc *, int quiet);
212static void iwi_init_locked(struct iwi_softc *);
213static void iwi_init(void *);
214static int iwi_init_fw_dma(struct iwi_softc *, int);
215static void iwi_stop_locked(void *);
216static void iwi_stop(struct iwi_softc *);
217static void iwi_restart(void *, int);
218static int iwi_getrfkill(struct iwi_softc *);
219static void iwi_radio_on(void *, int);
220static void iwi_radio_off(void *, int);
221static void iwi_sysctlattach(struct iwi_softc *);
222static void iwi_led_event(struct iwi_softc *, int);
223static void iwi_ledattach(struct iwi_softc *);
224
225static int iwi_probe(device_t);
226static int iwi_attach(device_t);
227static int iwi_detach(device_t);
228static int iwi_shutdown(device_t);
229static int iwi_suspend(device_t);
230static int iwi_resume(device_t);
231
232static device_method_t iwi_methods[] = {
233 /* Device interface */
234 DEVMETHOD(device_probe, iwi_probe),
235 DEVMETHOD(device_attach, iwi_attach),
236 DEVMETHOD(device_detach, iwi_detach),
237 DEVMETHOD(device_shutdown, iwi_shutdown),
238 DEVMETHOD(device_suspend, iwi_suspend),
239 DEVMETHOD(device_resume, iwi_resume),
240
241 DEVMETHOD_END
242};
243
244static driver_t iwi_driver = {
245 "iwi",
246 iwi_methods,
247 sizeof (struct iwi_softc)
248};
249
250static devclass_t iwi_devclass;
251
252DRIVER_MODULE(iwi, pci, iwi_driver, iwi_devclass, NULL, NULL);
253
254MODULE_VERSION(iwi, 1);
255
256static __inline uint8_t
257MEM_READ_1(struct iwi_softc *sc, uint32_t addr)
258{
259 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
260 return CSR_READ_1(sc, IWI_CSR_INDIRECT_DATA);
261}
262
263static __inline uint32_t
264MEM_READ_4(struct iwi_softc *sc, uint32_t addr)
265{
266 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
267 return CSR_READ_4(sc, IWI_CSR_INDIRECT_DATA);
268}
269
270static int
271iwi_probe(device_t dev)
272{
273 const struct iwi_ident *ident;
274
275 for (ident = iwi_ident_table; ident->name != NULL; ident++) {
276 if (pci_get_vendor(dev) == ident->vendor &&
277 pci_get_device(dev) == ident->device) {
278 device_set_desc(dev, ident->name);
279 return (BUS_PROBE_DEFAULT);
280 }
281 }
282 return ENXIO;
283}
284
285static int
286iwi_attach(device_t dev)
287{
288 struct iwi_softc *sc = device_get_softc(dev);
289 struct ieee80211com *ic = &sc->sc_ic;
290 uint16_t val;
291 uint8_t bands[IEEE80211_MODE_BYTES];
292 int i, error;
293
294 sc->sc_dev = dev;
295
296 IWI_LOCK_INIT(sc);
297 mbufq_init(&sc->sc_snd, ifqmaxlen);
298
299#if defined(__DragonFly__)
300 devfs_clone_bitmap_init(&sc->sc_unr);
301#else
302 sc->sc_unr = new_unrhdr(1, IWI_MAX_IBSSNODE-1, &sc->sc_mtx);
303#endif
304
305 TASK_INIT(&sc->sc_radiontask, 0, iwi_radio_on, sc);
306 TASK_INIT(&sc->sc_radiofftask, 0, iwi_radio_off, sc);
307 TASK_INIT(&sc->sc_restarttask, 0, iwi_restart, sc);
308 TASK_INIT(&sc->sc_disassoctask, 0, iwi_disassoc, sc);
309 TASK_INIT(&sc->sc_monitortask, 0, iwi_monitor_scan, sc);
310
311#if defined(__DragonFly__)
312 callout_init_lk(&sc->sc_wdtimer, &sc->sc_lock);
313 callout_init_lk(&sc->sc_rftimer, &sc->sc_lock);
314#else
315 callout_init_mtx(&sc->sc_wdtimer, &sc->sc_mtx, 0);
316 callout_init_mtx(&sc->sc_rftimer, &sc->sc_mtx, 0);
317#endif
318
319 pci_write_config(dev, 0x41, 0, 1);
320
321 /* enable bus-mastering */
322 pci_enable_busmaster(dev);
323
324 i = PCIR_BAR(0);
325 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &i, RF_ACTIVE);
326 if (sc->mem == NULL) {
327 device_printf(dev, "could not allocate memory resource\n");
328 goto fail;
329 }
330
331 sc->sc_st = rman_get_bustag(sc->mem);
332 sc->sc_sh = rman_get_bushandle(sc->mem);
333
334 i = 0;
335 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i,
336 RF_ACTIVE | RF_SHAREABLE);
337 if (sc->irq == NULL) {
338 device_printf(dev, "could not allocate interrupt resource\n");
339 goto fail;
340 }
341
342 if (iwi_reset(sc) != 0) {
343 device_printf(dev, "could not reset adapter\n");
344 goto fail;
345 }
346
347 /*
348 * Allocate rings.
349 */
350 if (iwi_alloc_cmd_ring(sc, &sc->cmdq, IWI_CMD_RING_COUNT) != 0) {
351 device_printf(dev, "could not allocate Cmd ring\n");
352 goto fail;
353 }
354
355 for (i = 0; i < 4; i++) {
356 error = iwi_alloc_tx_ring(sc, &sc->txq[i], IWI_TX_RING_COUNT,
357 IWI_CSR_TX1_RIDX + i * 4,
358 IWI_CSR_TX1_WIDX + i * 4);
359 if (error != 0) {
360 device_printf(dev, "could not allocate Tx ring %d\n",
361 i+i);
362 goto fail;
363 }
364 }
365
366 if (iwi_alloc_rx_ring(sc, &sc->rxq, IWI_RX_RING_COUNT) != 0) {
367 device_printf(dev, "could not allocate Rx ring\n");
368 goto fail;
369 }
370
371 iwi_wme_init(sc);
372
373 ic->ic_softc = sc;
374 ic->ic_name = device_get_nameunit(dev);
375 ic->ic_opmode = IEEE80211_M_STA;
376 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
377
378 /* set device capabilities */
379 ic->ic_caps =
380 IEEE80211_C_STA /* station mode supported */
381 | IEEE80211_C_IBSS /* IBSS mode supported */
382 | IEEE80211_C_MONITOR /* monitor mode supported */
383 | IEEE80211_C_PMGT /* power save supported */
384 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
385 | IEEE80211_C_WPA /* 802.11i */
386 | IEEE80211_C_WME /* 802.11e */
387#if 0
388 | IEEE80211_C_BGSCAN /* capable of bg scanning */
389#endif
390 ;
391
392 /* read MAC address from EEPROM */
393 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 0);
394 ic->ic_macaddr[0] = val & 0xff;
395 ic->ic_macaddr[1] = val >> 8;
396 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 1);
397 ic->ic_macaddr[2] = val & 0xff;
398 ic->ic_macaddr[3] = val >> 8;
399 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 2);
400 ic->ic_macaddr[4] = val & 0xff;
401 ic->ic_macaddr[5] = val >> 8;
402
403 memset(bands, 0, sizeof(bands));
404 setbit(bands, IEEE80211_MODE_11B);
405 setbit(bands, IEEE80211_MODE_11G);
406 if (pci_get_device(dev) >= 0x4223)
407 setbit(bands, IEEE80211_MODE_11A);
408 ieee80211_init_channels(ic, NULL, bands);
409
410 ieee80211_ifattach(ic);
411 /* override default methods */
412 ic->ic_node_alloc = iwi_node_alloc;
413 sc->sc_node_free = ic->ic_node_free;
414 ic->ic_node_free = iwi_node_free;
415 ic->ic_raw_xmit = iwi_raw_xmit;
416 ic->ic_scan_start = iwi_scan_start;
417 ic->ic_scan_end = iwi_scan_end;
418 ic->ic_set_channel = iwi_set_channel;
419 ic->ic_scan_curchan = iwi_scan_curchan;
420 ic->ic_scan_mindwell = iwi_scan_mindwell;
421 ic->ic_wme.wme_update = iwi_wme_update;
422
423 ic->ic_vap_create = iwi_vap_create;
424 ic->ic_vap_delete = iwi_vap_delete;
425 ic->ic_transmit = iwi_transmit;
426 ic->ic_parent = iwi_parent;
427
428 ieee80211_radiotap_attach(ic,
429 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
430 IWI_TX_RADIOTAP_PRESENT,
431 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
432 IWI_RX_RADIOTAP_PRESENT);
433
434 iwi_sysctlattach(sc);
435 iwi_ledattach(sc);
436
437 /*
438 * Hook our interrupt after all initialization is complete.
439 */
440#if defined(__DragonFly__)
441 error = bus_setup_intr(dev, sc->irq, INTR_MPSAFE,
442 iwi_intr, sc, &sc->sc_ih, &wlan_global_serializer);
443#else
444 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
445 NULL, iwi_intr, sc, &sc->sc_ih);
446#endif
447 if (error != 0) {
448 device_printf(dev, "could not set up interrupt\n");
449 goto fail;
450 }
451
452 if (bootverbose)
453 ieee80211_announce(ic);
454
455 return 0;
456fail:
457 /* XXX fix */
458 iwi_detach(dev);
459 return ENXIO;
460}
461
462static int
463iwi_detach(device_t dev)
464{
465 struct iwi_softc *sc = device_get_softc(dev);
466 struct ieee80211com *ic = &sc->sc_ic;
467
468 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
469
470 /* NB: do early to drain any pending tasks */
471 ieee80211_draintask(ic, &sc->sc_radiontask);
472 ieee80211_draintask(ic, &sc->sc_radiofftask);
473 ieee80211_draintask(ic, &sc->sc_restarttask);
474 ieee80211_draintask(ic, &sc->sc_disassoctask);
475 ieee80211_draintask(ic, &sc->sc_monitortask);
476
477 iwi_stop(sc);
478
479 ieee80211_ifdetach(ic);
480
481 iwi_put_firmware(sc);
482 iwi_release_fw_dma(sc);
483
484 iwi_free_cmd_ring(sc, &sc->cmdq);
485 iwi_free_tx_ring(sc, &sc->txq[0]);
486 iwi_free_tx_ring(sc, &sc->txq[1]);
487 iwi_free_tx_ring(sc, &sc->txq[2]);
488 iwi_free_tx_ring(sc, &sc->txq[3]);
489 iwi_free_rx_ring(sc, &sc->rxq);
490
491 bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq), sc->irq);
492
493 bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->mem),
494 sc->mem);
495
496#if defined(__DragonFly__)
497 devfs_clone_bitmap_uninit(&sc->sc_unr);
498#else
499 delete_unrhdr(sc->sc_unr);
500#endif
501 mbufq_drain(&sc->sc_snd);
502
503 IWI_LOCK_DESTROY(sc);
504
505 return 0;
506}
507
508static struct ieee80211vap *
509iwi_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
510 enum ieee80211_opmode opmode, int flags,
511 const uint8_t bssid[IEEE80211_ADDR_LEN],
512 const uint8_t mac[IEEE80211_ADDR_LEN])
513{
514 struct iwi_softc *sc = ic->ic_softc;
515 struct iwi_vap *ivp;
516 struct ieee80211vap *vap;
517 int i;
518
519 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
520 return NULL;
521 /*
522 * Get firmware image (and possibly dma memory) on mode change.
523 */
524 if (iwi_get_firmware(sc, opmode))
525 return NULL;
526 /* allocate DMA memory for mapping firmware image */
527 i = sc->fw_fw.size;
528 if (sc->fw_boot.size > i)
529 i = sc->fw_boot.size;
530 /* XXX do we dma the ucode as well ? */
531 if (sc->fw_uc.size > i)
532 i = sc->fw_uc.size;
533 if (iwi_init_fw_dma(sc, i))
534 return NULL;
535
536 ivp = kmalloc(sizeof(struct iwi_vap), M_80211_VAP, M_WAITOK | M_ZERO);
537 vap = &ivp->iwi_vap;
538 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
539 /* override the default, the setting comes from the linux driver */
540 vap->iv_bmissthreshold = 24;
541 /* override with driver methods */
542 ivp->iwi_newstate = vap->iv_newstate;
543 vap->iv_newstate = iwi_newstate;
544
545 /* complete setup */
546 ieee80211_vap_attach(vap, ieee80211_media_change, iwi_media_status,
547 mac);
548 ic->ic_opmode = opmode;
549 return vap;
550}
551
552static void
553iwi_vap_delete(struct ieee80211vap *vap)
554{
555 struct iwi_vap *ivp = IWI_VAP(vap);
556
557 ieee80211_vap_detach(vap);
558 kfree(ivp, M_80211_VAP);
559}
560
561static void
562iwi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
563{
564 if (error != 0)
565 return;
566
567 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
568
569 *(bus_addr_t *)arg = segs[0].ds_addr;
570}
571
572static int
573iwi_alloc_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring, int count)
574{
575 int error;
576
577 ring->count = count;
578 ring->queued = 0;
579 ring->cur = ring->next = 0;
580
581#if defined(__DragonFly__)
582 error = bus_dma_tag_create(NULL, 4, 0,
583 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
584 count * IWI_CMD_DESC_SIZE, 1, count * IWI_CMD_DESC_SIZE,
585 0 , &ring->desc_dmat);
586#else
587 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
588 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
589 count * IWI_CMD_DESC_SIZE, 1, count * IWI_CMD_DESC_SIZE, 0,
590 NULL, NULL, &ring->desc_dmat);
591#endif
592 if (error != 0) {
593 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
594 goto fail;
595 }
596
597 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
598 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map);
599 if (error != 0) {
600 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
601 goto fail;
602 }
603
604 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
605 count * IWI_CMD_DESC_SIZE, iwi_dma_map_addr, &ring->physaddr, 0);
606 if (error != 0) {
607 device_printf(sc->sc_dev, "could not load desc DMA map\n");
608 goto fail;
609 }
610
611 return 0;
612
613fail: iwi_free_cmd_ring(sc, ring);
614 return error;
615}
616
617static void
618iwi_reset_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
619{
620 ring->queued = 0;
621 ring->cur = ring->next = 0;
622}
623
624static void
625iwi_free_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
626{
627 if (ring->desc != NULL) {
628 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
629 BUS_DMASYNC_POSTWRITE);
630 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
631 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
632 }
633
634 if (ring->desc_dmat != NULL)
635 bus_dma_tag_destroy(ring->desc_dmat);
636}
637
638static int
639iwi_alloc_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring, int count,
640 bus_addr_t csr_ridx, bus_addr_t csr_widx)
641{
642 int i, error;
643
644 ring->count = count;
645 ring->queued = 0;
646 ring->cur = ring->next = 0;
647 ring->csr_ridx = csr_ridx;
648 ring->csr_widx = csr_widx;
649
650#if defined(__DragonFly__)
651 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
652 BUS_SPACE_MAXADDR, count * IWI_TX_DESC_SIZE, 1,
653 count * IWI_TX_DESC_SIZE, 0, &ring->desc_dmat);
654#else
655 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
656 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
657 count * IWI_TX_DESC_SIZE, 1, count * IWI_TX_DESC_SIZE, 0, NULL,
658 NULL, &ring->desc_dmat);
659#endif
660 if (error != 0) {
661 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
662 goto fail;
663 }
664
665 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
666 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map);
667 if (error != 0) {
668 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
669 goto fail;
670 }
671
672 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
673 count * IWI_TX_DESC_SIZE, iwi_dma_map_addr, &ring->physaddr, 0);
674 if (error != 0) {
675 device_printf(sc->sc_dev, "could not load desc DMA map\n");
676 goto fail;
677 }
678
679 ring->data = kmalloc(count * sizeof (struct iwi_tx_data), M_DEVBUF,
680 M_WAITOK | M_ZERO);
681 if (ring->data == NULL) {
682 device_printf(sc->sc_dev, "could not allocate soft data\n");
683 error = ENOMEM;
684 goto fail;
685 }
686
687#if defined(__DragonFly__)
688 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
689 BUS_SPACE_MAXADDR, MCLBYTES, IWI_MAX_NSEG,
690 MCLBYTES, 0, &ring->data_dmat);
691#else
692 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
693 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
694 IWI_MAX_NSEG, MCLBYTES, 0, NULL, NULL, &ring->data_dmat);
695#endif
696 if (error != 0) {
697 device_printf(sc->sc_dev, "could not create data DMA tag\n");
698 goto fail;
699 }
700
701 for (i = 0; i < count; i++) {
702 error = bus_dmamap_create(ring->data_dmat, 0,
703 &ring->data[i].map);
704 if (error != 0) {
705 device_printf(sc->sc_dev, "could not create DMA map\n");
706 goto fail;
707 }
708 }
709
710 return 0;
711
712fail: iwi_free_tx_ring(sc, ring);
713 return error;
714}
715
716static void
717iwi_reset_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
718{
719 struct iwi_tx_data *data;
720 int i;
721
722 for (i = 0; i < ring->count; i++) {
723 data = &ring->data[i];
724
725 if (data->m != NULL) {
726 bus_dmamap_sync(ring->data_dmat, data->map,
727 BUS_DMASYNC_POSTWRITE);
728 bus_dmamap_unload(ring->data_dmat, data->map);
729 m_freem(data->m);
730 data->m = NULL;
731 }
732
733 if (data->ni != NULL) {
734 ieee80211_free_node(data->ni);
735 data->ni = NULL;
736 }
737 }
738
739 ring->queued = 0;
740 ring->cur = ring->next = 0;
741}
742
743static void
744iwi_free_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
745{
746 struct iwi_tx_data *data;
747 int i;
748
749 if (ring->desc != NULL) {
750 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
751 BUS_DMASYNC_POSTWRITE);
752 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
753 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
754 }
755
756 if (ring->desc_dmat != NULL)
757 bus_dma_tag_destroy(ring->desc_dmat);
758
759 if (ring->data != NULL) {
760 for (i = 0; i < ring->count; i++) {
761 data = &ring->data[i];
762
763 if (data->m != NULL) {
764 bus_dmamap_sync(ring->data_dmat, data->map,
765 BUS_DMASYNC_POSTWRITE);
766 bus_dmamap_unload(ring->data_dmat, data->map);
767 m_freem(data->m);
768 }
769
770 if (data->ni != NULL)
771 ieee80211_free_node(data->ni);
772
773 if (data->map != NULL)
774 bus_dmamap_destroy(ring->data_dmat, data->map);
775 }
776
777 kfree(ring->data, M_DEVBUF);
778 }
779
780 if (ring->data_dmat != NULL)
781 bus_dma_tag_destroy(ring->data_dmat);
782}
783
784static int
785iwi_alloc_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring, int count)
786{
787 struct iwi_rx_data *data;
788 int i, error;
789
790 ring->count = count;
791 ring->cur = 0;
792
793 ring->data = kmalloc(count * sizeof (struct iwi_rx_data), M_DEVBUF,
794 M_WAITOK | M_ZERO);
795 if (ring->data == NULL) {
796 device_printf(sc->sc_dev, "could not allocate soft data\n");
797 error = ENOMEM;
798 goto fail;
799 }
800
801#if defined(__DragonFly__)
802 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
803 BUS_SPACE_MAXADDR, MCLBYTES, 1, MCLBYTES,
804 0, &ring->data_dmat);
805#else
806 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
807 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
808 1, MCLBYTES, 0, NULL, NULL, &ring->data_dmat);
809#endif
810 if (error != 0) {
811 device_printf(sc->sc_dev, "could not create data DMA tag\n");
812 goto fail;
813 }
814
815 for (i = 0; i < count; i++) {
816 data = &ring->data[i];
817
818 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
819 if (error != 0) {
820 device_printf(sc->sc_dev, "could not create DMA map\n");
821 goto fail;
822 }
823
824 data->m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
825 if (data->m == NULL) {
826 device_printf(sc->sc_dev,
827 "could not allocate rx mbuf\n");
828 error = ENOMEM;
829 goto fail;
830 }
831
832 error = bus_dmamap_load(ring->data_dmat, data->map,
833 mtod(data->m, void *), MCLBYTES, iwi_dma_map_addr,
834 &data->physaddr, 0);
835 if (error != 0) {
836 device_printf(sc->sc_dev,
837 "could not load rx buf DMA map");
838 goto fail;
839 }
840
841 data->reg = IWI_CSR_RX_BASE + i * 4;
842 }
843
844 return 0;
845
846fail: iwi_free_rx_ring(sc, ring);
847 return error;
848}
849
850static void
851iwi_reset_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
852{
853 ring->cur = 0;
854}
855
856static void
857iwi_free_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
858{
859 struct iwi_rx_data *data;
860 int i;
861
862 if (ring->data != NULL) {
863 for (i = 0; i < ring->count; i++) {
864 data = &ring->data[i];
865
866 if (data->m != NULL) {
867 bus_dmamap_sync(ring->data_dmat, data->map,
868 BUS_DMASYNC_POSTREAD);
869 bus_dmamap_unload(ring->data_dmat, data->map);
870 m_freem(data->m);
871 }
872
873 if (data->map != NULL)
874 bus_dmamap_destroy(ring->data_dmat, data->map);
875 }
876
877 kfree(ring->data, M_DEVBUF);
878 }
879
880 if (ring->data_dmat != NULL)
881 bus_dma_tag_destroy(ring->data_dmat);
882}
883
884static int
885iwi_shutdown(device_t dev)
886{
887 struct iwi_softc *sc = device_get_softc(dev);
888
889 iwi_stop(sc);
890 iwi_put_firmware(sc); /* ??? XXX */
891
892 return 0;
893}
894
895static int
896iwi_suspend(device_t dev)
897{
898 struct iwi_softc *sc = device_get_softc(dev);
899 struct ieee80211com *ic = &sc->sc_ic;
900
901 ieee80211_suspend_all(ic);
902 return 0;
903}
904
905static int
906iwi_resume(device_t dev)
907{
908 struct iwi_softc *sc = device_get_softc(dev);
909 struct ieee80211com *ic = &sc->sc_ic;
910
911 pci_write_config(dev, 0x41, 0, 1);
912
913 ieee80211_resume_all(ic);
914 return 0;
915}
916
917static struct ieee80211_node *
918iwi_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
919{
920 struct iwi_node *in;
921
922 in = kmalloc(sizeof (struct iwi_node), M_80211_NODE, M_INTWAIT | M_ZERO);
923 if (in == NULL)
924 return NULL;
925 /* XXX assign sta table entry for adhoc */
926 in->in_station = -1;
927
928 return &in->in_node;
929}
930
931static void
932iwi_node_free(struct ieee80211_node *ni)
933{
934 struct ieee80211com *ic = ni->ni_ic;
935 struct iwi_softc *sc = ic->ic_softc;
936 struct iwi_node *in = (struct iwi_node *)ni;
937
938 if (in->in_station != -1) {
939#if defined(__DragonFly__)
940 DPRINTF(("%s mac %s station %u\n", __func__,
941 ether_sprintf(ni->ni_macaddr), in->in_station));
942#else
943 DPRINTF(("%s mac %6D station %u\n", __func__,
944 ni->ni_macaddr, ":", in->in_station));
945#endif
946#if defined(__DragonFly__)
947 devfs_clone_bitmap_put(&sc->sc_unr, in->in_station);
948#else
949 free_unr(sc->sc_unr, in->in_station);
950#endif
951 }
952
953 sc->sc_node_free(ni);
954}
955
956/*
957 * Convert h/w rate code to IEEE rate code.
958 */
959static int
960iwi_cvtrate(int iwirate)
961{
962 switch (iwirate) {
963 case IWI_RATE_DS1: return 2;
964 case IWI_RATE_DS2: return 4;
965 case IWI_RATE_DS5: return 11;
966 case IWI_RATE_DS11: return 22;
967 case IWI_RATE_OFDM6: return 12;
968 case IWI_RATE_OFDM9: return 18;
969 case IWI_RATE_OFDM12: return 24;
970 case IWI_RATE_OFDM18: return 36;
971 case IWI_RATE_OFDM24: return 48;
972 case IWI_RATE_OFDM36: return 72;
973 case IWI_RATE_OFDM48: return 96;
974 case IWI_RATE_OFDM54: return 108;
975 }
976 return 0;
977}
978
979/*
980 * The firmware automatically adapts the transmit speed. We report its current
981 * value here.
982 */
983static void
984iwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
985{
986 struct ieee80211vap *vap = ifp->if_softc;
987 struct ieee80211com *ic = vap->iv_ic;
988 struct iwi_softc *sc = ic->ic_softc;
989 struct ieee80211_node *ni;
990
991 /* read current transmission rate from adapter */
992 ni = ieee80211_ref_node(vap->iv_bss);
993 ni->ni_txrate =
994 iwi_cvtrate(CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE));
995 ieee80211_free_node(ni);
996 ieee80211_media_status(ifp, imr);
997}
998
999static int
1000iwi_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1001{
1002 struct iwi_vap *ivp = IWI_VAP(vap);
1003 struct ieee80211com *ic = vap->iv_ic;
1004 struct iwi_softc *sc = ic->ic_softc;
1005 IWI_LOCK_DECL;
1006
1007 DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
1008 ieee80211_state_name[vap->iv_state],
1009 ieee80211_state_name[nstate], sc->flags));
1010
1011 IEEE80211_UNLOCK(ic);
1012 IWI_LOCK(sc);
1013 switch (nstate) {
1014 case IEEE80211_S_INIT:
1015 /*
1016 * NB: don't try to do this if iwi_stop_master has
1017 * shutdown the firmware and disabled interrupts.
1018 */
1019 if (vap->iv_state == IEEE80211_S_RUN &&
1020 (sc->flags & IWI_FLAG_FW_INITED))
1021 iwi_disassociate(sc, 0);
1022 break;
1023 case IEEE80211_S_AUTH:
1024 iwi_auth_and_assoc(sc, vap);
1025 break;
1026 case IEEE80211_S_RUN:
1027 if (vap->iv_opmode == IEEE80211_M_IBSS &&
1028 vap->iv_state == IEEE80211_S_SCAN) {
1029 /*
1030 * XXX when joining an ibss network we are called
1031 * with a SCAN -> RUN transition on scan complete.
1032 * Use that to call iwi_auth_and_assoc. On completing
1033 * the join we are then called again with an
1034 * AUTH -> RUN transition and we want to do nothing.
1035 * This is all totally bogus and needs to be redone.
1036 */
1037 iwi_auth_and_assoc(sc, vap);
1038 } else if (vap->iv_opmode == IEEE80211_M_MONITOR)
1039 ieee80211_runtask(ic, &sc->sc_monitortask);
1040 break;
1041 case IEEE80211_S_ASSOC:
1042 /*
1043 * If we are transitioning from AUTH then just wait
1044 * for the ASSOC status to come back from the firmware.
1045 * Otherwise we need to issue the association request.
1046 */
1047 if (vap->iv_state == IEEE80211_S_AUTH)
1048 break;
1049 iwi_auth_and_assoc(sc, vap);
1050 break;
1051 default:
1052 break;
1053 }
1054 IWI_UNLOCK(sc);
1055 IEEE80211_LOCK(ic);
1056 return ivp->iwi_newstate(vap, nstate, arg);
1057}
1058
1059/*
1060 * WME parameters coming from IEEE 802.11e specification. These values are
1061 * already declared in ieee80211_proto.c, but they are static so they can't
1062 * be reused here.
1063 */
1064static const struct wmeParams iwi_wme_cck_params[WME_NUM_AC] = {
1065 { 0, 3, 5, 7, 0 }, /* WME_AC_BE */
1066 { 0, 3, 5, 10, 0 }, /* WME_AC_BK */
1067 { 0, 2, 4, 5, 188 }, /* WME_AC_VI */
1068 { 0, 2, 3, 4, 102 } /* WME_AC_VO */
1069};
1070
1071static const struct wmeParams iwi_wme_ofdm_params[WME_NUM_AC] = {
1072 { 0, 3, 4, 6, 0 }, /* WME_AC_BE */
1073 { 0, 3, 4, 10, 0 }, /* WME_AC_BK */
1074 { 0, 2, 3, 4, 94 }, /* WME_AC_VI */
1075 { 0, 2, 2, 3, 47 } /* WME_AC_VO */
1076};
1077#define IWI_EXP2(v) htole16((1 << (v)) - 1)
1078#define IWI_USEC(v) htole16(IEEE80211_TXOP_TO_US(v))
1079
1080static void
1081iwi_wme_init(struct iwi_softc *sc)
1082{
1083 const struct wmeParams *wmep;
1084 int ac;
1085
1086 memset(sc->wme, 0, sizeof sc->wme);
1087 for (ac = 0; ac < WME_NUM_AC; ac++) {
1088 /* set WME values for CCK modulation */
1089 wmep = &iwi_wme_cck_params[ac];
1090 sc->wme[1].aifsn[ac] = wmep->wmep_aifsn;
1091 sc->wme[1].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1092 sc->wme[1].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1093 sc->wme[1].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1094 sc->wme[1].acm[ac] = wmep->wmep_acm;
1095
1096 /* set WME values for OFDM modulation */
1097 wmep = &iwi_wme_ofdm_params[ac];
1098 sc->wme[2].aifsn[ac] = wmep->wmep_aifsn;
1099 sc->wme[2].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1100 sc->wme[2].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1101 sc->wme[2].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1102 sc->wme[2].acm[ac] = wmep->wmep_acm;
1103 }
1104}
1105
1106static int
1107iwi_wme_setparams(struct iwi_softc *sc)
1108{
1109 struct ieee80211com *ic = &sc->sc_ic;
1110 const struct wmeParams *wmep;
1111 int ac;
1112
1113 for (ac = 0; ac < WME_NUM_AC; ac++) {
1114 /* set WME values for current operating mode */
1115 wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
1116 sc->wme[0].aifsn[ac] = wmep->wmep_aifsn;
1117 sc->wme[0].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1118 sc->wme[0].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1119 sc->wme[0].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1120 sc->wme[0].acm[ac] = wmep->wmep_acm;
1121 }
1122
1123 DPRINTF(("Setting WME parameters\n"));
1124 return iwi_cmd(sc, IWI_CMD_SET_WME_PARAMS, sc->wme, sizeof sc->wme);
1125}
1126#undef IWI_USEC
1127#undef IWI_EXP2
1128
1129static int
1130iwi_wme_update(struct ieee80211com *ic)
1131{
1132 struct iwi_softc *sc = ic->ic_softc;
1133 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1134 IWI_LOCK_DECL;
1135
1136 /*
1137 * We may be called to update the WME parameters in
1138 * the adapter at various places. If we're already
1139 * associated then initiate the request immediately;
1140 * otherwise we assume the params will get sent down
1141 * to the adapter as part of the work iwi_auth_and_assoc
1142 * does.
1143 */
1144 if (vap->iv_state == IEEE80211_S_RUN) {
1145 IWI_LOCK(sc);
1146 iwi_wme_setparams(sc);
1147 IWI_UNLOCK(sc);
1148 }
1149 return (0);
1150}
1151
1152static int
1153iwi_wme_setie(struct iwi_softc *sc)
1154{
1155 struct ieee80211_wme_info wme;
1156
1157 memset(&wme, 0, sizeof wme);
1158 wme.wme_id = IEEE80211_ELEMID_VENDOR;
1159 wme.wme_len = sizeof (struct ieee80211_wme_info) - 2;
1160 wme.wme_oui[0] = 0x00;
1161 wme.wme_oui[1] = 0x50;
1162 wme.wme_oui[2] = 0xf2;
1163 wme.wme_type = WME_OUI_TYPE;
1164 wme.wme_subtype = WME_INFO_OUI_SUBTYPE;
1165 wme.wme_version = WME_VERSION;
1166 wme.wme_info = 0;
1167
1168 DPRINTF(("Setting WME IE (len=%u)\n", wme.wme_len));
1169 return iwi_cmd(sc, IWI_CMD_SET_WMEIE, &wme, sizeof wme);
1170}
1171
1172/*
1173 * Read 16 bits at address 'addr' from the serial EEPROM.
1174 */
1175static uint16_t
1176iwi_read_prom_word(struct iwi_softc *sc, uint8_t addr)
1177{
1178 uint32_t tmp;
1179 uint16_t val;
1180 int n;
1181
1182 /* clock C once before the first command */
1183 IWI_EEPROM_CTL(sc, 0);
1184 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1185 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1186 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1187
1188 /* write start bit (1) */
1189 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1190 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1191
1192 /* write READ opcode (10) */
1193 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1194 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1195 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1196 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1197
1198 /* write address A7-A0 */
1199 for (n = 7; n >= 0; n--) {
1200 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1201 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D));
1202 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1203 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D) | IWI_EEPROM_C);
1204 }
1205
1206 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1207
1208 /* read data Q15-Q0 */
1209 val = 0;
1210 for (n = 15; n >= 0; n--) {
1211 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1212 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1213 tmp = MEM_READ_4(sc, IWI_MEM_EEPROM_CTL);
1214 val |= ((tmp & IWI_EEPROM_Q) >> IWI_EEPROM_SHIFT_Q) << n;
1215 }
1216
1217 IWI_EEPROM_CTL(sc, 0);
1218
1219 /* clear Chip Select and clock C */
1220 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1221 IWI_EEPROM_CTL(sc, 0);
1222 IWI_EEPROM_CTL(sc, IWI_EEPROM_C);
1223
1224 return val;
1225}
1226
1227static void
1228iwi_setcurchan(struct iwi_softc *sc, int chan)
1229{
1230 struct ieee80211com *ic = &sc->sc_ic;
1231
1232 sc->curchan = chan;
1233 ieee80211_radiotap_chan_change(ic);
1234}
1235
1236static void
1237iwi_frame_intr(struct iwi_softc *sc, struct iwi_rx_data *data, int i,
1238 struct iwi_frame *frame)
1239{
1240 struct ieee80211com *ic = &sc->sc_ic;
1241 struct mbuf *mnew, *m;
1242 struct ieee80211_node *ni;
1243 int type, error, framelen;
1244 int8_t rssi, nf;
1245 IWI_LOCK_DECL;
1246
1247 framelen = le16toh(frame->len);
1248 if (framelen < IEEE80211_MIN_LEN || framelen > MCLBYTES) {
1249 /*
1250 * XXX >MCLBYTES is bogus as it means the h/w dma'd
1251 * out of bounds; need to figure out how to limit
1252 * frame size in the firmware
1253 */
1254 /* XXX stat */
1255 DPRINTFN(1,
1256 ("drop rx frame len=%u chan=%u rssi=%u rssi_dbm=%u\n",
1257 le16toh(frame->len), frame->chan, frame->rssi,
1258 frame->rssi_dbm));
1259 return;
1260 }
1261
1262 DPRINTFN(5, ("received frame len=%u chan=%u rssi=%u rssi_dbm=%u\n",
1263 le16toh(frame->len), frame->chan, frame->rssi, frame->rssi_dbm));
1264
1265 if (frame->chan != sc->curchan)
1266 iwi_setcurchan(sc, frame->chan);
1267
1268 /*
1269 * Try to allocate a new mbuf for this ring element and load it before
1270 * processing the current mbuf. If the ring element cannot be loaded,
1271 * drop the received packet and reuse the old mbuf. In the unlikely
1272 * case that the old mbuf can't be reloaded either, explicitly panic.
1273 */
1274 mnew = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1275 if (mnew == NULL) {
1276#if defined(__DragonFly__)
1277 ++ic->ic_ierrors;
1278#else
1279 counter_u64_add(ic->ic_ierrors, 1);
1280#endif
1281 return;
1282 }
1283
1284 bus_dmamap_unload(sc->rxq.data_dmat, data->map);
1285
1286 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1287 mtod(mnew, void *), MCLBYTES, iwi_dma_map_addr, &data->physaddr,
1288 0);
1289 if (error != 0) {
1290 m_freem(mnew);
1291
1292 /* try to reload the old mbuf */
1293 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1294 mtod(data->m, void *), MCLBYTES, iwi_dma_map_addr,
1295 &data->physaddr, 0);
1296 if (error != 0) {
1297 /* very unlikely that it will fail... */
1298 panic("%s: could not load old rx mbuf",
1299 device_get_name(sc->sc_dev));
1300 }
1301#if defined(__DragonFly__)
1302 ++ic->ic_ierrors;
1303#else
1304 counter_u64_add(ic->ic_ierrors, 1);
1305#endif
1306 return;
1307 }
1308
1309 /*
1310 * New mbuf successfully loaded, update Rx ring and continue
1311 * processing.
1312 */
1313 m = data->m;
1314 data->m = mnew;
1315 CSR_WRITE_4(sc, data->reg, data->physaddr);
1316
1317 /* finalize mbuf */
1318 m->m_pkthdr.len = m->m_len = sizeof (struct iwi_hdr) +
1319 sizeof (struct iwi_frame) + framelen;
1320
1321 m_adj(m, sizeof (struct iwi_hdr) + sizeof (struct iwi_frame));
1322
1323 rssi = frame->rssi_dbm;
1324 nf = -95;
1325 if (ieee80211_radiotap_active(ic)) {
1326 struct iwi_rx_radiotap_header *tap = &sc->sc_rxtap;
1327
1328 tap->wr_flags = 0;
1329 tap->wr_antsignal = rssi;
1330 tap->wr_antnoise = nf;
1331 tap->wr_rate = iwi_cvtrate(frame->rate);
1332 tap->wr_antenna = frame->antenna;
1333 }
1334 IWI_UNLOCK(sc);
1335
1336 ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1337 if (ni != NULL) {
1338 type = ieee80211_input(ni, m, rssi, nf);
1339 ieee80211_free_node(ni);
1340 } else
1341 type = ieee80211_input_all(ic, m, rssi, nf);
1342
1343 IWI_LOCK(sc);
1344 if (sc->sc_softled) {
1345 /*
1346 * Blink for any data frame. Otherwise do a
1347 * heartbeat-style blink when idle. The latter
1348 * is mainly for station mode where we depend on
1349 * periodic beacon frames to trigger the poll event.
1350 */
1351 if (type == IEEE80211_FC0_TYPE_DATA) {
1352 sc->sc_rxrate = frame->rate;
1353 iwi_led_event(sc, IWI_LED_RX);
1354 } else if (ticks - sc->sc_ledevent >= sc->sc_ledidle)
1355 iwi_led_event(sc, IWI_LED_POLL);
1356 }
1357}
1358
1359/*
1360 * Check for an association response frame to see if QoS
1361 * has been negotiated. We parse just enough to figure
1362 * out if we're supposed to use QoS. The proper solution
1363 * is to pass the frame up so ieee80211_input can do the
1364 * work but that's made hard by how things currently are
1365 * done in the driver.
1366 */
1367static void
1368iwi_checkforqos(struct ieee80211vap *vap,
1369 const struct ieee80211_frame *wh, int len)
1370{
1371#define SUBTYPE(wh) ((wh)->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
1372 const uint8_t *frm, *efrm, *wme;
1373 struct ieee80211_node *ni;
1374 uint16_t capinfo, status, associd;
1375
1376 /* NB: +8 for capinfo, status, associd, and first ie */
1377 if (!(sizeof(*wh)+8 < len && len < IEEE80211_MAX_LEN) ||
1378 SUBTYPE(wh) != IEEE80211_FC0_SUBTYPE_ASSOC_RESP)
1379 return;
1380 /*
1381 * asresp frame format
1382 * [2] capability information
1383 * [2] status
1384 * [2] association ID
1385 * [tlv] supported rates
1386 * [tlv] extended supported rates
1387 * [tlv] WME
1388 */
1389 frm = (const uint8_t *)&wh[1];
1390 efrm = ((const uint8_t *) wh) + len;
1391
1392 capinfo = le16toh(*(const uint16_t *)frm);
1393 frm += 2;
1394 status = le16toh(*(const uint16_t *)frm);
1395 frm += 2;
1396 associd = le16toh(*(const uint16_t *)frm);
1397 frm += 2;
1398
1399 wme = NULL;
1400 while (efrm - frm > 1) {
1401 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1402 switch (*frm) {
1403 case IEEE80211_ELEMID_VENDOR:
1404 if (iswmeoui(frm))
1405 wme = frm;
1406 break;
1407 }
1408 frm += frm[1] + 2;
1409 }
1410
1411 ni = ieee80211_ref_node(vap->iv_bss);
1412 ni->ni_capinfo = capinfo;
1413 ni->ni_associd = associd & 0x3fff;
1414 if (wme != NULL)
1415 ni->ni_flags |= IEEE80211_NODE_QOS;
1416 else
1417 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1418 ieee80211_free_node(ni);
1419#undef SUBTYPE
1420}
1421
1422static void
1423iwi_notif_link_quality(struct iwi_softc *sc, struct iwi_notif *notif)
1424{
1425 struct iwi_notif_link_quality *lq;
1426 int len;
1427
1428 len = le16toh(notif->len);
1429
1430 DPRINTFN(5, ("Notification (%u) - len=%d, sizeof=%zu\n",
1431 notif->type,
1432 len,
1433 sizeof(struct iwi_notif_link_quality)
1434 ));
1435
1436 /* enforce length */
1437 if (len != sizeof(struct iwi_notif_link_quality)) {
1438 DPRINTFN(5, ("Notification: (%u) too short (%d)\n",
1439 notif->type,
1440 len));
1441 return;
1442 }
1443
1444 lq = (struct iwi_notif_link_quality *)(notif + 1);
1445 memcpy(&sc->sc_linkqual, lq, sizeof(sc->sc_linkqual));
1446 sc->sc_linkqual_valid = 1;
1447}
1448
1449/*
1450 * Task queue callbacks for iwi_notification_intr used to avoid LOR's.
1451 */
1452
1453static void
1454iwi_notification_intr(struct iwi_softc *sc, struct iwi_notif *notif)
1455{
1456 struct ieee80211com *ic = &sc->sc_ic;
1457 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1458 struct iwi_notif_scan_channel *chan;
1459 struct iwi_notif_scan_complete *scan;
1460 struct iwi_notif_authentication *auth;
1461 struct iwi_notif_association *assoc;
1462 struct iwi_notif_beacon_state *beacon;
1463
1464 switch (notif->type) {
1465 case IWI_NOTIF_TYPE_SCAN_CHANNEL:
1466 chan = (struct iwi_notif_scan_channel *)(notif + 1);
1467
1468 DPRINTFN(3, ("Scan of channel %u complete (%u)\n",
1469 ieee80211_ieee2mhz(chan->nchan, 0), chan->nchan));
1470
1471 /* Reset the timer, the scan is still going */
1472 sc->sc_state_timer = 3;
1473 break;
1474
1475 case IWI_NOTIF_TYPE_SCAN_COMPLETE:
1476 scan = (struct iwi_notif_scan_complete *)(notif + 1);
1477
1478 DPRINTFN(2, ("Scan completed (%u, %u)\n", scan->nchan,
1479 scan->status));
1480
1481 IWI_STATE_END(sc, IWI_FW_SCANNING);
1482
1483 /*
1484 * Monitor mode works by doing a passive scan to set
1485 * the channel and enable rx. Because we don't want
1486 * to abort a scan lest the firmware crash we scan
1487 * for a short period of time and automatically restart
1488 * the scan when notified the sweep has completed.
1489 */
1490 if (vap->iv_opmode == IEEE80211_M_MONITOR) {
1491 ieee80211_runtask(ic, &sc->sc_monitortask);
1492 break;
1493 }
1494
1495 if (scan->status == IWI_SCAN_COMPLETED) {
1496 /* NB: don't need to defer, net80211 does it for us */
1497 ieee80211_scan_next(vap);
1498 }
1499 break;
1500
1501 case IWI_NOTIF_TYPE_AUTHENTICATION:
1502 auth = (struct iwi_notif_authentication *)(notif + 1);
1503 switch (auth->state) {
1504 case IWI_AUTH_SUCCESS:
1505 DPRINTFN(2, ("Authentication succeeeded\n"));
1506 ieee80211_new_state(vap, IEEE80211_S_ASSOC, -1);
1507 break;
1508 case IWI_AUTH_FAIL:
1509 /*
1510 * These are delivered as an unsolicited deauth
1511 * (e.g. due to inactivity) or in response to an
1512 * associate request.
1513 */
1514 sc->flags &= ~IWI_FLAG_ASSOCIATED;
1515 if (vap->iv_state != IEEE80211_S_RUN) {
1516 DPRINTFN(2, ("Authentication failed\n"));
1517 vap->iv_stats.is_rx_auth_fail++;
1518 IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
1519 } else {
1520 DPRINTFN(2, ("Deauthenticated\n"));
1521 vap->iv_stats.is_rx_deauth++;
1522 }
1523 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1524 break;
1525 case IWI_AUTH_SENT_1:
1526 case IWI_AUTH_RECV_2:
1527 case IWI_AUTH_SEQ1_PASS:
1528 break;
1529 case IWI_AUTH_SEQ1_FAIL:
1530 DPRINTFN(2, ("Initial authentication handshake failed; "
1531 "you probably need shared key\n"));
1532 vap->iv_stats.is_rx_auth_fail++;
1533 IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
1534 /* XXX retry shared key when in auto */
1535 break;
1536 default:
1537 device_printf(sc->sc_dev,
1538 "unknown authentication state %u\n", auth->state);
1539 break;
1540 }
1541 break;
1542
1543 case IWI_NOTIF_TYPE_ASSOCIATION:
1544 assoc = (struct iwi_notif_association *)(notif + 1);
1545 switch (assoc->state) {
1546 case IWI_AUTH_SUCCESS:
1547 /* re-association, do nothing */
1548 break;
1549 case IWI_ASSOC_SUCCESS:
1550 DPRINTFN(2, ("Association succeeded\n"));
1551 sc->flags |= IWI_FLAG_ASSOCIATED;
1552 IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
1553 iwi_checkforqos(vap,
1554 (const struct ieee80211_frame *)(assoc+1),
1555 le16toh(notif->len) - sizeof(*assoc) - 1);
1556 ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
1557 break;
1558 case IWI_ASSOC_INIT:
1559 sc->flags &= ~IWI_FLAG_ASSOCIATED;
1560 switch (sc->fw_state) {
1561 case IWI_FW_ASSOCIATING:
1562 DPRINTFN(2, ("Association failed\n"));
1563 IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
1564 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1565 break;
1566
1567 case IWI_FW_DISASSOCIATING:
1568 DPRINTFN(2, ("Dissassociated\n"));
1569 IWI_STATE_END(sc, IWI_FW_DISASSOCIATING);
1570 vap->iv_stats.is_rx_disassoc++;
1571 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1572 break;
1573 }
1574 break;
1575 default:
1576 device_printf(sc->sc_dev,
1577 "unknown association state %u\n", assoc->state);
1578 break;
1579 }
1580 break;
1581
1582 case IWI_NOTIF_TYPE_BEACON:
1583 /* XXX check struct length */
1584 beacon = (struct iwi_notif_beacon_state *)(notif + 1);
1585
1586 DPRINTFN(5, ("Beacon state (%u, %u)\n",
1587 beacon->state, le32toh(beacon->number)));
1588
1589 if (beacon->state == IWI_BEACON_MISS) {
1590 /*
1591 * The firmware notifies us of every beacon miss
1592 * so we need to track the count against the
1593 * configured threshold before notifying the
1594 * 802.11 layer.
1595 * XXX try to roam, drop assoc only on much higher count
1596 */
1597 if (le32toh(beacon->number) >= vap->iv_bmissthreshold) {
1598 DPRINTF(("Beacon miss: %u >= %u\n",
1599 le32toh(beacon->number),
1600 vap->iv_bmissthreshold));
1601 vap->iv_stats.is_beacon_miss++;
1602 /*
1603 * It's pointless to notify the 802.11 layer
1604 * as it'll try to send a probe request (which
1605 * we'll discard) and then timeout and drop us
1606 * into scan state. Instead tell the firmware
1607 * to disassociate and then on completion we'll
1608 * kick the state machine to scan.
1609 */
1610 ieee80211_runtask(ic, &sc->sc_disassoctask);
1611 }
1612 }
1613 break;
1614
1615 case IWI_NOTIF_TYPE_CALIBRATION:
1616 case IWI_NOTIF_TYPE_NOISE:
1617 /* XXX handle? */
1618 DPRINTFN(5, ("Notification (%u)\n", notif->type));
1619 break;
1620 case IWI_NOTIF_TYPE_LINK_QUALITY:
1621 iwi_notif_link_quality(sc, notif);
1622 break;
1623
1624 default:
1625 DPRINTF(("unknown notification type %u flags 0x%x len %u\n",
1626 notif->type, notif->flags, le16toh(notif->len)));
1627 break;
1628 }
1629}
1630
1631static void
1632iwi_rx_intr(struct iwi_softc *sc)
1633{
1634 struct iwi_rx_data *data;
1635 struct iwi_hdr *hdr;
1636 uint32_t hw;
1637
1638 hw = CSR_READ_4(sc, IWI_CSR_RX_RIDX);
1639
1640 for (; sc->rxq.cur != hw;) {
1641 data = &sc->rxq.data[sc->rxq.cur];
1642
1643 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
1644 BUS_DMASYNC_POSTREAD);
1645
1646 hdr = mtod(data->m, struct iwi_hdr *);
1647
1648 switch (hdr->type) {
1649 case IWI_HDR_TYPE_FRAME:
1650 iwi_frame_intr(sc, data, sc->rxq.cur,
1651 (struct iwi_frame *)(hdr + 1));
1652 break;
1653
1654 case IWI_HDR_TYPE_NOTIF:
1655 iwi_notification_intr(sc,
1656 (struct iwi_notif *)(hdr + 1));
1657 break;
1658
1659 default:
1660 device_printf(sc->sc_dev, "unknown hdr type %u\n",
1661 hdr->type);
1662 }
1663
1664 DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur));
1665
1666 sc->rxq.cur = (sc->rxq.cur + 1) % IWI_RX_RING_COUNT;
1667 }
1668
1669 /* tell the firmware what we have processed */
1670 hw = (hw == 0) ? IWI_RX_RING_COUNT - 1 : hw - 1;
1671 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, hw);
1672}
1673
1674static void
1675iwi_tx_intr(struct iwi_softc *sc, struct iwi_tx_ring *txq)
1676{
1677 struct iwi_tx_data *data;
1678 uint32_t hw;
1679
1680 hw = CSR_READ_4(sc, txq->csr_ridx);
1681
1682 while (txq->next != hw) {
1683 data = &txq->data[txq->next];
1684 DPRINTFN(15, ("tx done idx=%u\n", txq->next));
1685 bus_dmamap_sync(txq->data_dmat, data->map,
1686 BUS_DMASYNC_POSTWRITE);
1687 bus_dmamap_unload(txq->data_dmat, data->map);
1688 ieee80211_tx_complete(data->ni, data->m, 0);
1689 data->ni = NULL;
1690 data->m = NULL;
1691 txq->queued--;
1692 txq->next = (txq->next + 1) % IWI_TX_RING_COUNT;
1693 }
1694 sc->sc_tx_timer = 0;
1695 if (sc->sc_softled)
1696 iwi_led_event(sc, IWI_LED_TX);
1697 iwi_start(sc);
1698}
1699
1700static void
1701iwi_fatal_error_intr(struct iwi_softc *sc)
1702{
1703 struct ieee80211com *ic = &sc->sc_ic;
1704 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1705
1706 device_printf(sc->sc_dev, "firmware error\n");
1707 if (vap != NULL)
1708 ieee80211_cancel_scan(vap);
1709 ieee80211_runtask(ic, &sc->sc_restarttask);
1710
1711 sc->flags &= ~IWI_FLAG_BUSY;
1712 sc->sc_busy_timer = 0;
1713 wakeup(sc);
1714}
1715
1716static void
1717iwi_radio_off_intr(struct iwi_softc *sc)
1718{
1719
1720 ieee80211_runtask(&sc->sc_ic, &sc->sc_radiofftask);
1721}
1722
1723static void
1724iwi_intr(void *arg)
1725{
1726 struct iwi_softc *sc = arg;
1727 uint32_t r;
1728 IWI_LOCK_DECL;
1729
1730 IWI_LOCK(sc);
1731
1732 if ((r = CSR_READ_4(sc, IWI_CSR_INTR)) == 0 || r == 0xffffffff) {
1733 IWI_UNLOCK(sc);
1734 return;
1735 }
1736
1737 /* acknowledge interrupts */
1738 CSR_WRITE_4(sc, IWI_CSR_INTR, r);
1739
1740 if (r & IWI_INTR_FATAL_ERROR) {
1741 iwi_fatal_error_intr(sc);
1742 goto done;
1743 }
1744
1745 if (r & IWI_INTR_FW_INITED) {
1746 if (!(r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)))
1747 wakeup(sc);
1748 }
1749
1750 if (r & IWI_INTR_RADIO_OFF)
1751 iwi_radio_off_intr(sc);
1752
1753 if (r & IWI_INTR_CMD_DONE) {
1754 sc->flags &= ~IWI_FLAG_BUSY;
1755 sc->sc_busy_timer = 0;
1756 wakeup(sc);
1757 }
1758
1759 if (r & IWI_INTR_TX1_DONE)
1760 iwi_tx_intr(sc, &sc->txq[0]);
1761
1762 if (r & IWI_INTR_TX2_DONE)
1763 iwi_tx_intr(sc, &sc->txq[1]);
1764
1765 if (r & IWI_INTR_TX3_DONE)
1766 iwi_tx_intr(sc, &sc->txq[2]);
1767
1768 if (r & IWI_INTR_TX4_DONE)
1769 iwi_tx_intr(sc, &sc->txq[3]);
1770
1771 if (r & IWI_INTR_RX_DONE)
1772 iwi_rx_intr(sc);
1773
1774 if (r & IWI_INTR_PARITY_ERROR) {
1775 /* XXX rate-limit */
1776 device_printf(sc->sc_dev, "parity error\n");
1777 }
1778done:
1779 IWI_UNLOCK(sc);
1780}
1781
1782static int
1783iwi_cmd(struct iwi_softc *sc, uint8_t type, void *data, uint8_t len)
1784{
1785 struct iwi_cmd_desc *desc;
1786
1787 IWI_LOCK_ASSERT(sc);
1788
1789 if (sc->flags & IWI_FLAG_BUSY) {
1790 device_printf(sc->sc_dev, "%s: cmd %d not sent, busy\n",
1791 __func__, type);
1792 return EAGAIN;
1793 }
1794 sc->flags |= IWI_FLAG_BUSY;
1795 sc->sc_busy_timer = 2;
1796
1797 desc = &sc->cmdq.desc[sc->cmdq.cur];
1798
1799 desc->hdr.type = IWI_HDR_TYPE_COMMAND;
1800 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1801 desc->type = type;
1802 desc->len = len;
1803 memcpy(desc->data, data, len);
1804
1805 bus_dmamap_sync(sc->cmdq.desc_dmat, sc->cmdq.desc_map,
1806 BUS_DMASYNC_PREWRITE);
1807
1808 DPRINTFN(2, ("sending command idx=%u type=%u len=%u\n", sc->cmdq.cur,
1809 type, len));
1810
1811 sc->cmdq.cur = (sc->cmdq.cur + 1) % IWI_CMD_RING_COUNT;
1812 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
1813
1814#if defined(__DragonFly__)
1815 return lksleep(sc, &sc->sc_lock, 0, "iwicmd", hz);
1816#else
1817 return msleep(sc, &sc->sc_mtx, 0, "iwicmd", hz);
1818#endif
1819}
1820
1821static void
1822iwi_write_ibssnode(struct iwi_softc *sc,
1823 const u_int8_t addr[IEEE80211_ADDR_LEN], int entry)
1824{
1825 struct iwi_ibssnode node;
1826
1827 /* write node information into NIC memory */
1828 memset(&node, 0, sizeof node);
1829 IEEE80211_ADDR_COPY(node.bssid, addr);
1830#if defined(__DragonFly__)
1831 DPRINTF(("%s mac %s station %u\n", __func__, ether_sprintf(node.bssid),
1832 entry));
1833#else
1834 DPRINTF(("%s mac %6D station %u\n", __func__, node.bssid, ":", entry));
1835#endif
1836
1837 CSR_WRITE_REGION_1(sc,
1838 IWI_CSR_NODE_BASE + entry * sizeof node,
1839 (uint8_t *)&node, sizeof node);
1840}
1841
1842static int
1843iwi_tx_start(struct iwi_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1844 int ac)
1845{
1846 struct ieee80211vap *vap = ni->ni_vap;
1847 struct ieee80211com *ic = ni->ni_ic;
1848 struct iwi_node *in = (struct iwi_node *)ni;
1849 const struct ieee80211_frame *wh;
1850 struct ieee80211_key *k;
1851 const struct chanAccParams *cap;
1852 struct iwi_tx_ring *txq = &sc->txq[ac];
1853 struct iwi_tx_data *data;
1854 struct iwi_tx_desc *desc;
1855 struct mbuf *mnew;
1856 bus_dma_segment_t segs[IWI_MAX_NSEG];
1857 int error, nsegs, hdrlen, i;
1858 int ismcast, flags, xflags, staid;
1859
1860 IWI_LOCK_ASSERT(sc);
1861 wh = mtod(m0, const struct ieee80211_frame *);
1862 /* NB: only data frames use this path */
1863 hdrlen = ieee80211_hdrsize(wh);
1864 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1865 flags = xflags = 0;
1866
1867 if (!ismcast)
1868 flags |= IWI_DATA_FLAG_NEED_ACK;
1869 if (vap->iv_flags & IEEE80211_F_SHPREAMBLE)
1870 flags |= IWI_DATA_FLAG_SHPREAMBLE;
1871 if (IEEE80211_QOS_HAS_SEQ(wh)) {
1872 xflags |= IWI_DATA_XFLAG_QOS;
1873 cap = &ic->ic_wme.wme_chanParams;
1874 if (!cap->cap_wmeParams[ac].wmep_noackPolicy)
1875 flags &= ~IWI_DATA_FLAG_NEED_ACK;
1876 }
1877
1878 /*
1879 * This is only used in IBSS mode where the firmware expect an index
1880 * in a h/w table instead of a destination address.
1881 */
1882 if (vap->iv_opmode == IEEE80211_M_IBSS) {
1883 if (!ismcast) {
1884 if (in->in_station == -1) {
1885#if defined(__DragonFly__)
1886 in->in_station = devfs_clone_bitmap_get(&sc->sc_unr,
1887 IWI_MAX_IBSSNODE-1);
1888#else
1889 in->in_station = alloc_unr(sc->sc_unr);
1890#endif
1891 if (in->in_station == -1) {
1892 /* h/w table is full */
1893 if_inc_counter(ni->ni_vap->iv_ifp,
1894 IFCOUNTER_OERRORS, 1);
1895 m_freem(m0);
1896 ieee80211_free_node(ni);
1897 return 0;
1898 }
1899 iwi_write_ibssnode(sc,
1900 ni->ni_macaddr, in->in_station);
1901 }
1902 staid = in->in_station;
1903 } else {
1904 /*
1905 * Multicast addresses have no associated node
1906 * so there will be no station entry. We reserve
1907 * entry 0 for one mcast address and use that.
1908 * If there are many being used this will be
1909 * expensive and we'll need to do a better job
1910 * but for now this handles the broadcast case.
1911 */
1912 if (!IEEE80211_ADDR_EQ(wh->i_addr1, sc->sc_mcast)) {
1913 IEEE80211_ADDR_COPY(sc->sc_mcast, wh->i_addr1);
1914 iwi_write_ibssnode(sc, sc->sc_mcast, 0);
1915 }
1916 staid = 0;
1917 }
1918 } else
1919 staid = 0;
1920
1921 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1922 k = ieee80211_crypto_encap(ni, m0);
1923 if (k == NULL) {
1924 m_freem(m0);
1925 return ENOBUFS;
1926 }
1927
1928 /* packet header may have moved, reset our local pointer */
1929 wh = mtod(m0, struct ieee80211_frame *);
1930 }
1931
1932 if (ieee80211_radiotap_active_vap(vap)) {
1933 struct iwi_tx_radiotap_header *tap = &sc->sc_txtap;
1934
1935 tap->wt_flags = 0;
1936
1937 ieee80211_radiotap_tx(vap, m0);
1938 }
1939
1940 data = &txq->data[txq->cur];
1941 desc = &txq->desc[txq->cur];
1942
1943 /* save and trim IEEE802.11 header */
1944 m_copydata(m0, 0, hdrlen, &desc->wh);
1945 m_adj(m0, hdrlen);
1946
1947#if defined(__DragonFly__)
1948 error = bus_dmamap_load_mbuf_segment(txq->data_dmat, data->map,
1949 m0, segs, 1, &nsegs, BUS_DMA_NOWAIT);
1950#else
1951 error = bus_dmamap_load_mbuf_sg(txq->data_dmat, data->map, m0, segs,
1952 &nsegs, 0);
1953#endif
1954 if (error != 0 && error != EFBIG) {
1955 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1956 error);
1957 m_freem(m0);
1958 return error;
1959 }
1960 if (error != 0) {
1961 mnew = m_defrag(m0, M_NOWAIT);
1962 if (mnew == NULL) {
1963 device_printf(sc->sc_dev,
1964 "could not defragment mbuf\n");
1965 m_freem(m0);
1966 return ENOBUFS;
1967 }
1968 m0 = mnew;
1969
1970#if defined(__DragonFly__)
1971 error = bus_dmamap_load_mbuf_segment(txq->data_dmat,
1972 data->map, m0, segs, 1, &nsegs, BUS_DMA_NOWAIT);
1973#else
1974 error = bus_dmamap_load_mbuf_sg(txq->data_dmat, data->map,
1975 m0, segs, &nsegs, 0);
1976#endif
1977 if (error != 0) {
1978 device_printf(sc->sc_dev,
1979 "could not map mbuf (error %d)\n", error);
1980 m_freem(m0);
1981 return error;
1982 }
1983 }
1984
1985 data->m = m0;
1986 data->ni = ni;
1987
1988 desc->hdr.type = IWI_HDR_TYPE_DATA;
1989 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1990 desc->station = staid;
1991 desc->cmd = IWI_DATA_CMD_TX;
1992 desc->len = htole16(m0->m_pkthdr.len);
1993 desc->flags = flags;
1994 desc->xflags = xflags;
1995
1996#if 0
1997 if (vap->iv_flags & IEEE80211_F_PRIVACY)
1998 desc->wep_txkey = vap->iv_def_txkey;
1999 else
2000#endif
2001 desc->flags |= IWI_DATA_FLAG_NO_WEP;
2002
2003 desc->nseg = htole32(nsegs);
2004 for (i = 0; i < nsegs; i++) {
2005 desc->seg_addr[i] = htole32(segs[i].ds_addr);
2006 desc->seg_len[i] = htole16(segs[i].ds_len);
2007 }
2008
2009 bus_dmamap_sync(txq->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
2010 bus_dmamap_sync(txq->desc_dmat, txq->desc_map, BUS_DMASYNC_PREWRITE);
2011
2012 DPRINTFN(5, ("sending data frame txq=%u idx=%u len=%u nseg=%u\n",
2013 ac, txq->cur, le16toh(desc->len), nsegs));
2014
2015 txq->queued++;
2016 txq->cur = (txq->cur + 1) % IWI_TX_RING_COUNT;
2017 CSR_WRITE_4(sc, txq->csr_widx, txq->cur);
2018
2019 return 0;
2020}
2021
2022static int
2023iwi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2024 const struct ieee80211_bpf_params *params)
2025{
2026 /* no support; just discard */
2027 m_freem(m);
2028 ieee80211_free_node(ni);
2029 return 0;
2030}
2031
2032static int
2033iwi_transmit(struct ieee80211com *ic, struct mbuf *m)
2034{
2035 struct iwi_softc *sc = ic->ic_softc;
2036 int error;
2037 IWI_LOCK_DECL;
2038
2039 IWI_LOCK(sc);
2040 if (!sc->sc_running) {
2041 IWI_UNLOCK(sc);
2042 return (ENXIO);
2043 }
2044 error = mbufq_enqueue(&sc->sc_snd, m);
2045 if (error) {
2046 IWI_UNLOCK(sc);
2047 return (error);
2048 }
2049 iwi_start(sc);
2050 IWI_UNLOCK(sc);
2051 return (0);
2052}
2053
2054static void
2055iwi_start(struct iwi_softc *sc)
2056{
2057 struct mbuf *m;
2058 struct ieee80211_node *ni;
2059 int ac;
2060
2061 IWI_LOCK_ASSERT(sc);
2062
2063 while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
2064 ac = M_WME_GETAC(m);
2065 if (sc->txq[ac].queued > IWI_TX_RING_COUNT - 8) {
2066 /* there is no place left in this ring; tail drop */
2067 /* XXX tail drop */
2068 mbufq_prepend(&sc->sc_snd, m);
2069 break;
2070 }
2071 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
2072 if (iwi_tx_start(sc, m, ni, ac) != 0) {
2073 ieee80211_free_node(ni);
2074 if_inc_counter(ni->ni_vap->iv_ifp,
2075 IFCOUNTER_OERRORS, 1);
2076 break;
2077 }
2078 sc->sc_tx_timer = 5;
2079 }
2080}
2081
2082static void
2083iwi_watchdog(void *arg)
2084{
2085 struct iwi_softc *sc = arg;
2086 struct ieee80211com *ic = &sc->sc_ic;
2087
2088 IWI_LOCK_ASSERT(sc);
2089
2090 if (sc->sc_tx_timer > 0) {
2091 if (--sc->sc_tx_timer == 0) {
2092 device_printf(sc->sc_dev, "device timeout\n");
2093#if defined(__DragonFly__)
2094 ++ic->ic_oerrors;
2095#else
2096 counter_u64_add(ic->ic_oerrors, 1);
2097#endif
2098 ieee80211_runtask(ic, &sc->sc_restarttask);
2099 }
2100 }
2101 if (sc->sc_state_timer > 0) {
2102 if (--sc->sc_state_timer == 0) {
2103 device_printf(sc->sc_dev,
2104 "firmware stuck in state %d, resetting\n",
2105 sc->fw_state);
2106 if (sc->fw_state == IWI_FW_SCANNING)
2107 ieee80211_cancel_scan(TAILQ_FIRST(&ic->ic_vaps));
2108 ieee80211_runtask(ic, &sc->sc_restarttask);
2109 sc->sc_state_timer = 3;
2110 }
2111 }
2112 if (sc->sc_busy_timer > 0) {
2113 if (--sc->sc_busy_timer == 0) {
2114 device_printf(sc->sc_dev,
2115 "firmware command timeout, resetting\n");
2116 ieee80211_runtask(ic, &sc->sc_restarttask);
2117 }
2118 }
2119 callout_reset(&sc->sc_wdtimer, hz, iwi_watchdog, sc);
2120}
2121
2122static void
2123iwi_parent(struct ieee80211com *ic)
2124{
2125 struct iwi_softc *sc = ic->ic_softc;
2126 int startall = 0;
2127 IWI_LOCK_DECL;
2128
2129 IWI_LOCK(sc);
2130 if (ic->ic_nrunning > 0) {
2131 if (!sc->sc_running) {
2132 iwi_init_locked(sc);
2133 startall = 1;
2134 }
2135 } else if (sc->sc_running)
2136 iwi_stop_locked(sc);
2137 IWI_UNLOCK(sc);
2138 if (startall)
2139 ieee80211_start_all(ic);
2140}
2141
2142static void
2143iwi_stop_master(struct iwi_softc *sc)
2144{
2145 uint32_t tmp;
2146 int ntries;
2147
2148 /* disable interrupts */
2149 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
2150
2151 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER);
2152 for (ntries = 0; ntries < 5; ntries++) {
2153 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
2154 break;
2155 DELAY(10);
2156 }
2157 if (ntries == 5)
2158 device_printf(sc->sc_dev, "timeout waiting for master\n");
2159
2160 tmp = CSR_READ_4(sc, IWI_CSR_RST);
2161 CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_PRINCETON_RESET);
2162
2163 sc->flags &= ~IWI_FLAG_FW_INITED;
2164}
2165
2166static int
2167iwi_reset(struct iwi_softc *sc)
2168{
2169 uint32_t tmp;
2170 int i, ntries;
2171
2172 iwi_stop_master(sc);
2173
2174 tmp = CSR_READ_4(sc, IWI_CSR_CTL);
2175 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT);
2176
2177 CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST);
2178
2179 /* wait for clock stabilization */
2180 for (ntries = 0; ntries < 1000; ntries++) {
2181 if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY)
2182 break;
2183 DELAY(200);
2184 }
2185 if (ntries == 1000) {
2186 device_printf(sc->sc_dev,
2187 "timeout waiting for clock stabilization\n");
2188 return EIO;
2189 }
2190
2191 tmp = CSR_READ_4(sc, IWI_CSR_RST);
2192 CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_SOFT_RESET);
2193
2194 DELAY(10);
2195
2196 tmp = CSR_READ_4(sc, IWI_CSR_CTL);
2197 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT);
2198
2199 /* clear NIC memory */
2200 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0);
2201 for (i = 0; i < 0xc000; i++)
2202 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
2203
2204 return 0;
2205}
2206
2207static const struct iwi_firmware_ohdr *
2208iwi_setup_ofw(struct iwi_softc *sc, struct iwi_fw *fw)
2209{
2210 const struct firmware *fp = fw->fp;
2211 const struct iwi_firmware_ohdr *hdr;
2212
2213 if (fp->datasize < sizeof (struct iwi_firmware_ohdr)) {
2214 device_printf(sc->sc_dev, "image '%s' too small\n", fp->name);
2215 return NULL;
2216 }
2217 hdr = (const struct iwi_firmware_ohdr *)fp->data;
2218 if ((IWI_FW_GET_MAJOR(le32toh(hdr->version)) != IWI_FW_REQ_MAJOR) ||
2219 (IWI_FW_GET_MINOR(le32toh(hdr->version)) != IWI_FW_REQ_MINOR)) {
2220 device_printf(sc->sc_dev, "version for '%s' %d.%d != %d.%d\n",
2221 fp->name, IWI_FW_GET_MAJOR(le32toh(hdr->version)),
2222 IWI_FW_GET_MINOR(le32toh(hdr->version)), IWI_FW_REQ_MAJOR,
2223 IWI_FW_REQ_MINOR);
2224 return NULL;
2225 }
2226 fw->data = ((const char *) fp->data) + sizeof(struct iwi_firmware_ohdr);
2227 fw->size = fp->datasize - sizeof(struct iwi_firmware_ohdr);
2228 fw->name = fp->name;
2229 return hdr;
2230}
2231
2232static const struct iwi_firmware_ohdr *
2233iwi_setup_oucode(struct iwi_softc *sc, struct iwi_fw *fw)
2234{
2235 const struct iwi_firmware_ohdr *hdr;
2236
2237 hdr = iwi_setup_ofw(sc, fw);
2238 if (hdr != NULL && le32toh(hdr->mode) != IWI_FW_MODE_UCODE) {
2239 device_printf(sc->sc_dev, "%s is not a ucode image\n",
2240 fw->name);
2241 hdr = NULL;
2242 }
2243 return hdr;
2244}
2245
2246static void
2247iwi_getfw(struct iwi_fw *fw, const char *fwname,
2248 struct iwi_fw *uc, const char *ucname)
2249{
2250 if (fw->fp == NULL)
2251 fw->fp = firmware_get(fwname);
2252 /* NB: pre-3.0 ucode is packaged separately */
2253 if (uc->fp == NULL && fw->fp != NULL && fw->fp->version < 300)
2254 uc->fp = firmware_get(ucname);
2255}
2256
2257/*
2258 * Get the required firmware images if not already loaded.
2259 * Note that we hold firmware images so long as the device
2260 * is marked up in case we need to reload them on device init.
2261 * This is necessary because we re-init the device sometimes
2262 * from a context where we cannot read from the filesystem
2263 * (e.g. from the taskqueue thread when rfkill is re-enabled).
2264 * XXX return 0 on success, 1 on error.
2265 *
2266 * NB: the order of get'ing and put'ing images here is
2267 * intentional to support handling firmware images bundled
2268 * by operating mode and/or all together in one file with
2269 * the boot firmware as "master".
2270 */
2271static int
2272iwi_get_firmware(struct iwi_softc *sc, enum ieee80211_opmode opmode)
2273{
2274 const struct iwi_firmware_hdr *hdr;
2275 const struct firmware *fp;
2276
2277 /* invalidate cached firmware on mode change */
2278 if (sc->fw_mode != opmode)
2279 iwi_put_firmware(sc);
2280
2281 switch (opmode) {
2282 case IEEE80211_M_STA:
2283 iwi_getfw(&sc->fw_fw, "iwi_bss", &sc->fw_uc, "iwi_ucode_bss");
2284 break;
2285 case IEEE80211_M_IBSS:
2286 iwi_getfw(&sc->fw_fw, "iwi_ibss", &sc->fw_uc, "iwi_ucode_ibss");
2287 break;
2288 case IEEE80211_M_MONITOR:
2289 iwi_getfw(&sc->fw_fw, "iwi_monitor",
2290 &sc->fw_uc, "iwi_ucode_monitor");
2291 break;
2292 default:
2293 device_printf(sc->sc_dev, "unknown opmode %d\n", opmode);
2294 return EINVAL;
2295 }
2296 fp = sc->fw_fw.fp;
2297 if (fp == NULL) {
2298 device_printf(sc->sc_dev, "could not load firmware\n");
2299 goto bad;
2300 }
2301 if (fp->version < 300) {
2302 /*
2303 * Firmware prior to 3.0 was packaged as separate
2304 * boot, firmware, and ucode images. Verify the
2305 * ucode image was read in, retrieve the boot image
2306 * if needed, and check version stamps for consistency.
2307 * The version stamps in the data are also checked
2308 * above; this is a bit paranoid but is a cheap
2309 * safeguard against mis-packaging.
2310 */
2311 if (sc->fw_uc.fp == NULL) {
2312 device_printf(sc->sc_dev, "could not load ucode\n");
2313 goto bad;
2314 }
2315 if (sc->fw_boot.fp == NULL) {
2316 sc->fw_boot.fp = firmware_get("iwi_boot");
2317 if (sc->fw_boot.fp == NULL) {
2318 device_printf(sc->sc_dev,
2319 "could not load boot firmware\n");
2320 goto bad;
2321 }
2322 }
2323 if (sc->fw_boot.fp->version != sc->fw_fw.fp->version ||
2324 sc->fw_boot.fp->version != sc->fw_uc.fp->version) {
2325 device_printf(sc->sc_dev,
2326 "firmware version mismatch: "
2327 "'%s' is %d, '%s' is %d, '%s' is %d\n",
2328 sc->fw_boot.fp->name, sc->fw_boot.fp->version,
2329 sc->fw_uc.fp->name, sc->fw_uc.fp->version,
2330 sc->fw_fw.fp->name, sc->fw_fw.fp->version
2331 );
2332 goto bad;
2333 }
2334 /*
2335 * Check and setup each image.
2336 */
2337 if (iwi_setup_oucode(sc, &sc->fw_uc) == NULL ||
2338 iwi_setup_ofw(sc, &sc->fw_boot) == NULL ||
2339 iwi_setup_ofw(sc, &sc->fw_fw) == NULL)
2340 goto bad;
2341 } else {
2342 /*
2343 * Check and setup combined image.
2344 */
2345 if (fp->datasize < sizeof(struct iwi_firmware_hdr)) {
2346 device_printf(sc->sc_dev, "image '%s' too small\n",
2347 fp->name);
2348 goto bad;
2349 }
2350 hdr = (const struct iwi_firmware_hdr *)fp->data;
2351 if (fp->datasize < sizeof(*hdr) + le32toh(hdr->bsize) + le32toh(hdr->usize)
2352 + le32toh(hdr->fsize)) {
2353 device_printf(sc->sc_dev, "image '%s' too small (2)\n",
2354 fp->name);
2355 goto bad;
2356 }
2357 sc->fw_boot.data = ((const char *) fp->data) + sizeof(*hdr);
2358 sc->fw_boot.size = le32toh(hdr->bsize);
2359 sc->fw_boot.name = fp->name;
2360 sc->fw_uc.data = sc->fw_boot.data + sc->fw_boot.size;
2361 sc->fw_uc.size = le32toh(hdr->usize);
2362 sc->fw_uc.name = fp->name;
2363 sc->fw_fw.data = sc->fw_uc.data + sc->fw_uc.size;
2364 sc->fw_fw.size = le32toh(hdr->fsize);
2365 sc->fw_fw.name = fp->name;
2366 }
2367#if 0
2368 device_printf(sc->sc_dev, "boot %d ucode %d fw %d bytes\n",
2369 sc->fw_boot.size, sc->fw_uc.size, sc->fw_fw.size);
2370#endif
2371
2372 sc->fw_mode = opmode;
2373 return 0;
2374bad:
2375 iwi_put_firmware(sc);
2376 return 1;
2377}
2378
2379static void
2380iwi_put_fw(struct iwi_fw *fw)
2381{
2382 if (fw->fp != NULL) {
2383 firmware_put(fw->fp, FIRMWARE_UNLOAD);
2384 fw->fp = NULL;
2385 }
2386 fw->data = NULL;
2387 fw->size = 0;
2388 fw->name = NULL;
2389}
2390
2391/*
2392 * Release any cached firmware images.
2393 */
2394static void
2395iwi_put_firmware(struct iwi_softc *sc)
2396{
2397 iwi_put_fw(&sc->fw_uc);
2398 iwi_put_fw(&sc->fw_fw);
2399 iwi_put_fw(&sc->fw_boot);
2400}
2401
2402static int
2403iwi_load_ucode(struct iwi_softc *sc, const struct iwi_fw *fw)
2404{
2405 uint32_t tmp;
2406 const uint16_t *w;
2407 const char *uc = fw->data;
2408 size_t size = fw->size;
2409 int i, ntries, error;
2410
2411 IWI_LOCK_ASSERT(sc);
2412 error = 0;
2413 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
2414 IWI_RST_STOP_MASTER);
2415 for (ntries = 0; ntries < 5; ntries++) {
2416 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
2417 break;
2418 DELAY(10);
2419 }
2420 if (ntries == 5) {
2421 device_printf(sc->sc_dev, "timeout waiting for master\n");
2422 error = EIO;
2423 goto fail;
2424 }
2425
2426 MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
2427 DELAY(5000);
2428
2429 tmp = CSR_READ_4(sc, IWI_CSR_RST);
2430 tmp &= ~IWI_RST_PRINCETON_RESET;
2431 CSR_WRITE_4(sc, IWI_CSR_RST, tmp);
2432
2433 DELAY(5000);
2434 MEM_WRITE_4(sc, 0x3000e0, 0);
2435 DELAY(1000);
2436 MEM_WRITE_4(sc, IWI_MEM_EEPROM_EVENT, 1);
2437 DELAY(1000);
2438 MEM_WRITE_4(sc, IWI_MEM_EEPROM_EVENT, 0);
2439 DELAY(1000);
2440 MEM_WRITE_1(sc, 0x200000, 0x00);
2441 MEM_WRITE_1(sc, 0x200000, 0x40);
2442 DELAY(1000);
2443
2444 /* write microcode into adapter memory */
2445 for (w = (const uint16_t *)uc; size > 0; w++, size -= 2)
2446 MEM_WRITE_2(sc, 0x200010, htole16(*w));
2447
2448 MEM_WRITE_1(sc, 0x200000, 0x00);
2449 MEM_WRITE_1(sc, 0x200000, 0x80);
2450
2451 /* wait until we get an answer */
2452 for (ntries = 0; ntries < 100; ntries++) {
2453 if (MEM_READ_1(sc, 0x200000) & 1)
2454 break;
2455 DELAY(100);
2456 }
2457 if (ntries == 100) {
2458 device_printf(sc->sc_dev,
2459 "timeout waiting for ucode to initialize\n");
2460 error = EIO;
2461 goto fail;
2462 }
2463
2464 /* read the answer or the firmware will not initialize properly */
2465 for (i = 0; i < 7; i++)
2466 MEM_READ_4(sc, 0x200004);
2467
2468 MEM_WRITE_1(sc, 0x200000, 0x00);
2469
2470fail:
2471 return error;
2472}
2473
2474/* macro to handle unaligned little endian data in firmware image */
2475#define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
2476
2477static int
2478iwi_load_firmware(struct iwi_softc *sc, const struct iwi_fw *fw)
2479{
2480 u_char *p, *end;
2481 uint32_t sentinel, ctl, src, dst, sum, len, mlen, tmp;
2482 int ntries, error;
2483
2484 IWI_LOCK_ASSERT(sc);
2485
2486 /* copy firmware image to DMA memory */
2487 memcpy(sc->fw_virtaddr, fw->data, fw->size);
2488
2489 /* make sure the adapter will get up-to-date values */
2490 bus_dmamap_sync(sc->fw_dmat, sc->fw_map, BUS_DMASYNC_PREWRITE);
2491
2492 /* tell the adapter where the command blocks are stored */
2493 MEM_WRITE_4(sc, 0x3000a0, 0x27000);
2494
2495 /*
2496 * Store command blocks into adapter's internal memory using register
2497 * indirections. The adapter will read the firmware image through DMA
2498 * using information stored in command blocks.
2499 */
2500 src = sc->fw_physaddr;
2501 p = sc->fw_virtaddr;
2502 end = p + fw->size;
2503 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000);
2504
2505 while (p < end) {
2506 dst = GETLE32(p); p += 4; src += 4;
2507 len = GETLE32(p); p += 4; src += 4;
2508 p += len;
2509
2510 while (len > 0) {
2511 mlen = min(len, IWI_CB_MAXDATALEN);
2512
2513 ctl = IWI_CB_DEFAULT_CTL | mlen;
2514 sum = ctl ^ src ^ dst;
2515
2516 /* write a command block */
2517 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, ctl);
2518 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, src);
2519 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, dst);
2520 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum);
2521
2522 src += mlen;
2523 dst += mlen;
2524 len -= mlen;
2525 }
2526 }
2527
2528 /* write a fictive final command block (sentinel) */
2529 sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR);
2530 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
2531
2532 tmp = CSR_READ_4(sc, IWI_CSR_RST);
2533 tmp &= ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER);
2534 CSR_WRITE_4(sc, IWI_CSR_RST, tmp);
2535
2536 /* tell the adapter to start processing command blocks */
2537 MEM_WRITE_4(sc, 0x3000a4, 0x540100);
2538
2539 /* wait until the adapter reaches the sentinel */
2540 for (ntries = 0; ntries < 400; ntries++) {
2541 if (MEM_READ_4(sc, 0x3000d0) >= sentinel)
2542 break;
2543 DELAY(100);
2544 }
2545 /* sync dma, just in case */
2546 bus_dmamap_sync(sc->fw_dmat, sc->fw_map, BUS_DMASYNC_POSTWRITE);
2547 if (ntries == 400) {
2548 device_printf(sc->sc_dev,
2549 "timeout processing command blocks for %s firmware\n",
2550 fw->name);
2551 return EIO;
2552 }
2553
2554 /* we're done with command blocks processing */
2555 MEM_WRITE_4(sc, 0x3000a4, 0x540c00);
2556
2557 /* allow interrupts so we know when the firmware is ready */
2558 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
2559
2560 /* tell the adapter to initialize the firmware */
2561 CSR_WRITE_4(sc, IWI_CSR_RST, 0);
2562
2563 tmp = CSR_READ_4(sc, IWI_CSR_CTL);
2564 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_ALLOW_STANDBY);
2565
2566 /* wait at most one second for firmware initialization to complete */
2567#if defined(__DragonFly__)
2568 if ((error = lksleep(sc, &sc->sc_lock, 0, "iwiinit", hz)) != 0) {
2569#else
2570 if ((error = msleep(sc, &sc->sc_mtx, 0, "iwiinit", hz)) != 0) {
2571#endif
2572 device_printf(sc->sc_dev, "timeout waiting for %s firmware "
2573 "initialization to complete\n", fw->name);
2574 }
2575
2576 return error;
2577}
2578
2579static int
2580iwi_setpowermode(struct iwi_softc *sc, struct ieee80211vap *vap)
2581{
2582 uint32_t data;
2583
2584 if (vap->iv_flags & IEEE80211_F_PMGTON) {
2585 /* XXX set more fine-grained operation */
2586 data = htole32(IWI_POWER_MODE_MAX);
2587 } else
2588 data = htole32(IWI_POWER_MODE_CAM);
2589
2590 DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2591 return iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data);
2592}
2593
2594static int
2595iwi_setwepkeys(struct iwi_softc *sc, struct ieee80211vap *vap)
2596{
2597 struct iwi_wep_key wepkey;
2598 struct ieee80211_key *wk;
2599 int error, i;
2600
2601 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2602 wk = &vap->iv_nw_keys[i];
2603
2604 wepkey.cmd = IWI_WEP_KEY_CMD_SETKEY;
2605 wepkey.idx = i;
2606 wepkey.len = wk->wk_keylen;
2607 memset(wepkey.key, 0, sizeof wepkey.key);
2608 memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2609 DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx,
2610 wepkey.len));
2611 error = iwi_cmd(sc, IWI_CMD_SET_WEP_KEY, &wepkey,
2612 sizeof wepkey);
2613 if (error != 0)
2614 return error;
2615 }
2616 return 0;
2617}
2618
2619static int
2620iwi_config(struct iwi_softc *sc)
2621{
2622 struct ieee80211com *ic = &sc->sc_ic;
2623 struct iwi_configuration config;
2624 struct iwi_rateset rs;
2625 struct iwi_txpower power;
2626 uint32_t data;
2627 int error, i;
2628
2629 IWI_LOCK_ASSERT(sc);
2630
2631#if defined(__DragonFly__)
2632 DPRINTF(("Setting MAC address to %s\n", ether_sprintf(ic->ic_macaddr)));
2633#else
2634 DPRINTF(("Setting MAC address to %6D\n", ic->ic_macaddr, ":"));
2635#endif
2636 error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, ic->ic_macaddr,
2637 IEEE80211_ADDR_LEN);
2638 if (error != 0)
2639 return error;
2640
2641 memset(&config, 0, sizeof config);
2642 config.bluetooth_coexistence = sc->bluetooth;
2643 config.silence_threshold = 0x1e;
2644 config.antenna = sc->antenna;
2645 config.multicast_enabled = 1;
2646 config.answer_pbreq = (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
2647 config.disable_unicast_decryption = 1;
2648 config.disable_multicast_decryption = 1;
2649 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2650 config.allow_invalid_frames = 1;
2651 config.allow_beacon_and_probe_resp = 1;
2652 config.allow_mgt = 1;
2653 }
2654 DPRINTF(("Configuring adapter\n"));
2655 error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config);
2656 if (error != 0)
2657 return error;
2658 if (ic->ic_opmode == IEEE80211_M_IBSS) {
2659 power.mode = IWI_MODE_11B;
2660 power.nchan = 11;
2661 for (i = 0; i < 11; i++) {
2662 power.chan[i].chan = i + 1;
2663 power.chan[i].power = IWI_TXPOWER_MAX;
2664 }
2665 DPRINTF(("Setting .11b channels tx power\n"));
2666 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power);
2667 if (error != 0)
2668 return error;
2669
2670 power.mode = IWI_MODE_11G;
2671 DPRINTF(("Setting .11g channels tx power\n"));
2672 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power);
2673 if (error != 0)
2674 return error;
2675 }
2676
2677 memset(&rs, 0, sizeof rs);
2678 rs.mode = IWI_MODE_11G;
2679 rs.type = IWI_RATESET_TYPE_SUPPORTED;
2680 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates;
2681 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11G].rs_rates,
2682 rs.nrates);
2683 DPRINTF(("Setting .11bg supported rates (%u)\n", rs.nrates));
2684 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs);
2685 if (error != 0)
2686 return error;
2687
2688 memset(&rs, 0, sizeof rs);
2689 rs.mode = IWI_MODE_11A;
2690 rs.type = IWI_RATESET_TYPE_SUPPORTED;
2691 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates;
2692 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11A].rs_rates,
2693 rs.nrates);
2694 DPRINTF(("Setting .11a supported rates (%u)\n", rs.nrates));
2695 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs);
2696 if (error != 0)
2697 return error;
2698
2699 data = htole32(karc4random());
2700 DPRINTF(("Setting initialization vector to %u\n", le32toh(data)));
2701 error = iwi_cmd(sc, IWI_CMD_SET_IV, &data, sizeof data);
2702 if (error != 0)
2703 return error;
2704
2705 /* enable adapter */
2706 DPRINTF(("Enabling adapter\n"));
2707 return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0);
2708}
2709
2710static __inline void
2711set_scan_type(struct iwi_scan_ext *scan, int ix, int scan_type)
2712{
2713 uint8_t *st = &scan->scan_type[ix / 2];
2714 if (ix % 2)
2715 *st = (*st & 0xf0) | ((scan_type & 0xf) << 0);
2716 else
2717 *st = (*st & 0x0f) | ((scan_type & 0xf) << 4);
2718}
2719
2720static int
2721scan_type(const struct ieee80211_scan_state *ss,
2722 const struct ieee80211_channel *chan)
2723{
2724 /* We can only set one essid for a directed scan */
2725 if (ss->ss_nssid != 0)
2726 return IWI_SCAN_TYPE_BDIRECTED;
2727 if ((ss->ss_flags & IEEE80211_SCAN_ACTIVE) &&
2728 (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0)
2729 return IWI_SCAN_TYPE_BROADCAST;
2730 return IWI_SCAN_TYPE_PASSIVE;
2731}
2732
2733static __inline int
2734scan_band(const struct ieee80211_channel *c)
2735{
2736 return IEEE80211_IS_CHAN_5GHZ(c) ? IWI_CHAN_5GHZ : IWI_CHAN_2GHZ;
2737}
2738
2739static void
2740iwi_monitor_scan(void *arg, int npending)
2741{
2742 struct iwi_softc *sc = arg;
2743 IWI_LOCK_DECL;
2744
2745 IWI_LOCK(sc);
2746 (void) iwi_scanchan(sc, 2000, 0);
2747 IWI_UNLOCK(sc);
2748}
2749
2750/*
2751 * Start a scan on the current channel or all channels.
2752 */
2753static int
2754iwi_scanchan(struct iwi_softc *sc, unsigned long maxdwell, int allchan)
2755{
2756 struct ieee80211com *ic = &sc->sc_ic;
2757 struct ieee80211_channel *chan;
2758 struct ieee80211_scan_state *ss;
2759 struct iwi_scan_ext scan;
2760 int error = 0;
2761
2762 IWI_LOCK_ASSERT(sc);
2763 if (sc->fw_state == IWI_FW_SCANNING) {
2764 /*
2765 * This should not happen as we only trigger scan_next after
2766 * completion
2767 */
2768 DPRINTF(("%s: called too early - still scanning\n", __func__));
2769 return (EBUSY);
2770 }
2771 IWI_STATE_BEGIN(sc, IWI_FW_SCANNING);
2772
2773 ss = ic->ic_scan;
2774
2775 memset(&scan, 0, sizeof scan);
2776 scan.full_scan_index = htole32(++sc->sc_scangen);
2777 scan.dwell_time[IWI_SCAN_TYPE_PASSIVE] = htole16(maxdwell);
2778 if (ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) {
2779 /*
2780 * Use very short dwell times for when we send probe request
2781 * frames. Without this bg scans hang. Ideally this should
2782 * be handled with early-termination as done by net80211 but
2783 * that's not feasible (aborting a scan is problematic).
2784 */
2785 scan.dwell_time[IWI_SCAN_TYPE_BROADCAST] = htole16(30);
2786 scan.dwell_time[IWI_SCAN_TYPE_BDIRECTED] = htole16(30);
2787 } else {
2788 scan.dwell_time[IWI_SCAN_TYPE_BROADCAST] = htole16(maxdwell);
2789 scan.dwell_time[IWI_SCAN_TYPE_BDIRECTED] = htole16(maxdwell);
2790 }
2791
2792 /* We can only set one essid for a directed scan */
2793 if (ss->ss_nssid != 0) {
2794 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ss->ss_ssid[0].ssid,
2795 ss->ss_ssid[0].len);
2796 if (error)
2797 return (error);
2798 }
2799
2800 if (allchan) {
2801 int i, next, band, b, bstart;
2802 /*
2803 * Convert scan list to run-length encoded channel list
2804 * the firmware requires (preserving the order setup by
2805 * net80211). The first entry in each run specifies the
2806 * band and the count of items in the run.
2807 */
2808 next = 0; /* next open slot */
2809 bstart = 0; /* NB: not needed, silence compiler */
2810 band = -1; /* NB: impossible value */
2811 KASSERT(ss->ss_last > 0, ("no channels"));
2812 for (i = 0; i < ss->ss_last; i++) {
2813 chan = ss->ss_chans[i];
2814 b = scan_band(chan);
2815 if (b != band) {
2816 if (band != -1)
2817 scan.channels[bstart] =
2818 (next - bstart) | band;
2819 /* NB: this allocates a slot for the run-len */
2820 band = b, bstart = next++;
2821 }
2822 if (next >= IWI_SCAN_CHANNELS) {
2823 DPRINTF(("truncating scan list\n"));
2824 break;
2825 }
2826 scan.channels[next] = ieee80211_chan2ieee(ic, chan);
2827 set_scan_type(&scan, next, scan_type(ss, chan));
2828 next++;
2829 }
2830 scan.channels[bstart] = (next - bstart) | band;
2831 } else {
2832 /* Scan the current channel only */
2833 chan = ic->ic_curchan;
2834 scan.channels[0] = 1 | scan_band(chan);
2835 scan.channels[1] = ieee80211_chan2ieee(ic, chan);
2836 set_scan_type(&scan, 1, scan_type(ss, chan));
2837 }
2838#ifdef IWI_DEBUG
2839 if (iwi_debug > 0) {
2840 static const char *scantype[8] =
2841 { "PSTOP", "PASV", "DIR", "BCAST", "BDIR", "5", "6", "7" };
2842 int i;
2843 kprintf("Scan request: index %u dwell %d/%d/%d\n"
2844 , le32toh(scan.full_scan_index)
2845 , le16toh(scan.dwell_time[IWI_SCAN_TYPE_PASSIVE])
2846 , le16toh(scan.dwell_time[IWI_SCAN_TYPE_BROADCAST])
2847 , le16toh(scan.dwell_time[IWI_SCAN_TYPE_BDIRECTED])
2848 );
2849 i = 0;
2850 do {
2851 int run = scan.channels[i];
2852 if (run == 0)
2853 break;
2854 kprintf("Scan %d %s channels:", run & 0x3f,
2855 run & IWI_CHAN_2GHZ ? "2.4GHz" : "5GHz");
2856 for (run &= 0x3f, i++; run > 0; run--, i++) {
2857 uint8_t type = scan.scan_type[i/2];
2858 kprintf(" %u/%s", scan.channels[i],
2859 scantype[(i & 1 ? type : type>>4) & 7]);
2860 }
2861 kprintf("\n");
2862 } while (i < IWI_SCAN_CHANNELS);
2863 }
2864#endif
2865
2866 return (iwi_cmd(sc, IWI_CMD_SCAN_EXT, &scan, sizeof scan));
2867}
2868
2869static int
2870iwi_set_sensitivity(struct iwi_softc *sc, int8_t rssi_dbm)
2871{
2872 struct iwi_sensitivity sens;
2873
2874 DPRINTF(("Setting sensitivity to %d\n", rssi_dbm));
2875
2876 memset(&sens, 0, sizeof sens);
2877 sens.rssi = htole16(rssi_dbm);
2878 return iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &sens, sizeof sens);
2879}
2880
2881static int
2882iwi_auth_and_assoc(struct iwi_softc *sc, struct ieee80211vap *vap)
2883{
2884 struct ieee80211com *ic = vap->iv_ic;
2885 struct ifnet *ifp = vap->iv_ifp;
2886 struct ieee80211_node *ni;
2887 struct iwi_configuration config;
2888 struct iwi_associate *assoc = &sc->assoc;
2889 struct iwi_rateset rs;
2890 uint16_t capinfo;
2891 uint32_t data;
2892 int error, mode;
2893
2894 IWI_LOCK_ASSERT(sc);
2895
2896 ni = ieee80211_ref_node(vap->iv_bss);
2897
2898 if (sc->flags & IWI_FLAG_ASSOCIATED) {
2899 DPRINTF(("Already associated\n"));
2900 return (-1);
2901 }
2902
2903 IWI_STATE_BEGIN(sc, IWI_FW_ASSOCIATING);
2904 error = 0;
2905 mode = 0;
2906
2907 if (IEEE80211_IS_CHAN_A(ic->ic_curchan))
2908 mode = IWI_MODE_11A;
2909 else if (IEEE80211_IS_CHAN_G(ic->ic_curchan))
2910 mode = IWI_MODE_11G;
2911 if (IEEE80211_IS_CHAN_B(ic->ic_curchan))
2912 mode = IWI_MODE_11B;
2913
2914 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
2915 memset(&config, 0, sizeof config);
2916 config.bluetooth_coexistence = sc->bluetooth;
2917 config.antenna = sc->antenna;
2918 config.multicast_enabled = 1;
2919 if (mode == IWI_MODE_11G)
2920 config.use_protection = 1;
2921 config.answer_pbreq =
2922 (vap->iv_opmode == IEEE80211_M_IBSS) ? 1 : 0;
2923 config.disable_unicast_decryption = 1;
2924 config.disable_multicast_decryption = 1;
2925 DPRINTF(("Configuring adapter\n"));
2926 error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config);
2927 if (error != 0)
2928 goto done;
2929 }
2930
2931#ifdef IWI_DEBUG
2932 if (iwi_debug > 0) {
2933 kprintf("Setting ESSID to ");
2934 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
2935 kprintf("\n");
2936 }
2937#endif
2938 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen);
2939 if (error != 0)
2940 goto done;
2941
2942 error = iwi_setpowermode(sc, vap);
2943 if (error != 0)
2944 goto done;
2945
2946 data = htole32(vap->iv_rtsthreshold);
2947 DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2948 error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
2949 if (error != 0)
2950 goto done;
2951
2952 data = htole32(vap->iv_fragthreshold);
2953 DPRINTF(("Setting fragmentation threshold to %u\n", le32toh(data)));
2954 error = iwi_cmd(sc, IWI_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
2955 if (error != 0)
2956 goto done;
2957
2958 /* the rate set has already been "negotiated" */
2959 memset(&rs, 0, sizeof rs);
2960 rs.mode = mode;
2961 rs.type = IWI_RATESET_TYPE_NEGOTIATED;
2962 rs.nrates = ni->ni_rates.rs_nrates;
2963 if (rs.nrates > IWI_RATESET_SIZE) {
2964 DPRINTF(("Truncating negotiated rate set from %u\n",
2965 rs.nrates));
2966 rs.nrates = IWI_RATESET_SIZE;
2967 }
2968 memcpy(rs.rates, ni->ni_rates.rs_rates, rs.nrates);
2969 DPRINTF(("Setting negotiated rates (%u)\n", rs.nrates));
2970 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs);
2971 if (error != 0)
2972 goto done;
2973
2974 memset(assoc, 0, sizeof *assoc);
2975
2976 if ((vap->iv_flags & IEEE80211_F_WME) && ni->ni_ies.wme_ie != NULL) {
2977 /* NB: don't treat WME setup as failure */
2978 if (iwi_wme_setparams(sc) == 0 && iwi_wme_setie(sc) == 0)
2979 assoc->policy |= htole16(IWI_POLICY_WME);
2980 /* XXX complain on failure? */
2981 }
2982
2983 if (vap->iv_appie_wpa != NULL) {
2984 struct ieee80211_appie *ie = vap->iv_appie_wpa;
2985
2986 DPRINTF(("Setting optional IE (len=%u)\n", ie->ie_len));
2987 error = iwi_cmd(sc, IWI_CMD_SET_OPTIE, ie->ie_data, ie->ie_len);
2988 if (error != 0)
2989 goto done;
2990 }
2991
2992 error = iwi_set_sensitivity(sc, ic->ic_node_getrssi(ni));
2993 if (error != 0)
2994 goto done;
2995
2996 assoc->mode = mode;
2997 assoc->chan = ic->ic_curchan->ic_ieee;
2998 /*
2999 * NB: do not arrange for shared key auth w/o privacy
3000 * (i.e. a wep key); it causes a firmware error.
3001 */
3002 if ((vap->iv_flags & IEEE80211_F_PRIVACY) &&
3003 ni->ni_authmode == IEEE80211_AUTH_SHARED) {
3004 assoc->auth = IWI_AUTH_SHARED;
3005 /*
3006 * It's possible to have privacy marked but no default
3007 * key setup. This typically is due to a user app bug
3008 * but if we blindly grab the key the firmware will
3009 * barf so avoid it for now.
3010 */
3011 if (vap->iv_def_txkey != IEEE80211_KEYIX_NONE)
3012 assoc->auth |= vap->iv_def_txkey << 4;
3013
3014 error = iwi_setwepkeys(sc, vap);
3015 if (error != 0)
3016 goto done;
3017 }
3018 if (vap->iv_flags & IEEE80211_F_WPA)
3019 assoc->policy |= htole16(IWI_POLICY_WPA);
3020 if (vap->iv_opmode == IEEE80211_M_IBSS && ni->ni_tstamp.tsf == 0)
3021 assoc->type = IWI_HC_IBSS_START;
3022 else
3023 assoc->type = IWI_HC_ASSOC;
3024 memcpy(assoc->tstamp, ni->ni_tstamp.data, 8);
3025
3026 if (vap->iv_opmode == IEEE80211_M_IBSS)
3027 capinfo = IEEE80211_CAPINFO_IBSS;
3028 else
3029 capinfo = IEEE80211_CAPINFO_ESS;
3030 if (vap->iv_flags & IEEE80211_F_PRIVACY)
3031 capinfo |= IEEE80211_CAPINFO_PRIVACY;
3032 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
3033 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
3034 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
3035 if (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
3036 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
3037 assoc->capinfo = htole16(capinfo);
3038
3039 assoc->lintval = htole16(ic->ic_lintval);
3040 assoc->intval = htole16(ni->ni_intval);
3041 IEEE80211_ADDR_COPY(assoc->bssid, ni->ni_bssid);
3042 if (vap->iv_opmode == IEEE80211_M_IBSS)
3043 IEEE80211_ADDR_COPY(assoc->dst, ifp->if_broadcastaddr);
3044 else
3045 IEEE80211_ADDR_COPY(assoc->dst, ni->ni_bssid);
3046
3047#if defined(__DragonFly__)
3048 DPRINTF(("%s bssid %s dst %s channel %u policy 0x%x "
3049 "auth %u capinfo 0x%x lintval %u bintval %u\n",
3050 assoc->type == IWI_HC_IBSS_START ? "Start" : "Join",
3051 ether_sprintf(assoc->bssid), ether_sprintf(assoc->dst),
3052 assoc->chan, le16toh(assoc->policy), assoc->auth,
3053 le16toh(assoc->capinfo), le16toh(assoc->lintval),
3054 le16toh(assoc->intval)));
3055#else
3056 DPRINTF(("%s bssid %6D dst %6D channel %u policy 0x%x "
3057 "auth %u capinfo 0x%x lintval %u bintval %u\n",
3058 assoc->type == IWI_HC_IBSS_START ? "Start" : "Join",
3059 assoc->bssid, ":", assoc->dst, ":",
3060 assoc->chan, le16toh(assoc->policy), assoc->auth,
3061 le16toh(assoc->capinfo), le16toh(assoc->lintval),
3062 le16toh(assoc->intval)));
3063#endif
3064 error = iwi_cmd(sc, IWI_CMD_ASSOCIATE, assoc, sizeof *assoc);
3065done:
3066 ieee80211_free_node(ni);
3067 if (error)
3068 IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
3069
3070 return (error);
3071}
3072
3073static void
3074iwi_disassoc(void *arg, int pending)
3075{
3076 struct iwi_softc *sc = arg;
3077 IWI_LOCK_DECL;
3078
3079 IWI_LOCK(sc);
3080 iwi_disassociate(sc, 0);
3081 IWI_UNLOCK(sc);
3082}
3083
3084static int
3085iwi_disassociate(struct iwi_softc *sc, int quiet)
3086{
3087 struct iwi_associate *assoc = &sc->assoc;
3088
3089 if ((sc->flags & IWI_FLAG_ASSOCIATED) == 0) {
3090 DPRINTF(("Not associated\n"));
3091 return (-1);
3092 }
3093
3094 IWI_STATE_BEGIN(sc, IWI_FW_DISASSOCIATING);
3095
3096 if (quiet)
3097 assoc->type = IWI_HC_DISASSOC_QUIET;
3098 else
3099 assoc->type = IWI_HC_DISASSOC;
3100
3101#if defined(__DragonFly__)
3102 DPRINTF(("Trying to disassociate from %s channel %u\n",
3103 ether_sprintf(assoc->bssid), assoc->chan));
3104#else
3105 DPRINTF(("Trying to disassociate from %6D channel %u\n",
3106 assoc->bssid, ":", assoc->chan));
3107#endif
3108 return iwi_cmd(sc, IWI_CMD_ASSOCIATE, assoc, sizeof *assoc);
3109}
3110
3111/*
3112 * release dma resources for the firmware
3113 */
3114static void
3115iwi_release_fw_dma(struct iwi_softc *sc)
3116{
3117 if (sc->fw_flags & IWI_FW_HAVE_PHY)
3118 bus_dmamap_unload(sc->fw_dmat, sc->fw_map);
3119 if (sc->fw_flags & IWI_FW_HAVE_MAP)
3120 bus_dmamem_free(sc->fw_dmat, sc->fw_virtaddr, sc->fw_map);
3121 if (sc->fw_flags & IWI_FW_HAVE_DMAT)
3122 bus_dma_tag_destroy(sc->fw_dmat);
3123
3124 sc->fw_flags = 0;
3125 sc->fw_dma_size = 0;
3126 sc->fw_dmat = NULL;
3127 sc->fw_map = NULL;
3128 sc->fw_physaddr = 0;
3129 sc->fw_virtaddr = NULL;
3130}
3131
3132/*
3133 * allocate the dma descriptor for the firmware.
3134 * Return 0 on success, 1 on error.
3135 * Must be called unlocked, protected by IWI_FLAG_FW_LOADING.
3136 */
3137static int
3138iwi_init_fw_dma(struct iwi_softc *sc, int size)
3139{
3140 if (sc->fw_dma_size >= size)
3141 return 0;
3142#if defined(__DragonFly__)
3143 if (bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
3144 BUS_SPACE_MAXADDR, size, 1, size,
3145 0, &sc->fw_dmat) != 0) {
3146#else
3147 if (bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
3148 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
3149 size, 1, size, 0, NULL, NULL, &sc->fw_dmat) != 0) {
3150#endif
3151 device_printf(sc->sc_dev,
3152 "could not create firmware DMA tag\n");
3153 goto error;
3154 }
3155 sc->fw_flags |= IWI_FW_HAVE_DMAT;
3156 if (bus_dmamem_alloc(sc->fw_dmat, &sc->fw_virtaddr, 0,
3157 &sc->fw_map) != 0) {
3158 device_printf(sc->sc_dev,
3159 "could not allocate firmware DMA memory\n");
3160 goto error;
3161 }
3162 sc->fw_flags |= IWI_FW_HAVE_MAP;
3163 if (bus_dmamap_load(sc->fw_dmat, sc->fw_map, sc->fw_virtaddr,
3164 size, iwi_dma_map_addr, &sc->fw_physaddr, 0) != 0) {
3165 device_printf(sc->sc_dev, "could not load firmware DMA map\n");
3166 goto error;
3167 }
3168 sc->fw_flags |= IWI_FW_HAVE_PHY;
3169 sc->fw_dma_size = size;
3170 return 0;
3171
3172error:
3173 iwi_release_fw_dma(sc);
3174 return 1;
3175}
3176
3177static void
3178iwi_init_locked(struct iwi_softc *sc)
3179{
3180 struct iwi_rx_data *data;
3181 int i;
3182
3183 IWI_LOCK_ASSERT(sc);
3184
3185 if (sc->fw_state == IWI_FW_LOADING) {
3186 device_printf(sc->sc_dev, "%s: already loading\n", __func__);
3187 return; /* XXX: condvar? */
3188 }
3189
3190 iwi_stop_locked(sc);
3191
3192 IWI_STATE_BEGIN(sc, IWI_FW_LOADING);
3193
3194 if (iwi_reset(sc) != 0) {
3195 device_printf(sc->sc_dev, "could not reset adapter\n");
3196 goto fail;
3197 }
3198 if (iwi_load_firmware(sc, &sc->fw_boot) != 0) {
3199 device_printf(sc->sc_dev,
3200 "could not load boot firmware %s\n", sc->fw_boot.name);
3201 goto fail;
3202 }
3203 if (iwi_load_ucode(sc, &sc->fw_uc) != 0) {
3204 device_printf(sc->sc_dev,
3205 "could not load microcode %s\n", sc->fw_uc.name);
3206 goto fail;
3207 }
3208
3209 iwi_stop_master(sc);
3210
3211 CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmdq.physaddr);
3212 CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, sc->cmdq.count);
3213 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
3214
3215 CSR_WRITE_4(sc, IWI_CSR_TX1_BASE, sc->txq[0].physaddr);
3216 CSR_WRITE_4(sc, IWI_CSR_TX1_SIZE, sc->txq[0].count);
3217 CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq[0].cur);
3218
3219 CSR_WRITE_4(sc, IWI_CSR_TX2_BASE, sc->txq[1].physaddr);
3220 CSR_WRITE_4(sc, IWI_CSR_TX2_SIZE, sc->txq[1].count);
3221 CSR_WRITE_4(sc, IWI_CSR_TX2_WIDX, sc->txq[1].cur);
3222
3223 CSR_WRITE_4(sc, IWI_CSR_TX3_BASE, sc->txq[2].physaddr);
3224 CSR_WRITE_4(sc, IWI_CSR_TX3_SIZE, sc->txq[2].count);
3225 CSR_WRITE_4(sc, IWI_CSR_TX3_WIDX, sc->txq[2].cur);
3226
3227 CSR_WRITE_4(sc, IWI_CSR_TX4_BASE, sc->txq[3].physaddr);
3228 CSR_WRITE_4(sc, IWI_CSR_TX4_SIZE, sc->txq[3].count);
3229 CSR_WRITE_4(sc, IWI_CSR_TX4_WIDX, sc->txq[3].cur);
3230
3231 for (i = 0; i < sc->rxq.count; i++) {
3232 data = &sc->rxq.data[i];
3233 CSR_WRITE_4(sc, data->reg, data->physaddr);
3234 }
3235
3236 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, sc->rxq.count - 1);
3237
3238 if (iwi_load_firmware(sc, &sc->fw_fw) != 0) {
3239 device_printf(sc->sc_dev,
3240 "could not load main firmware %s\n", sc->fw_fw.name);
3241 goto fail;
3242 }
3243 sc->flags |= IWI_FLAG_FW_INITED;
3244
3245 IWI_STATE_END(sc, IWI_FW_LOADING);
3246
3247 if (iwi_config(sc) != 0) {
3248 device_printf(sc->sc_dev, "unable to enable adapter\n");
3249 goto fail2;
3250 }
3251
3252 callout_reset(&sc->sc_wdtimer, hz, iwi_watchdog, sc);
3253 sc->sc_running = 1;
3254 return;
3255fail:
3256 IWI_STATE_END(sc, IWI_FW_LOADING);
3257fail2:
3258 iwi_stop_locked(sc);
3259}
3260
3261static void
3262iwi_init(void *priv)
3263{
3264 struct iwi_softc *sc = priv;
3265 struct ieee80211com *ic = &sc->sc_ic;
3266 IWI_LOCK_DECL;
3267
3268 IWI_LOCK(sc);
3269 iwi_init_locked(sc);
3270 IWI_UNLOCK(sc);
3271
3272 if (sc->sc_running)
3273 ieee80211_start_all(ic);
3274}
3275
3276static void
3277iwi_stop_locked(void *priv)
3278{
3279 struct iwi_softc *sc = priv;
3280
3281 IWI_LOCK_ASSERT(sc);
3282
3283 sc->sc_running = 0;
3284
3285 if (sc->sc_softled) {
3286 callout_stop(&sc->sc_ledtimer);
3287 sc->sc_blinking = 0;
3288 }
3289 callout_stop(&sc->sc_wdtimer);
3290 callout_stop(&sc->sc_rftimer);
3291
3292 iwi_stop_master(sc);
3293
3294 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SOFT_RESET);
3295
3296 /* reset rings */
3297 iwi_reset_cmd_ring(sc, &sc->cmdq);
3298 iwi_reset_tx_ring(sc, &sc->txq[0]);
3299 iwi_reset_tx_ring(sc, &sc->txq[1]);
3300 iwi_reset_tx_ring(sc, &sc->txq[2]);
3301 iwi_reset_tx_ring(sc, &sc->txq[3]);
3302 iwi_reset_rx_ring(sc, &sc->rxq);
3303
3304 sc->sc_tx_timer = 0;
3305 sc->sc_state_timer = 0;
3306 sc->sc_busy_timer = 0;
3307 sc->flags &= ~(IWI_FLAG_BUSY | IWI_FLAG_ASSOCIATED);
3308 sc->fw_state = IWI_FW_IDLE;
3309 wakeup(sc);
3310}
3311
3312static void
3313iwi_stop(struct iwi_softc *sc)
3314{
3315 IWI_LOCK_DECL;
3316
3317 IWI_LOCK(sc);
3318 iwi_stop_locked(sc);
3319 IWI_UNLOCK(sc);
3320}
3321
3322static void
3323iwi_restart(void *arg, int npending)
3324{
3325 struct iwi_softc *sc = arg;
3326
3327 iwi_init(sc);
3328}
3329
3330/*
3331 * Return whether or not the radio is enabled in hardware
3332 * (i.e. the rfkill switch is "off").
3333 */
3334static int
3335iwi_getrfkill(struct iwi_softc *sc)
3336{
3337 return (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) == 0;
3338}
3339
3340static void
3341iwi_radio_on(void *arg, int pending)
3342{
3343 struct iwi_softc *sc = arg;
3344 struct ieee80211com *ic = &sc->sc_ic;
3345
3346 device_printf(sc->sc_dev, "radio turned on\n");
3347
3348 iwi_init(sc);
3349 ieee80211_notify_radio(ic, 1);
3350}
3351
3352static void
3353iwi_rfkill_poll(void *arg)
3354{
3355 struct iwi_softc *sc = arg;
3356
3357 IWI_LOCK_ASSERT(sc);
3358
3359 /*
3360 * Check for a change in rfkill state. We get an
3361 * interrupt when a radio is disabled but not when
3362 * it is enabled so we must poll for the latter.
3363 */
3364 if (!iwi_getrfkill(sc)) {
3365 ieee80211_runtask(&sc->sc_ic, &sc->sc_radiontask);
3366 return;
3367 }
3368 callout_reset(&sc->sc_rftimer, 2*hz, iwi_rfkill_poll, sc);
3369}
3370
3371static void
3372iwi_radio_off(void *arg, int pending)
3373{
3374 struct iwi_softc *sc = arg;
3375 struct ieee80211com *ic = &sc->sc_ic;
3376 IWI_LOCK_DECL;
3377
3378 device_printf(sc->sc_dev, "radio turned off\n");
3379
3380 ieee80211_notify_radio(ic, 0);
3381
3382 IWI_LOCK(sc);
3383 iwi_stop_locked(sc);
3384 iwi_rfkill_poll(sc);
3385 IWI_UNLOCK(sc);
3386}
3387
3388static int
3389iwi_sysctl_stats(SYSCTL_HANDLER_ARGS)
3390{
3391 struct iwi_softc *sc = arg1;
3392 uint32_t size, buf[128];
3393
3394 memset(buf, 0, sizeof buf);
3395
3396 if (!(sc->flags & IWI_FLAG_FW_INITED))
3397 return SYSCTL_OUT(req, buf, sizeof buf);
3398
3399 size = min(CSR_READ_4(sc, IWI_CSR_TABLE0_SIZE), 128 - 1);
3400 CSR_READ_REGION_4(sc, IWI_CSR_TABLE0_BASE, &buf[1], size);
3401
3402 return SYSCTL_OUT(req, buf, size);
3403}
3404
3405static int
3406iwi_sysctl_radio(SYSCTL_HANDLER_ARGS)
3407{
3408 struct iwi_softc *sc = arg1;
3409 int val = !iwi_getrfkill(sc);
3410
3411 return SYSCTL_OUT(req, &val, sizeof val);
3412}
3413
3414/*
3415 * Add sysctl knobs.
3416 */
3417static void
3418iwi_sysctlattach(struct iwi_softc *sc)
3419{
3420 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
3421 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
3422
3423 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "radio",
3424 CTLTYPE_INT | CTLFLAG_RD, sc, 0, iwi_sysctl_radio, "I",
3425 "radio transmitter switch state (0=off, 1=on)");
3426
3427 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "stats",
3428 CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, iwi_sysctl_stats, "S",
3429 "statistics");
3430
3431 sc->bluetooth = 0;
3432 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "bluetooth",
3433 CTLFLAG_RW, &sc->bluetooth, 0, "bluetooth coexistence");
3434
3435 sc->antenna = IWI_ANTENNA_AUTO;
3436 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "antenna",
3437 CTLFLAG_RW, &sc->antenna, 0, "antenna (0=auto)");
3438}
3439
3440/*
3441 * LED support.
3442 *
3443 * Different cards have different capabilities. Some have three
3444 * led's while others have only one. The linux ipw driver defines
3445 * led's for link state (associated or not), band (11a, 11g, 11b),
3446 * and for link activity. We use one led and vary the blink rate
3447 * according to the tx/rx traffic a la the ath driver.
3448 */
3449
3450static __inline uint32_t
3451iwi_toggle_event(uint32_t r)
3452{
3453 return r &~ (IWI_RST_STANDBY | IWI_RST_GATE_ODMA |
3454 IWI_RST_GATE_IDMA | IWI_RST_GATE_ADMA);
3455}
3456
3457static uint32_t
3458iwi_read_event(struct iwi_softc *sc)
3459{
3460 return MEM_READ_4(sc, IWI_MEM_EEPROM_EVENT);
3461}
3462
3463static void
3464iwi_write_event(struct iwi_softc *sc, uint32_t v)
3465{
3466 MEM_WRITE_4(sc, IWI_MEM_EEPROM_EVENT, v);
3467}
3468
3469static void
3470iwi_led_done(void *arg)
3471{
3472 struct iwi_softc *sc = arg;
3473
3474 sc->sc_blinking = 0;
3475}
3476
3477/*
3478 * Turn the activity LED off: flip the pin and then set a timer so no
3479 * update will happen for the specified duration.
3480 */
3481static void
3482iwi_led_off(void *arg)
3483{
3484 struct iwi_softc *sc = arg;
3485 uint32_t v;
3486
3487 v = iwi_read_event(sc);
3488 v &= ~sc->sc_ledpin;
3489 iwi_write_event(sc, iwi_toggle_event(v));
3490 callout_reset(&sc->sc_ledtimer, sc->sc_ledoff, iwi_led_done, sc);
3491}
3492
3493/*
3494 * Blink the LED according to the specified on/off times.
3495 */
3496static void
3497iwi_led_blink(struct iwi_softc *sc, int on, int off)
3498{
3499 uint32_t v;
3500
3501 v = iwi_read_event(sc);
3502 v |= sc->sc_ledpin;
3503 iwi_write_event(sc, iwi_toggle_event(v));
3504 sc->sc_blinking = 1;
3505 sc->sc_ledoff = off;
3506 callout_reset(&sc->sc_ledtimer, on, iwi_led_off, sc);
3507}
3508
3509static void
3510iwi_led_event(struct iwi_softc *sc, int event)
3511{
3512 /* NB: on/off times from the Atheros NDIS driver, w/ permission */
3513 static const struct {
3514 u_int rate; /* tx/rx iwi rate */
3515 u_int16_t timeOn; /* LED on time (ms) */
3516 u_int16_t timeOff; /* LED off time (ms) */
3517 } blinkrates[] = {
3518 { IWI_RATE_OFDM54, 40, 10 },
3519 { IWI_RATE_OFDM48, 44, 11 },
3520 { IWI_RATE_OFDM36, 50, 13 },
3521 { IWI_RATE_OFDM24, 57, 14 },
3522 { IWI_RATE_OFDM18, 67, 16 },
3523 { IWI_RATE_OFDM12, 80, 20 },
3524 { IWI_RATE_DS11, 100, 25 },
3525 { IWI_RATE_OFDM9, 133, 34 },
3526 { IWI_RATE_OFDM6, 160, 40 },
3527 { IWI_RATE_DS5, 200, 50 },
3528 { 6, 240, 58 }, /* XXX 3Mb/s if it existed */
3529 { IWI_RATE_DS2, 267, 66 },
3530 { IWI_RATE_DS1, 400, 100 },
3531 { 0, 500, 130 }, /* unknown rate/polling */
3532 };
3533 uint32_t txrate;
3534 int j = 0; /* XXX silence compiler */
3535
3536 sc->sc_ledevent = ticks; /* time of last event */
3537 if (sc->sc_blinking) /* don't interrupt active blink */
3538 return;
3539 switch (event) {
3540 case IWI_LED_POLL:
3541 j = nitems(blinkrates)-1;
3542 break;
3543 case IWI_LED_TX:
3544 /* read current transmission rate from adapter */
3545 txrate = CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE);
3546 if (blinkrates[sc->sc_txrix].rate != txrate) {
3547 for (j = 0; j < nitems(blinkrates)-1; j++)
3548 if (blinkrates[j].rate == txrate)
3549 break;
3550 sc->sc_txrix = j;
3551 } else
3552 j = sc->sc_txrix;
3553 break;
3554 case IWI_LED_RX:
3555 if (blinkrates[sc->sc_rxrix].rate != sc->sc_rxrate) {
3556 for (j = 0; j < nitems(blinkrates)-1; j++)
3557 if (blinkrates[j].rate == sc->sc_rxrate)
3558 break;
3559 sc->sc_rxrix = j;
3560 } else
3561 j = sc->sc_rxrix;
3562 break;
3563 }
3564 /* XXX beware of overflow */
3565 iwi_led_blink(sc, (blinkrates[j].timeOn * hz) / 1000,
3566 (blinkrates[j].timeOff * hz) / 1000);
3567}
3568
3569static int
3570iwi_sysctl_softled(SYSCTL_HANDLER_ARGS)
3571{
3572 struct iwi_softc *sc = arg1;
3573 int softled = sc->sc_softled;
3574 int error;
3575
3576 error = sysctl_handle_int(oidp, &softled, 0, req);
3577 if (error || !req->newptr)
3578 return error;
3579 softled = (softled != 0);
3580 if (softled != sc->sc_softled) {
3581 if (softled) {
3582 uint32_t v = iwi_read_event(sc);
3583 v &= ~sc->sc_ledpin;
3584 iwi_write_event(sc, iwi_toggle_event(v));
3585 }
3586 sc->sc_softled = softled;
3587 }
3588 return 0;
3589}
3590
3591static void
3592iwi_ledattach(struct iwi_softc *sc)
3593{
3594 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
3595 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
3596
3597 sc->sc_blinking = 0;
3598 sc->sc_ledstate = 1;
3599 sc->sc_ledidle = (2700*hz)/1000; /* 2.7sec */
3600#if defined(__DragonFly__)
3601 callout_init_lk(&sc->sc_ledtimer, &sc->sc_lock);
3602#else
3603 callout_init_mtx(&sc->sc_ledtimer, &sc->sc_mtx, 0);
3604#endif
3605
3606 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
3607 "softled", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
3608 iwi_sysctl_softled, "I", "enable/disable software LED support");
3609 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
3610 "ledpin", CTLFLAG_RW, &sc->sc_ledpin, 0,
3611 "pin setting to turn activity LED on");
3612 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
3613 "ledidle", CTLFLAG_RW, &sc->sc_ledidle, 0,
3614 "idle time for inactivity LED (ticks)");
3615 /* XXX for debugging */
3616 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
3617 "nictype", CTLFLAG_RD, &sc->sc_nictype, 0,
3618 "NIC type from EEPROM");
3619
3620 sc->sc_ledpin = IWI_RST_LED_ACTIVITY;
3621 sc->sc_softled = 1;
3622
3623 sc->sc_nictype = (iwi_read_prom_word(sc, IWI_EEPROM_NIC) >> 8) & 0xff;
3624 if (sc->sc_nictype == 1) {
3625 /*
3626 * NB: led's are reversed.
3627 */
3628 sc->sc_ledpin = IWI_RST_LED_ASSOCIATED;
3629 }
3630}
3631
3632static void
3633iwi_scan_start(struct ieee80211com *ic)
3634{
3635 /* ignore */
3636}
3637
3638static void
3639iwi_set_channel(struct ieee80211com *ic)
3640{
3641 struct iwi_softc *sc = ic->ic_softc;
3642
3643 if (sc->fw_state == IWI_FW_IDLE)
3644 iwi_setcurchan(sc, ic->ic_curchan->ic_ieee);
3645}
3646
3647static void
3648iwi_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
3649{
3650 struct ieee80211vap *vap = ss->ss_vap;
3651 struct iwi_softc *sc = vap->iv_ic->ic_softc;
3652 IWI_LOCK_DECL;
3653
3654 IWI_LOCK(sc);
3655 if (iwi_scanchan(sc, maxdwell, 0))
3656 ieee80211_cancel_scan(vap);
3657 IWI_UNLOCK(sc);
3658}
3659
3660static void
3661iwi_scan_mindwell(struct ieee80211_scan_state *ss)
3662{
3663 /* NB: don't try to abort scan; wait for firmware to finish */
3664}
3665
3666static void
3667iwi_scan_end(struct ieee80211com *ic)
3668{
3669 struct iwi_softc *sc = ic->ic_softc;
3670 IWI_LOCK_DECL;
3671
3672 IWI_LOCK(sc);
3673 sc->flags &= ~IWI_FLAG_CHANNEL_SCAN;
3674 /* NB: make sure we're still scanning */
3675 if (sc->fw_state == IWI_FW_SCANNING)
3676 iwi_cmd(sc, IWI_CMD_ABORT_SCAN, NULL, 0);
3677 IWI_UNLOCK(sc);
3678}