if_iwn.c revision 1.2 1 /* $netbsd: if_iwn.c,v 1.0 2007/09/07 20:02:54 ober Exp $ */
2
3 /*-
4 * Copyright (c) 2007
5 * Damien Bergamini <damien.bergamini (at) free.fr>
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 #include <sys/cdefs.h>
21 __KERNEL_RCSID(0, "$NetBSD: if_iwn.c,v 1.2 2008/02/09 18:19:33 ober Exp $");
22
23
24 /*
25 * Driver for Intel Wireless WiFi Link 4965AGN 802.11 network adapters.
26 */
27
28 #include "bpfilter.h"
29
30 #include <sys/param.h>
31 #include <sys/sockio.h>
32 #include <sys/sysctl.h>
33 #include <sys/mbuf.h>
34 #include <sys/kernel.h>
35 #include <sys/socket.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/conf.h>
39 #include <sys/kauth.h>
40 #include <sys/callout.h>
41
42 #include <machine/bus.h>
43 #include <machine/endian.h>
44 #include <machine/intr.h>
45
46 #include <dev/pci/pcireg.h>
47 #include <dev/pci/pcivar.h>
48 #include <dev/pci/pcidevs.h>
49
50 #if NBPFILTER > 0
51 #include <net/bpf.h>
52 #endif
53 #include <net/if.h>
54 #include <net/if_arp.h>
55 #include <net/if_dl.h>
56 #include <net/if_media.h>
57 #include <net/if_types.h>
58
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/in_var.h>
62 #include <net/if_ether.h>
63 #include <netinet/ip.h>
64
65 #include <net80211/ieee80211_var.h>
66 #include <net80211/ieee80211_amrr.h>
67 #include <net80211/ieee80211_radiotap.h>
68
69 #include <dev/firmload.h>
70
71 #include <dev/pci/if_iwnreg.h>
72 #include <dev/pci/if_iwnvar.h>
73
74 #if 0
75 static const struct pci_matchid iwn_devices[] = {
76 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_PRO_WL_4965AGN_1 },
77 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_PRO_WL_4965AGN_2 }
78 };
79 #endif
80
81 /*
82 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
83 */
84 static const struct ieee80211_rateset iwn_rateset_11a =
85 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
86
87 static const struct ieee80211_rateset iwn_rateset_11b =
88 { 4, { 2, 4, 11, 22 } };
89
90 static const struct ieee80211_rateset iwn_rateset_11g =
91 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
92
93
94 #define EDCA_NUM_AC 4
95 static int iwn_match(device_t , struct cfdata *, void *);
96 static void iwn_attach(device_t , device_t, void *);
97 static int iwn_detach(device_t, int);
98
99 static void iwn_radiotap_attach(struct iwn_softc *);
100 static int iwn_dma_contig_alloc(bus_dma_tag_t, struct iwn_dma_info *,
101 void **, bus_size_t, bus_size_t, int);
102 static void iwn_dma_contig_free(struct iwn_dma_info *);
103 static int iwn_alloc_shared(struct iwn_softc *);
104 static void iwn_free_shared(struct iwn_softc *);
105 static int iwn_alloc_kw(struct iwn_softc *);
106 static void iwn_free_kw(struct iwn_softc *);
107 static int iwn_alloc_fwmem(struct iwn_softc *);
108 static void iwn_free_fwmem(struct iwn_softc *);
109 static struct iwn_rbuf *iwn_alloc_rbuf(struct iwn_softc *);
110 static void iwn_free_rbuf(struct mbuf *, void *, size_t, void *);
111 static int iwn_alloc_rpool(struct iwn_softc *);
112 static void iwn_free_rpool(struct iwn_softc *);
113 static int iwn_alloc_rx_ring(struct iwn_softc *, struct iwn_rx_ring *);
114 static void iwn_reset_rx_ring(struct iwn_softc *, struct iwn_rx_ring *);
115 static void iwn_free_rx_ring(struct iwn_softc *, struct iwn_rx_ring *);
116 static int iwn_alloc_tx_ring(struct iwn_softc *, struct iwn_tx_ring *,
117 int, int);
118 static void iwn_reset_tx_ring(struct iwn_softc *, struct iwn_tx_ring *);
119 static void iwn_free_tx_ring(struct iwn_softc *, struct iwn_tx_ring *);
120 static struct ieee80211_node *iwn_node_alloc(struct ieee80211_node_table *);
121 static void iwn_newassoc(struct ieee80211_node *, int);
122 static int iwn_media_change(struct ifnet *);
123 static int iwn_newstate(struct ieee80211com *, enum ieee80211_state, int);
124 static void iwn_mem_lock(struct iwn_softc *);
125 static void iwn_mem_unlock(struct iwn_softc *);
126 static uint32_t iwn_mem_read(struct iwn_softc *, uint32_t);
127 static void iwn_mem_write(struct iwn_softc *, uint32_t, uint32_t);
128 static void iwn_mem_write_region_4(struct iwn_softc *, uint32_t,
129 const uint32_t *, int);
130 static int iwn_eeprom_lock(struct iwn_softc *);
131 static void iwn_eeprom_unlock(struct iwn_softc *);
132 static int iwn_read_prom_data(struct iwn_softc *, uint32_t, void *, int);
133 static int iwn_load_microcode(struct iwn_softc *, const uint8_t *, int);
134 static int iwn_load_firmware(struct iwn_softc *);
135 static void iwn_calib_timeout(void *);
136 static void iwn_iter_func(void *, struct ieee80211_node *);
137 static void iwn_ampdu_rx_start(struct iwn_softc *, struct iwn_rx_desc *);
138 static void iwn_rx_intr(struct iwn_softc *, struct iwn_rx_desc *,
139 struct iwn_rx_data *);
140 static void iwn_rx_statistics(struct iwn_softc *, struct iwn_rx_desc *);
141 static void iwn_tx_intr(struct iwn_softc *, struct iwn_rx_desc *);
142 static void iwn_cmd_intr(struct iwn_softc *, struct iwn_rx_desc *);
143 static void iwn_notif_intr(struct iwn_softc *);
144 static int iwn_intr(void *);
145 static void iwn_read_eeprom(struct iwn_softc *);
146 static void iwn_read_eeprom_channels(struct iwn_softc *, int);
147 static void iwn_print_power_group(struct iwn_softc *, int);
148 static uint8_t iwn_plcp_signal(int);
149 static int iwn_tx_data(struct iwn_softc *, struct mbuf *,
150 struct ieee80211_node *, int);
151 static void iwn_start(struct ifnet *);
152 static void iwn_watchdog(struct ifnet *);
153 static int iwn_ioctl(struct ifnet *, u_long, void *);
154 static int iwn_cmd(struct iwn_softc *, int, const void *, int, int);
155 static int iwn_wme_update(struct ieee80211com *);
156 static int iwn_setup_node_mrr(struct iwn_softc *, uint8_t, int);
157 static void iwn_set_led(struct iwn_softc *, uint8_t, uint8_t, uint8_t);
158 static int iwn_set_critical_temp(struct iwn_softc *);
159 static void iwn_enable_tsf(struct iwn_softc *, struct ieee80211_node *);
160 static void iwn_power_calibration(struct iwn_softc *, int);
161 static int iwn_set_txpower(struct iwn_softc *,
162 struct ieee80211_channel *, int);
163 static int iwn_get_rssi(const struct iwn_rx_stat *);
164 static int iwn_get_noise(const struct iwn_rx_general_stats *);
165 static int iwn_get_temperature(struct iwn_softc *);
166 static int iwn_init_sensitivity(struct iwn_softc *);
167 static void iwn_compute_differential_gain(struct iwn_softc *,
168 const struct iwn_rx_general_stats *);
169 static void iwn_tune_sensitivity(struct iwn_softc *,
170 const struct iwn_rx_stats *);
171 static int iwn_send_sensitivity(struct iwn_softc *);
172 /*static int iwn_setup_beacon(struct iwn_softc *, struct ieee80211_node *);*/
173 static int iwn_auth(struct iwn_softc *);
174 static int iwn_run(struct iwn_softc *);
175 static int iwn_scan(struct iwn_softc *, uint16_t);
176 static int iwn_config(struct iwn_softc *);
177 static void iwn_post_alive(struct iwn_softc *);
178 static void iwn_stop_master(struct iwn_softc *);
179 static int iwn_reset(struct iwn_softc *);
180 static void iwn_hw_config(struct iwn_softc *);
181 static int iwn_init(struct ifnet *);
182 static void iwn_stop(struct ifnet *, int);
183 static void iwn_fix_channel(struct ieee80211com *, struct mbuf *);
184 static bool iwn_resume(device_t dv);
185
186
187
188 #define IWN_DEBUG
189
190 #ifdef IWN_DEBUG
191 #define DPRINTF(x) do { if (iwn_debug > 0) printf x; } while (0)
192 #define DPRINTFN(n, x) do { if (iwn_debug >= (n)) printf x; } while (0)
193 int iwn_debug = 2;
194 #else
195 #define DPRINTF(x)
196 #define DPRINTFN(n, x)
197 #endif
198
199 CFATTACH_DECL_NEW(iwn, sizeof(struct iwn_softc), iwn_match, iwn_attach,
200 iwn_detach, NULL);
201
202 static int
203 iwn_match(device_t parent, struct cfdata *match __unused, void *aux)
204 {
205 struct pci_attach_args *pa = aux;
206
207 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
208 return 0;
209
210 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_4965AGN_1 ||
211 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_4965AGN_2)
212 return 1;
213
214 return 0;
215 }
216
217 /* Base Address Register */
218 #define IWN_PCI_BAR0 0x10
219
220 static void
221 iwn_attach(device_t parent __unused, device_t self, void *aux)
222 {
223 struct iwn_softc *sc = device_private(self);
224 struct ieee80211com *ic = &sc->sc_ic;
225 struct ifnet *ifp = &sc->sc_ec.ec_if;
226 struct pci_attach_args *pa = aux;
227 const char *intrstr;
228 char devinfo[256];
229 pci_intr_handle_t ih;
230 pcireg_t memtype, data;
231 int i, error, revision;
232
233 sc->sc_dev = self;
234 sc->sc_pct = pa->pa_pc;
235 sc->sc_pcitag = pa->pa_tag;
236
237 callout_init(&sc->calib_to, 0);
238 callout_setfunc(&sc->calib_to, iwn_calib_timeout, sc);
239
240 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof devinfo);
241 revision = PCI_REVISION(pa->pa_class);
242 aprint_normal(": %s (rev. 0x%2x)\n", devinfo, revision);
243
244
245 /* clear device specific PCI configuration register 0x41 */
246 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
247 data &= ~0x0000ff00;
248 pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data);
249
250 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
251 data |= PCI_COMMAND_MASTER_ENABLE;
252 pci_conf_write(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, data);
253
254 /* enable bus-mastering */
255 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
256 data |= PCI_COMMAND_MASTER_ENABLE;
257 pci_conf_write(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, data);
258
259 /* map the register window */
260 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, IWN_PCI_BAR0);
261 error = pci_mapreg_map(pa, IWN_PCI_BAR0, memtype, 0, &sc->sc_st,
262 &sc->sc_sh, NULL, &sc->sc_sz);
263 if (error != 0) {
264 aprint_error_dev(self, "could not map memory space\n");
265 return;
266 }
267
268 sc->sc_dmat = pa->pa_dmat;
269
270 if (pci_intr_map(pa, &ih) != 0) {
271 aprint_error_dev(self, "could not map interrupt\n");
272 return;
273 }
274
275 intrstr = pci_intr_string(sc->sc_pct, ih);
276 sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, iwn_intr, sc);
277
278 if (sc->sc_ih == NULL) {
279 aprint_error_dev(self, "could not establish interrupt");
280 if (intrstr != NULL)
281 aprint_error(" at %s", intrstr);
282 aprint_error("\n");
283 return;
284 }
285 aprint_normal_dev(self, "interrupting at %s\n", intrstr);
286
287
288 if (iwn_reset(sc) != 0) {
289 aprint_error_dev(self, "could not reset adapter\n");
290 return;
291 }
292
293 /*
294 * Allocate DMA memory for firmware transfers.
295 */
296 if ((error = iwn_alloc_fwmem(sc)) != 0) {
297 aprint_error_dev(self, "could not allocate firmware memory\n");
298 return;
299 }
300
301 /*
302 * Allocate a "keep warm" page.
303 */
304 if ((error = iwn_alloc_kw(sc)) != 0) {
305 aprint_error_dev(self, "could not allocate keep warm page\n");
306 goto fail1;
307 }
308
309 /*
310 * Allocate shared area (communication area).
311 */
312 if ((error = iwn_alloc_shared(sc)) != 0) {
313 aprint_error_dev(self, "could not allocate shared area\n");
314 goto fail2;
315 }
316
317 /*
318 * Allocate Rx buffers and Tx/Rx rings.
319 */
320 if ((error = iwn_alloc_rpool(sc)) != 0) {
321 aprint_error_dev(self, "could not allocate Rx buffers\n");
322 goto fail3;
323 }
324
325 for (i = 0; i < IWN_NTXQUEUES; i++) {
326 struct iwn_tx_ring *txq = &sc->txq[i];
327 error = iwn_alloc_tx_ring(sc, txq, IWN_TX_RING_COUNT, i);
328 if (error != 0) {
329 aprint_error_dev(self, "could not allocate Tx ring %d\n", i);
330 goto fail4;
331 }
332 }
333
334 if (iwn_alloc_rx_ring(sc, &sc->rxq) != 0) {
335 aprint_error_dev(self, "could not allocate Rx ring\n");
336 goto fail4;
337 }
338
339
340 ic->ic_ifp = ifp;
341 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
342 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
343 ic->ic_state = IEEE80211_S_INIT;
344
345 /* set device capabilities */
346 ic->ic_caps =
347 IEEE80211_C_IBSS | /* IBSS mode support */
348 IEEE80211_C_WPA | /* 802.11i */
349 IEEE80211_C_MONITOR | /* monitor mode supported */
350 IEEE80211_C_TXPMGT | /* tx power management */
351 IEEE80211_C_SHSLOT | /* short slot time supported */
352 IEEE80211_C_SHPREAMBLE| /* short preamble supported */
353 IEEE80211_C_WME; /* 802.11e */
354
355 /* read supported channels and MAC address from EEPROM */
356 iwn_read_eeprom(sc);
357
358 /* set supported .11a, .11b and .11g rates */
359 ic->ic_sup_rates[IEEE80211_MODE_11A] = iwn_rateset_11a;
360 ic->ic_sup_rates[IEEE80211_MODE_11B] = iwn_rateset_11b;
361 ic->ic_sup_rates[IEEE80211_MODE_11G] = iwn_rateset_11g;
362
363 /* IBSS channel undefined for now */
364 ic->ic_ibss_chan = &ic->ic_channels[0];
365
366 ifp->if_softc = sc;
367 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
368 ifp->if_init = iwn_init;
369 ifp->if_stop = iwn_stop;
370 ifp->if_ioctl = iwn_ioctl;
371 ifp->if_start = iwn_start;
372 ifp->if_watchdog = iwn_watchdog;
373 IFQ_SET_READY(&ifp->if_snd);
374 memcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
375
376 if_attach(ifp);
377 ieee80211_ifattach(ic);
378 ic->ic_node_alloc = iwn_node_alloc;
379 ic->ic_newassoc = iwn_newassoc;
380 ic->ic_wme.wme_update = iwn_wme_update;
381
382 /* override state transition machine */
383 sc->sc_newstate = ic->ic_newstate;
384 ic->ic_newstate = iwn_newstate;
385 ieee80211_media_init(ic, iwn_media_change, ieee80211_media_status);
386
387 sc->amrr.amrr_min_success_threshold = 1;
388 sc->amrr.amrr_max_success_threshold = 15;
389
390 if (!pmf_device_register(self, NULL, iwn_resume))
391 aprint_error_dev(self, "couldn't establish power handler\n");
392 else
393 pmf_class_network_register(self, ifp);
394
395 iwn_radiotap_attach(sc);
396
397 ieee80211_announce(ic);
398
399 return;
400
401 /* free allocated memory if something failed during attachment */
402 fail4: while (--i >= 0)
403 iwn_free_tx_ring(sc, &sc->txq[i]);
404 iwn_free_rpool(sc);
405 fail3: iwn_free_shared(sc);
406 fail2: iwn_free_kw(sc);
407 fail1: iwn_free_fwmem(sc);
408 }
409
410 static int
411 iwn_detach(struct device* self, int flags __unused)
412 {
413 struct iwn_softc *sc = (struct iwn_softc *)self;
414 struct ifnet *ifp = sc->sc_ic.ic_ifp;
415 int ac;
416
417 iwn_stop(ifp, 1);
418
419 #if NBPFILTER > 0
420 if (ifp != NULL)
421 bpfdetach(ifp);
422 #endif
423 ieee80211_ifdetach(&sc->sc_ic);
424 if (ifp != NULL)
425 if_detach(ifp);
426
427 for (ac = 0; ac < IWN_NTXQUEUES; ac++)
428 iwn_free_tx_ring(sc, &sc->txq[ac]);
429 iwn_free_rx_ring(sc, &sc->rxq);
430 iwn_free_rpool(sc);
431 iwn_free_shared(sc);
432
433 if (sc->sc_ih != NULL) {
434 pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
435 sc->sc_ih = NULL;
436 }
437
438 bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
439
440 return 0;
441 }
442
443 /*
444 * Attach the interface to 802.11 radiotap.
445 */
446 static void
447 iwn_radiotap_attach(struct iwn_softc *sc)
448 {
449 struct ifnet *ifp = sc->sc_ic.ic_ifp;
450
451 #if NBPFILTER > 0
452 bpfattach2(ifp, DLT_IEEE802_11_RADIO,
453 sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
454 &sc->sc_drvbpf);
455
456 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
457 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
458 sc->sc_rxtap.wr_ihdr.it_present = htole32(IWN_RX_RADIOTAP_PRESENT);
459
460 sc->sc_txtap_len = sizeof sc->sc_txtapu;
461 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
462 sc->sc_txtap.wt_ihdr.it_present = htole32(IWN_TX_RADIOTAP_PRESENT);
463 #endif
464 }
465
466
467 /*
468 * Build a beacon frame that the firmware will broadcast periodically in
469 * IBSS or HostAP modes.
470 */
471 #if 0
472 static int
473 iwn_setup_beacon(struct iwn_softc *sc, struct ieee80211_node *ni)
474 {
475 struct ieee80211com *ic = &sc->sc_ic;
476 struct iwn_tx_ring *ring = &sc->txq[4];
477 struct iwn_tx_desc *desc;
478 struct iwn_tx_data *data;
479 struct iwn_tx_cmd *cmd;
480 struct iwn_cmd_beacon *bcn;
481 struct ieee80211_beacon_offsets bo;
482 struct mbuf *m0;
483 bus_addr_t paddr;
484 int error;
485
486 desc = &ring->desc[ring->cur];
487 data = &ring->data[ring->cur];
488
489 m0 = ieee80211_beacon_alloc(ic, ni, &bo);
490 if (m0 == NULL) {
491 aprint_error_dev(sc->sc_dev, "could not allocate beacon frame\n");
492 return ENOMEM;
493 }
494
495 cmd = &ring->cmd[ring->cur];
496 cmd->code = IWN_CMD_SET_BEACON;
497 cmd->flags = 0;
498 cmd->qid = ring->qid;
499 cmd->idx = ring->cur;
500
501 bcn = (struct iwn_cmd_beacon *)cmd->data;
502 memset(bcn, 0, sizeof (struct iwn_cmd_beacon));
503 bcn->id = IWN_ID_BROADCAST;
504 bcn->ofdm_mask = 0xff;
505 bcn->cck_mask = 0x0f;
506 bcn->lifetime = htole32(IWN_LIFETIME_INFINITE);
507 bcn->len = htole16(m0->m_pkthdr.len);
508 bcn->rate = (ic->ic_curmode == IEEE80211_MODE_11A) ?
509 iwn_plcp_signal(12) : iwn_plcp_signal(2);
510 bcn->flags = htole32(IWN_TX_AUTO_SEQ | IWN_TX_INSERT_TSTAMP);
511
512 /* save and trim IEEE802.11 header */
513 m_copydata(m0, 0, sizeof (struct ieee80211_frame), (void *)&bcn->wh);
514 m_adj(m0, sizeof (struct ieee80211_frame));
515
516 /* assume beacon frame is contiguous */
517 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
518 BUS_DMA_READ | BUS_DMA_NOWAIT);
519 if (error) {
520 aprint_error_dev(sc->sc_dev, "could not map beacon\n");
521 m_freem(m0);
522 return error;
523 }
524
525 data->m = m0;
526
527 /* first scatter/gather segment is used by the beacon command */
528 paddr = ring->cmd_dma.paddr + ring->cur * sizeof (struct iwn_tx_cmd);
529
530 IWN_SET_DESC_NSEGS(desc, 2);
531 IWN_SET_DESC_SEG(desc, 0, paddr , 4 + sizeof(struct iwn_cmd_beacon));
532 IWN_SET_DESC_SEG(desc, 1, data->map->dm_segs[0].ds_addr,
533 data->map->dm_segs[1].ds_len);
534
535
536 /* kick cmd ring */
537 ring->cur = (ring->cur + 1) % IWN_TX_RING_COUNT;
538 IWN_WRITE(sc, IWN_TX_WIDX, ring->qid << 8 | ring->cur);
539
540 return 0;
541 }
542 #endif
543
544 static int
545 iwn_dma_contig_alloc(bus_dma_tag_t tag, struct iwn_dma_info *dma, void **kvap,
546 bus_size_t size, bus_size_t alignment, int flags)
547 {
548 int nsegs, error;
549
550 dma->tag = tag;
551 dma->size = size;
552
553 error = bus_dmamap_create(tag, size, 1, size, 0, flags, &dma->map);
554 if (error != 0)
555 goto fail;
556
557 error = bus_dmamem_alloc(tag, size, alignment, 0, &dma->seg, 1, &nsegs,
558 flags);
559 if (error != 0)
560 goto fail;
561
562 error = bus_dmamem_map(tag, &dma->seg, 1, size, &dma->vaddr, flags);
563 if (error != 0)
564 goto fail;
565
566 error = bus_dmamap_load(tag, dma->map, dma->vaddr, size, NULL, flags);
567 if (error != 0)
568 goto fail;
569
570 memset(dma->vaddr, 0, size);
571
572 dma->paddr = dma->map->dm_segs[0].ds_addr;
573 if (kvap != NULL)
574 *kvap = dma->vaddr;
575
576 return 0;
577
578 fail: iwn_dma_contig_free(dma);
579 return error;
580 }
581
582 static void
583 iwn_dma_contig_free(struct iwn_dma_info *dma)
584 {
585 if (dma->map != NULL) {
586 if (dma->vaddr != NULL) {
587 bus_dmamap_unload(dma->tag, dma->map);
588 bus_dmamem_unmap(dma->tag, dma->vaddr, dma->size);
589 bus_dmamem_free(dma->tag, &dma->seg, 1);
590 dma->vaddr = NULL;
591 }
592 bus_dmamap_destroy(dma->tag, dma->map);
593 dma->map = NULL;
594 }
595 }
596
597 static int
598 iwn_alloc_shared(struct iwn_softc *sc)
599 {
600 int error;
601 /* must be aligned on a 1KB boundary */
602 error = iwn_dma_contig_alloc(sc->sc_dmat, &sc->shared_dma,
603 (void **)&sc->shared, sizeof (struct iwn_shared),
604 1024,BUS_DMA_NOWAIT);
605 if (error != 0)
606 aprint_error_dev(sc->sc_dev,
607 "could not allocate shared area DMA memory\n");
608
609 return error;
610
611 }
612
613 static void
614 iwn_free_shared(struct iwn_softc *sc)
615 {
616 iwn_dma_contig_free(&sc->shared_dma);
617 }
618
619 static int
620 iwn_alloc_kw(struct iwn_softc *sc)
621 {
622 /* must be aligned on a 16-byte boundary */
623 return iwn_dma_contig_alloc(sc->sc_dmat, &sc->kw_dma, NULL,
624 PAGE_SIZE, PAGE_SIZE, BUS_DMA_NOWAIT);
625 }
626
627 static void
628 iwn_free_kw(struct iwn_softc *sc)
629 {
630 iwn_dma_contig_free(&sc->kw_dma);
631 }
632
633 static int
634 iwn_alloc_fwmem(struct iwn_softc *sc)
635 {
636 int error;
637 /* allocate enough contiguous space to store text and data */
638 error = iwn_dma_contig_alloc(sc->sc_dmat, &sc->fw_dma, NULL,
639 IWN_FW_MAIN_TEXT_MAXSZ + IWN_FW_MAIN_DATA_MAXSZ, 16,
640 BUS_DMA_NOWAIT);
641
642 if (error != 0){
643 aprint_error_dev(sc->sc_dev,
644 "could not allocate firmware transfer area DMA memory\n" );
645
646 }
647 return error;
648 }
649
650 static void
651 iwn_free_fwmem(struct iwn_softc *sc)
652 {
653 iwn_dma_contig_free(&sc->fw_dma);
654 }
655
656 static struct iwn_rbuf *
657 iwn_alloc_rbuf(struct iwn_softc *sc)
658 {
659 struct iwn_rbuf *rbuf;
660
661 rbuf = SLIST_FIRST(&sc->rxq.freelist);
662 if (rbuf == NULL)
663 return NULL;
664 SLIST_REMOVE_HEAD(&sc->rxq.freelist, next);
665 sc->rxq.nb_free_entries --;
666 return rbuf;
667 }
668
669 /*
670 * This is called automatically by the network stack when the mbuf to which
671 * our Rx buffer is attached is freed.
672 */
673 static void
674 iwn_free_rbuf(struct mbuf* m, void *buf, size_t size, void *arg)
675 {
676 struct iwn_rbuf *rbuf = arg;
677 struct iwn_softc *sc = rbuf->sc;
678
679 /* put the buffer back in the free list */
680 SLIST_INSERT_HEAD(&sc->rxq.freelist, rbuf, next);
681
682 sc->rxq.nb_free_entries ++;
683
684 if (__predict_true(m != NULL))
685 pool_cache_put(mb_cache, m);
686 }
687
688
689 static int
690 iwn_alloc_rpool(struct iwn_softc *sc)
691 {
692 struct iwn_rx_ring *ring = &sc->rxq;
693 struct iwn_rbuf *rbuf;
694 int i, error;
695
696 /* allocate a big chunk of DMA'able memory.. */
697 error = iwn_dma_contig_alloc(sc->sc_dmat, &ring->buf_dma, NULL,
698 IWN_RBUF_COUNT * IWN_RBUF_SIZE, IWN_BUF_ALIGN, BUS_DMA_NOWAIT);
699 if (error != 0) {
700 aprint_error_dev(sc->sc_dev, "could not allocate Rx buffers DMA memory\n");
701 return error;
702 }
703
704 /* ..and split it into chunks of "rbufsz" bytes */
705 SLIST_INIT(&ring->freelist);
706 for (i = 0; i < IWN_RBUF_COUNT; i++) {
707 rbuf = &ring->rbuf[i];
708
709 rbuf->sc = sc; /* backpointer for callbacks */
710 rbuf->vaddr = (char *)ring->buf_dma.vaddr + i * IWN_RBUF_SIZE;
711 rbuf->paddr = ring->buf_dma.paddr + i * IWN_RBUF_SIZE;
712
713 SLIST_INSERT_HEAD(&ring->freelist, rbuf, next);
714 }
715 ring->nb_free_entries = IWN_RBUF_COUNT;
716 return 0;
717 }
718
719 static void
720 iwn_free_rpool(struct iwn_softc *sc)
721 {
722 iwn_dma_contig_free(&sc->rxq.buf_dma);
723 }
724
725 static int
726 iwn_alloc_rx_ring(struct iwn_softc *sc, struct iwn_rx_ring *ring)
727 {
728 struct iwn_rx_data *data;
729 struct iwn_rbuf *rbuf;
730 int i, error;
731
732 ring->cur = 0;
733
734 error = iwn_dma_contig_alloc(sc->sc_dmat, &ring->desc_dma,
735 (void **)&ring->desc, IWN_RX_RING_COUNT * sizeof (struct iwn_rx_desc),
736 IWN_RING_DMA_ALIGN, BUS_DMA_NOWAIT);
737 if (error != 0) {
738 aprint_error_dev(sc->sc_dev, "could not allocate rx ring DMA memory\n");
739 goto fail;
740 }
741
742 /*
743 * Setup Rx buffers.
744 */
745 for (i = 0; i < IWN_RX_RING_COUNT; i++) {
746 data = &ring->data[i];
747
748 MGETHDR(data->m, M_DONTWAIT, MT_DATA);
749 if (data->m == NULL) {
750 aprint_error_dev(sc->sc_dev, "could not allocate rx mbuf\n");
751 error = ENOMEM;
752 goto fail;
753 }
754 if ((rbuf = iwn_alloc_rbuf(sc)) == NULL) {
755 m_freem(data->m);
756 data->m = NULL;
757 aprint_error_dev(sc->sc_dev, "could not allocate rx buffer\n");
758 error = ENOMEM;
759 goto fail;
760 }
761 /* attach Rx buffer to mbuf */
762 MEXTADD(data->m, rbuf->vaddr, IWN_RBUF_SIZE, 0, iwn_free_rbuf,
763 rbuf);
764
765 data->m->m_flags |= M_EXT_RW;
766 /* Rx buffers are aligned on a 256-byte boundary */
767 ring->desc[i] = htole32(rbuf->paddr >> 8);
768 }
769
770 return 0;
771
772 fail: iwn_free_rx_ring(sc, ring);
773 return error;
774 }
775
776 static void
777 iwn_reset_rx_ring(struct iwn_softc *sc, struct iwn_rx_ring *ring)
778 {
779 int ntries;
780
781 iwn_mem_lock(sc);
782
783 IWN_WRITE(sc, IWN_RX_CONFIG, 0);
784 for (ntries = 0; ntries < 100; ntries++) {
785 if (IWN_READ(sc, IWN_RX_STATUS) & IWN_RX_IDLE)
786 break;
787 DELAY(10);
788 }
789 #ifdef IWN_DEBUG
790 if (ntries == 100 && iwn_debug > 0)
791 aprint_error_dev(sc->sc_dev, "timeout resetting Rx ring\n");
792 #endif
793 iwn_mem_unlock(sc);
794
795 ring->cur = 0;
796 }
797
798 static void
799 iwn_free_rx_ring(struct iwn_softc *sc, struct iwn_rx_ring *ring)
800 {
801 int i;
802
803 iwn_dma_contig_free(&ring->desc_dma);
804
805 for (i = 0; i < IWN_RX_RING_COUNT; i++) {
806 if (ring->data[i].m != NULL)
807 m_freem(ring->data[i].m);
808 }
809 }
810
811 static int
812 iwn_alloc_tx_ring(struct iwn_softc *sc, struct iwn_tx_ring *ring, int count,
813 int qid)
814 {
815 struct iwn_tx_data *data;
816 int i, error;
817
818 ring->qid = qid;
819 ring->count = count;
820 ring->queued = 0;
821 ring->cur = 0;
822
823 error = iwn_dma_contig_alloc(sc->sc_dmat, &ring->desc_dma,
824 (void **)&ring->desc, count * sizeof (struct iwn_tx_desc),
825 IWN_RING_DMA_ALIGN, BUS_DMA_NOWAIT);
826 if (error != 0) {
827 aprint_error_dev(sc->sc_dev, "could not allocate tx ring DMA memory\n");
828 goto fail;
829 }
830
831 error = iwn_dma_contig_alloc(sc->sc_dmat, &ring->cmd_dma,
832 (void **)&ring->cmd, count * sizeof (struct iwn_tx_cmd), 4,
833 BUS_DMA_NOWAIT);
834 if (error != 0) {
835 aprint_error_dev(sc->sc_dev, "could not allocate tx cmd DMA memory\n");
836 goto fail;
837 }
838
839 ring->data = malloc(count * sizeof (struct iwn_tx_data), M_DEVBUF, M_NOWAIT);
840
841 if (ring->data == NULL) {
842 aprint_error_dev(sc->sc_dev,"could not allocate tx data slots\n");
843 goto fail;
844 }
845
846 memset(ring->data, 0, count * sizeof (struct iwn_tx_data));
847
848 for (i = 0; i < count; i++) {
849 data = &ring->data[i];
850
851 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
852 IWN_MAX_SCATTER - 1, MCLBYTES, 0, BUS_DMA_NOWAIT,
853 &data->map);
854 if (error != 0) {
855 aprint_error_dev(sc->sc_dev, "could not create tx buf DMA map\n");
856 goto fail;
857 }
858 }
859
860 return 0;
861
862 fail: iwn_free_tx_ring(sc, ring);
863 return error;
864 }
865
866 static void
867 iwn_reset_tx_ring(struct iwn_softc *sc, struct iwn_tx_ring *ring)
868 {
869 struct iwn_tx_data *data;
870 uint32_t tmp;
871 int i, ntries;
872
873 iwn_mem_lock(sc);
874
875 IWN_WRITE(sc, IWN_TX_CONFIG(ring->qid), 0);
876 for (ntries = 0; ntries < 100; ntries++) {
877 tmp = IWN_READ(sc, IWN_TX_STATUS);
878 if ((tmp & IWN_TX_IDLE(ring->qid)) == IWN_TX_IDLE(ring->qid))
879 break;
880 DELAY(10);
881 }
882 #ifdef IWN_DEBUG
883 if (ntries == 100 && iwn_debug > 1) {
884 aprint_error_dev(sc->sc_dev, "timeout resetting Tx ring %d\n", ring->qid);
885
886 }
887 #endif
888 iwn_mem_unlock(sc);
889
890 for (i = 0; i < ring->count; i++) {
891 data = &ring->data[i];
892
893 if (data->m != NULL) {
894 bus_dmamap_unload(sc->sc_dmat, data->map);
895 m_freem(data->m);
896 data->m = NULL;
897 }
898 }
899
900 ring->queued = 0;
901 ring->cur = 0;
902 }
903
904 static void
905 iwn_free_tx_ring(struct iwn_softc *sc, struct iwn_tx_ring *ring)
906 {
907 struct iwn_tx_data *data;
908 int i;
909
910
911 iwn_dma_contig_free(&ring->desc_dma);
912 iwn_dma_contig_free(&ring->cmd_dma);
913
914 if (ring->data != NULL) {
915 for (i = 0; i < ring->count; i++) {
916 data = &ring->data[i];
917
918 if (data->m != NULL) {
919 bus_dmamap_unload(sc->sc_dmat, data->map);
920 m_freem(data->m);
921 }
922 }
923 free(ring->data, M_DEVBUF);
924 }
925 }
926
927 /*ARGUSED*/
928 struct ieee80211_node *
929 iwn_node_alloc(struct ieee80211_node_table *nt __unused)
930 {
931 struct iwn_node *wn;
932
933 wn = malloc(sizeof (struct iwn_node), M_DEVBUF, M_NOWAIT);
934
935 if (wn != NULL)
936 memset(wn, 0, sizeof (struct iwn_node));
937 return (struct ieee80211_node *)wn;
938
939 }
940
941 static void
942 iwn_newassoc(struct ieee80211_node *ni, int isnew)
943 {
944 struct iwn_softc *sc = ni->ni_ic->ic_ifp->if_softc;
945 int i;
946
947 ieee80211_amrr_node_init(&sc->amrr, &((struct iwn_node *)ni)->amn);
948
949 /* set rate to some reasonable initial value */
950 for (i = ni->ni_rates.rs_nrates - 1;
951 i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
952 i--);
953 ni->ni_txrate = i;
954 }
955
956 static int
957 iwn_media_change(struct ifnet *ifp)
958 {
959 int error;
960
961 error = ieee80211_media_change(ifp);
962 if (error != ENETRESET)
963 return error;
964
965 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
966 iwn_init(ifp);
967
968 return 0;
969 }
970
971 static int
972 iwn_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
973 {
974 struct ifnet *ifp = ic->ic_ifp;
975 struct iwn_softc *sc = ifp->if_softc;
976 int error;
977
978 callout_stop(&sc->calib_to);
979
980 switch (nstate) {
981
982 case IEEE80211_S_SCAN:
983
984 if (sc->is_scanning)
985 break;
986
987 sc->is_scanning = true;
988 ieee80211_node_table_reset(&ic->ic_scan);
989 ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN;
990
991 /* make the link LED blink while we're scanning */
992 iwn_set_led(sc, IWN_LED_LINK, 20, 2);
993
994 if ((error = iwn_scan(sc, IEEE80211_CHAN_G)) != 0) {
995 aprint_error_dev(sc->sc_dev, "could not initiate scan\n");
996 ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
997 return error;
998 }
999 ic->ic_state = nstate;
1000 return 0;
1001
1002 case IEEE80211_S_ASSOC:
1003 if (ic->ic_state != IEEE80211_S_RUN)
1004 break;
1005 /* FALLTHROUGH */
1006 case IEEE80211_S_AUTH:
1007 /* reset state to handle reassociations correctly */
1008 sc->config.associd = 0;
1009 sc->config.filter &= ~htole32(IWN_FILTER_BSS);
1010 /*sc->calib.state = IWN_CALIB_STATE_INIT;*/
1011
1012 if ((error = iwn_auth(sc)) != 0) {
1013 aprint_error_dev(sc->sc_dev, "could not move to auth state\n");
1014 return error;
1015 }
1016 break;
1017
1018 case IEEE80211_S_RUN:
1019 if ((error = iwn_run(sc)) != 0) {
1020 aprint_error_dev(sc->sc_dev, "could not move to run state\n");
1021 return error;
1022 }
1023
1024 #if 0
1025 /* JAF - code has changed here. need to verify iwn_run handles this properly XXX added to iwn_run */
1026 if (ic->ic_opmode != IEEE80211_M_STA) {
1027 (void) iwn_auth(sc); /* XXX */
1028 iwn_setup_beacon(sc, ni);
1029 }
1030 #endif
1031
1032
1033 break;
1034
1035 case IEEE80211_S_INIT:
1036 sc->is_scanning = false;
1037 break;
1038 }
1039
1040 return sc->sc_newstate(ic, nstate, arg);
1041 }
1042
1043 /*
1044 * Grab exclusive access to NIC memory.
1045 */
1046 static void
1047 iwn_mem_lock(struct iwn_softc *sc)
1048 {
1049 uint32_t tmp;
1050 int ntries;
1051
1052 tmp = IWN_READ(sc, IWN_GPIO_CTL);
1053 IWN_WRITE(sc, IWN_GPIO_CTL, tmp | IWN_GPIO_MAC);
1054
1055 /* spin until we actually get the lock */
1056 for (ntries = 0; ntries < 1000; ntries++) {
1057 if ((IWN_READ(sc, IWN_GPIO_CTL) &
1058 (IWN_GPIO_CLOCK | IWN_GPIO_SLEEP)) == IWN_GPIO_CLOCK)
1059 break;
1060 DELAY(10);
1061 }
1062 if (ntries == 1000)
1063 aprint_error_dev(sc->sc_dev, "could not lock memory\n");
1064 }
1065
1066 /*
1067 * Release lock on NIC memory.
1068 */
1069 static void
1070 iwn_mem_unlock(struct iwn_softc *sc)
1071 {
1072 uint32_t tmp = IWN_READ(sc, IWN_GPIO_CTL);
1073 IWN_WRITE(sc, IWN_GPIO_CTL, tmp & ~IWN_GPIO_MAC);
1074 }
1075
1076 static uint32_t
1077 iwn_mem_read(struct iwn_softc *sc, uint32_t addr)
1078 {
1079 IWN_WRITE(sc, IWN_READ_MEM_ADDR, IWN_MEM_4 | addr);
1080 return IWN_READ(sc, IWN_READ_MEM_DATA);
1081 }
1082
1083 static void
1084 iwn_mem_write(struct iwn_softc *sc, uint32_t addr, uint32_t data)
1085 {
1086 IWN_WRITE(sc, IWN_WRITE_MEM_ADDR, IWN_MEM_4 | addr);
1087 IWN_WRITE(sc, IWN_WRITE_MEM_DATA, data);
1088 }
1089
1090 static void
1091 iwn_mem_write_region_4(struct iwn_softc *sc, uint32_t addr,
1092 const uint32_t *data, int wlen)
1093 {
1094 for (; wlen > 0; wlen--, data++, addr += 4)
1095 iwn_mem_write(sc, addr, *data);
1096 }
1097
1098 static int
1099 iwn_eeprom_lock(struct iwn_softc *sc)
1100 {
1101 uint32_t tmp;
1102 int ntries;
1103
1104 tmp = IWN_READ(sc, IWN_HWCONFIG);
1105 IWN_WRITE(sc, IWN_HWCONFIG, tmp | IWN_HW_EEPROM_LOCKED);
1106
1107 /* spin until we actually get the lock */
1108 for (ntries = 0; ntries < 100; ntries++) {
1109 if (IWN_READ(sc, IWN_HWCONFIG) & IWN_HW_EEPROM_LOCKED)
1110 return 0;
1111 DELAY(10);
1112 }
1113 return ETIMEDOUT;
1114 }
1115
1116 static void
1117 iwn_eeprom_unlock(struct iwn_softc *sc)
1118 {
1119 uint32_t tmp = IWN_READ(sc, IWN_HWCONFIG);
1120 IWN_WRITE(sc, IWN_HWCONFIG, tmp & ~IWN_HW_EEPROM_LOCKED);
1121 }
1122
1123 /*
1124 * Read `len' bytes from the EEPROM. We access the EEPROM through the MAC
1125 * instead of using the traditional bit-bang method.
1126 */
1127 static int
1128 iwn_read_prom_data(struct iwn_softc *sc, uint32_t addr, void *data, int len)
1129 {
1130 uint8_t *out = data;
1131 uint32_t val;
1132 int ntries;
1133
1134 iwn_mem_lock(sc);
1135 for (; len > 0; len -= 2, addr++) {
1136 IWN_WRITE(sc, IWN_EEPROM_CTL, addr << 2);
1137 IWN_WRITE(sc, IWN_EEPROM_CTL,
1138 IWN_READ(sc, IWN_EEPROM_CTL) & ~IWN_EEPROM_CMD);
1139
1140 for (ntries = 0; ntries < 10; ntries++) {
1141 if ((val = IWN_READ(sc, IWN_EEPROM_CTL)) &
1142 IWN_EEPROM_READY)
1143 break;
1144 DELAY(5);
1145 }
1146 if (ntries == 10) {
1147 aprint_error_dev(sc->sc_dev, "could not read EEPROM\n");
1148 return ETIMEDOUT;
1149 }
1150 *out++ = val >> 16;
1151 if (len > 1)
1152 *out++ = val >> 24;
1153 }
1154 iwn_mem_unlock(sc);
1155
1156 return 0;
1157 }
1158
1159 /*
1160 * The firmware boot code is small and is intended to be copied directly into
1161 * the NIC internal memory.
1162 */
1163 static int
1164 iwn_load_microcode(struct iwn_softc *sc, const uint8_t *ucode, int size)
1165 {
1166 int ntries;
1167
1168 size /= sizeof (uint32_t);
1169
1170 iwn_mem_lock(sc);
1171
1172 /* copy microcode image into NIC memory */
1173 iwn_mem_write_region_4(sc, IWN_MEM_UCODE_BASE,
1174 (const uint32_t *)ucode, size);
1175
1176 iwn_mem_write(sc, IWN_MEM_UCODE_SRC, 0);
1177 iwn_mem_write(sc, IWN_MEM_UCODE_DST, IWN_FW_TEXT);
1178 iwn_mem_write(sc, IWN_MEM_UCODE_SIZE, size);
1179
1180 /* run microcode */
1181 iwn_mem_write(sc, IWN_MEM_UCODE_CTL, IWN_UC_RUN);
1182
1183 /* wait for transfer to complete */
1184 for (ntries = 0; ntries < 1000; ntries++) {
1185 if (!(iwn_mem_read(sc, IWN_MEM_UCODE_CTL) & IWN_UC_RUN))
1186 break;
1187 DELAY(10);
1188 }
1189 if (ntries == 1000) {
1190 iwn_mem_unlock(sc);
1191 aprint_error_dev(sc->sc_dev, "could not load boot firmware\n");
1192 return ETIMEDOUT;
1193 }
1194 iwn_mem_write(sc, IWN_MEM_UCODE_CTL, IWN_UC_ENABLE);
1195
1196 iwn_mem_unlock(sc);
1197
1198 return 0;
1199 }
1200
1201 static int
1202 iwn_load_firmware(struct iwn_softc *sc)
1203 {
1204 struct iwn_dma_info *dma = &sc->fw_dma;
1205 struct iwn_firmware_hdr hdr;
1206 const uint8_t *init_text, *init_data, *main_text, *main_data;
1207 const uint8_t *boot_text;
1208 uint32_t init_textsz, init_datasz, main_textsz, main_datasz;
1209 uint32_t boot_textsz;
1210 firmware_handle_t fw;
1211 u_char *dfw;
1212 size_t size;
1213 int error;
1214
1215 /* load firmware image from disk */
1216 if ((error = firmware_open("if_iwn","iwlwifi-4965.ucode", &fw) != 0)) {
1217 aprint_error_dev(sc->sc_dev, "could not read firmware file\n");
1218 goto fail1;
1219 }
1220
1221 size = firmware_get_size(fw);
1222
1223 /* extract firmware header information */
1224 if (size < sizeof (struct iwn_firmware_hdr)) {
1225 aprint_error_dev(sc->sc_dev, "truncated firmware header: %zu bytes\n", size);
1226
1227 error = EINVAL;
1228 goto fail2;
1229 }
1230
1231
1232 if ((error = firmware_read(fw, 0, &hdr,
1233 sizeof (struct iwn_firmware_hdr))) != 0) {
1234 aprint_error_dev(sc->sc_dev, "can't get firmware header\n");
1235 goto fail2;
1236 }
1237
1238 main_textsz = le32toh(hdr.main_textsz);
1239 main_datasz = le32toh(hdr.main_datasz);
1240 init_textsz = le32toh(hdr.init_textsz);
1241 init_datasz = le32toh(hdr.init_datasz);
1242 boot_textsz = le32toh(hdr.boot_textsz);
1243
1244 /* sanity-check firmware segments sizes */
1245 if (main_textsz > IWN_FW_MAIN_TEXT_MAXSZ ||
1246 main_datasz > IWN_FW_MAIN_DATA_MAXSZ ||
1247 init_textsz > IWN_FW_INIT_TEXT_MAXSZ ||
1248 init_datasz > IWN_FW_INIT_DATA_MAXSZ ||
1249 boot_textsz > IWN_FW_BOOT_TEXT_MAXSZ ||
1250 (boot_textsz & 3) != 0) {
1251 aprint_error_dev(sc->sc_dev, "invalid firmware header\n");
1252 error = EINVAL;
1253 goto fail2;
1254 }
1255
1256 /* check that all firmware segments are present */
1257 if (size < sizeof (struct iwn_firmware_hdr) + main_textsz +
1258 main_datasz + init_textsz + init_datasz + boot_textsz) {
1259 aprint_error_dev(sc->sc_dev, "firmware file too short: %zu bytes\n", size);
1260 error = EINVAL;
1261 goto fail2;
1262 }
1263
1264 dfw = firmware_malloc(size);
1265 if (dfw == NULL) {
1266 aprint_error_dev(sc->sc_dev, "not enough memory to stock firmware\n");
1267 error = ENOMEM;
1268 goto fail2;
1269 }
1270
1271 if ((error = firmware_read(fw, 0, dfw, size)) != 0) {
1272 aprint_error_dev(sc->sc_dev, "can't get firmware\n");
1273 goto fail2;
1274 }
1275
1276 /* get pointers to firmware segments */
1277 main_text = dfw + sizeof (struct iwn_firmware_hdr);
1278 main_data = main_text + main_textsz;
1279 init_text = main_data + main_datasz;
1280 init_data = init_text + init_textsz;
1281 boot_text = init_data + init_datasz;
1282
1283 /* copy initialization images into pre-allocated DMA-safe memory */
1284 memcpy(dma->vaddr, init_data, init_datasz);
1285 memcpy((char *)dma->vaddr + IWN_FW_INIT_DATA_MAXSZ, init_text, init_textsz);
1286
1287 /* tell adapter where to find initialization images */
1288 iwn_mem_lock(sc);
1289 iwn_mem_write(sc, IWN_MEM_DATA_BASE, dma->paddr >> 4);
1290 iwn_mem_write(sc, IWN_MEM_DATA_SIZE, init_datasz);
1291 iwn_mem_write(sc, IWN_MEM_TEXT_BASE,
1292 (dma->paddr + IWN_FW_INIT_DATA_MAXSZ) >> 4);
1293 iwn_mem_write(sc, IWN_MEM_TEXT_SIZE, init_textsz);
1294 iwn_mem_unlock(sc);
1295
1296 /* load firmware boot code */
1297 if ((error = iwn_load_microcode(sc, boot_text, boot_textsz)) != 0) {
1298 aprint_error_dev(sc->sc_dev, "could not load boot firmware\n");
1299 goto fail3;
1300 }
1301
1302 /* now press "execute" ;-) */
1303 IWN_WRITE(sc, IWN_RESET, 0);
1304
1305 /* ..and wait at most one second for adapter to initialize */
1306 if ((error = tsleep(sc, PCATCH, "iwninit", hz)) != 0) {
1307 /* this isn't what was supposed to happen.. */
1308 aprint_error_dev(sc->sc_dev, "timeout waiting for adapter to initialize\n");
1309 }
1310
1311 /* copy runtime images into pre-allocated DMA-safe memory */
1312 memcpy((char *)dma->vaddr, main_data, main_datasz);
1313 memcpy((char *)dma->vaddr + IWN_FW_MAIN_DATA_MAXSZ, main_text, main_textsz);
1314
1315 /* tell adapter where to find runtime images */
1316 iwn_mem_lock(sc);
1317 iwn_mem_write(sc, IWN_MEM_DATA_BASE, dma->paddr >> 4);
1318 iwn_mem_write(sc, IWN_MEM_DATA_SIZE, main_datasz);
1319 iwn_mem_write(sc, IWN_MEM_TEXT_BASE,
1320 (dma->paddr + IWN_FW_MAIN_DATA_MAXSZ) >> 4);
1321 iwn_mem_write(sc, IWN_MEM_TEXT_SIZE, IWN_FW_UPDATED | main_textsz);
1322 iwn_mem_unlock(sc);
1323
1324 /* wait at most one second for second alive notification */
1325 if ((error = tsleep(sc, PCATCH, "iwninit", hz)) != 0) {
1326 /* this isn't what was supposed to happen.. */
1327 aprint_error_dev(sc->sc_dev, "timeout waiting for adapter to initialize\n");
1328 }
1329
1330 fail3: firmware_free(dfw,size);
1331 fail2: firmware_close(fw);
1332 fail1: return error;
1333 }
1334
1335 static void
1336 iwn_calib_timeout(void *arg)
1337 {
1338 struct iwn_softc *sc = arg;
1339 struct ieee80211com *ic = &sc->sc_ic;
1340 int s;
1341
1342 /* automatic rate control triggered every 500ms */
1343 if (ic->ic_fixed_rate == -1) {
1344 s = splnet();
1345 if (ic->ic_opmode == IEEE80211_M_STA)
1346 iwn_iter_func(sc, ic->ic_bss);
1347 else
1348 ieee80211_iterate_nodes(&ic->ic_sta, iwn_iter_func, sc);
1349 splx(s);
1350 }
1351
1352 /* automatic calibration every 60s */
1353 if (++sc->calib_cnt >= 120) {
1354 DPRINTF(("sending request for statistics\n"));
1355 (void)iwn_cmd(sc, IWN_CMD_GET_STATISTICS, NULL, 0, 1);
1356 sc->calib_cnt = 0;
1357 }
1358
1359 callout_schedule(&sc->calib_to, hz/2);
1360
1361 }
1362
1363 static void
1364 iwn_iter_func(void *arg, struct ieee80211_node *ni)
1365 {
1366 struct iwn_softc *sc = arg;
1367 struct iwn_node *wn = (struct iwn_node *)ni;
1368
1369 ieee80211_amrr_choose(&sc->amrr, ni, &wn->amn);
1370 }
1371
1372 static void
1373 iwn_ampdu_rx_start(struct iwn_softc *sc, struct iwn_rx_desc *desc)
1374 {
1375 struct iwn_rx_stat *stat;
1376
1377 DPRINTFN(2, ("received AMPDU stats\n"));
1378 /* save Rx statistics, they will be used on IWN_AMPDU_RX_DONE */
1379 stat = (struct iwn_rx_stat *)(desc + 1);
1380 memcpy(&sc->last_rx_stat, stat, sizeof (*stat));
1381 sc->last_rx_valid = 1;
1382 }
1383
1384 void
1385 iwn_rx_intr(struct iwn_softc *sc, struct iwn_rx_desc *desc,
1386 struct iwn_rx_data *data)
1387 {
1388 struct ieee80211com *ic = &sc->sc_ic;
1389 struct ifnet *ifp = ic->ic_ifp;
1390 struct iwn_rx_ring *ring = &sc->rxq;
1391 struct iwn_rbuf *rbuf;
1392 struct ieee80211_frame *wh;
1393 struct ieee80211_node *ni;
1394 struct mbuf *m, *mnew;
1395 struct iwn_rx_stat *stat;
1396 char *head;
1397 uint32_t *tail;
1398 int len, rssi;
1399
1400 if (desc->type == IWN_AMPDU_RX_DONE) {
1401 /* check for prior AMPDU_RX_START */
1402 if (!sc->last_rx_valid) {
1403 DPRINTF(("missing AMPDU_RX_START\n"));
1404 ifp->if_ierrors++;
1405 return;
1406 }
1407 sc->last_rx_valid = 0;
1408 stat = &sc->last_rx_stat;
1409 } else
1410 stat = (struct iwn_rx_stat *)(desc + 1);
1411
1412 if (stat->cfg_phy_len > IWN_STAT_MAXLEN) {
1413 aprint_error_dev(sc->sc_dev, "invalid rx statistic header\n");
1414 ifp->if_ierrors++;
1415 return;
1416 }
1417
1418 if (desc->type == IWN_AMPDU_RX_DONE) {
1419 struct iwn_rx_ampdu *ampdu =
1420 (struct iwn_rx_ampdu *)(desc + 1);
1421 head = (char *)(ampdu + 1);
1422 len = le16toh(ampdu->len);
1423 } else {
1424 head = (char *)(stat + 1) + stat->cfg_phy_len;
1425 len = le16toh(stat->len);
1426 }
1427
1428 /* discard Rx frames with bad CRC early */
1429 tail = (uint32_t *)(head + len);
1430 if ((le32toh(*tail) & IWN_RX_NOERROR) != IWN_RX_NOERROR) {
1431 DPRINTFN(2, ("rx flags error %x\n", le32toh(*tail)));
1432 ifp->if_ierrors++;
1433 return;
1434 }
1435 /* XXX for ieee80211_find_rxnode() */
1436 if (len < sizeof (struct ieee80211_frame)) {
1437 DPRINTF(("frame too short: %d\n", len));
1438 ic->ic_stats.is_rx_tooshort++;
1439 ifp->if_ierrors++;
1440 return;
1441 }
1442
1443 m = data->m;
1444
1445 /* finalize mbuf */
1446 m->m_pkthdr.rcvif = ifp;
1447 m->m_data = head;
1448 m->m_pkthdr.len = m->m_len = len;
1449
1450 if ((rbuf = SLIST_FIRST(&sc->rxq.freelist)) != NULL) {
1451 MGETHDR(mnew, M_DONTWAIT, MT_DATA);
1452 if (mnew == NULL) {
1453 ic->ic_stats.is_rx_nobuf++;
1454 ifp->if_ierrors++;
1455 return;
1456 }
1457
1458 /* attach Rx buffer to mbuf */
1459 MEXTADD(mnew, rbuf->vaddr, IWN_RBUF_SIZE, 0, iwn_free_rbuf,
1460 rbuf);
1461 mnew->m_flags |= M_EXT_RW;
1462 SLIST_REMOVE_HEAD(&sc->rxq.freelist, next);
1463
1464 data->m = mnew;
1465
1466 /* update Rx descriptor */
1467 ring->desc[ring->cur] = htole32(rbuf->paddr >> 8);
1468 } else {
1469 /* no free rbufs, copy frame */
1470 m = m_dup(m, 0, M_COPYALL, M_DONTWAIT);
1471 if (m == NULL) {
1472 /* no free mbufs either, drop frame */
1473 ic->ic_stats.is_rx_nobuf++;
1474 ifp->if_ierrors++;
1475 return;
1476 }
1477 }
1478
1479 rssi = iwn_get_rssi(stat);
1480
1481 if (ic->ic_state == IEEE80211_S_SCAN)
1482 iwn_fix_channel(ic, m);
1483
1484 #if NBPFILTER > 0
1485 if (sc->sc_drvbpf != NULL) {
1486 struct iwn_rx_radiotap_header *tap = &sc->sc_rxtap;
1487
1488 tap->wr_flags = 0;
1489 tap->wr_chan_freq =
1490 htole16(ic->ic_channels[stat->chan].ic_freq);
1491 tap->wr_chan_flags =
1492 htole16(ic->ic_channels[stat->chan].ic_flags);
1493 tap->wr_dbm_antsignal = (int8_t)rssi;
1494 tap->wr_dbm_antnoise = (int8_t)sc->noise;
1495 tap->wr_tsft = stat->tstamp;
1496 switch (stat->rate) {
1497 /* CCK rates */
1498 case 10: tap->wr_rate = 2; break;
1499 case 20: tap->wr_rate = 4; break;
1500 case 55: tap->wr_rate = 11; break;
1501 case 110: tap->wr_rate = 22; break;
1502 /* OFDM rates */
1503 case 0xd: tap->wr_rate = 12; break;
1504 case 0xf: tap->wr_rate = 18; break;
1505 case 0x5: tap->wr_rate = 24; break;
1506 case 0x7: tap->wr_rate = 36; break;
1507 case 0x9: tap->wr_rate = 48; break;
1508 case 0xb: tap->wr_rate = 72; break;
1509 case 0x1: tap->wr_rate = 96; break;
1510 case 0x3: tap->wr_rate = 108; break;
1511 /* unknown rate: should not happen */
1512 default: tap->wr_rate = 0;
1513 }
1514
1515 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
1516 }
1517 #endif
1518
1519 /* grab a reference to the source node */
1520 wh = mtod(m, struct ieee80211_frame *);
1521 ni = ieee80211_find_rxnode(ic,(struct ieee80211_frame_min *)wh);
1522
1523 /* send the frame to the 802.11 layer */
1524 ieee80211_input(ic, m, ni, rssi, 0);
1525
1526 /* node is no longer needed */
1527 ieee80211_free_node(ni);
1528 }
1529
1530
1531 /*
1532 * XXX: Hack to set the current channel to the value advertised in beacons or
1533 * probe responses. Only used during AP detection.
1534 * XXX: Duplicated from if_iwi.c
1535 */
1536 static void
1537 iwn_fix_channel(struct ieee80211com *ic, struct mbuf *m)
1538 {
1539 struct ieee80211_frame *wh;
1540 uint8_t subtype;
1541 uint8_t *frm, *efrm;
1542
1543 wh = mtod(m, struct ieee80211_frame *);
1544
1545 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
1546 return;
1547
1548 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1549
1550 if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
1551 subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1552 return;
1553
1554 frm = (uint8_t *)(wh + 1);
1555 efrm = mtod(m, uint8_t *) + m->m_len;
1556
1557 frm += 12; /* skip tstamp, bintval and capinfo fields */
1558 while (frm < efrm) {
1559 if (*frm == IEEE80211_ELEMID_DSPARMS)
1560 #if IEEE80211_CHAN_MAX < 255
1561 if (frm[2] <= IEEE80211_CHAN_MAX)
1562 #endif
1563 ic->ic_curchan = &ic->ic_channels[frm[2]];
1564
1565 frm += frm[1] + 2;
1566 }
1567 }
1568
1569 static void
1570 iwn_rx_statistics(struct iwn_softc *sc, struct iwn_rx_desc *desc)
1571 {
1572 struct ieee80211com *ic = &sc->sc_ic;
1573 struct iwn_calib_state *calib = &sc->calib;
1574 struct iwn_stats *stats = (struct iwn_stats *)(desc + 1);
1575
1576 /* ignore beacon statistics received during a scan */
1577 if (ic->ic_state != IEEE80211_S_RUN)
1578 return;
1579
1580 DPRINTFN(3, ("received statistics (cmd=%d)\n", desc->type));
1581 sc->calib_cnt = 0; /* reset timeout */
1582
1583 /* test if temperature has changed */
1584 if (stats->general.temp != sc->rawtemp) {
1585 int temp;
1586
1587 sc->rawtemp = stats->general.temp;
1588 temp = iwn_get_temperature(sc);
1589 DPRINTFN(2, ("temperature=%d\n", temp));
1590
1591 /* update Tx power if need be */
1592 iwn_power_calibration(sc, temp);
1593 }
1594
1595 if (desc->type != IWN_BEACON_STATISTICS)
1596 return; /* reply to a statistics request */
1597
1598 sc->noise = iwn_get_noise(&stats->rx.general);
1599 DPRINTFN(3, ("noise=%d\n", sc->noise));
1600
1601 /* test that RSSI and noise are present in stats report */
1602 if (le32toh(stats->rx.general.flags) != 1) {
1603 DPRINTF(("received statistics without RSSI\n"));
1604 return;
1605 }
1606
1607 if (calib->state == IWN_CALIB_STATE_ASSOC)
1608 iwn_compute_differential_gain(sc, &stats->rx.general);
1609 else if (calib->state == IWN_CALIB_STATE_RUN)
1610 iwn_tune_sensitivity(sc, &stats->rx);
1611 }
1612
1613 static void
1614 iwn_tx_intr(struct iwn_softc *sc, struct iwn_rx_desc *desc)
1615 {
1616 struct ifnet *ifp = sc->sc_ic.ic_ifp;
1617 struct iwn_tx_ring *ring = &sc->txq[desc->qid & 0xf];
1618 struct iwn_tx_data *txdata = &ring->data[desc->idx];
1619 struct iwn_tx_stat *stat = (struct iwn_tx_stat *)(desc + 1);
1620 struct iwn_node *wn = (struct iwn_node *)txdata->ni;
1621 uint32_t status;
1622
1623 DPRINTFN(4, ("tx done: qid=%d idx=%d retries=%d nkill=%d rate=%x "
1624 "duration=%d status=%x\n", desc->qid, desc->idx, stat->ntries,
1625 stat->nkill, stat->rate, le16toh(stat->duration),
1626 le32toh(stat->status)));
1627
1628 /*
1629 * Update rate control statistics for the node.
1630 */
1631 wn->amn.amn_txcnt++;
1632 if (stat->ntries > 0) {
1633 DPRINTFN(3, ("tx intr ntries %d\n", stat->ntries));
1634 wn->amn.amn_retrycnt++;
1635 }
1636
1637 status = le32toh(stat->status) & 0xff;
1638 if (status != 1 && status != 2)
1639 ifp->if_oerrors++;
1640 else
1641 ifp->if_opackets++;
1642
1643 bus_dmamap_unload(sc->sc_dmat, txdata->map);
1644 m_freem(txdata->m);
1645 txdata->m = NULL;
1646 ieee80211_free_node(txdata->ni);
1647 txdata->ni = NULL;
1648
1649 ring->queued--;
1650
1651 sc->sc_tx_timer = 0;
1652 ifp->if_flags &= ~IFF_OACTIVE;
1653 iwn_start(ifp);
1654 }
1655
1656 static void
1657 iwn_cmd_intr(struct iwn_softc *sc, struct iwn_rx_desc *desc)
1658 {
1659 struct iwn_tx_ring *ring = &sc->txq[4];
1660 struct iwn_tx_data *data;
1661
1662 if ((desc->qid & 0xf) != 4)
1663 return; /* not a command ack */
1664
1665 data = &ring->data[desc->idx];
1666
1667 /* if the command was mapped in a mbuf, free it */
1668 if (data->m != NULL) {
1669 bus_dmamap_unload(sc->sc_dmat, data->map);
1670 m_freem(data->m);
1671 data->m = NULL;
1672 }
1673
1674 wakeup(&ring->cmd[desc->idx]);
1675 }
1676
1677 static void
1678 iwn_notif_intr(struct iwn_softc *sc)
1679 {
1680 struct ieee80211com *ic = &sc->sc_ic;
1681 struct ifnet *ifp = ic->ic_ifp;
1682 uint16_t hw;
1683
1684 hw = le16toh(sc->shared->closed_count);
1685 while (sc->rxq.cur != hw) {
1686 struct iwn_rx_data *data = &sc->rxq.data[sc->rxq.cur];
1687 struct iwn_rx_desc *desc = (void *)data->m->m_ext.ext_buf;
1688
1689 DPRINTFN(4,("rx notification qid=%x idx=%d flags=%x type=%d "
1690 "len=%d\n", desc->qid, desc->idx, desc->flags, desc->type,
1691 le32toh(desc->len)));
1692
1693 if (!(desc->qid & 0x80)) /* reply to a command */
1694 iwn_cmd_intr(sc, desc);
1695
1696 switch (desc->type) {
1697 case IWN_RX_DONE:
1698 case IWN_AMPDU_RX_DONE:
1699 iwn_rx_intr(sc, desc, data);
1700 break;
1701
1702 case IWN_AMPDU_RX_START:
1703 iwn_ampdu_rx_start(sc, desc);
1704 break;
1705
1706 case IWN_TX_DONE:
1707 /* a 802.11 frame has been transmitted */
1708 iwn_tx_intr(sc, desc);
1709 break;
1710
1711 case IWN_RX_STATISTICS:
1712 case IWN_BEACON_STATISTICS:
1713 iwn_rx_statistics(sc, desc);
1714 break;
1715
1716 case IWN_BEACON_MISSED:
1717 {
1718 struct iwn_beacon_missed *miss =
1719 (struct iwn_beacon_missed *)(desc + 1);
1720 /*
1721 * If more than 5 consecutive beacons are missed,
1722 * reinitialize the sensitivity state machine.
1723 */
1724 DPRINTFN(2, ("beacons missed %d/%d\n",
1725 le32toh(miss->consecutive), le32toh(miss->total)));
1726 if (ic->ic_state == IEEE80211_S_RUN &&
1727 le32toh(miss->consecutive) > 5)
1728 (void)iwn_init_sensitivity(sc);
1729 break;
1730 }
1731
1732 case IWN_UC_READY:
1733 {
1734 struct iwn_ucode_info *uc =
1735 (struct iwn_ucode_info *)(desc + 1);
1736
1737 /* the microcontroller is ready */
1738 DPRINTF(("microcode alive notification version=%d.%d "
1739 "subtype=%x alive=%x\n", uc->major, uc->minor,
1740 uc->subtype, le32toh(uc->valid)));
1741
1742 if (le32toh(uc->valid) != 1) {
1743 aprint_error_dev(sc->sc_dev, "microcontroller initialization "
1744 "failed\n");
1745 break;
1746 }
1747 if (uc->subtype == IWN_UCODE_INIT) {
1748 /* save microcontroller's report */
1749 memcpy(&sc->ucode_info, uc, sizeof (*uc));
1750 }
1751 break;
1752 }
1753 case IWN_STATE_CHANGED:
1754 {
1755 uint32_t *status = (uint32_t *)(desc + 1);
1756
1757 /* enabled/disabled notification */
1758 DPRINTF(("state changed to %x\n", le32toh(*status)));
1759
1760 if (le32toh(*status) & 1) {
1761 /* the radio button has to be pushed */
1762 aprint_error_dev(sc->sc_dev, "Radio transmitter is off\n");
1763 /* turn the interface down */
1764 ifp->if_flags &= ~IFF_UP;
1765 iwn_stop(ifp, 1);
1766 return; /* no further processing */
1767 }
1768 break;
1769 }
1770 case IWN_START_SCAN:
1771 {
1772 struct iwn_start_scan *scan =
1773 (struct iwn_start_scan *)(desc + 1);
1774
1775 DPRINTFN(2, ("scanning channel %d status %x\n",
1776 scan->chan, le32toh(scan->status)));
1777
1778 /* fix current channel */
1779 ic->ic_bss->ni_chan = &ic->ic_channels[scan->chan];
1780 break;
1781 }
1782 case IWN_STOP_SCAN:
1783 {
1784 struct iwn_stop_scan *scan =
1785 (struct iwn_stop_scan *)(desc + 1);
1786
1787 DPRINTF(("scan finished nchan=%d status=%d chan=%d\n",
1788 scan->nchan, scan->status, scan->chan));
1789
1790 if (scan->status == 1 && scan->chan <= 14) {
1791 /*
1792 * We just finished scanning 802.11g channels,
1793 * start scanning 802.11a ones.
1794 */
1795 if (iwn_scan(sc, IEEE80211_CHAN_A) == 0)
1796 break;
1797 }
1798 sc->is_scanning = false;
1799 ieee80211_end_scan(ic);
1800 break;
1801 }
1802 }
1803
1804 sc->rxq.cur = (sc->rxq.cur + 1) % IWN_RX_RING_COUNT;
1805 }
1806
1807 /* tell the firmware what we have processed */
1808 hw = (hw == 0) ? IWN_RX_RING_COUNT - 1 : hw - 1;
1809 IWN_WRITE(sc, IWN_RX_WIDX, hw & ~7);
1810 }
1811
1812 static int
1813 iwn_intr(void *arg)
1814 {
1815 struct iwn_softc *sc = arg;
1816 struct ifnet *ifp = sc->sc_ic.ic_ifp;
1817 uint32_t r1, r2;
1818
1819 /* disable interrupts */
1820 IWN_WRITE(sc, IWN_MASK, 0);
1821
1822 r1 = IWN_READ(sc, IWN_INTR);
1823 r2 = IWN_READ(sc, IWN_INTR_STATUS);
1824
1825 if (r1 == 0 && r2 == 0) {
1826 if (ifp->if_flags & IFF_UP)
1827 IWN_WRITE(sc, IWN_MASK, IWN_INTR_MASK);
1828 return 0; /* not for us */
1829 }
1830
1831 if (r1 == 0xffffffff)
1832 return 0; /* hardware gone */
1833
1834 /* ack interrupts */
1835 IWN_WRITE(sc, IWN_INTR, r1);
1836 IWN_WRITE(sc, IWN_INTR_STATUS, r2);
1837
1838 DPRINTFN(5, ("interrupt reg1=%x reg2=%x\n", r1, r2));
1839
1840 if (r1 & IWN_RF_TOGGLED) {
1841 uint32_t tmp = IWN_READ(sc, IWN_GPIO_CTL);
1842 aprint_error_dev(sc->sc_dev, "RF switch: radio %s\n",
1843 (tmp & IWN_GPIO_RF_ENABLED) ? "enabled" : "disabled");
1844 }
1845 if (r1 & IWN_CT_REACHED) {
1846 aprint_error_dev(sc->sc_dev, "critical temperature reached!\n");
1847 }
1848 if (r1 & (IWN_SW_ERROR | IWN_HW_ERROR)) {
1849 aprint_error_dev(sc->sc_dev, "fatal firmware error\n");
1850 sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP;
1851 iwn_stop(sc->sc_ic.ic_ifp, 1);
1852 return 1;
1853 }
1854
1855 if ((r1 & (IWN_RX_INTR | IWN_SW_RX_INTR)) ||
1856 (r2 & IWN_RX_STATUS_INTR))
1857 iwn_notif_intr(sc);
1858
1859 if (r1 & IWN_ALIVE_INTR)
1860 wakeup(sc);
1861
1862 /* re-enable interrupts */
1863 if (ifp->if_flags & IFF_UP)
1864 IWN_WRITE(sc, IWN_MASK, IWN_INTR_MASK);
1865
1866 return 1;
1867 }
1868
1869 static uint8_t
1870 iwn_plcp_signal(int rate)
1871 {
1872 switch (rate) {
1873 /* CCK rates (returned values are device-dependent) */
1874 case 2: return 10;
1875 case 4: return 20;
1876 case 11: return 55;
1877 case 22: return 110;
1878
1879 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1880 /* R1-R4, (u)ral is R4-R1 */
1881 case 12: return 0xd;
1882 case 18: return 0xf;
1883 case 24: return 0x5;
1884 case 36: return 0x7;
1885 case 48: return 0x9;
1886 case 72: return 0xb;
1887 case 96: return 0x1;
1888 case 108: return 0x3;
1889 case 120: return 0x3;
1890 }
1891 /* unknown rate (should not get there) */
1892 return 0;
1893 }
1894
1895 /* determine if a given rate is CCK or OFDM */
1896 #define IWN_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
1897
1898 static int
1899 iwn_tx_data(struct iwn_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1900 int ac)
1901 {
1902 struct ieee80211com *ic = &sc->sc_ic;
1903 struct iwn_tx_ring *ring = &sc->txq[ac];
1904 struct iwn_tx_desc *desc;
1905 struct iwn_tx_data *data;
1906 struct iwn_tx_cmd *cmd;
1907 struct iwn_cmd_data *tx;
1908 struct ieee80211_frame *wh;
1909 struct ieee80211_key *k;
1910 const struct chanAccParams *cap;
1911 struct mbuf *mnew;
1912 bus_addr_t paddr;
1913 uint32_t flags;
1914 uint8_t type;
1915 int i, error, pad, rate, hdrlen, noack = 0;
1916
1917 desc = &ring->desc[ring->cur];
1918 data = &ring->data[ring->cur];
1919
1920 wh = mtod(m0, struct ieee80211_frame *);
1921 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1922 /* JAF XXX two lines above were not in wpi. check we don't duplicate this */
1923
1924 if (IEEE80211_QOS_HAS_SEQ(wh)) {
1925 hdrlen = sizeof (struct ieee80211_qosframe);
1926 cap = &ic->ic_wme.wme_chanParams;
1927 noack = cap->cap_wmeParams[ac].wmep_noackPolicy;
1928 } else
1929 hdrlen = sizeof (struct ieee80211_frame);
1930
1931 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1932 k = ieee80211_crypto_encap(ic, ni, m0);
1933 if (k == NULL) {
1934 m_freem(m0);
1935 return ENOBUFS;
1936 }
1937 /* packet header may have moved, reset our local pointer */
1938 wh = mtod(m0, struct ieee80211_frame *);
1939 }
1940
1941 /* pickup a rate */
1942 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1943 IEEE80211_FC0_TYPE_MGT) {
1944 /* mgmt frames are sent at the lowest available bit-rate */
1945 rate = ni->ni_rates.rs_rates[0];
1946 } else {
1947 if (ic->ic_fixed_rate != -1) {
1948 rate = ic->ic_sup_rates[ic->ic_curmode].
1949 rs_rates[ic->ic_fixed_rate];
1950 } else
1951 rate = ni->ni_rates.rs_rates[ni->ni_txrate];
1952 }
1953 rate &= IEEE80211_RATE_VAL;
1954
1955 #if NBPFILTER > 0
1956 if (sc->sc_drvbpf != NULL) {
1957 struct iwn_tx_radiotap_header *tap = &sc->sc_txtap;
1958
1959 tap->wt_flags = 0;
1960 tap->wt_chan_freq = htole16(ni->ni_chan->ic_freq);
1961 tap->wt_chan_flags = htole16(ni->ni_chan->ic_flags);
1962 tap->wt_rate = rate;
1963 tap->wt_hwqueue = ac;
1964 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
1965 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
1966
1967 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1968 }
1969 #endif
1970
1971 cmd = &ring->cmd[ring->cur];
1972 cmd->code = IWN_CMD_TX_DATA;
1973 cmd->flags = 0;
1974 cmd->qid = ring->qid;
1975 cmd->idx = ring->cur;
1976
1977 tx = (struct iwn_cmd_data *)cmd->data;
1978
1979 flags = IWN_TX_AUTO_SEQ;
1980 if (!noack && !IEEE80211_IS_MULTICAST(wh->i_addr1)){
1981 flags |= IWN_TX_NEED_ACK;
1982 }else if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > ic->ic_rtsthreshold)
1983 flags |= htole32(IWN_TX_NEED_RTS | IWN_TX_FULL_TXOP);
1984
1985 tx->id = IEEE80211_IS_MULTICAST(wh->i_addr1) ? IWN_ID_BROADCAST : IWN_ID_BSS;
1986
1987 if (type == IEEE80211_FC0_TYPE_MGT) {
1988 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1989
1990 /* tell h/w to set timestamp in probe responses */
1991 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1992 flags |= IWN_TX_INSERT_TSTAMP;
1993
1994 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ ||
1995 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ)
1996 tx->timeout = htole16(3);
1997 else
1998 tx->timeout = htole16(2);
1999 } else
2000 tx->timeout = htole16(0);
2001
2002 if (hdrlen & 3) {
2003 /* first segment's length must be a multiple of 4 */
2004 flags |= IWN_TX_NEED_PADDING;
2005 pad = 4 - (hdrlen & 3);
2006 } else
2007 pad = 0;
2008
2009 tx->flags = htole32(flags);
2010 tx->len = htole16(m0->m_pkthdr.len);
2011 tx->rate = iwn_plcp_signal(rate);
2012 tx->rts_ntries = 60;
2013 tx->data_ntries = 15;
2014 tx->lifetime = htole32(IWN_LIFETIME_INFINITE);
2015
2016 /* XXX alternate between Ant A and Ant B ? */
2017 tx->rflags = IWN_RFLAG_ANT_B;
2018 if (tx->id == IWN_ID_BROADCAST) {
2019 tx->ridx = IWN_MAX_TX_RETRIES - 1;
2020 if (!IWN_RATE_IS_OFDM(rate))
2021 tx->rflags |= IWN_RFLAG_CCK;
2022 } else {
2023 tx->ridx = 0;
2024 /* tell adapter to ignore rflags */
2025 tx->flags |= htole32(IWN_TX_USE_NODE_RATE);
2026 }
2027
2028 /* copy and trim IEEE802.11 header */
2029 memcpy((uint8_t *)(tx + 1), wh, hdrlen);
2030 m_adj(m0, hdrlen);
2031
2032 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
2033 BUS_DMA_WRITE | BUS_DMA_NOWAIT);
2034 if (error != 0 && error != EFBIG) {
2035 aprint_error_dev(sc->sc_dev, "could not map mbuf (error %d)\n", error);
2036 m_freem(m0);
2037 return error;
2038 }
2039 if (error != 0) {
2040 /* too many fragments, linearize */
2041
2042 MGETHDR(mnew, M_DONTWAIT, MT_DATA);
2043 if (mnew == NULL) {
2044 m_freem(m0);
2045 return ENOMEM;
2046 }
2047 M_COPY_PKTHDR(mnew, m0);
2048 if (m0->m_pkthdr.len > MHLEN) {
2049 MCLGET(mnew, M_DONTWAIT);
2050 if (!(mnew->m_flags & M_EXT)) {
2051 m_freem(m0);
2052 m_freem(mnew);
2053 return ENOMEM;
2054 }
2055 }
2056
2057 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(mnew, void *));
2058 m_freem(m0);
2059 mnew->m_len = mnew->m_pkthdr.len;
2060 m0 = mnew;
2061
2062 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
2063 BUS_DMA_WRITE | BUS_DMA_NOWAIT);
2064 if (error != 0) {
2065 aprint_error_dev(sc->sc_dev, "could not map mbuf (error %d)\n", error);
2066 m_freem(m0);
2067 return error;
2068 }
2069 }
2070
2071 data->m = m0;
2072 data->ni = ni;
2073
2074 DPRINTFN(4, ("sending data: qid=%d idx=%d len=%d nsegs=%d\n",
2075 ring->qid, ring->cur, m0->m_pkthdr.len, data->map->dm_nsegs));
2076
2077 paddr = ring->cmd_dma.paddr + ring->cur * sizeof (struct iwn_tx_cmd);
2078 tx->loaddr = htole32(paddr + 4 +
2079 offsetof(struct iwn_cmd_data, ntries));
2080 tx->hiaddr = 0; /* limit to 32-bit physical addresses */
2081
2082 /* first scatter/gather segment is used by the tx data command */
2083 IWN_SET_DESC_NSEGS(desc, 1 + data->map->dm_nsegs);
2084 IWN_SET_DESC_SEG(desc, 0, paddr, 4 + sizeof (*tx) + hdrlen + pad);
2085 for (i = 1; i <= data->map->dm_nsegs; i++) {
2086 IWN_SET_DESC_SEG(desc, i, data->map->dm_segs[i - 1].ds_addr,
2087 data->map->dm_segs[i - 1].ds_len);
2088 }
2089 sc->shared->len[ring->qid][ring->cur] =
2090 htole16(hdrlen + m0->m_pkthdr.len + 8);
2091 if (ring->cur < IWN_TX_WINDOW) {
2092 sc->shared->len[ring->qid][ring->cur + IWN_TX_RING_COUNT] =
2093 htole16(hdrlen + m0->m_pkthdr.len + 8);
2094 }
2095
2096 ring->queued++;
2097
2098 /* kick ring */
2099 ring->cur = (ring->cur + 1) % IWN_TX_RING_COUNT;
2100 IWN_WRITE(sc, IWN_TX_WIDX, ring->qid << 8 | ring->cur);
2101
2102 return 0;
2103 }
2104
2105 static void
2106 iwn_start(struct ifnet *ifp)
2107 {
2108 struct iwn_softc *sc = ifp->if_softc;
2109 struct ieee80211com *ic = &sc->sc_ic;
2110 struct ieee80211_node *ni;
2111 struct ether_header *eh;
2112 struct mbuf *m0;
2113 int ac;
2114
2115 /*
2116 * net80211 may still try to send management frames even if the
2117 * IFF_RUNNING flag is not set...
2118 */
2119 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
2120 return;
2121
2122 for (;;) {
2123 IF_DEQUEUE(&ic->ic_mgtq, m0);
2124 if (m0 != NULL) {
2125 /* management frames go into ring 0 */
2126
2127
2128 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
2129 m0->m_pkthdr.rcvif = NULL;
2130
2131 /* management goes into ring 0 */
2132 if (sc->txq[0].queued > sc->txq[0].count - 8) {
2133 ifp->if_oerrors++;
2134 continue;
2135 }
2136
2137 #if NBPFILTER > 0
2138 if (ic->ic_rawbpf != NULL)
2139 bpf_mtap(ic->ic_rawbpf, m0);
2140 #endif
2141 if (iwn_tx_data(sc, m0, ni, 0) != 0) {
2142 ifp->if_oerrors++;
2143 break;
2144 }
2145 } else {
2146 if (ic->ic_state != IEEE80211_S_RUN)
2147 break;
2148 IFQ_POLL(&ifp->if_snd, m0);
2149 if (m0 == NULL)
2150 break;
2151
2152 if (m0->m_len < sizeof (*eh) &&
2153 (m0 = m_pullup(m0, sizeof (*eh))) != NULL) {
2154 ifp->if_oerrors++;
2155 continue;
2156 }
2157 eh = mtod(m0, struct ether_header *);
2158 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
2159 if (ni == NULL) {
2160 m_freem(m0);
2161 ifp->if_oerrors++;
2162 continue;
2163 }
2164 /*JAF C266 */
2165 /* classify mbuf so we can find which tx ring to use */
2166 if (ieee80211_classify(ic, m0, ni) != 0) {
2167 m_freem(m0);
2168 ieee80211_free_node(ni);
2169 ifp->if_oerrors++;
2170 continue;
2171 }
2172
2173 /* no QoS encapsulation for EAPOL frames */
2174 ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ?
2175 M_WME_GETAC(m0) : WME_AC_BE;
2176
2177 if (sc->txq[ac].queued > sc->txq[ac].count - 8) {
2178
2179 /* there is no place left in this ring */
2180 ifp->if_flags |= IFF_OACTIVE;
2181 break;
2182 }
2183 IFQ_DEQUEUE(&ifp->if_snd, m0);
2184 #if NBPFILTER > 0
2185 if (ifp->if_bpf != NULL)
2186 bpf_mtap(ifp->if_bpf, m0);
2187 #endif
2188 m0 = ieee80211_encap(ic, m0, ni);
2189 if (m0 == NULL) {
2190 ieee80211_free_node(ni);
2191 ifp->if_oerrors++;
2192 continue;
2193 }
2194 #if NBPFILTER > 0
2195 if (ic->ic_rawbpf != NULL)
2196 bpf_mtap(ic->ic_rawbpf, m0);
2197 #endif
2198 if (iwn_tx_data(sc, m0, ni, ac) != 0) {
2199 ieee80211_free_node(ni);
2200 ifp->if_oerrors++;
2201 break;
2202 }
2203 }
2204
2205 sc->sc_tx_timer = 5;
2206 ifp->if_timer = 1;
2207 }
2208 }
2209
2210 static void
2211 iwn_watchdog(struct ifnet *ifp)
2212 {
2213 struct iwn_softc *sc = ifp->if_softc;
2214
2215 ifp->if_timer = 0;
2216
2217 if (sc->sc_tx_timer > 0) {
2218 if (--sc->sc_tx_timer == 0) {
2219 aprint_error_dev(sc->sc_dev, "device timeout\n");
2220 ifp->if_flags &= ~IFF_UP;
2221 iwn_stop(ifp, 1);
2222 ifp->if_oerrors++;
2223 return;
2224 }
2225 ifp->if_timer = 1;
2226 }
2227
2228 ieee80211_watchdog(&sc->sc_ic);
2229 }
2230
2231 static int
2232 iwn_ioctl(struct ifnet *ifp, u_long cmd, void * data)
2233 {
2234
2235 #define IS_RUNNING(ifp) \
2236 ((ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING))
2237
2238 struct iwn_softc *sc = ifp->if_softc;
2239 struct ieee80211com *ic = &sc->sc_ic;
2240 int s, error = 0;
2241
2242 s = splnet();
2243
2244 switch (cmd) {
2245 case SIOCSIFFLAGS:
2246 if (ifp->if_flags & IFF_UP) {
2247 if (!(ifp->if_flags & IFF_RUNNING))
2248 iwn_init(ifp);
2249 } else {
2250 if (ifp->if_flags & IFF_RUNNING)
2251 iwn_stop(ifp, 1);
2252 }
2253 break;
2254
2255 case SIOCADDMULTI:
2256 case SIOCDELMULTI:
2257 /* XXX no h/w multicast filter? --dyoung */
2258 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
2259 /* setup multicast filter, etc */
2260 error = 0;
2261 }
2262 break;
2263
2264 default:
2265 error = ieee80211_ioctl(ic, cmd, data);
2266 }
2267
2268 if (error == ENETRESET) {
2269 if (IS_RUNNING(ifp) &&
2270 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
2271 iwn_init(ifp);
2272 error = 0;
2273 }
2274
2275 splx(s);
2276 return error;
2277
2278 #undef IS_RUNNING
2279 }
2280
2281 static void
2282 iwn_read_eeprom(struct iwn_softc *sc)
2283 {
2284 struct ieee80211com *ic = &sc->sc_ic;
2285 char domain[4];
2286 uint16_t val;
2287 int i, error;
2288
2289 if ((error = iwn_eeprom_lock(sc)) != 0) {
2290 aprint_error_dev(sc->sc_dev, "could not lock EEPROM (error=%d)\n", error);
2291 return;
2292 }
2293 /* read and print regulatory domain */
2294 iwn_read_prom_data(sc, IWN_EEPROM_DOMAIN, domain, 4);
2295 aprint_error_dev(sc->sc_dev, "%.4s", domain);
2296
2297 /* read and print MAC address */
2298 iwn_read_prom_data(sc, IWN_EEPROM_MAC, ic->ic_myaddr, 6);
2299 aprint_error(", address %s\n", ether_sprintf(ic->ic_myaddr));
2300
2301 /* read the list of authorized channels */
2302 for (i = 0; i < IWN_CHAN_BANDS_COUNT; i++)
2303 iwn_read_eeprom_channels(sc, i);
2304
2305 /* read maximum allowed Tx power for 2GHz and 5GHz bands */
2306 iwn_read_prom_data(sc, IWN_EEPROM_MAXPOW, &val, 2);
2307 sc->maxpwr2GHz = val & 0xff;
2308 sc->maxpwr5GHz = val >> 8;
2309 /* check that EEPROM values are correct */
2310 if (sc->maxpwr5GHz < 20 || sc->maxpwr5GHz > 50)
2311 sc->maxpwr5GHz = 38;
2312 if (sc->maxpwr2GHz < 20 || sc->maxpwr2GHz > 50)
2313 sc->maxpwr2GHz = 38;
2314 DPRINTF(("maxpwr 2GHz=%d 5GHz=%d\n", sc->maxpwr2GHz, sc->maxpwr5GHz));
2315
2316 /* read voltage at which samples were taken */
2317 iwn_read_prom_data(sc, IWN_EEPROM_VOLTAGE, &val, 2);
2318 sc->eeprom_voltage = (int16_t)le16toh(val);
2319 DPRINTF(("voltage=%d (in 0.3V)\n", sc->eeprom_voltage));
2320
2321 /* read power groups */
2322 iwn_read_prom_data(sc, IWN_EEPROM_BANDS, sc->bands, sizeof sc->bands);
2323 #ifdef IWN_DEBUG
2324 if (iwn_debug > 0) {
2325 for (i = 0; i < IWN_NBANDS; i++)
2326 iwn_print_power_group(sc, i);
2327 }
2328 #endif
2329 iwn_eeprom_unlock(sc);
2330 }
2331
2332 static void
2333 iwn_read_eeprom_channels(struct iwn_softc *sc, int n)
2334 {
2335 struct ieee80211com *ic = &sc->sc_ic;
2336 const struct iwn_chan_band *band = &iwn_bands[n];
2337 struct iwn_eeprom_chan channels[IWN_MAX_CHAN_PER_BAND];
2338 int chan, i;
2339
2340 iwn_read_prom_data(sc, band->addr, channels,
2341 band->nchan * sizeof (struct iwn_eeprom_chan));
2342
2343 for (i = 0; i < band->nchan; i++) {
2344 if (!(channels[i].flags & IWN_EEPROM_CHAN_VALID))
2345 continue;
2346
2347 chan = band->chan[i];
2348
2349 if (n == 0) { /* 2GHz band */
2350 ic->ic_channels[chan].ic_freq =
2351 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ);
2352 ic->ic_channels[chan].ic_flags =
2353 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
2354 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
2355
2356 } else { /* 5GHz band */
2357 /*
2358 * Some adapters support channels 7, 8, 11 and 12
2359 * both in the 2GHz *and* 5GHz bands.
2360 * Because of limitations in our net80211(9) stack,
2361 * we can't support these channels in 5GHz band.
2362 */
2363 if (chan <= 14)
2364 continue;
2365
2366 ic->ic_channels[chan].ic_freq =
2367 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_5GHZ);
2368 ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_A;
2369 }
2370
2371 /* is active scan allowed on this channel? */
2372 if (!(channels[i].flags & IWN_EEPROM_CHAN_ACTIVE)) {
2373 ic->ic_channels[chan].ic_flags |=
2374 IEEE80211_CHAN_PASSIVE;
2375 }
2376
2377 /* save maximum allowed power for this channel */
2378 sc->maxpwr[chan] = channels[i].maxpwr;
2379
2380 DPRINTF(("adding chan %d flags=0x%x maxpwr=%d\n",
2381 chan, channels[i].flags, sc->maxpwr[chan]));
2382 }
2383 }
2384
2385 #ifdef IWN_DEBUG
2386 static void
2387 iwn_print_power_group(struct iwn_softc *sc, int i)
2388 {
2389 struct iwn_eeprom_band *band = &sc->bands[i];
2390 struct iwn_eeprom_chan_samples *chans = band->chans;
2391 int j, c;
2392
2393 DPRINTF(("===band %d===\n", i));
2394 DPRINTF(("chan lo=%d, chan hi=%d\n", band->lo, band->hi));
2395 DPRINTF(("chan1 num=%d\n", chans[0].num));
2396 for (c = 0; c < IWN_NTXCHAINS; c++) {
2397 for (j = 0; j < IWN_NSAMPLES; j++) {
2398 DPRINTF(("chain %d, sample %d: temp=%d gain=%d "
2399 "power=%d pa_det=%d\n", c, j,
2400 chans[0].samples[c][j].temp,
2401 chans[0].samples[c][j].gain,
2402 chans[0].samples[c][j].power,
2403 chans[0].samples[c][j].pa_det));
2404 }
2405 }
2406 DPRINTF(("chan2 num=%d\n", chans[1].num));
2407 for (c = 0; c < IWN_NTXCHAINS; c++) {
2408 for (j = 0; j < IWN_NSAMPLES; j++) {
2409 DPRINTF(("chain %d, sample %d: temp=%d gain=%d "
2410 "power=%d pa_det=%d\n", c, j,
2411 chans[1].samples[c][j].temp,
2412 chans[1].samples[c][j].gain,
2413 chans[1].samples[c][j].power,
2414 chans[1].samples[c][j].pa_det));
2415 }
2416 }
2417 }
2418 #endif
2419
2420 /*
2421 * Send a command to the firmware.
2422 */
2423 static int
2424 iwn_cmd(struct iwn_softc *sc, int code, const void *buf, int size, int async)
2425 {
2426 struct iwn_tx_ring *ring = &sc->txq[4];
2427 struct iwn_tx_desc *desc;
2428 struct iwn_tx_cmd *cmd;
2429 bus_addr_t paddr;
2430
2431 KASSERT(size <= sizeof cmd->data);
2432
2433 desc = &ring->desc[ring->cur];
2434 cmd = &ring->cmd[ring->cur];
2435
2436 cmd->code = code;
2437 cmd->flags = 0;
2438 cmd->qid = ring->qid;
2439 cmd->idx = ring->cur;
2440 memcpy(cmd->data, buf, size);
2441
2442 paddr = ring->cmd_dma.paddr + ring->cur * sizeof (struct iwn_tx_cmd);
2443
2444 IWN_SET_DESC_NSEGS(desc, 1);
2445 IWN_SET_DESC_SEG(desc, 0, paddr, 4 + size);
2446 sc->shared->len[ring->qid][ring->cur] = htole16(8);
2447 if (ring->cur < IWN_TX_WINDOW) {
2448 sc->shared->len[ring->qid][ring->cur + IWN_TX_RING_COUNT] =
2449 htole16(8);
2450 }
2451
2452 /* kick cmd ring */
2453 ring->cur = (ring->cur + 1) % IWN_TX_RING_COUNT;
2454 IWN_WRITE(sc, IWN_TX_WIDX, ring->qid << 8 | ring->cur);
2455
2456 return async ? 0 : tsleep(cmd, PCATCH, "iwncmd", hz);
2457 }
2458
2459 /*
2460 * Configure hardware multi-rate retries for one node.
2461 */
2462 static int
2463 iwn_setup_node_mrr(struct iwn_softc *sc, uint8_t id, int async)
2464 {
2465 struct ieee80211com *ic = &sc->sc_ic;
2466 struct iwn_cmd_mrr mrr;
2467 int i, ridx;
2468
2469 memset(&mrr, 0, sizeof mrr);
2470 mrr.id = id;
2471 mrr.ssmask = 2;
2472 mrr.dsmask = 3;
2473 mrr.ampdu_disable = 3;
2474 mrr.ampdu_limit = 4000;
2475
2476 if (id == IWN_ID_BSS)
2477 ridx = IWN_OFDM54;
2478 else if (ic->ic_curmode == IEEE80211_MODE_11A)
2479 ridx = IWN_OFDM6;
2480 else
2481 ridx = IWN_CCK1;
2482 for (i = 0; i < IWN_MAX_TX_RETRIES; i++) {
2483 mrr.table[i].rate = iwn_ridx_to_plcp[ridx];
2484 mrr.table[i].rflags = IWN_RFLAG_ANT_B;
2485 if (ridx <= IWN_CCK11)
2486 mrr.table[i].rflags |= IWN_RFLAG_CCK;
2487 ridx = iwn_prev_ridx[ridx];
2488 }
2489 return iwn_cmd(sc, IWN_CMD_NODE_MRR_SETUP, &mrr, sizeof mrr, async);
2490 }
2491
2492 static int
2493 iwn_wme_update(struct ieee80211com *ic)
2494 {
2495 #define IWN_EXP2(v) htole16((1 << (v)) - 1)
2496 #define IWN_USEC(v) htole16(IEEE80211_TXOP_TO_US(v))
2497 struct iwn_softc *sc = ic->ic_ifp->if_softc;
2498 const struct wmeParams *wmep;
2499 struct iwn_wme_setup wme;
2500 int ac;
2501
2502 /* don't override default WME values if WME is not actually enabled */
2503 if (!(ic->ic_flags & IEEE80211_F_WME))
2504 return 0;
2505
2506 wme.flags = 0;
2507 for (ac = 0; ac < WME_NUM_AC; ac++) {
2508 wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
2509 wme.ac[ac].aifsn = wmep->wmep_aifsn;
2510 wme.ac[ac].cwmin = IWN_EXP2(wmep->wmep_logcwmin);
2511 wme.ac[ac].cwmax = IWN_EXP2(wmep->wmep_logcwmax);
2512 wme.ac[ac].txop = IWN_USEC(wmep->wmep_txopLimit);
2513
2514 DPRINTF(("setting WME for queue %d aifsn=%d cwmin=%d cwmax=%d "
2515 "txop=%d\n", ac, wme.ac[ac].aifsn, wme.ac[ac].cwmin,
2516 wme.ac[ac].cwmax, wme.ac[ac].txop));
2517 }
2518
2519 return iwn_cmd(sc, IWN_CMD_SET_WME, &wme, sizeof wme, 1);
2520 #undef IWN_USEC
2521 #undef IWN_EXP2
2522 }
2523
2524
2525
2526 static void
2527 iwn_set_led(struct iwn_softc *sc, uint8_t which, uint8_t off, uint8_t on)
2528 {
2529 struct iwn_cmd_led led;
2530
2531 led.which = which;
2532 led.unit = htole32(100000); /* on/off in unit of 100ms */
2533 led.off = off;
2534 led.on = on;
2535
2536 (void)iwn_cmd(sc, IWN_CMD_SET_LED, &led, sizeof led, 1);
2537 }
2538
2539 /*
2540 * Set the critical temperature at which the firmware will automatically stop
2541 * the radio transmitter.
2542 */
2543 static int
2544 iwn_set_critical_temp(struct iwn_softc *sc)
2545 {
2546 struct iwn_ucode_info *uc = &sc->ucode_info;
2547 struct iwn_critical_temp crit;
2548 uint32_t r1, r2, r3, temp;
2549
2550 IWN_WRITE(sc, IWN_UCODE_CLR, IWN_CTEMP_STOP_RF);
2551
2552 r1 = le32toh(uc->temp[0].chan20MHz);
2553 r2 = le32toh(uc->temp[1].chan20MHz);
2554 r3 = le32toh(uc->temp[2].chan20MHz);
2555 /* inverse function of iwn_get_temperature() */
2556
2557 temp = r2 + ((IWN_CTOK(110) * (r3 - r1)) / 259);
2558
2559 memset(&crit, 0, sizeof crit);
2560 crit.tempR = htole32(temp);
2561 DPRINTF(("setting critical temperature to %u\n", temp));
2562 return iwn_cmd(sc, IWN_CMD_SET_CRITICAL_TEMP, &crit, sizeof crit, 0);
2563 }
2564
2565 static void
2566 iwn_enable_tsf(struct iwn_softc *sc, struct ieee80211_node *ni)
2567 {
2568 struct iwn_cmd_tsf tsf;
2569 uint64_t val, mod;
2570
2571 memset(&tsf, 0, sizeof tsf);
2572 memcpy(&tsf.tstamp, ni->ni_tstamp.data, 8);
2573 tsf.bintval = htole16(ni->ni_intval);
2574 tsf.lintval = htole16(10);
2575
2576 /* compute remaining time until next beacon */
2577 val = (uint64_t)ni->ni_intval * 1024; /* msecs -> usecs */
2578 mod = le64toh(tsf.tstamp) % val;
2579 tsf.binitval = htole32((uint32_t)(val - mod));
2580
2581 DPRINTF(("TSF bintval=%u tstamp=%llu, init=%u\n",
2582 ni->ni_intval, le64toh(tsf.tstamp), (uint32_t)(val - mod)));
2583
2584 if (iwn_cmd(sc, IWN_CMD_TSF, &tsf, sizeof tsf, 1) != 0)
2585 aprint_error_dev(sc->sc_dev, "could not enable TSF\n");
2586 }
2587
2588 static void
2589 iwn_power_calibration(struct iwn_softc *sc, int temp)
2590 {
2591 struct ieee80211com *ic = &sc->sc_ic;
2592
2593 DPRINTF(("temperature %d->%d\n", sc->temp, temp));
2594
2595 /* adjust Tx power if need be (delta >= 3C) */
2596 if (abs(temp - sc->temp) < 3)
2597 return;
2598
2599 sc->temp = temp;
2600
2601 DPRINTF(("setting Tx power for channel %d\n",
2602 ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan)));
2603 if (iwn_set_txpower(sc, ic->ic_bss->ni_chan, 1) != 0) {
2604 /* just warn, too bad for the automatic calibration... */
2605 aprint_error_dev(sc->sc_dev, "could not adjust Tx power\n");
2606 }
2607 }
2608
2609 /*
2610 * Set Tx power for a given channel (each rate has its own power settings).
2611 * This function takes into account the regulatory information from EEPROM,
2612 * the current temperature and the current voltage.
2613 */
2614 static int
2615 iwn_set_txpower(struct iwn_softc *sc, struct ieee80211_channel *ch, int async)
2616 {
2617 /* fixed-point arithmetic division using a n-bit fractional part */
2618 #define fdivround(a, b, n) \
2619 ((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n))
2620 /* linear interpolation */
2621 #define interpolate(x, x1, y1, x2, y2, n) \
2622 ((y1) + fdivround(((int)(x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n))
2623
2624 static const int tdiv[IWN_NATTEN_GROUPS] = { 9, 8, 8, 8, 6 };
2625 struct ieee80211com *ic = &sc->sc_ic;
2626 struct iwn_ucode_info *uc = &sc->ucode_info;
2627 struct iwn_cmd_txpower cmd;
2628 struct iwn_eeprom_chan_samples *chans;
2629 const uint8_t *rf_gain, *dsp_gain;
2630 int32_t vdiff, tdiff;
2631 int i, c, grp, maxpwr;
2632 u_int chan;
2633
2634 /* get channel number */
2635 chan = ieee80211_chan2ieee(ic, ch);
2636
2637 memset(&cmd, 0, sizeof cmd);
2638 cmd.band = IEEE80211_IS_CHAN_5GHZ(ch) ? 0 : 1;
2639 cmd.chan = chan;
2640
2641 if (IEEE80211_IS_CHAN_5GHZ(ch)) {
2642 maxpwr = sc->maxpwr5GHz;
2643 rf_gain = iwn_rf_gain_5ghz;
2644 dsp_gain = iwn_dsp_gain_5ghz;
2645 } else {
2646 maxpwr = sc->maxpwr2GHz;
2647 rf_gain = iwn_rf_gain_2ghz;
2648 dsp_gain = iwn_dsp_gain_2ghz;
2649 }
2650
2651 /* compute voltage compensation */
2652 vdiff = ((int32_t)le32toh(uc->volt) - sc->eeprom_voltage) / 7;
2653 if (vdiff > 0)
2654 vdiff *= 2;
2655 if (abs(vdiff) > 2)
2656 vdiff = 0;
2657 DPRINTF(("voltage compensation=%d (UCODE=%d, EEPROM=%d)\n",
2658 vdiff, le32toh(uc->volt), sc->eeprom_voltage));
2659
2660 /* get channel's attenuation group */
2661 if (chan <= 20) /* 1-20 */
2662 grp = 4;
2663 else if (chan <= 43) /* 34-43 */
2664 grp = 0;
2665 else if (chan <= 70) /* 44-70 */
2666 grp = 1;
2667 else if (chan <= 124) /* 71-124 */
2668 grp = 2;
2669 else /* 125-200 */
2670 grp = 3;
2671 DPRINTF(("chan %d, attenuation group=%d\n", chan, grp));
2672
2673 /* get channel's sub-band */
2674 for (i = 0; i < IWN_NBANDS; i++)
2675 if (sc->bands[i].lo != 0 &&
2676 sc->bands[i].lo <= chan && chan <= sc->bands[i].hi)
2677 break;
2678 chans = sc->bands[i].chans;
2679 DPRINTF(("chan %d sub-band=%d\n", chan, i));
2680
2681 for (c = 0; c < IWN_NTXCHAINS; c++) {
2682 uint8_t power, gain, temp;
2683 int maxchpwr, pwr, ridx, idx;
2684
2685 power = interpolate(chan,
2686 chans[0].num, chans[0].samples[c][1].power,
2687 chans[1].num, chans[1].samples[c][1].power, 1);
2688 gain = interpolate(chan,
2689 chans[0].num, chans[0].samples[c][1].gain,
2690 chans[1].num, chans[1].samples[c][1].gain, 1);
2691 temp = interpolate(chan,
2692 chans[0].num, chans[0].samples[c][1].temp,
2693 chans[1].num, chans[1].samples[c][1].temp, 1);
2694 DPRINTF(("Tx chain %d: power=%d gain=%d temp=%d\n",
2695 c, power, gain, temp));
2696
2697 /* compute temperature compensation */
2698 tdiff = ((sc->temp - temp) * 2) / tdiv[grp];
2699 DPRINTF(("temperature compensation=%d (current=%d, "
2700 "EEPROM=%d)\n", tdiff, sc->temp, temp));
2701
2702 for (ridx = 0; ridx <= IWN_RIDX_MAX; ridx++) {
2703 maxchpwr = sc->maxpwr[chan] * 2;
2704 if ((ridx / 8) & 1) {
2705 /* MIMO: decrease Tx power (-3dB) */
2706 maxchpwr -= 6;
2707 }
2708
2709 pwr = maxpwr - 10;
2710
2711 /* decrease power for highest OFDM rates */
2712 if ((ridx % 8) == 5) /* 48Mbit/s */
2713 pwr -= 5;
2714 else if ((ridx % 8) == 6) /* 54Mbit/s */
2715 pwr -= 7;
2716 else if ((ridx % 8) == 7) /* 60Mbit/s */
2717 pwr -= 10;
2718
2719 if (pwr > maxchpwr)
2720 pwr = maxchpwr;
2721
2722 idx = gain - (pwr - power) - tdiff - vdiff;
2723 if ((ridx / 8) & 1) /* MIMO */
2724 idx += (int32_t)le32toh(uc->atten[grp][c]);
2725
2726 if (cmd.band == 0)
2727 idx += 9; /* 5GHz */
2728 if (ridx == IWN_RIDX_MAX)
2729 idx += 5; /* CCK */
2730
2731 /* make sure idx stays in a valid range */
2732 if (idx < 0)
2733 idx = 0;
2734 else if (idx > IWN_MAX_PWR_INDEX)
2735 idx = IWN_MAX_PWR_INDEX;
2736
2737 DPRINTF(("Tx chain %d, rate idx %d: power=%d\n",
2738 c, ridx, idx));
2739 cmd.power[ridx].rf_gain[c] = rf_gain[idx];
2740 cmd.power[ridx].dsp_gain[c] = dsp_gain[idx];
2741 }
2742 }
2743
2744 DPRINTF(("setting tx power for chan %d\n", chan));
2745 return iwn_cmd(sc, IWN_CMD_TXPOWER, &cmd, sizeof cmd, async);
2746
2747 #undef interpolate
2748 #undef fdivround
2749 }
2750
2751 /*
2752 * Get the best (maximum) RSSI among Rx antennas (in dBm).
2753 */
2754 static int
2755 iwn_get_rssi(const struct iwn_rx_stat *stat)
2756 {
2757 uint8_t mask, agc;
2758 int rssi;
2759
2760 mask = (le16toh(stat->antenna) >> 4) & 0x7;
2761 agc = (le16toh(stat->agc) >> 7) & 0x7f;
2762
2763 rssi = 0;
2764 if (mask & (1 << 0)) /* Ant A */
2765 rssi = max(rssi, stat->rssi[0]);
2766 if (mask & (1 << 1)) /* Ant B */
2767 rssi = max(rssi, stat->rssi[2]);
2768 if (mask & (1 << 2)) /* Ant C */
2769 rssi = max(rssi, stat->rssi[4]);
2770
2771 return rssi - agc - IWN_RSSI_TO_DBM;
2772 }
2773
2774 /*
2775 * Get the average noise among Rx antennas (in dBm).
2776 */
2777 static int
2778 iwn_get_noise(const struct iwn_rx_general_stats *stats)
2779 {
2780 int i, total, nbant, noise;
2781
2782 total = nbant = 0;
2783 for (i = 0; i < 3; i++) {
2784 if ((noise = le32toh(stats->noise[i]) & 0xff) == 0)
2785 continue;
2786 total += noise;
2787 nbant++;
2788 }
2789 /* there should be at least one antenna but check anyway */
2790 return (nbant == 0) ? -127 : (total / nbant) - 107;
2791 }
2792
2793 /*
2794 * Read temperature (in degC) from the on-board thermal sensor.
2795 */
2796 static int
2797 iwn_get_temperature(struct iwn_softc *sc)
2798 {
2799 struct iwn_ucode_info *uc = &sc->ucode_info;
2800 int32_t r1, r2, r3, r4, temp;
2801
2802 r1 = le32toh(uc->temp[0].chan20MHz);
2803 r2 = le32toh(uc->temp[1].chan20MHz);
2804 r3 = le32toh(uc->temp[2].chan20MHz);
2805 r4 = le32toh(sc->rawtemp);
2806
2807 if (r1 == r3) /* prevents division by 0 (should not happen) */
2808 return 0;
2809
2810 /* sign-extend 23-bit R4 value to 32-bit */
2811 r4 = (r4 << 8) >> 8;
2812 /* compute temperature */
2813 temp = (259 * (r4 - r2)) / (r3 - r1);
2814 temp = (temp * 97) / 100 + 8;
2815
2816 DPRINTF(("temperature %dK/%dC\n", temp, IWN_KTOC(temp)));
2817 return IWN_KTOC(temp);
2818 }
2819
2820 /*
2821 * Initialize sensitivity calibration state machine.
2822 */
2823 static int
2824 iwn_init_sensitivity(struct iwn_softc *sc)
2825 {
2826 struct iwn_calib_state *calib = &sc->calib;
2827 struct iwn_phy_calib_cmd cmd;
2828 int error;
2829
2830 /* reset calibration state */
2831 memset(calib, 0, sizeof (*calib));
2832 calib->state = IWN_CALIB_STATE_INIT;
2833 calib->cck_state = IWN_CCK_STATE_HIFA;
2834 /* initial values taken from the reference driver */
2835 calib->corr_ofdm_x1 = 105;
2836 calib->corr_ofdm_mrc_x1 = 220;
2837 calib->corr_ofdm_x4 = 90;
2838 calib->corr_ofdm_mrc_x4 = 170;
2839 calib->corr_cck_x4 = 125;
2840 calib->corr_cck_mrc_x4 = 200;
2841 calib->energy_cck = 100;
2842
2843 /* write initial sensitivity values */
2844 if ((error = iwn_send_sensitivity(sc)) != 0)
2845 return error;
2846
2847 memset(&cmd, 0, sizeof cmd);
2848 cmd.code = IWN_SET_DIFF_GAIN;
2849 /* differential gains initially set to 0 for all 3 antennas */
2850 DPRINTF(("setting differential gains\n"));
2851 return iwn_cmd(sc, IWN_PHY_CALIB, &cmd, sizeof cmd, 1);
2852 }
2853
2854 /*
2855 * Collect noise and RSSI statistics for the first 20 beacons received
2856 * after association and use them to determine connected antennas and
2857 * set differential gains.
2858 */
2859 static void
2860 iwn_compute_differential_gain(struct iwn_softc *sc,
2861 const struct iwn_rx_general_stats *stats)
2862 {
2863 struct iwn_calib_state *calib = &sc->calib;
2864 struct iwn_phy_calib_cmd cmd;
2865 int i, val;
2866
2867 /* accumulate RSSI and noise for all 3 antennas */
2868 for (i = 0; i < 3; i++) {
2869 calib->rssi[i] += le32toh(stats->rssi[i]) & 0xff;
2870 calib->noise[i] += le32toh(stats->noise[i]) & 0xff;
2871 }
2872
2873 /* we update differential gain only once after 20 beacons */
2874 if (++calib->nbeacons < 20)
2875 return;
2876
2877 /* determine antenna with highest average RSSI */
2878 val = max(calib->rssi[0], calib->rssi[1]);
2879 val = max(calib->rssi[2], val);
2880
2881 /* determine which antennas are connected */
2882 sc->antmsk = 0;
2883 for (i = 0; i < 3; i++)
2884 if (val - calib->rssi[i] <= 15 * 20)
2885 sc->antmsk |= 1 << i;
2886 /* if neither Ant A and Ant B are connected.. */
2887 if ((sc->antmsk & (1 << 0 | 1 << 1)) == 0)
2888 sc->antmsk |= 1 << 1; /* ..mark Ant B as connected! */
2889
2890 /* get minimal noise among connected antennas */
2891 val = INT_MAX; /* ok, there's at least one */
2892 for (i = 0; i < 3; i++)
2893 if (sc->antmsk & (1 << i))
2894 val = min(calib->noise[i], val);
2895
2896 memset(&cmd, 0, sizeof cmd);
2897 cmd.code = IWN_SET_DIFF_GAIN;
2898 /* set differential gains for connected antennas */
2899 for (i = 0; i < 3; i++) {
2900 if (sc->antmsk & (1 << i)) {
2901 cmd.gain[i] = (calib->noise[i] - val) / 30;
2902 /* limit differential gain to 3 */
2903 cmd.gain[i] = min(cmd.gain[i], 3);
2904 cmd.gain[i] |= IWN_GAIN_SET;
2905 }
2906 }
2907 DPRINTF(("setting differential gains Ant A/B/C: %x/%x/%x (%x)\n",
2908 cmd.gain[0], cmd.gain[1], cmd.gain[2], sc->antmsk));
2909 if (iwn_cmd(sc, IWN_PHY_CALIB, &cmd, sizeof cmd, 1) == 0)
2910 calib->state = IWN_CALIB_STATE_RUN;
2911 }
2912
2913 /*
2914 * Tune RF Rx sensitivity based on the number of false alarms detected
2915 * during the last beacon period.
2916 */
2917 static void
2918 iwn_tune_sensitivity(struct iwn_softc *sc, const struct iwn_rx_stats *stats)
2919 {
2920 #define inc_clip(val, inc, max) \
2921 if ((val) < (max)) { \
2922 if ((val) < (max) - (inc)) \
2923 (val) += (inc); \
2924 else \
2925 (val) = (max); \
2926 needs_update = 1; \
2927 }
2928 #define dec_clip(val, dec, min) \
2929 if ((val) > (min)) { \
2930 if ((val) > (min) + (dec)) \
2931 (val) -= (dec); \
2932 else \
2933 (val) = (min); \
2934 needs_update = 1; \
2935 }
2936
2937 struct iwn_calib_state *calib = &sc->calib;
2938 uint32_t val, rxena, fa;
2939 uint32_t energy[3], energy_min;
2940 uint8_t noise[3], noise_ref;
2941 int i, needs_update = 0;
2942
2943 /* check that we've been enabled long enough */
2944 if ((rxena = le32toh(stats->general.load)) == 0)
2945 return;
2946
2947 /* compute number of false alarms since last call for OFDM */
2948 fa = le32toh(stats->ofdm.bad_plcp) - calib->bad_plcp_ofdm;
2949 fa += le32toh(stats->ofdm.fa) - calib->fa_ofdm;
2950 fa *= 200 * 1024; /* 200TU */
2951
2952 /* save counters values for next call */
2953 calib->bad_plcp_ofdm = le32toh(stats->ofdm.bad_plcp);
2954 calib->fa_ofdm = le32toh(stats->ofdm.fa);
2955
2956 if (fa > 50 * rxena) {
2957 /* high false alarm count, decrease sensitivity */
2958 DPRINTFN(2, ("OFDM high false alarm count: %u\n", fa));
2959 inc_clip(calib->corr_ofdm_x1, 1, 140);
2960 inc_clip(calib->corr_ofdm_mrc_x1, 1, 270);
2961 inc_clip(calib->corr_ofdm_x4, 1, 120);
2962 inc_clip(calib->corr_ofdm_mrc_x4, 1, 210);
2963
2964 } else if (fa < 5 * rxena) {
2965 /* low false alarm count, increase sensitivity */
2966 DPRINTFN(2, ("OFDM low false alarm count: %u\n", fa));
2967 dec_clip(calib->corr_ofdm_x1, 1, 105);
2968 dec_clip(calib->corr_ofdm_mrc_x1, 1, 220);
2969 dec_clip(calib->corr_ofdm_x4, 1, 85);
2970 dec_clip(calib->corr_ofdm_mrc_x4, 1, 170);
2971 }
2972
2973 /* compute maximum noise among 3 antennas */
2974 for (i = 0; i < 3; i++)
2975 noise[i] = (le32toh(stats->general.noise[i]) >> 8) & 0xff;
2976 val = max(noise[0], noise[1]);
2977 val = max(noise[2], val);
2978 /* insert it into our samples table */
2979 calib->noise_samples[calib->cur_noise_sample] = val;
2980 calib->cur_noise_sample = (calib->cur_noise_sample + 1) % 20;
2981
2982 /* compute maximum noise among last 20 samples */
2983 noise_ref = calib->noise_samples[0];
2984 for (i = 1; i < 20; i++)
2985 noise_ref = max(noise_ref, calib->noise_samples[i]);
2986
2987 /* compute maximum energy among 3 antennas */
2988 for (i = 0; i < 3; i++)
2989 energy[i] = le32toh(stats->general.energy[i]);
2990 val = min(energy[0], energy[1]);
2991 val = min(energy[2], val);
2992 /* insert it into our samples table */
2993 calib->energy_samples[calib->cur_energy_sample] = val;
2994 calib->cur_energy_sample = (calib->cur_energy_sample + 1) % 10;
2995
2996 /* compute minimum energy among last 10 samples */
2997 energy_min = calib->energy_samples[0];
2998 for (i = 1; i < 10; i++)
2999 energy_min = max(energy_min, calib->energy_samples[i]);
3000 energy_min += 6;
3001
3002 /* compute number of false alarms since last call for CCK */
3003 fa = le32toh(stats->cck.bad_plcp) - calib->bad_plcp_cck;
3004 fa += le32toh(stats->cck.fa) - calib->fa_cck;
3005 fa *= 200 * 1024; /* 200TU */
3006
3007 /* save counters values for next call */
3008 calib->bad_plcp_cck = le32toh(stats->cck.bad_plcp);
3009 calib->fa_cck = le32toh(stats->cck.fa);
3010
3011 if (fa > 50 * rxena) {
3012 /* high false alarm count, decrease sensitivity */
3013 DPRINTFN(2, ("CCK high false alarm count: %u\n", fa));
3014 calib->cck_state = IWN_CCK_STATE_HIFA;
3015 calib->low_fa = 0;
3016
3017 if (calib->corr_cck_x4 > 160) {
3018 calib->noise_ref = noise_ref;
3019 if (calib->energy_cck > 2)
3020 dec_clip(calib->energy_cck, 2, energy_min);
3021 }
3022 if (calib->corr_cck_x4 < 160) {
3023 calib->corr_cck_x4 = 161;
3024 needs_update = 1;
3025 } else
3026 inc_clip(calib->corr_cck_x4, 3, 200);
3027
3028 inc_clip(calib->corr_cck_mrc_x4, 3, 400);
3029
3030 } else if (fa < 5 * rxena) {
3031 /* low false alarm count, increase sensitivity */
3032 DPRINTFN(2, ("CCK low false alarm count: %u\n", fa));
3033 calib->cck_state = IWN_CCK_STATE_LOFA;
3034 calib->low_fa++;
3035
3036 if (calib->cck_state != 0 &&
3037 ((calib->noise_ref - noise_ref) > 2 ||
3038 calib->low_fa > 100)) {
3039 inc_clip(calib->energy_cck, 2, 97);
3040 dec_clip(calib->corr_cck_x4, 3, 125);
3041 dec_clip(calib->corr_cck_mrc_x4, 3, 200);
3042 }
3043 } else {
3044 /* not worth to increase or decrease sensitivity */
3045 DPRINTFN(2, ("CCK normal false alarm count: %u\n", fa));
3046 calib->low_fa = 0;
3047 calib->noise_ref = noise_ref;
3048
3049 if (calib->cck_state == IWN_CCK_STATE_HIFA) {
3050 /* previous interval had many false alarms */
3051 dec_clip(calib->energy_cck, 8, energy_min);
3052 }
3053 calib->cck_state = IWN_CCK_STATE_INIT;
3054 }
3055
3056 if (needs_update)
3057 (void)iwn_send_sensitivity(sc);
3058 #undef dec_clip
3059 #undef inc_clip
3060 }
3061
3062 static int
3063 iwn_send_sensitivity(struct iwn_softc *sc)
3064 {
3065 struct iwn_calib_state *calib = &sc->calib;
3066 struct iwn_sensitivity_cmd cmd;
3067
3068 memset(&cmd, 0, sizeof cmd);
3069 cmd.which = IWN_SENSITIVITY_WORKTBL;
3070 /* OFDM modulation */
3071 cmd.corr_ofdm_x1 = le16toh(calib->corr_ofdm_x1);
3072 cmd.corr_ofdm_mrc_x1 = le16toh(calib->corr_ofdm_mrc_x1);
3073 cmd.corr_ofdm_x4 = le16toh(calib->corr_ofdm_x4);
3074 cmd.corr_ofdm_mrc_x4 = le16toh(calib->corr_ofdm_mrc_x4);
3075 cmd.energy_ofdm = le16toh(100);
3076 cmd.energy_ofdm_th = le16toh(62);
3077 /* CCK modulation */
3078 cmd.corr_cck_x4 = le16toh(calib->corr_cck_x4);
3079 cmd.corr_cck_mrc_x4 = le16toh(calib->corr_cck_mrc_x4);
3080 cmd.energy_cck = le16toh(calib->energy_cck);
3081 /* Barker modulation: use default values */
3082 cmd.corr_barker = le16toh(190);
3083 cmd.corr_barker_mrc = le16toh(390);
3084
3085 DPRINTFN(2, ("setting sensitivity\n"));
3086 return iwn_cmd(sc, IWN_SENSITIVITY, &cmd, sizeof cmd, 1);
3087 }
3088
3089 static int
3090 iwn_auth(struct iwn_softc *sc)
3091 {
3092 struct ieee80211com *ic = &sc->sc_ic;
3093 struct ieee80211_node *ni = ic->ic_bss;
3094 struct iwn_node_info node;
3095 int error;
3096
3097 /* update adapter's configuration */
3098 IEEE80211_ADDR_COPY(sc->config.bssid, ni->ni_bssid);
3099 sc->config.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
3100 sc->config.flags = htole32(IWN_CONFIG_TSF);
3101 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
3102 sc->config.flags |= htole32(IWN_CONFIG_AUTO |
3103 IWN_CONFIG_24GHZ);
3104 }
3105 switch (ic->ic_curmode) {
3106 case IEEE80211_MODE_11A:
3107 sc->config.cck_mask = 0;
3108 sc->config.ofdm_mask = 0x15;
3109 break;
3110 case IEEE80211_MODE_11B:
3111 sc->config.cck_mask = 0x03;
3112 sc->config.ofdm_mask = 0;
3113 break;
3114 default: /* assume 802.11b/g */
3115 sc->config.cck_mask = 0xf;
3116 sc->config.ofdm_mask = 0x15;
3117 }
3118 DPRINTF(("config chan %d flags %x cck %x ofdm %x\n", sc->config.chan,
3119 sc->config.flags, sc->config.cck_mask, sc->config.ofdm_mask));
3120 error = iwn_cmd(sc, IWN_CMD_CONFIGURE, &sc->config,
3121 sizeof (struct iwn_config), 1);
3122 if (error != 0) {
3123 aprint_error_dev(sc->sc_dev, "could not configure\n");
3124 return error;
3125 }
3126
3127 /* configuration has changed, set Tx power accordingly */
3128 if ((error = iwn_set_txpower(sc, ni->ni_chan, 1)) != 0) {
3129 aprint_error_dev(sc->sc_dev, "could not set Tx power\n");
3130 return error;
3131 }
3132
3133 /*
3134 * Reconfiguring clears the adapter's nodes table so we must
3135 * add the broadcast node again.
3136 */
3137 memset(&node, 0, sizeof node);
3138 IEEE80211_ADDR_COPY(node.macaddr, etherbroadcastaddr);
3139 node.id = IWN_ID_BROADCAST;
3140 DPRINTF(("adding broadcast node\n"));
3141 error = iwn_cmd(sc, IWN_CMD_ADD_NODE, &node, sizeof node, 1);
3142 if (error != 0) {
3143 aprint_error_dev(sc->sc_dev, "could not add broadcast node\n");
3144 return error;
3145 }
3146 DPRINTF(("setting MRR for node %d\n", node.id));
3147 if ((error = iwn_setup_node_mrr(sc, node.id, 1)) != 0) {
3148 aprint_error_dev(sc->sc_dev, "could not setup MRR for broadcast node\n");
3149 return error;
3150 }
3151
3152 return 0;
3153 }
3154
3155 /*
3156 * Configure the adapter for associated state.
3157 */
3158 static int
3159 iwn_run(struct iwn_softc *sc)
3160 {
3161 struct ieee80211com *ic = &sc->sc_ic;
3162 struct ieee80211_node *ni = ic->ic_bss;
3163 struct iwn_node_info node;
3164 int error;
3165
3166 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
3167 /* link LED blinks while monitoring */
3168 iwn_set_led(sc, IWN_LED_LINK, 5, 5);
3169 return 0;
3170 }
3171
3172 #if 0
3173 if (ic->ic_opmode != IEEE80211_M_STA) {
3174 (void) iwn_auth(sc); /* XXX */
3175 iwn_setup_beacon(sc, ni);
3176 }
3177 #endif
3178
3179 iwn_enable_tsf(sc, ni);
3180
3181 /* update adapter's configuration */
3182 sc->config.associd = htole16(ni->ni_associd & ~0xc000);
3183 /* short preamble/slot time are negotiated when associating */
3184 sc->config.flags &= ~htole32(IWN_CONFIG_SHPREAMBLE |
3185 IWN_CONFIG_SHSLOT);
3186 if (ic->ic_flags & IEEE80211_F_SHSLOT)
3187 sc->config.flags |= htole32(IWN_CONFIG_SHSLOT);
3188 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
3189 sc->config.flags |= htole32(IWN_CONFIG_SHPREAMBLE);
3190 sc->config.filter |= htole32(IWN_FILTER_BSS);
3191
3192 DPRINTF(("config chan %d flags %x\n", sc->config.chan,
3193 sc->config.flags));
3194 error = iwn_cmd(sc, IWN_CMD_CONFIGURE, &sc->config,
3195 sizeof (struct iwn_config), 1);
3196 if (error != 0) {
3197 aprint_error_dev(sc->sc_dev, "could not update configuration\n");
3198 return error;
3199 }
3200
3201 /* configuration has changed, set Tx power accordingly */
3202 if ((error = iwn_set_txpower(sc, ni->ni_chan, 1)) != 0) {
3203 aprint_error_dev(sc->sc_dev, "could not set Tx power\n");
3204 return error;
3205 }
3206
3207 /* add BSS node */
3208 memset(&node, 0, sizeof node);
3209 IEEE80211_ADDR_COPY(node.macaddr, ni->ni_macaddr);
3210 node.id = IWN_ID_BSS;
3211 node.htflags = htole32(3 << IWN_AMDPU_SIZE_FACTOR_SHIFT |
3212 5 << IWN_AMDPU_DENSITY_SHIFT);
3213 DPRINTF(("adding BSS node\n"));
3214 error = iwn_cmd(sc, IWN_CMD_ADD_NODE, &node, sizeof node, 1);
3215 if (error != 0) {
3216 aprint_error_dev(sc->sc_dev, "could not add BSS node\n");
3217 return error;
3218 }
3219 DPRINTF(("setting MRR for node %d\n", node.id));
3220 if ((error = iwn_setup_node_mrr(sc, node.id, 1)) != 0) {
3221 aprint_error_dev(sc->sc_dev, "could not setup MRR for node %d\n", node.id);
3222 return error;
3223 }
3224
3225 if (ic->ic_opmode == IEEE80211_M_STA) {
3226 /* fake a join to init the tx rate */
3227 iwn_newassoc(ni, 1);
3228 }
3229
3230 if ((error = iwn_init_sensitivity(sc)) != 0) {
3231 aprint_error_dev(sc->sc_dev, "could not set sensitivity\n");
3232 return error;
3233 }
3234
3235 /* start periodic calibration timer */
3236 sc->calib.state = IWN_CALIB_STATE_ASSOC;
3237 sc->calib_cnt = 0;
3238 callout_schedule(&sc->calib_to, hz / 2);
3239
3240 /* link LED always on while associated */
3241 iwn_set_led(sc, IWN_LED_LINK, 0, 1);
3242
3243 return 0;
3244 }
3245
3246 /*
3247 * Send a scan request to the firmware. Since this command is huge, we map it
3248 * into a mbuf instead of using the pre-allocated set of commands.
3249 */
3250 static int
3251 iwn_scan(struct iwn_softc *sc, uint16_t flags)
3252 {
3253 struct ieee80211com *ic = &sc->sc_ic;
3254 struct iwn_tx_ring *ring = &sc->txq[4];
3255 struct iwn_tx_desc *desc;
3256 struct iwn_tx_data *data;
3257 struct iwn_tx_cmd *cmd;
3258 struct iwn_cmd_data *tx;
3259 struct iwn_scan_hdr *hdr;
3260 struct iwn_scan_essid *essid;
3261 struct iwn_scan_chan *chan;
3262 struct ieee80211_frame *wh;
3263 struct ieee80211_rateset *rs;
3264 struct ieee80211_channel *c;
3265 enum ieee80211_phymode mode;
3266 uint8_t *frm;
3267 int pktlen, error, nrates;
3268
3269 desc = &ring->desc[ring->cur];
3270 data = &ring->data[ring->cur];
3271
3272 MGETHDR(data->m, M_DONTWAIT, MT_DATA);
3273 if (data->m == NULL) {
3274 aprint_error_dev(sc->sc_dev, "could not allocate mbuf for scan command\n");
3275 return ENOMEM;
3276 }
3277 MCLGET(data->m, M_DONTWAIT);
3278 if (!(data->m->m_flags & M_EXT)) {
3279 m_freem(data->m);
3280 data->m = NULL;
3281 aprint_error_dev(sc->sc_dev, "could not allocate mbuf for scan command\n");
3282 return ENOMEM;
3283 }
3284
3285 cmd = mtod(data->m, struct iwn_tx_cmd *);
3286 cmd->code = IWN_CMD_SCAN;
3287 cmd->flags = 0;
3288 cmd->qid = ring->qid;
3289 cmd->idx = ring->cur;
3290
3291 hdr = (struct iwn_scan_hdr *)cmd->data;
3292 memset(hdr, 0, sizeof (struct iwn_scan_hdr));
3293 /*
3294 * Move to the next channel if no packets are received within 5 msecs
3295 * after sending the probe request (this helps to reduce the duration
3296 * of active scans).
3297 */
3298 hdr->quiet = htole16(5); /* timeout in milliseconds */
3299 hdr->plcp_threshold = htole16(1); /* min # of packets */
3300
3301 /* select Ant B and Ant C for scanning */
3302 hdr->rxchain = htole16(0x3e1 | 7 << IWN_RXCHAIN_ANTMSK_SHIFT);
3303
3304 tx = (struct iwn_cmd_data *)(hdr + 1);
3305 memset(tx, 0, sizeof (struct iwn_cmd_data));
3306 tx->flags = htole32(IWN_TX_AUTO_SEQ | 0x200); // XXX
3307 tx->id = IWN_ID_BROADCAST;
3308 tx->lifetime = htole32(IWN_LIFETIME_INFINITE);
3309 tx->rflags = IWN_RFLAG_ANT_B;
3310
3311 if (flags & IEEE80211_CHAN_A) {
3312 hdr->crc_threshold = htole16(1);
3313 /* send probe requests at 6Mbps */
3314 tx->rate = iwn_ridx_to_plcp[IWN_OFDM6];
3315 } else {
3316 hdr->flags = htole32(IWN_CONFIG_24GHZ | IWN_CONFIG_AUTO);
3317 /* send probe requests at 1Mbps */
3318 tx->rate = iwn_ridx_to_plcp[IWN_CCK1];
3319 tx->rflags |= IWN_RFLAG_CCK;
3320 }
3321
3322 essid = (struct iwn_scan_essid *)(tx + 1);
3323 memset(essid, 0, 4 * sizeof (struct iwn_scan_essid));
3324 essid[0].id = IEEE80211_ELEMID_SSID;
3325 essid[0].len = ic->ic_des_esslen;
3326 memcpy(essid[0].data, ic->ic_des_essid, ic->ic_des_esslen);
3327
3328 /*
3329 * Build a probe request frame. Most of the following code is a
3330 * copy & paste of what is done in net80211.
3331 */
3332 wh = (struct ieee80211_frame *)&essid[4];
3333 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
3334 IEEE80211_FC0_SUBTYPE_PROBE_REQ;
3335 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3336 IEEE80211_ADDR_COPY(wh->i_addr1, etherbroadcastaddr);
3337 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
3338 IEEE80211_ADDR_COPY(wh->i_addr3, etherbroadcastaddr);
3339 *(u_int16_t *)&wh->i_dur[0] = 0; /* filled by h/w */
3340 *(u_int16_t *)&wh->i_seq[0] = 0; /* filled by h/w */
3341
3342 frm = (uint8_t *)(wh + 1);
3343
3344 /* add empty SSID IE (firmware generates it for directed scans) */
3345 *frm++ = IEEE80211_ELEMID_SSID;
3346 *frm++ = 0;
3347
3348 mode = ieee80211_chan2mode(ic, ic->ic_ibss_chan);
3349 rs = &ic->ic_sup_rates[mode];
3350
3351 /* add supported rates IE */
3352
3353 *frm++ = IEEE80211_ELEMID_RATES;
3354 nrates = rs->rs_nrates;
3355 if (nrates > IEEE80211_RATE_SIZE)
3356 nrates = IEEE80211_RATE_SIZE;
3357 *frm++ = nrates;
3358 memcpy(frm, rs->rs_rates, nrates);
3359 frm += nrates;
3360
3361 /* add supported xrates IE */
3362
3363 if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
3364 nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
3365 *frm++ = IEEE80211_ELEMID_XRATES;
3366 *frm++ = nrates;
3367 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
3368 frm += nrates;
3369 }
3370
3371 /* setup length of probe request */
3372 tx->len = htole16(frm - (uint8_t *)wh);
3373
3374 chan = (struct iwn_scan_chan *)frm;
3375 for (c = &ic->ic_channels[1];
3376 c <= &ic->ic_channels[IEEE80211_CHAN_MAX]; c++) {
3377 if ((c->ic_flags & flags) != flags)
3378 continue;
3379
3380 chan->chan = ieee80211_chan2ieee(ic, c);
3381 chan->flags = 0;
3382 if (!(c->ic_flags & IEEE80211_CHAN_PASSIVE)) {
3383 chan->flags |= IWN_CHAN_ACTIVE;
3384 if (ic->ic_des_esslen != 0)
3385 chan->flags |= IWN_CHAN_DIRECT;
3386 }
3387 chan->dsp_gain = 0x6e;
3388 if (IEEE80211_IS_CHAN_5GHZ(c)) {
3389 chan->rf_gain = 0x3b;
3390 chan->active = htole16(10);
3391 chan->passive = htole16(110);
3392 } else {
3393 chan->rf_gain = 0x28;
3394 chan->active = htole16(20);
3395 chan->passive = htole16(120);
3396 }
3397 hdr->nchan++;
3398 chan++;
3399
3400 frm += sizeof (struct iwn_scan_chan);
3401 }
3402
3403 hdr->len = htole16(frm - (uint8_t *)hdr);
3404 pktlen = frm - (uint8_t *)cmd;
3405
3406 error = bus_dmamap_load(sc->sc_dmat, data->map, cmd, pktlen, NULL,
3407 BUS_DMA_NOWAIT);
3408 if (error) {
3409 aprint_error_dev(sc->sc_dev, "could not map scan command\n");
3410 m_freem(data->m);
3411 data->m = NULL;
3412 return error;
3413 }
3414
3415 IWN_SET_DESC_NSEGS(desc, 1);
3416 IWN_SET_DESC_SEG(desc, 0, data->map->dm_segs[0].ds_addr,
3417 data->map->dm_segs[0].ds_len);
3418 sc->shared->len[ring->qid][ring->cur] = htole16(8);
3419 if (ring->cur < IWN_TX_WINDOW) {
3420 sc->shared->len[ring->qid][ring->cur + IWN_TX_RING_COUNT] =
3421 htole16(8);
3422 }
3423
3424 /* kick cmd ring */
3425 ring->cur = (ring->cur + 1) % IWN_TX_RING_COUNT;
3426 IWN_WRITE(sc, IWN_TX_WIDX, ring->qid << 8 | ring->cur);
3427
3428 return 0; /* will be notified async. of failure/success */
3429 }
3430
3431 static int
3432 iwn_config(struct iwn_softc *sc)
3433 {
3434 struct ieee80211com *ic = &sc->sc_ic;
3435 struct ifnet *ifp = ic->ic_ifp;
3436 struct iwn_power power;
3437 struct iwn_bluetooth bluetooth;
3438 struct iwn_node_info node;
3439 int error;
3440
3441 /* set power mode */
3442 memset(&power, 0, sizeof power);
3443 power.flags = htole16(IWN_POWER_CAM | 0x8);
3444 DPRINTF(("setting power mode\n"));
3445 error = iwn_cmd(sc, IWN_CMD_SET_POWER_MODE, &power, sizeof power, 0);
3446 if (error != 0) {
3447 aprint_error_dev(sc->sc_dev, "could not set power mode\n");
3448 return error;
3449 }
3450
3451 /* configure bluetooth coexistence */
3452 memset(&bluetooth, 0, sizeof bluetooth);
3453 bluetooth.flags = 3;
3454 bluetooth.lead = 0xaa;
3455 bluetooth.kill = 1;
3456 DPRINTF(("configuring bluetooth coexistence\n"));
3457 error = iwn_cmd(sc, IWN_CMD_BLUETOOTH, &bluetooth, sizeof bluetooth,
3458 0);
3459 if (error != 0) {
3460 aprint_error_dev(sc->sc_dev, "could not configure bluetooth coexistence\n");
3461 return error;
3462 }
3463
3464 /* configure adapter */
3465 memset(&sc->config, 0, sizeof (struct iwn_config));
3466 IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
3467 IEEE80211_ADDR_COPY(sc->config.myaddr, ic->ic_myaddr);
3468 IEEE80211_ADDR_COPY(sc->config.wlap, ic->ic_myaddr);
3469 /* set default channel */
3470 sc->config.chan = ieee80211_chan2ieee(ic, ic->ic_ibss_chan);
3471 sc->config.flags = htole32(IWN_CONFIG_TSF);
3472 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_ibss_chan)) {
3473 sc->config.flags |= htole32(IWN_CONFIG_AUTO |
3474 IWN_CONFIG_24GHZ);
3475 }
3476 sc->config.filter = 0;
3477 switch (ic->ic_opmode) {
3478 case IEEE80211_M_STA:
3479 sc->config.mode = IWN_MODE_STA;
3480 sc->config.filter |= htole32(IWN_FILTER_MULTICAST);
3481 break;
3482 case IEEE80211_M_IBSS:
3483 case IEEE80211_M_AHDEMO:
3484 sc->config.mode = IWN_MODE_IBSS;
3485 break;
3486 case IEEE80211_M_HOSTAP:
3487 sc->config.mode = IWN_MODE_HOSTAP;
3488 break;
3489 case IEEE80211_M_MONITOR:
3490 sc->config.mode = IWN_MODE_MONITOR;
3491 sc->config.filter |= htole32(IWN_FILTER_MULTICAST |
3492 IWN_FILTER_CTL | IWN_FILTER_PROMISC);
3493 break;
3494 }
3495 sc->config.cck_mask = 0x0f; /* not yet negotiated */
3496 sc->config.ofdm_mask = 0xff; /* not yet negotiated */
3497 sc->config.ht_single_mask = 0xff;
3498 sc->config.ht_dual_mask = 0xff;
3499 sc->config.rxchain = htole16(0x2800 | 7 << IWN_RXCHAIN_ANTMSK_SHIFT);
3500 DPRINTF(("setting configuration\n"));
3501 error = iwn_cmd(sc, IWN_CMD_CONFIGURE, &sc->config,
3502 sizeof (struct iwn_config), 0);
3503 if (error != 0) {
3504 aprint_error_dev(sc->sc_dev, "configure command failed\n");
3505 return error;
3506 }
3507
3508 /* configuration has changed, set Tx power accordingly */
3509 if ((error = iwn_set_txpower(sc, ic->ic_ibss_chan, 0)) != 0) {
3510 aprint_error_dev(sc->sc_dev, "could not set Tx power\n");
3511 return error;
3512 }
3513
3514 /* add broadcast node */
3515 memset(&node, 0, sizeof node);
3516 IEEE80211_ADDR_COPY(node.macaddr, etherbroadcastaddr);
3517 node.id = IWN_ID_BROADCAST;
3518 DPRINTF(("adding broadcast node\n"));
3519 error = iwn_cmd(sc, IWN_CMD_ADD_NODE, &node, sizeof node, 0);
3520 if (error != 0) {
3521 aprint_error_dev(sc->sc_dev, "could not add broadcast node\n");
3522 return error;
3523 }
3524 DPRINTF(("setting MRR for node %d\n", node.id));
3525 if ((error = iwn_setup_node_mrr(sc, node.id, 0)) != 0) {
3526 aprint_error_dev(sc->sc_dev, "could not setup MRR for node %d\n", node.id);
3527 return error;
3528 }
3529
3530 if ((error = iwn_set_critical_temp(sc)) != 0) {
3531 aprint_error_dev(sc->sc_dev, "could not set critical temperature\n");
3532 return error;
3533 }
3534
3535 return 0;
3536 }
3537
3538 /*
3539 * Do post-alive initialization of the NIC (after firmware upload).
3540 */
3541 static void
3542 iwn_post_alive(struct iwn_softc *sc)
3543 {
3544 uint32_t base;
3545 uint16_t offset;
3546 int qid;
3547
3548 iwn_mem_lock(sc);
3549
3550 /* clear SRAM */
3551 base = iwn_mem_read(sc, IWN_SRAM_BASE);
3552 for (offset = 0x380; offset < 0x520; offset += 4) {
3553 IWN_WRITE(sc, IWN_MEM_WADDR, base + offset);
3554 IWN_WRITE(sc, IWN_MEM_WDATA, 0);
3555 }
3556
3557 /* shared area is aligned on a 1K boundary */
3558 iwn_mem_write(sc, IWN_SRAM_BASE, sc->shared_dma.paddr >> 10);
3559 iwn_mem_write(sc, IWN_SELECT_QCHAIN, 0);
3560
3561 for (qid = 0; qid < IWN_NTXQUEUES; qid++) {
3562 iwn_mem_write(sc, IWN_QUEUE_RIDX(qid), 0);
3563 IWN_WRITE(sc, IWN_TX_WIDX, qid << 8 | 0);
3564
3565 /* set sched. window size */
3566 IWN_WRITE(sc, IWN_MEM_WADDR, base + IWN_QUEUE_OFFSET(qid));
3567 IWN_WRITE(sc, IWN_MEM_WDATA, 64);
3568 /* set sched. frame limit */
3569 IWN_WRITE(sc, IWN_MEM_WADDR, base + IWN_QUEUE_OFFSET(qid) + 4);
3570 IWN_WRITE(sc, IWN_MEM_WDATA, 64 << 16);
3571 }
3572
3573 /* enable interrupts for all 16 queues */
3574 iwn_mem_write(sc, IWN_QUEUE_INTR_MASK, 0xffff);
3575
3576 /* identify active Tx rings (0-7) */
3577 iwn_mem_write(sc, IWN_TX_ACTIVE, 0xff);
3578
3579 /* mark Tx rings (4 EDCA + cmd + 2 HCCA) as active */
3580 for (qid = 0; qid < 7; qid++) {
3581 iwn_mem_write(sc, IWN_TXQ_STATUS(qid),
3582 IWN_TXQ_STATUS_ACTIVE | qid << 1);
3583 }
3584
3585 iwn_mem_unlock(sc);
3586 }
3587
3588 static void
3589 iwn_stop_master(struct iwn_softc *sc)
3590 {
3591 uint32_t tmp;
3592 int ntries;
3593
3594 tmp = IWN_READ(sc, IWN_RESET);
3595 IWN_WRITE(sc, IWN_RESET, tmp | IWN_STOP_MASTER);
3596
3597 tmp = IWN_READ(sc, IWN_GPIO_CTL);
3598 if ((tmp & IWN_GPIO_PWR_STATUS) == IWN_GPIO_PWR_SLEEP)
3599 return; /* already asleep */
3600
3601 for (ntries = 0; ntries < 100; ntries++) {
3602 if (IWN_READ(sc, IWN_RESET) & IWN_MASTER_DISABLED)
3603 break;
3604 DELAY(10);
3605 }
3606 if (ntries == 100) {
3607 aprint_error_dev(sc->sc_dev, "timeout waiting for master\n");
3608 }
3609 }
3610
3611 static int
3612 iwn_reset(struct iwn_softc *sc)
3613 {
3614 uint32_t tmp;
3615 int ntries;
3616
3617 /* clear any pending interrupts */
3618 IWN_WRITE(sc, IWN_INTR, 0xffffffff);
3619
3620 tmp = IWN_READ(sc, IWN_CHICKEN);
3621 IWN_WRITE(sc, IWN_CHICKEN, tmp | IWN_CHICKEN_DISLOS);
3622
3623 tmp = IWN_READ(sc, IWN_GPIO_CTL);
3624 IWN_WRITE(sc, IWN_GPIO_CTL, tmp | IWN_GPIO_INIT);
3625
3626 /* wait for clock stabilization */
3627 for (ntries = 0; ntries < 1000; ntries++) {
3628 if (IWN_READ(sc, IWN_GPIO_CTL) & IWN_GPIO_CLOCK)
3629 break;
3630 DELAY(10);
3631 }
3632 if (ntries == 1000) {
3633 aprint_error_dev(sc->sc_dev, "timeout waiting for clock stabilization\n");
3634 return ETIMEDOUT;
3635 }
3636 return 0;
3637 }
3638
3639 static void
3640 iwn_hw_config(struct iwn_softc *sc)
3641 {
3642 uint32_t tmp, hw;
3643
3644 /* enable interrupts mitigation */
3645 IWN_WRITE(sc, IWN_INTR_MIT, 512 / 32);
3646
3647 /* voodoo from the reference driver */
3648 tmp = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_CLASS_REG);
3649 tmp = PCI_REVISION(tmp);
3650 if ((tmp & 0x80) && (tmp & 0x7f) < 8) {
3651 /* enable "no snoop" field */
3652 tmp = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0xe8);
3653 tmp &= ~IWN_DIS_NOSNOOP;
3654 pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0xe8, tmp);
3655 }
3656
3657 /* disable L1 entry to work around a hardware bug */
3658 tmp = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0xf0);
3659 tmp &= ~IWN_ENA_L1;
3660 pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0xf0, tmp);
3661
3662 hw = IWN_READ(sc, IWN_HWCONFIG);
3663 IWN_WRITE(sc, IWN_HWCONFIG, hw | 0x310);
3664
3665 iwn_mem_lock(sc);
3666 tmp = iwn_mem_read(sc, IWN_MEM_POWER);
3667 iwn_mem_write(sc, IWN_MEM_POWER, tmp | IWN_POWER_RESET);
3668 DELAY(5);
3669 tmp = iwn_mem_read(sc, IWN_MEM_POWER);
3670 iwn_mem_write(sc, IWN_MEM_POWER, tmp & ~IWN_POWER_RESET);
3671 iwn_mem_unlock(sc);
3672 }
3673
3674 static int
3675 iwn_init(struct ifnet *ifp)
3676 {
3677 struct iwn_softc *sc = ifp->if_softc;
3678 struct ieee80211com *ic = &sc->sc_ic;
3679 uint32_t tmp;
3680 int error, qid;
3681
3682 iwn_stop(ifp, 1);
3683 if ((error = iwn_reset(sc)) != 0) {
3684 aprint_error_dev(sc->sc_dev, "could not reset adapter\n");
3685 goto fail1;
3686 }
3687
3688 iwn_mem_lock(sc);
3689 iwn_mem_read(sc, IWN_CLOCK_CTL);
3690 iwn_mem_write(sc, IWN_CLOCK_CTL, 0xa00);
3691 iwn_mem_read(sc, IWN_CLOCK_CTL);
3692 iwn_mem_unlock(sc);
3693
3694 DELAY(20);
3695
3696 iwn_mem_lock(sc);
3697 tmp = iwn_mem_read(sc, IWN_MEM_PCIDEV);
3698 iwn_mem_write(sc, IWN_MEM_PCIDEV, tmp | 0x800);
3699 iwn_mem_unlock(sc);
3700
3701 iwn_mem_lock(sc);
3702 tmp = iwn_mem_read(sc, IWN_MEM_POWER);
3703 iwn_mem_write(sc, IWN_MEM_POWER, tmp & ~0x03000000);
3704 iwn_mem_unlock(sc);
3705
3706 iwn_hw_config(sc);
3707
3708 /* init Rx ring */
3709 iwn_mem_lock(sc);
3710 IWN_WRITE(sc, IWN_RX_CONFIG, 0);
3711 IWN_WRITE(sc, IWN_RX_WIDX, 0);
3712 /* Rx ring is aligned on a 256-byte boundary */
3713 IWN_WRITE(sc, IWN_RX_BASE, sc->rxq.desc_dma.paddr >> 8);
3714 /* shared area is aligned on a 16-byte boundary */
3715 IWN_WRITE(sc, IWN_RW_WIDX_PTR, (sc->shared_dma.paddr +
3716 offsetof(struct iwn_shared, closed_count)) >> 4);
3717 IWN_WRITE(sc, IWN_RX_CONFIG, 0x80601000);
3718 iwn_mem_unlock(sc);
3719
3720 IWN_WRITE(sc, IWN_RX_WIDX, (IWN_RX_RING_COUNT - 1) & ~7);
3721
3722 iwn_mem_lock(sc);
3723 iwn_mem_write(sc, IWN_TX_ACTIVE, 0);
3724
3725 /* set physical address of "keep warm" page */
3726 IWN_WRITE(sc, IWN_KW_BASE, sc->kw_dma.paddr >> 4);
3727
3728 /* init Tx rings */
3729 for (qid = 0; qid < IWN_NTXQUEUES; qid++) {
3730 struct iwn_tx_ring *txq = &sc->txq[qid];
3731 IWN_WRITE(sc, IWN_TX_BASE(qid), txq->desc_dma.paddr >> 8);
3732 IWN_WRITE(sc, IWN_TX_CONFIG(qid), 0x80000008);
3733 }
3734 iwn_mem_unlock(sc);
3735
3736 /* clear "radio off" and "disable command" bits (reversed logic) */
3737 IWN_WRITE(sc, IWN_UCODE_CLR, IWN_RADIO_OFF);
3738 IWN_WRITE(sc, IWN_UCODE_CLR, IWN_DISABLE_CMD);
3739
3740 /* clear any pending interrupts */
3741 IWN_WRITE(sc, IWN_INTR, 0xffffffff);
3742 /* enable interrupts */
3743 IWN_WRITE(sc, IWN_MASK, IWN_INTR_MASK);
3744
3745 /* not sure why/if this is necessary... */
3746 IWN_WRITE(sc, IWN_UCODE_CLR, IWN_RADIO_OFF);
3747 IWN_WRITE(sc, IWN_UCODE_CLR, IWN_RADIO_OFF);
3748
3749 /* check that the radio is not disabled by RF switch */
3750 if (!(IWN_READ(sc, IWN_GPIO_CTL) & IWN_GPIO_RF_ENABLED)) {
3751 aprint_error_dev(sc->sc_dev, "radio is disabled by hardware switch\n");
3752 error = EBUSY; /* XXX ;-) */
3753 goto fail1;
3754 }
3755
3756 if ((error = iwn_load_firmware(sc)) != 0) {
3757 aprint_error_dev(sc->sc_dev, "could not load firmware\n");
3758 goto fail1;
3759 }
3760
3761 /* firmware has notified us that it is alive.. */
3762 iwn_post_alive(sc); /* ..do post alive initialization */
3763
3764 sc->rawtemp = sc->ucode_info.temp[3].chan20MHz;
3765 sc->temp = iwn_get_temperature(sc);
3766 DPRINTF(("temperature=%d\n", sc->temp));
3767
3768 if ((error = iwn_config(sc)) != 0) {
3769 aprint_error_dev(sc->sc_dev, "could not configure device\n");
3770 goto fail1;
3771 }
3772
3773 DPRINTF(("iwn_config end\n"));
3774
3775 ifp->if_flags &= ~IFF_OACTIVE;
3776 ifp->if_flags |= IFF_RUNNING;
3777
3778 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
3779 if (ic->ic_opmode != IEEE80211_ROAMING_MANUAL)
3780 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
3781 }
3782 else
3783 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
3784
3785 DPRINTF(("iwn_init ok\n"));
3786 return 0;
3787
3788 fail1:
3789 DPRINTF(("iwn_init error\n"));
3790 iwn_stop(ifp, 1);
3791 return error;
3792 }
3793
3794 static void
3795 iwn_stop(struct ifnet *ifp, int disable)
3796 {
3797 struct iwn_softc *sc = ifp->if_softc;
3798 struct ieee80211com *ic = &sc->sc_ic;
3799 uint32_t tmp;
3800 int i;
3801
3802 ifp->if_timer = sc->sc_tx_timer = 0;
3803 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
3804
3805 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
3806
3807 IWN_WRITE(sc, IWN_RESET, IWN_NEVO_RESET);
3808
3809 /* disable interrupts */
3810 IWN_WRITE(sc, IWN_MASK, 0);
3811 IWN_WRITE(sc, IWN_INTR, 0xffffffff);
3812 IWN_WRITE(sc, IWN_INTR_STATUS, 0xffffffff);
3813
3814 /* make sure we no longer hold the memory lock */
3815 iwn_mem_unlock(sc);
3816
3817 /* reset all Tx rings */
3818 for (i = 0; i < IWN_NTXQUEUES; i++)
3819 iwn_reset_tx_ring(sc, &sc->txq[i]);
3820
3821 /* reset Rx ring */
3822 iwn_reset_rx_ring(sc, &sc->rxq);
3823
3824 iwn_mem_lock(sc);
3825 iwn_mem_write(sc, IWN_MEM_CLOCK2, 0x200);
3826 iwn_mem_unlock(sc);
3827
3828 DELAY(5);
3829
3830 iwn_stop_master(sc);
3831 tmp = IWN_READ(sc, IWN_RESET);
3832 IWN_WRITE(sc, IWN_RESET, tmp | IWN_SW_RESET);
3833 }
3834
3835 static bool
3836 iwn_resume(device_t dv)
3837 {
3838 struct iwn_softc *sc = device_private(dv);
3839
3840 pci_disable_retry(sc->sc_pct, sc->sc_pcitag);
3841 (void)iwn_reset(sc);
3842
3843 return true;
3844 }
3845