]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/iwm/if_iwm_pcie_trans.c
MFhead@r313433
[FreeBSD/FreeBSD.git] / sys / dev / iwm / if_iwm_pcie_trans.c
1 /*      $OpenBSD: if_iwm.c,v 1.39 2015/03/23 00:35:19 jsg Exp $ */
2
3 /*
4  * Copyright (c) 2014 genua mbh <info@genua.de>
5  * Copyright (c) 2014 Fixup Software Ltd.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19
20 /*-
21  * Based on BSD-licensed source modules in the Linux iwlwifi driver,
22  * which were used as the reference documentation for this implementation.
23  *
24  * Driver version we are currently based off of is
25  * Linux 3.14.3 (tag id a2df521e42b1d9a23f620ac79dbfe8655a8391dd)
26  *
27  ***********************************************************************
28  *
29  * This file is provided under a dual BSD/GPLv2 license.  When using or
30  * redistributing this file, you may do so under either license.
31  *
32  * GPL LICENSE SUMMARY
33  *
34  * Copyright(c) 2007 - 2013 Intel Corporation. All rights reserved.
35  *
36  * This program is free software; you can redistribute it and/or modify
37  * it under the terms of version 2 of the GNU General Public License as
38  * published by the Free Software Foundation.
39  *
40  * This program is distributed in the hope that it will be useful, but
41  * WITHOUT ANY WARRANTY; without even the implied warranty of
42  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
43  * General Public License for more details.
44  *
45  * You should have received a copy of the GNU General Public License
46  * along with this program; if not, write to the Free Software
47  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
48  * USA
49  *
50  * The full GNU General Public License is included in this distribution
51  * in the file called COPYING.
52  *
53  * Contact Information:
54  *  Intel Linux Wireless <ilw@linux.intel.com>
55  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
56  *
57  *
58  * BSD LICENSE
59  *
60  * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved.
61  * All rights reserved.
62  *
63  * Redistribution and use in source and binary forms, with or without
64  * modification, are permitted provided that the following conditions
65  * are met:
66  *
67  *  * Redistributions of source code must retain the above copyright
68  *    notice, this list of conditions and the following disclaimer.
69  *  * Redistributions in binary form must reproduce the above copyright
70  *    notice, this list of conditions and the following disclaimer in
71  *    the documentation and/or other materials provided with the
72  *    distribution.
73  *  * Neither the name Intel Corporation nor the names of its
74  *    contributors may be used to endorse or promote products derived
75  *    from this software without specific prior written permission.
76  *
77  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
78  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
79  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
80  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
81  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
82  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
83  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
84  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
85  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
86  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
87  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
88  */
89
90 /*-
91  * Copyright (c) 2007-2010 Damien Bergamini <damien.bergamini@free.fr>
92  *
93  * Permission to use, copy, modify, and distribute this software for any
94  * purpose with or without fee is hereby granted, provided that the above
95  * copyright notice and this permission notice appear in all copies.
96  *
97  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
98  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
99  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
100  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
101  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
102  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
103  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
104  */
105 #include <sys/cdefs.h>
106 __FBSDID("$FreeBSD$");
107
108 #include "opt_wlan.h"
109
110 #include <sys/param.h>
111 #include <sys/bus.h>
112 #include <sys/conf.h>
113 #include <sys/endian.h>
114 #include <sys/firmware.h>
115 #include <sys/kernel.h>
116 #include <sys/malloc.h>
117 #include <sys/mbuf.h>
118 #include <sys/mutex.h>
119 #include <sys/module.h>
120 #include <sys/proc.h>
121 #include <sys/rman.h>
122 #include <sys/socket.h>
123 #include <sys/sockio.h>
124 #include <sys/sysctl.h>
125 #include <sys/linker.h>
126
127 #include <machine/bus.h>
128 #include <machine/endian.h>
129 #include <machine/resource.h>
130
131 #include <dev/pci/pcivar.h>
132 #include <dev/pci/pcireg.h>
133
134 #include <net/bpf.h>
135
136 #include <net/if.h>
137 #include <net/if_var.h>
138 #include <net/if_arp.h>
139 #include <net/if_dl.h>
140 #include <net/if_media.h>
141 #include <net/if_types.h>
142
143 #include <netinet/in.h>
144 #include <netinet/in_systm.h>
145 #include <netinet/if_ether.h>
146 #include <netinet/ip.h>
147
148 #include <net80211/ieee80211_var.h>
149 #include <net80211/ieee80211_regdomain.h>
150 #include <net80211/ieee80211_ratectl.h>
151 #include <net80211/ieee80211_radiotap.h>
152
153 #include <dev/iwm/if_iwmreg.h>
154 #include <dev/iwm/if_iwmvar.h>
155 #include <dev/iwm/if_iwm_debug.h>
156 #include <dev/iwm/if_iwm_pcie_trans.h>
157
158 /*
159  * This is a subset of what's in linux iwlwifi/pcie/trans.c.
160  * The rest can be migrated out into here once they're no longer in
161  * if_iwm.c.
162  */
163
164 /*
165  * basic device access
166  */
167
168 uint32_t
169 iwm_read_prph(struct iwm_softc *sc, uint32_t addr)
170 {
171         IWM_WRITE(sc,
172             IWM_HBUS_TARG_PRPH_RADDR, ((addr & 0x000fffff) | (3 << 24)));
173         IWM_BARRIER_READ_WRITE(sc);
174         return IWM_READ(sc, IWM_HBUS_TARG_PRPH_RDAT);
175 }
176
177 void
178 iwm_write_prph(struct iwm_softc *sc, uint32_t addr, uint32_t val)
179 {
180         IWM_WRITE(sc,
181             IWM_HBUS_TARG_PRPH_WADDR, ((addr & 0x000fffff) | (3 << 24)));
182         IWM_BARRIER_WRITE(sc);
183         IWM_WRITE(sc, IWM_HBUS_TARG_PRPH_WDAT, val);
184 }
185
186 #ifdef IWM_DEBUG
187 /* iwlwifi: pcie/trans.c */
188 int
189 iwm_read_mem(struct iwm_softc *sc, uint32_t addr, void *buf, int dwords)
190 {
191         int offs, ret = 0;
192         uint32_t *vals = buf;
193
194         if (iwm_nic_lock(sc)) {
195                 IWM_WRITE(sc, IWM_HBUS_TARG_MEM_RADDR, addr);
196                 for (offs = 0; offs < dwords; offs++)
197                         vals[offs] = IWM_READ(sc, IWM_HBUS_TARG_MEM_RDAT);
198                 iwm_nic_unlock(sc);
199         } else {
200                 ret = EBUSY;
201         }
202         return ret;
203 }
204 #endif
205
206 /* iwlwifi: pcie/trans.c */
207 int
208 iwm_write_mem(struct iwm_softc *sc, uint32_t addr, const void *buf, int dwords)
209 {
210         int offs;
211         const uint32_t *vals = buf;
212
213         if (iwm_nic_lock(sc)) {
214                 IWM_WRITE(sc, IWM_HBUS_TARG_MEM_WADDR, addr);
215                 /* WADDR auto-increments */
216                 for (offs = 0; offs < dwords; offs++) {
217                         uint32_t val = vals ? vals[offs] : 0;
218                         IWM_WRITE(sc, IWM_HBUS_TARG_MEM_WDAT, val);
219                 }
220                 iwm_nic_unlock(sc);
221         } else {
222                 IWM_DPRINTF(sc, IWM_DEBUG_TRANS,
223                     "%s: write_mem failed\n", __func__);
224                 return EBUSY;
225         }
226         return 0;
227 }
228
229 int
230 iwm_write_mem32(struct iwm_softc *sc, uint32_t addr, uint32_t val)
231 {
232         return iwm_write_mem(sc, addr, &val, 1);
233 }
234
235 int
236 iwm_poll_bit(struct iwm_softc *sc, int reg,
237         uint32_t bits, uint32_t mask, int timo)
238 {
239         for (;;) {
240                 if ((IWM_READ(sc, reg) & mask) == (bits & mask)) {
241                         return 1;
242                 }
243                 if (timo < 10) {
244                         return 0;
245                 }
246                 timo -= 10;
247                 DELAY(10);
248         }
249 }
250
251 int
252 iwm_nic_lock(struct iwm_softc *sc)
253 {
254         int rv = 0;
255
256         if (sc->cmd_hold_nic_awake)
257                 return 1;
258
259         IWM_SETBITS(sc, IWM_CSR_GP_CNTRL,
260             IWM_CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
261
262         if (sc->cfg->device_family == IWM_DEVICE_FAMILY_8000)
263                 DELAY(2);
264
265         if (iwm_poll_bit(sc, IWM_CSR_GP_CNTRL,
266             IWM_CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
267             IWM_CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY
268              | IWM_CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP, 15000)) {
269                 rv = 1;
270         } else {
271                 /* jolt */
272                 IWM_DPRINTF(sc, IWM_DEBUG_RESET,
273                     "%s: resetting device via NMI\n", __func__);
274                 IWM_WRITE(sc, IWM_CSR_RESET, IWM_CSR_RESET_REG_FLAG_FORCE_NMI);
275         }
276
277         return rv;
278 }
279
280 void
281 iwm_nic_unlock(struct iwm_softc *sc)
282 {
283         if (sc->cmd_hold_nic_awake)
284                 return;
285
286         IWM_CLRBITS(sc, IWM_CSR_GP_CNTRL,
287             IWM_CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
288 }
289
290 void
291 iwm_set_bits_mask_prph(struct iwm_softc *sc,
292         uint32_t reg, uint32_t bits, uint32_t mask)
293 {
294         uint32_t val;
295
296         /* XXX: no error path? */
297         if (iwm_nic_lock(sc)) {
298                 val = iwm_read_prph(sc, reg) & mask;
299                 val |= bits;
300                 iwm_write_prph(sc, reg, val);
301                 iwm_nic_unlock(sc);
302         }
303 }
304
305 void
306 iwm_set_bits_prph(struct iwm_softc *sc, uint32_t reg, uint32_t bits)
307 {
308         iwm_set_bits_mask_prph(sc, reg, bits, ~0);
309 }
310
311 void
312 iwm_clear_bits_prph(struct iwm_softc *sc, uint32_t reg, uint32_t bits)
313 {
314         iwm_set_bits_mask_prph(sc, reg, 0, ~bits);
315 }
316
317 /*
318  * High-level hardware frobbing routines
319  */
320
321 void
322 iwm_enable_rfkill_int(struct iwm_softc *sc)
323 {
324         sc->sc_intmask = IWM_CSR_INT_BIT_RF_KILL;
325         IWM_WRITE(sc, IWM_CSR_INT_MASK, sc->sc_intmask);
326 }
327
328 int
329 iwm_check_rfkill(struct iwm_softc *sc)
330 {
331         uint32_t v;
332         int rv;
333
334         /*
335          * "documentation" is not really helpful here:
336          *  27: HW_RF_KILL_SW
337          *      Indicates state of (platform's) hardware RF-Kill switch
338          *
339          * But apparently when it's off, it's on ...
340          */
341         v = IWM_READ(sc, IWM_CSR_GP_CNTRL);
342         rv = (v & IWM_CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW) == 0;
343         if (rv) {
344                 sc->sc_flags |= IWM_FLAG_RFKILL;
345         } else {
346                 sc->sc_flags &= ~IWM_FLAG_RFKILL;
347         }
348
349         return rv;
350 }
351
352
353 #define IWM_HW_READY_TIMEOUT 50
354 int
355 iwm_set_hw_ready(struct iwm_softc *sc)
356 {
357         int ready;
358
359         IWM_SETBITS(sc, IWM_CSR_HW_IF_CONFIG_REG,
360             IWM_CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
361
362         ready = iwm_poll_bit(sc, IWM_CSR_HW_IF_CONFIG_REG,
363             IWM_CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
364             IWM_CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
365             IWM_HW_READY_TIMEOUT);
366         if (ready) {
367                 IWM_SETBITS(sc, IWM_CSR_MBOX_SET_REG,
368                     IWM_CSR_MBOX_SET_REG_OS_ALIVE);
369         }
370         return ready;
371 }
372 #undef IWM_HW_READY_TIMEOUT
373
374 int
375 iwm_prepare_card_hw(struct iwm_softc *sc)
376 {
377         int rv = 0;
378         int t = 0;
379
380         IWM_DPRINTF(sc, IWM_DEBUG_RESET, "->%s\n", __func__);
381         if (iwm_set_hw_ready(sc))
382                 goto out;
383
384         DELAY(100);
385
386         /* If HW is not ready, prepare the conditions to check again */
387         IWM_SETBITS(sc, IWM_CSR_HW_IF_CONFIG_REG,
388             IWM_CSR_HW_IF_CONFIG_REG_PREPARE);
389
390         do {
391                 if (iwm_set_hw_ready(sc))
392                         goto out;
393                 DELAY(200);
394                 t += 200;
395         } while (t < 150000);
396
397         rv = ETIMEDOUT;
398
399  out:
400         IWM_DPRINTF(sc, IWM_DEBUG_RESET, "<-%s\n", __func__);
401         return rv;
402 }
403
404 void
405 iwm_apm_config(struct iwm_softc *sc)
406 {
407         uint16_t reg;
408
409         reg = pci_read_config(sc->sc_dev, PCIER_LINK_CTL, sizeof(reg));
410         if (reg & PCIEM_LINK_CTL_ASPMC_L1)  {
411                 /* Um the Linux driver prints "Disabling L0S for this one ... */
412                 IWM_SETBITS(sc, IWM_CSR_GIO_REG,
413                     IWM_CSR_GIO_REG_VAL_L0S_ENABLED);
414         } else {
415                 /* ... and "Enabling" here */
416                 IWM_CLRBITS(sc, IWM_CSR_GIO_REG,
417                     IWM_CSR_GIO_REG_VAL_L0S_ENABLED);
418         }
419 }
420
421 /*
422  * Start up NIC's basic functionality after it has been reset
423  * (e.g. after platform boot, or shutdown via iwm_pcie_apm_stop())
424  * NOTE:  This does not load uCode nor start the embedded processor
425  */
426 int
427 iwm_apm_init(struct iwm_softc *sc)
428 {
429         int error = 0;
430
431         IWM_DPRINTF(sc, IWM_DEBUG_RESET, "iwm apm start\n");
432
433         /* Disable L0S exit timer (platform NMI Work/Around) */
434         if (sc->cfg->device_family != IWM_DEVICE_FAMILY_8000) {
435                 IWM_SETBITS(sc, IWM_CSR_GIO_CHICKEN_BITS,
436                     IWM_CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
437         }
438
439         /*
440          * Disable L0s without affecting L1;
441          *  don't wait for ICH L0s (ICH bug W/A)
442          */
443         IWM_SETBITS(sc, IWM_CSR_GIO_CHICKEN_BITS,
444             IWM_CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
445
446         /* Set FH wait threshold to maximum (HW error during stress W/A) */
447         IWM_SETBITS(sc, IWM_CSR_DBG_HPET_MEM_REG, IWM_CSR_DBG_HPET_MEM_REG_VAL);
448
449         /*
450          * Enable HAP INTA (interrupt from management bus) to
451          * wake device's PCI Express link L1a -> L0s
452          */
453         IWM_SETBITS(sc, IWM_CSR_HW_IF_CONFIG_REG,
454             IWM_CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
455
456         iwm_apm_config(sc);
457
458 #if 0 /* not for 7k/8k */
459         /* Configure analog phase-lock-loop before activating to D0A */
460         if (trans->cfg->base_params->pll_cfg_val)
461                 IWM_SETBITS(trans, IWM_CSR_ANA_PLL_CFG,
462                     trans->cfg->base_params->pll_cfg_val);
463 #endif
464
465         /*
466          * Set "initialization complete" bit to move adapter from
467          * D0U* --> D0A* (powered-up active) state.
468          */
469         IWM_SETBITS(sc, IWM_CSR_GP_CNTRL, IWM_CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
470
471         /*
472          * Wait for clock stabilization; once stabilized, access to
473          * device-internal resources is supported, e.g. iwm_write_prph()
474          * and accesses to uCode SRAM.
475          */
476         if (!iwm_poll_bit(sc, IWM_CSR_GP_CNTRL,
477             IWM_CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
478             IWM_CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000)) {
479                 device_printf(sc->sc_dev,
480                     "timeout waiting for clock stabilization\n");
481                 error = ETIMEDOUT;
482                 goto out;
483         }
484
485         if (sc->cfg->host_interrupt_operation_mode) {
486                 /*
487                  * This is a bit of an abuse - This is needed for 7260 / 3160
488                  * only check host_interrupt_operation_mode even if this is
489                  * not related to host_interrupt_operation_mode.
490                  *
491                  * Enable the oscillator to count wake up time for L1 exit. This
492                  * consumes slightly more power (100uA) - but allows to be sure
493                  * that we wake up from L1 on time.
494                  *
495                  * This looks weird: read twice the same register, discard the
496                  * value, set a bit, and yet again, read that same register
497                  * just to discard the value. But that's the way the hardware
498                  * seems to like it.
499                  */
500                 iwm_read_prph(sc, IWM_OSC_CLK);
501                 iwm_read_prph(sc, IWM_OSC_CLK);
502                 iwm_set_bits_prph(sc, IWM_OSC_CLK, IWM_OSC_CLK_FORCE_CONTROL);
503                 iwm_read_prph(sc, IWM_OSC_CLK);
504                 iwm_read_prph(sc, IWM_OSC_CLK);
505         }
506
507         /*
508          * Enable DMA clock and wait for it to stabilize.
509          *
510          * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" bits
511          * do not disable clocks.  This preserves any hardware bits already
512          * set by default in "CLK_CTRL_REG" after reset.
513          */
514         if (sc->cfg->device_family == IWM_DEVICE_FAMILY_7000) {
515                 iwm_write_prph(sc, IWM_APMG_CLK_EN_REG,
516                     IWM_APMG_CLK_VAL_DMA_CLK_RQT);
517                 DELAY(20);
518
519                 /* Disable L1-Active */
520                 iwm_set_bits_prph(sc, IWM_APMG_PCIDEV_STT_REG,
521                     IWM_APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
522
523                 /* Clear the interrupt in APMG if the NIC is in RFKILL */
524                 iwm_write_prph(sc, IWM_APMG_RTC_INT_STT_REG,
525                     IWM_APMG_RTC_INT_STT_RFKILL);
526         }
527  out:
528         if (error)
529                 device_printf(sc->sc_dev, "apm init error %d\n", error);
530         return error;
531 }
532
533 /* iwlwifi/pcie/trans.c */
534 void
535 iwm_apm_stop(struct iwm_softc *sc)
536 {
537         /* stop device's busmaster DMA activity */
538         IWM_SETBITS(sc, IWM_CSR_RESET, IWM_CSR_RESET_REG_FLAG_STOP_MASTER);
539
540         if (!iwm_poll_bit(sc, IWM_CSR_RESET,
541             IWM_CSR_RESET_REG_FLAG_MASTER_DISABLED,
542             IWM_CSR_RESET_REG_FLAG_MASTER_DISABLED, 100))
543                 device_printf(sc->sc_dev, "timeout waiting for master\n");
544         IWM_DPRINTF(sc, IWM_DEBUG_TRANS, "%s: iwm apm stop\n", __func__);
545 }
546
547 /* iwlwifi pcie/trans.c */
548 int
549 iwm_start_hw(struct iwm_softc *sc)
550 {
551         int error;
552
553         if ((error = iwm_prepare_card_hw(sc)) != 0)
554                 return error;
555
556         /* Reset the entire device */
557         IWM_WRITE(sc, IWM_CSR_RESET, IWM_CSR_RESET_REG_FLAG_SW_RESET);
558         DELAY(10);
559
560         if ((error = iwm_apm_init(sc)) != 0)
561                 return error;
562
563         iwm_enable_rfkill_int(sc);
564         iwm_check_rfkill(sc);
565
566         return 0;
567 }
568
569 /* iwlwifi pcie/trans.c (always main power) */
570 void
571 iwm_set_pwr(struct iwm_softc *sc)
572 {
573         iwm_set_bits_mask_prph(sc, IWM_APMG_PS_CTRL_REG,
574             IWM_APMG_PS_CTRL_VAL_PWR_SRC_VMAIN, ~IWM_APMG_PS_CTRL_MSK_PWR_SRC);
575 }
576
577 /* iwlwifi pcie/rx.c */
578 int
579 iwm_pcie_rx_stop(struct iwm_softc *sc)
580 {
581         int ret = 0;
582         if (iwm_nic_lock(sc)) {
583                 IWM_WRITE(sc, IWM_FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
584                 ret = iwm_poll_bit(sc, IWM_FH_MEM_RSSR_RX_STATUS_REG,
585                     IWM_FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE,
586                     IWM_FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE,
587                     1000);
588                 iwm_nic_unlock(sc);
589         }
590         return ret;
591 }
592
593 void
594 iwm_pcie_clear_cmd_in_flight(struct iwm_softc *sc)
595 {
596         if (!sc->cfg->apmg_wake_up_wa)
597                 return;
598
599         if (!sc->cmd_hold_nic_awake) {
600                 device_printf(sc->sc_dev,
601                     "%s: cmd_hold_nic_awake not set\n", __func__);
602                 return;
603         }
604
605         sc->cmd_hold_nic_awake = 0;
606         IWM_CLRBITS(sc, IWM_CSR_GP_CNTRL,
607             IWM_CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
608 }
609
610 int
611 iwm_pcie_set_cmd_in_flight(struct iwm_softc *sc)
612 {
613         int ret;
614
615         /*
616          * wake up the NIC to make sure that the firmware will see the host
617          * command - we will let the NIC sleep once all the host commands
618          * returned. This needs to be done only on NICs that have
619          * apmg_wake_up_wa set.
620          */
621         if (sc->cfg->apmg_wake_up_wa &&
622             !sc->cmd_hold_nic_awake) {
623
624                 IWM_SETBITS(sc, IWM_CSR_GP_CNTRL,
625                     IWM_CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
626
627                 ret = iwm_poll_bit(sc, IWM_CSR_GP_CNTRL,
628                     IWM_CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
629                     (IWM_CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
630                      IWM_CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP),
631                     15000);
632                 if (ret == 0) {
633                         IWM_CLRBITS(sc, IWM_CSR_GP_CNTRL,
634                             IWM_CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
635                         device_printf(sc->sc_dev,
636                             "%s: Failed to wake NIC for hcmd\n", __func__);
637                         return EIO;
638                 }
639                 sc->cmd_hold_nic_awake = 1;
640         }
641
642         return 0;
643 }