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