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