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