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