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