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