if_iwi.c revision 1.11 1 /* $NetBSD: if_iwi.c,v 1.11 2005/07/07 00:43:01 dyoung 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.11 2005/07/07 00:43:01 dyoung Exp $");
32
33 /*-
34 * Intel(R) PRO/Wireless 2200BG/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
50 #include <machine/bus.h>
51 #include <machine/endian.h>
52 #include <machine/intr.h>
53
54 #include <dev/pci/pcireg.h>
55 #include <dev/pci/pcivar.h>
56 #include <dev/pci/pcidevs.h>
57
58 #if NBPFILTER > 0
59 #include <net/bpf.h>
60 #endif
61 #include <net/if.h>
62 #include <net/if_arp.h>
63 #include <net/if_dl.h>
64 #include <net/if_ether.h>
65 #include <net/if_media.h>
66 #include <net/if_types.h>
67
68 #include <net80211/ieee80211_var.h>
69 #include <net80211/ieee80211_radiotap.h>
70
71 #include <netinet/in.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/in_var.h>
74 #include <netinet/ip.h>
75
76 #include <crypto/arc4/arc4.h>
77
78 #include <dev/pci/if_iwireg.h>
79 #include <dev/pci/if_iwivar.h>
80
81 static const struct ieee80211_rateset iwi_rateset_11a =
82 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
83
84 static const struct ieee80211_rateset iwi_rateset_11b =
85 { 4, { 2, 4, 11, 22 } };
86
87 static const struct ieee80211_rateset iwi_rateset_11g =
88 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
89
90 static int iwi_match(struct device *, struct cfdata *, void *);
91 static void iwi_attach(struct device *, struct device *, void *);
92 static int iwi_detach(struct device *, int);
93 static int iwi_dma_alloc(struct iwi_softc *);
94 static void iwi_release(struct iwi_softc *);
95 static int iwi_media_change(struct ifnet *);
96 static void iwi_media_status(struct ifnet *, struct ifmediareq *);
97 static u_int16_t iwi_read_prom_word(struct iwi_softc *, u_int8_t);
98 static int iwi_newstate(struct ieee80211com *, enum ieee80211_state, int);
99 static void iwi_fix_channel(struct ieee80211com *, struct mbuf *);
100 static void iwi_frame_intr(struct iwi_softc *, struct iwi_rx_buf *, int,
101 struct iwi_frame *);
102 static void iwi_notification_intr(struct iwi_softc *, struct iwi_rx_buf *,
103 struct iwi_notif *);
104 static void iwi_rx_intr(struct iwi_softc *);
105 static void iwi_tx_intr(struct iwi_softc *);
106 static int iwi_intr(void *);
107 static int iwi_cmd(struct iwi_softc *, u_int8_t, void *, u_int8_t, int);
108 static int iwi_tx_start(struct ifnet *, struct mbuf *, struct ieee80211_node *);
109 static void iwi_start(struct ifnet *);
110 static void iwi_watchdog(struct ifnet *);
111 static int iwi_get_table0(struct iwi_softc *, u_int32_t *);
112 static int iwi_get_radio(struct iwi_softc *, int *);
113 static int iwi_ioctl(struct ifnet *, u_long, caddr_t);
114 static void iwi_stop_master(struct iwi_softc *);
115 static int iwi_reset(struct iwi_softc *);
116 static int iwi_load_ucode(struct iwi_softc *, void *, int);
117 static int iwi_load_firmware(struct iwi_softc *, void *, int);
118 static int iwi_cache_firmware(struct iwi_softc *, void *);
119 static void iwi_free_firmware(struct iwi_softc *);
120 static int iwi_config(struct iwi_softc *);
121 static int iwi_set_chan(struct iwi_softc *, struct ieee80211_channel *);
122 static int iwi_scan(struct iwi_softc *);
123 static int iwi_auth_and_assoc(struct iwi_softc *);
124 static int iwi_init(struct ifnet *);
125 static void iwi_stop(struct ifnet *, int);
126 static int iwi_key_alloc(struct ieee80211com *, const struct ieee80211_key *);
127
128 static __inline u_int8_t MEM_READ_1(struct iwi_softc *sc, u_int32_t addr)
129 {
130 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
131 return CSR_READ_1(sc, IWI_CSR_INDIRECT_DATA);
132 }
133
134 static __inline u_int32_t MEM_READ_4(struct iwi_softc *sc, u_int32_t addr)
135 {
136 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
137 return CSR_READ_4(sc, IWI_CSR_INDIRECT_DATA);
138 }
139
140 #ifdef IWI_DEBUG
141 #define DPRINTF(x) if (iwi_debug > 0) printf x
142 #define DPRINTFN(n, x) if (iwi_debug >= (n)) printf x
143 int iwi_debug = 0;
144 #else
145 #define DPRINTF(x)
146 #define DPRINTFN(n, x)
147 #endif
148
149 CFATTACH_DECL(iwi, sizeof (struct iwi_softc), iwi_match, iwi_attach,
150 iwi_detach, NULL);
151
152 static int
153 iwi_match(struct device *parent, struct cfdata *match, void *aux)
154 {
155 struct pci_attach_args *pa = aux;
156
157 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
158 return 0;
159
160 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2200BG ||
161 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_1)
162 return 1;
163
164 return 0;
165 }
166
167 /* Base Address Register */
168 #define IWI_PCI_BAR0 0x10
169
170 static void
171 iwi_attach(struct device *parent, struct device *self, void *aux)
172 {
173 struct iwi_softc *sc = (struct iwi_softc *)self;
174 struct ieee80211com *ic = &sc->sc_ic;
175 struct ifnet *ifp = &sc->sc_if;
176 struct pci_attach_args *pa = aux;
177 const char *intrstr;
178 char devinfo[256];
179 bus_space_tag_t memt;
180 bus_space_handle_t memh;
181 bus_addr_t base;
182 pci_intr_handle_t ih;
183 pcireg_t data;
184 u_int16_t val;
185 int error, revision, i;
186
187 sc->sc_pct = pa->pa_pc;
188 sc->sc_pcitag = pa->pa_tag;
189
190 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof devinfo);
191 revision = PCI_REVISION(pa->pa_class);
192 aprint_normal(": %s (rev. 0x%02x)\n", devinfo, revision);
193
194 /* clear device specific PCI configuration register 0x41 */
195 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
196 data &= ~0x0000ff00;
197 pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data);
198
199 /* enable bus-mastering */
200 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
201 data |= PCI_COMMAND_MASTER_ENABLE;
202 pci_conf_write(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, data);
203
204 /* map the register window */
205 error = pci_mapreg_map(pa, IWI_PCI_BAR0, PCI_MAPREG_TYPE_MEM |
206 PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh, &base, &sc->sc_sz);
207 if (error != 0) {
208 aprint_error("%s: could not map memory space\n",
209 sc->sc_dev.dv_xname);
210 return;
211 }
212
213 sc->sc_st = memt;
214 sc->sc_sh = memh;
215 sc->sc_dmat = pa->pa_dmat;
216
217 /* disable interrupts */
218 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
219
220 if (pci_intr_map(pa, &ih) != 0) {
221 aprint_error("%s: could not map interrupt\n",
222 sc->sc_dev.dv_xname);
223 return;
224 }
225
226 intrstr = pci_intr_string(sc->sc_pct, ih);
227 sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, iwi_intr, sc);
228 if (sc->sc_ih == NULL) {
229 aprint_error("%s: could not establish interrupt",
230 sc->sc_dev.dv_xname);
231 if (intrstr != NULL)
232 aprint_error(" at %s", intrstr);
233 aprint_error("\n");
234 return;
235 }
236 aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
237
238 if (iwi_reset(sc) != 0) {
239 aprint_error("%s: could not reset adapter\n",
240 sc->sc_dev.dv_xname);
241 return;
242 }
243
244 if (iwi_dma_alloc(sc) != 0) {
245 aprint_error("%s: could not allocate DMA resources\n",
246 sc->sc_dev.dv_xname);
247 return;
248 }
249
250 ic->ic_ifp = ifp;
251 ic->ic_phytype = IEEE80211_T_OFDM;
252 ic->ic_opmode = IEEE80211_M_STA;
253 ic->ic_state = IEEE80211_S_INIT;
254
255 /* set device capabilities */
256 ic->ic_caps = IEEE80211_C_IBSS | IEEE80211_C_PMGT | IEEE80211_C_WEP |
257 IEEE80211_C_TXPMGT | IEEE80211_C_SHPREAMBLE | IEEE80211_C_MONITOR;
258
259 /* read MAC address from EEPROM */
260 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 0);
261 ic->ic_myaddr[0] = val >> 8;
262 ic->ic_myaddr[1] = val & 0xff;
263 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 1);
264 ic->ic_myaddr[2] = val >> 8;
265 ic->ic_myaddr[3] = val & 0xff;
266 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 2);
267 ic->ic_myaddr[4] = val >> 8;
268 ic->ic_myaddr[5] = val & 0xff;
269
270 aprint_normal("%s: 802.11 address %s\n", sc->sc_dev.dv_xname,
271 ether_sprintf(ic->ic_myaddr));
272
273 if (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_INTEL_PRO_WL_2200BG) {
274 /* set supported .11a rates */
275 ic->ic_sup_rates[IEEE80211_MODE_11A] = iwi_rateset_11a;
276
277 /* set supported .11a channels */
278 for (i = 36; i <= 64; i += 4) {
279 ic->ic_channels[i].ic_freq =
280 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
281 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
282 }
283 for (i = 149; i <= 161; i += 4) {
284 ic->ic_channels[i].ic_freq =
285 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
286 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
287 }
288 }
289
290 /* set supported .11b and .11g rates */
291 ic->ic_sup_rates[IEEE80211_MODE_11B] = iwi_rateset_11b;
292 ic->ic_sup_rates[IEEE80211_MODE_11G] = iwi_rateset_11g;
293
294 /* set supported .11b and .11g channels (1 through 14) */
295 for (i = 1; i <= 14; i++) {
296 ic->ic_channels[i].ic_freq =
297 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
298 ic->ic_channels[i].ic_flags =
299 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
300 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
301 }
302
303 /* default to authmode OPEN */
304 sc->authmode = IEEE80211_AUTH_OPEN;
305
306 /* IBSS channel undefined for now */
307 ic->ic_ibss_chan = &ic->ic_channels[0];
308
309 ifp->if_softc = sc;
310 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
311 ifp->if_init = iwi_init;
312 ifp->if_stop = iwi_stop;
313 ifp->if_ioctl = iwi_ioctl;
314 ifp->if_start = iwi_start;
315 ifp->if_watchdog = iwi_watchdog;
316 IFQ_SET_READY(&ifp->if_snd);
317 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
318
319 if_attach(ifp);
320 ieee80211_ifattach(ic);
321 /* override state transition machine */
322 sc->sc_newstate = ic->ic_newstate;
323 ic->ic_newstate = iwi_newstate;
324 ic->ic_crypto.cs_key_alloc = iwi_key_alloc;
325 ieee80211_media_init(ic, iwi_media_change, iwi_media_status);
326
327 #if NBPFILTER > 0
328 bpfattach2(ifp, DLT_IEEE802_11_RADIO,
329 sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf);
330
331 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
332 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
333 sc->sc_rxtap.wr_ihdr.it_present = htole32(IWI_RX_RADIOTAP_PRESENT);
334
335 sc->sc_txtap_len = sizeof sc->sc_txtapu;
336 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
337 sc->sc_txtap.wt_ihdr.it_present = htole32(IWI_TX_RADIOTAP_PRESENT);
338 #endif
339 }
340
341 static int
342 iwi_detach(struct device* self, int flags)
343 {
344 struct iwi_softc *sc = (struct iwi_softc *)self;
345 struct ifnet *ifp = &sc->sc_if;
346
347 iwi_stop(ifp, 1);
348 iwi_free_firmware(sc);
349
350 #if NBPFILTER > 0
351 bpfdetach(ifp);
352 #endif
353 ieee80211_ifdetach(&sc->sc_ic);
354 if_detach(ifp);
355
356 iwi_release(sc);
357
358 if (sc->sc_ih != NULL) {
359 pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
360 sc->sc_ih = NULL;
361 }
362
363 bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
364
365 return 0;
366 }
367
368 static int
369 iwi_dma_alloc(struct iwi_softc *sc)
370 {
371 int i, nsegs, error;
372
373 /*
374 * Allocate and map Tx ring
375 */
376 error = bus_dmamap_create(sc->sc_dmat,
377 sizeof (struct iwi_tx_desc) * IWI_TX_RING_SIZE, 1,
378 sizeof (struct iwi_tx_desc) * IWI_TX_RING_SIZE, 0, BUS_DMA_NOWAIT,
379 &sc->tx_ring_map);
380 if (error != 0) {
381 aprint_error("%s: could not create tx ring DMA map\n",
382 sc->sc_dev.dv_xname);
383 goto fail;
384 }
385
386 error = bus_dmamem_alloc(sc->sc_dmat,
387 sizeof (struct iwi_tx_desc) * IWI_TX_RING_SIZE, PAGE_SIZE, 0,
388 &sc->tx_ring_seg, 1, &nsegs, BUS_DMA_NOWAIT);
389 if (error != 0) {
390 aprint_error("%s: could not allocate tx ring DMA memory\n",
391 sc->sc_dev.dv_xname);
392 goto fail;
393 }
394
395 error = bus_dmamem_map(sc->sc_dmat, &sc->tx_ring_seg, nsegs,
396 sizeof (struct iwi_tx_desc) * IWI_TX_RING_SIZE,
397 (caddr_t *)&sc->tx_desc, BUS_DMA_NOWAIT);
398 if (error != 0) {
399 aprint_error("%s: could not map tx ring DMA memory\n",
400 sc->sc_dev.dv_xname);
401 goto fail;
402 }
403
404 error = bus_dmamap_load(sc->sc_dmat, sc->tx_ring_map, sc->tx_desc,
405 sizeof (struct iwi_tx_desc) * IWI_TX_RING_SIZE, NULL,
406 BUS_DMA_NOWAIT);
407 if (error != 0) {
408 aprint_error("%s: could not load tx ring DMA map\n",
409 sc->sc_dev.dv_xname);
410 goto fail;
411 }
412
413 memset(sc->tx_desc, 0, sizeof (struct iwi_tx_desc) * IWI_TX_RING_SIZE);
414
415 /*
416 * Allocate and map command ring
417 */
418 error = bus_dmamap_create(sc->sc_dmat,
419 sizeof (struct iwi_cmd_desc) * IWI_CMD_RING_SIZE, 1,
420 sizeof (struct iwi_cmd_desc) * IWI_CMD_RING_SIZE, 0,
421 BUS_DMA_NOWAIT, &sc->cmd_ring_map);
422 if (error != 0) {
423 aprint_error("%s: could not create command ring DMA map\n",
424 sc->sc_dev.dv_xname);
425 goto fail;
426 }
427
428 error = bus_dmamem_alloc(sc->sc_dmat,
429 sizeof (struct iwi_cmd_desc) * IWI_CMD_RING_SIZE, PAGE_SIZE, 0,
430 &sc->cmd_ring_seg, 1, &nsegs, BUS_DMA_NOWAIT);
431 if (error != 0) {
432 aprint_error("%s: could not allocate command ring DMA memory\n",
433 sc->sc_dev.dv_xname);
434 goto fail;
435 }
436
437 error = bus_dmamem_map(sc->sc_dmat, &sc->cmd_ring_seg, nsegs,
438 sizeof (struct iwi_cmd_desc) * IWI_CMD_RING_SIZE,
439 (caddr_t *)&sc->cmd_desc, BUS_DMA_NOWAIT);
440 if (error != 0) {
441 aprint_error("%s: could not map command ring DMA memory\n",
442 sc->sc_dev.dv_xname);
443 goto fail;
444 }
445
446 error = bus_dmamap_load(sc->sc_dmat, sc->cmd_ring_map, sc->cmd_desc,
447 sizeof (struct iwi_cmd_desc) * IWI_CMD_RING_SIZE, NULL,
448 BUS_DMA_NOWAIT);
449 if (error != 0) {
450 aprint_error("%s: could not load command ring DMA map\n",
451 sc->sc_dev.dv_xname);
452 goto fail;
453 }
454
455 memset(sc->cmd_desc, 0,
456 sizeof (struct iwi_cmd_desc) * IWI_CMD_RING_SIZE);
457
458 /*
459 * Allocate Tx buffers DMA maps
460 */
461 for (i = 0; i < IWI_TX_RING_SIZE; i++) {
462 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, IWI_MAX_NSEG,
463 MCLBYTES, 0, BUS_DMA_NOWAIT, &sc->tx_buf[i].map);
464 if (error != 0) {
465 aprint_error("%s: could not create tx buf DMA map",
466 sc->sc_dev.dv_xname);
467 goto fail;
468 }
469 }
470
471 /*
472 * Allocate and map Rx buffers
473 */
474 for (i = 0; i < IWI_RX_RING_SIZE; i++) {
475
476 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
477 0, BUS_DMA_NOWAIT, &sc->rx_buf[i].map);
478 if (error != 0) {
479 aprint_error("%s: could not create rx buf DMA map",
480 sc->sc_dev.dv_xname);
481 goto fail;
482 }
483
484 MGETHDR(sc->rx_buf[i].m, M_DONTWAIT, MT_DATA);
485 if (sc->rx_buf[i].m == NULL) {
486 aprint_error("%s: could not allocate rx mbuf\n",
487 sc->sc_dev.dv_xname);
488 error = ENOMEM;
489 goto fail;
490 }
491
492 MCLGET(sc->rx_buf[i].m, M_DONTWAIT);
493 if (!(sc->rx_buf[i].m->m_flags & M_EXT)) {
494 m_freem(sc->rx_buf[i].m);
495 aprint_error("%s: could not allocate rx mbuf cluster\n",
496 sc->sc_dev.dv_xname);
497 error = ENOMEM;
498 goto fail;
499 }
500
501 error = bus_dmamap_load(sc->sc_dmat, sc->rx_buf[i].map,
502 mtod(sc->rx_buf[i].m, void *), MCLBYTES, NULL,
503 BUS_DMA_NOWAIT);
504 if (error != 0) {
505 aprint_error("%s: could not load rx buffer DMA map\n",
506 sc->sc_dev.dv_xname);
507 goto fail;
508 }
509 }
510
511 return 0;
512
513 fail: iwi_release(sc);
514 return error;
515 }
516
517 static void
518 iwi_release(struct iwi_softc *sc)
519 {
520 int i;
521
522 if (sc->tx_ring_map != NULL) {
523 if (sc->tx_desc != NULL) {
524 bus_dmamap_unload(sc->sc_dmat, sc->tx_ring_map);
525 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->tx_desc,
526 sizeof (struct iwi_tx_desc) * IWI_TX_RING_SIZE);
527 bus_dmamem_free(sc->sc_dmat, &sc->tx_ring_seg, 1);
528 }
529 bus_dmamap_destroy(sc->sc_dmat, sc->tx_ring_map);
530 }
531
532 if (sc->cmd_ring_map != NULL) {
533 if (sc->cmd_desc != NULL) {
534 bus_dmamap_unload(sc->sc_dmat, sc->cmd_ring_map);
535 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->cmd_desc,
536 sizeof (struct iwi_cmd_desc) * IWI_CMD_RING_SIZE);
537 bus_dmamem_free(sc->sc_dmat, &sc->cmd_ring_seg, 1);
538 }
539 bus_dmamap_destroy(sc->sc_dmat, sc->cmd_ring_map);
540 }
541
542 for (i = 0; i < IWI_TX_RING_SIZE; i++) {
543 if (sc->tx_buf[i].m != NULL) {
544 bus_dmamap_unload(sc->sc_dmat, sc->tx_buf[i].map);
545 m_freem(sc->tx_buf[i].m);
546 }
547 bus_dmamap_destroy(sc->sc_dmat, sc->tx_buf[i].map);
548 }
549
550 for (i = 0; i < IWI_RX_RING_SIZE; i++) {
551 if (sc->rx_buf[i].m != NULL) {
552 bus_dmamap_unload(sc->sc_dmat, sc->rx_buf[i].map);
553 m_freem(sc->rx_buf[i].m);
554 }
555 bus_dmamap_destroy(sc->sc_dmat, sc->rx_buf[i].map);
556 }
557 }
558
559 static int
560 iwi_key_alloc(struct ieee80211com *ic, const struct ieee80211_key *k)
561 {
562 if (k >= &ic->ic_nw_keys[0] && k < &ic->ic_nw_keys[IEEE80211_WEP_NKID])
563 return k - ic->ic_nw_keys;
564
565 return IEEE80211_KEYIX_NONE;
566 }
567
568 static int
569 iwi_media_change(struct ifnet *ifp)
570 {
571 int error;
572
573 error = ieee80211_media_change(ifp);
574 if (error != ENETRESET)
575 return error;
576
577 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
578 iwi_init(ifp);
579
580 return 0;
581 }
582
583 static void
584 iwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
585 {
586 struct iwi_softc *sc = ifp->if_softc;
587 struct ieee80211com *ic = &sc->sc_ic;
588 #define N(a) (sizeof (a) / sizeof (a[0]))
589 static const struct {
590 u_int32_t val;
591 int rate;
592 } rates[] = {
593 { IWI_RATE_DS1, 2 },
594 { IWI_RATE_DS2, 4 },
595 { IWI_RATE_DS5, 11 },
596 { IWI_RATE_DS11, 22 },
597 { IWI_RATE_OFDM6, 12 },
598 { IWI_RATE_OFDM9, 18 },
599 { IWI_RATE_OFDM12, 24 },
600 { IWI_RATE_OFDM18, 36 },
601 { IWI_RATE_OFDM24, 48 },
602 { IWI_RATE_OFDM36, 72 },
603 { IWI_RATE_OFDM48, 96 },
604 { IWI_RATE_OFDM54, 108 },
605 };
606 u_int32_t val;
607 int rate, i;
608
609 imr->ifm_status = IFM_AVALID;
610 imr->ifm_active = IFM_IEEE80211;
611 if (ic->ic_state == IEEE80211_S_RUN)
612 imr->ifm_status |= IFM_ACTIVE;
613
614 /* read current transmission rate from adapter */
615 val = CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE);
616
617 /* convert rate to 802.11 rate */
618 for (i = 0; i < N(rates) && rates[i].val != val; i++);
619 rate = (i < N(rates)) ? rates[i].rate : 0;
620
621 imr->ifm_active |= ieee80211_rate2media(ic, rate, ic->ic_curmode);
622 switch (ic->ic_opmode) {
623 case IEEE80211_M_STA:
624 break;
625
626 case IEEE80211_M_IBSS:
627 imr->ifm_active |= IFM_IEEE80211_ADHOC;
628 break;
629
630 case IEEE80211_M_MONITOR:
631 imr->ifm_active |= IFM_IEEE80211_MONITOR;
632 break;
633
634 case IEEE80211_M_AHDEMO:
635 case IEEE80211_M_HOSTAP:
636 /* should not get there */
637 break;
638 }
639 #undef N
640 }
641
642 static int
643 iwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
644 {
645 struct iwi_softc *sc = ic->ic_ifp->if_softc;
646
647 switch (nstate) {
648 case IEEE80211_S_SCAN:
649 iwi_scan(sc);
650 break;
651
652 case IEEE80211_S_AUTH:
653 iwi_auth_and_assoc(sc);
654 break;
655
656 case IEEE80211_S_RUN:
657 if (ic->ic_opmode == IEEE80211_M_IBSS)
658 ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
659 else if (ic->ic_opmode == IEEE80211_M_MONITOR)
660 iwi_set_chan(sc, ic->ic_ibss_chan);
661 break;
662
663 case IEEE80211_S_ASSOC:
664 case IEEE80211_S_INIT:
665 break;
666 }
667
668 ic->ic_state = nstate;
669 return 0;
670 }
671
672 /*
673 * Read 16 bits at address 'addr' from the serial EEPROM.
674 * DON'T PLAY WITH THIS CODE UNLESS YOU KNOW *EXACTLY* WHAT YOU'RE DOING!
675 */
676 static u_int16_t
677 iwi_read_prom_word(struct iwi_softc *sc, u_int8_t addr)
678 {
679 u_int32_t tmp;
680 u_int16_t val;
681 int n;
682
683 /* Clock C once before the first command */
684 IWI_EEPROM_CTL(sc, 0);
685 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
686 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
687 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
688
689 /* Write start bit (1) */
690 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
691 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
692
693 /* Write READ opcode (10) */
694 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
695 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
696 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
697 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
698
699 /* Write address A7-A0 */
700 for (n = 7; n >= 0; n--) {
701 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
702 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D));
703 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
704 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D) | IWI_EEPROM_C);
705 }
706
707 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
708
709 /* Read data Q15-Q0 */
710 val = 0;
711 for (n = 15; n >= 0; n--) {
712 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
713 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
714 tmp = MEM_READ_4(sc, IWI_MEM_EEPROM_CTL);
715 val |= ((tmp & IWI_EEPROM_Q) >> IWI_EEPROM_SHIFT_Q) << n;
716 }
717
718 IWI_EEPROM_CTL(sc, 0);
719
720 /* Clear Chip Select and clock C */
721 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
722 IWI_EEPROM_CTL(sc, 0);
723 IWI_EEPROM_CTL(sc, IWI_EEPROM_C);
724
725 return be16toh(val);
726 }
727
728 /*
729 * XXX: Hack to set the current channel to the value advertised in beacons or
730 * probe responses. Only used during AP detection.
731 */
732 static void
733 iwi_fix_channel(struct ieee80211com *ic, struct mbuf *m)
734 {
735 struct ieee80211_frame *wh;
736 u_int8_t subtype;
737 u_int8_t *frm, *efrm;
738
739 wh = mtod(m, struct ieee80211_frame *);
740
741 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
742 return;
743
744 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
745
746 if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
747 subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
748 return;
749
750 frm = (u_int8_t *)(wh + 1);
751 efrm = mtod(m, u_int8_t *) + m->m_len;
752
753 frm += 12; /* skip tstamp, bintval and capinfo fields */
754 while (frm < efrm) {
755 if (*frm == IEEE80211_ELEMID_DSPARMS)
756 #if IEEE80211_CHAN_MAX < 255
757 if (frm[2] <= IEEE80211_CHAN_MAX)
758 #endif
759 ic->ic_bss->ni_chan = &ic->ic_channels[frm[2]];
760
761 frm += frm[1] + 2;
762 }
763 }
764
765 static void
766 iwi_frame_intr(struct iwi_softc *sc, struct iwi_rx_buf *buf, int i,
767 struct iwi_frame *frame)
768 {
769 struct ieee80211com *ic = &sc->sc_ic;
770 struct ifnet *ifp = &sc->sc_if;
771 struct mbuf *m;
772 struct ieee80211_frame_min *wh;
773 struct ieee80211_node *ni;
774 int error;
775
776 DPRINTFN(5, ("RX!DATA!%u!%u!%u\n", le16toh(frame->len), frame->chan,
777 frame->rssi_dbm));
778
779 bus_dmamap_sync(sc->sc_dmat, buf->map, sizeof (struct iwi_hdr),
780 sizeof (struct iwi_frame) + le16toh(frame->len),
781 BUS_DMASYNC_POSTREAD);
782
783 if (le16toh(frame->len) < sizeof (struct ieee80211_frame_min) ||
784 le16toh(frame->len) > MCLBYTES) {
785 aprint_error("%s: bad frame length\n", sc->sc_dev.dv_xname);
786 }
787
788 bus_dmamap_unload(sc->sc_dmat, buf->map);
789
790 /* Finalize mbuf */
791 m = buf->m;
792 m->m_pkthdr.rcvif = ifp;
793 m->m_pkthdr.len = m->m_len = sizeof (struct iwi_hdr) +
794 sizeof (struct iwi_frame) + le16toh(frame->len);
795
796 m_adj(m, sizeof (struct iwi_hdr) + sizeof (struct iwi_frame));
797
798 wh = mtod(m, struct ieee80211_frame_min *);
799
800 #if NBPFILTER > 0
801 if (sc->sc_drvbpf != NULL) {
802 struct iwi_rx_radiotap_header *tap = &sc->sc_rxtap;
803
804 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m);
805 }
806 #endif
807
808 if (ic->ic_state == IEEE80211_S_SCAN)
809 iwi_fix_channel(ic, m);
810
811 ni = ieee80211_find_rxnode(ic, wh);
812
813 /* Send the frame to the upper layer */
814 ieee80211_input(ic, m, ni, IWI_RSSIDBM2RAW(frame->rssi_dbm), 0);
815
816 ieee80211_free_node(ni);
817
818 MGETHDR(buf->m, M_DONTWAIT, MT_DATA);
819 if (buf->m == NULL) {
820 aprint_error("%s: could not allocate rx mbuf\n",
821 sc->sc_dev.dv_xname);
822 return;
823 }
824
825 MCLGET(buf->m, M_DONTWAIT);
826 if (!(buf->m->m_flags & M_EXT)) {
827 aprint_error("%s: could not allocate rx mbuf cluster\n",
828 sc->sc_dev.dv_xname);
829 m_freem(buf->m);
830 buf->m = NULL;
831 return;
832 }
833
834 error = bus_dmamap_load(sc->sc_dmat, buf->map, mtod(buf->m, void *),
835 MCLBYTES, NULL, BUS_DMA_NOWAIT);
836 if (error != 0) {
837 aprint_error("%s: could not load rx buf DMA map\n",
838 sc->sc_dev.dv_xname);
839 m_freem(buf->m);
840 buf->m = NULL;
841 return;
842 }
843
844 CSR_WRITE_4(sc, IWI_CSR_RX_BASE + i * 4, buf->map->dm_segs[0].ds_addr);
845 }
846
847 static void
848 iwi_notification_intr(struct iwi_softc *sc, struct iwi_rx_buf *buf,
849 struct iwi_notif *notif)
850 {
851 struct ieee80211com *ic = &sc->sc_ic;
852 struct iwi_notif_scan_channel *chan;
853 struct iwi_notif_scan_complete *scan;
854 struct iwi_notif_authentication *auth;
855 struct iwi_notif_association *assoc;
856
857 bus_dmamap_sync(sc->sc_dmat, buf->map, sizeof (struct iwi_hdr),
858 sizeof (struct iwi_notif) + le16toh(notif->len),
859 BUS_DMASYNC_POSTREAD);
860
861 switch (notif->type) {
862 case IWI_NOTIF_TYPE_SCAN_CHANNEL:
863 chan = (struct iwi_notif_scan_channel *)(notif + 1);
864
865 DPRINTFN(2, ("Scan channel (%u)\n", chan->nchan));
866 break;
867
868 case IWI_NOTIF_TYPE_SCAN_COMPLETE:
869 scan = (struct iwi_notif_scan_complete *)(notif + 1);
870
871 DPRINTFN(2, ("Scan completed (%u, %u)\n", scan->nchan,
872 scan->status));
873
874 /* monitor mode uses scan to set the channel ... */
875 if (ic->ic_opmode != IEEE80211_M_MONITOR)
876 ieee80211_end_scan(ic);
877 else
878 iwi_set_chan(sc, ic->ic_ibss_chan);
879 break;
880
881 case IWI_NOTIF_TYPE_AUTHENTICATION:
882 auth = (struct iwi_notif_authentication *)(notif + 1);
883
884 DPRINTFN(2, ("Authentication (%u)\n", auth->state));
885
886 switch (auth->state) {
887 case IWI_AUTHENTICATED:
888 ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1);
889 break;
890
891 case IWI_DEAUTHENTICATED:
892 break;
893
894 default:
895 aprint_error("%s: unknown authentication state %u\n",
896 sc->sc_dev.dv_xname, auth->state);
897 }
898 break;
899
900 case IWI_NOTIF_TYPE_ASSOCIATION:
901 assoc = (struct iwi_notif_association *)(notif + 1);
902
903 DPRINTFN(2, ("Association (%u, %u)\n", assoc->state,
904 assoc->status));
905
906 switch (assoc->state) {
907 case IWI_ASSOCIATED:
908 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
909 break;
910
911 case IWI_DEASSOCIATED:
912 ieee80211_begin_scan(ic, 0);
913 break;
914
915 default:
916 aprint_error("%s: unknown association state %u\n",
917 sc->sc_dev.dv_xname, assoc->state);
918 }
919 break;
920
921 case IWI_NOTIF_TYPE_CALIBRATION:
922 case IWI_NOTIF_TYPE_BEACON:
923 case IWI_NOTIF_TYPE_NOISE:
924 DPRINTFN(5, ("Notification (%u)\n", notif->type));
925 break;
926
927 default:
928 aprint_error("%s: unknown notification type %u\n",
929 sc->sc_dev.dv_xname, notif->type);
930 }
931 }
932
933 static void
934 iwi_rx_intr(struct iwi_softc *sc)
935 {
936 struct iwi_rx_buf *buf;
937 struct iwi_hdr *hdr;
938 u_int32_t r, i;
939
940 r = CSR_READ_4(sc, IWI_CSR_RX_READ_INDEX);
941
942 for (i = (sc->rx_cur + 1) % IWI_RX_RING_SIZE; i != r;
943 i = (i + 1) % IWI_RX_RING_SIZE) {
944
945 buf = &sc->rx_buf[i];
946
947 bus_dmamap_sync(sc->sc_dmat, buf->map, 0,
948 sizeof (struct iwi_hdr), BUS_DMASYNC_POSTREAD);
949
950 hdr = mtod(buf->m, struct iwi_hdr *);
951
952 switch (hdr->type) {
953 case IWI_HDR_TYPE_FRAME:
954 iwi_frame_intr(sc, buf, i,
955 (struct iwi_frame *)(hdr + 1));
956 break;
957
958 case IWI_HDR_TYPE_NOTIF:
959 iwi_notification_intr(sc, buf,
960 (struct iwi_notif *)(hdr + 1));
961 break;
962
963 default:
964 aprint_error("%s: unknown hdr type %u\n",
965 sc->sc_dev.dv_xname, hdr->type);
966 }
967 }
968
969 /* Tell the firmware what we have processed */
970 sc->rx_cur = (r == 0) ? IWI_RX_RING_SIZE - 1 : r - 1;
971 CSR_WRITE_4(sc, IWI_CSR_RX_WRITE_INDEX, sc->rx_cur);
972 }
973
974 static void
975 iwi_tx_intr(struct iwi_softc *sc)
976 {
977 struct ifnet *ifp = &sc->sc_if;
978 struct iwi_tx_buf *buf;
979 u_int32_t r, i;
980
981 r = CSR_READ_4(sc, IWI_CSR_TX1_READ_INDEX);
982
983 for (i = (sc->tx_old + 1) % IWI_TX_RING_SIZE; i != r;
984 i = (i + 1) % IWI_TX_RING_SIZE) {
985
986 buf = &sc->tx_buf[i];
987
988 bus_dmamap_unload(sc->sc_dmat, buf->map);
989 m_freem(buf->m);
990 buf->m = NULL;
991 ieee80211_free_node(buf->ni);
992 buf->ni = NULL;
993
994 sc->tx_queued--;
995
996 /* kill watchdog timer */
997 sc->sc_tx_timer = 0;
998 }
999
1000 /* Remember what the firmware has processed */
1001 sc->tx_old = (r == 0) ? IWI_TX_RING_SIZE - 1 : r - 1;
1002
1003 /* Call start() since some buffer descriptors have been released */
1004 ifp->if_flags &= ~IFF_OACTIVE;
1005 (*ifp->if_start)(ifp);
1006 }
1007
1008 static int
1009 iwi_intr(void *arg)
1010 {
1011 struct iwi_softc *sc = arg;
1012 u_int32_t r;
1013
1014 if ((r = CSR_READ_4(sc, IWI_CSR_INTR)) == 0 || r == 0xffffffff)
1015 return 0;
1016
1017 /* Disable interrupts */
1018 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
1019
1020 DPRINTFN(8, ("INTR!0x%08x\n", r));
1021
1022 if (r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)) {
1023 aprint_error("%s: fatal error\n", sc->sc_dev.dv_xname);
1024 iwi_stop(&sc->sc_if, 1);
1025 }
1026
1027 if (r & IWI_INTR_FW_INITED) {
1028 if (!(r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)))
1029 wakeup(sc);
1030 }
1031
1032 if (r & IWI_INTR_RADIO_OFF) {
1033 DPRINTF(("radio transmitter off\n"));
1034 iwi_stop(&sc->sc_if, 1);
1035 }
1036
1037 if (r & IWI_INTR_RX_TRANSFER)
1038 iwi_rx_intr(sc);
1039
1040 if (r & IWI_INTR_CMD_TRANSFER)
1041 wakeup(sc);
1042
1043 if (r & IWI_INTR_TX1_TRANSFER)
1044 iwi_tx_intr(sc);
1045
1046 /* Acknowledge interrupts */
1047 CSR_WRITE_4(sc, IWI_CSR_INTR, r);
1048
1049 /* Re-enable interrupts */
1050 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
1051
1052 return 1;
1053 }
1054
1055 static int
1056 iwi_cmd(struct iwi_softc *sc, u_int8_t type, void *data, u_int8_t len,
1057 int async)
1058 {
1059 struct iwi_cmd_desc *desc;
1060
1061 DPRINTFN(2, ("TX!CMD!%u!%u\n", type, len));
1062
1063 desc = &sc->cmd_desc[sc->cmd_cur];
1064 desc->hdr.type = IWI_HDR_TYPE_COMMAND;
1065 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1066 desc->type = type;
1067 desc->len = len;
1068 memcpy(desc->data, data, len);
1069
1070 bus_dmamap_sync(sc->sc_dmat, sc->cmd_ring_map,
1071 sc->cmd_cur * sizeof (struct iwi_cmd_desc),
1072 sizeof (struct iwi_cmd_desc), BUS_DMASYNC_PREWRITE);
1073
1074 sc->cmd_cur = (sc->cmd_cur + 1) % IWI_CMD_RING_SIZE;
1075 CSR_WRITE_4(sc, IWI_CSR_CMD_WRITE_INDEX, sc->cmd_cur);
1076
1077 return async ? 0 : tsleep(sc, 0, "iwicmd", hz);
1078 }
1079
1080 static int
1081 iwi_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni)
1082 {
1083 struct iwi_softc *sc = ifp->if_softc;
1084 struct ieee80211com *ic = &sc->sc_ic;
1085 struct ieee80211_frame *wh;
1086 struct iwi_tx_buf *buf;
1087 struct iwi_tx_desc *desc;
1088 struct mbuf *mnew;
1089 int error, i;
1090
1091 #if NBPFILTER > 0
1092 if (sc->sc_drvbpf != NULL) {
1093 struct iwi_tx_radiotap_header *tap = &sc->sc_txtap;
1094
1095 tap->wt_flags = 0;
1096 tap->wt_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
1097 tap->wt_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
1098
1099 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1100 }
1101 #endif
1102
1103 buf = &sc->tx_buf[sc->tx_cur];
1104 desc = &sc->tx_desc[sc->tx_cur];
1105
1106 wh = mtod(m0, struct ieee80211_frame *);
1107
1108 /* trim IEEE802.11 header */
1109 m_adj(m0, sizeof (struct ieee80211_frame));
1110
1111 error = bus_dmamap_load_mbuf(sc->sc_dmat, buf->map, m0, BUS_DMA_NOWAIT);
1112 if (error != 0 && error != EFBIG) {
1113 aprint_error("%s: could not map mbuf (error %d)\n",
1114 sc->sc_dev.dv_xname, error);
1115 m_freem(m0);
1116 return error;
1117 }
1118 if (error != 0) {
1119 /* too many fragments, linearize */
1120
1121 MGETHDR(mnew, M_DONTWAIT, MT_DATA);
1122 if (mnew == NULL) {
1123 m_freem(m0);
1124 return ENOMEM;
1125 }
1126
1127 M_COPY_PKTHDR(mnew, m0);
1128 MCLGET(mnew, M_DONTWAIT);
1129 if (!(mnew->m_flags & M_EXT)) {
1130 m_freem(m0);
1131 m_freem(mnew);
1132 return ENOMEM;
1133 }
1134
1135 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(mnew, caddr_t));
1136 m_freem(m0);
1137 mnew->m_len = mnew->m_pkthdr.len;
1138 m0 = mnew;
1139
1140 error = bus_dmamap_load_mbuf(sc->sc_dmat, buf->map, m0,
1141 BUS_DMA_NOWAIT);
1142 if (error != 0) {
1143 aprint_error("%s: could not map mbuf (error %d)\n",
1144 sc->sc_dev.dv_xname, error);
1145 m_freem(m0);
1146 return error;
1147 }
1148 }
1149
1150 buf->m = m0;
1151 buf->ni = ni;
1152
1153 desc->hdr.type = IWI_HDR_TYPE_DATA;
1154 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1155 desc->cmd = IWI_DATA_CMD_TX;
1156 desc->len = htole16(m0->m_pkthdr.len);
1157 desc->flags = 0;
1158 if (ic->ic_opmode == IEEE80211_M_IBSS) {
1159 if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
1160 desc->flags |= IWI_DATA_FLAG_NEED_ACK;
1161 } else if (!IEEE80211_IS_MULTICAST(wh->i_addr3))
1162 desc->flags |= IWI_DATA_FLAG_NEED_ACK;
1163
1164 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
1165 wh->i_fc[1] |= IEEE80211_FC1_WEP;
1166 desc->wep_txkey = ic->ic_def_txkey;
1167 } else
1168 desc->flags |= IWI_DATA_FLAG_NO_WEP;
1169
1170 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1171 desc->flags |= IWI_DATA_FLAG_SHPREAMBLE;
1172
1173 memcpy(&desc->wh, wh, sizeof (struct ieee80211_frame));
1174 desc->nseg = htole32(buf->map->dm_nsegs);
1175 for (i = 0; i < buf->map->dm_nsegs; i++) {
1176 desc->seg_addr[i] = htole32(buf->map->dm_segs[i].ds_addr);
1177 desc->seg_len[i] = htole32(buf->map->dm_segs[i].ds_len);
1178 }
1179
1180 bus_dmamap_sync(sc->sc_dmat, sc->tx_ring_map,
1181 sc->tx_cur * sizeof (struct iwi_tx_desc),
1182 sizeof (struct iwi_tx_desc), BUS_DMASYNC_PREWRITE);
1183
1184 bus_dmamap_sync(sc->sc_dmat, buf->map, 0, MCLBYTES,
1185 BUS_DMASYNC_PREWRITE);
1186
1187 DPRINTFN(5, ("TX!DATA!%u!%u\n", desc->len, desc->nseg));
1188
1189 /* Inform firmware about this new packet */
1190 sc->tx_queued++;
1191 sc->tx_cur = (sc->tx_cur + 1) % IWI_TX_RING_SIZE;
1192 CSR_WRITE_4(sc, IWI_CSR_TX1_WRITE_INDEX, sc->tx_cur);
1193
1194 return 0;
1195 }
1196
1197 static void
1198 iwi_start(struct ifnet *ifp)
1199 {
1200 struct iwi_softc *sc = ifp->if_softc;
1201 struct ieee80211com *ic = &sc->sc_ic;
1202 struct mbuf *m0;
1203 struct ieee80211_node *ni;
1204
1205 if (ic->ic_state != IEEE80211_S_RUN)
1206 return;
1207
1208 for (;;) {
1209 IF_DEQUEUE(&ifp->if_snd, m0);
1210 if (m0 == NULL)
1211 break;
1212
1213 if (sc->tx_queued >= IWI_TX_RING_SIZE - 4) {
1214 IF_PREPEND(&ifp->if_snd, m0);
1215 ifp->if_flags |= IFF_OACTIVE;
1216 break;
1217 }
1218
1219 #if NBPFILTER > 0
1220 if (ifp->if_bpf != NULL)
1221 bpf_mtap(ifp->if_bpf, m0);
1222 #endif
1223
1224 if ((ni = ieee80211_find_txnode(ic,
1225 mtod(m0, struct ether_header *)->ether_dhost)) == NULL) {
1226 m_freem(m0);
1227 continue;
1228 }
1229 m0 = ieee80211_encap(ic, m0, ni);
1230 if (m0 == NULL)
1231 continue;
1232
1233 #if NBPFILTER > 0
1234 if (ic->ic_rawbpf != NULL)
1235 bpf_mtap(ic->ic_rawbpf, m0);
1236 #endif
1237
1238 if (iwi_tx_start(ifp, m0, ni) != 0) {
1239 if (ni != NULL)
1240 ieee80211_free_node(ni);
1241 break;
1242 }
1243
1244 /* start watchdog timer */
1245 sc->sc_tx_timer = 5;
1246 ifp->if_timer = 1;
1247 }
1248 }
1249
1250 static void
1251 iwi_watchdog(struct ifnet *ifp)
1252 {
1253 struct iwi_softc *sc = ifp->if_softc;
1254
1255 ifp->if_timer = 0;
1256
1257 if (sc->sc_tx_timer > 0) {
1258 if (--sc->sc_tx_timer == 0) {
1259 aprint_error("%s: device timeout\n",
1260 sc->sc_dev.dv_xname);
1261 iwi_stop(ifp, 1);
1262 return;
1263 }
1264 ifp->if_timer = 1;
1265 }
1266
1267 ieee80211_watchdog(&sc->sc_ic);
1268 }
1269
1270 static int
1271 iwi_get_table0(struct iwi_softc *sc, u_int32_t *tbl)
1272 {
1273 u_int32_t size, buf[128];
1274
1275 if (!(sc->flags & IWI_FLAG_FW_INITED)) {
1276 memset(buf, 0, sizeof buf);
1277 return copyout(buf, tbl, sizeof buf);
1278 }
1279
1280 size = min(CSR_READ_4(sc, IWI_CSR_TABLE0_SIZE), 128 - 1);
1281 CSR_READ_REGION_4(sc, IWI_CSR_TABLE0_BASE, &buf[1], size);
1282
1283 return copyout(buf, tbl, sizeof buf);
1284 }
1285
1286 static int
1287 iwi_get_radio(struct iwi_softc *sc, int *ret)
1288 {
1289 int val;
1290
1291 val = (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) ? 1 : 0;
1292 return copyout(&val, ret, sizeof val);
1293 }
1294
1295 static int
1296 iwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1297 {
1298 struct iwi_softc *sc = ifp->if_softc;
1299 struct ifreq *ifr;
1300 int s, error = 0;
1301
1302 s = splnet();
1303
1304 switch (cmd) {
1305 case SIOCSIFFLAGS:
1306 if (ifp->if_flags & IFF_UP) {
1307 if (!(ifp->if_flags & IFF_RUNNING))
1308 iwi_init(ifp);
1309 } else {
1310 if (ifp->if_flags & IFF_RUNNING)
1311 iwi_stop(ifp, 1);
1312 }
1313 break;
1314
1315 case SIOCGTABLE0:
1316 ifr = (struct ifreq *)data;
1317 error = iwi_get_table0(sc, (u_int32_t *)ifr->ifr_data);
1318 break;
1319
1320 case SIOCGRADIO:
1321 ifr = (struct ifreq *)data;
1322 error = iwi_get_radio(sc, (int *)ifr->ifr_data);
1323 break;
1324
1325 case SIOCSLOADFW:
1326 /* only super-user can do that! */
1327 if ((error = suser(curproc->p_ucred, &curproc->p_acflag)) != 0)
1328 break;
1329
1330 ifr = (struct ifreq *)data;
1331 error = iwi_cache_firmware(sc, ifr->ifr_data);
1332 break;
1333
1334 case SIOCSKILLFW:
1335 /* only super-user can do that! */
1336 if ((error = suser(curproc->p_ucred, &curproc->p_acflag)) != 0)
1337 break;
1338
1339 iwi_stop(ifp, 1);
1340 iwi_free_firmware(sc);
1341 break;
1342
1343 case SIOCG80211AUTH:
1344 ((struct ieee80211_auth *)data)->i_authtype = sc->authmode;
1345 break;
1346
1347 case SIOCS80211AUTH:
1348 /* only super-user can do that! */
1349 if ((error = suser(curproc->p_ucred, &curproc->p_acflag)) != 0)
1350 break;
1351
1352 sc->authmode = ((struct ieee80211_auth *)data)->i_authtype;
1353 break;
1354
1355 default:
1356 error = ieee80211_ioctl(&sc->sc_ic, cmd, data);
1357 }
1358
1359 if (error == ENETRESET && cmd != SIOCADDMULTI) {
1360 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1361 (IFF_UP | IFF_RUNNING))
1362 iwi_init(ifp);
1363 error = 0;
1364 }
1365
1366 splx(s);
1367 return error;
1368 }
1369
1370 static void
1371 iwi_stop_master(struct iwi_softc *sc)
1372 {
1373 int ntries;
1374
1375 /* Disable interrupts */
1376 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
1377
1378 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER);
1379 for (ntries = 0; ntries < 5; ntries++) {
1380 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
1381 break;
1382 DELAY(10);
1383 }
1384 if (ntries == 5)
1385 aprint_error("%s: timeout waiting for master\n",
1386 sc->sc_dev.dv_xname);
1387
1388 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
1389 IWI_RST_PRINCETON_RESET);
1390
1391 sc->flags &= ~IWI_FLAG_FW_INITED;
1392 }
1393
1394 static int
1395 iwi_reset(struct iwi_softc *sc)
1396 {
1397 int i, ntries;
1398
1399 iwi_stop_master(sc);
1400
1401 /* Move adapter to D0 state */
1402 CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
1403 IWI_CTL_INIT);
1404
1405 /* Initialize Phase-Locked Level (PLL) */
1406 CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST);
1407
1408 /* Wait for clock stabilization */
1409 for (ntries = 0; ntries < 1000; ntries++) {
1410 if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY)
1411 break;
1412 DELAY(200);
1413 }
1414 if (ntries == 1000)
1415 return EIO;
1416
1417 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
1418 IWI_RST_SW_RESET);
1419
1420 DELAY(10);
1421
1422 CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
1423 IWI_CTL_INIT);
1424
1425 /* Clear NIC memory */
1426 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0);
1427 for (i = 0; i < 0xc000; i++)
1428 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
1429
1430 return 0;
1431 }
1432
1433 static int
1434 iwi_load_ucode(struct iwi_softc *sc, void *uc, int size)
1435 {
1436 u_int16_t *w;
1437 int ntries, i;
1438
1439 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
1440 IWI_RST_STOP_MASTER);
1441 for (ntries = 0; ntries < 5; ntries++) {
1442 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
1443 break;
1444 DELAY(10);
1445 }
1446 if (ntries == 5) {
1447 aprint_error("%s: timeout waiting for master\n",
1448 sc->sc_dev.dv_xname);
1449 return EIO;
1450 }
1451
1452 MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
1453 DELAY(5000);
1454 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) &
1455 ~IWI_RST_PRINCETON_RESET);
1456 DELAY(5000);
1457 MEM_WRITE_4(sc, 0x3000e0, 0);
1458 DELAY(1000);
1459 MEM_WRITE_4(sc, 0x300004, 1);
1460 DELAY(1000);
1461 MEM_WRITE_4(sc, 0x300004, 0);
1462 DELAY(1000);
1463 MEM_WRITE_1(sc, 0x200000, 0x00);
1464 MEM_WRITE_1(sc, 0x200000, 0x40);
1465
1466 /* Adapter is buggy, we must set the address for each word */
1467 for (w = uc; size > 0; w++, size -= 2)
1468 MEM_WRITE_2(sc, 0x200010, *w);
1469
1470 MEM_WRITE_1(sc, 0x200000, 0x00);
1471 MEM_WRITE_1(sc, 0x200000, 0x80);
1472
1473 /* Wait until we get a response in the uc queue */
1474 for (ntries = 0; ntries < 100; ntries++) {
1475 if (MEM_READ_1(sc, 0x200000) & 1)
1476 break;
1477 DELAY(100);
1478 }
1479 if (ntries == 100) {
1480 aprint_error("%s: timeout waiting for ucode to initialize\n",
1481 sc->sc_dev.dv_xname);
1482 return EIO;
1483 }
1484
1485 /* Empty the uc queue or the firmware will not initialize properly */
1486 for (i = 0; i < 7; i++)
1487 MEM_READ_4(sc, 0x200004);
1488
1489 MEM_WRITE_1(sc, 0x200000, 0x00);
1490
1491 return 0;
1492 }
1493
1494 /* macro to handle unaligned little endian data in firmware image */
1495 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
1496 static int
1497 iwi_load_firmware(struct iwi_softc *sc, void *fw, int size)
1498 {
1499 bus_dmamap_t map;
1500 bus_dma_segment_t seg;
1501 caddr_t virtaddr;
1502 u_char *p, *end;
1503 u_int32_t sentinel, ctl, src, dst, sum, len, mlen;
1504 int ntries, nsegs, error;
1505
1506 /* Allocate DMA memory for storing firmware image */
1507 error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
1508 BUS_DMA_NOWAIT, &map);
1509 if (error != 0) {
1510 aprint_error("%s: could not create firmware DMA map\n",
1511 sc->sc_dev.dv_xname);
1512 goto fail1;
1513 }
1514
1515 /*
1516 * We cannot map fw directly because of some hardware constraints on
1517 * the mapping address.
1518 */
1519 error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg, 1,
1520 &nsegs, BUS_DMA_NOWAIT);
1521 if (error != 0) {
1522 aprint_error("%s: could not allocate firmware DMA memory\n",
1523 sc->sc_dev.dv_xname);
1524 goto fail2;
1525 }
1526
1527 error = bus_dmamem_map(sc->sc_dmat, &seg, nsegs, size, &virtaddr,
1528 BUS_DMA_NOWAIT);
1529 if (error != 0) {
1530 aprint_error("%s: could not load firmware DMA map\n",
1531 sc->sc_dev.dv_xname);
1532 goto fail3;
1533 }
1534
1535 error = bus_dmamap_load(sc->sc_dmat, map, virtaddr, size, NULL,
1536 BUS_DMA_NOWAIT);
1537 if (error != 0) {
1538 aprint_error("%s: could not load fw dma map\n",
1539 sc->sc_dev.dv_xname);
1540 goto fail4;
1541 }
1542
1543 /* Copy firmware image to DMA memory */
1544 memcpy(virtaddr, fw, size);
1545
1546 /* Make sure the adapter will get up-to-date values */
1547 bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_PREWRITE);
1548
1549 /* Tell the adapter where the command blocks are stored */
1550 MEM_WRITE_4(sc, 0x3000a0, 0x27000);
1551
1552 /*
1553 * Store command blocks into adapter's internal memory using register
1554 * indirections. The adapter will read the firmware image through DMA
1555 * using information stored in command blocks.
1556 */
1557 src = map->dm_segs[0].ds_addr;
1558 p = virtaddr;
1559 end = p + size;
1560 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000);
1561
1562 while (p < end) {
1563 dst = GETLE32(p); p += 4; src += 4;
1564 len = GETLE32(p); p += 4; src += 4;
1565 p += len;
1566
1567 while (len > 0) {
1568 mlen = min(len, IWI_CB_MAXDATALEN);
1569
1570 ctl = IWI_CB_DEFAULT_CTL | mlen;
1571 sum = ctl ^ src ^ dst;
1572
1573 /* Write a command block */
1574 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, ctl);
1575 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, src);
1576 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, dst);
1577 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum);
1578
1579 src += mlen;
1580 dst += mlen;
1581 len -= mlen;
1582 }
1583 }
1584
1585 /* Write a fictive final command block (sentinel) */
1586 sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR);
1587 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
1588
1589 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) &
1590 ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER));
1591
1592 /* Tell the adapter to start processing command blocks */
1593 MEM_WRITE_4(sc, 0x3000a4, 0x540100);
1594
1595 /* Wait until the adapter has processed all command blocks */
1596 for (ntries = 0; ntries < 400; ntries++) {
1597 if (MEM_READ_4(sc, 0x3000d0) >= sentinel)
1598 break;
1599 DELAY(100);
1600 }
1601 if (ntries == 400) {
1602 aprint_error("%s: timeout processing cb\n",
1603 sc->sc_dev.dv_xname);
1604 error = EIO;
1605 goto fail5;
1606 }
1607
1608 /* We're done with command blocks processing */
1609 MEM_WRITE_4(sc, 0x3000a4, 0x540c00);
1610
1611 /* Allow interrupts so we know when the firmware is inited */
1612 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
1613
1614 /* Tell the adapter to initialize the firmware */
1615 CSR_WRITE_4(sc, IWI_CSR_RST, 0);
1616 CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
1617 IWI_CTL_ALLOW_STANDBY);
1618
1619 /* Wait at most one second for firmware initialization to complete */
1620 if ((error = tsleep(sc, 0, "iwiinit", hz)) != 0) {
1621 aprint_error("%s: timeout waiting for firmware initialization "
1622 "to complete\n", sc->sc_dev.dv_xname);
1623 goto fail5;
1624 }
1625
1626 fail5: bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_POSTWRITE);
1627 bus_dmamap_unload(sc->sc_dmat, map);
1628 fail4: bus_dmamem_unmap(sc->sc_dmat, virtaddr, size);
1629 fail3: bus_dmamem_free(sc->sc_dmat, &seg, 1);
1630 fail2: bus_dmamap_destroy(sc->sc_dmat, map);
1631
1632 fail1: return error;
1633 }
1634
1635 /*
1636 * Store firmware into kernel memory so we can download it when we need to,
1637 * e.g when the adapter wakes up from suspend mode.
1638 */
1639 static int
1640 iwi_cache_firmware(struct iwi_softc *sc, void *data)
1641 {
1642 struct iwi_firmware *kfw = &sc->fw;
1643 struct iwi_firmware ufw;
1644 int error;
1645
1646 iwi_free_firmware(sc);
1647
1648 if ((error = copyin(data, &ufw, sizeof ufw)) != 0)
1649 goto fail1;
1650
1651 kfw->boot_size = ufw.boot_size;
1652 kfw->ucode_size = ufw.ucode_size;
1653 kfw->main_size = ufw.main_size;
1654
1655 kfw->boot = malloc(kfw->boot_size, M_DEVBUF, M_NOWAIT);
1656 if (kfw->boot == NULL) {
1657 error = ENOMEM;
1658 goto fail1;
1659 }
1660
1661 kfw->ucode = malloc(kfw->ucode_size, M_DEVBUF, M_NOWAIT);
1662 if (kfw->ucode == NULL) {
1663 error = ENOMEM;
1664 goto fail2;
1665 }
1666
1667 kfw->main = malloc(kfw->main_size, M_DEVBUF, M_NOWAIT);
1668 if (kfw->main == NULL) {
1669 error = ENOMEM;
1670 goto fail3;
1671 }
1672
1673 if ((error = copyin(ufw.boot, kfw->boot, kfw->boot_size)) != 0)
1674 goto fail4;
1675
1676 if ((error = copyin(ufw.ucode, kfw->ucode, kfw->ucode_size)) != 0)
1677 goto fail4;
1678
1679 if ((error = copyin(ufw.main, kfw->main, kfw->main_size)) != 0)
1680 goto fail4;
1681
1682 DPRINTF(("Firmware cached: boot %u, ucode %u, main %u\n",
1683 kfw->boot_size, kfw->ucode_size, kfw->main_size));
1684
1685 sc->flags |= IWI_FLAG_FW_CACHED;
1686
1687 return 0;
1688
1689 fail4: free(kfw->boot, M_DEVBUF);
1690 fail3: free(kfw->ucode, M_DEVBUF);
1691 fail2: free(kfw->main, M_DEVBUF);
1692 fail1:
1693 return error;
1694 }
1695
1696 static void
1697 iwi_free_firmware(struct iwi_softc *sc)
1698 {
1699 if (!(sc->flags & IWI_FLAG_FW_CACHED))
1700 return;
1701
1702 free(sc->fw.boot, M_DEVBUF);
1703 free(sc->fw.ucode, M_DEVBUF);
1704 free(sc->fw.main, M_DEVBUF);
1705
1706 sc->flags &= ~IWI_FLAG_FW_CACHED;
1707 }
1708
1709 static int
1710 iwi_config(struct iwi_softc *sc)
1711 {
1712 struct ieee80211com *ic = &sc->sc_ic;
1713 struct ifnet *ifp = &sc->sc_if;
1714 struct iwi_configuration config;
1715 struct iwi_rateset rs;
1716 struct iwi_txpower power;
1717 struct ieee80211_key *k;
1718 struct iwi_wep_key wepkey;
1719 u_int32_t data;
1720 int error, i;
1721
1722 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
1723 DPRINTF(("Setting MAC address to %s\n", ether_sprintf(ic->ic_myaddr)));
1724 error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, ic->ic_myaddr,
1725 IEEE80211_ADDR_LEN, 0);
1726 if (error != 0)
1727 return error;
1728
1729 memset(&config, 0, sizeof config);
1730 config.bluetooth_coexistence = 1;
1731 config.multicast_enabled = 1;
1732 config.noise_reported = 1;
1733 DPRINTF(("Configuring adapter\n"));
1734 error = iwi_cmd(sc, IWI_CMD_SET_CONFIGURATION, &config, sizeof config,
1735 0);
1736 if (error != 0)
1737 return error;
1738
1739 data = htole32(IWI_POWER_MODE_CAM);
1740 DPRINTF(("Setting power mode to %u\n", le32toh(data)));
1741 error = iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data, 0);
1742 if (error != 0)
1743 return error;
1744
1745 data = htole32(ic->ic_rtsthreshold);
1746 DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
1747 error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data, 0);
1748 if (error != 0)
1749 return error;
1750
1751 if (ic->ic_opmode == IEEE80211_M_IBSS) {
1752 power.mode = IWI_MODE_11B;
1753 power.nchan = 11;
1754 for (i = 0; i < 11; i++) {
1755 power.chan[i].chan = i + 1;
1756 power.chan[i].power = IWI_TXPOWER_MAX;
1757 }
1758 DPRINTF(("Setting .11b channels tx power\n"));
1759 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power,
1760 0);
1761 if (error != 0)
1762 return error;
1763
1764 power.mode = IWI_MODE_11G;
1765 DPRINTF(("Setting .11g channels tx power\n"));
1766 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power,
1767 0);
1768 if (error != 0)
1769 return error;
1770 }
1771
1772 rs.mode = IWI_MODE_11G;
1773 rs.type = IWI_RATESET_TYPE_SUPPORTED;
1774 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates;
1775 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11G].rs_rates,
1776 rs.nrates);
1777 DPRINTF(("Setting .11bg supported rates (%u)\n", rs.nrates));
1778 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
1779 if (error != 0)
1780 return error;
1781
1782 rs.mode = IWI_MODE_11A;
1783 rs.type = IWI_RATESET_TYPE_SUPPORTED;
1784 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates;
1785 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11A].rs_rates,
1786 rs.nrates);
1787 DPRINTF(("Setting .11a supported rates (%u)\n", rs.nrates));
1788 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
1789 if (error != 0)
1790 return error;
1791
1792 data = htole32(arc4random());
1793 DPRINTF(("Setting initialization vector to %u\n", le32toh(data)));
1794 error = iwi_cmd(sc, IWI_CMD_SET_IV, &data, sizeof data, 0);
1795 if (error != 0)
1796 return error;
1797
1798 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
1799 k = ic->ic_nw_keys;
1800 for (i = 0; i < IEEE80211_WEP_NKID; i++, k++) {
1801 wepkey.cmd = IWI_WEP_KEY_CMD_SETKEY;
1802 wepkey.idx = i;
1803 wepkey.len = k->wk_keylen;
1804 memset(wepkey.key, 0, sizeof wepkey.key);
1805 memcpy(wepkey.key, k->wk_key, k->wk_keylen);
1806 DPRINTF(("Setting wep key index %u len %u\n",
1807 wepkey.idx, wepkey.len));
1808 error = iwi_cmd(sc, IWI_CMD_SET_WEP_KEY, &wepkey,
1809 sizeof wepkey, 0);
1810 if (error != 0)
1811 return error;
1812 }
1813 }
1814
1815 /* Enable adapter */
1816 DPRINTF(("Enabling adapter\n"));
1817 return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0, 0);
1818 }
1819
1820 static int
1821 iwi_set_chan(struct iwi_softc *sc, struct ieee80211_channel *chan)
1822 {
1823 struct ieee80211com *ic = &sc->sc_ic;
1824 struct iwi_scan scan;
1825
1826 bzero(&scan, sizeof scan);
1827 scan.type = IWI_SCAN_TYPE_PASSIVE;
1828 scan.intval = htole16(2000);
1829 scan.channels[0] = 1 | (IEEE80211_IS_CHAN_5GHZ(chan) ? IWI_CHAN_5GHZ :
1830 IWI_CHAN_2GHZ);
1831 scan.channels[1] = ieee80211_chan2ieee(ic, chan);
1832
1833 DPRINTF(("Setting channel to %u\n", ieee80211_chan2ieee(ic, chan)));
1834 return iwi_cmd(sc, IWI_CMD_SCAN, &scan, sizeof scan, 1);
1835 }
1836
1837
1838 static int
1839 iwi_scan(struct iwi_softc *sc)
1840 {
1841 struct ieee80211com *ic = &sc->sc_ic;
1842 struct iwi_scan scan;
1843 u_int8_t *p;
1844 int i, count;
1845
1846 memset(&scan, 0, sizeof scan);
1847 scan.type = IWI_SCAN_TYPE_BROADCAST;
1848 scan.intval = htole16(40);
1849
1850 p = scan.channels;
1851 count = 0;
1852 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
1853 if (IEEE80211_IS_CHAN_5GHZ(&ic->ic_channels[i]) &&
1854 isset(ic->ic_chan_active, i)) {
1855 *++p = i;
1856 count++;
1857 }
1858 }
1859 *(p - count) = IWI_CHAN_5GHZ | count;
1860
1861 count = 0;
1862 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
1863 if (IEEE80211_IS_CHAN_2GHZ(&ic->ic_channels[i]) &&
1864 isset(ic->ic_chan_active, i)) {
1865 *++p = i;
1866 count++;
1867 }
1868 }
1869 *(p - count) = IWI_CHAN_2GHZ | count;
1870
1871 DPRINTF(("Start scanning\n"));
1872 return iwi_cmd(sc, IWI_CMD_SCAN, &scan, sizeof scan, 1);
1873 }
1874
1875 static int
1876 iwi_auth_and_assoc(struct iwi_softc *sc)
1877 {
1878 struct ieee80211com *ic = &sc->sc_ic;
1879 struct ieee80211_node *ni = ic->ic_bss;
1880 struct iwi_configuration config;
1881 struct iwi_associate assoc;
1882 struct iwi_rateset rs;
1883 u_int32_t data;
1884 int error;
1885
1886 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
1887 /* enable b/g autodection */
1888 memset(&config, 0, sizeof config);
1889 config.bluetooth_coexistence = 1;
1890 config.multicast_enabled = 1;
1891 config.bg_autodetection = 1;
1892 config.noise_reported = 1;
1893 DPRINTF(("Configuring adapter\n"));
1894 error = iwi_cmd(sc, IWI_CMD_SET_CONFIGURATION, &config,
1895 sizeof config, 1);
1896 if (error != 0)
1897 return error;
1898 }
1899
1900 #ifdef IWI_DEBUG
1901 if (iwi_debug > 0) {
1902 printf("Setting ESSID to ");
1903 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
1904 printf("\n");
1905 }
1906 #endif
1907 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen, 1);
1908 if (error != 0)
1909 return error;
1910
1911 /* the rate set has already been "negociated" */
1912 rs.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A :
1913 IWI_MODE_11G;
1914 rs.type = IWI_RATESET_TYPE_NEGOCIATED;
1915 rs.nrates = ni->ni_rates.rs_nrates;
1916 memcpy(rs.rates, ni->ni_rates.rs_rates, rs.nrates);
1917 DPRINTF(("Setting negociated rates (%u)\n", rs.nrates));
1918 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 1);
1919 if (error != 0)
1920 return error;
1921
1922 data = htole32(ni->ni_rssi);
1923 DPRINTF(("Setting sensitivity to %d\n", (int8_t)ni->ni_rssi));
1924 error = iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &data, sizeof data, 1);
1925 if (error != 0)
1926 return error;
1927
1928 memset(&assoc, 0, sizeof assoc);
1929 assoc.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A :
1930 IWI_MODE_11G;
1931 assoc.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
1932 if (sc->authmode == IEEE80211_AUTH_SHARED)
1933 assoc.auth = (ic->ic_def_txkey << 4) | IWI_AUTH_SHARED;
1934 memcpy(assoc.tstamp, ni->ni_tstamp.data, 8);
1935 assoc.capinfo = htole16(ni->ni_capinfo);
1936 assoc.lintval = htole16(ic->ic_lintval);
1937 assoc.intval = htole16(ni->ni_intval);
1938 IEEE80211_ADDR_COPY(assoc.bssid, ni->ni_bssid);
1939 IEEE80211_ADDR_COPY(assoc.dst, ni->ni_bssid);
1940 DPRINTF(("Trying to associate to %s channel %u auth %u\n",
1941 ether_sprintf(assoc.bssid), assoc.chan, assoc.auth));
1942 return iwi_cmd(sc, IWI_CMD_ASSOCIATE, &assoc, sizeof assoc, 1);
1943 }
1944
1945 static int
1946 iwi_init(struct ifnet *ifp)
1947 {
1948 struct iwi_softc *sc = ifp->if_softc;
1949 struct ieee80211com *ic = &sc->sc_ic;
1950 struct iwi_firmware *fw = &sc->fw;
1951 int i, error;
1952
1953 /* exit immediately if firmware has not been ioctl'd */
1954 if (!(sc->flags & IWI_FLAG_FW_CACHED)) {
1955 ifp->if_flags &= ~IFF_UP;
1956 return EIO;
1957 }
1958
1959 if ((error = iwi_reset(sc)) != 0) {
1960 aprint_error("%s: could not reset adapter\n",
1961 sc->sc_dev.dv_xname);
1962 goto fail;
1963 }
1964
1965 if ((error = iwi_load_firmware(sc, fw->boot, fw->boot_size)) != 0) {
1966 aprint_error("%s: could not load boot firmware\n",
1967 sc->sc_dev.dv_xname);
1968 goto fail;
1969 }
1970
1971 if ((error = iwi_load_ucode(sc, fw->ucode, fw->ucode_size)) != 0) {
1972 aprint_error("%s: could not load microcode\n",
1973 sc->sc_dev.dv_xname);
1974 goto fail;
1975 }
1976
1977 iwi_stop_master(sc);
1978
1979 sc->tx_cur = 0;
1980 sc->tx_queued = 0;
1981 sc->tx_old = IWI_TX_RING_SIZE - 1;
1982 sc->cmd_cur = 0;
1983 sc->rx_cur = IWI_RX_RING_SIZE - 1;
1984
1985 CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmd_ring_map->dm_segs[0].ds_addr);
1986 CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, IWI_CMD_RING_SIZE);
1987 CSR_WRITE_4(sc, IWI_CSR_CMD_READ_INDEX, 0);
1988 CSR_WRITE_4(sc, IWI_CSR_CMD_WRITE_INDEX, sc->cmd_cur);
1989
1990 CSR_WRITE_4(sc, IWI_CSR_TX1_BASE, sc->tx_ring_map->dm_segs[0].ds_addr);
1991 CSR_WRITE_4(sc, IWI_CSR_TX1_SIZE, IWI_TX_RING_SIZE);
1992 CSR_WRITE_4(sc, IWI_CSR_TX1_READ_INDEX, 0);
1993 CSR_WRITE_4(sc, IWI_CSR_TX1_WRITE_INDEX, sc->tx_cur);
1994
1995 CSR_WRITE_4(sc, IWI_CSR_TX2_BASE, sc->tx_ring_map->dm_segs[0].ds_addr);
1996 CSR_WRITE_4(sc, IWI_CSR_TX2_SIZE, IWI_TX_RING_SIZE);
1997 CSR_WRITE_4(sc, IWI_CSR_TX2_READ_INDEX, 0);
1998 CSR_WRITE_4(sc, IWI_CSR_TX2_WRITE_INDEX, 0);
1999
2000 CSR_WRITE_4(sc, IWI_CSR_TX3_BASE, sc->tx_ring_map->dm_segs[0].ds_addr);
2001 CSR_WRITE_4(sc, IWI_CSR_TX3_SIZE, IWI_TX_RING_SIZE);
2002 CSR_WRITE_4(sc, IWI_CSR_TX3_READ_INDEX, 0);
2003 CSR_WRITE_4(sc, IWI_CSR_TX3_WRITE_INDEX, 0);
2004
2005 CSR_WRITE_4(sc, IWI_CSR_TX4_BASE, sc->tx_ring_map->dm_segs[0].ds_addr);
2006 CSR_WRITE_4(sc, IWI_CSR_TX4_SIZE, IWI_TX_RING_SIZE);
2007 CSR_WRITE_4(sc, IWI_CSR_TX4_READ_INDEX, 0);
2008 CSR_WRITE_4(sc, IWI_CSR_TX4_WRITE_INDEX, 0);
2009
2010 for (i = 0; i < IWI_RX_RING_SIZE; i++)
2011 CSR_WRITE_4(sc, IWI_CSR_RX_BASE + i * 4,
2012 sc->rx_buf[i].map->dm_segs[0].ds_addr);
2013
2014 /*
2015 * Kick Rx
2016 */
2017 CSR_WRITE_4(sc, IWI_CSR_RX_WRITE_INDEX, sc->rx_cur);
2018 CSR_WRITE_4(sc, IWI_CSR_RX_READ_INDEX, 0);
2019
2020 if ((error = iwi_load_firmware(sc, fw->main, fw->main_size)) != 0) {
2021 aprint_error("%s: could not load main firmware\n",
2022 sc->sc_dev.dv_xname);
2023 goto fail;
2024 }
2025
2026 sc->flags |= IWI_FLAG_FW_INITED;
2027
2028 if ((error = iwi_config(sc)) != 0) {
2029 aprint_error("%s: device configuration failed\n",
2030 sc->sc_dev.dv_xname);
2031 goto fail;
2032 }
2033
2034 if (ic->ic_opmode != IEEE80211_M_MONITOR)
2035 ieee80211_begin_scan(ic, 1);
2036 else
2037 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2038
2039 ifp->if_flags &= ~IFF_OACTIVE;
2040 ifp->if_flags |= IFF_RUNNING;
2041
2042 return 0;
2043
2044 fail: iwi_stop(ifp, 0);
2045
2046 return error;
2047 }
2048
2049 static void
2050 iwi_stop(struct ifnet *ifp, int disable)
2051 {
2052 struct iwi_softc *sc = ifp->if_softc;
2053 struct ieee80211com *ic = &sc->sc_ic;
2054 struct iwi_tx_buf *buf;
2055 int i;
2056
2057 iwi_stop_master(sc);
2058 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SW_RESET);
2059
2060 /*
2061 * Release Tx buffers
2062 */
2063 for (i = 0; i < IWI_TX_RING_SIZE; i++) {
2064 buf = &sc->tx_buf[i];
2065
2066 if (buf->m != NULL) {
2067 bus_dmamap_unload(sc->sc_dmat, buf->map);
2068 m_freem(buf->m);
2069 buf->m = NULL;
2070
2071 if (buf->ni != NULL) {
2072 ieee80211_free_node(buf->ni);
2073 buf->ni = NULL;
2074 }
2075 }
2076 }
2077
2078 ifp->if_timer = 0;
2079 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2080
2081 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2082 }
2083