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