xref: /freebsd/sys/dev/ioat/ioat.c (revision 71bf3900b7827643830dfd9aa1db81bc2926eadb)
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 
73*71bf3900SAlexander 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 
150e974f91cSConrad Meyer static devclass_t ioat_devclass;
151*71bf3900SAlexander Motin DRIVER_MODULE(ioat, pci, ioat_pci_driver, ioat_devclass, ioat_modevent, NULL);
152c2b69205SConrad Meyer MODULE_VERSION(ioat, 1);
153e974f91cSConrad Meyer 
154e974f91cSConrad Meyer /*
155e974f91cSConrad Meyer  * Private data structures
156e974f91cSConrad Meyer  */
157e974f91cSConrad Meyer static struct ioat_softc *ioat_channel[IOAT_MAX_CHANNELS];
158df1928aaSConrad Meyer static unsigned ioat_channel_index = 0;
159df1928aaSConrad Meyer SYSCTL_UINT(_hw_ioat, OID_AUTO, channels, CTLFLAG_RD, &ioat_channel_index, 0,
160e974f91cSConrad Meyer     "Number of IOAT channels attached");
1612f03a95fSAlexander Motin static struct mtx ioat_list_mtx;
1622f03a95fSAlexander Motin MTX_SYSINIT(ioat_list_mtx, &ioat_list_mtx, "ioat list mtx", MTX_DEF);
163e974f91cSConrad Meyer 
164e974f91cSConrad Meyer static struct _pcsid
165e974f91cSConrad Meyer {
166e974f91cSConrad Meyer 	u_int32_t   type;
167e974f91cSConrad Meyer 	const char  *desc;
168e974f91cSConrad Meyer } pci_ids[] = {
169e974f91cSConrad Meyer 	{ 0x34308086, "TBG IOAT Ch0" },
170e974f91cSConrad Meyer 	{ 0x34318086, "TBG IOAT Ch1" },
171e974f91cSConrad Meyer 	{ 0x34328086, "TBG IOAT Ch2" },
172e974f91cSConrad Meyer 	{ 0x34338086, "TBG IOAT Ch3" },
173e974f91cSConrad Meyer 	{ 0x34298086, "TBG IOAT Ch4" },
174e974f91cSConrad Meyer 	{ 0x342a8086, "TBG IOAT Ch5" },
175e974f91cSConrad Meyer 	{ 0x342b8086, "TBG IOAT Ch6" },
176e974f91cSConrad Meyer 	{ 0x342c8086, "TBG IOAT Ch7" },
177e974f91cSConrad Meyer 
178e974f91cSConrad Meyer 	{ 0x37108086, "JSF IOAT Ch0" },
179e974f91cSConrad Meyer 	{ 0x37118086, "JSF IOAT Ch1" },
180e974f91cSConrad Meyer 	{ 0x37128086, "JSF IOAT Ch2" },
181e974f91cSConrad Meyer 	{ 0x37138086, "JSF IOAT Ch3" },
182e974f91cSConrad Meyer 	{ 0x37148086, "JSF IOAT Ch4" },
183e974f91cSConrad Meyer 	{ 0x37158086, "JSF IOAT Ch5" },
184e974f91cSConrad Meyer 	{ 0x37168086, "JSF IOAT Ch6" },
185e974f91cSConrad Meyer 	{ 0x37178086, "JSF IOAT Ch7" },
186e974f91cSConrad Meyer 	{ 0x37188086, "JSF IOAT Ch0 (RAID)" },
187e974f91cSConrad Meyer 	{ 0x37198086, "JSF IOAT Ch1 (RAID)" },
188e974f91cSConrad Meyer 
189e974f91cSConrad Meyer 	{ 0x3c208086, "SNB IOAT Ch0" },
190e974f91cSConrad Meyer 	{ 0x3c218086, "SNB IOAT Ch1" },
191e974f91cSConrad Meyer 	{ 0x3c228086, "SNB IOAT Ch2" },
192e974f91cSConrad Meyer 	{ 0x3c238086, "SNB IOAT Ch3" },
193e974f91cSConrad Meyer 	{ 0x3c248086, "SNB IOAT Ch4" },
194e974f91cSConrad Meyer 	{ 0x3c258086, "SNB IOAT Ch5" },
195e974f91cSConrad Meyer 	{ 0x3c268086, "SNB IOAT Ch6" },
196e974f91cSConrad Meyer 	{ 0x3c278086, "SNB IOAT Ch7" },
197e974f91cSConrad Meyer 	{ 0x3c2e8086, "SNB IOAT Ch0 (RAID)" },
198e974f91cSConrad Meyer 	{ 0x3c2f8086, "SNB IOAT Ch1 (RAID)" },
199e974f91cSConrad Meyer 
200e974f91cSConrad Meyer 	{ 0x0e208086, "IVB IOAT Ch0" },
201e974f91cSConrad Meyer 	{ 0x0e218086, "IVB IOAT Ch1" },
202e974f91cSConrad Meyer 	{ 0x0e228086, "IVB IOAT Ch2" },
203e974f91cSConrad Meyer 	{ 0x0e238086, "IVB IOAT Ch3" },
204e974f91cSConrad Meyer 	{ 0x0e248086, "IVB IOAT Ch4" },
205e974f91cSConrad Meyer 	{ 0x0e258086, "IVB IOAT Ch5" },
206e974f91cSConrad Meyer 	{ 0x0e268086, "IVB IOAT Ch6" },
207e974f91cSConrad Meyer 	{ 0x0e278086, "IVB IOAT Ch7" },
208e974f91cSConrad Meyer 	{ 0x0e2e8086, "IVB IOAT Ch0 (RAID)" },
209e974f91cSConrad Meyer 	{ 0x0e2f8086, "IVB IOAT Ch1 (RAID)" },
210e974f91cSConrad Meyer 
211e974f91cSConrad Meyer 	{ 0x2f208086, "HSW IOAT Ch0" },
212e974f91cSConrad Meyer 	{ 0x2f218086, "HSW IOAT Ch1" },
213e974f91cSConrad Meyer 	{ 0x2f228086, "HSW IOAT Ch2" },
214e974f91cSConrad Meyer 	{ 0x2f238086, "HSW IOAT Ch3" },
215e974f91cSConrad Meyer 	{ 0x2f248086, "HSW IOAT Ch4" },
216e974f91cSConrad Meyer 	{ 0x2f258086, "HSW IOAT Ch5" },
217e974f91cSConrad Meyer 	{ 0x2f268086, "HSW IOAT Ch6" },
218e974f91cSConrad Meyer 	{ 0x2f278086, "HSW IOAT Ch7" },
219e974f91cSConrad Meyer 	{ 0x2f2e8086, "HSW IOAT Ch0 (RAID)" },
220e974f91cSConrad Meyer 	{ 0x2f2f8086, "HSW IOAT Ch1 (RAID)" },
221e974f91cSConrad Meyer 
222e974f91cSConrad Meyer 	{ 0x0c508086, "BWD IOAT Ch0" },
223e974f91cSConrad Meyer 	{ 0x0c518086, "BWD IOAT Ch1" },
224e974f91cSConrad Meyer 	{ 0x0c528086, "BWD IOAT Ch2" },
225e974f91cSConrad Meyer 	{ 0x0c538086, "BWD IOAT Ch3" },
226e974f91cSConrad Meyer 
227e974f91cSConrad Meyer 	{ 0x6f508086, "BDXDE IOAT Ch0" },
228e974f91cSConrad Meyer 	{ 0x6f518086, "BDXDE IOAT Ch1" },
229e974f91cSConrad Meyer 	{ 0x6f528086, "BDXDE IOAT Ch2" },
230e974f91cSConrad Meyer 	{ 0x6f538086, "BDXDE IOAT Ch3" },
231e974f91cSConrad Meyer 
2325afc2508SConrad Meyer 	{ 0x6f208086, "BDX IOAT Ch0" },
2335afc2508SConrad Meyer 	{ 0x6f218086, "BDX IOAT Ch1" },
2345afc2508SConrad Meyer 	{ 0x6f228086, "BDX IOAT Ch2" },
2355afc2508SConrad Meyer 	{ 0x6f238086, "BDX IOAT Ch3" },
2365afc2508SConrad Meyer 	{ 0x6f248086, "BDX IOAT Ch4" },
2375afc2508SConrad Meyer 	{ 0x6f258086, "BDX IOAT Ch5" },
2385afc2508SConrad Meyer 	{ 0x6f268086, "BDX IOAT Ch6" },
2395afc2508SConrad Meyer 	{ 0x6f278086, "BDX IOAT Ch7" },
2405afc2508SConrad Meyer 	{ 0x6f2e8086, "BDX IOAT Ch0 (RAID)" },
2415afc2508SConrad Meyer 	{ 0x6f2f8086, "BDX IOAT Ch1 (RAID)" },
24208f16d0cSConrad Meyer 
24308f16d0cSConrad Meyer 	{ 0x20218086, "SKX IOAT" },
244e974f91cSConrad Meyer };
245e974f91cSConrad Meyer 
246d2064cf0SWarner Losh MODULE_PNP_INFO("W32:vendor/device;D:#", pci, ioat, pci_ids,
247329e817fSWarner Losh     nitems(pci_ids));
248a64bf59cSConrad Meyer 
249e974f91cSConrad Meyer /*
250e974f91cSConrad Meyer  * OS <-> Driver linkage functions
251e974f91cSConrad Meyer  */
252e974f91cSConrad Meyer static int
253*71bf3900SAlexander Motin ioat_modevent(module_t mod __unused, int type, void *data __unused)
254*71bf3900SAlexander Motin {
255*71bf3900SAlexander Motin 	switch(type) {
256*71bf3900SAlexander Motin 	case MOD_LOAD:
257*71bf3900SAlexander Motin 		break;
258*71bf3900SAlexander Motin 
259*71bf3900SAlexander Motin 	case MOD_UNLOAD:
260*71bf3900SAlexander Motin 		ioat_test_detach();
261*71bf3900SAlexander Motin 		break;
262*71bf3900SAlexander Motin 
263*71bf3900SAlexander Motin 	case MOD_SHUTDOWN:
264*71bf3900SAlexander Motin 		break;
265*71bf3900SAlexander Motin 
266*71bf3900SAlexander Motin 	default:
267*71bf3900SAlexander Motin 		return (EOPNOTSUPP);
268*71bf3900SAlexander Motin 	}
269*71bf3900SAlexander Motin 
270*71bf3900SAlexander Motin 	return (0);
271*71bf3900SAlexander Motin }
272*71bf3900SAlexander Motin 
273*71bf3900SAlexander Motin static int
274e974f91cSConrad Meyer ioat_probe(device_t device)
275e974f91cSConrad Meyer {
276e974f91cSConrad Meyer 	struct _pcsid *ep;
277e974f91cSConrad Meyer 	u_int32_t type;
278e974f91cSConrad Meyer 
279e974f91cSConrad Meyer 	type = pci_get_devid(device);
280a64bf59cSConrad Meyer 	for (ep = pci_ids; ep < &pci_ids[nitems(pci_ids)]; ep++) {
281e974f91cSConrad Meyer 		if (ep->type == type) {
282e974f91cSConrad Meyer 			device_set_desc(device, ep->desc);
283e974f91cSConrad Meyer 			return (0);
284e974f91cSConrad Meyer 		}
285e974f91cSConrad Meyer 	}
286e974f91cSConrad Meyer 	return (ENXIO);
287e974f91cSConrad Meyer }
288e974f91cSConrad Meyer 
289e974f91cSConrad Meyer static int
290e974f91cSConrad Meyer ioat_attach(device_t device)
291e974f91cSConrad Meyer {
292e974f91cSConrad Meyer 	struct ioat_softc *ioat;
2932f03a95fSAlexander Motin 	int error, i;
294e974f91cSConrad Meyer 
295e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
296e974f91cSConrad Meyer 	ioat->device = device;
297657dc81dSAlexander Motin 	if (bus_get_domain(device, &ioat->domain) != 0)
298657dc81dSAlexander Motin 		ioat->domain = 0;
299657dc81dSAlexander Motin 	ioat->cpu = CPU_FFS(&cpuset_domain[ioat->domain]) - 1;
300657dc81dSAlexander Motin 	if (ioat->cpu < 0)
301657dc81dSAlexander Motin 		ioat->cpu = CPU_FIRST();
302e974f91cSConrad Meyer 
303e974f91cSConrad Meyer 	error = ioat_map_pci_bar(ioat);
304e974f91cSConrad Meyer 	if (error != 0)
305e974f91cSConrad Meyer 		goto err;
306e974f91cSConrad Meyer 
307e974f91cSConrad Meyer 	ioat->version = ioat_read_cbver(ioat);
308e974f91cSConrad Meyer 	if (ioat->version < IOAT_VER_3_0) {
309e974f91cSConrad Meyer 		error = ENODEV;
310e974f91cSConrad Meyer 		goto err;
311e974f91cSConrad Meyer 	}
312e974f91cSConrad Meyer 
313e974f91cSConrad Meyer 	error = ioat3_attach(device);
314e974f91cSConrad Meyer 	if (error != 0)
315e974f91cSConrad Meyer 		goto err;
316e974f91cSConrad Meyer 
317e974f91cSConrad Meyer 	error = pci_enable_busmaster(device);
318e974f91cSConrad Meyer 	if (error != 0)
319e974f91cSConrad Meyer 		goto err;
320e974f91cSConrad Meyer 
321466b3540SConrad Meyer 	error = ioat_setup_intr(ioat);
322466b3540SConrad Meyer 	if (error != 0)
323466b3540SConrad Meyer 		goto err;
324466b3540SConrad Meyer 
325cea5b880SConrad Meyer 	error = ioat_reset_hw(ioat);
3267afbb263SConrad Meyer 	if (error != 0)
327466b3540SConrad Meyer 		goto err;
3287afbb263SConrad Meyer 
3292f03a95fSAlexander Motin 	ioat_process_events(ioat, FALSE);
3307afbb263SConrad Meyer 	ioat_setup_sysctl(device);
3317afbb263SConrad Meyer 
3322f03a95fSAlexander Motin 	mtx_lock(&ioat_list_mtx);
3332f03a95fSAlexander Motin 	for (i = 0; i < IOAT_MAX_CHANNELS; i++) {
3342f03a95fSAlexander Motin 		if (ioat_channel[i] == NULL)
3352f03a95fSAlexander Motin 			break;
3362f03a95fSAlexander Motin 	}
3372f03a95fSAlexander Motin 	if (i >= IOAT_MAX_CHANNELS) {
3382f03a95fSAlexander Motin 		mtx_unlock(&ioat_list_mtx);
3392f03a95fSAlexander Motin 		device_printf(device, "Too many I/OAT devices in system\n");
3402f03a95fSAlexander Motin 		error = ENXIO;
3412f03a95fSAlexander Motin 		goto err;
3422f03a95fSAlexander Motin 	}
3432f03a95fSAlexander Motin 	ioat->chan_idx = i;
3442f03a95fSAlexander Motin 	ioat_channel[i] = ioat;
3452f03a95fSAlexander Motin 	if (i >= ioat_channel_index)
3462f03a95fSAlexander Motin 		ioat_channel_index = i + 1;
3472f03a95fSAlexander Motin 	mtx_unlock(&ioat_list_mtx);
3482f03a95fSAlexander Motin 
3497afbb263SConrad Meyer 	ioat_test_attach();
350e974f91cSConrad Meyer 
351e974f91cSConrad Meyer err:
352e974f91cSConrad Meyer 	if (error != 0)
353e974f91cSConrad Meyer 		ioat_detach(device);
354e974f91cSConrad Meyer 	return (error);
355e974f91cSConrad Meyer }
356e974f91cSConrad Meyer 
357b80b32a2STycho Nightingale static inline int
358b80b32a2STycho Nightingale ioat_bus_dmamap_destroy(struct ioat_softc *ioat, const char *func,
359b80b32a2STycho Nightingale     bus_dma_tag_t dmat, bus_dmamap_t map)
360b80b32a2STycho Nightingale {
361b80b32a2STycho Nightingale 	int error;
362b80b32a2STycho Nightingale 
363b80b32a2STycho Nightingale 	error = bus_dmamap_destroy(dmat, map);
364b80b32a2STycho Nightingale 	if (error != 0) {
365b80b32a2STycho Nightingale 		ioat_log_message(0,
366b80b32a2STycho Nightingale 		    "%s: bus_dmamap_destroy failed %d\n", func, error);
367b80b32a2STycho Nightingale 	}
368b80b32a2STycho Nightingale 
369b80b32a2STycho Nightingale 	return (error);
370b80b32a2STycho Nightingale }
371b80b32a2STycho Nightingale 
372e974f91cSConrad Meyer static int
373e974f91cSConrad Meyer ioat_detach(device_t device)
374e974f91cSConrad Meyer {
375e974f91cSConrad Meyer 	struct ioat_softc *ioat;
376b80b32a2STycho Nightingale 	int i, error;
377e974f91cSConrad Meyer 
378e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
3797afbb263SConrad Meyer 
3802f03a95fSAlexander Motin 	mtx_lock(&ioat_list_mtx);
3812f03a95fSAlexander Motin 	ioat_channel[ioat->chan_idx] = NULL;
3822f03a95fSAlexander Motin 	while (ioat_channel_index > 0 &&
3832f03a95fSAlexander Motin 	    ioat_channel[ioat_channel_index - 1] == NULL)
3842f03a95fSAlexander Motin 		ioat_channel_index--;
3852f03a95fSAlexander Motin 	mtx_unlock(&ioat_list_mtx);
3862f03a95fSAlexander Motin 
387374b05e1SConrad Meyer 	taskqueue_drain(taskqueue_thread, &ioat->reset_task);
3885f77bd3eSConrad Meyer 
3892f03a95fSAlexander Motin 	mtx_lock(&ioat->submit_lock);
3905f77bd3eSConrad Meyer 	ioat->quiescing = TRUE;
3910ff814e8SConrad Meyer 	ioat->destroying = TRUE;
3920ff814e8SConrad Meyer 	wakeup(&ioat->quiescing);
39393f7f84aSConrad Meyer 	wakeup(&ioat->resetting);
3940ff814e8SConrad Meyer 
3955f77bd3eSConrad Meyer 	ioat_drain_locked(ioat);
3962f03a95fSAlexander Motin 	mtx_unlock(&ioat->submit_lock);
3972f03a95fSAlexander Motin 	mtx_lock(&ioat->cleanup_lock);
3982f03a95fSAlexander Motin 	while (ioat_get_active(ioat) > 0)
3992f03a95fSAlexander Motin 		msleep(&ioat->tail, &ioat->cleanup_lock, 0, "ioat_drain", 1);
4002f03a95fSAlexander Motin 	mtx_unlock(&ioat->cleanup_lock);
401fe720f5aSConrad Meyer 
402fe720f5aSConrad Meyer 	ioat_teardown_intr(ioat);
4035ac77963SConrad Meyer 	callout_drain(&ioat->poll_timer);
404e974f91cSConrad Meyer 
405e974f91cSConrad Meyer 	pci_disable_busmaster(device);
406e974f91cSConrad Meyer 
407e974f91cSConrad Meyer 	if (ioat->pci_resource != NULL)
408e974f91cSConrad Meyer 		bus_release_resource(device, SYS_RES_MEMORY,
409e974f91cSConrad Meyer 		    ioat->pci_resource_id, ioat->pci_resource);
410e974f91cSConrad Meyer 
411b80b32a2STycho Nightingale 	if (ioat->data_tag != NULL) {
412b80b32a2STycho Nightingale 		for (i = 0; i < 1 << ioat->ring_size_order; i++) {
413b80b32a2STycho Nightingale 			error = ioat_bus_dmamap_destroy(ioat, __func__,
414b80b32a2STycho Nightingale 			    ioat->data_tag, ioat->ring[i].src_dmamap);
415b80b32a2STycho Nightingale 			if (error != 0)
416b80b32a2STycho Nightingale 				return (error);
417b80b32a2STycho Nightingale 		}
418b80b32a2STycho Nightingale 		for (i = 0; i < 1 << ioat->ring_size_order; i++) {
419b80b32a2STycho Nightingale 			error = ioat_bus_dmamap_destroy(ioat, __func__,
420b80b32a2STycho Nightingale 			    ioat->data_tag, ioat->ring[i].dst_dmamap);
421b80b32a2STycho Nightingale 			if (error != 0)
422b80b32a2STycho Nightingale 				return (error);
423b80b32a2STycho Nightingale 		}
424b80b32a2STycho Nightingale 
425b80b32a2STycho Nightingale 		for (i = 0; i < 1 << ioat->ring_size_order; i++) {
426b80b32a2STycho Nightingale 			error = ioat_bus_dmamap_destroy(ioat, __func__,
427b80b32a2STycho Nightingale 			    ioat->data_tag, ioat->ring[i].src2_dmamap);
428b80b32a2STycho Nightingale 			if (error != 0)
429b80b32a2STycho Nightingale 				return (error);
430b80b32a2STycho Nightingale 		}
431b80b32a2STycho Nightingale 		for (i = 0; i < 1 << ioat->ring_size_order; i++) {
432b80b32a2STycho Nightingale 			error = ioat_bus_dmamap_destroy(ioat, __func__,
433b80b32a2STycho Nightingale 			    ioat->data_tag, ioat->ring[i].dst2_dmamap);
434b80b32a2STycho Nightingale 			if (error != 0)
435b80b32a2STycho Nightingale 				return (error);
436b80b32a2STycho Nightingale 		}
437b80b32a2STycho Nightingale 
438b80b32a2STycho Nightingale 		bus_dma_tag_destroy(ioat->data_tag);
439b80b32a2STycho Nightingale 	}
440b80b32a2STycho Nightingale 
441bf8553eaSConrad Meyer 	if (ioat->ring != NULL)
442bf8553eaSConrad Meyer 		ioat_free_ring(ioat, 1 << ioat->ring_size_order, ioat->ring);
443e974f91cSConrad Meyer 
444e974f91cSConrad Meyer 	if (ioat->comp_update != NULL) {
445e974f91cSConrad Meyer 		bus_dmamap_unload(ioat->comp_update_tag, ioat->comp_update_map);
446e974f91cSConrad Meyer 		bus_dmamem_free(ioat->comp_update_tag, ioat->comp_update,
447e974f91cSConrad Meyer 		    ioat->comp_update_map);
448e974f91cSConrad Meyer 		bus_dma_tag_destroy(ioat->comp_update_tag);
449e974f91cSConrad Meyer 	}
450e974f91cSConrad Meyer 
4518e269d99SConrad Meyer 	if (ioat->hw_desc_ring != NULL) {
4528e269d99SConrad Meyer 		bus_dmamap_unload(ioat->hw_desc_tag, ioat->hw_desc_map);
4538e269d99SConrad Meyer 		bus_dmamem_free(ioat->hw_desc_tag, ioat->hw_desc_ring,
4548e269d99SConrad Meyer 		    ioat->hw_desc_map);
455e974f91cSConrad Meyer 		bus_dma_tag_destroy(ioat->hw_desc_tag);
4568e269d99SConrad Meyer 	}
457e974f91cSConrad Meyer 
4584253ea50SConrad Meyer 	return (0);
4594253ea50SConrad Meyer }
4604253ea50SConrad Meyer 
4614253ea50SConrad Meyer static int
4624253ea50SConrad Meyer ioat_teardown_intr(struct ioat_softc *ioat)
4634253ea50SConrad Meyer {
4644253ea50SConrad Meyer 
465e974f91cSConrad Meyer 	if (ioat->tag != NULL)
4664253ea50SConrad Meyer 		bus_teardown_intr(ioat->device, ioat->res, ioat->tag);
467e974f91cSConrad Meyer 
468e974f91cSConrad Meyer 	if (ioat->res != NULL)
4694253ea50SConrad Meyer 		bus_release_resource(ioat->device, SYS_RES_IRQ,
470e974f91cSConrad Meyer 		    rman_get_rid(ioat->res), ioat->res);
471e974f91cSConrad Meyer 
4724253ea50SConrad Meyer 	pci_release_msi(ioat->device);
473e974f91cSConrad Meyer 	return (0);
474e974f91cSConrad Meyer }
475e974f91cSConrad Meyer 
476e974f91cSConrad Meyer static int
477cea5b880SConrad Meyer ioat_start_channel(struct ioat_softc *ioat)
478e974f91cSConrad Meyer {
479fe8712f8SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
480fe8712f8SConrad Meyer 	struct ioat_descriptor *desc;
481fe8712f8SConrad Meyer 	struct bus_dmadesc *dmadesc;
482e974f91cSConrad Meyer 	uint64_t status;
483e974f91cSConrad Meyer 	uint32_t chanerr;
484e974f91cSConrad Meyer 	int i;
485e974f91cSConrad Meyer 
486e974f91cSConrad Meyer 	ioat_acquire(&ioat->dmaengine);
487fe8712f8SConrad Meyer 
488fe8712f8SConrad Meyer 	/* Submit 'NULL' operation manually to avoid quiescing flag */
489fe8712f8SConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->head);
4908e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, ioat->head)->dma;
491fe8712f8SConrad Meyer 	dmadesc = &desc->bus_dmadesc;
492fe8712f8SConrad Meyer 
493fe8712f8SConrad Meyer 	dmadesc->callback_fn = NULL;
494fe8712f8SConrad Meyer 	dmadesc->callback_arg = NULL;
495fe8712f8SConrad Meyer 
496fe8712f8SConrad Meyer 	hw_desc->u.control_raw = 0;
497fe8712f8SConrad Meyer 	hw_desc->u.control_generic.op = IOAT_OP_COPY;
498fe8712f8SConrad Meyer 	hw_desc->u.control_generic.completion_update = 1;
499fe8712f8SConrad Meyer 	hw_desc->size = 8;
500fe8712f8SConrad Meyer 	hw_desc->src_addr = 0;
501fe8712f8SConrad Meyer 	hw_desc->dest_addr = 0;
502fe8712f8SConrad Meyer 	hw_desc->u.control.null = 1;
503fe8712f8SConrad Meyer 
504fe8712f8SConrad Meyer 	ioat_submit_single(ioat);
505e974f91cSConrad Meyer 	ioat_release(&ioat->dmaengine);
506e974f91cSConrad Meyer 
507e974f91cSConrad Meyer 	for (i = 0; i < 100; i++) {
508e974f91cSConrad Meyer 		DELAY(1);
509e974f91cSConrad Meyer 		status = ioat_get_chansts(ioat);
510e974f91cSConrad Meyer 		if (is_ioat_idle(status))
511e974f91cSConrad Meyer 			return (0);
512e974f91cSConrad Meyer 	}
513e974f91cSConrad Meyer 
514e974f91cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
515e974f91cSConrad Meyer 	ioat_log_message(0, "could not start channel: "
51659acd4baSConrad Meyer 	    "status = %#jx error = %b\n", (uintmax_t)status, (int)chanerr,
51759acd4baSConrad Meyer 	    IOAT_CHANERR_STR);
518e974f91cSConrad Meyer 	return (ENXIO);
519e974f91cSConrad Meyer }
520e974f91cSConrad Meyer 
521e974f91cSConrad Meyer /*
522e974f91cSConrad Meyer  * Initialize Hardware
523e974f91cSConrad Meyer  */
524e974f91cSConrad Meyer static int
525e974f91cSConrad Meyer ioat3_attach(device_t device)
526e974f91cSConrad Meyer {
527e974f91cSConrad Meyer 	struct ioat_softc *ioat;
5288e269d99SConrad Meyer 	struct ioat_descriptor *ring;
529e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *dma_hw_desc;
5308e269d99SConrad Meyer 	void *hw_desc;
5311f4a469dSAlexander Motin 	bus_addr_t lowaddr;
5328e269d99SConrad Meyer 	size_t ringsz;
533e974f91cSConrad Meyer 	int i, num_descriptors;
534e974f91cSConrad Meyer 	int error;
535e974f91cSConrad Meyer 	uint8_t xfercap;
536e974f91cSConrad Meyer 
537e974f91cSConrad Meyer 	error = 0;
538e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
5391693d27bSConrad Meyer 	ioat->capabilities = ioat_read_dmacapability(ioat);
5401693d27bSConrad Meyer 
54148fed014SConrad Meyer 	ioat_log_message(0, "Capabilities: %b\n", (int)ioat->capabilities,
5421693d27bSConrad Meyer 	    IOAT_DMACAP_STR);
543e974f91cSConrad Meyer 
544e974f91cSConrad Meyer 	xfercap = ioat_read_xfercap(ioat);
545e974f91cSConrad Meyer 	ioat->max_xfer_size = 1 << xfercap;
546e974f91cSConrad Meyer 
5475ca9fc2aSConrad Meyer 	ioat->intrdelay_supported = (ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) &
5485ca9fc2aSConrad Meyer 	    IOAT_INTRDELAY_SUPPORTED) != 0;
5495ca9fc2aSConrad Meyer 	if (ioat->intrdelay_supported)
5505ca9fc2aSConrad Meyer 		ioat->intrdelay_max = IOAT_INTRDELAY_US_MASK;
5515ca9fc2aSConrad Meyer 
552e974f91cSConrad Meyer 	/* TODO: need to check DCA here if we ever do XOR/PQ */
553e974f91cSConrad Meyer 
554e974f91cSConrad Meyer 	mtx_init(&ioat->submit_lock, "ioat_submit", NULL, MTX_DEF);
555faefad9cSConrad Meyer 	mtx_init(&ioat->cleanup_lock, "ioat_cleanup", NULL, MTX_DEF);
5565ac77963SConrad Meyer 	callout_init(&ioat->poll_timer, 1);
557374b05e1SConrad Meyer 	TASK_INIT(&ioat->reset_task, 0, ioat_reset_hw_task, ioat);
558e974f91cSConrad Meyer 
559faefad9cSConrad Meyer 	/* Establish lock order for Witness */
560faefad9cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
5612f03a95fSAlexander Motin 	mtx_lock(&ioat->submit_lock);
562faefad9cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
5632f03a95fSAlexander Motin 	mtx_unlock(&ioat->cleanup_lock);
564faefad9cSConrad Meyer 
56525ad9585SConrad Meyer 	ioat->is_submitter_processing = FALSE;
566e974f91cSConrad Meyer 
5671f4a469dSAlexander Motin 	if (ioat->version >= IOAT_VER_3_3)
5681f4a469dSAlexander Motin 		lowaddr = BUS_SPACE_MAXADDR_48BIT;
5691f4a469dSAlexander Motin 	else if (ioat->version >= IOAT_VER_3_2)
5701f4a469dSAlexander Motin 		lowaddr = BUS_SPACE_MAXADDR_46BIT;
5711f4a469dSAlexander Motin 	else
5721f4a469dSAlexander Motin 		lowaddr = BUS_SPACE_MAXADDR_40BIT;
5731f4a469dSAlexander Motin 
5741f4a469dSAlexander Motin 	error = bus_dma_tag_create(bus_get_dma_tag(ioat->device),
5751f4a469dSAlexander Motin 	    sizeof(uint64_t), 0x0, lowaddr, BUS_SPACE_MAXADDR, NULL, NULL,
576e974f91cSConrad Meyer 	    sizeof(uint64_t), 1, sizeof(uint64_t), 0, NULL, NULL,
577e974f91cSConrad Meyer 	    &ioat->comp_update_tag);
5781f4a469dSAlexander Motin 	if (error != 0)
5791f4a469dSAlexander Motin 		return (error);
580e974f91cSConrad Meyer 
581e974f91cSConrad Meyer 	error = bus_dmamem_alloc(ioat->comp_update_tag,
5823eb70a09SAlexander Motin 	    (void **)&ioat->comp_update, BUS_DMA_ZERO | BUS_DMA_WAITOK,
5833eb70a09SAlexander Motin 	    &ioat->comp_update_map);
5841f4a469dSAlexander Motin 	if (error != 0)
5851f4a469dSAlexander Motin 		return (error);
586e974f91cSConrad Meyer 
587e974f91cSConrad Meyer 	error = bus_dmamap_load(ioat->comp_update_tag, ioat->comp_update_map,
588e974f91cSConrad Meyer 	    ioat->comp_update, sizeof(uint64_t), ioat_comp_update_map, ioat,
5893eb70a09SAlexander Motin 	    BUS_DMA_NOWAIT);
590e974f91cSConrad Meyer 	if (error != 0)
591e974f91cSConrad Meyer 		return (error);
592e974f91cSConrad Meyer 
593a0992979SConrad Meyer 	ioat->ring_size_order = g_ioat_ring_order;
594e974f91cSConrad Meyer 	num_descriptors = 1 << ioat->ring_size_order;
5958e269d99SConrad Meyer 	ringsz = sizeof(struct ioat_dma_hw_descriptor) * num_descriptors;
596e974f91cSConrad Meyer 
5978e269d99SConrad Meyer 	error = bus_dma_tag_create(bus_get_dma_tag(ioat->device),
5981f4a469dSAlexander Motin 	    2 * 1024 * 1024, 0x0, lowaddr, BUS_SPACE_MAXADDR, NULL, NULL,
5991f4a469dSAlexander Motin 	    ringsz, 1, ringsz, 0, NULL, NULL, &ioat->hw_desc_tag);
6008e269d99SConrad Meyer 	if (error != 0)
6018e269d99SConrad Meyer 		return (error);
6028e269d99SConrad Meyer 
6038e269d99SConrad Meyer 	error = bus_dmamem_alloc(ioat->hw_desc_tag, &hw_desc,
6048e269d99SConrad Meyer 	    BUS_DMA_ZERO | BUS_DMA_WAITOK, &ioat->hw_desc_map);
6058e269d99SConrad Meyer 	if (error != 0)
6068e269d99SConrad Meyer 		return (error);
6078e269d99SConrad Meyer 
6088e269d99SConrad Meyer 	error = bus_dmamap_load(ioat->hw_desc_tag, ioat->hw_desc_map, hw_desc,
6093eb70a09SAlexander Motin 	    ringsz, ioat_dmamap_cb, &ioat->hw_desc_bus_addr, BUS_DMA_NOWAIT);
6108e269d99SConrad Meyer 	if (error)
6118e269d99SConrad Meyer 		return (error);
6128e269d99SConrad Meyer 
6138e269d99SConrad Meyer 	ioat->hw_desc_ring = hw_desc;
614e974f91cSConrad Meyer 
615b80b32a2STycho Nightingale 	error = bus_dma_tag_create(bus_get_dma_tag(ioat->device),
6161f4a469dSAlexander Motin 	    1, 0, lowaddr, BUS_SPACE_MAXADDR, NULL, NULL,
617b80b32a2STycho Nightingale 	    ioat->max_xfer_size, 1, ioat->max_xfer_size, 0, NULL, NULL,
618b80b32a2STycho Nightingale 	    &ioat->data_tag);
6191f4a469dSAlexander Motin 	if (error != 0)
620b80b32a2STycho Nightingale 		return (error);
621657dc81dSAlexander Motin 	ioat->ring = malloc_domainset(num_descriptors * sizeof(*ring), M_IOAT,
622657dc81dSAlexander Motin 	    DOMAINSET_PREF(ioat->domain), M_ZERO | M_WAITOK);
623e974f91cSConrad Meyer 
624e974f91cSConrad Meyer 	ring = ioat->ring;
625e974f91cSConrad Meyer 	for (i = 0; i < num_descriptors; i++) {
6268e269d99SConrad Meyer 		memset(&ring[i].bus_dmadesc, 0, sizeof(ring[i].bus_dmadesc));
6278e269d99SConrad Meyer 		ring[i].id = i;
628b80b32a2STycho Nightingale 		error = bus_dmamap_create(ioat->data_tag, 0,
629b80b32a2STycho Nightingale                     &ring[i].src_dmamap);
630b80b32a2STycho Nightingale 		if (error != 0) {
631b80b32a2STycho Nightingale 			ioat_log_message(0,
632b80b32a2STycho Nightingale 			    "%s: bus_dmamap_create failed %d\n", __func__,
633b80b32a2STycho Nightingale 			    error);
634b80b32a2STycho Nightingale 			return (error);
635b80b32a2STycho Nightingale 		}
636b80b32a2STycho Nightingale 		error = bus_dmamap_create(ioat->data_tag, 0,
637b80b32a2STycho Nightingale                     &ring[i].dst_dmamap);
638b80b32a2STycho Nightingale 		if (error != 0) {
639b80b32a2STycho Nightingale 			ioat_log_message(0,
640b80b32a2STycho Nightingale 			    "%s: bus_dmamap_create failed %d\n", __func__,
641b80b32a2STycho Nightingale 			    error);
642b80b32a2STycho Nightingale 			return (error);
643b80b32a2STycho Nightingale 		}
644b80b32a2STycho Nightingale 		error = bus_dmamap_create(ioat->data_tag, 0,
645b80b32a2STycho Nightingale                     &ring[i].src2_dmamap);
646b80b32a2STycho Nightingale 		if (error != 0) {
647b80b32a2STycho Nightingale 			ioat_log_message(0,
648b80b32a2STycho Nightingale 			    "%s: bus_dmamap_create failed %d\n", __func__,
649b80b32a2STycho Nightingale 			    error);
650b80b32a2STycho Nightingale 			return (error);
651b80b32a2STycho Nightingale 		}
652b80b32a2STycho Nightingale 		error = bus_dmamap_create(ioat->data_tag, 0,
653b80b32a2STycho Nightingale                     &ring[i].dst2_dmamap);
654b80b32a2STycho Nightingale 		if (error != 0) {
655b80b32a2STycho Nightingale 			ioat_log_message(0,
656b80b32a2STycho Nightingale 			    "%s: bus_dmamap_create failed %d\n", __func__,
657b80b32a2STycho Nightingale 			    error);
658b80b32a2STycho Nightingale 			return (error);
659b80b32a2STycho Nightingale 		}
660e974f91cSConrad Meyer 	}
661e974f91cSConrad Meyer 
6628e269d99SConrad Meyer 	for (i = 0; i < num_descriptors; i++) {
6638e269d99SConrad Meyer 		dma_hw_desc = &ioat->hw_desc_ring[i].dma;
6648e269d99SConrad Meyer 		dma_hw_desc->next = RING_PHYS_ADDR(ioat, i + 1);
665e974f91cSConrad Meyer 	}
666e974f91cSConrad Meyer 
667348efb14SAlexander Motin 	ioat->tail = ioat->head = 0;
668348efb14SAlexander Motin 	*ioat->comp_update = ioat->last_seen =
669348efb14SAlexander Motin 	    RING_PHYS_ADDR(ioat, ioat->tail - 1);
670e974f91cSConrad Meyer 	return (0);
671e974f91cSConrad Meyer }
672e974f91cSConrad Meyer 
673e974f91cSConrad Meyer static int
674e974f91cSConrad Meyer ioat_map_pci_bar(struct ioat_softc *ioat)
675e974f91cSConrad Meyer {
676e974f91cSConrad Meyer 
677e974f91cSConrad Meyer 	ioat->pci_resource_id = PCIR_BAR(0);
678e88e14b9SConrad Meyer 	ioat->pci_resource = bus_alloc_resource_any(ioat->device,
679e88e14b9SConrad Meyer 	    SYS_RES_MEMORY, &ioat->pci_resource_id, RF_ACTIVE);
680e974f91cSConrad Meyer 
681e974f91cSConrad Meyer 	if (ioat->pci_resource == NULL) {
682e974f91cSConrad Meyer 		ioat_log_message(0, "unable to allocate pci resource\n");
683e974f91cSConrad Meyer 		return (ENODEV);
684e974f91cSConrad Meyer 	}
685e974f91cSConrad Meyer 
686e974f91cSConrad Meyer 	ioat->pci_bus_tag = rman_get_bustag(ioat->pci_resource);
687e974f91cSConrad Meyer 	ioat->pci_bus_handle = rman_get_bushandle(ioat->pci_resource);
688e974f91cSConrad Meyer 	return (0);
689e974f91cSConrad Meyer }
690e974f91cSConrad Meyer 
691e974f91cSConrad Meyer static void
692e974f91cSConrad Meyer ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg, int error)
693e974f91cSConrad Meyer {
694e974f91cSConrad Meyer 	struct ioat_softc *ioat = arg;
695e974f91cSConrad Meyer 
696cea5b880SConrad Meyer 	KASSERT(error == 0, ("%s: error:%d", __func__, error));
697e974f91cSConrad Meyer 	ioat->comp_update_bus_addr = seg[0].ds_addr;
698e974f91cSConrad Meyer }
699e974f91cSConrad Meyer 
700e974f91cSConrad Meyer static void
701e974f91cSConrad Meyer ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
702e974f91cSConrad Meyer {
703e974f91cSConrad Meyer 	bus_addr_t *baddr;
704e974f91cSConrad Meyer 
705cea5b880SConrad Meyer 	KASSERT(error == 0, ("%s: error:%d", __func__, error));
706e974f91cSConrad Meyer 	baddr = arg;
707e974f91cSConrad Meyer 	*baddr = segs->ds_addr;
708e974f91cSConrad Meyer }
709e974f91cSConrad Meyer 
710e974f91cSConrad Meyer /*
711e974f91cSConrad Meyer  * Interrupt setup and handlers
712e974f91cSConrad Meyer  */
713e974f91cSConrad Meyer static int
7144253ea50SConrad Meyer ioat_setup_intr(struct ioat_softc *ioat)
715e974f91cSConrad Meyer {
716e974f91cSConrad Meyer 	uint32_t num_vectors;
717e974f91cSConrad Meyer 	int error;
718e974f91cSConrad Meyer 	boolean_t use_msix;
719e974f91cSConrad Meyer 	boolean_t force_legacy_interrupts;
720e974f91cSConrad Meyer 
721e974f91cSConrad Meyer 	use_msix = FALSE;
722e974f91cSConrad Meyer 	force_legacy_interrupts = FALSE;
723e974f91cSConrad Meyer 
724e974f91cSConrad Meyer 	if (!g_force_legacy_interrupts && pci_msix_count(ioat->device) >= 1) {
725e974f91cSConrad Meyer 		num_vectors = 1;
726e974f91cSConrad Meyer 		pci_alloc_msix(ioat->device, &num_vectors);
727e974f91cSConrad Meyer 		if (num_vectors == 1)
728e974f91cSConrad Meyer 			use_msix = TRUE;
729e974f91cSConrad Meyer 	}
730e974f91cSConrad Meyer 
731e974f91cSConrad Meyer 	if (use_msix) {
732e974f91cSConrad Meyer 		ioat->rid = 1;
733e974f91cSConrad Meyer 		ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
734e974f91cSConrad Meyer 		    &ioat->rid, RF_ACTIVE);
735e974f91cSConrad Meyer 	} else {
736e974f91cSConrad Meyer 		ioat->rid = 0;
737e974f91cSConrad Meyer 		ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
738e974f91cSConrad Meyer 		    &ioat->rid, RF_SHAREABLE | RF_ACTIVE);
739e974f91cSConrad Meyer 	}
740e974f91cSConrad Meyer 	if (ioat->res == NULL) {
741e974f91cSConrad Meyer 		ioat_log_message(0, "bus_alloc_resource failed\n");
742e974f91cSConrad Meyer 		return (ENOMEM);
743e974f91cSConrad Meyer 	}
744e974f91cSConrad Meyer 
745e974f91cSConrad Meyer 	ioat->tag = NULL;
746e974f91cSConrad Meyer 	error = bus_setup_intr(ioat->device, ioat->res, INTR_MPSAFE |
747e974f91cSConrad Meyer 	    INTR_TYPE_MISC, NULL, ioat_interrupt_handler, ioat, &ioat->tag);
748e974f91cSConrad Meyer 	if (error != 0) {
749e974f91cSConrad Meyer 		ioat_log_message(0, "bus_setup_intr failed\n");
750e974f91cSConrad Meyer 		return (error);
751e974f91cSConrad Meyer 	}
752e974f91cSConrad Meyer 
753e974f91cSConrad Meyer 	ioat_write_intrctrl(ioat, IOAT_INTRCTRL_MASTER_INT_EN);
754e974f91cSConrad Meyer 	return (0);
755e974f91cSConrad Meyer }
756e974f91cSConrad Meyer 
7574253ea50SConrad Meyer static boolean_t
7580d1a05d9SConrad Meyer ioat_model_resets_msix(struct ioat_softc *ioat)
7594253ea50SConrad Meyer {
7604253ea50SConrad Meyer 	u_int32_t pciid;
7614253ea50SConrad Meyer 
7624253ea50SConrad Meyer 	pciid = pci_get_devid(ioat->device);
7634253ea50SConrad Meyer 	switch (pciid) {
7640d1a05d9SConrad Meyer 		/* BWD: */
7650d1a05d9SConrad Meyer 	case 0x0c508086:
7660d1a05d9SConrad Meyer 	case 0x0c518086:
7670d1a05d9SConrad Meyer 	case 0x0c528086:
7680d1a05d9SConrad Meyer 	case 0x0c538086:
7690d1a05d9SConrad Meyer 		/* BDXDE: */
7704253ea50SConrad Meyer 	case 0x6f508086:
7714253ea50SConrad Meyer 	case 0x6f518086:
7724253ea50SConrad Meyer 	case 0x6f528086:
7734253ea50SConrad Meyer 	case 0x6f538086:
7744253ea50SConrad Meyer 		return (TRUE);
7754253ea50SConrad Meyer 	}
7764253ea50SConrad Meyer 
7774253ea50SConrad Meyer 	return (FALSE);
7784253ea50SConrad Meyer }
7794253ea50SConrad Meyer 
780e974f91cSConrad Meyer static void
781e974f91cSConrad Meyer ioat_interrupt_handler(void *arg)
782e974f91cSConrad Meyer {
783e974f91cSConrad Meyer 	struct ioat_softc *ioat = arg;
784e974f91cSConrad Meyer 
78501fbbc88SConrad Meyer 	ioat->stats.interrupts++;
7862f03a95fSAlexander Motin 	ioat_process_events(ioat, TRUE);
787e974f91cSConrad Meyer }
788e974f91cSConrad Meyer 
789faefad9cSConrad Meyer static int
790faefad9cSConrad Meyer chanerr_to_errno(uint32_t chanerr)
791faefad9cSConrad Meyer {
792faefad9cSConrad Meyer 
793faefad9cSConrad Meyer 	if (chanerr == 0)
794faefad9cSConrad Meyer 		return (0);
795faefad9cSConrad Meyer 	if ((chanerr & (IOAT_CHANERR_XSADDERR | IOAT_CHANERR_XDADDERR)) != 0)
796faefad9cSConrad Meyer 		return (EFAULT);
797faefad9cSConrad Meyer 	if ((chanerr & (IOAT_CHANERR_RDERR | IOAT_CHANERR_WDERR)) != 0)
798faefad9cSConrad Meyer 		return (EIO);
799faefad9cSConrad Meyer 	/* This one is probably our fault: */
800faefad9cSConrad Meyer 	if ((chanerr & IOAT_CHANERR_NDADDERR) != 0)
801faefad9cSConrad Meyer 		return (EIO);
802faefad9cSConrad Meyer 	return (EIO);
803faefad9cSConrad Meyer }
804faefad9cSConrad Meyer 
805e974f91cSConrad Meyer static void
8062f03a95fSAlexander Motin ioat_process_events(struct ioat_softc *ioat, boolean_t intr)
807e974f91cSConrad Meyer {
808e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
809e974f91cSConrad Meyer 	struct bus_dmadesc *dmadesc;
810e974f91cSConrad Meyer 	uint64_t comp_update, status;
811faefad9cSConrad Meyer 	uint32_t completed, chanerr;
812faefad9cSConrad Meyer 	int error;
813e974f91cSConrad Meyer 
8148acd3f12SAlexander Motin 	if (intr) {
815e974f91cSConrad Meyer 		mtx_lock(&ioat->cleanup_lock);
8168acd3f12SAlexander Motin 	} else {
8178acd3f12SAlexander Motin 		if (!mtx_trylock(&ioat->cleanup_lock))
8188acd3f12SAlexander Motin 			return;
8198acd3f12SAlexander Motin 	}
820e974f91cSConrad Meyer 
821fe8712f8SConrad Meyer 	/*
822fe8712f8SConrad Meyer 	 * Don't run while the hardware is being reset.  Reset is responsible
823fe8712f8SConrad Meyer 	 * for blocking new work and draining & completing existing work, so
824fe8712f8SConrad Meyer 	 * there is nothing to do until new work is queued after reset anyway.
825fe8712f8SConrad Meyer 	 */
826fe8712f8SConrad Meyer 	if (ioat->resetting_cleanup) {
827fe8712f8SConrad Meyer 		mtx_unlock(&ioat->cleanup_lock);
828fe8712f8SConrad Meyer 		return;
829fe8712f8SConrad Meyer 	}
830fe8712f8SConrad Meyer 
831e974f91cSConrad Meyer 	completed = 0;
8320d0f2640SConrad Meyer 	comp_update = *ioat->comp_update;
833e974f91cSConrad Meyer 	status = comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK;
834e974f91cSConrad Meyer 
8358e269d99SConrad Meyer 	if (status < ioat->hw_desc_bus_addr ||
8368e269d99SConrad Meyer 	    status >= ioat->hw_desc_bus_addr + (1 << ioat->ring_size_order) *
8378e269d99SConrad Meyer 	    sizeof(struct ioat_generic_hw_descriptor))
8388e269d99SConrad Meyer 		panic("Bogus completion address %jx (channel %u)",
8398e269d99SConrad Meyer 		    (uintmax_t)status, ioat->chan_idx);
8408e269d99SConrad Meyer 
8411bbc06b8SConrad Meyer 	if (status == ioat->last_seen) {
8421bbc06b8SConrad Meyer 		/*
8431bbc06b8SConrad Meyer 		 * If we landed in process_events and nothing has been
8441bbc06b8SConrad Meyer 		 * completed, check for a timeout due to channel halt.
8451bbc06b8SConrad Meyer 		 */
8461bbc06b8SConrad Meyer 		goto out;
8471bbc06b8SConrad Meyer 	}
848dc465059SConrad Meyer 	CTR4(KTR_IOAT, "%s channel=%u hw_status=0x%lx last_seen=0x%lx",
849dc465059SConrad Meyer 	    __func__, ioat->chan_idx, comp_update, ioat->last_seen);
8501bbc06b8SConrad Meyer 
8518e269d99SConrad Meyer 	while (RING_PHYS_ADDR(ioat, ioat->tail - 1) != status) {
852e974f91cSConrad Meyer 		desc = ioat_get_ring_entry(ioat, ioat->tail);
853e974f91cSConrad Meyer 		dmadesc = &desc->bus_dmadesc;
85405e4cffbSConrad Meyer 		CTR5(KTR_IOAT, "channel=%u completing desc idx %u (%p) ok  cb %p(%p)",
85505e4cffbSConrad Meyer 		    ioat->chan_idx, ioat->tail, dmadesc, dmadesc->callback_fn,
856520f6023SConrad Meyer 		    dmadesc->callback_arg);
857e974f91cSConrad Meyer 
858b80b32a2STycho Nightingale 		bus_dmamap_unload(ioat->data_tag, desc->src_dmamap);
859b80b32a2STycho Nightingale 		bus_dmamap_unload(ioat->data_tag, desc->dst_dmamap);
860b80b32a2STycho Nightingale 		bus_dmamap_unload(ioat->data_tag, desc->src2_dmamap);
861b80b32a2STycho Nightingale 		bus_dmamap_unload(ioat->data_tag, desc->dst2_dmamap);
862b80b32a2STycho Nightingale 
863faefad9cSConrad Meyer 		if (dmadesc->callback_fn != NULL)
864faefad9cSConrad Meyer 			dmadesc->callback_fn(dmadesc->callback_arg, 0);
865e974f91cSConrad Meyer 
866466b3540SConrad Meyer 		completed++;
867e974f91cSConrad Meyer 		ioat->tail++;
868e974f91cSConrad Meyer 	}
86905e4cffbSConrad Meyer 	CTR5(KTR_IOAT, "%s channel=%u head=%u tail=%u active=%u", __func__,
87005e4cffbSConrad Meyer 	    ioat->chan_idx, ioat->head, ioat->tail, ioat_get_active(ioat));
871e974f91cSConrad Meyer 
8720283c0f5SConrad Meyer 	if (completed != 0) {
8738e269d99SConrad Meyer 		ioat->last_seen = RING_PHYS_ADDR(ioat, ioat->tail - 1);
87401fbbc88SConrad Meyer 		ioat->stats.descriptors_processed += completed;
8752f03a95fSAlexander Motin 		wakeup(&ioat->tail);
8760283c0f5SConrad Meyer 	}
87701fbbc88SConrad Meyer 
8781bbc06b8SConrad Meyer out:
879e974f91cSConrad Meyer 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
880e974f91cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
881faefad9cSConrad Meyer 
882ea9e23edSConrad Meyer 	/*
883ea9e23edSConrad Meyer 	 * The device doesn't seem to reliably push suspend/halt statuses to
884ea9e23edSConrad Meyer 	 * the channel completion memory address, so poll the device register
8852f03a95fSAlexander Motin 	 * here.  For performance reasons skip it on interrupts, do it only
8862f03a95fSAlexander Motin 	 * on much more rare polling events.
887ea9e23edSConrad Meyer 	 */
8882f03a95fSAlexander Motin 	if (!intr)
889ea9e23edSConrad Meyer 		comp_update = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS;
890d2c55e5aSConrad Meyer 	if (!is_ioat_halted(comp_update) && !is_ioat_suspended(comp_update))
891faefad9cSConrad Meyer 		return;
892faefad9cSConrad Meyer 
89301fbbc88SConrad Meyer 	ioat->stats.channel_halts++;
89401fbbc88SConrad Meyer 
895faefad9cSConrad Meyer 	/*
896faefad9cSConrad Meyer 	 * Fatal programming error on this DMA channel.  Flush any outstanding
897faefad9cSConrad Meyer 	 * work with error status and restart the engine.
898faefad9cSConrad Meyer 	 */
899faefad9cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
900faefad9cSConrad Meyer 	ioat->quiescing = TRUE;
9012f03a95fSAlexander Motin 	mtx_unlock(&ioat->submit_lock);
9022f03a95fSAlexander Motin 
9033a370919SConrad Meyer 	/*
9042f03a95fSAlexander Motin 	 * This is safe to do here because the submit queue is quiesced.  We
9052f03a95fSAlexander Motin 	 * know that we will drain all outstanding events, so ioat_reset_hw
9062f03a95fSAlexander Motin 	 * can't deadlock. It is necessary to protect other ioat_process_event
9072f03a95fSAlexander Motin 	 * threads from racing ioat_reset_hw, reading an indeterminate hw
9082f03a95fSAlexander Motin 	 * state, and attempting to continue issuing completions.
9093a370919SConrad Meyer 	 */
9102f03a95fSAlexander Motin 	mtx_lock(&ioat->cleanup_lock);
9113a370919SConrad Meyer 	ioat->resetting_cleanup = TRUE;
912faefad9cSConrad Meyer 
913faefad9cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
914985efa77SConrad Meyer 	if (1 <= g_ioat_debug_level)
915faefad9cSConrad Meyer 		ioat_halted_debug(ioat, chanerr);
91601fbbc88SConrad Meyer 	ioat->stats.last_halt_chanerr = chanerr;
917faefad9cSConrad Meyer 
918faefad9cSConrad Meyer 	while (ioat_get_active(ioat) > 0) {
919faefad9cSConrad Meyer 		desc = ioat_get_ring_entry(ioat, ioat->tail);
920faefad9cSConrad Meyer 		dmadesc = &desc->bus_dmadesc;
92105e4cffbSConrad Meyer 		CTR5(KTR_IOAT, "channel=%u completing desc idx %u (%p) err cb %p(%p)",
92205e4cffbSConrad Meyer 		    ioat->chan_idx, ioat->tail, dmadesc, dmadesc->callback_fn,
923520f6023SConrad Meyer 		    dmadesc->callback_arg);
924faefad9cSConrad Meyer 
925faefad9cSConrad Meyer 		if (dmadesc->callback_fn != NULL)
926faefad9cSConrad Meyer 			dmadesc->callback_fn(dmadesc->callback_arg,
927faefad9cSConrad Meyer 			    chanerr_to_errno(chanerr));
928faefad9cSConrad Meyer 
929faefad9cSConrad Meyer 		ioat->tail++;
93001fbbc88SConrad Meyer 		ioat->stats.descriptors_processed++;
93101fbbc88SConrad Meyer 		ioat->stats.descriptors_error++;
932faefad9cSConrad Meyer 	}
93305e4cffbSConrad Meyer 	CTR5(KTR_IOAT, "%s channel=%u head=%u tail=%u active=%u", __func__,
93405e4cffbSConrad Meyer 	    ioat->chan_idx, ioat->head, ioat->tail, ioat_get_active(ioat));
935faefad9cSConrad Meyer 
936faefad9cSConrad Meyer 	/* Clear error status */
937faefad9cSConrad Meyer 	ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
938faefad9cSConrad Meyer 
939faefad9cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
940faefad9cSConrad Meyer 
941faefad9cSConrad Meyer 	ioat_log_message(0, "Resetting channel to recover from error\n");
942374b05e1SConrad Meyer 	error = taskqueue_enqueue(taskqueue_thread, &ioat->reset_task);
943374b05e1SConrad Meyer 	KASSERT(error == 0,
944374b05e1SConrad Meyer 	    ("%s: taskqueue_enqueue failed: %d", __func__, error));
945374b05e1SConrad Meyer }
946374b05e1SConrad Meyer 
947374b05e1SConrad Meyer static void
948374b05e1SConrad Meyer ioat_reset_hw_task(void *ctx, int pending __unused)
949374b05e1SConrad Meyer {
950374b05e1SConrad Meyer 	struct ioat_softc *ioat;
951374b05e1SConrad Meyer 	int error;
952374b05e1SConrad Meyer 
953374b05e1SConrad Meyer 	ioat = ctx;
954374b05e1SConrad Meyer 	ioat_log_message(1, "%s: Resetting channel\n", __func__);
955374b05e1SConrad Meyer 
956faefad9cSConrad Meyer 	error = ioat_reset_hw(ioat);
957faefad9cSConrad Meyer 	KASSERT(error == 0, ("%s: reset failed: %d", __func__, error));
958374b05e1SConrad Meyer 	(void)error;
959e974f91cSConrad Meyer }
960e974f91cSConrad Meyer 
961e974f91cSConrad Meyer /*
962e974f91cSConrad Meyer  * User API functions
963e974f91cSConrad Meyer  */
964f8f92e91SConrad Meyer unsigned
965f8f92e91SConrad Meyer ioat_get_nchannels(void)
966f8f92e91SConrad Meyer {
967f8f92e91SConrad Meyer 
968f8f92e91SConrad Meyer 	return (ioat_channel_index);
969f8f92e91SConrad Meyer }
970f8f92e91SConrad Meyer 
971e974f91cSConrad Meyer bus_dmaengine_t
9720ff814e8SConrad Meyer ioat_get_dmaengine(uint32_t index, int flags)
973e974f91cSConrad Meyer {
9740ff814e8SConrad Meyer 	struct ioat_softc *ioat;
9750ff814e8SConrad Meyer 
9760ff814e8SConrad Meyer 	KASSERT((flags & ~(M_NOWAIT | M_WAITOK)) == 0,
9770ff814e8SConrad Meyer 	    ("invalid flags: 0x%08x", flags));
9780ff814e8SConrad Meyer 	KASSERT((flags & (M_NOWAIT | M_WAITOK)) != (M_NOWAIT | M_WAITOK),
9790ff814e8SConrad Meyer 	    ("invalid wait | nowait"));
980e974f91cSConrad Meyer 
9812f03a95fSAlexander Motin 	mtx_lock(&ioat_list_mtx);
9822f03a95fSAlexander Motin 	if (index >= ioat_channel_index ||
9832f03a95fSAlexander Motin 	    (ioat = ioat_channel[index]) == NULL) {
9842f03a95fSAlexander Motin 		mtx_unlock(&ioat_list_mtx);
985e974f91cSConrad Meyer 		return (NULL);
9862f03a95fSAlexander Motin 	}
9872f03a95fSAlexander Motin 	mtx_lock(&ioat->submit_lock);
9882f03a95fSAlexander Motin 	mtx_unlock(&ioat_list_mtx);
9895f77bd3eSConrad Meyer 
9902f03a95fSAlexander Motin 	if (ioat->destroying) {
9912f03a95fSAlexander Motin 		mtx_unlock(&ioat->submit_lock);
9920ff814e8SConrad Meyer 		return (NULL);
9930ff814e8SConrad Meyer 	}
9940ff814e8SConrad Meyer 
9952f03a95fSAlexander Motin 	ioat_get(ioat);
9962f03a95fSAlexander Motin 	if (ioat->quiescing) {
9972f03a95fSAlexander Motin 		if ((flags & M_NOWAIT) != 0) {
9982f03a95fSAlexander Motin 			ioat_put(ioat);
9992f03a95fSAlexander Motin 			mtx_unlock(&ioat->submit_lock);
10002f03a95fSAlexander Motin 			return (NULL);
10012f03a95fSAlexander Motin 		}
10022f03a95fSAlexander Motin 
10032f03a95fSAlexander Motin 		while (ioat->quiescing && !ioat->destroying)
10042f03a95fSAlexander Motin 			msleep(&ioat->quiescing, &ioat->submit_lock, 0, "getdma", 0);
10052f03a95fSAlexander Motin 
10062f03a95fSAlexander Motin 		if (ioat->destroying) {
10072f03a95fSAlexander Motin 			ioat_put(ioat);
10082f03a95fSAlexander Motin 			mtx_unlock(&ioat->submit_lock);
10092f03a95fSAlexander Motin 			return (NULL);
10102f03a95fSAlexander Motin 		}
10112f03a95fSAlexander Motin 	}
10122f03a95fSAlexander Motin 	mtx_unlock(&ioat->submit_lock);
10132f03a95fSAlexander Motin 	return (&ioat->dmaengine);
1014466b3540SConrad Meyer }
1015466b3540SConrad Meyer 
1016466b3540SConrad Meyer void
1017466b3540SConrad Meyer ioat_put_dmaengine(bus_dmaengine_t dmaengine)
1018466b3540SConrad Meyer {
1019466b3540SConrad Meyer 	struct ioat_softc *ioat;
1020466b3540SConrad Meyer 
1021466b3540SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
10222f03a95fSAlexander Motin 	mtx_lock(&ioat->submit_lock);
10232f03a95fSAlexander Motin 	ioat_put(ioat);
10242f03a95fSAlexander Motin 	mtx_unlock(&ioat->submit_lock);
1025e974f91cSConrad Meyer }
1026e974f91cSConrad Meyer 
10275ca9fc2aSConrad Meyer int
102831bf2875SConrad Meyer ioat_get_hwversion(bus_dmaengine_t dmaengine)
102931bf2875SConrad Meyer {
103031bf2875SConrad Meyer 	struct ioat_softc *ioat;
103131bf2875SConrad Meyer 
103231bf2875SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
103331bf2875SConrad Meyer 	return (ioat->version);
103431bf2875SConrad Meyer }
103531bf2875SConrad Meyer 
1036bd81fe68SConrad Meyer size_t
1037bd81fe68SConrad Meyer ioat_get_max_io_size(bus_dmaengine_t dmaengine)
1038bd81fe68SConrad Meyer {
1039bd81fe68SConrad Meyer 	struct ioat_softc *ioat;
1040bd81fe68SConrad Meyer 
1041bd81fe68SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1042bd81fe68SConrad Meyer 	return (ioat->max_xfer_size);
1043bd81fe68SConrad Meyer }
1044bd81fe68SConrad Meyer 
1045f8253f1aSConrad Meyer uint32_t
1046f8253f1aSConrad Meyer ioat_get_capabilities(bus_dmaengine_t dmaengine)
1047f8253f1aSConrad Meyer {
1048f8253f1aSConrad Meyer 	struct ioat_softc *ioat;
1049f8253f1aSConrad Meyer 
1050f8253f1aSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1051f8253f1aSConrad Meyer 	return (ioat->capabilities);
1052f8253f1aSConrad Meyer }
1053f8253f1aSConrad Meyer 
105431bf2875SConrad Meyer int
10557280125eSAlexander Motin ioat_get_domain(bus_dmaengine_t dmaengine, int *domain)
10567280125eSAlexander Motin {
10577280125eSAlexander Motin 	struct ioat_softc *ioat;
10587280125eSAlexander Motin 
10597280125eSAlexander Motin 	ioat = to_ioat_softc(dmaengine);
10607280125eSAlexander Motin 	return (bus_get_domain(ioat->device, domain));
10617280125eSAlexander Motin }
10627280125eSAlexander Motin 
10637280125eSAlexander Motin int
10645ca9fc2aSConrad Meyer ioat_set_interrupt_coalesce(bus_dmaengine_t dmaengine, uint16_t delay)
10655ca9fc2aSConrad Meyer {
10665ca9fc2aSConrad Meyer 	struct ioat_softc *ioat;
10675ca9fc2aSConrad Meyer 
10685ca9fc2aSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
10695ca9fc2aSConrad Meyer 	if (!ioat->intrdelay_supported)
10705ca9fc2aSConrad Meyer 		return (ENODEV);
10715ca9fc2aSConrad Meyer 	if (delay > ioat->intrdelay_max)
10725ca9fc2aSConrad Meyer 		return (ERANGE);
10735ca9fc2aSConrad Meyer 
10745ca9fc2aSConrad Meyer 	ioat_write_2(ioat, IOAT_INTRDELAY_OFFSET, delay);
10755ca9fc2aSConrad Meyer 	ioat->cached_intrdelay =
10765ca9fc2aSConrad Meyer 	    ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) & IOAT_INTRDELAY_US_MASK;
10775ca9fc2aSConrad Meyer 	return (0);
10785ca9fc2aSConrad Meyer }
10795ca9fc2aSConrad Meyer 
10805ca9fc2aSConrad Meyer uint16_t
10815ca9fc2aSConrad Meyer ioat_get_max_coalesce_period(bus_dmaengine_t dmaengine)
10825ca9fc2aSConrad Meyer {
10835ca9fc2aSConrad Meyer 	struct ioat_softc *ioat;
10845ca9fc2aSConrad Meyer 
10855ca9fc2aSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
10865ca9fc2aSConrad Meyer 	return (ioat->intrdelay_max);
10875ca9fc2aSConrad Meyer }
10885ca9fc2aSConrad Meyer 
1089e974f91cSConrad Meyer void
1090e974f91cSConrad Meyer ioat_acquire(bus_dmaengine_t dmaengine)
1091e974f91cSConrad Meyer {
1092e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1093e974f91cSConrad Meyer 
1094e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1095e974f91cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
1096520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
109758a639b7SConrad Meyer 	ioat->acq_head = ioat->head;
1098e974f91cSConrad Meyer }
1099e974f91cSConrad Meyer 
11001502e363SConrad Meyer int
11011502e363SConrad Meyer ioat_acquire_reserve(bus_dmaengine_t dmaengine, unsigned n, int mflags)
11021502e363SConrad Meyer {
11031502e363SConrad Meyer 	struct ioat_softc *ioat;
11041502e363SConrad Meyer 	int error;
11051502e363SConrad Meyer 
11061502e363SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
11071502e363SConrad Meyer 	ioat_acquire(dmaengine);
11081502e363SConrad Meyer 
11091502e363SConrad Meyer 	error = ioat_reserve_space(ioat, n, mflags);
11101502e363SConrad Meyer 	if (error != 0)
11111502e363SConrad Meyer 		ioat_release(dmaengine);
11121502e363SConrad Meyer 	return (error);
11131502e363SConrad Meyer }
11141502e363SConrad Meyer 
1115e974f91cSConrad Meyer void
1116e974f91cSConrad Meyer ioat_release(bus_dmaengine_t dmaengine)
1117e974f91cSConrad Meyer {
1118e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1119e974f91cSConrad Meyer 
1120e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
11212f03a95fSAlexander Motin 	CTR3(KTR_IOAT, "%s channel=%u dispatch1 head=%u", __func__,
11222f03a95fSAlexander Motin 	    ioat->chan_idx, ioat->head);
112376305bb8SConrad Meyer 	KFAIL_POINT_CODE(DEBUG_FP, ioat_release, /* do nothing */);
11242f03a95fSAlexander Motin 	CTR3(KTR_IOAT, "%s channel=%u dispatch2 head=%u", __func__,
11252f03a95fSAlexander Motin 	    ioat->chan_idx, ioat->head);
112676305bb8SConrad Meyer 
112758a639b7SConrad Meyer 	if (ioat->acq_head != ioat->head) {
112858a639b7SConrad Meyer 		ioat_write_2(ioat, IOAT_DMACOUNT_OFFSET,
11292f03a95fSAlexander Motin 		    (uint16_t)ioat->head);
1130f44abf1fSConrad Meyer 
11312f03a95fSAlexander Motin 		if (!callout_pending(&ioat->poll_timer)) {
1132657dc81dSAlexander Motin 			callout_reset_on(&ioat->poll_timer, 1,
1133657dc81dSAlexander Motin 			    ioat_poll_timer_callback, ioat, ioat->cpu);
113458a639b7SConrad Meyer 		}
1135f44abf1fSConrad Meyer 	}
1136e974f91cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
1137e974f91cSConrad Meyer }
1138e974f91cSConrad Meyer 
11399e3bbf26SConrad Meyer static struct ioat_descriptor *
11409e3bbf26SConrad Meyer ioat_op_generic(struct ioat_softc *ioat, uint8_t op,
11419e3bbf26SConrad Meyer     uint32_t size, uint64_t src, uint64_t dst,
11429e3bbf26SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg,
11439e3bbf26SConrad Meyer     uint32_t flags)
1144e974f91cSConrad Meyer {
11459e3bbf26SConrad Meyer 	struct ioat_generic_hw_descriptor *hw_desc;
1146e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
1147b80b32a2STycho Nightingale 	bus_dma_segment_t seg;
1148b80b32a2STycho Nightingale 	int mflags, nseg, error;
1149e974f91cSConrad Meyer 
11509e3bbf26SConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
11519e3bbf26SConrad Meyer 
1152bec7ff79SConrad Meyer 	KASSERT((flags & ~_DMA_GENERIC_FLAGS) == 0,
1153bec7ff79SConrad Meyer 	    ("Unrecognized flag(s): %#x", flags & ~_DMA_GENERIC_FLAGS));
1154630d9800SAlexander Motin 	KASSERT(size <= ioat->max_xfer_size, ("%s: size too big (%u > %u)",
1155630d9800SAlexander Motin 	    __func__, (unsigned)size, ioat->max_xfer_size));
1156630d9800SAlexander Motin 
1157bf8553eaSConrad Meyer 	if ((flags & DMA_NO_WAIT) != 0)
1158bf8553eaSConrad Meyer 		mflags = M_NOWAIT;
1159bf8553eaSConrad Meyer 	else
1160bf8553eaSConrad Meyer 		mflags = M_WAITOK;
1161e974f91cSConrad Meyer 
1162bf8553eaSConrad Meyer 	if (ioat_reserve_space(ioat, 1, mflags) != 0)
1163e974f91cSConrad Meyer 		return (NULL);
1164e974f91cSConrad Meyer 
1165e974f91cSConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->head);
11668e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, ioat->head)->generic;
1167e974f91cSConrad Meyer 
1168e974f91cSConrad Meyer 	hw_desc->u.control_raw = 0;
11699e3bbf26SConrad Meyer 	hw_desc->u.control_generic.op = op;
11709e3bbf26SConrad Meyer 	hw_desc->u.control_generic.completion_update = 1;
1171e974f91cSConrad Meyer 
1172e974f91cSConrad Meyer 	if ((flags & DMA_INT_EN) != 0)
11739e3bbf26SConrad Meyer 		hw_desc->u.control_generic.int_enable = 1;
11746ca07079SConrad Meyer 	if ((flags & DMA_FENCE) != 0)
11756ca07079SConrad Meyer 		hw_desc->u.control_generic.fence = 1;
1176e974f91cSConrad Meyer 
11779e3bbf26SConrad Meyer 	hw_desc->size = size;
1178b80b32a2STycho Nightingale 
1179b80b32a2STycho Nightingale 	if (src != 0) {
1180b80b32a2STycho Nightingale 		nseg = -1;
1181b80b32a2STycho Nightingale 		error = _bus_dmamap_load_phys(ioat->data_tag, desc->src_dmamap,
1182b80b32a2STycho Nightingale 		    src, size, 0, &seg, &nseg);
1183b80b32a2STycho Nightingale 		if (error != 0) {
1184b80b32a2STycho Nightingale 			ioat_log_message(0, "%s: _bus_dmamap_load_phys"
1185b80b32a2STycho Nightingale 			    " failed %d\n", __func__, error);
1186b80b32a2STycho Nightingale 			return (NULL);
1187b80b32a2STycho Nightingale 		}
1188b80b32a2STycho Nightingale 		hw_desc->src_addr = seg.ds_addr;
1189b80b32a2STycho Nightingale 	}
1190b80b32a2STycho Nightingale 
1191b80b32a2STycho Nightingale 	if (dst != 0) {
1192b80b32a2STycho Nightingale 		nseg = -1;
1193b80b32a2STycho Nightingale 		error = _bus_dmamap_load_phys(ioat->data_tag, desc->dst_dmamap,
1194b80b32a2STycho Nightingale 		    dst, size, 0, &seg, &nseg);
1195b80b32a2STycho Nightingale 		if (error != 0) {
1196b80b32a2STycho Nightingale 			ioat_log_message(0, "%s: _bus_dmamap_load_phys"
1197b80b32a2STycho Nightingale 			    " failed %d\n", __func__, error);
1198b80b32a2STycho Nightingale 			return (NULL);
1199b80b32a2STycho Nightingale 		}
1200b80b32a2STycho Nightingale 		hw_desc->dest_addr = seg.ds_addr;
1201b80b32a2STycho Nightingale 	}
1202e974f91cSConrad Meyer 
1203e974f91cSConrad Meyer 	desc->bus_dmadesc.callback_fn = callback_fn;
1204e974f91cSConrad Meyer 	desc->bus_dmadesc.callback_arg = callback_arg;
12059e3bbf26SConrad Meyer 	return (desc);
12069e3bbf26SConrad Meyer }
1207e974f91cSConrad Meyer 
12089e3bbf26SConrad Meyer struct bus_dmadesc *
12099e3bbf26SConrad Meyer ioat_null(bus_dmaengine_t dmaengine, bus_dmaengine_callback_t callback_fn,
12109e3bbf26SConrad Meyer     void *callback_arg, uint32_t flags)
12119e3bbf26SConrad Meyer {
12129e3bbf26SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
12139e3bbf26SConrad Meyer 	struct ioat_descriptor *desc;
12149e3bbf26SConrad Meyer 	struct ioat_softc *ioat;
12159e3bbf26SConrad Meyer 
12169e3bbf26SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1217520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
12189e3bbf26SConrad Meyer 
12199e3bbf26SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, 8, 0, 0, callback_fn,
12209e3bbf26SConrad Meyer 	    callback_arg, flags);
12219e3bbf26SConrad Meyer 	if (desc == NULL)
12229e3bbf26SConrad Meyer 		return (NULL);
12239e3bbf26SConrad Meyer 
12248e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->dma;
12259e3bbf26SConrad Meyer 	hw_desc->u.control.null = 1;
1226e974f91cSConrad Meyer 	ioat_submit_single(ioat);
1227e974f91cSConrad Meyer 	return (&desc->bus_dmadesc);
1228e974f91cSConrad Meyer }
1229e974f91cSConrad Meyer 
1230e974f91cSConrad Meyer struct bus_dmadesc *
1231e974f91cSConrad Meyer ioat_copy(bus_dmaengine_t dmaengine, bus_addr_t dst,
1232e974f91cSConrad Meyer     bus_addr_t src, bus_size_t len, bus_dmaengine_callback_t callback_fn,
1233e974f91cSConrad Meyer     void *callback_arg, uint32_t flags)
1234e974f91cSConrad Meyer {
1235e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
12369e3bbf26SConrad Meyer 	struct ioat_descriptor *desc;
1237e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1238e974f91cSConrad Meyer 
1239e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
12409e3bbf26SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, len, src, dst, callback_fn,
12419e3bbf26SConrad Meyer 	    callback_arg, flags);
12429e3bbf26SConrad Meyer 	if (desc == NULL)
1243e974f91cSConrad Meyer 		return (NULL);
1244e974f91cSConrad Meyer 
12458e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->dma;
1246e974f91cSConrad Meyer 	if (g_ioat_debug_level >= 3)
1247e974f91cSConrad Meyer 		dump_descriptor(hw_desc);
1248e974f91cSConrad Meyer 
1249e974f91cSConrad Meyer 	ioat_submit_single(ioat);
125005e4cffbSConrad Meyer 	CTR6(KTR_IOAT, "%s channel=%u desc=%p dest=%lx src=%lx len=%lx",
125105e4cffbSConrad Meyer 	    __func__, ioat->chan_idx, &desc->bus_dmadesc, dst, src, len);
1252e974f91cSConrad Meyer 	return (&desc->bus_dmadesc);
1253e974f91cSConrad Meyer }
1254e974f91cSConrad Meyer 
12552a4fd6b1SConrad Meyer struct bus_dmadesc *
12569950fde0SConrad Meyer ioat_copy_8k_aligned(bus_dmaengine_t dmaengine, bus_addr_t dst1,
12579950fde0SConrad Meyer     bus_addr_t dst2, bus_addr_t src1, bus_addr_t src2,
12589950fde0SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
12599950fde0SConrad Meyer {
12609950fde0SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
12619950fde0SConrad Meyer 	struct ioat_descriptor *desc;
12629950fde0SConrad Meyer 	struct ioat_softc *ioat;
1263b80b32a2STycho Nightingale 	bus_size_t src1_len, dst1_len;
1264b80b32a2STycho Nightingale 	bus_dma_segment_t seg;
1265b80b32a2STycho Nightingale 	int nseg, error;
12669950fde0SConrad Meyer 
12679950fde0SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1268520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
12699950fde0SConrad Meyer 
1270630d9800SAlexander Motin 	KASSERT(((src1 | src2 | dst1 | dst2) & PAGE_MASK) == 0,
1271630d9800SAlexander Motin 	    ("%s: addresses are not page-aligned", __func__));
12729950fde0SConrad Meyer 
1273b80b32a2STycho Nightingale 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, 2 * PAGE_SIZE, 0, 0,
12749950fde0SConrad Meyer 	    callback_fn, callback_arg, flags);
12759950fde0SConrad Meyer 	if (desc == NULL)
12769950fde0SConrad Meyer 		return (NULL);
12779950fde0SConrad Meyer 
12788e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->dma;
1279b80b32a2STycho Nightingale 
1280b80b32a2STycho Nightingale 	src1_len = (src2 != src1 + PAGE_SIZE) ? PAGE_SIZE : 2 * PAGE_SIZE;
1281b80b32a2STycho Nightingale 	nseg = -1;
1282b80b32a2STycho Nightingale 	error = _bus_dmamap_load_phys(ioat->data_tag,
1283b80b32a2STycho Nightingale 	    desc->src_dmamap, src1, src1_len, 0, &seg, &nseg);
1284b80b32a2STycho Nightingale 	if (error != 0) {
1285b80b32a2STycho Nightingale 		ioat_log_message(0, "%s: _bus_dmamap_load_phys"
1286b80b32a2STycho Nightingale 		    " failed %d\n", __func__, error);
1287b80b32a2STycho Nightingale 		return (NULL);
12889950fde0SConrad Meyer 	}
1289b80b32a2STycho Nightingale 	hw_desc->src_addr = seg.ds_addr;
1290b80b32a2STycho Nightingale 	if (src1_len != 2 * PAGE_SIZE) {
1291b80b32a2STycho Nightingale 		hw_desc->u.control.src_page_break = 1;
1292b80b32a2STycho Nightingale 		nseg = -1;
1293b80b32a2STycho Nightingale 		error = _bus_dmamap_load_phys(ioat->data_tag,
1294b80b32a2STycho Nightingale 		    desc->src2_dmamap, src2, PAGE_SIZE, 0, &seg, &nseg);
1295b80b32a2STycho Nightingale 		if (error != 0) {
1296b80b32a2STycho Nightingale 			ioat_log_message(0, "%s: _bus_dmamap_load_phys"
1297b80b32a2STycho Nightingale 			    " failed %d\n", __func__, error);
1298b80b32a2STycho Nightingale 			return (NULL);
1299b80b32a2STycho Nightingale 		}
1300b80b32a2STycho Nightingale 		hw_desc->next_src_addr = seg.ds_addr;
1301b80b32a2STycho Nightingale 	}
1302b80b32a2STycho Nightingale 
1303b80b32a2STycho Nightingale 	dst1_len = (dst2 != dst1 + PAGE_SIZE) ? PAGE_SIZE : 2 * PAGE_SIZE;
1304b80b32a2STycho Nightingale 	nseg = -1;
1305b80b32a2STycho Nightingale 	error = _bus_dmamap_load_phys(ioat->data_tag,
1306b80b32a2STycho Nightingale 	    desc->dst_dmamap, dst1, dst1_len, 0, &seg, &nseg);
1307b80b32a2STycho Nightingale 	if (error != 0) {
1308b80b32a2STycho Nightingale 		ioat_log_message(0, "%s: _bus_dmamap_load_phys"
1309b80b32a2STycho Nightingale 		    " failed %d\n", __func__, error);
1310b80b32a2STycho Nightingale 		return (NULL);
1311b80b32a2STycho Nightingale 	}
1312b80b32a2STycho Nightingale 	hw_desc->dest_addr = seg.ds_addr;
1313b80b32a2STycho Nightingale 	if (dst1_len != 2 * PAGE_SIZE) {
13149950fde0SConrad Meyer 		hw_desc->u.control.dest_page_break = 1;
1315b80b32a2STycho Nightingale 		nseg = -1;
1316b80b32a2STycho Nightingale 		error = _bus_dmamap_load_phys(ioat->data_tag,
1317b80b32a2STycho Nightingale 		    desc->dst2_dmamap, dst2, PAGE_SIZE, 0, &seg, &nseg);
1318b80b32a2STycho Nightingale 		if (error != 0) {
1319b80b32a2STycho Nightingale 			ioat_log_message(0, "%s: _bus_dmamap_load_phys"
1320b80b32a2STycho Nightingale 			    " failed %d\n", __func__, error);
1321b80b32a2STycho Nightingale 			return (NULL);
1322b80b32a2STycho Nightingale 		}
1323b80b32a2STycho Nightingale 		hw_desc->next_dest_addr = seg.ds_addr;
13249950fde0SConrad Meyer 	}
13259950fde0SConrad Meyer 
13269950fde0SConrad Meyer 	if (g_ioat_debug_level >= 3)
13279950fde0SConrad Meyer 		dump_descriptor(hw_desc);
13289950fde0SConrad Meyer 
13299950fde0SConrad Meyer 	ioat_submit_single(ioat);
13309950fde0SConrad Meyer 	return (&desc->bus_dmadesc);
13319950fde0SConrad Meyer }
13329950fde0SConrad Meyer 
13339950fde0SConrad Meyer struct bus_dmadesc *
1334bec7ff79SConrad Meyer ioat_copy_crc(bus_dmaengine_t dmaengine, bus_addr_t dst, bus_addr_t src,
1335bec7ff79SConrad Meyer     bus_size_t len, uint32_t *initialseed, bus_addr_t crcptr,
1336bec7ff79SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
1337bec7ff79SConrad Meyer {
1338bec7ff79SConrad Meyer 	struct ioat_crc32_hw_descriptor *hw_desc;
1339bec7ff79SConrad Meyer 	struct ioat_descriptor *desc;
1340bec7ff79SConrad Meyer 	struct ioat_softc *ioat;
1341bec7ff79SConrad Meyer 	uint32_t teststore;
1342bec7ff79SConrad Meyer 	uint8_t op;
1343b80b32a2STycho Nightingale 	bus_dma_segment_t seg;
1344b80b32a2STycho Nightingale 	int nseg, error;
1345bec7ff79SConrad Meyer 
1346bec7ff79SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1347520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1348bec7ff79SConrad Meyer 
1349630d9800SAlexander Motin 	KASSERT((ioat->capabilities & IOAT_DMACAP_MOVECRC) != 0,
1350630d9800SAlexander Motin 	    ("%s: device lacks MOVECRC capability", __func__));
1351bec7ff79SConrad Meyer 	teststore = (flags & _DMA_CRC_TESTSTORE);
1352630d9800SAlexander Motin 	KASSERT(teststore != _DMA_CRC_TESTSTORE,
1353630d9800SAlexander Motin 	    ("%s: TEST and STORE invalid", __func__));
1354630d9800SAlexander Motin 	KASSERT(teststore != 0 || (flags & DMA_CRC_INLINE) == 0,
1355630d9800SAlexander Motin 	    ("%s: INLINE invalid without TEST or STORE", __func__));
1356bec7ff79SConrad Meyer 
1357bec7ff79SConrad Meyer 	switch (teststore) {
1358bec7ff79SConrad Meyer 	case DMA_CRC_STORE:
1359bec7ff79SConrad Meyer 		op = IOAT_OP_MOVECRC_STORE;
1360bec7ff79SConrad Meyer 		break;
1361bec7ff79SConrad Meyer 	case DMA_CRC_TEST:
1362bec7ff79SConrad Meyer 		op = IOAT_OP_MOVECRC_TEST;
1363bec7ff79SConrad Meyer 		break;
1364bec7ff79SConrad Meyer 	default:
1365bec7ff79SConrad Meyer 		KASSERT(teststore == 0, ("bogus"));
1366bec7ff79SConrad Meyer 		op = IOAT_OP_MOVECRC;
1367bec7ff79SConrad Meyer 		break;
1368bec7ff79SConrad Meyer 	}
1369bec7ff79SConrad Meyer 
1370bec7ff79SConrad Meyer 	desc = ioat_op_generic(ioat, op, len, src, dst, callback_fn,
1371bec7ff79SConrad Meyer 	    callback_arg, flags & ~_DMA_CRC_FLAGS);
1372bec7ff79SConrad Meyer 	if (desc == NULL)
1373bec7ff79SConrad Meyer 		return (NULL);
1374bec7ff79SConrad Meyer 
13758e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->crc32;
1376bec7ff79SConrad Meyer 
1377b80b32a2STycho Nightingale 	if ((flags & DMA_CRC_INLINE) == 0) {
1378b80b32a2STycho Nightingale 		nseg = -1;
13791f4a469dSAlexander Motin 		error = _bus_dmamap_load_phys(ioat->data_tag,
13801f4a469dSAlexander Motin 		    desc->dst2_dmamap, crcptr, sizeof(uint32_t), 0,
1381b80b32a2STycho Nightingale 		    &seg, &nseg);
1382b80b32a2STycho Nightingale 		if (error != 0) {
1383b80b32a2STycho Nightingale 			ioat_log_message(0, "%s: _bus_dmamap_load_phys"
1384b80b32a2STycho Nightingale 			    " failed %d\n", __func__, error);
1385b80b32a2STycho Nightingale 			return (NULL);
1386b80b32a2STycho Nightingale 		}
1387b80b32a2STycho Nightingale 		hw_desc->crc_address = seg.ds_addr;
1388b80b32a2STycho Nightingale 	} else
1389bec7ff79SConrad Meyer 		hw_desc->u.control.crc_location = 1;
1390bec7ff79SConrad Meyer 
1391bec7ff79SConrad Meyer 	if (initialseed != NULL) {
1392bec7ff79SConrad Meyer 		hw_desc->u.control.use_seed = 1;
1393bec7ff79SConrad Meyer 		hw_desc->seed = *initialseed;
1394bec7ff79SConrad Meyer 	}
1395bec7ff79SConrad Meyer 
1396bec7ff79SConrad Meyer 	if (g_ioat_debug_level >= 3)
1397bec7ff79SConrad Meyer 		dump_descriptor(hw_desc);
1398bec7ff79SConrad Meyer 
1399bec7ff79SConrad Meyer 	ioat_submit_single(ioat);
1400bec7ff79SConrad Meyer 	return (&desc->bus_dmadesc);
1401bec7ff79SConrad Meyer }
1402bec7ff79SConrad Meyer 
1403bec7ff79SConrad Meyer struct bus_dmadesc *
1404bec7ff79SConrad Meyer ioat_crc(bus_dmaengine_t dmaengine, bus_addr_t src, bus_size_t len,
1405bec7ff79SConrad Meyer     uint32_t *initialseed, bus_addr_t crcptr,
1406bec7ff79SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
1407bec7ff79SConrad Meyer {
1408bec7ff79SConrad Meyer 	struct ioat_crc32_hw_descriptor *hw_desc;
1409bec7ff79SConrad Meyer 	struct ioat_descriptor *desc;
1410bec7ff79SConrad Meyer 	struct ioat_softc *ioat;
1411bec7ff79SConrad Meyer 	uint32_t teststore;
1412bec7ff79SConrad Meyer 	uint8_t op;
1413b80b32a2STycho Nightingale 	bus_dma_segment_t seg;
1414b80b32a2STycho Nightingale 	int nseg, error;
1415bec7ff79SConrad Meyer 
1416bec7ff79SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1417520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1418bec7ff79SConrad Meyer 
1419630d9800SAlexander Motin 	KASSERT((ioat->capabilities & IOAT_DMACAP_CRC) != 0,
1420630d9800SAlexander Motin 	    ("%s: device lacks CRC capability", __func__));
1421bec7ff79SConrad Meyer 	teststore = (flags & _DMA_CRC_TESTSTORE);
1422630d9800SAlexander Motin 	KASSERT(teststore != _DMA_CRC_TESTSTORE,
1423630d9800SAlexander Motin 	    ("%s: TEST and STORE invalid", __func__));
1424630d9800SAlexander Motin 	KASSERT(teststore != 0 || (flags & DMA_CRC_INLINE) == 0,
1425630d9800SAlexander Motin 	    ("%s: INLINE invalid without TEST or STORE", __func__));
1426bec7ff79SConrad Meyer 
1427bec7ff79SConrad Meyer 	switch (teststore) {
1428bec7ff79SConrad Meyer 	case DMA_CRC_STORE:
1429bec7ff79SConrad Meyer 		op = IOAT_OP_CRC_STORE;
1430bec7ff79SConrad Meyer 		break;
1431bec7ff79SConrad Meyer 	case DMA_CRC_TEST:
1432bec7ff79SConrad Meyer 		op = IOAT_OP_CRC_TEST;
1433bec7ff79SConrad Meyer 		break;
1434bec7ff79SConrad Meyer 	default:
1435bec7ff79SConrad Meyer 		KASSERT(teststore == 0, ("bogus"));
1436bec7ff79SConrad Meyer 		op = IOAT_OP_CRC;
1437bec7ff79SConrad Meyer 		break;
1438bec7ff79SConrad Meyer 	}
1439bec7ff79SConrad Meyer 
1440bec7ff79SConrad Meyer 	desc = ioat_op_generic(ioat, op, len, src, 0, callback_fn,
1441bec7ff79SConrad Meyer 	    callback_arg, flags & ~_DMA_CRC_FLAGS);
1442bec7ff79SConrad Meyer 	if (desc == NULL)
1443bec7ff79SConrad Meyer 		return (NULL);
1444bec7ff79SConrad Meyer 
14458e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->crc32;
1446bec7ff79SConrad Meyer 
1447b80b32a2STycho Nightingale 	if ((flags & DMA_CRC_INLINE) == 0) {
1448b80b32a2STycho Nightingale 		nseg = -1;
14491f4a469dSAlexander Motin 		error = _bus_dmamap_load_phys(ioat->data_tag,
14501f4a469dSAlexander Motin 		    desc->dst2_dmamap, crcptr, sizeof(uint32_t), 0,
1451b80b32a2STycho Nightingale 		    &seg, &nseg);
1452b80b32a2STycho Nightingale 		if (error != 0) {
1453b80b32a2STycho Nightingale 			ioat_log_message(0, "%s: _bus_dmamap_load_phys"
1454b80b32a2STycho Nightingale 			    " failed %d\n", __func__, error);
1455b80b32a2STycho Nightingale 			return (NULL);
1456b80b32a2STycho Nightingale 		}
1457b80b32a2STycho Nightingale 		hw_desc->crc_address = seg.ds_addr;
1458b80b32a2STycho Nightingale 	} else
1459bec7ff79SConrad Meyer 		hw_desc->u.control.crc_location = 1;
1460bec7ff79SConrad Meyer 
1461bec7ff79SConrad Meyer 	if (initialseed != NULL) {
1462bec7ff79SConrad Meyer 		hw_desc->u.control.use_seed = 1;
1463bec7ff79SConrad Meyer 		hw_desc->seed = *initialseed;
1464bec7ff79SConrad Meyer 	}
1465bec7ff79SConrad Meyer 
1466bec7ff79SConrad Meyer 	if (g_ioat_debug_level >= 3)
1467bec7ff79SConrad Meyer 		dump_descriptor(hw_desc);
1468bec7ff79SConrad Meyer 
1469bec7ff79SConrad Meyer 	ioat_submit_single(ioat);
1470bec7ff79SConrad Meyer 	return (&desc->bus_dmadesc);
1471bec7ff79SConrad Meyer }
1472bec7ff79SConrad Meyer 
1473bec7ff79SConrad Meyer struct bus_dmadesc *
14742a4fd6b1SConrad Meyer ioat_blockfill(bus_dmaengine_t dmaengine, bus_addr_t dst, uint64_t fillpattern,
14752a4fd6b1SConrad Meyer     bus_size_t len, bus_dmaengine_callback_t callback_fn, void *callback_arg,
14762a4fd6b1SConrad Meyer     uint32_t flags)
14772a4fd6b1SConrad Meyer {
14782a4fd6b1SConrad Meyer 	struct ioat_fill_hw_descriptor *hw_desc;
14792a4fd6b1SConrad Meyer 	struct ioat_descriptor *desc;
14802a4fd6b1SConrad Meyer 	struct ioat_softc *ioat;
14812a4fd6b1SConrad Meyer 
14822a4fd6b1SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1483520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
14842a4fd6b1SConrad Meyer 
1485630d9800SAlexander Motin 	KASSERT((ioat->capabilities & IOAT_DMACAP_BFILL) != 0,
1486630d9800SAlexander Motin 	    ("%s: device lacks BFILL capability", __func__));
14872a4fd6b1SConrad Meyer 
1488b80b32a2STycho Nightingale 	desc = ioat_op_generic(ioat, IOAT_OP_FILL, len, 0, dst,
14892a4fd6b1SConrad Meyer 	    callback_fn, callback_arg, flags);
14902a4fd6b1SConrad Meyer 	if (desc == NULL)
14912a4fd6b1SConrad Meyer 		return (NULL);
14922a4fd6b1SConrad Meyer 
14938e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->fill;
1494b80b32a2STycho Nightingale 	hw_desc->src_data = fillpattern;
14952a4fd6b1SConrad Meyer 	if (g_ioat_debug_level >= 3)
14962a4fd6b1SConrad Meyer 		dump_descriptor(hw_desc);
14972a4fd6b1SConrad Meyer 
14982a4fd6b1SConrad Meyer 	ioat_submit_single(ioat);
14992a4fd6b1SConrad Meyer 	return (&desc->bus_dmadesc);
15002a4fd6b1SConrad Meyer }
15012a4fd6b1SConrad Meyer 
1502e974f91cSConrad Meyer /*
1503e974f91cSConrad Meyer  * Ring Management
1504e974f91cSConrad Meyer  */
1505e974f91cSConrad Meyer static inline uint32_t
1506e974f91cSConrad Meyer ioat_get_active(struct ioat_softc *ioat)
1507e974f91cSConrad Meyer {
1508e974f91cSConrad Meyer 
1509e974f91cSConrad Meyer 	return ((ioat->head - ioat->tail) & ((1 << ioat->ring_size_order) - 1));
1510e974f91cSConrad Meyer }
1511e974f91cSConrad Meyer 
1512e974f91cSConrad Meyer static inline uint32_t
1513e974f91cSConrad Meyer ioat_get_ring_space(struct ioat_softc *ioat)
1514e974f91cSConrad Meyer {
1515e974f91cSConrad Meyer 
1516e974f91cSConrad Meyer 	return ((1 << ioat->ring_size_order) - ioat_get_active(ioat) - 1);
1517e974f91cSConrad Meyer }
1518e974f91cSConrad Meyer 
1519bf8553eaSConrad Meyer /*
1520bf8553eaSConrad Meyer  * Reserves space in this IOAT descriptor ring by ensuring enough slots remain
1521bf8553eaSConrad Meyer  * for 'num_descs'.
1522bf8553eaSConrad Meyer  *
1523bf8553eaSConrad Meyer  * If mflags contains M_WAITOK, blocks until enough space is available.
1524bf8553eaSConrad Meyer  *
1525bf8553eaSConrad Meyer  * Returns zero on success, or an errno on error.  If num_descs is beyond the
1526bf8553eaSConrad Meyer  * maximum ring size, returns EINVAl; if allocation would block and mflags
1527bf8553eaSConrad Meyer  * contains M_NOWAIT, returns EAGAIN.
1528bf8553eaSConrad Meyer  *
1529bf8553eaSConrad Meyer  * Must be called with the submit_lock held; returns with the lock held.  The
1530bf8553eaSConrad Meyer  * lock may be dropped to allocate the ring.
1531bf8553eaSConrad Meyer  *
1532bf8553eaSConrad Meyer  * (The submit_lock is needed to add any entries to the ring, so callers are
1533bf8553eaSConrad Meyer  * assured enough room is available.)
1534bf8553eaSConrad Meyer  */
1535e974f91cSConrad Meyer static int
1536bf8553eaSConrad Meyer ioat_reserve_space(struct ioat_softc *ioat, uint32_t num_descs, int mflags)
1537e974f91cSConrad Meyer {
153825ad9585SConrad Meyer 	boolean_t dug;
1539bf8553eaSConrad Meyer 	int error;
1540e974f91cSConrad Meyer 
1541bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1542bf8553eaSConrad Meyer 	error = 0;
154325ad9585SConrad Meyer 	dug = FALSE;
1544e974f91cSConrad Meyer 
1545a0992979SConrad Meyer 	if (num_descs < 1 || num_descs >= (1 << ioat->ring_size_order)) {
1546bf8553eaSConrad Meyer 		error = EINVAL;
1547bf8553eaSConrad Meyer 		goto out;
1548e974f91cSConrad Meyer 	}
154908a6b96aSConrad Meyer 
155008a6b96aSConrad Meyer 	for (;;) {
15515f77bd3eSConrad Meyer 		if (ioat->quiescing) {
15525f77bd3eSConrad Meyer 			error = ENXIO;
15535f77bd3eSConrad Meyer 			goto out;
15545f77bd3eSConrad Meyer 		}
1555bf8553eaSConrad Meyer 
1556bf8553eaSConrad Meyer 		if (ioat_get_ring_space(ioat) >= num_descs)
1557bf8553eaSConrad Meyer 			goto out;
1558bf8553eaSConrad Meyer 
155905e4cffbSConrad Meyer 		CTR3(KTR_IOAT, "%s channel=%u starved (%u)", __func__,
156005e4cffbSConrad Meyer 		    ioat->chan_idx, num_descs);
156105e4cffbSConrad Meyer 
1562a0992979SConrad Meyer 		if (!dug && !ioat->is_submitter_processing) {
156325ad9585SConrad Meyer 			ioat->is_submitter_processing = TRUE;
156425ad9585SConrad Meyer 			mtx_unlock(&ioat->submit_lock);
156525ad9585SConrad Meyer 
156605e4cffbSConrad Meyer 			CTR2(KTR_IOAT, "%s channel=%u attempting to process events",
156705e4cffbSConrad Meyer 			    __func__, ioat->chan_idx);
15682f03a95fSAlexander Motin 			ioat_process_events(ioat, FALSE);
156925ad9585SConrad Meyer 
157025ad9585SConrad Meyer 			mtx_lock(&ioat->submit_lock);
157125ad9585SConrad Meyer 			dug = TRUE;
157225ad9585SConrad Meyer 			KASSERT(ioat->is_submitter_processing == TRUE,
157325ad9585SConrad Meyer 			    ("is_submitter_processing"));
157425ad9585SConrad Meyer 			ioat->is_submitter_processing = FALSE;
157525ad9585SConrad Meyer 			wakeup(&ioat->tail);
157625ad9585SConrad Meyer 			continue;
157725ad9585SConrad Meyer 		}
157825ad9585SConrad Meyer 
1579a0992979SConrad Meyer 		if ((mflags & M_WAITOK) == 0) {
1580a0992979SConrad Meyer 			error = EAGAIN;
1581a0992979SConrad Meyer 			break;
1582a0992979SConrad Meyer 		}
158305e4cffbSConrad Meyer 		CTR2(KTR_IOAT, "%s channel=%u blocking on completions",
158405e4cffbSConrad Meyer 		    __func__, ioat->chan_idx);
1585bf8553eaSConrad Meyer 		msleep(&ioat->tail, &ioat->submit_lock, 0,
1586a0992979SConrad Meyer 		    "ioat_full", 0);
1587bf8553eaSConrad Meyer 		continue;
1588bf8553eaSConrad Meyer 	}
1589bf8553eaSConrad Meyer 
1590bf8553eaSConrad Meyer out:
1591bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
159208a6b96aSConrad Meyer 	KASSERT(!ioat->quiescing || error == ENXIO,
159308a6b96aSConrad Meyer 	    ("reserved during quiesce"));
1594bf8553eaSConrad Meyer 	return (error);
1595bf8553eaSConrad Meyer }
1596bf8553eaSConrad Meyer 
1597bf8553eaSConrad Meyer static void
1598bf8553eaSConrad Meyer ioat_free_ring(struct ioat_softc *ioat, uint32_t size,
15998e269d99SConrad Meyer     struct ioat_descriptor *ring)
1600bf8553eaSConrad Meyer {
1601bf8553eaSConrad Meyer 
160296ad26eeSMark Johnston 	free(ring, M_IOAT);
1603e974f91cSConrad Meyer }
1604e974f91cSConrad Meyer 
1605e974f91cSConrad Meyer static struct ioat_descriptor *
1606e974f91cSConrad Meyer ioat_get_ring_entry(struct ioat_softc *ioat, uint32_t index)
1607e974f91cSConrad Meyer {
1608e974f91cSConrad Meyer 
16098e269d99SConrad Meyer 	return (&ioat->ring[index % (1 << ioat->ring_size_order)]);
16108e269d99SConrad Meyer }
16118e269d99SConrad Meyer 
16128e269d99SConrad Meyer static union ioat_hw_descriptor *
16138e269d99SConrad Meyer ioat_get_descriptor(struct ioat_softc *ioat, uint32_t index)
16148e269d99SConrad Meyer {
16158e269d99SConrad Meyer 
16168e269d99SConrad Meyer 	return (&ioat->hw_desc_ring[index % (1 << ioat->ring_size_order)]);
1617e974f91cSConrad Meyer }
1618e974f91cSConrad Meyer 
1619e974f91cSConrad Meyer static void
16208f274637SConrad Meyer ioat_halted_debug(struct ioat_softc *ioat, uint32_t chanerr)
1621e974f91cSConrad Meyer {
16228e269d99SConrad Meyer 	union ioat_hw_descriptor *desc;
16238f274637SConrad Meyer 
162459acd4baSConrad Meyer 	ioat_log_message(0, "Channel halted (%b)\n", (int)chanerr,
162559acd4baSConrad Meyer 	    IOAT_CHANERR_STR);
16268f274637SConrad Meyer 	if (chanerr == 0)
16278f274637SConrad Meyer 		return;
16288f274637SConrad Meyer 
1629faefad9cSConrad Meyer 	mtx_assert(&ioat->cleanup_lock, MA_OWNED);
1630faefad9cSConrad Meyer 
16318e269d99SConrad Meyer 	desc = ioat_get_descriptor(ioat, ioat->tail + 0);
16328e269d99SConrad Meyer 	dump_descriptor(desc);
16338f274637SConrad Meyer 
16348e269d99SConrad Meyer 	desc = ioat_get_descriptor(ioat, ioat->tail + 1);
16358e269d99SConrad Meyer 	dump_descriptor(desc);
16368f274637SConrad Meyer }
16378f274637SConrad Meyer 
16388f274637SConrad Meyer static void
16395ac77963SConrad Meyer ioat_poll_timer_callback(void *arg)
16405ac77963SConrad Meyer {
16415ac77963SConrad Meyer 	struct ioat_softc *ioat;
16425ac77963SConrad Meyer 
16435ac77963SConrad Meyer 	ioat = arg;
1644630d9800SAlexander Motin 	CTR1(KTR_IOAT, "%s", __func__);
16455ac77963SConrad Meyer 
16462f03a95fSAlexander Motin 	ioat_process_events(ioat, FALSE);
16472f03a95fSAlexander Motin 
16482f03a95fSAlexander Motin 	mtx_lock(&ioat->submit_lock);
16492f03a95fSAlexander Motin 	if (ioat_get_active(ioat) > 0)
16502f03a95fSAlexander Motin 		callout_schedule(&ioat->poll_timer, 1);
16512f03a95fSAlexander Motin 	mtx_unlock(&ioat->submit_lock);
16525ac77963SConrad Meyer }
16535ac77963SConrad Meyer 
1654e974f91cSConrad Meyer /*
1655e974f91cSConrad Meyer  * Support Functions
1656e974f91cSConrad Meyer  */
1657e974f91cSConrad Meyer static void
1658e974f91cSConrad Meyer ioat_submit_single(struct ioat_softc *ioat)
1659e974f91cSConrad Meyer {
1660e974f91cSConrad Meyer 
16615a8af401SConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
16625a8af401SConrad Meyer 
16632f03a95fSAlexander Motin 	ioat->head++;
16642f03a95fSAlexander Motin 	CTR4(KTR_IOAT, "%s channel=%u head=%u tail=%u", __func__,
16652f03a95fSAlexander Motin 	    ioat->chan_idx, ioat->head, ioat->tail);
1666e974f91cSConrad Meyer 
166701fbbc88SConrad Meyer 	ioat->stats.descriptors_submitted++;
1668e974f91cSConrad Meyer }
1669e974f91cSConrad Meyer 
1670e974f91cSConrad Meyer static int
1671e974f91cSConrad Meyer ioat_reset_hw(struct ioat_softc *ioat)
1672e974f91cSConrad Meyer {
1673e974f91cSConrad Meyer 	uint64_t status;
1674e974f91cSConrad Meyer 	uint32_t chanerr;
1675cea5b880SConrad Meyer 	unsigned timeout;
16765f77bd3eSConrad Meyer 	int error;
16775f77bd3eSConrad Meyer 
1678520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
16796d41e1edSConrad Meyer 
16802f03a95fSAlexander Motin 	mtx_lock(&ioat->submit_lock);
168193f7f84aSConrad Meyer 	while (ioat->resetting && !ioat->destroying)
16822f03a95fSAlexander Motin 		msleep(&ioat->resetting, &ioat->submit_lock, 0, "IRH_drain", 0);
168393f7f84aSConrad Meyer 	if (ioat->destroying) {
16842f03a95fSAlexander Motin 		mtx_unlock(&ioat->submit_lock);
168593f7f84aSConrad Meyer 		return (ENXIO);
168693f7f84aSConrad Meyer 	}
168793f7f84aSConrad Meyer 	ioat->resetting = TRUE;
16885f77bd3eSConrad Meyer 	ioat->quiescing = TRUE;
16892f03a95fSAlexander Motin 	mtx_unlock(&ioat->submit_lock);
16902f03a95fSAlexander Motin 	mtx_lock(&ioat->cleanup_lock);
16912f03a95fSAlexander Motin 	while (ioat_get_active(ioat) > 0)
16922f03a95fSAlexander Motin 		msleep(&ioat->tail, &ioat->cleanup_lock, 0, "ioat_drain", 1);
1693e974f91cSConrad Meyer 
1694fe8712f8SConrad Meyer 	/*
1695fe8712f8SConrad Meyer 	 * Suspend ioat_process_events while the hardware and softc are in an
1696fe8712f8SConrad Meyer 	 * indeterminate state.
1697fe8712f8SConrad Meyer 	 */
1698fe8712f8SConrad Meyer 	ioat->resetting_cleanup = TRUE;
1699fe8712f8SConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
1700fe8712f8SConrad Meyer 
1701ee5642bbSConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u quiesced and drained", __func__,
1702ee5642bbSConrad Meyer 	    ioat->chan_idx);
1703ee5642bbSConrad Meyer 
1704e974f91cSConrad Meyer 	status = ioat_get_chansts(ioat);
1705e974f91cSConrad Meyer 	if (is_ioat_active(status) || is_ioat_idle(status))
1706e974f91cSConrad Meyer 		ioat_suspend(ioat);
1707e974f91cSConrad Meyer 
1708e974f91cSConrad Meyer 	/* Wait at most 20 ms */
1709e974f91cSConrad Meyer 	for (timeout = 0; (is_ioat_active(status) || is_ioat_idle(status)) &&
1710e974f91cSConrad Meyer 	    timeout < 20; timeout++) {
1711e974f91cSConrad Meyer 		DELAY(1000);
1712e974f91cSConrad Meyer 		status = ioat_get_chansts(ioat);
1713e974f91cSConrad Meyer 	}
17145f77bd3eSConrad Meyer 	if (timeout == 20) {
17155f77bd3eSConrad Meyer 		error = ETIMEDOUT;
17165f77bd3eSConrad Meyer 		goto out;
17175f77bd3eSConrad Meyer 	}
1718e974f91cSConrad Meyer 
1719cea5b880SConrad Meyer 	KASSERT(ioat_get_active(ioat) == 0, ("active after quiesce"));
1720cea5b880SConrad Meyer 
1721e974f91cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
1722e974f91cSConrad Meyer 	ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
1723e974f91cSConrad Meyer 
1724ee5642bbSConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u hardware suspended", __func__,
1725ee5642bbSConrad Meyer 	    ioat->chan_idx);
1726ee5642bbSConrad Meyer 
1727e974f91cSConrad Meyer 	/*
1728e974f91cSConrad Meyer 	 * IOAT v3 workaround - CHANERRMSK_INT with 3E07h to masks out errors
1729e974f91cSConrad Meyer 	 *  that can cause stability issues for IOAT v3.
1730e974f91cSConrad Meyer 	 */
1731e974f91cSConrad Meyer 	pci_write_config(ioat->device, IOAT_CFG_CHANERRMASK_INT_OFFSET, 0x3e07,
1732e974f91cSConrad Meyer 	    4);
1733e974f91cSConrad Meyer 	chanerr = pci_read_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, 4);
1734e974f91cSConrad Meyer 	pci_write_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, chanerr, 4);
1735e974f91cSConrad Meyer 
17360d1a05d9SConrad Meyer 	/*
17370d1a05d9SConrad Meyer 	 * BDXDE and BWD models reset MSI-X registers on device reset.
17380d1a05d9SConrad Meyer 	 * Save/restore their contents manually.
17390d1a05d9SConrad Meyer 	 */
1740f7157235SConrad Meyer 	if (ioat_model_resets_msix(ioat)) {
1741f7157235SConrad Meyer 		ioat_log_message(1, "device resets MSI-X registers; saving\n");
17420d1a05d9SConrad Meyer 		pci_save_state(ioat->device);
1743f7157235SConrad Meyer 	}
17440d1a05d9SConrad Meyer 
1745e974f91cSConrad Meyer 	ioat_reset(ioat);
1746ee5642bbSConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u hardware reset", __func__,
1747ee5642bbSConrad Meyer 	    ioat->chan_idx);
1748e974f91cSConrad Meyer 
1749e974f91cSConrad Meyer 	/* Wait at most 20 ms */
1750e974f91cSConrad Meyer 	for (timeout = 0; ioat_reset_pending(ioat) && timeout < 20; timeout++)
1751e974f91cSConrad Meyer 		DELAY(1000);
17525f77bd3eSConrad Meyer 	if (timeout == 20) {
17535f77bd3eSConrad Meyer 		error = ETIMEDOUT;
17545f77bd3eSConrad Meyer 		goto out;
17555f77bd3eSConrad Meyer 	}
1756e974f91cSConrad Meyer 
1757f7157235SConrad Meyer 	if (ioat_model_resets_msix(ioat)) {
1758f7157235SConrad Meyer 		ioat_log_message(1, "device resets registers; restored\n");
17590d1a05d9SConrad Meyer 		pci_restore_state(ioat->device);
1760f7157235SConrad Meyer 	}
17614253ea50SConrad Meyer 
1762cea5b880SConrad Meyer 	/* Reset attempts to return the hardware to "halted." */
1763cea5b880SConrad Meyer 	status = ioat_get_chansts(ioat);
1764cea5b880SConrad Meyer 	if (is_ioat_active(status) || is_ioat_idle(status)) {
1765cea5b880SConrad Meyer 		/* So this really shouldn't happen... */
1766cea5b880SConrad Meyer 		ioat_log_message(0, "Device is active after a reset?\n");
1767cea5b880SConrad Meyer 		ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
17685f77bd3eSConrad Meyer 		error = 0;
17695f77bd3eSConrad Meyer 		goto out;
1770e974f91cSConrad Meyer 	}
1771e974f91cSConrad Meyer 
1772cea5b880SConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
17735f77bd3eSConrad Meyer 	if (chanerr != 0) {
1774faefad9cSConrad Meyer 		mtx_lock(&ioat->cleanup_lock);
1775faefad9cSConrad Meyer 		ioat_halted_debug(ioat, chanerr);
1776faefad9cSConrad Meyer 		mtx_unlock(&ioat->cleanup_lock);
17775f77bd3eSConrad Meyer 		error = EIO;
17785f77bd3eSConrad Meyer 		goto out;
17795f77bd3eSConrad Meyer 	}
1780cea5b880SConrad Meyer 
1781cea5b880SConrad Meyer 	/*
1782cea5b880SConrad Meyer 	 * Bring device back online after reset.  Writing CHAINADDR brings the
1783cea5b880SConrad Meyer 	 * device back to active.
1784cea5b880SConrad Meyer 	 *
1785cea5b880SConrad Meyer 	 * The internal ring counter resets to zero, so we have to start over
1786cea5b880SConrad Meyer 	 * at zero as well.
1787cea5b880SConrad Meyer 	 */
17882f03a95fSAlexander Motin 	ioat->tail = ioat->head = 0;
1789348efb14SAlexander Motin 	*ioat->comp_update = ioat->last_seen =
1790348efb14SAlexander Motin 	    RING_PHYS_ADDR(ioat, ioat->tail - 1);
1791cea5b880SConrad Meyer 
1792cea5b880SConrad Meyer 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
1793cea5b880SConrad Meyer 	ioat_write_chancmp(ioat, ioat->comp_update_bus_addr);
17948e269d99SConrad Meyer 	ioat_write_chainaddr(ioat, RING_PHYS_ADDR(ioat, 0));
17955f77bd3eSConrad Meyer 	error = 0;
1796ee5642bbSConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u configured channel", __func__,
1797ee5642bbSConrad Meyer 	    ioat->chan_idx);
17985f77bd3eSConrad Meyer 
17995f77bd3eSConrad Meyer out:
18001bbc06b8SConrad Meyer 	/* Enqueues a null operation and ensures it completes. */
1801ee5642bbSConrad Meyer 	if (error == 0) {
18021bbc06b8SConrad Meyer 		error = ioat_start_channel(ioat);
1803ee5642bbSConrad Meyer 		CTR2(KTR_IOAT, "%s channel=%u started channel", __func__,
1804ee5642bbSConrad Meyer 		    ioat->chan_idx);
1805ee5642bbSConrad Meyer 	}
18061bbc06b8SConrad Meyer 
1807fe8712f8SConrad Meyer 	/*
1808fe8712f8SConrad Meyer 	 * Resume completions now that ring state is consistent.
1809fe8712f8SConrad Meyer 	 */
1810fe8712f8SConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
1811fe8712f8SConrad Meyer 	ioat->resetting_cleanup = FALSE;
1812fe8712f8SConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
181393f7f84aSConrad Meyer 
1814fe8712f8SConrad Meyer 	/* Unblock submission of new work */
18152f03a95fSAlexander Motin 	mtx_lock(&ioat->submit_lock);
1816fe8712f8SConrad Meyer 	ioat->quiescing = FALSE;
1817fe8712f8SConrad Meyer 	wakeup(&ioat->quiescing);
1818fe8712f8SConrad Meyer 
1819fe8712f8SConrad Meyer 	ioat->resetting = FALSE;
1820fe8712f8SConrad Meyer 	wakeup(&ioat->resetting);
18211bbc06b8SConrad Meyer 
1822ee5642bbSConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u reset done", __func__, ioat->chan_idx);
18232f03a95fSAlexander Motin 	mtx_unlock(&ioat->submit_lock);
1824fe8712f8SConrad Meyer 
18255f77bd3eSConrad Meyer 	return (error);
1826cea5b880SConrad Meyer }
1827cea5b880SConrad Meyer 
1828f7157235SConrad Meyer static int
1829faefad9cSConrad Meyer sysctl_handle_chansts(SYSCTL_HANDLER_ARGS)
1830faefad9cSConrad Meyer {
1831faefad9cSConrad Meyer 	struct ioat_softc *ioat;
1832faefad9cSConrad Meyer 	struct sbuf sb;
1833faefad9cSConrad Meyer 	uint64_t status;
1834faefad9cSConrad Meyer 	int error;
1835faefad9cSConrad Meyer 
1836faefad9cSConrad Meyer 	ioat = arg1;
1837faefad9cSConrad Meyer 
1838faefad9cSConrad Meyer 	status = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS;
1839faefad9cSConrad Meyer 
1840faefad9cSConrad Meyer 	sbuf_new_for_sysctl(&sb, NULL, 256, req);
1841faefad9cSConrad Meyer 	switch (status) {
1842faefad9cSConrad Meyer 	case IOAT_CHANSTS_ACTIVE:
1843faefad9cSConrad Meyer 		sbuf_printf(&sb, "ACTIVE");
1844faefad9cSConrad Meyer 		break;
1845faefad9cSConrad Meyer 	case IOAT_CHANSTS_IDLE:
1846faefad9cSConrad Meyer 		sbuf_printf(&sb, "IDLE");
1847faefad9cSConrad Meyer 		break;
1848faefad9cSConrad Meyer 	case IOAT_CHANSTS_SUSPENDED:
1849faefad9cSConrad Meyer 		sbuf_printf(&sb, "SUSPENDED");
1850faefad9cSConrad Meyer 		break;
1851faefad9cSConrad Meyer 	case IOAT_CHANSTS_HALTED:
1852faefad9cSConrad Meyer 		sbuf_printf(&sb, "HALTED");
1853faefad9cSConrad Meyer 		break;
1854faefad9cSConrad Meyer 	case IOAT_CHANSTS_ARMED:
1855faefad9cSConrad Meyer 		sbuf_printf(&sb, "ARMED");
1856faefad9cSConrad Meyer 		break;
1857faefad9cSConrad Meyer 	default:
1858faefad9cSConrad Meyer 		sbuf_printf(&sb, "UNKNOWN");
1859faefad9cSConrad Meyer 		break;
1860faefad9cSConrad Meyer 	}
1861faefad9cSConrad Meyer 	error = sbuf_finish(&sb);
1862faefad9cSConrad Meyer 	sbuf_delete(&sb);
1863faefad9cSConrad Meyer 
1864faefad9cSConrad Meyer 	if (error != 0 || req->newptr == NULL)
1865faefad9cSConrad Meyer 		return (error);
1866faefad9cSConrad Meyer 	return (EINVAL);
1867faefad9cSConrad Meyer }
1868faefad9cSConrad Meyer 
1869faefad9cSConrad Meyer static int
187001fbbc88SConrad Meyer sysctl_handle_dpi(SYSCTL_HANDLER_ARGS)
187101fbbc88SConrad Meyer {
187201fbbc88SConrad Meyer 	struct ioat_softc *ioat;
187301fbbc88SConrad Meyer 	struct sbuf sb;
187401fbbc88SConrad Meyer #define	PRECISION	"1"
187501fbbc88SConrad Meyer 	const uintmax_t factor = 10;
187601fbbc88SConrad Meyer 	uintmax_t rate;
187701fbbc88SConrad Meyer 	int error;
187801fbbc88SConrad Meyer 
187901fbbc88SConrad Meyer 	ioat = arg1;
188001fbbc88SConrad Meyer 	sbuf_new_for_sysctl(&sb, NULL, 16, req);
188101fbbc88SConrad Meyer 
188201fbbc88SConrad Meyer 	if (ioat->stats.interrupts == 0) {
188301fbbc88SConrad Meyer 		sbuf_printf(&sb, "NaN");
188401fbbc88SConrad Meyer 		goto out;
188501fbbc88SConrad Meyer 	}
188601fbbc88SConrad Meyer 	rate = ioat->stats.descriptors_processed * factor /
188701fbbc88SConrad Meyer 	    ioat->stats.interrupts;
188801fbbc88SConrad Meyer 	sbuf_printf(&sb, "%ju.%." PRECISION "ju", rate / factor,
188901fbbc88SConrad Meyer 	    rate % factor);
189001fbbc88SConrad Meyer #undef	PRECISION
189101fbbc88SConrad Meyer out:
189201fbbc88SConrad Meyer 	error = sbuf_finish(&sb);
189301fbbc88SConrad Meyer 	sbuf_delete(&sb);
189401fbbc88SConrad Meyer 	if (error != 0 || req->newptr == NULL)
189501fbbc88SConrad Meyer 		return (error);
189601fbbc88SConrad Meyer 	return (EINVAL);
189701fbbc88SConrad Meyer }
189801fbbc88SConrad Meyer 
189901fbbc88SConrad Meyer static int
1900f7157235SConrad Meyer sysctl_handle_reset(SYSCTL_HANDLER_ARGS)
1901f7157235SConrad Meyer {
1902f7157235SConrad Meyer 	struct ioat_softc *ioat;
1903f7157235SConrad Meyer 	int error, arg;
1904f7157235SConrad Meyer 
1905f7157235SConrad Meyer 	ioat = arg1;
1906f7157235SConrad Meyer 
1907f7157235SConrad Meyer 	arg = 0;
1908f7157235SConrad Meyer 	error = SYSCTL_OUT(req, &arg, sizeof(arg));
1909f7157235SConrad Meyer 	if (error != 0 || req->newptr == NULL)
1910f7157235SConrad Meyer 		return (error);
1911f7157235SConrad Meyer 
1912f7157235SConrad Meyer 	error = SYSCTL_IN(req, &arg, sizeof(arg));
1913f7157235SConrad Meyer 	if (error != 0)
1914f7157235SConrad Meyer 		return (error);
1915f7157235SConrad Meyer 
1916f7157235SConrad Meyer 	if (arg != 0)
1917f7157235SConrad Meyer 		error = ioat_reset_hw(ioat);
1918f7157235SConrad Meyer 
1919f7157235SConrad Meyer 	return (error);
1920f7157235SConrad Meyer }
1921f7157235SConrad Meyer 
1922e974f91cSConrad Meyer static void
1923e974f91cSConrad Meyer dump_descriptor(void *hw_desc)
1924e974f91cSConrad Meyer {
1925e974f91cSConrad Meyer 	int i, j;
1926e974f91cSConrad Meyer 
1927e974f91cSConrad Meyer 	for (i = 0; i < 2; i++) {
1928e974f91cSConrad Meyer 		for (j = 0; j < 8; j++)
1929e974f91cSConrad Meyer 			printf("%08x ", ((uint32_t *)hw_desc)[i * 8 + j]);
1930e974f91cSConrad Meyer 		printf("\n");
1931e974f91cSConrad Meyer 	}
1932e974f91cSConrad Meyer }
1933e974f91cSConrad Meyer 
1934e974f91cSConrad Meyer static void
1935e974f91cSConrad Meyer ioat_setup_sysctl(device_t device)
1936e974f91cSConrad Meyer {
193701fbbc88SConrad Meyer 	struct sysctl_oid_list *par, *statpar, *state, *hammer;
1938f7157235SConrad Meyer 	struct sysctl_ctx_list *ctx;
193901fbbc88SConrad Meyer 	struct sysctl_oid *tree, *tmp;
1940e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1941e974f91cSConrad Meyer 
1942e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
1943f7157235SConrad Meyer 	ctx = device_get_sysctl_ctx(device);
1944f7157235SConrad Meyer 	tree = device_get_sysctl_tree(device);
1945f7157235SConrad Meyer 	par = SYSCTL_CHILDREN(tree);
1946e974f91cSConrad Meyer 
194765e4f8adSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "version", CTLFLAG_RD,
194865e4f8adSConrad Meyer 	    &ioat->version, 0, "HW version (0xMM form)");
194965e4f8adSConrad Meyer 	SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "max_xfer_size", CTLFLAG_RD,
195065e4f8adSConrad Meyer 	    &ioat->max_xfer_size, 0, "HW maximum transfer size");
19515ca9fc2aSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "intrdelay_supported", CTLFLAG_RD,
19525ca9fc2aSConrad Meyer 	    &ioat->intrdelay_supported, 0, "Is INTRDELAY supported");
19535ca9fc2aSConrad Meyer 	SYSCTL_ADD_U16(ctx, par, OID_AUTO, "intrdelay_max", CTLFLAG_RD,
19545ca9fc2aSConrad Meyer 	    &ioat->intrdelay_max, 0,
19555ca9fc2aSConrad Meyer 	    "Maximum configurable INTRDELAY on this channel (microseconds)");
195665e4f8adSConrad Meyer 
19577029da5cSPawel Biernacki 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "state",
19587029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "IOAT channel internal state");
195901fbbc88SConrad Meyer 	state = SYSCTL_CHILDREN(tmp);
196001fbbc88SConrad Meyer 
196101fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "ring_size_order", CTLFLAG_RD,
1962bf8553eaSConrad Meyer 	    &ioat->ring_size_order, 0, "SW descriptor ring size order");
196301fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "head", CTLFLAG_RD, &ioat->head,
196401fbbc88SConrad Meyer 	    0, "SW descriptor head pointer index");
196501fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "tail", CTLFLAG_RD, &ioat->tail,
196601fbbc88SConrad Meyer 	    0, "SW descriptor tail pointer index");
1967f7157235SConrad Meyer 
196801fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, state, OID_AUTO, "last_completion", CTLFLAG_RD,
196965e4f8adSConrad Meyer 	    ioat->comp_update, "HW addr of last completion");
197065e4f8adSConrad Meyer 
197125ad9585SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_submitter_processing",
197225ad9585SConrad Meyer 	    CTLFLAG_RD, &ioat->is_submitter_processing, 0,
197325ad9585SConrad Meyer 	    "submitter processing");
197465e4f8adSConrad Meyer 
197501fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, state, OID_AUTO, "chansts",
1976b776de67SAlexander Motin 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, ioat, 0,
19777029da5cSPawel Biernacki 	    sysctl_handle_chansts, "A", "String of the channel status");
197801fbbc88SConrad Meyer 
19795ca9fc2aSConrad Meyer 	SYSCTL_ADD_U16(ctx, state, OID_AUTO, "intrdelay", CTLFLAG_RD,
19805ca9fc2aSConrad Meyer 	    &ioat->cached_intrdelay, 0,
19815ca9fc2aSConrad Meyer 	    "Current INTRDELAY on this channel (cached, microseconds)");
19825ca9fc2aSConrad Meyer 
19837029da5cSPawel Biernacki 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "hammer",
19847029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
198501fbbc88SConrad Meyer 	    "Big hammers (mostly for testing)");
198601fbbc88SConrad Meyer 	hammer = SYSCTL_CHILDREN(tmp);
198701fbbc88SConrad Meyer 
198801fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, hammer, OID_AUTO, "force_hw_reset",
1989b776de67SAlexander Motin 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, ioat, 0,
19907029da5cSPawel Biernacki 	    sysctl_handle_reset, "I", "Set to non-zero to reset the hardware");
199101fbbc88SConrad Meyer 
19927029da5cSPawel Biernacki 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "stats",
19937029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "IOAT channel statistics");
199401fbbc88SConrad Meyer 	statpar = SYSCTL_CHILDREN(tmp);
199501fbbc88SConrad Meyer 
199683b82cd4SEric van Gyzen 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "interrupts",
199783b82cd4SEric van Gyzen 	    CTLFLAG_RW | CTLFLAG_STATS, &ioat->stats.interrupts,
199801fbbc88SConrad Meyer 	    "Number of interrupts processed on this channel");
199983b82cd4SEric van Gyzen 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "descriptors",
200083b82cd4SEric van Gyzen 	    CTLFLAG_RW | CTLFLAG_STATS, &ioat->stats.descriptors_processed,
200101fbbc88SConrad Meyer 	    "Number of descriptors processed on this channel");
200283b82cd4SEric van Gyzen 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "submitted",
200383b82cd4SEric van Gyzen 	    CTLFLAG_RW | CTLFLAG_STATS, &ioat->stats.descriptors_submitted,
200401fbbc88SConrad Meyer 	    "Number of descriptors submitted to this channel");
200583b82cd4SEric van Gyzen 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "errored",
200683b82cd4SEric van Gyzen 	    CTLFLAG_RW | CTLFLAG_STATS, &ioat->stats.descriptors_error,
200701fbbc88SConrad Meyer 	    "Number of descriptors failed by channel errors");
200883b82cd4SEric van Gyzen 	SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "halts",
200983b82cd4SEric van Gyzen 	    CTLFLAG_RW | CTLFLAG_STATS, &ioat->stats.channel_halts, 0,
201001fbbc88SConrad Meyer 	    "Number of times the channel has halted");
201183b82cd4SEric van Gyzen 	SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "last_halt_chanerr",
201283b82cd4SEric van Gyzen 	    CTLFLAG_RW | CTLFLAG_STATS, &ioat->stats.last_halt_chanerr, 0,
201301fbbc88SConrad Meyer 	    "The raw CHANERR when the channel was last halted");
201401fbbc88SConrad Meyer 
201501fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, statpar, OID_AUTO, "desc_per_interrupt",
2016b776de67SAlexander Motin 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, ioat, 0,
20177029da5cSPawel Biernacki 	    sysctl_handle_dpi, "A", "Descriptors per interrupt");
2018e974f91cSConrad Meyer }
2019466b3540SConrad Meyer 
20202f03a95fSAlexander Motin static void
20212f03a95fSAlexander Motin ioat_get(struct ioat_softc *ioat)
2022466b3540SConrad Meyer {
2023faefad9cSConrad Meyer 
20242f03a95fSAlexander Motin 	mtx_assert(&ioat->submit_lock, MA_OWNED);
20252f03a95fSAlexander Motin 	KASSERT(ioat->refcnt < UINT32_MAX, ("refcnt overflow"));
20262f03a95fSAlexander Motin 
20272f03a95fSAlexander Motin 	ioat->refcnt++;
2028faefad9cSConrad Meyer }
2029faefad9cSConrad Meyer 
20302f03a95fSAlexander Motin static void
20312f03a95fSAlexander Motin ioat_put(struct ioat_softc *ioat)
2032faefad9cSConrad Meyer {
2033faefad9cSConrad Meyer 
20342f03a95fSAlexander Motin 	mtx_assert(&ioat->submit_lock, MA_OWNED);
20352f03a95fSAlexander Motin 	KASSERT(ioat->refcnt >= 1, ("refcnt error"));
2036faefad9cSConrad Meyer 
20372f03a95fSAlexander Motin 	if (--ioat->refcnt == 0)
20382f03a95fSAlexander Motin 		wakeup(&ioat->refcnt);
2039466b3540SConrad Meyer }
2040466b3540SConrad Meyer 
2041466b3540SConrad Meyer static void
20425f77bd3eSConrad Meyer ioat_drain_locked(struct ioat_softc *ioat)
2043466b3540SConrad Meyer {
2044466b3540SConrad Meyer 
20452f03a95fSAlexander Motin 	mtx_assert(&ioat->submit_lock, MA_OWNED);
20462f03a95fSAlexander Motin 
2047466b3540SConrad Meyer 	while (ioat->refcnt > 0)
20482f03a95fSAlexander Motin 		msleep(&ioat->refcnt, &ioat->submit_lock, 0, "ioat_drain", 0);
2049466b3540SConrad Meyer }
20500bb35debSConrad Meyer 
20510bb35debSConrad Meyer #ifdef DDB
20520bb35debSConrad Meyer #define	_db_show_lock(lo)	LOCK_CLASS(lo)->lc_ddb_show(lo)
20530bb35debSConrad Meyer #define	db_show_lock(lk)	_db_show_lock(&(lk)->lock_object)
20540bb35debSConrad Meyer DB_SHOW_COMMAND(ioat, db_show_ioat)
20550bb35debSConrad Meyer {
20560bb35debSConrad Meyer 	struct ioat_softc *sc;
20570bb35debSConrad Meyer 	unsigned idx;
20580bb35debSConrad Meyer 
20590bb35debSConrad Meyer 	if (!have_addr)
20600bb35debSConrad Meyer 		goto usage;
20610bb35debSConrad Meyer 	idx = (unsigned)addr;
206235b7a45eSConrad Meyer 	if (idx >= ioat_channel_index)
20630bb35debSConrad Meyer 		goto usage;
20640bb35debSConrad Meyer 
20650bb35debSConrad Meyer 	sc = ioat_channel[idx];
20660bb35debSConrad Meyer 	db_printf("ioat softc at %p\n", sc);
20670bb35debSConrad Meyer 	if (sc == NULL)
20680bb35debSConrad Meyer 		return;
20690bb35debSConrad Meyer 
20700bb35debSConrad Meyer 	db_printf(" version: %d\n", sc->version);
20710bb35debSConrad Meyer 	db_printf(" chan_idx: %u\n", sc->chan_idx);
20720bb35debSConrad Meyer 	db_printf(" submit_lock: ");
20730bb35debSConrad Meyer 	db_show_lock(&sc->submit_lock);
20740bb35debSConrad Meyer 
20750bb35debSConrad Meyer 	db_printf(" capabilities: %b\n", (int)sc->capabilities,
20760bb35debSConrad Meyer 	    IOAT_DMACAP_STR);
20770bb35debSConrad Meyer 	db_printf(" cached_intrdelay: %u\n", sc->cached_intrdelay);
20780bb35debSConrad Meyer 	db_printf(" *comp_update: 0x%jx\n", (uintmax_t)*sc->comp_update);
20790bb35debSConrad Meyer 
20805ac77963SConrad Meyer 	db_printf(" poll_timer:\n");
20815ac77963SConrad Meyer 	db_printf("  c_time: %ju\n", (uintmax_t)sc->poll_timer.c_time);
20825ac77963SConrad Meyer 	db_printf("  c_arg: %p\n", sc->poll_timer.c_arg);
20835ac77963SConrad Meyer 	db_printf("  c_func: %p\n", sc->poll_timer.c_func);
20845ac77963SConrad Meyer 	db_printf("  c_lock: %p\n", sc->poll_timer.c_lock);
20855ac77963SConrad Meyer 	db_printf("  c_flags: 0x%x\n", (unsigned)sc->poll_timer.c_flags);
20865ac77963SConrad Meyer 
20870bb35debSConrad Meyer 	db_printf(" quiescing: %d\n", (int)sc->quiescing);
20880bb35debSConrad Meyer 	db_printf(" destroying: %d\n", (int)sc->destroying);
208925ad9585SConrad Meyer 	db_printf(" is_submitter_processing: %d\n",
209025ad9585SConrad Meyer 	    (int)sc->is_submitter_processing);
20910bb35debSConrad Meyer 	db_printf(" intrdelay_supported: %d\n", (int)sc->intrdelay_supported);
209293f7f84aSConrad Meyer 	db_printf(" resetting: %d\n", (int)sc->resetting);
20930bb35debSConrad Meyer 
20940bb35debSConrad Meyer 	db_printf(" head: %u\n", sc->head);
20950bb35debSConrad Meyer 	db_printf(" tail: %u\n", sc->tail);
20960bb35debSConrad Meyer 	db_printf(" ring_size_order: %u\n", sc->ring_size_order);
20970bb35debSConrad Meyer 	db_printf(" last_seen: 0x%lx\n", sc->last_seen);
20980bb35debSConrad Meyer 	db_printf(" ring: %p\n", sc->ring);
20998e269d99SConrad Meyer 	db_printf(" descriptors: %p\n", sc->hw_desc_ring);
21008e269d99SConrad Meyer 	db_printf(" descriptors (phys): 0x%jx\n",
21018e269d99SConrad Meyer 	    (uintmax_t)sc->hw_desc_bus_addr);
21020bb35debSConrad Meyer 
210335b7a45eSConrad Meyer 	db_printf("  ring[%u] (tail):\n", sc->tail %
210435b7a45eSConrad Meyer 	    (1 << sc->ring_size_order));
210535b7a45eSConrad Meyer 	db_printf("   id: %u\n", ioat_get_ring_entry(sc, sc->tail)->id);
210635b7a45eSConrad Meyer 	db_printf("   addr: 0x%lx\n",
21078e269d99SConrad Meyer 	    RING_PHYS_ADDR(sc, sc->tail));
210835b7a45eSConrad Meyer 	db_printf("   next: 0x%lx\n",
21098e269d99SConrad Meyer 	     ioat_get_descriptor(sc, sc->tail)->generic.next);
211035b7a45eSConrad Meyer 
211135b7a45eSConrad Meyer 	db_printf("  ring[%u] (head - 1):\n", (sc->head - 1) %
211235b7a45eSConrad Meyer 	    (1 << sc->ring_size_order));
211335b7a45eSConrad Meyer 	db_printf("   id: %u\n", ioat_get_ring_entry(sc, sc->head - 1)->id);
211435b7a45eSConrad Meyer 	db_printf("   addr: 0x%lx\n",
21158e269d99SConrad Meyer 	    RING_PHYS_ADDR(sc, sc->head - 1));
211635b7a45eSConrad Meyer 	db_printf("   next: 0x%lx\n",
21178e269d99SConrad Meyer 	     ioat_get_descriptor(sc, sc->head - 1)->generic.next);
211835b7a45eSConrad Meyer 
211935b7a45eSConrad Meyer 	db_printf("  ring[%u] (head):\n", (sc->head) %
212035b7a45eSConrad Meyer 	    (1 << sc->ring_size_order));
212135b7a45eSConrad Meyer 	db_printf("   id: %u\n", ioat_get_ring_entry(sc, sc->head)->id);
212235b7a45eSConrad Meyer 	db_printf("   addr: 0x%lx\n",
21238e269d99SConrad Meyer 	    RING_PHYS_ADDR(sc, sc->head));
212435b7a45eSConrad Meyer 	db_printf("   next: 0x%lx\n",
21258e269d99SConrad Meyer 	     ioat_get_descriptor(sc, sc->head)->generic.next);
212635b7a45eSConrad Meyer 
212735b7a45eSConrad Meyer 	for (idx = 0; idx < (1 << sc->ring_size_order); idx++)
212835b7a45eSConrad Meyer 		if ((*sc->comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK)
21298e269d99SConrad Meyer 		    == RING_PHYS_ADDR(sc, idx))
213035b7a45eSConrad Meyer 			db_printf("  ring[%u] == hardware tail\n", idx);
213135b7a45eSConrad Meyer 
21320bb35debSConrad Meyer 	db_printf(" cleanup_lock: ");
21330bb35debSConrad Meyer 	db_show_lock(&sc->cleanup_lock);
21340bb35debSConrad Meyer 
21350bb35debSConrad Meyer 	db_printf(" refcnt: %u\n", sc->refcnt);
21360bb35debSConrad Meyer 	db_printf(" stats:\n");
21370bb35debSConrad Meyer 	db_printf("  interrupts: %lu\n", sc->stats.interrupts);
21380bb35debSConrad Meyer 	db_printf("  descriptors_processed: %lu\n", sc->stats.descriptors_processed);
21390bb35debSConrad Meyer 	db_printf("  descriptors_error: %lu\n", sc->stats.descriptors_error);
21400bb35debSConrad Meyer 	db_printf("  descriptors_submitted: %lu\n", sc->stats.descriptors_submitted);
21410bb35debSConrad Meyer 
21420bb35debSConrad Meyer 	db_printf("  channel_halts: %u\n", sc->stats.channel_halts);
21430bb35debSConrad Meyer 	db_printf("  last_halt_chanerr: %u\n", sc->stats.last_halt_chanerr);
21440bb35debSConrad Meyer 
21450bb35debSConrad Meyer 	if (db_pager_quit)
21460bb35debSConrad Meyer 		return;
21470bb35debSConrad Meyer 
21480bb35debSConrad Meyer 	db_printf(" hw status:\n");
21490bb35debSConrad Meyer 	db_printf("  status: 0x%lx\n", ioat_get_chansts(sc));
21500bb35debSConrad Meyer 	db_printf("  chanctrl: 0x%x\n",
21510bb35debSConrad Meyer 	    (unsigned)ioat_read_2(sc, IOAT_CHANCTRL_OFFSET));
21520bb35debSConrad Meyer 	db_printf("  chancmd: 0x%x\n",
21530bb35debSConrad Meyer 	    (unsigned)ioat_read_1(sc, IOAT_CHANCMD_OFFSET));
21540bb35debSConrad Meyer 	db_printf("  dmacount: 0x%x\n",
21550bb35debSConrad Meyer 	    (unsigned)ioat_read_2(sc, IOAT_DMACOUNT_OFFSET));
21560bb35debSConrad Meyer 	db_printf("  chainaddr: 0x%lx\n",
21570bb35debSConrad Meyer 	    ioat_read_double_4(sc, IOAT_CHAINADDR_OFFSET_LOW));
21580bb35debSConrad Meyer 	db_printf("  chancmp: 0x%lx\n",
21590bb35debSConrad Meyer 	    ioat_read_double_4(sc, IOAT_CHANCMP_OFFSET_LOW));
21600bb35debSConrad Meyer 	db_printf("  chanerr: %b\n",
21610bb35debSConrad Meyer 	    (int)ioat_read_4(sc, IOAT_CHANERR_OFFSET), IOAT_CHANERR_STR);
21620bb35debSConrad Meyer 	return;
21630bb35debSConrad Meyer usage:
21640bb35debSConrad Meyer 	db_printf("usage: show ioat <0-%u>\n", ioat_channel_index);
21650bb35debSConrad Meyer 	return;
21660bb35debSConrad Meyer }
21670bb35debSConrad Meyer #endif /* DDB */
2168