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