1 /* $OpenBSD: if_iwi.c,v 1.149 2024/05/24 06:02:53 jsg Exp $ */
2
3 /*-
4 * Copyright (c) 2004-2008
5 * Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 /*
21 * Driver for Intel PRO/Wireless 2200BG/2915ABG 802.11 network adapters.
22 */
23
24 #include "bpfilter.h"
25
26 #include <sys/param.h>
27 #include <sys/sockio.h>
28 #include <sys/mbuf.h>
29 #include <sys/rwlock.h>
30 #include <sys/socket.h>
31 #include <sys/systm.h>
32 #include <sys/device.h>
33 #include <sys/task.h>
34 #include <sys/endian.h>
35
36 #include <machine/bus.h>
37 #include <machine/intr.h>
38
39 #include <dev/pci/pcireg.h>
40 #include <dev/pci/pcivar.h>
41 #include <dev/pci/pcidevs.h>
42
43 #if NBPFILTER > 0
44 #include <net/bpf.h>
45 #endif
46 #include <net/if.h>
47 #include <net/if_dl.h>
48 #include <net/if_media.h>
49
50 #include <netinet/in.h>
51 #include <netinet/if_ether.h>
52
53 #include <net80211/ieee80211_var.h>
54 #include <net80211/ieee80211_radiotap.h>
55
56 #include <dev/pci/if_iwireg.h>
57 #include <dev/pci/if_iwivar.h>
58
59 const struct pci_matchid iwi_devices[] = {
60 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_PRO_WL_2200BG },
61 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_PRO_WL_2225BG },
62 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_PRO_WL_2915ABG_1 },
63 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_PRO_WL_2915ABG_2 }
64 };
65
66 int iwi_match(struct device *, void *, void *);
67 void iwi_attach(struct device *, struct device *, void *);
68 int iwi_activate(struct device *, int);
69 void iwi_wakeup(struct iwi_softc *);
70 void iwi_init_task(void *);
71 int iwi_alloc_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
72 void iwi_reset_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
73 void iwi_free_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
74 int iwi_alloc_tx_ring(struct iwi_softc *, struct iwi_tx_ring *,
75 int);
76 void iwi_reset_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
77 void iwi_free_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
78 int iwi_alloc_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
79 void iwi_reset_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
80 void iwi_free_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
81 int iwi_media_change(struct ifnet *);
82 void iwi_media_status(struct ifnet *, struct ifmediareq *);
83 uint16_t iwi_read_prom_word(struct iwi_softc *, uint8_t);
84 int iwi_find_txnode(struct iwi_softc *, const uint8_t *);
85 int iwi_newstate(struct ieee80211com *, enum ieee80211_state, int);
86 uint8_t iwi_rate(int);
87 void iwi_frame_intr(struct iwi_softc *, struct iwi_rx_data *,
88 struct iwi_frame *, struct mbuf_list *);
89 void iwi_notification_intr(struct iwi_softc *, struct iwi_rx_data *,
90 struct iwi_notif *);
91 void iwi_rx_intr(struct iwi_softc *);
92 void iwi_tx_intr(struct iwi_softc *, struct iwi_tx_ring *);
93 int iwi_intr(void *);
94 int iwi_cmd(struct iwi_softc *, uint8_t, void *, uint8_t, int);
95 int iwi_send_mgmt(struct ieee80211com *, struct ieee80211_node *,
96 int, int, int);
97 int iwi_tx_start(struct ifnet *, struct mbuf *,
98 struct ieee80211_node *);
99 void iwi_start(struct ifnet *);
100 void iwi_watchdog(struct ifnet *);
101 int iwi_ioctl(struct ifnet *, u_long, caddr_t);
102 void iwi_stop_master(struct iwi_softc *);
103 int iwi_reset(struct iwi_softc *);
104 int iwi_load_ucode(struct iwi_softc *, const char *, int);
105 int iwi_load_firmware(struct iwi_softc *, const char *, int);
106 int iwi_config(struct iwi_softc *);
107 void iwi_update_edca(struct ieee80211com *);
108 int iwi_set_chan(struct iwi_softc *, struct ieee80211_channel *);
109 int iwi_scan(struct iwi_softc *);
110 int iwi_auth_and_assoc(struct iwi_softc *);
111 int iwi_init(struct ifnet *);
112 void iwi_stop(struct ifnet *, int);
113
114 static __inline uint8_t
MEM_READ_1(struct iwi_softc * sc,uint32_t addr)115 MEM_READ_1(struct iwi_softc *sc, uint32_t addr)
116 {
117 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
118 return CSR_READ_1(sc, IWI_CSR_INDIRECT_DATA);
119 }
120
121 static __inline uint32_t
MEM_READ_4(struct iwi_softc * sc,uint32_t addr)122 MEM_READ_4(struct iwi_softc *sc, uint32_t addr)
123 {
124 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
125 return CSR_READ_4(sc, IWI_CSR_INDIRECT_DATA);
126 }
127
128 #ifdef IWI_DEBUG
129 #define DPRINTF(x) do { if (iwi_debug > 0) printf x; } while (0)
130 #define DPRINTFN(n, x) do { if (iwi_debug >= (n)) printf x; } while (0)
131 int iwi_debug = 0;
132 #else
133 #define DPRINTF(x)
134 #define DPRINTFN(n, x)
135 #endif
136
137 const struct cfattach iwi_ca = {
138 sizeof (struct iwi_softc), iwi_match, iwi_attach, NULL,
139 iwi_activate
140 };
141
142 int
iwi_match(struct device * parent,void * match,void * aux)143 iwi_match(struct device *parent, void *match, void *aux)
144 {
145 return pci_matchbyid((struct pci_attach_args *)aux, iwi_devices,
146 nitems(iwi_devices));
147 }
148
149 /* Base Address Register */
150 #define IWI_PCI_BAR0 0x10
151
152 void
iwi_attach(struct device * parent,struct device * self,void * aux)153 iwi_attach(struct device *parent, struct device *self, void *aux)
154 {
155 struct iwi_softc *sc = (struct iwi_softc *)self;
156 struct ieee80211com *ic = &sc->sc_ic;
157 struct ifnet *ifp = &ic->ic_if;
158 struct pci_attach_args *pa = aux;
159 const char *intrstr;
160 bus_space_tag_t memt;
161 bus_space_handle_t memh;
162 pci_intr_handle_t ih;
163 pcireg_t data;
164 uint16_t val;
165 int error, ac, i;
166
167 sc->sc_pct = pa->pa_pc;
168 sc->sc_pcitag = pa->pa_tag;
169
170 /* clear device specific PCI configuration register 0x41 */
171 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
172 data &= ~0x0000ff00;
173 pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data);
174
175 /* map the register window */
176 error = pci_mapreg_map(pa, IWI_PCI_BAR0, PCI_MAPREG_TYPE_MEM |
177 PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh, NULL, &sc->sc_sz, 0);
178 if (error != 0) {
179 printf(": can't map mem space\n");
180 return;
181 }
182
183 sc->sc_st = memt;
184 sc->sc_sh = memh;
185 sc->sc_dmat = pa->pa_dmat;
186
187 if (pci_intr_map(pa, &ih) != 0) {
188 printf(": can't map interrupt\n");
189 return;
190 }
191
192 intrstr = pci_intr_string(sc->sc_pct, ih);
193 sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, iwi_intr, sc,
194 sc->sc_dev.dv_xname);
195 if (sc->sc_ih == NULL) {
196 printf(": can't establish interrupt");
197 if (intrstr != NULL)
198 printf(" at %s", intrstr);
199 printf("\n");
200 return;
201 }
202 printf(": %s", intrstr);
203
204 if (iwi_reset(sc) != 0) {
205 printf(": could not reset adapter\n");
206 return;
207 }
208
209 /*
210 * Allocate rings.
211 */
212 if (iwi_alloc_cmd_ring(sc, &sc->cmdq) != 0) {
213 printf(": could not allocate Cmd ring\n");
214 return;
215 }
216 for (ac = 0; ac < EDCA_NUM_AC; ac++) {
217 if (iwi_alloc_tx_ring(sc, &sc->txq[ac], ac) != 0) {
218 printf(": could not allocate Tx ring %d\n", ac);
219 goto fail;
220 }
221 }
222 if (iwi_alloc_rx_ring(sc, &sc->rxq) != 0) {
223 printf(": could not allocate Rx ring\n");
224 goto fail;
225 }
226
227 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
228 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
229 ic->ic_state = IEEE80211_S_INIT;
230
231 /* set device capabilities */
232 ic->ic_caps =
233 #ifndef IEEE80211_STA_ONLY
234 IEEE80211_C_IBSS | /* IBSS mode supported */
235 #endif
236 IEEE80211_C_MONITOR | /* monitor mode supported */
237 IEEE80211_C_TXPMGT | /* tx power management */
238 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
239 IEEE80211_C_SHSLOT | /* short slot time supported */
240 IEEE80211_C_WEP | /* s/w WEP */
241 IEEE80211_C_RSN | /* WPA/RSN supported */
242 IEEE80211_C_SCANALL; /* h/w scanning */
243
244 /* read MAC address from EEPROM */
245 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 0);
246 ic->ic_myaddr[0] = val & 0xff;
247 ic->ic_myaddr[1] = val >> 8;
248 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 1);
249 ic->ic_myaddr[2] = val & 0xff;
250 ic->ic_myaddr[3] = val >> 8;
251 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 2);
252 ic->ic_myaddr[4] = val & 0xff;
253 ic->ic_myaddr[5] = val >> 8;
254
255 printf(", address %s\n", ether_sprintf(ic->ic_myaddr));
256
257 if (PCI_PRODUCT(pa->pa_id) >= PCI_PRODUCT_INTEL_PRO_WL_2915ABG_1) {
258 /* set supported .11a rates */
259 ic->ic_sup_rates[IEEE80211_MODE_11A] =
260 ieee80211_std_rateset_11a;
261
262 /* set supported .11a channels */
263 for (i = 36; i <= 64; i += 4) {
264 ic->ic_channels[i].ic_freq =
265 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
266 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
267 }
268 for (i = 149; i <= 165; i += 4) {
269 ic->ic_channels[i].ic_freq =
270 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
271 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
272 }
273 }
274
275 /* set supported .11b and .11g rates */
276 ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
277 ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_rateset_11g;
278
279 /* set supported .11b and .11g channels (1 through 14) */
280 for (i = 1; i <= 14; i++) {
281 ic->ic_channels[i].ic_freq =
282 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
283 ic->ic_channels[i].ic_flags =
284 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
285 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
286 }
287
288 /* IBSS channel undefined for now */
289 ic->ic_ibss_chan = &ic->ic_channels[0];
290
291 ifp->if_softc = sc;
292 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
293 ifp->if_ioctl = iwi_ioctl;
294 ifp->if_start = iwi_start;
295 ifp->if_watchdog = iwi_watchdog;
296 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
297
298 if_attach(ifp);
299 ieee80211_ifattach(ifp);
300 /* override state transition machine */
301 sc->sc_newstate = ic->ic_newstate;
302 ic->ic_newstate = iwi_newstate;
303 ic->ic_send_mgmt = iwi_send_mgmt;
304 ieee80211_media_init(ifp, iwi_media_change, iwi_media_status);
305
306 #if NBPFILTER > 0
307 bpfattach(&sc->sc_drvbpf, ifp, DLT_IEEE802_11_RADIO,
308 sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN);
309
310 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
311 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
312 sc->sc_rxtap.wr_ihdr.it_present = htole32(IWI_RX_RADIOTAP_PRESENT);
313
314 sc->sc_txtap_len = sizeof sc->sc_txtapu;
315 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
316 sc->sc_txtap.wt_ihdr.it_present = htole32(IWI_TX_RADIOTAP_PRESENT);
317 #endif
318
319 rw_init(&sc->sc_rwlock, "iwilock");
320 task_set(&sc->init_task, iwi_init_task, sc);
321 return;
322
323 fail: while (--ac >= 0)
324 iwi_free_tx_ring(sc, &sc->txq[ac]);
325 iwi_free_cmd_ring(sc, &sc->cmdq);
326 }
327
328 int
iwi_activate(struct device * self,int act)329 iwi_activate(struct device *self, int act)
330 {
331 struct iwi_softc *sc = (struct iwi_softc *)self;
332 struct ifnet *ifp = &sc->sc_ic.ic_if;
333
334 switch (act) {
335 case DVACT_SUSPEND:
336 if (ifp->if_flags & IFF_RUNNING)
337 iwi_stop(ifp, 0);
338 break;
339 case DVACT_WAKEUP:
340 iwi_wakeup(sc);
341 break;
342 }
343
344 return 0;
345 }
346
347 void
iwi_wakeup(struct iwi_softc * sc)348 iwi_wakeup(struct iwi_softc *sc)
349 {
350 pcireg_t data;
351
352 /* clear device specific PCI configuration register 0x41 */
353 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
354 data &= ~0x0000ff00;
355 pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data);
356
357 iwi_init_task(sc);
358 }
359
360 void
iwi_init_task(void * arg1)361 iwi_init_task(void *arg1)
362 {
363 struct iwi_softc *sc = arg1;
364 struct ifnet *ifp = &sc->sc_ic.ic_if;
365 int s;
366
367 rw_enter_write(&sc->sc_rwlock);
368 s = splnet();
369
370 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == IFF_UP)
371 iwi_init(ifp);
372
373 splx(s);
374 rw_exit_write(&sc->sc_rwlock);
375 }
376
377 int
iwi_alloc_cmd_ring(struct iwi_softc * sc,struct iwi_cmd_ring * ring)378 iwi_alloc_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
379 {
380 int nsegs, error;
381
382 ring->queued = 0;
383 ring->cur = ring->next = 0;
384
385 error = bus_dmamap_create(sc->sc_dmat,
386 sizeof (struct iwi_cmd_desc) * IWI_CMD_RING_COUNT, 1,
387 sizeof (struct iwi_cmd_desc) * IWI_CMD_RING_COUNT, 0,
388 BUS_DMA_NOWAIT, &ring->map);
389 if (error != 0) {
390 printf("%s: could not create cmd ring DMA map\n",
391 sc->sc_dev.dv_xname);
392 goto fail;
393 }
394
395 error = bus_dmamem_alloc(sc->sc_dmat,
396 sizeof (struct iwi_cmd_desc) * IWI_CMD_RING_COUNT, PAGE_SIZE, 0,
397 &ring->seg, 1, &nsegs, BUS_DMA_NOWAIT | BUS_DMA_ZERO);
398 if (error != 0) {
399 printf("%s: could not allocate cmd ring DMA memory\n",
400 sc->sc_dev.dv_xname);
401 goto fail;
402 }
403
404 error = bus_dmamem_map(sc->sc_dmat, &ring->seg, nsegs,
405 sizeof (struct iwi_cmd_desc) * IWI_CMD_RING_COUNT,
406 (caddr_t *)&ring->desc, BUS_DMA_NOWAIT);
407 if (error != 0) {
408 printf("%s: can't map cmd ring DMA memory\n",
409 sc->sc_dev.dv_xname);
410 goto fail;
411 }
412
413 error = bus_dmamap_load(sc->sc_dmat, ring->map, ring->desc,
414 sizeof (struct iwi_cmd_desc) * IWI_CMD_RING_COUNT, NULL,
415 BUS_DMA_NOWAIT);
416 if (error != 0) {
417 printf("%s: could not load cmd ring DMA map\n",
418 sc->sc_dev.dv_xname);
419 goto fail;
420 }
421
422 return 0;
423
424 fail: iwi_free_cmd_ring(sc, ring);
425 return error;
426 }
427
428 void
iwi_reset_cmd_ring(struct iwi_softc * sc,struct iwi_cmd_ring * ring)429 iwi_reset_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
430 {
431 ring->queued = 0;
432 ring->cur = ring->next = 0;
433 }
434
435 void
iwi_free_cmd_ring(struct iwi_softc * sc,struct iwi_cmd_ring * ring)436 iwi_free_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
437 {
438 if (ring->map != NULL) {
439 if (ring->desc != NULL) {
440 bus_dmamap_unload(sc->sc_dmat, ring->map);
441 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)ring->desc,
442 sizeof (struct iwi_cmd_desc) * IWI_CMD_RING_COUNT);
443 bus_dmamem_free(sc->sc_dmat, &ring->seg, 1);
444 }
445 bus_dmamap_destroy(sc->sc_dmat, ring->map);
446 }
447 }
448
449 int
iwi_alloc_tx_ring(struct iwi_softc * sc,struct iwi_tx_ring * ring,int ac)450 iwi_alloc_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring, int ac)
451 {
452 struct iwi_tx_data *data;
453 int i, nsegs, error;
454
455 ring->queued = 0;
456 ring->cur = ring->next = 0;
457 ring->csr_ridx = IWI_CSR_TX_RIDX(ac);
458 ring->csr_widx = IWI_CSR_TX_WIDX(ac);
459
460 error = bus_dmamap_create(sc->sc_dmat,
461 sizeof (struct iwi_tx_desc) * IWI_TX_RING_COUNT, 1,
462 sizeof (struct iwi_tx_desc) * IWI_TX_RING_COUNT, 0, BUS_DMA_NOWAIT,
463 &ring->map);
464 if (error != 0) {
465 printf("%s: could not create tx ring DMA map\n",
466 sc->sc_dev.dv_xname);
467 goto fail;
468 }
469
470 error = bus_dmamem_alloc(sc->sc_dmat,
471 sizeof (struct iwi_tx_desc) * IWI_TX_RING_COUNT, PAGE_SIZE, 0,
472 &ring->seg, 1, &nsegs, BUS_DMA_NOWAIT | BUS_DMA_ZERO);
473 if (error != 0) {
474 printf("%s: could not allocate tx ring DMA memory\n",
475 sc->sc_dev.dv_xname);
476 goto fail;
477 }
478
479 error = bus_dmamem_map(sc->sc_dmat, &ring->seg, nsegs,
480 sizeof (struct iwi_tx_desc) * IWI_TX_RING_COUNT,
481 (caddr_t *)&ring->desc, BUS_DMA_NOWAIT);
482 if (error != 0) {
483 printf("%s: can't map tx ring DMA memory\n",
484 sc->sc_dev.dv_xname);
485 goto fail;
486 }
487
488 error = bus_dmamap_load(sc->sc_dmat, ring->map, ring->desc,
489 sizeof (struct iwi_tx_desc) * IWI_TX_RING_COUNT, NULL,
490 BUS_DMA_NOWAIT);
491 if (error != 0) {
492 printf("%s: could not load tx ring DMA map\n",
493 sc->sc_dev.dv_xname);
494 goto fail;
495 }
496
497 for (i = 0; i < IWI_TX_RING_COUNT; i++) {
498 data = &ring->data[i];
499
500 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
501 IWI_MAX_SCATTER, MCLBYTES, 0, BUS_DMA_NOWAIT, &data->map);
502 if (error != 0) {
503 printf("%s: could not create tx buf DMA map\n",
504 sc->sc_dev.dv_xname);
505 goto fail;
506 }
507 }
508
509 return 0;
510
511 fail: iwi_free_tx_ring(sc, ring);
512 return error;
513 }
514
515 void
iwi_reset_tx_ring(struct iwi_softc * sc,struct iwi_tx_ring * ring)516 iwi_reset_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
517 {
518 struct iwi_tx_data *data;
519 int i;
520
521 for (i = 0; i < IWI_TX_RING_COUNT; i++) {
522 data = &ring->data[i];
523
524 if (data->m != NULL) {
525 bus_dmamap_unload(sc->sc_dmat, data->map);
526 m_freem(data->m);
527 data->m = NULL;
528 }
529 }
530
531 ring->queued = 0;
532 ring->cur = ring->next = 0;
533 }
534
535 void
iwi_free_tx_ring(struct iwi_softc * sc,struct iwi_tx_ring * ring)536 iwi_free_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
537 {
538 struct iwi_tx_data *data;
539 int i;
540
541 if (ring->map != NULL) {
542 if (ring->desc != NULL) {
543 bus_dmamap_unload(sc->sc_dmat, ring->map);
544 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)ring->desc,
545 sizeof (struct iwi_tx_desc) * IWI_TX_RING_COUNT);
546 bus_dmamem_free(sc->sc_dmat, &ring->seg, 1);
547 }
548 bus_dmamap_destroy(sc->sc_dmat, ring->map);
549 }
550
551 for (i = 0; i < IWI_TX_RING_COUNT; i++) {
552 data = &ring->data[i];
553
554 if (data->m != NULL) {
555 bus_dmamap_unload(sc->sc_dmat, data->map);
556 m_freem(data->m);
557 }
558 bus_dmamap_destroy(sc->sc_dmat, data->map);
559 }
560 }
561
562 int
iwi_alloc_rx_ring(struct iwi_softc * sc,struct iwi_rx_ring * ring)563 iwi_alloc_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
564 {
565 struct iwi_rx_data *data;
566 int i, error;
567
568 ring->cur = 0;
569
570 for (i = 0; i < IWI_RX_RING_COUNT; i++) {
571 data = &sc->rxq.data[i];
572
573 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
574 0, BUS_DMA_NOWAIT, &data->map);
575 if (error != 0) {
576 printf("%s: could not create rx buf DMA map\n",
577 sc->sc_dev.dv_xname);
578 goto fail;
579 }
580
581 MGETHDR(data->m, M_DONTWAIT, MT_DATA);
582 if (data->m == NULL) {
583 printf("%s: could not allocate rx mbuf\n",
584 sc->sc_dev.dv_xname);
585 error = ENOMEM;
586 goto fail;
587 }
588 MCLGET(data->m, M_DONTWAIT);
589 if (!(data->m->m_flags & M_EXT)) {
590 m_freem(data->m);
591 data->m = NULL;
592 printf("%s: could not allocate rx mbuf cluster\n",
593 sc->sc_dev.dv_xname);
594 error = ENOMEM;
595 goto fail;
596 }
597
598 error = bus_dmamap_load(sc->sc_dmat, data->map,
599 mtod(data->m, void *), MCLBYTES, NULL, BUS_DMA_NOWAIT);
600 if (error != 0) {
601 printf("%s: could not load rx buf DMA map\n",
602 sc->sc_dev.dv_xname);
603 goto fail;
604 }
605
606 data->reg = IWI_CSR_RX_BASE + i * 4;
607 }
608
609 return 0;
610
611 fail: iwi_free_rx_ring(sc, ring);
612 return error;
613 }
614
615 void
iwi_reset_rx_ring(struct iwi_softc * sc,struct iwi_rx_ring * ring)616 iwi_reset_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
617 {
618 ring->cur = 0;
619 }
620
621 void
iwi_free_rx_ring(struct iwi_softc * sc,struct iwi_rx_ring * ring)622 iwi_free_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
623 {
624 struct iwi_rx_data *data;
625 int i;
626
627 for (i = 0; i < IWI_RX_RING_COUNT; i++) {
628 data = &sc->rxq.data[i];
629
630 if (data->m != NULL) {
631 bus_dmamap_unload(sc->sc_dmat, data->map);
632 m_freem(data->m);
633 }
634 bus_dmamap_destroy(sc->sc_dmat, data->map);
635 }
636 }
637
638 int
iwi_media_change(struct ifnet * ifp)639 iwi_media_change(struct ifnet *ifp)
640 {
641 int error;
642
643 error = ieee80211_media_change(ifp);
644 if (error != ENETRESET)
645 return error;
646
647 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
648 error = iwi_init(ifp);
649
650 return error;
651 }
652
653 void
iwi_media_status(struct ifnet * ifp,struct ifmediareq * imr)654 iwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
655 {
656 struct iwi_softc *sc = ifp->if_softc;
657 struct ieee80211com *ic = &sc->sc_ic;
658 uint32_t val;
659 int rate;
660
661 imr->ifm_status = IFM_AVALID;
662 imr->ifm_active = IFM_IEEE80211;
663 if (ic->ic_state == IEEE80211_S_RUN)
664 imr->ifm_status |= IFM_ACTIVE;
665
666 /* read current transmission rate from adapter */
667 val = CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE);
668 /* convert PLCP signal to 802.11 rate */
669 rate = iwi_rate(val);
670
671 imr->ifm_active |= ieee80211_rate2media(ic, rate, ic->ic_curmode);
672 switch (ic->ic_opmode) {
673 case IEEE80211_M_STA:
674 break;
675 #ifndef IEEE80211_STA_ONLY
676 case IEEE80211_M_IBSS:
677 imr->ifm_active |= IFM_IEEE80211_ADHOC;
678 break;
679 #endif
680 case IEEE80211_M_MONITOR:
681 imr->ifm_active |= IFM_IEEE80211_MONITOR;
682 break;
683 default:
684 /* should not get there */
685 break;
686 }
687 }
688
689 #ifndef IEEE80211_STA_ONLY
690 /*
691 * This is only used for IBSS mode where the firmware expect an index to an
692 * internal node table instead of a destination address.
693 */
694 int
iwi_find_txnode(struct iwi_softc * sc,const uint8_t * macaddr)695 iwi_find_txnode(struct iwi_softc *sc, const uint8_t *macaddr)
696 {
697 struct iwi_node node;
698 int i;
699
700 for (i = 0; i < sc->nsta; i++)
701 if (IEEE80211_ADDR_EQ(sc->sta[i], macaddr))
702 return i; /* already existing node */
703
704 if (i == IWI_MAX_NODE)
705 return -1; /* no place left in neighbor table */
706
707 /* save this new node in our softc table */
708 IEEE80211_ADDR_COPY(sc->sta[i], macaddr);
709 sc->nsta = i;
710
711 /* write node information into NIC memory */
712 bzero(&node, sizeof node);
713 IEEE80211_ADDR_COPY(node.bssid, macaddr);
714
715 CSR_WRITE_REGION_1(sc, IWI_CSR_NODE_BASE + i * sizeof node,
716 (uint8_t *)&node, sizeof node);
717
718 return i;
719 }
720 #endif
721
722 int
iwi_newstate(struct ieee80211com * ic,enum ieee80211_state nstate,int arg)723 iwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
724 {
725 struct iwi_softc *sc = ic->ic_softc;
726 struct ifnet *ifp = &ic->ic_if;
727 enum ieee80211_state ostate;
728 uint32_t tmp;
729
730 if (LINK_STATE_IS_UP(ifp->if_link_state))
731 ieee80211_set_link_state(ic, LINK_STATE_DOWN);
732
733 ostate = ic->ic_state;
734
735 switch (nstate) {
736 case IEEE80211_S_SCAN:
737 iwi_scan(sc);
738 break;
739
740 case IEEE80211_S_AUTH:
741 if (iwi_auth_and_assoc(sc)) {
742 ieee80211_begin_scan(&ic->ic_if);
743 return 0;
744 }
745 break;
746
747 case IEEE80211_S_RUN:
748 #ifndef IEEE80211_STA_ONLY
749 if (ic->ic_opmode == IEEE80211_M_IBSS) {
750 sc->nsta = 0; /* flush IBSS nodes */
751 ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
752 } else
753 #endif
754 if (ic->ic_opmode == IEEE80211_M_MONITOR)
755 iwi_set_chan(sc, ic->ic_ibss_chan);
756
757 /* assoc led on */
758 tmp = MEM_READ_4(sc, IWI_MEM_EVENT_CTL) & IWI_LED_MASK;
759 MEM_WRITE_4(sc, IWI_MEM_EVENT_CTL, tmp | IWI_LED_ASSOC);
760
761 if (!(ic->ic_flags & IEEE80211_F_RSNON)) {
762 /*
763 * NB: When RSN is enabled, we defer setting
764 * the link up until the port is valid.
765 */
766 ieee80211_set_link_state(ic, LINK_STATE_UP);
767 }
768 break;
769
770 case IEEE80211_S_INIT:
771 if (ostate != IEEE80211_S_RUN)
772 break;
773
774 /* assoc led off */
775 tmp = MEM_READ_4(sc, IWI_MEM_EVENT_CTL) & IWI_LED_MASK;
776 MEM_WRITE_4(sc, IWI_MEM_EVENT_CTL, tmp & ~IWI_LED_ASSOC);
777 break;
778
779 case IEEE80211_S_ASSOC:
780 break;
781 }
782
783 ic->ic_state = nstate;
784 return 0;
785 }
786
787 /*
788 * Read 16 bits at address 'addr' from the serial EEPROM.
789 * DON'T PLAY WITH THIS CODE UNLESS YOU KNOW *EXACTLY* WHAT YOU'RE DOING!
790 */
791 uint16_t
iwi_read_prom_word(struct iwi_softc * sc,uint8_t addr)792 iwi_read_prom_word(struct iwi_softc *sc, uint8_t addr)
793 {
794 uint32_t tmp;
795 uint16_t val;
796 int n;
797
798 /* clock C once before the first command */
799 IWI_EEPROM_CTL(sc, 0);
800 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
801 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
802 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
803
804 /* write start bit (1) */
805 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
806 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
807
808 /* write READ opcode (10) */
809 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
810 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
811 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
812 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
813
814 /* write address A7-A0 */
815 for (n = 7; n >= 0; n--) {
816 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
817 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D));
818 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
819 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D) | IWI_EEPROM_C);
820 }
821
822 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
823
824 /* read data Q15-Q0 */
825 val = 0;
826 for (n = 15; n >= 0; n--) {
827 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
828 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
829 tmp = MEM_READ_4(sc, IWI_MEM_EEPROM_CTL);
830 val |= ((tmp & IWI_EEPROM_Q) >> IWI_EEPROM_SHIFT_Q) << n;
831 }
832
833 IWI_EEPROM_CTL(sc, 0);
834
835 /* clear Chip Select and clock C */
836 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
837 IWI_EEPROM_CTL(sc, 0);
838 IWI_EEPROM_CTL(sc, IWI_EEPROM_C);
839
840 return val;
841 }
842
843 uint8_t
iwi_rate(int plcp)844 iwi_rate(int plcp)
845 {
846 switch (plcp) {
847 /* CCK rates (values are device-dependent) */
848 case 10: return 2;
849 case 20: return 4;
850 case 55: return 11;
851 case 110: return 22;
852
853 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
854 case 0xd: return 12;
855 case 0xf: return 18;
856 case 0x5: return 24;
857 case 0x7: return 36;
858 case 0x9: return 48;
859 case 0xb: return 72;
860 case 0x1: return 96;
861 case 0x3: return 108;
862
863 /* unknown rate: should not happen */
864 default: return 0;
865 }
866 }
867
868 void
iwi_frame_intr(struct iwi_softc * sc,struct iwi_rx_data * data,struct iwi_frame * frame,struct mbuf_list * ml)869 iwi_frame_intr(struct iwi_softc *sc, struct iwi_rx_data *data,
870 struct iwi_frame *frame, struct mbuf_list *ml)
871 {
872 struct ieee80211com *ic = &sc->sc_ic;
873 struct ifnet *ifp = &ic->ic_if;
874 struct mbuf *mnew, *m;
875 struct ieee80211_frame *wh;
876 struct ieee80211_rxinfo rxi;
877 struct ieee80211_node *ni;
878 int error;
879
880 DPRINTFN(5, ("received frame len=%u chan=%u rssi=%u\n",
881 letoh16(frame->len), frame->chan, frame->rssi_dbm));
882
883 if (letoh16(frame->len) < sizeof (struct ieee80211_frame_min) ||
884 letoh16(frame->len) > MCLBYTES) {
885 DPRINTF(("%s: bad frame length\n", sc->sc_dev.dv_xname));
886 ifp->if_ierrors++;
887 return;
888 }
889
890 /*
891 * Try to allocate a new mbuf for this ring element and load it before
892 * processing the current mbuf. If the ring element cannot be loaded,
893 * drop the received packet and reuse the old mbuf. In the unlikely
894 * case that the old mbuf can't be reloaded either, explicitly panic.
895 */
896 MGETHDR(mnew, M_DONTWAIT, MT_DATA);
897 if (mnew == NULL) {
898 ifp->if_ierrors++;
899 return;
900 }
901 MCLGET(mnew, M_DONTWAIT);
902 if (!(mnew->m_flags & M_EXT)) {
903 m_freem(mnew);
904 ifp->if_ierrors++;
905 return;
906 }
907
908 bus_dmamap_unload(sc->sc_dmat, data->map);
909
910 error = bus_dmamap_load(sc->sc_dmat, data->map, mtod(mnew, void *),
911 MCLBYTES, NULL, BUS_DMA_NOWAIT);
912 if (error != 0) {
913 m_freem(mnew);
914
915 /* try to reload the old mbuf */
916 error = bus_dmamap_load(sc->sc_dmat, data->map,
917 mtod(data->m, void *), MCLBYTES, NULL, BUS_DMA_NOWAIT);
918 if (error != 0) {
919 /* very unlikely that it will fail... */
920 panic("%s: could not load old rx mbuf",
921 sc->sc_dev.dv_xname);
922 }
923 CSR_WRITE_4(sc, data->reg, data->map->dm_segs[0].ds_addr);
924 ifp->if_ierrors++;
925 return;
926 }
927
928 m = data->m;
929 data->m = mnew;
930 CSR_WRITE_4(sc, data->reg, data->map->dm_segs[0].ds_addr);
931
932 /* finalize mbuf */
933 m->m_pkthdr.len = m->m_len = sizeof (struct iwi_hdr) +
934 sizeof (struct iwi_frame) + letoh16(frame->len);
935 m_adj(m, sizeof (struct iwi_hdr) + sizeof (struct iwi_frame));
936
937 #if NBPFILTER > 0
938 if (sc->sc_drvbpf != NULL) {
939 struct iwi_rx_radiotap_header *tap = &sc->sc_rxtap;
940
941 tap->wr_flags = 0;
942 tap->wr_rate = iwi_rate(frame->rate);
943 tap->wr_chan_freq =
944 htole16(ic->ic_channels[frame->chan].ic_freq);
945 tap->wr_chan_flags =
946 htole16(ic->ic_channels[frame->chan].ic_flags);
947 tap->wr_antsignal = frame->signal;
948 tap->wr_antenna = frame->antenna & 0x3;
949 if (frame->antenna & 0x40)
950 tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
951
952 bpf_mtap_hdr(sc->sc_drvbpf, tap, sc->sc_rxtap_len,
953 m, BPF_DIRECTION_IN);
954 }
955 #endif
956
957 wh = mtod(m, struct ieee80211_frame *);
958 ni = ieee80211_find_rxnode(ic, wh);
959
960 /* send the frame to the upper layer */
961 memset(&rxi, 0, sizeof(rxi));
962 rxi.rxi_rssi = frame->rssi_dbm;
963 ieee80211_inputm(ifp, m, ni, &rxi, ml);
964
965 /* node is no longer needed */
966 ieee80211_release_node(ic, ni);
967 }
968
969 void
iwi_notification_intr(struct iwi_softc * sc,struct iwi_rx_data * data,struct iwi_notif * notif)970 iwi_notification_intr(struct iwi_softc *sc, struct iwi_rx_data *data,
971 struct iwi_notif *notif)
972 {
973 struct ieee80211com *ic = &sc->sc_ic;
974 struct ieee80211_node *ni = ic->ic_bss;
975 struct ifnet *ifp = &ic->ic_if;
976
977 switch (notif->type) {
978 case IWI_NOTIF_TYPE_SCAN_CHANNEL:
979 {
980 #ifdef IWI_DEBUG
981 struct iwi_notif_scan_channel *chan =
982 (struct iwi_notif_scan_channel *)(notif + 1);
983 #endif
984 DPRINTFN(2, ("Scanning channel (%u)\n", chan->nchan));
985 break;
986 }
987 case IWI_NOTIF_TYPE_SCAN_COMPLETE:
988 {
989 #ifdef IWI_DEBUG
990 struct iwi_notif_scan_complete *scan =
991 (struct iwi_notif_scan_complete *)(notif + 1);
992 #endif
993 DPRINTFN(2, ("Scan completed (%u, %u)\n", scan->nchan,
994 scan->status));
995
996 /* monitor mode uses scan to set the channel ... */
997 if (ic->ic_opmode != IEEE80211_M_MONITOR)
998 ieee80211_end_scan(ifp);
999 else
1000 iwi_set_chan(sc, ic->ic_ibss_chan);
1001 break;
1002 }
1003 case IWI_NOTIF_TYPE_AUTHENTICATION:
1004 {
1005 struct iwi_notif_authentication *auth =
1006 (struct iwi_notif_authentication *)(notif + 1);
1007
1008 DPRINTFN(2, ("Authentication (%u)\n", auth->state));
1009
1010 switch (auth->state) {
1011 case IWI_AUTHENTICATED:
1012 ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1);
1013 break;
1014
1015 case IWI_DEAUTHENTICATED:
1016 break;
1017
1018 default:
1019 printf("%s: unknown authentication state %u\n",
1020 sc->sc_dev.dv_xname, auth->state);
1021 }
1022 break;
1023 }
1024 case IWI_NOTIF_TYPE_ASSOCIATION:
1025 {
1026 struct iwi_notif_association *assoc =
1027 (struct iwi_notif_association *)(notif + 1);
1028
1029 DPRINTFN(2, ("Association (%u, %u)\n", assoc->state,
1030 assoc->status));
1031
1032 switch (assoc->state) {
1033 case IWI_AUTHENTICATED:
1034 /* re-association, do nothing */
1035 break;
1036
1037 case IWI_ASSOCIATED:
1038 if (ic->ic_flags & IEEE80211_F_RSNON)
1039 ni->ni_rsn_supp_state = RSNA_SUPP_PTKSTART;
1040 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1041 break;
1042
1043 case IWI_DEASSOCIATED:
1044 ieee80211_begin_scan(ifp);
1045 break;
1046
1047 default:
1048 printf("%s: unknown association state %u\n",
1049 sc->sc_dev.dv_xname, assoc->state);
1050 }
1051 break;
1052 }
1053 case IWI_NOTIF_TYPE_BEACON:
1054 {
1055 struct iwi_notif_beacon *beacon =
1056 (struct iwi_notif_beacon *)(notif + 1);
1057
1058 if (letoh32(beacon->status) == IWI_BEACON_MISSED) {
1059 /* XXX should roam when too many beacons missed */
1060 DPRINTFN(2, ("%s: %u beacon(s) missed\n",
1061 sc->sc_dev.dv_xname, letoh32(beacon->count)));
1062 }
1063 break;
1064 }
1065 case IWI_NOTIF_TYPE_BAD_LINK:
1066 DPRINTFN(2, ("link deterioration detected\n"));
1067 break;
1068
1069 case IWI_NOTIF_TYPE_NOISE:
1070 DPRINTFN(5, ("Measured noise %u\n",
1071 letoh32(*(uint32_t *)(notif + 1)) & 0xff));
1072 break;
1073
1074 default:
1075 DPRINTFN(5, ("Notification (%u)\n", notif->type));
1076 }
1077 }
1078
1079 void
iwi_rx_intr(struct iwi_softc * sc)1080 iwi_rx_intr(struct iwi_softc *sc)
1081 {
1082 struct mbuf_list ml = MBUF_LIST_INITIALIZER();
1083 struct iwi_rx_data *data;
1084 struct iwi_hdr *hdr;
1085 uint32_t hw;
1086
1087 hw = CSR_READ_4(sc, IWI_CSR_RX_RIDX);
1088
1089 for (; sc->rxq.cur != hw;) {
1090 data = &sc->rxq.data[sc->rxq.cur];
1091
1092 bus_dmamap_sync(sc->sc_dmat, data->map, 0, MCLBYTES,
1093 BUS_DMASYNC_POSTREAD);
1094
1095 hdr = mtod(data->m, struct iwi_hdr *);
1096
1097 switch (hdr->type) {
1098 case IWI_HDR_TYPE_FRAME:
1099 iwi_frame_intr(sc, data,
1100 (struct iwi_frame *)(hdr + 1), &ml);
1101 break;
1102
1103 case IWI_HDR_TYPE_NOTIF:
1104 iwi_notification_intr(sc, data,
1105 (struct iwi_notif *)(hdr + 1));
1106 break;
1107
1108 default:
1109 printf("%s: unknown hdr type %u\n",
1110 sc->sc_dev.dv_xname, hdr->type);
1111 }
1112
1113 sc->rxq.cur = (sc->rxq.cur + 1) % IWI_RX_RING_COUNT;
1114 }
1115 if_input(&sc->sc_ic.ic_if, &ml);
1116
1117 /* tell the firmware what we have processed */
1118 hw = (hw == 0) ? IWI_RX_RING_COUNT - 1 : hw - 1;
1119 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, hw);
1120 }
1121
1122 void
iwi_tx_intr(struct iwi_softc * sc,struct iwi_tx_ring * txq)1123 iwi_tx_intr(struct iwi_softc *sc, struct iwi_tx_ring *txq)
1124 {
1125 struct ieee80211com *ic = &sc->sc_ic;
1126 struct ifnet *ifp = &ic->ic_if;
1127 struct iwi_tx_data *data;
1128 uint32_t hw;
1129
1130 hw = CSR_READ_4(sc, txq->csr_ridx);
1131
1132 for (; txq->next != hw;) {
1133 data = &txq->data[txq->next];
1134
1135 bus_dmamap_unload(sc->sc_dmat, data->map);
1136 m_freem(data->m);
1137 data->m = NULL;
1138 ieee80211_release_node(ic, data->ni);
1139 data->ni = NULL;
1140
1141 txq->queued--;
1142 txq->next = (txq->next + 1) % IWI_TX_RING_COUNT;
1143 }
1144
1145 sc->sc_tx_timer = 0;
1146 ifq_clr_oactive(&ifp->if_snd);
1147 (*ifp->if_start)(ifp);
1148 }
1149
1150 int
iwi_intr(void * arg)1151 iwi_intr(void *arg)
1152 {
1153 struct iwi_softc *sc = arg;
1154 struct ifnet *ifp = &sc->sc_ic.ic_if;
1155 uint32_t r;
1156
1157 if ((r = CSR_READ_4(sc, IWI_CSR_INTR)) == 0 || r == 0xffffffff)
1158 return 0;
1159
1160 /* disable interrupts */
1161 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
1162
1163 /* acknowledge interrupts */
1164 CSR_WRITE_4(sc, IWI_CSR_INTR, r);
1165
1166 if (r & IWI_INTR_FATAL_ERROR) {
1167 printf("%s: fatal firmware error\n", sc->sc_dev.dv_xname);
1168 iwi_stop(ifp, 1);
1169 task_add(systq, &sc->init_task);
1170 return 1;
1171 }
1172
1173 if (r & IWI_INTR_FW_INITED)
1174 wakeup(sc);
1175
1176 if (r & IWI_INTR_RADIO_OFF) {
1177 DPRINTF(("radio transmitter off\n"));
1178 iwi_stop(ifp, 1);
1179 return 1;
1180 }
1181
1182 if (r & IWI_INTR_CMD_DONE) {
1183 /* kick next pending command if any */
1184 sc->cmdq.next = (sc->cmdq.next + 1) % IWI_CMD_RING_COUNT;
1185 if (--sc->cmdq.queued > 0)
1186 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.next);
1187
1188 wakeup(sc);
1189 }
1190
1191 if (r & IWI_INTR_TX1_DONE)
1192 iwi_tx_intr(sc, &sc->txq[0]);
1193
1194 if (r & IWI_INTR_TX2_DONE)
1195 iwi_tx_intr(sc, &sc->txq[1]);
1196
1197 if (r & IWI_INTR_TX3_DONE)
1198 iwi_tx_intr(sc, &sc->txq[2]);
1199
1200 if (r & IWI_INTR_TX4_DONE)
1201 iwi_tx_intr(sc, &sc->txq[3]);
1202
1203 if (r & IWI_INTR_RX_DONE)
1204 iwi_rx_intr(sc);
1205
1206 /* re-enable interrupts */
1207 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
1208
1209 return 1;
1210 }
1211
1212 int
iwi_cmd(struct iwi_softc * sc,uint8_t type,void * data,uint8_t len,int async)1213 iwi_cmd(struct iwi_softc *sc, uint8_t type, void *data, uint8_t len, int async)
1214 {
1215 struct iwi_cmd_desc *desc;
1216
1217 desc = &sc->cmdq.desc[sc->cmdq.cur];
1218 desc->hdr.type = IWI_HDR_TYPE_COMMAND;
1219 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1220 desc->type = type;
1221 desc->len = len;
1222 bcopy(data, desc->data, len);
1223
1224 bus_dmamap_sync(sc->sc_dmat, sc->cmdq.map,
1225 sc->cmdq.cur * sizeof (struct iwi_cmd_desc),
1226 sizeof (struct iwi_cmd_desc), BUS_DMASYNC_PREWRITE);
1227
1228 DPRINTFN(2, ("sending command idx=%u type=%u len=%u\n", sc->cmdq.cur,
1229 type, len));
1230
1231 sc->cmdq.cur = (sc->cmdq.cur + 1) % IWI_CMD_RING_COUNT;
1232
1233 /* don't kick cmd immediately if another async command is pending */
1234 if (++sc->cmdq.queued == 1) {
1235 sc->cmdq.next = sc->cmdq.cur;
1236 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.next);
1237 }
1238
1239 return async ? 0 : tsleep_nsec(sc, PCATCH, "iwicmd", SEC_TO_NSEC(1));
1240 }
1241
1242 int
iwi_send_mgmt(struct ieee80211com * ic,struct ieee80211_node * ni,int type,int arg1,int arg2)1243 iwi_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni, int type,
1244 int arg1, int arg2)
1245 {
1246 return EOPNOTSUPP;
1247 }
1248
1249 int
iwi_tx_start(struct ifnet * ifp,struct mbuf * m0,struct ieee80211_node * ni)1250 iwi_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni)
1251 {
1252 struct iwi_softc *sc = ifp->if_softc;
1253 struct ieee80211com *ic = &sc->sc_ic;
1254 struct ieee80211_frame *wh;
1255 struct ieee80211_key *k;
1256 struct iwi_tx_data *data;
1257 struct iwi_tx_desc *desc;
1258 struct iwi_tx_ring *txq = &sc->txq[0];
1259 int hdrlen, error, i, station = 0;
1260
1261 wh = mtod(m0, struct ieee80211_frame *);
1262
1263 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1264 k = ieee80211_get_txkey(ic, wh, ni);
1265
1266 if ((m0 = ieee80211_encrypt(ic, m0, k)) == NULL)
1267 return ENOBUFS;
1268
1269 /* packet header may have moved, reset our local pointer */
1270 wh = mtod(m0, struct ieee80211_frame *);
1271 }
1272
1273 #if NBPFILTER > 0
1274 if (sc->sc_drvbpf != NULL) {
1275 struct iwi_tx_radiotap_header *tap = &sc->sc_txtap;
1276
1277 tap->wt_flags = 0;
1278 tap->wt_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
1279 tap->wt_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
1280
1281 bpf_mtap_hdr(sc->sc_drvbpf, tap, sc->sc_txtap_len,
1282 m0, BPF_DIRECTION_OUT);
1283 }
1284 #endif
1285
1286 data = &txq->data[txq->cur];
1287 desc = &txq->desc[txq->cur];
1288
1289 /* copy and trim IEEE802.11 header */
1290 hdrlen = ieee80211_get_hdrlen(wh);
1291 bcopy(wh, &desc->wh, hdrlen);
1292 m_adj(m0, hdrlen);
1293
1294 #ifndef IEEE80211_STA_ONLY
1295 if (ic->ic_opmode == IEEE80211_M_IBSS) {
1296 station = iwi_find_txnode(sc, desc->wh.i_addr1);
1297 if (station == -1) {
1298 m_freem(m0);
1299 ieee80211_release_node(ic, ni);
1300 ifp->if_oerrors++;
1301 return 0;
1302 }
1303 }
1304 #endif
1305
1306 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
1307 BUS_DMA_NOWAIT);
1308 if (error != 0 && error != EFBIG) {
1309 printf("%s: can't map mbuf (error %d)\n",
1310 sc->sc_dev.dv_xname, error);
1311 m_freem(m0);
1312 return error;
1313 }
1314 if (error != 0) {
1315 /* too many fragments, linearize */
1316 if (m_defrag(m0, M_DONTWAIT)) {
1317 m_freem(m0);
1318 return ENOBUFS;
1319 }
1320 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
1321 BUS_DMA_NOWAIT);
1322 if (error != 0) {
1323 printf("%s: can't map mbuf (error %d)\n",
1324 sc->sc_dev.dv_xname, error);
1325 m_freem(m0);
1326 return error;
1327 }
1328 }
1329
1330 data->m = m0;
1331 data->ni = ni;
1332
1333 desc->hdr.type = IWI_HDR_TYPE_DATA;
1334 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1335 desc->cmd = IWI_DATA_CMD_TX;
1336 desc->len = htole16(m0->m_pkthdr.len);
1337 desc->station = station;
1338 desc->flags = IWI_DATA_FLAG_NO_WEP;
1339 desc->xflags = 0;
1340
1341 if (!IEEE80211_IS_MULTICAST(desc->wh.i_addr1))
1342 desc->flags |= IWI_DATA_FLAG_NEED_ACK;
1343
1344 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1345 desc->flags |= IWI_DATA_FLAG_SHPREAMBLE;
1346
1347 if ((desc->wh.i_fc[0] &
1348 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_QOS)) ==
1349 (IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS))
1350 desc->xflags |= IWI_DATA_XFLAG_QOS;
1351
1352 if (ic->ic_curmode == IEEE80211_MODE_11B)
1353 desc->xflags |= IWI_DATA_XFLAG_CCK;
1354
1355 desc->nseg = htole32(data->map->dm_nsegs);
1356 for (i = 0; i < data->map->dm_nsegs; i++) {
1357 desc->seg_addr[i] = htole32(data->map->dm_segs[i].ds_addr);
1358 desc->seg_len[i] = htole16(data->map->dm_segs[i].ds_len);
1359 }
1360
1361 bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize,
1362 BUS_DMASYNC_PREWRITE);
1363 bus_dmamap_sync(sc->sc_dmat, txq->map,
1364 txq->cur * sizeof (struct iwi_tx_desc),
1365 sizeof (struct iwi_tx_desc), BUS_DMASYNC_PREWRITE);
1366
1367 DPRINTFN(5, ("sending data frame idx=%u len=%u nseg=%u\n", txq->cur,
1368 letoh16(desc->len), data->map->dm_nsegs));
1369
1370 txq->queued++;
1371 txq->cur = (txq->cur + 1) % IWI_TX_RING_COUNT;
1372 CSR_WRITE_4(sc, txq->csr_widx, txq->cur);
1373
1374 return 0;
1375 }
1376
1377 void
iwi_start(struct ifnet * ifp)1378 iwi_start(struct ifnet *ifp)
1379 {
1380 struct iwi_softc *sc = ifp->if_softc;
1381 struct ieee80211com *ic = &sc->sc_ic;
1382 struct mbuf *m0;
1383 struct ieee80211_node *ni;
1384
1385 if (ic->ic_state != IEEE80211_S_RUN)
1386 return;
1387
1388 for (;;) {
1389 if (sc->txq[0].queued + IWI_MAX_NSEG + 2 >= IWI_TX_RING_COUNT) {
1390 ifq_set_oactive(&ifp->if_snd);
1391 break;
1392 }
1393
1394 m0 = ifq_dequeue(&ifp->if_snd);
1395 if (m0 == NULL)
1396 break;
1397
1398 #if NBPFILTER > 0
1399 if (ifp->if_bpf != NULL)
1400 bpf_mtap(ifp->if_bpf, m0, BPF_DIRECTION_OUT);
1401 #endif
1402
1403 m0 = ieee80211_encap(ifp, m0, &ni);
1404 if (m0 == NULL)
1405 continue;
1406
1407 #if NBPFILTER > 0
1408 if (ic->ic_rawbpf != NULL)
1409 bpf_mtap(ic->ic_rawbpf, m0, BPF_DIRECTION_OUT);
1410 #endif
1411
1412 if (iwi_tx_start(ifp, m0, ni) != 0) {
1413 if (ni != NULL)
1414 ieee80211_release_node(ic, ni);
1415 ifp->if_oerrors++;
1416 break;
1417 }
1418
1419 /* start watchdog timer */
1420 sc->sc_tx_timer = 5;
1421 ifp->if_timer = 1;
1422 }
1423 }
1424
1425 void
iwi_watchdog(struct ifnet * ifp)1426 iwi_watchdog(struct ifnet *ifp)
1427 {
1428 struct iwi_softc *sc = ifp->if_softc;
1429
1430 ifp->if_timer = 0;
1431
1432 if (sc->sc_tx_timer > 0) {
1433 if (--sc->sc_tx_timer == 0) {
1434 printf("%s: device timeout\n", sc->sc_dev.dv_xname);
1435 iwi_stop(ifp, 1);
1436 ifp->if_oerrors++;
1437 return;
1438 }
1439 ifp->if_timer = 1;
1440 }
1441
1442 ieee80211_watchdog(ifp);
1443 }
1444
1445 int
iwi_ioctl(struct ifnet * ifp,u_long cmd,caddr_t data)1446 iwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1447 {
1448 struct iwi_softc *sc = ifp->if_softc;
1449 int s, error = 0;
1450
1451 error = rw_enter(&sc->sc_rwlock, RW_WRITE | RW_INTR);
1452 if (error)
1453 return error;
1454 s = splnet();
1455
1456 switch (cmd) {
1457 case SIOCSIFADDR:
1458 ifp->if_flags |= IFF_UP;
1459 /* FALLTHROUGH */
1460 case SIOCSIFFLAGS:
1461 if (ifp->if_flags & IFF_UP) {
1462 if (!(ifp->if_flags & IFF_RUNNING))
1463 iwi_init(ifp);
1464 } else {
1465 if (ifp->if_flags & IFF_RUNNING)
1466 iwi_stop(ifp, 1);
1467 }
1468 break;
1469
1470 case SIOCG80211TXPOWER:
1471 /*
1472 * If the hardware radio transmitter switch is off, report a
1473 * tx power of IEEE80211_TXPOWER_MIN to indicate that radio
1474 * transmitter is killed.
1475 */
1476 ((struct ieee80211_txpower *)data)->i_val =
1477 (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) ?
1478 sc->sc_ic.ic_txpower : IEEE80211_TXPOWER_MIN;
1479 break;
1480
1481 default:
1482 error = ieee80211_ioctl(ifp, cmd, data);
1483 }
1484
1485 if (error == ENETRESET) {
1486 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1487 (IFF_UP | IFF_RUNNING))
1488 iwi_init(ifp);
1489 error = 0;
1490 }
1491
1492 splx(s);
1493 rw_exit_write(&sc->sc_rwlock);
1494 return error;
1495 }
1496
1497 void
iwi_stop_master(struct iwi_softc * sc)1498 iwi_stop_master(struct iwi_softc *sc)
1499 {
1500 uint32_t tmp;
1501 int ntries;
1502
1503 /* disable interrupts */
1504 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
1505
1506 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER);
1507 for (ntries = 0; ntries < 5; ntries++) {
1508 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
1509 break;
1510 DELAY(10);
1511 }
1512 if (ntries == 5) {
1513 printf("%s: timeout waiting for master\n",
1514 sc->sc_dev.dv_xname);
1515 }
1516
1517 tmp = CSR_READ_4(sc, IWI_CSR_RST);
1518 CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_PRINCETON_RESET);
1519 }
1520
1521 int
iwi_reset(struct iwi_softc * sc)1522 iwi_reset(struct iwi_softc *sc)
1523 {
1524 uint32_t tmp;
1525 int i, ntries;
1526
1527 iwi_stop_master(sc);
1528
1529 /* move adapter to D0 state */
1530 tmp = CSR_READ_4(sc, IWI_CSR_CTL);
1531 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT);
1532
1533 CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST);
1534
1535 /* wait for clock stabilization */
1536 for (ntries = 0; ntries < 1000; ntries++) {
1537 if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY)
1538 break;
1539 DELAY(200);
1540 }
1541 if (ntries == 1000) {
1542 printf("%s: timeout waiting for clock stabilization\n",
1543 sc->sc_dev.dv_xname);
1544 return ETIMEDOUT;
1545 }
1546
1547 tmp = CSR_READ_4(sc, IWI_CSR_RST);
1548 CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_SW_RESET);
1549
1550 DELAY(10);
1551
1552 tmp = CSR_READ_4(sc, IWI_CSR_CTL);
1553 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT);
1554
1555 /* clear NIC memory */
1556 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0);
1557 for (i = 0; i < 0xc000; i++)
1558 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
1559
1560 return 0;
1561 }
1562
1563 int
iwi_load_ucode(struct iwi_softc * sc,const char * data,int size)1564 iwi_load_ucode(struct iwi_softc *sc, const char *data, int size)
1565 {
1566 const uint16_t *w;
1567 uint32_t tmp;
1568 int ntries, i;
1569
1570 tmp = CSR_READ_4(sc, IWI_CSR_RST);
1571 CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_STOP_MASTER);
1572 for (ntries = 0; ntries < 5; ntries++) {
1573 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
1574 break;
1575 DELAY(10);
1576 }
1577 if (ntries == 5) {
1578 printf("%s: timeout waiting for master\n",
1579 sc->sc_dev.dv_xname);
1580 return ETIMEDOUT;
1581 }
1582
1583 MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
1584 DELAY(5000);
1585
1586 tmp = CSR_READ_4(sc, IWI_CSR_RST);
1587 CSR_WRITE_4(sc, IWI_CSR_RST, tmp & ~IWI_RST_PRINCETON_RESET);
1588
1589 DELAY(5000);
1590 MEM_WRITE_4(sc, 0x3000e0, 0);
1591 DELAY(1000);
1592 MEM_WRITE_4(sc, IWI_MEM_EVENT_CTL, 1);
1593 DELAY(1000);
1594 MEM_WRITE_4(sc, IWI_MEM_EVENT_CTL, 0);
1595 DELAY(1000);
1596 MEM_WRITE_1(sc, 0x200000, 0x00);
1597 MEM_WRITE_1(sc, 0x200000, 0x40);
1598 DELAY(1000);
1599
1600 /* adapter is buggy, we must set the address for each word */
1601 for (w = (const uint16_t *)data; size > 0; w++, size -= 2)
1602 MEM_WRITE_2(sc, 0x200010, htole16(*w));
1603
1604 MEM_WRITE_1(sc, 0x200000, 0x00);
1605 MEM_WRITE_1(sc, 0x200000, 0x80);
1606
1607 /* wait until we get an answer */
1608 for (ntries = 0; ntries < 100; ntries++) {
1609 if (MEM_READ_1(sc, 0x200000) & 1)
1610 break;
1611 DELAY(100);
1612 }
1613 if (ntries == 100) {
1614 printf("%s: timeout waiting for ucode to initialize\n",
1615 sc->sc_dev.dv_xname);
1616 return ETIMEDOUT;
1617 }
1618
1619 /* read the answer or the firmware will not initialize properly */
1620 for (i = 0; i < 7; i++)
1621 MEM_READ_4(sc, 0x200004);
1622
1623 MEM_WRITE_1(sc, 0x200000, 0x00);
1624
1625 return 0;
1626 }
1627
1628 /* macro to handle unaligned little endian data in firmware image */
1629 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
1630
1631 int
iwi_load_firmware(struct iwi_softc * sc,const char * data,int size)1632 iwi_load_firmware(struct iwi_softc *sc, const char *data, int size)
1633 {
1634 bus_dmamap_t map;
1635 bus_dma_segment_t seg;
1636 caddr_t virtaddr;
1637 u_char *p, *end;
1638 uint32_t sentinel, tmp, ctl, src, dst, sum, len, mlen;
1639 int ntries, nsegs, error;
1640
1641 /* allocate DMA memory to store firmware image */
1642 error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
1643 BUS_DMA_NOWAIT, &map);
1644 if (error != 0) {
1645 printf("%s: could not create firmware DMA map\n",
1646 sc->sc_dev.dv_xname);
1647 goto fail1;
1648 }
1649
1650 error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg, 1,
1651 &nsegs, BUS_DMA_NOWAIT);
1652 if (error != 0) {
1653 printf("%s: could not allocate firmware DMA memory\n",
1654 sc->sc_dev.dv_xname);
1655 goto fail2;
1656 }
1657
1658 error = bus_dmamem_map(sc->sc_dmat, &seg, nsegs, size, &virtaddr,
1659 BUS_DMA_NOWAIT);
1660 if (error != 0) {
1661 printf("%s: can't map firmware DMA memory\n",
1662 sc->sc_dev.dv_xname);
1663 goto fail3;
1664 }
1665
1666 error = bus_dmamap_load(sc->sc_dmat, map, virtaddr, size, NULL,
1667 BUS_DMA_NOWAIT);
1668 if (error != 0) {
1669 printf("%s: could not load firmware DMA map\n",
1670 sc->sc_dev.dv_xname);
1671 goto fail4;
1672 }
1673
1674 /* copy firmware image to DMA memory */
1675 bcopy(data, virtaddr, size);
1676
1677 /* make sure the adapter will get up-to-date values */
1678 bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_PREWRITE);
1679
1680 /* tell the adapter where the command blocks are stored */
1681 MEM_WRITE_4(sc, 0x3000a0, 0x27000);
1682
1683 /*
1684 * Store command blocks into adapter's internal memory using register
1685 * indirections. The adapter will read the firmware image through DMA
1686 * using information stored in command blocks.
1687 */
1688 src = map->dm_segs[0].ds_addr;
1689 p = virtaddr;
1690 end = p + size;
1691 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000);
1692
1693 while (p < end) {
1694 dst = GETLE32(p); p += 4; src += 4;
1695 len = GETLE32(p); p += 4; src += 4;
1696 p += len;
1697
1698 while (len > 0) {
1699 mlen = min(len, IWI_CB_MAXDATALEN);
1700
1701 ctl = IWI_CB_DEFAULT_CTL | mlen;
1702 sum = ctl ^ src ^ dst;
1703
1704 /* write a command block */
1705 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, ctl);
1706 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, src);
1707 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, dst);
1708 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum);
1709
1710 src += mlen;
1711 dst += mlen;
1712 len -= mlen;
1713 }
1714 }
1715
1716 /* write a fictive final command block (sentinel) */
1717 sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR);
1718 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
1719
1720 tmp = CSR_READ_4(sc, IWI_CSR_RST);
1721 tmp &= ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER);
1722 CSR_WRITE_4(sc, IWI_CSR_RST, tmp);
1723
1724 /* tell the adapter to start processing command blocks */
1725 MEM_WRITE_4(sc, 0x3000a4, 0x540100);
1726
1727 /* wait until the adapter has processed all command blocks */
1728 for (ntries = 0; ntries < 400; ntries++) {
1729 if (MEM_READ_4(sc, 0x3000d0) >= sentinel)
1730 break;
1731 DELAY(100);
1732 }
1733 if (ntries == 400) {
1734 printf("%s: timeout processing cb\n", sc->sc_dev.dv_xname);
1735 error = ETIMEDOUT;
1736 goto fail5;
1737 }
1738
1739 /* we're done with command blocks processing */
1740 MEM_WRITE_4(sc, 0x3000a4, 0x540c00);
1741
1742 /* allow interrupts so we know when the firmware is inited */
1743 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
1744
1745 /* tell the adapter to initialize the firmware */
1746 CSR_WRITE_4(sc, IWI_CSR_RST, 0);
1747
1748 tmp = CSR_READ_4(sc, IWI_CSR_CTL);
1749 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_ALLOW_STANDBY);
1750
1751 /* wait at most one second for firmware initialization to complete */
1752 if ((error = tsleep_nsec(sc, PCATCH, "iwiinit", SEC_TO_NSEC(1))) != 0) {
1753 printf("%s: timeout waiting for firmware initialization to "
1754 "complete\n", sc->sc_dev.dv_xname);
1755 goto fail5;
1756 }
1757
1758 fail5: bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_POSTWRITE);
1759 bus_dmamap_unload(sc->sc_dmat, map);
1760 fail4: bus_dmamem_unmap(sc->sc_dmat, virtaddr, size);
1761 fail3: bus_dmamem_free(sc->sc_dmat, &seg, 1);
1762 fail2: bus_dmamap_destroy(sc->sc_dmat, map);
1763 fail1: return error;
1764 }
1765
1766 int
iwi_config(struct iwi_softc * sc)1767 iwi_config(struct iwi_softc *sc)
1768 {
1769 struct ieee80211com *ic = &sc->sc_ic;
1770 struct ifnet *ifp = &ic->ic_if;
1771 struct iwi_configuration config;
1772 struct iwi_rateset rs;
1773 struct iwi_txpower power;
1774 uint32_t data;
1775 int error, nchan, i;
1776
1777 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
1778 DPRINTF(("Setting MAC address to %s\n", ether_sprintf(ic->ic_myaddr)));
1779 error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, ic->ic_myaddr,
1780 IEEE80211_ADDR_LEN, 0);
1781 if (error != 0)
1782 return error;
1783
1784 bzero(&config, sizeof config);
1785 config.multicast_enabled = 1;
1786 config.silence_threshold = 30;
1787 config.report_noise = 1;
1788 config.answer_pbreq =
1789 #ifndef IEEE80211_STA_ONLY
1790 (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 :
1791 #endif
1792 0;
1793 DPRINTF(("Configuring adapter\n"));
1794 error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config, 0);
1795 if (error != 0)
1796 return error;
1797
1798 data = htole32(IWI_POWER_MODE_CAM);
1799 DPRINTF(("Setting power mode to %u\n", letoh32(data)));
1800 error = iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data, 0);
1801 if (error != 0)
1802 return error;
1803
1804 data = htole32(ic->ic_rtsthreshold);
1805 DPRINTF(("Setting RTS threshold to %u\n", letoh32(data)));
1806 error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data, 0);
1807 if (error != 0)
1808 return error;
1809
1810 data = htole32(ic->ic_fragthreshold);
1811 DPRINTF(("Setting fragmentation threshold to %u\n", letoh32(data)));
1812 error = iwi_cmd(sc, IWI_CMD_SET_FRAG_THRESHOLD, &data, sizeof data, 0);
1813 if (error != 0)
1814 return error;
1815
1816 /*
1817 * Set default Tx power for 802.11b/g and 802.11a channels.
1818 */
1819 nchan = 0;
1820 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
1821 if (!IEEE80211_IS_CHAN_2GHZ(&ic->ic_channels[i]))
1822 continue;
1823 power.chan[nchan].chan = i;
1824 power.chan[nchan].power = IWI_TXPOWER_MAX;
1825 nchan++;
1826 }
1827 power.nchan = nchan;
1828
1829 power.mode = IWI_MODE_11G;
1830 DPRINTF(("Setting .11g channels tx power\n"));
1831 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power, 0);
1832 if (error != 0)
1833 return error;
1834
1835 power.mode = IWI_MODE_11B;
1836 DPRINTF(("Setting .11b channels tx power\n"));
1837 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power, 0);
1838 if (error != 0)
1839 return error;
1840
1841 nchan = 0;
1842 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
1843 if (!IEEE80211_IS_CHAN_5GHZ(&ic->ic_channels[i]))
1844 continue;
1845 power.chan[nchan].chan = i;
1846 power.chan[nchan].power = IWI_TXPOWER_MAX;
1847 nchan++;
1848 }
1849 power.nchan = nchan;
1850
1851 if (nchan > 0) { /* 2915ABG only */
1852 power.mode = IWI_MODE_11A;
1853 DPRINTF(("Setting .11a channels tx power\n"));
1854 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power,
1855 0);
1856 if (error != 0)
1857 return error;
1858 }
1859
1860 rs.mode = IWI_MODE_11G;
1861 rs.type = IWI_RATESET_TYPE_SUPPORTED;
1862 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates;
1863 bcopy(ic->ic_sup_rates[IEEE80211_MODE_11G].rs_rates, rs.rates,
1864 rs.nrates);
1865 DPRINTF(("Setting .11bg supported rates (%u)\n", rs.nrates));
1866 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
1867 if (error != 0)
1868 return error;
1869
1870 rs.mode = IWI_MODE_11A;
1871 rs.type = IWI_RATESET_TYPE_SUPPORTED;
1872 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates;
1873 bcopy(ic->ic_sup_rates[IEEE80211_MODE_11A].rs_rates, rs.rates,
1874 rs.nrates);
1875 DPRINTF(("Setting .11a supported rates (%u)\n", rs.nrates));
1876 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
1877 if (error != 0)
1878 return error;
1879
1880 /* if we have a desired ESSID, set it now */
1881 if (ic->ic_des_esslen != 0) {
1882 #ifdef IWI_DEBUG
1883 if (iwi_debug > 0) {
1884 printf("Setting desired ESSID to ");
1885 ieee80211_print_essid(ic->ic_des_essid,
1886 ic->ic_des_esslen);
1887 printf("\n");
1888 }
1889 #endif
1890 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ic->ic_des_essid,
1891 ic->ic_des_esslen, 0);
1892 if (error != 0)
1893 return error;
1894 }
1895
1896 arc4random_buf(&data, sizeof data);
1897 DPRINTF(("Setting random seed to %u\n", data));
1898 error = iwi_cmd(sc, IWI_CMD_SET_RANDOM_SEED, &data, sizeof data, 0);
1899 if (error != 0)
1900 return error;
1901
1902 /* enable adapter */
1903 DPRINTF(("Enabling adapter\n"));
1904 return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0, 0);
1905 }
1906
1907 void
iwi_update_edca(struct ieee80211com * ic)1908 iwi_update_edca(struct ieee80211com *ic)
1909 {
1910 #define IWI_EXP2(v) htole16((1 << (v)) - 1)
1911 #define IWI_TXOP(v) IEEE80211_TXOP_TO_US(v)
1912 struct iwi_softc *sc = ic->ic_softc;
1913 struct iwi_qos_cmd cmd;
1914 struct iwi_qos_params *qos;
1915 struct ieee80211_edca_ac_params *edca = ic->ic_edca_ac;
1916 int aci;
1917
1918 /* set default QoS parameters for CCK */
1919 qos = &cmd.cck;
1920 for (aci = 0; aci < EDCA_NUM_AC; aci++) {
1921 qos->cwmin[aci] = IWI_EXP2(iwi_cck[aci].ac_ecwmin);
1922 qos->cwmax[aci] = IWI_EXP2(iwi_cck[aci].ac_ecwmax);
1923 qos->txop [aci] = IWI_TXOP(iwi_cck[aci].ac_txoplimit);
1924 qos->aifsn[aci] = iwi_cck[aci].ac_aifsn;
1925 qos->acm [aci] = 0;
1926 }
1927 /* set default QoS parameters for OFDM */
1928 qos = &cmd.ofdm;
1929 for (aci = 0; aci < EDCA_NUM_AC; aci++) {
1930 qos->cwmin[aci] = IWI_EXP2(iwi_ofdm[aci].ac_ecwmin);
1931 qos->cwmax[aci] = IWI_EXP2(iwi_ofdm[aci].ac_ecwmax);
1932 qos->txop [aci] = IWI_TXOP(iwi_ofdm[aci].ac_txoplimit);
1933 qos->aifsn[aci] = iwi_ofdm[aci].ac_aifsn;
1934 qos->acm [aci] = 0;
1935 }
1936 /* set current QoS parameters */
1937 qos = &cmd.current;
1938 for (aci = 0; aci < EDCA_NUM_AC; aci++) {
1939 qos->cwmin[aci] = IWI_EXP2(edca[aci].ac_ecwmin);
1940 qos->cwmax[aci] = IWI_EXP2(edca[aci].ac_ecwmax);
1941 qos->txop [aci] = IWI_TXOP(edca[aci].ac_txoplimit);
1942 qos->aifsn[aci] = edca[aci].ac_aifsn;
1943 qos->acm [aci] = 0;
1944 }
1945
1946 DPRINTF(("Setting QoS parameters\n"));
1947 (void)iwi_cmd(sc, IWI_CMD_SET_QOS_PARAMS, &cmd, sizeof cmd, 1);
1948 #undef IWI_EXP2
1949 #undef IWI_TXOP
1950 }
1951
1952 int
iwi_set_chan(struct iwi_softc * sc,struct ieee80211_channel * chan)1953 iwi_set_chan(struct iwi_softc *sc, struct ieee80211_channel *chan)
1954 {
1955 struct ieee80211com *ic = &sc->sc_ic;
1956 struct iwi_scan scan;
1957
1958 bzero(&scan, sizeof scan);
1959 memset(scan.type, IWI_SCAN_TYPE_PASSIVE, sizeof scan.type);
1960 scan.passive = htole16(2000);
1961 scan.channels[0] = 1 |
1962 (IEEE80211_IS_CHAN_5GHZ(chan) ? IWI_CHAN_5GHZ : IWI_CHAN_2GHZ);
1963 scan.channels[1] = ieee80211_chan2ieee(ic, chan);
1964
1965 DPRINTF(("Setting channel to %u\n", ieee80211_chan2ieee(ic, chan)));
1966 return iwi_cmd(sc, IWI_CMD_SCAN, &scan, sizeof scan, 1);
1967 }
1968
1969 int
iwi_scan(struct iwi_softc * sc)1970 iwi_scan(struct iwi_softc *sc)
1971 {
1972 struct ieee80211com *ic = &sc->sc_ic;
1973 struct iwi_scan scan;
1974 uint8_t *p;
1975 int i, count;
1976
1977 bzero(&scan, sizeof scan);
1978
1979 if (ic->ic_des_esslen != 0) {
1980 scan.bdirected = htole16(40);
1981 memset(scan.type, IWI_SCAN_TYPE_BDIRECTED, sizeof scan.type);
1982 } else {
1983 scan.broadcast = htole16(40);
1984 memset(scan.type, IWI_SCAN_TYPE_BROADCAST, sizeof scan.type);
1985 }
1986
1987 p = scan.channels;
1988 count = 0;
1989 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
1990 if (IEEE80211_IS_CHAN_5GHZ(&ic->ic_channels[i])) {
1991 *++p = i;
1992 count++;
1993 }
1994 }
1995 *(p - count) = IWI_CHAN_5GHZ | count;
1996
1997 p = (count > 0) ? p + 1 : scan.channels;
1998 count = 0;
1999 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
2000 if (IEEE80211_IS_CHAN_2GHZ(&ic->ic_channels[i])) {
2001 *++p = i;
2002 count++;
2003 }
2004 }
2005 *(p - count) = IWI_CHAN_2GHZ | count;
2006
2007 DPRINTF(("Start scanning\n"));
2008 return iwi_cmd(sc, IWI_CMD_SCAN, &scan, sizeof scan, 1);
2009 }
2010
2011 int
iwi_auth_and_assoc(struct iwi_softc * sc)2012 iwi_auth_and_assoc(struct iwi_softc *sc)
2013 {
2014 struct ieee80211com *ic = &sc->sc_ic;
2015 struct ieee80211_node *ni = ic->ic_bss;
2016 struct iwi_configuration config;
2017 struct iwi_associate assoc;
2018 struct iwi_rateset rs;
2019 uint8_t *frm;
2020 uint32_t data;
2021 uint16_t capinfo;
2022 uint8_t buf[64]; /* XXX max WPA/RSN/WMM IE length */
2023 int error;
2024
2025 /* update adapter configuration */
2026 bzero(&config, sizeof config);
2027 config.multicast_enabled = 1;
2028 config.disable_unicast_decryption = 1;
2029 config.disable_multicast_decryption = 1;
2030 config.silence_threshold = 30;
2031 config.report_noise = 1;
2032 config.allow_mgt = 1;
2033 config.answer_pbreq =
2034 #ifndef IEEE80211_STA_ONLY
2035 (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 :
2036 #endif
2037 0;
2038 if (ic->ic_curmode == IEEE80211_MODE_11G)
2039 config.bg_autodetection = 1;
2040 DPRINTF(("Configuring adapter\n"));
2041 error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config, 1);
2042 if (error != 0)
2043 return error;
2044
2045 #ifdef IWI_DEBUG
2046 if (iwi_debug > 0) {
2047 printf("Setting ESSID to ");
2048 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
2049 printf("\n");
2050 }
2051 #endif
2052 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen, 1);
2053 if (error != 0)
2054 return error;
2055
2056 /* the rate set has already been "negotiated" */
2057 rs.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A :
2058 IWI_MODE_11G;
2059 rs.type = IWI_RATESET_TYPE_NEGOTIATED;
2060 rs.nrates = ni->ni_rates.rs_nrates;
2061 if (rs.nrates > sizeof rs.rates) {
2062 #ifdef DIAGNOSTIC
2063 /* should not happen since the rates are negotiated */
2064 printf("%s: XXX too many rates (count=%d, last=%d)\n",
2065 sc->sc_dev.dv_xname, ni->ni_rates.rs_nrates,
2066 ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates - 1] &
2067 IEEE80211_RATE_VAL);
2068 #endif
2069 rs.nrates = sizeof rs.rates;
2070 }
2071 bcopy(ni->ni_rates.rs_rates, rs.rates, rs.nrates);
2072 DPRINTF(("Setting negotiated rates (%u)\n", rs.nrates));
2073 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 1);
2074 if (error != 0)
2075 return error;
2076
2077 data = htole32(ni->ni_rssi);
2078 DPRINTF(("Setting sensitivity to %d\n", (int8_t)ni->ni_rssi));
2079 error = iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &data, sizeof data, 1);
2080 if (error != 0)
2081 return error;
2082
2083 if (ic->ic_flags & IEEE80211_F_QOS) {
2084 iwi_update_edca(ic);
2085
2086 frm = ieee80211_add_qos_capability(buf, ic);
2087 DPRINTF(("Setting QoS Capability IE length %d\n", frm - buf));
2088 error = iwi_cmd(sc, IWI_CMD_SET_QOS_CAP, buf, frm - buf, 1);
2089 if (error != 0)
2090 return error;
2091 }
2092 if (ic->ic_flags & IEEE80211_F_RSNON) {
2093 /* tell firmware to add WPA/RSN IE to (re)assoc request */
2094 if (ni->ni_rsnprotos == IEEE80211_PROTO_RSN)
2095 frm = ieee80211_add_rsn(buf, ic, ni);
2096 else
2097 frm = ieee80211_add_wpa(buf, ic, ni);
2098 DPRINTF(("Setting RSN IE length %d\n", frm - buf));
2099 error = iwi_cmd(sc, IWI_CMD_SET_OPTIE, buf, frm - buf, 1);
2100 if (error != 0)
2101 return error;
2102 }
2103
2104 bzero(&assoc, sizeof assoc);
2105 #ifndef IEEE80211_STA_ONLY
2106 if (ic->ic_flags & IEEE80211_F_SIBSS)
2107 assoc.type = IWI_ASSOC_SIBSS;
2108 else
2109 #endif
2110 assoc.type = IWI_ASSOC_ASSOCIATE;
2111 assoc.policy = 0;
2112 if (ic->ic_flags & IEEE80211_F_RSNON)
2113 assoc.policy |= htole16(IWI_ASSOC_POLICY_RSN);
2114 if (ic->ic_flags & IEEE80211_F_QOS)
2115 assoc.policy |= htole16(IWI_ASSOC_POLICY_QOS);
2116 if (ic->ic_curmode == IEEE80211_MODE_11A)
2117 assoc.mode = IWI_MODE_11A;
2118 else if (ic->ic_curmode == IEEE80211_MODE_11B)
2119 assoc.mode = IWI_MODE_11B;
2120 else /* assume 802.11b/g */
2121 assoc.mode = IWI_MODE_11G;
2122 assoc.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
2123 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2124 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
2125 assoc.plen = IWI_ASSOC_SHPREAMBLE;
2126 bcopy(ni->ni_tstamp, assoc.tstamp, 8);
2127 capinfo = IEEE80211_CAPINFO_ESS;
2128 if (ic->ic_flags & IEEE80211_F_WEPON)
2129 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2130 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2131 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
2132 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2133 if (ic->ic_caps & IEEE80211_C_SHSLOT)
2134 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2135 assoc.capinfo = htole16(capinfo);
2136
2137 assoc.lintval = htole16(ic->ic_lintval);
2138 assoc.intval = htole16(ni->ni_intval);
2139 IEEE80211_ADDR_COPY(assoc.bssid, ni->ni_bssid);
2140 #ifndef IEEE80211_STA_ONLY
2141 if (ic->ic_opmode == IEEE80211_M_IBSS)
2142 IEEE80211_ADDR_COPY(assoc.dst, etherbroadcastaddr);
2143 else
2144 #endif
2145 IEEE80211_ADDR_COPY(assoc.dst, ni->ni_bssid);
2146
2147 DPRINTF(("Trying to associate to %s channel %u auth %u\n",
2148 ether_sprintf(assoc.bssid), assoc.chan, assoc.auth));
2149 return iwi_cmd(sc, IWI_CMD_ASSOCIATE, &assoc, sizeof assoc, 1);
2150 }
2151
2152 int
iwi_init(struct ifnet * ifp)2153 iwi_init(struct ifnet *ifp)
2154 {
2155 struct iwi_softc *sc = ifp->if_softc;
2156 struct ieee80211com *ic = &sc->sc_ic;
2157 struct iwi_firmware_hdr *hdr;
2158 const char *name, *fw;
2159 u_char *data;
2160 size_t size;
2161 int i, ac, error;
2162
2163 iwi_stop(ifp, 0);
2164
2165 if ((error = iwi_reset(sc)) != 0) {
2166 printf("%s: could not reset adapter\n", sc->sc_dev.dv_xname);
2167 goto fail1;
2168 }
2169
2170 switch (ic->ic_opmode) {
2171 case IEEE80211_M_STA:
2172 name = "iwi-bss";
2173 break;
2174 #ifndef IEEE80211_STA_ONLY
2175 case IEEE80211_M_IBSS:
2176 case IEEE80211_M_AHDEMO:
2177 name = "iwi-ibss";
2178 break;
2179 #endif
2180 case IEEE80211_M_MONITOR:
2181 name = "iwi-monitor";
2182 break;
2183 default:
2184 /* should not get there */
2185 error = EINVAL;
2186 goto fail1;
2187 }
2188
2189 if ((error = loadfirmware(name, &data, &size)) != 0) {
2190 printf("%s: error %d, could not read firmware %s\n",
2191 sc->sc_dev.dv_xname, error, name);
2192 goto fail1;
2193 }
2194 if (size < sizeof (struct iwi_firmware_hdr)) {
2195 printf("%s: firmware image too short: %zu bytes\n",
2196 sc->sc_dev.dv_xname, size);
2197 error = EINVAL;
2198 goto fail2;
2199 }
2200 hdr = (struct iwi_firmware_hdr *)data;
2201
2202 if (hdr->vermaj < 3 || hdr->bootsz == 0 || hdr->ucodesz == 0 ||
2203 hdr->mainsz == 0) {
2204 printf("%s: firmware image too old (need at least 3.0)\n",
2205 sc->sc_dev.dv_xname);
2206 error = EINVAL;
2207 goto fail2;
2208 }
2209
2210 if (size < sizeof (struct iwi_firmware_hdr) + letoh32(hdr->bootsz) +
2211 letoh32(hdr->ucodesz) + letoh32(hdr->mainsz)) {
2212 printf("%s: firmware image too short: %zu bytes\n",
2213 sc->sc_dev.dv_xname, size);
2214 error = EINVAL;
2215 goto fail2;
2216 }
2217
2218 fw = (const char *)data + sizeof (struct iwi_firmware_hdr);
2219 if ((error = iwi_load_firmware(sc, fw, letoh32(hdr->bootsz))) != 0) {
2220 printf("%s: could not load boot firmware\n",
2221 sc->sc_dev.dv_xname);
2222 goto fail2;
2223 }
2224
2225 fw = (const char *)data + sizeof (struct iwi_firmware_hdr) +
2226 letoh32(hdr->bootsz);
2227 if ((error = iwi_load_ucode(sc, fw, letoh32(hdr->ucodesz))) != 0) {
2228 printf("%s: could not load microcode\n", sc->sc_dev.dv_xname);
2229 goto fail2;
2230 }
2231
2232 iwi_stop_master(sc);
2233
2234 CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmdq.map->dm_segs[0].ds_addr);
2235 CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, IWI_CMD_RING_COUNT);
2236 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
2237
2238 for (ac = 0; ac < EDCA_NUM_AC; ac++) {
2239 CSR_WRITE_4(sc, IWI_CSR_TX_BASE(ac),
2240 sc->txq[ac].map->dm_segs[0].ds_addr);
2241 CSR_WRITE_4(sc, IWI_CSR_TX_SIZE(ac), IWI_TX_RING_COUNT);
2242 CSR_WRITE_4(sc, IWI_CSR_TX_WIDX(ac), sc->txq[ac].cur);
2243 }
2244
2245 for (i = 0; i < IWI_RX_RING_COUNT; i++) {
2246 struct iwi_rx_data *data = &sc->rxq.data[i];
2247 CSR_WRITE_4(sc, data->reg, data->map->dm_segs[0].ds_addr);
2248 }
2249
2250 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, IWI_RX_RING_COUNT - 1);
2251
2252 fw = (const char *)data + sizeof (struct iwi_firmware_hdr) +
2253 letoh32(hdr->bootsz) + letoh32(hdr->ucodesz);
2254 if ((error = iwi_load_firmware(sc, fw, letoh32(hdr->mainsz))) != 0) {
2255 printf("%s: could not load main firmware\n",
2256 sc->sc_dev.dv_xname);
2257 goto fail2;
2258 }
2259
2260 free(data, M_DEVBUF, size);
2261
2262 if ((error = iwi_config(sc)) != 0) {
2263 printf("%s: device configuration failed\n",
2264 sc->sc_dev.dv_xname);
2265 goto fail1;
2266 }
2267
2268 ifq_clr_oactive(&ifp->if_snd);
2269 ifp->if_flags |= IFF_RUNNING;
2270
2271 if (ic->ic_opmode != IEEE80211_M_MONITOR)
2272 ieee80211_begin_scan(ifp);
2273 else
2274 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2275
2276 return 0;
2277
2278 fail2: free(data, M_DEVBUF, size);
2279 fail1: iwi_stop(ifp, 0);
2280 return error;
2281 }
2282
2283 void
iwi_stop(struct ifnet * ifp,int disable)2284 iwi_stop(struct ifnet *ifp, int disable)
2285 {
2286 struct iwi_softc *sc = ifp->if_softc;
2287 struct ieee80211com *ic = &sc->sc_ic;
2288 int ac;
2289
2290 sc->sc_tx_timer = 0;
2291 ifp->if_timer = 0;
2292 ifp->if_flags &= ~IFF_RUNNING;
2293 ifq_clr_oactive(&ifp->if_snd);
2294
2295 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2296
2297 iwi_stop_master(sc);
2298
2299 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SW_RESET);
2300
2301 /* reset rings */
2302 iwi_reset_cmd_ring(sc, &sc->cmdq);
2303 for (ac = 0; ac < EDCA_NUM_AC; ac++)
2304 iwi_reset_tx_ring(sc, &sc->txq[ac]);
2305 iwi_reset_rx_ring(sc, &sc->rxq);
2306 }
2307
2308 struct cfdriver iwi_cd = {
2309 NULL, "iwi", DV_IFNET
2310 };
2311