if_iwn.c revision 1.38 1 /* $NetBSD: if_iwn.c,v 1.38 2010/04/05 07:20:26 joerg Exp $ */
2 /* $OpenBSD: if_iwn.c,v 1.49 2009/03/29 21:53:52 sthen Exp $ */
3
4 /*-
5 * Copyright (c) 2007, 2008
6 * Damien Bergamini <damien.bergamini (at) free.fr>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20
21 /*
22 * Driver for Intel Wireless WiFi Link 4965 and Intel WiFi Link 5000 Series
23 * 802.11 network adapters.
24 */
25 #include <sys/cdefs.h>
26 __KERNEL_RCSID(0, "$NetBSD: if_iwn.c,v 1.38 2010/04/05 07:20:26 joerg Exp $");
27
28
29 #include <sys/param.h>
30 #include <sys/sockio.h>
31 #include <sys/sysctl.h>
32 #include <sys/mbuf.h>
33 #include <sys/kernel.h>
34 #include <sys/socket.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/mutex.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 #include <net/bpf.h>
51 #include <net/if.h>
52 #include <net/if_arp.h>
53 #include <net/if_dl.h>
54 #include <net/if_media.h>
55 #include <net/if_types.h>
56
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/in_var.h>
60 #include <net/if_ether.h>
61 #include <netinet/ip.h>
62
63 #include <net80211/ieee80211_var.h>
64 #include <net80211/ieee80211_amrr.h>
65 #include <net80211/ieee80211_radiotap.h>
66
67 #include <dev/firmload.h>
68
69 #include <dev/pci/if_iwnreg.h>
70 #include <dev/pci/if_iwnvar.h>
71
72 static const pci_product_id_t iwn_devices[] = {
73 PCI_PRODUCT_INTEL_PRO_WL_4965AGN_1,
74 PCI_PRODUCT_INTEL_PRO_WL_4965AGN_2,
75 PCI_PRODUCT_INTEL_PRO_WL_5100AGN_1,
76 PCI_PRODUCT_INTEL_PRO_WL_5100AGN_2,
77 #ifdef notyet
78 PCI_PRODUCT_INTEL_PRO_WL_5150AGN_1,
79 PCI_PRODUCT_INTEL_PRO_WL_5150AGN_2,
80 #endif
81 PCI_PRODUCT_INTEL_PRO_WL_5300AGN_1,
82 PCI_PRODUCT_INTEL_PRO_WL_5300AGN_2,
83 PCI_PRODUCT_INTEL_PRO_WL_5350AGN_1,
84 PCI_PRODUCT_INTEL_PRO_WL_5350AGN_2,
85 #ifdef notyet
86 PCI_PRODUCT_INTEL_WIFI_LINK_4965_1,
87 PCI_PRODUCT_INTEL_WIFI_LINK_4965_2,
88 PCI_PRODUCT_INTEL_WIFI_LINK_5100_1,
89 PCI_PRODUCT_INTEL_WIFI_LINK_5100_2,
90 PCI_PRODUCT_INTEL_WIFI_LINK_5150_1,
91 PCI_PRODUCT_INTEL_WIFI_LINK_5150_2,
92 PCI_PRODUCT_INTEL_WIFI_LINK_5300_1,
93 PCI_PRODUCT_INTEL_WIFI_LINK_5300_2,
94 PCI_PRODUCT_INTEL_WIFI_LINK_5350_1,
95 PCI_PRODUCT_INTEL_WIFI_LINK_5350_2,
96 PCI_PRODUCT_INTEL_WIFI_LINK_6000_1,
97 PCI_PRODUCT_INTEL_WIFI_LINK_6000_2,
98 PCI_PRODUCT_INTEL_WIFI_LINK_6000_3,
99 PCI_PRODUCT_INTEL_WIFI_LINK_6000_4,
100 PCI_PRODUCT_INTEL_WIFI_LINK_6050_1,
101 PCI_PRODUCT_INTEL_WIFI_LINK_6050_2,
102 PCI_PRODUCT_INTEL_WIFI_LINK_6050_3,
103 PCI_PRODUCT_INTEL_WIFI_LINK_6050_4,
104 PCI_PRODUCT_INTEL_WIFI_LINK_1000_1,
105 PCI_PRODUCT_INTEL_WIFI_LINK_1000_2,
106 #endif
107 };
108
109 /*
110 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
111 */
112 static const struct ieee80211_rateset iwn_rateset_11a =
113 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
114
115 static const struct ieee80211_rateset iwn_rateset_11b =
116 { 4, { 2, 4, 11, 22 } };
117
118 static const struct ieee80211_rateset iwn_rateset_11g =
119 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
120
121
122 static int iwn_match(device_t , struct cfdata *, void *);
123 static void iwn_attach(device_t , device_t, void *);
124 static int iwn_detach(device_t, int);
125
126 const struct iwn_hal *iwn_hal_attach(struct iwn_softc *);
127 static int iwn_nic_lock(struct iwn_softc *);
128 static int iwn_eeprom_lock(struct iwn_softc *);
129 static int iwn_read_prom_data(struct iwn_softc *, uint32_t, void *, int);
130 static void iwn_radiotap_attach(struct iwn_softc *);
131 static int iwn_dma_contig_alloc(bus_dma_tag_t, struct iwn_dma_info *,
132 void **, bus_size_t, bus_size_t, int);
133 static void iwn_dma_contig_free(struct iwn_dma_info *);
134 static int iwn_alloc_sched(struct iwn_softc *);
135 static void iwn_free_sched(struct iwn_softc *);
136 static int iwn_alloc_kw(struct iwn_softc *);
137 static void iwn_free_kw(struct iwn_softc *);
138 static int iwn_alloc_fwmem(struct iwn_softc *);
139 static void iwn_free_fwmem(struct iwn_softc *);
140 static struct iwn_rbuf *iwn_alloc_rbuf(struct iwn_softc *);
141 static void iwn_free_rbuf(struct mbuf *, void *, size_t, void *);
142 static int iwn_alloc_rpool(struct iwn_softc *);
143 static void iwn_free_rpool(struct iwn_softc *);
144 static int iwn_alloc_rx_ring(struct iwn_softc *, struct iwn_rx_ring *);
145 static void iwn_reset_rx_ring(struct iwn_softc *, struct iwn_rx_ring *);
146 static void iwn_free_rx_ring(struct iwn_softc *, struct iwn_rx_ring *);
147 static int iwn_alloc_tx_ring(struct iwn_softc *, struct iwn_tx_ring *,
148 int, int);
149 static void iwn_reset_tx_ring(struct iwn_softc *, struct iwn_tx_ring *);
150 static void iwn_free_tx_ring(struct iwn_softc *, struct iwn_tx_ring *);
151 static int iwn_read_eeprom(struct iwn_softc *);
152 static void iwn4965_read_eeprom(struct iwn_softc *);
153 static void iwn5000_read_eeprom(struct iwn_softc *);
154 static void iwn_read_eeprom_channels(struct iwn_softc *, int, uint32_t);
155 static struct ieee80211_node *iwn_node_alloc(struct ieee80211_node_table *);
156 static void iwn_newassoc(struct ieee80211_node *, int);
157 static int iwn_media_change(struct ifnet *);
158 static int iwn_newstate(struct ieee80211com *, enum ieee80211_state, int);
159 static void iwn_iter_func(void *, struct ieee80211_node *);
160 static void iwn_calib_timeout(void *);
161 #if 0
162 static int iwn_ccmp_decap(struct iwn_softc *, struct mbuf *,
163 struct ieee80211_key *);
164 #endif
165 static void iwn_rx_phy(struct iwn_softc *, struct iwn_rx_desc *);
166 static void iwn_rx_done(struct iwn_softc *, struct iwn_rx_desc *,
167 struct iwn_rx_data *);
168 static void iwn5000_rx_calib_results(struct iwn_softc *,
169 struct iwn_rx_desc *, struct iwn_rx_data *);
170 static void iwn_rx_statistics(struct iwn_softc *, struct iwn_rx_desc *,
171 struct iwn_rx_data *);
172 static void iwn4965_tx_done(struct iwn_softc *, struct iwn_rx_desc *,
173 struct iwn_rx_data *);
174 static void iwn5000_tx_done(struct iwn_softc *, struct iwn_rx_desc *,
175 struct iwn_rx_data *);
176 static void iwn_tx_done(struct iwn_softc *, struct iwn_rx_desc *, int,
177 uint8_t);
178 static void iwn_cmd_done(struct iwn_softc *, struct iwn_rx_desc *);
179 static void iwn_notif_intr(struct iwn_softc *);
180 static void iwn_wakeup_intr(struct iwn_softc *);
181 static void iwn_fatal_intr(struct iwn_softc *);
182 static int iwn_intr(void *);
183 static void iwn4965_update_sched(struct iwn_softc *, int, int, uint8_t,
184 uint16_t);
185 static void iwn5000_update_sched(struct iwn_softc *, int, int, uint8_t,
186 uint16_t);
187 static void iwn5000_reset_sched(struct iwn_softc *, int, int);
188 static int iwn_tx(struct iwn_softc *, struct mbuf *,
189 struct ieee80211_node *, int);
190 static void iwn_start(struct ifnet *);
191 static void iwn_watchdog(struct ifnet *);
192 static int iwn_ioctl(struct ifnet *, u_long, void *);
193 static int iwn_cmd(struct iwn_softc *, int, const void *, int, int);
194 static int iwn_wme_update(struct ieee80211com *);
195 static int iwn4965_add_node(struct iwn_softc *, struct iwn_node_info *,
196 int);
197 static int iwn5000_add_node(struct iwn_softc *, struct iwn_node_info *,
198 int);
199 static int iwn_set_link_quality(struct iwn_softc *,
200 struct ieee80211_node *);
201 static int iwn_add_broadcast_node(struct iwn_softc *, int);
202 static void iwn_set_led(struct iwn_softc *, uint8_t, uint8_t, uint8_t);
203 static int iwn_set_critical_temp(struct iwn_softc *);
204 static int iwn_set_timing(struct iwn_softc *, struct ieee80211_node *);
205 //static void iwn4965_power_calibration(struct iwn_softc *, int);
206 static int iwn4965_set_txpower(struct iwn_softc *, int);
207 static int iwn5000_set_txpower(struct iwn_softc *, int);
208 static int iwn4965_get_rssi(const struct iwn_rx_stat *);
209 static int iwn5000_get_rssi(const struct iwn_rx_stat *);
210 static int iwn_get_noise(const struct iwn_rx_general_stats *);
211 static int iwn4965_get_temperature(struct iwn_softc *);
212 static int iwn5000_get_temperature(struct iwn_softc *);
213 static int iwn_init_sensitivity(struct iwn_softc *);
214 static void iwn_collect_noise(struct iwn_softc *,
215 const struct iwn_rx_general_stats *);
216 static int iwn4965_init_gains(struct iwn_softc *);
217 static int iwn5000_init_gains(struct iwn_softc *);
218 static int iwn4965_set_gains(struct iwn_softc *);
219 static int iwn5000_set_gains(struct iwn_softc *);
220 static void iwn_tune_sensitivity(struct iwn_softc *,
221 const struct iwn_rx_stats *);
222 static int iwn_send_sensitivity(struct iwn_softc *);
223 // XXX static int iwn_set_pslevel(struct iwn_softc *, int, int, int);
224 static int iwn_config(struct iwn_softc *);
225 static int iwn_scan(struct iwn_softc *, uint16_t);
226 static int iwn_auth(struct iwn_softc *);
227 static int iwn_run(struct iwn_softc *);
228 #ifdef notyet
229 static void iwn_delete_key(struct ieee80211com *, struct ieee80211_node *,
230 struct ieee80211_key *);
231 #endif
232 #ifndef IEEE80211_NO_HT
233 static int iwn_ampdu_rx_start(struct ieee80211com *,
234 struct ieee80211_node *, uint8_t, uint16_t);
235 static void iwn_ampdu_rx_stop(struct ieee80211com *,
236 struct ieee80211_node *, uint8_t, uint16_t);
237 static int iwn_ampdu_tx_start(struct ieee80211com *,
238 struct ieee80211_node *, uint8_t, uint16_t);
239 static void iwn_ampdu_tx_stop(struct ieee80211com *,
240 struct ieee80211_node *, uint8_t, uint16_t);
241 static void iwn4965_ampdu_tx_start(struct iwn_softc *,
242 struct ieee80211_node *, uint8_t, uint16_t);
243 static void iwn4965_ampdu_tx_stop(struct iwn_softc *,
244 uint8_t, uint16_t);
245 static void iwn5000_ampdu_tx_start(struct iwn_softc *,
246 struct ieee80211_node *, uint8_t, uint16_t);
247 static void iwn5000_ampdu_tx_stop(struct iwn_softc *,
248 uint8_t, uint16_t);
249 #endif
250 static int iwn5000_query_calibration(struct iwn_softc *);
251 static int iwn5000_send_calibration(struct iwn_softc *);
252 static int iwn4965_post_alive(struct iwn_softc *);
253 static int iwn5000_post_alive(struct iwn_softc *);
254 static int iwn4965_load_bootcode(struct iwn_softc *, const uint8_t *,
255 int);
256 static int iwn4965_load_firmware(struct iwn_softc *);
257 static int iwn5000_load_firmware_section(struct iwn_softc *, uint32_t,
258 const uint8_t *, int);
259 static int iwn5000_load_firmware(struct iwn_softc *);
260 static int iwn_read_firmware(struct iwn_softc *);
261 static int iwn_clock_wait(struct iwn_softc *);
262 static int iwn4965_apm_init(struct iwn_softc *);
263 static int iwn5000_apm_init(struct iwn_softc *);
264 static void iwn_apm_stop_master(struct iwn_softc *);
265 static void iwn_apm_stop(struct iwn_softc *);
266 static int iwn4965_nic_config(struct iwn_softc *);
267 static int iwn5000_nic_config(struct iwn_softc *);
268 static int iwn_hw_init(struct iwn_softc *);
269 static void iwn_hw_stop(struct iwn_softc *);
270 static int iwn_init(struct ifnet *);
271 static void iwn_stop(struct ifnet *, int);
272 static void iwn_fix_channel(struct ieee80211com *, struct mbuf *);
273 static bool iwn_resume(device_t, const pmf_qual_t *);
274
275 #define IWN_DEBUG
276 #ifdef IWN_DEBUG
277 #define DPRINTF(x) do { if (iwn_debug > 0) printf x; } while (0)
278 #define DPRINTFN(n, x) do { if (iwn_debug >= (n)) printf x; } while (0)
279 int iwn_debug = 0;
280 #else
281 #define DPRINTF(x)
282 #define DPRINTFN(n, x)
283 #endif
284 #ifdef IWN_DEBUG
285 static void iwn4965_print_power_group(struct iwn_softc *, int);
286 #endif
287
288 static const struct iwn_hal iwn4965_hal = {
289 iwn4965_load_firmware,
290 iwn4965_read_eeprom,
291 iwn4965_post_alive,
292 iwn4965_apm_init,
293 iwn4965_nic_config,
294 iwn4965_update_sched,
295 iwn4965_get_temperature,
296 iwn4965_get_rssi,
297 iwn4965_set_txpower,
298 iwn4965_init_gains,
299 iwn4965_set_gains,
300 iwn4965_add_node,
301 iwn4965_tx_done,
302 #ifndef IEEE80211_NO_HT
303 iwn4965_ampdu_tx_start,
304 iwn4965_ampdu_tx_stop,
305 #endif
306 &iwn4965_sensitivity_limits,
307 IWN4965_NTXQUEUES,
308 IWN4965_ID_BROADCAST,
309 IWN4965_RXONSZ,
310 IWN4965_SCHEDSZ,
311 IWN4965_FW_TEXT_MAXSZ,
312 IWN4965_FW_DATA_MAXSZ,
313 IWN4965_FWSZ,
314 IWN4965_SCHED_TXFACT
315 };
316
317 static const struct iwn_hal iwn5000_hal = {
318 iwn5000_load_firmware,
319 iwn5000_read_eeprom,
320 iwn5000_post_alive,
321 iwn5000_apm_init,
322 iwn5000_nic_config,
323 iwn5000_update_sched,
324 iwn5000_get_temperature,
325 iwn5000_get_rssi,
326 iwn5000_set_txpower,
327 iwn5000_init_gains,
328 iwn5000_set_gains,
329 iwn5000_add_node,
330 iwn5000_tx_done,
331 #ifndef IEEE80211_NO_HT
332 iwn5000_ampdu_tx_start,
333 iwn5000_ampdu_tx_stop,
334 #endif
335 &iwn5000_sensitivity_limits,
336 IWN5000_NTXQUEUES,
337 IWN5000_ID_BROADCAST,
338 IWN5000_RXONSZ,
339 IWN5000_SCHEDSZ,
340 IWN5000_FW_TEXT_MAXSZ,
341 IWN5000_FW_DATA_MAXSZ,
342 IWN5000_FWSZ,
343 IWN5000_SCHED_TXFACT
344 };
345
346 CFATTACH_DECL_NEW(iwn, sizeof(struct iwn_softc), iwn_match, iwn_attach,
347 iwn_detach, NULL);
348
349
350 static int
351 iwn_match(device_t parent, cfdata_t match __unused, void *aux)
352 {
353 struct pci_attach_args *pa = aux;
354 size_t i;
355
356 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
357 return 0;
358
359 for (i = 0; i < __arraycount(iwn_devices); i++)
360 if (PCI_PRODUCT(pa->pa_id) == iwn_devices[i])
361 return 1;
362
363 return 0;
364 }
365
366 static void
367 iwn_attach(device_t parent __unused, device_t self, void *aux)
368 {
369 struct iwn_softc *sc = device_private(self);
370 struct ieee80211com *ic = &sc->sc_ic;
371 struct ifnet *ifp = &sc->sc_ec.ec_if;
372 struct pci_attach_args *pa = aux;
373 const struct iwn_hal *hal;
374 const char *intrstr;
375 char devinfo[256];
376 pci_intr_handle_t ih;
377 pcireg_t memtype, reg;
378 int i, error, revision;
379
380 sc->sc_dev = self;
381 sc->sc_pct = pa->pa_pc;
382 sc->sc_pcitag = pa->pa_tag;
383
384 callout_init(&sc->calib_to, 0);
385 callout_setfunc(&sc->calib_to, iwn_calib_timeout, sc);
386
387 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof devinfo);
388 revision = PCI_REVISION(pa->pa_class);
389 aprint_normal(": %s (rev. 0x%02x)\n", devinfo, revision);
390
391 /*
392 * Get the offset of the PCI Express Capability Structure in PCI
393 * Configuration Space (the vendor driver hard-codes it as E0h.)
394 */
395 error = pci_get_capability(sc->sc_pct, sc->sc_pcitag,
396 PCI_CAP_PCIEXPRESS, &sc->sc_cap_off, NULL);
397 if (error == 0) {
398 aprint_error_dev(self, "PCIe capability structure not found!\n");
399 return;
400 }
401
402 /* Clear device-specific "PCI retry timeout" register (41h). */
403 reg = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
404 reg &= ~0xff00;
405 pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, reg);
406
407 /* enable bus-mastering */
408 reg = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
409 reg |= PCI_COMMAND_MASTER_ENABLE;
410 pci_conf_write(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, reg);
411
412 /* map the register window */
413 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, IWN_PCI_BAR0);
414 error = pci_mapreg_map(pa, IWN_PCI_BAR0, memtype, 0, &sc->sc_st,
415 &sc->sc_sh, NULL, &sc->sc_sz);
416 if (error != 0) {
417 aprint_error_dev(self, "could not map memory space\n");
418 return;
419 }
420 #if 1
421 sc->sc_dmat = pa->pa_dmat;
422 #else
423 /* XXX may not be needed */
424 if (bus_dmatag_subregion(pa->pa_dmat, 0, 3 << 30,
425 &(sc->sc_dmat), BUS_DMA_NOWAIT) != 0) {
426 aprint_error_dev(self,
427 "WARNING: failed to restrict dma range, "
428 "falling back to parent bus dma range\n");
429 sc->sc_dmat = pa->pa_dmat;
430 }
431 #endif
432
433 /* Install interrupt handler. */
434 if (pci_intr_map(pa, &ih) != 0) {
435 aprint_error_dev(self, "could not map interrupt\n");
436 return;
437 }
438 intrstr = pci_intr_string(sc->sc_pct, ih);
439 sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, iwn_intr, sc);
440
441 if (sc->sc_ih == NULL) {
442 aprint_error_dev(self, "could not establish interrupt");
443 if (intrstr != NULL)
444 aprint_error(" at %s", intrstr);
445 aprint_error("\n");
446 return;
447 }
448 aprint_normal_dev(self, "interrupting at %s\n", intrstr);
449
450 /* Attach Hardware Abstraction Layer. */
451 if ((hal = iwn_hal_attach(sc)) == NULL)
452 return;
453
454 /* Power ON adapter. */
455 if ((error = hal->apm_init(sc)) != 0) {
456 aprint_error_dev(self, "could not power ON adapter\n");
457 return;
458 }
459
460 /* Read MAC address, channels, etc from EEPROM. */
461 if ((error = iwn_read_eeprom(sc)) != 0) {
462 aprint_error_dev(self, "could not read EEPROM\n");
463 return;
464 }
465
466 /* Allocate DMA memory for firmware transfers. */
467 if ((error = iwn_alloc_fwmem(sc)) != 0) {
468 aprint_error_dev(self,
469 "could not allocate memory for firmware\n");
470 return;
471 }
472
473 /* Allocate "Keep Warm" page. */
474 if ((error = iwn_alloc_kw(sc)) != 0) {
475 aprint_error_dev(self, "could not allocate keep warm page\n");
476 goto fail1;
477 }
478
479 /* Allocate TX scheduler "rings". */
480 if ((error = iwn_alloc_sched(sc)) != 0) {
481 aprint_error_dev(self,
482 "could not allocate TX scheduler rings\n");
483 goto fail2;
484 }
485
486 /* Allocate RX buffers. */
487 if ((error = iwn_alloc_rpool(sc)) != 0) {
488 aprint_error_dev(self, "could not allocate RX buffers\n");
489 goto fail3;
490 }
491
492 /* Allocate TX rings (16 on 4965AGN, 20 on 5000.) */
493 for (i = 0; i < hal->ntxqs; i++) {
494 struct iwn_tx_ring *txq = &sc->txq[i];
495 error = iwn_alloc_tx_ring(sc, txq, IWN_TX_RING_COUNT, i);
496 if (error != 0) {
497 aprint_error_dev(self,
498 "could not allocate TX ring %d\n", i);
499 goto fail4;
500 }
501 }
502
503 /* Allocate RX ring. */
504 if (iwn_alloc_rx_ring(sc, &sc->rxq) != 0) {
505 aprint_error_dev(self, "could not allocate RX ring\n");
506 goto fail4;
507 }
508
509 /* Power OFF adapter. */
510 iwn_apm_stop(sc);
511 /* Clear pending interrupts. */
512 IWN_WRITE(sc, IWN_INT, 0xffffffff);
513
514 aprint_normal_dev(self, "MIMO %dT%dR, %.4s, address %s\n", sc->ntxchains,
515 sc->nrxchains, sc->eeprom_domain, ether_sprintf(ic->ic_myaddr));
516
517 /* Initialization firmware has not been loaded yet. */
518 sc->sc_flags |= IWN_FLAG_FIRST_BOOT;
519
520 /* Set the state of the RF kill switch */
521 sc->sc_radio = (IWN_READ(sc, IWN_GP_CNTRL) & IWN_GP_CNTRL_RFKILL);
522
523 ic->ic_ifp = ifp;
524 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
525 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
526 ic->ic_state = IEEE80211_S_INIT;
527
528 /* Set device capabilities. */
529 ic->ic_caps =
530 IEEE80211_C_IBSS | /* IBSS mode support */
531 IEEE80211_C_WPA | /* 802.11i */
532 IEEE80211_C_MONITOR | /* monitor mode supported */
533 IEEE80211_C_TXPMGT | /* tx power management */
534 IEEE80211_C_SHSLOT | /* short slot time supported */
535 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
536 IEEE80211_C_WME; /* 802.11e */
537
538 /* Set supported rates. */
539 ic->ic_sup_rates[IEEE80211_MODE_11B] = iwn_rateset_11b;
540 ic->ic_sup_rates[IEEE80211_MODE_11G] = iwn_rateset_11g;
541 if (sc->sc_flags & IWN_FLAG_HAS_5GHZ) {
542 ic->ic_sup_rates[IEEE80211_MODE_11A] = iwn_rateset_11a;
543 }
544
545 /* IBSS channel undefined for now. */
546 ic->ic_ibss_chan = &ic->ic_channels[0];
547
548 memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
549 ic->ic_des_esslen = 0;
550
551 ifp->if_softc = sc;
552 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
553 ifp->if_stop = iwn_stop;
554 ifp->if_init = iwn_init;
555 ifp->if_ioctl = iwn_ioctl;
556 ifp->if_start = iwn_start;
557 ifp->if_watchdog = iwn_watchdog;
558 IFQ_SET_READY(&ifp->if_snd);
559 memcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
560
561 if_attach(ifp);
562 ieee80211_ifattach(ic);
563 ic->ic_node_alloc = iwn_node_alloc;
564 ic->ic_newassoc = iwn_newassoc;
565 ic->ic_wme.wme_update = iwn_wme_update;
566 #ifdef notyet
567 ic->ic_updateedca = iwn_updateedca;
568 ic->ic_set_key = iwn_set_key;
569 ic->ic_delete_key = iwn_delete_key;
570 #endif
571 #ifndef IEEE80211_NO_HT
572 ic->ic_ampdu_rx_start = iwn_ampdu_rx_start;
573 ic->ic_ampdu_rx_stop = iwn_ampdu_rx_stop;
574 ic->ic_ampdu_tx_start = iwn_ampdu_tx_start;
575 ic->ic_ampdu_tx_stop = iwn_ampdu_tx_stop;
576 #endif
577
578 /* Override 802.11 state transition machine. */
579 sc->sc_newstate = ic->ic_newstate;
580 ic->ic_newstate = iwn_newstate;
581 ieee80211_media_init(ic, iwn_media_change, ieee80211_media_status);
582
583 sc->amrr.amrr_min_success_threshold = 1;
584 sc->amrr.amrr_max_success_threshold = 15;
585
586 if (pmf_device_register(self, NULL, iwn_resume))
587 pmf_class_network_register(self, ifp);
588 else
589 aprint_error_dev(self, "couldn't establish power handler\n");
590
591 iwn_radiotap_attach(sc);
592
593 ieee80211_announce(ic);
594
595 return;
596
597 /* Free allocated memory if something failed during attachment. */
598 fail4: while (--i >= 0)
599 iwn_free_tx_ring(sc, &sc->txq[i]);
600 iwn_free_rpool(sc);
601 fail3: iwn_free_sched(sc);
602 fail2: iwn_free_kw(sc);
603 fail1: iwn_free_fwmem(sc);
604 }
605
606 static int
607 iwn_detach(device_t self, int flags __unused)
608 {
609 struct iwn_softc *sc = device_private(self);
610 struct ifnet *ifp = sc->sc_ic.ic_ifp;
611 int ac;
612
613 iwn_stop(ifp, 1);
614 if (ifp != NULL)
615 bpf_detach(ifp);
616 ieee80211_ifdetach(&sc->sc_ic);
617 if (ifp != NULL)
618 if_detach(ifp);
619
620 for (ac = 0; ac < sc->sc_hal->ntxqs; ac++)
621 iwn_free_tx_ring(sc, &sc->txq[ac]);
622 iwn_free_rx_ring(sc, &sc->rxq);
623 iwn_free_rpool(sc);
624 iwn_free_sched(sc);
625
626 if (sc->sc_ih != NULL) {
627 pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
628 sc->sc_ih = NULL;
629 }
630
631 bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
632
633 return 0;
634 }
635
636 const struct iwn_hal *
637 iwn_hal_attach(struct iwn_softc *sc)
638 {
639 sc->hw_type = (IWN_READ(sc, IWN_HW_REV) >> 4) & 0xf;
640
641 switch (sc->hw_type) {
642 case IWN_HW_REV_TYPE_4965:
643 sc->sc_hal = &iwn4965_hal;
644 sc->fwname = "iwlwifi-4965-1.ucode";
645 sc->critical_temp = IWN_CTOK(110);
646 sc->txantmsk = IWN_ANT_A | IWN_ANT_B;
647 sc->rxantmsk = IWN_ANT_ABC;
648 sc->ntxchains = 2;
649 sc->nrxchains = 3;
650 break;
651 case IWN_HW_REV_TYPE_5100:
652 sc->sc_hal = &iwn5000_hal;
653 sc->fwname = "iwlwifi-5000-1.ucode";
654 sc->critical_temp = 110;
655 sc->txantmsk = IWN_ANT_B;
656 sc->rxantmsk = IWN_ANT_A | IWN_ANT_B;
657 sc->ntxchains = 1;
658 sc->nrxchains = 2;
659 break;
660 case IWN_HW_REV_TYPE_5150:
661 sc->sc_hal = &iwn5000_hal;
662 sc->fwname = "iwlwifi-5150-1.ucode";
663 /* NB: critical temperature will be read from EEPROM. */
664 sc->txantmsk = IWN_ANT_A;
665 sc->rxantmsk = IWN_ANT_A | IWN_ANT_B;
666 sc->ntxchains = 1;
667 sc->nrxchains = 2;
668 break;
669 case IWN_HW_REV_TYPE_5300:
670 case IWN_HW_REV_TYPE_5350:
671 sc->sc_hal = &iwn5000_hal;
672 sc->fwname = "iwlwifi-5000-1.ucode";
673 sc->critical_temp = 110;
674 sc->txantmsk = sc->rxantmsk = IWN_ANT_ABC;
675 sc->ntxchains = sc->nrxchains = 3;
676 break;
677 #ifdef notyet
678 case IWN_HW_REV_TYPE_1000:
679 sc->sc_hal = &iwn5000_hal;
680 sc->fwname = "iwn-1000";
681 sc->critical_temp = 110;
682 sc->txantmsk = IWN_ANT_A;
683 sc->rxantmsk = IWN_ANT_A | IWN_ANT_B;
684 sc->ntxchains = 1;
685 sc->nrxchains = 2;
686 break;
687 case IWN_HW_REV_TYPE_6000:
688 sc->sc_hal = &iwn5000_hal;
689 sc->fwname = "iwn-6000";
690 sc->critical_temp = 110;
691 sc->txantmsk = IWN_ANT_ABC;
692 sc->rxantmsk = IWN_ANT_ABC;
693 sc->ntxchains = 3;
694 sc->nrxchains = 3;
695 break;
696 case IWN_HW_REV_TYPE_6050:
697 sc->sc_hal = &iwn5000_hal;
698 sc->fwname = "iwn-6050";
699 sc->critical_temp = 110;
700 sc->txantmsk = IWN_ANT_ABC;
701 sc->rxantmsk = IWN_ANT_ABC;
702 sc->ntxchains = 3;
703 sc->nrxchains = 3;
704 break;
705 #endif
706 default:
707 printf(": adapter type %d not supported\n", sc->hw_type);
708 return NULL;
709 }
710 return sc->sc_hal;
711 }
712
713 #if 0
714 /*
715 * Attach the adapter's on-board thermal sensor to the sensors framework.
716 */
717 void
718 iwn_sensor_attach(struct iwn_softc *sc)
719 {
720 strlcpy(sc->sensordev.xname, sc->sc_dev.dv_xname,
721 sizeof sc->sensordev.xname);
722 sc->sensor.type = SENSOR_TEMP;
723 /* Temperature is not valid unless interface is up. */
724 sc->sensor.value = 0;
725 sc->sensor.flags = SENSOR_FINVALID;
726 sensor_attach(&sc->sensordev, &sc->sensor);
727 sensordev_install(&sc->sensordev);
728 }
729 #endif /* 0 */
730
731 /*
732 * Attach the interface to 802.11 radiotap.
733 */
734 static void
735 iwn_radiotap_attach(struct iwn_softc *sc)
736 {
737 struct ifnet *ifp = sc->sc_ic.ic_ifp;
738
739 bpf_attach2(ifp, DLT_IEEE802_11_RADIO,
740 sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
741 &sc->sc_drvbpf);
742
743 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
744 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
745 sc->sc_rxtap.wr_ihdr.it_present = htole32(IWN_RX_RADIOTAP_PRESENT);
746
747 sc->sc_txtap_len = sizeof sc->sc_txtapu;
748 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
749 sc->sc_txtap.wt_ihdr.it_present = htole32(IWN_TX_RADIOTAP_PRESENT);
750 }
751
752 #if 0 /* XXX */
753 /*
754 * Build a beacon frame that the firmware will broadcast periodically in
755 * IBSS or HostAP modes.
756 */
757 static int
758 iwn_setup_beacon(struct iwn_softc *sc, struct ieee80211_node *ni)
759 {
760 struct ieee80211com *ic = &sc->sc_ic;
761 struct iwn_tx_ring *ring = &sc->txq[4];
762 struct iwn_tx_desc *desc;
763 struct iwn_tx_data *data;
764 struct iwn_tx_cmd *cmd;
765 struct iwn_cmd_beacon *bcn;
766 struct ieee80211_beacon_offsets bo;
767 struct mbuf *m0;
768 bus_addr_t paddr;
769 int error;
770
771 desc = &ring->desc[ring->cur];
772 data = &ring->data[ring->cur];
773 m0 = ieee80211_beacon_alloc(ic, ni, &bo);
774 if (m0 == NULL) {
775 aprint_error_dev(sc->sc_dev, "could not allocate beacon frame\n"
776 );
777 return ENOMEM;
778 }
779 cmd = &ring->cmd[ring->cur];
780 cmd->code = IWN_CMD_SET_BEACON;
781 cmd->flags = 0;
782 cmd->qid = ring->qid;
783 cmd->idx = ring->cur;
784
785 bcn = (struct iwn_cmd_beacon *)cmd->data;
786 memset(bcn, 0, sizeof (struct iwn_cmd_beacon));
787 bcn->id = sc->sc_hal->broadcast_id;
788 bcn->lifetime = htole32(IWN_LIFETIME_INFINITE);
789 bcn->len = htole16(m0->m_pkthdr.len);
790 #if 0
791 XXX
792 bcn->rate = (ic->ic_curmode == IEEE80211_MODE_11A) ?
793 iwn_plcp_signal(12) : iwn_plcp_signal(2);
794 #endif
795 bcn->flags2 = 0x2; /* RATE_MCS_CCK_MSK */
796 bcn->flags = htole32(IWN_TX_AUTO_SEQ | IWN_TX_INSERT_TSTAMP;
797 // XXX | IWN_TX_USE_NODE_RATE);
798
799 /* save and trim IEEE802.11 header */
800 m_copydata(m0, 0, sizeof (struct ieee80211_frame), (void *)&bcn->wh);
801 m_adj(m0, sizeof (struct ieee80211_frame));
802
803 /* assume beacon frame is contiguous */
804 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
805 BUS_DMA_READ | BUS_DMA_NOWAIT);
806 if (error) {
807 aprint_error_dev(sc->sc_dev, "could not map beacon\n");
808 m_freem(m0);
809 return error;
810 }
811
812 data->m = m0;
813
814 /* first scatter/gather segment is used by the beacon command */
815 paddr = ring->cmd_dma.paddr + ring->cur * sizeof (struct iwn_tx_cmd);
816
817 IWN_SET_DESC_NSEGS(desc, 2);
818 IWN_SET_DESC_SEG(desc, 0, paddr , 4 + sizeof(struct iwn_cmd_beacon));
819 IWN_SET_DESC_SEG(desc, 1, data->map->dm_segs[0].ds_addr,
820 data->map->dm_segs[1].ds_len);
821
822 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
823 data->map->dm_mapsize /* calc? */, BUS_DMASYNC_PREWRITE);
824
825 /* kick cmd ring */
826 ring->cur = (ring->cur + 1) % IWN_TX_RING_COUNT;
827 IWN_WRITE(sc, IWN_TX_WIDX, ring->qid << 8 | ring->cur);
828 return 0;
829 }
830 #endif
831
832 static int
833 iwn_nic_lock(struct iwn_softc *sc)
834 {
835 int ntries;
836
837 /* Request exclusive access to NIC. */
838 IWN_SETBITS(sc, IWN_GP_CNTRL, IWN_GP_CNTRL_MAC_ACCESS_REQ);
839
840 /* Spin until we actually get the lock. */
841 for (ntries = 0; ntries < 1000; ntries++) {
842 if ((IWN_READ(sc, IWN_GP_CNTRL) &
843 (IWN_GP_CNTRL_MAC_ACCESS_ENA | IWN_GP_CNTRL_SLEEP)) ==
844 IWN_GP_CNTRL_MAC_ACCESS_ENA)
845 return 0;
846 DELAY(10);
847 }
848 return ETIMEDOUT;
849 }
850
851 static __inline void
852 iwn_nic_unlock(struct iwn_softc *sc)
853 {
854 IWN_CLRBITS(sc, IWN_GP_CNTRL, IWN_GP_CNTRL_MAC_ACCESS_REQ);
855 }
856
857 static __inline uint32_t
858 iwn_prph_read(struct iwn_softc *sc, uint32_t addr)
859 {
860 IWN_WRITE(sc, IWN_PRPH_RADDR, IWN_PRPH_DWORD | addr);
861 return IWN_READ(sc, IWN_PRPH_RDATA);
862 }
863
864 static __inline void
865 iwn_prph_write(struct iwn_softc *sc, uint32_t addr, uint32_t data)
866 {
867 IWN_WRITE(sc, IWN_PRPH_WADDR, IWN_PRPH_DWORD | addr);
868 IWN_WRITE(sc, IWN_PRPH_WDATA, data);
869 }
870
871 static __inline void
872 iwn_prph_setbits(struct iwn_softc *sc, uint32_t addr, uint32_t mask)
873 {
874 iwn_prph_write(sc, addr, iwn_prph_read(sc, addr) | mask);
875 }
876
877 static __inline void
878 iwn_prph_clrbits(struct iwn_softc *sc, uint32_t addr, uint32_t mask)
879 {
880 iwn_prph_write(sc, addr, iwn_prph_read(sc, addr) & ~mask);
881 }
882
883 static __inline void
884 iwn_prph_write_region_4(struct iwn_softc *sc, uint32_t addr,
885 const uint32_t *data, int count)
886 {
887 for (; count > 0; count--, data++, addr += 4)
888 iwn_prph_write(sc, addr, *data);
889 }
890
891 static __inline uint32_t
892 iwn_mem_read(struct iwn_softc *sc, uint32_t addr)
893 {
894 IWN_WRITE(sc, IWN_MEM_RADDR, addr);
895 return IWN_READ(sc, IWN_MEM_RDATA);
896 }
897
898 static __inline void
899 iwn_mem_write(struct iwn_softc *sc, uint32_t addr, uint32_t data)
900 {
901 IWN_WRITE(sc, IWN_MEM_WADDR, addr);
902 IWN_WRITE(sc, IWN_MEM_WDATA, data);
903 }
904
905 static __inline void
906 iwn_mem_write_2(struct iwn_softc *sc, uint32_t addr, uint16_t data)
907 {
908 uint32_t tmp;
909
910 tmp = iwn_mem_read(sc, addr & ~3);
911 if (addr & 3)
912 tmp = (tmp & 0x0000ffff) | data << 16;
913 else
914 tmp = (tmp & 0xffff0000) | data;
915 iwn_mem_write(sc, addr & ~3, tmp);
916 }
917
918 static __inline void
919 iwn_mem_read_region_4(struct iwn_softc *sc, uint32_t addr, uint32_t *data,
920 int count)
921 {
922 for (; count > 0; count--, addr += 4)
923 *data++ = iwn_mem_read(sc, addr);
924 }
925
926 static __inline void
927 iwn_mem_set_region_4(struct iwn_softc *sc, uint32_t addr, uint32_t val,
928 int count)
929 {
930 for (; count > 0; count--, addr += 4)
931 iwn_mem_write(sc, addr, val);
932 }
933
934 static int
935 iwn_eeprom_lock(struct iwn_softc *sc)
936 {
937 int i, ntries;
938
939 for (i = 0; i < 100; i++) {
940 /* Request exclusive access to EEPROM. */
941 IWN_SETBITS(sc, IWN_HW_IF_CONFIG,
942 IWN_HW_IF_CONFIG_EEPROM_LOCKED);
943
944 /* Spin until we actually get the lock. */
945 for (ntries = 0; ntries < 100; ntries++) {
946 if (IWN_READ(sc, IWN_HW_IF_CONFIG) &
947 IWN_HW_IF_CONFIG_EEPROM_LOCKED)
948 return 0;
949 DELAY(10);
950 }
951 }
952 return ETIMEDOUT;
953 }
954
955 static __inline void
956 iwn_eeprom_unlock(struct iwn_softc *sc)
957 {
958 IWN_CLRBITS(sc, IWN_HW_IF_CONFIG, IWN_HW_IF_CONFIG_EEPROM_LOCKED);
959 }
960
961 static int
962 iwn_read_prom_data(struct iwn_softc *sc, uint32_t addr, void *data, int count)
963 {
964 uint8_t *out = data;
965 uint32_t val;
966 int ntries;
967
968 for (; count > 0; count -= 2, addr++) {
969 IWN_WRITE(sc, IWN_EEPROM, addr << 2);
970 IWN_CLRBITS(sc, IWN_EEPROM, IWN_EEPROM_CMD);
971
972 for (ntries = 0; ntries < 10; ntries++) {
973 val = IWN_READ(sc, IWN_EEPROM);
974 if (val & IWN_EEPROM_READ_VALID)
975 break;
976 DELAY(5);
977 }
978 if (ntries == 10) {
979 aprint_error_dev(sc->sc_dev, "could not read EEPROM\n");
980 return ETIMEDOUT;
981 }
982 *out++ = val >> 16;
983 if (count > 1)
984 *out++ = val >> 24;
985 }
986 return 0;
987 }
988
989 static int
990 iwn_dma_contig_alloc(bus_dma_tag_t tag, struct iwn_dma_info *dma, void **kvap,
991 bus_size_t size, bus_size_t alignment, int flags)
992 {
993 int nsegs, error;
994
995 dma->tag = tag;
996 dma->size = size;
997
998 error = bus_dmamap_create(tag, size, 1, size, 0, flags, &dma->map);
999 if (error != 0)
1000 goto fail;
1001
1002 error = bus_dmamem_alloc(tag, size, alignment, 0, &dma->seg, 1, &nsegs,
1003 flags);
1004 if (error != 0)
1005 goto fail;
1006
1007 error = bus_dmamem_map(tag, &dma->seg, 1, size, &dma->vaddr, flags);
1008 if (error != 0)
1009 goto fail;
1010
1011 error = bus_dmamap_load(tag, dma->map, dma->vaddr, size, NULL, flags);
1012 if (error != 0)
1013 goto fail;
1014
1015 memset(dma->vaddr, 0, size);
1016 bus_dmamap_sync(tag, dma->map, 0, size, BUS_DMASYNC_PREWRITE);
1017
1018 dma->paddr = dma->map->dm_segs[0].ds_addr;
1019 if (kvap != NULL)
1020 *kvap = dma->vaddr;
1021
1022 return 0;
1023
1024 fail: iwn_dma_contig_free(dma);
1025 return error;
1026 }
1027
1028 static void
1029 iwn_dma_contig_free(struct iwn_dma_info *dma)
1030 {
1031 if (dma->map != NULL) {
1032 if (dma->vaddr != NULL) {
1033 bus_dmamap_sync(dma->tag, dma->map, 0, dma->size,
1034 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1035 bus_dmamap_unload(dma->tag, dma->map);
1036 bus_dmamem_unmap(dma->tag, dma->vaddr, dma->size);
1037 bus_dmamem_free(dma->tag, &dma->seg, 1);
1038 dma->vaddr = NULL;
1039 }
1040 bus_dmamap_destroy(dma->tag, dma->map);
1041 dma->map = NULL;
1042 }
1043 }
1044
1045 static int
1046 iwn_alloc_sched(struct iwn_softc *sc)
1047 {
1048 int error;
1049 /* TX scheduler rings must be aligned on a 1KB boundary. */
1050 error = iwn_dma_contig_alloc(sc->sc_dmat, &sc->sched_dma,
1051 (void **)&sc->sched, sc->sc_hal->schedsz, 1024, BUS_DMA_NOWAIT);
1052 if (error != 0)
1053 aprint_error_dev(sc->sc_dev,
1054 "could not allocate shared area DMA memory\n");
1055 return error;
1056 }
1057
1058 static void
1059 iwn_free_sched(struct iwn_softc *sc)
1060 {
1061 iwn_dma_contig_free(&sc->sched_dma);
1062 }
1063
1064 static int
1065 iwn_alloc_kw(struct iwn_softc *sc)
1066 {
1067 /* "Keep Warm" page must be aligned on a 16-byte boundary. */
1068 return iwn_dma_contig_alloc(sc->sc_dmat, &sc->kw_dma, NULL, 4096,
1069 4096, BUS_DMA_NOWAIT);
1070 }
1071
1072 static void
1073 iwn_free_kw(struct iwn_softc *sc)
1074 {
1075 iwn_dma_contig_free(&sc->kw_dma);
1076 }
1077
1078 static int
1079 iwn_alloc_fwmem(struct iwn_softc *sc)
1080 {
1081 int error;
1082 /* Must be aligned on a 16-byte boundary. */
1083 error = iwn_dma_contig_alloc(sc->sc_dmat, &sc->fw_dma, NULL,
1084 sc->sc_hal->fwsz, 16, BUS_DMA_NOWAIT);
1085 if (error != 0) {
1086 aprint_error_dev(sc->sc_dev,
1087 "could not allocate firmware transfer area DMA memory\n");
1088 }
1089 return error;
1090 }
1091
1092 static void
1093 iwn_free_fwmem(struct iwn_softc *sc)
1094 {
1095 iwn_dma_contig_free(&sc->fw_dma);
1096 }
1097
1098 static struct iwn_rbuf *
1099 iwn_alloc_rbuf(struct iwn_softc *sc)
1100 {
1101 struct iwn_rbuf *rbuf;
1102 mutex_enter(&sc->rxq.freelist_mtx);
1103
1104 rbuf = SLIST_FIRST(&sc->rxq.freelist);
1105 if (rbuf != NULL) {
1106 SLIST_REMOVE_HEAD(&sc->rxq.freelist, next);
1107 sc->rxq.nb_free_entries --;
1108 }
1109 mutex_exit(&sc->rxq.freelist_mtx);
1110 return rbuf;
1111 }
1112
1113 /*
1114 * This is called automatically by the network stack when the mbuf to which
1115 * our RX buffer is attached is freed.
1116 */
1117 static void
1118 iwn_free_rbuf(struct mbuf* m, void *buf, size_t size, void *arg)
1119 {
1120 struct iwn_rbuf *rbuf = arg;
1121 struct iwn_softc *sc = rbuf->sc;
1122
1123 /* Put the RX buffer back in the free list. */
1124 mutex_enter(&sc->rxq.freelist_mtx);
1125 SLIST_INSERT_HEAD(&sc->rxq.freelist, rbuf, next);
1126 mutex_exit(&sc->rxq.freelist_mtx);
1127
1128 sc->rxq.nb_free_entries ++;
1129 if (__predict_true(m != NULL))
1130 pool_cache_put(mb_cache, m);
1131 }
1132
1133 static int
1134 iwn_alloc_rpool(struct iwn_softc *sc)
1135 {
1136 struct iwn_rx_ring *ring = &sc->rxq;
1137 struct iwn_rbuf *rbuf;
1138 int i, error;
1139
1140 mutex_init(&ring->freelist_mtx, MUTEX_DEFAULT, IPL_NET);
1141
1142 /* Allocate a big chunk of DMA'able memory... */
1143 error = iwn_dma_contig_alloc(sc->sc_dmat, &ring->buf_dma, NULL,
1144 IWN_RBUF_COUNT * IWN_RBUF_SIZE, PAGE_SIZE, BUS_DMA_NOWAIT);
1145 if (error != 0) {
1146 aprint_error_dev(sc->sc_dev,
1147 "could not allocate RX buffers DMA memory\n");
1148 return error;
1149 }
1150 /* ...and split it into chunks of IWN_RBUF_SIZE bytes. */
1151 SLIST_INIT(&ring->freelist);
1152 for (i = 0; i < IWN_RBUF_COUNT; i++) {
1153 rbuf = &ring->rbuf[i];
1154
1155 rbuf->sc = sc; /* Backpointer for callbacks. */
1156 rbuf->vaddr = (void *)((vaddr_t)ring->buf_dma.vaddr + i * IWN_RBUF_SIZE);
1157 rbuf->paddr = ring->buf_dma.paddr + i * IWN_RBUF_SIZE;
1158
1159 SLIST_INSERT_HEAD(&ring->freelist, rbuf, next);
1160 }
1161 ring->nb_free_entries = IWN_RBUF_COUNT;
1162 return 0;
1163 }
1164
1165 static void
1166 iwn_free_rpool(struct iwn_softc *sc)
1167 {
1168 iwn_dma_contig_free(&sc->rxq.buf_dma);
1169 }
1170
1171 static int
1172 iwn_alloc_rx_ring(struct iwn_softc *sc, struct iwn_rx_ring *ring)
1173 {
1174 struct iwn_rx_data *data;
1175 bus_size_t size;
1176 struct iwn_rbuf *rbuf;
1177 int i, error;
1178
1179 ring->cur = 0;
1180
1181 /* Allocate RX descriptors (256-byte aligned.) */
1182 size = IWN_RX_RING_COUNT * sizeof (struct iwn_rx_desc);
1183 error = iwn_dma_contig_alloc(sc->sc_dmat, &ring->desc_dma,
1184 (void **)&ring->desc, size, 256, BUS_DMA_NOWAIT);
1185 if (error != 0) {
1186 aprint_error_dev(sc->sc_dev,
1187 "could not allocate RX ring DMA memory\n");
1188 goto fail;
1189 }
1190
1191 /* Allocate RX status area (16-byte aligned.) */
1192 error = iwn_dma_contig_alloc(sc->sc_dmat, &ring->stat_dma,
1193 (void **)&ring->stat, sizeof (struct iwn_rx_status), 16,
1194 BUS_DMA_NOWAIT);
1195 if (error != 0) {
1196 aprint_error_dev(sc->sc_dev,
1197 "could not allocate RX status DMA memory\n");
1198 goto fail;
1199 }
1200
1201 /*
1202 * Allocate and map RX buffers.
1203 */
1204 for (i = 0; i < IWN_RX_RING_COUNT; i++) {
1205 data = &ring->data[i];
1206
1207 error = bus_dmamap_create(sc->sc_dmat, IWN_RBUF_SIZE, 1,
1208 IWN_RBUF_SIZE, 0, BUS_DMA_NOWAIT, &data->map);
1209 if (error != 0) {
1210 aprint_error_dev(sc->sc_dev,
1211 "could not create RX buf DMA map\n");
1212 goto fail;
1213 }
1214 MGETHDR(data->m, M_DONTWAIT, MT_DATA);
1215 if (data->m == NULL) {
1216 aprint_error_dev(sc->sc_dev,
1217 "could not allocate RX mbuf\n");
1218 error = ENOMEM;
1219 goto fail;
1220 }
1221 if ((rbuf = iwn_alloc_rbuf(sc)) == NULL) {
1222 m_freem(data->m);
1223 data->m = NULL;
1224 aprint_error_dev(sc->sc_dev,
1225 "could not allocate RX buffer\n");
1226 error = ENOMEM;
1227 goto fail;
1228 }
1229 /* Attach RX buffer to mbuf header. */
1230 MEXTADD(data->m, rbuf->vaddr, IWN_RBUF_SIZE, 0, iwn_free_rbuf,
1231 rbuf);
1232 data->m->m_flags |= M_EXT_RW;
1233 error = bus_dmamap_load(sc->sc_dmat, data->map,
1234 data->m->m_ext.ext_buf, IWN_RBUF_SIZE, NULL,
1235 BUS_DMA_NOWAIT);
1236 if (error != 0) {
1237 aprint_error_dev(sc->sc_dev, "can't not map mbuf"
1238 " (error %d)\n", error);
1239 goto fail;
1240 }
1241
1242 /* Set physical address of RX buffer (256-byte aligned.) */
1243 ring->desc[i] = htole32(data->map->dm_segs[0].ds_addr >> 8);
1244 }
1245
1246 bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map,
1247 0, ring->desc_dma.size, BUS_DMASYNC_PREWRITE);
1248
1249 return 0;
1250
1251 fail: iwn_free_rx_ring(sc, ring);
1252 return error;
1253 }
1254
1255 static void
1256 iwn_reset_rx_ring(struct iwn_softc *sc, struct iwn_rx_ring *ring)
1257 {
1258 int ntries;
1259
1260 if (iwn_nic_lock(sc) == 0) {
1261 IWN_WRITE(sc, IWN_FH_RX_CONFIG, 0);
1262 for (ntries = 0; ntries < 1000; ntries++) {
1263 if (IWN_READ(sc, IWN_FH_RX_STATUS) &
1264 IWN_FH_RX_STATUS_IDLE)
1265 break;
1266 DELAY(10);
1267 }
1268 iwn_nic_unlock(sc);
1269 }
1270 ring->cur = 0;
1271 sc->last_rx_valid = 0;
1272 }
1273
1274 static void
1275 iwn_free_rx_ring(struct iwn_softc *sc, struct iwn_rx_ring *ring)
1276 {
1277 int i;
1278
1279 iwn_dma_contig_free(&ring->desc_dma);
1280 iwn_dma_contig_free(&ring->stat_dma);
1281
1282 for (i = 0; i < IWN_RX_RING_COUNT; i++) {
1283 struct iwn_rx_data *data = &ring->data[i];
1284
1285 if (data->m != NULL) {
1286 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
1287 data->map->dm_mapsize, BUS_DMASYNC_POSTREAD);
1288 bus_dmamap_unload(sc->sc_dmat, data->map);
1289 m_freem(data->m);
1290 }
1291 if (data->map != NULL)
1292 bus_dmamap_destroy(sc->sc_dmat, data->map);
1293 }
1294 }
1295
1296 static int
1297 iwn_alloc_tx_ring(struct iwn_softc *sc, struct iwn_tx_ring *ring, int count,
1298 int qid)
1299 {
1300 bus_addr_t paddr;
1301 struct iwn_tx_data *data;
1302 int i, error, size;
1303
1304 ring->qid = qid;
1305 ring->count = count;
1306 ring->queued = 0;
1307 ring->cur = 0;
1308
1309 /* Allocate TX descriptors (256-byte aligned.) */
1310 size = count * sizeof (struct iwn_tx_desc);
1311 error = iwn_dma_contig_alloc(sc->sc_dmat, &ring->desc_dma,
1312 (void **)&ring->desc, size, 256, BUS_DMA_NOWAIT);
1313 if (error != 0) {
1314 aprint_error_dev(sc->sc_dev,
1315 "could not allocate TX ring DMA memory\n");
1316 goto fail;
1317 }
1318 /*
1319 * We only use rings 0 through 4 (4 EDCA + cmd) so there is no need
1320 * to allocate commands space for other rings.
1321 * XXX Do we really need to allocate descriptors for other rings?
1322 */
1323 if (qid > 4)
1324 return 0;
1325
1326 size = count * sizeof (struct iwn_tx_cmd);
1327 error = iwn_dma_contig_alloc(sc->sc_dmat, &ring->cmd_dma,
1328 (void **)&ring->cmd, size, 4, BUS_DMA_NOWAIT);
1329 if (error != 0) {
1330 aprint_error_dev(sc->sc_dev,
1331 "could not allocate TX cmd DMA memory\n");
1332 goto fail;
1333 }
1334
1335 paddr = ring->cmd_dma.paddr;
1336
1337 for (i = 0; i < count; i++) {
1338 data = &ring->data[i];
1339
1340 data->cmd_paddr = paddr;
1341 data->scratch_paddr = paddr + 12;
1342 paddr += sizeof (struct iwn_tx_cmd);
1343
1344 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
1345 IWN_MAX_SCATTER - 1, MCLBYTES, 0, BUS_DMA_NOWAIT,
1346 &data->map);
1347 if (error != 0) {
1348 aprint_error_dev(sc->sc_dev,
1349 "could not create TX buf DMA map\n");
1350 goto fail;
1351 }
1352 }
1353 return 0;
1354
1355 fail: iwn_free_tx_ring(sc, ring);
1356 return error;
1357 }
1358
1359 static void
1360 iwn_reset_tx_ring(struct iwn_softc *sc, struct iwn_tx_ring *ring)
1361 {
1362 struct iwn_tx_data *data;
1363 uint32_t tmp;
1364 int i, ntries;
1365
1366 if (iwn_nic_lock(sc) == 0) {
1367 IWN_WRITE(sc, IWN_FH_TX_CONFIG(ring->qid), 0);
1368 for (ntries = 0; ntries < 200; ntries++) {
1369 tmp = IWN_READ(sc, IWN_FH_TX_STATUS);
1370 if ((tmp & IWN_FH_TX_STATUS_IDLE(ring->qid)) ==
1371 IWN_FH_TX_STATUS_IDLE(ring->qid))
1372 break;
1373 DELAY(10);
1374 }
1375 iwn_nic_unlock(sc);
1376 }
1377 for (i = 0; i < ring->count; i++) {
1378 data = &ring->data[i];
1379
1380 if (data->m != NULL) {
1381 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
1382 data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1383 bus_dmamap_unload(sc->sc_dmat, data->map);
1384 m_freem(data->m);
1385 data->m = NULL;
1386 }
1387 }
1388 /* Clear TX descriptors. */
1389 memset(ring->desc, 0, ring->desc_dma.size);
1390 bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map, 0,
1391 ring->desc_dma.size, BUS_DMASYNC_PREWRITE);
1392 sc->qfullmsk &= ~(1 << ring->qid);
1393 ring->queued = 0;
1394 ring->cur = 0;
1395 }
1396
1397 static void
1398 iwn_free_tx_ring(struct iwn_softc *sc, struct iwn_tx_ring *ring)
1399 {
1400 struct iwn_tx_data *data;
1401 int i;
1402
1403 iwn_dma_contig_free(&ring->desc_dma);
1404 iwn_dma_contig_free(&ring->cmd_dma);
1405
1406 if (ring->data != NULL) {
1407 for (i = 0; i < ring->count; i++) {
1408 data = &ring->data[i];
1409
1410 if (data->m != NULL) {
1411 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
1412 data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1413 bus_dmamap_unload(sc->sc_dmat, data->map);
1414 m_freem(data->m);
1415 }
1416 if (data->map != NULL)
1417 bus_dmamap_destroy(sc->sc_dmat, data->map);
1418 }
1419 free(ring->data, M_DEVBUF);
1420 }
1421 }
1422
1423 static int
1424 iwn_read_eeprom(struct iwn_softc *sc)
1425 {
1426 const struct iwn_hal *hal = sc->sc_hal;
1427 struct ieee80211com *ic = &sc->sc_ic;
1428 uint16_t val;
1429 int error;
1430
1431 if ((IWN_READ(sc, IWN_EEPROM_GP) & 0x6) == 0) {
1432 aprint_error_dev(sc->sc_dev, "bad EEPROM signature\n");
1433 return EIO;
1434 }
1435 if ((error = iwn_eeprom_lock(sc)) != 0) {
1436 aprint_error_dev(sc->sc_dev,
1437 "could not lock EEPROM (error=%d)\n", error);
1438 return error;
1439 }
1440
1441 iwn_read_prom_data(sc, IWN_EEPROM_RFCFG, &val, 2);
1442 sc->rfcfg = le16toh(val);
1443 DPRINTF(("radio config=0x%04x\n", sc->rfcfg));
1444
1445 /* Read MAC address. */
1446 iwn_read_prom_data(sc, IWN_EEPROM_MAC, ic->ic_myaddr, 6);
1447
1448 /* Read adapter-specific information from EEPROM. */
1449 hal->read_eeprom(sc);
1450
1451 iwn_eeprom_unlock(sc);
1452 return 0;
1453 }
1454
1455 static void
1456 iwn4965_read_eeprom(struct iwn_softc *sc)
1457 {
1458 uint32_t addr;
1459 uint16_t val;
1460 int i;
1461
1462 /* Read regulatory domain (4 ASCII characters.) */
1463 iwn_read_prom_data(sc, IWN4965_EEPROM_DOMAIN, sc->eeprom_domain, 4);
1464
1465 /* Read the list of authorized channels (20MHz ones only.) */
1466 for (i = 0; i < 5; i++) {
1467 addr = iwn4965_regulatory_bands[i];
1468 iwn_read_eeprom_channels(sc, i, addr);
1469 }
1470
1471 /* Read maximum allowed TX power for 2GHz and 5GHz bands. */
1472 iwn_read_prom_data(sc, IWN4965_EEPROM_MAXPOW, &val, 2);
1473 sc->maxpwr2GHz = val & 0xff;
1474 sc->maxpwr5GHz = val >> 8;
1475 /* Check that EEPROM values are within valid range. */
1476 if (sc->maxpwr5GHz < 20 || sc->maxpwr5GHz > 50)
1477 sc->maxpwr5GHz = 38;
1478 if (sc->maxpwr2GHz < 20 || sc->maxpwr2GHz > 50)
1479 sc->maxpwr2GHz = 38;
1480 DPRINTF(("maxpwr 2GHz=%d 5GHz=%d\n", sc->maxpwr2GHz, sc->maxpwr5GHz));
1481
1482 /* Read samples for each TX power group. */
1483 iwn_read_prom_data(sc, IWN4965_EEPROM_BANDS, sc->bands,
1484 sizeof sc->bands);
1485
1486 /* Read voltage at which samples were taken. */
1487 iwn_read_prom_data(sc, IWN4965_EEPROM_VOLTAGE, &val, 2);
1488 sc->eeprom_voltage = (int16_t)le16toh(val);
1489 DPRINTF(("voltage=%d (in 0.3V)\n", sc->eeprom_voltage));
1490
1491 #ifdef IWN_DEBUG
1492 /* Print samples. */
1493 if (iwn_debug > 0) {
1494 for (i = 0; i < IWN_NBANDS; i++)
1495 iwn4965_print_power_group(sc, i);
1496 }
1497 #endif
1498 }
1499
1500 #ifdef IWN_DEBUG
1501 static void
1502 iwn4965_print_power_group(struct iwn_softc *sc, int i)
1503 {
1504 struct iwn4965_eeprom_band *band = &sc->bands[i];
1505 struct iwn4965_eeprom_chan_samples *chans = band->chans;
1506 int j, c;
1507
1508 printf("===band %d===\n", i);
1509 printf("chan lo=%d, chan hi=%d\n", band->lo, band->hi);
1510 printf("chan1 num=%d\n", chans[0].num);
1511 for (c = 0; c < 2; c++) {
1512 for (j = 0; j < IWN_NSAMPLES; j++) {
1513 printf("chain %d, sample %d: temp=%d gain=%d "
1514 "power=%d pa_det=%d\n", c, j,
1515 chans[0].samples[c][j].temp,
1516 chans[0].samples[c][j].gain,
1517 chans[0].samples[c][j].power,
1518 chans[0].samples[c][j].pa_det);
1519 }
1520 }
1521 printf("chan2 num=%d\n", chans[1].num);
1522 for (c = 0; c < 2; c++) {
1523 for (j = 0; j < IWN_NSAMPLES; j++) {
1524 printf("chain %d, sample %d: temp=%d gain=%d "
1525 "power=%d pa_det=%d\n", c, j,
1526 chans[1].samples[c][j].temp,
1527 chans[1].samples[c][j].gain,
1528 chans[1].samples[c][j].power,
1529 chans[1].samples[c][j].pa_det);
1530 }
1531 }
1532 }
1533 #endif
1534
1535 static void
1536 iwn5000_read_eeprom(struct iwn_softc *sc)
1537 {
1538 int32_t temp, volt, delta;
1539 uint32_t base, addr;
1540 uint16_t val;
1541 int i;
1542
1543 /* Read regulatory domain (4 ASCII characters.) */
1544 iwn_read_prom_data(sc, IWN5000_EEPROM_REG, &val, 2);
1545 base = le16toh(val);
1546 iwn_read_prom_data(sc, base + IWN5000_EEPROM_DOMAIN,
1547 sc->eeprom_domain, 4);
1548
1549 /* Read the list of authorized channels (20MHz ones only.) */
1550 for (i = 0; i < 5; i++) {
1551 addr = base + iwn5000_regulatory_bands[i];
1552 iwn_read_eeprom_channels(sc, i, addr);
1553 }
1554
1555 iwn_read_prom_data(sc, IWN5000_EEPROM_CAL, &val, 2);
1556 base = le16toh(val);
1557 if (sc->hw_type == IWN_HW_REV_TYPE_5150) {
1558 /* Compute critical temperature (in Kelvin.) */
1559 iwn_read_prom_data(sc, base + IWN5000_EEPROM_TEMP, &val, 2);
1560 temp = le16toh(val);
1561 iwn_read_prom_data(sc, base + IWN5000_EEPROM_VOLT, &val, 2);
1562 volt = le16toh(val);
1563 delta = temp - (volt / -5);
1564 sc->critical_temp = (IWN_CTOK(110) - delta) * -5;
1565 DPRINTF(("temp=%d volt=%d delta=%dK\n",
1566 temp, volt, delta));
1567 } else {
1568 /* Read crystal calibration. */
1569 iwn_read_prom_data(sc, base + IWN5000_EEPROM_CRYSTAL,
1570 &sc->eeprom_crystal, sizeof (uint32_t));
1571 DPRINTF(("crystal calibration 0x%08x\n",
1572 le32toh(sc->eeprom_crystal)));
1573 }
1574 }
1575
1576 static void
1577 iwn_read_eeprom_channels(struct iwn_softc *sc, int n, uint32_t addr)
1578 {
1579 struct ieee80211com *ic = &sc->sc_ic;
1580 const struct iwn_chan_band *band = &iwn_bands[n];
1581 struct iwn_eeprom_chan channels[IWN_MAX_CHAN_PER_BAND];
1582 uint8_t chan;
1583 int i;
1584
1585 iwn_read_prom_data(sc, addr, channels,
1586 band->nchan * sizeof (struct iwn_eeprom_chan));
1587
1588 for (i = 0; i < band->nchan; i++) {
1589 if (!(channels[i].flags & IWN_EEPROM_CHAN_VALID))
1590 continue;
1591
1592 chan = band->chan[i];
1593
1594 if (n == 0) { /* 2GHz band */
1595 ic->ic_channels[chan].ic_freq =
1596 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ);
1597 ic->ic_channels[chan].ic_flags =
1598 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
1599 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
1600
1601 } else { /* 5GHz band */
1602 /*
1603 * Some adapters support channels 7, 8, 11 and 12
1604 * both in the 2GHz and 4.9GHz bands.
1605 * Because of limitations in our net80211 layer,
1606 * we don't support them in the 4.9GHz band.
1607 */
1608 if (chan <= 14)
1609 continue;
1610
1611 ic->ic_channels[chan].ic_freq =
1612 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_5GHZ);
1613 ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_A;
1614 /* We have at least one valid 5GHz channel. */
1615 sc->sc_flags |= IWN_FLAG_HAS_5GHZ;
1616 }
1617
1618 /* Is active scan allowed on this channel? */
1619 if (!(channels[i].flags & IWN_EEPROM_CHAN_ACTIVE)) {
1620 ic->ic_channels[chan].ic_flags |=
1621 IEEE80211_CHAN_PASSIVE;
1622 }
1623
1624 /* Save maximum allowed TX power for this channel. */
1625 sc->maxpwr[chan] = channels[i].maxpwr;
1626
1627 DPRINTF(("adding chan %d flags=0x%x maxpwr=%d\n",
1628 chan, channels[i].flags, sc->maxpwr[chan]));
1629 }
1630 }
1631
1632 /*ARGUSED*/
1633 static struct ieee80211_node *
1634 iwn_node_alloc(struct ieee80211_node_table *nt __unused)
1635 {
1636 struct iwn_node *wn;
1637
1638 wn = malloc(sizeof (struct iwn_node), M_80211_NODE, M_NOWAIT | M_ZERO);
1639 return (struct ieee80211_node *)wn;
1640 }
1641
1642 static void
1643 iwn_newassoc(struct ieee80211_node *ni, int isnew)
1644 {
1645 struct iwn_softc *sc = ni->ni_ic->ic_ifp->if_softc;
1646 struct iwn_node *wn = (void *)ni;
1647 uint8_t rate;
1648 int ridx, i;
1649
1650 ieee80211_amrr_node_init(&sc->amrr, &wn->amn);
1651
1652 for (i = 0; i < ni->ni_rates.rs_nrates; i++) {
1653 rate = ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL;
1654 /* Map 802.11 rate to HW rate index. */
1655 for (ridx = 0; ridx <= IWN_RIDX_MAX; ridx++)
1656 if (iwn_rates[ridx].rate == rate)
1657 break;
1658 wn->ridx[i] = ridx;
1659 /* Initial TX rate <= 24Mbps. */
1660 if (rate <= 48)
1661 ni->ni_txrate = i;
1662 }
1663 }
1664
1665 static int
1666 iwn_media_change(struct ifnet *ifp)
1667 {
1668 struct iwn_softc *sc = ifp->if_softc;
1669 struct ieee80211com *ic = &sc->sc_ic;
1670 uint8_t rate, ridx;
1671 int error;
1672
1673 error = ieee80211_media_change(ifp);
1674 if (error != ENETRESET)
1675 return error;
1676
1677 if (ic->ic_fixed_rate != -1) {
1678 rate = ic->ic_sup_rates[ic->ic_curmode].
1679 rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
1680 /* Map 802.11 rate to HW rate index. */
1681 for (ridx = 0; ridx <= IWN_RIDX_MAX; ridx++)
1682 if (iwn_rates[ridx].rate == rate)
1683 break;
1684 sc->fixed_ridx = ridx;
1685 }
1686
1687 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1688 (IFF_UP | IFF_RUNNING)) {
1689 iwn_stop(ifp, 0);
1690 error = iwn_init(ifp);
1691 }
1692 return error;
1693 }
1694
1695 static int
1696 iwn_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
1697 {
1698 struct ifnet *ifp = ic->ic_ifp;
1699 struct iwn_softc *sc = ifp->if_softc;
1700 int error;
1701
1702 callout_stop(&sc->calib_to);
1703
1704 switch (nstate) {
1705 case IEEE80211_S_SCAN:
1706 if (sc->is_scanning)
1707 break;
1708
1709 sc->is_scanning = true;
1710 ieee80211_node_table_reset(&ic->ic_scan);
1711 ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN;
1712
1713 /* Make the link LED blink while we're scanning. */
1714 iwn_set_led(sc, IWN_LED_LINK, 10, 10);
1715
1716 if ((error = iwn_scan(sc, IEEE80211_CHAN_2GHZ)) != 0) {
1717 aprint_error_dev(sc->sc_dev,
1718 "could not initiate scan\n");
1719 return error;
1720 }
1721 ic->ic_state = nstate;
1722 return 0;
1723
1724 case IEEE80211_S_ASSOC:
1725 if (ic->ic_state != IEEE80211_S_RUN)
1726 break;
1727 /* FALLTHROUGH */
1728 case IEEE80211_S_AUTH:
1729 /* Reset state to handle reassociations correctly. */
1730 sc->rxon.associd = 0;
1731 sc->rxon.filter &= ~htole32(IWN_FILTER_BSS);
1732 sc->calib.state = IWN_CALIB_STATE_INIT;
1733
1734 if ((error = iwn_auth(sc)) != 0) {
1735 aprint_error_dev(sc->sc_dev,
1736 "could not move to auth state\n");
1737 return error;
1738 }
1739 break;
1740
1741 case IEEE80211_S_RUN:
1742 if ((error = iwn_run(sc)) != 0) {
1743 aprint_error_dev(sc->sc_dev,
1744 "could not move to run state\n");
1745 return error;
1746 }
1747 break;
1748
1749 case IEEE80211_S_INIT:
1750 sc->is_scanning = false;
1751 sc->calib.state = IWN_CALIB_STATE_INIT;
1752 break;
1753 }
1754
1755 return sc->sc_newstate(ic, nstate, arg);
1756 }
1757
1758 static void
1759 iwn_iter_func(void *arg, struct ieee80211_node *ni)
1760 {
1761 struct iwn_softc *sc = arg;
1762 struct iwn_node *wn = (struct iwn_node *)ni;
1763
1764 ieee80211_amrr_choose(&sc->amrr, ni, &wn->amn);
1765 }
1766
1767 static void
1768 iwn_calib_timeout(void *arg)
1769 {
1770 struct iwn_softc *sc = arg;
1771 struct ieee80211com *ic = &sc->sc_ic;
1772 int s;
1773
1774 if (ic->ic_fixed_rate == -1) {
1775 s = splnet();
1776 if (ic->ic_opmode == IEEE80211_M_STA)
1777 iwn_iter_func(sc, ic->ic_bss);
1778 else
1779 ieee80211_iterate_nodes(&ic->ic_sta, iwn_iter_func, sc);
1780 splx(s);
1781 }
1782 /* Force automatic TX power calibration every 60 secs. */
1783 if (++sc->calib_cnt >= 120) {
1784 uint32_t flags = 0;
1785
1786 DPRINTF(("sending request for statistics\n"));
1787 (void)iwn_cmd(sc, IWN_CMD_GET_STATISTICS, &flags,
1788 sizeof flags, 1);
1789 sc->calib_cnt = 0;
1790 }
1791 /* Automatic rate control triggered every 500ms. */
1792 callout_schedule(&sc->calib_to, hz/2);
1793 }
1794
1795 #if 0
1796 static int
1797 iwn_ccmp_decap(struct iwn_softc *sc, struct mbuf *m, struct ieee80211_key *k)
1798 {
1799 struct ieee80211_frame *wh;
1800 uint64_t pn, *prsc;
1801 uint8_t *ivp;
1802 uint8_t tid;
1803 int hdrlen;
1804
1805 wh = mtod(m, struct ieee80211_frame *);
1806 hdrlen = ieee80211_get_hdrlen(wh);
1807 ivp = (uint8_t *)wh + hdrlen;
1808
1809 /* Check that ExtIV bit is be set. */
1810 if (!(ivp[3] & IEEE80211_WEP_EXTIV)) {
1811 DPRINTF(("CCMP decap ExtIV not set\n"));
1812 return 1;
1813 }
1814 tid = ieee80211_has_qos(wh) ?
1815 ieee80211_get_qos(wh) & IEEE80211_QOS_TID : 0;
1816 prsc = &k->k_rsc[tid];
1817
1818 /* Extract the 48-bit PN from the CCMP header. */
1819 pn = (uint64_t)ivp[0] |
1820 (uint64_t)ivp[1] << 8 |
1821 (uint64_t)ivp[4] << 16 |
1822 (uint64_t)ivp[5] << 24 |
1823 (uint64_t)ivp[6] << 32 |
1824 (uint64_t)ivp[7] << 40;
1825 if (pn <= *prsc) {
1826 /*
1827 * Not necessarily a replayed frame since we did not check
1828 * the sequence number of the 802.11 header yet.
1829 */
1830 DPRINTF(("CCMP replayed\n"));
1831 return 1;
1832 }
1833 /* Update last seen packet number. */
1834 *prsc = pn;
1835
1836 /* Clear Protected bit and strip IV. */
1837 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
1838 memmove(mtod(m, char *) + IEEE80211_CCMP_HDRLEN, wh, hdrlen);
1839 m_adj(m, IEEE80211_CCMP_HDRLEN);
1840 /* Strip MIC. */
1841 m_adj(m, -IEEE80211_CCMP_MICLEN);
1842 return 0;
1843 }
1844 #endif
1845
1846 /*
1847 * Process an RX_PHY firmware notification. This is usually immediately
1848 * followed by an MPDU_RX_DONE notification.
1849 */
1850 void
1851 iwn_rx_phy(struct iwn_softc *sc, struct iwn_rx_desc *desc)
1852 {
1853 struct iwn_rx_stat *stat = (struct iwn_rx_stat *)(desc + 1);
1854
1855 DPRINTFN(2, ("received PHY stats\n"));
1856 bus_dmamap_sync(sc->sc_dmat, sc->rxq.buf_dma.map,
1857 (vaddr_t)stat - (vaddr_t)sc->rxq.buf_dma.vaddr, sizeof (*stat),
1858 BUS_DMASYNC_POSTREAD);
1859
1860 /* Save RX statistics, they will be used on MPDU_RX_DONE. */
1861 memcpy(&sc->last_rx_stat, stat, sizeof (*stat));
1862 sc->last_rx_valid = 1;
1863 }
1864
1865 /*
1866 * Process an RX_DONE (4965AGN only) or MPDU_RX_DONE firmware notification.
1867 * Each MPDU_RX_DONE notification must be preceded by an RX_PHY one.
1868 */
1869 void
1870 iwn_rx_done(struct iwn_softc *sc, struct iwn_rx_desc *desc,
1871 struct iwn_rx_data *data)
1872 {
1873 const struct iwn_hal *hal = sc->sc_hal;
1874 struct ieee80211com *ic = &sc->sc_ic;
1875 struct ifnet *ifp = ic->ic_ifp;
1876 struct iwn_rx_ring *ring = &sc->rxq;
1877 struct iwn_rbuf *rbuf;
1878 struct ieee80211_frame *wh;
1879 struct ieee80211_node *ni;
1880 struct mbuf *m, *m1;
1881 struct iwn_rx_stat *stat;
1882 char * head;
1883 uint32_t flags;
1884 int len, rssi, error;
1885
1886 if (desc->type == IWN_MPDU_RX_DONE) {
1887 /* Check for prior RX_PHY notification. */
1888 if (!sc->last_rx_valid) {
1889 DPRINTF(("missing RX_PHY\n"));
1890 ifp->if_ierrors++;
1891 return;
1892 }
1893 sc->last_rx_valid = 0;
1894 stat = &sc->last_rx_stat;
1895 } else
1896 stat = (struct iwn_rx_stat *)(desc + 1);
1897
1898 bus_dmamap_sync(sc->sc_dmat, data->map, 0, IWN_RBUF_SIZE,
1899 BUS_DMASYNC_POSTREAD);
1900
1901 if (stat->cfg_phy_len > IWN_STAT_MAXLEN) {
1902 aprint_error_dev(sc->sc_dev, "invalid RX statistic header\n");
1903 ifp->if_ierrors++;
1904 return;
1905 }
1906 if (desc->type == IWN_MPDU_RX_DONE) {
1907 struct iwn_rx_mpdu *mpdu =
1908 (struct iwn_rx_mpdu *)(desc + 1);
1909 head = (char *)(mpdu + 1);
1910 len = le16toh(mpdu->len);
1911 } else {
1912 head = (char *)(stat + 1) + stat->cfg_phy_len;
1913 len = le16toh(stat->len);
1914 }
1915
1916 flags = le32toh(*(uint32_t *)(head + len));
1917
1918 /* Discard frames with a bad FCS early. */
1919 if ((flags & IWN_RX_NOERROR) != IWN_RX_NOERROR) {
1920 DPRINTFN(2, ("RX flags error %x\n", flags));
1921 ifp->if_ierrors++;
1922 return;
1923 }
1924 /* Discard frames that are too short. */
1925 if (len < sizeof (struct ieee80211_frame)) {
1926 DPRINTF(("frame too short: %d\n", len));
1927 ic->ic_stats.is_rx_tooshort++;
1928 ifp->if_ierrors++;
1929 return;
1930 }
1931
1932 /*
1933 * See comment in if_wpi.c:wpi_rx_intr() about locking
1934 * nb_free_entries here. In short: it's not required.
1935 */
1936 MGETHDR(m1, M_DONTWAIT, MT_DATA);
1937 if (m1 == NULL) {
1938 ic->ic_stats.is_rx_nobuf++;
1939 ifp->if_ierrors++;
1940 return;
1941 }
1942 if (sc->rxq.nb_free_entries <= 0) {
1943 ic->ic_stats.is_rx_nobuf++;
1944 ifp->if_ierrors++;
1945 m_freem(m1);
1946 return;
1947 }
1948 rbuf = iwn_alloc_rbuf(sc);
1949 /* Attach RX buffer to mbuf header. */
1950 MEXTADD(m1, rbuf->vaddr, IWN_RBUF_SIZE, 0, iwn_free_rbuf,
1951 rbuf);
1952 m1->m_flags |= M_EXT_RW;
1953 bus_dmamap_unload(sc->sc_dmat, data->map);
1954
1955 error = bus_dmamap_load(sc->sc_dmat, data->map, m1->m_ext.ext_buf,
1956 IWN_RBUF_SIZE, NULL, BUS_DMA_NOWAIT);
1957 if (error != 0) {
1958 m_freem(m1);
1959
1960 /* Try to reload the old mbuf. */
1961 error = bus_dmamap_load(sc->sc_dmat, data->map,
1962 data->m->m_ext.ext_buf, IWN_RBUF_SIZE, NULL,
1963 BUS_DMA_NOWAIT);
1964 if (error != 0) {
1965 panic("%s: could not load old RX mbuf",
1966 device_xname(sc->sc_dev));
1967 }
1968 /* Physical address may have changed. */
1969 ring->desc[ring->cur] =
1970 htole32(data->map->dm_segs[0].ds_addr >> 8);
1971 bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map,
1972 ring->cur * sizeof (uint32_t), sizeof (uint32_t),
1973 BUS_DMASYNC_PREWRITE);
1974 ifp->if_ierrors++;
1975 return;
1976 }
1977 m = data->m;
1978 data->m = m1;
1979
1980 /* Update RX descriptor. */
1981 ring->desc[ring->cur] = htole32(data->map->dm_segs[0].ds_addr >> 8);
1982 bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map,
1983 ring->cur * sizeof (uint32_t), sizeof (uint32_t),
1984 BUS_DMASYNC_PREWRITE);
1985
1986 /* Finalize mbuf. */
1987 m->m_pkthdr.rcvif = ifp;
1988 m->m_data = head;
1989 m->m_pkthdr.len = m->m_len = len;
1990
1991 /* Grab a reference to the source node. */
1992 wh = mtod(m, struct ieee80211_frame *);
1993 ni = ieee80211_find_rxnode(ic,(struct ieee80211_frame_min *)wh);
1994
1995 #if 0
1996 rxi.rxi_flags = 0;
1997 if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
1998 !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1999 (ni->ni_flags & IEEE80211_NODE_RXPROT) &&
2000 ni->ni_pairwise_key.k_cipher == IEEE80211_CIPHER_CCMP) {
2001 if ((flags & IWN_RX_CIPHER_MASK) != IWN_RX_CIPHER_CCMP) {
2002 ic->ic_stats.is_ccmp_dec_errs++;
2003 ifp->if_ierrors++;
2004 return;
2005 }
2006 /* Check whether decryption was successful or not. */
2007 if ((desc->type == IWN_MPDU_RX_DONE &&
2008 (flags & (IWN_RX_MPDU_DEC | IWN_RX_MPDU_MIC_OK)) !=
2009 (IWN_RX_MPDU_DEC | IWN_RX_MPDU_MIC_OK)) ||
2010 (desc->type != IWN_MPDU_RX_DONE &&
2011 (flags & IWN_RX_DECRYPT_MASK) != IWN_RX_DECRYPT_OK)) {
2012 DPRINTF(("CCMP decryption failed 0x%x\n", flags));
2013 ic->ic_stats.is_ccmp_dec_errs++;
2014 ifp->if_ierrors++;
2015 return;
2016 }
2017 if (iwn_ccmp_decap(sc, m, &ni->ni_pairwise_key) != 0) {
2018 ifp->if_ierrors++;
2019 return;
2020 }
2021 rxi.rxi_flags |= IEEE80211_RXI_HWDEC;
2022 }
2023 #endif
2024
2025 rssi = hal->get_rssi(stat);
2026 if (ic->ic_state == IEEE80211_S_SCAN)
2027 iwn_fix_channel(ic, m);
2028
2029 if (sc->sc_drvbpf != NULL) {
2030 struct iwn_rx_radiotap_header *tap = &sc->sc_rxtap;
2031
2032 tap->wr_flags = 0;
2033 if (stat->flags & htole16(IWN_STAT_FLAG_SHPREAMBLE))
2034 tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2035 tap->wr_chan_freq =
2036 htole16(ic->ic_channels[stat->chan].ic_freq);
2037 tap->wr_chan_flags =
2038 htole16(ic->ic_channels[stat->chan].ic_flags);
2039 tap->wr_dbm_antsignal = (int8_t)rssi;
2040 tap->wr_dbm_antnoise = (int8_t)sc->noise;
2041 tap->wr_tsft = stat->tstamp;
2042 switch (stat->rate) {
2043 /* CCK rates. */
2044 case 10: tap->wr_rate = 2; break;
2045 case 20: tap->wr_rate = 4; break;
2046 case 55: tap->wr_rate = 11; break;
2047 case 110: tap->wr_rate = 22; break;
2048 /* OFDM rates. */
2049 case 0xd: tap->wr_rate = 12; break;
2050 case 0xf: tap->wr_rate = 18; break;
2051 case 0x5: tap->wr_rate = 24; break;
2052 case 0x7: tap->wr_rate = 36; break;
2053 case 0x9: tap->wr_rate = 48; break;
2054 case 0xb: tap->wr_rate = 72; break;
2055 case 0x1: tap->wr_rate = 96; break;
2056 case 0x3: tap->wr_rate = 108; break;
2057 /* Unknown rate: should not happen. */
2058 default: tap->wr_rate = 0;
2059 }
2060
2061 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
2062 }
2063
2064 /* Send the frame to the 802.11 layer. */
2065 ieee80211_input(ic, m, ni, rssi, 0);
2066
2067 /* Node is no longer needed. */
2068 ieee80211_free_node(ni);
2069 }
2070
2071 /*
2072 * Process a CALIBRATION_RESULT notification sent by the initialization
2073 * firmware on response to a CMD_CALIB_CONFIG command (5000 only.)
2074 */
2075 void
2076 iwn5000_rx_calib_results(struct iwn_softc *sc, struct iwn_rx_desc *desc,
2077 struct iwn_rx_data *data)
2078 {
2079 struct iwn_phy_calib *calib = (struct iwn_phy_calib *)(desc + 1);
2080 int len, idx = -1;
2081
2082 /* Runtime firmware should not send such a notification. */
2083 if (!(sc->sc_flags & IWN_FLAG_FIRST_BOOT))
2084 return;
2085
2086 len = (le32toh(desc->len) & 0x3fff) - 4;
2087 bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (*desc), len,
2088 BUS_DMASYNC_POSTREAD);
2089
2090 switch (calib->code) {
2091 case IWN5000_PHY_CALIB_DC:
2092 if (sc->hw_type == IWN_HW_REV_TYPE_5150)
2093 idx = 0;
2094 break;
2095 case IWN5000_PHY_CALIB_LO:
2096 idx = 1;
2097 break;
2098 case IWN5000_PHY_CALIB_TX_IQ:
2099 idx = 2;
2100 break;
2101 case IWN5000_PHY_CALIB_TX_IQ_PERD:
2102 if (sc->hw_type != IWN_HW_REV_TYPE_5150)
2103 idx = 3;
2104 break;
2105 case IWN5000_PHY_CALIB_BASE_BAND:
2106 idx = 4;
2107 break;
2108 }
2109 if (idx == -1) /* Ignore other results. */
2110 return;
2111
2112 /* Save calibration result. */
2113 if (sc->calibcmd[idx].buf != NULL)
2114 free(sc->calibcmd[idx].buf, M_DEVBUF);
2115 sc->calibcmd[idx].buf = malloc(len, M_DEVBUF, M_NOWAIT);
2116 if (sc->calibcmd[idx].buf == NULL) {
2117 DPRINTF(("not enough memory for calibration result %d\n",
2118 calib->code));
2119 return;
2120 }
2121 DPRINTF(("saving calibration result code=%d len=%d\n",
2122 calib->code, len));
2123 sc->calibcmd[idx].len = len;
2124 memcpy(sc->calibcmd[idx].buf, calib, len);
2125 }
2126
2127
2128 /*
2129 * XXX: Hack to set the current channel to the value advertised in beacons or
2130 * probe responses. Only used during AP detection.
2131 * XXX: Duplicated from if_iwi.c
2132 */
2133 static void
2134 iwn_fix_channel(struct ieee80211com *ic, struct mbuf *m)
2135 {
2136 struct ieee80211_frame *wh;
2137 uint8_t subtype;
2138 uint8_t *frm, *efrm;
2139
2140 wh = mtod(m, struct ieee80211_frame *);
2141
2142 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
2143 return;
2144
2145 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2146
2147 if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
2148 subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
2149 return;
2150
2151 frm = (uint8_t *)(wh + 1);
2152 efrm = mtod(m, uint8_t *) + m->m_len;
2153
2154 frm += 12; /* skip tstamp, bintval and capinfo fields */
2155 while (frm < efrm) {
2156 if (*frm == IEEE80211_ELEMID_DSPARMS)
2157 #if IEEE80211_CHAN_MAX < 255
2158 if (frm[2] <= IEEE80211_CHAN_MAX)
2159 #endif
2160 ic->ic_curchan = &ic->ic_channels[frm[2]];
2161
2162 frm += frm[1] + 2;
2163 }
2164 }
2165
2166
2167 /*
2168 * Process an RX_STATISTICS or BEACON_STATISTICS firmware notification.
2169 * The latter is sent by the firmware after each received beacon.
2170 */
2171 static void
2172 iwn_rx_statistics(struct iwn_softc *sc, struct iwn_rx_desc *desc,
2173 struct iwn_rx_data *data)
2174 {
2175 struct ieee80211com *ic = &sc->sc_ic;
2176 struct iwn_calib_state *calib = &sc->calib;
2177 struct iwn_stats *stats = (struct iwn_stats *)(desc + 1);
2178
2179 /* Ignore statistics received during a scan. */
2180 if (ic->ic_state != IEEE80211_S_RUN)
2181 return;
2182
2183 bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (*desc),
2184 sizeof (*stats), BUS_DMASYNC_POSTREAD);
2185
2186 DPRINTFN(3, ("received statistics (cmd=%d)\n", desc->type));
2187 sc->calib_cnt = 0; /* Reset TX power calibration timeout. */
2188
2189 #if 0
2190 /* Test if temperature has changed. */
2191 if (stats->general.temp != sc->rawtemp) {
2192 /* Convert "raw" temperature to degC. */
2193 sc->rawtemp = stats->general.temp;
2194 temp = hal->get_temperature(sc);
2195 DPRINTFN(2, ("temperature=%dC\n", temp));
2196
2197 /* Update temperature sensor. */
2198 sc->sensor.value = IWN_CTOMUK(temp);
2199 sc->sensor.flags &= ~SENSOR_FINVALID;
2200
2201 /* Update TX power if need be (4965AGN only.) */
2202 if (sc->hw_type == IWN_HW_REV_TYPE_4965)
2203 iwn4965_power_calibration(sc, temp);
2204 }
2205 #endif
2206
2207 if (desc->type != IWN_BEACON_STATISTICS)
2208 return; /* Reply to a statistics request. */
2209
2210 sc->noise = iwn_get_noise(&stats->rx.general);
2211
2212 /* Test that RSSI and noise are present in stats report. */
2213 if (le32toh(stats->rx.general.flags) != 1) {
2214 DPRINTF(("received statistics without RSSI\n"));
2215 return;
2216 }
2217
2218 if (calib->state == IWN_CALIB_STATE_ASSOC)
2219 iwn_collect_noise(sc, &stats->rx.general);
2220 else if (calib->state == IWN_CALIB_STATE_RUN)
2221 iwn_tune_sensitivity(sc, &stats->rx);
2222 }
2223
2224 /*
2225 * Process a TX_DONE firmware notification. Unfortunately, the 4965AGN
2226 * and 5000 adapters have different incompatible TX status formats.
2227 */
2228 static void
2229 iwn4965_tx_done(struct iwn_softc *sc, struct iwn_rx_desc *desc,
2230 struct iwn_rx_data *data)
2231 {
2232 struct iwn4965_tx_stat *stat = (struct iwn4965_tx_stat *)(desc + 1);
2233
2234 bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (*desc),
2235 sizeof (*stat), BUS_DMASYNC_POSTREAD);
2236 iwn_tx_done(sc, desc, stat->retrycnt, le32toh(stat->status) & 0xff);
2237 }
2238
2239 static void
2240 iwn5000_tx_done(struct iwn_softc *sc, struct iwn_rx_desc *desc,
2241 struct iwn_rx_data *data)
2242 {
2243 struct iwn5000_tx_stat *stat = (struct iwn5000_tx_stat *)(desc + 1);
2244
2245 /* Reset TX scheduler slot. */
2246 iwn5000_reset_sched(sc, desc->qid & 0xf, desc->idx);
2247
2248 bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (*desc),
2249 sizeof (*stat), BUS_DMASYNC_POSTREAD);
2250 iwn_tx_done(sc, desc, stat->retrycnt, le16toh(stat->status) & 0xff);
2251 }
2252
2253 /*
2254 * Adapter-independent backend for TX_DONE firmware notifications.
2255 */
2256 static void
2257 iwn_tx_done(struct iwn_softc *sc, struct iwn_rx_desc *desc, int retrycnt,
2258 uint8_t status)
2259 {
2260 struct ifnet *ifp = sc->sc_ic.ic_ifp;
2261 struct iwn_tx_ring *ring = &sc->txq[desc->qid & 0xf];
2262 struct iwn_tx_data *data = &ring->data[desc->idx];
2263 struct iwn_node *wn = (struct iwn_node *)data->ni;
2264
2265 /* Update rate control statistics. */
2266 wn->amn.amn_txcnt++;
2267 if (retrycnt > 0)
2268 wn->amn.amn_retrycnt++;
2269
2270 if (status != 1 && status != 2)
2271 ifp->if_oerrors++;
2272 else
2273 ifp->if_opackets++;
2274
2275 /* Unmap and free mbuf. */
2276 bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize,
2277 BUS_DMASYNC_POSTWRITE);
2278 bus_dmamap_unload(sc->sc_dmat, data->map);
2279 m_freem(data->m);
2280 data->m = NULL;
2281 ieee80211_free_node(data->ni);
2282 data->ni = NULL;
2283
2284 sc->sc_tx_timer = 0;
2285 if (--ring->queued < IWN_TX_RING_LOMARK) {
2286 sc->qfullmsk &= ~(1 << ring->qid);
2287 if (sc->qfullmsk == 0 && (ifp->if_flags & IFF_OACTIVE)) {
2288 ifp->if_flags &= ~IFF_OACTIVE;
2289 iwn_start(ifp);
2290 }
2291 }
2292 }
2293
2294 /*
2295 * Process a "command done" firmware notification. This is where we wakeup
2296 * processes waiting for a synchronous command completion.
2297 */
2298 static void
2299 iwn_cmd_done(struct iwn_softc *sc, struct iwn_rx_desc *desc)
2300 {
2301 struct iwn_tx_ring *ring = &sc->txq[4];
2302 struct iwn_tx_data *data;
2303
2304 if ((desc->qid & 0xf) != 4)
2305 return; /* Not a command ack. */
2306
2307 data = &ring->data[desc->idx];
2308
2309 /* If the command was mapped in an mbuf, free it. */
2310 if (data->m != NULL) {
2311 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
2312 data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
2313 bus_dmamap_unload(sc->sc_dmat, data->map);
2314 m_freem(data->m);
2315 data->m = NULL;
2316 }
2317 wakeup(&ring->desc[desc->idx]);
2318 }
2319
2320 /*
2321 * Process an INT_FH_RX or INT_SW_RX interrupt.
2322 */
2323 static void
2324 iwn_microcode_ready(struct iwn_softc *sc, struct iwn_ucode_info *uc)
2325 {
2326
2327 /* The microcontroller is ready */
2328 DPRINTF(("microcode alive notification version=%d.%d "
2329 "subtype=%x alive=%x\n", uc->major, uc->minor,
2330 uc->subtype, le32toh(uc->valid)));
2331
2332
2333 if (le32toh(uc->valid) != 1) {
2334 aprint_error_dev(sc->sc_dev, "microcontroller initialization "
2335 "failed\n");
2336 return;
2337 }
2338 if (uc->subtype == IWN_UCODE_INIT) {
2339 /* save microcontroller's report */
2340 memcpy(&sc->ucode_info, uc, sizeof (*uc));
2341 }
2342 /* Save the address of the error log in SRAM. */
2343 sc->errptr = le32toh(uc->errptr);
2344 }
2345
2346 static void
2347 iwn_notif_intr(struct iwn_softc *sc)
2348 {
2349 struct ieee80211com *ic = &sc->sc_ic;
2350 struct ifnet *ifp = ic->ic_ifp;
2351 struct iwn_rx_data *data;
2352 struct iwn_rx_desc *desc;
2353 uint16_t hw;
2354
2355 bus_dmamap_sync(sc->sc_dmat, sc->rxq.stat_dma.map,
2356 0, sc->rxq.stat_dma.size, BUS_DMASYNC_POSTREAD);
2357
2358 hw = le16toh(sc->rxq.stat->closed_count) & 0xfff;
2359
2360 /*
2361 * If the radio is disabled then down the interface and stop
2362 * processing - scan the queue for a microcode load command
2363 * result. It is the only thing that we can do with the radio
2364 * off.
2365 */
2366 if (!sc->sc_radio) {
2367 while (sc->rxq.cur != hw) {
2368 data = &sc->rxq.data[sc->rxq.cur];
2369 desc = (void *)data->m->m_ext.ext_buf;
2370 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
2371 sizeof(*desc), BUS_DMASYNC_POSTREAD);
2372 if (desc->type == IWN_UC_READY) {
2373 iwn_microcode_ready(sc,
2374 (struct iwn_ucode_info *)(desc + 1));
2375 } else if (desc->type == IWN_STATE_CHANGED) {
2376 uint32_t *status = (uint32_t *)(desc + 1);
2377
2378 /* enabled/disabled notification */
2379 DPRINTF(("state changed to %x\n",
2380 le32toh(*status)));
2381
2382 sc->sc_radio = !(le32toh(*status) & 1);
2383 }
2384
2385 sc->rxq.cur = (sc->rxq.cur + 1) % IWN_RX_RING_COUNT;
2386 }
2387
2388 if (!sc->sc_radio) {
2389 iwn_stop(ifp, 1);
2390 }
2391
2392 return;
2393 }
2394
2395 bus_dmamap_sync(sc->sc_dmat, sc->rxq.stat_dma.map,
2396 0, sc->rxq.stat_dma.size, BUS_DMASYNC_POSTREAD);
2397
2398 hw = le16toh(sc->rxq.stat->closed_count) & 0xfff;
2399 while (sc->rxq.cur != hw) {
2400 data = &sc->rxq.data[sc->rxq.cur];
2401 desc = (void *)data->m->m_ext.ext_buf;
2402
2403 bus_dmamap_sync(sc->sc_dmat, data->map, 0, sizeof (*desc),
2404 BUS_DMASYNC_POSTREAD);
2405
2406 DPRINTFN(4, ("notification qid=%d idx=%d flags=%x type=%d\n",
2407 desc->qid & 0xf, desc->idx, desc->flags, desc->type));
2408
2409 if (!(desc->qid & 0x80)) /* Reply to a command. */
2410 iwn_cmd_done(sc, desc);
2411
2412 switch (desc->type) {
2413 case IWN_RX_PHY:
2414 iwn_rx_phy(sc, desc);
2415 break;
2416
2417 case IWN_RX_DONE: /* 4965AGN only. */
2418 case IWN_MPDU_RX_DONE:
2419 /* An 802.11 frame has been received. */
2420 iwn_rx_done(sc, desc, data);
2421 break;
2422
2423 case IWN_TX_DONE:
2424 /* An 802.11 frame has been transmitted. */
2425 sc->sc_hal->tx_done(sc, desc, data);
2426 break;
2427
2428 case IWN_RX_STATISTICS:
2429 case IWN_BEACON_STATISTICS:
2430 iwn_rx_statistics(sc, desc, data);
2431 break;
2432
2433 case IWN_BEACON_MISSED:
2434 {
2435 struct iwn_beacon_missed *miss =
2436 (struct iwn_beacon_missed *)(desc + 1);
2437
2438 bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (*desc),
2439 sizeof (*miss), BUS_DMASYNC_POSTREAD);
2440 /*
2441 * If more than 5 consecutive beacons are missed,
2442 * reinitialize the sensitivity state machine.
2443 */
2444 DPRINTF(("beacons missed %d/%d\n",
2445 le32toh(miss->consecutive), le32toh(miss->total)));
2446 if (ic->ic_state == IEEE80211_S_RUN &&
2447 le32toh(miss->consecutive) > 5)
2448 (void)iwn_init_sensitivity(sc);
2449 break;
2450 }
2451 case IWN_UC_READY:
2452 {
2453 iwn_microcode_ready(sc,
2454 (struct iwn_ucode_info *)(desc + 1));
2455 break;
2456 }
2457 case IWN_STATE_CHANGED:
2458 {
2459 uint32_t *status = (uint32_t *)(desc + 1);
2460
2461 /* Enabled/disabled notification. */
2462 bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (*desc),
2463 sizeof (*status), BUS_DMASYNC_POSTREAD);
2464 DPRINTF(("state changed to %x\n", le32toh(*status)));
2465
2466 sc->sc_radio = !(le32toh(*status) & 1);
2467
2468 if (le32toh(*status) & 1) {
2469 /* The radio button has to be pushed. */
2470 aprint_error_dev(sc->sc_dev,
2471 "Radio transmitter is off\n");
2472 /* Turn the interface down. */
2473 iwn_stop(ifp, 1);
2474 return; /* No further processing. */
2475 }
2476 break;
2477 }
2478 case IWN_START_SCAN:
2479 {
2480 struct iwn_start_scan *scan =
2481 (struct iwn_start_scan *)(desc + 1);
2482
2483 bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (*desc),
2484 sizeof (*scan), BUS_DMASYNC_POSTREAD);
2485 DPRINTFN(2, ("scanning channel %d status %x\n",
2486 scan->chan, le32toh(scan->status)));
2487
2488 /* Fix current channel. */
2489 ic->ic_bss->ni_chan = &ic->ic_channels[scan->chan];
2490 break;
2491 }
2492 case IWN_STOP_SCAN:
2493 {
2494 struct iwn_stop_scan *scan =
2495 (struct iwn_stop_scan *)(desc + 1);
2496
2497 bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (*desc),
2498 sizeof (*scan), BUS_DMASYNC_POSTREAD);
2499 DPRINTF(("scan finished nchan=%d status=%d chan=%d\n",
2500 scan->nchan, scan->status, scan->chan));
2501
2502 if (scan->status == 1 && scan->chan <= 14 &&
2503 (sc->sc_flags & IWN_FLAG_HAS_5GHZ)) {
2504 /*
2505 * We just finished scanning 2GHz channels,
2506 * start scanning 5GHz ones.
2507 */
2508 if (iwn_scan(sc, IEEE80211_CHAN_5GHZ) == 0)
2509 break;
2510 }
2511 sc->is_scanning = false;
2512 ieee80211_end_scan(ic);
2513 break;
2514 }
2515 case IWN5000_CALIBRATION_RESULT:
2516 iwn5000_rx_calib_results(sc, desc, data);
2517 break;
2518
2519 case IWN5000_CALIBRATION_DONE:
2520 wakeup(sc);
2521 break;
2522 }
2523
2524 sc->rxq.cur = (sc->rxq.cur + 1) % IWN_RX_RING_COUNT;
2525 }
2526
2527 /* Tell the firmware what we have processed. */
2528 hw = (hw == 0) ? IWN_RX_RING_COUNT - 1 : hw - 1;
2529 IWN_WRITE(sc, IWN_FH_RX_WPTR, hw & ~7);
2530 }
2531
2532 /*
2533 * Process an INT_WAKEUP interrupt raised when the microcontroller wakes up
2534 * from power-down sleep mode.
2535 */
2536 static void
2537 iwn_wakeup_intr(struct iwn_softc *sc)
2538 {
2539 int qid;
2540
2541 DPRINTF(("ucode wakeup from power-down sleep\n"));
2542
2543 /* Wakeup RX and TX rings. */
2544 IWN_WRITE(sc, IWN_FH_RX_WPTR, sc->rxq.cur & ~7);
2545 for (qid = 0; qid < 6; qid++) {
2546 struct iwn_tx_ring *ring = &sc->txq[qid];
2547 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, qid << 8 | ring->cur);
2548 }
2549 }
2550
2551 /*
2552 * Dump the error log of the firmware when a firmware panic occurs. Although
2553 * we can't debug the firmware because it is neither open source nor free, it
2554 * can help us to identify certain classes of problems.
2555 */
2556 void
2557 iwn_fatal_intr(struct iwn_softc *sc)
2558 {
2559 const struct iwn_hal *hal = sc->sc_hal;
2560 struct iwn_fw_dump dump;
2561 int i;
2562
2563 /* Check that the error log address is valid. */
2564 if (sc->errptr < IWN_FW_DATA_BASE ||
2565 sc->errptr + sizeof (dump) >
2566 IWN_FW_DATA_BASE + hal->fw_data_maxsz) {
2567 aprint_error_dev(sc->sc_dev,
2568 "bad firmware error log address 0x%08x\n", sc->errptr);
2569 return;
2570 }
2571 if (iwn_nic_lock(sc) != 0) {
2572 aprint_error_dev(sc->sc_dev,
2573 "could not read firmware error log\n");
2574 return;
2575 }
2576 /* Read firmware error log from SRAM. */
2577 iwn_mem_read_region_4(sc, sc->errptr, (uint32_t *)&dump,
2578 sizeof (dump) / sizeof (uint32_t));
2579 iwn_nic_unlock(sc);
2580
2581 if (dump.valid == 0) {
2582 aprint_error_dev(sc->sc_dev, "firmware error log is empty\n");
2583 return;
2584 }
2585 printf("firmware error log:\n");
2586 #if 0
2587 printf(" error type = \"%s\" (0x%08X)\n",
2588 (dump.id < nitems(iwn_fw_errmsg)) ?
2589 iwn_fw_errmsg[dump.id] : "UNKNOWN",
2590 dump.id);
2591 #endif
2592 printf(" program counter = 0x%08X\n", dump.pc);
2593 printf(" source line = 0x%08X\n", dump.src_line);
2594 printf(" error data = 0x%08X%08X\n",
2595 dump.error_data[0], dump.error_data[1]);
2596 printf(" branch link = 0x%08X%08X\n",
2597 dump.branch_link[0], dump.branch_link[1]);
2598 printf(" interrupt link = 0x%08X%08X\n",
2599 dump.interrupt_link[0], dump.interrupt_link[1]);
2600 printf(" time = %u\n", dump.time[0]);
2601
2602 /* Dump driver status (TX and RX rings) while we're here. */
2603 printf("driver status:\n");
2604 for (i = 0; i < hal->ntxqs; i++) {
2605 struct iwn_tx_ring *ring = &sc->txq[i];
2606 printf(" tx ring %2d: qid=%-2d cur=%-3d queued=%-3d\n",
2607 i, ring->qid, ring->cur, ring->queued);
2608 }
2609 printf(" rx ring: cur=%d\n", sc->rxq.cur);
2610 printf(" 802.11 state %d\n", sc->sc_ic.ic_state);
2611 }
2612
2613 static int
2614 iwn_intr(void *arg)
2615 {
2616 struct iwn_softc *sc = arg;
2617 struct ifnet *ifp = sc->sc_ic.ic_ifp;
2618 uint32_t r1, r2;
2619
2620 /* Disable interrupts. */
2621 IWN_WRITE(sc, IWN_MASK, 0);
2622
2623 r1 = IWN_READ(sc, IWN_INT);
2624 r2 = IWN_READ(sc, IWN_FH_INT);
2625
2626 if (r1 == 0 && r2 == 0) {
2627 if (ifp->if_flags & IFF_UP)
2628 IWN_WRITE(sc, IWN_MASK, IWN_INT_MASK);
2629 return 0; /* Interrupt not for us. */
2630 }
2631 if (r1 == 0xffffffff || (r1 & 0xfffffff0) == 0xa5a5a5a0)
2632 return 0; /* Hardware gone! */
2633
2634 /* Acknowledge interrupts. */
2635 IWN_WRITE(sc, IWN_INT, r1);
2636 IWN_WRITE(sc, IWN_FH_INT, r2);
2637
2638 if (r1 & IWN_INT_RF_TOGGLED) {
2639 uint32_t tmp = IWN_READ(sc, IWN_GP_CNTRL);
2640 aprint_error_dev(sc->sc_dev, "RF switch: radio %s\n",
2641 (tmp & IWN_GP_CNTRL_RFKILL) ? "enabled" : "disabled");
2642 sc->sc_radio = (tmp & IWN_GP_CNTRL_RFKILL);
2643 }
2644 if (r1 & IWN_INT_CT_REACHED) {
2645 aprint_error_dev(sc->sc_dev, "critical temperature reached!\n");
2646 /* XXX Reduce TX power? */
2647 }
2648 if (r1 & (IWN_INT_SW_ERR | IWN_INT_HW_ERR)) {
2649 aprint_error_dev(sc->sc_dev, "fatal firmware error\n");
2650 /* Dump firmware error log and stop. */
2651 iwn_fatal_intr(sc);
2652 iwn_stop(sc->sc_ic.ic_ifp, 1);
2653 return 1;
2654 }
2655 if ((r1 & (IWN_INT_FH_RX | IWN_INT_SW_RX)) ||
2656 (r2 & IWN_FH_INT_RX))
2657 iwn_notif_intr(sc);
2658
2659 if ((r1 & IWN_INT_FH_TX) || (r2 & IWN_FH_INT_TX))
2660 wakeup(sc); /* FH DMA transfer completed. */
2661
2662 if (r1 & IWN_INT_ALIVE)
2663 wakeup(sc); /* Firmware is alive. */
2664
2665 if (r1 & IWN_INT_WAKEUP)
2666 iwn_wakeup_intr(sc);
2667
2668 /* Re-enable interrupts. */
2669 if (ifp->if_flags & IFF_UP)
2670 IWN_WRITE(sc, IWN_MASK, IWN_INT_MASK);
2671
2672 return 1;
2673 }
2674
2675 /*
2676 * Update TX scheduler ring when transmitting an 802.11 frame (4965AGN and
2677 * 5000 adapters use a slightly different format.)
2678 */
2679 static void
2680 iwn4965_update_sched(struct iwn_softc *sc, int qid, int idx, uint8_t id,
2681 uint16_t len)
2682 {
2683 uint16_t *w = &sc->sched[qid * IWN4965_SCHED_COUNT + idx];
2684
2685 *w = htole16(len + 8);
2686 bus_dmamap_sync(sc->sc_dmat, sc->sched_dma.map,
2687 (char *)(void *)w - (char *)(void *)sc->sched_dma.vaddr,
2688 sizeof (uint16_t), BUS_DMASYNC_PREWRITE);
2689 if (idx < IWN_SCHED_WINSZ) {
2690 *(w + IWN_TX_RING_COUNT) = *w;
2691 bus_dmamap_sync(sc->sc_dmat, sc->sched_dma.map,
2692 (char *)(void *)(w + IWN_TX_RING_COUNT) -
2693 (char *)(void *)sc->sched_dma.vaddr,
2694 sizeof (uint16_t), BUS_DMASYNC_PREWRITE);
2695 }
2696 }
2697
2698 static void
2699 iwn5000_update_sched(struct iwn_softc *sc, int qid, int idx, uint8_t id,
2700 uint16_t len)
2701 {
2702 uint16_t *w = &sc->sched[qid * IWN5000_SCHED_COUNT + idx];
2703
2704 *w = htole16(id << 12 | (len + 8));
2705 bus_dmamap_sync(sc->sc_dmat, sc->sched_dma.map,
2706 (char *)(void *)w - (char *)(void *)sc->sched_dma.vaddr,
2707 sizeof (uint16_t), BUS_DMASYNC_PREWRITE);
2708 if (idx < IWN_SCHED_WINSZ) {
2709 *(w + IWN_TX_RING_COUNT) = *w;
2710 bus_dmamap_sync(sc->sc_dmat, sc->sched_dma.map,
2711 (char *)(void *)(w + IWN_TX_RING_COUNT) -
2712 (char *)(void *)sc->sched_dma.vaddr,
2713 sizeof (uint16_t), BUS_DMASYNC_PREWRITE);
2714 }
2715 }
2716
2717 static void
2718 iwn5000_reset_sched(struct iwn_softc *sc, int qid, int idx)
2719 {
2720 uint16_t *w = &sc->sched[qid * IWN5000_SCHED_COUNT + idx];
2721
2722 *w = (*w & htole16(0xf000)) | htole16(1);
2723 bus_dmamap_sync(sc->sc_dmat, sc->sched_dma.map,
2724 (char *)(void *)w - (char *)(void *)sc->sched_dma.vaddr,
2725 sizeof (uint16_t), BUS_DMASYNC_PREWRITE);
2726 if (idx < IWN_SCHED_WINSZ) {
2727 *(w + IWN_TX_RING_COUNT) = *w;
2728 bus_dmamap_sync(sc->sc_dmat, sc->sched_dma.map,
2729 (char *)(void *)(w + IWN_TX_RING_COUNT) -
2730 (char *)(void *)sc->sched_dma.vaddr,
2731 sizeof (uint16_t), BUS_DMASYNC_PREWRITE);
2732 }
2733 }
2734
2735 static int
2736 iwn_tx(struct iwn_softc *sc, struct mbuf *m, struct ieee80211_node *ni, int ac)
2737 {
2738 const struct iwn_hal *hal = sc->sc_hal;
2739 struct ieee80211com *ic = &sc->sc_ic;
2740 struct iwn_node *wn = (void *)ni;
2741 struct iwn_tx_ring *ring;
2742 struct iwn_tx_desc *desc;
2743 struct iwn_tx_data *data;
2744 struct iwn_tx_cmd *cmd;
2745 struct iwn_cmd_data *tx;
2746 const struct iwn_rate *rinfo;
2747 struct ieee80211_frame *wh;
2748 struct ieee80211_key *k = NULL;
2749 const struct chanAccParams *cap;
2750 struct mbuf *m1;
2751 uint32_t flags;
2752 u_int hdrlen;
2753 bus_dma_segment_t *seg;
2754 uint8_t ridx, txant, type;
2755 int i, totlen, error, pad, noack;
2756
2757 wh = mtod(m, struct ieee80211_frame *);
2758 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2759
2760 /* JAF XXX two lines above were not in wpi. check we don't duplicate this */
2761
2762 if (IEEE80211_QOS_HAS_SEQ(wh)) {
2763 hdrlen = sizeof (struct ieee80211_qosframe);
2764 cap = &ic->ic_wme.wme_chanParams;
2765 noack = cap->cap_wmeParams[ac].wmep_noackPolicy;
2766 } else {
2767 hdrlen = sizeof (struct ieee80211_frame);
2768 noack = 0;
2769 }
2770
2771 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2772 k = ieee80211_crypto_encap(ic, ni, m);
2773 if (k == NULL) {
2774 m_freem(m);
2775 return ENOBUFS;
2776 }
2777 /* packet header may have moved, reset our local pointer */
2778 wh = mtod(m, struct ieee80211_frame *);
2779 }
2780
2781 ring = &sc->txq[ac];
2782 desc = &ring->desc[ring->cur];
2783 data = &ring->data[ring->cur];
2784
2785 /* Choose a TX rate index. */
2786 if (type == IEEE80211_FC0_TYPE_MGT) {
2787 /* mgmt frames are sent at the lowest available bit-rate */
2788 ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
2789 IWN_RIDX_OFDM6 : IWN_RIDX_CCK1;
2790 } else {
2791 if (ic->ic_fixed_rate != -1) {
2792 ridx = sc->fixed_ridx;
2793 } else
2794 ridx = wn->ridx[ni->ni_txrate];
2795 }
2796 rinfo = &iwn_rates[ridx];
2797
2798 if (sc->sc_drvbpf != NULL) {
2799 struct iwn_tx_radiotap_header *tap = &sc->sc_txtap;
2800
2801 tap->wt_flags = 0;
2802 tap->wt_chan_freq = htole16(ni->ni_chan->ic_freq);
2803 tap->wt_chan_flags = htole16(ni->ni_chan->ic_flags);
2804 tap->wt_rate = rinfo->rate;
2805 tap->wt_hwqueue = ac;
2806 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
2807 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2808
2809 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m);
2810 }
2811
2812 totlen = m->m_pkthdr.len;
2813
2814 /* Encrypt the frame if need be. */
2815 #ifdef IEEE80211_FC1_PROTECTED
2816 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2817 /* Retrieve key for TX. */
2818 k = ieee80211_get_txkey(ic, wh, ni);
2819 if (k->k_cipher != IEEE80211_CIPHER_CCMP) {
2820 /* Do software encryption. */
2821 if ((m = ieee80211_encrypt(ic, m, k)) == NULL)
2822 return ENOBUFS;
2823 /* 802.11 header may have moved. */
2824 wh = mtod(m, struct ieee80211_frame *);
2825 totlen = m->m_pkthdr.len;
2826
2827 } else /* HW appends CCMP MIC. */
2828 totlen += IEEE80211_CCMP_HDRLEN;
2829 }
2830 #endif
2831
2832 /* Prepare TX firmware command. */
2833 cmd = &ring->cmd[ring->cur];
2834 cmd->code = IWN_CMD_TX_DATA;
2835 cmd->flags = 0;
2836 cmd->qid = ring->qid;
2837 cmd->idx = ring->cur;
2838
2839 tx = (struct iwn_cmd_data *)cmd->data;
2840 /* NB: No need to clear tx, all fields are reinitialized here. */
2841 tx->scratch = 0; /* clear "scratch" area */
2842
2843 flags = 0;
2844 if (!noack && !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2845 flags |= IWN_TX_NEED_ACK;
2846 } else if (m->m_pkthdr.len + IEEE80211_CRC_LEN > ic->ic_rtsthreshold)
2847 flags |= IWN_TX_NEED_RTS | IWN_TX_FULL_TXOP;
2848
2849 #ifdef notyet
2850 if ((wh->i_fc[0] &
2851 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
2852 (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR))
2853 flags |= IWN_TX_IMM_BA; /* Cannot happen yet. */
2854 #endif
2855
2856 if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG)
2857 flags |= IWN_TX_MORE_FRAG; /* Cannot happen yet. */
2858
2859 /* Check if frame must be protected using RTS/CTS or CTS-to-self. */
2860 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2861 /* NB: Group frames are sent using CCK in 802.11b/g. */
2862 if (totlen + IEEE80211_CRC_LEN > ic->ic_rtsthreshold) {
2863 flags |= IWN_TX_NEED_RTS;
2864 } else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
2865 ridx >= IWN_RIDX_OFDM6) {
2866 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2867 flags |= IWN_TX_NEED_CTS;
2868 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2869 flags |= IWN_TX_NEED_RTS;
2870 }
2871 if (flags & (IWN_TX_NEED_RTS | IWN_TX_NEED_CTS)) {
2872 if (sc->hw_type != IWN_HW_REV_TYPE_4965) {
2873 /* 5000 autoselects RTS/CTS or CTS-to-self. */
2874 flags &= ~(IWN_TX_NEED_RTS | IWN_TX_NEED_CTS);
2875 flags |= IWN_TX_NEED_PROTECTION;
2876 } else
2877 flags |= IWN_TX_FULL_TXOP;
2878 }
2879 }
2880
2881 if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
2882 type != IEEE80211_FC0_TYPE_DATA)
2883 tx->id = hal->broadcast_id;
2884 else
2885 tx->id = wn->id;
2886
2887 if (type == IEEE80211_FC0_TYPE_MGT) {
2888 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2889
2890 #ifndef IEEE80211_STA_ONLY
2891 /* Tell HW to set timestamp in probe responses. */
2892 if ((subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) ||
2893 (subtype == IEEE80211_FC0_SUBTYPE_PROBE_REQ))
2894 flags |= IWN_TX_INSERT_TSTAMP;
2895 #endif
2896 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ ||
2897 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ ||
2898 subtype == IEEE80211_FC0_SUBTYPE_AUTH ||
2899 subtype == IEEE80211_FC0_SUBTYPE_DEAUTH) {
2900 flags &= ~IWN_TX_NEED_RTS;
2901 flags |= IWN_TX_NEED_CTS;
2902 tx->timeout = htole16(3);
2903 } else
2904 tx->timeout = htole16(2);
2905 } else
2906 tx->timeout = htole16(0);
2907
2908 if (hdrlen & 3) {
2909 /* First segment's length must be a multiple of 4. */
2910 flags |= IWN_TX_NEED_PADDING;
2911 pad = 4 - (hdrlen & 3);
2912 } else
2913 pad = 0;
2914
2915 #if 0
2916 if (type == IEEE80211_FC0_TYPE_CTL) {
2917 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2918
2919 /* tell h/w to set timestamp in probe responses */
2920 if (subtype == 0x0080) /* linux says this is "back request" */
2921 /* linux says (1 << 6) is IMM_BA_RSP_MASK */
2922 flags |= (IWN_TX_NEED_ACK | (1 << 6));
2923 }
2924 #endif
2925
2926 tx->len = htole16(totlen);
2927 tx->tid = 0/* tid */;
2928 tx->rts_ntries = 60;
2929 tx->data_ntries = 15;
2930 tx->lifetime = htole32(IWN_LIFETIME_INFINITE);
2931 tx->plcp = rinfo->plcp;
2932 tx->rflags = rinfo->flags;
2933 if (tx->id == hal->broadcast_id) {
2934 /* Group or management frame. */
2935 tx->linkq = 0;
2936 /* XXX Alternate between antenna A and B? */
2937 txant = IWN_LSB(sc->txantmsk);
2938 tx->rflags |= IWN_RFLAG_ANT(txant);
2939 } else {
2940 tx->linkq = ni->ni_rates.rs_nrates - ni->ni_txrate - 1;
2941 flags |= IWN_TX_LINKQ; /* enable MRR */
2942 }
2943 /* Set physical address of "scratch area". */
2944 tx->loaddr = htole32(IWN_LOADDR(data->scratch_paddr));
2945 tx->hiaddr = IWN_HIADDR(data->scratch_paddr);
2946
2947 /* Copy 802.11 header in TX command. */
2948 memcpy(((uint8_t *)tx) + sizeof(*tx), wh, hdrlen);
2949
2950 /* Trim 802.11 header. */
2951 m_adj(m, hdrlen);
2952 tx->security = 0;
2953
2954 #ifdef notyet
2955 if (k != NULL && k->k_cipher == IEEE80211_CIPHER_CCMP) {
2956 /* Trim 802.11 header and prepend CCMP IV. */
2957 m_adj(m, hdrlen - IEEE80211_CCMP_HDRLEN);
2958 ivp = mtod(m, uint8_t *);
2959 k->k_tsc++;
2960 ivp[0] = k->k_tsc;
2961 ivp[1] = k->k_tsc >> 8;
2962 ivp[2] = 0;
2963 ivp[3] = k->k_id << 6 | IEEE80211_WEP_EXTIV;
2964 ivp[4] = k->k_tsc >> 16;
2965 ivp[5] = k->k_tsc >> 24;
2966 ivp[6] = k->k_tsc >> 32;
2967 ivp[7] = k->k_tsc >> 40;
2968
2969 tx->security = IWN_CIPHER_CCMP;
2970 /* XXX flags |= IWN_TX_AMPDU_CCMP; */
2971 memcpy(tx->key, k->k_key, k->k_len);
2972
2973 /* TX scheduler includes CCMP MIC len w/5000 Series. */
2974 if (sc->hw_type != IWN_HW_REV_TYPE_4965)
2975 totlen += IEEE80211_CCMP_MICLEN;
2976 } else {
2977 /* Trim 802.11 header. */
2978 m_adj(m, hdrlen);
2979 tx->security = 0;
2980 }
2981 #endif
2982 tx->flags = htole32(flags);
2983
2984 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m,
2985 BUS_DMA_WRITE | BUS_DMA_NOWAIT);
2986 if (error != 0 && error != EFBIG) {
2987 aprint_error_dev(sc->sc_dev, "could not map mbuf (error %d)\n",
2988 error);
2989 m_freem(m);
2990 return error;
2991 }
2992 if (error != 0) {
2993 /* Too many DMA segments, linearize mbuf. */
2994 MGETHDR(m1, M_DONTWAIT, MT_DATA);
2995 if (m1 == NULL) {
2996 m_freem(m);
2997 return ENOBUFS;
2998 }
2999 if (m->m_pkthdr.len > MHLEN) {
3000 MCLGET(m1, M_DONTWAIT);
3001 if (!(m1->m_flags & M_EXT)) {
3002 m_freem(m);
3003 m_freem(m1);
3004 return ENOBUFS;
3005 }
3006 }
3007 m_copydata(m, 0, m->m_pkthdr.len, mtod(m1, void *));
3008 m1->m_pkthdr.len = m1->m_len = m->m_pkthdr.len;
3009 m_freem(m);
3010 m = m1;
3011
3012 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m,
3013 BUS_DMA_WRITE | BUS_DMA_NOWAIT);
3014 if (error != 0) {
3015 aprint_error_dev(sc->sc_dev,
3016 "could not map mbuf (error %d)\n", error);
3017 m_freem(m);
3018 return error;
3019 }
3020 }
3021
3022 data->m = m;
3023 data->ni = ni;
3024
3025 DPRINTFN(4, ("sending data: qid=%d idx=%d len=%d nsegs=%d\n",
3026 ring->qid, ring->cur, m->m_pkthdr.len, data->map->dm_nsegs));
3027
3028 /* Fill TX descriptor. */
3029 desc->nsegs = 1 + data->map->dm_nsegs;
3030 /* First DMA segment is used by the TX command. */
3031 desc->segs[0].addr = htole32(IWN_LOADDR(data->cmd_paddr));
3032 desc->segs[0].len = htole16(IWN_HIADDR(data->cmd_paddr) |
3033 (4 + sizeof (*tx) + hdrlen + pad) << 4);
3034 /* Other DMA segments are for data payload. */
3035 seg = data->map->dm_segs;
3036 for (i = 1; i <= data->map->dm_nsegs; i++) {
3037 desc->segs[i].addr = htole32(IWN_LOADDR(seg->ds_addr));
3038 desc->segs[i].len = htole16(IWN_HIADDR(seg->ds_addr) |
3039 seg->ds_len << 4);
3040 seg++;
3041 }
3042
3043 bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize,
3044 BUS_DMASYNC_PREWRITE);
3045 bus_dmamap_sync(sc->sc_dmat, ring->cmd_dma.map,
3046 (char *)(void *)cmd - (char *)(void *)ring->cmd_dma.vaddr,
3047 sizeof (*cmd), BUS_DMASYNC_PREWRITE);
3048 bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map,
3049 (char *)(void *)desc - (char *)(void *)ring->desc_dma.vaddr,
3050 sizeof (*desc), BUS_DMASYNC_PREWRITE);
3051
3052 /* Update TX scheduler. */
3053 hal->update_sched(sc, ring->qid, ring->cur, tx->id, totlen);
3054
3055 /* Kick TX ring. */
3056 ring->cur = (ring->cur + 1) % IWN_TX_RING_COUNT;
3057 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur);
3058
3059 /* Mark TX ring as full if we reach a certain threshold. */
3060 if (++ring->queued > IWN_TX_RING_HIMARK)
3061 sc->qfullmsk |= 1 << ring->qid;
3062
3063 return 0;
3064 }
3065
3066 static void
3067 iwn_start(struct ifnet *ifp)
3068 {
3069 struct iwn_softc *sc = ifp->if_softc;
3070 struct ieee80211com *ic = &sc->sc_ic;
3071 struct ieee80211_node *ni;
3072 struct ether_header *eh;
3073 struct mbuf *m;
3074 int ac;
3075
3076 DPRINTFN(5, ("iwn_start enter\n"));
3077
3078 /*
3079 * net80211 may still try to send management frames even if the
3080 * IFF_RUNNING flag is not set... Also, don't bother if the radio
3081 * is not enabled.
3082 */
3083 if (((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) ||
3084 !sc->sc_radio)
3085 return;
3086
3087 for (;;) {
3088 if (sc->qfullmsk != 0) {
3089 ifp->if_flags |= IFF_OACTIVE;
3090 break;
3091 }
3092 /* Send pending management frames first. */
3093 IF_DEQUEUE(&ic->ic_mgtq, m);
3094 if (m != NULL) {
3095 ni = (void *)m->m_pkthdr.rcvif;
3096 ac = 0;
3097 goto sendit;
3098 }
3099 if (ic->ic_state != IEEE80211_S_RUN)
3100 break;
3101
3102 /* Encapsulate and send data frames. */
3103 IFQ_DEQUEUE(&ifp->if_snd, m);
3104 if (m == NULL)
3105 break;
3106 if (m->m_len < sizeof (*eh) &&
3107 (m = m_pullup(m, sizeof (*eh))) == NULL) {
3108 ifp->if_oerrors++;
3109 continue;
3110 }
3111 eh = mtod(m, struct ether_header *);
3112 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
3113 if (ni == NULL) {
3114 m_freem(m);
3115 ifp->if_oerrors++;
3116 continue;
3117 }
3118 /* classify mbuf so we can find which tx ring to use */
3119 if (ieee80211_classify(ic, m, ni) != 0) {
3120 m_freem(m);
3121 ieee80211_free_node(ni);
3122 ifp->if_oerrors++;
3123 continue;
3124 }
3125
3126 /* no QoS encapsulation for EAPOL frames */
3127 ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ?
3128 M_WME_GETAC(m) : WME_AC_BE;
3129 bpf_mtap(ifp, m);
3130 if ((m = ieee80211_encap(ic, m, ni)) == NULL) {
3131 ieee80211_free_node(ni);
3132 ifp->if_oerrors++;
3133 continue;
3134 }
3135 sendit:
3136 bpf_mtap3(ic->ic_rawbpf, m);
3137 if (iwn_tx(sc, m, ni, ac) != 0) {
3138 ieee80211_free_node(ni);
3139 ifp->if_oerrors++;
3140 continue;
3141 }
3142
3143 sc->sc_tx_timer = 5;
3144 ifp->if_timer = 1;
3145 }
3146 }
3147
3148 static void
3149 iwn_watchdog(struct ifnet *ifp)
3150 {
3151 struct iwn_softc *sc = ifp->if_softc;
3152
3153 ifp->if_timer = 0;
3154
3155 if (sc->sc_tx_timer > 0) {
3156 if (--sc->sc_tx_timer == 0) {
3157 aprint_error_dev(sc->sc_dev, "device timeout\n");
3158 iwn_stop(ifp, 1);
3159 ifp->if_oerrors++;
3160 return;
3161 }
3162 ifp->if_timer = 1;
3163 }
3164
3165 ieee80211_watchdog(&sc->sc_ic);
3166 }
3167
3168 static int
3169 iwn_ioctl(struct ifnet *ifp, u_long cmd, void* data)
3170 {
3171 #define IS_RUNNING(ifp) \
3172 ((ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING))
3173
3174 struct iwn_softc *sc = ifp->if_softc;
3175 struct ieee80211com *ic = &sc->sc_ic;
3176 int s, error = 0;
3177
3178 s = splnet();
3179
3180 switch (cmd) {
3181 case SIOCSIFADDR:
3182 /* FALLTHROUGH */
3183 case SIOCSIFFLAGS:
3184 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
3185 break;
3186 if (ifp->if_flags & IFF_UP) {
3187 /*
3188 * resync the radio state just in case we missed
3189 * and event.
3190 */
3191 sc->sc_radio =
3192 (IWN_READ(sc, IWN_GP_CNTRL) & IWN_GP_CNTRL_RFKILL);
3193
3194 if (!sc->sc_radio) {
3195 error = EBUSY; /* XXX not really but same as els
3196 ewhere in driver */
3197 if (ifp->if_flags & IFF_RUNNING)
3198 iwn_stop(ifp, 1);
3199 } else if (!(ifp->if_flags & IFF_RUNNING))
3200 error = iwn_init(ifp);
3201 } else {
3202 if (ifp->if_flags & IFF_RUNNING)
3203 iwn_stop(ifp, 1);
3204 }
3205 break;
3206
3207 case SIOCADDMULTI:
3208 case SIOCDELMULTI:
3209 /* XXX no h/w multicast filter? --dyoung */
3210 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
3211 /* setup multicast filter, etc */
3212 error = 0;
3213 }
3214 break;
3215
3216 #if 0
3217 case SIOCS80211POWER:
3218 error = ieee80211_ioctl(ifp, cmd, data);
3219 if (error != ENETRESET)
3220 break;
3221 if (ic->ic_state == IEEE80211_S_RUN &&
3222 sc->calib.state == IWN_CALIB_STATE_RUN) {
3223 if (ic->ic_flags & IEEE80211_F_PMGTON)
3224 error = iwn_set_pslevel(sc, 0, 3, 0);
3225 else /* back to CAM */
3226 error = iwn_set_pslevel(sc, 0, 0, 0);
3227 } else {
3228 /* Defer until transition to IWN_CALIB_STATE_RUN. */
3229 error = 0;
3230 }
3231 break;
3232 #endif
3233
3234 default:
3235 error = ieee80211_ioctl(ic, cmd, data);
3236 }
3237
3238 if (error == ENETRESET) {
3239 error = 0;
3240 if (IS_RUNNING(ifp) &&
3241 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)) {
3242 iwn_stop(ifp, 0);
3243 error = iwn_init(ifp);
3244 }
3245 }
3246 splx(s);
3247 return error;
3248 #undef IS_RUNNING
3249 }
3250
3251 /*
3252 * Send a command to the firmware.
3253 */
3254 static int
3255 iwn_cmd(struct iwn_softc *sc, int code, const void *buf, int size, int async)
3256 {
3257 const struct iwn_hal *hal = sc->sc_hal;
3258 struct iwn_tx_ring *ring = &sc->txq[4];
3259 struct iwn_tx_desc *desc;
3260 struct iwn_tx_data *data;
3261 struct iwn_tx_cmd *cmd;
3262 struct mbuf *m;
3263 bus_addr_t paddr;
3264 int totlen, error;
3265
3266 desc = &ring->desc[ring->cur];
3267 data = &ring->data[ring->cur];
3268 totlen = 4 + size;
3269
3270 if (size > sizeof cmd->data) {
3271 /* Command is too large to fit in a descriptor. */
3272 if (totlen > MCLBYTES)
3273 return EINVAL;
3274 MGETHDR(m, M_DONTWAIT, MT_DATA);
3275 if (m == NULL)
3276 return ENOMEM;
3277 if (totlen > MHLEN) {
3278 MCLGET(m, M_DONTWAIT);
3279 if (!(m->m_flags & M_EXT)) {
3280 m_freem(m);
3281 return ENOMEM;
3282 }
3283 }
3284 cmd = mtod(m, struct iwn_tx_cmd *);
3285 error = bus_dmamap_load(sc->sc_dmat, data->map, cmd, totlen,
3286 NULL, BUS_DMA_NOWAIT);
3287 if (error != 0) {
3288 m_freem(m);
3289 return error;
3290 }
3291 data->m = m;
3292 paddr = data->map->dm_segs[0].ds_addr;
3293 } else {
3294 cmd = &ring->cmd[ring->cur];
3295 paddr = data->cmd_paddr;
3296 }
3297
3298 cmd->code = code;
3299 cmd->flags = 0;
3300 cmd->qid = ring->qid;
3301 cmd->idx = ring->cur;
3302 memcpy(cmd->data, buf, size);
3303
3304 desc->nsegs = 1;
3305 desc->segs[0].addr = htole32(IWN_LOADDR(paddr));
3306 desc->segs[0].len = htole16(IWN_HIADDR(paddr) | totlen << 4);
3307
3308 if (size > sizeof cmd->data) {
3309 bus_dmamap_sync(sc->sc_dmat, data->map, 0, totlen,
3310 BUS_DMASYNC_PREWRITE);
3311 } else {
3312 bus_dmamap_sync(sc->sc_dmat, ring->cmd_dma.map,
3313 (char *)(void *)cmd - (char *)(void *)ring->cmd_dma.vaddr,
3314 totlen, BUS_DMASYNC_PREWRITE);
3315 }
3316 bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map,
3317 (char *)(void *)desc - (char *)(void *)ring->desc_dma.vaddr,
3318 sizeof (*desc), BUS_DMASYNC_PREWRITE);
3319
3320 /* Update TX scheduler. */
3321 hal->update_sched(sc, ring->qid, ring->cur, 0, 0);
3322
3323 /* Kick command ring. */
3324 ring->cur = (ring->cur + 1) % IWN_TX_RING_COUNT;
3325 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur);
3326
3327 return async ? 0 : tsleep(desc, PCATCH, "iwncmd", hz);
3328 }
3329
3330 static int
3331 iwn_add_node(struct iwn_softc *sc, struct ieee80211_node *ni, bool broadcast,
3332 bool async, uint32_t htflags)
3333 {
3334 const struct iwn_hal *hal = sc->sc_hal;
3335 struct iwn_node_info node;
3336 int error;
3337
3338 error = 0;
3339
3340 memset(&node, 0, sizeof node);
3341 if (broadcast == true) {
3342 IEEE80211_ADDR_COPY(node.macaddr, etherbroadcastaddr);
3343 node.id = hal->broadcast_id;
3344 DPRINTF(("adding broadcast node\n"));
3345 } else {
3346 IEEE80211_ADDR_COPY(node.macaddr, ni->ni_macaddr);
3347 node.id = IWN_ID_BSS;
3348 node.htflags = htole32(htflags);
3349 DPRINTF(("adding BSS node\n"));
3350 }
3351 if ((error = hal->add_node(sc, &node, async)) != 0) {
3352 aprint_error_dev(sc->sc_dev, "could not add %s node\n",
3353 (broadcast == 1)? "broadcast" : "BSS");
3354 return error;
3355 }
3356 DPRINTF(("setting link quality for node %d\n", node.id));
3357 if ((error = iwn_set_link_quality(sc, ni)) != 0) {
3358 aprint_error_dev(sc->sc_dev,
3359 "could not setup MRR for %s node\n",
3360 (broadcast == 1)? "broadcast" : "BSS");
3361 return error;
3362 }
3363 if ((error = iwn_init_sensitivity(sc)) != 0) {
3364 aprint_error_dev(sc->sc_dev, "could not set sensitivity\n");
3365 return error;
3366 }
3367
3368 return error;
3369 }
3370
3371
3372 static int
3373 iwn4965_add_node(struct iwn_softc *sc, struct iwn_node_info *node, int async)
3374 {
3375 struct iwn4965_node_info hnode;
3376 char *src, *dst;
3377
3378 /*
3379 * We use the node structure for 5000 Series internally (it is
3380 * a superset of the one for 4965AGN). We thus copy the common
3381 * fields before sending the command.
3382 */
3383 src = (char *)node;
3384 dst = (char *)&hnode;
3385 memcpy(dst, src, 48);
3386 /* Skip TSC, RX MIC and TX MIC fields from ``src''. */
3387 memcpy(dst + 48, src + 72, 20);
3388 return iwn_cmd(sc, IWN_CMD_ADD_NODE, &hnode, sizeof hnode, async);
3389 }
3390
3391 static int
3392 iwn5000_add_node(struct iwn_softc *sc, struct iwn_node_info *node, int async)
3393 {
3394 /* Direct mapping. */
3395 return iwn_cmd(sc, IWN_CMD_ADD_NODE, node, sizeof (*node), async);
3396 }
3397
3398 static int
3399 iwn_set_link_quality(struct iwn_softc *sc, struct ieee80211_node *ni)
3400 {
3401 struct iwn_node *wn = (void *)ni;
3402 struct ieee80211_rateset *rs = &ni->ni_rates;
3403 struct iwn_cmd_link_quality linkq;
3404 const struct iwn_rate *rinfo;
3405 uint8_t txant;
3406 int i, txrate;
3407
3408 /* Use the first valid TX antenna. */
3409 txant = IWN_LSB(sc->txantmsk);
3410
3411 memset(&linkq, 0, sizeof linkq);
3412 linkq.id = wn->id;
3413 linkq.antmsk_1stream = txant;
3414 linkq.antmsk_2stream = IWN_ANT_A | IWN_ANT_B;
3415 linkq.ampdu_max = 64;
3416 linkq.ampdu_threshold = 3;
3417 linkq.ampdu_limit = htole16(4000); /* 4ms */
3418
3419 /* Start at highest available bit-rate. */
3420 txrate = rs->rs_nrates - 1;
3421 for (i = 0; i < IWN_MAX_TX_RETRIES; i++) {
3422 rinfo = &iwn_rates[wn->ridx[txrate]];
3423 linkq.retry[i].plcp = rinfo->plcp;
3424 linkq.retry[i].rflags = rinfo->flags;
3425 linkq.retry[i].rflags |= IWN_RFLAG_ANT(txant);
3426 /* Next retry at immediate lower bit-rate. */
3427 if (txrate > 0)
3428 txrate--;
3429 }
3430 return iwn_cmd(sc, IWN_CMD_LINK_QUALITY, &linkq, sizeof linkq, 1);
3431 }
3432
3433 /*
3434 * Broadcast node is used to send group-addressed and management frames.
3435 */
3436 static int
3437 iwn_add_broadcast_node(struct iwn_softc *sc, int async)
3438 {
3439 const struct iwn_hal *hal = sc->sc_hal;
3440 struct iwn_node_info node;
3441 struct iwn_cmd_link_quality linkq;
3442 const struct iwn_rate *rinfo;
3443 uint8_t txant;
3444 int i, error;
3445
3446 memset(&node, 0, sizeof node);
3447 IEEE80211_ADDR_COPY(node.macaddr, etherbroadcastaddr);
3448 node.id = hal->broadcast_id;
3449 DPRINTF(("adding broadcast node\n"));
3450 if ((error = hal->add_node(sc, &node, async)) != 0)
3451 return error;
3452
3453 /* Use the first valid TX antenna. */
3454 txant = IWN_LSB(sc->txantmsk);
3455
3456 memset(&linkq, 0, sizeof linkq);
3457 linkq.id = hal->broadcast_id;
3458 linkq.antmsk_1stream = txant;
3459 linkq.antmsk_2stream = IWN_ANT_A | IWN_ANT_B;
3460 linkq.ampdu_max = 64;
3461 linkq.ampdu_threshold = 3;
3462 linkq.ampdu_limit = htole16(4000); /* 4ms */
3463
3464 /* Use lowest mandatory bit-rate. */
3465 rinfo = (sc->sc_ic.ic_curmode != IEEE80211_MODE_11A) ?
3466 &iwn_rates[IWN_RIDX_CCK1] : &iwn_rates[IWN_RIDX_OFDM6];
3467 linkq.retry[0].plcp = rinfo->plcp;
3468 linkq.retry[0].rflags = rinfo->flags;
3469 linkq.retry[0].rflags |= IWN_RFLAG_ANT(txant);
3470 /* Use same bit-rate for all TX retries. */
3471 for (i = 1; i < IWN_MAX_TX_RETRIES; i++) {
3472 linkq.retry[i].plcp = linkq.retry[0].plcp;
3473 linkq.retry[i].rflags = linkq.retry[0].rflags;
3474 }
3475 return iwn_cmd(sc, IWN_CMD_LINK_QUALITY, &linkq, sizeof linkq, async);
3476 }
3477
3478 #ifdef notyet
3479 static void
3480 iwn_updateedca(struct ieee80211com *ic)
3481 {
3482 #define IWN_EXP2(x) ((1 << (x)) - 1) /* CWmin = 2^ECWmin - 1 */
3483 struct iwn_softc *sc = ic->ic_softc;
3484 struct iwn_edca_params cmd;
3485 int aci;
3486
3487 memset(&cmd, 0, sizeof cmd);
3488 cmd.flags = htole32(IWN_EDCA_UPDATE);
3489 for (aci = 0; aci < EDCA_NUM_AC; aci++) {
3490 const struct ieee80211_edca_ac_params *ac =
3491 &ic->ic_edca_ac[aci];
3492 cmd.ac[aci].aifsn = ac->ac_aifsn;
3493 cmd.ac[aci].cwmin = htole16(IWN_EXP2(ac->ac_ecwmin));
3494 cmd.ac[aci].cwmax = htole16(IWN_EXP2(ac->ac_ecwmax));
3495 cmd.ac[aci].txoplimit =
3496 htole16(IEEE80211_TXOP_TO_US(ac->ac_txoplimit));
3497 }
3498 (void)iwn_cmd(sc, IWN_CMD_EDCA_PARAMS, &cmd, sizeof cmd, 1);
3499 #undef IWN_EXP2
3500 }
3501 #endif
3502
3503 static void
3504 iwn_set_led(struct iwn_softc *sc, uint8_t which, uint8_t off, uint8_t on)
3505 {
3506 struct iwn_cmd_led led;
3507
3508 /* Clear microcode LED ownership. */
3509 IWN_CLRBITS(sc, IWN_LED, IWN_LED_BSM_CTRL);
3510
3511 led.which = which;
3512 led.unit = htole32(10000); /* on/off in unit of 100ms */
3513 led.off = off;
3514 led.on = on;
3515 (void)iwn_cmd(sc, IWN_CMD_SET_LED, &led, sizeof led, 1);
3516 }
3517
3518 /*
3519 * Set the critical temperature at which the firmware will notify us.
3520 */
3521 static int
3522 iwn_set_critical_temp(struct iwn_softc *sc)
3523 {
3524 struct iwn_critical_temp crit;
3525
3526 IWN_WRITE(sc, IWN_UCODE_GP1_CLR, IWN_UCODE_GP1_CTEMP_STOP_RF);
3527
3528 memset(&crit, 0, sizeof crit);
3529 crit.tempR = htole32(sc->critical_temp);
3530 DPRINTF(("setting critical temperature to %u\n", sc->critical_temp));
3531 return iwn_cmd(sc, IWN_CMD_SET_CRITICAL_TEMP, &crit, sizeof crit, 0);
3532 }
3533
3534 static int
3535 iwn_set_timing(struct iwn_softc *sc, struct ieee80211_node *ni)
3536 {
3537 struct iwn_cmd_timing cmd;
3538 uint64_t val, mod;
3539
3540 memset(&cmd, 0, sizeof cmd);
3541 memcpy(&cmd.tstamp, ni->ni_tstamp.data, sizeof (uint64_t));
3542 cmd.bintval = htole16(ni->ni_intval);
3543 cmd.lintval = htole16(10);
3544
3545 /* Compute remaining time until next beacon. */
3546 val = (uint64_t)ni->ni_intval * 1024; /* msecs -> usecs */
3547 mod = le64toh(cmd.tstamp) % val;
3548 cmd.binitval = htole32((uint32_t)(val - mod));
3549
3550 DPRINTF(("timing bintval=%u, tstamp=%llu, init=%u\n",
3551 ni->ni_intval, (unsigned long long)le64toh(cmd.tstamp),
3552 (uint32_t)(val - mod)));
3553
3554 return iwn_cmd(sc, IWN_CMD_TIMING, &cmd, sizeof cmd, 1);
3555 }
3556
3557 #if 0
3558 static void
3559 iwn4965_power_calibration(struct iwn_softc *sc, int temp)
3560 {
3561 /* Adjust TX power if need be (delta >= 3 degC.) */
3562 DPRINTF(("temperature %d->%d\n", sc->temp, temp));
3563 if (abs(temp - sc->temp) >= 3) {
3564 /* Record temperature of last calibration. */
3565 sc->temp = temp;
3566 (void)iwn4965_set_txpower(sc, 1);
3567 }
3568 }
3569 #endif
3570
3571 /*
3572 * Set TX power for current channel (each rate has its own power settings).
3573 * This function takes into account the regulatory information from EEPROM,
3574 * the current temperature and the current voltage.
3575 */
3576 static int
3577 iwn4965_set_txpower(struct iwn_softc *sc, int async)
3578 {
3579 /* Fixed-point arithmetic division using a n-bit fractional part. */
3580 #define fdivround(a, b, n) \
3581 ((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n))
3582 /* Linear interpolation. */
3583 #define interpolate(x, x1, y1, x2, y2, n) \
3584 ((y1) + fdivround(((int)(x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n))
3585
3586 static const int tdiv[IWN_NATTEN_GROUPS] = { 9, 8, 8, 8, 6 };
3587 struct ieee80211com *ic = &sc->sc_ic;
3588 struct iwn_ucode_info *uc = &sc->ucode_info;
3589 struct ieee80211_channel *ch;
3590 struct iwn4965_cmd_txpower cmd;
3591 struct iwn4965_eeprom_chan_samples *chans;
3592 const uint8_t *rf_gain, *dsp_gain;
3593 int32_t vdiff, tdiff;
3594 int i, c, grp, maxpwr;
3595 uint8_t chan;
3596
3597 /* Retrieve current channel from last RXON. */
3598 chan = sc->rxon.chan;
3599 DPRINTF(("setting TX power for channel %d\n", chan));
3600 ch = &ic->ic_channels[chan];
3601
3602 memset(&cmd, 0, sizeof cmd);
3603 cmd.band = IEEE80211_IS_CHAN_5GHZ(ch) ? 0 : 1;
3604 cmd.chan = chan;
3605
3606 if (IEEE80211_IS_CHAN_5GHZ(ch)) {
3607 maxpwr = sc->maxpwr5GHz;
3608 rf_gain = iwn4965_rf_gain_5ghz;
3609 dsp_gain = iwn4965_dsp_gain_5ghz;
3610 } else {
3611 maxpwr = sc->maxpwr2GHz;
3612 rf_gain = iwn4965_rf_gain_2ghz;
3613 dsp_gain = iwn4965_dsp_gain_2ghz;
3614 }
3615
3616 /* Compute voltage compensation. */
3617 vdiff = ((int32_t)le32toh(uc->volt) - sc->eeprom_voltage) / 7;
3618 if (vdiff > 0)
3619 vdiff *= 2;
3620 if (abs(vdiff) > 2)
3621 vdiff = 0;
3622 DPRINTF(("voltage compensation=%d (UCODE=%d, EEPROM=%d)\n",
3623 vdiff, le32toh(uc->volt), sc->eeprom_voltage));
3624
3625 /* Get channel's attenuation group. */
3626 if (chan <= 20) /* 1-20 */
3627 grp = 4;
3628 else if (chan <= 43) /* 34-43 */
3629 grp = 0;
3630 else if (chan <= 70) /* 44-70 */
3631 grp = 1;
3632 else if (chan <= 124) /* 71-124 */
3633 grp = 2;
3634 else /* 125-200 */
3635 grp = 3;
3636 DPRINTF(("chan %d, attenuation group=%d\n", chan, grp));
3637
3638 /* Get channel's sub-band. */
3639 for (i = 0; i < IWN_NBANDS; i++)
3640 if (sc->bands[i].lo != 0 &&
3641 sc->bands[i].lo <= chan && chan <= sc->bands[i].hi)
3642 break;
3643 chans = sc->bands[i].chans;
3644 DPRINTF(("chan %d sub-band=%d\n", chan, i));
3645
3646 for (c = 0; c < 2; c++) {
3647 uint8_t power, gain, temp;
3648 int maxchpwr, pwr, ridx, idx;
3649
3650 power = interpolate(chan,
3651 chans[0].num, chans[0].samples[c][1].power,
3652 chans[1].num, chans[1].samples[c][1].power, 1);
3653 gain = interpolate(chan,
3654 chans[0].num, chans[0].samples[c][1].gain,
3655 chans[1].num, chans[1].samples[c][1].gain, 1);
3656 temp = interpolate(chan,
3657 chans[0].num, chans[0].samples[c][1].temp,
3658 chans[1].num, chans[1].samples[c][1].temp, 1);
3659 DPRINTF(("TX chain %d: power=%d gain=%d temp=%d\n",
3660 c, power, gain, temp));
3661
3662 /* Compute temperature compensation. */
3663 tdiff = ((sc->temp - temp) * 2) / tdiv[grp];
3664 DPRINTF(("temperature compensation=%d (current=%d, "
3665 "EEPROM=%d)\n", tdiff, sc->temp, temp));
3666
3667 for (ridx = 0; ridx <= IWN_RIDX_MAX; ridx++) {
3668 maxchpwr = sc->maxpwr[chan] * 2;
3669 if ((ridx / 8) & 1)
3670 maxchpwr -= 6; /* MIMO 2T: -3dB */
3671
3672 pwr = maxpwr;
3673
3674 /* Adjust TX power based on rate. */
3675 if ((ridx % 8) == 5)
3676 pwr -= 15; /* OFDM48: -7.5dB */
3677 else if ((ridx % 8) == 6)
3678 pwr -= 17; /* OFDM54: -8.5dB */
3679 else if ((ridx % 8) == 7)
3680 pwr -= 20; /* OFDM60: -10dB */
3681 else
3682 pwr -= 10; /* Others: -5dB */
3683
3684 /* Do not exceed channel's max TX power. */
3685 if (pwr > maxchpwr)
3686 pwr = maxchpwr;
3687
3688 idx = gain - (pwr - power) - tdiff - vdiff;
3689 if ((ridx / 8) & 1) /* MIMO */
3690 idx += (int32_t)le32toh(uc->atten[grp][c]);
3691
3692 if (cmd.band == 0)
3693 idx += 9; /* 5GHz */
3694 if (ridx == IWN_RIDX_MAX)
3695 idx += 5; /* CCK */
3696
3697 /* Make sure idx stays in a valid range. */
3698 if (idx < 0)
3699 idx = 0;
3700 else if (idx > IWN4965_MAX_PWR_INDEX)
3701 idx = IWN4965_MAX_PWR_INDEX;
3702
3703 DPRINTF(("TX chain %d, rate idx %d: power=%d\n",
3704 c, ridx, idx));
3705 cmd.power[ridx].rf_gain[c] = rf_gain[idx];
3706 cmd.power[ridx].dsp_gain[c] = dsp_gain[idx];
3707 }
3708 }
3709
3710 DPRINTF(("setting TX power for chan %d\n", chan));
3711 return iwn_cmd(sc, IWN_CMD_TXPOWER, &cmd, sizeof cmd, async);
3712
3713 #undef interpolate
3714 #undef fdivround
3715 }
3716
3717 static int
3718 iwn5000_set_txpower(struct iwn_softc *sc, int async)
3719 {
3720 struct iwn5000_cmd_txpower cmd;
3721
3722 /*
3723 * TX power calibration is handled automatically by the firmware
3724 * for 5000 Series.
3725 */
3726 memset(&cmd, 0, sizeof cmd);
3727 cmd.global_limit = 2 * IWN5000_TXPOWER_MAX_DBM; /* 16 dBm */
3728 cmd.flags = IWN5000_TXPOWER_NO_CLOSED;
3729 cmd.srv_limit = IWN5000_TXPOWER_AUTO;
3730 DPRINTF(("setting TX power\n"));
3731 return iwn_cmd(sc, IWN_CMD_TXPOWER_DBM, &cmd, sizeof cmd, async);
3732 }
3733
3734 /*
3735 * Retrieve the maximum RSSI (in dBm) among receivers.
3736 */
3737 static int
3738 iwn4965_get_rssi(const struct iwn_rx_stat *stat)
3739 {
3740 const struct iwn4965_rx_phystat *phy = (const void *)stat->phybuf;
3741 uint8_t mask, agc;
3742 int rssi;
3743
3744 mask = (le16toh(phy->antenna) >> 4) & 0x7;
3745 agc = (le16toh(phy->agc) >> 7) & 0x7f;
3746
3747 rssi = 0;
3748 if (mask & IWN_ANT_A)
3749 rssi = MAX(rssi, phy->rssi[0]);
3750 if (mask & IWN_ANT_B)
3751 rssi = MAX(rssi, phy->rssi[2]);
3752 if (mask & IWN_ANT_C)
3753 rssi = MAX(rssi, phy->rssi[4]);
3754
3755 return rssi - agc - IWN_RSSI_TO_DBM;
3756 }
3757
3758 static int
3759 iwn5000_get_rssi(const struct iwn_rx_stat *stat)
3760 {
3761 const struct iwn5000_rx_phystat *phy = (const void *)stat->phybuf;
3762 uint8_t agc;
3763 int rssi;
3764
3765 agc = (le32toh(phy->agc) >> 9) & 0x7f;
3766
3767 rssi = MAX(le16toh(phy->rssi[0]) & 0xff,
3768 le16toh(phy->rssi[1]) & 0xff);
3769 rssi = MAX(le16toh(phy->rssi[2]) & 0xff, rssi);
3770
3771 return rssi - agc - IWN_RSSI_TO_DBM;
3772 }
3773
3774 /*
3775 * Retrieve the average noise (in dBm) among receivers.
3776 */
3777 static int
3778 iwn_get_noise(const struct iwn_rx_general_stats *stats)
3779 {
3780 int i, total, nbant, noise;
3781
3782 total = nbant = 0;
3783 for (i = 0; i < 3; i++) {
3784 if ((noise = le32toh(stats->noise[i]) & 0xff) == 0)
3785 continue;
3786 total += noise;
3787 nbant++;
3788 }
3789 /* There should be at least one antenna but check anyway. */
3790 return (nbant == 0) ? -127 : (total / nbant) - 107;
3791 }
3792
3793 /*
3794 * Compute temperature (in degC) from last received statistics.
3795 */
3796 static int
3797 iwn4965_get_temperature(struct iwn_softc *sc)
3798 {
3799 struct iwn_ucode_info *uc = &sc->ucode_info;
3800 int32_t r1, r2, r3, r4, temp;
3801
3802 r1 = le32toh(uc->temp[0].chan20MHz);
3803 r2 = le32toh(uc->temp[1].chan20MHz);
3804 r3 = le32toh(uc->temp[2].chan20MHz);
3805 r4 = le32toh(sc->rawtemp);
3806
3807 if (r1 == r3) /* Prevents division by 0 (should not happen.) */
3808 return 0;
3809
3810 /* Sign-extend 23-bit R4 value to 32-bit. */
3811 r4 = (r4 << 8) >> 8;
3812 /* Compute temperature in Kelvin. */
3813 temp = (259 * (r4 - r2)) / (r3 - r1);
3814 temp = (temp * 97) / 100 + 8;
3815
3816 DPRINTF(("temperature %dK/%dC\n", temp, IWN_KTOC(temp)));
3817 return IWN_KTOC(temp);
3818 }
3819
3820 static int
3821 iwn5000_get_temperature(struct iwn_softc *sc)
3822 {
3823 /*
3824 * Temperature is not used by the driver for 5000 Series because
3825 * TX power calibration is handled by firmware. We export it to
3826 * users through the sensor framework though.
3827 */
3828 return le32toh(sc->rawtemp);
3829 }
3830
3831 /*
3832 * Initialize sensitivity calibration state machine.
3833 */
3834 static int
3835 iwn_init_sensitivity(struct iwn_softc *sc)
3836 {
3837 const struct iwn_hal *hal = sc->sc_hal;
3838 struct iwn_calib_state *calib = &sc->calib;
3839 uint32_t flags;
3840 int error;
3841
3842 /* Reset calibration state machine. */
3843 memset(calib, 0, sizeof (*calib));
3844 calib->state = IWN_CALIB_STATE_INIT;
3845 calib->cck_state = IWN_CCK_STATE_HIFA;
3846 /* Set initial correlation values. */
3847 calib->ofdm_x1 = hal->limits->min_ofdm_x1;
3848 calib->ofdm_mrc_x1 = hal->limits->min_ofdm_mrc_x1;
3849 calib->ofdm_x4 = 90;
3850 calib->ofdm_mrc_x4 = hal->limits->min_ofdm_mrc_x4;
3851 calib->cck_x4 = 125;
3852 calib->cck_mrc_x4 = hal->limits->min_cck_mrc_x4;
3853 calib->energy_cck = hal->limits->energy_cck;
3854
3855 /* Write initial sensitivity. */
3856 if ((error = iwn_send_sensitivity(sc)) != 0)
3857 return error;
3858
3859 /* Write initial gains. */
3860 if ((error = hal->init_gains(sc)) != 0)
3861 return error;
3862
3863 /* Request statistics at each beacon interval. */
3864 flags = 0;
3865 DPRINTF(("sending request for statistics\n"));
3866 return iwn_cmd(sc, IWN_CMD_GET_STATISTICS, &flags, sizeof flags, 1);
3867 }
3868
3869 /*
3870 * Collect noise and RSSI statistics for the first 20 beacons received
3871 * after association and use them to determine connected antennas and
3872 * to set differential gains.
3873 */
3874 static void
3875 iwn_collect_noise(struct iwn_softc *sc,
3876 const struct iwn_rx_general_stats *stats)
3877 {
3878 const struct iwn_hal *hal = sc->sc_hal;
3879 struct iwn_calib_state *calib = &sc->calib;
3880 uint32_t val;
3881 int i;
3882
3883 /* Accumulate RSSI and noise for all 3 antennas. */
3884 for (i = 0; i < 3; i++) {
3885 calib->rssi[i] += le32toh(stats->rssi[i]) & 0xff;
3886 calib->noise[i] += le32toh(stats->noise[i]) & 0xff;
3887 }
3888 /* NB: We update differential gains only once after 20 beacons. */
3889 if (++calib->nbeacons < 20)
3890 return;
3891
3892 /* Determine highest average RSSI. */
3893 val = MAX(calib->rssi[0], calib->rssi[1]);
3894 val = MAX(calib->rssi[2], val);
3895
3896 /* Determine which antennas are connected. */
3897 sc->antmsk = 0;
3898 for (i = 0; i < 3; i++)
3899 if (val - calib->rssi[i] <= 15 * 20)
3900 sc->antmsk |= 1 << i;
3901 /* If none of the TX antennas are connected, keep at least one. */
3902 if ((sc->antmsk & sc->txantmsk) == 0)
3903 sc->antmsk |= IWN_LSB(sc->txantmsk);
3904
3905 (void)hal->set_gains(sc);
3906 calib->state = IWN_CALIB_STATE_RUN;
3907
3908 #ifdef notyet
3909 /* XXX Disable RX chains with no antennas connected. */
3910 sc->rxon.rxchain = htole16(IWN_RXCHAIN_SEL(sc->antmsk));
3911 (void)iwn_cmd(sc, IWN_CMD_CONFIGURE, &sc->rxon, hal->rxonsz, 1);
3912
3913 /* Enable power-saving mode if requested by user. */
3914 if (sc->sc_ic.ic_flags & IEEE80211_F_PMGTON)
3915 (void)iwn_set_pslevel(sc, 0, 3, 1);
3916 #endif
3917 }
3918
3919 static int
3920 iwn4965_init_gains(struct iwn_softc *sc)
3921 {
3922 struct iwn_phy_calib_gain cmd;
3923
3924 memset(&cmd, 0, sizeof cmd);
3925 cmd.code = IWN4965_PHY_CALIB_DIFF_GAIN;
3926 /* Differential gains initially set to 0 for all 3 antennas. */
3927 DPRINTF(("setting initial differential gains\n"));
3928 return iwn_cmd(sc, IWN_CMD_PHY_CALIB, &cmd, sizeof cmd, 1);
3929 }
3930
3931 static int
3932 iwn5000_init_gains(struct iwn_softc *sc)
3933 {
3934 struct iwn_phy_calib cmd;
3935
3936 if (sc->hw_type == IWN_HW_REV_TYPE_6000 ||
3937 sc->hw_type == IWN_HW_REV_TYPE_6050)
3938 return 0;
3939
3940 memset(&cmd, 0, sizeof cmd);
3941 cmd.code = IWN5000_PHY_CALIB_RESET_NOISE_GAIN;
3942 cmd.ngroups = 1;
3943 cmd.isvalid = 1;
3944 DPRINTF(("setting initial differential gains\n"));
3945 return iwn_cmd(sc, IWN_CMD_PHY_CALIB, &cmd, sizeof cmd, 1);
3946 }
3947
3948 static int
3949 iwn4965_set_gains(struct iwn_softc *sc)
3950 {
3951 struct iwn_calib_state *calib = &sc->calib;
3952 struct iwn_phy_calib_gain cmd;
3953 int i, delta, noise;
3954
3955 /* Get minimal noise among connected antennas. */
3956 noise = INT_MAX; /* NB: There's at least one antenna. */
3957 for (i = 0; i < 3; i++)
3958 if (sc->antmsk & (1 << i))
3959 noise = MIN(calib->noise[i], noise);
3960
3961 memset(&cmd, 0, sizeof cmd);
3962 cmd.code = IWN4965_PHY_CALIB_DIFF_GAIN;
3963 /* Set differential gains for connected antennas. */
3964 for (i = 0; i < 3; i++) {
3965 if (sc->antmsk & (1 << i)) {
3966 /* Compute attenuation (in unit of 1.5dB). */
3967 delta = (noise - (int32_t)calib->noise[i]) / 30;
3968 /* NB: delta <= 0 */
3969 /* Limit to [-4.5dB,0]. */
3970 cmd.gain[i] = MIN(abs(delta), 3);
3971 if (delta < 0)
3972 cmd.gain[i] |= 1 << 2; /* sign bit */
3973 }
3974 }
3975 DPRINTF(("setting differential gains Ant A/B/C: %x/%x/%x (%x)\n",
3976 cmd.gain[0], cmd.gain[1], cmd.gain[2], sc->antmsk));
3977 return iwn_cmd(sc, IWN_CMD_PHY_CALIB, &cmd, sizeof cmd, 1);
3978 }
3979
3980 static int
3981 iwn5000_set_gains(struct iwn_softc *sc)
3982 {
3983 struct iwn_calib_state *calib = &sc->calib;
3984 struct iwn_phy_calib_gain cmd;
3985 int i, delta;
3986
3987 if (sc->hw_type == IWN_HW_REV_TYPE_6000 ||
3988 sc->hw_type == IWN_HW_REV_TYPE_6050)
3989 return 0;
3990
3991 memset(&cmd, 0, sizeof cmd);
3992 cmd.code = IWN5000_PHY_CALIB_NOISE_GAIN;
3993 cmd.ngroups = 1;
3994 cmd.isvalid = 1;
3995 /* Set differential gains for antennas B and C. */
3996 for (i = 1; i < 3; i++) {
3997 if (sc->antmsk & (1 << i)) {
3998 /* The delta is relative to antenna A. */
3999 delta = ((int32_t)calib->noise[0] -
4000 (int32_t)calib->noise[i]) / 30;
4001 /* Limit to [-4.5dB,+4.5dB]. */
4002 cmd.gain[i - 1] = MIN(abs(delta), 3);
4003 if (delta < 0)
4004 cmd.gain[i - 1] |= 1 << 2; /* sign bit */
4005 }
4006 }
4007 DPRINTF(("setting differential gains Ant B/C: %x/%x (%x)\n",
4008 cmd.gain[0], cmd.gain[1], sc->antmsk));
4009 return iwn_cmd(sc, IWN_CMD_PHY_CALIB, &cmd, sizeof cmd, 1);
4010 }
4011
4012 /*
4013 * Tune RF RX sensitivity based on the number of false alarms detected
4014 * during the last beacon period.
4015 */
4016 static void
4017 iwn_tune_sensitivity(struct iwn_softc *sc, const struct iwn_rx_stats *stats)
4018 {
4019 #define inc(val, inc, max) \
4020 if ((val) < (max)) { \
4021 if ((val) < (max) - (inc)) \
4022 (val) += (inc); \
4023 else \
4024 (val) = (max); \
4025 needs_update = 1; \
4026 }
4027 #define dec(val, dec, min) \
4028 if ((val) > (min)) { \
4029 if ((val) > (min) + (dec)) \
4030 (val) -= (dec); \
4031 else \
4032 (val) = (min); \
4033 needs_update = 1; \
4034 }
4035
4036 const struct iwn_hal *hal = sc->sc_hal;
4037 const struct iwn_sensitivity_limits *limits = hal->limits;
4038 struct iwn_calib_state *calib = &sc->calib;
4039 uint32_t val, rxena, fa;
4040 uint32_t energy[3], energy_min;
4041 uint8_t noise[3], noise_ref;
4042 int i, needs_update = 0;
4043
4044 /* Check that we've been enabled long enough. */
4045 if ((rxena = le32toh(stats->general.load)) == 0)
4046 return;
4047
4048 /* Compute number of false alarms since last call for OFDM. */
4049 fa = le32toh(stats->ofdm.bad_plcp) - calib->bad_plcp_ofdm;
4050 fa += le32toh(stats->ofdm.fa) - calib->fa_ofdm;
4051 fa *= 200 * 1024; /* 200TU */
4052
4053 /* Save counters values for next call. */
4054 calib->bad_plcp_ofdm = le32toh(stats->ofdm.bad_plcp);
4055 calib->fa_ofdm = le32toh(stats->ofdm.fa);
4056
4057 if (fa > 50 * rxena) {
4058 /* High false alarm count, decrease sensitivity. */
4059 DPRINTFN(2, ("OFDM high false alarm count: %u\n", fa));
4060 inc(calib->ofdm_x1, 1, limits->max_ofdm_x1);
4061 inc(calib->ofdm_mrc_x1, 1, limits->max_ofdm_mrc_x1);
4062 inc(calib->ofdm_x4, 1, limits->max_ofdm_x4);
4063 inc(calib->ofdm_mrc_x4, 1, limits->max_ofdm_mrc_x4);
4064
4065 } else if (fa < 5 * rxena) {
4066 /* Low false alarm count, increase sensitivity. */
4067 DPRINTFN(2, ("OFDM low false alarm count: %u\n", fa));
4068 dec(calib->ofdm_x1, 1, limits->min_ofdm_x1);
4069 dec(calib->ofdm_mrc_x1, 1, limits->min_ofdm_mrc_x1);
4070 dec(calib->ofdm_x4, 1, limits->min_ofdm_x4);
4071 dec(calib->ofdm_mrc_x4, 1, limits->min_ofdm_mrc_x4);
4072 }
4073
4074 /* Compute maximum noise among 3 receivers. */
4075 for (i = 0; i < 3; i++)
4076 noise[i] = (le32toh(stats->general.noise[i]) >> 8) & 0xff;
4077 val = MAX(noise[0], noise[1]);
4078 val = MAX(noise[2], val);
4079 /* Insert it into our samples table. */
4080 calib->noise_samples[calib->cur_noise_sample] = val;
4081 calib->cur_noise_sample = (calib->cur_noise_sample + 1) % 20;
4082
4083 /* Compute maximum noise among last 20 samples. */
4084 noise_ref = calib->noise_samples[0];
4085 for (i = 1; i < 20; i++)
4086 noise_ref = MAX(noise_ref, calib->noise_samples[i]);
4087
4088 /* Compute maximum energy among 3 receivers. */
4089 for (i = 0; i < 3; i++)
4090 energy[i] = le32toh(stats->general.energy[i]);
4091 val = MIN(energy[0], energy[1]);
4092 val = MIN(energy[2], val);
4093 /* Insert it into our samples table. */
4094 calib->energy_samples[calib->cur_energy_sample] = val;
4095 calib->cur_energy_sample = (calib->cur_energy_sample + 1) % 10;
4096
4097 /* Compute minimum energy among last 10 samples. */
4098 energy_min = calib->energy_samples[0];
4099 for (i = 1; i < 10; i++)
4100 energy_min = MAX(energy_min, calib->energy_samples[i]);
4101 energy_min += 6;
4102
4103 /* Compute number of false alarms since last call for CCK. */
4104 fa = le32toh(stats->cck.bad_plcp) - calib->bad_plcp_cck;
4105 fa += le32toh(stats->cck.fa) - calib->fa_cck;
4106 fa *= 200 * 1024; /* 200TU */
4107
4108 /* Save counters values for next call. */
4109 calib->bad_plcp_cck = le32toh(stats->cck.bad_plcp);
4110 calib->fa_cck = le32toh(stats->cck.fa);
4111
4112 if (fa > 50 * rxena) {
4113 /* High false alarm count, decrease sensitivity. */
4114 DPRINTFN(2, ("CCK high false alarm count: %u\n", fa));
4115 calib->cck_state = IWN_CCK_STATE_HIFA;
4116 calib->low_fa = 0;
4117
4118 if (calib->cck_x4 > 160) {
4119 calib->noise_ref = noise_ref;
4120 if (calib->energy_cck > 2)
4121 dec(calib->energy_cck, 2, energy_min);
4122 }
4123 if (calib->cck_x4 < 160) {
4124 calib->cck_x4 = 161;
4125 needs_update = 1;
4126 } else
4127 inc(calib->cck_x4, 3, limits->max_cck_x4);
4128
4129 inc(calib->cck_mrc_x4, 3, limits->max_cck_mrc_x4);
4130
4131 } else if (fa < 5 * rxena) {
4132 /* Low false alarm count, increase sensitivity. */
4133 DPRINTFN(2, ("CCK low false alarm count: %u\n", fa));
4134 calib->cck_state = IWN_CCK_STATE_LOFA;
4135 calib->low_fa++;
4136
4137 if (calib->cck_state != IWN_CCK_STATE_INIT &&
4138 (((int32_t)calib->noise_ref - (int32_t)noise_ref) > 2 ||
4139 calib->low_fa > 100)) {
4140 inc(calib->energy_cck, 2, limits->min_energy_cck);
4141 dec(calib->cck_x4, 3, limits->min_cck_x4);
4142 dec(calib->cck_mrc_x4, 3, limits->min_cck_mrc_x4);
4143 }
4144 } else {
4145 /* Not worth to increase or decrease sensitivity. */
4146 DPRINTFN(2, ("CCK normal false alarm count: %u\n", fa));
4147 calib->low_fa = 0;
4148 calib->noise_ref = noise_ref;
4149
4150 if (calib->cck_state == IWN_CCK_STATE_HIFA) {
4151 /* Previous interval had many false alarms. */
4152 dec(calib->energy_cck, 8, energy_min);
4153 }
4154 calib->cck_state = IWN_CCK_STATE_INIT;
4155 }
4156
4157 if (needs_update)
4158 (void)iwn_send_sensitivity(sc);
4159 #undef dec
4160 #undef inc
4161 }
4162
4163 static int
4164 iwn_send_sensitivity(struct iwn_softc *sc)
4165 {
4166 const struct iwn_hal *hal = sc->sc_hal;
4167 struct iwn_calib_state *calib = &sc->calib;
4168 struct iwn_sensitivity_cmd cmd;
4169
4170 memset(&cmd, 0, sizeof cmd);
4171 cmd.which = IWN_SENSITIVITY_WORKTBL;
4172 /* OFDM modulation. */
4173 cmd.corr_ofdm_x1 = htole16(calib->ofdm_x1);
4174 cmd.corr_ofdm_mrc_x1 = htole16(calib->ofdm_mrc_x1);
4175 cmd.corr_ofdm_x4 = htole16(calib->ofdm_x4);
4176 cmd.corr_ofdm_mrc_x4 = htole16(calib->ofdm_mrc_x4);
4177 cmd.energy_ofdm = htole16(hal->limits->energy_ofdm);
4178 cmd.energy_ofdm_th = htole16(62);
4179 /* CCK modulation. */
4180 cmd.corr_cck_x4 = htole16(calib->cck_x4);
4181 cmd.corr_cck_mrc_x4 = htole16(calib->cck_mrc_x4);
4182 cmd.energy_cck = htole16(calib->energy_cck);
4183 /* Barker modulation: use default values. */
4184 cmd.corr_barker = htole16(190);
4185 cmd.corr_barker_mrc = htole16(390);
4186
4187 DPRINTFN(2, ("setting sensitivity %d/%d/%d/%d/%d/%d/%d\n",
4188 calib->ofdm_x1, calib->ofdm_mrc_x1, calib->ofdm_x4,
4189 calib->ofdm_mrc_x4, calib->cck_x4, calib->cck_mrc_x4,
4190 calib->energy_cck));
4191 return iwn_cmd(sc, IWN_CMD_SET_SENSITIVITY, &cmd, sizeof cmd, 1);
4192 }
4193
4194 #if 0
4195 /*
4196 * Set STA mode power saving level (between 0 and 5).
4197 * Level 0 is CAM (Continuously Aware Mode), 5 is for maximum power saving.
4198 */
4199 static int
4200 iwn_set_pslevel(struct iwn_softc *sc, int dtim, int level, int async)
4201 {
4202 struct iwn_pmgt_cmd cmd;
4203 const struct iwn_pmgt *pmgt;
4204 uint32_t umax, skip_dtim;
4205 pcireg_t reg;
4206 int i;
4207
4208 /* Select which PS parameters to use. */
4209 if (dtim <= 2)
4210 pmgt = &iwn_pmgt[0][level];
4211 else if (dtim <= 10)
4212 pmgt = &iwn_pmgt[1][level];
4213 else
4214 pmgt = &iwn_pmgt[2][level];
4215
4216 memset(&cmd, 0, sizeof cmd);
4217 if (level != 0) /* not CAM */
4218 cmd.flags |= htole16(IWN_PS_ALLOW_SLEEP);
4219 if (level == 5)
4220 cmd.flags |= htole16(IWN_PS_FAST_PD);
4221 /* Retrieve PCIe Active State Power Management (ASPM). */
4222 reg = pci_conf_read(sc->sc_pct, sc->sc_pcitag,
4223 sc->sc_cap_off + PCI_PCIE_LCSR);
4224 if (!(reg & PCI_PCIE_LCSR_ASPM_L0S)) /* L0s Entry disabled. */
4225 cmd.flags |= htole16(IWN_PS_PCI_PMGT);
4226 cmd.rxtimeout = htole32(pmgt->rxtimeout * 1024);
4227 cmd.txtimeout = htole32(pmgt->txtimeout * 1024);
4228
4229 if (dtim == 0) {
4230 dtim = 1;
4231 skip_dtim = 0;
4232 } else
4233 skip_dtim = pmgt->skip_dtim;
4234 if (skip_dtim != 0) {
4235 cmd.flags |= htole16(IWN_PS_SLEEP_OVER_DTIM);
4236 umax = pmgt->intval[4];
4237 if (umax == (uint32_t)-1)
4238 umax = dtim * (skip_dtim + 1);
4239 else if (umax > dtim)
4240 umax = (umax / dtim) * dtim;
4241 } else
4242 umax = dtim;
4243 for (i = 0; i < 5; i++)
4244 cmd.intval[i] = htole32(MIN(umax, pmgt->intval[i]));
4245
4246 DPRINTF(("setting power saving level to %d\n", level));
4247 return iwn_cmd(sc, IWN_CMD_SET_POWER_MODE, &cmd, sizeof cmd, async);
4248 }
4249 #endif
4250
4251 static int
4252 iwn_config(struct iwn_softc *sc)
4253 {
4254 const struct iwn_hal *hal = sc->sc_hal;
4255 struct ieee80211com *ic = &sc->sc_ic;
4256 struct ifnet *ifp = ic->ic_ifp;
4257 struct iwn_bluetooth bluetooth;
4258 uint16_t rxchain;
4259 int error;
4260 struct iwn_pmgt_cmd power;
4261
4262
4263 #if 0
4264 /* Set power saving level to CAM during initialization. */
4265 if ((error = iwn_set_pslevel(sc, 0, 0, 0)) != 0) {
4266 aprint_error_dev(sc->sc_dev,
4267 "could not set power saving level\n");
4268 return error;
4269 }
4270 #else
4271 /* set power mode */
4272 memset(&power, 0, sizeof power);
4273 power.flags = htole16(/*IWN_POWER_CAM*/0 | 0x8);
4274 DPRINTF(("setting power mode\n"));
4275 error = iwn_cmd(sc, IWN_CMD_SET_POWER_MODE, &power, sizeof power, 0);
4276 if (error != 0) {
4277 aprint_error_dev(sc->sc_dev, "could not set power mode\n");
4278 return error;
4279 }
4280 #endif
4281
4282 /* Configure bluetooth coexistence. */
4283 memset(&bluetooth, 0, sizeof bluetooth);
4284 bluetooth.flags = 3;
4285 bluetooth.lead = 0xaa;
4286 bluetooth.kill = 1;
4287 DPRINTF(("configuring bluetooth coexistence\n"));
4288 error = iwn_cmd(sc, IWN_CMD_BT_COEX, &bluetooth, sizeof bluetooth, 0);
4289 if (error != 0) {
4290 aprint_error_dev(sc->sc_dev,
4291 "could not configure bluetooth coexistence\n");
4292 return error;
4293 }
4294
4295 /* Configure adapter. */
4296 memset(&sc->rxon, 0, sizeof (struct iwn_rxon));
4297 IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
4298 IEEE80211_ADDR_COPY(sc->rxon.myaddr, ic->ic_myaddr);
4299 IEEE80211_ADDR_COPY(sc->rxon.wlap, ic->ic_myaddr);
4300 /* Set default channel. */
4301 sc->rxon.chan = htole16(ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
4302 sc->rxon.flags = htole32(IWN_RXON_TSF | IWN_RXON_CTS_TO_SELF);
4303 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_ibss_chan))
4304 sc->rxon.flags |= htole32(IWN_RXON_AUTO | IWN_RXON_24GHZ);
4305 switch (ic->ic_opmode) {
4306 case IEEE80211_M_STA:
4307 sc->rxon.mode = IWN_MODE_STA;
4308 sc->rxon.filter = htole32(IWN_FILTER_MULTICAST);
4309 break;
4310 case IEEE80211_M_MONITOR:
4311 sc->rxon.mode = IWN_MODE_MONITOR;
4312 sc->rxon.filter = htole32(IWN_FILTER_MULTICAST |
4313 IWN_FILTER_CTL | IWN_FILTER_PROMISC);
4314 break;
4315 default:
4316 /* Should not get there. */
4317 break;
4318 }
4319 sc->rxon.cck_mask = 0x0f; /* not yet negotiated */
4320 sc->rxon.ofdm_mask = 0xff; /* not yet negotiated */
4321 sc->rxon.ht_single_mask = 0xff;
4322 sc->rxon.ht_dual_mask = 0xff;
4323 rxchain = IWN_RXCHAIN_VALID(IWN_ANT_ABC) | IWN_RXCHAIN_IDLE_COUNT(2) |
4324 IWN_RXCHAIN_MIMO_COUNT(2);
4325 sc->rxon.rxchain = htole16(rxchain);
4326 DPRINTF(("setting configuration\n"));
4327 #ifdef notdef
4328 if (ic->ic_flags & IEEE80211_F_SHSLOT)
4329 sc->rxon.flags |= htole32(IWN_RXON_SHSLOT);
4330 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4331 sc->rxon.flags |= htole32(IWN_RXON_SHPREAMBLE);
4332 sc->rxon.filter &= ~htole32(IWN_FILTER_BSS);
4333 #endif
4334 DPRINTF(("rxon chan %d flags %x cck %x ofdm %x\n", sc->rxon.chan,
4335 sc->rxon.flags, sc->rxon.cck_mask, sc->rxon.ofdm_mask));
4336 error = iwn_cmd(sc, IWN_CMD_CONFIGURE, &sc->rxon, hal->rxonsz, 0);
4337 if (error != 0) {
4338 aprint_error_dev(sc->sc_dev, "configure command failed\n");
4339 return error;
4340 }
4341
4342 /* Configuration has changed, set TX power accordingly. */
4343 if ((error = hal->set_txpower(sc, 0)) != 0) {
4344 aprint_error_dev(sc->sc_dev, "could not set TX power\n");
4345 return error;
4346 }
4347
4348 if ((error = iwn_add_broadcast_node(sc, 0)) != 0) {
4349 aprint_error_dev(sc->sc_dev, "could not add broadcast node\n");
4350 return error;
4351 }
4352
4353 if ((error = iwn_set_critical_temp(sc)) != 0) {
4354 aprint_error_dev(sc->sc_dev,
4355 "could not set critical temperature\n");
4356 return error;
4357 }
4358 return 0;
4359 }
4360
4361 static int
4362 iwn_scan(struct iwn_softc *sc, uint16_t flags)
4363 {
4364 struct ieee80211com *ic = &sc->sc_ic;
4365 struct iwn_scan_hdr *hdr;
4366 struct iwn_cmd_data *tx;
4367 struct iwn_scan_chan *chan;
4368 struct ieee80211_frame *wh;
4369 struct ieee80211_rateset *rs;
4370 struct ieee80211_channel *c;
4371 enum ieee80211_phymode mode;
4372 uint8_t *buf, *frm;
4373 uint16_t rxchain;
4374 uint8_t txant;
4375 int buflen, error, nrates;
4376
4377 buf = malloc(IWN_SCAN_MAXSZ, M_DEVBUF, M_NOWAIT | M_ZERO);
4378 if (buf == NULL) {
4379 aprint_error_dev(sc->sc_dev,
4380 "could not allocate buffer for scan command\n");
4381 return ENOMEM;
4382 }
4383 hdr = (struct iwn_scan_hdr *)buf;
4384 /*
4385 * Move to the next channel if no frames are received within 10ms
4386 * after sending the probe request.
4387 */
4388 hdr->quiet_time = htole16(10); /* timeout in milliseconds */
4389 hdr->quiet_threshold = htole16(1); /* min # of packets */
4390
4391 /* Select antennas for scanning. */
4392 rxchain = IWN_RXCHAIN_FORCE | IWN_RXCHAIN_VALID(IWN_ANT_ABC) |
4393 IWN_RXCHAIN_MIMO(IWN_ANT_ABC);
4394 if ((flags & IEEE80211_CHAN_5GHZ) &&
4395 sc->hw_type == IWN_HW_REV_TYPE_4965) {
4396 /* Ant A must be avoided in 5GHz because of an HW bug. */
4397 rxchain |= IWN_RXCHAIN_SEL(IWN_ANT_B | IWN_ANT_C);
4398 } else /* Use all available RX antennas. */
4399 rxchain |= IWN_RXCHAIN_SEL(IWN_ANT_ABC);
4400 hdr->rxchain = htole16(rxchain);
4401 hdr->filter = htole32(IWN_FILTER_MULTICAST | IWN_FILTER_BEACON);
4402
4403 tx = &(hdr->tx_cmd);
4404 tx->flags = htole32(IWN_TX_AUTO_SEQ);
4405 tx->id = sc->sc_hal->broadcast_id;
4406 tx->lifetime = htole32(IWN_LIFETIME_INFINITE);
4407
4408 if (flags & IEEE80211_CHAN_5GHZ) {
4409 hdr->crc_threshold = htole16(1);
4410 /* Send probe requests at 6Mbps. */
4411 tx->plcp = iwn_rates[IWN_RIDX_OFDM6].plcp;
4412 rs = &ic->ic_sup_rates[IEEE80211_MODE_11A];
4413 } else {
4414 hdr->flags = htole32(IWN_RXON_24GHZ | IWN_RXON_AUTO);
4415 /* Send probe requests at 1Mbps. */
4416 tx->plcp = iwn_rates[IWN_RIDX_CCK1].plcp;
4417 tx->rflags = IWN_RFLAG_CCK;
4418 rs = &ic->ic_sup_rates[IEEE80211_MODE_11G];
4419 }
4420 /* Use the first valid TX antenna. */
4421 txant = IWN_LSB(sc->txantmsk);
4422 tx->rflags |= IWN_RFLAG_ANT(txant);
4423
4424 if (ic->ic_des_esslen != 0) {
4425 hdr->scan_essid[0].id = IEEE80211_ELEMID_SSID;
4426 hdr->scan_essid[0].len = ic->ic_des_esslen;
4427 memcpy(hdr->scan_essid[0].data, ic->ic_des_essid, ic->ic_des_esslen);
4428 }
4429 /*
4430 * Build a probe request frame. Most of the following code is a
4431 * copy & paste of what is done in net80211.
4432 */
4433 wh = &(hdr->wh);
4434 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
4435 IEEE80211_FC0_SUBTYPE_PROBE_REQ;
4436 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
4437 IEEE80211_ADDR_COPY(wh->i_addr1, etherbroadcastaddr);
4438 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
4439 IEEE80211_ADDR_COPY(wh->i_addr3, etherbroadcastaddr);
4440 *(uint16_t *)&wh->i_dur[0] = 0; /* filled by HW */
4441 *(uint16_t *)&wh->i_seq[0] = 0; /* filled by HW */
4442
4443 frm = &(hdr->data[0]);
4444 /* add empty SSID IE */
4445 *frm++ = IEEE80211_ELEMID_SSID;
4446 *frm++ = 0;
4447
4448 mode = ieee80211_chan2mode(ic, ic->ic_ibss_chan);
4449 rs = &ic->ic_sup_rates[mode];
4450
4451 /* add supported rates IE */
4452 *frm++ = IEEE80211_ELEMID_RATES;
4453 nrates = rs->rs_nrates;
4454 if (nrates > IEEE80211_RATE_SIZE)
4455 nrates = IEEE80211_RATE_SIZE;
4456 *frm++ = nrates;
4457 memcpy(frm, rs->rs_rates, nrates);
4458 frm += nrates;
4459
4460 if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
4461 nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
4462 *frm++ = IEEE80211_ELEMID_XRATES;
4463 *frm++ = nrates;
4464 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
4465 frm += nrates;
4466 }
4467
4468 /* Set length of probe request. */
4469 tx->len = htole16(frm - (uint8_t *)wh);
4470
4471 chan = (struct iwn_scan_chan *)frm;
4472 for (c = &ic->ic_channels[1];
4473 c <= &ic->ic_channels[IEEE80211_CHAN_MAX]; c++) {
4474 if ((c->ic_flags & flags) != flags)
4475 continue;
4476
4477 chan->chan = htole16(ieee80211_chan2ieee(ic, c));
4478 DPRINTFN(2, ("adding channel %d\n", chan->chan));
4479 chan->flags = 0;
4480 if (!(c->ic_flags & IEEE80211_CHAN_PASSIVE))
4481 chan->flags |= htole32(IWN_CHAN_ACTIVE);
4482 if (ic->ic_des_esslen != 0)
4483 chan->flags |= htole32(IWN_CHAN_NPBREQS(1));
4484 chan->dsp_gain = 0x6e;
4485 if (IEEE80211_IS_CHAN_5GHZ(c)) {
4486 chan->rf_gain = 0x3b;
4487 chan->active = htole16(24);
4488 chan->passive = htole16(110);
4489 } else {
4490 chan->rf_gain = 0x28;
4491 chan->active = htole16(36);
4492 chan->passive = htole16(120);
4493 }
4494 hdr->nchan++;
4495 chan++;
4496 }
4497
4498 buflen = (uint8_t *)chan - buf;
4499 hdr->len = htole16(buflen);
4500
4501 DPRINTF(("sending scan command nchan=%d\n", hdr->nchan));
4502 error = iwn_cmd(sc, IWN_CMD_SCAN, buf, buflen, 1);
4503 free(buf, M_DEVBUF);
4504 return error;
4505 }
4506
4507 static int
4508 iwn_auth(struct iwn_softc *sc)
4509 {
4510 const struct iwn_hal *hal = sc->sc_hal;
4511 struct ieee80211com *ic = &sc->sc_ic;
4512 struct ieee80211_node *ni = ic->ic_bss;
4513 int error;
4514
4515 sc->calib.state = IWN_CALIB_STATE_INIT;
4516
4517 /* Update adapter's configuration. */
4518 sc->rxon.associd = 0;
4519 IEEE80211_ADDR_COPY(sc->rxon.bssid, ni->ni_bssid);
4520 sc->rxon.chan = htole16(ieee80211_chan2ieee(ic, ni->ni_chan));
4521 sc->rxon.flags = htole32(IWN_RXON_TSF | IWN_RXON_CTS_TO_SELF);
4522 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
4523 sc->rxon.flags |= htole32(IWN_RXON_AUTO | IWN_RXON_24GHZ);
4524 if (ic->ic_flags & IEEE80211_F_SHSLOT)
4525 sc->rxon.flags |= htole32(IWN_RXON_SHSLOT);
4526 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4527 sc->rxon.flags |= htole32(IWN_RXON_SHPREAMBLE);
4528 switch (ic->ic_curmode) {
4529 case IEEE80211_MODE_11A:
4530 sc->rxon.cck_mask = 0;
4531 sc->rxon.ofdm_mask = 0x15;
4532 break;
4533 case IEEE80211_MODE_11B:
4534 sc->rxon.cck_mask = 0x03;
4535 sc->rxon.ofdm_mask = 0;
4536 break;
4537 default: /* Assume 802.11b/g. */
4538 sc->rxon.cck_mask = 0x0f;
4539 sc->rxon.ofdm_mask = 0x15;
4540 break;
4541 }
4542 #if 1
4543 DPRINTF(("rxon chan %d flags %x cck %x ofdm %x\n", sc->rxon.chan,
4544 sc->rxon.flags, sc->rxon.cck_mask, sc->rxon.ofdm_mask));
4545 error = iwn_cmd(sc, IWN_CMD_CONFIGURE, &sc->rxon, hal->rxonsz, 1);
4546 if (error != 0) {
4547 aprint_error_dev(sc->sc_dev, "could not configure\n");
4548 return error;
4549 }
4550
4551 /* Configuration has changed, set TX power accordingly. */
4552 if ((error = hal->set_txpower(sc, 1)) != 0) {
4553 aprint_error_dev(sc->sc_dev, "could not set TX power\n");
4554 return error;
4555 }
4556 /*
4557 * Reconfiguring RXON clears the firmware's nodes table so we must
4558 * add the broadcast node again.
4559 */
4560 if ((error = iwn_add_broadcast_node(sc, 1)) != 0) {
4561 aprint_error_dev(sc->sc_dev, "could not add broadcast node\n");
4562 return error;
4563 }
4564 #else
4565 /* iwn_enable_tsf(sc, ni);*/
4566 if (ic->ic_flags & IEEE80211_F_SHSLOT)
4567 sc->rxon.flags |= htole32(IWN_RXON_SHSLOT);
4568 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4569 sc->rxon.flags |= htole32(IWN_RXON_SHPREAMBLE);
4570 sc->rxon.filter &= ~htole32(IWN_FILTER_BSS);
4571
4572 DPRINTF(("rxon chan %d flags %x cck %x ofdm %x\n", sc->rxon.chan,
4573 sc->rxon.flags, sc->rxon.cck_mask, sc->rxon.ofdm_mask));
4574 error = iwn_cmd(sc, IWN_CMD_CONFIGURE, &sc->rxon, hal->rxonsz, 1);
4575 if (error != 0) {
4576 aprint_error_dev(sc->sc_dev, "could not configure\n");
4577 return error;
4578 }
4579
4580 /* Configuration has changed, set TX power accordingly. */
4581 if ((error = hal->set_txpower(sc, 1)) != 0) {
4582 aprint_error_dev(sc->sc_dev, "could not set TX power\n");
4583 return error;
4584 }
4585 /*
4586 * Reconfiguring RXON clears the firmware's nodes table so we must
4587 * add the broadcast node again.
4588 */
4589 if ((error = iwn_add_broadcast_node(sc, 1)) != 0) {
4590 aprint_error_dev(sc->sc_dev, "could not add broadcast node\n");
4591 return error;
4592 }
4593 /* add BSS node */
4594 DPRINTF(("adding BSS node from auth\n"));
4595 if ((error = iwn_add_node(sc, ni, false, true, 0)) != 0)
4596 return error;
4597
4598 if (ic->ic_opmode == IEEE80211_M_STA) {
4599 /* fake a join to init the tx rate */
4600 iwn_newassoc(ni, 1);
4601 }
4602
4603 if ((error = iwn_init_sensitivity(sc)) != 0) {
4604 aprint_error_dev(sc->sc_dev, "could not set sensitivity\n");
4605 return error;
4606 }
4607 #endif
4608 return 0;
4609 }
4610
4611 static int
4612 iwn_run(struct iwn_softc *sc)
4613 {
4614 const struct iwn_hal *hal = sc->sc_hal;
4615 struct ieee80211com *ic = &sc->sc_ic;
4616 struct ieee80211_node *ni = ic->ic_bss;
4617 int error;
4618
4619 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
4620 /* Link LED blinks while monitoring. */
4621 iwn_set_led(sc, IWN_LED_LINK, 5, 5);
4622 return 0;
4623 }
4624 if ((error = iwn_set_timing(sc, ni)) != 0) {
4625 aprint_error_dev(sc->sc_dev, "could not set timing\n");
4626 return error;
4627 }
4628
4629 /* Update adapter's configuration. */
4630 sc->rxon.associd = htole16(IEEE80211_AID(ni->ni_associd));
4631 /* Short preamble and slot time are negotiated when associating. */
4632 sc->rxon.flags &= ~htole32(IWN_RXON_SHPREAMBLE | IWN_RXON_SHSLOT);
4633 if (ic->ic_flags & IEEE80211_F_SHSLOT)
4634 sc->rxon.flags |= htole32(IWN_RXON_SHSLOT);
4635 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4636 sc->rxon.flags |= htole32(IWN_RXON_SHPREAMBLE);
4637 sc->rxon.filter |= htole32(IWN_FILTER_BSS);
4638 DPRINTF(("rxon chan %d flags %x\n", sc->rxon.chan, sc->rxon.flags));
4639 error = iwn_cmd(sc, IWN_CMD_CONFIGURE, &sc->rxon, hal->rxonsz, 1);
4640 if (error != 0) {
4641 aprint_error_dev(sc->sc_dev,
4642 "could not update configuration\n");
4643 return error;
4644 }
4645
4646 /* Configuration has changed, set TX power accordingly. */
4647 if ((error = hal->set_txpower(sc, 1)) != 0) {
4648 aprint_error_dev(sc->sc_dev, "could not set TX power\n");
4649 return error;
4650 }
4651
4652 /* Fake a join to initialize the TX rate. */
4653 ((struct iwn_node *)ni)->id = IWN_ID_BSS;
4654 iwn_newassoc(ni, 1);
4655
4656 /* Add BSS node. */
4657 iwn_add_node(sc, ni, false, true, 0);
4658 /* Start periodic calibration timer. */
4659 sc->calib.state = IWN_CALIB_STATE_ASSOC;
4660 sc->calib_cnt = 0;
4661 callout_schedule(&sc->calib_to, hz / 2);
4662
4663 /* Link LED always on while associated. */
4664 iwn_set_led(sc, IWN_LED_LINK, 0, 1);
4665 return 0;
4666 }
4667
4668 static int
4669 iwn_wme_update(struct ieee80211com *ic)
4670 {
4671 #define IWN_EXP2(v) htole16((1 << (v)) - 1)
4672 #define IWN_USEC(v) htole16(IEEE80211_TXOP_TO_US(v))
4673 struct iwn_softc *sc = ic->ic_ifp->if_softc;
4674 const struct wmeParams *wmep;
4675 struct iwn_edca_params cmd;
4676 int ac;
4677
4678 /* don't override default WME values if WME is not actually enabled */
4679 if (!(ic->ic_flags & IEEE80211_F_WME))
4680 return 0;
4681 cmd.flags = 0;
4682 for (ac = 0; ac < WME_NUM_AC; ac++) {
4683 wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
4684 cmd.ac[ac].aifsn = wmep->wmep_aifsn;
4685 cmd.ac[ac].cwmin = IWN_EXP2(wmep->wmep_logcwmin);
4686 cmd.ac[ac].cwmax = IWN_EXP2(wmep->wmep_logcwmax);
4687 cmd.ac[ac].txoplimit = IWN_USEC(wmep->wmep_txopLimit);
4688
4689 DPRINTF(("setting WME for queue %d aifsn=%d cwmin=%d cwmax=%d "
4690 "txop=%d\n", ac, cmd.ac[ac].aifsn,
4691 cmd.ac[ac].cwmin,
4692 cmd.ac[ac].cwmax, cmd.ac[ac].txoplimit));
4693 }
4694 return iwn_cmd(sc, IWN_CMD_EDCA_PARAMS, &cmd, sizeof cmd, 1);
4695 #undef IWN_USEC
4696 #undef IWN_EXP2
4697 }
4698
4699 #if 0
4700 /*
4701 * We support CCMP hardware encryption/decryption of unicast frames only.
4702 * HW support for TKIP really sucks. We should let TKIP die anyway.
4703 */
4704 static int
4705 iwn_set_key(struct ieee80211com *ic, struct ieee80211_node *ni,
4706 struct ieee80211_key *k)
4707 {
4708 struct iwn_softc *sc = ic->ic_softc;
4709 const struct iwn_hal *hal = sc->sc_hal;
4710 struct iwn_node *wn = (void *)ni;
4711 struct iwn_node_info node;
4712 uint16_t kflags;
4713
4714 if ((k->k_flags & IEEE80211_KEY_GROUP) ||
4715 k->k_cipher != IEEE80211_CIPHER_CCMP)
4716 return ieee80211_set_key(ic, ni, k);
4717
4718 kflags = IWN_KFLAG_CCMP | IWN_KFLAG_MAP | IWN_KFLAG_KID(k->k_id);
4719 if (k->k_flags & IEEE80211_KEY_GROUP)
4720 kflags |= IWN_KFLAG_GROUP;
4721
4722 memset(&node, 0, sizeof node);
4723 node.id = (k->k_flags & IEEE80211_KEY_GROUP) ?
4724 hal->broadcast_id : wn->id;
4725 node.control = IWN_NODE_UPDATE;
4726 node.flags = IWN_FLAG_SET_KEY;
4727 node.kflags = htole16(kflags);
4728 node.kid = k->k_id;
4729 memcpy(node.key, k->k_key, k->k_len);
4730 DPRINTF(("set key id=%d for node %d\n", k->k_id, node.id));
4731 return hal->add_node(sc, &node, 1);
4732 }
4733
4734 static void
4735 iwn_delete_key(struct ieee80211com *ic, struct ieee80211_node *ni,
4736 struct ieee80211_key *k)
4737 {
4738 struct iwn_softc *sc = ic->ic_softc;
4739 const struct iwn_hal *hal = sc->sc_hal;
4740 struct iwn_node *wn = (void *)ni;
4741 struct iwn_node_info node;
4742
4743 if ((k->k_flags & IEEE80211_KEY_GROUP) ||
4744 k->k_cipher != IEEE80211_CIPHER_CCMP) {
4745 /* See comment about other ciphers above. */
4746 ieee80211_delete_key(ic, ni, k);
4747 return;
4748 }
4749 if (ic->ic_state != IEEE80211_S_RUN)
4750 return; /* Nothing to do. */
4751 memset(&node, 0, sizeof node);
4752 node.id = (k->k_flags & IEEE80211_KEY_GROUP) ?
4753 hal->broadcast_id : wn->id;
4754 node.control = IWN_NODE_UPDATE;
4755 node.flags = IWN_FLAG_SET_KEY;
4756 node.kflags = htole16(IWN_KFLAG_INVALID);
4757 node.kid = 0xff;
4758 DPRINTF(("delete keys for node %d\n", node.id));
4759 (void)hal->add_node(sc, &node, 1);
4760 }
4761 #endif
4762
4763 #ifndef IEEE80211_NO_HT
4764 /*
4765 * This function is called by upper layer when a ADDBA request is received
4766 * from another STA and before the ADDBA response is sent.
4767 */
4768 static int
4769 iwn_ampdu_rx_start(struct ieee80211com *ic, struct ieee80211_node *ni,
4770 uint8_t tid, uint16_t ssn)
4771 {
4772 struct iwn_softc *sc = ic->ic_softc;
4773 struct iwn_node *wn = (void *)ni;
4774 struct iwn_node_info node;
4775
4776 memset(&node, 0, sizeof node);
4777 node.id = wn->id;
4778 node.control = IWN_NODE_UPDATE;
4779 node.flags = IWN_FLAG_SET_ADDBA;
4780 node.addba_tid = tid;
4781 node.addba_ssn = htole16(ssn);
4782 DPRINTFN(2, ("ADDBA RA=%d TID=%d SSN=%d\n", wn->id, tid, ssn));
4783 return sc->sc_hal->add_node(sc, &node, 1);
4784 }
4785
4786 /*
4787 * This function is called by upper layer on teardown of an HT-immediate
4788 * Block Ack (eg. uppon receipt of a DELBA frame.)
4789 */
4790 static void
4791 iwn_ampdu_rx_stop(struct ieee80211com *ic, struct ieee80211_node *ni,
4792 uint8_t tid, uint16_t ssn)
4793 {
4794 struct iwn_softc *sc = ic->ic_softc;
4795 struct iwn_node *wn = (void *)ni;
4796 struct iwn_node_info node;
4797
4798 memset(&node, 0, sizeof node);
4799 node.id = wn->id;
4800 node.control = IWN_NODE_UPDATE;
4801 node.flags = IWN_FLAG_SET_DELBA;
4802 node.delba_tid = tid;
4803 DPRINTFN(2, ("DELBA RA=%d TID=%d\n", wn->id, tid));
4804 (void)sc->sc_hal->add_node(sc, &node, 1);
4805 }
4806
4807 /*
4808 * This function is called by upper layer when a ADDBA response is received
4809 * from another STA.
4810 */
4811 static int
4812 iwn_ampdu_tx_start(struct ieee80211com *ic, struct ieee80211_node *ni,
4813 uint8_t tid, uint16_t ssn)
4814 {
4815 struct iwn_softc *sc = ic->ic_softc;
4816 const struct iwn_hal *hal = sc->sc_hal;
4817 struct iwn_node *wn = (void *)ni;
4818 struct iwn_node_info node;
4819 int error;
4820
4821 /* Enable TX for the specified RA/TID. */
4822 wn->disable_tid &= ~(1 << tid);
4823 memset(&node, 0, sizeof node);
4824 node.id = wn->id;
4825 node.control = IWN_NODE_UPDATE;
4826 node.flags = IWN_FLAG_SET_DISABLE_TID;
4827 node.disable_tid = htole16(wn->disable_tid);
4828 error = hal->add_node(sc, &node, 1);
4829 if (error != 0)
4830 return error;
4831
4832 if ((error = iwn_nic_lock(sc)) != 0)
4833 return error;
4834 hal->ampdu_tx_start(sc, ni, tid, ssn);
4835 iwn_nic_unlock(sc);
4836 return 0;
4837 }
4838
4839 static void
4840 iwn_ampdu_tx_stop(struct ieee80211com *ic, struct ieee80211_node *ni,
4841 uint8_t tid, uint16_t ssn)
4842 {
4843 struct iwn_softc *sc = ic->ic_softc;
4844
4845 if (iwn_nic_lock(sc) != 0)
4846 return;
4847 sc->sc_hal->ampdu_tx_stop(sc, tid, ssn);
4848 iwn_nic_unlock(sc);
4849 }
4850
4851 static void
4852 iwn4965_ampdu_tx_start(struct iwn_softc *sc, struct ieee80211_node *ni,
4853 uint8_t tid, uint16_t ssn)
4854 {
4855 struct iwn_node *wn = (void *)ni;
4856 int qid = 7 + tid;
4857
4858 /* Stop TX scheduler while we're changing its configuration. */
4859 iwn_prph_write(sc, IWN4965_SCHED_QUEUE_STATUS(qid),
4860 IWN4965_TXQ_STATUS_CHGACT);
4861
4862 /* Assign RA/TID translation to the queue. */
4863 iwn_mem_write_2(sc, sc->sched_base + IWN4965_SCHED_TRANS_TBL(qid),
4864 wn->id << 4 | tid);
4865
4866 /* Enable chain mode for the queue. */
4867 iwn_prph_setbits(sc, IWN4965_SCHED_QCHAIN_SEL, 1 << qid);
4868
4869 /* Set starting sequence number from the ADDBA request. */
4870 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, ssn);
4871 iwn_prph_write(sc, IWN4965_SCHED_QUEUE_RDPTR(qid), ssn);
4872
4873 /* Set scheduler window size. */
4874 iwn_mem_write(sc, sc->sched_base + IWN4965_SCHED_QUEUE_OFFSET(qid),
4875 IWN_SCHED_WINSZ);
4876 /* Set scheduler frame limit. */
4877 iwn_mem_write(sc, sc->sched_base + IWN4965_SCHED_QUEUE_OFFSET(qid) + 4,
4878 IWN_SCHED_LIMIT << 16);
4879
4880 /* Enable interrupts for the queue. */
4881 iwn_prph_setbits(sc, IWN4965_SCHED_INTR_MASK, 1 << qid);
4882
4883 /* Mark the queue as active. */
4884 iwn_prph_write(sc, IWN4965_SCHED_QUEUE_STATUS(qid),
4885 IWN4965_TXQ_STATUS_ACTIVE | IWN4965_TXQ_STATUS_AGGR_ENA |
4886 iwn_tid2fifo[tid] << 1);
4887 }
4888
4889 static void
4890 iwn4965_ampdu_tx_stop(struct iwn_softc *sc, uint8_t tid, uint16_t ssn)
4891 {
4892 int qid = 7 + tid;
4893
4894 /* Stop TX scheduler while we're changing its configuration. */
4895 iwn_prph_write(sc, IWN4965_SCHED_QUEUE_STATUS(qid),
4896 IWN4965_TXQ_STATUS_CHGACT);
4897
4898 /* Set starting sequence number from the ADDBA request. */
4899 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, ssn);
4900 iwn_prph_write(sc, IWN4965_SCHED_QUEUE_RDPTR(qid), ssn);
4901
4902 /* Disable interrupts for the queue. */
4903 iwn_prph_clrbits(sc, IWN4965_SCHED_INTR_MASK, 1 << qid);
4904
4905 /* Mark the queue as inactive. */
4906 iwn_prph_write(sc, IWN4965_SCHED_QUEUE_STATUS(qid),
4907 IWN4965_TXQ_STATUS_INACTIVE | iwn_tid2fifo[tid] << 1);
4908 }
4909
4910 static void
4911 iwn5000_ampdu_tx_start(struct iwn_softc *sc, struct ieee80211_node *ni,
4912 uint8_t tid, uint16_t ssn)
4913 {
4914 struct iwn_node *wn = (void *)ni;
4915 int qid = 10 + tid;
4916
4917 /* Stop TX scheduler while we're changing its configuration. */
4918 iwn_prph_write(sc, IWN5000_SCHED_QUEUE_STATUS(qid),
4919 IWN5000_TXQ_STATUS_CHGACT);
4920
4921 /* Assign RA/TID translation to the queue. */
4922 iwn_mem_write_2(sc, sc->sched_base + IWN5000_SCHED_TRANS_TBL(qid),
4923 wn->id << 4 | tid);
4924
4925 /* Enable chain mode for the queue. */
4926 iwn_prph_setbits(sc, IWN5000_SCHED_QCHAIN_SEL, 1 << qid);
4927
4928 /* Enable aggregation for the queue. */
4929 iwn_prph_setbits(sc, IWN5000_SCHED_AGGR_SEL, 1 << qid);
4930
4931 /* Set starting sequence number from the ADDBA request. */
4932 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, ssn);
4933 iwn_prph_write(sc, IWN5000_SCHED_QUEUE_RDPTR(qid), ssn);
4934
4935 /* Set scheduler window size and frame limit. */
4936 iwn_mem_write(sc, sc->sched_base + IWN5000_SCHED_QUEUE_OFFSET(qid) + 4,
4937 IWN_SCHED_LIMIT << 16 | IWN_SCHED_WINSZ);
4938
4939 /* Enable interrupts for the queue. */
4940 iwn_prph_setbits(sc, IWN5000_SCHED_INTR_MASK, 1 << qid);
4941
4942 /* Mark the queue as active. */
4943 iwn_prph_write(sc, IWN5000_SCHED_QUEUE_STATUS(qid),
4944 IWN5000_TXQ_STATUS_ACTIVE | iwn_tid2fifo[tid]);
4945 }
4946
4947 static void
4948 iwn5000_ampdu_tx_stop(struct iwn_softc *sc, uint8_t tid, uint16_t ssn)
4949 {
4950 int qid = 10 + tid;
4951
4952 /* Stop TX scheduler while we're changing its configuration. */
4953 iwn_prph_write(sc, IWN5000_SCHED_QUEUE_STATUS(qid),
4954 IWN5000_TXQ_STATUS_CHGACT);
4955
4956 /* Disable aggregation for the queue. */
4957 iwn_prph_clrbits(sc, IWN5000_SCHED_AGGR_SEL, 1 << qid);
4958
4959 /* Set starting sequence number from the ADDBA request. */
4960 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, ssn);
4961 iwn_prph_write(sc, IWN5000_SCHED_QUEUE_RDPTR(qid), ssn);
4962
4963 /* Disable interrupts for the queue. */
4964 iwn_prph_clrbits(sc, IWN5000_SCHED_INTR_MASK, 1 << qid);
4965
4966 /* Mark the queue as inactive. */
4967 iwn_prph_write(sc, IWN5000_SCHED_QUEUE_STATUS(qid),
4968 IWN5000_TXQ_STATUS_INACTIVE | iwn_tid2fifo[tid]);
4969 }
4970 #endif /* 0 */
4971
4972 /*
4973 * Query calibration tables from the initialization firmware. We do this
4974 * only once at first boot. Called from a process context.
4975 */
4976 static int
4977 iwn5000_query_calibration(struct iwn_softc *sc)
4978 {
4979 struct iwn5000_calib_config cmd;
4980 int error;
4981
4982 memset(&cmd, 0, sizeof cmd);
4983 cmd.ucode.once.enable = 0xffffffff;
4984 cmd.ucode.once.start = 0xffffffff;
4985 cmd.ucode.once.send = 0xffffffff;
4986 cmd.ucode.flags = 0xffffffff;
4987 DPRINTF(("sending calibration query\n"));
4988 error = iwn_cmd(sc, IWN5000_CMD_CALIB_CONFIG, &cmd, sizeof cmd, 0);
4989 if (error != 0)
4990 return error;
4991
4992 /* Wait at most two seconds for calibration to complete. */
4993 return tsleep(sc, PCATCH, "iwncal", 2 * hz);
4994 }
4995
4996 /*
4997 * Send calibration results to the runtime firmware. These results were
4998 * obtained on first boot from the initialization firmware.
4999 */
5000 static int
5001 iwn5000_send_calibration(struct iwn_softc *sc)
5002 {
5003 int idx, error;
5004
5005 for (idx = 0; idx < 5; idx++) {
5006 if (sc->calibcmd[idx].buf == NULL)
5007 continue; /* No results available. */
5008 DPRINTF(("send calibration result idx=%d len=%d\n",
5009 idx, sc->calibcmd[idx].len));
5010 error = iwn_cmd(sc, IWN_CMD_PHY_CALIB, sc->calibcmd[idx].buf,
5011 sc->calibcmd[idx].len, 0);
5012 if (error != 0) {
5013 aprint_error_dev(sc->sc_dev,
5014 "could not send calibration result\n");
5015 return error;
5016 }
5017 }
5018 return 0;
5019 }
5020
5021 /*
5022 * This function is called after the runtime firmware notifies us of its
5023 * readiness (called in a process context.)
5024 */
5025 static int
5026 iwn4965_post_alive(struct iwn_softc *sc)
5027 {
5028 int error, qid;
5029
5030 if ((error = iwn_nic_lock(sc)) != 0)
5031 return error;
5032
5033 /* Clear TX scheduler's state in SRAM. */
5034 sc->sched_base = iwn_prph_read(sc, IWN_SCHED_SRAM_ADDR);
5035 iwn_mem_set_region_4(sc, sc->sched_base + IWN4965_SCHED_CTX_OFF, 0,
5036 IWN4965_SCHED_CTX_LEN);
5037
5038 /* Set physical address of TX scheduler rings (1KB aligned.) */
5039 iwn_prph_write(sc, IWN4965_SCHED_DRAM_ADDR, sc->sched_dma.paddr >> 10);
5040
5041 IWN_SETBITS(sc, IWN_FH_TX_CHICKEN, IWN_FH_TX_CHICKEN_SCHED_RETRY);
5042
5043 /* Disable chain mode for all our 16 queues. */
5044 iwn_prph_write(sc, IWN4965_SCHED_QCHAIN_SEL, 0);
5045
5046 for (qid = 0; qid < IWN4965_NTXQUEUES; qid++) {
5047 iwn_prph_write(sc, IWN4965_SCHED_QUEUE_RDPTR(qid), 0);
5048 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, qid << 8 | 0);
5049
5050 /* Set scheduler window size. */
5051 iwn_mem_write(sc, sc->sched_base +
5052 IWN4965_SCHED_QUEUE_OFFSET(qid), IWN_SCHED_WINSZ);
5053 /* Set scheduler frame limit. */
5054 iwn_mem_write(sc, sc->sched_base +
5055 IWN4965_SCHED_QUEUE_OFFSET(qid) + 4,
5056 IWN_SCHED_LIMIT << 16);
5057 }
5058
5059 /* Enable interrupts for all our 16 queues. */
5060 iwn_prph_write(sc, IWN4965_SCHED_INTR_MASK, 0xffff);
5061 /* Identify TX FIFO rings (0-7). */
5062 iwn_prph_write(sc, IWN4965_SCHED_TXFACT, 0xff);
5063
5064 /* Mark TX rings (4 EDCA + cmd + 2 HCCA) as active. */
5065 for (qid = 0; qid < 7; qid++) {
5066 static uint8_t qid2fifo[] = { 3, 2, 1, 0, 4, 5, 6 };
5067 iwn_prph_write(sc, IWN4965_SCHED_QUEUE_STATUS(qid),
5068 IWN4965_TXQ_STATUS_ACTIVE | qid2fifo[qid] << 1);
5069 }
5070 iwn_nic_unlock(sc);
5071 return 0;
5072 }
5073
5074 /*
5075 * This function is called after the initialization or runtime firmware
5076 * notifies us of its readiness (called in a process context.)
5077 */
5078 static int
5079 iwn5000_post_alive(struct iwn_softc *sc)
5080 {
5081 struct iwn5000_wimax_coex wimax;
5082 int error, qid;
5083
5084 if ((error = iwn_nic_lock(sc)) != 0)
5085 return error;
5086
5087 /* Clear TX scheduler's state in SRAM. */
5088 sc->sched_base = iwn_prph_read(sc, IWN_SCHED_SRAM_ADDR);
5089 iwn_mem_set_region_4(sc, sc->sched_base + IWN5000_SCHED_CTX_OFF, 0,
5090 IWN5000_SCHED_CTX_LEN);
5091
5092 /* Set physical address of TX scheduler rings (1KB aligned.) */
5093 iwn_prph_write(sc, IWN5000_SCHED_DRAM_ADDR, sc->sched_dma.paddr >> 10);
5094
5095 IWN_SETBITS(sc, IWN_FH_TX_CHICKEN, IWN_FH_TX_CHICKEN_SCHED_RETRY);
5096
5097 /* Enable chain mode for all our 20 queues. */
5098 iwn_prph_write(sc, IWN5000_SCHED_QCHAIN_SEL, 0xfffff);
5099 iwn_prph_write(sc, IWN5000_SCHED_AGGR_SEL, 0);
5100
5101 for (qid = 0; qid < IWN5000_NTXQUEUES; qid++) {
5102 iwn_prph_write(sc, IWN5000_SCHED_QUEUE_RDPTR(qid), 0);
5103 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, qid << 8 | 0);
5104
5105 iwn_mem_write(sc, sc->sched_base +
5106 IWN5000_SCHED_QUEUE_OFFSET(qid), 0);
5107 /* Set scheduler window size and frame limit. */
5108 iwn_mem_write(sc, sc->sched_base +
5109 IWN5000_SCHED_QUEUE_OFFSET(qid) + 4,
5110 IWN_SCHED_LIMIT << 16 | IWN_SCHED_WINSZ);
5111 }
5112
5113 /* Enable interrupts for all our 20 queues. */
5114 iwn_prph_write(sc, IWN5000_SCHED_INTR_MASK, 0xfffff);
5115 /* Identify TX FIFO rings (0-7). */
5116 iwn_prph_write(sc, IWN5000_SCHED_TXFACT, 0xff);
5117
5118 /* Mark TX rings (4 EDCA + cmd + 2 HCCA) as active. */
5119 for (qid = 0; qid < 7; qid++) {
5120 static uint8_t qid2fifo[] = { 3, 2, 1, 0, 7, 5, 6 };
5121 iwn_prph_write(sc, IWN5000_SCHED_QUEUE_STATUS(qid),
5122 IWN5000_TXQ_STATUS_ACTIVE | qid2fifo[qid]);
5123 }
5124 iwn_nic_unlock(sc);
5125
5126 /* Configure WiMAX (IEEE 802.16e) coexistence. */
5127 memset(&wimax, 0, sizeof wimax);
5128 DPRINTF(("Configuring WiMAX coexistence\n"));
5129 error = iwn_cmd(sc, IWN5000_CMD_WIMAX_COEX, &wimax, sizeof wimax, 0);
5130 if (error != 0) {
5131 aprint_error_dev(sc->sc_dev,
5132 "could not configure WiMAX coexistence\n");
5133 return error;
5134 }
5135
5136 if (sc->hw_type != IWN_HW_REV_TYPE_5150) {
5137 struct iwn5000_phy_calib_crystal cmd;
5138
5139 /* Perform crystal calibration. */
5140 memset(&cmd, 0, sizeof cmd);
5141 cmd.code = IWN5000_PHY_CALIB_CRYSTAL;
5142 cmd.ngroups = 1;
5143 cmd.isvalid = 1;
5144 cmd.cap_pin[0] = le32toh(sc->eeprom_crystal) & 0xff;
5145 cmd.cap_pin[1] = (le32toh(sc->eeprom_crystal) >> 16) & 0xff;
5146 DPRINTF(("sending crystal calibration %d, %d\n",
5147 cmd.cap_pin[0], cmd.cap_pin[1]));
5148 error = iwn_cmd(sc, IWN_CMD_PHY_CALIB, &cmd, sizeof cmd, 0);
5149 if (error != 0) {
5150 aprint_error_dev(sc->sc_dev,
5151 "crystal calibration failed\n");
5152 return error;
5153 }
5154 }
5155 if (sc->sc_flags & IWN_FLAG_FIRST_BOOT) {
5156 /* Query calibration from the initialization firmware. */
5157 if ((error = iwn5000_query_calibration(sc)) != 0) {
5158 aprint_error_dev(sc->sc_dev,
5159 "could not query calibration\n");
5160 return error;
5161 }
5162 /*
5163 * We have the calibration results now so we can skip
5164 * loading the initialization firmware next time.
5165 */
5166 sc->sc_flags &= ~IWN_FLAG_FIRST_BOOT;
5167
5168 /* Reboot (call ourselves recursively!) */
5169 iwn_hw_stop(sc);
5170 error = iwn_hw_init(sc);
5171 } else {
5172 /* Send calibration results to runtime firmware. */
5173 error = iwn5000_send_calibration(sc);
5174 }
5175 return error;
5176 }
5177
5178 /*
5179 * The firmware boot code is small and is intended to be copied directly into
5180 * the NIC internal memory (no DMA transfer.)
5181 */
5182 static int
5183 iwn4965_load_bootcode(struct iwn_softc *sc, const uint8_t *ucode, int size)
5184 {
5185 int error, ntries;
5186
5187 size /= sizeof (uint32_t);
5188
5189 if ((error = iwn_nic_lock(sc)) != 0)
5190 return error;
5191
5192 /* Copy microcode image into NIC memory. */
5193 iwn_prph_write_region_4(sc, IWN_BSM_SRAM_BASE,
5194 (const uint32_t *)ucode, size);
5195
5196 iwn_prph_write(sc, IWN_BSM_WR_MEM_SRC, 0);
5197 iwn_prph_write(sc, IWN_BSM_WR_MEM_DST, IWN_FW_TEXT_BASE);
5198 iwn_prph_write(sc, IWN_BSM_WR_DWCOUNT, size);
5199
5200 /* Start boot load now. */
5201 iwn_prph_write(sc, IWN_BSM_WR_CTRL, IWN_BSM_WR_CTRL_START);
5202
5203 /* Wait for transfer to complete. */
5204 for (ntries = 0; ntries < 1000; ntries++) {
5205 if (!(iwn_prph_read(sc, IWN_BSM_WR_CTRL) &
5206 IWN_BSM_WR_CTRL_START))
5207 break;
5208 DELAY(10);
5209 }
5210 if (ntries == 1000) {
5211 aprint_error_dev(sc->sc_dev, "could not load boot firmware\n");
5212 iwn_nic_unlock(sc);
5213 return ETIMEDOUT;
5214 }
5215
5216 /* Enable boot after power up. */
5217 iwn_prph_write(sc, IWN_BSM_WR_CTRL, IWN_BSM_WR_CTRL_START_EN);
5218
5219 iwn_nic_unlock(sc);
5220 return 0;
5221 }
5222
5223 static int
5224 iwn4965_load_firmware(struct iwn_softc *sc)
5225 {
5226 struct iwn_fw_info *fw = &sc->fw;
5227 struct iwn_dma_info *dma = &sc->fw_dma;
5228 int error;
5229
5230 /* Copy initialization sections into pre-allocated DMA-safe memory. */
5231 memcpy(dma->vaddr, fw->init.data, fw->init.datasz);
5232 bus_dmamap_sync(sc->sc_dmat, dma->map, 0, fw->init.datasz,
5233 BUS_DMASYNC_PREWRITE);
5234 memcpy((char *)dma->vaddr + IWN4965_FW_DATA_MAXSZ,
5235 fw->init.text, fw->init.textsz);
5236 bus_dmamap_sync(sc->sc_dmat, dma->map, IWN4965_FW_DATA_MAXSZ,
5237 fw->init.textsz, BUS_DMASYNC_PREWRITE);
5238
5239 /* Tell adapter where to find initialization sections. */
5240 if ((error = iwn_nic_lock(sc)) != 0)
5241 return error;
5242 iwn_prph_write(sc, IWN_BSM_DRAM_DATA_ADDR, dma->paddr >> 4);
5243 iwn_prph_write(sc, IWN_BSM_DRAM_DATA_SIZE, fw->init.datasz);
5244 iwn_prph_write(sc, IWN_BSM_DRAM_TEXT_ADDR,
5245 (dma->paddr + IWN4965_FW_DATA_MAXSZ) >> 4);
5246 iwn_prph_write(sc, IWN_BSM_DRAM_TEXT_SIZE, fw->init.textsz);
5247 iwn_nic_unlock(sc);
5248
5249 /* Load firmware boot code. */
5250 error = iwn4965_load_bootcode(sc, fw->boot.text, fw->boot.textsz);
5251 if (error != 0) {
5252 aprint_error_dev(sc->sc_dev, "could not load boot firmware\n");
5253 return error;
5254 }
5255 /* Now press "execute". */
5256 IWN_WRITE(sc, IWN_RESET, 0);
5257
5258 /* Wait at most one second for first alive notification. */
5259 if ((error = tsleep(sc, PCATCH, "iwninit", hz)) != 0) {
5260 aprint_error_dev(sc->sc_dev,
5261 "timeout waiting for adapter to initialize %d\n", error);
5262 return error;
5263 }
5264
5265 /* Retrieve current temperature for initial TX power calibration. */
5266 sc->rawtemp = sc->ucode_info.temp[3].chan20MHz;
5267 sc->temp = iwn4965_get_temperature(sc);
5268
5269 /* Copy runtime sections into pre-allocated DMA-safe memory. */
5270 memcpy(dma->vaddr, fw->main.data, fw->main.datasz);
5271 bus_dmamap_sync(sc->sc_dmat, dma->map, 0, fw->main.datasz,
5272 BUS_DMASYNC_PREWRITE);
5273 memcpy((char *)dma->vaddr + IWN4965_FW_DATA_MAXSZ,
5274 fw->main.text, fw->main.textsz);
5275 bus_dmamap_sync(sc->sc_dmat, dma->map, IWN4965_FW_DATA_MAXSZ,
5276 fw->main.textsz, BUS_DMASYNC_PREWRITE);
5277
5278 /* Tell adapter where to find runtime sections. */
5279 if ((error = iwn_nic_lock(sc)) != 0)
5280 return error;
5281 iwn_prph_write(sc, IWN_BSM_DRAM_DATA_ADDR, dma->paddr >> 4);
5282 iwn_prph_write(sc, IWN_BSM_DRAM_DATA_SIZE, fw->main.datasz);
5283 iwn_prph_write(sc, IWN_BSM_DRAM_TEXT_ADDR,
5284 (dma->paddr + IWN4965_FW_DATA_MAXSZ) >> 4);
5285 iwn_prph_write(sc, IWN_BSM_DRAM_TEXT_SIZE,
5286 IWN_FW_UPDATED | fw->main.textsz);
5287 iwn_nic_unlock(sc);
5288
5289 return 0;
5290 }
5291
5292 static int
5293 iwn5000_load_firmware_section(struct iwn_softc *sc, uint32_t dst,
5294 const uint8_t *section, int size)
5295 {
5296 struct iwn_dma_info *dma = &sc->fw_dma;
5297 int error;
5298
5299 /* Copy firmware section into pre-allocated DMA-safe memory. */
5300 memcpy(dma->vaddr, section, size);
5301 bus_dmamap_sync(sc->sc_dmat, dma->map, 0, size, BUS_DMASYNC_PREWRITE);
5302
5303 if ((error = iwn_nic_lock(sc)) != 0)
5304 return error;
5305
5306 IWN_WRITE(sc, IWN_FH_TX_CONFIG(IWN_SRVC_CHNL),
5307 IWN_FH_TX_CONFIG_DMA_PAUSE);
5308
5309 IWN_WRITE(sc, IWN_FH_SRAM_ADDR(IWN_SRVC_CHNL), dst);
5310 IWN_WRITE(sc, IWN_FH_TFBD_CTRL0(IWN_SRVC_CHNL),
5311 IWN_LOADDR(dma->paddr));
5312 IWN_WRITE(sc, IWN_FH_TFBD_CTRL1(IWN_SRVC_CHNL),
5313 IWN_HIADDR(dma->paddr) << 28 | size);
5314 IWN_WRITE(sc, IWN_FH_TXBUF_STATUS(IWN_SRVC_CHNL),
5315 IWN_FH_TXBUF_STATUS_TBNUM(1) |
5316 IWN_FH_TXBUF_STATUS_TBIDX(1) |
5317 IWN_FH_TXBUF_STATUS_TFBD_VALID);
5318
5319 /* Kick Flow Handler to start DMA transfer. */
5320 IWN_WRITE(sc, IWN_FH_TX_CONFIG(IWN_SRVC_CHNL),
5321 IWN_FH_TX_CONFIG_DMA_ENA | IWN_FH_TX_CONFIG_CIRQ_HOST_ENDTFD);
5322
5323 iwn_nic_unlock(sc);
5324
5325 /* Wait at most five seconds for FH DMA transfer to complete. */
5326 return tsleep(sc, PCATCH, "iwninit", 5 * hz);
5327 }
5328
5329 static int
5330 iwn5000_load_firmware(struct iwn_softc *sc)
5331 {
5332 struct iwn_fw_part *fw;
5333 int error;
5334
5335 /* Load the initialization firmware on first boot only. */
5336 fw = (sc->sc_flags & IWN_FLAG_FIRST_BOOT) ?
5337 &sc->fw.init : &sc->fw.main;
5338
5339 error = iwn5000_load_firmware_section(sc, IWN_FW_TEXT_BASE,
5340 fw->text, fw->textsz);
5341 if (error != 0) {
5342 aprint_error_dev(sc->sc_dev,
5343 "could not load firmware %s section\n",
5344 ".text");
5345 return error;
5346 }
5347 error = iwn5000_load_firmware_section(sc, IWN_FW_DATA_BASE,
5348 fw->data, fw->datasz);
5349 if (error != 0) {
5350 aprint_error_dev(sc->sc_dev,
5351 "could not load firmware %s section\n",
5352 ".data");
5353 return error;
5354 }
5355
5356 /* Now press "execute". */
5357 IWN_WRITE(sc, IWN_RESET, 0);
5358 return 0;
5359 }
5360
5361 static int
5362 iwn_read_firmware(struct iwn_softc *sc)
5363 {
5364 const struct iwn_hal *hal = sc->sc_hal;
5365 struct iwn_fw_info *fw = &sc->fw;
5366 struct iwn_firmware_hdr hdr;
5367 firmware_handle_t fwh;
5368 size_t size;
5369 int error;
5370
5371 /* Read firmware image from filesystem. */
5372 if ((error = firmware_open("if_iwn", sc->fwname, &fwh)) != 0) {
5373 aprint_error_dev(sc->sc_dev,
5374 "could not read firmware file %s\n", sc->fwname);
5375 return error;
5376 }
5377 size = firmware_get_size(fwh);
5378 if (size < sizeof (hdr)) {
5379 aprint_error_dev(sc->sc_dev,
5380 "truncated firmware header: %zu bytes\n", size);
5381 error = EINVAL;
5382 goto fail2;
5383 }
5384 /* Extract firmware header information. */
5385 if ((error = firmware_read(fwh, 0, &hdr,
5386 sizeof (struct iwn_firmware_hdr))) != 0) {
5387 aprint_error_dev(sc->sc_dev, "can't get firmware header\n");
5388 goto fail2;
5389 }
5390 fw->main.textsz = le32toh(hdr.main_textsz);
5391 fw->main.datasz = le32toh(hdr.main_datasz);
5392 fw->init.textsz = le32toh(hdr.init_textsz);
5393 fw->init.datasz = le32toh(hdr.init_datasz);
5394 fw->boot.textsz = le32toh(hdr.boot_textsz);
5395 fw->boot.datasz = 0;
5396
5397 /* Sanity-check firmware header. */
5398 if (fw->main.textsz > hal->fw_text_maxsz ||
5399 fw->main.datasz > hal->fw_data_maxsz ||
5400 fw->init.textsz > hal->fw_text_maxsz ||
5401 fw->init.datasz > hal->fw_data_maxsz ||
5402 fw->boot.textsz > IWN_FW_BOOT_TEXT_MAXSZ ||
5403 (fw->boot.textsz & 3) != 0) {
5404 aprint_error_dev(sc->sc_dev, "invalid firmware header\n");
5405 error = EINVAL;
5406 goto fail2;
5407 }
5408
5409 /* Check that all firmware sections fit. */
5410 if (size < sizeof (hdr) + fw->main.textsz + fw->main.datasz +
5411 fw->init.textsz + fw->init.datasz + fw->boot.textsz) {
5412 aprint_error_dev(sc->sc_dev,
5413 "firmware file too short: %zu bytes\n", size);
5414 error = EINVAL;
5415 goto fail2;
5416 }
5417 fw->data = firmware_malloc(size);
5418 if (fw->data == NULL) {
5419 aprint_error_dev(sc->sc_dev,
5420 "not enough memory to stock firmware\n");
5421 error = ENOMEM;
5422 goto fail2;
5423 }
5424 if ((error = firmware_read(fwh, 0, fw->data, size)) != 0) {
5425 aprint_error_dev(sc->sc_dev, "can't get firmware\n");
5426 goto fail3;
5427 }
5428
5429 /* Get pointers to firmware sections. */
5430 fw->main.text = fw->data + sizeof (struct iwn_firmware_hdr);
5431 fw->main.data = fw->main.text + fw->main.textsz;
5432 fw->init.text = fw->main.data + fw->main.datasz;
5433 fw->init.data = fw->init.text + fw->init.textsz;
5434 fw->boot.text = fw->init.data + fw->init.datasz;
5435
5436 return 0;
5437 fail3: firmware_free(fw->data, size);
5438 fail2: firmware_close(fwh);
5439 return error;
5440 }
5441
5442 static int
5443 iwn_clock_wait(struct iwn_softc *sc)
5444 {
5445 int ntries;
5446
5447 /* Set "initialization complete" bit. */
5448 IWN_SETBITS(sc, IWN_GP_CNTRL, IWN_GP_CNTRL_INIT_DONE);
5449
5450 /* Wait for clock stabilization. */
5451 for (ntries = 0; ntries < 25000; ntries++) {
5452 if (IWN_READ(sc, IWN_GP_CNTRL) & IWN_GP_CNTRL_MAC_CLOCK_READY)
5453 return 0;
5454 DELAY(100);
5455 }
5456 aprint_error_dev(sc->sc_dev,
5457 "timeout waiting for clock stabilization\n");
5458 return ETIMEDOUT;
5459 }
5460
5461 static int
5462 iwn4965_apm_init(struct iwn_softc *sc)
5463 {
5464 int error;
5465
5466 /* Disable L0s. */
5467 IWN_SETBITS(sc, IWN_GIO_CHICKEN, IWN_GIO_CHICKEN_DIS_L0S_TIMER);
5468 IWN_SETBITS(sc, IWN_GIO_CHICKEN, IWN_GIO_CHICKEN_L1A_NO_L0S_RX);
5469
5470 if ((error = iwn_clock_wait(sc)) != 0)
5471 return error;
5472
5473 if ((error = iwn_nic_lock(sc)) != 0)
5474 return error;
5475 /* Enable DMA. */
5476 iwn_prph_write(sc, IWN_APMG_CLK_CTRL,
5477 IWN_APMG_CLK_CTRL_DMA_CLK_RQT | IWN_APMG_CLK_CTRL_BSM_CLK_RQT);
5478 DELAY(20);
5479 /* Disable L1. */
5480 iwn_prph_setbits(sc, IWN_APMG_PCI_STT, IWN_APMG_PCI_STT_L1A_DIS);
5481 iwn_nic_unlock(sc);
5482
5483 return 0;
5484 }
5485
5486 static int
5487 iwn5000_apm_init(struct iwn_softc *sc)
5488 {
5489 int error;
5490
5491 /* Disable L0s. */
5492 IWN_SETBITS(sc, IWN_GIO_CHICKEN, IWN_GIO_CHICKEN_DIS_L0S_TIMER);
5493 IWN_SETBITS(sc, IWN_GIO_CHICKEN, IWN_GIO_CHICKEN_L1A_NO_L0S_RX);
5494
5495 /* Set Flow Handler wait threshold to the maximum. */
5496 IWN_SETBITS(sc, IWN_DBG_HPET_MEM, 0xffff0000);
5497
5498 /* Enable HAP to move adapter from L1a to L0s. */
5499 IWN_SETBITS(sc, IWN_HW_IF_CONFIG, IWN_HW_IF_CONFIG_HAP_WAKE_L1A);
5500
5501 if (sc->hw_type != IWN_HW_REV_TYPE_6000 &&
5502 sc->hw_type != IWN_HW_REV_TYPE_6050)
5503 IWN_SETBITS(sc, IWN_ANA_PLL, IWN_ANA_PLL_INIT);
5504
5505 if ((error = iwn_clock_wait(sc)) != 0)
5506 return error;
5507
5508 if ((error = iwn_nic_lock(sc)) != 0)
5509 return error;
5510 /* Enable DMA. */
5511 iwn_prph_write(sc, IWN_APMG_CLK_CTRL, IWN_APMG_CLK_CTRL_DMA_CLK_RQT);
5512 DELAY(20);
5513 /* Disable L1. */
5514 iwn_prph_setbits(sc, IWN_APMG_PCI_STT, IWN_APMG_PCI_STT_L1A_DIS);
5515 iwn_nic_unlock(sc);
5516
5517 return 0;
5518 }
5519
5520 static void
5521 iwn_apm_stop_master(struct iwn_softc *sc)
5522 {
5523 int ntries;
5524
5525 IWN_SETBITS(sc, IWN_RESET, IWN_RESET_STOP_MASTER);
5526 for (ntries = 0; ntries < 100; ntries++) {
5527 if (IWN_READ(sc, IWN_RESET) & IWN_RESET_MASTER_DISABLED)
5528 return;
5529 DELAY(10);
5530 }
5531 aprint_error_dev(sc->sc_dev, "timeout waiting for master\n");
5532 }
5533
5534 static void
5535 iwn_apm_stop(struct iwn_softc *sc)
5536 {
5537 iwn_apm_stop_master(sc);
5538
5539 IWN_SETBITS(sc, IWN_RESET, IWN_RESET_SW);
5540 DELAY(10);
5541 /* Clear "initialization complete" bit. */
5542 IWN_CLRBITS(sc, IWN_GP_CNTRL, IWN_GP_CNTRL_INIT_DONE);
5543 }
5544
5545 static int
5546 iwn4965_nic_config(struct iwn_softc *sc)
5547 {
5548 pcireg_t reg;
5549
5550 /* Retrieve PCIe Active State Power Management (ASPM). */
5551 reg = pci_conf_read(sc->sc_pct, sc->sc_pcitag,
5552 sc->sc_cap_off + PCI_PCIE_LCSR);
5553 if (reg & PCI_PCIE_LCSR_ASPM_L1) /* L1 Entry enabled. */
5554 IWN_SETBITS(sc, IWN_GIO, IWN_GIO_L0S_ENA);
5555 else
5556 IWN_CLRBITS(sc, IWN_GIO, IWN_GIO_L0S_ENA);
5557
5558 if (IWN_RFCFG_TYPE(sc->rfcfg) == 1) {
5559 /*
5560 * I don't believe this to be correct but this is what the
5561 * vendor driver is doing. Probably the bits should not be
5562 * shifted in IWN_RFCFG_*.
5563 */
5564 IWN_SETBITS(sc, IWN_HW_IF_CONFIG,
5565 IWN_RFCFG_TYPE(sc->rfcfg) |
5566 IWN_RFCFG_STEP(sc->rfcfg) |
5567 IWN_RFCFG_DASH(sc->rfcfg));
5568 }
5569 IWN_SETBITS(sc, IWN_HW_IF_CONFIG,
5570 IWN_HW_IF_CONFIG_RADIO_SI | IWN_HW_IF_CONFIG_MAC_SI);
5571 return 0;
5572 }
5573
5574 static int
5575 iwn5000_nic_config(struct iwn_softc *sc)
5576 {
5577 int error;
5578 pcireg_t reg;
5579
5580 /* Retrieve PCIe Active State Power Management (ASPM). */
5581 reg = pci_conf_read(sc->sc_pct, sc->sc_pcitag,
5582 sc->sc_cap_off + PCI_PCIE_LCSR);
5583 if (reg & PCI_PCIE_LCSR_ASPM_L1) /* L1 Entry enabled. */
5584 IWN_SETBITS(sc, IWN_GIO, IWN_GIO_L0S_ENA);
5585 else
5586 IWN_CLRBITS(sc, IWN_GIO, IWN_GIO_L0S_ENA);
5587
5588 if (IWN_RFCFG_TYPE(sc->rfcfg) < 3) {
5589 IWN_SETBITS(sc, IWN_HW_IF_CONFIG,
5590 IWN_RFCFG_TYPE(sc->rfcfg) |
5591 IWN_RFCFG_STEP(sc->rfcfg) |
5592 IWN_RFCFG_DASH(sc->rfcfg));
5593 }
5594 IWN_SETBITS(sc, IWN_HW_IF_CONFIG,
5595 IWN_HW_IF_CONFIG_RADIO_SI | IWN_HW_IF_CONFIG_MAC_SI);
5596
5597 if ((error = iwn_nic_lock(sc)) != 0)
5598 return error;
5599 iwn_prph_setbits(sc, IWN_APMG_PS, IWN_APMG_PS_EARLY_PWROFF_DIS);
5600 iwn_nic_unlock(sc);
5601 return 0;
5602 }
5603
5604 static int
5605 iwn_hw_init(struct iwn_softc *sc)
5606 {
5607 const struct iwn_hal *hal = sc->sc_hal;
5608 int error, qid;
5609
5610 /* Clear pending interrupts. */
5611 IWN_WRITE(sc, IWN_INT, 0xffffffff);
5612
5613 if ((error = hal->apm_init(sc)) != 0) {
5614 aprint_error_dev(sc->sc_dev, "could not power ON adapter\n");
5615 return error;
5616 }
5617
5618 /* Select VMAIN power source. */
5619 if ((error = iwn_nic_lock(sc)) != 0)
5620 return error;
5621 iwn_prph_clrbits(sc, IWN_APMG_PS, IWN_APMG_PS_PWR_SRC_MASK);
5622 iwn_nic_unlock(sc);
5623
5624 /* Perform adapter-specific initialization. */
5625 if ((error = hal->nic_config(sc)) != 0)
5626 return error;
5627
5628 /* Initialize RX ring. */
5629 if ((error = iwn_nic_lock(sc)) != 0)
5630 return error;
5631 IWN_WRITE(sc, IWN_FH_RX_CONFIG, 0);
5632 IWN_WRITE(sc, IWN_FH_RX_WPTR, 0);
5633 /* Set physical address of RX ring (256-byte aligned.) */
5634 IWN_WRITE(sc, IWN_FH_RX_BASE, sc->rxq.desc_dma.paddr >> 8);
5635 /* Set physical address of RX status (16-byte aligned.) */
5636 IWN_WRITE(sc, IWN_FH_STATUS_WPTR, sc->rxq.stat_dma.paddr >> 4);
5637 /* Enable RX. */
5638 IWN_WRITE(sc, IWN_FH_RX_CONFIG,
5639 IWN_FH_RX_CONFIG_ENA |
5640 IWN_FH_RX_CONFIG_IGN_RXF_EMPTY | /* HW bug workaround */
5641 IWN_FH_RX_CONFIG_IRQ_DST_HOST |
5642 IWN_FH_RX_CONFIG_SINGLE_FRAME |
5643 IWN_FH_RX_CONFIG_RB_TIMEOUT(0) |
5644 IWN_FH_RX_CONFIG_NRBD(IWN_RX_RING_COUNT_LOG));
5645 iwn_nic_unlock(sc);
5646 IWN_WRITE(sc, IWN_FH_RX_WPTR, (IWN_RX_RING_COUNT - 1) & ~7);
5647
5648 if ((error = iwn_nic_lock(sc)) != 0)
5649 return error;
5650
5651 /* Initialize TX scheduler. */
5652 iwn_prph_write(sc, hal->sched_txfact_addr, 0);
5653
5654 /* Set physical address of "keep warm" page (16-byte aligned.) */
5655 IWN_WRITE(sc, IWN_FH_KW_ADDR, sc->kw_dma.paddr >> 4);
5656
5657 /* Initialize TX rings. */
5658 for (qid = 0; qid < hal->ntxqs; qid++) {
5659 struct iwn_tx_ring *txq = &sc->txq[qid];
5660
5661 /* Set physical address of TX ring (256-byte aligned.) */
5662 IWN_WRITE(sc, IWN_FH_CBBC_QUEUE(qid),
5663 txq->desc_dma.paddr >> 8);
5664 /* Enable TX for this ring. */
5665 IWN_WRITE(sc, IWN_FH_TX_CONFIG(qid),
5666 IWN_FH_TX_CONFIG_DMA_ENA |
5667 IWN_FH_TX_CONFIG_DMA_CREDIT_ENA);
5668 }
5669 iwn_nic_unlock(sc);
5670
5671 /* Clear "radio off" and "commands blocked" bits. */
5672 IWN_WRITE(sc, IWN_UCODE_GP1_CLR, IWN_UCODE_GP1_RFKILL);
5673 IWN_WRITE(sc, IWN_UCODE_GP1_CLR, IWN_UCODE_GP1_CMD_BLOCKED);
5674
5675 /* Clear pending interrupts. */
5676 IWN_WRITE(sc, IWN_INT, 0xffffffff);
5677 /* Enable interrupt coalescing. */
5678 IWN_WRITE(sc, IWN_INT_COALESCING, 512 / 8);
5679 /* Enable interrupts. */
5680 IWN_WRITE(sc, IWN_MASK, IWN_INT_MASK);
5681
5682 /* _Really_ make sure "radio off" bit is cleared! */
5683 IWN_WRITE(sc, IWN_UCODE_GP1_CLR, IWN_UCODE_GP1_RFKILL);
5684 IWN_WRITE(sc, IWN_UCODE_GP1_CLR, IWN_UCODE_GP1_RFKILL);
5685
5686 if ((error = hal->load_firmware(sc)) != 0) {
5687 aprint_error_dev(sc->sc_dev, "could not load firmware\n");
5688 return error;
5689 }
5690 /* Wait at most one second for firmware alive notification. */
5691 if ((error = tsleep(sc, PCATCH, "iwninit", hz)) != 0) {
5692 aprint_error_dev(sc->sc_dev,
5693 "timeout waiting for adapter to initialize %d\n" ,error);
5694 return error;
5695 }
5696 /* Do post-firmware initialization. */
5697 return hal->post_alive(sc);
5698 }
5699
5700 static void
5701 iwn_hw_stop(struct iwn_softc *sc)
5702 {
5703 const struct iwn_hal *hal = sc->sc_hal;
5704 int qid;
5705
5706 IWN_WRITE(sc, IWN_RESET, IWN_RESET_NEVO);
5707
5708 /* Disable interrupts. */
5709 IWN_WRITE(sc, IWN_MASK, 0);
5710 IWN_WRITE(sc, IWN_INT, 0xffffffff);
5711 IWN_WRITE(sc, IWN_FH_INT, 0xffffffff);
5712
5713 /* Make sure we no longer hold the NIC lock. */
5714 iwn_nic_unlock(sc);
5715
5716 /* Stop TX scheduler. */
5717 iwn_prph_write(sc, hal->sched_txfact_addr, 0);
5718
5719 /* Stop all TX rings. */
5720 for (qid = 0; qid < hal->ntxqs; qid++)
5721 iwn_reset_tx_ring(sc, &sc->txq[qid]);
5722
5723 /* Stop RX ring. */
5724 iwn_reset_rx_ring(sc, &sc->rxq);
5725
5726 if (iwn_nic_lock(sc) == 0) {
5727 iwn_prph_write(sc, IWN_APMG_CLK_DIS, IWN_APMG_CLK_DMA_RQT);
5728 iwn_nic_unlock(sc);
5729 }
5730 DELAY(5);
5731 /* Power OFF adapter. */
5732 iwn_apm_stop(sc);
5733 }
5734
5735 static int
5736 iwn_init(struct ifnet *ifp)
5737 {
5738 struct iwn_softc *sc = ifp->if_softc;
5739 struct ieee80211com *ic = &sc->sc_ic;
5740 int error;
5741
5742 /* Check that the radio is not disabled by hardware switch. */
5743 if (!(IWN_READ(sc, IWN_GP_CNTRL) & IWN_GP_CNTRL_RFKILL)) {
5744 aprint_error_dev(sc->sc_dev,
5745 "radio is disabled by hardware switch\n");
5746 sc->sc_radio = false;
5747 error = EPERM; /* :-) */
5748 goto fail;
5749 }
5750 sc->sc_radio = true;
5751
5752 /* Read firmware images from the filesystem. */
5753 if ((error = iwn_read_firmware(sc)) != 0) {
5754 aprint_error_dev(sc->sc_dev, "could not read firmware\n");
5755 goto fail;
5756 }
5757
5758 /* Initialize hardware and upload firmware. */
5759 error = iwn_hw_init(sc);
5760 free(sc->fw.data, M_DEVBUF);
5761 if (error != 0) {
5762 aprint_error_dev(sc->sc_dev, "could not initialize hardware\n");
5763 goto fail;
5764 }
5765
5766 /* Configure adapter now that it is ready. */
5767 if ((error = iwn_config(sc)) != 0) {
5768 aprint_error_dev(sc->sc_dev, "could not configure device\n");
5769 goto fail;
5770 }
5771
5772 ifp->if_flags &= ~IFF_OACTIVE;
5773 ifp->if_flags |= IFF_RUNNING;
5774
5775 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
5776 if (ic->ic_opmode != IEEE80211_ROAMING_MANUAL)
5777 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
5778 } else
5779 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
5780
5781 return 0;
5782
5783 fail: iwn_stop(ifp, 1);
5784 return error;
5785 }
5786
5787 static void
5788 iwn_stop(struct ifnet *ifp, int disable)
5789 {
5790 struct iwn_softc *sc = ifp->if_softc;
5791 struct ieee80211com *ic = &sc->sc_ic;
5792
5793 ifp->if_timer = sc->sc_tx_timer = 0;
5794 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
5795
5796 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
5797
5798 /* Power OFF hardware. */
5799 iwn_hw_stop(sc);
5800
5801 #if 0
5802 /* Temperature sensor is no longer valid. */
5803 sc->sensor.value = 0;
5804 sc->sensor.flags |= SENSOR_FINVALID;
5805 #endif
5806 }
5807
5808 static bool
5809 iwn_resume(device_t dv, const pmf_qual_t *qual)
5810 {
5811 #if 0
5812 struct iwn_softc *sc = device_private(dv);
5813
5814 (void)iwn_reset(sc);
5815 #endif
5816
5817 return true;
5818 }
5819