xref: /dflybsd-src/sys/dev/netif/iwm/if_iwm_pcie_trans.c (revision 6acbba79fd1a245dc4f49a6e542db4c015d39b8e)
124a8d46aSMatthew Dillon /*	$OpenBSD: if_iwm.c,v 1.39 2015/03/23 00:35:19 jsg Exp $	*/
224a8d46aSMatthew Dillon 
324a8d46aSMatthew Dillon /*
424a8d46aSMatthew Dillon  * Copyright (c) 2014 genua mbh <info@genua.de>
524a8d46aSMatthew Dillon  * Copyright (c) 2014 Fixup Software Ltd.
624a8d46aSMatthew Dillon  *
724a8d46aSMatthew Dillon  * Permission to use, copy, modify, and distribute this software for any
824a8d46aSMatthew Dillon  * purpose with or without fee is hereby granted, provided that the above
924a8d46aSMatthew Dillon  * copyright notice and this permission notice appear in all copies.
1024a8d46aSMatthew Dillon  *
1124a8d46aSMatthew Dillon  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1224a8d46aSMatthew Dillon  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1324a8d46aSMatthew Dillon  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1424a8d46aSMatthew Dillon  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1524a8d46aSMatthew Dillon  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1624a8d46aSMatthew Dillon  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1724a8d46aSMatthew Dillon  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1824a8d46aSMatthew Dillon  */
1924a8d46aSMatthew Dillon 
2024a8d46aSMatthew Dillon /*-
2124a8d46aSMatthew Dillon  * Based on BSD-licensed source modules in the Linux iwlwifi driver,
2224a8d46aSMatthew Dillon  * which were used as the reference documentation for this implementation.
2324a8d46aSMatthew Dillon  *
2424a8d46aSMatthew Dillon  * Driver version we are currently based off of is
2524a8d46aSMatthew Dillon  * Linux 3.14.3 (tag id a2df521e42b1d9a23f620ac79dbfe8655a8391dd)
2624a8d46aSMatthew Dillon  *
2724a8d46aSMatthew Dillon  ***********************************************************************
2824a8d46aSMatthew Dillon  *
2924a8d46aSMatthew Dillon  * This file is provided under a dual BSD/GPLv2 license.  When using or
3024a8d46aSMatthew Dillon  * redistributing this file, you may do so under either license.
3124a8d46aSMatthew Dillon  *
3224a8d46aSMatthew Dillon  * GPL LICENSE SUMMARY
3324a8d46aSMatthew Dillon  *
3424a8d46aSMatthew Dillon  * Copyright(c) 2007 - 2013 Intel Corporation. All rights reserved.
3524a8d46aSMatthew Dillon  *
3624a8d46aSMatthew Dillon  * This program is free software; you can redistribute it and/or modify
3724a8d46aSMatthew Dillon  * it under the terms of version 2 of the GNU General Public License as
3824a8d46aSMatthew Dillon  * published by the Free Software Foundation.
3924a8d46aSMatthew Dillon  *
4024a8d46aSMatthew Dillon  * This program is distributed in the hope that it will be useful, but
4124a8d46aSMatthew Dillon  * WITHOUT ANY WARRANTY; without even the implied warranty of
4224a8d46aSMatthew Dillon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4324a8d46aSMatthew Dillon  * General Public License for more details.
4424a8d46aSMatthew Dillon  *
4524a8d46aSMatthew Dillon  * You should have received a copy of the GNU General Public License
4624a8d46aSMatthew Dillon  * along with this program; if not, write to the Free Software
4724a8d46aSMatthew Dillon  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
4824a8d46aSMatthew Dillon  * USA
4924a8d46aSMatthew Dillon  *
5024a8d46aSMatthew Dillon  * The full GNU General Public License is included in this distribution
5124a8d46aSMatthew Dillon  * in the file called COPYING.
5224a8d46aSMatthew Dillon  *
5324a8d46aSMatthew Dillon  * Contact Information:
5424a8d46aSMatthew Dillon  *  Intel Linux Wireless <ilw@linux.intel.com>
5524a8d46aSMatthew Dillon  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
5624a8d46aSMatthew Dillon  *
5724a8d46aSMatthew Dillon  *
5824a8d46aSMatthew Dillon  * BSD LICENSE
5924a8d46aSMatthew Dillon  *
6024a8d46aSMatthew Dillon  * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved.
6124a8d46aSMatthew Dillon  * All rights reserved.
6224a8d46aSMatthew Dillon  *
6324a8d46aSMatthew Dillon  * Redistribution and use in source and binary forms, with or without
6424a8d46aSMatthew Dillon  * modification, are permitted provided that the following conditions
6524a8d46aSMatthew Dillon  * are met:
6624a8d46aSMatthew Dillon  *
6724a8d46aSMatthew Dillon  *  * Redistributions of source code must retain the above copyright
6824a8d46aSMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
6924a8d46aSMatthew Dillon  *  * Redistributions in binary form must reproduce the above copyright
7024a8d46aSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
7124a8d46aSMatthew Dillon  *    the documentation and/or other materials provided with the
7224a8d46aSMatthew Dillon  *    distribution.
7324a8d46aSMatthew Dillon  *  * Neither the name Intel Corporation nor the names of its
7424a8d46aSMatthew Dillon  *    contributors may be used to endorse or promote products derived
7524a8d46aSMatthew Dillon  *    from this software without specific prior written permission.
7624a8d46aSMatthew Dillon  *
7724a8d46aSMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7824a8d46aSMatthew Dillon  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7924a8d46aSMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8024a8d46aSMatthew Dillon  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8124a8d46aSMatthew Dillon  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8224a8d46aSMatthew Dillon  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
8324a8d46aSMatthew Dillon  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
8424a8d46aSMatthew Dillon  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
8524a8d46aSMatthew Dillon  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
8624a8d46aSMatthew Dillon  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
8724a8d46aSMatthew Dillon  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8824a8d46aSMatthew Dillon  */
8924a8d46aSMatthew Dillon 
9024a8d46aSMatthew Dillon /*-
9124a8d46aSMatthew Dillon  * Copyright (c) 2007-2010 Damien Bergamini <damien.bergamini@free.fr>
9224a8d46aSMatthew Dillon  *
9324a8d46aSMatthew Dillon  * Permission to use, copy, modify, and distribute this software for any
9424a8d46aSMatthew Dillon  * purpose with or without fee is hereby granted, provided that the above
9524a8d46aSMatthew Dillon  * copyright notice and this permission notice appear in all copies.
9624a8d46aSMatthew Dillon  *
9724a8d46aSMatthew Dillon  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9824a8d46aSMatthew Dillon  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9924a8d46aSMatthew Dillon  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10024a8d46aSMatthew Dillon  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
10124a8d46aSMatthew Dillon  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
10224a8d46aSMatthew Dillon  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
10324a8d46aSMatthew Dillon  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
10424a8d46aSMatthew Dillon  */
10524a8d46aSMatthew Dillon #include <sys/param.h>
10624a8d46aSMatthew Dillon #include <sys/bus.h>
107*6acbba79SMatthew Dillon #include <sys/conf.h>
10824a8d46aSMatthew Dillon #include <sys/endian.h>
10924a8d46aSMatthew Dillon #include <sys/firmware.h>
11024a8d46aSMatthew Dillon #include <sys/kernel.h>
11124a8d46aSMatthew Dillon #include <sys/malloc.h>
11224a8d46aSMatthew Dillon #include <sys/mbuf.h>
11324a8d46aSMatthew Dillon #include <sys/rman.h>
11424a8d46aSMatthew Dillon #include <sys/sysctl.h>
11524a8d46aSMatthew Dillon #include <sys/linker.h>
11624a8d46aSMatthew Dillon 
11724a8d46aSMatthew Dillon #include <machine/endian.h>
11824a8d46aSMatthew Dillon 
11945bc40b1SMatthew Dillon #include <bus/pci/pcivar.h>
12045bc40b1SMatthew Dillon #include <bus/pci/pcireg.h>
12124a8d46aSMatthew Dillon 
12224a8d46aSMatthew Dillon #include <net/bpf.h>
12324a8d46aSMatthew Dillon 
12424a8d46aSMatthew Dillon #include <net/if.h>
12524a8d46aSMatthew Dillon #include <net/if_var.h>
12624a8d46aSMatthew Dillon #include <net/if_arp.h>
12724a8d46aSMatthew Dillon #include <net/if_dl.h>
12824a8d46aSMatthew Dillon #include <net/if_media.h>
12924a8d46aSMatthew Dillon #include <net/if_types.h>
13024a8d46aSMatthew Dillon 
13124a8d46aSMatthew Dillon #include <netinet/in.h>
13224a8d46aSMatthew Dillon #include <netinet/in_systm.h>
13324a8d46aSMatthew Dillon #include <netinet/if_ether.h>
13424a8d46aSMatthew Dillon #include <netinet/ip.h>
13524a8d46aSMatthew Dillon 
13645bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_var.h>
13745bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_regdomain.h>
13845bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_ratectl.h>
13945bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_radiotap.h>
14024a8d46aSMatthew Dillon 
14145bc40b1SMatthew Dillon #include "if_iwmreg.h"
14245bc40b1SMatthew Dillon #include "if_iwmvar.h"
14327d11320SImre Vadász #include "if_iwm_config.h"
14445bc40b1SMatthew Dillon #include "if_iwm_debug.h"
14545bc40b1SMatthew Dillon #include "if_iwm_pcie_trans.h"
14624a8d46aSMatthew Dillon 
14724a8d46aSMatthew Dillon /*
14824a8d46aSMatthew Dillon  * This is a subset of what's in linux iwlwifi/pcie/trans.c.
14924a8d46aSMatthew Dillon  * The rest can be migrated out into here once they're no longer in
15024a8d46aSMatthew Dillon  * if_iwm.c.
15124a8d46aSMatthew Dillon  */
15224a8d46aSMatthew Dillon 
15324a8d46aSMatthew Dillon /*
15424a8d46aSMatthew Dillon  * basic device access
15524a8d46aSMatthew Dillon  */
15624a8d46aSMatthew Dillon 
15724a8d46aSMatthew Dillon uint32_t
iwm_read_prph(struct iwm_softc * sc,uint32_t addr)15824a8d46aSMatthew Dillon iwm_read_prph(struct iwm_softc *sc, uint32_t addr)
15924a8d46aSMatthew Dillon {
16024a8d46aSMatthew Dillon 	IWM_WRITE(sc,
16124a8d46aSMatthew Dillon 	    IWM_HBUS_TARG_PRPH_RADDR, ((addr & 0x000fffff) | (3 << 24)));
16224a8d46aSMatthew Dillon 	IWM_BARRIER_READ_WRITE(sc);
16324a8d46aSMatthew Dillon 	return IWM_READ(sc, IWM_HBUS_TARG_PRPH_RDAT);
16424a8d46aSMatthew Dillon }
16524a8d46aSMatthew Dillon 
16624a8d46aSMatthew Dillon void
iwm_write_prph(struct iwm_softc * sc,uint32_t addr,uint32_t val)16724a8d46aSMatthew Dillon iwm_write_prph(struct iwm_softc *sc, uint32_t addr, uint32_t val)
16824a8d46aSMatthew Dillon {
16924a8d46aSMatthew Dillon 	IWM_WRITE(sc,
17024a8d46aSMatthew Dillon 	    IWM_HBUS_TARG_PRPH_WADDR, ((addr & 0x000fffff) | (3 << 24)));
17124a8d46aSMatthew Dillon 	IWM_BARRIER_WRITE(sc);
17224a8d46aSMatthew Dillon 	IWM_WRITE(sc, IWM_HBUS_TARG_PRPH_WDAT, val);
17324a8d46aSMatthew Dillon }
17424a8d46aSMatthew Dillon 
175*6acbba79SMatthew Dillon void
iwm_write_prph64(struct iwm_softc * sc,uint64_t addr,uint64_t val)176*6acbba79SMatthew Dillon iwm_write_prph64(struct iwm_softc *sc, uint64_t addr, uint64_t val)
177*6acbba79SMatthew Dillon {
178*6acbba79SMatthew Dillon 	iwm_write_prph(sc, (uint32_t)addr, val & 0xffffffff);
179*6acbba79SMatthew Dillon 	iwm_write_prph(sc, (uint32_t)addr + 4, val >> 32);
180*6acbba79SMatthew Dillon }
181*6acbba79SMatthew Dillon 
182*6acbba79SMatthew Dillon int
iwm_poll_prph(struct iwm_softc * sc,uint32_t addr,uint32_t bits,uint32_t mask,int timeout)183*6acbba79SMatthew Dillon iwm_poll_prph(struct iwm_softc *sc, uint32_t addr, uint32_t bits, uint32_t mask,
184*6acbba79SMatthew Dillon     int timeout)
185*6acbba79SMatthew Dillon {
186*6acbba79SMatthew Dillon 	do {
187*6acbba79SMatthew Dillon 		if ((iwm_read_prph(sc, addr) & mask) == (bits & mask))
188*6acbba79SMatthew Dillon 			return (0);
189*6acbba79SMatthew Dillon 		DELAY(10);
190*6acbba79SMatthew Dillon 		timeout -= 10;
191*6acbba79SMatthew Dillon 	} while (timeout > 0);
192*6acbba79SMatthew Dillon 
193*6acbba79SMatthew Dillon 	return (ETIMEDOUT);
194*6acbba79SMatthew Dillon }
195*6acbba79SMatthew Dillon 
19624a8d46aSMatthew Dillon #ifdef IWM_DEBUG
197*6acbba79SMatthew Dillon /* iwlwifi: pcie/trans.c */
19824a8d46aSMatthew Dillon int
iwm_read_mem(struct iwm_softc * sc,uint32_t addr,void * buf,int dwords)19924a8d46aSMatthew Dillon iwm_read_mem(struct iwm_softc *sc, uint32_t addr, void *buf, int dwords)
20024a8d46aSMatthew Dillon {
20124a8d46aSMatthew Dillon 	int offs, ret = 0;
20224a8d46aSMatthew Dillon 	uint32_t *vals = buf;
20324a8d46aSMatthew Dillon 
20424a8d46aSMatthew Dillon 	if (iwm_nic_lock(sc)) {
20524a8d46aSMatthew Dillon 		IWM_WRITE(sc, IWM_HBUS_TARG_MEM_RADDR, addr);
20624a8d46aSMatthew Dillon 		for (offs = 0; offs < dwords; offs++)
20724a8d46aSMatthew Dillon 			vals[offs] = IWM_READ(sc, IWM_HBUS_TARG_MEM_RDAT);
20824a8d46aSMatthew Dillon 		iwm_nic_unlock(sc);
20924a8d46aSMatthew Dillon 	} else {
21024a8d46aSMatthew Dillon 		ret = EBUSY;
21124a8d46aSMatthew Dillon 	}
21224a8d46aSMatthew Dillon 	return ret;
21324a8d46aSMatthew Dillon }
21424a8d46aSMatthew Dillon #endif
21524a8d46aSMatthew Dillon 
216*6acbba79SMatthew Dillon /* iwlwifi: pcie/trans.c */
21724a8d46aSMatthew Dillon int
iwm_write_mem(struct iwm_softc * sc,uint32_t addr,const void * buf,int dwords)21824a8d46aSMatthew Dillon iwm_write_mem(struct iwm_softc *sc, uint32_t addr, const void *buf, int dwords)
21924a8d46aSMatthew Dillon {
22024a8d46aSMatthew Dillon 	int offs;
22124a8d46aSMatthew Dillon 	const uint32_t *vals = buf;
22224a8d46aSMatthew Dillon 
22324a8d46aSMatthew Dillon 	if (iwm_nic_lock(sc)) {
22424a8d46aSMatthew Dillon 		IWM_WRITE(sc, IWM_HBUS_TARG_MEM_WADDR, addr);
22524a8d46aSMatthew Dillon 		/* WADDR auto-increments */
22624a8d46aSMatthew Dillon 		for (offs = 0; offs < dwords; offs++) {
22724a8d46aSMatthew Dillon 			uint32_t val = vals ? vals[offs] : 0;
22824a8d46aSMatthew Dillon 			IWM_WRITE(sc, IWM_HBUS_TARG_MEM_WDAT, val);
22924a8d46aSMatthew Dillon 		}
23024a8d46aSMatthew Dillon 		iwm_nic_unlock(sc);
23124a8d46aSMatthew Dillon 	} else {
23224a8d46aSMatthew Dillon 		IWM_DPRINTF(sc, IWM_DEBUG_TRANS,
23324a8d46aSMatthew Dillon 		    "%s: write_mem failed\n", __func__);
23424a8d46aSMatthew Dillon 		return EBUSY;
23524a8d46aSMatthew Dillon 	}
23624a8d46aSMatthew Dillon 	return 0;
23724a8d46aSMatthew Dillon }
23824a8d46aSMatthew Dillon 
23924a8d46aSMatthew Dillon int
iwm_write_mem32(struct iwm_softc * sc,uint32_t addr,uint32_t val)24024a8d46aSMatthew Dillon iwm_write_mem32(struct iwm_softc *sc, uint32_t addr, uint32_t val)
24124a8d46aSMatthew Dillon {
24224a8d46aSMatthew Dillon 	return iwm_write_mem(sc, addr, &val, 1);
24324a8d46aSMatthew Dillon }
24424a8d46aSMatthew Dillon 
24524a8d46aSMatthew Dillon int
iwm_poll_bit(struct iwm_softc * sc,int reg,uint32_t bits,uint32_t mask,int timo)24624a8d46aSMatthew Dillon iwm_poll_bit(struct iwm_softc *sc, int reg,
24724a8d46aSMatthew Dillon 	uint32_t bits, uint32_t mask, int timo)
24824a8d46aSMatthew Dillon {
24924a8d46aSMatthew Dillon 	for (;;) {
25024a8d46aSMatthew Dillon 		if ((IWM_READ(sc, reg) & mask) == (bits & mask)) {
25124a8d46aSMatthew Dillon 			return 1;
25224a8d46aSMatthew Dillon 		}
25324a8d46aSMatthew Dillon 		if (timo < 10) {
25424a8d46aSMatthew Dillon 			return 0;
25524a8d46aSMatthew Dillon 		}
25624a8d46aSMatthew Dillon 		timo -= 10;
25724a8d46aSMatthew Dillon 		DELAY(10);
25824a8d46aSMatthew Dillon 	}
25924a8d46aSMatthew Dillon }
26024a8d46aSMatthew Dillon 
26124a8d46aSMatthew Dillon int
iwm_nic_lock(struct iwm_softc * sc)26224a8d46aSMatthew Dillon iwm_nic_lock(struct iwm_softc *sc)
26324a8d46aSMatthew Dillon {
26424a8d46aSMatthew Dillon 	int rv = 0;
26524a8d46aSMatthew Dillon 
266ce43f57fSImre Vadász 	if (sc->cmd_hold_nic_awake)
267ce43f57fSImre Vadász 		return 1;
268ce43f57fSImre Vadász 
26924a8d46aSMatthew Dillon 	IWM_SETBITS(sc, IWM_CSR_GP_CNTRL,
27024a8d46aSMatthew Dillon 	    IWM_CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
27124a8d46aSMatthew Dillon 
272*6acbba79SMatthew Dillon 	if (sc->cfg->device_family >= IWM_DEVICE_FAMILY_8000)
273e8951a47SImre Vadász 		DELAY(2);
274e8951a47SImre Vadász 
27524a8d46aSMatthew Dillon 	if (iwm_poll_bit(sc, IWM_CSR_GP_CNTRL,
27624a8d46aSMatthew Dillon 	    IWM_CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
27724a8d46aSMatthew Dillon 	    IWM_CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY
27824a8d46aSMatthew Dillon 	     | IWM_CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP, 15000)) {
27924a8d46aSMatthew Dillon 		rv = 1;
28024a8d46aSMatthew Dillon 	} else {
28124a8d46aSMatthew Dillon 		/* jolt */
282e8951a47SImre Vadász 		IWM_DPRINTF(sc, IWM_DEBUG_RESET,
283e8951a47SImre Vadász 		    "%s: resetting device via NMI\n", __func__);
28424a8d46aSMatthew Dillon 		IWM_WRITE(sc, IWM_CSR_RESET, IWM_CSR_RESET_REG_FLAG_FORCE_NMI);
28524a8d46aSMatthew Dillon 	}
28624a8d46aSMatthew Dillon 
28724a8d46aSMatthew Dillon 	return rv;
28824a8d46aSMatthew Dillon }
28924a8d46aSMatthew Dillon 
29024a8d46aSMatthew Dillon void
iwm_nic_unlock(struct iwm_softc * sc)29124a8d46aSMatthew Dillon iwm_nic_unlock(struct iwm_softc *sc)
29224a8d46aSMatthew Dillon {
293ce43f57fSImre Vadász 	if (sc->cmd_hold_nic_awake)
294ce43f57fSImre Vadász 		return;
295ce43f57fSImre Vadász 
29624a8d46aSMatthew Dillon 	IWM_CLRBITS(sc, IWM_CSR_GP_CNTRL,
29724a8d46aSMatthew Dillon 	    IWM_CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
29824a8d46aSMatthew Dillon }
29924a8d46aSMatthew Dillon 
30024a8d46aSMatthew Dillon void
iwm_set_bits_mask_prph(struct iwm_softc * sc,uint32_t reg,uint32_t bits,uint32_t mask)30124a8d46aSMatthew Dillon iwm_set_bits_mask_prph(struct iwm_softc *sc,
30224a8d46aSMatthew Dillon 	uint32_t reg, uint32_t bits, uint32_t mask)
30324a8d46aSMatthew Dillon {
30424a8d46aSMatthew Dillon 	uint32_t val;
30524a8d46aSMatthew Dillon 
30624a8d46aSMatthew Dillon 	/* XXX: no error path? */
30724a8d46aSMatthew Dillon 	if (iwm_nic_lock(sc)) {
30824a8d46aSMatthew Dillon 		val = iwm_read_prph(sc, reg) & mask;
30924a8d46aSMatthew Dillon 		val |= bits;
31024a8d46aSMatthew Dillon 		iwm_write_prph(sc, reg, val);
31124a8d46aSMatthew Dillon 		iwm_nic_unlock(sc);
31224a8d46aSMatthew Dillon 	}
31324a8d46aSMatthew Dillon }
31424a8d46aSMatthew Dillon 
31524a8d46aSMatthew Dillon void
iwm_set_bits_prph(struct iwm_softc * sc,uint32_t reg,uint32_t bits)31624a8d46aSMatthew Dillon iwm_set_bits_prph(struct iwm_softc *sc, uint32_t reg, uint32_t bits)
31724a8d46aSMatthew Dillon {
31824a8d46aSMatthew Dillon 	iwm_set_bits_mask_prph(sc, reg, bits, ~0);
31924a8d46aSMatthew Dillon }
32024a8d46aSMatthew Dillon 
32124a8d46aSMatthew Dillon void
iwm_clear_bits_prph(struct iwm_softc * sc,uint32_t reg,uint32_t bits)32224a8d46aSMatthew Dillon iwm_clear_bits_prph(struct iwm_softc *sc, uint32_t reg, uint32_t bits)
32324a8d46aSMatthew Dillon {
32424a8d46aSMatthew Dillon 	iwm_set_bits_mask_prph(sc, reg, 0, ~bits);
32524a8d46aSMatthew Dillon }
32624a8d46aSMatthew Dillon 
32724a8d46aSMatthew Dillon /*
32824a8d46aSMatthew Dillon  * High-level hardware frobbing routines
32924a8d46aSMatthew Dillon  */
33024a8d46aSMatthew Dillon 
33124a8d46aSMatthew Dillon void
iwm_enable_rfkill_int(struct iwm_softc * sc)33224a8d46aSMatthew Dillon iwm_enable_rfkill_int(struct iwm_softc *sc)
33324a8d46aSMatthew Dillon {
33424a8d46aSMatthew Dillon 	sc->sc_intmask = IWM_CSR_INT_BIT_RF_KILL;
33524a8d46aSMatthew Dillon 	IWM_WRITE(sc, IWM_CSR_INT_MASK, sc->sc_intmask);
336*6acbba79SMatthew Dillon 	IWM_SETBITS(sc, IWM_CSR_GP_CNTRL,
337*6acbba79SMatthew Dillon 	    IWM_CSR_GP_CNTRL_REG_FLAG_RFKILL_WAKE_L1A_EN);
33824a8d46aSMatthew Dillon }
33924a8d46aSMatthew Dillon 
34024a8d46aSMatthew Dillon int
iwm_check_rfkill(struct iwm_softc * sc)34124a8d46aSMatthew Dillon iwm_check_rfkill(struct iwm_softc *sc)
34224a8d46aSMatthew Dillon {
34324a8d46aSMatthew Dillon 	uint32_t v;
34424a8d46aSMatthew Dillon 	int rv;
34524a8d46aSMatthew Dillon 
34624a8d46aSMatthew Dillon 	/*
34724a8d46aSMatthew Dillon 	 * "documentation" is not really helpful here:
34824a8d46aSMatthew Dillon 	 *  27:	HW_RF_KILL_SW
34924a8d46aSMatthew Dillon 	 *	Indicates state of (platform's) hardware RF-Kill switch
35024a8d46aSMatthew Dillon 	 *
35124a8d46aSMatthew Dillon 	 * But apparently when it's off, it's on ...
35224a8d46aSMatthew Dillon 	 */
35324a8d46aSMatthew Dillon 	v = IWM_READ(sc, IWM_CSR_GP_CNTRL);
35424a8d46aSMatthew Dillon 	rv = (v & IWM_CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW) == 0;
35524a8d46aSMatthew Dillon 	if (rv) {
35624a8d46aSMatthew Dillon 		sc->sc_flags |= IWM_FLAG_RFKILL;
35724a8d46aSMatthew Dillon 	} else {
35824a8d46aSMatthew Dillon 		sc->sc_flags &= ~IWM_FLAG_RFKILL;
35924a8d46aSMatthew Dillon 	}
36024a8d46aSMatthew Dillon 
36124a8d46aSMatthew Dillon 	return rv;
36224a8d46aSMatthew Dillon }
36324a8d46aSMatthew Dillon 
36424a8d46aSMatthew Dillon 
36524a8d46aSMatthew Dillon #define IWM_HW_READY_TIMEOUT 50
36624a8d46aSMatthew Dillon int
iwm_set_hw_ready(struct iwm_softc * sc)36724a8d46aSMatthew Dillon iwm_set_hw_ready(struct iwm_softc *sc)
36824a8d46aSMatthew Dillon {
369edfc8a07SImre Vadász 	int ready;
370edfc8a07SImre Vadász 
37124a8d46aSMatthew Dillon 	IWM_SETBITS(sc, IWM_CSR_HW_IF_CONFIG_REG,
37224a8d46aSMatthew Dillon 	    IWM_CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
37324a8d46aSMatthew Dillon 
374edfc8a07SImre Vadász 	ready = iwm_poll_bit(sc, IWM_CSR_HW_IF_CONFIG_REG,
37524a8d46aSMatthew Dillon 	    IWM_CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
37624a8d46aSMatthew Dillon 	    IWM_CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
37724a8d46aSMatthew Dillon 	    IWM_HW_READY_TIMEOUT);
378edfc8a07SImre Vadász 	if (ready) {
379edfc8a07SImre Vadász 		IWM_SETBITS(sc, IWM_CSR_MBOX_SET_REG,
380edfc8a07SImre Vadász 		    IWM_CSR_MBOX_SET_REG_OS_ALIVE);
381edfc8a07SImre Vadász 	}
382edfc8a07SImre Vadász 	return ready;
38324a8d46aSMatthew Dillon }
38424a8d46aSMatthew Dillon #undef IWM_HW_READY_TIMEOUT
38524a8d46aSMatthew Dillon 
38624a8d46aSMatthew Dillon int
iwm_prepare_card_hw(struct iwm_softc * sc)38724a8d46aSMatthew Dillon iwm_prepare_card_hw(struct iwm_softc *sc)
38824a8d46aSMatthew Dillon {
38924a8d46aSMatthew Dillon 	int rv = 0;
39024a8d46aSMatthew Dillon 	int t = 0;
39124a8d46aSMatthew Dillon 
39224a8d46aSMatthew Dillon 	IWM_DPRINTF(sc, IWM_DEBUG_RESET, "->%s\n", __func__);
39324a8d46aSMatthew Dillon 	if (iwm_set_hw_ready(sc))
39424a8d46aSMatthew Dillon 		goto out;
39524a8d46aSMatthew Dillon 
396*6acbba79SMatthew Dillon 	IWM_SETBITS(sc, IWM_CSR_DBG_LINK_PWR_MGMT_REG,
397*6acbba79SMatthew Dillon 	    IWM_CSR_RESET_LINK_PWR_MGMT_DISABLED);
398*6acbba79SMatthew Dillon 	DELAY(1000);
39929fcb331SImre Vadász 
40024a8d46aSMatthew Dillon 	/* If HW is not ready, prepare the conditions to check again */
40124a8d46aSMatthew Dillon 	IWM_SETBITS(sc, IWM_CSR_HW_IF_CONFIG_REG,
40224a8d46aSMatthew Dillon 	    IWM_CSR_HW_IF_CONFIG_REG_PREPARE);
40324a8d46aSMatthew Dillon 
40424a8d46aSMatthew Dillon 	do {
40524a8d46aSMatthew Dillon 		if (iwm_set_hw_ready(sc))
40624a8d46aSMatthew Dillon 			goto out;
40724a8d46aSMatthew Dillon 		DELAY(200);
40824a8d46aSMatthew Dillon 		t += 200;
40924a8d46aSMatthew Dillon 	} while (t < 150000);
41024a8d46aSMatthew Dillon 
41124a8d46aSMatthew Dillon 	rv = ETIMEDOUT;
41224a8d46aSMatthew Dillon 
41324a8d46aSMatthew Dillon  out:
41424a8d46aSMatthew Dillon 	IWM_DPRINTF(sc, IWM_DEBUG_RESET, "<-%s\n", __func__);
41524a8d46aSMatthew Dillon 	return rv;
41624a8d46aSMatthew Dillon }
41724a8d46aSMatthew Dillon 
41824a8d46aSMatthew Dillon void
iwm_apm_config(struct iwm_softc * sc)41924a8d46aSMatthew Dillon iwm_apm_config(struct iwm_softc *sc)
42024a8d46aSMatthew Dillon {
42108a7ad5aSImre Vadász 	uint16_t lctl, cap;
42208a7ad5aSImre Vadász 	int pcie_ptr;
42324a8d46aSMatthew Dillon 
42408a7ad5aSImre Vadász 	/*
42508a7ad5aSImre Vadász 	 * HW bug W/A for instability in PCIe bus L0S->L1 transition.
42608a7ad5aSImre Vadász 	 * Check if BIOS (or OS) enabled L1-ASPM on this device.
42708a7ad5aSImre Vadász 	 * If so (likely), disable L0S, so device moves directly L0->L1;
42808a7ad5aSImre Vadász 	 *    costs negligible amount of power savings.
42908a7ad5aSImre Vadász 	 * If not (unlikely), enable L0S, so there is at least some
43008a7ad5aSImre Vadász 	 *    power savings, even without L1.
43108a7ad5aSImre Vadász 	 */
432*6acbba79SMatthew Dillon #if !defined(__DragonFly__)
43308a7ad5aSImre Vadász 	int error;
43408a7ad5aSImre Vadász 
43508a7ad5aSImre Vadász 	error = pci_find_cap(sc->sc_dev, PCIY_EXPRESS, &pcie_ptr);
43608a7ad5aSImre Vadász 	if (error != 0)
43708a7ad5aSImre Vadász 		return;
43808a7ad5aSImre Vadász 	lctl = pci_read_config(sc->sc_dev, pcie_ptr + PCIER_LINK_CTL,
43908a7ad5aSImre Vadász 	    sizeof(lctl));
44008a7ad5aSImre Vadász 	if (lctl & PCIEM_LINK_CTL_ASPMC_L1)  {
441*6acbba79SMatthew Dillon #else
442*6acbba79SMatthew Dillon 	pcie_ptr = pci_get_pciecap_ptr(sc->sc_dev);
443*6acbba79SMatthew Dillon 	if (pcie_ptr == 0)
444*6acbba79SMatthew Dillon 		return;
445*6acbba79SMatthew Dillon 	lctl = pci_read_config(sc->sc_dev, pcie_ptr + PCIER_LINKCTRL,
446*6acbba79SMatthew Dillon 		sizeof(lctl));
447*6acbba79SMatthew Dillon 	if (lctl & PCIEM_LNKCTL_ASPM_L1)  {
44845bc40b1SMatthew Dillon #endif
44924a8d46aSMatthew Dillon 		IWM_SETBITS(sc, IWM_CSR_GIO_REG,
45024a8d46aSMatthew Dillon 		    IWM_CSR_GIO_REG_VAL_L0S_ENABLED);
45124a8d46aSMatthew Dillon 	} else {
45224a8d46aSMatthew Dillon 		IWM_CLRBITS(sc, IWM_CSR_GIO_REG,
45324a8d46aSMatthew Dillon 		    IWM_CSR_GIO_REG_VAL_L0S_ENABLED);
45424a8d46aSMatthew Dillon 	}
45508a7ad5aSImre Vadász 
456*6acbba79SMatthew Dillon #if !defined(__DragonFly__)
45708a7ad5aSImre Vadász 	cap = pci_read_config(sc->sc_dev, pcie_ptr + PCIER_DEVICE_CTL2,
45808a7ad5aSImre Vadász 	    sizeof(cap));
45908a7ad5aSImre Vadász 	sc->sc_ltr_enabled = (cap & PCIEM_CTL2_LTR_ENABLE) ? 1 : 0;
46008a7ad5aSImre Vadász 	IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_PWRSAVE,
46108a7ad5aSImre Vadász 	    "L1 %sabled - LTR %sabled\n",
46208a7ad5aSImre Vadász 	    (lctl & PCIEM_LINK_CTL_ASPMC_L1) ? "En" : "Dis",
46308a7ad5aSImre Vadász 	    sc->sc_ltr_enabled ? "En" : "Dis");
464*6acbba79SMatthew Dillon #else
465*6acbba79SMatthew Dillon 	cap = pci_read_config(sc->sc_dev, pcie_ptr + PCIER_DEVCTRL2,
466*6acbba79SMatthew Dillon 	    sizeof(cap));
467*6acbba79SMatthew Dillon 	sc->sc_ltr_enabled = (cap & PCIEM_DEVCTL2_LTR_ENABLE) ? 1 : 0;
468*6acbba79SMatthew Dillon 	IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_PWRSAVE,
469*6acbba79SMatthew Dillon 	    "L1 %sabled - LTR %sabled\n",
470*6acbba79SMatthew Dillon 	    (lctl & PCIEM_LNKCTL_ASPM_L1) ? "En" : "Dis",
471*6acbba79SMatthew Dillon 	    sc->sc_ltr_enabled ? "En" : "Dis");
472*6acbba79SMatthew Dillon #endif
47324a8d46aSMatthew Dillon }
47424a8d46aSMatthew Dillon 
47524a8d46aSMatthew Dillon /*
47624a8d46aSMatthew Dillon  * Start up NIC's basic functionality after it has been reset
47724a8d46aSMatthew Dillon  * (e.g. after platform boot, or shutdown via iwm_pcie_apm_stop())
47824a8d46aSMatthew Dillon  * NOTE:  This does not load uCode nor start the embedded processor
47924a8d46aSMatthew Dillon  */
48024a8d46aSMatthew Dillon int
48124a8d46aSMatthew Dillon iwm_apm_init(struct iwm_softc *sc)
48224a8d46aSMatthew Dillon {
48324a8d46aSMatthew Dillon 	int error = 0;
48424a8d46aSMatthew Dillon 
48524a8d46aSMatthew Dillon 	IWM_DPRINTF(sc, IWM_DEBUG_RESET, "iwm apm start\n");
48624a8d46aSMatthew Dillon 
48724a8d46aSMatthew Dillon 	/* Disable L0S exit timer (platform NMI Work/Around) */
488*6acbba79SMatthew Dillon 	if (sc->cfg->device_family < IWM_DEVICE_FAMILY_8000) {
48924a8d46aSMatthew Dillon 		IWM_SETBITS(sc, IWM_CSR_GIO_CHICKEN_BITS,
49024a8d46aSMatthew Dillon 		    IWM_CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
491e8951a47SImre Vadász 	}
49224a8d46aSMatthew Dillon 
49324a8d46aSMatthew Dillon 	/*
49424a8d46aSMatthew Dillon 	 * Disable L0s without affecting L1;
49524a8d46aSMatthew Dillon 	 *  don't wait for ICH L0s (ICH bug W/A)
49624a8d46aSMatthew Dillon 	 */
49724a8d46aSMatthew Dillon 	IWM_SETBITS(sc, IWM_CSR_GIO_CHICKEN_BITS,
49824a8d46aSMatthew Dillon 	    IWM_CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
49924a8d46aSMatthew Dillon 
50024a8d46aSMatthew Dillon 	/* Set FH wait threshold to maximum (HW error during stress W/A) */
50124a8d46aSMatthew Dillon 	IWM_SETBITS(sc, IWM_CSR_DBG_HPET_MEM_REG, IWM_CSR_DBG_HPET_MEM_REG_VAL);
50224a8d46aSMatthew Dillon 
50324a8d46aSMatthew Dillon 	/*
50424a8d46aSMatthew Dillon 	 * Enable HAP INTA (interrupt from management bus) to
50524a8d46aSMatthew Dillon 	 * wake device's PCI Express link L1a -> L0s
50624a8d46aSMatthew Dillon 	 */
50724a8d46aSMatthew Dillon 	IWM_SETBITS(sc, IWM_CSR_HW_IF_CONFIG_REG,
50824a8d46aSMatthew Dillon 	    IWM_CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
50924a8d46aSMatthew Dillon 
51024a8d46aSMatthew Dillon 	iwm_apm_config(sc);
51124a8d46aSMatthew Dillon 
512e8951a47SImre Vadász #if 0 /* not for 7k/8k */
51324a8d46aSMatthew Dillon 	/* Configure analog phase-lock-loop before activating to D0A */
51424a8d46aSMatthew Dillon 	if (trans->cfg->base_params->pll_cfg_val)
51524a8d46aSMatthew Dillon 		IWM_SETBITS(trans, IWM_CSR_ANA_PLL_CFG,
51624a8d46aSMatthew Dillon 		    trans->cfg->base_params->pll_cfg_val);
51724a8d46aSMatthew Dillon #endif
51824a8d46aSMatthew Dillon 
51924a8d46aSMatthew Dillon 	/*
52024a8d46aSMatthew Dillon 	 * Set "initialization complete" bit to move adapter from
52124a8d46aSMatthew Dillon 	 * D0U* --> D0A* (powered-up active) state.
52224a8d46aSMatthew Dillon 	 */
52324a8d46aSMatthew Dillon 	IWM_SETBITS(sc, IWM_CSR_GP_CNTRL, IWM_CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
52424a8d46aSMatthew Dillon 
52524a8d46aSMatthew Dillon 	/*
52624a8d46aSMatthew Dillon 	 * Wait for clock stabilization; once stabilized, access to
52724a8d46aSMatthew Dillon 	 * device-internal resources is supported, e.g. iwm_write_prph()
52824a8d46aSMatthew Dillon 	 * and accesses to uCode SRAM.
52924a8d46aSMatthew Dillon 	 */
53024a8d46aSMatthew Dillon 	if (!iwm_poll_bit(sc, IWM_CSR_GP_CNTRL,
53124a8d46aSMatthew Dillon 	    IWM_CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
53224a8d46aSMatthew Dillon 	    IWM_CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000)) {
53324a8d46aSMatthew Dillon 		device_printf(sc->sc_dev,
53424a8d46aSMatthew Dillon 		    "timeout waiting for clock stabilization\n");
53529fcb331SImre Vadász 		error = ETIMEDOUT;
53624a8d46aSMatthew Dillon 		goto out;
53724a8d46aSMatthew Dillon 	}
53824a8d46aSMatthew Dillon 
539cc8d6ccfSImre Vadász 	if (sc->cfg->host_interrupt_operation_mode) {
54024a8d46aSMatthew Dillon 		/*
54124a8d46aSMatthew Dillon 		 * This is a bit of an abuse - This is needed for 7260 / 3160
54224a8d46aSMatthew Dillon 		 * only check host_interrupt_operation_mode even if this is
54324a8d46aSMatthew Dillon 		 * not related to host_interrupt_operation_mode.
54424a8d46aSMatthew Dillon 		 *
54524a8d46aSMatthew Dillon 		 * Enable the oscillator to count wake up time for L1 exit. This
54624a8d46aSMatthew Dillon 		 * consumes slightly more power (100uA) - but allows to be sure
54724a8d46aSMatthew Dillon 		 * that we wake up from L1 on time.
54824a8d46aSMatthew Dillon 		 *
54924a8d46aSMatthew Dillon 		 * This looks weird: read twice the same register, discard the
55024a8d46aSMatthew Dillon 		 * value, set a bit, and yet again, read that same register
55124a8d46aSMatthew Dillon 		 * just to discard the value. But that's the way the hardware
55224a8d46aSMatthew Dillon 		 * seems to like it.
55324a8d46aSMatthew Dillon 		 */
5546c5470f2SImre Vadász 		if (iwm_nic_lock(sc)) {
55524a8d46aSMatthew Dillon 			iwm_read_prph(sc, IWM_OSC_CLK);
55624a8d46aSMatthew Dillon 			iwm_read_prph(sc, IWM_OSC_CLK);
557f88ab372SImre Vadász 			iwm_nic_unlock(sc);
5586c5470f2SImre Vadász 		}
55924a8d46aSMatthew Dillon 		iwm_set_bits_prph(sc, IWM_OSC_CLK, IWM_OSC_CLK_FORCE_CONTROL);
5606c5470f2SImre Vadász 		if (iwm_nic_lock(sc)) {
56124a8d46aSMatthew Dillon 			iwm_read_prph(sc, IWM_OSC_CLK);
56224a8d46aSMatthew Dillon 			iwm_read_prph(sc, IWM_OSC_CLK);
563f88ab372SImre Vadász 			iwm_nic_unlock(sc);
56424a8d46aSMatthew Dillon 		}
5656c5470f2SImre Vadász 	}
56624a8d46aSMatthew Dillon 
56724a8d46aSMatthew Dillon 	/*
56824a8d46aSMatthew Dillon 	 * Enable DMA clock and wait for it to stabilize.
56924a8d46aSMatthew Dillon 	 *
57024a8d46aSMatthew Dillon 	 * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" bits
57124a8d46aSMatthew Dillon 	 * do not disable clocks.  This preserves any hardware bits already
57224a8d46aSMatthew Dillon 	 * set by default in "CLK_CTRL_REG" after reset.
57324a8d46aSMatthew Dillon 	 */
574cc8d6ccfSImre Vadász 	if (sc->cfg->device_family == IWM_DEVICE_FAMILY_7000) {
5756c5470f2SImre Vadász 		if (iwm_nic_lock(sc)) {
576e8951a47SImre Vadász 			iwm_write_prph(sc, IWM_APMG_CLK_EN_REG,
577e8951a47SImre Vadász 			    IWM_APMG_CLK_VAL_DMA_CLK_RQT);
578f88ab372SImre Vadász 			iwm_nic_unlock(sc);
5796c5470f2SImre Vadász 		}
58024a8d46aSMatthew Dillon 		DELAY(20);
58124a8d46aSMatthew Dillon 
58224a8d46aSMatthew Dillon 		/* Disable L1-Active */
58324a8d46aSMatthew Dillon 		iwm_set_bits_prph(sc, IWM_APMG_PCIDEV_STT_REG,
58424a8d46aSMatthew Dillon 		    IWM_APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
58524a8d46aSMatthew Dillon 
58624a8d46aSMatthew Dillon 		/* Clear the interrupt in APMG if the NIC is in RFKILL */
5876c5470f2SImre Vadász 		if (iwm_nic_lock(sc)) {
58824a8d46aSMatthew Dillon 			iwm_write_prph(sc, IWM_APMG_RTC_INT_STT_REG,
58924a8d46aSMatthew Dillon 			    IWM_APMG_RTC_INT_STT_RFKILL);
590f88ab372SImre Vadász 			iwm_nic_unlock(sc);
591e8951a47SImre Vadász 		}
5926c5470f2SImre Vadász 	}
59324a8d46aSMatthew Dillon  out:
59424a8d46aSMatthew Dillon 	if (error)
59524a8d46aSMatthew Dillon 		device_printf(sc->sc_dev, "apm init error %d\n", error);
59624a8d46aSMatthew Dillon 	return error;
59724a8d46aSMatthew Dillon }
59824a8d46aSMatthew Dillon 
599*6acbba79SMatthew Dillon /* iwlwifi/pcie/trans.c */
60024a8d46aSMatthew Dillon void
60124a8d46aSMatthew Dillon iwm_apm_stop(struct iwm_softc *sc)
60224a8d46aSMatthew Dillon {
603*6acbba79SMatthew Dillon 	IWM_SETBITS(sc, IWM_CSR_DBG_LINK_PWR_MGMT_REG,
604*6acbba79SMatthew Dillon 	    IWM_CSR_RESET_LINK_PWR_MGMT_DISABLED);
605*6acbba79SMatthew Dillon 	IWM_SETBITS(sc, IWM_CSR_HW_IF_CONFIG_REG,
606*6acbba79SMatthew Dillon 	    IWM_CSR_HW_IF_CONFIG_REG_PREPARE |
607*6acbba79SMatthew Dillon 	    IWM_CSR_HW_IF_CONFIG_REG_ENABLE_PME);
608*6acbba79SMatthew Dillon 	DELAY(1000);
609*6acbba79SMatthew Dillon 	IWM_CLRBITS(sc, IWM_CSR_DBG_LINK_PWR_MGMT_REG,
610*6acbba79SMatthew Dillon 	    IWM_CSR_RESET_LINK_PWR_MGMT_DISABLED);
611*6acbba79SMatthew Dillon 	DELAY(5000);
6122427de16SImre Vadász 
61324a8d46aSMatthew Dillon 	/* stop device's busmaster DMA activity */
61424a8d46aSMatthew Dillon 	IWM_SETBITS(sc, IWM_CSR_RESET, IWM_CSR_RESET_REG_FLAG_STOP_MASTER);
61524a8d46aSMatthew Dillon 
61624a8d46aSMatthew Dillon 	if (!iwm_poll_bit(sc, IWM_CSR_RESET,
61724a8d46aSMatthew Dillon 	    IWM_CSR_RESET_REG_FLAG_MASTER_DISABLED,
61824a8d46aSMatthew Dillon 	    IWM_CSR_RESET_REG_FLAG_MASTER_DISABLED, 100))
61924a8d46aSMatthew Dillon 		device_printf(sc->sc_dev, "timeout waiting for master\n");
6202427de16SImre Vadász 
6212427de16SImre Vadász 	/*
6222427de16SImre Vadász 	 * Clear "initialization complete" bit to move adapter from
6232427de16SImre Vadász 	 * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
6242427de16SImre Vadász 	 */
6252427de16SImre Vadász 	IWM_CLRBITS(sc, IWM_CSR_GP_CNTRL,
6262427de16SImre Vadász 	    IWM_CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
627*6acbba79SMatthew Dillon 
628*6acbba79SMatthew Dillon 	IWM_DPRINTF(sc, IWM_DEBUG_TRANS, "%s: iwm apm stop\n", __func__);
62924a8d46aSMatthew Dillon }
63024a8d46aSMatthew Dillon 
631*6acbba79SMatthew Dillon /* iwlwifi pcie/trans.c */
63224a8d46aSMatthew Dillon int
63324a8d46aSMatthew Dillon iwm_start_hw(struct iwm_softc *sc)
63424a8d46aSMatthew Dillon {
63524a8d46aSMatthew Dillon 	int error;
63624a8d46aSMatthew Dillon 
63724a8d46aSMatthew Dillon 	if ((error = iwm_prepare_card_hw(sc)) != 0)
63824a8d46aSMatthew Dillon 		return error;
63924a8d46aSMatthew Dillon 
64024a8d46aSMatthew Dillon 	/* Reset the entire device */
641edfc8a07SImre Vadász 	IWM_WRITE(sc, IWM_CSR_RESET, IWM_CSR_RESET_REG_FLAG_SW_RESET);
642*6acbba79SMatthew Dillon 	DELAY(5000);
64324a8d46aSMatthew Dillon 
64424a8d46aSMatthew Dillon 	if ((error = iwm_apm_init(sc)) != 0)
64524a8d46aSMatthew Dillon 		return error;
64624a8d46aSMatthew Dillon 
647*6acbba79SMatthew Dillon 	/* On newer chipsets MSI is disabled by default. */
648*6acbba79SMatthew Dillon 	if (sc->cfg->mqrx_supported)
649*6acbba79SMatthew Dillon 		iwm_write_prph(sc, IWM_UREG_CHICK, IWM_UREG_CHICK_MSI_ENABLE);
650*6acbba79SMatthew Dillon 
65124a8d46aSMatthew Dillon 	iwm_enable_rfkill_int(sc);
65224a8d46aSMatthew Dillon 	iwm_check_rfkill(sc);
65324a8d46aSMatthew Dillon 
65424a8d46aSMatthew Dillon 	return 0;
65524a8d46aSMatthew Dillon }
65624a8d46aSMatthew Dillon 
657*6acbba79SMatthew Dillon /* iwlwifi pcie/trans.c (always main power) */
65824a8d46aSMatthew Dillon void
65924a8d46aSMatthew Dillon iwm_set_pwr(struct iwm_softc *sc)
66024a8d46aSMatthew Dillon {
66124a8d46aSMatthew Dillon 	iwm_set_bits_mask_prph(sc, IWM_APMG_PS_CTRL_REG,
66224a8d46aSMatthew Dillon 	    IWM_APMG_PS_CTRL_VAL_PWR_SRC_VMAIN, ~IWM_APMG_PS_CTRL_MSK_PWR_SRC);
66324a8d46aSMatthew Dillon }
66424a8d46aSMatthew Dillon 
665*6acbba79SMatthew Dillon /* iwlwifi pcie/rx.c */
66624a8d46aSMatthew Dillon int
66724a8d46aSMatthew Dillon iwm_pcie_rx_stop(struct iwm_softc *sc)
66824a8d46aSMatthew Dillon {
669*6acbba79SMatthew Dillon 	int ret;
670*6acbba79SMatthew Dillon 
671*6acbba79SMatthew Dillon 	ret = 0;
67250787d03SImre Vadász 	if (iwm_nic_lock(sc)) {
673*6acbba79SMatthew Dillon 		if (sc->cfg->mqrx_supported) {
674*6acbba79SMatthew Dillon 			iwm_write_prph(sc, IWM_RFH_RXF_DMA_CFG, 0);
675*6acbba79SMatthew Dillon 			ret = iwm_poll_prph(sc, IWM_RFH_GEN_STATUS,
676*6acbba79SMatthew Dillon 			    IWM_RXF_DMA_IDLE, IWM_RXF_DMA_IDLE, 1000);
677*6acbba79SMatthew Dillon 		} else {
67824a8d46aSMatthew Dillon 			IWM_WRITE(sc, IWM_FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
67950787d03SImre Vadász 			ret = iwm_poll_bit(sc, IWM_FH_MEM_RSSR_RX_STATUS_REG,
68024a8d46aSMatthew Dillon 			    IWM_FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE,
68124a8d46aSMatthew Dillon 			    IWM_FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE,
68250787d03SImre Vadász 			    1000);
683*6acbba79SMatthew Dillon 		}
68450787d03SImre Vadász 		iwm_nic_unlock(sc);
68550787d03SImre Vadász 	}
68650787d03SImre Vadász 	return ret;
68724a8d46aSMatthew Dillon }
688ce43f57fSImre Vadász 
689ce43f57fSImre Vadász void
690ce43f57fSImre Vadász iwm_pcie_clear_cmd_in_flight(struct iwm_softc *sc)
691ce43f57fSImre Vadász {
692ce43f57fSImre Vadász 	if (!sc->cfg->apmg_wake_up_wa)
693ce43f57fSImre Vadász 		return;
694ce43f57fSImre Vadász 
695ce43f57fSImre Vadász 	if (!sc->cmd_hold_nic_awake) {
696ce43f57fSImre Vadász 		device_printf(sc->sc_dev,
697ce43f57fSImre Vadász 		    "%s: cmd_hold_nic_awake not set\n", __func__);
698ce43f57fSImre Vadász 		return;
699ce43f57fSImre Vadász 	}
700ce43f57fSImre Vadász 
701ce43f57fSImre Vadász 	sc->cmd_hold_nic_awake = 0;
702ce43f57fSImre Vadász 	IWM_CLRBITS(sc, IWM_CSR_GP_CNTRL,
703ce43f57fSImre Vadász 	    IWM_CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
704ce43f57fSImre Vadász }
705ce43f57fSImre Vadász 
706ce43f57fSImre Vadász int
707ce43f57fSImre Vadász iwm_pcie_set_cmd_in_flight(struct iwm_softc *sc)
708ce43f57fSImre Vadász {
709ce43f57fSImre Vadász 	int ret;
710ce43f57fSImre Vadász 
711ce43f57fSImre Vadász 	/*
712ce43f57fSImre Vadász 	 * wake up the NIC to make sure that the firmware will see the host
713ce43f57fSImre Vadász 	 * command - we will let the NIC sleep once all the host commands
714ce43f57fSImre Vadász 	 * returned. This needs to be done only on NICs that have
715ce43f57fSImre Vadász 	 * apmg_wake_up_wa set.
716ce43f57fSImre Vadász 	 */
717ce43f57fSImre Vadász 	if (sc->cfg->apmg_wake_up_wa &&
718ce43f57fSImre Vadász 	    !sc->cmd_hold_nic_awake) {
719ce43f57fSImre Vadász 
720ce43f57fSImre Vadász 		IWM_SETBITS(sc, IWM_CSR_GP_CNTRL,
721ce43f57fSImre Vadász 		    IWM_CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
722ce43f57fSImre Vadász 
723ce43f57fSImre Vadász 		ret = iwm_poll_bit(sc, IWM_CSR_GP_CNTRL,
724ce43f57fSImre Vadász 		    IWM_CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
725ce43f57fSImre Vadász 		    (IWM_CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
726ce43f57fSImre Vadász 		     IWM_CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP),
727ce43f57fSImre Vadász 		    15000);
728ce43f57fSImre Vadász 		if (ret == 0) {
729ce43f57fSImre Vadász 			IWM_CLRBITS(sc, IWM_CSR_GP_CNTRL,
730ce43f57fSImre Vadász 			    IWM_CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
731ce43f57fSImre Vadász 			device_printf(sc->sc_dev,
732ce43f57fSImre Vadász 			    "%s: Failed to wake NIC for hcmd\n", __func__);
733ce43f57fSImre Vadász 			return EIO;
734ce43f57fSImre Vadász 		}
735ce43f57fSImre Vadász 		sc->cmd_hold_nic_awake = 1;
736ce43f57fSImre Vadász 	}
737ce43f57fSImre Vadász 
738ce43f57fSImre Vadász 	return 0;
739ce43f57fSImre Vadász }
740