if_iwi.c revision 1.107.2.2 1 /* $NetBSD: if_iwi.c,v 1.107.2.2 2020/04/08 14:08:09 martin Exp $ */
2 /* $OpenBSD: if_iwi.c,v 1.111 2010/11/15 19:11:57 damien Exp $ */
3
4 /*-
5 * Copyright (c) 2004-2008
6 * Damien Bergamini <damien.bergamini (at) free.fr>. All rights reserved.
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20
21 #include <sys/cdefs.h>
22 __KERNEL_RCSID(0, "$NetBSD: if_iwi.c,v 1.107.2.2 2020/04/08 14:08:09 martin Exp $");
23
24 /*-
25 * Intel(R) PRO/Wireless 2200BG/2225BG/2915ABG driver
26 * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
27 */
28
29
30 #include <sys/param.h>
31 #include <sys/sockio.h>
32 #include <sys/sysctl.h>
33 #include <sys/mbuf.h>
34 #include <sys/kernel.h>
35 #include <sys/socket.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/conf.h>
39 #include <sys/kauth.h>
40 #include <sys/proc.h>
41 #include <sys/cprng.h>
42
43 #include <sys/bus.h>
44 #include <machine/endian.h>
45 #include <sys/intr.h>
46
47 #include <dev/firmload.h>
48
49 #include <dev/pci/pcireg.h>
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/pcidevs.h>
52
53 #include <net/bpf.h>
54 #include <net/if.h>
55 #include <net/if_arp.h>
56 #include <net/if_dl.h>
57 #include <net/if_ether.h>
58 #include <net/if_media.h>
59 #include <net/if_types.h>
60
61 #include <net80211/ieee80211_var.h>
62 #include <net80211/ieee80211_radiotap.h>
63
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/in_var.h>
67 #include <netinet/ip.h>
68
69 #include <dev/pci/if_iwireg.h>
70 #include <dev/pci/if_iwivar.h>
71
72 #ifdef IWI_DEBUG
73 #define DPRINTF(x) if (iwi_debug > 0) printf x
74 #define DPRINTFN(n, x) if (iwi_debug >= (n)) printf x
75 int iwi_debug = 4;
76 #else
77 #define DPRINTF(x)
78 #define DPRINTFN(n, x)
79 #endif
80
81 /* Permit loading the Intel firmware */
82 static int iwi_accept_eula;
83
84 static int iwi_match(device_t, cfdata_t, void *);
85 static void iwi_attach(device_t, device_t, void *);
86 static int iwi_detach(device_t, int);
87
88 static int iwi_alloc_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *,
89 int);
90 static void iwi_reset_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
91 static void iwi_free_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
92 static int iwi_alloc_tx_ring(struct iwi_softc *, struct iwi_tx_ring *,
93 int, bus_size_t, bus_size_t);
94 static void iwi_reset_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
95 static void iwi_free_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
96 static struct mbuf *
97 iwi_alloc_rx_buf(struct iwi_softc *sc);
98 static int iwi_alloc_rx_ring(struct iwi_softc *, struct iwi_rx_ring *,
99 int);
100 static void iwi_reset_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
101 static void iwi_free_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
102
103 static struct ieee80211_node *iwi_node_alloc(struct ieee80211_node_table *);
104 static void iwi_node_free(struct ieee80211_node *);
105
106 static int iwi_cvtrate(int);
107 static int iwi_media_change(struct ifnet *);
108 static void iwi_media_status(struct ifnet *, struct ifmediareq *);
109 static int iwi_wme_update(struct ieee80211com *);
110 static uint16_t iwi_read_prom_word(struct iwi_softc *, uint8_t);
111 static int iwi_newstate(struct ieee80211com *, enum ieee80211_state, int);
112 static void iwi_fix_channel(struct ieee80211com *, struct mbuf *);
113 static void iwi_frame_intr(struct iwi_softc *, struct iwi_rx_data *, int,
114 struct iwi_frame *);
115 static void iwi_notification_intr(struct iwi_softc *, struct iwi_notif *);
116 static void iwi_cmd_intr(struct iwi_softc *);
117 static void iwi_rx_intr(struct iwi_softc *);
118 static void iwi_tx_intr(struct iwi_softc *, struct iwi_tx_ring *);
119 static int iwi_intr(void *);
120 static void iwi_softintr(void *);
121 static int iwi_cmd(struct iwi_softc *, uint8_t, void *, uint8_t, int);
122 static void iwi_write_ibssnode(struct iwi_softc *, const struct iwi_node *);
123 static int iwi_tx_start(struct ifnet *, struct mbuf *, struct ieee80211_node *,
124 int);
125 static void iwi_start(struct ifnet *);
126 static void iwi_watchdog(struct ifnet *);
127
128 static int iwi_alloc_unr(struct iwi_softc *);
129 static void iwi_free_unr(struct iwi_softc *, int);
130
131 static int iwi_get_table0(struct iwi_softc *, uint32_t *);
132
133 static int iwi_ioctl(struct ifnet *, u_long, void *);
134 static void iwi_stop_master(struct iwi_softc *);
135 static int iwi_reset(struct iwi_softc *);
136 static int iwi_load_ucode(struct iwi_softc *, void *, int);
137 static int iwi_load_firmware(struct iwi_softc *, void *, int);
138 static int iwi_cache_firmware(struct iwi_softc *);
139 static void iwi_free_firmware(struct iwi_softc *);
140 static int iwi_config(struct iwi_softc *);
141 static int iwi_set_chan(struct iwi_softc *, struct ieee80211_channel *);
142 static int iwi_scan(struct iwi_softc *);
143 static int iwi_auth_and_assoc(struct iwi_softc *);
144 static int iwi_init(struct ifnet *);
145 static void iwi_stop(struct ifnet *, int);
146 static int iwi_getrfkill(struct iwi_softc *);
147 static void iwi_led_set(struct iwi_softc *, uint32_t, int);
148 static void iwi_sysctlattach(struct iwi_softc *);
149
150 static inline uint8_t
151 MEM_READ_1(struct iwi_softc *sc, uint32_t addr)
152 {
153 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
154 return CSR_READ_1(sc, IWI_CSR_INDIRECT_DATA);
155 }
156
157 static inline uint32_t
158 MEM_READ_4(struct iwi_softc *sc, uint32_t addr)
159 {
160 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
161 return CSR_READ_4(sc, IWI_CSR_INDIRECT_DATA);
162 }
163
164 CFATTACH_DECL_NEW(iwi, sizeof (struct iwi_softc), iwi_match, iwi_attach,
165 iwi_detach, NULL);
166
167 static int
168 iwi_match(device_t parent, cfdata_t match, void *aux)
169 {
170 struct pci_attach_args *pa = aux;
171
172 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
173 return 0;
174
175 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2200BG ||
176 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2225BG ||
177 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_1 ||
178 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_2)
179 return 1;
180
181 return 0;
182 }
183
184 /* Base Address Register */
185 #define IWI_PCI_BAR0 0x10
186
187 static void
188 iwi_attach(device_t parent, device_t self, void *aux)
189 {
190 struct iwi_softc *sc = device_private(self);
191 struct ieee80211com *ic = &sc->sc_ic;
192 struct ifnet *ifp = &sc->sc_if;
193 struct pci_attach_args *pa = aux;
194 const char *intrstr;
195 bus_space_tag_t memt;
196 bus_space_handle_t memh;
197 pci_intr_handle_t ih;
198 pcireg_t data;
199 uint16_t val;
200 int error, i;
201 char intrbuf[PCI_INTRSTR_LEN];
202
203 sc->sc_dev = self;
204 sc->sc_pct = pa->pa_pc;
205 sc->sc_pcitag = pa->pa_tag;
206
207 pci_aprint_devinfo(pa, NULL);
208
209 /* clear unit numbers allocated to IBSS */
210 sc->sc_unr = 0;
211
212 /* power up chip */
213 if ((error = pci_activate(pa->pa_pc, pa->pa_tag, self,
214 NULL)) && error != EOPNOTSUPP) {
215 aprint_error_dev(self, "cannot activate %d\n", error);
216 return;
217 }
218
219 /* clear device specific PCI configuration register 0x41 */
220 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
221 data &= ~0x0000ff00;
222 pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data);
223
224
225 /* enable bus-mastering */
226 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
227 data |= PCI_COMMAND_MASTER_ENABLE;
228 pci_conf_write(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, data);
229
230 /* map the register window */
231 error = pci_mapreg_map(pa, IWI_PCI_BAR0, PCI_MAPREG_TYPE_MEM |
232 PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh, NULL, &sc->sc_sz);
233 if (error != 0) {
234 aprint_error_dev(self, "could not map memory space\n");
235 return;
236 }
237
238 sc->sc_st = memt;
239 sc->sc_sh = memh;
240 sc->sc_dmat = pa->pa_dmat;
241
242 /* disable interrupts */
243 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
244
245 sc->sc_soft_ih = softint_establish(SOFTINT_NET, iwi_softintr, sc);
246 if (sc->sc_soft_ih == NULL) {
247 aprint_error_dev(self, "could not establish softint\n");
248 return;
249 }
250
251 if (pci_intr_map(pa, &ih) != 0) {
252 softint_disestablish(sc->sc_soft_ih);
253 sc->sc_soft_ih = NULL;
254 aprint_error_dev(self, "could not map interrupt\n");
255 return;
256 }
257
258 intrstr = pci_intr_string(sc->sc_pct, ih, intrbuf, sizeof(intrbuf));
259 sc->sc_ih = pci_intr_establish_xname(sc->sc_pct, ih, IPL_NET, iwi_intr,
260 sc, device_xname(self));
261 if (sc->sc_ih == NULL) {
262 softint_disestablish(sc->sc_soft_ih);
263 sc->sc_soft_ih = NULL;
264 aprint_error_dev(self, "could not establish interrupt");
265 if (intrstr != NULL)
266 aprint_error(" at %s", intrstr);
267 aprint_error("\n");
268 return;
269 }
270 aprint_normal_dev(self, "interrupting at %s\n", intrstr);
271
272 if (iwi_reset(sc) != 0) {
273 pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
274 softint_disestablish(sc->sc_soft_ih);
275 sc->sc_soft_ih = NULL;
276 aprint_error_dev(self, "could not reset adapter\n");
277 return;
278 }
279
280 ic->ic_ifp = ifp;
281 ic->ic_wme.wme_update = iwi_wme_update;
282 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
283 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
284 ic->ic_state = IEEE80211_S_INIT;
285
286 sc->sc_fwname = "ipw2200-bss.fw";
287
288 /* set device capabilities */
289 ic->ic_caps =
290 IEEE80211_C_IBSS | /* IBSS mode supported */
291 IEEE80211_C_MONITOR | /* monitor mode supported */
292 IEEE80211_C_TXPMGT | /* tx power management */
293 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
294 IEEE80211_C_SHSLOT | /* short slot time supported */
295 IEEE80211_C_WPA | /* 802.11i */
296 IEEE80211_C_WME; /* 802.11e */
297
298 /* read MAC address from EEPROM */
299 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 0);
300 ic->ic_myaddr[0] = val & 0xff;
301 ic->ic_myaddr[1] = val >> 8;
302 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 1);
303 ic->ic_myaddr[2] = val & 0xff;
304 ic->ic_myaddr[3] = val >> 8;
305 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 2);
306 ic->ic_myaddr[4] = val & 0xff;
307 ic->ic_myaddr[5] = val >> 8;
308
309 aprint_verbose_dev(self, "802.11 address %s\n",
310 ether_sprintf(ic->ic_myaddr));
311
312 /* read the NIC type from EEPROM */
313 val = iwi_read_prom_word(sc, IWI_EEPROM_NIC_TYPE);
314 sc->nictype = val & 0xff;
315
316 DPRINTF(("%s: NIC type %d\n", device_xname(self), sc->nictype));
317
318 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_1 ||
319 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_2) {
320 /* set supported .11a rates (2915ABG only) */
321 ic->ic_sup_rates[IEEE80211_MODE_11A] = ieee80211_std_rateset_11a;
322
323 /* set supported .11a channels */
324 for (i = 36; i <= 64; i += 4) {
325 ic->ic_channels[i].ic_freq =
326 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
327 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
328 }
329 for (i = 149; i <= 165; i += 4) {
330 ic->ic_channels[i].ic_freq =
331 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
332 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
333 }
334 }
335
336 /* set supported .11b and .11g rates */
337 ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
338 ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_rateset_11g;
339
340 /* set supported .11b and .11g channels (1 through 14) */
341 for (i = 1; i <= 14; i++) {
342 ic->ic_channels[i].ic_freq =
343 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
344 ic->ic_channels[i].ic_flags =
345 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
346 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
347 }
348
349 ifp->if_softc = sc;
350 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
351 ifp->if_init = iwi_init;
352 ifp->if_stop = iwi_stop;
353 ifp->if_ioctl = iwi_ioctl;
354 ifp->if_start = iwi_start;
355 ifp->if_watchdog = iwi_watchdog;
356 IFQ_SET_READY(&ifp->if_snd);
357 memcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
358
359 error = if_initialize(ifp);
360 if (error != 0) {
361 ifp->if_softc = NULL; /* For iwi_detach() */
362 aprint_error_dev(sc->sc_dev, "if_initialize failed(%d)\n",
363 error);
364 goto fail;
365 }
366 ieee80211_ifattach(ic);
367 /* Use common softint-based if_input */
368 ifp->if_percpuq = if_percpuq_create(ifp);
369 if_register(ifp);
370
371 /* override default methods */
372 ic->ic_node_alloc = iwi_node_alloc;
373 sc->sc_node_free = ic->ic_node_free;
374 ic->ic_node_free = iwi_node_free;
375 /* override state transition machine */
376 sc->sc_newstate = ic->ic_newstate;
377 ic->ic_newstate = iwi_newstate;
378
379 /* XXX media locking needs revisiting */
380 mutex_init(&sc->sc_media_mtx, MUTEX_DEFAULT, IPL_SOFTNET);
381 ieee80211_media_init_with_lock(ic,
382 iwi_media_change, iwi_media_status, &sc->sc_media_mtx);
383
384 /*
385 * Allocate rings.
386 */
387 if (iwi_alloc_cmd_ring(sc, &sc->cmdq, IWI_CMD_RING_COUNT) != 0) {
388 aprint_error_dev(self, "could not allocate command ring\n");
389 goto fail;
390 }
391
392 error = iwi_alloc_tx_ring(sc, &sc->txq[0], IWI_TX_RING_COUNT,
393 IWI_CSR_TX1_RIDX, IWI_CSR_TX1_WIDX);
394 if (error != 0) {
395 aprint_error_dev(self, "could not allocate Tx ring 1\n");
396 goto fail;
397 }
398
399 error = iwi_alloc_tx_ring(sc, &sc->txq[1], IWI_TX_RING_COUNT,
400 IWI_CSR_TX2_RIDX, IWI_CSR_TX2_WIDX);
401 if (error != 0) {
402 aprint_error_dev(self, "could not allocate Tx ring 2\n");
403 goto fail;
404 }
405
406 error = iwi_alloc_tx_ring(sc, &sc->txq[2], IWI_TX_RING_COUNT,
407 IWI_CSR_TX3_RIDX, IWI_CSR_TX3_WIDX);
408 if (error != 0) {
409 aprint_error_dev(self, "could not allocate Tx ring 3\n");
410 goto fail;
411 }
412
413 error = iwi_alloc_tx_ring(sc, &sc->txq[3], IWI_TX_RING_COUNT,
414 IWI_CSR_TX4_RIDX, IWI_CSR_TX4_WIDX);
415 if (error != 0) {
416 aprint_error_dev(self, "could not allocate Tx ring 4\n");
417 goto fail;
418 }
419
420 if (iwi_alloc_rx_ring(sc, &sc->rxq, IWI_RX_RING_COUNT) != 0) {
421 aprint_error_dev(self, "could not allocate Rx ring\n");
422 goto fail;
423 }
424
425 bpf_attach2(ifp, DLT_IEEE802_11_RADIO,
426 sizeof(struct ieee80211_frame) + 64, &sc->sc_drvbpf);
427
428 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
429 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
430 sc->sc_rxtap.wr_ihdr.it_present = htole32(IWI_RX_RADIOTAP_PRESENT);
431
432 sc->sc_txtap_len = sizeof sc->sc_txtapu;
433 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
434 sc->sc_txtap.wt_ihdr.it_present = htole32(IWI_TX_RADIOTAP_PRESENT);
435
436 iwi_sysctlattach(sc);
437
438 if (pmf_device_register(self, NULL, NULL))
439 pmf_class_network_register(self, ifp);
440 else
441 aprint_error_dev(self, "couldn't establish power handler\n");
442
443 ieee80211_announce(ic);
444
445 return;
446
447 fail: iwi_detach(self, 0);
448 }
449
450 static int
451 iwi_detach(device_t self, int flags)
452 {
453 struct iwi_softc *sc = device_private(self);
454 struct ifnet *ifp = &sc->sc_if;
455
456 if (ifp->if_softc != NULL) {
457 pmf_device_deregister(self);
458 iwi_stop(ifp, 1);
459 iwi_free_firmware(sc);
460 ieee80211_ifdetach(&sc->sc_ic);
461 if_detach(ifp);
462 }
463
464 iwi_free_cmd_ring(sc, &sc->cmdq);
465 iwi_free_tx_ring(sc, &sc->txq[0]);
466 iwi_free_tx_ring(sc, &sc->txq[1]);
467 iwi_free_tx_ring(sc, &sc->txq[2]);
468 iwi_free_tx_ring(sc, &sc->txq[3]);
469 iwi_free_rx_ring(sc, &sc->rxq);
470
471 if (sc->sc_ih != NULL) {
472 pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
473 sc->sc_ih = NULL;
474 }
475
476 if (sc->sc_soft_ih != NULL) {
477 softint_disestablish(sc->sc_soft_ih);
478 sc->sc_soft_ih = NULL;
479 }
480
481 bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
482
483 return 0;
484 }
485
486 static int
487 iwi_alloc_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring,
488 int count)
489 {
490 int error, nsegs;
491
492 ring->count = count;
493 ring->queued = 0;
494 ring->cur = ring->next = 0;
495
496 /*
497 * Allocate and map command ring
498 */
499 error = bus_dmamap_create(sc->sc_dmat,
500 IWI_CMD_DESC_SIZE * count, 1,
501 IWI_CMD_DESC_SIZE * count, 0,
502 BUS_DMA_NOWAIT, &ring->desc_map);
503 if (error != 0) {
504 aprint_error_dev(sc->sc_dev,
505 "could not create command ring DMA map\n");
506 ring->desc_map = NULL;
507 goto fail;
508 }
509
510 error = bus_dmamem_alloc(sc->sc_dmat,
511 IWI_CMD_DESC_SIZE * count, PAGE_SIZE, 0,
512 &sc->cmdq.desc_seg, 1, &nsegs, BUS_DMA_NOWAIT);
513 if (error != 0) {
514 aprint_error_dev(sc->sc_dev,
515 "could not allocate command ring DMA memory\n");
516 goto fail;
517 }
518
519 error = bus_dmamem_map(sc->sc_dmat, &sc->cmdq.desc_seg, nsegs,
520 IWI_CMD_DESC_SIZE * count,
521 (void **)&sc->cmdq.desc, BUS_DMA_NOWAIT);
522 if (error != 0) {
523 aprint_error_dev(sc->sc_dev,
524 "could not map command ring DMA memory\n");
525 goto fail;
526 }
527
528 error = bus_dmamap_load(sc->sc_dmat, sc->cmdq.desc_map, sc->cmdq.desc,
529 IWI_CMD_DESC_SIZE * count, NULL,
530 BUS_DMA_NOWAIT);
531 if (error != 0) {
532 aprint_error_dev(sc->sc_dev,
533 "could not load command ring DMA map\n");
534 goto fail;
535 }
536
537 memset(sc->cmdq.desc, 0,
538 IWI_CMD_DESC_SIZE * count);
539
540 return 0;
541
542 fail: return error;
543 }
544
545 static void
546 iwi_reset_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
547 {
548 int i;
549
550 for (i = ring->next; i != ring->cur;) {
551 bus_dmamap_sync(sc->sc_dmat, sc->cmdq.desc_map,
552 i * IWI_CMD_DESC_SIZE, IWI_CMD_DESC_SIZE,
553 BUS_DMASYNC_POSTWRITE);
554
555 wakeup(&ring->desc[i]);
556 i = (i + 1) % ring->count;
557 }
558
559 ring->queued = 0;
560 ring->cur = ring->next = 0;
561 }
562
563 static void
564 iwi_free_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
565 {
566 if (ring->desc_map != NULL) {
567 if (ring->desc != NULL) {
568 bus_dmamap_unload(sc->sc_dmat, ring->desc_map);
569 bus_dmamem_unmap(sc->sc_dmat, (void *)ring->desc,
570 IWI_CMD_DESC_SIZE * ring->count);
571 bus_dmamem_free(sc->sc_dmat, &ring->desc_seg, 1);
572 }
573 bus_dmamap_destroy(sc->sc_dmat, ring->desc_map);
574 }
575 }
576
577 static int
578 iwi_alloc_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring,
579 int count, bus_size_t csr_ridx, bus_size_t csr_widx)
580 {
581 int i, error, nsegs;
582
583 ring->count = 0;
584 ring->queued = 0;
585 ring->cur = ring->next = 0;
586 ring->csr_ridx = csr_ridx;
587 ring->csr_widx = csr_widx;
588
589 /*
590 * Allocate and map Tx ring
591 */
592 error = bus_dmamap_create(sc->sc_dmat,
593 IWI_TX_DESC_SIZE * count, 1,
594 IWI_TX_DESC_SIZE * count, 0, BUS_DMA_NOWAIT,
595 &ring->desc_map);
596 if (error != 0) {
597 aprint_error_dev(sc->sc_dev,
598 "could not create tx ring DMA map\n");
599 ring->desc_map = NULL;
600 goto fail;
601 }
602
603 error = bus_dmamem_alloc(sc->sc_dmat,
604 IWI_TX_DESC_SIZE * count, PAGE_SIZE, 0,
605 &ring->desc_seg, 1, &nsegs, BUS_DMA_NOWAIT);
606 if (error != 0) {
607 aprint_error_dev(sc->sc_dev,
608 "could not allocate tx ring DMA memory\n");
609 goto fail;
610 }
611
612 error = bus_dmamem_map(sc->sc_dmat, &ring->desc_seg, nsegs,
613 IWI_TX_DESC_SIZE * count,
614 (void **)&ring->desc, BUS_DMA_NOWAIT);
615 if (error != 0) {
616 aprint_error_dev(sc->sc_dev,
617 "could not map tx ring DMA memory\n");
618 goto fail;
619 }
620
621 error = bus_dmamap_load(sc->sc_dmat, ring->desc_map, ring->desc,
622 IWI_TX_DESC_SIZE * count, NULL,
623 BUS_DMA_NOWAIT);
624 if (error != 0) {
625 aprint_error_dev(sc->sc_dev,
626 "could not load tx ring DMA map\n");
627 goto fail;
628 }
629
630 memset(ring->desc, 0, IWI_TX_DESC_SIZE * count);
631
632 ring->data = malloc(count * sizeof (struct iwi_tx_data), M_DEVBUF,
633 M_NOWAIT | M_ZERO);
634 if (ring->data == NULL) {
635 aprint_error_dev(sc->sc_dev, "could not allocate soft data\n");
636 error = ENOMEM;
637 goto fail;
638 }
639 ring->count = count;
640
641 /*
642 * Allocate Tx buffers DMA maps
643 */
644 for (i = 0; i < count; i++) {
645 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, IWI_MAX_NSEG,
646 MCLBYTES, 0, BUS_DMA_NOWAIT, &ring->data[i].map);
647 if (error != 0) {
648 aprint_error_dev(sc->sc_dev,
649 "could not create tx buf DMA map");
650 ring->data[i].map = NULL;
651 goto fail;
652 }
653 }
654 return 0;
655
656 fail: return error;
657 }
658
659 static void
660 iwi_reset_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
661 {
662 struct iwi_tx_data *data;
663 int i;
664
665 for (i = 0; i < ring->count; i++) {
666 data = &ring->data[i];
667
668 if (data->m != NULL) {
669 m_freem(data->m);
670 data->m = NULL;
671 }
672
673 if (data->map != NULL) {
674 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
675 data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
676 bus_dmamap_unload(sc->sc_dmat, data->map);
677 }
678
679 if (data->ni != NULL) {
680 ieee80211_free_node(data->ni);
681 data->ni = NULL;
682 }
683 }
684
685 ring->queued = 0;
686 ring->cur = ring->next = 0;
687 }
688
689 static void
690 iwi_free_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
691 {
692 int i;
693 struct iwi_tx_data *data;
694
695 if (ring->desc_map != NULL) {
696 if (ring->desc != NULL) {
697 bus_dmamap_unload(sc->sc_dmat, ring->desc_map);
698 bus_dmamem_unmap(sc->sc_dmat, (void *)ring->desc,
699 IWI_TX_DESC_SIZE * ring->count);
700 bus_dmamem_free(sc->sc_dmat, &ring->desc_seg, 1);
701 }
702 bus_dmamap_destroy(sc->sc_dmat, ring->desc_map);
703 }
704
705 for (i = 0; i < ring->count; i++) {
706 data = &ring->data[i];
707
708 if (data->m != NULL) {
709 m_freem(data->m);
710 }
711
712 if (data->map != NULL) {
713 bus_dmamap_unload(sc->sc_dmat, data->map);
714 bus_dmamap_destroy(sc->sc_dmat, data->map);
715 }
716 }
717 }
718
719 static int
720 iwi_alloc_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring, int count)
721 {
722 int i, error;
723
724 ring->count = 0;
725 ring->cur = 0;
726
727 ring->data = malloc(count * sizeof (struct iwi_rx_data), M_DEVBUF,
728 M_NOWAIT | M_ZERO);
729 if (ring->data == NULL) {
730 aprint_error_dev(sc->sc_dev, "could not allocate soft data\n");
731 error = ENOMEM;
732 goto fail;
733 }
734
735 ring->count = count;
736
737 /*
738 * Allocate and map Rx buffers
739 */
740 for (i = 0; i < count; i++) {
741
742 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
743 0, BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, &ring->data[i].map);
744 if (error != 0) {
745 aprint_error_dev(sc->sc_dev,
746 "could not create rx buf DMA map");
747 ring->data[i].map = NULL;
748 goto fail;
749 }
750
751 if ((ring->data[i].m = iwi_alloc_rx_buf(sc)) == NULL) {
752 error = ENOMEM;
753 goto fail;
754 }
755
756 error = bus_dmamap_load_mbuf(sc->sc_dmat, ring->data[i].map,
757 ring->data[i].m, BUS_DMA_READ | BUS_DMA_NOWAIT);
758 if (error != 0) {
759 aprint_error_dev(sc->sc_dev,
760 "could not load rx buffer DMA map\n");
761 goto fail;
762 }
763
764 bus_dmamap_sync(sc->sc_dmat, ring->data[i].map, 0,
765 ring->data[i].map->dm_mapsize, BUS_DMASYNC_PREREAD);
766 }
767
768 return 0;
769
770 fail: return error;
771 }
772
773 static void
774 iwi_reset_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
775 {
776 ring->cur = 0;
777 }
778
779 static void
780 iwi_free_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
781 {
782 int i;
783 struct iwi_rx_data *data;
784
785 for (i = 0; i < ring->count; i++) {
786 data = &ring->data[i];
787
788 if (data->m != NULL) {
789 m_freem(data->m);
790 }
791
792 if (data->map != NULL) {
793 bus_dmamap_unload(sc->sc_dmat, data->map);
794 bus_dmamap_destroy(sc->sc_dmat, data->map);
795 }
796
797 }
798 }
799
800 static struct ieee80211_node *
801 iwi_node_alloc(struct ieee80211_node_table *nt)
802 {
803 struct iwi_node *in;
804
805 in = malloc(sizeof (struct iwi_node), M_80211_NODE, M_NOWAIT | M_ZERO);
806 if (in == NULL)
807 return NULL;
808
809 in->in_station = -1;
810
811 return &in->in_node;
812 }
813
814 static int
815 iwi_alloc_unr(struct iwi_softc *sc)
816 {
817 int i;
818
819 for (i = 0; i < IWI_MAX_IBSSNODE - 1; i++)
820 if ((sc->sc_unr & (1 << i)) == 0) {
821 sc->sc_unr |= 1 << i;
822 return i;
823 }
824
825 return -1;
826 }
827
828 static void
829 iwi_free_unr(struct iwi_softc *sc, int r)
830 {
831
832 sc->sc_unr &= 1 << r;
833 }
834
835 static void
836 iwi_node_free(struct ieee80211_node *ni)
837 {
838 struct ieee80211com *ic = ni->ni_ic;
839 struct iwi_softc *sc = ic->ic_ifp->if_softc;
840 struct iwi_node *in = (struct iwi_node *)ni;
841
842 if (in->in_station != -1)
843 iwi_free_unr(sc, in->in_station);
844
845 sc->sc_node_free(ni);
846 }
847
848 static int
849 iwi_media_change(struct ifnet *ifp)
850 {
851 int error;
852
853 error = ieee80211_media_change(ifp);
854 if (error != ENETRESET)
855 return error;
856
857 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
858 iwi_init(ifp);
859
860 return 0;
861 }
862
863 /*
864 * Convert h/w rate code to IEEE rate code.
865 */
866 static int
867 iwi_cvtrate(int iwirate)
868 {
869 switch (iwirate) {
870 case IWI_RATE_DS1: return 2;
871 case IWI_RATE_DS2: return 4;
872 case IWI_RATE_DS5: return 11;
873 case IWI_RATE_DS11: return 22;
874 case IWI_RATE_OFDM6: return 12;
875 case IWI_RATE_OFDM9: return 18;
876 case IWI_RATE_OFDM12: return 24;
877 case IWI_RATE_OFDM18: return 36;
878 case IWI_RATE_OFDM24: return 48;
879 case IWI_RATE_OFDM36: return 72;
880 case IWI_RATE_OFDM48: return 96;
881 case IWI_RATE_OFDM54: return 108;
882 }
883 return 0;
884 }
885
886 /*
887 * The firmware automatically adapts the transmit speed. We report its current
888 * value here.
889 */
890 static void
891 iwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
892 {
893 struct iwi_softc *sc = ifp->if_softc;
894 struct ieee80211com *ic = &sc->sc_ic;
895 int rate;
896
897 imr->ifm_status = IFM_AVALID;
898 imr->ifm_active = IFM_IEEE80211;
899 if (ic->ic_state == IEEE80211_S_RUN)
900 imr->ifm_status |= IFM_ACTIVE;
901
902 /* read current transmission rate from adapter */
903 rate = iwi_cvtrate(CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE));
904 imr->ifm_active |= ieee80211_rate2media(ic, rate, ic->ic_curmode);
905
906 switch (ic->ic_opmode) {
907 case IEEE80211_M_STA:
908 break;
909
910 case IEEE80211_M_IBSS:
911 imr->ifm_active |= IFM_IEEE80211_ADHOC;
912 break;
913
914 case IEEE80211_M_MONITOR:
915 imr->ifm_active |= IFM_IEEE80211_MONITOR;
916 break;
917
918 case IEEE80211_M_AHDEMO:
919 case IEEE80211_M_HOSTAP:
920 /* should not get there */
921 break;
922 }
923 }
924
925 static int
926 iwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
927 {
928 struct iwi_softc *sc = ic->ic_ifp->if_softc;
929
930 DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
931 ieee80211_state_name[ic->ic_state],
932 ieee80211_state_name[nstate], sc->flags));
933
934 switch (nstate) {
935 case IEEE80211_S_SCAN:
936 if (sc->flags & IWI_FLAG_SCANNING)
937 break;
938
939 ieee80211_node_table_reset(&ic->ic_scan);
940 ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN;
941 sc->flags |= IWI_FLAG_SCANNING;
942 /* blink the led while scanning */
943 iwi_led_set(sc, IWI_LED_ASSOCIATED, 1);
944 iwi_scan(sc);
945 break;
946
947 case IEEE80211_S_AUTH:
948 iwi_auth_and_assoc(sc);
949 break;
950
951 case IEEE80211_S_RUN:
952 if (ic->ic_opmode == IEEE80211_M_IBSS &&
953 ic->ic_state == IEEE80211_S_SCAN)
954 iwi_auth_and_assoc(sc);
955 else if (ic->ic_opmode == IEEE80211_M_MONITOR)
956 iwi_set_chan(sc, ic->ic_ibss_chan);
957 break;
958 case IEEE80211_S_ASSOC:
959 iwi_led_set(sc, IWI_LED_ASSOCIATED, 0);
960 if (ic->ic_state == IEEE80211_S_AUTH)
961 break;
962 iwi_auth_and_assoc(sc);
963 break;
964
965 case IEEE80211_S_INIT:
966 sc->flags &= ~IWI_FLAG_SCANNING;
967 break;
968 }
969
970 return sc->sc_newstate(ic, nstate, arg);
971 }
972
973 /*
974 * WME parameters coming from IEEE 802.11e specification. These values are
975 * already declared in ieee80211_proto.c, but they are static so they can't
976 * be reused here.
977 */
978 static const struct wmeParams iwi_wme_cck_params[WME_NUM_AC] = {
979 { 0, 3, 5, 7, 0, 0, }, /* WME_AC_BE */
980 { 0, 3, 5, 10, 0, 0, }, /* WME_AC_BK */
981 { 0, 2, 4, 5, 188, 0, }, /* WME_AC_VI */
982 { 0, 2, 3, 4, 102, 0, }, /* WME_AC_VO */
983 };
984
985 static const struct wmeParams iwi_wme_ofdm_params[WME_NUM_AC] = {
986 { 0, 3, 4, 6, 0, 0, }, /* WME_AC_BE */
987 { 0, 3, 4, 10, 0, 0, }, /* WME_AC_BK */
988 { 0, 2, 3, 4, 94, 0, }, /* WME_AC_VI */
989 { 0, 2, 2, 3, 47, 0, }, /* WME_AC_VO */
990 };
991
992 static int
993 iwi_wme_update(struct ieee80211com *ic)
994 {
995 #define IWI_EXP2(v) htole16((1 << (v)) - 1)
996 #define IWI_USEC(v) htole16(IEEE80211_TXOP_TO_US(v))
997 struct iwi_softc *sc = ic->ic_ifp->if_softc;
998 struct iwi_wme_params wme[3];
999 const struct wmeParams *wmep;
1000 int ac;
1001
1002 /*
1003 * We shall not override firmware default WME values if WME is not
1004 * actually enabled.
1005 */
1006 if (!(ic->ic_flags & IEEE80211_F_WME))
1007 return 0;
1008
1009 for (ac = 0; ac < WME_NUM_AC; ac++) {
1010 /* set WME values for current operating mode */
1011 wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
1012 wme[0].aifsn[ac] = wmep->wmep_aifsn;
1013 wme[0].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1014 wme[0].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1015 wme[0].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1016 wme[0].acm[ac] = wmep->wmep_acm;
1017
1018 /* set WME values for CCK modulation */
1019 wmep = &iwi_wme_cck_params[ac];
1020 wme[1].aifsn[ac] = wmep->wmep_aifsn;
1021 wme[1].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1022 wme[1].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1023 wme[1].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1024 wme[1].acm[ac] = wmep->wmep_acm;
1025
1026 /* set WME values for OFDM modulation */
1027 wmep = &iwi_wme_ofdm_params[ac];
1028 wme[2].aifsn[ac] = wmep->wmep_aifsn;
1029 wme[2].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1030 wme[2].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1031 wme[2].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1032 wme[2].acm[ac] = wmep->wmep_acm;
1033 }
1034
1035 DPRINTF(("Setting WME parameters\n"));
1036 return iwi_cmd(sc, IWI_CMD_SET_WME_PARAMS, wme, sizeof wme, 1);
1037 #undef IWI_USEC
1038 #undef IWI_EXP2
1039 }
1040
1041 /*
1042 * Read 16 bits at address 'addr' from the serial EEPROM.
1043 */
1044 static uint16_t
1045 iwi_read_prom_word(struct iwi_softc *sc, uint8_t addr)
1046 {
1047 uint32_t tmp;
1048 uint16_t val;
1049 int n;
1050
1051 /* Clock C once before the first command */
1052 IWI_EEPROM_CTL(sc, 0);
1053 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1054 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1055 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1056
1057 /* Write start bit (1) */
1058 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1059 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1060
1061 /* Write READ opcode (10) */
1062 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1063 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1064 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1065 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1066
1067 /* Write address A7-A0 */
1068 for (n = 7; n >= 0; n--) {
1069 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1070 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D));
1071 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1072 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D) | IWI_EEPROM_C);
1073 }
1074
1075 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1076
1077 /* Read data Q15-Q0 */
1078 val = 0;
1079 for (n = 15; n >= 0; n--) {
1080 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1081 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1082 tmp = MEM_READ_4(sc, IWI_MEM_EEPROM_CTL);
1083 val |= ((tmp & IWI_EEPROM_Q) >> IWI_EEPROM_SHIFT_Q) << n;
1084 }
1085
1086 IWI_EEPROM_CTL(sc, 0);
1087
1088 /* Clear Chip Select and clock C */
1089 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1090 IWI_EEPROM_CTL(sc, 0);
1091 IWI_EEPROM_CTL(sc, IWI_EEPROM_C);
1092
1093 return val;
1094 }
1095
1096 /*
1097 * XXX: Hack to set the current channel to the value advertised in beacons or
1098 * probe responses. Only used during AP detection.
1099 */
1100 static void
1101 iwi_fix_channel(struct ieee80211com *ic, struct mbuf *m)
1102 {
1103 struct ieee80211_frame *wh;
1104 uint8_t subtype;
1105 uint8_t *frm, *efrm;
1106
1107 wh = mtod(m, struct ieee80211_frame *);
1108
1109 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
1110 return;
1111
1112 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1113
1114 if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
1115 subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1116 return;
1117
1118 frm = (uint8_t *)(wh + 1);
1119 efrm = mtod(m, uint8_t *) + m->m_len;
1120
1121 frm += 12; /* skip tstamp, bintval and capinfo fields */
1122 while (frm + 2 < efrm) {
1123 if (*frm == IEEE80211_ELEMID_DSPARMS) {
1124 #if IEEE80211_CHAN_MAX < 255
1125 if (frm[2] <= IEEE80211_CHAN_MAX)
1126 #endif
1127 ic->ic_curchan = &ic->ic_channels[frm[2]];
1128 }
1129
1130 frm += frm[1] + 2;
1131 }
1132 }
1133
1134 static struct mbuf *
1135 iwi_alloc_rx_buf(struct iwi_softc *sc)
1136 {
1137 struct mbuf *m;
1138
1139 MGETHDR(m, M_DONTWAIT, MT_DATA);
1140 if (m == NULL) {
1141 aprint_error_dev(sc->sc_dev, "could not allocate rx mbuf\n");
1142 return NULL;
1143 }
1144
1145 MCLGET(m, M_DONTWAIT);
1146 if (!(m->m_flags & M_EXT)) {
1147 aprint_error_dev(sc->sc_dev,
1148 "could not allocate rx mbuf cluster\n");
1149 m_freem(m);
1150 return NULL;
1151 }
1152
1153 m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
1154 return m;
1155 }
1156
1157 static void
1158 iwi_frame_intr(struct iwi_softc *sc, struct iwi_rx_data *data, int i,
1159 struct iwi_frame *frame)
1160 {
1161 struct ieee80211com *ic = &sc->sc_ic;
1162 struct ifnet *ifp = ic->ic_ifp;
1163 struct mbuf *m, *m_new;
1164 struct ieee80211_frame *wh;
1165 struct ieee80211_node *ni;
1166 int error, s;
1167
1168 DPRINTFN(5, ("received frame len=%u chan=%u rssi=%u\n",
1169 le16toh(frame->len), frame->chan, frame->rssi_dbm));
1170
1171 if (le16toh(frame->len) < sizeof (struct ieee80211_frame) ||
1172 le16toh(frame->len) > MCLBYTES) {
1173 DPRINTF(("%s: bad frame length\n", device_xname(sc->sc_dev)));
1174 if_statinc(ifp, if_ierrors);
1175 return;
1176 }
1177
1178 /*
1179 * Try to allocate a new mbuf for this ring element and
1180 * load it before processing the current mbuf. If the ring
1181 * element cannot be reloaded, drop the received packet
1182 * and reuse the old mbuf. In the unlikely case that
1183 * the old mbuf can't be reloaded either, explicitly panic.
1184 *
1185 * XXX Reorganize buffer by moving elements from the logical
1186 * end of the ring to the front instead of dropping.
1187 */
1188 if ((m_new = iwi_alloc_rx_buf(sc)) == NULL) {
1189 if_statinc(ifp, if_ierrors);
1190 return;
1191 }
1192
1193 bus_dmamap_unload(sc->sc_dmat, data->map);
1194
1195 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m_new,
1196 BUS_DMA_READ | BUS_DMA_NOWAIT);
1197 if (error != 0) {
1198 aprint_error_dev(sc->sc_dev,
1199 "could not load rx buf DMA map\n");
1200 m_freem(m_new);
1201 if_statinc(ifp, if_ierrors);
1202 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map,
1203 data->m, BUS_DMA_READ | BUS_DMA_NOWAIT);
1204 if (error)
1205 panic("%s: unable to remap rx buf",
1206 device_xname(sc->sc_dev));
1207 return;
1208 }
1209
1210 /*
1211 * New mbuf successfully loaded, update RX ring and continue
1212 * processing.
1213 */
1214 m = data->m;
1215 data->m = m_new;
1216 CSR_WRITE_4(sc, IWI_CSR_RX_BASE + i * 4, data->map->dm_segs[0].ds_addr);
1217
1218 /* Finalize mbuf */
1219 m_set_rcvif(m, ifp);
1220 m->m_pkthdr.len = m->m_len = sizeof (struct iwi_hdr) +
1221 sizeof (struct iwi_frame) + le16toh(frame->len);
1222
1223 m_adj(m, sizeof (struct iwi_hdr) + sizeof (struct iwi_frame));
1224
1225 s = splnet();
1226
1227 if (ic->ic_state == IEEE80211_S_SCAN)
1228 iwi_fix_channel(ic, m);
1229
1230 if (sc->sc_drvbpf != NULL) {
1231 struct iwi_rx_radiotap_header *tap = &sc->sc_rxtap;
1232
1233 tap->wr_flags = 0;
1234 tap->wr_rate = iwi_cvtrate(frame->rate);
1235 tap->wr_chan_freq =
1236 htole16(ic->ic_channels[frame->chan].ic_freq);
1237 tap->wr_chan_flags =
1238 htole16(ic->ic_channels[frame->chan].ic_flags);
1239 tap->wr_antsignal = frame->signal;
1240 tap->wr_antenna = frame->antenna;
1241
1242 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m, BPF_D_IN);
1243 }
1244 wh = mtod(m, struct ieee80211_frame *);
1245 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1246
1247 /* Send the frame to the upper layer */
1248 ieee80211_input(ic, m, ni, frame->rssi_dbm, 0);
1249
1250 /* node is no longer needed */
1251 ieee80211_free_node(ni);
1252
1253 splx(s);
1254 }
1255
1256 static void
1257 iwi_notification_intr(struct iwi_softc *sc, struct iwi_notif *notif)
1258 {
1259 struct ieee80211com *ic = &sc->sc_ic;
1260 struct iwi_notif_authentication *auth;
1261 struct iwi_notif_association *assoc;
1262 struct iwi_notif_beacon_state *beacon;
1263 int s;
1264
1265 switch (notif->type) {
1266 case IWI_NOTIF_TYPE_SCAN_CHANNEL:
1267 #ifdef IWI_DEBUG
1268 {
1269 struct iwi_notif_scan_channel *chan =
1270 (struct iwi_notif_scan_channel *)(notif + 1);
1271
1272 DPRINTFN(2, ("Scan of channel %u complete (%u)\n",
1273 ic->ic_channels[chan->nchan].ic_freq, chan->nchan));
1274 }
1275 #endif
1276 break;
1277
1278 case IWI_NOTIF_TYPE_SCAN_COMPLETE:
1279 #ifdef IWI_DEBUG
1280 {
1281 struct iwi_notif_scan_complete *scan =
1282 (struct iwi_notif_scan_complete *)(notif + 1);
1283
1284 DPRINTFN(2, ("Scan completed (%u, %u)\n", scan->nchan,
1285 scan->status));
1286 }
1287 #endif
1288
1289 /* monitor mode uses scan to set the channel ... */
1290 s = splnet();
1291 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1292 sc->flags &= ~IWI_FLAG_SCANNING;
1293 ieee80211_end_scan(ic);
1294 } else
1295 iwi_set_chan(sc, ic->ic_ibss_chan);
1296 splx(s);
1297 break;
1298
1299 case IWI_NOTIF_TYPE_AUTHENTICATION:
1300 auth = (struct iwi_notif_authentication *)(notif + 1);
1301
1302 DPRINTFN(2, ("Authentication (%u)\n", auth->state));
1303
1304 switch (auth->state) {
1305 case IWI_AUTH_SUCCESS:
1306 s = splnet();
1307 ieee80211_node_authorize(ic->ic_bss);
1308 ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1);
1309 splx(s);
1310 break;
1311
1312 case IWI_AUTH_FAIL:
1313 break;
1314
1315 case IWI_AUTH_SENT_1:
1316 case IWI_AUTH_RECV_2:
1317 case IWI_AUTH_SEQ1_PASS:
1318 break;
1319
1320 case IWI_AUTH_SEQ1_FAIL:
1321 break;
1322
1323 default:
1324 aprint_error_dev(sc->sc_dev,
1325 "unknown authentication state %u\n", auth->state);
1326 }
1327 break;
1328
1329 case IWI_NOTIF_TYPE_ASSOCIATION:
1330 assoc = (struct iwi_notif_association *)(notif + 1);
1331
1332 DPRINTFN(2, ("Association (%u, %u)\n", assoc->state,
1333 assoc->status));
1334
1335 switch (assoc->state) {
1336 case IWI_AUTH_SUCCESS:
1337 /* re-association, do nothing */
1338 break;
1339
1340 case IWI_ASSOC_SUCCESS:
1341 s = splnet();
1342 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1343 splx(s);
1344 break;
1345
1346 case IWI_ASSOC_FAIL:
1347 s = splnet();
1348 ieee80211_begin_scan(ic, 1);
1349 splx(s);
1350 break;
1351
1352 default:
1353 aprint_error_dev(sc->sc_dev,
1354 "unknown association state %u\n", assoc->state);
1355 }
1356 break;
1357
1358 case IWI_NOTIF_TYPE_BEACON:
1359 beacon = (struct iwi_notif_beacon_state *)(notif + 1);
1360
1361 if (beacon->state == IWI_BEACON_MISS) {
1362 DPRINTFN(5, ("%s: %u beacon(s) missed\n",
1363 device_xname(sc->sc_dev), le32toh(beacon->number)));
1364 }
1365 break;
1366
1367 case IWI_NOTIF_TYPE_FRAG_LENGTH:
1368 case IWI_NOTIF_TYPE_LINK_QUALITY:
1369 case IWI_NOTIF_TYPE_TGI_TX_KEY:
1370 case IWI_NOTIF_TYPE_CALIBRATION:
1371 case IWI_NOTIF_TYPE_NOISE:
1372 DPRINTFN(5, ("Notification (%u)\n", notif->type));
1373 break;
1374
1375 default:
1376 DPRINTF(("%s: unknown notification type %u flags 0x%x len %d\n",
1377 device_xname(sc->sc_dev), notif->type, notif->flags,
1378 le16toh(notif->len)));
1379 }
1380 }
1381
1382 static void
1383 iwi_cmd_intr(struct iwi_softc *sc)
1384 {
1385
1386 (void)CSR_READ_4(sc, IWI_CSR_CMD_RIDX);
1387
1388 bus_dmamap_sync(sc->sc_dmat, sc->cmdq.desc_map,
1389 sc->cmdq.next * IWI_CMD_DESC_SIZE, IWI_CMD_DESC_SIZE,
1390 BUS_DMASYNC_POSTWRITE);
1391
1392 wakeup(&sc->cmdq.desc[sc->cmdq.next]);
1393
1394 sc->cmdq.next = (sc->cmdq.next + 1) % sc->cmdq.count;
1395
1396 if (--sc->cmdq.queued > 0) {
1397 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX,
1398 (sc->cmdq.next + 1) % sc->cmdq.count);
1399 }
1400 }
1401
1402 static void
1403 iwi_rx_intr(struct iwi_softc *sc)
1404 {
1405 struct iwi_rx_data *data;
1406 struct iwi_hdr *hdr;
1407 uint32_t hw;
1408
1409 hw = CSR_READ_4(sc, IWI_CSR_RX_RIDX);
1410
1411 for (; sc->rxq.cur != hw;) {
1412 data = &sc->rxq.data[sc->rxq.cur];
1413
1414 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
1415 data->map->dm_mapsize, BUS_DMASYNC_POSTREAD);
1416
1417 hdr = mtod(data->m, struct iwi_hdr *);
1418
1419 switch (hdr->type) {
1420 case IWI_HDR_TYPE_FRAME:
1421 iwi_frame_intr(sc, data, sc->rxq.cur,
1422 (struct iwi_frame *)(hdr + 1));
1423 break;
1424
1425 case IWI_HDR_TYPE_NOTIF:
1426 iwi_notification_intr(sc,
1427 (struct iwi_notif *)(hdr + 1));
1428 break;
1429
1430 default:
1431 aprint_error_dev(sc->sc_dev, "unknown hdr type %u\n",
1432 hdr->type);
1433 }
1434
1435 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
1436 data->map->dm_mapsize, BUS_DMASYNC_PREREAD);
1437
1438 DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur));
1439
1440 sc->rxq.cur = (sc->rxq.cur + 1) % sc->rxq.count;
1441 }
1442
1443 /* Tell the firmware what we have processed */
1444 hw = (hw == 0) ? sc->rxq.count - 1 : hw - 1;
1445 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, hw);
1446 }
1447
1448 static void
1449 iwi_tx_intr(struct iwi_softc *sc, struct iwi_tx_ring *txq)
1450 {
1451 struct ifnet *ifp = &sc->sc_if;
1452 struct iwi_tx_data *data;
1453 uint32_t hw;
1454 int s;
1455
1456 s = splnet();
1457
1458 hw = CSR_READ_4(sc, txq->csr_ridx);
1459
1460 for (; txq->next != hw;) {
1461 data = &txq->data[txq->next];
1462
1463 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
1464 data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1465 bus_dmamap_unload(sc->sc_dmat, data->map);
1466 m_freem(data->m);
1467 data->m = NULL;
1468 ieee80211_free_node(data->ni);
1469 data->ni = NULL;
1470
1471 DPRINTFN(15, ("tx done idx=%u\n", txq->next));
1472
1473 if_statinc(ifp, if_opackets);
1474
1475 txq->queued--;
1476 txq->next = (txq->next + 1) % txq->count;
1477 }
1478
1479 sc->sc_tx_timer = 0;
1480
1481 if (txq->queued < txq->count - 8 - 8 && (ifp->if_flags & IFF_OACTIVE)) {
1482 ifp->if_flags &= ~IFF_OACTIVE;
1483
1484 /* Call start() since some buffer descriptors have been released */
1485 iwi_start(ifp); /* in softint */
1486 }
1487
1488 splx(s);
1489 }
1490
1491 static int
1492 iwi_intr(void *arg)
1493 {
1494 struct iwi_softc *sc = arg;
1495 uint32_t r;
1496
1497 if ((r = CSR_READ_4(sc, IWI_CSR_INTR)) == 0 || r == 0xffffffff)
1498 return 0;
1499
1500 /* Disable interrupts */
1501 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
1502
1503 softint_schedule(sc->sc_soft_ih);
1504 return 1;
1505 }
1506
1507 static void
1508 iwi_softintr(void *arg)
1509 {
1510 struct iwi_softc *sc = arg;
1511 uint32_t r;
1512 int s;
1513
1514 if ((r = CSR_READ_4(sc, IWI_CSR_INTR)) == 0 || r == 0xffffffff)
1515 goto out;
1516
1517 /* Acknowledge interrupts */
1518 CSR_WRITE_4(sc, IWI_CSR_INTR, r);
1519
1520 if (r & IWI_INTR_FATAL_ERROR) {
1521 aprint_error_dev(sc->sc_dev, "fatal error\n");
1522 s = splnet();
1523 sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP;
1524 iwi_stop(&sc->sc_if, 1);
1525 splx(s);
1526 return;
1527 }
1528
1529 if (r & IWI_INTR_FW_INITED) {
1530 if (!(r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)))
1531 wakeup(sc);
1532 }
1533
1534 if (r & IWI_INTR_RADIO_OFF) {
1535 DPRINTF(("radio transmitter off\n"));
1536 s = splnet();
1537 sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP;
1538 iwi_stop(&sc->sc_if, 1);
1539 splx(s);
1540 return;
1541 }
1542
1543 if (r & IWI_INTR_CMD_DONE)
1544 iwi_cmd_intr(sc);
1545
1546 if (r & IWI_INTR_TX1_DONE)
1547 iwi_tx_intr(sc, &sc->txq[0]);
1548
1549 if (r & IWI_INTR_TX2_DONE)
1550 iwi_tx_intr(sc, &sc->txq[1]);
1551
1552 if (r & IWI_INTR_TX3_DONE)
1553 iwi_tx_intr(sc, &sc->txq[2]);
1554
1555 if (r & IWI_INTR_TX4_DONE)
1556 iwi_tx_intr(sc, &sc->txq[3]);
1557
1558 if (r & IWI_INTR_RX_DONE)
1559 iwi_rx_intr(sc);
1560
1561 if (r & IWI_INTR_PARITY_ERROR)
1562 aprint_error_dev(sc->sc_dev, "parity error\n");
1563
1564 out:
1565 /* Re-enable interrupts */
1566 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
1567 }
1568
1569 static int
1570 iwi_cmd(struct iwi_softc *sc, uint8_t type, void *data, uint8_t len,
1571 int async)
1572 {
1573 struct iwi_cmd_desc *desc;
1574
1575 desc = &sc->cmdq.desc[sc->cmdq.cur];
1576
1577 desc->hdr.type = IWI_HDR_TYPE_COMMAND;
1578 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1579 desc->type = type;
1580 desc->len = len;
1581 memcpy(desc->data, data, len);
1582
1583 bus_dmamap_sync(sc->sc_dmat, sc->cmdq.desc_map,
1584 sc->cmdq.cur * IWI_CMD_DESC_SIZE,
1585 IWI_CMD_DESC_SIZE, BUS_DMASYNC_PREWRITE);
1586
1587 DPRINTFN(2, ("sending command idx=%u type=%u len=%u async=%d\n",
1588 sc->cmdq.cur, type, len, async));
1589
1590 sc->cmdq.cur = (sc->cmdq.cur + 1) % sc->cmdq.count;
1591
1592 if (++sc->cmdq.queued == 1)
1593 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
1594
1595 return async ? 0 : tsleep(desc, 0, "iwicmd", hz);
1596 }
1597
1598 static void
1599 iwi_write_ibssnode(struct iwi_softc *sc, const struct iwi_node *in)
1600 {
1601 struct iwi_ibssnode node;
1602
1603 /* write node information into NIC memory */
1604 memset(&node, 0, sizeof node);
1605 IEEE80211_ADDR_COPY(node.bssid, in->in_node.ni_macaddr);
1606
1607 CSR_WRITE_REGION_1(sc,
1608 IWI_CSR_NODE_BASE + in->in_station * sizeof node,
1609 (uint8_t *)&node, sizeof node);
1610 }
1611
1612 static int
1613 iwi_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni,
1614 int ac)
1615 {
1616 struct iwi_softc *sc = ifp->if_softc;
1617 struct ieee80211com *ic = &sc->sc_ic;
1618 struct iwi_node *in = (struct iwi_node *)ni;
1619 struct ieee80211_frame *wh;
1620 struct ieee80211_key *k;
1621 const struct chanAccParams *cap;
1622 struct iwi_tx_ring *txq = &sc->txq[ac];
1623 struct iwi_tx_data *data;
1624 struct iwi_tx_desc *desc;
1625 struct mbuf *mnew;
1626 int error, hdrlen, i, noack = 0;
1627
1628 wh = mtod(m0, struct ieee80211_frame *);
1629
1630 if (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_QOS) {
1631 hdrlen = sizeof (struct ieee80211_qosframe);
1632 cap = &ic->ic_wme.wme_chanParams;
1633 noack = cap->cap_wmeParams[ac].wmep_noackPolicy;
1634 } else
1635 hdrlen = sizeof (struct ieee80211_frame);
1636
1637 /*
1638 * This is only used in IBSS mode where the firmware expect an index
1639 * in a h/w table instead of a destination address.
1640 */
1641 if (ic->ic_opmode == IEEE80211_M_IBSS && in->in_station == -1) {
1642 in->in_station = iwi_alloc_unr(sc);
1643
1644 if (in->in_station == -1) { /* h/w table is full */
1645 m_freem(m0);
1646 ieee80211_free_node(ni);
1647 if_statinc(ifp, if_oerrors);
1648 return 0;
1649 }
1650 iwi_write_ibssnode(sc, in);
1651 }
1652
1653 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1654 k = ieee80211_crypto_encap(ic, ni, m0);
1655 if (k == NULL) {
1656 m_freem(m0);
1657 return ENOBUFS;
1658 }
1659
1660 /* packet header may have moved, reset our local pointer */
1661 wh = mtod(m0, struct ieee80211_frame *);
1662 }
1663
1664 if (sc->sc_drvbpf != NULL) {
1665 struct iwi_tx_radiotap_header *tap = &sc->sc_txtap;
1666
1667 tap->wt_flags = 0;
1668 tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
1669 tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
1670
1671 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0, BPF_D_OUT);
1672 }
1673
1674 data = &txq->data[txq->cur];
1675 desc = &txq->desc[txq->cur];
1676
1677 /* save and trim IEEE802.11 header */
1678 m_copydata(m0, 0, hdrlen, (void *)&desc->wh);
1679 m_adj(m0, hdrlen);
1680
1681 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
1682 BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1683 if (error != 0 && error != EFBIG) {
1684 aprint_error_dev(sc->sc_dev, "could not map mbuf (error %d)\n",
1685 error);
1686 m_freem(m0);
1687 return error;
1688 }
1689 if (error != 0) {
1690 /* too many fragments, linearize */
1691
1692 MGETHDR(mnew, M_DONTWAIT, MT_DATA);
1693 if (mnew == NULL) {
1694 m_freem(m0);
1695 return ENOMEM;
1696 }
1697
1698 m_copy_pkthdr(mnew, m0);
1699
1700 /* If the data won't fit in the header, get a cluster */
1701 if (m0->m_pkthdr.len > MHLEN) {
1702 MCLGET(mnew, M_DONTWAIT);
1703 if (!(mnew->m_flags & M_EXT)) {
1704 m_freem(m0);
1705 m_freem(mnew);
1706 return ENOMEM;
1707 }
1708 }
1709 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(mnew, void *));
1710 m_freem(m0);
1711 mnew->m_len = mnew->m_pkthdr.len;
1712 m0 = mnew;
1713
1714 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
1715 BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1716 if (error != 0) {
1717 aprint_error_dev(sc->sc_dev,
1718 "could not map mbuf (error %d)\n", error);
1719 m_freem(m0);
1720 return error;
1721 }
1722 }
1723
1724 data->m = m0;
1725 data->ni = ni;
1726
1727 desc->hdr.type = IWI_HDR_TYPE_DATA;
1728 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1729 desc->station =
1730 (ic->ic_opmode == IEEE80211_M_IBSS) ? in->in_station : 0;
1731 desc->cmd = IWI_DATA_CMD_TX;
1732 desc->len = htole16(m0->m_pkthdr.len);
1733 desc->flags = 0;
1734 desc->xflags = 0;
1735
1736 if (!noack && !IEEE80211_IS_MULTICAST(desc->wh.i_addr1))
1737 desc->flags |= IWI_DATA_FLAG_NEED_ACK;
1738
1739 #if 0
1740 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
1741 desc->wh.i_fc[1] |= IEEE80211_FC1_WEP;
1742 desc->wep_txkey = ic->ic_crypto.cs_def_txkey;
1743 } else
1744 #endif
1745 desc->flags |= IWI_DATA_FLAG_NO_WEP;
1746
1747 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1748 desc->flags |= IWI_DATA_FLAG_SHPREAMBLE;
1749
1750 if (desc->wh.i_fc[0] & IEEE80211_FC0_SUBTYPE_QOS)
1751 desc->xflags |= IWI_DATA_XFLAG_QOS;
1752
1753 if (ic->ic_curmode == IEEE80211_MODE_11B)
1754 desc->xflags |= IWI_DATA_XFLAG_CCK;
1755
1756 desc->nseg = htole32(data->map->dm_nsegs);
1757 for (i = 0; i < data->map->dm_nsegs; i++) {
1758 desc->seg_addr[i] = htole32(data->map->dm_segs[i].ds_addr);
1759 desc->seg_len[i] = htole16(data->map->dm_segs[i].ds_len);
1760 }
1761
1762 bus_dmamap_sync(sc->sc_dmat, txq->desc_map,
1763 txq->cur * IWI_TX_DESC_SIZE,
1764 IWI_TX_DESC_SIZE, BUS_DMASYNC_PREWRITE);
1765
1766 bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize,
1767 BUS_DMASYNC_PREWRITE);
1768
1769 DPRINTFN(5, ("sending data frame txq=%u idx=%u len=%u nseg=%u\n",
1770 ac, txq->cur, le16toh(desc->len), le32toh(desc->nseg)));
1771
1772 /* Inform firmware about this new packet */
1773 txq->queued++;
1774 txq->cur = (txq->cur + 1) % txq->count;
1775 CSR_WRITE_4(sc, txq->csr_widx, txq->cur);
1776
1777 return 0;
1778 }
1779
1780 static void
1781 iwi_start(struct ifnet *ifp)
1782 {
1783 struct iwi_softc *sc = ifp->if_softc;
1784 struct ieee80211com *ic = &sc->sc_ic;
1785 struct mbuf *m0;
1786 struct ether_header *eh;
1787 struct ieee80211_node *ni;
1788 int ac;
1789
1790 if (ic->ic_state != IEEE80211_S_RUN)
1791 return;
1792
1793 for (;;) {
1794 IFQ_DEQUEUE(&ifp->if_snd, m0);
1795 if (m0 == NULL)
1796 break;
1797
1798 if (m0->m_len < sizeof (struct ether_header) &&
1799 (m0 = m_pullup(m0, sizeof (struct ether_header))) == NULL) {
1800 if_statinc(ifp, if_oerrors);
1801 continue;
1802 }
1803
1804 eh = mtod(m0, struct ether_header *);
1805 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1806 if (ni == NULL) {
1807 m_freem(m0);
1808 if_statinc(ifp, if_oerrors);
1809 continue;
1810 }
1811
1812 /* classify mbuf so we can find which tx ring to use */
1813 if (ieee80211_classify(ic, m0, ni) != 0) {
1814 m_freem(m0);
1815 ieee80211_free_node(ni);
1816 if_statinc(ifp, if_oerrors);
1817 continue;
1818 }
1819
1820 /* no QoS encapsulation for EAPOL frames */
1821 ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ?
1822 M_WME_GETAC(m0) : WME_AC_BE;
1823
1824 if (sc->txq[ac].queued > sc->txq[ac].count - 8) {
1825 /* there is no place left in this ring */
1826 IFQ_LOCK(&ifp->if_snd);
1827 IF_PREPEND(&ifp->if_snd, m0);
1828 IFQ_UNLOCK(&ifp->if_snd);
1829 ifp->if_flags |= IFF_OACTIVE;
1830 break;
1831 }
1832
1833 bpf_mtap(ifp, m0, BPF_D_OUT);
1834
1835 m0 = ieee80211_encap(ic, m0, ni);
1836 if (m0 == NULL) {
1837 ieee80211_free_node(ni);
1838 if_statinc(ifp, if_oerrors);
1839 continue;
1840 }
1841
1842 bpf_mtap3(ic->ic_rawbpf, m0, BPF_D_OUT);
1843
1844 if (iwi_tx_start(ifp, m0, ni, ac) != 0) {
1845 ieee80211_free_node(ni);
1846 if_statinc(ifp, if_oerrors);
1847 break;
1848 }
1849
1850 /* start watchdog timer */
1851 sc->sc_tx_timer = 5;
1852 ifp->if_timer = 1;
1853 }
1854 }
1855
1856 static void
1857 iwi_watchdog(struct ifnet *ifp)
1858 {
1859 struct iwi_softc *sc = ifp->if_softc;
1860
1861 ifp->if_timer = 0;
1862
1863 if (sc->sc_tx_timer > 0) {
1864 if (--sc->sc_tx_timer == 0) {
1865 aprint_error_dev(sc->sc_dev, "device timeout\n");
1866 if_statinc(ifp, if_oerrors);
1867 ifp->if_flags &= ~IFF_UP;
1868 iwi_stop(ifp, 1);
1869 return;
1870 }
1871 ifp->if_timer = 1;
1872 }
1873
1874 ieee80211_watchdog(&sc->sc_ic);
1875 }
1876
1877 static int
1878 iwi_get_table0(struct iwi_softc *sc, uint32_t *tbl)
1879 {
1880 uint32_t size, buf[128];
1881
1882 if (!(sc->flags & IWI_FLAG_FW_INITED)) {
1883 memset(buf, 0, sizeof buf);
1884 return copyout(buf, tbl, sizeof buf);
1885 }
1886
1887 size = uimin(CSR_READ_4(sc, IWI_CSR_TABLE0_SIZE), 128 - 1);
1888 CSR_READ_REGION_4(sc, IWI_CSR_TABLE0_BASE, &buf[1], size);
1889
1890 return copyout(buf, tbl, sizeof buf);
1891 }
1892
1893 static int
1894 iwi_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1895 {
1896 #define IS_RUNNING(ifp) \
1897 ((ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING))
1898
1899 struct iwi_softc *sc = ifp->if_softc;
1900 struct ieee80211com *ic = &sc->sc_ic;
1901 struct ifreq *ifr = (struct ifreq *)data;
1902 int s, error = 0;
1903 int val;
1904
1905 s = splnet();
1906
1907 switch (cmd) {
1908 case SIOCSIFFLAGS:
1909 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1910 break;
1911 if (ifp->if_flags & IFF_UP) {
1912 if (!(ifp->if_flags & IFF_RUNNING))
1913 iwi_init(ifp);
1914 } else {
1915 if (ifp->if_flags & IFF_RUNNING)
1916 iwi_stop(ifp, 1);
1917 }
1918 break;
1919
1920 case SIOCADDMULTI:
1921 case SIOCDELMULTI:
1922 /* XXX no h/w multicast filter? --dyoung */
1923 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
1924 /* setup multicast filter, etc */
1925 error = 0;
1926 }
1927 break;
1928
1929 case SIOCGTABLE0:
1930 error = iwi_get_table0(sc, (uint32_t *)ifr->ifr_data);
1931 break;
1932
1933 case SIOCGRADIO:
1934 val = !iwi_getrfkill(sc);
1935 error = copyout(&val, (int *)ifr->ifr_data, sizeof val);
1936 break;
1937
1938 case SIOCSIFMEDIA:
1939 if (ifr->ifr_media & IFM_IEEE80211_ADHOC) {
1940 sc->sc_fwname = "ipw2200-ibss.fw";
1941 } else if (ifr->ifr_media & IFM_IEEE80211_MONITOR) {
1942 sc->sc_fwname = "ipw2200-sniffer.fw";
1943 } else {
1944 sc->sc_fwname = "ipw2200-bss.fw";
1945 }
1946 error = iwi_cache_firmware(sc);
1947 if (error)
1948 break;
1949
1950 /* FALLTHROUGH */
1951 default:
1952 error = ieee80211_ioctl(&sc->sc_ic, cmd, data);
1953
1954 if (error == ENETRESET) {
1955 if (IS_RUNNING(ifp) &&
1956 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
1957 iwi_init(ifp);
1958 error = 0;
1959 }
1960 }
1961
1962 splx(s);
1963 return error;
1964 #undef IS_RUNNING
1965 }
1966
1967 static void
1968 iwi_stop_master(struct iwi_softc *sc)
1969 {
1970 int ntries;
1971
1972 /* Disable interrupts */
1973 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
1974
1975 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER);
1976 for (ntries = 0; ntries < 5; ntries++) {
1977 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
1978 break;
1979 DELAY(10);
1980 }
1981 if (ntries == 5)
1982 aprint_error_dev(sc->sc_dev, "timeout waiting for master\n");
1983
1984 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
1985 IWI_RST_PRINCETON_RESET);
1986
1987 sc->flags &= ~IWI_FLAG_FW_INITED;
1988 }
1989
1990 static int
1991 iwi_reset(struct iwi_softc *sc)
1992 {
1993 int i, ntries;
1994
1995 iwi_stop_master(sc);
1996
1997 /* Move adapter to D0 state */
1998 CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
1999 IWI_CTL_INIT);
2000
2001 /* Initialize Phase-Locked Level (PLL) */
2002 CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST);
2003
2004 /* Wait for clock stabilization */
2005 for (ntries = 0; ntries < 1000; ntries++) {
2006 if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY)
2007 break;
2008 DELAY(200);
2009 }
2010 if (ntries == 1000) {
2011 aprint_error_dev(sc->sc_dev,
2012 "timeout waiting for clock stabilization\n");
2013 return ETIMEDOUT;
2014 }
2015
2016 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
2017 IWI_RST_SW_RESET);
2018
2019 DELAY(10);
2020
2021 CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
2022 IWI_CTL_INIT);
2023
2024 /* Clear NIC memory */
2025 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0);
2026 for (i = 0; i < 0xc000; i++)
2027 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
2028
2029 return 0;
2030 }
2031
2032 static int
2033 iwi_load_ucode(struct iwi_softc *sc, void *uc, int size)
2034 {
2035 uint16_t *w;
2036 int ntries, i;
2037
2038 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
2039 IWI_RST_STOP_MASTER);
2040 for (ntries = 0; ntries < 5; ntries++) {
2041 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
2042 break;
2043 DELAY(10);
2044 }
2045 if (ntries == 5) {
2046 aprint_error_dev(sc->sc_dev, "timeout waiting for master\n");
2047 return ETIMEDOUT;
2048 }
2049
2050 MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
2051 DELAY(5000);
2052 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) &
2053 ~IWI_RST_PRINCETON_RESET);
2054 DELAY(5000);
2055 MEM_WRITE_4(sc, 0x3000e0, 0);
2056 DELAY(1000);
2057 MEM_WRITE_4(sc, 0x300004, 1);
2058 DELAY(1000);
2059 MEM_WRITE_4(sc, 0x300004, 0);
2060 DELAY(1000);
2061 MEM_WRITE_1(sc, 0x200000, 0x00);
2062 MEM_WRITE_1(sc, 0x200000, 0x40);
2063 DELAY(1000);
2064
2065 /* Adapter is buggy, we must set the address for each word */
2066 for (w = uc; size > 0; w++, size -= 2)
2067 MEM_WRITE_2(sc, 0x200010, htole16(*w));
2068
2069 MEM_WRITE_1(sc, 0x200000, 0x00);
2070 MEM_WRITE_1(sc, 0x200000, 0x80);
2071
2072 /* Wait until we get a response in the uc queue */
2073 for (ntries = 0; ntries < 100; ntries++) {
2074 if (MEM_READ_1(sc, 0x200000) & 1)
2075 break;
2076 DELAY(100);
2077 }
2078 if (ntries == 100) {
2079 aprint_error_dev(sc->sc_dev,
2080 "timeout waiting for ucode to initialize\n");
2081 return ETIMEDOUT;
2082 }
2083
2084 /* Empty the uc queue or the firmware will not initialize properly */
2085 for (i = 0; i < 7; i++)
2086 MEM_READ_4(sc, 0x200004);
2087
2088 MEM_WRITE_1(sc, 0x200000, 0x00);
2089
2090 return 0;
2091 }
2092
2093 /* macro to handle unaligned little endian data in firmware image */
2094 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
2095 static int
2096 iwi_load_firmware(struct iwi_softc *sc, void *fw, int size)
2097 {
2098 bus_dmamap_t map;
2099 u_char *p, *end;
2100 uint32_t sentinel, ctl, sum;
2101 uint32_t cs, sl, cd, cl;
2102 int ntries, nsegs, error;
2103 int sn;
2104
2105 nsegs = atop((vaddr_t)fw+size-1) - atop((vaddr_t)fw) + 1;
2106
2107 /* Create a DMA map for the firmware image */
2108 error = bus_dmamap_create(sc->sc_dmat, size, nsegs, size, 0,
2109 BUS_DMA_NOWAIT, &map);
2110 if (error != 0) {
2111 aprint_error_dev(sc->sc_dev,
2112 "could not create firmware DMA map\n");
2113 map = NULL;
2114 goto fail1;
2115 }
2116
2117 error = bus_dmamap_load(sc->sc_dmat, map, fw, size, NULL,
2118 BUS_DMA_NOWAIT | BUS_DMA_WRITE);
2119 if (error != 0) {
2120 aprint_error_dev(sc->sc_dev, "could not load fw dma map(%d)\n",
2121 error);
2122 goto fail2;
2123 }
2124
2125 /* Make sure the adapter will get up-to-date values */
2126 bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_PREWRITE);
2127
2128 /* Tell the adapter where the command blocks are stored */
2129 MEM_WRITE_4(sc, 0x3000a0, 0x27000);
2130
2131 /*
2132 * Store command blocks into adapter's internal memory using register
2133 * indirections. The adapter will read the firmware image through DMA
2134 * using information stored in command blocks.
2135 */
2136 p = fw;
2137 end = p + size;
2138 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000);
2139
2140 sn = 0;
2141 sl = cl = 0;
2142 cs = cd = 0;
2143 while (p < end) {
2144 if (sl == 0) {
2145 cs = map->dm_segs[sn].ds_addr;
2146 sl = map->dm_segs[sn].ds_len;
2147 sn++;
2148 }
2149 if (cl == 0) {
2150 cd = GETLE32(p); p += 4; cs += 4; sl -= 4;
2151 cl = GETLE32(p); p += 4; cs += 4; sl -= 4;
2152 }
2153 while (sl > 0 && cl > 0) {
2154 int len = uimin(cl, sl);
2155
2156 sl -= len;
2157 cl -= len;
2158 p += len;
2159
2160 while (len > 0) {
2161 int mlen = uimin(len, IWI_CB_MAXDATALEN);
2162
2163 ctl = IWI_CB_DEFAULT_CTL | mlen;
2164 sum = ctl ^ cs ^ cd;
2165
2166 /* Write a command block */
2167 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, ctl);
2168 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, cs);
2169 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, cd);
2170 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum);
2171
2172 cs += mlen;
2173 cd += mlen;
2174 len -= mlen;
2175 }
2176 }
2177 }
2178
2179 /* Write a fictive final command block (sentinel) */
2180 sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR);
2181 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
2182
2183 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) &
2184 ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER));
2185
2186 /* Tell the adapter to start processing command blocks */
2187 MEM_WRITE_4(sc, 0x3000a4, 0x540100);
2188
2189 /* Wait until the adapter has processed all command blocks */
2190 for (ntries = 0; ntries < 400; ntries++) {
2191 if (MEM_READ_4(sc, 0x3000d0) >= sentinel)
2192 break;
2193 DELAY(100);
2194 }
2195 if (ntries == 400) {
2196 aprint_error_dev(sc->sc_dev, "timeout processing cb\n");
2197 error = ETIMEDOUT;
2198 goto fail3;
2199 }
2200
2201 /* We're done with command blocks processing */
2202 MEM_WRITE_4(sc, 0x3000a4, 0x540c00);
2203
2204 /* Allow interrupts so we know when the firmware is inited */
2205 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
2206
2207 /* Tell the adapter to initialize the firmware */
2208 CSR_WRITE_4(sc, IWI_CSR_RST, 0);
2209 CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
2210 IWI_CTL_ALLOW_STANDBY);
2211
2212 /* Wait at most one second for firmware initialization to complete */
2213 if ((error = tsleep(sc, 0, "iwiinit", hz)) != 0) {
2214 aprint_error_dev(sc->sc_dev,
2215 "timeout waiting for firmware initialization to complete\n");
2216 goto fail3;
2217 }
2218
2219 fail3:
2220 bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_POSTWRITE);
2221 bus_dmamap_unload(sc->sc_dmat, map);
2222 fail2:
2223 if (map != NULL)
2224 bus_dmamap_destroy(sc->sc_dmat, map);
2225
2226 fail1:
2227 return error;
2228 }
2229
2230 /*
2231 * Store firmware into kernel memory so we can download it when we need to,
2232 * e.g when the adapter wakes up from suspend mode.
2233 */
2234 static int
2235 iwi_cache_firmware(struct iwi_softc *sc)
2236 {
2237 struct iwi_firmware *kfw = &sc->fw;
2238 firmware_handle_t fwh;
2239 struct iwi_firmware_hdr *hdr;
2240 off_t size;
2241 char *fw;
2242 int error;
2243
2244 if (iwi_accept_eula == 0) {
2245 aprint_error_dev(sc->sc_dev,
2246 "EULA not accepted; please see the iwi(4) man page.\n");
2247 return EPERM;
2248 }
2249
2250 iwi_free_firmware(sc);
2251 error = firmware_open("if_iwi", sc->sc_fwname, &fwh);
2252 if (error != 0) {
2253 aprint_error_dev(sc->sc_dev, "firmware_open failed\n");
2254 goto fail1;
2255 }
2256
2257 size = firmware_get_size(fwh);
2258 if (size < sizeof(struct iwi_firmware_hdr)) {
2259 aprint_error_dev(sc->sc_dev, "image '%s' has no header\n",
2260 sc->sc_fwname);
2261 error = EIO;
2262 goto fail1;
2263 }
2264 sc->sc_blobsize = size;
2265
2266 sc->sc_blob = firmware_malloc(size);
2267 if (sc->sc_blob == NULL) {
2268 error = ENOMEM;
2269 firmware_close(fwh);
2270 goto fail1;
2271 }
2272
2273 error = firmware_read(fwh, 0, sc->sc_blob, size);
2274 firmware_close(fwh);
2275 if (error != 0)
2276 goto fail2;
2277
2278 hdr = (struct iwi_firmware_hdr *)sc->sc_blob;
2279 hdr->version = le32toh(hdr->version);
2280 hdr->bsize = le32toh(hdr->bsize);
2281 hdr->usize = le32toh(hdr->usize);
2282 hdr->fsize = le32toh(hdr->fsize);
2283
2284 if (size < sizeof(struct iwi_firmware_hdr) + hdr->bsize + hdr->usize + hdr->fsize) {
2285 aprint_error_dev(sc->sc_dev, "image '%s' too small\n",
2286 sc->sc_fwname);
2287 error = EIO;
2288 goto fail2;
2289 }
2290
2291 DPRINTF(("firmware version = %d\n", hdr->version));
2292 if ((IWI_FW_GET_MAJOR(hdr->version) != IWI_FW_REQ_MAJOR) ||
2293 (IWI_FW_GET_MINOR(hdr->version) != IWI_FW_REQ_MINOR)) {
2294 aprint_error_dev(sc->sc_dev,
2295 "version for '%s' %d.%d != %d.%d\n", sc->sc_fwname,
2296 IWI_FW_GET_MAJOR(hdr->version),
2297 IWI_FW_GET_MINOR(hdr->version),
2298 IWI_FW_REQ_MAJOR, IWI_FW_REQ_MINOR);
2299 error = EIO;
2300 goto fail2;
2301 }
2302
2303 kfw->boot_size = hdr->bsize;
2304 kfw->ucode_size = hdr->usize;
2305 kfw->main_size = hdr->fsize;
2306
2307 fw = sc->sc_blob + sizeof(struct iwi_firmware_hdr);
2308 kfw->boot = fw;
2309 fw += kfw->boot_size;
2310 kfw->ucode = fw;
2311 fw += kfw->ucode_size;
2312 kfw->main = fw;
2313
2314 DPRINTF(("Firmware cached: boot %p, ucode %p, main %p\n",
2315 kfw->boot, kfw->ucode, kfw->main));
2316 DPRINTF(("Firmware cached: boot %u, ucode %u, main %u\n",
2317 kfw->boot_size, kfw->ucode_size, kfw->main_size));
2318
2319 sc->flags |= IWI_FLAG_FW_CACHED;
2320
2321 return 0;
2322
2323
2324 fail2: firmware_free(sc->sc_blob, sc->sc_blobsize);
2325 fail1:
2326 return error;
2327 }
2328
2329 static void
2330 iwi_free_firmware(struct iwi_softc *sc)
2331 {
2332
2333 if (!(sc->flags & IWI_FLAG_FW_CACHED))
2334 return;
2335
2336 firmware_free(sc->sc_blob, sc->sc_blobsize);
2337
2338 sc->flags &= ~IWI_FLAG_FW_CACHED;
2339 }
2340
2341 static int
2342 iwi_config(struct iwi_softc *sc)
2343 {
2344 struct ieee80211com *ic = &sc->sc_ic;
2345 struct ifnet *ifp = &sc->sc_if;
2346 struct iwi_configuration config;
2347 struct iwi_rateset rs;
2348 struct iwi_txpower power;
2349 struct ieee80211_key *wk;
2350 struct iwi_wep_key wepkey;
2351 uint32_t data;
2352 int error, nchan, i;
2353
2354 IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
2355 DPRINTF(("Setting MAC address to %s\n", ether_sprintf(ic->ic_myaddr)));
2356 error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, ic->ic_myaddr,
2357 IEEE80211_ADDR_LEN, 0);
2358 if (error != 0)
2359 return error;
2360
2361 memset(&config, 0, sizeof config);
2362 config.bluetooth_coexistence = sc->bluetooth;
2363 config.antenna = sc->antenna;
2364 config.silence_threshold = 0x1e;
2365 config.multicast_enabled = 1;
2366 config.answer_pbreq = (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
2367 config.disable_unicast_decryption = 1;
2368 config.disable_multicast_decryption = 1;
2369 DPRINTF(("Configuring adapter\n"));
2370 error = iwi_cmd(sc, IWI_CMD_SET_CONFIGURATION, &config, sizeof config,
2371 0);
2372 if (error != 0)
2373 return error;
2374
2375 data = htole32(IWI_POWER_MODE_CAM);
2376 DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2377 error = iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data, 0);
2378 if (error != 0)
2379 return error;
2380
2381 data = htole32(ic->ic_rtsthreshold);
2382 DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2383 error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data, 0);
2384 if (error != 0)
2385 return error;
2386
2387 data = htole32(ic->ic_fragthreshold);
2388 DPRINTF(("Setting fragmentation threshold to %u\n", le32toh(data)));
2389 error = iwi_cmd(sc, IWI_CMD_SET_FRAG_THRESHOLD, &data, sizeof data, 0);
2390 if (error != 0)
2391 return error;
2392
2393 /*
2394 * Set default Tx power for 802.11b/g and 802.11a channels.
2395 */
2396 nchan = 0;
2397 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
2398 if (!IEEE80211_IS_CHAN_2GHZ(&ic->ic_channels[i]))
2399 continue;
2400 power.chan[nchan].chan = i;
2401 power.chan[nchan].power = IWI_TXPOWER_MAX;
2402 nchan++;
2403 }
2404 power.nchan = nchan;
2405
2406 power.mode = IWI_MODE_11G;
2407 DPRINTF(("Setting .11g channels tx power\n"));
2408 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power, 0);
2409 if (error != 0)
2410 return error;
2411
2412 power.mode = IWI_MODE_11B;
2413 DPRINTF(("Setting .11b channels tx power\n"));
2414 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power, 0);
2415 if (error != 0)
2416 return error;
2417
2418 nchan = 0;
2419 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
2420 if (!IEEE80211_IS_CHAN_5GHZ(&ic->ic_channels[i]))
2421 continue;
2422 power.chan[nchan].chan = i;
2423 power.chan[nchan].power = IWI_TXPOWER_MAX;
2424 nchan++;
2425 }
2426 power.nchan = nchan;
2427
2428 if (nchan > 0) { /* 2915ABG only */
2429 power.mode = IWI_MODE_11A;
2430 DPRINTF(("Setting .11a channels tx power\n"));
2431 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power,
2432 0);
2433 if (error != 0)
2434 return error;
2435 }
2436
2437 rs.mode = IWI_MODE_11G;
2438 rs.type = IWI_RATESET_TYPE_SUPPORTED;
2439 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates;
2440 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11G].rs_rates,
2441 rs.nrates);
2442 DPRINTF(("Setting .11bg supported rates (%u)\n", rs.nrates));
2443 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
2444 if (error != 0)
2445 return error;
2446
2447 rs.mode = IWI_MODE_11A;
2448 rs.type = IWI_RATESET_TYPE_SUPPORTED;
2449 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates;
2450 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11A].rs_rates,
2451 rs.nrates);
2452 DPRINTF(("Setting .11a supported rates (%u)\n", rs.nrates));
2453 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
2454 if (error != 0)
2455 return error;
2456
2457 /* if we have a desired ESSID, set it now */
2458 if (ic->ic_des_esslen != 0) {
2459 #ifdef IWI_DEBUG
2460 if (iwi_debug > 0) {
2461 printf("Setting desired ESSID to ");
2462 ieee80211_print_essid(ic->ic_des_essid,
2463 ic->ic_des_esslen);
2464 printf("\n");
2465 }
2466 #endif
2467 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ic->ic_des_essid,
2468 ic->ic_des_esslen, 0);
2469 if (error != 0)
2470 return error;
2471 }
2472
2473 cprng_fast(&data, sizeof(data));
2474 data = htole32(data);
2475 DPRINTF(("Setting initialization vector to %u\n", le32toh(data)));
2476 error = iwi_cmd(sc, IWI_CMD_SET_IV, &data, sizeof data, 0);
2477 if (error != 0)
2478 return error;
2479
2480 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
2481 /* XXX iwi_setwepkeys? */
2482 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2483 wk = &ic->ic_crypto.cs_nw_keys[i];
2484
2485 wepkey.cmd = IWI_WEP_KEY_CMD_SETKEY;
2486 wepkey.idx = i;
2487 wepkey.len = wk->wk_keylen;
2488 memset(wepkey.key, 0, sizeof wepkey.key);
2489 memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2490 DPRINTF(("Setting wep key index %u len %u\n",
2491 wepkey.idx, wepkey.len));
2492 error = iwi_cmd(sc, IWI_CMD_SET_WEP_KEY, &wepkey,
2493 sizeof wepkey, 0);
2494 if (error != 0)
2495 return error;
2496 }
2497 }
2498
2499 /* Enable adapter */
2500 DPRINTF(("Enabling adapter\n"));
2501 return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0, 0);
2502 }
2503
2504 static int
2505 iwi_set_chan(struct iwi_softc *sc, struct ieee80211_channel *chan)
2506 {
2507 struct ieee80211com *ic = &sc->sc_ic;
2508 struct iwi_scan_v2 scan;
2509
2510 (void)memset(&scan, 0, sizeof scan);
2511
2512 scan.dwelltime[IWI_SCAN_TYPE_PASSIVE] = htole16(2000);
2513 scan.channels[0] = 1 |
2514 (IEEE80211_IS_CHAN_5GHZ(chan) ? IWI_CHAN_5GHZ : IWI_CHAN_2GHZ);
2515 scan.channels[1] = ieee80211_chan2ieee(ic, chan);
2516 iwi_scan_type_set(scan, 1, IWI_SCAN_TYPE_PASSIVE);
2517
2518 DPRINTF(("Setting channel to %u\n", ieee80211_chan2ieee(ic, chan)));
2519 return iwi_cmd(sc, IWI_CMD_SCAN_V2, &scan, sizeof scan, 1);
2520 }
2521
2522 static int
2523 iwi_scan(struct iwi_softc *sc)
2524 {
2525 struct ieee80211com *ic = &sc->sc_ic;
2526 struct iwi_scan_v2 scan;
2527 uint32_t type;
2528 uint8_t *p;
2529 int i, count, idx;
2530
2531 (void)memset(&scan, 0, sizeof scan);
2532 scan.dwelltime[IWI_SCAN_TYPE_ACTIVE_BROADCAST] =
2533 htole16(sc->dwelltime);
2534 scan.dwelltime[IWI_SCAN_TYPE_ACTIVE_BDIRECT] =
2535 htole16(sc->dwelltime);
2536
2537 /* tell the firmware about the desired essid */
2538 if (ic->ic_des_esslen) {
2539 int error;
2540
2541 DPRINTF(("%s: Setting adapter desired ESSID to %s\n",
2542 __func__, ic->ic_des_essid));
2543
2544 error = iwi_cmd(sc, IWI_CMD_SET_ESSID,
2545 ic->ic_des_essid, ic->ic_des_esslen, 1);
2546 if (error)
2547 return error;
2548
2549 type = IWI_SCAN_TYPE_ACTIVE_BDIRECT;
2550 } else {
2551 type = IWI_SCAN_TYPE_ACTIVE_BROADCAST;
2552 }
2553
2554 p = &scan.channels[0];
2555 count = idx = 0;
2556 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
2557 if (IEEE80211_IS_CHAN_5GHZ(&ic->ic_channels[i]) &&
2558 isset(ic->ic_chan_active, i)) {
2559 *++p = i;
2560 count++;
2561 idx++;
2562 iwi_scan_type_set(scan, idx, type);
2563 }
2564 }
2565 if (count) {
2566 *(p - count) = IWI_CHAN_5GHZ | count;
2567 p++;
2568 }
2569
2570 count = 0;
2571 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
2572 if (IEEE80211_IS_CHAN_2GHZ(&ic->ic_channels[i]) &&
2573 isset(ic->ic_chan_active, i)) {
2574 *++p = i;
2575 count++;
2576 idx++;
2577 iwi_scan_type_set(scan, idx, type);
2578 }
2579 }
2580 *(p - count) = IWI_CHAN_2GHZ | count;
2581
2582 DPRINTF(("Start scanning\n"));
2583 return iwi_cmd(sc, IWI_CMD_SCAN_V2, &scan, sizeof scan, 1);
2584 }
2585
2586 static int
2587 iwi_auth_and_assoc(struct iwi_softc *sc)
2588 {
2589 struct ieee80211com *ic = &sc->sc_ic;
2590 struct ieee80211_node *ni = ic->ic_bss;
2591 struct ifnet *ifp = &sc->sc_if;
2592 struct ieee80211_wme_info wme;
2593 struct iwi_configuration config;
2594 struct iwi_associate assoc;
2595 struct iwi_rateset rs;
2596 uint16_t capinfo;
2597 uint32_t data;
2598 int error;
2599
2600 memset(&config, 0, sizeof config);
2601 config.bluetooth_coexistence = sc->bluetooth;
2602 config.antenna = sc->antenna;
2603 config.multicast_enabled = 1;
2604 config.silence_threshold = 0x1e;
2605 if (ic->ic_curmode == IEEE80211_MODE_11G)
2606 config.use_protection = 1;
2607 config.answer_pbreq = (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
2608 config.disable_unicast_decryption = 1;
2609 config.disable_multicast_decryption = 1;
2610
2611 DPRINTF(("Configuring adapter\n"));
2612 error = iwi_cmd(sc, IWI_CMD_SET_CONFIGURATION, &config,
2613 sizeof config, 1);
2614 if (error != 0)
2615 return error;
2616
2617 #ifdef IWI_DEBUG
2618 if (iwi_debug > 0) {
2619 aprint_debug_dev(sc->sc_dev, "Setting ESSID to ");
2620 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
2621 aprint_debug("\n");
2622 }
2623 #endif
2624 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen, 1);
2625 if (error != 0)
2626 return error;
2627
2628 /* the rate set has already been "negotiated" */
2629 rs.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A :
2630 IWI_MODE_11G;
2631 rs.type = IWI_RATESET_TYPE_NEGOTIATED;
2632 rs.nrates = ni->ni_rates.rs_nrates;
2633
2634 if (rs.nrates > IWI_RATESET_SIZE) {
2635 DPRINTF(("Truncating negotiated rate set from %u\n",
2636 rs.nrates));
2637 rs.nrates = IWI_RATESET_SIZE;
2638 }
2639 memcpy(rs.rates, ni->ni_rates.rs_rates, rs.nrates);
2640 DPRINTF(("Setting negotiated rates (%u)\n", rs.nrates));
2641 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 1);
2642 if (error != 0)
2643 return error;
2644
2645 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL) {
2646 wme.wme_id = IEEE80211_ELEMID_VENDOR;
2647 wme.wme_len = sizeof (struct ieee80211_wme_info) - 2;
2648 wme.wme_oui[0] = 0x00;
2649 wme.wme_oui[1] = 0x50;
2650 wme.wme_oui[2] = 0xf2;
2651 wme.wme_type = WME_OUI_TYPE;
2652 wme.wme_subtype = WME_INFO_OUI_SUBTYPE;
2653 wme.wme_version = WME_VERSION;
2654 wme.wme_info = 0;
2655
2656 DPRINTF(("Setting WME IE (len=%u)\n", wme.wme_len));
2657 error = iwi_cmd(sc, IWI_CMD_SET_WMEIE, &wme, sizeof wme, 1);
2658 if (error != 0)
2659 return error;
2660 }
2661
2662 if (ic->ic_opt_ie != NULL) {
2663 DPRINTF(("Setting optional IE (len=%u)\n", ic->ic_opt_ie_len));
2664 error = iwi_cmd(sc, IWI_CMD_SET_OPTIE, ic->ic_opt_ie,
2665 ic->ic_opt_ie_len, 1);
2666 if (error != 0)
2667 return error;
2668 }
2669 data = htole32(ni->ni_rssi);
2670 DPRINTF(("Setting sensitivity to %d\n", (int8_t)ni->ni_rssi));
2671 error = iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &data, sizeof data, 1);
2672 if (error != 0)
2673 return error;
2674
2675 memset(&assoc, 0, sizeof assoc);
2676 if (IEEE80211_IS_CHAN_A(ni->ni_chan))
2677 assoc.mode = IWI_MODE_11A;
2678 else if (IEEE80211_IS_CHAN_G(ni->ni_chan))
2679 assoc.mode = IWI_MODE_11G;
2680 else if (IEEE80211_IS_CHAN_B(ni->ni_chan))
2681 assoc.mode = IWI_MODE_11B;
2682
2683 assoc.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
2684
2685 if (ni->ni_authmode == IEEE80211_AUTH_SHARED)
2686 assoc.auth = (ic->ic_crypto.cs_def_txkey << 4) | IWI_AUTH_SHARED;
2687
2688 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
2689 assoc.plen = IWI_ASSOC_SHPREAMBLE;
2690
2691 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
2692 assoc.policy |= htole16(IWI_POLICY_WME);
2693 if (ic->ic_flags & IEEE80211_F_WPA)
2694 assoc.policy |= htole16(IWI_POLICY_WPA);
2695 if (ic->ic_opmode == IEEE80211_M_IBSS && ni->ni_tstamp.tsf == 0)
2696 assoc.type = IWI_HC_IBSS_START;
2697 else
2698 assoc.type = IWI_HC_ASSOC;
2699 memcpy(assoc.tstamp, ni->ni_tstamp.data, 8);
2700
2701 if (ic->ic_opmode == IEEE80211_M_IBSS)
2702 capinfo = IEEE80211_CAPINFO_IBSS;
2703 else
2704 capinfo = IEEE80211_CAPINFO_ESS;
2705 if (ic->ic_flags & IEEE80211_F_PRIVACY)
2706 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2707 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2708 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
2709 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2710 if (ic->ic_flags & IEEE80211_F_SHSLOT)
2711 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2712 assoc.capinfo = htole16(capinfo);
2713
2714 assoc.lintval = htole16(ic->ic_lintval);
2715 assoc.intval = htole16(ni->ni_intval);
2716 IEEE80211_ADDR_COPY(assoc.bssid, ni->ni_bssid);
2717 if (ic->ic_opmode == IEEE80211_M_IBSS)
2718 IEEE80211_ADDR_COPY(assoc.dst, ifp->if_broadcastaddr);
2719 else
2720 IEEE80211_ADDR_COPY(assoc.dst, ni->ni_bssid);
2721
2722 DPRINTF(("%s bssid %s dst %s channel %u policy 0x%x "
2723 "auth %u capinfo 0x%x lintval %u bintval %u\n",
2724 assoc.type == IWI_HC_IBSS_START ? "Start" : "Join",
2725 ether_sprintf(assoc.bssid), ether_sprintf(assoc.dst),
2726 assoc.chan, le16toh(assoc.policy), assoc.auth,
2727 le16toh(assoc.capinfo), le16toh(assoc.lintval),
2728 le16toh(assoc.intval)));
2729
2730 return iwi_cmd(sc, IWI_CMD_ASSOCIATE, &assoc, sizeof assoc, 1);
2731 }
2732
2733 static int
2734 iwi_init(struct ifnet *ifp)
2735 {
2736 struct iwi_softc *sc = ifp->if_softc;
2737 struct ieee80211com *ic = &sc->sc_ic;
2738 struct iwi_firmware *fw = &sc->fw;
2739 int i, error;
2740
2741 /* exit immediately if firmware has not been ioctl'd */
2742 if (!(sc->flags & IWI_FLAG_FW_CACHED)) {
2743 if ((error = iwi_cache_firmware(sc)) != 0) {
2744 aprint_error_dev(sc->sc_dev,
2745 "could not cache the firmware\n");
2746 goto fail;
2747 }
2748 }
2749
2750 iwi_stop(ifp, 0);
2751
2752 if ((error = iwi_reset(sc)) != 0) {
2753 aprint_error_dev(sc->sc_dev, "could not reset adapter\n");
2754 goto fail;
2755 }
2756
2757 if ((error = iwi_load_firmware(sc, fw->boot, fw->boot_size)) != 0) {
2758 aprint_error_dev(sc->sc_dev, "could not load boot firmware\n");
2759 goto fail;
2760 }
2761
2762 if ((error = iwi_load_ucode(sc, fw->ucode, fw->ucode_size)) != 0) {
2763 aprint_error_dev(sc->sc_dev, "could not load microcode\n");
2764 goto fail;
2765 }
2766
2767 iwi_stop_master(sc);
2768
2769 CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmdq.desc_map->dm_segs[0].ds_addr);
2770 CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, sc->cmdq.count);
2771 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
2772
2773 CSR_WRITE_4(sc, IWI_CSR_TX1_BASE, sc->txq[0].desc_map->dm_segs[0].ds_addr);
2774 CSR_WRITE_4(sc, IWI_CSR_TX1_SIZE, sc->txq[0].count);
2775 CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq[0].cur);
2776
2777 CSR_WRITE_4(sc, IWI_CSR_TX2_BASE, sc->txq[1].desc_map->dm_segs[0].ds_addr);
2778 CSR_WRITE_4(sc, IWI_CSR_TX2_SIZE, sc->txq[1].count);
2779 CSR_WRITE_4(sc, IWI_CSR_TX2_WIDX, sc->txq[1].cur);
2780
2781 CSR_WRITE_4(sc, IWI_CSR_TX3_BASE, sc->txq[2].desc_map->dm_segs[0].ds_addr);
2782 CSR_WRITE_4(sc, IWI_CSR_TX3_SIZE, sc->txq[2].count);
2783 CSR_WRITE_4(sc, IWI_CSR_TX3_WIDX, sc->txq[2].cur);
2784
2785 CSR_WRITE_4(sc, IWI_CSR_TX4_BASE, sc->txq[3].desc_map->dm_segs[0].ds_addr);
2786 CSR_WRITE_4(sc, IWI_CSR_TX4_SIZE, sc->txq[3].count);
2787 CSR_WRITE_4(sc, IWI_CSR_TX4_WIDX, sc->txq[3].cur);
2788
2789 for (i = 0; i < sc->rxq.count; i++)
2790 CSR_WRITE_4(sc, IWI_CSR_RX_BASE + i * 4,
2791 sc->rxq.data[i].map->dm_segs[0].ds_addr);
2792
2793 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, sc->rxq.count -1);
2794
2795 if ((error = iwi_load_firmware(sc, fw->main, fw->main_size)) != 0) {
2796 aprint_error_dev(sc->sc_dev, "could not load main firmware\n");
2797 goto fail;
2798 }
2799
2800 sc->flags |= IWI_FLAG_FW_INITED;
2801
2802 if ((error = iwi_config(sc)) != 0) {
2803 aprint_error_dev(sc->sc_dev, "device configuration failed\n");
2804 goto fail;
2805 }
2806
2807 ic->ic_state = IEEE80211_S_INIT;
2808
2809 ifp->if_flags &= ~IFF_OACTIVE;
2810 ifp->if_flags |= IFF_RUNNING;
2811
2812 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2813 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2814 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2815 } else
2816 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2817
2818 return 0;
2819
2820 fail: ifp->if_flags &= ~IFF_UP;
2821 iwi_stop(ifp, 0);
2822
2823 return error;
2824 }
2825
2826
2827 /*
2828 * Return whether or not the radio is enabled in hardware
2829 * (i.e. the rfkill switch is "off").
2830 */
2831 static int
2832 iwi_getrfkill(struct iwi_softc *sc)
2833 {
2834 return (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) == 0;
2835 }
2836
2837 static int
2838 iwi_sysctl_radio(SYSCTLFN_ARGS)
2839 {
2840 struct sysctlnode node;
2841 struct iwi_softc *sc;
2842 int val, error;
2843
2844 node = *rnode;
2845 sc = (struct iwi_softc *)node.sysctl_data;
2846
2847 val = !iwi_getrfkill(sc);
2848
2849 node.sysctl_data = &val;
2850 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2851
2852 if (error || newp == NULL)
2853 return error;
2854
2855 return 0;
2856 }
2857
2858 #ifdef IWI_DEBUG
2859 SYSCTL_SETUP(sysctl_iwi, "sysctl iwi(4) subtree setup")
2860 {
2861 int rc;
2862 const struct sysctlnode *rnode;
2863 const struct sysctlnode *cnode;
2864
2865 if ((rc = sysctl_createv(clog, 0, NULL, &rnode,
2866 CTLFLAG_PERMANENT, CTLTYPE_NODE, "iwi",
2867 SYSCTL_DESCR("iwi global controls"),
2868 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
2869 goto err;
2870
2871 /* control debugging printfs */
2872 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
2873 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
2874 "debug", SYSCTL_DESCR("Enable debugging output"),
2875 NULL, 0, &iwi_debug, 0, CTL_CREATE, CTL_EOL)) != 0)
2876 goto err;
2877
2878 return;
2879 err:
2880 aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
2881 }
2882
2883 #endif /* IWI_DEBUG */
2884
2885 /*
2886 * Add sysctl knobs.
2887 */
2888 static void
2889 iwi_sysctlattach(struct iwi_softc *sc)
2890 {
2891 int rc;
2892 const struct sysctlnode *rnode;
2893 const struct sysctlnode *cnode;
2894
2895 struct sysctllog **clog = &sc->sc_sysctllog;
2896
2897 if ((rc = sysctl_createv(clog, 0, NULL, &rnode,
2898 CTLFLAG_PERMANENT, CTLTYPE_NODE, device_xname(sc->sc_dev),
2899 SYSCTL_DESCR("iwi controls and statistics"),
2900 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
2901 goto err;
2902
2903 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
2904 CTLFLAG_PERMANENT, CTLTYPE_INT, "radio",
2905 SYSCTL_DESCR("radio transmitter switch state (0=off, 1=on)"),
2906 iwi_sysctl_radio, 0, (void *)sc, 0, CTL_CREATE, CTL_EOL)) != 0)
2907 goto err;
2908
2909 sc->dwelltime = 100;
2910 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
2911 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
2912 "dwell", SYSCTL_DESCR("channel dwell time (ms) for AP/station scanning"),
2913 NULL, 0, &sc->dwelltime, 0, CTL_CREATE, CTL_EOL)) != 0)
2914 goto err;
2915
2916 sc->bluetooth = 0;
2917 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
2918 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
2919 "bluetooth", SYSCTL_DESCR("bluetooth coexistence"),
2920 NULL, 0, &sc->bluetooth, 0, CTL_CREATE, CTL_EOL)) != 0)
2921 goto err;
2922
2923 sc->antenna = IWI_ANTENNA_AUTO;
2924 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
2925 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
2926 "antenna", SYSCTL_DESCR("antenna (0=auto)"),
2927 NULL, 0, &sc->antenna, 0, CTL_CREATE, CTL_EOL)) != 0)
2928 goto err;
2929
2930 return;
2931 err:
2932 aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
2933 }
2934
2935 static void
2936 iwi_stop(struct ifnet *ifp, int disable)
2937 {
2938 struct iwi_softc *sc = ifp->if_softc;
2939 struct ieee80211com *ic = &sc->sc_ic;
2940
2941 IWI_LED_OFF(sc);
2942
2943 iwi_stop_master(sc);
2944 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SW_RESET);
2945
2946 /* reset rings */
2947 iwi_reset_cmd_ring(sc, &sc->cmdq);
2948 iwi_reset_tx_ring(sc, &sc->txq[0]);
2949 iwi_reset_tx_ring(sc, &sc->txq[1]);
2950 iwi_reset_tx_ring(sc, &sc->txq[2]);
2951 iwi_reset_tx_ring(sc, &sc->txq[3]);
2952 iwi_reset_rx_ring(sc, &sc->rxq);
2953
2954 ifp->if_timer = 0;
2955 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2956
2957 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2958 }
2959
2960 static void
2961 iwi_led_set(struct iwi_softc *sc, uint32_t state, int toggle)
2962 {
2963 uint32_t val;
2964
2965 val = MEM_READ_4(sc, IWI_MEM_EVENT_CTL);
2966
2967 switch (sc->nictype) {
2968 case 1:
2969 /* special NIC type: reversed leds */
2970 if (state == IWI_LED_ACTIVITY) {
2971 state &= ~IWI_LED_ACTIVITY;
2972 state |= IWI_LED_ASSOCIATED;
2973 } else if (state == IWI_LED_ASSOCIATED) {
2974 state &= ~IWI_LED_ASSOCIATED;
2975 state |= IWI_LED_ACTIVITY;
2976 }
2977 /* and ignore toggle effect */
2978 val |= state;
2979 break;
2980 case 0:
2981 case 2:
2982 case 3:
2983 case 4:
2984 val = (toggle && (val & state)) ? val & ~state : val | state;
2985 break;
2986 default:
2987 aprint_normal_dev(sc->sc_dev, "unknown NIC type %d\n",
2988 sc->nictype);
2989 return;
2990 break;
2991 }
2992
2993 MEM_WRITE_4(sc, IWI_MEM_EVENT_CTL, val);
2994
2995 return;
2996 }
2997
2998 SYSCTL_SETUP(sysctl_hw_iwi_accept_eula_setup, "sysctl hw.iwi.accept_eula")
2999 {
3000 const struct sysctlnode *rnode;
3001 const struct sysctlnode *cnode;
3002
3003 sysctl_createv(NULL, 0, NULL, &rnode,
3004 CTLFLAG_PERMANENT,
3005 CTLTYPE_NODE, "iwi",
3006 NULL,
3007 NULL, 0,
3008 NULL, 0,
3009 CTL_HW, CTL_CREATE, CTL_EOL);
3010
3011 sysctl_createv(NULL, 0, &rnode, &cnode,
3012 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
3013 CTLTYPE_INT, "accept_eula",
3014 SYSCTL_DESCR("Accept Intel EULA and permit use of iwi(4) firmware"),
3015 NULL, 0,
3016 &iwi_accept_eula, sizeof(iwi_accept_eula),
3017 CTL_CREATE, CTL_EOL);
3018 }
3019