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