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