xref: /freebsd/sys/dev/ioat/ioat.c (revision 7029da5c36f2d3cf6bb6c81bf551229f416399e8)
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 
73e974f91cSConrad Meyer static int ioat_probe(device_t device);
74e974f91cSConrad Meyer static int ioat_attach(device_t device);
75e974f91cSConrad Meyer static int ioat_detach(device_t device);
764253ea50SConrad Meyer static int ioat_setup_intr(struct ioat_softc *ioat);
774253ea50SConrad Meyer static int ioat_teardown_intr(struct ioat_softc *ioat);
78e974f91cSConrad Meyer static int ioat3_attach(device_t device);
79cea5b880SConrad Meyer static int ioat_start_channel(struct ioat_softc *ioat);
80e974f91cSConrad Meyer static int ioat_map_pci_bar(struct ioat_softc *ioat);
81e974f91cSConrad Meyer static void ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg,
82e974f91cSConrad Meyer     int error);
83e974f91cSConrad Meyer static void ioat_interrupt_handler(void *arg);
840d1a05d9SConrad Meyer static boolean_t ioat_model_resets_msix(struct ioat_softc *ioat);
85faefad9cSConrad Meyer static int chanerr_to_errno(uint32_t);
862f03a95fSAlexander Motin static void ioat_process_events(struct ioat_softc *ioat, boolean_t intr);
87e974f91cSConrad Meyer static inline uint32_t ioat_get_active(struct ioat_softc *ioat);
88e974f91cSConrad Meyer static inline uint32_t ioat_get_ring_space(struct ioat_softc *ioat);
89bf8553eaSConrad Meyer static void ioat_free_ring(struct ioat_softc *, uint32_t size,
908e269d99SConrad Meyer     struct ioat_descriptor *);
91bf8553eaSConrad Meyer static int ioat_reserve_space(struct ioat_softc *, uint32_t, int mflags);
928e269d99SConrad Meyer static union ioat_hw_descriptor *ioat_get_descriptor(struct ioat_softc *,
938e269d99SConrad Meyer     uint32_t index);
948e269d99SConrad Meyer static struct ioat_descriptor *ioat_get_ring_entry(struct ioat_softc *,
95e974f91cSConrad Meyer     uint32_t index);
96faefad9cSConrad Meyer static void ioat_halted_debug(struct ioat_softc *, uint32_t);
975ac77963SConrad Meyer static void ioat_poll_timer_callback(void *arg);
98e974f91cSConrad Meyer static void dump_descriptor(void *hw_desc);
99e974f91cSConrad Meyer static void ioat_submit_single(struct ioat_softc *ioat);
100e974f91cSConrad Meyer static void ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg,
101e974f91cSConrad Meyer     int error);
102e974f91cSConrad Meyer static int ioat_reset_hw(struct ioat_softc *ioat);
103374b05e1SConrad Meyer static void ioat_reset_hw_task(void *, int);
104e974f91cSConrad Meyer static void ioat_setup_sysctl(device_t device);
105f7157235SConrad Meyer static int sysctl_handle_reset(SYSCTL_HANDLER_ARGS);
1062f03a95fSAlexander Motin static void ioat_get(struct ioat_softc *);
1072f03a95fSAlexander Motin static void ioat_put(struct ioat_softc *);
1085f77bd3eSConrad Meyer static void ioat_drain_locked(struct ioat_softc *);
109e974f91cSConrad Meyer 
1101c25420eSConrad Meyer #define	ioat_log_message(v, ...) do {					\
1111c25420eSConrad Meyer 	if ((v) <= g_ioat_debug_level) {				\
1121c25420eSConrad Meyer 		device_printf(ioat->device, __VA_ARGS__);		\
1131c25420eSConrad Meyer 	}								\
1141c25420eSConrad Meyer } while (0)
1151c25420eSConrad Meyer 
116e974f91cSConrad Meyer MALLOC_DEFINE(M_IOAT, "ioat", "ioat driver memory allocations");
117*7029da5cSPawel Biernacki SYSCTL_NODE(_hw, OID_AUTO, ioat, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
118*7029da5cSPawel Biernacki     "ioat node");
119e974f91cSConrad Meyer 
120e974f91cSConrad Meyer static int g_force_legacy_interrupts;
121e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, force_legacy_interrupts, CTLFLAG_RDTUN,
122e974f91cSConrad Meyer     &g_force_legacy_interrupts, 0, "Set to non-zero to force MSI-X disabled");
123e974f91cSConrad Meyer 
1241c25420eSConrad Meyer int g_ioat_debug_level = 0;
125e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, debug_level, CTLFLAG_RWTUN, &g_ioat_debug_level,
126e974f91cSConrad Meyer     0, "Set log level (0-3) for ioat(4). Higher is more verbose.");
127e974f91cSConrad Meyer 
128a0992979SConrad Meyer unsigned g_ioat_ring_order = 13;
129a0992979SConrad Meyer SYSCTL_UINT(_hw_ioat, OID_AUTO, ring_order, CTLFLAG_RDTUN, &g_ioat_ring_order,
130a0992979SConrad Meyer     0, "Set IOAT ring order.  (1 << this) == ring size.");
131a0992979SConrad Meyer 
132e974f91cSConrad Meyer /*
133e974f91cSConrad Meyer  * OS <-> Driver interface structures
134e974f91cSConrad Meyer  */
135e974f91cSConrad Meyer static device_method_t ioat_pci_methods[] = {
136e974f91cSConrad Meyer 	/* Device interface */
137e974f91cSConrad Meyer 	DEVMETHOD(device_probe,     ioat_probe),
138e974f91cSConrad Meyer 	DEVMETHOD(device_attach,    ioat_attach),
139e974f91cSConrad Meyer 	DEVMETHOD(device_detach,    ioat_detach),
140aa853cb4SEnji Cooper 	DEVMETHOD_END
141e974f91cSConrad Meyer };
142e974f91cSConrad Meyer 
143e974f91cSConrad Meyer static driver_t ioat_pci_driver = {
144e974f91cSConrad Meyer 	"ioat",
145e974f91cSConrad Meyer 	ioat_pci_methods,
146e974f91cSConrad Meyer 	sizeof(struct ioat_softc),
147e974f91cSConrad Meyer };
148e974f91cSConrad Meyer 
149e974f91cSConrad Meyer static devclass_t ioat_devclass;
150e974f91cSConrad Meyer DRIVER_MODULE(ioat, pci, ioat_pci_driver, ioat_devclass, 0, 0);
151c2b69205SConrad Meyer MODULE_VERSION(ioat, 1);
152e974f91cSConrad Meyer 
153e974f91cSConrad Meyer /*
154e974f91cSConrad Meyer  * Private data structures
155e974f91cSConrad Meyer  */
156e974f91cSConrad Meyer static struct ioat_softc *ioat_channel[IOAT_MAX_CHANNELS];
157df1928aaSConrad Meyer static unsigned ioat_channel_index = 0;
158df1928aaSConrad Meyer SYSCTL_UINT(_hw_ioat, OID_AUTO, channels, CTLFLAG_RD, &ioat_channel_index, 0,
159e974f91cSConrad Meyer     "Number of IOAT channels attached");
1602f03a95fSAlexander Motin static struct mtx ioat_list_mtx;
1612f03a95fSAlexander Motin MTX_SYSINIT(ioat_list_mtx, &ioat_list_mtx, "ioat list mtx", MTX_DEF);
162e974f91cSConrad Meyer 
163e974f91cSConrad Meyer static struct _pcsid
164e974f91cSConrad Meyer {
165e974f91cSConrad Meyer 	u_int32_t   type;
166e974f91cSConrad Meyer 	const char  *desc;
167e974f91cSConrad Meyer } pci_ids[] = {
168e974f91cSConrad Meyer 	{ 0x34308086, "TBG IOAT Ch0" },
169e974f91cSConrad Meyer 	{ 0x34318086, "TBG IOAT Ch1" },
170e974f91cSConrad Meyer 	{ 0x34328086, "TBG IOAT Ch2" },
171e974f91cSConrad Meyer 	{ 0x34338086, "TBG IOAT Ch3" },
172e974f91cSConrad Meyer 	{ 0x34298086, "TBG IOAT Ch4" },
173e974f91cSConrad Meyer 	{ 0x342a8086, "TBG IOAT Ch5" },
174e974f91cSConrad Meyer 	{ 0x342b8086, "TBG IOAT Ch6" },
175e974f91cSConrad Meyer 	{ 0x342c8086, "TBG IOAT Ch7" },
176e974f91cSConrad Meyer 
177e974f91cSConrad Meyer 	{ 0x37108086, "JSF IOAT Ch0" },
178e974f91cSConrad Meyer 	{ 0x37118086, "JSF IOAT Ch1" },
179e974f91cSConrad Meyer 	{ 0x37128086, "JSF IOAT Ch2" },
180e974f91cSConrad Meyer 	{ 0x37138086, "JSF IOAT Ch3" },
181e974f91cSConrad Meyer 	{ 0x37148086, "JSF IOAT Ch4" },
182e974f91cSConrad Meyer 	{ 0x37158086, "JSF IOAT Ch5" },
183e974f91cSConrad Meyer 	{ 0x37168086, "JSF IOAT Ch6" },
184e974f91cSConrad Meyer 	{ 0x37178086, "JSF IOAT Ch7" },
185e974f91cSConrad Meyer 	{ 0x37188086, "JSF IOAT Ch0 (RAID)" },
186e974f91cSConrad Meyer 	{ 0x37198086, "JSF IOAT Ch1 (RAID)" },
187e974f91cSConrad Meyer 
188e974f91cSConrad Meyer 	{ 0x3c208086, "SNB IOAT Ch0" },
189e974f91cSConrad Meyer 	{ 0x3c218086, "SNB IOAT Ch1" },
190e974f91cSConrad Meyer 	{ 0x3c228086, "SNB IOAT Ch2" },
191e974f91cSConrad Meyer 	{ 0x3c238086, "SNB IOAT Ch3" },
192e974f91cSConrad Meyer 	{ 0x3c248086, "SNB IOAT Ch4" },
193e974f91cSConrad Meyer 	{ 0x3c258086, "SNB IOAT Ch5" },
194e974f91cSConrad Meyer 	{ 0x3c268086, "SNB IOAT Ch6" },
195e974f91cSConrad Meyer 	{ 0x3c278086, "SNB IOAT Ch7" },
196e974f91cSConrad Meyer 	{ 0x3c2e8086, "SNB IOAT Ch0 (RAID)" },
197e974f91cSConrad Meyer 	{ 0x3c2f8086, "SNB IOAT Ch1 (RAID)" },
198e974f91cSConrad Meyer 
199e974f91cSConrad Meyer 	{ 0x0e208086, "IVB IOAT Ch0" },
200e974f91cSConrad Meyer 	{ 0x0e218086, "IVB IOAT Ch1" },
201e974f91cSConrad Meyer 	{ 0x0e228086, "IVB IOAT Ch2" },
202e974f91cSConrad Meyer 	{ 0x0e238086, "IVB IOAT Ch3" },
203e974f91cSConrad Meyer 	{ 0x0e248086, "IVB IOAT Ch4" },
204e974f91cSConrad Meyer 	{ 0x0e258086, "IVB IOAT Ch5" },
205e974f91cSConrad Meyer 	{ 0x0e268086, "IVB IOAT Ch6" },
206e974f91cSConrad Meyer 	{ 0x0e278086, "IVB IOAT Ch7" },
207e974f91cSConrad Meyer 	{ 0x0e2e8086, "IVB IOAT Ch0 (RAID)" },
208e974f91cSConrad Meyer 	{ 0x0e2f8086, "IVB IOAT Ch1 (RAID)" },
209e974f91cSConrad Meyer 
210e974f91cSConrad Meyer 	{ 0x2f208086, "HSW IOAT Ch0" },
211e974f91cSConrad Meyer 	{ 0x2f218086, "HSW IOAT Ch1" },
212e974f91cSConrad Meyer 	{ 0x2f228086, "HSW IOAT Ch2" },
213e974f91cSConrad Meyer 	{ 0x2f238086, "HSW IOAT Ch3" },
214e974f91cSConrad Meyer 	{ 0x2f248086, "HSW IOAT Ch4" },
215e974f91cSConrad Meyer 	{ 0x2f258086, "HSW IOAT Ch5" },
216e974f91cSConrad Meyer 	{ 0x2f268086, "HSW IOAT Ch6" },
217e974f91cSConrad Meyer 	{ 0x2f278086, "HSW IOAT Ch7" },
218e974f91cSConrad Meyer 	{ 0x2f2e8086, "HSW IOAT Ch0 (RAID)" },
219e974f91cSConrad Meyer 	{ 0x2f2f8086, "HSW IOAT Ch1 (RAID)" },
220e974f91cSConrad Meyer 
221e974f91cSConrad Meyer 	{ 0x0c508086, "BWD IOAT Ch0" },
222e974f91cSConrad Meyer 	{ 0x0c518086, "BWD IOAT Ch1" },
223e974f91cSConrad Meyer 	{ 0x0c528086, "BWD IOAT Ch2" },
224e974f91cSConrad Meyer 	{ 0x0c538086, "BWD IOAT Ch3" },
225e974f91cSConrad Meyer 
226e974f91cSConrad Meyer 	{ 0x6f508086, "BDXDE IOAT Ch0" },
227e974f91cSConrad Meyer 	{ 0x6f518086, "BDXDE IOAT Ch1" },
228e974f91cSConrad Meyer 	{ 0x6f528086, "BDXDE IOAT Ch2" },
229e974f91cSConrad Meyer 	{ 0x6f538086, "BDXDE IOAT Ch3" },
230e974f91cSConrad Meyer 
2315afc2508SConrad Meyer 	{ 0x6f208086, "BDX IOAT Ch0" },
2325afc2508SConrad Meyer 	{ 0x6f218086, "BDX IOAT Ch1" },
2335afc2508SConrad Meyer 	{ 0x6f228086, "BDX IOAT Ch2" },
2345afc2508SConrad Meyer 	{ 0x6f238086, "BDX IOAT Ch3" },
2355afc2508SConrad Meyer 	{ 0x6f248086, "BDX IOAT Ch4" },
2365afc2508SConrad Meyer 	{ 0x6f258086, "BDX IOAT Ch5" },
2375afc2508SConrad Meyer 	{ 0x6f268086, "BDX IOAT Ch6" },
2385afc2508SConrad Meyer 	{ 0x6f278086, "BDX IOAT Ch7" },
2395afc2508SConrad Meyer 	{ 0x6f2e8086, "BDX IOAT Ch0 (RAID)" },
2405afc2508SConrad Meyer 	{ 0x6f2f8086, "BDX IOAT Ch1 (RAID)" },
24108f16d0cSConrad Meyer 
24208f16d0cSConrad Meyer 	{ 0x20218086, "SKX IOAT" },
243e974f91cSConrad Meyer };
244e974f91cSConrad Meyer 
245d2064cf0SWarner Losh MODULE_PNP_INFO("W32:vendor/device;D:#", pci, ioat, pci_ids,
246329e817fSWarner Losh     nitems(pci_ids));
247a64bf59cSConrad Meyer 
248e974f91cSConrad Meyer /*
249e974f91cSConrad Meyer  * OS <-> Driver linkage functions
250e974f91cSConrad Meyer  */
251e974f91cSConrad Meyer static int
252e974f91cSConrad Meyer ioat_probe(device_t device)
253e974f91cSConrad Meyer {
254e974f91cSConrad Meyer 	struct _pcsid *ep;
255e974f91cSConrad Meyer 	u_int32_t type;
256e974f91cSConrad Meyer 
257e974f91cSConrad Meyer 	type = pci_get_devid(device);
258a64bf59cSConrad Meyer 	for (ep = pci_ids; ep < &pci_ids[nitems(pci_ids)]; ep++) {
259e974f91cSConrad Meyer 		if (ep->type == type) {
260e974f91cSConrad Meyer 			device_set_desc(device, ep->desc);
261e974f91cSConrad Meyer 			return (0);
262e974f91cSConrad Meyer 		}
263e974f91cSConrad Meyer 	}
264e974f91cSConrad Meyer 	return (ENXIO);
265e974f91cSConrad Meyer }
266e974f91cSConrad Meyer 
267e974f91cSConrad Meyer static int
268e974f91cSConrad Meyer ioat_attach(device_t device)
269e974f91cSConrad Meyer {
270e974f91cSConrad Meyer 	struct ioat_softc *ioat;
2712f03a95fSAlexander Motin 	int error, i;
272e974f91cSConrad Meyer 
273e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
274e974f91cSConrad Meyer 	ioat->device = device;
275657dc81dSAlexander Motin 	if (bus_get_domain(device, &ioat->domain) != 0)
276657dc81dSAlexander Motin 		ioat->domain = 0;
277657dc81dSAlexander Motin 	ioat->cpu = CPU_FFS(&cpuset_domain[ioat->domain]) - 1;
278657dc81dSAlexander Motin 	if (ioat->cpu < 0)
279657dc81dSAlexander Motin 		ioat->cpu = CPU_FIRST();
280e974f91cSConrad Meyer 
281e974f91cSConrad Meyer 	error = ioat_map_pci_bar(ioat);
282e974f91cSConrad Meyer 	if (error != 0)
283e974f91cSConrad Meyer 		goto err;
284e974f91cSConrad Meyer 
285e974f91cSConrad Meyer 	ioat->version = ioat_read_cbver(ioat);
286e974f91cSConrad Meyer 	if (ioat->version < IOAT_VER_3_0) {
287e974f91cSConrad Meyer 		error = ENODEV;
288e974f91cSConrad Meyer 		goto err;
289e974f91cSConrad Meyer 	}
290e974f91cSConrad Meyer 
291e974f91cSConrad Meyer 	error = ioat3_attach(device);
292e974f91cSConrad Meyer 	if (error != 0)
293e974f91cSConrad Meyer 		goto err;
294e974f91cSConrad Meyer 
295e974f91cSConrad Meyer 	error = pci_enable_busmaster(device);
296e974f91cSConrad Meyer 	if (error != 0)
297e974f91cSConrad Meyer 		goto err;
298e974f91cSConrad Meyer 
299466b3540SConrad Meyer 	error = ioat_setup_intr(ioat);
300466b3540SConrad Meyer 	if (error != 0)
301466b3540SConrad Meyer 		goto err;
302466b3540SConrad Meyer 
303cea5b880SConrad Meyer 	error = ioat_reset_hw(ioat);
3047afbb263SConrad Meyer 	if (error != 0)
305466b3540SConrad Meyer 		goto err;
3067afbb263SConrad Meyer 
3072f03a95fSAlexander Motin 	ioat_process_events(ioat, FALSE);
3087afbb263SConrad Meyer 	ioat_setup_sysctl(device);
3097afbb263SConrad Meyer 
3102f03a95fSAlexander Motin 	mtx_lock(&ioat_list_mtx);
3112f03a95fSAlexander Motin 	for (i = 0; i < IOAT_MAX_CHANNELS; i++) {
3122f03a95fSAlexander Motin 		if (ioat_channel[i] == NULL)
3132f03a95fSAlexander Motin 			break;
3142f03a95fSAlexander Motin 	}
3152f03a95fSAlexander Motin 	if (i >= IOAT_MAX_CHANNELS) {
3162f03a95fSAlexander Motin 		mtx_unlock(&ioat_list_mtx);
3172f03a95fSAlexander Motin 		device_printf(device, "Too many I/OAT devices in system\n");
3182f03a95fSAlexander Motin 		error = ENXIO;
3192f03a95fSAlexander Motin 		goto err;
3202f03a95fSAlexander Motin 	}
3212f03a95fSAlexander Motin 	ioat->chan_idx = i;
3222f03a95fSAlexander Motin 	ioat_channel[i] = ioat;
3232f03a95fSAlexander Motin 	if (i >= ioat_channel_index)
3242f03a95fSAlexander Motin 		ioat_channel_index = i + 1;
3252f03a95fSAlexander Motin 	mtx_unlock(&ioat_list_mtx);
3262f03a95fSAlexander Motin 
3277afbb263SConrad Meyer 	ioat_test_attach();
328e974f91cSConrad Meyer 
329e974f91cSConrad Meyer err:
330e974f91cSConrad Meyer 	if (error != 0)
331e974f91cSConrad Meyer 		ioat_detach(device);
332e974f91cSConrad Meyer 	return (error);
333e974f91cSConrad Meyer }
334e974f91cSConrad Meyer 
335b80b32a2STycho Nightingale static inline int
336b80b32a2STycho Nightingale ioat_bus_dmamap_destroy(struct ioat_softc *ioat, const char *func,
337b80b32a2STycho Nightingale     bus_dma_tag_t dmat, bus_dmamap_t map)
338b80b32a2STycho Nightingale {
339b80b32a2STycho Nightingale 	int error;
340b80b32a2STycho Nightingale 
341b80b32a2STycho Nightingale 	error = bus_dmamap_destroy(dmat, map);
342b80b32a2STycho Nightingale 	if (error != 0) {
343b80b32a2STycho Nightingale 		ioat_log_message(0,
344b80b32a2STycho Nightingale 		    "%s: bus_dmamap_destroy failed %d\n", func, error);
345b80b32a2STycho Nightingale 	}
346b80b32a2STycho Nightingale 
347b80b32a2STycho Nightingale 	return (error);
348b80b32a2STycho Nightingale }
349b80b32a2STycho Nightingale 
350e974f91cSConrad Meyer static int
351e974f91cSConrad Meyer ioat_detach(device_t device)
352e974f91cSConrad Meyer {
353e974f91cSConrad Meyer 	struct ioat_softc *ioat;
354b80b32a2STycho Nightingale 	int i, error;
355e974f91cSConrad Meyer 
356e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
3577afbb263SConrad Meyer 
3582f03a95fSAlexander Motin 	mtx_lock(&ioat_list_mtx);
3592f03a95fSAlexander Motin 	ioat_channel[ioat->chan_idx] = NULL;
3602f03a95fSAlexander Motin 	while (ioat_channel_index > 0 &&
3612f03a95fSAlexander Motin 	    ioat_channel[ioat_channel_index - 1] == NULL)
3622f03a95fSAlexander Motin 		ioat_channel_index--;
3632f03a95fSAlexander Motin 	mtx_unlock(&ioat_list_mtx);
3642f03a95fSAlexander Motin 
3657afbb263SConrad Meyer 	ioat_test_detach();
366374b05e1SConrad Meyer 	taskqueue_drain(taskqueue_thread, &ioat->reset_task);
3675f77bd3eSConrad Meyer 
3682f03a95fSAlexander Motin 	mtx_lock(&ioat->submit_lock);
3695f77bd3eSConrad Meyer 	ioat->quiescing = TRUE;
3700ff814e8SConrad Meyer 	ioat->destroying = TRUE;
3710ff814e8SConrad Meyer 	wakeup(&ioat->quiescing);
37293f7f84aSConrad Meyer 	wakeup(&ioat->resetting);
3730ff814e8SConrad Meyer 
3745f77bd3eSConrad Meyer 	ioat_drain_locked(ioat);
3752f03a95fSAlexander Motin 	mtx_unlock(&ioat->submit_lock);
3762f03a95fSAlexander Motin 	mtx_lock(&ioat->cleanup_lock);
3772f03a95fSAlexander Motin 	while (ioat_get_active(ioat) > 0)
3782f03a95fSAlexander Motin 		msleep(&ioat->tail, &ioat->cleanup_lock, 0, "ioat_drain", 1);
3792f03a95fSAlexander Motin 	mtx_unlock(&ioat->cleanup_lock);
380fe720f5aSConrad Meyer 
381fe720f5aSConrad Meyer 	ioat_teardown_intr(ioat);
3825ac77963SConrad Meyer 	callout_drain(&ioat->poll_timer);
383e974f91cSConrad Meyer 
384e974f91cSConrad Meyer 	pci_disable_busmaster(device);
385e974f91cSConrad Meyer 
386e974f91cSConrad Meyer 	if (ioat->pci_resource != NULL)
387e974f91cSConrad Meyer 		bus_release_resource(device, SYS_RES_MEMORY,
388e974f91cSConrad Meyer 		    ioat->pci_resource_id, ioat->pci_resource);
389e974f91cSConrad Meyer 
390b80b32a2STycho Nightingale 	if (ioat->data_tag != NULL) {
391b80b32a2STycho Nightingale 		for (i = 0; i < 1 << ioat->ring_size_order; i++) {
392b80b32a2STycho Nightingale 			error = ioat_bus_dmamap_destroy(ioat, __func__,
393b80b32a2STycho Nightingale 			    ioat->data_tag, ioat->ring[i].src_dmamap);
394b80b32a2STycho Nightingale 			if (error != 0)
395b80b32a2STycho Nightingale 				return (error);
396b80b32a2STycho Nightingale 		}
397b80b32a2STycho Nightingale 		for (i = 0; i < 1 << ioat->ring_size_order; i++) {
398b80b32a2STycho Nightingale 			error = ioat_bus_dmamap_destroy(ioat, __func__,
399b80b32a2STycho Nightingale 			    ioat->data_tag, ioat->ring[i].dst_dmamap);
400b80b32a2STycho Nightingale 			if (error != 0)
401b80b32a2STycho Nightingale 				return (error);
402b80b32a2STycho Nightingale 		}
403b80b32a2STycho Nightingale 
404b80b32a2STycho Nightingale 		for (i = 0; i < 1 << ioat->ring_size_order; i++) {
405b80b32a2STycho Nightingale 			error = ioat_bus_dmamap_destroy(ioat, __func__,
406b80b32a2STycho Nightingale 			    ioat->data_tag, ioat->ring[i].src2_dmamap);
407b80b32a2STycho Nightingale 			if (error != 0)
408b80b32a2STycho Nightingale 				return (error);
409b80b32a2STycho Nightingale 		}
410b80b32a2STycho Nightingale 		for (i = 0; i < 1 << ioat->ring_size_order; i++) {
411b80b32a2STycho Nightingale 			error = ioat_bus_dmamap_destroy(ioat, __func__,
412b80b32a2STycho Nightingale 			    ioat->data_tag, ioat->ring[i].dst2_dmamap);
413b80b32a2STycho Nightingale 			if (error != 0)
414b80b32a2STycho Nightingale 				return (error);
415b80b32a2STycho Nightingale 		}
416b80b32a2STycho Nightingale 
417b80b32a2STycho Nightingale 		bus_dma_tag_destroy(ioat->data_tag);
418b80b32a2STycho Nightingale 	}
419b80b32a2STycho Nightingale 
420bf8553eaSConrad Meyer 	if (ioat->ring != NULL)
421bf8553eaSConrad Meyer 		ioat_free_ring(ioat, 1 << ioat->ring_size_order, ioat->ring);
422e974f91cSConrad Meyer 
423e974f91cSConrad Meyer 	if (ioat->comp_update != NULL) {
424e974f91cSConrad Meyer 		bus_dmamap_unload(ioat->comp_update_tag, ioat->comp_update_map);
425e974f91cSConrad Meyer 		bus_dmamem_free(ioat->comp_update_tag, ioat->comp_update,
426e974f91cSConrad Meyer 		    ioat->comp_update_map);
427e974f91cSConrad Meyer 		bus_dma_tag_destroy(ioat->comp_update_tag);
428e974f91cSConrad Meyer 	}
429e974f91cSConrad Meyer 
4308e269d99SConrad Meyer 	if (ioat->hw_desc_ring != NULL) {
4318e269d99SConrad Meyer 		bus_dmamap_unload(ioat->hw_desc_tag, ioat->hw_desc_map);
4328e269d99SConrad Meyer 		bus_dmamem_free(ioat->hw_desc_tag, ioat->hw_desc_ring,
4338e269d99SConrad Meyer 		    ioat->hw_desc_map);
434e974f91cSConrad Meyer 		bus_dma_tag_destroy(ioat->hw_desc_tag);
4358e269d99SConrad Meyer 	}
436e974f91cSConrad Meyer 
4374253ea50SConrad Meyer 	return (0);
4384253ea50SConrad Meyer }
4394253ea50SConrad Meyer 
4404253ea50SConrad Meyer static int
4414253ea50SConrad Meyer ioat_teardown_intr(struct ioat_softc *ioat)
4424253ea50SConrad Meyer {
4434253ea50SConrad Meyer 
444e974f91cSConrad Meyer 	if (ioat->tag != NULL)
4454253ea50SConrad Meyer 		bus_teardown_intr(ioat->device, ioat->res, ioat->tag);
446e974f91cSConrad Meyer 
447e974f91cSConrad Meyer 	if (ioat->res != NULL)
4484253ea50SConrad Meyer 		bus_release_resource(ioat->device, SYS_RES_IRQ,
449e974f91cSConrad Meyer 		    rman_get_rid(ioat->res), ioat->res);
450e974f91cSConrad Meyer 
4514253ea50SConrad Meyer 	pci_release_msi(ioat->device);
452e974f91cSConrad Meyer 	return (0);
453e974f91cSConrad Meyer }
454e974f91cSConrad Meyer 
455e974f91cSConrad Meyer static int
456cea5b880SConrad Meyer ioat_start_channel(struct ioat_softc *ioat)
457e974f91cSConrad Meyer {
458fe8712f8SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
459fe8712f8SConrad Meyer 	struct ioat_descriptor *desc;
460fe8712f8SConrad Meyer 	struct bus_dmadesc *dmadesc;
461e974f91cSConrad Meyer 	uint64_t status;
462e974f91cSConrad Meyer 	uint32_t chanerr;
463e974f91cSConrad Meyer 	int i;
464e974f91cSConrad Meyer 
465e974f91cSConrad Meyer 	ioat_acquire(&ioat->dmaengine);
466fe8712f8SConrad Meyer 
467fe8712f8SConrad Meyer 	/* Submit 'NULL' operation manually to avoid quiescing flag */
468fe8712f8SConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->head);
4698e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, ioat->head)->dma;
470fe8712f8SConrad Meyer 	dmadesc = &desc->bus_dmadesc;
471fe8712f8SConrad Meyer 
472fe8712f8SConrad Meyer 	dmadesc->callback_fn = NULL;
473fe8712f8SConrad Meyer 	dmadesc->callback_arg = NULL;
474fe8712f8SConrad Meyer 
475fe8712f8SConrad Meyer 	hw_desc->u.control_raw = 0;
476fe8712f8SConrad Meyer 	hw_desc->u.control_generic.op = IOAT_OP_COPY;
477fe8712f8SConrad Meyer 	hw_desc->u.control_generic.completion_update = 1;
478fe8712f8SConrad Meyer 	hw_desc->size = 8;
479fe8712f8SConrad Meyer 	hw_desc->src_addr = 0;
480fe8712f8SConrad Meyer 	hw_desc->dest_addr = 0;
481fe8712f8SConrad Meyer 	hw_desc->u.control.null = 1;
482fe8712f8SConrad Meyer 
483fe8712f8SConrad Meyer 	ioat_submit_single(ioat);
484e974f91cSConrad Meyer 	ioat_release(&ioat->dmaengine);
485e974f91cSConrad Meyer 
486e974f91cSConrad Meyer 	for (i = 0; i < 100; i++) {
487e974f91cSConrad Meyer 		DELAY(1);
488e974f91cSConrad Meyer 		status = ioat_get_chansts(ioat);
489e974f91cSConrad Meyer 		if (is_ioat_idle(status))
490e974f91cSConrad Meyer 			return (0);
491e974f91cSConrad Meyer 	}
492e974f91cSConrad Meyer 
493e974f91cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
494e974f91cSConrad Meyer 	ioat_log_message(0, "could not start channel: "
49559acd4baSConrad Meyer 	    "status = %#jx error = %b\n", (uintmax_t)status, (int)chanerr,
49659acd4baSConrad Meyer 	    IOAT_CHANERR_STR);
497e974f91cSConrad Meyer 	return (ENXIO);
498e974f91cSConrad Meyer }
499e974f91cSConrad Meyer 
500e974f91cSConrad Meyer /*
501e974f91cSConrad Meyer  * Initialize Hardware
502e974f91cSConrad Meyer  */
503e974f91cSConrad Meyer static int
504e974f91cSConrad Meyer ioat3_attach(device_t device)
505e974f91cSConrad Meyer {
506e974f91cSConrad Meyer 	struct ioat_softc *ioat;
5078e269d99SConrad Meyer 	struct ioat_descriptor *ring;
508e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *dma_hw_desc;
5098e269d99SConrad Meyer 	void *hw_desc;
5101f4a469dSAlexander Motin 	bus_addr_t lowaddr;
5118e269d99SConrad Meyer 	size_t ringsz;
512e974f91cSConrad Meyer 	int i, num_descriptors;
513e974f91cSConrad Meyer 	int error;
514e974f91cSConrad Meyer 	uint8_t xfercap;
515e974f91cSConrad Meyer 
516e974f91cSConrad Meyer 	error = 0;
517e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
5181693d27bSConrad Meyer 	ioat->capabilities = ioat_read_dmacapability(ioat);
5191693d27bSConrad Meyer 
52048fed014SConrad Meyer 	ioat_log_message(0, "Capabilities: %b\n", (int)ioat->capabilities,
5211693d27bSConrad Meyer 	    IOAT_DMACAP_STR);
522e974f91cSConrad Meyer 
523e974f91cSConrad Meyer 	xfercap = ioat_read_xfercap(ioat);
524e974f91cSConrad Meyer 	ioat->max_xfer_size = 1 << xfercap;
525e974f91cSConrad Meyer 
5265ca9fc2aSConrad Meyer 	ioat->intrdelay_supported = (ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) &
5275ca9fc2aSConrad Meyer 	    IOAT_INTRDELAY_SUPPORTED) != 0;
5285ca9fc2aSConrad Meyer 	if (ioat->intrdelay_supported)
5295ca9fc2aSConrad Meyer 		ioat->intrdelay_max = IOAT_INTRDELAY_US_MASK;
5305ca9fc2aSConrad Meyer 
531e974f91cSConrad Meyer 	/* TODO: need to check DCA here if we ever do XOR/PQ */
532e974f91cSConrad Meyer 
533e974f91cSConrad Meyer 	mtx_init(&ioat->submit_lock, "ioat_submit", NULL, MTX_DEF);
534faefad9cSConrad Meyer 	mtx_init(&ioat->cleanup_lock, "ioat_cleanup", NULL, MTX_DEF);
5355ac77963SConrad Meyer 	callout_init(&ioat->poll_timer, 1);
536374b05e1SConrad Meyer 	TASK_INIT(&ioat->reset_task, 0, ioat_reset_hw_task, ioat);
537e974f91cSConrad Meyer 
538faefad9cSConrad Meyer 	/* Establish lock order for Witness */
539faefad9cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
5402f03a95fSAlexander Motin 	mtx_lock(&ioat->submit_lock);
541faefad9cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
5422f03a95fSAlexander Motin 	mtx_unlock(&ioat->cleanup_lock);
543faefad9cSConrad Meyer 
54425ad9585SConrad Meyer 	ioat->is_submitter_processing = FALSE;
545e974f91cSConrad Meyer 
5461f4a469dSAlexander Motin 	if (ioat->version >= IOAT_VER_3_3)
5471f4a469dSAlexander Motin 		lowaddr = BUS_SPACE_MAXADDR_48BIT;
5481f4a469dSAlexander Motin 	else if (ioat->version >= IOAT_VER_3_2)
5491f4a469dSAlexander Motin 		lowaddr = BUS_SPACE_MAXADDR_46BIT;
5501f4a469dSAlexander Motin 	else
5511f4a469dSAlexander Motin 		lowaddr = BUS_SPACE_MAXADDR_40BIT;
5521f4a469dSAlexander Motin 
5531f4a469dSAlexander Motin 	error = bus_dma_tag_create(bus_get_dma_tag(ioat->device),
5541f4a469dSAlexander Motin 	    sizeof(uint64_t), 0x0, lowaddr, BUS_SPACE_MAXADDR, NULL, NULL,
555e974f91cSConrad Meyer 	    sizeof(uint64_t), 1, sizeof(uint64_t), 0, NULL, NULL,
556e974f91cSConrad Meyer 	    &ioat->comp_update_tag);
5571f4a469dSAlexander Motin 	if (error != 0)
5581f4a469dSAlexander Motin 		return (error);
559e974f91cSConrad Meyer 
560e974f91cSConrad Meyer 	error = bus_dmamem_alloc(ioat->comp_update_tag,
5613eb70a09SAlexander Motin 	    (void **)&ioat->comp_update, BUS_DMA_ZERO | BUS_DMA_WAITOK,
5623eb70a09SAlexander Motin 	    &ioat->comp_update_map);
5631f4a469dSAlexander Motin 	if (error != 0)
5641f4a469dSAlexander Motin 		return (error);
565e974f91cSConrad Meyer 
566e974f91cSConrad Meyer 	error = bus_dmamap_load(ioat->comp_update_tag, ioat->comp_update_map,
567e974f91cSConrad Meyer 	    ioat->comp_update, sizeof(uint64_t), ioat_comp_update_map, ioat,
5683eb70a09SAlexander Motin 	    BUS_DMA_NOWAIT);
569e974f91cSConrad Meyer 	if (error != 0)
570e974f91cSConrad Meyer 		return (error);
571e974f91cSConrad Meyer 
572a0992979SConrad Meyer 	ioat->ring_size_order = g_ioat_ring_order;
573e974f91cSConrad Meyer 	num_descriptors = 1 << ioat->ring_size_order;
5748e269d99SConrad Meyer 	ringsz = sizeof(struct ioat_dma_hw_descriptor) * num_descriptors;
575e974f91cSConrad Meyer 
5768e269d99SConrad Meyer 	error = bus_dma_tag_create(bus_get_dma_tag(ioat->device),
5771f4a469dSAlexander Motin 	    2 * 1024 * 1024, 0x0, lowaddr, BUS_SPACE_MAXADDR, NULL, NULL,
5781f4a469dSAlexander Motin 	    ringsz, 1, ringsz, 0, NULL, NULL, &ioat->hw_desc_tag);
5798e269d99SConrad Meyer 	if (error != 0)
5808e269d99SConrad Meyer 		return (error);
5818e269d99SConrad Meyer 
5828e269d99SConrad Meyer 	error = bus_dmamem_alloc(ioat->hw_desc_tag, &hw_desc,
5838e269d99SConrad Meyer 	    BUS_DMA_ZERO | BUS_DMA_WAITOK, &ioat->hw_desc_map);
5848e269d99SConrad Meyer 	if (error != 0)
5858e269d99SConrad Meyer 		return (error);
5868e269d99SConrad Meyer 
5878e269d99SConrad Meyer 	error = bus_dmamap_load(ioat->hw_desc_tag, ioat->hw_desc_map, hw_desc,
5883eb70a09SAlexander Motin 	    ringsz, ioat_dmamap_cb, &ioat->hw_desc_bus_addr, BUS_DMA_NOWAIT);
5898e269d99SConrad Meyer 	if (error)
5908e269d99SConrad Meyer 		return (error);
5918e269d99SConrad Meyer 
5928e269d99SConrad Meyer 	ioat->hw_desc_ring = hw_desc;
593e974f91cSConrad Meyer 
594b80b32a2STycho Nightingale 	error = bus_dma_tag_create(bus_get_dma_tag(ioat->device),
5951f4a469dSAlexander Motin 	    1, 0, lowaddr, BUS_SPACE_MAXADDR, NULL, NULL,
596b80b32a2STycho Nightingale 	    ioat->max_xfer_size, 1, ioat->max_xfer_size, 0, NULL, NULL,
597b80b32a2STycho Nightingale 	    &ioat->data_tag);
5981f4a469dSAlexander Motin 	if (error != 0)
599b80b32a2STycho Nightingale 		return (error);
600657dc81dSAlexander Motin 	ioat->ring = malloc_domainset(num_descriptors * sizeof(*ring), M_IOAT,
601657dc81dSAlexander Motin 	    DOMAINSET_PREF(ioat->domain), M_ZERO | M_WAITOK);
602e974f91cSConrad Meyer 
603e974f91cSConrad Meyer 	ring = ioat->ring;
604e974f91cSConrad Meyer 	for (i = 0; i < num_descriptors; i++) {
6058e269d99SConrad Meyer 		memset(&ring[i].bus_dmadesc, 0, sizeof(ring[i].bus_dmadesc));
6068e269d99SConrad Meyer 		ring[i].id = i;
607b80b32a2STycho Nightingale 		error = bus_dmamap_create(ioat->data_tag, 0,
608b80b32a2STycho Nightingale                     &ring[i].src_dmamap);
609b80b32a2STycho Nightingale 		if (error != 0) {
610b80b32a2STycho Nightingale 			ioat_log_message(0,
611b80b32a2STycho Nightingale 			    "%s: bus_dmamap_create failed %d\n", __func__,
612b80b32a2STycho Nightingale 			    error);
613b80b32a2STycho Nightingale 			return (error);
614b80b32a2STycho Nightingale 		}
615b80b32a2STycho Nightingale 		error = bus_dmamap_create(ioat->data_tag, 0,
616b80b32a2STycho Nightingale                     &ring[i].dst_dmamap);
617b80b32a2STycho Nightingale 		if (error != 0) {
618b80b32a2STycho Nightingale 			ioat_log_message(0,
619b80b32a2STycho Nightingale 			    "%s: bus_dmamap_create failed %d\n", __func__,
620b80b32a2STycho Nightingale 			    error);
621b80b32a2STycho Nightingale 			return (error);
622b80b32a2STycho Nightingale 		}
623b80b32a2STycho Nightingale 		error = bus_dmamap_create(ioat->data_tag, 0,
624b80b32a2STycho Nightingale                     &ring[i].src2_dmamap);
625b80b32a2STycho Nightingale 		if (error != 0) {
626b80b32a2STycho Nightingale 			ioat_log_message(0,
627b80b32a2STycho Nightingale 			    "%s: bus_dmamap_create failed %d\n", __func__,
628b80b32a2STycho Nightingale 			    error);
629b80b32a2STycho Nightingale 			return (error);
630b80b32a2STycho Nightingale 		}
631b80b32a2STycho Nightingale 		error = bus_dmamap_create(ioat->data_tag, 0,
632b80b32a2STycho Nightingale                     &ring[i].dst2_dmamap);
633b80b32a2STycho Nightingale 		if (error != 0) {
634b80b32a2STycho Nightingale 			ioat_log_message(0,
635b80b32a2STycho Nightingale 			    "%s: bus_dmamap_create failed %d\n", __func__,
636b80b32a2STycho Nightingale 			    error);
637b80b32a2STycho Nightingale 			return (error);
638b80b32a2STycho Nightingale 		}
639e974f91cSConrad Meyer 	}
640e974f91cSConrad Meyer 
6418e269d99SConrad Meyer 	for (i = 0; i < num_descriptors; i++) {
6428e269d99SConrad Meyer 		dma_hw_desc = &ioat->hw_desc_ring[i].dma;
6438e269d99SConrad Meyer 		dma_hw_desc->next = RING_PHYS_ADDR(ioat, i + 1);
644e974f91cSConrad Meyer 	}
645e974f91cSConrad Meyer 
646348efb14SAlexander Motin 	ioat->tail = ioat->head = 0;
647348efb14SAlexander Motin 	*ioat->comp_update = ioat->last_seen =
648348efb14SAlexander Motin 	    RING_PHYS_ADDR(ioat, ioat->tail - 1);
649e974f91cSConrad Meyer 	return (0);
650e974f91cSConrad Meyer }
651e974f91cSConrad Meyer 
652e974f91cSConrad Meyer static int
653e974f91cSConrad Meyer ioat_map_pci_bar(struct ioat_softc *ioat)
654e974f91cSConrad Meyer {
655e974f91cSConrad Meyer 
656e974f91cSConrad Meyer 	ioat->pci_resource_id = PCIR_BAR(0);
657e88e14b9SConrad Meyer 	ioat->pci_resource = bus_alloc_resource_any(ioat->device,
658e88e14b9SConrad Meyer 	    SYS_RES_MEMORY, &ioat->pci_resource_id, RF_ACTIVE);
659e974f91cSConrad Meyer 
660e974f91cSConrad Meyer 	if (ioat->pci_resource == NULL) {
661e974f91cSConrad Meyer 		ioat_log_message(0, "unable to allocate pci resource\n");
662e974f91cSConrad Meyer 		return (ENODEV);
663e974f91cSConrad Meyer 	}
664e974f91cSConrad Meyer 
665e974f91cSConrad Meyer 	ioat->pci_bus_tag = rman_get_bustag(ioat->pci_resource);
666e974f91cSConrad Meyer 	ioat->pci_bus_handle = rman_get_bushandle(ioat->pci_resource);
667e974f91cSConrad Meyer 	return (0);
668e974f91cSConrad Meyer }
669e974f91cSConrad Meyer 
670e974f91cSConrad Meyer static void
671e974f91cSConrad Meyer ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg, int error)
672e974f91cSConrad Meyer {
673e974f91cSConrad Meyer 	struct ioat_softc *ioat = arg;
674e974f91cSConrad Meyer 
675cea5b880SConrad Meyer 	KASSERT(error == 0, ("%s: error:%d", __func__, error));
676e974f91cSConrad Meyer 	ioat->comp_update_bus_addr = seg[0].ds_addr;
677e974f91cSConrad Meyer }
678e974f91cSConrad Meyer 
679e974f91cSConrad Meyer static void
680e974f91cSConrad Meyer ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
681e974f91cSConrad Meyer {
682e974f91cSConrad Meyer 	bus_addr_t *baddr;
683e974f91cSConrad Meyer 
684cea5b880SConrad Meyer 	KASSERT(error == 0, ("%s: error:%d", __func__, error));
685e974f91cSConrad Meyer 	baddr = arg;
686e974f91cSConrad Meyer 	*baddr = segs->ds_addr;
687e974f91cSConrad Meyer }
688e974f91cSConrad Meyer 
689e974f91cSConrad Meyer /*
690e974f91cSConrad Meyer  * Interrupt setup and handlers
691e974f91cSConrad Meyer  */
692e974f91cSConrad Meyer static int
6934253ea50SConrad Meyer ioat_setup_intr(struct ioat_softc *ioat)
694e974f91cSConrad Meyer {
695e974f91cSConrad Meyer 	uint32_t num_vectors;
696e974f91cSConrad Meyer 	int error;
697e974f91cSConrad Meyer 	boolean_t use_msix;
698e974f91cSConrad Meyer 	boolean_t force_legacy_interrupts;
699e974f91cSConrad Meyer 
700e974f91cSConrad Meyer 	use_msix = FALSE;
701e974f91cSConrad Meyer 	force_legacy_interrupts = FALSE;
702e974f91cSConrad Meyer 
703e974f91cSConrad Meyer 	if (!g_force_legacy_interrupts && pci_msix_count(ioat->device) >= 1) {
704e974f91cSConrad Meyer 		num_vectors = 1;
705e974f91cSConrad Meyer 		pci_alloc_msix(ioat->device, &num_vectors);
706e974f91cSConrad Meyer 		if (num_vectors == 1)
707e974f91cSConrad Meyer 			use_msix = TRUE;
708e974f91cSConrad Meyer 	}
709e974f91cSConrad Meyer 
710e974f91cSConrad Meyer 	if (use_msix) {
711e974f91cSConrad Meyer 		ioat->rid = 1;
712e974f91cSConrad Meyer 		ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
713e974f91cSConrad Meyer 		    &ioat->rid, RF_ACTIVE);
714e974f91cSConrad Meyer 	} else {
715e974f91cSConrad Meyer 		ioat->rid = 0;
716e974f91cSConrad Meyer 		ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
717e974f91cSConrad Meyer 		    &ioat->rid, RF_SHAREABLE | RF_ACTIVE);
718e974f91cSConrad Meyer 	}
719e974f91cSConrad Meyer 	if (ioat->res == NULL) {
720e974f91cSConrad Meyer 		ioat_log_message(0, "bus_alloc_resource failed\n");
721e974f91cSConrad Meyer 		return (ENOMEM);
722e974f91cSConrad Meyer 	}
723e974f91cSConrad Meyer 
724e974f91cSConrad Meyer 	ioat->tag = NULL;
725e974f91cSConrad Meyer 	error = bus_setup_intr(ioat->device, ioat->res, INTR_MPSAFE |
726e974f91cSConrad Meyer 	    INTR_TYPE_MISC, NULL, ioat_interrupt_handler, ioat, &ioat->tag);
727e974f91cSConrad Meyer 	if (error != 0) {
728e974f91cSConrad Meyer 		ioat_log_message(0, "bus_setup_intr failed\n");
729e974f91cSConrad Meyer 		return (error);
730e974f91cSConrad Meyer 	}
731e974f91cSConrad Meyer 
732e974f91cSConrad Meyer 	ioat_write_intrctrl(ioat, IOAT_INTRCTRL_MASTER_INT_EN);
733e974f91cSConrad Meyer 	return (0);
734e974f91cSConrad Meyer }
735e974f91cSConrad Meyer 
7364253ea50SConrad Meyer static boolean_t
7370d1a05d9SConrad Meyer ioat_model_resets_msix(struct ioat_softc *ioat)
7384253ea50SConrad Meyer {
7394253ea50SConrad Meyer 	u_int32_t pciid;
7404253ea50SConrad Meyer 
7414253ea50SConrad Meyer 	pciid = pci_get_devid(ioat->device);
7424253ea50SConrad Meyer 	switch (pciid) {
7430d1a05d9SConrad Meyer 		/* BWD: */
7440d1a05d9SConrad Meyer 	case 0x0c508086:
7450d1a05d9SConrad Meyer 	case 0x0c518086:
7460d1a05d9SConrad Meyer 	case 0x0c528086:
7470d1a05d9SConrad Meyer 	case 0x0c538086:
7480d1a05d9SConrad Meyer 		/* BDXDE: */
7494253ea50SConrad Meyer 	case 0x6f508086:
7504253ea50SConrad Meyer 	case 0x6f518086:
7514253ea50SConrad Meyer 	case 0x6f528086:
7524253ea50SConrad Meyer 	case 0x6f538086:
7534253ea50SConrad Meyer 		return (TRUE);
7544253ea50SConrad Meyer 	}
7554253ea50SConrad Meyer 
7564253ea50SConrad Meyer 	return (FALSE);
7574253ea50SConrad Meyer }
7584253ea50SConrad Meyer 
759e974f91cSConrad Meyer static void
760e974f91cSConrad Meyer ioat_interrupt_handler(void *arg)
761e974f91cSConrad Meyer {
762e974f91cSConrad Meyer 	struct ioat_softc *ioat = arg;
763e974f91cSConrad Meyer 
76401fbbc88SConrad Meyer 	ioat->stats.interrupts++;
7652f03a95fSAlexander Motin 	ioat_process_events(ioat, TRUE);
766e974f91cSConrad Meyer }
767e974f91cSConrad Meyer 
768faefad9cSConrad Meyer static int
769faefad9cSConrad Meyer chanerr_to_errno(uint32_t chanerr)
770faefad9cSConrad Meyer {
771faefad9cSConrad Meyer 
772faefad9cSConrad Meyer 	if (chanerr == 0)
773faefad9cSConrad Meyer 		return (0);
774faefad9cSConrad Meyer 	if ((chanerr & (IOAT_CHANERR_XSADDERR | IOAT_CHANERR_XDADDERR)) != 0)
775faefad9cSConrad Meyer 		return (EFAULT);
776faefad9cSConrad Meyer 	if ((chanerr & (IOAT_CHANERR_RDERR | IOAT_CHANERR_WDERR)) != 0)
777faefad9cSConrad Meyer 		return (EIO);
778faefad9cSConrad Meyer 	/* This one is probably our fault: */
779faefad9cSConrad Meyer 	if ((chanerr & IOAT_CHANERR_NDADDERR) != 0)
780faefad9cSConrad Meyer 		return (EIO);
781faefad9cSConrad Meyer 	return (EIO);
782faefad9cSConrad Meyer }
783faefad9cSConrad Meyer 
784e974f91cSConrad Meyer static void
7852f03a95fSAlexander Motin ioat_process_events(struct ioat_softc *ioat, boolean_t intr)
786e974f91cSConrad Meyer {
787e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
788e974f91cSConrad Meyer 	struct bus_dmadesc *dmadesc;
789e974f91cSConrad Meyer 	uint64_t comp_update, status;
790faefad9cSConrad Meyer 	uint32_t completed, chanerr;
791faefad9cSConrad Meyer 	int error;
792e974f91cSConrad Meyer 
7938acd3f12SAlexander Motin 	if (intr) {
794e974f91cSConrad Meyer 		mtx_lock(&ioat->cleanup_lock);
7958acd3f12SAlexander Motin 	} else {
7968acd3f12SAlexander Motin 		if (!mtx_trylock(&ioat->cleanup_lock))
7978acd3f12SAlexander Motin 			return;
7988acd3f12SAlexander Motin 	}
799e974f91cSConrad Meyer 
800fe8712f8SConrad Meyer 	/*
801fe8712f8SConrad Meyer 	 * Don't run while the hardware is being reset.  Reset is responsible
802fe8712f8SConrad Meyer 	 * for blocking new work and draining & completing existing work, so
803fe8712f8SConrad Meyer 	 * there is nothing to do until new work is queued after reset anyway.
804fe8712f8SConrad Meyer 	 */
805fe8712f8SConrad Meyer 	if (ioat->resetting_cleanup) {
806fe8712f8SConrad Meyer 		mtx_unlock(&ioat->cleanup_lock);
807fe8712f8SConrad Meyer 		return;
808fe8712f8SConrad Meyer 	}
809fe8712f8SConrad Meyer 
810e974f91cSConrad Meyer 	completed = 0;
8110d0f2640SConrad Meyer 	comp_update = *ioat->comp_update;
812e974f91cSConrad Meyer 	status = comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK;
813e974f91cSConrad Meyer 
8148e269d99SConrad Meyer 	if (status < ioat->hw_desc_bus_addr ||
8158e269d99SConrad Meyer 	    status >= ioat->hw_desc_bus_addr + (1 << ioat->ring_size_order) *
8168e269d99SConrad Meyer 	    sizeof(struct ioat_generic_hw_descriptor))
8178e269d99SConrad Meyer 		panic("Bogus completion address %jx (channel %u)",
8188e269d99SConrad Meyer 		    (uintmax_t)status, ioat->chan_idx);
8198e269d99SConrad Meyer 
8201bbc06b8SConrad Meyer 	if (status == ioat->last_seen) {
8211bbc06b8SConrad Meyer 		/*
8221bbc06b8SConrad Meyer 		 * If we landed in process_events and nothing has been
8231bbc06b8SConrad Meyer 		 * completed, check for a timeout due to channel halt.
8241bbc06b8SConrad Meyer 		 */
8251bbc06b8SConrad Meyer 		goto out;
8261bbc06b8SConrad Meyer 	}
827dc465059SConrad Meyer 	CTR4(KTR_IOAT, "%s channel=%u hw_status=0x%lx last_seen=0x%lx",
828dc465059SConrad Meyer 	    __func__, ioat->chan_idx, comp_update, ioat->last_seen);
8291bbc06b8SConrad Meyer 
8308e269d99SConrad Meyer 	while (RING_PHYS_ADDR(ioat, ioat->tail - 1) != status) {
831e974f91cSConrad Meyer 		desc = ioat_get_ring_entry(ioat, ioat->tail);
832e974f91cSConrad Meyer 		dmadesc = &desc->bus_dmadesc;
83305e4cffbSConrad Meyer 		CTR5(KTR_IOAT, "channel=%u completing desc idx %u (%p) ok  cb %p(%p)",
83405e4cffbSConrad Meyer 		    ioat->chan_idx, ioat->tail, dmadesc, dmadesc->callback_fn,
835520f6023SConrad Meyer 		    dmadesc->callback_arg);
836e974f91cSConrad Meyer 
837b80b32a2STycho Nightingale 		bus_dmamap_unload(ioat->data_tag, desc->src_dmamap);
838b80b32a2STycho Nightingale 		bus_dmamap_unload(ioat->data_tag, desc->dst_dmamap);
839b80b32a2STycho Nightingale 		bus_dmamap_unload(ioat->data_tag, desc->src2_dmamap);
840b80b32a2STycho Nightingale 		bus_dmamap_unload(ioat->data_tag, desc->dst2_dmamap);
841b80b32a2STycho Nightingale 
842faefad9cSConrad Meyer 		if (dmadesc->callback_fn != NULL)
843faefad9cSConrad Meyer 			dmadesc->callback_fn(dmadesc->callback_arg, 0);
844e974f91cSConrad Meyer 
845466b3540SConrad Meyer 		completed++;
846e974f91cSConrad Meyer 		ioat->tail++;
847e974f91cSConrad Meyer 	}
84805e4cffbSConrad Meyer 	CTR5(KTR_IOAT, "%s channel=%u head=%u tail=%u active=%u", __func__,
84905e4cffbSConrad Meyer 	    ioat->chan_idx, ioat->head, ioat->tail, ioat_get_active(ioat));
850e974f91cSConrad Meyer 
8510283c0f5SConrad Meyer 	if (completed != 0) {
8528e269d99SConrad Meyer 		ioat->last_seen = RING_PHYS_ADDR(ioat, ioat->tail - 1);
85301fbbc88SConrad Meyer 		ioat->stats.descriptors_processed += completed;
8542f03a95fSAlexander Motin 		wakeup(&ioat->tail);
8550283c0f5SConrad Meyer 	}
85601fbbc88SConrad Meyer 
8571bbc06b8SConrad Meyer out:
858e974f91cSConrad Meyer 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
859e974f91cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
860faefad9cSConrad Meyer 
861ea9e23edSConrad Meyer 	/*
862ea9e23edSConrad Meyer 	 * The device doesn't seem to reliably push suspend/halt statuses to
863ea9e23edSConrad Meyer 	 * the channel completion memory address, so poll the device register
8642f03a95fSAlexander Motin 	 * here.  For performance reasons skip it on interrupts, do it only
8652f03a95fSAlexander Motin 	 * on much more rare polling events.
866ea9e23edSConrad Meyer 	 */
8672f03a95fSAlexander Motin 	if (!intr)
868ea9e23edSConrad Meyer 		comp_update = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS;
869d2c55e5aSConrad Meyer 	if (!is_ioat_halted(comp_update) && !is_ioat_suspended(comp_update))
870faefad9cSConrad Meyer 		return;
871faefad9cSConrad Meyer 
87201fbbc88SConrad Meyer 	ioat->stats.channel_halts++;
87301fbbc88SConrad Meyer 
874faefad9cSConrad Meyer 	/*
875faefad9cSConrad Meyer 	 * Fatal programming error on this DMA channel.  Flush any outstanding
876faefad9cSConrad Meyer 	 * work with error status and restart the engine.
877faefad9cSConrad Meyer 	 */
878faefad9cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
879faefad9cSConrad Meyer 	ioat->quiescing = TRUE;
8802f03a95fSAlexander Motin 	mtx_unlock(&ioat->submit_lock);
8812f03a95fSAlexander Motin 
8823a370919SConrad Meyer 	/*
8832f03a95fSAlexander Motin 	 * This is safe to do here because the submit queue is quiesced.  We
8842f03a95fSAlexander Motin 	 * know that we will drain all outstanding events, so ioat_reset_hw
8852f03a95fSAlexander Motin 	 * can't deadlock. It is necessary to protect other ioat_process_event
8862f03a95fSAlexander Motin 	 * threads from racing ioat_reset_hw, reading an indeterminate hw
8872f03a95fSAlexander Motin 	 * state, and attempting to continue issuing completions.
8883a370919SConrad Meyer 	 */
8892f03a95fSAlexander Motin 	mtx_lock(&ioat->cleanup_lock);
8903a370919SConrad Meyer 	ioat->resetting_cleanup = TRUE;
891faefad9cSConrad Meyer 
892faefad9cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
893985efa77SConrad Meyer 	if (1 <= g_ioat_debug_level)
894faefad9cSConrad Meyer 		ioat_halted_debug(ioat, chanerr);
89501fbbc88SConrad Meyer 	ioat->stats.last_halt_chanerr = chanerr;
896faefad9cSConrad Meyer 
897faefad9cSConrad Meyer 	while (ioat_get_active(ioat) > 0) {
898faefad9cSConrad Meyer 		desc = ioat_get_ring_entry(ioat, ioat->tail);
899faefad9cSConrad Meyer 		dmadesc = &desc->bus_dmadesc;
90005e4cffbSConrad Meyer 		CTR5(KTR_IOAT, "channel=%u completing desc idx %u (%p) err cb %p(%p)",
90105e4cffbSConrad Meyer 		    ioat->chan_idx, ioat->tail, dmadesc, dmadesc->callback_fn,
902520f6023SConrad Meyer 		    dmadesc->callback_arg);
903faefad9cSConrad Meyer 
904faefad9cSConrad Meyer 		if (dmadesc->callback_fn != NULL)
905faefad9cSConrad Meyer 			dmadesc->callback_fn(dmadesc->callback_arg,
906faefad9cSConrad Meyer 			    chanerr_to_errno(chanerr));
907faefad9cSConrad Meyer 
908faefad9cSConrad Meyer 		ioat->tail++;
90901fbbc88SConrad Meyer 		ioat->stats.descriptors_processed++;
91001fbbc88SConrad Meyer 		ioat->stats.descriptors_error++;
911faefad9cSConrad Meyer 	}
91205e4cffbSConrad Meyer 	CTR5(KTR_IOAT, "%s channel=%u head=%u tail=%u active=%u", __func__,
91305e4cffbSConrad Meyer 	    ioat->chan_idx, ioat->head, ioat->tail, ioat_get_active(ioat));
914faefad9cSConrad Meyer 
915faefad9cSConrad Meyer 	/* Clear error status */
916faefad9cSConrad Meyer 	ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
917faefad9cSConrad Meyer 
918faefad9cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
919faefad9cSConrad Meyer 
920faefad9cSConrad Meyer 	ioat_log_message(0, "Resetting channel to recover from error\n");
921374b05e1SConrad Meyer 	error = taskqueue_enqueue(taskqueue_thread, &ioat->reset_task);
922374b05e1SConrad Meyer 	KASSERT(error == 0,
923374b05e1SConrad Meyer 	    ("%s: taskqueue_enqueue failed: %d", __func__, error));
924374b05e1SConrad Meyer }
925374b05e1SConrad Meyer 
926374b05e1SConrad Meyer static void
927374b05e1SConrad Meyer ioat_reset_hw_task(void *ctx, int pending __unused)
928374b05e1SConrad Meyer {
929374b05e1SConrad Meyer 	struct ioat_softc *ioat;
930374b05e1SConrad Meyer 	int error;
931374b05e1SConrad Meyer 
932374b05e1SConrad Meyer 	ioat = ctx;
933374b05e1SConrad Meyer 	ioat_log_message(1, "%s: Resetting channel\n", __func__);
934374b05e1SConrad Meyer 
935faefad9cSConrad Meyer 	error = ioat_reset_hw(ioat);
936faefad9cSConrad Meyer 	KASSERT(error == 0, ("%s: reset failed: %d", __func__, error));
937374b05e1SConrad Meyer 	(void)error;
938e974f91cSConrad Meyer }
939e974f91cSConrad Meyer 
940e974f91cSConrad Meyer /*
941e974f91cSConrad Meyer  * User API functions
942e974f91cSConrad Meyer  */
943f8f92e91SConrad Meyer unsigned
944f8f92e91SConrad Meyer ioat_get_nchannels(void)
945f8f92e91SConrad Meyer {
946f8f92e91SConrad Meyer 
947f8f92e91SConrad Meyer 	return (ioat_channel_index);
948f8f92e91SConrad Meyer }
949f8f92e91SConrad Meyer 
950e974f91cSConrad Meyer bus_dmaengine_t
9510ff814e8SConrad Meyer ioat_get_dmaengine(uint32_t index, int flags)
952e974f91cSConrad Meyer {
9530ff814e8SConrad Meyer 	struct ioat_softc *ioat;
9540ff814e8SConrad Meyer 
9550ff814e8SConrad Meyer 	KASSERT((flags & ~(M_NOWAIT | M_WAITOK)) == 0,
9560ff814e8SConrad Meyer 	    ("invalid flags: 0x%08x", flags));
9570ff814e8SConrad Meyer 	KASSERT((flags & (M_NOWAIT | M_WAITOK)) != (M_NOWAIT | M_WAITOK),
9580ff814e8SConrad Meyer 	    ("invalid wait | nowait"));
959e974f91cSConrad Meyer 
9602f03a95fSAlexander Motin 	mtx_lock(&ioat_list_mtx);
9612f03a95fSAlexander Motin 	if (index >= ioat_channel_index ||
9622f03a95fSAlexander Motin 	    (ioat = ioat_channel[index]) == NULL) {
9632f03a95fSAlexander Motin 		mtx_unlock(&ioat_list_mtx);
964e974f91cSConrad Meyer 		return (NULL);
9652f03a95fSAlexander Motin 	}
9662f03a95fSAlexander Motin 	mtx_lock(&ioat->submit_lock);
9672f03a95fSAlexander Motin 	mtx_unlock(&ioat_list_mtx);
9685f77bd3eSConrad Meyer 
9692f03a95fSAlexander Motin 	if (ioat->destroying) {
9702f03a95fSAlexander Motin 		mtx_unlock(&ioat->submit_lock);
9710ff814e8SConrad Meyer 		return (NULL);
9720ff814e8SConrad Meyer 	}
9730ff814e8SConrad Meyer 
9742f03a95fSAlexander Motin 	ioat_get(ioat);
9752f03a95fSAlexander Motin 	if (ioat->quiescing) {
9762f03a95fSAlexander Motin 		if ((flags & M_NOWAIT) != 0) {
9772f03a95fSAlexander Motin 			ioat_put(ioat);
9782f03a95fSAlexander Motin 			mtx_unlock(&ioat->submit_lock);
9792f03a95fSAlexander Motin 			return (NULL);
9802f03a95fSAlexander Motin 		}
9812f03a95fSAlexander Motin 
9822f03a95fSAlexander Motin 		while (ioat->quiescing && !ioat->destroying)
9832f03a95fSAlexander Motin 			msleep(&ioat->quiescing, &ioat->submit_lock, 0, "getdma", 0);
9842f03a95fSAlexander Motin 
9852f03a95fSAlexander Motin 		if (ioat->destroying) {
9862f03a95fSAlexander Motin 			ioat_put(ioat);
9872f03a95fSAlexander Motin 			mtx_unlock(&ioat->submit_lock);
9882f03a95fSAlexander Motin 			return (NULL);
9892f03a95fSAlexander Motin 		}
9902f03a95fSAlexander Motin 	}
9912f03a95fSAlexander Motin 	mtx_unlock(&ioat->submit_lock);
9922f03a95fSAlexander Motin 	return (&ioat->dmaengine);
993466b3540SConrad Meyer }
994466b3540SConrad Meyer 
995466b3540SConrad Meyer void
996466b3540SConrad Meyer ioat_put_dmaengine(bus_dmaengine_t dmaengine)
997466b3540SConrad Meyer {
998466b3540SConrad Meyer 	struct ioat_softc *ioat;
999466b3540SConrad Meyer 
1000466b3540SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
10012f03a95fSAlexander Motin 	mtx_lock(&ioat->submit_lock);
10022f03a95fSAlexander Motin 	ioat_put(ioat);
10032f03a95fSAlexander Motin 	mtx_unlock(&ioat->submit_lock);
1004e974f91cSConrad Meyer }
1005e974f91cSConrad Meyer 
10065ca9fc2aSConrad Meyer int
100731bf2875SConrad Meyer ioat_get_hwversion(bus_dmaengine_t dmaengine)
100831bf2875SConrad Meyer {
100931bf2875SConrad Meyer 	struct ioat_softc *ioat;
101031bf2875SConrad Meyer 
101131bf2875SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
101231bf2875SConrad Meyer 	return (ioat->version);
101331bf2875SConrad Meyer }
101431bf2875SConrad Meyer 
1015bd81fe68SConrad Meyer size_t
1016bd81fe68SConrad Meyer ioat_get_max_io_size(bus_dmaengine_t dmaengine)
1017bd81fe68SConrad Meyer {
1018bd81fe68SConrad Meyer 	struct ioat_softc *ioat;
1019bd81fe68SConrad Meyer 
1020bd81fe68SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1021bd81fe68SConrad Meyer 	return (ioat->max_xfer_size);
1022bd81fe68SConrad Meyer }
1023bd81fe68SConrad Meyer 
1024f8253f1aSConrad Meyer uint32_t
1025f8253f1aSConrad Meyer ioat_get_capabilities(bus_dmaengine_t dmaengine)
1026f8253f1aSConrad Meyer {
1027f8253f1aSConrad Meyer 	struct ioat_softc *ioat;
1028f8253f1aSConrad Meyer 
1029f8253f1aSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1030f8253f1aSConrad Meyer 	return (ioat->capabilities);
1031f8253f1aSConrad Meyer }
1032f8253f1aSConrad Meyer 
103331bf2875SConrad Meyer int
10347280125eSAlexander Motin ioat_get_domain(bus_dmaengine_t dmaengine, int *domain)
10357280125eSAlexander Motin {
10367280125eSAlexander Motin 	struct ioat_softc *ioat;
10377280125eSAlexander Motin 
10387280125eSAlexander Motin 	ioat = to_ioat_softc(dmaengine);
10397280125eSAlexander Motin 	return (bus_get_domain(ioat->device, domain));
10407280125eSAlexander Motin }
10417280125eSAlexander Motin 
10427280125eSAlexander Motin int
10435ca9fc2aSConrad Meyer ioat_set_interrupt_coalesce(bus_dmaengine_t dmaengine, uint16_t delay)
10445ca9fc2aSConrad Meyer {
10455ca9fc2aSConrad Meyer 	struct ioat_softc *ioat;
10465ca9fc2aSConrad Meyer 
10475ca9fc2aSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
10485ca9fc2aSConrad Meyer 	if (!ioat->intrdelay_supported)
10495ca9fc2aSConrad Meyer 		return (ENODEV);
10505ca9fc2aSConrad Meyer 	if (delay > ioat->intrdelay_max)
10515ca9fc2aSConrad Meyer 		return (ERANGE);
10525ca9fc2aSConrad Meyer 
10535ca9fc2aSConrad Meyer 	ioat_write_2(ioat, IOAT_INTRDELAY_OFFSET, delay);
10545ca9fc2aSConrad Meyer 	ioat->cached_intrdelay =
10555ca9fc2aSConrad Meyer 	    ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) & IOAT_INTRDELAY_US_MASK;
10565ca9fc2aSConrad Meyer 	return (0);
10575ca9fc2aSConrad Meyer }
10585ca9fc2aSConrad Meyer 
10595ca9fc2aSConrad Meyer uint16_t
10605ca9fc2aSConrad Meyer ioat_get_max_coalesce_period(bus_dmaengine_t dmaengine)
10615ca9fc2aSConrad Meyer {
10625ca9fc2aSConrad Meyer 	struct ioat_softc *ioat;
10635ca9fc2aSConrad Meyer 
10645ca9fc2aSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
10655ca9fc2aSConrad Meyer 	return (ioat->intrdelay_max);
10665ca9fc2aSConrad Meyer }
10675ca9fc2aSConrad Meyer 
1068e974f91cSConrad Meyer void
1069e974f91cSConrad Meyer ioat_acquire(bus_dmaengine_t dmaengine)
1070e974f91cSConrad Meyer {
1071e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1072e974f91cSConrad Meyer 
1073e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1074e974f91cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
1075520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
107658a639b7SConrad Meyer 	ioat->acq_head = ioat->head;
1077e974f91cSConrad Meyer }
1078e974f91cSConrad Meyer 
10791502e363SConrad Meyer int
10801502e363SConrad Meyer ioat_acquire_reserve(bus_dmaengine_t dmaengine, unsigned n, int mflags)
10811502e363SConrad Meyer {
10821502e363SConrad Meyer 	struct ioat_softc *ioat;
10831502e363SConrad Meyer 	int error;
10841502e363SConrad Meyer 
10851502e363SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
10861502e363SConrad Meyer 	ioat_acquire(dmaengine);
10871502e363SConrad Meyer 
10881502e363SConrad Meyer 	error = ioat_reserve_space(ioat, n, mflags);
10891502e363SConrad Meyer 	if (error != 0)
10901502e363SConrad Meyer 		ioat_release(dmaengine);
10911502e363SConrad Meyer 	return (error);
10921502e363SConrad Meyer }
10931502e363SConrad Meyer 
1094e974f91cSConrad Meyer void
1095e974f91cSConrad Meyer ioat_release(bus_dmaengine_t dmaengine)
1096e974f91cSConrad Meyer {
1097e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1098e974f91cSConrad Meyer 
1099e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
11002f03a95fSAlexander Motin 	CTR3(KTR_IOAT, "%s channel=%u dispatch1 head=%u", __func__,
11012f03a95fSAlexander Motin 	    ioat->chan_idx, ioat->head);
110276305bb8SConrad Meyer 	KFAIL_POINT_CODE(DEBUG_FP, ioat_release, /* do nothing */);
11032f03a95fSAlexander Motin 	CTR3(KTR_IOAT, "%s channel=%u dispatch2 head=%u", __func__,
11042f03a95fSAlexander Motin 	    ioat->chan_idx, ioat->head);
110576305bb8SConrad Meyer 
110658a639b7SConrad Meyer 	if (ioat->acq_head != ioat->head) {
110758a639b7SConrad Meyer 		ioat_write_2(ioat, IOAT_DMACOUNT_OFFSET,
11082f03a95fSAlexander Motin 		    (uint16_t)ioat->head);
1109f44abf1fSConrad Meyer 
11102f03a95fSAlexander Motin 		if (!callout_pending(&ioat->poll_timer)) {
1111657dc81dSAlexander Motin 			callout_reset_on(&ioat->poll_timer, 1,
1112657dc81dSAlexander Motin 			    ioat_poll_timer_callback, ioat, ioat->cpu);
111358a639b7SConrad Meyer 		}
1114f44abf1fSConrad Meyer 	}
1115e974f91cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
1116e974f91cSConrad Meyer }
1117e974f91cSConrad Meyer 
11189e3bbf26SConrad Meyer static struct ioat_descriptor *
11199e3bbf26SConrad Meyer ioat_op_generic(struct ioat_softc *ioat, uint8_t op,
11209e3bbf26SConrad Meyer     uint32_t size, uint64_t src, uint64_t dst,
11219e3bbf26SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg,
11229e3bbf26SConrad Meyer     uint32_t flags)
1123e974f91cSConrad Meyer {
11249e3bbf26SConrad Meyer 	struct ioat_generic_hw_descriptor *hw_desc;
1125e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
1126b80b32a2STycho Nightingale 	bus_dma_segment_t seg;
1127b80b32a2STycho Nightingale 	int mflags, nseg, error;
1128e974f91cSConrad Meyer 
11299e3bbf26SConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
11309e3bbf26SConrad Meyer 
1131bec7ff79SConrad Meyer 	KASSERT((flags & ~_DMA_GENERIC_FLAGS) == 0,
1132bec7ff79SConrad Meyer 	    ("Unrecognized flag(s): %#x", flags & ~_DMA_GENERIC_FLAGS));
1133630d9800SAlexander Motin 	KASSERT(size <= ioat->max_xfer_size, ("%s: size too big (%u > %u)",
1134630d9800SAlexander Motin 	    __func__, (unsigned)size, ioat->max_xfer_size));
1135630d9800SAlexander Motin 
1136bf8553eaSConrad Meyer 	if ((flags & DMA_NO_WAIT) != 0)
1137bf8553eaSConrad Meyer 		mflags = M_NOWAIT;
1138bf8553eaSConrad Meyer 	else
1139bf8553eaSConrad Meyer 		mflags = M_WAITOK;
1140e974f91cSConrad Meyer 
1141bf8553eaSConrad Meyer 	if (ioat_reserve_space(ioat, 1, mflags) != 0)
1142e974f91cSConrad Meyer 		return (NULL);
1143e974f91cSConrad Meyer 
1144e974f91cSConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->head);
11458e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, ioat->head)->generic;
1146e974f91cSConrad Meyer 
1147e974f91cSConrad Meyer 	hw_desc->u.control_raw = 0;
11489e3bbf26SConrad Meyer 	hw_desc->u.control_generic.op = op;
11499e3bbf26SConrad Meyer 	hw_desc->u.control_generic.completion_update = 1;
1150e974f91cSConrad Meyer 
1151e974f91cSConrad Meyer 	if ((flags & DMA_INT_EN) != 0)
11529e3bbf26SConrad Meyer 		hw_desc->u.control_generic.int_enable = 1;
11536ca07079SConrad Meyer 	if ((flags & DMA_FENCE) != 0)
11546ca07079SConrad Meyer 		hw_desc->u.control_generic.fence = 1;
1155e974f91cSConrad Meyer 
11569e3bbf26SConrad Meyer 	hw_desc->size = size;
1157b80b32a2STycho Nightingale 
1158b80b32a2STycho Nightingale 	if (src != 0) {
1159b80b32a2STycho Nightingale 		nseg = -1;
1160b80b32a2STycho Nightingale 		error = _bus_dmamap_load_phys(ioat->data_tag, desc->src_dmamap,
1161b80b32a2STycho Nightingale 		    src, size, 0, &seg, &nseg);
1162b80b32a2STycho Nightingale 		if (error != 0) {
1163b80b32a2STycho Nightingale 			ioat_log_message(0, "%s: _bus_dmamap_load_phys"
1164b80b32a2STycho Nightingale 			    " failed %d\n", __func__, error);
1165b80b32a2STycho Nightingale 			return (NULL);
1166b80b32a2STycho Nightingale 		}
1167b80b32a2STycho Nightingale 		hw_desc->src_addr = seg.ds_addr;
1168b80b32a2STycho Nightingale 	}
1169b80b32a2STycho Nightingale 
1170b80b32a2STycho Nightingale 	if (dst != 0) {
1171b80b32a2STycho Nightingale 		nseg = -1;
1172b80b32a2STycho Nightingale 		error = _bus_dmamap_load_phys(ioat->data_tag, desc->dst_dmamap,
1173b80b32a2STycho Nightingale 		    dst, size, 0, &seg, &nseg);
1174b80b32a2STycho Nightingale 		if (error != 0) {
1175b80b32a2STycho Nightingale 			ioat_log_message(0, "%s: _bus_dmamap_load_phys"
1176b80b32a2STycho Nightingale 			    " failed %d\n", __func__, error);
1177b80b32a2STycho Nightingale 			return (NULL);
1178b80b32a2STycho Nightingale 		}
1179b80b32a2STycho Nightingale 		hw_desc->dest_addr = seg.ds_addr;
1180b80b32a2STycho Nightingale 	}
1181e974f91cSConrad Meyer 
1182e974f91cSConrad Meyer 	desc->bus_dmadesc.callback_fn = callback_fn;
1183e974f91cSConrad Meyer 	desc->bus_dmadesc.callback_arg = callback_arg;
11849e3bbf26SConrad Meyer 	return (desc);
11859e3bbf26SConrad Meyer }
1186e974f91cSConrad Meyer 
11879e3bbf26SConrad Meyer struct bus_dmadesc *
11889e3bbf26SConrad Meyer ioat_null(bus_dmaengine_t dmaengine, bus_dmaengine_callback_t callback_fn,
11899e3bbf26SConrad Meyer     void *callback_arg, uint32_t flags)
11909e3bbf26SConrad Meyer {
11919e3bbf26SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
11929e3bbf26SConrad Meyer 	struct ioat_descriptor *desc;
11939e3bbf26SConrad Meyer 	struct ioat_softc *ioat;
11949e3bbf26SConrad Meyer 
11959e3bbf26SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1196520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
11979e3bbf26SConrad Meyer 
11989e3bbf26SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, 8, 0, 0, callback_fn,
11999e3bbf26SConrad Meyer 	    callback_arg, flags);
12009e3bbf26SConrad Meyer 	if (desc == NULL)
12019e3bbf26SConrad Meyer 		return (NULL);
12029e3bbf26SConrad Meyer 
12038e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->dma;
12049e3bbf26SConrad Meyer 	hw_desc->u.control.null = 1;
1205e974f91cSConrad Meyer 	ioat_submit_single(ioat);
1206e974f91cSConrad Meyer 	return (&desc->bus_dmadesc);
1207e974f91cSConrad Meyer }
1208e974f91cSConrad Meyer 
1209e974f91cSConrad Meyer struct bus_dmadesc *
1210e974f91cSConrad Meyer ioat_copy(bus_dmaengine_t dmaengine, bus_addr_t dst,
1211e974f91cSConrad Meyer     bus_addr_t src, bus_size_t len, bus_dmaengine_callback_t callback_fn,
1212e974f91cSConrad Meyer     void *callback_arg, uint32_t flags)
1213e974f91cSConrad Meyer {
1214e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
12159e3bbf26SConrad Meyer 	struct ioat_descriptor *desc;
1216e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1217e974f91cSConrad Meyer 
1218e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
12199e3bbf26SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, len, src, dst, callback_fn,
12209e3bbf26SConrad Meyer 	    callback_arg, flags);
12219e3bbf26SConrad Meyer 	if (desc == NULL)
1222e974f91cSConrad Meyer 		return (NULL);
1223e974f91cSConrad Meyer 
12248e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->dma;
1225e974f91cSConrad Meyer 	if (g_ioat_debug_level >= 3)
1226e974f91cSConrad Meyer 		dump_descriptor(hw_desc);
1227e974f91cSConrad Meyer 
1228e974f91cSConrad Meyer 	ioat_submit_single(ioat);
122905e4cffbSConrad Meyer 	CTR6(KTR_IOAT, "%s channel=%u desc=%p dest=%lx src=%lx len=%lx",
123005e4cffbSConrad Meyer 	    __func__, ioat->chan_idx, &desc->bus_dmadesc, dst, src, len);
1231e974f91cSConrad Meyer 	return (&desc->bus_dmadesc);
1232e974f91cSConrad Meyer }
1233e974f91cSConrad Meyer 
12342a4fd6b1SConrad Meyer struct bus_dmadesc *
12359950fde0SConrad Meyer ioat_copy_8k_aligned(bus_dmaengine_t dmaengine, bus_addr_t dst1,
12369950fde0SConrad Meyer     bus_addr_t dst2, bus_addr_t src1, bus_addr_t src2,
12379950fde0SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
12389950fde0SConrad Meyer {
12399950fde0SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
12409950fde0SConrad Meyer 	struct ioat_descriptor *desc;
12419950fde0SConrad Meyer 	struct ioat_softc *ioat;
1242b80b32a2STycho Nightingale 	bus_size_t src1_len, dst1_len;
1243b80b32a2STycho Nightingale 	bus_dma_segment_t seg;
1244b80b32a2STycho Nightingale 	int nseg, error;
12459950fde0SConrad Meyer 
12469950fde0SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1247520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
12489950fde0SConrad Meyer 
1249630d9800SAlexander Motin 	KASSERT(((src1 | src2 | dst1 | dst2) & PAGE_MASK) == 0,
1250630d9800SAlexander Motin 	    ("%s: addresses are not page-aligned", __func__));
12519950fde0SConrad Meyer 
1252b80b32a2STycho Nightingale 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, 2 * PAGE_SIZE, 0, 0,
12539950fde0SConrad Meyer 	    callback_fn, callback_arg, flags);
12549950fde0SConrad Meyer 	if (desc == NULL)
12559950fde0SConrad Meyer 		return (NULL);
12569950fde0SConrad Meyer 
12578e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->dma;
1258b80b32a2STycho Nightingale 
1259b80b32a2STycho Nightingale 	src1_len = (src2 != src1 + PAGE_SIZE) ? PAGE_SIZE : 2 * PAGE_SIZE;
1260b80b32a2STycho Nightingale 	nseg = -1;
1261b80b32a2STycho Nightingale 	error = _bus_dmamap_load_phys(ioat->data_tag,
1262b80b32a2STycho Nightingale 	    desc->src_dmamap, src1, src1_len, 0, &seg, &nseg);
1263b80b32a2STycho Nightingale 	if (error != 0) {
1264b80b32a2STycho Nightingale 		ioat_log_message(0, "%s: _bus_dmamap_load_phys"
1265b80b32a2STycho Nightingale 		    " failed %d\n", __func__, error);
1266b80b32a2STycho Nightingale 		return (NULL);
12679950fde0SConrad Meyer 	}
1268b80b32a2STycho Nightingale 	hw_desc->src_addr = seg.ds_addr;
1269b80b32a2STycho Nightingale 	if (src1_len != 2 * PAGE_SIZE) {
1270b80b32a2STycho Nightingale 		hw_desc->u.control.src_page_break = 1;
1271b80b32a2STycho Nightingale 		nseg = -1;
1272b80b32a2STycho Nightingale 		error = _bus_dmamap_load_phys(ioat->data_tag,
1273b80b32a2STycho Nightingale 		    desc->src2_dmamap, src2, PAGE_SIZE, 0, &seg, &nseg);
1274b80b32a2STycho Nightingale 		if (error != 0) {
1275b80b32a2STycho Nightingale 			ioat_log_message(0, "%s: _bus_dmamap_load_phys"
1276b80b32a2STycho Nightingale 			    " failed %d\n", __func__, error);
1277b80b32a2STycho Nightingale 			return (NULL);
1278b80b32a2STycho Nightingale 		}
1279b80b32a2STycho Nightingale 		hw_desc->next_src_addr = seg.ds_addr;
1280b80b32a2STycho Nightingale 	}
1281b80b32a2STycho Nightingale 
1282b80b32a2STycho Nightingale 	dst1_len = (dst2 != dst1 + PAGE_SIZE) ? PAGE_SIZE : 2 * PAGE_SIZE;
1283b80b32a2STycho Nightingale 	nseg = -1;
1284b80b32a2STycho Nightingale 	error = _bus_dmamap_load_phys(ioat->data_tag,
1285b80b32a2STycho Nightingale 	    desc->dst_dmamap, dst1, dst1_len, 0, &seg, &nseg);
1286b80b32a2STycho Nightingale 	if (error != 0) {
1287b80b32a2STycho Nightingale 		ioat_log_message(0, "%s: _bus_dmamap_load_phys"
1288b80b32a2STycho Nightingale 		    " failed %d\n", __func__, error);
1289b80b32a2STycho Nightingale 		return (NULL);
1290b80b32a2STycho Nightingale 	}
1291b80b32a2STycho Nightingale 	hw_desc->dest_addr = seg.ds_addr;
1292b80b32a2STycho Nightingale 	if (dst1_len != 2 * PAGE_SIZE) {
12939950fde0SConrad Meyer 		hw_desc->u.control.dest_page_break = 1;
1294b80b32a2STycho Nightingale 		nseg = -1;
1295b80b32a2STycho Nightingale 		error = _bus_dmamap_load_phys(ioat->data_tag,
1296b80b32a2STycho Nightingale 		    desc->dst2_dmamap, dst2, PAGE_SIZE, 0, &seg, &nseg);
1297b80b32a2STycho Nightingale 		if (error != 0) {
1298b80b32a2STycho Nightingale 			ioat_log_message(0, "%s: _bus_dmamap_load_phys"
1299b80b32a2STycho Nightingale 			    " failed %d\n", __func__, error);
1300b80b32a2STycho Nightingale 			return (NULL);
1301b80b32a2STycho Nightingale 		}
1302b80b32a2STycho Nightingale 		hw_desc->next_dest_addr = seg.ds_addr;
13039950fde0SConrad Meyer 	}
13049950fde0SConrad Meyer 
13059950fde0SConrad Meyer 	if (g_ioat_debug_level >= 3)
13069950fde0SConrad Meyer 		dump_descriptor(hw_desc);
13079950fde0SConrad Meyer 
13089950fde0SConrad Meyer 	ioat_submit_single(ioat);
13099950fde0SConrad Meyer 	return (&desc->bus_dmadesc);
13109950fde0SConrad Meyer }
13119950fde0SConrad Meyer 
13129950fde0SConrad Meyer struct bus_dmadesc *
1313bec7ff79SConrad Meyer ioat_copy_crc(bus_dmaengine_t dmaengine, bus_addr_t dst, bus_addr_t src,
1314bec7ff79SConrad Meyer     bus_size_t len, uint32_t *initialseed, bus_addr_t crcptr,
1315bec7ff79SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
1316bec7ff79SConrad Meyer {
1317bec7ff79SConrad Meyer 	struct ioat_crc32_hw_descriptor *hw_desc;
1318bec7ff79SConrad Meyer 	struct ioat_descriptor *desc;
1319bec7ff79SConrad Meyer 	struct ioat_softc *ioat;
1320bec7ff79SConrad Meyer 	uint32_t teststore;
1321bec7ff79SConrad Meyer 	uint8_t op;
1322b80b32a2STycho Nightingale 	bus_dma_segment_t seg;
1323b80b32a2STycho Nightingale 	int nseg, error;
1324bec7ff79SConrad Meyer 
1325bec7ff79SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1326520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1327bec7ff79SConrad Meyer 
1328630d9800SAlexander Motin 	KASSERT((ioat->capabilities & IOAT_DMACAP_MOVECRC) != 0,
1329630d9800SAlexander Motin 	    ("%s: device lacks MOVECRC capability", __func__));
1330bec7ff79SConrad Meyer 	teststore = (flags & _DMA_CRC_TESTSTORE);
1331630d9800SAlexander Motin 	KASSERT(teststore != _DMA_CRC_TESTSTORE,
1332630d9800SAlexander Motin 	    ("%s: TEST and STORE invalid", __func__));
1333630d9800SAlexander Motin 	KASSERT(teststore != 0 || (flags & DMA_CRC_INLINE) == 0,
1334630d9800SAlexander Motin 	    ("%s: INLINE invalid without TEST or STORE", __func__));
1335bec7ff79SConrad Meyer 
1336bec7ff79SConrad Meyer 	switch (teststore) {
1337bec7ff79SConrad Meyer 	case DMA_CRC_STORE:
1338bec7ff79SConrad Meyer 		op = IOAT_OP_MOVECRC_STORE;
1339bec7ff79SConrad Meyer 		break;
1340bec7ff79SConrad Meyer 	case DMA_CRC_TEST:
1341bec7ff79SConrad Meyer 		op = IOAT_OP_MOVECRC_TEST;
1342bec7ff79SConrad Meyer 		break;
1343bec7ff79SConrad Meyer 	default:
1344bec7ff79SConrad Meyer 		KASSERT(teststore == 0, ("bogus"));
1345bec7ff79SConrad Meyer 		op = IOAT_OP_MOVECRC;
1346bec7ff79SConrad Meyer 		break;
1347bec7ff79SConrad Meyer 	}
1348bec7ff79SConrad Meyer 
1349bec7ff79SConrad Meyer 	desc = ioat_op_generic(ioat, op, len, src, dst, callback_fn,
1350bec7ff79SConrad Meyer 	    callback_arg, flags & ~_DMA_CRC_FLAGS);
1351bec7ff79SConrad Meyer 	if (desc == NULL)
1352bec7ff79SConrad Meyer 		return (NULL);
1353bec7ff79SConrad Meyer 
13548e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->crc32;
1355bec7ff79SConrad Meyer 
1356b80b32a2STycho Nightingale 	if ((flags & DMA_CRC_INLINE) == 0) {
1357b80b32a2STycho Nightingale 		nseg = -1;
13581f4a469dSAlexander Motin 		error = _bus_dmamap_load_phys(ioat->data_tag,
13591f4a469dSAlexander Motin 		    desc->dst2_dmamap, crcptr, sizeof(uint32_t), 0,
1360b80b32a2STycho Nightingale 		    &seg, &nseg);
1361b80b32a2STycho Nightingale 		if (error != 0) {
1362b80b32a2STycho Nightingale 			ioat_log_message(0, "%s: _bus_dmamap_load_phys"
1363b80b32a2STycho Nightingale 			    " failed %d\n", __func__, error);
1364b80b32a2STycho Nightingale 			return (NULL);
1365b80b32a2STycho Nightingale 		}
1366b80b32a2STycho Nightingale 		hw_desc->crc_address = seg.ds_addr;
1367b80b32a2STycho Nightingale 	} else
1368bec7ff79SConrad Meyer 		hw_desc->u.control.crc_location = 1;
1369bec7ff79SConrad Meyer 
1370bec7ff79SConrad Meyer 	if (initialseed != NULL) {
1371bec7ff79SConrad Meyer 		hw_desc->u.control.use_seed = 1;
1372bec7ff79SConrad Meyer 		hw_desc->seed = *initialseed;
1373bec7ff79SConrad Meyer 	}
1374bec7ff79SConrad Meyer 
1375bec7ff79SConrad Meyer 	if (g_ioat_debug_level >= 3)
1376bec7ff79SConrad Meyer 		dump_descriptor(hw_desc);
1377bec7ff79SConrad Meyer 
1378bec7ff79SConrad Meyer 	ioat_submit_single(ioat);
1379bec7ff79SConrad Meyer 	return (&desc->bus_dmadesc);
1380bec7ff79SConrad Meyer }
1381bec7ff79SConrad Meyer 
1382bec7ff79SConrad Meyer struct bus_dmadesc *
1383bec7ff79SConrad Meyer ioat_crc(bus_dmaengine_t dmaengine, bus_addr_t src, bus_size_t len,
1384bec7ff79SConrad Meyer     uint32_t *initialseed, bus_addr_t crcptr,
1385bec7ff79SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
1386bec7ff79SConrad Meyer {
1387bec7ff79SConrad Meyer 	struct ioat_crc32_hw_descriptor *hw_desc;
1388bec7ff79SConrad Meyer 	struct ioat_descriptor *desc;
1389bec7ff79SConrad Meyer 	struct ioat_softc *ioat;
1390bec7ff79SConrad Meyer 	uint32_t teststore;
1391bec7ff79SConrad Meyer 	uint8_t op;
1392b80b32a2STycho Nightingale 	bus_dma_segment_t seg;
1393b80b32a2STycho Nightingale 	int nseg, error;
1394bec7ff79SConrad Meyer 
1395bec7ff79SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1396520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1397bec7ff79SConrad Meyer 
1398630d9800SAlexander Motin 	KASSERT((ioat->capabilities & IOAT_DMACAP_CRC) != 0,
1399630d9800SAlexander Motin 	    ("%s: device lacks CRC capability", __func__));
1400bec7ff79SConrad Meyer 	teststore = (flags & _DMA_CRC_TESTSTORE);
1401630d9800SAlexander Motin 	KASSERT(teststore != _DMA_CRC_TESTSTORE,
1402630d9800SAlexander Motin 	    ("%s: TEST and STORE invalid", __func__));
1403630d9800SAlexander Motin 	KASSERT(teststore != 0 || (flags & DMA_CRC_INLINE) == 0,
1404630d9800SAlexander Motin 	    ("%s: INLINE invalid without TEST or STORE", __func__));
1405bec7ff79SConrad Meyer 
1406bec7ff79SConrad Meyer 	switch (teststore) {
1407bec7ff79SConrad Meyer 	case DMA_CRC_STORE:
1408bec7ff79SConrad Meyer 		op = IOAT_OP_CRC_STORE;
1409bec7ff79SConrad Meyer 		break;
1410bec7ff79SConrad Meyer 	case DMA_CRC_TEST:
1411bec7ff79SConrad Meyer 		op = IOAT_OP_CRC_TEST;
1412bec7ff79SConrad Meyer 		break;
1413bec7ff79SConrad Meyer 	default:
1414bec7ff79SConrad Meyer 		KASSERT(teststore == 0, ("bogus"));
1415bec7ff79SConrad Meyer 		op = IOAT_OP_CRC;
1416bec7ff79SConrad Meyer 		break;
1417bec7ff79SConrad Meyer 	}
1418bec7ff79SConrad Meyer 
1419bec7ff79SConrad Meyer 	desc = ioat_op_generic(ioat, op, len, src, 0, callback_fn,
1420bec7ff79SConrad Meyer 	    callback_arg, flags & ~_DMA_CRC_FLAGS);
1421bec7ff79SConrad Meyer 	if (desc == NULL)
1422bec7ff79SConrad Meyer 		return (NULL);
1423bec7ff79SConrad Meyer 
14248e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->crc32;
1425bec7ff79SConrad Meyer 
1426b80b32a2STycho Nightingale 	if ((flags & DMA_CRC_INLINE) == 0) {
1427b80b32a2STycho Nightingale 		nseg = -1;
14281f4a469dSAlexander Motin 		error = _bus_dmamap_load_phys(ioat->data_tag,
14291f4a469dSAlexander Motin 		    desc->dst2_dmamap, crcptr, sizeof(uint32_t), 0,
1430b80b32a2STycho Nightingale 		    &seg, &nseg);
1431b80b32a2STycho Nightingale 		if (error != 0) {
1432b80b32a2STycho Nightingale 			ioat_log_message(0, "%s: _bus_dmamap_load_phys"
1433b80b32a2STycho Nightingale 			    " failed %d\n", __func__, error);
1434b80b32a2STycho Nightingale 			return (NULL);
1435b80b32a2STycho Nightingale 		}
1436b80b32a2STycho Nightingale 		hw_desc->crc_address = seg.ds_addr;
1437b80b32a2STycho Nightingale 	} else
1438bec7ff79SConrad Meyer 		hw_desc->u.control.crc_location = 1;
1439bec7ff79SConrad Meyer 
1440bec7ff79SConrad Meyer 	if (initialseed != NULL) {
1441bec7ff79SConrad Meyer 		hw_desc->u.control.use_seed = 1;
1442bec7ff79SConrad Meyer 		hw_desc->seed = *initialseed;
1443bec7ff79SConrad Meyer 	}
1444bec7ff79SConrad Meyer 
1445bec7ff79SConrad Meyer 	if (g_ioat_debug_level >= 3)
1446bec7ff79SConrad Meyer 		dump_descriptor(hw_desc);
1447bec7ff79SConrad Meyer 
1448bec7ff79SConrad Meyer 	ioat_submit_single(ioat);
1449bec7ff79SConrad Meyer 	return (&desc->bus_dmadesc);
1450bec7ff79SConrad Meyer }
1451bec7ff79SConrad Meyer 
1452bec7ff79SConrad Meyer struct bus_dmadesc *
14532a4fd6b1SConrad Meyer ioat_blockfill(bus_dmaengine_t dmaengine, bus_addr_t dst, uint64_t fillpattern,
14542a4fd6b1SConrad Meyer     bus_size_t len, bus_dmaengine_callback_t callback_fn, void *callback_arg,
14552a4fd6b1SConrad Meyer     uint32_t flags)
14562a4fd6b1SConrad Meyer {
14572a4fd6b1SConrad Meyer 	struct ioat_fill_hw_descriptor *hw_desc;
14582a4fd6b1SConrad Meyer 	struct ioat_descriptor *desc;
14592a4fd6b1SConrad Meyer 	struct ioat_softc *ioat;
14602a4fd6b1SConrad Meyer 
14612a4fd6b1SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1462520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
14632a4fd6b1SConrad Meyer 
1464630d9800SAlexander Motin 	KASSERT((ioat->capabilities & IOAT_DMACAP_BFILL) != 0,
1465630d9800SAlexander Motin 	    ("%s: device lacks BFILL capability", __func__));
14662a4fd6b1SConrad Meyer 
1467b80b32a2STycho Nightingale 	desc = ioat_op_generic(ioat, IOAT_OP_FILL, len, 0, dst,
14682a4fd6b1SConrad Meyer 	    callback_fn, callback_arg, flags);
14692a4fd6b1SConrad Meyer 	if (desc == NULL)
14702a4fd6b1SConrad Meyer 		return (NULL);
14712a4fd6b1SConrad Meyer 
14728e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->fill;
1473b80b32a2STycho Nightingale 	hw_desc->src_data = fillpattern;
14742a4fd6b1SConrad Meyer 	if (g_ioat_debug_level >= 3)
14752a4fd6b1SConrad Meyer 		dump_descriptor(hw_desc);
14762a4fd6b1SConrad Meyer 
14772a4fd6b1SConrad Meyer 	ioat_submit_single(ioat);
14782a4fd6b1SConrad Meyer 	return (&desc->bus_dmadesc);
14792a4fd6b1SConrad Meyer }
14802a4fd6b1SConrad Meyer 
1481e974f91cSConrad Meyer /*
1482e974f91cSConrad Meyer  * Ring Management
1483e974f91cSConrad Meyer  */
1484e974f91cSConrad Meyer static inline uint32_t
1485e974f91cSConrad Meyer ioat_get_active(struct ioat_softc *ioat)
1486e974f91cSConrad Meyer {
1487e974f91cSConrad Meyer 
1488e974f91cSConrad Meyer 	return ((ioat->head - ioat->tail) & ((1 << ioat->ring_size_order) - 1));
1489e974f91cSConrad Meyer }
1490e974f91cSConrad Meyer 
1491e974f91cSConrad Meyer static inline uint32_t
1492e974f91cSConrad Meyer ioat_get_ring_space(struct ioat_softc *ioat)
1493e974f91cSConrad Meyer {
1494e974f91cSConrad Meyer 
1495e974f91cSConrad Meyer 	return ((1 << ioat->ring_size_order) - ioat_get_active(ioat) - 1);
1496e974f91cSConrad Meyer }
1497e974f91cSConrad Meyer 
1498bf8553eaSConrad Meyer /*
1499bf8553eaSConrad Meyer  * Reserves space in this IOAT descriptor ring by ensuring enough slots remain
1500bf8553eaSConrad Meyer  * for 'num_descs'.
1501bf8553eaSConrad Meyer  *
1502bf8553eaSConrad Meyer  * If mflags contains M_WAITOK, blocks until enough space is available.
1503bf8553eaSConrad Meyer  *
1504bf8553eaSConrad Meyer  * Returns zero on success, or an errno on error.  If num_descs is beyond the
1505bf8553eaSConrad Meyer  * maximum ring size, returns EINVAl; if allocation would block and mflags
1506bf8553eaSConrad Meyer  * contains M_NOWAIT, returns EAGAIN.
1507bf8553eaSConrad Meyer  *
1508bf8553eaSConrad Meyer  * Must be called with the submit_lock held; returns with the lock held.  The
1509bf8553eaSConrad Meyer  * lock may be dropped to allocate the ring.
1510bf8553eaSConrad Meyer  *
1511bf8553eaSConrad Meyer  * (The submit_lock is needed to add any entries to the ring, so callers are
1512bf8553eaSConrad Meyer  * assured enough room is available.)
1513bf8553eaSConrad Meyer  */
1514e974f91cSConrad Meyer static int
1515bf8553eaSConrad Meyer ioat_reserve_space(struct ioat_softc *ioat, uint32_t num_descs, int mflags)
1516e974f91cSConrad Meyer {
151725ad9585SConrad Meyer 	boolean_t dug;
1518bf8553eaSConrad Meyer 	int error;
1519e974f91cSConrad Meyer 
1520bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1521bf8553eaSConrad Meyer 	error = 0;
152225ad9585SConrad Meyer 	dug = FALSE;
1523e974f91cSConrad Meyer 
1524a0992979SConrad Meyer 	if (num_descs < 1 || num_descs >= (1 << ioat->ring_size_order)) {
1525bf8553eaSConrad Meyer 		error = EINVAL;
1526bf8553eaSConrad Meyer 		goto out;
1527e974f91cSConrad Meyer 	}
152808a6b96aSConrad Meyer 
152908a6b96aSConrad Meyer 	for (;;) {
15305f77bd3eSConrad Meyer 		if (ioat->quiescing) {
15315f77bd3eSConrad Meyer 			error = ENXIO;
15325f77bd3eSConrad Meyer 			goto out;
15335f77bd3eSConrad Meyer 		}
1534bf8553eaSConrad Meyer 
1535bf8553eaSConrad Meyer 		if (ioat_get_ring_space(ioat) >= num_descs)
1536bf8553eaSConrad Meyer 			goto out;
1537bf8553eaSConrad Meyer 
153805e4cffbSConrad Meyer 		CTR3(KTR_IOAT, "%s channel=%u starved (%u)", __func__,
153905e4cffbSConrad Meyer 		    ioat->chan_idx, num_descs);
154005e4cffbSConrad Meyer 
1541a0992979SConrad Meyer 		if (!dug && !ioat->is_submitter_processing) {
154225ad9585SConrad Meyer 			ioat->is_submitter_processing = TRUE;
154325ad9585SConrad Meyer 			mtx_unlock(&ioat->submit_lock);
154425ad9585SConrad Meyer 
154505e4cffbSConrad Meyer 			CTR2(KTR_IOAT, "%s channel=%u attempting to process events",
154605e4cffbSConrad Meyer 			    __func__, ioat->chan_idx);
15472f03a95fSAlexander Motin 			ioat_process_events(ioat, FALSE);
154825ad9585SConrad Meyer 
154925ad9585SConrad Meyer 			mtx_lock(&ioat->submit_lock);
155025ad9585SConrad Meyer 			dug = TRUE;
155125ad9585SConrad Meyer 			KASSERT(ioat->is_submitter_processing == TRUE,
155225ad9585SConrad Meyer 			    ("is_submitter_processing"));
155325ad9585SConrad Meyer 			ioat->is_submitter_processing = FALSE;
155425ad9585SConrad Meyer 			wakeup(&ioat->tail);
155525ad9585SConrad Meyer 			continue;
155625ad9585SConrad Meyer 		}
155725ad9585SConrad Meyer 
1558a0992979SConrad Meyer 		if ((mflags & M_WAITOK) == 0) {
1559a0992979SConrad Meyer 			error = EAGAIN;
1560a0992979SConrad Meyer 			break;
1561a0992979SConrad Meyer 		}
156205e4cffbSConrad Meyer 		CTR2(KTR_IOAT, "%s channel=%u blocking on completions",
156305e4cffbSConrad Meyer 		    __func__, ioat->chan_idx);
1564bf8553eaSConrad Meyer 		msleep(&ioat->tail, &ioat->submit_lock, 0,
1565a0992979SConrad Meyer 		    "ioat_full", 0);
1566bf8553eaSConrad Meyer 		continue;
1567bf8553eaSConrad Meyer 	}
1568bf8553eaSConrad Meyer 
1569bf8553eaSConrad Meyer out:
1570bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
157108a6b96aSConrad Meyer 	KASSERT(!ioat->quiescing || error == ENXIO,
157208a6b96aSConrad Meyer 	    ("reserved during quiesce"));
1573bf8553eaSConrad Meyer 	return (error);
1574bf8553eaSConrad Meyer }
1575bf8553eaSConrad Meyer 
1576bf8553eaSConrad Meyer static void
1577bf8553eaSConrad Meyer ioat_free_ring(struct ioat_softc *ioat, uint32_t size,
15788e269d99SConrad Meyer     struct ioat_descriptor *ring)
1579bf8553eaSConrad Meyer {
1580bf8553eaSConrad Meyer 
1581657dc81dSAlexander Motin 	free_domain(ring, M_IOAT);
1582e974f91cSConrad Meyer }
1583e974f91cSConrad Meyer 
1584e974f91cSConrad Meyer static struct ioat_descriptor *
1585e974f91cSConrad Meyer ioat_get_ring_entry(struct ioat_softc *ioat, uint32_t index)
1586e974f91cSConrad Meyer {
1587e974f91cSConrad Meyer 
15888e269d99SConrad Meyer 	return (&ioat->ring[index % (1 << ioat->ring_size_order)]);
15898e269d99SConrad Meyer }
15908e269d99SConrad Meyer 
15918e269d99SConrad Meyer static union ioat_hw_descriptor *
15928e269d99SConrad Meyer ioat_get_descriptor(struct ioat_softc *ioat, uint32_t index)
15938e269d99SConrad Meyer {
15948e269d99SConrad Meyer 
15958e269d99SConrad Meyer 	return (&ioat->hw_desc_ring[index % (1 << ioat->ring_size_order)]);
1596e974f91cSConrad Meyer }
1597e974f91cSConrad Meyer 
1598e974f91cSConrad Meyer static void
15998f274637SConrad Meyer ioat_halted_debug(struct ioat_softc *ioat, uint32_t chanerr)
1600e974f91cSConrad Meyer {
16018e269d99SConrad Meyer 	union ioat_hw_descriptor *desc;
16028f274637SConrad Meyer 
160359acd4baSConrad Meyer 	ioat_log_message(0, "Channel halted (%b)\n", (int)chanerr,
160459acd4baSConrad Meyer 	    IOAT_CHANERR_STR);
16058f274637SConrad Meyer 	if (chanerr == 0)
16068f274637SConrad Meyer 		return;
16078f274637SConrad Meyer 
1608faefad9cSConrad Meyer 	mtx_assert(&ioat->cleanup_lock, MA_OWNED);
1609faefad9cSConrad Meyer 
16108e269d99SConrad Meyer 	desc = ioat_get_descriptor(ioat, ioat->tail + 0);
16118e269d99SConrad Meyer 	dump_descriptor(desc);
16128f274637SConrad Meyer 
16138e269d99SConrad Meyer 	desc = ioat_get_descriptor(ioat, ioat->tail + 1);
16148e269d99SConrad Meyer 	dump_descriptor(desc);
16158f274637SConrad Meyer }
16168f274637SConrad Meyer 
16178f274637SConrad Meyer static void
16185ac77963SConrad Meyer ioat_poll_timer_callback(void *arg)
16195ac77963SConrad Meyer {
16205ac77963SConrad Meyer 	struct ioat_softc *ioat;
16215ac77963SConrad Meyer 
16225ac77963SConrad Meyer 	ioat = arg;
1623630d9800SAlexander Motin 	CTR1(KTR_IOAT, "%s", __func__);
16245ac77963SConrad Meyer 
16252f03a95fSAlexander Motin 	ioat_process_events(ioat, FALSE);
16262f03a95fSAlexander Motin 
16272f03a95fSAlexander Motin 	mtx_lock(&ioat->submit_lock);
16282f03a95fSAlexander Motin 	if (ioat_get_active(ioat) > 0)
16292f03a95fSAlexander Motin 		callout_schedule(&ioat->poll_timer, 1);
16302f03a95fSAlexander Motin 	mtx_unlock(&ioat->submit_lock);
16315ac77963SConrad Meyer }
16325ac77963SConrad Meyer 
1633e974f91cSConrad Meyer /*
1634e974f91cSConrad Meyer  * Support Functions
1635e974f91cSConrad Meyer  */
1636e974f91cSConrad Meyer static void
1637e974f91cSConrad Meyer ioat_submit_single(struct ioat_softc *ioat)
1638e974f91cSConrad Meyer {
1639e974f91cSConrad Meyer 
16405a8af401SConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
16415a8af401SConrad Meyer 
16422f03a95fSAlexander Motin 	ioat->head++;
16432f03a95fSAlexander Motin 	CTR4(KTR_IOAT, "%s channel=%u head=%u tail=%u", __func__,
16442f03a95fSAlexander Motin 	    ioat->chan_idx, ioat->head, ioat->tail);
1645e974f91cSConrad Meyer 
164601fbbc88SConrad Meyer 	ioat->stats.descriptors_submitted++;
1647e974f91cSConrad Meyer }
1648e974f91cSConrad Meyer 
1649e974f91cSConrad Meyer static int
1650e974f91cSConrad Meyer ioat_reset_hw(struct ioat_softc *ioat)
1651e974f91cSConrad Meyer {
1652e974f91cSConrad Meyer 	uint64_t status;
1653e974f91cSConrad Meyer 	uint32_t chanerr;
1654cea5b880SConrad Meyer 	unsigned timeout;
16555f77bd3eSConrad Meyer 	int error;
16565f77bd3eSConrad Meyer 
1657520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
16586d41e1edSConrad Meyer 
16592f03a95fSAlexander Motin 	mtx_lock(&ioat->submit_lock);
166093f7f84aSConrad Meyer 	while (ioat->resetting && !ioat->destroying)
16612f03a95fSAlexander Motin 		msleep(&ioat->resetting, &ioat->submit_lock, 0, "IRH_drain", 0);
166293f7f84aSConrad Meyer 	if (ioat->destroying) {
16632f03a95fSAlexander Motin 		mtx_unlock(&ioat->submit_lock);
166493f7f84aSConrad Meyer 		return (ENXIO);
166593f7f84aSConrad Meyer 	}
166693f7f84aSConrad Meyer 	ioat->resetting = TRUE;
16675f77bd3eSConrad Meyer 	ioat->quiescing = TRUE;
16682f03a95fSAlexander Motin 	mtx_unlock(&ioat->submit_lock);
16692f03a95fSAlexander Motin 	mtx_lock(&ioat->cleanup_lock);
16702f03a95fSAlexander Motin 	while (ioat_get_active(ioat) > 0)
16712f03a95fSAlexander Motin 		msleep(&ioat->tail, &ioat->cleanup_lock, 0, "ioat_drain", 1);
1672e974f91cSConrad Meyer 
1673fe8712f8SConrad Meyer 	/*
1674fe8712f8SConrad Meyer 	 * Suspend ioat_process_events while the hardware and softc are in an
1675fe8712f8SConrad Meyer 	 * indeterminate state.
1676fe8712f8SConrad Meyer 	 */
1677fe8712f8SConrad Meyer 	ioat->resetting_cleanup = TRUE;
1678fe8712f8SConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
1679fe8712f8SConrad Meyer 
1680ee5642bbSConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u quiesced and drained", __func__,
1681ee5642bbSConrad Meyer 	    ioat->chan_idx);
1682ee5642bbSConrad Meyer 
1683e974f91cSConrad Meyer 	status = ioat_get_chansts(ioat);
1684e974f91cSConrad Meyer 	if (is_ioat_active(status) || is_ioat_idle(status))
1685e974f91cSConrad Meyer 		ioat_suspend(ioat);
1686e974f91cSConrad Meyer 
1687e974f91cSConrad Meyer 	/* Wait at most 20 ms */
1688e974f91cSConrad Meyer 	for (timeout = 0; (is_ioat_active(status) || is_ioat_idle(status)) &&
1689e974f91cSConrad Meyer 	    timeout < 20; timeout++) {
1690e974f91cSConrad Meyer 		DELAY(1000);
1691e974f91cSConrad Meyer 		status = ioat_get_chansts(ioat);
1692e974f91cSConrad Meyer 	}
16935f77bd3eSConrad Meyer 	if (timeout == 20) {
16945f77bd3eSConrad Meyer 		error = ETIMEDOUT;
16955f77bd3eSConrad Meyer 		goto out;
16965f77bd3eSConrad Meyer 	}
1697e974f91cSConrad Meyer 
1698cea5b880SConrad Meyer 	KASSERT(ioat_get_active(ioat) == 0, ("active after quiesce"));
1699cea5b880SConrad Meyer 
1700e974f91cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
1701e974f91cSConrad Meyer 	ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
1702e974f91cSConrad Meyer 
1703ee5642bbSConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u hardware suspended", __func__,
1704ee5642bbSConrad Meyer 	    ioat->chan_idx);
1705ee5642bbSConrad Meyer 
1706e974f91cSConrad Meyer 	/*
1707e974f91cSConrad Meyer 	 * IOAT v3 workaround - CHANERRMSK_INT with 3E07h to masks out errors
1708e974f91cSConrad Meyer 	 *  that can cause stability issues for IOAT v3.
1709e974f91cSConrad Meyer 	 */
1710e974f91cSConrad Meyer 	pci_write_config(ioat->device, IOAT_CFG_CHANERRMASK_INT_OFFSET, 0x3e07,
1711e974f91cSConrad Meyer 	    4);
1712e974f91cSConrad Meyer 	chanerr = pci_read_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, 4);
1713e974f91cSConrad Meyer 	pci_write_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, chanerr, 4);
1714e974f91cSConrad Meyer 
17150d1a05d9SConrad Meyer 	/*
17160d1a05d9SConrad Meyer 	 * BDXDE and BWD models reset MSI-X registers on device reset.
17170d1a05d9SConrad Meyer 	 * Save/restore their contents manually.
17180d1a05d9SConrad Meyer 	 */
1719f7157235SConrad Meyer 	if (ioat_model_resets_msix(ioat)) {
1720f7157235SConrad Meyer 		ioat_log_message(1, "device resets MSI-X registers; saving\n");
17210d1a05d9SConrad Meyer 		pci_save_state(ioat->device);
1722f7157235SConrad Meyer 	}
17230d1a05d9SConrad Meyer 
1724e974f91cSConrad Meyer 	ioat_reset(ioat);
1725ee5642bbSConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u hardware reset", __func__,
1726ee5642bbSConrad Meyer 	    ioat->chan_idx);
1727e974f91cSConrad Meyer 
1728e974f91cSConrad Meyer 	/* Wait at most 20 ms */
1729e974f91cSConrad Meyer 	for (timeout = 0; ioat_reset_pending(ioat) && timeout < 20; timeout++)
1730e974f91cSConrad Meyer 		DELAY(1000);
17315f77bd3eSConrad Meyer 	if (timeout == 20) {
17325f77bd3eSConrad Meyer 		error = ETIMEDOUT;
17335f77bd3eSConrad Meyer 		goto out;
17345f77bd3eSConrad Meyer 	}
1735e974f91cSConrad Meyer 
1736f7157235SConrad Meyer 	if (ioat_model_resets_msix(ioat)) {
1737f7157235SConrad Meyer 		ioat_log_message(1, "device resets registers; restored\n");
17380d1a05d9SConrad Meyer 		pci_restore_state(ioat->device);
1739f7157235SConrad Meyer 	}
17404253ea50SConrad Meyer 
1741cea5b880SConrad Meyer 	/* Reset attempts to return the hardware to "halted." */
1742cea5b880SConrad Meyer 	status = ioat_get_chansts(ioat);
1743cea5b880SConrad Meyer 	if (is_ioat_active(status) || is_ioat_idle(status)) {
1744cea5b880SConrad Meyer 		/* So this really shouldn't happen... */
1745cea5b880SConrad Meyer 		ioat_log_message(0, "Device is active after a reset?\n");
1746cea5b880SConrad Meyer 		ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
17475f77bd3eSConrad Meyer 		error = 0;
17485f77bd3eSConrad Meyer 		goto out;
1749e974f91cSConrad Meyer 	}
1750e974f91cSConrad Meyer 
1751cea5b880SConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
17525f77bd3eSConrad Meyer 	if (chanerr != 0) {
1753faefad9cSConrad Meyer 		mtx_lock(&ioat->cleanup_lock);
1754faefad9cSConrad Meyer 		ioat_halted_debug(ioat, chanerr);
1755faefad9cSConrad Meyer 		mtx_unlock(&ioat->cleanup_lock);
17565f77bd3eSConrad Meyer 		error = EIO;
17575f77bd3eSConrad Meyer 		goto out;
17585f77bd3eSConrad Meyer 	}
1759cea5b880SConrad Meyer 
1760cea5b880SConrad Meyer 	/*
1761cea5b880SConrad Meyer 	 * Bring device back online after reset.  Writing CHAINADDR brings the
1762cea5b880SConrad Meyer 	 * device back to active.
1763cea5b880SConrad Meyer 	 *
1764cea5b880SConrad Meyer 	 * The internal ring counter resets to zero, so we have to start over
1765cea5b880SConrad Meyer 	 * at zero as well.
1766cea5b880SConrad Meyer 	 */
17672f03a95fSAlexander Motin 	ioat->tail = ioat->head = 0;
1768348efb14SAlexander Motin 	*ioat->comp_update = ioat->last_seen =
1769348efb14SAlexander Motin 	    RING_PHYS_ADDR(ioat, ioat->tail - 1);
1770cea5b880SConrad Meyer 
1771cea5b880SConrad Meyer 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
1772cea5b880SConrad Meyer 	ioat_write_chancmp(ioat, ioat->comp_update_bus_addr);
17738e269d99SConrad Meyer 	ioat_write_chainaddr(ioat, RING_PHYS_ADDR(ioat, 0));
17745f77bd3eSConrad Meyer 	error = 0;
1775ee5642bbSConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u configured channel", __func__,
1776ee5642bbSConrad Meyer 	    ioat->chan_idx);
17775f77bd3eSConrad Meyer 
17785f77bd3eSConrad Meyer out:
17791bbc06b8SConrad Meyer 	/* Enqueues a null operation and ensures it completes. */
1780ee5642bbSConrad Meyer 	if (error == 0) {
17811bbc06b8SConrad Meyer 		error = ioat_start_channel(ioat);
1782ee5642bbSConrad Meyer 		CTR2(KTR_IOAT, "%s channel=%u started channel", __func__,
1783ee5642bbSConrad Meyer 		    ioat->chan_idx);
1784ee5642bbSConrad Meyer 	}
17851bbc06b8SConrad Meyer 
1786fe8712f8SConrad Meyer 	/*
1787fe8712f8SConrad Meyer 	 * Resume completions now that ring state is consistent.
1788fe8712f8SConrad Meyer 	 */
1789fe8712f8SConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
1790fe8712f8SConrad Meyer 	ioat->resetting_cleanup = FALSE;
1791fe8712f8SConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
179293f7f84aSConrad Meyer 
1793fe8712f8SConrad Meyer 	/* Unblock submission of new work */
17942f03a95fSAlexander Motin 	mtx_lock(&ioat->submit_lock);
1795fe8712f8SConrad Meyer 	ioat->quiescing = FALSE;
1796fe8712f8SConrad Meyer 	wakeup(&ioat->quiescing);
1797fe8712f8SConrad Meyer 
1798fe8712f8SConrad Meyer 	ioat->resetting = FALSE;
1799fe8712f8SConrad Meyer 	wakeup(&ioat->resetting);
18001bbc06b8SConrad Meyer 
1801ee5642bbSConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u reset done", __func__, ioat->chan_idx);
18022f03a95fSAlexander Motin 	mtx_unlock(&ioat->submit_lock);
1803fe8712f8SConrad Meyer 
18045f77bd3eSConrad Meyer 	return (error);
1805cea5b880SConrad Meyer }
1806cea5b880SConrad Meyer 
1807f7157235SConrad Meyer static int
1808faefad9cSConrad Meyer sysctl_handle_chansts(SYSCTL_HANDLER_ARGS)
1809faefad9cSConrad Meyer {
1810faefad9cSConrad Meyer 	struct ioat_softc *ioat;
1811faefad9cSConrad Meyer 	struct sbuf sb;
1812faefad9cSConrad Meyer 	uint64_t status;
1813faefad9cSConrad Meyer 	int error;
1814faefad9cSConrad Meyer 
1815faefad9cSConrad Meyer 	ioat = arg1;
1816faefad9cSConrad Meyer 
1817faefad9cSConrad Meyer 	status = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS;
1818faefad9cSConrad Meyer 
1819faefad9cSConrad Meyer 	sbuf_new_for_sysctl(&sb, NULL, 256, req);
1820faefad9cSConrad Meyer 	switch (status) {
1821faefad9cSConrad Meyer 	case IOAT_CHANSTS_ACTIVE:
1822faefad9cSConrad Meyer 		sbuf_printf(&sb, "ACTIVE");
1823faefad9cSConrad Meyer 		break;
1824faefad9cSConrad Meyer 	case IOAT_CHANSTS_IDLE:
1825faefad9cSConrad Meyer 		sbuf_printf(&sb, "IDLE");
1826faefad9cSConrad Meyer 		break;
1827faefad9cSConrad Meyer 	case IOAT_CHANSTS_SUSPENDED:
1828faefad9cSConrad Meyer 		sbuf_printf(&sb, "SUSPENDED");
1829faefad9cSConrad Meyer 		break;
1830faefad9cSConrad Meyer 	case IOAT_CHANSTS_HALTED:
1831faefad9cSConrad Meyer 		sbuf_printf(&sb, "HALTED");
1832faefad9cSConrad Meyer 		break;
1833faefad9cSConrad Meyer 	case IOAT_CHANSTS_ARMED:
1834faefad9cSConrad Meyer 		sbuf_printf(&sb, "ARMED");
1835faefad9cSConrad Meyer 		break;
1836faefad9cSConrad Meyer 	default:
1837faefad9cSConrad Meyer 		sbuf_printf(&sb, "UNKNOWN");
1838faefad9cSConrad Meyer 		break;
1839faefad9cSConrad Meyer 	}
1840faefad9cSConrad Meyer 	error = sbuf_finish(&sb);
1841faefad9cSConrad Meyer 	sbuf_delete(&sb);
1842faefad9cSConrad Meyer 
1843faefad9cSConrad Meyer 	if (error != 0 || req->newptr == NULL)
1844faefad9cSConrad Meyer 		return (error);
1845faefad9cSConrad Meyer 	return (EINVAL);
1846faefad9cSConrad Meyer }
1847faefad9cSConrad Meyer 
1848faefad9cSConrad Meyer static int
184901fbbc88SConrad Meyer sysctl_handle_dpi(SYSCTL_HANDLER_ARGS)
185001fbbc88SConrad Meyer {
185101fbbc88SConrad Meyer 	struct ioat_softc *ioat;
185201fbbc88SConrad Meyer 	struct sbuf sb;
185301fbbc88SConrad Meyer #define	PRECISION	"1"
185401fbbc88SConrad Meyer 	const uintmax_t factor = 10;
185501fbbc88SConrad Meyer 	uintmax_t rate;
185601fbbc88SConrad Meyer 	int error;
185701fbbc88SConrad Meyer 
185801fbbc88SConrad Meyer 	ioat = arg1;
185901fbbc88SConrad Meyer 	sbuf_new_for_sysctl(&sb, NULL, 16, req);
186001fbbc88SConrad Meyer 
186101fbbc88SConrad Meyer 	if (ioat->stats.interrupts == 0) {
186201fbbc88SConrad Meyer 		sbuf_printf(&sb, "NaN");
186301fbbc88SConrad Meyer 		goto out;
186401fbbc88SConrad Meyer 	}
186501fbbc88SConrad Meyer 	rate = ioat->stats.descriptors_processed * factor /
186601fbbc88SConrad Meyer 	    ioat->stats.interrupts;
186701fbbc88SConrad Meyer 	sbuf_printf(&sb, "%ju.%." PRECISION "ju", rate / factor,
186801fbbc88SConrad Meyer 	    rate % factor);
186901fbbc88SConrad Meyer #undef	PRECISION
187001fbbc88SConrad Meyer out:
187101fbbc88SConrad Meyer 	error = sbuf_finish(&sb);
187201fbbc88SConrad Meyer 	sbuf_delete(&sb);
187301fbbc88SConrad Meyer 	if (error != 0 || req->newptr == NULL)
187401fbbc88SConrad Meyer 		return (error);
187501fbbc88SConrad Meyer 	return (EINVAL);
187601fbbc88SConrad Meyer }
187701fbbc88SConrad Meyer 
187801fbbc88SConrad Meyer static int
1879f7157235SConrad Meyer sysctl_handle_reset(SYSCTL_HANDLER_ARGS)
1880f7157235SConrad Meyer {
1881f7157235SConrad Meyer 	struct ioat_softc *ioat;
1882f7157235SConrad Meyer 	int error, arg;
1883f7157235SConrad Meyer 
1884f7157235SConrad Meyer 	ioat = arg1;
1885f7157235SConrad Meyer 
1886f7157235SConrad Meyer 	arg = 0;
1887f7157235SConrad Meyer 	error = SYSCTL_OUT(req, &arg, sizeof(arg));
1888f7157235SConrad Meyer 	if (error != 0 || req->newptr == NULL)
1889f7157235SConrad Meyer 		return (error);
1890f7157235SConrad Meyer 
1891f7157235SConrad Meyer 	error = SYSCTL_IN(req, &arg, sizeof(arg));
1892f7157235SConrad Meyer 	if (error != 0)
1893f7157235SConrad Meyer 		return (error);
1894f7157235SConrad Meyer 
1895f7157235SConrad Meyer 	if (arg != 0)
1896f7157235SConrad Meyer 		error = ioat_reset_hw(ioat);
1897f7157235SConrad Meyer 
1898f7157235SConrad Meyer 	return (error);
1899f7157235SConrad Meyer }
1900f7157235SConrad Meyer 
1901e974f91cSConrad Meyer static void
1902e974f91cSConrad Meyer dump_descriptor(void *hw_desc)
1903e974f91cSConrad Meyer {
1904e974f91cSConrad Meyer 	int i, j;
1905e974f91cSConrad Meyer 
1906e974f91cSConrad Meyer 	for (i = 0; i < 2; i++) {
1907e974f91cSConrad Meyer 		for (j = 0; j < 8; j++)
1908e974f91cSConrad Meyer 			printf("%08x ", ((uint32_t *)hw_desc)[i * 8 + j]);
1909e974f91cSConrad Meyer 		printf("\n");
1910e974f91cSConrad Meyer 	}
1911e974f91cSConrad Meyer }
1912e974f91cSConrad Meyer 
1913e974f91cSConrad Meyer static void
1914e974f91cSConrad Meyer ioat_setup_sysctl(device_t device)
1915e974f91cSConrad Meyer {
191601fbbc88SConrad Meyer 	struct sysctl_oid_list *par, *statpar, *state, *hammer;
1917f7157235SConrad Meyer 	struct sysctl_ctx_list *ctx;
191801fbbc88SConrad Meyer 	struct sysctl_oid *tree, *tmp;
1919e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1920e974f91cSConrad Meyer 
1921e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
1922f7157235SConrad Meyer 	ctx = device_get_sysctl_ctx(device);
1923f7157235SConrad Meyer 	tree = device_get_sysctl_tree(device);
1924f7157235SConrad Meyer 	par = SYSCTL_CHILDREN(tree);
1925e974f91cSConrad Meyer 
192665e4f8adSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "version", CTLFLAG_RD,
192765e4f8adSConrad Meyer 	    &ioat->version, 0, "HW version (0xMM form)");
192865e4f8adSConrad Meyer 	SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "max_xfer_size", CTLFLAG_RD,
192965e4f8adSConrad Meyer 	    &ioat->max_xfer_size, 0, "HW maximum transfer size");
19305ca9fc2aSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "intrdelay_supported", CTLFLAG_RD,
19315ca9fc2aSConrad Meyer 	    &ioat->intrdelay_supported, 0, "Is INTRDELAY supported");
19325ca9fc2aSConrad Meyer 	SYSCTL_ADD_U16(ctx, par, OID_AUTO, "intrdelay_max", CTLFLAG_RD,
19335ca9fc2aSConrad Meyer 	    &ioat->intrdelay_max, 0,
19345ca9fc2aSConrad Meyer 	    "Maximum configurable INTRDELAY on this channel (microseconds)");
193565e4f8adSConrad Meyer 
1936*7029da5cSPawel Biernacki 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "state",
1937*7029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "IOAT channel internal state");
193801fbbc88SConrad Meyer 	state = SYSCTL_CHILDREN(tmp);
193901fbbc88SConrad Meyer 
194001fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "ring_size_order", CTLFLAG_RD,
1941bf8553eaSConrad Meyer 	    &ioat->ring_size_order, 0, "SW descriptor ring size order");
194201fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "head", CTLFLAG_RD, &ioat->head,
194301fbbc88SConrad Meyer 	    0, "SW descriptor head pointer index");
194401fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "tail", CTLFLAG_RD, &ioat->tail,
194501fbbc88SConrad Meyer 	    0, "SW descriptor tail pointer index");
1946f7157235SConrad Meyer 
194701fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, state, OID_AUTO, "last_completion", CTLFLAG_RD,
194865e4f8adSConrad Meyer 	    ioat->comp_update, "HW addr of last completion");
194965e4f8adSConrad Meyer 
195025ad9585SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_submitter_processing",
195125ad9585SConrad Meyer 	    CTLFLAG_RD, &ioat->is_submitter_processing, 0,
195225ad9585SConrad Meyer 	    "submitter processing");
195365e4f8adSConrad Meyer 
195401fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, state, OID_AUTO, "chansts",
1955*7029da5cSPawel Biernacki 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, ioat, 0,
1956*7029da5cSPawel Biernacki 	    sysctl_handle_chansts, "A", "String of the channel status");
195701fbbc88SConrad Meyer 
19585ca9fc2aSConrad Meyer 	SYSCTL_ADD_U16(ctx, state, OID_AUTO, "intrdelay", CTLFLAG_RD,
19595ca9fc2aSConrad Meyer 	    &ioat->cached_intrdelay, 0,
19605ca9fc2aSConrad Meyer 	    "Current INTRDELAY on this channel (cached, microseconds)");
19615ca9fc2aSConrad Meyer 
1962*7029da5cSPawel Biernacki 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "hammer",
1963*7029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
196401fbbc88SConrad Meyer 	    "Big hammers (mostly for testing)");
196501fbbc88SConrad Meyer 	hammer = SYSCTL_CHILDREN(tmp);
196601fbbc88SConrad Meyer 
196701fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, hammer, OID_AUTO, "force_hw_reset",
1968*7029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, ioat, 0,
1969*7029da5cSPawel Biernacki 	    sysctl_handle_reset, "I", "Set to non-zero to reset the hardware");
197001fbbc88SConrad Meyer 
1971*7029da5cSPawel Biernacki 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "stats",
1972*7029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "IOAT channel statistics");
197301fbbc88SConrad Meyer 	statpar = SYSCTL_CHILDREN(tmp);
197401fbbc88SConrad Meyer 
197583b82cd4SEric van Gyzen 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "interrupts",
197683b82cd4SEric van Gyzen 	    CTLFLAG_RW | CTLFLAG_STATS, &ioat->stats.interrupts,
197701fbbc88SConrad Meyer 	    "Number of interrupts processed on this channel");
197883b82cd4SEric van Gyzen 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "descriptors",
197983b82cd4SEric van Gyzen 	    CTLFLAG_RW | CTLFLAG_STATS, &ioat->stats.descriptors_processed,
198001fbbc88SConrad Meyer 	    "Number of descriptors processed on this channel");
198183b82cd4SEric van Gyzen 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "submitted",
198283b82cd4SEric van Gyzen 	    CTLFLAG_RW | CTLFLAG_STATS, &ioat->stats.descriptors_submitted,
198301fbbc88SConrad Meyer 	    "Number of descriptors submitted to this channel");
198483b82cd4SEric van Gyzen 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "errored",
198583b82cd4SEric van Gyzen 	    CTLFLAG_RW | CTLFLAG_STATS, &ioat->stats.descriptors_error,
198601fbbc88SConrad Meyer 	    "Number of descriptors failed by channel errors");
198783b82cd4SEric van Gyzen 	SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "halts",
198883b82cd4SEric van Gyzen 	    CTLFLAG_RW | CTLFLAG_STATS, &ioat->stats.channel_halts, 0,
198901fbbc88SConrad Meyer 	    "Number of times the channel has halted");
199083b82cd4SEric van Gyzen 	SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "last_halt_chanerr",
199183b82cd4SEric van Gyzen 	    CTLFLAG_RW | CTLFLAG_STATS, &ioat->stats.last_halt_chanerr, 0,
199201fbbc88SConrad Meyer 	    "The raw CHANERR when the channel was last halted");
199301fbbc88SConrad Meyer 
199401fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, statpar, OID_AUTO, "desc_per_interrupt",
1995*7029da5cSPawel Biernacki 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, ioat, 0,
1996*7029da5cSPawel Biernacki 	    sysctl_handle_dpi, "A", "Descriptors per interrupt");
1997e974f91cSConrad Meyer }
1998466b3540SConrad Meyer 
19992f03a95fSAlexander Motin static void
20002f03a95fSAlexander Motin ioat_get(struct ioat_softc *ioat)
2001466b3540SConrad Meyer {
2002faefad9cSConrad Meyer 
20032f03a95fSAlexander Motin 	mtx_assert(&ioat->submit_lock, MA_OWNED);
20042f03a95fSAlexander Motin 	KASSERT(ioat->refcnt < UINT32_MAX, ("refcnt overflow"));
20052f03a95fSAlexander Motin 
20062f03a95fSAlexander Motin 	ioat->refcnt++;
2007faefad9cSConrad Meyer }
2008faefad9cSConrad Meyer 
20092f03a95fSAlexander Motin static void
20102f03a95fSAlexander Motin ioat_put(struct ioat_softc *ioat)
2011faefad9cSConrad Meyer {
2012faefad9cSConrad Meyer 
20132f03a95fSAlexander Motin 	mtx_assert(&ioat->submit_lock, MA_OWNED);
20142f03a95fSAlexander Motin 	KASSERT(ioat->refcnt >= 1, ("refcnt error"));
2015faefad9cSConrad Meyer 
20162f03a95fSAlexander Motin 	if (--ioat->refcnt == 0)
20172f03a95fSAlexander Motin 		wakeup(&ioat->refcnt);
2018466b3540SConrad Meyer }
2019466b3540SConrad Meyer 
2020466b3540SConrad Meyer static void
20215f77bd3eSConrad Meyer ioat_drain_locked(struct ioat_softc *ioat)
2022466b3540SConrad Meyer {
2023466b3540SConrad Meyer 
20242f03a95fSAlexander Motin 	mtx_assert(&ioat->submit_lock, MA_OWNED);
20252f03a95fSAlexander Motin 
2026466b3540SConrad Meyer 	while (ioat->refcnt > 0)
20272f03a95fSAlexander Motin 		msleep(&ioat->refcnt, &ioat->submit_lock, 0, "ioat_drain", 0);
2028466b3540SConrad Meyer }
20290bb35debSConrad Meyer 
20300bb35debSConrad Meyer #ifdef DDB
20310bb35debSConrad Meyer #define	_db_show_lock(lo)	LOCK_CLASS(lo)->lc_ddb_show(lo)
20320bb35debSConrad Meyer #define	db_show_lock(lk)	_db_show_lock(&(lk)->lock_object)
20330bb35debSConrad Meyer DB_SHOW_COMMAND(ioat, db_show_ioat)
20340bb35debSConrad Meyer {
20350bb35debSConrad Meyer 	struct ioat_softc *sc;
20360bb35debSConrad Meyer 	unsigned idx;
20370bb35debSConrad Meyer 
20380bb35debSConrad Meyer 	if (!have_addr)
20390bb35debSConrad Meyer 		goto usage;
20400bb35debSConrad Meyer 	idx = (unsigned)addr;
204135b7a45eSConrad Meyer 	if (idx >= ioat_channel_index)
20420bb35debSConrad Meyer 		goto usage;
20430bb35debSConrad Meyer 
20440bb35debSConrad Meyer 	sc = ioat_channel[idx];
20450bb35debSConrad Meyer 	db_printf("ioat softc at %p\n", sc);
20460bb35debSConrad Meyer 	if (sc == NULL)
20470bb35debSConrad Meyer 		return;
20480bb35debSConrad Meyer 
20490bb35debSConrad Meyer 	db_printf(" version: %d\n", sc->version);
20500bb35debSConrad Meyer 	db_printf(" chan_idx: %u\n", sc->chan_idx);
20510bb35debSConrad Meyer 	db_printf(" submit_lock: ");
20520bb35debSConrad Meyer 	db_show_lock(&sc->submit_lock);
20530bb35debSConrad Meyer 
20540bb35debSConrad Meyer 	db_printf(" capabilities: %b\n", (int)sc->capabilities,
20550bb35debSConrad Meyer 	    IOAT_DMACAP_STR);
20560bb35debSConrad Meyer 	db_printf(" cached_intrdelay: %u\n", sc->cached_intrdelay);
20570bb35debSConrad Meyer 	db_printf(" *comp_update: 0x%jx\n", (uintmax_t)*sc->comp_update);
20580bb35debSConrad Meyer 
20595ac77963SConrad Meyer 	db_printf(" poll_timer:\n");
20605ac77963SConrad Meyer 	db_printf("  c_time: %ju\n", (uintmax_t)sc->poll_timer.c_time);
20615ac77963SConrad Meyer 	db_printf("  c_arg: %p\n", sc->poll_timer.c_arg);
20625ac77963SConrad Meyer 	db_printf("  c_func: %p\n", sc->poll_timer.c_func);
20635ac77963SConrad Meyer 	db_printf("  c_lock: %p\n", sc->poll_timer.c_lock);
20645ac77963SConrad Meyer 	db_printf("  c_flags: 0x%x\n", (unsigned)sc->poll_timer.c_flags);
20655ac77963SConrad Meyer 
20660bb35debSConrad Meyer 	db_printf(" quiescing: %d\n", (int)sc->quiescing);
20670bb35debSConrad Meyer 	db_printf(" destroying: %d\n", (int)sc->destroying);
206825ad9585SConrad Meyer 	db_printf(" is_submitter_processing: %d\n",
206925ad9585SConrad Meyer 	    (int)sc->is_submitter_processing);
20700bb35debSConrad Meyer 	db_printf(" intrdelay_supported: %d\n", (int)sc->intrdelay_supported);
207193f7f84aSConrad Meyer 	db_printf(" resetting: %d\n", (int)sc->resetting);
20720bb35debSConrad Meyer 
20730bb35debSConrad Meyer 	db_printf(" head: %u\n", sc->head);
20740bb35debSConrad Meyer 	db_printf(" tail: %u\n", sc->tail);
20750bb35debSConrad Meyer 	db_printf(" ring_size_order: %u\n", sc->ring_size_order);
20760bb35debSConrad Meyer 	db_printf(" last_seen: 0x%lx\n", sc->last_seen);
20770bb35debSConrad Meyer 	db_printf(" ring: %p\n", sc->ring);
20788e269d99SConrad Meyer 	db_printf(" descriptors: %p\n", sc->hw_desc_ring);
20798e269d99SConrad Meyer 	db_printf(" descriptors (phys): 0x%jx\n",
20808e269d99SConrad Meyer 	    (uintmax_t)sc->hw_desc_bus_addr);
20810bb35debSConrad Meyer 
208235b7a45eSConrad Meyer 	db_printf("  ring[%u] (tail):\n", sc->tail %
208335b7a45eSConrad Meyer 	    (1 << sc->ring_size_order));
208435b7a45eSConrad Meyer 	db_printf("   id: %u\n", ioat_get_ring_entry(sc, sc->tail)->id);
208535b7a45eSConrad Meyer 	db_printf("   addr: 0x%lx\n",
20868e269d99SConrad Meyer 	    RING_PHYS_ADDR(sc, sc->tail));
208735b7a45eSConrad Meyer 	db_printf("   next: 0x%lx\n",
20888e269d99SConrad Meyer 	     ioat_get_descriptor(sc, sc->tail)->generic.next);
208935b7a45eSConrad Meyer 
209035b7a45eSConrad Meyer 	db_printf("  ring[%u] (head - 1):\n", (sc->head - 1) %
209135b7a45eSConrad Meyer 	    (1 << sc->ring_size_order));
209235b7a45eSConrad Meyer 	db_printf("   id: %u\n", ioat_get_ring_entry(sc, sc->head - 1)->id);
209335b7a45eSConrad Meyer 	db_printf("   addr: 0x%lx\n",
20948e269d99SConrad Meyer 	    RING_PHYS_ADDR(sc, sc->head - 1));
209535b7a45eSConrad Meyer 	db_printf("   next: 0x%lx\n",
20968e269d99SConrad Meyer 	     ioat_get_descriptor(sc, sc->head - 1)->generic.next);
209735b7a45eSConrad Meyer 
209835b7a45eSConrad Meyer 	db_printf("  ring[%u] (head):\n", (sc->head) %
209935b7a45eSConrad Meyer 	    (1 << sc->ring_size_order));
210035b7a45eSConrad Meyer 	db_printf("   id: %u\n", ioat_get_ring_entry(sc, sc->head)->id);
210135b7a45eSConrad Meyer 	db_printf("   addr: 0x%lx\n",
21028e269d99SConrad Meyer 	    RING_PHYS_ADDR(sc, sc->head));
210335b7a45eSConrad Meyer 	db_printf("   next: 0x%lx\n",
21048e269d99SConrad Meyer 	     ioat_get_descriptor(sc, sc->head)->generic.next);
210535b7a45eSConrad Meyer 
210635b7a45eSConrad Meyer 	for (idx = 0; idx < (1 << sc->ring_size_order); idx++)
210735b7a45eSConrad Meyer 		if ((*sc->comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK)
21088e269d99SConrad Meyer 		    == RING_PHYS_ADDR(sc, idx))
210935b7a45eSConrad Meyer 			db_printf("  ring[%u] == hardware tail\n", idx);
211035b7a45eSConrad Meyer 
21110bb35debSConrad Meyer 	db_printf(" cleanup_lock: ");
21120bb35debSConrad Meyer 	db_show_lock(&sc->cleanup_lock);
21130bb35debSConrad Meyer 
21140bb35debSConrad Meyer 	db_printf(" refcnt: %u\n", sc->refcnt);
21150bb35debSConrad Meyer 	db_printf(" stats:\n");
21160bb35debSConrad Meyer 	db_printf("  interrupts: %lu\n", sc->stats.interrupts);
21170bb35debSConrad Meyer 	db_printf("  descriptors_processed: %lu\n", sc->stats.descriptors_processed);
21180bb35debSConrad Meyer 	db_printf("  descriptors_error: %lu\n", sc->stats.descriptors_error);
21190bb35debSConrad Meyer 	db_printf("  descriptors_submitted: %lu\n", sc->stats.descriptors_submitted);
21200bb35debSConrad Meyer 
21210bb35debSConrad Meyer 	db_printf("  channel_halts: %u\n", sc->stats.channel_halts);
21220bb35debSConrad Meyer 	db_printf("  last_halt_chanerr: %u\n", sc->stats.last_halt_chanerr);
21230bb35debSConrad Meyer 
21240bb35debSConrad Meyer 	if (db_pager_quit)
21250bb35debSConrad Meyer 		return;
21260bb35debSConrad Meyer 
21270bb35debSConrad Meyer 	db_printf(" hw status:\n");
21280bb35debSConrad Meyer 	db_printf("  status: 0x%lx\n", ioat_get_chansts(sc));
21290bb35debSConrad Meyer 	db_printf("  chanctrl: 0x%x\n",
21300bb35debSConrad Meyer 	    (unsigned)ioat_read_2(sc, IOAT_CHANCTRL_OFFSET));
21310bb35debSConrad Meyer 	db_printf("  chancmd: 0x%x\n",
21320bb35debSConrad Meyer 	    (unsigned)ioat_read_1(sc, IOAT_CHANCMD_OFFSET));
21330bb35debSConrad Meyer 	db_printf("  dmacount: 0x%x\n",
21340bb35debSConrad Meyer 	    (unsigned)ioat_read_2(sc, IOAT_DMACOUNT_OFFSET));
21350bb35debSConrad Meyer 	db_printf("  chainaddr: 0x%lx\n",
21360bb35debSConrad Meyer 	    ioat_read_double_4(sc, IOAT_CHAINADDR_OFFSET_LOW));
21370bb35debSConrad Meyer 	db_printf("  chancmp: 0x%lx\n",
21380bb35debSConrad Meyer 	    ioat_read_double_4(sc, IOAT_CHANCMP_OFFSET_LOW));
21390bb35debSConrad Meyer 	db_printf("  chanerr: %b\n",
21400bb35debSConrad Meyer 	    (int)ioat_read_4(sc, IOAT_CHANERR_OFFSET), IOAT_CHANERR_STR);
21410bb35debSConrad Meyer 	return;
21420bb35debSConrad Meyer usage:
21430bb35debSConrad Meyer 	db_printf("usage: show ioat <0-%u>\n", ioat_channel_index);
21440bb35debSConrad Meyer 	return;
21450bb35debSConrad Meyer }
21460bb35debSConrad Meyer #endif /* DDB */
2147