xref: /freebsd/sys/dev/ioat/ioat.c (revision e4e91353c62e2af425061a4e56e1f24dcca2dd0f)
1e974f91cSConrad Meyer /*-
2e974f91cSConrad Meyer  * Copyright (C) 2012 Intel Corporation
3e974f91cSConrad Meyer  * All rights reserved.
42f03a95fSAlexander Motin  * Copyright (C) 2018 Alexander Motin <mav@FreeBSD.org>
5e974f91cSConrad Meyer  *
6e974f91cSConrad Meyer  * Redistribution and use in source and binary forms, with or without
7e974f91cSConrad Meyer  * modification, are permitted provided that the following conditions
8e974f91cSConrad Meyer  * are met:
9e974f91cSConrad Meyer  * 1. Redistributions of source code must retain the above copyright
10e974f91cSConrad Meyer  *    notice, this list of conditions and the following disclaimer.
11e974f91cSConrad Meyer  * 2. Redistributions in binary form must reproduce the above copyright
12e974f91cSConrad Meyer  *    notice, this list of conditions and the following disclaimer in the
13e974f91cSConrad Meyer  *    documentation and/or other materials provided with the distribution.
14e974f91cSConrad Meyer  *
15e974f91cSConrad Meyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16e974f91cSConrad Meyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17e974f91cSConrad Meyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18e974f91cSConrad Meyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19e974f91cSConrad Meyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20e974f91cSConrad Meyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21e974f91cSConrad Meyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22e974f91cSConrad Meyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23e974f91cSConrad Meyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24e974f91cSConrad Meyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25e974f91cSConrad Meyer  * SUCH DAMAGE.
26e974f91cSConrad Meyer  */
27e974f91cSConrad Meyer 
28e974f91cSConrad Meyer #include <sys/cdefs.h>
29e974f91cSConrad Meyer __FBSDID("$FreeBSD$");
30e974f91cSConrad Meyer 
310bb35debSConrad Meyer #include "opt_ddb.h"
320bb35debSConrad Meyer 
33e974f91cSConrad Meyer #include <sys/param.h>
34e974f91cSConrad Meyer #include <sys/systm.h>
35e974f91cSConrad Meyer #include <sys/bus.h>
36e974f91cSConrad Meyer #include <sys/conf.h>
37657dc81dSAlexander Motin #include <sys/domainset.h>
3876305bb8SConrad Meyer #include <sys/fail.h>
39e974f91cSConrad Meyer #include <sys/ioccom.h>
40e974f91cSConrad Meyer #include <sys/kernel.h>
41e2e050c8SConrad Meyer #include <sys/ktr.h>
42e974f91cSConrad Meyer #include <sys/lock.h>
43e974f91cSConrad Meyer #include <sys/malloc.h>
44e974f91cSConrad Meyer #include <sys/module.h>
45e974f91cSConrad Meyer #include <sys/mutex.h>
46e974f91cSConrad Meyer #include <sys/rman.h>
47faefad9cSConrad Meyer #include <sys/sbuf.h>
48657dc81dSAlexander Motin #include <sys/smp.h>
49e974f91cSConrad Meyer #include <sys/sysctl.h>
50374b05e1SConrad Meyer #include <sys/taskqueue.h>
51e974f91cSConrad Meyer #include <sys/time.h>
52e974f91cSConrad Meyer #include <dev/pci/pcireg.h>
53e974f91cSConrad Meyer #include <dev/pci/pcivar.h>
54e974f91cSConrad Meyer #include <machine/bus.h>
55e974f91cSConrad Meyer #include <machine/resource.h>
56e974f91cSConrad Meyer #include <machine/stdarg.h>
57e974f91cSConrad Meyer 
580bb35debSConrad Meyer #ifdef DDB
590bb35debSConrad Meyer #include <ddb/ddb.h>
600bb35debSConrad Meyer #endif
610bb35debSConrad Meyer 
62e974f91cSConrad Meyer #include "ioat.h"
63e974f91cSConrad Meyer #include "ioat_hw.h"
64e974f91cSConrad Meyer #include "ioat_internal.h"
65e974f91cSConrad Meyer 
66519e8baaSConrad Meyer #ifndef	BUS_SPACE_MAXADDR_40BIT
671f4a469dSAlexander Motin #define	BUS_SPACE_MAXADDR_40BIT	MIN(BUS_SPACE_MAXADDR, 0xFFFFFFFFFFULL)
681f4a469dSAlexander Motin #endif
691f4a469dSAlexander Motin #ifndef	BUS_SPACE_MAXADDR_46BIT
701f4a469dSAlexander Motin #define	BUS_SPACE_MAXADDR_46BIT	MIN(BUS_SPACE_MAXADDR, 0x3FFFFFFFFFFFULL)
71519e8baaSConrad Meyer #endif
72fe720f5aSConrad Meyer 
7371bf3900SAlexander Motin static int ioat_modevent(module_t mod, int type, void *data);
74e974f91cSConrad Meyer static int ioat_probe(device_t device);
75e974f91cSConrad Meyer static int ioat_attach(device_t device);
76e974f91cSConrad Meyer static int ioat_detach(device_t device);
774253ea50SConrad Meyer static int ioat_setup_intr(struct ioat_softc *ioat);
784253ea50SConrad Meyer static int ioat_teardown_intr(struct ioat_softc *ioat);
79e974f91cSConrad Meyer static int ioat3_attach(device_t device);
80cea5b880SConrad Meyer static int ioat_start_channel(struct ioat_softc *ioat);
81e974f91cSConrad Meyer static int ioat_map_pci_bar(struct ioat_softc *ioat);
82e974f91cSConrad Meyer static void ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg,
83e974f91cSConrad Meyer     int error);
84e974f91cSConrad Meyer static void ioat_interrupt_handler(void *arg);
850d1a05d9SConrad Meyer static boolean_t ioat_model_resets_msix(struct ioat_softc *ioat);
86faefad9cSConrad Meyer static int chanerr_to_errno(uint32_t);
872f03a95fSAlexander Motin static void ioat_process_events(struct ioat_softc *ioat, boolean_t intr);
88e974f91cSConrad Meyer static inline uint32_t ioat_get_active(struct ioat_softc *ioat);
89e974f91cSConrad Meyer static inline uint32_t ioat_get_ring_space(struct ioat_softc *ioat);
90bf8553eaSConrad Meyer static void ioat_free_ring(struct ioat_softc *, uint32_t size,
918e269d99SConrad Meyer     struct ioat_descriptor *);
92bf8553eaSConrad Meyer static int ioat_reserve_space(struct ioat_softc *, uint32_t, int mflags);
938e269d99SConrad Meyer static union ioat_hw_descriptor *ioat_get_descriptor(struct ioat_softc *,
948e269d99SConrad Meyer     uint32_t index);
958e269d99SConrad Meyer static struct ioat_descriptor *ioat_get_ring_entry(struct ioat_softc *,
96e974f91cSConrad Meyer     uint32_t index);
97faefad9cSConrad Meyer static void ioat_halted_debug(struct ioat_softc *, uint32_t);
985ac77963SConrad Meyer static void ioat_poll_timer_callback(void *arg);
99e974f91cSConrad Meyer static void dump_descriptor(void *hw_desc);
100e974f91cSConrad Meyer static void ioat_submit_single(struct ioat_softc *ioat);
101e974f91cSConrad Meyer static void ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg,
102e974f91cSConrad Meyer     int error);
103e974f91cSConrad Meyer static int ioat_reset_hw(struct ioat_softc *ioat);
104374b05e1SConrad Meyer static void ioat_reset_hw_task(void *, int);
105e974f91cSConrad Meyer static void ioat_setup_sysctl(device_t device);
106f7157235SConrad Meyer static int sysctl_handle_reset(SYSCTL_HANDLER_ARGS);
1072f03a95fSAlexander Motin static void ioat_get(struct ioat_softc *);
1082f03a95fSAlexander Motin static void ioat_put(struct ioat_softc *);
1095f77bd3eSConrad Meyer static void ioat_drain_locked(struct ioat_softc *);
110e974f91cSConrad Meyer 
1111c25420eSConrad Meyer #define	ioat_log_message(v, ...) do {					\
1121c25420eSConrad Meyer 	if ((v) <= g_ioat_debug_level) {				\
1131c25420eSConrad Meyer 		device_printf(ioat->device, __VA_ARGS__);		\
1141c25420eSConrad Meyer 	}								\
1151c25420eSConrad Meyer } while (0)
1161c25420eSConrad Meyer 
117e974f91cSConrad Meyer MALLOC_DEFINE(M_IOAT, "ioat", "ioat driver memory allocations");
1187029da5cSPawel Biernacki SYSCTL_NODE(_hw, OID_AUTO, ioat, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
1197029da5cSPawel Biernacki     "ioat node");
120e974f91cSConrad Meyer 
121e974f91cSConrad Meyer static int g_force_legacy_interrupts;
122e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, force_legacy_interrupts, CTLFLAG_RDTUN,
123e974f91cSConrad Meyer     &g_force_legacy_interrupts, 0, "Set to non-zero to force MSI-X disabled");
124e974f91cSConrad Meyer 
1251c25420eSConrad Meyer int g_ioat_debug_level = 0;
126e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, debug_level, CTLFLAG_RWTUN, &g_ioat_debug_level,
127e974f91cSConrad Meyer     0, "Set log level (0-3) for ioat(4). Higher is more verbose.");
128e974f91cSConrad Meyer 
129a0992979SConrad Meyer unsigned g_ioat_ring_order = 13;
130a0992979SConrad Meyer SYSCTL_UINT(_hw_ioat, OID_AUTO, ring_order, CTLFLAG_RDTUN, &g_ioat_ring_order,
131a0992979SConrad Meyer     0, "Set IOAT ring order.  (1 << this) == ring size.");
132a0992979SConrad Meyer 
133e974f91cSConrad Meyer /*
134e974f91cSConrad Meyer  * OS <-> Driver interface structures
135e974f91cSConrad Meyer  */
136e974f91cSConrad Meyer static device_method_t ioat_pci_methods[] = {
137e974f91cSConrad Meyer 	/* Device interface */
138e974f91cSConrad Meyer 	DEVMETHOD(device_probe,     ioat_probe),
139e974f91cSConrad Meyer 	DEVMETHOD(device_attach,    ioat_attach),
140e974f91cSConrad Meyer 	DEVMETHOD(device_detach,    ioat_detach),
141aa853cb4SEnji Cooper 	DEVMETHOD_END
142e974f91cSConrad Meyer };
143e974f91cSConrad Meyer 
144e974f91cSConrad Meyer static driver_t ioat_pci_driver = {
145e974f91cSConrad Meyer 	"ioat",
146e974f91cSConrad Meyer 	ioat_pci_methods,
147e974f91cSConrad Meyer 	sizeof(struct ioat_softc),
148e974f91cSConrad Meyer };
149e974f91cSConrad Meyer 
150ff318bf6SJohn Baldwin DRIVER_MODULE(ioat, pci, ioat_pci_driver, ioat_modevent, NULL);
151c2b69205SConrad Meyer MODULE_VERSION(ioat, 1);
152e974f91cSConrad Meyer 
153e974f91cSConrad Meyer /*
154e974f91cSConrad Meyer  * Private data structures
155e974f91cSConrad Meyer  */
156e974f91cSConrad Meyer static struct ioat_softc *ioat_channel[IOAT_MAX_CHANNELS];
157df1928aaSConrad Meyer static unsigned ioat_channel_index = 0;
158df1928aaSConrad Meyer SYSCTL_UINT(_hw_ioat, OID_AUTO, channels, CTLFLAG_RD, &ioat_channel_index, 0,
159e974f91cSConrad Meyer     "Number of IOAT channels attached");
1602f03a95fSAlexander Motin static struct mtx ioat_list_mtx;
1612f03a95fSAlexander Motin MTX_SYSINIT(ioat_list_mtx, &ioat_list_mtx, "ioat list mtx", MTX_DEF);
162e974f91cSConrad Meyer 
163e974f91cSConrad Meyer static struct _pcsid
164e974f91cSConrad Meyer {
165e974f91cSConrad Meyer 	u_int32_t   type;
166e974f91cSConrad Meyer 	const char  *desc;
167e974f91cSConrad Meyer } pci_ids[] = {
168e974f91cSConrad Meyer 	{ 0x34308086, "TBG IOAT Ch0" },
169e974f91cSConrad Meyer 	{ 0x34318086, "TBG IOAT Ch1" },
170e974f91cSConrad Meyer 	{ 0x34328086, "TBG IOAT Ch2" },
171e974f91cSConrad Meyer 	{ 0x34338086, "TBG IOAT Ch3" },
172e974f91cSConrad Meyer 	{ 0x34298086, "TBG IOAT Ch4" },
173e974f91cSConrad Meyer 	{ 0x342a8086, "TBG IOAT Ch5" },
174e974f91cSConrad Meyer 	{ 0x342b8086, "TBG IOAT Ch6" },
175e974f91cSConrad Meyer 	{ 0x342c8086, "TBG IOAT Ch7" },
176e974f91cSConrad Meyer 
177e974f91cSConrad Meyer 	{ 0x37108086, "JSF IOAT Ch0" },
178e974f91cSConrad Meyer 	{ 0x37118086, "JSF IOAT Ch1" },
179e974f91cSConrad Meyer 	{ 0x37128086, "JSF IOAT Ch2" },
180e974f91cSConrad Meyer 	{ 0x37138086, "JSF IOAT Ch3" },
181e974f91cSConrad Meyer 	{ 0x37148086, "JSF IOAT Ch4" },
182e974f91cSConrad Meyer 	{ 0x37158086, "JSF IOAT Ch5" },
183e974f91cSConrad Meyer 	{ 0x37168086, "JSF IOAT Ch6" },
184e974f91cSConrad Meyer 	{ 0x37178086, "JSF IOAT Ch7" },
185e974f91cSConrad Meyer 	{ 0x37188086, "JSF IOAT Ch0 (RAID)" },
186e974f91cSConrad Meyer 	{ 0x37198086, "JSF IOAT Ch1 (RAID)" },
187e974f91cSConrad Meyer 
188e974f91cSConrad Meyer 	{ 0x3c208086, "SNB IOAT Ch0" },
189e974f91cSConrad Meyer 	{ 0x3c218086, "SNB IOAT Ch1" },
190e974f91cSConrad Meyer 	{ 0x3c228086, "SNB IOAT Ch2" },
191e974f91cSConrad Meyer 	{ 0x3c238086, "SNB IOAT Ch3" },
192e974f91cSConrad Meyer 	{ 0x3c248086, "SNB IOAT Ch4" },
193e974f91cSConrad Meyer 	{ 0x3c258086, "SNB IOAT Ch5" },
194e974f91cSConrad Meyer 	{ 0x3c268086, "SNB IOAT Ch6" },
195e974f91cSConrad Meyer 	{ 0x3c278086, "SNB IOAT Ch7" },
196e974f91cSConrad Meyer 	{ 0x3c2e8086, "SNB IOAT Ch0 (RAID)" },
197e974f91cSConrad Meyer 	{ 0x3c2f8086, "SNB IOAT Ch1 (RAID)" },
198e974f91cSConrad Meyer 
199e974f91cSConrad Meyer 	{ 0x0e208086, "IVB IOAT Ch0" },
200e974f91cSConrad Meyer 	{ 0x0e218086, "IVB IOAT Ch1" },
201e974f91cSConrad Meyer 	{ 0x0e228086, "IVB IOAT Ch2" },
202e974f91cSConrad Meyer 	{ 0x0e238086, "IVB IOAT Ch3" },
203e974f91cSConrad Meyer 	{ 0x0e248086, "IVB IOAT Ch4" },
204e974f91cSConrad Meyer 	{ 0x0e258086, "IVB IOAT Ch5" },
205e974f91cSConrad Meyer 	{ 0x0e268086, "IVB IOAT Ch6" },
206e974f91cSConrad Meyer 	{ 0x0e278086, "IVB IOAT Ch7" },
207e974f91cSConrad Meyer 	{ 0x0e2e8086, "IVB IOAT Ch0 (RAID)" },
208e974f91cSConrad Meyer 	{ 0x0e2f8086, "IVB IOAT Ch1 (RAID)" },
209e974f91cSConrad Meyer 
210e974f91cSConrad Meyer 	{ 0x2f208086, "HSW IOAT Ch0" },
211e974f91cSConrad Meyer 	{ 0x2f218086, "HSW IOAT Ch1" },
212e974f91cSConrad Meyer 	{ 0x2f228086, "HSW IOAT Ch2" },
213e974f91cSConrad Meyer 	{ 0x2f238086, "HSW IOAT Ch3" },
214e974f91cSConrad Meyer 	{ 0x2f248086, "HSW IOAT Ch4" },
215e974f91cSConrad Meyer 	{ 0x2f258086, "HSW IOAT Ch5" },
216e974f91cSConrad Meyer 	{ 0x2f268086, "HSW IOAT Ch6" },
217e974f91cSConrad Meyer 	{ 0x2f278086, "HSW IOAT Ch7" },
218e974f91cSConrad Meyer 	{ 0x2f2e8086, "HSW IOAT Ch0 (RAID)" },
219e974f91cSConrad Meyer 	{ 0x2f2f8086, "HSW IOAT Ch1 (RAID)" },
220e974f91cSConrad Meyer 
221e974f91cSConrad Meyer 	{ 0x0c508086, "BWD IOAT Ch0" },
222e974f91cSConrad Meyer 	{ 0x0c518086, "BWD IOAT Ch1" },
223e974f91cSConrad Meyer 	{ 0x0c528086, "BWD IOAT Ch2" },
224e974f91cSConrad Meyer 	{ 0x0c538086, "BWD IOAT Ch3" },
225e974f91cSConrad Meyer 
226e974f91cSConrad Meyer 	{ 0x6f508086, "BDXDE IOAT Ch0" },
227e974f91cSConrad Meyer 	{ 0x6f518086, "BDXDE IOAT Ch1" },
228e974f91cSConrad Meyer 	{ 0x6f528086, "BDXDE IOAT Ch2" },
229e974f91cSConrad Meyer 	{ 0x6f538086, "BDXDE IOAT Ch3" },
230e974f91cSConrad Meyer 
2315afc2508SConrad Meyer 	{ 0x6f208086, "BDX IOAT Ch0" },
2325afc2508SConrad Meyer 	{ 0x6f218086, "BDX IOAT Ch1" },
2335afc2508SConrad Meyer 	{ 0x6f228086, "BDX IOAT Ch2" },
2345afc2508SConrad Meyer 	{ 0x6f238086, "BDX IOAT Ch3" },
2355afc2508SConrad Meyer 	{ 0x6f248086, "BDX IOAT Ch4" },
2365afc2508SConrad Meyer 	{ 0x6f258086, "BDX IOAT Ch5" },
2375afc2508SConrad Meyer 	{ 0x6f268086, "BDX IOAT Ch6" },
2385afc2508SConrad Meyer 	{ 0x6f278086, "BDX IOAT Ch7" },
2395afc2508SConrad Meyer 	{ 0x6f2e8086, "BDX IOAT Ch0 (RAID)" },
2405afc2508SConrad Meyer 	{ 0x6f2f8086, "BDX IOAT Ch1 (RAID)" },
24108f16d0cSConrad Meyer 
24208f16d0cSConrad Meyer 	{ 0x20218086, "SKX IOAT" },
243*e4e91353SAlexander Motin 
244*e4e91353SAlexander Motin 	{ 0x0b008086, "ICX IOAT" },
245e974f91cSConrad Meyer };
246e974f91cSConrad Meyer 
247d2064cf0SWarner Losh MODULE_PNP_INFO("W32:vendor/device;D:#", pci, ioat, pci_ids,
248329e817fSWarner Losh     nitems(pci_ids));
249a64bf59cSConrad Meyer 
250e974f91cSConrad Meyer /*
251e974f91cSConrad Meyer  * OS <-> Driver linkage functions
252e974f91cSConrad Meyer  */
253e974f91cSConrad Meyer static int
25471bf3900SAlexander Motin ioat_modevent(module_t mod __unused, int type, void *data __unused)
25571bf3900SAlexander Motin {
25671bf3900SAlexander Motin 	switch(type) {
25771bf3900SAlexander Motin 	case MOD_LOAD:
25871bf3900SAlexander Motin 		break;
25971bf3900SAlexander Motin 
26071bf3900SAlexander Motin 	case MOD_UNLOAD:
26171bf3900SAlexander Motin 		ioat_test_detach();
26271bf3900SAlexander Motin 		break;
26371bf3900SAlexander Motin 
26471bf3900SAlexander Motin 	case MOD_SHUTDOWN:
26571bf3900SAlexander Motin 		break;
26671bf3900SAlexander Motin 
26771bf3900SAlexander Motin 	default:
26871bf3900SAlexander Motin 		return (EOPNOTSUPP);
26971bf3900SAlexander Motin 	}
27071bf3900SAlexander Motin 
27171bf3900SAlexander Motin 	return (0);
27271bf3900SAlexander Motin }
27371bf3900SAlexander Motin 
27471bf3900SAlexander Motin static int
275e974f91cSConrad Meyer ioat_probe(device_t device)
276e974f91cSConrad Meyer {
277e974f91cSConrad Meyer 	struct _pcsid *ep;
278e974f91cSConrad Meyer 	u_int32_t type;
279e974f91cSConrad Meyer 
280e974f91cSConrad Meyer 	type = pci_get_devid(device);
281a64bf59cSConrad Meyer 	for (ep = pci_ids; ep < &pci_ids[nitems(pci_ids)]; ep++) {
282e974f91cSConrad Meyer 		if (ep->type == type) {
283e974f91cSConrad Meyer 			device_set_desc(device, ep->desc);
284e974f91cSConrad Meyer 			return (0);
285e974f91cSConrad Meyer 		}
286e974f91cSConrad Meyer 	}
287e974f91cSConrad Meyer 	return (ENXIO);
288e974f91cSConrad Meyer }
289e974f91cSConrad Meyer 
290e974f91cSConrad Meyer static int
291e974f91cSConrad Meyer ioat_attach(device_t device)
292e974f91cSConrad Meyer {
293e974f91cSConrad Meyer 	struct ioat_softc *ioat;
2942f03a95fSAlexander Motin 	int error, i;
295e974f91cSConrad Meyer 
296e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
297e974f91cSConrad Meyer 	ioat->device = device;
298657dc81dSAlexander Motin 	if (bus_get_domain(device, &ioat->domain) != 0)
299657dc81dSAlexander Motin 		ioat->domain = 0;
300657dc81dSAlexander Motin 	ioat->cpu = CPU_FFS(&cpuset_domain[ioat->domain]) - 1;
301657dc81dSAlexander Motin 	if (ioat->cpu < 0)
302657dc81dSAlexander Motin 		ioat->cpu = CPU_FIRST();
303e974f91cSConrad Meyer 
304e974f91cSConrad Meyer 	error = ioat_map_pci_bar(ioat);
305e974f91cSConrad Meyer 	if (error != 0)
306e974f91cSConrad Meyer 		goto err;
307e974f91cSConrad Meyer 
308e974f91cSConrad Meyer 	ioat->version = ioat_read_cbver(ioat);
309e974f91cSConrad Meyer 	if (ioat->version < IOAT_VER_3_0) {
310e974f91cSConrad Meyer 		error = ENODEV;
311e974f91cSConrad Meyer 		goto err;
312e974f91cSConrad Meyer 	}
313e974f91cSConrad Meyer 
314e974f91cSConrad Meyer 	error = ioat3_attach(device);
315e974f91cSConrad Meyer 	if (error != 0)
316e974f91cSConrad Meyer 		goto err;
317e974f91cSConrad Meyer 
318e974f91cSConrad Meyer 	error = pci_enable_busmaster(device);
319e974f91cSConrad Meyer 	if (error != 0)
320e974f91cSConrad Meyer 		goto err;
321e974f91cSConrad Meyer 
322466b3540SConrad Meyer 	error = ioat_setup_intr(ioat);
323466b3540SConrad Meyer 	if (error != 0)
324466b3540SConrad Meyer 		goto err;
325466b3540SConrad Meyer 
326cea5b880SConrad Meyer 	error = ioat_reset_hw(ioat);
3277afbb263SConrad Meyer 	if (error != 0)
328466b3540SConrad Meyer 		goto err;
3297afbb263SConrad Meyer 
3302f03a95fSAlexander Motin 	ioat_process_events(ioat, FALSE);
3317afbb263SConrad Meyer 	ioat_setup_sysctl(device);
3327afbb263SConrad Meyer 
3332f03a95fSAlexander Motin 	mtx_lock(&ioat_list_mtx);
3342f03a95fSAlexander Motin 	for (i = 0; i < IOAT_MAX_CHANNELS; i++) {
3352f03a95fSAlexander Motin 		if (ioat_channel[i] == NULL)
3362f03a95fSAlexander Motin 			break;
3372f03a95fSAlexander Motin 	}
3382f03a95fSAlexander Motin 	if (i >= IOAT_MAX_CHANNELS) {
3392f03a95fSAlexander Motin 		mtx_unlock(&ioat_list_mtx);
3402f03a95fSAlexander Motin 		device_printf(device, "Too many I/OAT devices in system\n");
3412f03a95fSAlexander Motin 		error = ENXIO;
3422f03a95fSAlexander Motin 		goto err;
3432f03a95fSAlexander Motin 	}
3442f03a95fSAlexander Motin 	ioat->chan_idx = i;
3452f03a95fSAlexander Motin 	ioat_channel[i] = ioat;
3462f03a95fSAlexander Motin 	if (i >= ioat_channel_index)
3472f03a95fSAlexander Motin 		ioat_channel_index = i + 1;
3482f03a95fSAlexander Motin 	mtx_unlock(&ioat_list_mtx);
3492f03a95fSAlexander Motin 
3507afbb263SConrad Meyer 	ioat_test_attach();
351e974f91cSConrad Meyer 
352e974f91cSConrad Meyer err:
353e974f91cSConrad Meyer 	if (error != 0)
354e974f91cSConrad Meyer 		ioat_detach(device);
355e974f91cSConrad Meyer 	return (error);
356e974f91cSConrad Meyer }
357e974f91cSConrad Meyer 
358b80b32a2STycho Nightingale static inline int
359b80b32a2STycho Nightingale ioat_bus_dmamap_destroy(struct ioat_softc *ioat, const char *func,
360b80b32a2STycho Nightingale     bus_dma_tag_t dmat, bus_dmamap_t map)
361b80b32a2STycho Nightingale {
362b80b32a2STycho Nightingale 	int error;
363b80b32a2STycho Nightingale 
364b80b32a2STycho Nightingale 	error = bus_dmamap_destroy(dmat, map);
365b80b32a2STycho Nightingale 	if (error != 0) {
366b80b32a2STycho Nightingale 		ioat_log_message(0,
367b80b32a2STycho Nightingale 		    "%s: bus_dmamap_destroy failed %d\n", func, error);
368b80b32a2STycho Nightingale 	}
369b80b32a2STycho Nightingale 
370b80b32a2STycho Nightingale 	return (error);
371b80b32a2STycho Nightingale }
372b80b32a2STycho Nightingale 
373e974f91cSConrad Meyer static int
374e974f91cSConrad Meyer ioat_detach(device_t device)
375e974f91cSConrad Meyer {
376e974f91cSConrad Meyer 	struct ioat_softc *ioat;
377b80b32a2STycho Nightingale 	int i, error;
378e974f91cSConrad Meyer 
379e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
3807afbb263SConrad Meyer 
3812f03a95fSAlexander Motin 	mtx_lock(&ioat_list_mtx);
3822f03a95fSAlexander Motin 	ioat_channel[ioat->chan_idx] = NULL;
3832f03a95fSAlexander Motin 	while (ioat_channel_index > 0 &&
3842f03a95fSAlexander Motin 	    ioat_channel[ioat_channel_index - 1] == NULL)
3852f03a95fSAlexander Motin 		ioat_channel_index--;
3862f03a95fSAlexander Motin 	mtx_unlock(&ioat_list_mtx);
3872f03a95fSAlexander Motin 
388374b05e1SConrad Meyer 	taskqueue_drain(taskqueue_thread, &ioat->reset_task);
3895f77bd3eSConrad Meyer 
3902f03a95fSAlexander Motin 	mtx_lock(&ioat->submit_lock);
3915f77bd3eSConrad Meyer 	ioat->quiescing = TRUE;
3920ff814e8SConrad Meyer 	ioat->destroying = TRUE;
3930ff814e8SConrad Meyer 	wakeup(&ioat->quiescing);
39493f7f84aSConrad Meyer 	wakeup(&ioat->resetting);
3950ff814e8SConrad Meyer 
3965f77bd3eSConrad Meyer 	ioat_drain_locked(ioat);
3972f03a95fSAlexander Motin 	mtx_unlock(&ioat->submit_lock);
3982f03a95fSAlexander Motin 	mtx_lock(&ioat->cleanup_lock);
3992f03a95fSAlexander Motin 	while (ioat_get_active(ioat) > 0)
4002f03a95fSAlexander Motin 		msleep(&ioat->tail, &ioat->cleanup_lock, 0, "ioat_drain", 1);
4012f03a95fSAlexander Motin 	mtx_unlock(&ioat->cleanup_lock);
402fe720f5aSConrad Meyer 
403fe720f5aSConrad Meyer 	ioat_teardown_intr(ioat);
4045ac77963SConrad Meyer 	callout_drain(&ioat->poll_timer);
405e974f91cSConrad Meyer 
406e974f91cSConrad Meyer 	pci_disable_busmaster(device);
407e974f91cSConrad Meyer 
408e974f91cSConrad Meyer 	if (ioat->pci_resource != NULL)
409e974f91cSConrad Meyer 		bus_release_resource(device, SYS_RES_MEMORY,
410e974f91cSConrad Meyer 		    ioat->pci_resource_id, ioat->pci_resource);
411e974f91cSConrad Meyer 
412b80b32a2STycho Nightingale 	if (ioat->data_tag != NULL) {
413b80b32a2STycho Nightingale 		for (i = 0; i < 1 << ioat->ring_size_order; i++) {
414b80b32a2STycho Nightingale 			error = ioat_bus_dmamap_destroy(ioat, __func__,
415b80b32a2STycho Nightingale 			    ioat->data_tag, ioat->ring[i].src_dmamap);
416b80b32a2STycho Nightingale 			if (error != 0)
417b80b32a2STycho Nightingale 				return (error);
418b80b32a2STycho Nightingale 		}
419b80b32a2STycho Nightingale 		for (i = 0; i < 1 << ioat->ring_size_order; i++) {
420b80b32a2STycho Nightingale 			error = ioat_bus_dmamap_destroy(ioat, __func__,
421b80b32a2STycho Nightingale 			    ioat->data_tag, ioat->ring[i].dst_dmamap);
422b80b32a2STycho Nightingale 			if (error != 0)
423b80b32a2STycho Nightingale 				return (error);
424b80b32a2STycho Nightingale 		}
425b80b32a2STycho Nightingale 
426b80b32a2STycho Nightingale 		for (i = 0; i < 1 << ioat->ring_size_order; i++) {
427b80b32a2STycho Nightingale 			error = ioat_bus_dmamap_destroy(ioat, __func__,
428b80b32a2STycho Nightingale 			    ioat->data_tag, ioat->ring[i].src2_dmamap);
429b80b32a2STycho Nightingale 			if (error != 0)
430b80b32a2STycho Nightingale 				return (error);
431b80b32a2STycho Nightingale 		}
432b80b32a2STycho Nightingale 		for (i = 0; i < 1 << ioat->ring_size_order; i++) {
433b80b32a2STycho Nightingale 			error = ioat_bus_dmamap_destroy(ioat, __func__,
434b80b32a2STycho Nightingale 			    ioat->data_tag, ioat->ring[i].dst2_dmamap);
435b80b32a2STycho Nightingale 			if (error != 0)
436b80b32a2STycho Nightingale 				return (error);
437b80b32a2STycho Nightingale 		}
438b80b32a2STycho Nightingale 
439b80b32a2STycho Nightingale 		bus_dma_tag_destroy(ioat->data_tag);
440b80b32a2STycho Nightingale 	}
441b80b32a2STycho Nightingale 
442bf8553eaSConrad Meyer 	if (ioat->ring != NULL)
443bf8553eaSConrad Meyer 		ioat_free_ring(ioat, 1 << ioat->ring_size_order, ioat->ring);
444e974f91cSConrad Meyer 
445e974f91cSConrad Meyer 	if (ioat->comp_update != NULL) {
446e974f91cSConrad Meyer 		bus_dmamap_unload(ioat->comp_update_tag, ioat->comp_update_map);
447e974f91cSConrad Meyer 		bus_dmamem_free(ioat->comp_update_tag, ioat->comp_update,
448e974f91cSConrad Meyer 		    ioat->comp_update_map);
449e974f91cSConrad Meyer 		bus_dma_tag_destroy(ioat->comp_update_tag);
450e974f91cSConrad Meyer 	}
451e974f91cSConrad Meyer 
4528e269d99SConrad Meyer 	if (ioat->hw_desc_ring != NULL) {
4538e269d99SConrad Meyer 		bus_dmamap_unload(ioat->hw_desc_tag, ioat->hw_desc_map);
4548e269d99SConrad Meyer 		bus_dmamem_free(ioat->hw_desc_tag, ioat->hw_desc_ring,
4558e269d99SConrad Meyer 		    ioat->hw_desc_map);
456e974f91cSConrad Meyer 		bus_dma_tag_destroy(ioat->hw_desc_tag);
4578e269d99SConrad Meyer 	}
458e974f91cSConrad Meyer 
4594253ea50SConrad Meyer 	return (0);
4604253ea50SConrad Meyer }
4614253ea50SConrad Meyer 
4624253ea50SConrad Meyer static int
4634253ea50SConrad Meyer ioat_teardown_intr(struct ioat_softc *ioat)
4644253ea50SConrad Meyer {
4654253ea50SConrad Meyer 
466e974f91cSConrad Meyer 	if (ioat->tag != NULL)
4674253ea50SConrad Meyer 		bus_teardown_intr(ioat->device, ioat->res, ioat->tag);
468e974f91cSConrad Meyer 
469e974f91cSConrad Meyer 	if (ioat->res != NULL)
4704253ea50SConrad Meyer 		bus_release_resource(ioat->device, SYS_RES_IRQ,
471e974f91cSConrad Meyer 		    rman_get_rid(ioat->res), ioat->res);
472e974f91cSConrad Meyer 
4734253ea50SConrad Meyer 	pci_release_msi(ioat->device);
474e974f91cSConrad Meyer 	return (0);
475e974f91cSConrad Meyer }
476e974f91cSConrad Meyer 
477e974f91cSConrad Meyer static int
478cea5b880SConrad Meyer ioat_start_channel(struct ioat_softc *ioat)
479e974f91cSConrad Meyer {
480fe8712f8SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
481fe8712f8SConrad Meyer 	struct ioat_descriptor *desc;
482fe8712f8SConrad Meyer 	struct bus_dmadesc *dmadesc;
483e974f91cSConrad Meyer 	uint64_t status;
484e974f91cSConrad Meyer 	uint32_t chanerr;
485e974f91cSConrad Meyer 	int i;
486e974f91cSConrad Meyer 
487e974f91cSConrad Meyer 	ioat_acquire(&ioat->dmaengine);
488fe8712f8SConrad Meyer 
489fe8712f8SConrad Meyer 	/* Submit 'NULL' operation manually to avoid quiescing flag */
490fe8712f8SConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->head);
4918e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, ioat->head)->dma;
492fe8712f8SConrad Meyer 	dmadesc = &desc->bus_dmadesc;
493fe8712f8SConrad Meyer 
494fe8712f8SConrad Meyer 	dmadesc->callback_fn = NULL;
495fe8712f8SConrad Meyer 	dmadesc->callback_arg = NULL;
496fe8712f8SConrad Meyer 
497fe8712f8SConrad Meyer 	hw_desc->u.control_raw = 0;
498fe8712f8SConrad Meyer 	hw_desc->u.control_generic.op = IOAT_OP_COPY;
499fe8712f8SConrad Meyer 	hw_desc->u.control_generic.completion_update = 1;
500fe8712f8SConrad Meyer 	hw_desc->size = 8;
501fe8712f8SConrad Meyer 	hw_desc->src_addr = 0;
502fe8712f8SConrad Meyer 	hw_desc->dest_addr = 0;
503fe8712f8SConrad Meyer 	hw_desc->u.control.null = 1;
504fe8712f8SConrad Meyer 
505fe8712f8SConrad Meyer 	ioat_submit_single(ioat);
506e974f91cSConrad Meyer 	ioat_release(&ioat->dmaengine);
507e974f91cSConrad Meyer 
508e974f91cSConrad Meyer 	for (i = 0; i < 100; i++) {
509e974f91cSConrad Meyer 		DELAY(1);
510e974f91cSConrad Meyer 		status = ioat_get_chansts(ioat);
511e974f91cSConrad Meyer 		if (is_ioat_idle(status))
512e974f91cSConrad Meyer 			return (0);
513e974f91cSConrad Meyer 	}
514e974f91cSConrad Meyer 
515e974f91cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
516e974f91cSConrad Meyer 	ioat_log_message(0, "could not start channel: "
51759acd4baSConrad Meyer 	    "status = %#jx error = %b\n", (uintmax_t)status, (int)chanerr,
51859acd4baSConrad Meyer 	    IOAT_CHANERR_STR);
519e974f91cSConrad Meyer 	return (ENXIO);
520e974f91cSConrad Meyer }
521e974f91cSConrad Meyer 
522e974f91cSConrad Meyer /*
523e974f91cSConrad Meyer  * Initialize Hardware
524e974f91cSConrad Meyer  */
525e974f91cSConrad Meyer static int
526e974f91cSConrad Meyer ioat3_attach(device_t device)
527e974f91cSConrad Meyer {
528e974f91cSConrad Meyer 	struct ioat_softc *ioat;
5298e269d99SConrad Meyer 	struct ioat_descriptor *ring;
530e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *dma_hw_desc;
5318e269d99SConrad Meyer 	void *hw_desc;
5321f4a469dSAlexander Motin 	bus_addr_t lowaddr;
5338e269d99SConrad Meyer 	size_t ringsz;
534e974f91cSConrad Meyer 	int i, num_descriptors;
535e974f91cSConrad Meyer 	int error;
536e974f91cSConrad Meyer 	uint8_t xfercap;
537e974f91cSConrad Meyer 
538e974f91cSConrad Meyer 	error = 0;
539e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
5401693d27bSConrad Meyer 	ioat->capabilities = ioat_read_dmacapability(ioat);
5411693d27bSConrad Meyer 
54248fed014SConrad Meyer 	ioat_log_message(0, "Capabilities: %b\n", (int)ioat->capabilities,
5431693d27bSConrad Meyer 	    IOAT_DMACAP_STR);
544e974f91cSConrad Meyer 
545e974f91cSConrad Meyer 	xfercap = ioat_read_xfercap(ioat);
546e974f91cSConrad Meyer 	ioat->max_xfer_size = 1 << xfercap;
547e974f91cSConrad Meyer 
5485ca9fc2aSConrad Meyer 	ioat->intrdelay_supported = (ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) &
5495ca9fc2aSConrad Meyer 	    IOAT_INTRDELAY_SUPPORTED) != 0;
5505ca9fc2aSConrad Meyer 	if (ioat->intrdelay_supported)
5515ca9fc2aSConrad Meyer 		ioat->intrdelay_max = IOAT_INTRDELAY_US_MASK;
5525ca9fc2aSConrad Meyer 
553e974f91cSConrad Meyer 	/* TODO: need to check DCA here if we ever do XOR/PQ */
554e974f91cSConrad Meyer 
555e974f91cSConrad Meyer 	mtx_init(&ioat->submit_lock, "ioat_submit", NULL, MTX_DEF);
556faefad9cSConrad Meyer 	mtx_init(&ioat->cleanup_lock, "ioat_cleanup", NULL, MTX_DEF);
5575ac77963SConrad Meyer 	callout_init(&ioat->poll_timer, 1);
558374b05e1SConrad Meyer 	TASK_INIT(&ioat->reset_task, 0, ioat_reset_hw_task, ioat);
559e974f91cSConrad Meyer 
560faefad9cSConrad Meyer 	/* Establish lock order for Witness */
561faefad9cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
5622f03a95fSAlexander Motin 	mtx_lock(&ioat->submit_lock);
563faefad9cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
5642f03a95fSAlexander Motin 	mtx_unlock(&ioat->cleanup_lock);
565faefad9cSConrad Meyer 
56625ad9585SConrad Meyer 	ioat->is_submitter_processing = FALSE;
567e974f91cSConrad Meyer 
5681f4a469dSAlexander Motin 	if (ioat->version >= IOAT_VER_3_3)
5691f4a469dSAlexander Motin 		lowaddr = BUS_SPACE_MAXADDR_48BIT;
5701f4a469dSAlexander Motin 	else if (ioat->version >= IOAT_VER_3_2)
5711f4a469dSAlexander Motin 		lowaddr = BUS_SPACE_MAXADDR_46BIT;
5721f4a469dSAlexander Motin 	else
5731f4a469dSAlexander Motin 		lowaddr = BUS_SPACE_MAXADDR_40BIT;
5741f4a469dSAlexander Motin 
5751f4a469dSAlexander Motin 	error = bus_dma_tag_create(bus_get_dma_tag(ioat->device),
5761f4a469dSAlexander Motin 	    sizeof(uint64_t), 0x0, lowaddr, BUS_SPACE_MAXADDR, NULL, NULL,
577e974f91cSConrad Meyer 	    sizeof(uint64_t), 1, sizeof(uint64_t), 0, NULL, NULL,
578e974f91cSConrad Meyer 	    &ioat->comp_update_tag);
5791f4a469dSAlexander Motin 	if (error != 0)
5801f4a469dSAlexander Motin 		return (error);
581e974f91cSConrad Meyer 
582e974f91cSConrad Meyer 	error = bus_dmamem_alloc(ioat->comp_update_tag,
5833eb70a09SAlexander Motin 	    (void **)&ioat->comp_update, BUS_DMA_ZERO | BUS_DMA_WAITOK,
5843eb70a09SAlexander Motin 	    &ioat->comp_update_map);
5851f4a469dSAlexander Motin 	if (error != 0)
5861f4a469dSAlexander Motin 		return (error);
587e974f91cSConrad Meyer 
588e974f91cSConrad Meyer 	error = bus_dmamap_load(ioat->comp_update_tag, ioat->comp_update_map,
589e974f91cSConrad Meyer 	    ioat->comp_update, sizeof(uint64_t), ioat_comp_update_map, ioat,
5903eb70a09SAlexander Motin 	    BUS_DMA_NOWAIT);
591e974f91cSConrad Meyer 	if (error != 0)
592e974f91cSConrad Meyer 		return (error);
593e974f91cSConrad Meyer 
594a0992979SConrad Meyer 	ioat->ring_size_order = g_ioat_ring_order;
595e974f91cSConrad Meyer 	num_descriptors = 1 << ioat->ring_size_order;
5968e269d99SConrad Meyer 	ringsz = sizeof(struct ioat_dma_hw_descriptor) * num_descriptors;
597e974f91cSConrad Meyer 
5988e269d99SConrad Meyer 	error = bus_dma_tag_create(bus_get_dma_tag(ioat->device),
5991f4a469dSAlexander Motin 	    2 * 1024 * 1024, 0x0, lowaddr, BUS_SPACE_MAXADDR, NULL, NULL,
6001f4a469dSAlexander Motin 	    ringsz, 1, ringsz, 0, NULL, NULL, &ioat->hw_desc_tag);
6018e269d99SConrad Meyer 	if (error != 0)
6028e269d99SConrad Meyer 		return (error);
6038e269d99SConrad Meyer 
6048e269d99SConrad Meyer 	error = bus_dmamem_alloc(ioat->hw_desc_tag, &hw_desc,
6058e269d99SConrad Meyer 	    BUS_DMA_ZERO | BUS_DMA_WAITOK, &ioat->hw_desc_map);
6068e269d99SConrad Meyer 	if (error != 0)
6078e269d99SConrad Meyer 		return (error);
6088e269d99SConrad Meyer 
6098e269d99SConrad Meyer 	error = bus_dmamap_load(ioat->hw_desc_tag, ioat->hw_desc_map, hw_desc,
6103eb70a09SAlexander Motin 	    ringsz, ioat_dmamap_cb, &ioat->hw_desc_bus_addr, BUS_DMA_NOWAIT);
6118e269d99SConrad Meyer 	if (error)
6128e269d99SConrad Meyer 		return (error);
6138e269d99SConrad Meyer 
6148e269d99SConrad Meyer 	ioat->hw_desc_ring = hw_desc;
615e974f91cSConrad Meyer 
616b80b32a2STycho Nightingale 	error = bus_dma_tag_create(bus_get_dma_tag(ioat->device),
6171f4a469dSAlexander Motin 	    1, 0, lowaddr, BUS_SPACE_MAXADDR, NULL, NULL,
618b80b32a2STycho Nightingale 	    ioat->max_xfer_size, 1, ioat->max_xfer_size, 0, NULL, NULL,
619b80b32a2STycho Nightingale 	    &ioat->data_tag);
6201f4a469dSAlexander Motin 	if (error != 0)
621b80b32a2STycho Nightingale 		return (error);
622657dc81dSAlexander Motin 	ioat->ring = malloc_domainset(num_descriptors * sizeof(*ring), M_IOAT,
623657dc81dSAlexander Motin 	    DOMAINSET_PREF(ioat->domain), M_ZERO | M_WAITOK);
624e974f91cSConrad Meyer 
625e974f91cSConrad Meyer 	ring = ioat->ring;
626e974f91cSConrad Meyer 	for (i = 0; i < num_descriptors; i++) {
6278e269d99SConrad Meyer 		memset(&ring[i].bus_dmadesc, 0, sizeof(ring[i].bus_dmadesc));
6288e269d99SConrad Meyer 		ring[i].id = i;
629b80b32a2STycho Nightingale 		error = bus_dmamap_create(ioat->data_tag, 0,
630b80b32a2STycho Nightingale                     &ring[i].src_dmamap);
631b80b32a2STycho Nightingale 		if (error != 0) {
632b80b32a2STycho Nightingale 			ioat_log_message(0,
633b80b32a2STycho Nightingale 			    "%s: bus_dmamap_create failed %d\n", __func__,
634b80b32a2STycho Nightingale 			    error);
635b80b32a2STycho Nightingale 			return (error);
636b80b32a2STycho Nightingale 		}
637b80b32a2STycho Nightingale 		error = bus_dmamap_create(ioat->data_tag, 0,
638b80b32a2STycho Nightingale                     &ring[i].dst_dmamap);
639b80b32a2STycho Nightingale 		if (error != 0) {
640b80b32a2STycho Nightingale 			ioat_log_message(0,
641b80b32a2STycho Nightingale 			    "%s: bus_dmamap_create failed %d\n", __func__,
642b80b32a2STycho Nightingale 			    error);
643b80b32a2STycho Nightingale 			return (error);
644b80b32a2STycho Nightingale 		}
645b80b32a2STycho Nightingale 		error = bus_dmamap_create(ioat->data_tag, 0,
646b80b32a2STycho Nightingale                     &ring[i].src2_dmamap);
647b80b32a2STycho Nightingale 		if (error != 0) {
648b80b32a2STycho Nightingale 			ioat_log_message(0,
649b80b32a2STycho Nightingale 			    "%s: bus_dmamap_create failed %d\n", __func__,
650b80b32a2STycho Nightingale 			    error);
651b80b32a2STycho Nightingale 			return (error);
652b80b32a2STycho Nightingale 		}
653b80b32a2STycho Nightingale 		error = bus_dmamap_create(ioat->data_tag, 0,
654b80b32a2STycho Nightingale                     &ring[i].dst2_dmamap);
655b80b32a2STycho Nightingale 		if (error != 0) {
656b80b32a2STycho Nightingale 			ioat_log_message(0,
657b80b32a2STycho Nightingale 			    "%s: bus_dmamap_create failed %d\n", __func__,
658b80b32a2STycho Nightingale 			    error);
659b80b32a2STycho Nightingale 			return (error);
660b80b32a2STycho Nightingale 		}
661e974f91cSConrad Meyer 	}
662e974f91cSConrad Meyer 
6638e269d99SConrad Meyer 	for (i = 0; i < num_descriptors; i++) {
6648e269d99SConrad Meyer 		dma_hw_desc = &ioat->hw_desc_ring[i].dma;
6658e269d99SConrad Meyer 		dma_hw_desc->next = RING_PHYS_ADDR(ioat, i + 1);
666e974f91cSConrad Meyer 	}
667e974f91cSConrad Meyer 
668348efb14SAlexander Motin 	ioat->tail = ioat->head = 0;
669348efb14SAlexander Motin 	*ioat->comp_update = ioat->last_seen =
670348efb14SAlexander Motin 	    RING_PHYS_ADDR(ioat, ioat->tail - 1);
671e974f91cSConrad Meyer 	return (0);
672e974f91cSConrad Meyer }
673e974f91cSConrad Meyer 
674e974f91cSConrad Meyer static int
675e974f91cSConrad Meyer ioat_map_pci_bar(struct ioat_softc *ioat)
676e974f91cSConrad Meyer {
677e974f91cSConrad Meyer 
678e974f91cSConrad Meyer 	ioat->pci_resource_id = PCIR_BAR(0);
679e88e14b9SConrad Meyer 	ioat->pci_resource = bus_alloc_resource_any(ioat->device,
680e88e14b9SConrad Meyer 	    SYS_RES_MEMORY, &ioat->pci_resource_id, RF_ACTIVE);
681e974f91cSConrad Meyer 
682e974f91cSConrad Meyer 	if (ioat->pci_resource == NULL) {
683e974f91cSConrad Meyer 		ioat_log_message(0, "unable to allocate pci resource\n");
684e974f91cSConrad Meyer 		return (ENODEV);
685e974f91cSConrad Meyer 	}
686e974f91cSConrad Meyer 
687e974f91cSConrad Meyer 	ioat->pci_bus_tag = rman_get_bustag(ioat->pci_resource);
688e974f91cSConrad Meyer 	ioat->pci_bus_handle = rman_get_bushandle(ioat->pci_resource);
689e974f91cSConrad Meyer 	return (0);
690e974f91cSConrad Meyer }
691e974f91cSConrad Meyer 
692e974f91cSConrad Meyer static void
693e974f91cSConrad Meyer ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg, int error)
694e974f91cSConrad Meyer {
695e974f91cSConrad Meyer 	struct ioat_softc *ioat = arg;
696e974f91cSConrad Meyer 
697cea5b880SConrad Meyer 	KASSERT(error == 0, ("%s: error:%d", __func__, error));
698e974f91cSConrad Meyer 	ioat->comp_update_bus_addr = seg[0].ds_addr;
699e974f91cSConrad Meyer }
700e974f91cSConrad Meyer 
701e974f91cSConrad Meyer static void
702e974f91cSConrad Meyer ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
703e974f91cSConrad Meyer {
704e974f91cSConrad Meyer 	bus_addr_t *baddr;
705e974f91cSConrad Meyer 
706cea5b880SConrad Meyer 	KASSERT(error == 0, ("%s: error:%d", __func__, error));
707e974f91cSConrad Meyer 	baddr = arg;
708e974f91cSConrad Meyer 	*baddr = segs->ds_addr;
709e974f91cSConrad Meyer }
710e974f91cSConrad Meyer 
711e974f91cSConrad Meyer /*
712e974f91cSConrad Meyer  * Interrupt setup and handlers
713e974f91cSConrad Meyer  */
714e974f91cSConrad Meyer static int
7154253ea50SConrad Meyer ioat_setup_intr(struct ioat_softc *ioat)
716e974f91cSConrad Meyer {
717e974f91cSConrad Meyer 	uint32_t num_vectors;
718e974f91cSConrad Meyer 	int error;
719e974f91cSConrad Meyer 	boolean_t use_msix;
720e974f91cSConrad Meyer 
721e974f91cSConrad Meyer 	use_msix = FALSE;
722e974f91cSConrad Meyer 
723e974f91cSConrad Meyer 	if (!g_force_legacy_interrupts && pci_msix_count(ioat->device) >= 1) {
724e974f91cSConrad Meyer 		num_vectors = 1;
725e974f91cSConrad Meyer 		pci_alloc_msix(ioat->device, &num_vectors);
726e974f91cSConrad Meyer 		if (num_vectors == 1)
727e974f91cSConrad Meyer 			use_msix = TRUE;
728e974f91cSConrad Meyer 	}
729e974f91cSConrad Meyer 
730e974f91cSConrad Meyer 	if (use_msix) {
731e974f91cSConrad Meyer 		ioat->rid = 1;
732e974f91cSConrad Meyer 		ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
733e974f91cSConrad Meyer 		    &ioat->rid, RF_ACTIVE);
734e974f91cSConrad Meyer 	} else {
735e974f91cSConrad Meyer 		ioat->rid = 0;
736e974f91cSConrad Meyer 		ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
737e974f91cSConrad Meyer 		    &ioat->rid, RF_SHAREABLE | RF_ACTIVE);
738e974f91cSConrad Meyer 	}
739e974f91cSConrad Meyer 	if (ioat->res == NULL) {
740e974f91cSConrad Meyer 		ioat_log_message(0, "bus_alloc_resource failed\n");
741e974f91cSConrad Meyer 		return (ENOMEM);
742e974f91cSConrad Meyer 	}
743e974f91cSConrad Meyer 
744e974f91cSConrad Meyer 	ioat->tag = NULL;
745e974f91cSConrad Meyer 	error = bus_setup_intr(ioat->device, ioat->res, INTR_MPSAFE |
746e974f91cSConrad Meyer 	    INTR_TYPE_MISC, NULL, ioat_interrupt_handler, ioat, &ioat->tag);
747e974f91cSConrad Meyer 	if (error != 0) {
748e974f91cSConrad Meyer 		ioat_log_message(0, "bus_setup_intr failed\n");
749e974f91cSConrad Meyer 		return (error);
750e974f91cSConrad Meyer 	}
751e974f91cSConrad Meyer 
752e974f91cSConrad Meyer 	ioat_write_intrctrl(ioat, IOAT_INTRCTRL_MASTER_INT_EN);
753e974f91cSConrad Meyer 	return (0);
754e974f91cSConrad Meyer }
755e974f91cSConrad Meyer 
7564253ea50SConrad Meyer static boolean_t
7570d1a05d9SConrad Meyer ioat_model_resets_msix(struct ioat_softc *ioat)
7584253ea50SConrad Meyer {
7594253ea50SConrad Meyer 	u_int32_t pciid;
7604253ea50SConrad Meyer 
7614253ea50SConrad Meyer 	pciid = pci_get_devid(ioat->device);
7624253ea50SConrad Meyer 	switch (pciid) {
7630d1a05d9SConrad Meyer 		/* BWD: */
7640d1a05d9SConrad Meyer 	case 0x0c508086:
7650d1a05d9SConrad Meyer 	case 0x0c518086:
7660d1a05d9SConrad Meyer 	case 0x0c528086:
7670d1a05d9SConrad Meyer 	case 0x0c538086:
7680d1a05d9SConrad Meyer 		/* BDXDE: */
7694253ea50SConrad Meyer 	case 0x6f508086:
7704253ea50SConrad Meyer 	case 0x6f518086:
7714253ea50SConrad Meyer 	case 0x6f528086:
7724253ea50SConrad Meyer 	case 0x6f538086:
7734253ea50SConrad Meyer 		return (TRUE);
7744253ea50SConrad Meyer 	}
7754253ea50SConrad Meyer 
7764253ea50SConrad Meyer 	return (FALSE);
7774253ea50SConrad Meyer }
7784253ea50SConrad Meyer 
779e974f91cSConrad Meyer static void
780e974f91cSConrad Meyer ioat_interrupt_handler(void *arg)
781e974f91cSConrad Meyer {
782e974f91cSConrad Meyer 	struct ioat_softc *ioat = arg;
783e974f91cSConrad Meyer 
78401fbbc88SConrad Meyer 	ioat->stats.interrupts++;
7852f03a95fSAlexander Motin 	ioat_process_events(ioat, TRUE);
786e974f91cSConrad Meyer }
787e974f91cSConrad Meyer 
788faefad9cSConrad Meyer static int
789faefad9cSConrad Meyer chanerr_to_errno(uint32_t chanerr)
790faefad9cSConrad Meyer {
791faefad9cSConrad Meyer 
792faefad9cSConrad Meyer 	if (chanerr == 0)
793faefad9cSConrad Meyer 		return (0);
794faefad9cSConrad Meyer 	if ((chanerr & (IOAT_CHANERR_XSADDERR | IOAT_CHANERR_XDADDERR)) != 0)
795faefad9cSConrad Meyer 		return (EFAULT);
796faefad9cSConrad Meyer 	if ((chanerr & (IOAT_CHANERR_RDERR | IOAT_CHANERR_WDERR)) != 0)
797faefad9cSConrad Meyer 		return (EIO);
798faefad9cSConrad Meyer 	/* This one is probably our fault: */
799faefad9cSConrad Meyer 	if ((chanerr & IOAT_CHANERR_NDADDERR) != 0)
800faefad9cSConrad Meyer 		return (EIO);
801faefad9cSConrad Meyer 	return (EIO);
802faefad9cSConrad Meyer }
803faefad9cSConrad Meyer 
804e974f91cSConrad Meyer static void
8052f03a95fSAlexander Motin ioat_process_events(struct ioat_softc *ioat, boolean_t intr)
806e974f91cSConrad Meyer {
807e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
808e974f91cSConrad Meyer 	struct bus_dmadesc *dmadesc;
809e974f91cSConrad Meyer 	uint64_t comp_update, status;
810faefad9cSConrad Meyer 	uint32_t completed, chanerr;
811f955fec7SMateusz Guzik 	int error __diagused;
812e974f91cSConrad Meyer 
8138acd3f12SAlexander Motin 	if (intr) {
814e974f91cSConrad Meyer 		mtx_lock(&ioat->cleanup_lock);
8158acd3f12SAlexander Motin 	} else {
8168acd3f12SAlexander Motin 		if (!mtx_trylock(&ioat->cleanup_lock))
8178acd3f12SAlexander Motin 			return;
8188acd3f12SAlexander Motin 	}
819e974f91cSConrad Meyer 
820fe8712f8SConrad Meyer 	/*
821fe8712f8SConrad Meyer 	 * Don't run while the hardware is being reset.  Reset is responsible
822fe8712f8SConrad Meyer 	 * for blocking new work and draining & completing existing work, so
823fe8712f8SConrad Meyer 	 * there is nothing to do until new work is queued after reset anyway.
824fe8712f8SConrad Meyer 	 */
825fe8712f8SConrad Meyer 	if (ioat->resetting_cleanup) {
826fe8712f8SConrad Meyer 		mtx_unlock(&ioat->cleanup_lock);
827fe8712f8SConrad Meyer 		return;
828fe8712f8SConrad Meyer 	}
829fe8712f8SConrad Meyer 
830e974f91cSConrad Meyer 	completed = 0;
8310d0f2640SConrad Meyer 	comp_update = *ioat->comp_update;
832e974f91cSConrad Meyer 	status = comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK;
833e974f91cSConrad Meyer 
8348e269d99SConrad Meyer 	if (status < ioat->hw_desc_bus_addr ||
8358e269d99SConrad Meyer 	    status >= ioat->hw_desc_bus_addr + (1 << ioat->ring_size_order) *
8368e269d99SConrad Meyer 	    sizeof(struct ioat_generic_hw_descriptor))
8378e269d99SConrad Meyer 		panic("Bogus completion address %jx (channel %u)",
8388e269d99SConrad Meyer 		    (uintmax_t)status, ioat->chan_idx);
8398e269d99SConrad Meyer 
8401bbc06b8SConrad Meyer 	if (status == ioat->last_seen) {
8411bbc06b8SConrad Meyer 		/*
8421bbc06b8SConrad Meyer 		 * If we landed in process_events and nothing has been
8431bbc06b8SConrad Meyer 		 * completed, check for a timeout due to channel halt.
8441bbc06b8SConrad Meyer 		 */
8451bbc06b8SConrad Meyer 		goto out;
8461bbc06b8SConrad Meyer 	}
847dc465059SConrad Meyer 	CTR4(KTR_IOAT, "%s channel=%u hw_status=0x%lx last_seen=0x%lx",
848dc465059SConrad Meyer 	    __func__, ioat->chan_idx, comp_update, ioat->last_seen);
8491bbc06b8SConrad Meyer 
8508e269d99SConrad Meyer 	while (RING_PHYS_ADDR(ioat, ioat->tail - 1) != status) {
851e974f91cSConrad Meyer 		desc = ioat_get_ring_entry(ioat, ioat->tail);
852e974f91cSConrad Meyer 		dmadesc = &desc->bus_dmadesc;
85305e4cffbSConrad Meyer 		CTR5(KTR_IOAT, "channel=%u completing desc idx %u (%p) ok  cb %p(%p)",
85405e4cffbSConrad Meyer 		    ioat->chan_idx, ioat->tail, dmadesc, dmadesc->callback_fn,
855520f6023SConrad Meyer 		    dmadesc->callback_arg);
856e974f91cSConrad Meyer 
857b80b32a2STycho Nightingale 		bus_dmamap_unload(ioat->data_tag, desc->src_dmamap);
858b80b32a2STycho Nightingale 		bus_dmamap_unload(ioat->data_tag, desc->dst_dmamap);
859b80b32a2STycho Nightingale 		bus_dmamap_unload(ioat->data_tag, desc->src2_dmamap);
860b80b32a2STycho Nightingale 		bus_dmamap_unload(ioat->data_tag, desc->dst2_dmamap);
861b80b32a2STycho Nightingale 
862faefad9cSConrad Meyer 		if (dmadesc->callback_fn != NULL)
863faefad9cSConrad Meyer 			dmadesc->callback_fn(dmadesc->callback_arg, 0);
864e974f91cSConrad Meyer 
865466b3540SConrad Meyer 		completed++;
866e974f91cSConrad Meyer 		ioat->tail++;
867e974f91cSConrad Meyer 	}
86805e4cffbSConrad Meyer 	CTR5(KTR_IOAT, "%s channel=%u head=%u tail=%u active=%u", __func__,
86905e4cffbSConrad Meyer 	    ioat->chan_idx, ioat->head, ioat->tail, ioat_get_active(ioat));
870e974f91cSConrad Meyer 
8710283c0f5SConrad Meyer 	if (completed != 0) {
8728e269d99SConrad Meyer 		ioat->last_seen = RING_PHYS_ADDR(ioat, ioat->tail - 1);
87301fbbc88SConrad Meyer 		ioat->stats.descriptors_processed += completed;
8742f03a95fSAlexander Motin 		wakeup(&ioat->tail);
8750283c0f5SConrad Meyer 	}
87601fbbc88SConrad Meyer 
8771bbc06b8SConrad Meyer out:
878e974f91cSConrad Meyer 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
879e974f91cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
880faefad9cSConrad Meyer 
881ea9e23edSConrad Meyer 	/*
882ea9e23edSConrad Meyer 	 * The device doesn't seem to reliably push suspend/halt statuses to
883ea9e23edSConrad Meyer 	 * the channel completion memory address, so poll the device register
8842f03a95fSAlexander Motin 	 * here.  For performance reasons skip it on interrupts, do it only
8852f03a95fSAlexander Motin 	 * on much more rare polling events.
886ea9e23edSConrad Meyer 	 */
8872f03a95fSAlexander Motin 	if (!intr)
888ea9e23edSConrad Meyer 		comp_update = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS;
889d2c55e5aSConrad Meyer 	if (!is_ioat_halted(comp_update) && !is_ioat_suspended(comp_update))
890faefad9cSConrad Meyer 		return;
891faefad9cSConrad Meyer 
89201fbbc88SConrad Meyer 	ioat->stats.channel_halts++;
89301fbbc88SConrad Meyer 
894faefad9cSConrad Meyer 	/*
895faefad9cSConrad Meyer 	 * Fatal programming error on this DMA channel.  Flush any outstanding
896faefad9cSConrad Meyer 	 * work with error status and restart the engine.
897faefad9cSConrad Meyer 	 */
898faefad9cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
899faefad9cSConrad Meyer 	ioat->quiescing = TRUE;
9002f03a95fSAlexander Motin 	mtx_unlock(&ioat->submit_lock);
9012f03a95fSAlexander Motin 
9023a370919SConrad Meyer 	/*
9032f03a95fSAlexander Motin 	 * This is safe to do here because the submit queue is quiesced.  We
9042f03a95fSAlexander Motin 	 * know that we will drain all outstanding events, so ioat_reset_hw
9052f03a95fSAlexander Motin 	 * can't deadlock. It is necessary to protect other ioat_process_event
9062f03a95fSAlexander Motin 	 * threads from racing ioat_reset_hw, reading an indeterminate hw
9072f03a95fSAlexander Motin 	 * state, and attempting to continue issuing completions.
9083a370919SConrad Meyer 	 */
9092f03a95fSAlexander Motin 	mtx_lock(&ioat->cleanup_lock);
9103a370919SConrad Meyer 	ioat->resetting_cleanup = TRUE;
911faefad9cSConrad Meyer 
912faefad9cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
913985efa77SConrad Meyer 	if (1 <= g_ioat_debug_level)
914faefad9cSConrad Meyer 		ioat_halted_debug(ioat, chanerr);
91501fbbc88SConrad Meyer 	ioat->stats.last_halt_chanerr = chanerr;
916faefad9cSConrad Meyer 
917faefad9cSConrad Meyer 	while (ioat_get_active(ioat) > 0) {
918faefad9cSConrad Meyer 		desc = ioat_get_ring_entry(ioat, ioat->tail);
919faefad9cSConrad Meyer 		dmadesc = &desc->bus_dmadesc;
92005e4cffbSConrad Meyer 		CTR5(KTR_IOAT, "channel=%u completing desc idx %u (%p) err cb %p(%p)",
92105e4cffbSConrad Meyer 		    ioat->chan_idx, ioat->tail, dmadesc, dmadesc->callback_fn,
922520f6023SConrad Meyer 		    dmadesc->callback_arg);
923faefad9cSConrad Meyer 
924faefad9cSConrad Meyer 		if (dmadesc->callback_fn != NULL)
925faefad9cSConrad Meyer 			dmadesc->callback_fn(dmadesc->callback_arg,
926faefad9cSConrad Meyer 			    chanerr_to_errno(chanerr));
927faefad9cSConrad Meyer 
928faefad9cSConrad Meyer 		ioat->tail++;
92901fbbc88SConrad Meyer 		ioat->stats.descriptors_processed++;
93001fbbc88SConrad Meyer 		ioat->stats.descriptors_error++;
931faefad9cSConrad Meyer 	}
93205e4cffbSConrad Meyer 	CTR5(KTR_IOAT, "%s channel=%u head=%u tail=%u active=%u", __func__,
93305e4cffbSConrad Meyer 	    ioat->chan_idx, ioat->head, ioat->tail, ioat_get_active(ioat));
934faefad9cSConrad Meyer 
935faefad9cSConrad Meyer 	/* Clear error status */
936faefad9cSConrad Meyer 	ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
937faefad9cSConrad Meyer 
938faefad9cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
939faefad9cSConrad Meyer 
940faefad9cSConrad Meyer 	ioat_log_message(0, "Resetting channel to recover from error\n");
941374b05e1SConrad Meyer 	error = taskqueue_enqueue(taskqueue_thread, &ioat->reset_task);
942374b05e1SConrad Meyer 	KASSERT(error == 0,
943374b05e1SConrad Meyer 	    ("%s: taskqueue_enqueue failed: %d", __func__, error));
944374b05e1SConrad Meyer }
945374b05e1SConrad Meyer 
946374b05e1SConrad Meyer static void
947374b05e1SConrad Meyer ioat_reset_hw_task(void *ctx, int pending __unused)
948374b05e1SConrad Meyer {
949374b05e1SConrad Meyer 	struct ioat_softc *ioat;
950f955fec7SMateusz Guzik 	int error __diagused;
951374b05e1SConrad Meyer 
952374b05e1SConrad Meyer 	ioat = ctx;
953374b05e1SConrad Meyer 	ioat_log_message(1, "%s: Resetting channel\n", __func__);
954374b05e1SConrad Meyer 
955faefad9cSConrad Meyer 	error = ioat_reset_hw(ioat);
956faefad9cSConrad Meyer 	KASSERT(error == 0, ("%s: reset failed: %d", __func__, error));
957e974f91cSConrad Meyer }
958e974f91cSConrad Meyer 
959e974f91cSConrad Meyer /*
960e974f91cSConrad Meyer  * User API functions
961e974f91cSConrad Meyer  */
962f8f92e91SConrad Meyer unsigned
963f8f92e91SConrad Meyer ioat_get_nchannels(void)
964f8f92e91SConrad Meyer {
965f8f92e91SConrad Meyer 
966f8f92e91SConrad Meyer 	return (ioat_channel_index);
967f8f92e91SConrad Meyer }
968f8f92e91SConrad Meyer 
969e974f91cSConrad Meyer bus_dmaengine_t
9700ff814e8SConrad Meyer ioat_get_dmaengine(uint32_t index, int flags)
971e974f91cSConrad Meyer {
9720ff814e8SConrad Meyer 	struct ioat_softc *ioat;
9730ff814e8SConrad Meyer 
9740ff814e8SConrad Meyer 	KASSERT((flags & ~(M_NOWAIT | M_WAITOK)) == 0,
9750ff814e8SConrad Meyer 	    ("invalid flags: 0x%08x", flags));
9760ff814e8SConrad Meyer 	KASSERT((flags & (M_NOWAIT | M_WAITOK)) != (M_NOWAIT | M_WAITOK),
9770ff814e8SConrad Meyer 	    ("invalid wait | nowait"));
978e974f91cSConrad Meyer 
9792f03a95fSAlexander Motin 	mtx_lock(&ioat_list_mtx);
9802f03a95fSAlexander Motin 	if (index >= ioat_channel_index ||
9812f03a95fSAlexander Motin 	    (ioat = ioat_channel[index]) == NULL) {
9822f03a95fSAlexander Motin 		mtx_unlock(&ioat_list_mtx);
983e974f91cSConrad Meyer 		return (NULL);
9842f03a95fSAlexander Motin 	}
9852f03a95fSAlexander Motin 	mtx_lock(&ioat->submit_lock);
9862f03a95fSAlexander Motin 	mtx_unlock(&ioat_list_mtx);
9875f77bd3eSConrad Meyer 
9882f03a95fSAlexander Motin 	if (ioat->destroying) {
9892f03a95fSAlexander Motin 		mtx_unlock(&ioat->submit_lock);
9900ff814e8SConrad Meyer 		return (NULL);
9910ff814e8SConrad Meyer 	}
9920ff814e8SConrad Meyer 
9932f03a95fSAlexander Motin 	ioat_get(ioat);
9942f03a95fSAlexander Motin 	if (ioat->quiescing) {
9952f03a95fSAlexander Motin 		if ((flags & M_NOWAIT) != 0) {
9962f03a95fSAlexander Motin 			ioat_put(ioat);
9972f03a95fSAlexander Motin 			mtx_unlock(&ioat->submit_lock);
9982f03a95fSAlexander Motin 			return (NULL);
9992f03a95fSAlexander Motin 		}
10002f03a95fSAlexander Motin 
10012f03a95fSAlexander Motin 		while (ioat->quiescing && !ioat->destroying)
10022f03a95fSAlexander Motin 			msleep(&ioat->quiescing, &ioat->submit_lock, 0, "getdma", 0);
10032f03a95fSAlexander Motin 
10042f03a95fSAlexander Motin 		if (ioat->destroying) {
10052f03a95fSAlexander Motin 			ioat_put(ioat);
10062f03a95fSAlexander Motin 			mtx_unlock(&ioat->submit_lock);
10072f03a95fSAlexander Motin 			return (NULL);
10082f03a95fSAlexander Motin 		}
10092f03a95fSAlexander Motin 	}
10102f03a95fSAlexander Motin 	mtx_unlock(&ioat->submit_lock);
10112f03a95fSAlexander Motin 	return (&ioat->dmaengine);
1012466b3540SConrad Meyer }
1013466b3540SConrad Meyer 
1014466b3540SConrad Meyer void
1015466b3540SConrad Meyer ioat_put_dmaengine(bus_dmaengine_t dmaengine)
1016466b3540SConrad Meyer {
1017466b3540SConrad Meyer 	struct ioat_softc *ioat;
1018466b3540SConrad Meyer 
1019466b3540SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
10202f03a95fSAlexander Motin 	mtx_lock(&ioat->submit_lock);
10212f03a95fSAlexander Motin 	ioat_put(ioat);
10222f03a95fSAlexander Motin 	mtx_unlock(&ioat->submit_lock);
1023e974f91cSConrad Meyer }
1024e974f91cSConrad Meyer 
10255ca9fc2aSConrad Meyer int
102631bf2875SConrad Meyer ioat_get_hwversion(bus_dmaengine_t dmaengine)
102731bf2875SConrad Meyer {
102831bf2875SConrad Meyer 	struct ioat_softc *ioat;
102931bf2875SConrad Meyer 
103031bf2875SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
103131bf2875SConrad Meyer 	return (ioat->version);
103231bf2875SConrad Meyer }
103331bf2875SConrad Meyer 
1034bd81fe68SConrad Meyer size_t
1035bd81fe68SConrad Meyer ioat_get_max_io_size(bus_dmaengine_t dmaengine)
1036bd81fe68SConrad Meyer {
1037bd81fe68SConrad Meyer 	struct ioat_softc *ioat;
1038bd81fe68SConrad Meyer 
1039bd81fe68SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1040bd81fe68SConrad Meyer 	return (ioat->max_xfer_size);
1041bd81fe68SConrad Meyer }
1042bd81fe68SConrad Meyer 
1043f8253f1aSConrad Meyer uint32_t
1044f8253f1aSConrad Meyer ioat_get_capabilities(bus_dmaengine_t dmaengine)
1045f8253f1aSConrad Meyer {
1046f8253f1aSConrad Meyer 	struct ioat_softc *ioat;
1047f8253f1aSConrad Meyer 
1048f8253f1aSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1049f8253f1aSConrad Meyer 	return (ioat->capabilities);
1050f8253f1aSConrad Meyer }
1051f8253f1aSConrad Meyer 
105231bf2875SConrad Meyer int
10537280125eSAlexander Motin ioat_get_domain(bus_dmaengine_t dmaengine, int *domain)
10547280125eSAlexander Motin {
10557280125eSAlexander Motin 	struct ioat_softc *ioat;
10567280125eSAlexander Motin 
10577280125eSAlexander Motin 	ioat = to_ioat_softc(dmaengine);
10587280125eSAlexander Motin 	return (bus_get_domain(ioat->device, domain));
10597280125eSAlexander Motin }
10607280125eSAlexander Motin 
10617280125eSAlexander Motin int
10625ca9fc2aSConrad Meyer ioat_set_interrupt_coalesce(bus_dmaengine_t dmaengine, uint16_t delay)
10635ca9fc2aSConrad Meyer {
10645ca9fc2aSConrad Meyer 	struct ioat_softc *ioat;
10655ca9fc2aSConrad Meyer 
10665ca9fc2aSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
10675ca9fc2aSConrad Meyer 	if (!ioat->intrdelay_supported)
10685ca9fc2aSConrad Meyer 		return (ENODEV);
10695ca9fc2aSConrad Meyer 	if (delay > ioat->intrdelay_max)
10705ca9fc2aSConrad Meyer 		return (ERANGE);
10715ca9fc2aSConrad Meyer 
10725ca9fc2aSConrad Meyer 	ioat_write_2(ioat, IOAT_INTRDELAY_OFFSET, delay);
10735ca9fc2aSConrad Meyer 	ioat->cached_intrdelay =
10745ca9fc2aSConrad Meyer 	    ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) & IOAT_INTRDELAY_US_MASK;
10755ca9fc2aSConrad Meyer 	return (0);
10765ca9fc2aSConrad Meyer }
10775ca9fc2aSConrad Meyer 
10785ca9fc2aSConrad Meyer uint16_t
10795ca9fc2aSConrad Meyer ioat_get_max_coalesce_period(bus_dmaengine_t dmaengine)
10805ca9fc2aSConrad Meyer {
10815ca9fc2aSConrad Meyer 	struct ioat_softc *ioat;
10825ca9fc2aSConrad Meyer 
10835ca9fc2aSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
10845ca9fc2aSConrad Meyer 	return (ioat->intrdelay_max);
10855ca9fc2aSConrad Meyer }
10865ca9fc2aSConrad Meyer 
1087e974f91cSConrad Meyer void
1088e974f91cSConrad Meyer ioat_acquire(bus_dmaengine_t dmaengine)
1089e974f91cSConrad Meyer {
1090e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1091e974f91cSConrad Meyer 
1092e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1093e974f91cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
1094520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
109558a639b7SConrad Meyer 	ioat->acq_head = ioat->head;
1096e974f91cSConrad Meyer }
1097e974f91cSConrad Meyer 
10981502e363SConrad Meyer int
10991502e363SConrad Meyer ioat_acquire_reserve(bus_dmaengine_t dmaengine, unsigned n, int mflags)
11001502e363SConrad Meyer {
11011502e363SConrad Meyer 	struct ioat_softc *ioat;
11021502e363SConrad Meyer 	int error;
11031502e363SConrad Meyer 
11041502e363SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
11051502e363SConrad Meyer 	ioat_acquire(dmaengine);
11061502e363SConrad Meyer 
11071502e363SConrad Meyer 	error = ioat_reserve_space(ioat, n, mflags);
11081502e363SConrad Meyer 	if (error != 0)
11091502e363SConrad Meyer 		ioat_release(dmaengine);
11101502e363SConrad Meyer 	return (error);
11111502e363SConrad Meyer }
11121502e363SConrad Meyer 
1113e974f91cSConrad Meyer void
1114e974f91cSConrad Meyer ioat_release(bus_dmaengine_t dmaengine)
1115e974f91cSConrad Meyer {
1116e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1117e974f91cSConrad Meyer 
1118e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
11192f03a95fSAlexander Motin 	CTR3(KTR_IOAT, "%s channel=%u dispatch1 head=%u", __func__,
11202f03a95fSAlexander Motin 	    ioat->chan_idx, ioat->head);
112176305bb8SConrad Meyer 	KFAIL_POINT_CODE(DEBUG_FP, ioat_release, /* do nothing */);
11222f03a95fSAlexander Motin 	CTR3(KTR_IOAT, "%s channel=%u dispatch2 head=%u", __func__,
11232f03a95fSAlexander Motin 	    ioat->chan_idx, ioat->head);
112476305bb8SConrad Meyer 
112558a639b7SConrad Meyer 	if (ioat->acq_head != ioat->head) {
112658a639b7SConrad Meyer 		ioat_write_2(ioat, IOAT_DMACOUNT_OFFSET,
11272f03a95fSAlexander Motin 		    (uint16_t)ioat->head);
1128f44abf1fSConrad Meyer 
11292f03a95fSAlexander Motin 		if (!callout_pending(&ioat->poll_timer)) {
1130657dc81dSAlexander Motin 			callout_reset_on(&ioat->poll_timer, 1,
1131657dc81dSAlexander Motin 			    ioat_poll_timer_callback, ioat, ioat->cpu);
113258a639b7SConrad Meyer 		}
1133f44abf1fSConrad Meyer 	}
1134e974f91cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
1135e974f91cSConrad Meyer }
1136e974f91cSConrad Meyer 
11379e3bbf26SConrad Meyer static struct ioat_descriptor *
11389e3bbf26SConrad Meyer ioat_op_generic(struct ioat_softc *ioat, uint8_t op,
11399e3bbf26SConrad Meyer     uint32_t size, uint64_t src, uint64_t dst,
11409e3bbf26SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg,
11419e3bbf26SConrad Meyer     uint32_t flags)
1142e974f91cSConrad Meyer {
11439e3bbf26SConrad Meyer 	struct ioat_generic_hw_descriptor *hw_desc;
1144e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
1145b80b32a2STycho Nightingale 	bus_dma_segment_t seg;
1146b80b32a2STycho Nightingale 	int mflags, nseg, error;
1147e974f91cSConrad Meyer 
11489e3bbf26SConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
11499e3bbf26SConrad Meyer 
1150bec7ff79SConrad Meyer 	KASSERT((flags & ~_DMA_GENERIC_FLAGS) == 0,
1151bec7ff79SConrad Meyer 	    ("Unrecognized flag(s): %#x", flags & ~_DMA_GENERIC_FLAGS));
1152630d9800SAlexander Motin 	KASSERT(size <= ioat->max_xfer_size, ("%s: size too big (%u > %u)",
1153630d9800SAlexander Motin 	    __func__, (unsigned)size, ioat->max_xfer_size));
1154630d9800SAlexander Motin 
1155bf8553eaSConrad Meyer 	if ((flags & DMA_NO_WAIT) != 0)
1156bf8553eaSConrad Meyer 		mflags = M_NOWAIT;
1157bf8553eaSConrad Meyer 	else
1158bf8553eaSConrad Meyer 		mflags = M_WAITOK;
1159e974f91cSConrad Meyer 
1160bf8553eaSConrad Meyer 	if (ioat_reserve_space(ioat, 1, mflags) != 0)
1161e974f91cSConrad Meyer 		return (NULL);
1162e974f91cSConrad Meyer 
1163e974f91cSConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->head);
11648e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, ioat->head)->generic;
1165e974f91cSConrad Meyer 
1166e974f91cSConrad Meyer 	hw_desc->u.control_raw = 0;
11679e3bbf26SConrad Meyer 	hw_desc->u.control_generic.op = op;
11689e3bbf26SConrad Meyer 	hw_desc->u.control_generic.completion_update = 1;
1169e974f91cSConrad Meyer 
1170e974f91cSConrad Meyer 	if ((flags & DMA_INT_EN) != 0)
11719e3bbf26SConrad Meyer 		hw_desc->u.control_generic.int_enable = 1;
11726ca07079SConrad Meyer 	if ((flags & DMA_FENCE) != 0)
11736ca07079SConrad Meyer 		hw_desc->u.control_generic.fence = 1;
1174e974f91cSConrad Meyer 
11759e3bbf26SConrad Meyer 	hw_desc->size = size;
1176b80b32a2STycho Nightingale 
1177b80b32a2STycho Nightingale 	if (src != 0) {
1178b80b32a2STycho Nightingale 		nseg = -1;
1179b80b32a2STycho Nightingale 		error = _bus_dmamap_load_phys(ioat->data_tag, desc->src_dmamap,
1180b80b32a2STycho Nightingale 		    src, size, 0, &seg, &nseg);
1181b80b32a2STycho Nightingale 		if (error != 0) {
1182b80b32a2STycho Nightingale 			ioat_log_message(0, "%s: _bus_dmamap_load_phys"
1183b80b32a2STycho Nightingale 			    " failed %d\n", __func__, error);
1184b80b32a2STycho Nightingale 			return (NULL);
1185b80b32a2STycho Nightingale 		}
1186b80b32a2STycho Nightingale 		hw_desc->src_addr = seg.ds_addr;
1187b80b32a2STycho Nightingale 	}
1188b80b32a2STycho Nightingale 
1189b80b32a2STycho Nightingale 	if (dst != 0) {
1190b80b32a2STycho Nightingale 		nseg = -1;
1191b80b32a2STycho Nightingale 		error = _bus_dmamap_load_phys(ioat->data_tag, desc->dst_dmamap,
1192b80b32a2STycho Nightingale 		    dst, size, 0, &seg, &nseg);
1193b80b32a2STycho Nightingale 		if (error != 0) {
1194b80b32a2STycho Nightingale 			ioat_log_message(0, "%s: _bus_dmamap_load_phys"
1195b80b32a2STycho Nightingale 			    " failed %d\n", __func__, error);
1196b80b32a2STycho Nightingale 			return (NULL);
1197b80b32a2STycho Nightingale 		}
1198b80b32a2STycho Nightingale 		hw_desc->dest_addr = seg.ds_addr;
1199b80b32a2STycho Nightingale 	}
1200e974f91cSConrad Meyer 
1201e974f91cSConrad Meyer 	desc->bus_dmadesc.callback_fn = callback_fn;
1202e974f91cSConrad Meyer 	desc->bus_dmadesc.callback_arg = callback_arg;
12039e3bbf26SConrad Meyer 	return (desc);
12049e3bbf26SConrad Meyer }
1205e974f91cSConrad Meyer 
12069e3bbf26SConrad Meyer struct bus_dmadesc *
12079e3bbf26SConrad Meyer ioat_null(bus_dmaengine_t dmaengine, bus_dmaengine_callback_t callback_fn,
12089e3bbf26SConrad Meyer     void *callback_arg, uint32_t flags)
12099e3bbf26SConrad Meyer {
12109e3bbf26SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
12119e3bbf26SConrad Meyer 	struct ioat_descriptor *desc;
12129e3bbf26SConrad Meyer 	struct ioat_softc *ioat;
12139e3bbf26SConrad Meyer 
12149e3bbf26SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1215520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
12169e3bbf26SConrad Meyer 
12179e3bbf26SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, 8, 0, 0, callback_fn,
12189e3bbf26SConrad Meyer 	    callback_arg, flags);
12199e3bbf26SConrad Meyer 	if (desc == NULL)
12209e3bbf26SConrad Meyer 		return (NULL);
12219e3bbf26SConrad Meyer 
12228e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->dma;
12239e3bbf26SConrad Meyer 	hw_desc->u.control.null = 1;
1224e974f91cSConrad Meyer 	ioat_submit_single(ioat);
1225e974f91cSConrad Meyer 	return (&desc->bus_dmadesc);
1226e974f91cSConrad Meyer }
1227e974f91cSConrad Meyer 
1228e974f91cSConrad Meyer struct bus_dmadesc *
1229e974f91cSConrad Meyer ioat_copy(bus_dmaengine_t dmaengine, bus_addr_t dst,
1230e974f91cSConrad Meyer     bus_addr_t src, bus_size_t len, bus_dmaengine_callback_t callback_fn,
1231e974f91cSConrad Meyer     void *callback_arg, uint32_t flags)
1232e974f91cSConrad Meyer {
1233e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
12349e3bbf26SConrad Meyer 	struct ioat_descriptor *desc;
1235e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1236e974f91cSConrad Meyer 
1237e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
12389e3bbf26SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, len, src, dst, callback_fn,
12399e3bbf26SConrad Meyer 	    callback_arg, flags);
12409e3bbf26SConrad Meyer 	if (desc == NULL)
1241e974f91cSConrad Meyer 		return (NULL);
1242e974f91cSConrad Meyer 
12438e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->dma;
1244e974f91cSConrad Meyer 	if (g_ioat_debug_level >= 3)
1245e974f91cSConrad Meyer 		dump_descriptor(hw_desc);
1246e974f91cSConrad Meyer 
1247e974f91cSConrad Meyer 	ioat_submit_single(ioat);
124805e4cffbSConrad Meyer 	CTR6(KTR_IOAT, "%s channel=%u desc=%p dest=%lx src=%lx len=%lx",
124905e4cffbSConrad Meyer 	    __func__, ioat->chan_idx, &desc->bus_dmadesc, dst, src, len);
1250e974f91cSConrad Meyer 	return (&desc->bus_dmadesc);
1251e974f91cSConrad Meyer }
1252e974f91cSConrad Meyer 
12532a4fd6b1SConrad Meyer struct bus_dmadesc *
12549950fde0SConrad Meyer ioat_copy_8k_aligned(bus_dmaengine_t dmaengine, bus_addr_t dst1,
12559950fde0SConrad Meyer     bus_addr_t dst2, bus_addr_t src1, bus_addr_t src2,
12569950fde0SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
12579950fde0SConrad Meyer {
12589950fde0SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
12599950fde0SConrad Meyer 	struct ioat_descriptor *desc;
12609950fde0SConrad Meyer 	struct ioat_softc *ioat;
1261b80b32a2STycho Nightingale 	bus_size_t src1_len, dst1_len;
1262b80b32a2STycho Nightingale 	bus_dma_segment_t seg;
1263b80b32a2STycho Nightingale 	int nseg, error;
12649950fde0SConrad Meyer 
12659950fde0SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1266520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
12679950fde0SConrad Meyer 
1268630d9800SAlexander Motin 	KASSERT(((src1 | src2 | dst1 | dst2) & PAGE_MASK) == 0,
1269630d9800SAlexander Motin 	    ("%s: addresses are not page-aligned", __func__));
12709950fde0SConrad Meyer 
1271b80b32a2STycho Nightingale 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, 2 * PAGE_SIZE, 0, 0,
12729950fde0SConrad Meyer 	    callback_fn, callback_arg, flags);
12739950fde0SConrad Meyer 	if (desc == NULL)
12749950fde0SConrad Meyer 		return (NULL);
12759950fde0SConrad Meyer 
12768e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->dma;
1277b80b32a2STycho Nightingale 
1278b80b32a2STycho Nightingale 	src1_len = (src2 != src1 + PAGE_SIZE) ? PAGE_SIZE : 2 * PAGE_SIZE;
1279b80b32a2STycho Nightingale 	nseg = -1;
1280b80b32a2STycho Nightingale 	error = _bus_dmamap_load_phys(ioat->data_tag,
1281b80b32a2STycho Nightingale 	    desc->src_dmamap, src1, src1_len, 0, &seg, &nseg);
1282b80b32a2STycho Nightingale 	if (error != 0) {
1283b80b32a2STycho Nightingale 		ioat_log_message(0, "%s: _bus_dmamap_load_phys"
1284b80b32a2STycho Nightingale 		    " failed %d\n", __func__, error);
1285b80b32a2STycho Nightingale 		return (NULL);
12869950fde0SConrad Meyer 	}
1287b80b32a2STycho Nightingale 	hw_desc->src_addr = seg.ds_addr;
1288b80b32a2STycho Nightingale 	if (src1_len != 2 * PAGE_SIZE) {
1289b80b32a2STycho Nightingale 		hw_desc->u.control.src_page_break = 1;
1290b80b32a2STycho Nightingale 		nseg = -1;
1291b80b32a2STycho Nightingale 		error = _bus_dmamap_load_phys(ioat->data_tag,
1292b80b32a2STycho Nightingale 		    desc->src2_dmamap, src2, PAGE_SIZE, 0, &seg, &nseg);
1293b80b32a2STycho Nightingale 		if (error != 0) {
1294b80b32a2STycho Nightingale 			ioat_log_message(0, "%s: _bus_dmamap_load_phys"
1295b80b32a2STycho Nightingale 			    " failed %d\n", __func__, error);
1296b80b32a2STycho Nightingale 			return (NULL);
1297b80b32a2STycho Nightingale 		}
1298b80b32a2STycho Nightingale 		hw_desc->next_src_addr = seg.ds_addr;
1299b80b32a2STycho Nightingale 	}
1300b80b32a2STycho Nightingale 
1301b80b32a2STycho Nightingale 	dst1_len = (dst2 != dst1 + PAGE_SIZE) ? PAGE_SIZE : 2 * PAGE_SIZE;
1302b80b32a2STycho Nightingale 	nseg = -1;
1303b80b32a2STycho Nightingale 	error = _bus_dmamap_load_phys(ioat->data_tag,
1304b80b32a2STycho Nightingale 	    desc->dst_dmamap, dst1, dst1_len, 0, &seg, &nseg);
1305b80b32a2STycho Nightingale 	if (error != 0) {
1306b80b32a2STycho Nightingale 		ioat_log_message(0, "%s: _bus_dmamap_load_phys"
1307b80b32a2STycho Nightingale 		    " failed %d\n", __func__, error);
1308b80b32a2STycho Nightingale 		return (NULL);
1309b80b32a2STycho Nightingale 	}
1310b80b32a2STycho Nightingale 	hw_desc->dest_addr = seg.ds_addr;
1311b80b32a2STycho Nightingale 	if (dst1_len != 2 * PAGE_SIZE) {
13129950fde0SConrad Meyer 		hw_desc->u.control.dest_page_break = 1;
1313b80b32a2STycho Nightingale 		nseg = -1;
1314b80b32a2STycho Nightingale 		error = _bus_dmamap_load_phys(ioat->data_tag,
1315b80b32a2STycho Nightingale 		    desc->dst2_dmamap, dst2, PAGE_SIZE, 0, &seg, &nseg);
1316b80b32a2STycho Nightingale 		if (error != 0) {
1317b80b32a2STycho Nightingale 			ioat_log_message(0, "%s: _bus_dmamap_load_phys"
1318b80b32a2STycho Nightingale 			    " failed %d\n", __func__, error);
1319b80b32a2STycho Nightingale 			return (NULL);
1320b80b32a2STycho Nightingale 		}
1321b80b32a2STycho Nightingale 		hw_desc->next_dest_addr = seg.ds_addr;
13229950fde0SConrad Meyer 	}
13239950fde0SConrad Meyer 
13249950fde0SConrad Meyer 	if (g_ioat_debug_level >= 3)
13259950fde0SConrad Meyer 		dump_descriptor(hw_desc);
13269950fde0SConrad Meyer 
13279950fde0SConrad Meyer 	ioat_submit_single(ioat);
13289950fde0SConrad Meyer 	return (&desc->bus_dmadesc);
13299950fde0SConrad Meyer }
13309950fde0SConrad Meyer 
13319950fde0SConrad Meyer struct bus_dmadesc *
1332bec7ff79SConrad Meyer ioat_copy_crc(bus_dmaengine_t dmaengine, bus_addr_t dst, bus_addr_t src,
1333bec7ff79SConrad Meyer     bus_size_t len, uint32_t *initialseed, bus_addr_t crcptr,
1334bec7ff79SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
1335bec7ff79SConrad Meyer {
1336bec7ff79SConrad Meyer 	struct ioat_crc32_hw_descriptor *hw_desc;
1337bec7ff79SConrad Meyer 	struct ioat_descriptor *desc;
1338bec7ff79SConrad Meyer 	struct ioat_softc *ioat;
1339bec7ff79SConrad Meyer 	uint32_t teststore;
1340bec7ff79SConrad Meyer 	uint8_t op;
1341b80b32a2STycho Nightingale 	bus_dma_segment_t seg;
1342b80b32a2STycho Nightingale 	int nseg, error;
1343bec7ff79SConrad Meyer 
1344bec7ff79SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1345520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1346bec7ff79SConrad Meyer 
1347630d9800SAlexander Motin 	KASSERT((ioat->capabilities & IOAT_DMACAP_MOVECRC) != 0,
1348630d9800SAlexander Motin 	    ("%s: device lacks MOVECRC capability", __func__));
1349bec7ff79SConrad Meyer 	teststore = (flags & _DMA_CRC_TESTSTORE);
1350630d9800SAlexander Motin 	KASSERT(teststore != _DMA_CRC_TESTSTORE,
1351630d9800SAlexander Motin 	    ("%s: TEST and STORE invalid", __func__));
1352630d9800SAlexander Motin 	KASSERT(teststore != 0 || (flags & DMA_CRC_INLINE) == 0,
1353630d9800SAlexander Motin 	    ("%s: INLINE invalid without TEST or STORE", __func__));
1354bec7ff79SConrad Meyer 
1355bec7ff79SConrad Meyer 	switch (teststore) {
1356bec7ff79SConrad Meyer 	case DMA_CRC_STORE:
1357bec7ff79SConrad Meyer 		op = IOAT_OP_MOVECRC_STORE;
1358bec7ff79SConrad Meyer 		break;
1359bec7ff79SConrad Meyer 	case DMA_CRC_TEST:
1360bec7ff79SConrad Meyer 		op = IOAT_OP_MOVECRC_TEST;
1361bec7ff79SConrad Meyer 		break;
1362bec7ff79SConrad Meyer 	default:
1363bec7ff79SConrad Meyer 		KASSERT(teststore == 0, ("bogus"));
1364bec7ff79SConrad Meyer 		op = IOAT_OP_MOVECRC;
1365bec7ff79SConrad Meyer 		break;
1366bec7ff79SConrad Meyer 	}
1367bec7ff79SConrad Meyer 
1368bec7ff79SConrad Meyer 	desc = ioat_op_generic(ioat, op, len, src, dst, callback_fn,
1369bec7ff79SConrad Meyer 	    callback_arg, flags & ~_DMA_CRC_FLAGS);
1370bec7ff79SConrad Meyer 	if (desc == NULL)
1371bec7ff79SConrad Meyer 		return (NULL);
1372bec7ff79SConrad Meyer 
13738e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->crc32;
1374bec7ff79SConrad Meyer 
1375b80b32a2STycho Nightingale 	if ((flags & DMA_CRC_INLINE) == 0) {
1376b80b32a2STycho Nightingale 		nseg = -1;
13771f4a469dSAlexander Motin 		error = _bus_dmamap_load_phys(ioat->data_tag,
13781f4a469dSAlexander Motin 		    desc->dst2_dmamap, crcptr, sizeof(uint32_t), 0,
1379b80b32a2STycho Nightingale 		    &seg, &nseg);
1380b80b32a2STycho Nightingale 		if (error != 0) {
1381b80b32a2STycho Nightingale 			ioat_log_message(0, "%s: _bus_dmamap_load_phys"
1382b80b32a2STycho Nightingale 			    " failed %d\n", __func__, error);
1383b80b32a2STycho Nightingale 			return (NULL);
1384b80b32a2STycho Nightingale 		}
1385b80b32a2STycho Nightingale 		hw_desc->crc_address = seg.ds_addr;
1386b80b32a2STycho Nightingale 	} else
1387bec7ff79SConrad Meyer 		hw_desc->u.control.crc_location = 1;
1388bec7ff79SConrad Meyer 
1389bec7ff79SConrad Meyer 	if (initialseed != NULL) {
1390bec7ff79SConrad Meyer 		hw_desc->u.control.use_seed = 1;
1391bec7ff79SConrad Meyer 		hw_desc->seed = *initialseed;
1392bec7ff79SConrad Meyer 	}
1393bec7ff79SConrad Meyer 
1394bec7ff79SConrad Meyer 	if (g_ioat_debug_level >= 3)
1395bec7ff79SConrad Meyer 		dump_descriptor(hw_desc);
1396bec7ff79SConrad Meyer 
1397bec7ff79SConrad Meyer 	ioat_submit_single(ioat);
1398bec7ff79SConrad Meyer 	return (&desc->bus_dmadesc);
1399bec7ff79SConrad Meyer }
1400bec7ff79SConrad Meyer 
1401bec7ff79SConrad Meyer struct bus_dmadesc *
1402bec7ff79SConrad Meyer ioat_crc(bus_dmaengine_t dmaengine, bus_addr_t src, bus_size_t len,
1403bec7ff79SConrad Meyer     uint32_t *initialseed, bus_addr_t crcptr,
1404bec7ff79SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
1405bec7ff79SConrad Meyer {
1406bec7ff79SConrad Meyer 	struct ioat_crc32_hw_descriptor *hw_desc;
1407bec7ff79SConrad Meyer 	struct ioat_descriptor *desc;
1408bec7ff79SConrad Meyer 	struct ioat_softc *ioat;
1409bec7ff79SConrad Meyer 	uint32_t teststore;
1410bec7ff79SConrad Meyer 	uint8_t op;
1411b80b32a2STycho Nightingale 	bus_dma_segment_t seg;
1412b80b32a2STycho Nightingale 	int nseg, error;
1413bec7ff79SConrad Meyer 
1414bec7ff79SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1415520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1416bec7ff79SConrad Meyer 
1417630d9800SAlexander Motin 	KASSERT((ioat->capabilities & IOAT_DMACAP_CRC) != 0,
1418630d9800SAlexander Motin 	    ("%s: device lacks CRC capability", __func__));
1419bec7ff79SConrad Meyer 	teststore = (flags & _DMA_CRC_TESTSTORE);
1420630d9800SAlexander Motin 	KASSERT(teststore != _DMA_CRC_TESTSTORE,
1421630d9800SAlexander Motin 	    ("%s: TEST and STORE invalid", __func__));
1422630d9800SAlexander Motin 	KASSERT(teststore != 0 || (flags & DMA_CRC_INLINE) == 0,
1423630d9800SAlexander Motin 	    ("%s: INLINE invalid without TEST or STORE", __func__));
1424bec7ff79SConrad Meyer 
1425bec7ff79SConrad Meyer 	switch (teststore) {
1426bec7ff79SConrad Meyer 	case DMA_CRC_STORE:
1427bec7ff79SConrad Meyer 		op = IOAT_OP_CRC_STORE;
1428bec7ff79SConrad Meyer 		break;
1429bec7ff79SConrad Meyer 	case DMA_CRC_TEST:
1430bec7ff79SConrad Meyer 		op = IOAT_OP_CRC_TEST;
1431bec7ff79SConrad Meyer 		break;
1432bec7ff79SConrad Meyer 	default:
1433bec7ff79SConrad Meyer 		KASSERT(teststore == 0, ("bogus"));
1434bec7ff79SConrad Meyer 		op = IOAT_OP_CRC;
1435bec7ff79SConrad Meyer 		break;
1436bec7ff79SConrad Meyer 	}
1437bec7ff79SConrad Meyer 
1438bec7ff79SConrad Meyer 	desc = ioat_op_generic(ioat, op, len, src, 0, callback_fn,
1439bec7ff79SConrad Meyer 	    callback_arg, flags & ~_DMA_CRC_FLAGS);
1440bec7ff79SConrad Meyer 	if (desc == NULL)
1441bec7ff79SConrad Meyer 		return (NULL);
1442bec7ff79SConrad Meyer 
14438e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->crc32;
1444bec7ff79SConrad Meyer 
1445b80b32a2STycho Nightingale 	if ((flags & DMA_CRC_INLINE) == 0) {
1446b80b32a2STycho Nightingale 		nseg = -1;
14471f4a469dSAlexander Motin 		error = _bus_dmamap_load_phys(ioat->data_tag,
14481f4a469dSAlexander Motin 		    desc->dst2_dmamap, crcptr, sizeof(uint32_t), 0,
1449b80b32a2STycho Nightingale 		    &seg, &nseg);
1450b80b32a2STycho Nightingale 		if (error != 0) {
1451b80b32a2STycho Nightingale 			ioat_log_message(0, "%s: _bus_dmamap_load_phys"
1452b80b32a2STycho Nightingale 			    " failed %d\n", __func__, error);
1453b80b32a2STycho Nightingale 			return (NULL);
1454b80b32a2STycho Nightingale 		}
1455b80b32a2STycho Nightingale 		hw_desc->crc_address = seg.ds_addr;
1456b80b32a2STycho Nightingale 	} else
1457bec7ff79SConrad Meyer 		hw_desc->u.control.crc_location = 1;
1458bec7ff79SConrad Meyer 
1459bec7ff79SConrad Meyer 	if (initialseed != NULL) {
1460bec7ff79SConrad Meyer 		hw_desc->u.control.use_seed = 1;
1461bec7ff79SConrad Meyer 		hw_desc->seed = *initialseed;
1462bec7ff79SConrad Meyer 	}
1463bec7ff79SConrad Meyer 
1464bec7ff79SConrad Meyer 	if (g_ioat_debug_level >= 3)
1465bec7ff79SConrad Meyer 		dump_descriptor(hw_desc);
1466bec7ff79SConrad Meyer 
1467bec7ff79SConrad Meyer 	ioat_submit_single(ioat);
1468bec7ff79SConrad Meyer 	return (&desc->bus_dmadesc);
1469bec7ff79SConrad Meyer }
1470bec7ff79SConrad Meyer 
1471bec7ff79SConrad Meyer struct bus_dmadesc *
14722a4fd6b1SConrad Meyer ioat_blockfill(bus_dmaengine_t dmaengine, bus_addr_t dst, uint64_t fillpattern,
14732a4fd6b1SConrad Meyer     bus_size_t len, bus_dmaengine_callback_t callback_fn, void *callback_arg,
14742a4fd6b1SConrad Meyer     uint32_t flags)
14752a4fd6b1SConrad Meyer {
14762a4fd6b1SConrad Meyer 	struct ioat_fill_hw_descriptor *hw_desc;
14772a4fd6b1SConrad Meyer 	struct ioat_descriptor *desc;
14782a4fd6b1SConrad Meyer 	struct ioat_softc *ioat;
14792a4fd6b1SConrad Meyer 
14802a4fd6b1SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1481520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
14822a4fd6b1SConrad Meyer 
1483630d9800SAlexander Motin 	KASSERT((ioat->capabilities & IOAT_DMACAP_BFILL) != 0,
1484630d9800SAlexander Motin 	    ("%s: device lacks BFILL capability", __func__));
14852a4fd6b1SConrad Meyer 
1486b80b32a2STycho Nightingale 	desc = ioat_op_generic(ioat, IOAT_OP_FILL, len, 0, dst,
14872a4fd6b1SConrad Meyer 	    callback_fn, callback_arg, flags);
14882a4fd6b1SConrad Meyer 	if (desc == NULL)
14892a4fd6b1SConrad Meyer 		return (NULL);
14902a4fd6b1SConrad Meyer 
14918e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->fill;
1492b80b32a2STycho Nightingale 	hw_desc->src_data = fillpattern;
14932a4fd6b1SConrad Meyer 	if (g_ioat_debug_level >= 3)
14942a4fd6b1SConrad Meyer 		dump_descriptor(hw_desc);
14952a4fd6b1SConrad Meyer 
14962a4fd6b1SConrad Meyer 	ioat_submit_single(ioat);
14972a4fd6b1SConrad Meyer 	return (&desc->bus_dmadesc);
14982a4fd6b1SConrad Meyer }
14992a4fd6b1SConrad Meyer 
1500e974f91cSConrad Meyer /*
1501e974f91cSConrad Meyer  * Ring Management
1502e974f91cSConrad Meyer  */
1503e974f91cSConrad Meyer static inline uint32_t
1504e974f91cSConrad Meyer ioat_get_active(struct ioat_softc *ioat)
1505e974f91cSConrad Meyer {
1506e974f91cSConrad Meyer 
1507e974f91cSConrad Meyer 	return ((ioat->head - ioat->tail) & ((1 << ioat->ring_size_order) - 1));
1508e974f91cSConrad Meyer }
1509e974f91cSConrad Meyer 
1510e974f91cSConrad Meyer static inline uint32_t
1511e974f91cSConrad Meyer ioat_get_ring_space(struct ioat_softc *ioat)
1512e974f91cSConrad Meyer {
1513e974f91cSConrad Meyer 
1514e974f91cSConrad Meyer 	return ((1 << ioat->ring_size_order) - ioat_get_active(ioat) - 1);
1515e974f91cSConrad Meyer }
1516e974f91cSConrad Meyer 
1517bf8553eaSConrad Meyer /*
1518bf8553eaSConrad Meyer  * Reserves space in this IOAT descriptor ring by ensuring enough slots remain
1519bf8553eaSConrad Meyer  * for 'num_descs'.
1520bf8553eaSConrad Meyer  *
1521bf8553eaSConrad Meyer  * If mflags contains M_WAITOK, blocks until enough space is available.
1522bf8553eaSConrad Meyer  *
1523bf8553eaSConrad Meyer  * Returns zero on success, or an errno on error.  If num_descs is beyond the
1524bf8553eaSConrad Meyer  * maximum ring size, returns EINVAl; if allocation would block and mflags
1525bf8553eaSConrad Meyer  * contains M_NOWAIT, returns EAGAIN.
1526bf8553eaSConrad Meyer  *
1527bf8553eaSConrad Meyer  * Must be called with the submit_lock held; returns with the lock held.  The
1528bf8553eaSConrad Meyer  * lock may be dropped to allocate the ring.
1529bf8553eaSConrad Meyer  *
1530bf8553eaSConrad Meyer  * (The submit_lock is needed to add any entries to the ring, so callers are
1531bf8553eaSConrad Meyer  * assured enough room is available.)
1532bf8553eaSConrad Meyer  */
1533e974f91cSConrad Meyer static int
1534bf8553eaSConrad Meyer ioat_reserve_space(struct ioat_softc *ioat, uint32_t num_descs, int mflags)
1535e974f91cSConrad Meyer {
153625ad9585SConrad Meyer 	boolean_t dug;
1537bf8553eaSConrad Meyer 	int error;
1538e974f91cSConrad Meyer 
1539bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1540bf8553eaSConrad Meyer 	error = 0;
154125ad9585SConrad Meyer 	dug = FALSE;
1542e974f91cSConrad Meyer 
1543a0992979SConrad Meyer 	if (num_descs < 1 || num_descs >= (1 << ioat->ring_size_order)) {
1544bf8553eaSConrad Meyer 		error = EINVAL;
1545bf8553eaSConrad Meyer 		goto out;
1546e974f91cSConrad Meyer 	}
154708a6b96aSConrad Meyer 
154808a6b96aSConrad Meyer 	for (;;) {
15495f77bd3eSConrad Meyer 		if (ioat->quiescing) {
15505f77bd3eSConrad Meyer 			error = ENXIO;
15515f77bd3eSConrad Meyer 			goto out;
15525f77bd3eSConrad Meyer 		}
1553bf8553eaSConrad Meyer 
1554bf8553eaSConrad Meyer 		if (ioat_get_ring_space(ioat) >= num_descs)
1555bf8553eaSConrad Meyer 			goto out;
1556bf8553eaSConrad Meyer 
155705e4cffbSConrad Meyer 		CTR3(KTR_IOAT, "%s channel=%u starved (%u)", __func__,
155805e4cffbSConrad Meyer 		    ioat->chan_idx, num_descs);
155905e4cffbSConrad Meyer 
1560a0992979SConrad Meyer 		if (!dug && !ioat->is_submitter_processing) {
156125ad9585SConrad Meyer 			ioat->is_submitter_processing = TRUE;
156225ad9585SConrad Meyer 			mtx_unlock(&ioat->submit_lock);
156325ad9585SConrad Meyer 
156405e4cffbSConrad Meyer 			CTR2(KTR_IOAT, "%s channel=%u attempting to process events",
156505e4cffbSConrad Meyer 			    __func__, ioat->chan_idx);
15662f03a95fSAlexander Motin 			ioat_process_events(ioat, FALSE);
156725ad9585SConrad Meyer 
156825ad9585SConrad Meyer 			mtx_lock(&ioat->submit_lock);
156925ad9585SConrad Meyer 			dug = TRUE;
157025ad9585SConrad Meyer 			KASSERT(ioat->is_submitter_processing == TRUE,
157125ad9585SConrad Meyer 			    ("is_submitter_processing"));
157225ad9585SConrad Meyer 			ioat->is_submitter_processing = FALSE;
157325ad9585SConrad Meyer 			wakeup(&ioat->tail);
157425ad9585SConrad Meyer 			continue;
157525ad9585SConrad Meyer 		}
157625ad9585SConrad Meyer 
1577a0992979SConrad Meyer 		if ((mflags & M_WAITOK) == 0) {
1578a0992979SConrad Meyer 			error = EAGAIN;
1579a0992979SConrad Meyer 			break;
1580a0992979SConrad Meyer 		}
158105e4cffbSConrad Meyer 		CTR2(KTR_IOAT, "%s channel=%u blocking on completions",
158205e4cffbSConrad Meyer 		    __func__, ioat->chan_idx);
1583bf8553eaSConrad Meyer 		msleep(&ioat->tail, &ioat->submit_lock, 0,
1584a0992979SConrad Meyer 		    "ioat_full", 0);
1585bf8553eaSConrad Meyer 		continue;
1586bf8553eaSConrad Meyer 	}
1587bf8553eaSConrad Meyer 
1588bf8553eaSConrad Meyer out:
1589bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
159008a6b96aSConrad Meyer 	KASSERT(!ioat->quiescing || error == ENXIO,
159108a6b96aSConrad Meyer 	    ("reserved during quiesce"));
1592bf8553eaSConrad Meyer 	return (error);
1593bf8553eaSConrad Meyer }
1594bf8553eaSConrad Meyer 
1595bf8553eaSConrad Meyer static void
1596bf8553eaSConrad Meyer ioat_free_ring(struct ioat_softc *ioat, uint32_t size,
15978e269d99SConrad Meyer     struct ioat_descriptor *ring)
1598bf8553eaSConrad Meyer {
1599bf8553eaSConrad Meyer 
160096ad26eeSMark Johnston 	free(ring, M_IOAT);
1601e974f91cSConrad Meyer }
1602e974f91cSConrad Meyer 
1603e974f91cSConrad Meyer static struct ioat_descriptor *
1604e974f91cSConrad Meyer ioat_get_ring_entry(struct ioat_softc *ioat, uint32_t index)
1605e974f91cSConrad Meyer {
1606e974f91cSConrad Meyer 
16078e269d99SConrad Meyer 	return (&ioat->ring[index % (1 << ioat->ring_size_order)]);
16088e269d99SConrad Meyer }
16098e269d99SConrad Meyer 
16108e269d99SConrad Meyer static union ioat_hw_descriptor *
16118e269d99SConrad Meyer ioat_get_descriptor(struct ioat_softc *ioat, uint32_t index)
16128e269d99SConrad Meyer {
16138e269d99SConrad Meyer 
16148e269d99SConrad Meyer 	return (&ioat->hw_desc_ring[index % (1 << ioat->ring_size_order)]);
1615e974f91cSConrad Meyer }
1616e974f91cSConrad Meyer 
1617e974f91cSConrad Meyer static void
16188f274637SConrad Meyer ioat_halted_debug(struct ioat_softc *ioat, uint32_t chanerr)
1619e974f91cSConrad Meyer {
16208e269d99SConrad Meyer 	union ioat_hw_descriptor *desc;
16218f274637SConrad Meyer 
162259acd4baSConrad Meyer 	ioat_log_message(0, "Channel halted (%b)\n", (int)chanerr,
162359acd4baSConrad Meyer 	    IOAT_CHANERR_STR);
16248f274637SConrad Meyer 	if (chanerr == 0)
16258f274637SConrad Meyer 		return;
16268f274637SConrad Meyer 
1627faefad9cSConrad Meyer 	mtx_assert(&ioat->cleanup_lock, MA_OWNED);
1628faefad9cSConrad Meyer 
16298e269d99SConrad Meyer 	desc = ioat_get_descriptor(ioat, ioat->tail + 0);
16308e269d99SConrad Meyer 	dump_descriptor(desc);
16318f274637SConrad Meyer 
16328e269d99SConrad Meyer 	desc = ioat_get_descriptor(ioat, ioat->tail + 1);
16338e269d99SConrad Meyer 	dump_descriptor(desc);
16348f274637SConrad Meyer }
16358f274637SConrad Meyer 
16368f274637SConrad Meyer static void
16375ac77963SConrad Meyer ioat_poll_timer_callback(void *arg)
16385ac77963SConrad Meyer {
16395ac77963SConrad Meyer 	struct ioat_softc *ioat;
16405ac77963SConrad Meyer 
16415ac77963SConrad Meyer 	ioat = arg;
1642630d9800SAlexander Motin 	CTR1(KTR_IOAT, "%s", __func__);
16435ac77963SConrad Meyer 
16442f03a95fSAlexander Motin 	ioat_process_events(ioat, FALSE);
16452f03a95fSAlexander Motin 
16462f03a95fSAlexander Motin 	mtx_lock(&ioat->submit_lock);
16472f03a95fSAlexander Motin 	if (ioat_get_active(ioat) > 0)
16482f03a95fSAlexander Motin 		callout_schedule(&ioat->poll_timer, 1);
16492f03a95fSAlexander Motin 	mtx_unlock(&ioat->submit_lock);
16505ac77963SConrad Meyer }
16515ac77963SConrad Meyer 
1652e974f91cSConrad Meyer /*
1653e974f91cSConrad Meyer  * Support Functions
1654e974f91cSConrad Meyer  */
1655e974f91cSConrad Meyer static void
1656e974f91cSConrad Meyer ioat_submit_single(struct ioat_softc *ioat)
1657e974f91cSConrad Meyer {
1658e974f91cSConrad Meyer 
16595a8af401SConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
16605a8af401SConrad Meyer 
16612f03a95fSAlexander Motin 	ioat->head++;
16622f03a95fSAlexander Motin 	CTR4(KTR_IOAT, "%s channel=%u head=%u tail=%u", __func__,
16632f03a95fSAlexander Motin 	    ioat->chan_idx, ioat->head, ioat->tail);
1664e974f91cSConrad Meyer 
166501fbbc88SConrad Meyer 	ioat->stats.descriptors_submitted++;
1666e974f91cSConrad Meyer }
1667e974f91cSConrad Meyer 
1668e974f91cSConrad Meyer static int
1669e974f91cSConrad Meyer ioat_reset_hw(struct ioat_softc *ioat)
1670e974f91cSConrad Meyer {
1671e974f91cSConrad Meyer 	uint64_t status;
1672e974f91cSConrad Meyer 	uint32_t chanerr;
1673cea5b880SConrad Meyer 	unsigned timeout;
16745f77bd3eSConrad Meyer 	int error;
16755f77bd3eSConrad Meyer 
1676520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
16776d41e1edSConrad Meyer 
16782f03a95fSAlexander Motin 	mtx_lock(&ioat->submit_lock);
167993f7f84aSConrad Meyer 	while (ioat->resetting && !ioat->destroying)
16802f03a95fSAlexander Motin 		msleep(&ioat->resetting, &ioat->submit_lock, 0, "IRH_drain", 0);
168193f7f84aSConrad Meyer 	if (ioat->destroying) {
16822f03a95fSAlexander Motin 		mtx_unlock(&ioat->submit_lock);
168393f7f84aSConrad Meyer 		return (ENXIO);
168493f7f84aSConrad Meyer 	}
168593f7f84aSConrad Meyer 	ioat->resetting = TRUE;
16865f77bd3eSConrad Meyer 	ioat->quiescing = TRUE;
16872f03a95fSAlexander Motin 	mtx_unlock(&ioat->submit_lock);
16882f03a95fSAlexander Motin 	mtx_lock(&ioat->cleanup_lock);
16892f03a95fSAlexander Motin 	while (ioat_get_active(ioat) > 0)
16902f03a95fSAlexander Motin 		msleep(&ioat->tail, &ioat->cleanup_lock, 0, "ioat_drain", 1);
1691e974f91cSConrad Meyer 
1692fe8712f8SConrad Meyer 	/*
1693fe8712f8SConrad Meyer 	 * Suspend ioat_process_events while the hardware and softc are in an
1694fe8712f8SConrad Meyer 	 * indeterminate state.
1695fe8712f8SConrad Meyer 	 */
1696fe8712f8SConrad Meyer 	ioat->resetting_cleanup = TRUE;
1697fe8712f8SConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
1698fe8712f8SConrad Meyer 
1699ee5642bbSConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u quiesced and drained", __func__,
1700ee5642bbSConrad Meyer 	    ioat->chan_idx);
1701ee5642bbSConrad Meyer 
1702e974f91cSConrad Meyer 	status = ioat_get_chansts(ioat);
1703e974f91cSConrad Meyer 	if (is_ioat_active(status) || is_ioat_idle(status))
1704e974f91cSConrad Meyer 		ioat_suspend(ioat);
1705e974f91cSConrad Meyer 
1706e974f91cSConrad Meyer 	/* Wait at most 20 ms */
1707e974f91cSConrad Meyer 	for (timeout = 0; (is_ioat_active(status) || is_ioat_idle(status)) &&
1708e974f91cSConrad Meyer 	    timeout < 20; timeout++) {
1709e974f91cSConrad Meyer 		DELAY(1000);
1710e974f91cSConrad Meyer 		status = ioat_get_chansts(ioat);
1711e974f91cSConrad Meyer 	}
17125f77bd3eSConrad Meyer 	if (timeout == 20) {
17135f77bd3eSConrad Meyer 		error = ETIMEDOUT;
17145f77bd3eSConrad Meyer 		goto out;
17155f77bd3eSConrad Meyer 	}
1716e974f91cSConrad Meyer 
1717cea5b880SConrad Meyer 	KASSERT(ioat_get_active(ioat) == 0, ("active after quiesce"));
1718cea5b880SConrad Meyer 
1719e974f91cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
1720e974f91cSConrad Meyer 	ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
1721e974f91cSConrad Meyer 
1722ee5642bbSConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u hardware suspended", __func__,
1723ee5642bbSConrad Meyer 	    ioat->chan_idx);
1724ee5642bbSConrad Meyer 
1725e974f91cSConrad Meyer 	/*
1726e974f91cSConrad Meyer 	 * IOAT v3 workaround - CHANERRMSK_INT with 3E07h to masks out errors
1727e974f91cSConrad Meyer 	 *  that can cause stability issues for IOAT v3.
1728e974f91cSConrad Meyer 	 */
1729e974f91cSConrad Meyer 	pci_write_config(ioat->device, IOAT_CFG_CHANERRMASK_INT_OFFSET, 0x3e07,
1730e974f91cSConrad Meyer 	    4);
1731e974f91cSConrad Meyer 	chanerr = pci_read_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, 4);
1732e974f91cSConrad Meyer 	pci_write_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, chanerr, 4);
1733e974f91cSConrad Meyer 
17340d1a05d9SConrad Meyer 	/*
17350d1a05d9SConrad Meyer 	 * BDXDE and BWD models reset MSI-X registers on device reset.
17360d1a05d9SConrad Meyer 	 * Save/restore their contents manually.
17370d1a05d9SConrad Meyer 	 */
1738f7157235SConrad Meyer 	if (ioat_model_resets_msix(ioat)) {
1739f7157235SConrad Meyer 		ioat_log_message(1, "device resets MSI-X registers; saving\n");
17400d1a05d9SConrad Meyer 		pci_save_state(ioat->device);
1741f7157235SConrad Meyer 	}
17420d1a05d9SConrad Meyer 
1743e974f91cSConrad Meyer 	ioat_reset(ioat);
1744ee5642bbSConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u hardware reset", __func__,
1745ee5642bbSConrad Meyer 	    ioat->chan_idx);
1746e974f91cSConrad Meyer 
1747e974f91cSConrad Meyer 	/* Wait at most 20 ms */
1748e974f91cSConrad Meyer 	for (timeout = 0; ioat_reset_pending(ioat) && timeout < 20; timeout++)
1749e974f91cSConrad Meyer 		DELAY(1000);
17505f77bd3eSConrad Meyer 	if (timeout == 20) {
17515f77bd3eSConrad Meyer 		error = ETIMEDOUT;
17525f77bd3eSConrad Meyer 		goto out;
17535f77bd3eSConrad Meyer 	}
1754e974f91cSConrad Meyer 
1755f7157235SConrad Meyer 	if (ioat_model_resets_msix(ioat)) {
1756f7157235SConrad Meyer 		ioat_log_message(1, "device resets registers; restored\n");
17570d1a05d9SConrad Meyer 		pci_restore_state(ioat->device);
1758f7157235SConrad Meyer 	}
17594253ea50SConrad Meyer 
1760cea5b880SConrad Meyer 	/* Reset attempts to return the hardware to "halted." */
1761cea5b880SConrad Meyer 	status = ioat_get_chansts(ioat);
1762cea5b880SConrad Meyer 	if (is_ioat_active(status) || is_ioat_idle(status)) {
1763cea5b880SConrad Meyer 		/* So this really shouldn't happen... */
1764cea5b880SConrad Meyer 		ioat_log_message(0, "Device is active after a reset?\n");
1765cea5b880SConrad Meyer 		ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
17665f77bd3eSConrad Meyer 		error = 0;
17675f77bd3eSConrad Meyer 		goto out;
1768e974f91cSConrad Meyer 	}
1769e974f91cSConrad Meyer 
1770cea5b880SConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
17715f77bd3eSConrad Meyer 	if (chanerr != 0) {
1772faefad9cSConrad Meyer 		mtx_lock(&ioat->cleanup_lock);
1773faefad9cSConrad Meyer 		ioat_halted_debug(ioat, chanerr);
1774faefad9cSConrad Meyer 		mtx_unlock(&ioat->cleanup_lock);
17755f77bd3eSConrad Meyer 		error = EIO;
17765f77bd3eSConrad Meyer 		goto out;
17775f77bd3eSConrad Meyer 	}
1778cea5b880SConrad Meyer 
1779cea5b880SConrad Meyer 	/*
1780cea5b880SConrad Meyer 	 * Bring device back online after reset.  Writing CHAINADDR brings the
1781cea5b880SConrad Meyer 	 * device back to active.
1782cea5b880SConrad Meyer 	 *
1783cea5b880SConrad Meyer 	 * The internal ring counter resets to zero, so we have to start over
1784cea5b880SConrad Meyer 	 * at zero as well.
1785cea5b880SConrad Meyer 	 */
17862f03a95fSAlexander Motin 	ioat->tail = ioat->head = 0;
1787348efb14SAlexander Motin 	*ioat->comp_update = ioat->last_seen =
1788348efb14SAlexander Motin 	    RING_PHYS_ADDR(ioat, ioat->tail - 1);
1789cea5b880SConrad Meyer 
1790cea5b880SConrad Meyer 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
1791cea5b880SConrad Meyer 	ioat_write_chancmp(ioat, ioat->comp_update_bus_addr);
17928e269d99SConrad Meyer 	ioat_write_chainaddr(ioat, RING_PHYS_ADDR(ioat, 0));
17935f77bd3eSConrad Meyer 	error = 0;
1794ee5642bbSConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u configured channel", __func__,
1795ee5642bbSConrad Meyer 	    ioat->chan_idx);
17965f77bd3eSConrad Meyer 
17975f77bd3eSConrad Meyer out:
17981bbc06b8SConrad Meyer 	/* Enqueues a null operation and ensures it completes. */
1799ee5642bbSConrad Meyer 	if (error == 0) {
18001bbc06b8SConrad Meyer 		error = ioat_start_channel(ioat);
1801ee5642bbSConrad Meyer 		CTR2(KTR_IOAT, "%s channel=%u started channel", __func__,
1802ee5642bbSConrad Meyer 		    ioat->chan_idx);
1803ee5642bbSConrad Meyer 	}
18041bbc06b8SConrad Meyer 
1805fe8712f8SConrad Meyer 	/*
1806fe8712f8SConrad Meyer 	 * Resume completions now that ring state is consistent.
1807fe8712f8SConrad Meyer 	 */
1808fe8712f8SConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
1809fe8712f8SConrad Meyer 	ioat->resetting_cleanup = FALSE;
1810fe8712f8SConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
181193f7f84aSConrad Meyer 
1812fe8712f8SConrad Meyer 	/* Unblock submission of new work */
18132f03a95fSAlexander Motin 	mtx_lock(&ioat->submit_lock);
1814fe8712f8SConrad Meyer 	ioat->quiescing = FALSE;
1815fe8712f8SConrad Meyer 	wakeup(&ioat->quiescing);
1816fe8712f8SConrad Meyer 
1817fe8712f8SConrad Meyer 	ioat->resetting = FALSE;
1818fe8712f8SConrad Meyer 	wakeup(&ioat->resetting);
18191bbc06b8SConrad Meyer 
1820ee5642bbSConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u reset done", __func__, ioat->chan_idx);
18212f03a95fSAlexander Motin 	mtx_unlock(&ioat->submit_lock);
1822fe8712f8SConrad Meyer 
18235f77bd3eSConrad Meyer 	return (error);
1824cea5b880SConrad Meyer }
1825cea5b880SConrad Meyer 
1826f7157235SConrad Meyer static int
1827faefad9cSConrad Meyer sysctl_handle_chansts(SYSCTL_HANDLER_ARGS)
1828faefad9cSConrad Meyer {
1829faefad9cSConrad Meyer 	struct ioat_softc *ioat;
1830faefad9cSConrad Meyer 	struct sbuf sb;
1831faefad9cSConrad Meyer 	uint64_t status;
1832faefad9cSConrad Meyer 	int error;
1833faefad9cSConrad Meyer 
1834faefad9cSConrad Meyer 	ioat = arg1;
1835faefad9cSConrad Meyer 
1836faefad9cSConrad Meyer 	status = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS;
1837faefad9cSConrad Meyer 
1838faefad9cSConrad Meyer 	sbuf_new_for_sysctl(&sb, NULL, 256, req);
1839faefad9cSConrad Meyer 	switch (status) {
1840faefad9cSConrad Meyer 	case IOAT_CHANSTS_ACTIVE:
1841faefad9cSConrad Meyer 		sbuf_printf(&sb, "ACTIVE");
1842faefad9cSConrad Meyer 		break;
1843faefad9cSConrad Meyer 	case IOAT_CHANSTS_IDLE:
1844faefad9cSConrad Meyer 		sbuf_printf(&sb, "IDLE");
1845faefad9cSConrad Meyer 		break;
1846faefad9cSConrad Meyer 	case IOAT_CHANSTS_SUSPENDED:
1847faefad9cSConrad Meyer 		sbuf_printf(&sb, "SUSPENDED");
1848faefad9cSConrad Meyer 		break;
1849faefad9cSConrad Meyer 	case IOAT_CHANSTS_HALTED:
1850faefad9cSConrad Meyer 		sbuf_printf(&sb, "HALTED");
1851faefad9cSConrad Meyer 		break;
1852faefad9cSConrad Meyer 	case IOAT_CHANSTS_ARMED:
1853faefad9cSConrad Meyer 		sbuf_printf(&sb, "ARMED");
1854faefad9cSConrad Meyer 		break;
1855faefad9cSConrad Meyer 	default:
1856faefad9cSConrad Meyer 		sbuf_printf(&sb, "UNKNOWN");
1857faefad9cSConrad Meyer 		break;
1858faefad9cSConrad Meyer 	}
1859faefad9cSConrad Meyer 	error = sbuf_finish(&sb);
1860faefad9cSConrad Meyer 	sbuf_delete(&sb);
1861faefad9cSConrad Meyer 
1862faefad9cSConrad Meyer 	if (error != 0 || req->newptr == NULL)
1863faefad9cSConrad Meyer 		return (error);
1864faefad9cSConrad Meyer 	return (EINVAL);
1865faefad9cSConrad Meyer }
1866faefad9cSConrad Meyer 
1867faefad9cSConrad Meyer static int
186801fbbc88SConrad Meyer sysctl_handle_dpi(SYSCTL_HANDLER_ARGS)
186901fbbc88SConrad Meyer {
187001fbbc88SConrad Meyer 	struct ioat_softc *ioat;
187101fbbc88SConrad Meyer 	struct sbuf sb;
187201fbbc88SConrad Meyer #define	PRECISION	"1"
187301fbbc88SConrad Meyer 	const uintmax_t factor = 10;
187401fbbc88SConrad Meyer 	uintmax_t rate;
187501fbbc88SConrad Meyer 	int error;
187601fbbc88SConrad Meyer 
187701fbbc88SConrad Meyer 	ioat = arg1;
187801fbbc88SConrad Meyer 	sbuf_new_for_sysctl(&sb, NULL, 16, req);
187901fbbc88SConrad Meyer 
188001fbbc88SConrad Meyer 	if (ioat->stats.interrupts == 0) {
188101fbbc88SConrad Meyer 		sbuf_printf(&sb, "NaN");
188201fbbc88SConrad Meyer 		goto out;
188301fbbc88SConrad Meyer 	}
188401fbbc88SConrad Meyer 	rate = ioat->stats.descriptors_processed * factor /
188501fbbc88SConrad Meyer 	    ioat->stats.interrupts;
188601fbbc88SConrad Meyer 	sbuf_printf(&sb, "%ju.%." PRECISION "ju", rate / factor,
188701fbbc88SConrad Meyer 	    rate % factor);
188801fbbc88SConrad Meyer #undef	PRECISION
188901fbbc88SConrad Meyer out:
189001fbbc88SConrad Meyer 	error = sbuf_finish(&sb);
189101fbbc88SConrad Meyer 	sbuf_delete(&sb);
189201fbbc88SConrad Meyer 	if (error != 0 || req->newptr == NULL)
189301fbbc88SConrad Meyer 		return (error);
189401fbbc88SConrad Meyer 	return (EINVAL);
189501fbbc88SConrad Meyer }
189601fbbc88SConrad Meyer 
189701fbbc88SConrad Meyer static int
1898f7157235SConrad Meyer sysctl_handle_reset(SYSCTL_HANDLER_ARGS)
1899f7157235SConrad Meyer {
1900f7157235SConrad Meyer 	struct ioat_softc *ioat;
1901f7157235SConrad Meyer 	int error, arg;
1902f7157235SConrad Meyer 
1903f7157235SConrad Meyer 	ioat = arg1;
1904f7157235SConrad Meyer 
1905f7157235SConrad Meyer 	arg = 0;
1906f7157235SConrad Meyer 	error = SYSCTL_OUT(req, &arg, sizeof(arg));
1907f7157235SConrad Meyer 	if (error != 0 || req->newptr == NULL)
1908f7157235SConrad Meyer 		return (error);
1909f7157235SConrad Meyer 
1910f7157235SConrad Meyer 	error = SYSCTL_IN(req, &arg, sizeof(arg));
1911f7157235SConrad Meyer 	if (error != 0)
1912f7157235SConrad Meyer 		return (error);
1913f7157235SConrad Meyer 
1914f7157235SConrad Meyer 	if (arg != 0)
1915f7157235SConrad Meyer 		error = ioat_reset_hw(ioat);
1916f7157235SConrad Meyer 
1917f7157235SConrad Meyer 	return (error);
1918f7157235SConrad Meyer }
1919f7157235SConrad Meyer 
1920e974f91cSConrad Meyer static void
1921e974f91cSConrad Meyer dump_descriptor(void *hw_desc)
1922e974f91cSConrad Meyer {
1923e974f91cSConrad Meyer 	int i, j;
1924e974f91cSConrad Meyer 
1925e974f91cSConrad Meyer 	for (i = 0; i < 2; i++) {
1926e974f91cSConrad Meyer 		for (j = 0; j < 8; j++)
1927e974f91cSConrad Meyer 			printf("%08x ", ((uint32_t *)hw_desc)[i * 8 + j]);
1928e974f91cSConrad Meyer 		printf("\n");
1929e974f91cSConrad Meyer 	}
1930e974f91cSConrad Meyer }
1931e974f91cSConrad Meyer 
1932e974f91cSConrad Meyer static void
1933e974f91cSConrad Meyer ioat_setup_sysctl(device_t device)
1934e974f91cSConrad Meyer {
193501fbbc88SConrad Meyer 	struct sysctl_oid_list *par, *statpar, *state, *hammer;
1936f7157235SConrad Meyer 	struct sysctl_ctx_list *ctx;
193701fbbc88SConrad Meyer 	struct sysctl_oid *tree, *tmp;
1938e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1939e974f91cSConrad Meyer 
1940e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
1941f7157235SConrad Meyer 	ctx = device_get_sysctl_ctx(device);
1942f7157235SConrad Meyer 	tree = device_get_sysctl_tree(device);
1943f7157235SConrad Meyer 	par = SYSCTL_CHILDREN(tree);
1944e974f91cSConrad Meyer 
194565e4f8adSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "version", CTLFLAG_RD,
194665e4f8adSConrad Meyer 	    &ioat->version, 0, "HW version (0xMM form)");
194765e4f8adSConrad Meyer 	SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "max_xfer_size", CTLFLAG_RD,
194865e4f8adSConrad Meyer 	    &ioat->max_xfer_size, 0, "HW maximum transfer size");
19495ca9fc2aSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "intrdelay_supported", CTLFLAG_RD,
19505ca9fc2aSConrad Meyer 	    &ioat->intrdelay_supported, 0, "Is INTRDELAY supported");
19515ca9fc2aSConrad Meyer 	SYSCTL_ADD_U16(ctx, par, OID_AUTO, "intrdelay_max", CTLFLAG_RD,
19525ca9fc2aSConrad Meyer 	    &ioat->intrdelay_max, 0,
19535ca9fc2aSConrad Meyer 	    "Maximum configurable INTRDELAY on this channel (microseconds)");
195465e4f8adSConrad Meyer 
19557029da5cSPawel Biernacki 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "state",
19567029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "IOAT channel internal state");
195701fbbc88SConrad Meyer 	state = SYSCTL_CHILDREN(tmp);
195801fbbc88SConrad Meyer 
195901fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "ring_size_order", CTLFLAG_RD,
1960bf8553eaSConrad Meyer 	    &ioat->ring_size_order, 0, "SW descriptor ring size order");
196101fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "head", CTLFLAG_RD, &ioat->head,
196201fbbc88SConrad Meyer 	    0, "SW descriptor head pointer index");
196301fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "tail", CTLFLAG_RD, &ioat->tail,
196401fbbc88SConrad Meyer 	    0, "SW descriptor tail pointer index");
1965f7157235SConrad Meyer 
196601fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, state, OID_AUTO, "last_completion", CTLFLAG_RD,
196765e4f8adSConrad Meyer 	    ioat->comp_update, "HW addr of last completion");
196865e4f8adSConrad Meyer 
196925ad9585SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_submitter_processing",
197025ad9585SConrad Meyer 	    CTLFLAG_RD, &ioat->is_submitter_processing, 0,
197125ad9585SConrad Meyer 	    "submitter processing");
197265e4f8adSConrad Meyer 
197301fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, state, OID_AUTO, "chansts",
1974b776de67SAlexander Motin 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, ioat, 0,
19757029da5cSPawel Biernacki 	    sysctl_handle_chansts, "A", "String of the channel status");
197601fbbc88SConrad Meyer 
19775ca9fc2aSConrad Meyer 	SYSCTL_ADD_U16(ctx, state, OID_AUTO, "intrdelay", CTLFLAG_RD,
19785ca9fc2aSConrad Meyer 	    &ioat->cached_intrdelay, 0,
19795ca9fc2aSConrad Meyer 	    "Current INTRDELAY on this channel (cached, microseconds)");
19805ca9fc2aSConrad Meyer 
19817029da5cSPawel Biernacki 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "hammer",
19827029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
198301fbbc88SConrad Meyer 	    "Big hammers (mostly for testing)");
198401fbbc88SConrad Meyer 	hammer = SYSCTL_CHILDREN(tmp);
198501fbbc88SConrad Meyer 
198601fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, hammer, OID_AUTO, "force_hw_reset",
1987b776de67SAlexander Motin 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, ioat, 0,
19887029da5cSPawel Biernacki 	    sysctl_handle_reset, "I", "Set to non-zero to reset the hardware");
198901fbbc88SConrad Meyer 
19907029da5cSPawel Biernacki 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "stats",
19917029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "IOAT channel statistics");
199201fbbc88SConrad Meyer 	statpar = SYSCTL_CHILDREN(tmp);
199301fbbc88SConrad Meyer 
199483b82cd4SEric van Gyzen 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "interrupts",
199583b82cd4SEric van Gyzen 	    CTLFLAG_RW | CTLFLAG_STATS, &ioat->stats.interrupts,
199601fbbc88SConrad Meyer 	    "Number of interrupts processed on this channel");
199783b82cd4SEric van Gyzen 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "descriptors",
199883b82cd4SEric van Gyzen 	    CTLFLAG_RW | CTLFLAG_STATS, &ioat->stats.descriptors_processed,
199901fbbc88SConrad Meyer 	    "Number of descriptors processed on this channel");
200083b82cd4SEric van Gyzen 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "submitted",
200183b82cd4SEric van Gyzen 	    CTLFLAG_RW | CTLFLAG_STATS, &ioat->stats.descriptors_submitted,
200201fbbc88SConrad Meyer 	    "Number of descriptors submitted to this channel");
200383b82cd4SEric van Gyzen 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "errored",
200483b82cd4SEric van Gyzen 	    CTLFLAG_RW | CTLFLAG_STATS, &ioat->stats.descriptors_error,
200501fbbc88SConrad Meyer 	    "Number of descriptors failed by channel errors");
200683b82cd4SEric van Gyzen 	SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "halts",
200783b82cd4SEric van Gyzen 	    CTLFLAG_RW | CTLFLAG_STATS, &ioat->stats.channel_halts, 0,
200801fbbc88SConrad Meyer 	    "Number of times the channel has halted");
200983b82cd4SEric van Gyzen 	SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "last_halt_chanerr",
201083b82cd4SEric van Gyzen 	    CTLFLAG_RW | CTLFLAG_STATS, &ioat->stats.last_halt_chanerr, 0,
201101fbbc88SConrad Meyer 	    "The raw CHANERR when the channel was last halted");
201201fbbc88SConrad Meyer 
201301fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, statpar, OID_AUTO, "desc_per_interrupt",
2014b776de67SAlexander Motin 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, ioat, 0,
20157029da5cSPawel Biernacki 	    sysctl_handle_dpi, "A", "Descriptors per interrupt");
2016e974f91cSConrad Meyer }
2017466b3540SConrad Meyer 
20182f03a95fSAlexander Motin static void
20192f03a95fSAlexander Motin ioat_get(struct ioat_softc *ioat)
2020466b3540SConrad Meyer {
2021faefad9cSConrad Meyer 
20222f03a95fSAlexander Motin 	mtx_assert(&ioat->submit_lock, MA_OWNED);
20232f03a95fSAlexander Motin 	KASSERT(ioat->refcnt < UINT32_MAX, ("refcnt overflow"));
20242f03a95fSAlexander Motin 
20252f03a95fSAlexander Motin 	ioat->refcnt++;
2026faefad9cSConrad Meyer }
2027faefad9cSConrad Meyer 
20282f03a95fSAlexander Motin static void
20292f03a95fSAlexander Motin ioat_put(struct ioat_softc *ioat)
2030faefad9cSConrad Meyer {
2031faefad9cSConrad Meyer 
20322f03a95fSAlexander Motin 	mtx_assert(&ioat->submit_lock, MA_OWNED);
20332f03a95fSAlexander Motin 	KASSERT(ioat->refcnt >= 1, ("refcnt error"));
2034faefad9cSConrad Meyer 
20352f03a95fSAlexander Motin 	if (--ioat->refcnt == 0)
20362f03a95fSAlexander Motin 		wakeup(&ioat->refcnt);
2037466b3540SConrad Meyer }
2038466b3540SConrad Meyer 
2039466b3540SConrad Meyer static void
20405f77bd3eSConrad Meyer ioat_drain_locked(struct ioat_softc *ioat)
2041466b3540SConrad Meyer {
2042466b3540SConrad Meyer 
20432f03a95fSAlexander Motin 	mtx_assert(&ioat->submit_lock, MA_OWNED);
20442f03a95fSAlexander Motin 
2045466b3540SConrad Meyer 	while (ioat->refcnt > 0)
20462f03a95fSAlexander Motin 		msleep(&ioat->refcnt, &ioat->submit_lock, 0, "ioat_drain", 0);
2047466b3540SConrad Meyer }
20480bb35debSConrad Meyer 
20490bb35debSConrad Meyer #ifdef DDB
20500bb35debSConrad Meyer #define	_db_show_lock(lo)	LOCK_CLASS(lo)->lc_ddb_show(lo)
20510bb35debSConrad Meyer #define	db_show_lock(lk)	_db_show_lock(&(lk)->lock_object)
20520bb35debSConrad Meyer DB_SHOW_COMMAND(ioat, db_show_ioat)
20530bb35debSConrad Meyer {
20540bb35debSConrad Meyer 	struct ioat_softc *sc;
20550bb35debSConrad Meyer 	unsigned idx;
20560bb35debSConrad Meyer 
20570bb35debSConrad Meyer 	if (!have_addr)
20580bb35debSConrad Meyer 		goto usage;
20590bb35debSConrad Meyer 	idx = (unsigned)addr;
206035b7a45eSConrad Meyer 	if (idx >= ioat_channel_index)
20610bb35debSConrad Meyer 		goto usage;
20620bb35debSConrad Meyer 
20630bb35debSConrad Meyer 	sc = ioat_channel[idx];
20640bb35debSConrad Meyer 	db_printf("ioat softc at %p\n", sc);
20650bb35debSConrad Meyer 	if (sc == NULL)
20660bb35debSConrad Meyer 		return;
20670bb35debSConrad Meyer 
20680bb35debSConrad Meyer 	db_printf(" version: %d\n", sc->version);
20690bb35debSConrad Meyer 	db_printf(" chan_idx: %u\n", sc->chan_idx);
20700bb35debSConrad Meyer 	db_printf(" submit_lock: ");
20710bb35debSConrad Meyer 	db_show_lock(&sc->submit_lock);
20720bb35debSConrad Meyer 
20730bb35debSConrad Meyer 	db_printf(" capabilities: %b\n", (int)sc->capabilities,
20740bb35debSConrad Meyer 	    IOAT_DMACAP_STR);
20750bb35debSConrad Meyer 	db_printf(" cached_intrdelay: %u\n", sc->cached_intrdelay);
20760bb35debSConrad Meyer 	db_printf(" *comp_update: 0x%jx\n", (uintmax_t)*sc->comp_update);
20770bb35debSConrad Meyer 
20785ac77963SConrad Meyer 	db_printf(" poll_timer:\n");
20795ac77963SConrad Meyer 	db_printf("  c_time: %ju\n", (uintmax_t)sc->poll_timer.c_time);
20805ac77963SConrad Meyer 	db_printf("  c_arg: %p\n", sc->poll_timer.c_arg);
20815ac77963SConrad Meyer 	db_printf("  c_func: %p\n", sc->poll_timer.c_func);
20825ac77963SConrad Meyer 	db_printf("  c_lock: %p\n", sc->poll_timer.c_lock);
20835ac77963SConrad Meyer 	db_printf("  c_flags: 0x%x\n", (unsigned)sc->poll_timer.c_flags);
20845ac77963SConrad Meyer 
20850bb35debSConrad Meyer 	db_printf(" quiescing: %d\n", (int)sc->quiescing);
20860bb35debSConrad Meyer 	db_printf(" destroying: %d\n", (int)sc->destroying);
208725ad9585SConrad Meyer 	db_printf(" is_submitter_processing: %d\n",
208825ad9585SConrad Meyer 	    (int)sc->is_submitter_processing);
20890bb35debSConrad Meyer 	db_printf(" intrdelay_supported: %d\n", (int)sc->intrdelay_supported);
209093f7f84aSConrad Meyer 	db_printf(" resetting: %d\n", (int)sc->resetting);
20910bb35debSConrad Meyer 
20920bb35debSConrad Meyer 	db_printf(" head: %u\n", sc->head);
20930bb35debSConrad Meyer 	db_printf(" tail: %u\n", sc->tail);
20940bb35debSConrad Meyer 	db_printf(" ring_size_order: %u\n", sc->ring_size_order);
20950bb35debSConrad Meyer 	db_printf(" last_seen: 0x%lx\n", sc->last_seen);
20960bb35debSConrad Meyer 	db_printf(" ring: %p\n", sc->ring);
20978e269d99SConrad Meyer 	db_printf(" descriptors: %p\n", sc->hw_desc_ring);
20988e269d99SConrad Meyer 	db_printf(" descriptors (phys): 0x%jx\n",
20998e269d99SConrad Meyer 	    (uintmax_t)sc->hw_desc_bus_addr);
21000bb35debSConrad Meyer 
210135b7a45eSConrad Meyer 	db_printf("  ring[%u] (tail):\n", sc->tail %
210235b7a45eSConrad Meyer 	    (1 << sc->ring_size_order));
210335b7a45eSConrad Meyer 	db_printf("   id: %u\n", ioat_get_ring_entry(sc, sc->tail)->id);
210435b7a45eSConrad Meyer 	db_printf("   addr: 0x%lx\n",
21058e269d99SConrad Meyer 	    RING_PHYS_ADDR(sc, sc->tail));
210635b7a45eSConrad Meyer 	db_printf("   next: 0x%lx\n",
21078e269d99SConrad Meyer 	     ioat_get_descriptor(sc, sc->tail)->generic.next);
210835b7a45eSConrad Meyer 
210935b7a45eSConrad Meyer 	db_printf("  ring[%u] (head - 1):\n", (sc->head - 1) %
211035b7a45eSConrad Meyer 	    (1 << sc->ring_size_order));
211135b7a45eSConrad Meyer 	db_printf("   id: %u\n", ioat_get_ring_entry(sc, sc->head - 1)->id);
211235b7a45eSConrad Meyer 	db_printf("   addr: 0x%lx\n",
21138e269d99SConrad Meyer 	    RING_PHYS_ADDR(sc, sc->head - 1));
211435b7a45eSConrad Meyer 	db_printf("   next: 0x%lx\n",
21158e269d99SConrad Meyer 	     ioat_get_descriptor(sc, sc->head - 1)->generic.next);
211635b7a45eSConrad Meyer 
211735b7a45eSConrad Meyer 	db_printf("  ring[%u] (head):\n", (sc->head) %
211835b7a45eSConrad Meyer 	    (1 << sc->ring_size_order));
211935b7a45eSConrad Meyer 	db_printf("   id: %u\n", ioat_get_ring_entry(sc, sc->head)->id);
212035b7a45eSConrad Meyer 	db_printf("   addr: 0x%lx\n",
21218e269d99SConrad Meyer 	    RING_PHYS_ADDR(sc, sc->head));
212235b7a45eSConrad Meyer 	db_printf("   next: 0x%lx\n",
21238e269d99SConrad Meyer 	     ioat_get_descriptor(sc, sc->head)->generic.next);
212435b7a45eSConrad Meyer 
212535b7a45eSConrad Meyer 	for (idx = 0; idx < (1 << sc->ring_size_order); idx++)
212635b7a45eSConrad Meyer 		if ((*sc->comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK)
21278e269d99SConrad Meyer 		    == RING_PHYS_ADDR(sc, idx))
212835b7a45eSConrad Meyer 			db_printf("  ring[%u] == hardware tail\n", idx);
212935b7a45eSConrad Meyer 
21300bb35debSConrad Meyer 	db_printf(" cleanup_lock: ");
21310bb35debSConrad Meyer 	db_show_lock(&sc->cleanup_lock);
21320bb35debSConrad Meyer 
21330bb35debSConrad Meyer 	db_printf(" refcnt: %u\n", sc->refcnt);
21340bb35debSConrad Meyer 	db_printf(" stats:\n");
21350bb35debSConrad Meyer 	db_printf("  interrupts: %lu\n", sc->stats.interrupts);
21360bb35debSConrad Meyer 	db_printf("  descriptors_processed: %lu\n", sc->stats.descriptors_processed);
21370bb35debSConrad Meyer 	db_printf("  descriptors_error: %lu\n", sc->stats.descriptors_error);
21380bb35debSConrad Meyer 	db_printf("  descriptors_submitted: %lu\n", sc->stats.descriptors_submitted);
21390bb35debSConrad Meyer 
21400bb35debSConrad Meyer 	db_printf("  channel_halts: %u\n", sc->stats.channel_halts);
21410bb35debSConrad Meyer 	db_printf("  last_halt_chanerr: %u\n", sc->stats.last_halt_chanerr);
21420bb35debSConrad Meyer 
21430bb35debSConrad Meyer 	if (db_pager_quit)
21440bb35debSConrad Meyer 		return;
21450bb35debSConrad Meyer 
21460bb35debSConrad Meyer 	db_printf(" hw status:\n");
21470bb35debSConrad Meyer 	db_printf("  status: 0x%lx\n", ioat_get_chansts(sc));
21480bb35debSConrad Meyer 	db_printf("  chanctrl: 0x%x\n",
21490bb35debSConrad Meyer 	    (unsigned)ioat_read_2(sc, IOAT_CHANCTRL_OFFSET));
21500bb35debSConrad Meyer 	db_printf("  chancmd: 0x%x\n",
21510bb35debSConrad Meyer 	    (unsigned)ioat_read_1(sc, IOAT_CHANCMD_OFFSET));
21520bb35debSConrad Meyer 	db_printf("  dmacount: 0x%x\n",
21530bb35debSConrad Meyer 	    (unsigned)ioat_read_2(sc, IOAT_DMACOUNT_OFFSET));
21540bb35debSConrad Meyer 	db_printf("  chainaddr: 0x%lx\n",
21550bb35debSConrad Meyer 	    ioat_read_double_4(sc, IOAT_CHAINADDR_OFFSET_LOW));
21560bb35debSConrad Meyer 	db_printf("  chancmp: 0x%lx\n",
21570bb35debSConrad Meyer 	    ioat_read_double_4(sc, IOAT_CHANCMP_OFFSET_LOW));
21580bb35debSConrad Meyer 	db_printf("  chanerr: %b\n",
21590bb35debSConrad Meyer 	    (int)ioat_read_4(sc, IOAT_CHANERR_OFFSET), IOAT_CHANERR_STR);
21600bb35debSConrad Meyer 	return;
21610bb35debSConrad Meyer usage:
21620bb35debSConrad Meyer 	db_printf("usage: show ioat <0-%u>\n", ioat_channel_index);
21630bb35debSConrad Meyer 	return;
21640bb35debSConrad Meyer }
21650bb35debSConrad Meyer #endif /* DDB */
2166