xref: /freebsd/sys/dev/ioat/ioat.c (revision ea9e23edf392405750dd0bf886a038e8a5202e17)
1e974f91cSConrad Meyer /*-
2e974f91cSConrad Meyer  * Copyright (C) 2012 Intel Corporation
3e974f91cSConrad Meyer  * All rights reserved.
4e974f91cSConrad Meyer  *
5e974f91cSConrad Meyer  * Redistribution and use in source and binary forms, with or without
6e974f91cSConrad Meyer  * modification, are permitted provided that the following conditions
7e974f91cSConrad Meyer  * are met:
8e974f91cSConrad Meyer  * 1. Redistributions of source code must retain the above copyright
9e974f91cSConrad Meyer  *    notice, this list of conditions and the following disclaimer.
10e974f91cSConrad Meyer  * 2. Redistributions in binary form must reproduce the above copyright
11e974f91cSConrad Meyer  *    notice, this list of conditions and the following disclaimer in the
12e974f91cSConrad Meyer  *    documentation and/or other materials provided with the distribution.
13e974f91cSConrad Meyer  *
14e974f91cSConrad Meyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15e974f91cSConrad Meyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16e974f91cSConrad Meyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17e974f91cSConrad Meyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18e974f91cSConrad Meyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19e974f91cSConrad Meyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20e974f91cSConrad Meyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21e974f91cSConrad Meyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22e974f91cSConrad Meyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23e974f91cSConrad Meyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24e974f91cSConrad Meyer  * SUCH DAMAGE.
25e974f91cSConrad Meyer  */
26e974f91cSConrad Meyer 
27e974f91cSConrad Meyer #include <sys/cdefs.h>
28e974f91cSConrad Meyer __FBSDID("$FreeBSD$");
29e974f91cSConrad Meyer 
300bb35debSConrad Meyer #include "opt_ddb.h"
310bb35debSConrad Meyer 
32e974f91cSConrad Meyer #include <sys/param.h>
33e974f91cSConrad Meyer #include <sys/systm.h>
34e974f91cSConrad Meyer #include <sys/bus.h>
35e974f91cSConrad Meyer #include <sys/conf.h>
3676305bb8SConrad Meyer #include <sys/fail.h>
37e974f91cSConrad Meyer #include <sys/ioccom.h>
38e974f91cSConrad Meyer #include <sys/kernel.h>
39e974f91cSConrad Meyer #include <sys/lock.h>
40e974f91cSConrad Meyer #include <sys/malloc.h>
41e974f91cSConrad Meyer #include <sys/module.h>
42e974f91cSConrad Meyer #include <sys/mutex.h>
43e974f91cSConrad Meyer #include <sys/rman.h>
44faefad9cSConrad Meyer #include <sys/sbuf.h>
45e974f91cSConrad Meyer #include <sys/sysctl.h>
46374b05e1SConrad Meyer #include <sys/taskqueue.h>
47e974f91cSConrad Meyer #include <sys/time.h>
48e974f91cSConrad Meyer #include <dev/pci/pcireg.h>
49e974f91cSConrad Meyer #include <dev/pci/pcivar.h>
50e974f91cSConrad Meyer #include <machine/bus.h>
51e974f91cSConrad Meyer #include <machine/resource.h>
52e974f91cSConrad Meyer #include <machine/stdarg.h>
53e974f91cSConrad Meyer 
540bb35debSConrad Meyer #ifdef DDB
550bb35debSConrad Meyer #include <ddb/ddb.h>
560bb35debSConrad Meyer #endif
570bb35debSConrad Meyer 
58e974f91cSConrad Meyer #include "ioat.h"
59e974f91cSConrad Meyer #include "ioat_hw.h"
60e974f91cSConrad Meyer #include "ioat_internal.h"
61e974f91cSConrad Meyer 
62519e8baaSConrad Meyer #ifndef	BUS_SPACE_MAXADDR_40BIT
63519e8baaSConrad Meyer #define	BUS_SPACE_MAXADDR_40BIT	0xFFFFFFFFFFULL
64519e8baaSConrad Meyer #endif
65466b3540SConrad Meyer #define	IOAT_REFLK	(&ioat->submit_lock)
66fe720f5aSConrad Meyer 
67e974f91cSConrad Meyer static int ioat_probe(device_t device);
68e974f91cSConrad Meyer static int ioat_attach(device_t device);
69e974f91cSConrad Meyer static int ioat_detach(device_t device);
704253ea50SConrad Meyer static int ioat_setup_intr(struct ioat_softc *ioat);
714253ea50SConrad Meyer static int ioat_teardown_intr(struct ioat_softc *ioat);
72e974f91cSConrad Meyer static int ioat3_attach(device_t device);
73cea5b880SConrad Meyer static int ioat_start_channel(struct ioat_softc *ioat);
74e974f91cSConrad Meyer static int ioat_map_pci_bar(struct ioat_softc *ioat);
75e974f91cSConrad Meyer static void ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg,
76e974f91cSConrad Meyer     int error);
77e974f91cSConrad Meyer static void ioat_interrupt_handler(void *arg);
780d1a05d9SConrad Meyer static boolean_t ioat_model_resets_msix(struct ioat_softc *ioat);
79faefad9cSConrad Meyer static int chanerr_to_errno(uint32_t);
80e974f91cSConrad Meyer static void ioat_process_events(struct ioat_softc *ioat);
81e974f91cSConrad Meyer static inline uint32_t ioat_get_active(struct ioat_softc *ioat);
82e974f91cSConrad Meyer static inline uint32_t ioat_get_ring_space(struct ioat_softc *ioat);
83bf8553eaSConrad Meyer static void ioat_free_ring(struct ioat_softc *, uint32_t size,
848e269d99SConrad Meyer     struct ioat_descriptor *);
85bf8553eaSConrad Meyer static int ioat_reserve_space(struct ioat_softc *, uint32_t, int mflags);
868e269d99SConrad Meyer static union ioat_hw_descriptor *ioat_get_descriptor(struct ioat_softc *,
878e269d99SConrad Meyer     uint32_t index);
888e269d99SConrad Meyer static struct ioat_descriptor *ioat_get_ring_entry(struct ioat_softc *,
89e974f91cSConrad Meyer     uint32_t index);
90faefad9cSConrad Meyer static void ioat_halted_debug(struct ioat_softc *, uint32_t);
915ac77963SConrad Meyer static void ioat_poll_timer_callback(void *arg);
92e974f91cSConrad Meyer static void dump_descriptor(void *hw_desc);
93e974f91cSConrad Meyer static void ioat_submit_single(struct ioat_softc *ioat);
94e974f91cSConrad Meyer static void ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg,
95e974f91cSConrad Meyer     int error);
96e974f91cSConrad Meyer static int ioat_reset_hw(struct ioat_softc *ioat);
97374b05e1SConrad Meyer static void ioat_reset_hw_task(void *, int);
98e974f91cSConrad Meyer static void ioat_setup_sysctl(device_t device);
99f7157235SConrad Meyer static int sysctl_handle_reset(SYSCTL_HANDLER_ARGS);
100466b3540SConrad Meyer static inline struct ioat_softc *ioat_get(struct ioat_softc *,
101466b3540SConrad Meyer     enum ioat_ref_kind);
102466b3540SConrad Meyer static inline void ioat_put(struct ioat_softc *, enum ioat_ref_kind);
103faefad9cSConrad Meyer static inline void _ioat_putn(struct ioat_softc *, uint32_t,
104faefad9cSConrad Meyer     enum ioat_ref_kind, boolean_t);
105466b3540SConrad Meyer static inline void ioat_putn(struct ioat_softc *, uint32_t,
106466b3540SConrad Meyer     enum ioat_ref_kind);
107faefad9cSConrad Meyer static inline void ioat_putn_locked(struct ioat_softc *, uint32_t,
108faefad9cSConrad Meyer     enum ioat_ref_kind);
1095f77bd3eSConrad Meyer static void ioat_drain_locked(struct ioat_softc *);
110e974f91cSConrad Meyer 
1111c25420eSConrad Meyer #define	ioat_log_message(v, ...) do {					\
1121c25420eSConrad Meyer 	if ((v) <= g_ioat_debug_level) {				\
1131c25420eSConrad Meyer 		device_printf(ioat->device, __VA_ARGS__);		\
1141c25420eSConrad Meyer 	}								\
1151c25420eSConrad Meyer } while (0)
1161c25420eSConrad Meyer 
117e974f91cSConrad Meyer MALLOC_DEFINE(M_IOAT, "ioat", "ioat driver memory allocations");
118e974f91cSConrad Meyer SYSCTL_NODE(_hw, OID_AUTO, ioat, CTLFLAG_RD, 0, "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");
160e974f91cSConrad Meyer 
161e974f91cSConrad Meyer static struct _pcsid
162e974f91cSConrad Meyer {
163e974f91cSConrad Meyer 	u_int32_t   type;
164e974f91cSConrad Meyer 	const char  *desc;
165e974f91cSConrad Meyer } pci_ids[] = {
166e974f91cSConrad Meyer 	{ 0x34308086, "TBG IOAT Ch0" },
167e974f91cSConrad Meyer 	{ 0x34318086, "TBG IOAT Ch1" },
168e974f91cSConrad Meyer 	{ 0x34328086, "TBG IOAT Ch2" },
169e974f91cSConrad Meyer 	{ 0x34338086, "TBG IOAT Ch3" },
170e974f91cSConrad Meyer 	{ 0x34298086, "TBG IOAT Ch4" },
171e974f91cSConrad Meyer 	{ 0x342a8086, "TBG IOAT Ch5" },
172e974f91cSConrad Meyer 	{ 0x342b8086, "TBG IOAT Ch6" },
173e974f91cSConrad Meyer 	{ 0x342c8086, "TBG IOAT Ch7" },
174e974f91cSConrad Meyer 
175e974f91cSConrad Meyer 	{ 0x37108086, "JSF IOAT Ch0" },
176e974f91cSConrad Meyer 	{ 0x37118086, "JSF IOAT Ch1" },
177e974f91cSConrad Meyer 	{ 0x37128086, "JSF IOAT Ch2" },
178e974f91cSConrad Meyer 	{ 0x37138086, "JSF IOAT Ch3" },
179e974f91cSConrad Meyer 	{ 0x37148086, "JSF IOAT Ch4" },
180e974f91cSConrad Meyer 	{ 0x37158086, "JSF IOAT Ch5" },
181e974f91cSConrad Meyer 	{ 0x37168086, "JSF IOAT Ch6" },
182e974f91cSConrad Meyer 	{ 0x37178086, "JSF IOAT Ch7" },
183e974f91cSConrad Meyer 	{ 0x37188086, "JSF IOAT Ch0 (RAID)" },
184e974f91cSConrad Meyer 	{ 0x37198086, "JSF IOAT Ch1 (RAID)" },
185e974f91cSConrad Meyer 
186e974f91cSConrad Meyer 	{ 0x3c208086, "SNB IOAT Ch0" },
187e974f91cSConrad Meyer 	{ 0x3c218086, "SNB IOAT Ch1" },
188e974f91cSConrad Meyer 	{ 0x3c228086, "SNB IOAT Ch2" },
189e974f91cSConrad Meyer 	{ 0x3c238086, "SNB IOAT Ch3" },
190e974f91cSConrad Meyer 	{ 0x3c248086, "SNB IOAT Ch4" },
191e974f91cSConrad Meyer 	{ 0x3c258086, "SNB IOAT Ch5" },
192e974f91cSConrad Meyer 	{ 0x3c268086, "SNB IOAT Ch6" },
193e974f91cSConrad Meyer 	{ 0x3c278086, "SNB IOAT Ch7" },
194e974f91cSConrad Meyer 	{ 0x3c2e8086, "SNB IOAT Ch0 (RAID)" },
195e974f91cSConrad Meyer 	{ 0x3c2f8086, "SNB IOAT Ch1 (RAID)" },
196e974f91cSConrad Meyer 
197e974f91cSConrad Meyer 	{ 0x0e208086, "IVB IOAT Ch0" },
198e974f91cSConrad Meyer 	{ 0x0e218086, "IVB IOAT Ch1" },
199e974f91cSConrad Meyer 	{ 0x0e228086, "IVB IOAT Ch2" },
200e974f91cSConrad Meyer 	{ 0x0e238086, "IVB IOAT Ch3" },
201e974f91cSConrad Meyer 	{ 0x0e248086, "IVB IOAT Ch4" },
202e974f91cSConrad Meyer 	{ 0x0e258086, "IVB IOAT Ch5" },
203e974f91cSConrad Meyer 	{ 0x0e268086, "IVB IOAT Ch6" },
204e974f91cSConrad Meyer 	{ 0x0e278086, "IVB IOAT Ch7" },
205e974f91cSConrad Meyer 	{ 0x0e2e8086, "IVB IOAT Ch0 (RAID)" },
206e974f91cSConrad Meyer 	{ 0x0e2f8086, "IVB IOAT Ch1 (RAID)" },
207e974f91cSConrad Meyer 
208e974f91cSConrad Meyer 	{ 0x2f208086, "HSW IOAT Ch0" },
209e974f91cSConrad Meyer 	{ 0x2f218086, "HSW IOAT Ch1" },
210e974f91cSConrad Meyer 	{ 0x2f228086, "HSW IOAT Ch2" },
211e974f91cSConrad Meyer 	{ 0x2f238086, "HSW IOAT Ch3" },
212e974f91cSConrad Meyer 	{ 0x2f248086, "HSW IOAT Ch4" },
213e974f91cSConrad Meyer 	{ 0x2f258086, "HSW IOAT Ch5" },
214e974f91cSConrad Meyer 	{ 0x2f268086, "HSW IOAT Ch6" },
215e974f91cSConrad Meyer 	{ 0x2f278086, "HSW IOAT Ch7" },
216e974f91cSConrad Meyer 	{ 0x2f2e8086, "HSW IOAT Ch0 (RAID)" },
217e974f91cSConrad Meyer 	{ 0x2f2f8086, "HSW IOAT Ch1 (RAID)" },
218e974f91cSConrad Meyer 
219e974f91cSConrad Meyer 	{ 0x0c508086, "BWD IOAT Ch0" },
220e974f91cSConrad Meyer 	{ 0x0c518086, "BWD IOAT Ch1" },
221e974f91cSConrad Meyer 	{ 0x0c528086, "BWD IOAT Ch2" },
222e974f91cSConrad Meyer 	{ 0x0c538086, "BWD IOAT Ch3" },
223e974f91cSConrad Meyer 
224e974f91cSConrad Meyer 	{ 0x6f508086, "BDXDE IOAT Ch0" },
225e974f91cSConrad Meyer 	{ 0x6f518086, "BDXDE IOAT Ch1" },
226e974f91cSConrad Meyer 	{ 0x6f528086, "BDXDE IOAT Ch2" },
227e974f91cSConrad Meyer 	{ 0x6f538086, "BDXDE IOAT Ch3" },
228e974f91cSConrad Meyer 
2295afc2508SConrad Meyer 	{ 0x6f208086, "BDX IOAT Ch0" },
2305afc2508SConrad Meyer 	{ 0x6f218086, "BDX IOAT Ch1" },
2315afc2508SConrad Meyer 	{ 0x6f228086, "BDX IOAT Ch2" },
2325afc2508SConrad Meyer 	{ 0x6f238086, "BDX IOAT Ch3" },
2335afc2508SConrad Meyer 	{ 0x6f248086, "BDX IOAT Ch4" },
2345afc2508SConrad Meyer 	{ 0x6f258086, "BDX IOAT Ch5" },
2355afc2508SConrad Meyer 	{ 0x6f268086, "BDX IOAT Ch6" },
2365afc2508SConrad Meyer 	{ 0x6f278086, "BDX IOAT Ch7" },
2375afc2508SConrad Meyer 	{ 0x6f2e8086, "BDX IOAT Ch0 (RAID)" },
2385afc2508SConrad Meyer 	{ 0x6f2f8086, "BDX IOAT Ch1 (RAID)" },
2395afc2508SConrad Meyer 
240e974f91cSConrad Meyer 	{ 0x00000000, NULL           }
241e974f91cSConrad Meyer };
242e974f91cSConrad Meyer 
243e974f91cSConrad Meyer /*
244e974f91cSConrad Meyer  * OS <-> Driver linkage functions
245e974f91cSConrad Meyer  */
246e974f91cSConrad Meyer static int
247e974f91cSConrad Meyer ioat_probe(device_t device)
248e974f91cSConrad Meyer {
249e974f91cSConrad Meyer 	struct _pcsid *ep;
250e974f91cSConrad Meyer 	u_int32_t type;
251e974f91cSConrad Meyer 
252e974f91cSConrad Meyer 	type = pci_get_devid(device);
253e974f91cSConrad Meyer 	for (ep = pci_ids; ep->type; ep++) {
254e974f91cSConrad Meyer 		if (ep->type == type) {
255e974f91cSConrad Meyer 			device_set_desc(device, ep->desc);
256e974f91cSConrad Meyer 			return (0);
257e974f91cSConrad Meyer 		}
258e974f91cSConrad Meyer 	}
259e974f91cSConrad Meyer 	return (ENXIO);
260e974f91cSConrad Meyer }
261e974f91cSConrad Meyer 
262e974f91cSConrad Meyer static int
263e974f91cSConrad Meyer ioat_attach(device_t device)
264e974f91cSConrad Meyer {
265e974f91cSConrad Meyer 	struct ioat_softc *ioat;
266e974f91cSConrad Meyer 	int error;
267e974f91cSConrad Meyer 
268e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
269e974f91cSConrad Meyer 	ioat->device = device;
270e974f91cSConrad Meyer 
271e974f91cSConrad Meyer 	error = ioat_map_pci_bar(ioat);
272e974f91cSConrad Meyer 	if (error != 0)
273e974f91cSConrad Meyer 		goto err;
274e974f91cSConrad Meyer 
275e974f91cSConrad Meyer 	ioat->version = ioat_read_cbver(ioat);
276e974f91cSConrad Meyer 	if (ioat->version < IOAT_VER_3_0) {
277e974f91cSConrad Meyer 		error = ENODEV;
278e974f91cSConrad Meyer 		goto err;
279e974f91cSConrad Meyer 	}
280e974f91cSConrad Meyer 
281e974f91cSConrad Meyer 	error = ioat3_attach(device);
282e974f91cSConrad Meyer 	if (error != 0)
283e974f91cSConrad Meyer 		goto err;
284e974f91cSConrad Meyer 
285e974f91cSConrad Meyer 	error = pci_enable_busmaster(device);
286e974f91cSConrad Meyer 	if (error != 0)
287e974f91cSConrad Meyer 		goto err;
288e974f91cSConrad Meyer 
289466b3540SConrad Meyer 	error = ioat_setup_intr(ioat);
290466b3540SConrad Meyer 	if (error != 0)
291466b3540SConrad Meyer 		goto err;
292466b3540SConrad Meyer 
293cea5b880SConrad Meyer 	error = ioat_reset_hw(ioat);
2947afbb263SConrad Meyer 	if (error != 0)
295466b3540SConrad Meyer 		goto err;
2967afbb263SConrad Meyer 
2977afbb263SConrad Meyer 	ioat_process_events(ioat);
2987afbb263SConrad Meyer 	ioat_setup_sysctl(device);
2997afbb263SConrad Meyer 
3005f77bd3eSConrad Meyer 	ioat->chan_idx = ioat_channel_index;
301e974f91cSConrad Meyer 	ioat_channel[ioat_channel_index++] = ioat;
3027afbb263SConrad Meyer 	ioat_test_attach();
303e974f91cSConrad Meyer 
304e974f91cSConrad Meyer err:
305e974f91cSConrad Meyer 	if (error != 0)
306e974f91cSConrad Meyer 		ioat_detach(device);
307e974f91cSConrad Meyer 	return (error);
308e974f91cSConrad Meyer }
309e974f91cSConrad Meyer 
310e974f91cSConrad Meyer static int
311e974f91cSConrad Meyer ioat_detach(device_t device)
312e974f91cSConrad Meyer {
313e974f91cSConrad Meyer 	struct ioat_softc *ioat;
314e974f91cSConrad Meyer 
315e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
3167afbb263SConrad Meyer 
3177afbb263SConrad Meyer 	ioat_test_detach();
318374b05e1SConrad Meyer 	taskqueue_drain(taskqueue_thread, &ioat->reset_task);
3195f77bd3eSConrad Meyer 
3205f77bd3eSConrad Meyer 	mtx_lock(IOAT_REFLK);
3215f77bd3eSConrad Meyer 	ioat->quiescing = TRUE;
3220ff814e8SConrad Meyer 	ioat->destroying = TRUE;
3230ff814e8SConrad Meyer 	wakeup(&ioat->quiescing);
32493f7f84aSConrad Meyer 	wakeup(&ioat->resetting);
3250ff814e8SConrad Meyer 
3265f77bd3eSConrad Meyer 	ioat_channel[ioat->chan_idx] = NULL;
3275f77bd3eSConrad Meyer 
3285f77bd3eSConrad Meyer 	ioat_drain_locked(ioat);
3295f77bd3eSConrad Meyer 	mtx_unlock(IOAT_REFLK);
330fe720f5aSConrad Meyer 
331fe720f5aSConrad Meyer 	ioat_teardown_intr(ioat);
3325ac77963SConrad Meyer 	callout_drain(&ioat->poll_timer);
333e974f91cSConrad Meyer 
334e974f91cSConrad Meyer 	pci_disable_busmaster(device);
335e974f91cSConrad Meyer 
336e974f91cSConrad Meyer 	if (ioat->pci_resource != NULL)
337e974f91cSConrad Meyer 		bus_release_resource(device, SYS_RES_MEMORY,
338e974f91cSConrad Meyer 		    ioat->pci_resource_id, ioat->pci_resource);
339e974f91cSConrad Meyer 
340bf8553eaSConrad Meyer 	if (ioat->ring != NULL)
341bf8553eaSConrad Meyer 		ioat_free_ring(ioat, 1 << ioat->ring_size_order, ioat->ring);
342e974f91cSConrad Meyer 
343e974f91cSConrad Meyer 	if (ioat->comp_update != NULL) {
344e974f91cSConrad Meyer 		bus_dmamap_unload(ioat->comp_update_tag, ioat->comp_update_map);
345e974f91cSConrad Meyer 		bus_dmamem_free(ioat->comp_update_tag, ioat->comp_update,
346e974f91cSConrad Meyer 		    ioat->comp_update_map);
347e974f91cSConrad Meyer 		bus_dma_tag_destroy(ioat->comp_update_tag);
348e974f91cSConrad Meyer 	}
349e974f91cSConrad Meyer 
3508e269d99SConrad Meyer 	if (ioat->hw_desc_ring != NULL) {
3518e269d99SConrad Meyer 		bus_dmamap_unload(ioat->hw_desc_tag, ioat->hw_desc_map);
3528e269d99SConrad Meyer 		bus_dmamem_free(ioat->hw_desc_tag, ioat->hw_desc_ring,
3538e269d99SConrad Meyer 		    ioat->hw_desc_map);
354e974f91cSConrad Meyer 		bus_dma_tag_destroy(ioat->hw_desc_tag);
3558e269d99SConrad Meyer 	}
356e974f91cSConrad Meyer 
3574253ea50SConrad Meyer 	return (0);
3584253ea50SConrad Meyer }
3594253ea50SConrad Meyer 
3604253ea50SConrad Meyer static int
3614253ea50SConrad Meyer ioat_teardown_intr(struct ioat_softc *ioat)
3624253ea50SConrad Meyer {
3634253ea50SConrad Meyer 
364e974f91cSConrad Meyer 	if (ioat->tag != NULL)
3654253ea50SConrad Meyer 		bus_teardown_intr(ioat->device, ioat->res, ioat->tag);
366e974f91cSConrad Meyer 
367e974f91cSConrad Meyer 	if (ioat->res != NULL)
3684253ea50SConrad Meyer 		bus_release_resource(ioat->device, SYS_RES_IRQ,
369e974f91cSConrad Meyer 		    rman_get_rid(ioat->res), ioat->res);
370e974f91cSConrad Meyer 
3714253ea50SConrad Meyer 	pci_release_msi(ioat->device);
372e974f91cSConrad Meyer 	return (0);
373e974f91cSConrad Meyer }
374e974f91cSConrad Meyer 
375e974f91cSConrad Meyer static int
376cea5b880SConrad Meyer ioat_start_channel(struct ioat_softc *ioat)
377e974f91cSConrad Meyer {
378fe8712f8SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
379fe8712f8SConrad Meyer 	struct ioat_descriptor *desc;
380fe8712f8SConrad Meyer 	struct bus_dmadesc *dmadesc;
381e974f91cSConrad Meyer 	uint64_t status;
382e974f91cSConrad Meyer 	uint32_t chanerr;
383e974f91cSConrad Meyer 	int i;
384e974f91cSConrad Meyer 
385e974f91cSConrad Meyer 	ioat_acquire(&ioat->dmaengine);
386fe8712f8SConrad Meyer 
387fe8712f8SConrad Meyer 	/* Submit 'NULL' operation manually to avoid quiescing flag */
388fe8712f8SConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->head);
3898e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, ioat->head)->dma;
390fe8712f8SConrad Meyer 	dmadesc = &desc->bus_dmadesc;
391fe8712f8SConrad Meyer 
392fe8712f8SConrad Meyer 	dmadesc->callback_fn = NULL;
393fe8712f8SConrad Meyer 	dmadesc->callback_arg = NULL;
394fe8712f8SConrad Meyer 
395fe8712f8SConrad Meyer 	hw_desc->u.control_raw = 0;
396fe8712f8SConrad Meyer 	hw_desc->u.control_generic.op = IOAT_OP_COPY;
397fe8712f8SConrad Meyer 	hw_desc->u.control_generic.completion_update = 1;
398fe8712f8SConrad Meyer 	hw_desc->size = 8;
399fe8712f8SConrad Meyer 	hw_desc->src_addr = 0;
400fe8712f8SConrad Meyer 	hw_desc->dest_addr = 0;
401fe8712f8SConrad Meyer 	hw_desc->u.control.null = 1;
402fe8712f8SConrad Meyer 
403fe8712f8SConrad Meyer 	ioat_submit_single(ioat);
404e974f91cSConrad Meyer 	ioat_release(&ioat->dmaengine);
405e974f91cSConrad Meyer 
406e974f91cSConrad Meyer 	for (i = 0; i < 100; i++) {
407e974f91cSConrad Meyer 		DELAY(1);
408e974f91cSConrad Meyer 		status = ioat_get_chansts(ioat);
409e974f91cSConrad Meyer 		if (is_ioat_idle(status))
410e974f91cSConrad Meyer 			return (0);
411e974f91cSConrad Meyer 	}
412e974f91cSConrad Meyer 
413e974f91cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
414e974f91cSConrad Meyer 	ioat_log_message(0, "could not start channel: "
41559acd4baSConrad Meyer 	    "status = %#jx error = %b\n", (uintmax_t)status, (int)chanerr,
41659acd4baSConrad Meyer 	    IOAT_CHANERR_STR);
417e974f91cSConrad Meyer 	return (ENXIO);
418e974f91cSConrad Meyer }
419e974f91cSConrad Meyer 
420e974f91cSConrad Meyer /*
421e974f91cSConrad Meyer  * Initialize Hardware
422e974f91cSConrad Meyer  */
423e974f91cSConrad Meyer static int
424e974f91cSConrad Meyer ioat3_attach(device_t device)
425e974f91cSConrad Meyer {
426e974f91cSConrad Meyer 	struct ioat_softc *ioat;
4278e269d99SConrad Meyer 	struct ioat_descriptor *ring;
428e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *dma_hw_desc;
4298e269d99SConrad Meyer 	void *hw_desc;
4308e269d99SConrad Meyer 	size_t ringsz;
431e974f91cSConrad Meyer 	int i, num_descriptors;
432e974f91cSConrad Meyer 	int error;
433e974f91cSConrad Meyer 	uint8_t xfercap;
434e974f91cSConrad Meyer 
435e974f91cSConrad Meyer 	error = 0;
436e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
4371693d27bSConrad Meyer 	ioat->capabilities = ioat_read_dmacapability(ioat);
4381693d27bSConrad Meyer 
43948fed014SConrad Meyer 	ioat_log_message(0, "Capabilities: %b\n", (int)ioat->capabilities,
4401693d27bSConrad Meyer 	    IOAT_DMACAP_STR);
441e974f91cSConrad Meyer 
442e974f91cSConrad Meyer 	xfercap = ioat_read_xfercap(ioat);
443e974f91cSConrad Meyer 	ioat->max_xfer_size = 1 << xfercap;
444e974f91cSConrad Meyer 
4455ca9fc2aSConrad Meyer 	ioat->intrdelay_supported = (ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) &
4465ca9fc2aSConrad Meyer 	    IOAT_INTRDELAY_SUPPORTED) != 0;
4475ca9fc2aSConrad Meyer 	if (ioat->intrdelay_supported)
4485ca9fc2aSConrad Meyer 		ioat->intrdelay_max = IOAT_INTRDELAY_US_MASK;
4495ca9fc2aSConrad Meyer 
450e974f91cSConrad Meyer 	/* TODO: need to check DCA here if we ever do XOR/PQ */
451e974f91cSConrad Meyer 
452e974f91cSConrad Meyer 	mtx_init(&ioat->submit_lock, "ioat_submit", NULL, MTX_DEF);
453faefad9cSConrad Meyer 	mtx_init(&ioat->cleanup_lock, "ioat_cleanup", NULL, MTX_DEF);
4545ac77963SConrad Meyer 	callout_init(&ioat->poll_timer, 1);
455374b05e1SConrad Meyer 	TASK_INIT(&ioat->reset_task, 0, ioat_reset_hw_task, ioat);
456e974f91cSConrad Meyer 
457faefad9cSConrad Meyer 	/* Establish lock order for Witness */
458faefad9cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
459faefad9cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
460faefad9cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
461faefad9cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
462faefad9cSConrad Meyer 
46325ad9585SConrad Meyer 	ioat->is_submitter_processing = FALSE;
464e974f91cSConrad Meyer 	ioat->is_completion_pending = FALSE;
465e974f91cSConrad Meyer 	ioat->is_reset_pending = FALSE;
466e974f91cSConrad Meyer 	ioat->is_channel_running = FALSE;
467e974f91cSConrad Meyer 
468e974f91cSConrad Meyer 	bus_dma_tag_create(bus_get_dma_tag(ioat->device), sizeof(uint64_t), 0x0,
469e974f91cSConrad Meyer 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
470e974f91cSConrad Meyer 	    sizeof(uint64_t), 1, sizeof(uint64_t), 0, NULL, NULL,
471e974f91cSConrad Meyer 	    &ioat->comp_update_tag);
472e974f91cSConrad Meyer 
473e974f91cSConrad Meyer 	error = bus_dmamem_alloc(ioat->comp_update_tag,
474e974f91cSConrad Meyer 	    (void **)&ioat->comp_update, BUS_DMA_ZERO, &ioat->comp_update_map);
475e974f91cSConrad Meyer 	if (ioat->comp_update == NULL)
476e974f91cSConrad Meyer 		return (ENOMEM);
477e974f91cSConrad Meyer 
478e974f91cSConrad Meyer 	error = bus_dmamap_load(ioat->comp_update_tag, ioat->comp_update_map,
479e974f91cSConrad Meyer 	    ioat->comp_update, sizeof(uint64_t), ioat_comp_update_map, ioat,
480e974f91cSConrad Meyer 	    0);
481e974f91cSConrad Meyer 	if (error != 0)
482e974f91cSConrad Meyer 		return (error);
483e974f91cSConrad Meyer 
484a0992979SConrad Meyer 	ioat->ring_size_order = g_ioat_ring_order;
485e974f91cSConrad Meyer 	num_descriptors = 1 << ioat->ring_size_order;
4868e269d99SConrad Meyer 	ringsz = sizeof(struct ioat_dma_hw_descriptor) * num_descriptors;
487e974f91cSConrad Meyer 
4888e269d99SConrad Meyer 	error = bus_dma_tag_create(bus_get_dma_tag(ioat->device),
4898e269d99SConrad Meyer 	    2 * 1024 * 1024, 0x0, BUS_SPACE_MAXADDR_40BIT, BUS_SPACE_MAXADDR,
4908e269d99SConrad Meyer 	    NULL, NULL, ringsz, 1, ringsz, 0, NULL, NULL, &ioat->hw_desc_tag);
4918e269d99SConrad Meyer 	if (error != 0)
4928e269d99SConrad Meyer 		return (error);
4938e269d99SConrad Meyer 
4948e269d99SConrad Meyer 	error = bus_dmamem_alloc(ioat->hw_desc_tag, &hw_desc,
4958e269d99SConrad Meyer 	    BUS_DMA_ZERO | BUS_DMA_WAITOK, &ioat->hw_desc_map);
4968e269d99SConrad Meyer 	if (error != 0)
4978e269d99SConrad Meyer 		return (error);
4988e269d99SConrad Meyer 
4998e269d99SConrad Meyer 	error = bus_dmamap_load(ioat->hw_desc_tag, ioat->hw_desc_map, hw_desc,
5008e269d99SConrad Meyer 	    ringsz, ioat_dmamap_cb, &ioat->hw_desc_bus_addr, BUS_DMA_WAITOK);
5018e269d99SConrad Meyer 	if (error)
5028e269d99SConrad Meyer 		return (error);
5038e269d99SConrad Meyer 
5048e269d99SConrad Meyer 	ioat->hw_desc_ring = hw_desc;
505e974f91cSConrad Meyer 
506e974f91cSConrad Meyer 	ioat->ring = malloc(num_descriptors * sizeof(*ring), M_IOAT,
507bf8553eaSConrad Meyer 	    M_ZERO | M_WAITOK);
508e974f91cSConrad Meyer 
509e974f91cSConrad Meyer 	ring = ioat->ring;
510e974f91cSConrad Meyer 	for (i = 0; i < num_descriptors; i++) {
5118e269d99SConrad Meyer 		memset(&ring[i].bus_dmadesc, 0, sizeof(ring[i].bus_dmadesc));
5128e269d99SConrad Meyer 		ring[i].id = i;
513e974f91cSConrad Meyer 	}
514e974f91cSConrad Meyer 
5158e269d99SConrad Meyer 	for (i = 0; i < num_descriptors; i++) {
5168e269d99SConrad Meyer 		dma_hw_desc = &ioat->hw_desc_ring[i].dma;
5178e269d99SConrad Meyer 		dma_hw_desc->next = RING_PHYS_ADDR(ioat, i + 1);
518e974f91cSConrad Meyer 	}
519e974f91cSConrad Meyer 
520bf8553eaSConrad Meyer 	ioat->head = ioat->hw_head = 0;
521e974f91cSConrad Meyer 	ioat->tail = 0;
522e974f91cSConrad Meyer 	ioat->last_seen = 0;
523fe8712f8SConrad Meyer 	*ioat->comp_update = 0;
524e974f91cSConrad Meyer 	return (0);
525e974f91cSConrad Meyer }
526e974f91cSConrad Meyer 
527e974f91cSConrad Meyer static int
528e974f91cSConrad Meyer ioat_map_pci_bar(struct ioat_softc *ioat)
529e974f91cSConrad Meyer {
530e974f91cSConrad Meyer 
531e974f91cSConrad Meyer 	ioat->pci_resource_id = PCIR_BAR(0);
532e88e14b9SConrad Meyer 	ioat->pci_resource = bus_alloc_resource_any(ioat->device,
533e88e14b9SConrad Meyer 	    SYS_RES_MEMORY, &ioat->pci_resource_id, RF_ACTIVE);
534e974f91cSConrad Meyer 
535e974f91cSConrad Meyer 	if (ioat->pci_resource == NULL) {
536e974f91cSConrad Meyer 		ioat_log_message(0, "unable to allocate pci resource\n");
537e974f91cSConrad Meyer 		return (ENODEV);
538e974f91cSConrad Meyer 	}
539e974f91cSConrad Meyer 
540e974f91cSConrad Meyer 	ioat->pci_bus_tag = rman_get_bustag(ioat->pci_resource);
541e974f91cSConrad Meyer 	ioat->pci_bus_handle = rman_get_bushandle(ioat->pci_resource);
542e974f91cSConrad Meyer 	return (0);
543e974f91cSConrad Meyer }
544e974f91cSConrad Meyer 
545e974f91cSConrad Meyer static void
546e974f91cSConrad Meyer ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg, int error)
547e974f91cSConrad Meyer {
548e974f91cSConrad Meyer 	struct ioat_softc *ioat = arg;
549e974f91cSConrad Meyer 
550cea5b880SConrad Meyer 	KASSERT(error == 0, ("%s: error:%d", __func__, error));
551e974f91cSConrad Meyer 	ioat->comp_update_bus_addr = seg[0].ds_addr;
552e974f91cSConrad Meyer }
553e974f91cSConrad Meyer 
554e974f91cSConrad Meyer static void
555e974f91cSConrad Meyer ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
556e974f91cSConrad Meyer {
557e974f91cSConrad Meyer 	bus_addr_t *baddr;
558e974f91cSConrad Meyer 
559cea5b880SConrad Meyer 	KASSERT(error == 0, ("%s: error:%d", __func__, error));
560e974f91cSConrad Meyer 	baddr = arg;
561e974f91cSConrad Meyer 	*baddr = segs->ds_addr;
562e974f91cSConrad Meyer }
563e974f91cSConrad Meyer 
564e974f91cSConrad Meyer /*
565e974f91cSConrad Meyer  * Interrupt setup and handlers
566e974f91cSConrad Meyer  */
567e974f91cSConrad Meyer static int
5684253ea50SConrad Meyer ioat_setup_intr(struct ioat_softc *ioat)
569e974f91cSConrad Meyer {
570e974f91cSConrad Meyer 	uint32_t num_vectors;
571e974f91cSConrad Meyer 	int error;
572e974f91cSConrad Meyer 	boolean_t use_msix;
573e974f91cSConrad Meyer 	boolean_t force_legacy_interrupts;
574e974f91cSConrad Meyer 
575e974f91cSConrad Meyer 	use_msix = FALSE;
576e974f91cSConrad Meyer 	force_legacy_interrupts = FALSE;
577e974f91cSConrad Meyer 
578e974f91cSConrad Meyer 	if (!g_force_legacy_interrupts && pci_msix_count(ioat->device) >= 1) {
579e974f91cSConrad Meyer 		num_vectors = 1;
580e974f91cSConrad Meyer 		pci_alloc_msix(ioat->device, &num_vectors);
581e974f91cSConrad Meyer 		if (num_vectors == 1)
582e974f91cSConrad Meyer 			use_msix = TRUE;
583e974f91cSConrad Meyer 	}
584e974f91cSConrad Meyer 
585e974f91cSConrad Meyer 	if (use_msix) {
586e974f91cSConrad Meyer 		ioat->rid = 1;
587e974f91cSConrad Meyer 		ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
588e974f91cSConrad Meyer 		    &ioat->rid, RF_ACTIVE);
589e974f91cSConrad Meyer 	} else {
590e974f91cSConrad Meyer 		ioat->rid = 0;
591e974f91cSConrad Meyer 		ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
592e974f91cSConrad Meyer 		    &ioat->rid, RF_SHAREABLE | RF_ACTIVE);
593e974f91cSConrad Meyer 	}
594e974f91cSConrad Meyer 	if (ioat->res == NULL) {
595e974f91cSConrad Meyer 		ioat_log_message(0, "bus_alloc_resource failed\n");
596e974f91cSConrad Meyer 		return (ENOMEM);
597e974f91cSConrad Meyer 	}
598e974f91cSConrad Meyer 
599e974f91cSConrad Meyer 	ioat->tag = NULL;
600e974f91cSConrad Meyer 	error = bus_setup_intr(ioat->device, ioat->res, INTR_MPSAFE |
601e974f91cSConrad Meyer 	    INTR_TYPE_MISC, NULL, ioat_interrupt_handler, ioat, &ioat->tag);
602e974f91cSConrad Meyer 	if (error != 0) {
603e974f91cSConrad Meyer 		ioat_log_message(0, "bus_setup_intr failed\n");
604e974f91cSConrad Meyer 		return (error);
605e974f91cSConrad Meyer 	}
606e974f91cSConrad Meyer 
607e974f91cSConrad Meyer 	ioat_write_intrctrl(ioat, IOAT_INTRCTRL_MASTER_INT_EN);
608e974f91cSConrad Meyer 	return (0);
609e974f91cSConrad Meyer }
610e974f91cSConrad Meyer 
6114253ea50SConrad Meyer static boolean_t
6120d1a05d9SConrad Meyer ioat_model_resets_msix(struct ioat_softc *ioat)
6134253ea50SConrad Meyer {
6144253ea50SConrad Meyer 	u_int32_t pciid;
6154253ea50SConrad Meyer 
6164253ea50SConrad Meyer 	pciid = pci_get_devid(ioat->device);
6174253ea50SConrad Meyer 	switch (pciid) {
6180d1a05d9SConrad Meyer 		/* BWD: */
6190d1a05d9SConrad Meyer 	case 0x0c508086:
6200d1a05d9SConrad Meyer 	case 0x0c518086:
6210d1a05d9SConrad Meyer 	case 0x0c528086:
6220d1a05d9SConrad Meyer 	case 0x0c538086:
6230d1a05d9SConrad Meyer 		/* BDXDE: */
6244253ea50SConrad Meyer 	case 0x6f508086:
6254253ea50SConrad Meyer 	case 0x6f518086:
6264253ea50SConrad Meyer 	case 0x6f528086:
6274253ea50SConrad Meyer 	case 0x6f538086:
6284253ea50SConrad Meyer 		return (TRUE);
6294253ea50SConrad Meyer 	}
6304253ea50SConrad Meyer 
6314253ea50SConrad Meyer 	return (FALSE);
6324253ea50SConrad Meyer }
6334253ea50SConrad Meyer 
634e974f91cSConrad Meyer static void
635e974f91cSConrad Meyer ioat_interrupt_handler(void *arg)
636e974f91cSConrad Meyer {
637e974f91cSConrad Meyer 	struct ioat_softc *ioat = arg;
638e974f91cSConrad Meyer 
63901fbbc88SConrad Meyer 	ioat->stats.interrupts++;
640e974f91cSConrad Meyer 	ioat_process_events(ioat);
641e974f91cSConrad Meyer }
642e974f91cSConrad Meyer 
643faefad9cSConrad Meyer static int
644faefad9cSConrad Meyer chanerr_to_errno(uint32_t chanerr)
645faefad9cSConrad Meyer {
646faefad9cSConrad Meyer 
647faefad9cSConrad Meyer 	if (chanerr == 0)
648faefad9cSConrad Meyer 		return (0);
649faefad9cSConrad Meyer 	if ((chanerr & (IOAT_CHANERR_XSADDERR | IOAT_CHANERR_XDADDERR)) != 0)
650faefad9cSConrad Meyer 		return (EFAULT);
651faefad9cSConrad Meyer 	if ((chanerr & (IOAT_CHANERR_RDERR | IOAT_CHANERR_WDERR)) != 0)
652faefad9cSConrad Meyer 		return (EIO);
653faefad9cSConrad Meyer 	/* This one is probably our fault: */
654faefad9cSConrad Meyer 	if ((chanerr & IOAT_CHANERR_NDADDERR) != 0)
655faefad9cSConrad Meyer 		return (EIO);
656faefad9cSConrad Meyer 	return (EIO);
657faefad9cSConrad Meyer }
658faefad9cSConrad Meyer 
659e974f91cSConrad Meyer static void
660e974f91cSConrad Meyer ioat_process_events(struct ioat_softc *ioat)
661e974f91cSConrad Meyer {
662e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
663e974f91cSConrad Meyer 	struct bus_dmadesc *dmadesc;
664e974f91cSConrad Meyer 	uint64_t comp_update, status;
665faefad9cSConrad Meyer 	uint32_t completed, chanerr;
6665ac77963SConrad Meyer 	boolean_t pending;
667faefad9cSConrad Meyer 	int error;
668e974f91cSConrad Meyer 
669e974f91cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
670e974f91cSConrad Meyer 
671fe8712f8SConrad Meyer 	/*
672fe8712f8SConrad Meyer 	 * Don't run while the hardware is being reset.  Reset is responsible
673fe8712f8SConrad Meyer 	 * for blocking new work and draining & completing existing work, so
674fe8712f8SConrad Meyer 	 * there is nothing to do until new work is queued after reset anyway.
675fe8712f8SConrad Meyer 	 */
676fe8712f8SConrad Meyer 	if (ioat->resetting_cleanup) {
677fe8712f8SConrad Meyer 		mtx_unlock(&ioat->cleanup_lock);
678fe8712f8SConrad Meyer 		return;
679fe8712f8SConrad Meyer 	}
680fe8712f8SConrad Meyer 
681e974f91cSConrad Meyer 	completed = 0;
6820d0f2640SConrad Meyer 	comp_update = *ioat->comp_update;
683e974f91cSConrad Meyer 	status = comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK;
684e974f91cSConrad Meyer 
6858e269d99SConrad Meyer 	if (status < ioat->hw_desc_bus_addr ||
6868e269d99SConrad Meyer 	    status >= ioat->hw_desc_bus_addr + (1 << ioat->ring_size_order) *
6878e269d99SConrad Meyer 	    sizeof(struct ioat_generic_hw_descriptor))
6888e269d99SConrad Meyer 		panic("Bogus completion address %jx (channel %u)",
6898e269d99SConrad Meyer 		    (uintmax_t)status, ioat->chan_idx);
6908e269d99SConrad Meyer 
6911bbc06b8SConrad Meyer 	if (status == ioat->last_seen) {
6921bbc06b8SConrad Meyer 		/*
6931bbc06b8SConrad Meyer 		 * If we landed in process_events and nothing has been
6941bbc06b8SConrad Meyer 		 * completed, check for a timeout due to channel halt.
6951bbc06b8SConrad Meyer 		 */
6961bbc06b8SConrad Meyer 		goto out;
6971bbc06b8SConrad Meyer 	}
698dc465059SConrad Meyer 	CTR4(KTR_IOAT, "%s channel=%u hw_status=0x%lx last_seen=0x%lx",
699dc465059SConrad Meyer 	    __func__, ioat->chan_idx, comp_update, ioat->last_seen);
7001bbc06b8SConrad Meyer 
7018e269d99SConrad Meyer 	while (RING_PHYS_ADDR(ioat, ioat->tail - 1) != status) {
702e974f91cSConrad Meyer 		desc = ioat_get_ring_entry(ioat, ioat->tail);
703e974f91cSConrad Meyer 		dmadesc = &desc->bus_dmadesc;
70405e4cffbSConrad Meyer 		CTR5(KTR_IOAT, "channel=%u completing desc idx %u (%p) ok  cb %p(%p)",
70505e4cffbSConrad Meyer 		    ioat->chan_idx, ioat->tail, dmadesc, dmadesc->callback_fn,
706520f6023SConrad Meyer 		    dmadesc->callback_arg);
707e974f91cSConrad Meyer 
708faefad9cSConrad Meyer 		if (dmadesc->callback_fn != NULL)
709faefad9cSConrad Meyer 			dmadesc->callback_fn(dmadesc->callback_arg, 0);
710e974f91cSConrad Meyer 
711466b3540SConrad Meyer 		completed++;
712e974f91cSConrad Meyer 		ioat->tail++;
713e974f91cSConrad Meyer 	}
71405e4cffbSConrad Meyer 	CTR5(KTR_IOAT, "%s channel=%u head=%u tail=%u active=%u", __func__,
71505e4cffbSConrad Meyer 	    ioat->chan_idx, ioat->head, ioat->tail, ioat_get_active(ioat));
716e974f91cSConrad Meyer 
7170283c0f5SConrad Meyer 	if (completed != 0) {
7188e269d99SConrad Meyer 		ioat->last_seen = RING_PHYS_ADDR(ioat, ioat->tail - 1);
71901fbbc88SConrad Meyer 		ioat->stats.descriptors_processed += completed;
7200283c0f5SConrad Meyer 	}
72101fbbc88SConrad Meyer 
7221bbc06b8SConrad Meyer out:
723e974f91cSConrad Meyer 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
7245ac77963SConrad Meyer 
7255ac77963SConrad Meyer 	/* Perform a racy check first; only take the locks if it passes. */
7265ac77963SConrad Meyer 	pending = (ioat_get_active(ioat) != 0);
7275ac77963SConrad Meyer 	if (!pending && ioat->is_completion_pending) {
728e974f91cSConrad Meyer 		mtx_unlock(&ioat->cleanup_lock);
7295ac77963SConrad Meyer 		mtx_lock(&ioat->submit_lock);
7305ac77963SConrad Meyer 		mtx_lock(&ioat->cleanup_lock);
7315ac77963SConrad Meyer 
7325ac77963SConrad Meyer 		pending = (ioat_get_active(ioat) != 0);
7335ac77963SConrad Meyer 		if (!pending && ioat->is_completion_pending) {
7345ac77963SConrad Meyer 			ioat->is_completion_pending = FALSE;
7355ac77963SConrad Meyer 			callout_stop(&ioat->poll_timer);
7365ac77963SConrad Meyer 		}
7375ac77963SConrad Meyer 		mtx_unlock(&ioat->submit_lock);
7385ac77963SConrad Meyer 	}
7395ac77963SConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
7405ac77963SConrad Meyer 
7415ac77963SConrad Meyer 	if (pending)
7425ac77963SConrad Meyer 		callout_reset(&ioat->poll_timer, 1, ioat_poll_timer_callback,
7435ac77963SConrad Meyer 		    ioat);
744466b3540SConrad Meyer 
745007a7030SConrad Meyer 	if (completed != 0) {
746466b3540SConrad Meyer 		ioat_putn(ioat, completed, IOAT_ACTIVE_DESCR_REF);
747bf8553eaSConrad Meyer 		wakeup(&ioat->tail);
748007a7030SConrad Meyer 	}
749faefad9cSConrad Meyer 
750*ea9e23edSConrad Meyer 	/*
751*ea9e23edSConrad Meyer 	 * The device doesn't seem to reliably push suspend/halt statuses to
752*ea9e23edSConrad Meyer 	 * the channel completion memory address, so poll the device register
753*ea9e23edSConrad Meyer 	 * here.
754*ea9e23edSConrad Meyer 	 */
755*ea9e23edSConrad Meyer 	comp_update = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS;
756d2c55e5aSConrad Meyer 	if (!is_ioat_halted(comp_update) && !is_ioat_suspended(comp_update))
757faefad9cSConrad Meyer 		return;
758faefad9cSConrad Meyer 
75901fbbc88SConrad Meyer 	ioat->stats.channel_halts++;
76001fbbc88SConrad Meyer 
761faefad9cSConrad Meyer 	/*
762faefad9cSConrad Meyer 	 * Fatal programming error on this DMA channel.  Flush any outstanding
763faefad9cSConrad Meyer 	 * work with error status and restart the engine.
764faefad9cSConrad Meyer 	 */
765faefad9cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
766faefad9cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
767faefad9cSConrad Meyer 	ioat->quiescing = TRUE;
768faefad9cSConrad Meyer 
769faefad9cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
770985efa77SConrad Meyer 	if (1 <= g_ioat_debug_level)
771faefad9cSConrad Meyer 		ioat_halted_debug(ioat, chanerr);
77201fbbc88SConrad Meyer 	ioat->stats.last_halt_chanerr = chanerr;
773faefad9cSConrad Meyer 
774faefad9cSConrad Meyer 	while (ioat_get_active(ioat) > 0) {
775faefad9cSConrad Meyer 		desc = ioat_get_ring_entry(ioat, ioat->tail);
776faefad9cSConrad Meyer 		dmadesc = &desc->bus_dmadesc;
77705e4cffbSConrad Meyer 		CTR5(KTR_IOAT, "channel=%u completing desc idx %u (%p) err cb %p(%p)",
77805e4cffbSConrad Meyer 		    ioat->chan_idx, ioat->tail, dmadesc, dmadesc->callback_fn,
779520f6023SConrad Meyer 		    dmadesc->callback_arg);
780faefad9cSConrad Meyer 
781faefad9cSConrad Meyer 		if (dmadesc->callback_fn != NULL)
782faefad9cSConrad Meyer 			dmadesc->callback_fn(dmadesc->callback_arg,
783faefad9cSConrad Meyer 			    chanerr_to_errno(chanerr));
784faefad9cSConrad Meyer 
785faefad9cSConrad Meyer 		ioat_putn_locked(ioat, 1, IOAT_ACTIVE_DESCR_REF);
786faefad9cSConrad Meyer 		ioat->tail++;
78701fbbc88SConrad Meyer 		ioat->stats.descriptors_processed++;
78801fbbc88SConrad Meyer 		ioat->stats.descriptors_error++;
789faefad9cSConrad Meyer 	}
79005e4cffbSConrad Meyer 	CTR5(KTR_IOAT, "%s channel=%u head=%u tail=%u active=%u", __func__,
79105e4cffbSConrad Meyer 	    ioat->chan_idx, ioat->head, ioat->tail, ioat_get_active(ioat));
792faefad9cSConrad Meyer 
793c114e741SConrad Meyer 	if (ioat->is_completion_pending) {
794c114e741SConrad Meyer 		ioat->is_completion_pending = FALSE;
795c114e741SConrad Meyer 		callout_stop(&ioat->poll_timer);
796c114e741SConrad Meyer 	}
797c114e741SConrad Meyer 
798faefad9cSConrad Meyer 	/* Clear error status */
799faefad9cSConrad Meyer 	ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
800faefad9cSConrad Meyer 
801faefad9cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
802faefad9cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
803faefad9cSConrad Meyer 
804faefad9cSConrad Meyer 	ioat_log_message(0, "Resetting channel to recover from error\n");
805374b05e1SConrad Meyer 	error = taskqueue_enqueue(taskqueue_thread, &ioat->reset_task);
806374b05e1SConrad Meyer 	KASSERT(error == 0,
807374b05e1SConrad Meyer 	    ("%s: taskqueue_enqueue failed: %d", __func__, error));
808374b05e1SConrad Meyer }
809374b05e1SConrad Meyer 
810374b05e1SConrad Meyer static void
811374b05e1SConrad Meyer ioat_reset_hw_task(void *ctx, int pending __unused)
812374b05e1SConrad Meyer {
813374b05e1SConrad Meyer 	struct ioat_softc *ioat;
814374b05e1SConrad Meyer 	int error;
815374b05e1SConrad Meyer 
816374b05e1SConrad Meyer 	ioat = ctx;
817374b05e1SConrad Meyer 	ioat_log_message(1, "%s: Resetting channel\n", __func__);
818374b05e1SConrad Meyer 
819faefad9cSConrad Meyer 	error = ioat_reset_hw(ioat);
820faefad9cSConrad Meyer 	KASSERT(error == 0, ("%s: reset failed: %d", __func__, error));
821374b05e1SConrad Meyer 	(void)error;
822e974f91cSConrad Meyer }
823e974f91cSConrad Meyer 
824e974f91cSConrad Meyer /*
825e974f91cSConrad Meyer  * User API functions
826e974f91cSConrad Meyer  */
827f8f92e91SConrad Meyer unsigned
828f8f92e91SConrad Meyer ioat_get_nchannels(void)
829f8f92e91SConrad Meyer {
830f8f92e91SConrad Meyer 
831f8f92e91SConrad Meyer 	return (ioat_channel_index);
832f8f92e91SConrad Meyer }
833f8f92e91SConrad Meyer 
834e974f91cSConrad Meyer bus_dmaengine_t
8350ff814e8SConrad Meyer ioat_get_dmaengine(uint32_t index, int flags)
836e974f91cSConrad Meyer {
8370ff814e8SConrad Meyer 	struct ioat_softc *ioat;
8380ff814e8SConrad Meyer 
8390ff814e8SConrad Meyer 	KASSERT((flags & ~(M_NOWAIT | M_WAITOK)) == 0,
8400ff814e8SConrad Meyer 	    ("invalid flags: 0x%08x", flags));
8410ff814e8SConrad Meyer 	KASSERT((flags & (M_NOWAIT | M_WAITOK)) != (M_NOWAIT | M_WAITOK),
8420ff814e8SConrad Meyer 	    ("invalid wait | nowait"));
843e974f91cSConrad Meyer 
844466b3540SConrad Meyer 	if (index >= ioat_channel_index)
845e974f91cSConrad Meyer 		return (NULL);
8465f77bd3eSConrad Meyer 
8470ff814e8SConrad Meyer 	ioat = ioat_channel[index];
8480ff814e8SConrad Meyer 	if (ioat == NULL || ioat->destroying)
8495f77bd3eSConrad Meyer 		return (NULL);
8505f77bd3eSConrad Meyer 
8510ff814e8SConrad Meyer 	if (ioat->quiescing) {
8520ff814e8SConrad Meyer 		if ((flags & M_NOWAIT) != 0)
8530ff814e8SConrad Meyer 			return (NULL);
8540ff814e8SConrad Meyer 
8550ff814e8SConrad Meyer 		mtx_lock(IOAT_REFLK);
8560ff814e8SConrad Meyer 		while (ioat->quiescing && !ioat->destroying)
8570ff814e8SConrad Meyer 			msleep(&ioat->quiescing, IOAT_REFLK, 0, "getdma", 0);
8580ff814e8SConrad Meyer 		mtx_unlock(IOAT_REFLK);
8590ff814e8SConrad Meyer 
8600ff814e8SConrad Meyer 		if (ioat->destroying)
8610ff814e8SConrad Meyer 			return (NULL);
8620ff814e8SConrad Meyer 	}
8630ff814e8SConrad Meyer 
8640ff814e8SConrad Meyer 	/*
8650ff814e8SConrad Meyer 	 * There's a race here between the quiescing check and HW reset or
8660ff814e8SConrad Meyer 	 * module destroy.
8670ff814e8SConrad Meyer 	 */
8680ff814e8SConrad Meyer 	return (&ioat_get(ioat, IOAT_DMAENGINE_REF)->dmaengine);
869466b3540SConrad Meyer }
870466b3540SConrad Meyer 
871466b3540SConrad Meyer void
872466b3540SConrad Meyer ioat_put_dmaengine(bus_dmaengine_t dmaengine)
873466b3540SConrad Meyer {
874466b3540SConrad Meyer 	struct ioat_softc *ioat;
875466b3540SConrad Meyer 
876466b3540SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
877466b3540SConrad Meyer 	ioat_put(ioat, IOAT_DMAENGINE_REF);
878e974f91cSConrad Meyer }
879e974f91cSConrad Meyer 
8805ca9fc2aSConrad Meyer int
88131bf2875SConrad Meyer ioat_get_hwversion(bus_dmaengine_t dmaengine)
88231bf2875SConrad Meyer {
88331bf2875SConrad Meyer 	struct ioat_softc *ioat;
88431bf2875SConrad Meyer 
88531bf2875SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
88631bf2875SConrad Meyer 	return (ioat->version);
88731bf2875SConrad Meyer }
88831bf2875SConrad Meyer 
889bd81fe68SConrad Meyer size_t
890bd81fe68SConrad Meyer ioat_get_max_io_size(bus_dmaengine_t dmaengine)
891bd81fe68SConrad Meyer {
892bd81fe68SConrad Meyer 	struct ioat_softc *ioat;
893bd81fe68SConrad Meyer 
894bd81fe68SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
895bd81fe68SConrad Meyer 	return (ioat->max_xfer_size);
896bd81fe68SConrad Meyer }
897bd81fe68SConrad Meyer 
898f8253f1aSConrad Meyer uint32_t
899f8253f1aSConrad Meyer ioat_get_capabilities(bus_dmaengine_t dmaengine)
900f8253f1aSConrad Meyer {
901f8253f1aSConrad Meyer 	struct ioat_softc *ioat;
902f8253f1aSConrad Meyer 
903f8253f1aSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
904f8253f1aSConrad Meyer 	return (ioat->capabilities);
905f8253f1aSConrad Meyer }
906f8253f1aSConrad Meyer 
90731bf2875SConrad Meyer int
9085ca9fc2aSConrad Meyer ioat_set_interrupt_coalesce(bus_dmaengine_t dmaengine, uint16_t delay)
9095ca9fc2aSConrad Meyer {
9105ca9fc2aSConrad Meyer 	struct ioat_softc *ioat;
9115ca9fc2aSConrad Meyer 
9125ca9fc2aSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
9135ca9fc2aSConrad Meyer 	if (!ioat->intrdelay_supported)
9145ca9fc2aSConrad Meyer 		return (ENODEV);
9155ca9fc2aSConrad Meyer 	if (delay > ioat->intrdelay_max)
9165ca9fc2aSConrad Meyer 		return (ERANGE);
9175ca9fc2aSConrad Meyer 
9185ca9fc2aSConrad Meyer 	ioat_write_2(ioat, IOAT_INTRDELAY_OFFSET, delay);
9195ca9fc2aSConrad Meyer 	ioat->cached_intrdelay =
9205ca9fc2aSConrad Meyer 	    ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) & IOAT_INTRDELAY_US_MASK;
9215ca9fc2aSConrad Meyer 	return (0);
9225ca9fc2aSConrad Meyer }
9235ca9fc2aSConrad Meyer 
9245ca9fc2aSConrad Meyer uint16_t
9255ca9fc2aSConrad Meyer ioat_get_max_coalesce_period(bus_dmaengine_t dmaengine)
9265ca9fc2aSConrad Meyer {
9275ca9fc2aSConrad Meyer 	struct ioat_softc *ioat;
9285ca9fc2aSConrad Meyer 
9295ca9fc2aSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
9305ca9fc2aSConrad Meyer 	return (ioat->intrdelay_max);
9315ca9fc2aSConrad Meyer }
9325ca9fc2aSConrad Meyer 
933e974f91cSConrad Meyer void
934e974f91cSConrad Meyer ioat_acquire(bus_dmaengine_t dmaengine)
935e974f91cSConrad Meyer {
936e974f91cSConrad Meyer 	struct ioat_softc *ioat;
937e974f91cSConrad Meyer 
938e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
939e974f91cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
940520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
941e974f91cSConrad Meyer }
942e974f91cSConrad Meyer 
9431502e363SConrad Meyer int
9441502e363SConrad Meyer ioat_acquire_reserve(bus_dmaengine_t dmaengine, unsigned n, int mflags)
9451502e363SConrad Meyer {
9461502e363SConrad Meyer 	struct ioat_softc *ioat;
9471502e363SConrad Meyer 	int error;
9481502e363SConrad Meyer 
9491502e363SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
9501502e363SConrad Meyer 	ioat_acquire(dmaengine);
9511502e363SConrad Meyer 
9521502e363SConrad Meyer 	error = ioat_reserve_space(ioat, n, mflags);
9531502e363SConrad Meyer 	if (error != 0)
9541502e363SConrad Meyer 		ioat_release(dmaengine);
9551502e363SConrad Meyer 	return (error);
9561502e363SConrad Meyer }
9571502e363SConrad Meyer 
958e974f91cSConrad Meyer void
959e974f91cSConrad Meyer ioat_release(bus_dmaengine_t dmaengine)
960e974f91cSConrad Meyer {
961e974f91cSConrad Meyer 	struct ioat_softc *ioat;
962e974f91cSConrad Meyer 
963e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
96476305bb8SConrad Meyer 	CTR4(KTR_IOAT, "%s channel=%u dispatch1 hw_head=%u head=%u", __func__,
96576305bb8SConrad Meyer 	    ioat->chan_idx, ioat->hw_head & UINT16_MAX, ioat->head);
96676305bb8SConrad Meyer 	KFAIL_POINT_CODE(DEBUG_FP, ioat_release, /* do nothing */);
96776305bb8SConrad Meyer 	CTR4(KTR_IOAT, "%s channel=%u dispatch2 hw_head=%u head=%u", __func__,
96876305bb8SConrad Meyer 	    ioat->chan_idx, ioat->hw_head & UINT16_MAX, ioat->head);
96976305bb8SConrad Meyer 
970bf8553eaSConrad Meyer 	ioat_write_2(ioat, IOAT_DMACOUNT_OFFSET, (uint16_t)ioat->hw_head);
971f44abf1fSConrad Meyer 
972f44abf1fSConrad Meyer 	if (!ioat->is_completion_pending) {
973f44abf1fSConrad Meyer 		ioat->is_completion_pending = TRUE;
974f44abf1fSConrad Meyer 		callout_reset(&ioat->poll_timer, 1, ioat_poll_timer_callback,
975f44abf1fSConrad Meyer 		    ioat);
976f44abf1fSConrad Meyer 	}
977e974f91cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
978e974f91cSConrad Meyer }
979e974f91cSConrad Meyer 
9809e3bbf26SConrad Meyer static struct ioat_descriptor *
9819e3bbf26SConrad Meyer ioat_op_generic(struct ioat_softc *ioat, uint8_t op,
9829e3bbf26SConrad Meyer     uint32_t size, uint64_t src, uint64_t dst,
9839e3bbf26SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg,
9849e3bbf26SConrad Meyer     uint32_t flags)
985e974f91cSConrad Meyer {
9869e3bbf26SConrad Meyer 	struct ioat_generic_hw_descriptor *hw_desc;
987e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
988bf8553eaSConrad Meyer 	int mflags;
989e974f91cSConrad Meyer 
9909e3bbf26SConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
9919e3bbf26SConrad Meyer 
992bec7ff79SConrad Meyer 	KASSERT((flags & ~_DMA_GENERIC_FLAGS) == 0,
993bec7ff79SConrad Meyer 	    ("Unrecognized flag(s): %#x", flags & ~_DMA_GENERIC_FLAGS));
994bf8553eaSConrad Meyer 	if ((flags & DMA_NO_WAIT) != 0)
995bf8553eaSConrad Meyer 		mflags = M_NOWAIT;
996bf8553eaSConrad Meyer 	else
997bf8553eaSConrad Meyer 		mflags = M_WAITOK;
998e974f91cSConrad Meyer 
9999e3bbf26SConrad Meyer 	if (size > ioat->max_xfer_size) {
10009e3bbf26SConrad Meyer 		ioat_log_message(0, "%s: max_xfer_size = %d, requested = %u\n",
10019e3bbf26SConrad Meyer 		    __func__, ioat->max_xfer_size, (unsigned)size);
10029e3bbf26SConrad Meyer 		return (NULL);
10039e3bbf26SConrad Meyer 	}
1004e974f91cSConrad Meyer 
1005bf8553eaSConrad Meyer 	if (ioat_reserve_space(ioat, 1, mflags) != 0)
1006e974f91cSConrad Meyer 		return (NULL);
1007e974f91cSConrad Meyer 
1008e974f91cSConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->head);
10098e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, ioat->head)->generic;
1010e974f91cSConrad Meyer 
1011e974f91cSConrad Meyer 	hw_desc->u.control_raw = 0;
10129e3bbf26SConrad Meyer 	hw_desc->u.control_generic.op = op;
10139e3bbf26SConrad Meyer 	hw_desc->u.control_generic.completion_update = 1;
1014e974f91cSConrad Meyer 
1015e974f91cSConrad Meyer 	if ((flags & DMA_INT_EN) != 0)
10169e3bbf26SConrad Meyer 		hw_desc->u.control_generic.int_enable = 1;
10176ca07079SConrad Meyer 	if ((flags & DMA_FENCE) != 0)
10186ca07079SConrad Meyer 		hw_desc->u.control_generic.fence = 1;
1019e974f91cSConrad Meyer 
10209e3bbf26SConrad Meyer 	hw_desc->size = size;
10219e3bbf26SConrad Meyer 	hw_desc->src_addr = src;
10229e3bbf26SConrad Meyer 	hw_desc->dest_addr = dst;
1023e974f91cSConrad Meyer 
1024e974f91cSConrad Meyer 	desc->bus_dmadesc.callback_fn = callback_fn;
1025e974f91cSConrad Meyer 	desc->bus_dmadesc.callback_arg = callback_arg;
10269e3bbf26SConrad Meyer 	return (desc);
10279e3bbf26SConrad Meyer }
1028e974f91cSConrad Meyer 
10299e3bbf26SConrad Meyer struct bus_dmadesc *
10309e3bbf26SConrad Meyer ioat_null(bus_dmaengine_t dmaengine, bus_dmaengine_callback_t callback_fn,
10319e3bbf26SConrad Meyer     void *callback_arg, uint32_t flags)
10329e3bbf26SConrad Meyer {
10339e3bbf26SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
10349e3bbf26SConrad Meyer 	struct ioat_descriptor *desc;
10359e3bbf26SConrad Meyer 	struct ioat_softc *ioat;
10369e3bbf26SConrad Meyer 
10379e3bbf26SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1038520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
10399e3bbf26SConrad Meyer 
10409e3bbf26SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, 8, 0, 0, callback_fn,
10419e3bbf26SConrad Meyer 	    callback_arg, flags);
10429e3bbf26SConrad Meyer 	if (desc == NULL)
10439e3bbf26SConrad Meyer 		return (NULL);
10449e3bbf26SConrad Meyer 
10458e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->dma;
10469e3bbf26SConrad Meyer 	hw_desc->u.control.null = 1;
1047e974f91cSConrad Meyer 	ioat_submit_single(ioat);
1048e974f91cSConrad Meyer 	return (&desc->bus_dmadesc);
1049e974f91cSConrad Meyer }
1050e974f91cSConrad Meyer 
1051e974f91cSConrad Meyer struct bus_dmadesc *
1052e974f91cSConrad Meyer ioat_copy(bus_dmaengine_t dmaengine, bus_addr_t dst,
1053e974f91cSConrad Meyer     bus_addr_t src, bus_size_t len, bus_dmaengine_callback_t callback_fn,
1054e974f91cSConrad Meyer     void *callback_arg, uint32_t flags)
1055e974f91cSConrad Meyer {
1056e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
10579e3bbf26SConrad Meyer 	struct ioat_descriptor *desc;
1058e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1059e974f91cSConrad Meyer 
1060e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1061e974f91cSConrad Meyer 
10629e3bbf26SConrad Meyer 	if (((src | dst) & (0xffffull << 48)) != 0) {
10639e3bbf26SConrad Meyer 		ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n",
10649e3bbf26SConrad Meyer 		    __func__);
1065e974f91cSConrad Meyer 		return (NULL);
1066e974f91cSConrad Meyer 	}
1067e974f91cSConrad Meyer 
10689e3bbf26SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, len, src, dst, callback_fn,
10699e3bbf26SConrad Meyer 	    callback_arg, flags);
10709e3bbf26SConrad Meyer 	if (desc == NULL)
1071e974f91cSConrad Meyer 		return (NULL);
1072e974f91cSConrad Meyer 
10738e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->dma;
1074e974f91cSConrad Meyer 	if (g_ioat_debug_level >= 3)
1075e974f91cSConrad Meyer 		dump_descriptor(hw_desc);
1076e974f91cSConrad Meyer 
1077e974f91cSConrad Meyer 	ioat_submit_single(ioat);
107805e4cffbSConrad Meyer 	CTR6(KTR_IOAT, "%s channel=%u desc=%p dest=%lx src=%lx len=%lx",
107905e4cffbSConrad Meyer 	    __func__, ioat->chan_idx, &desc->bus_dmadesc, dst, src, len);
1080e974f91cSConrad Meyer 	return (&desc->bus_dmadesc);
1081e974f91cSConrad Meyer }
1082e974f91cSConrad Meyer 
10832a4fd6b1SConrad Meyer struct bus_dmadesc *
10849950fde0SConrad Meyer ioat_copy_8k_aligned(bus_dmaengine_t dmaengine, bus_addr_t dst1,
10859950fde0SConrad Meyer     bus_addr_t dst2, bus_addr_t src1, bus_addr_t src2,
10869950fde0SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
10879950fde0SConrad Meyer {
10889950fde0SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
10899950fde0SConrad Meyer 	struct ioat_descriptor *desc;
10909950fde0SConrad Meyer 	struct ioat_softc *ioat;
10919950fde0SConrad Meyer 
10929950fde0SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1093520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
10949950fde0SConrad Meyer 
10959950fde0SConrad Meyer 	if (((src1 | src2 | dst1 | dst2) & (0xffffull << 48)) != 0) {
10969950fde0SConrad Meyer 		ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n",
10979950fde0SConrad Meyer 		    __func__);
10989950fde0SConrad Meyer 		return (NULL);
10999950fde0SConrad Meyer 	}
11009950fde0SConrad Meyer 	if (((src1 | src2 | dst1 | dst2) & PAGE_MASK) != 0) {
11019950fde0SConrad Meyer 		ioat_log_message(0, "%s: Addresses must be page-aligned\n",
11029950fde0SConrad Meyer 		    __func__);
11039950fde0SConrad Meyer 		return (NULL);
11049950fde0SConrad Meyer 	}
11059950fde0SConrad Meyer 
11069950fde0SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, 2 * PAGE_SIZE, src1, dst1,
11079950fde0SConrad Meyer 	    callback_fn, callback_arg, flags);
11089950fde0SConrad Meyer 	if (desc == NULL)
11099950fde0SConrad Meyer 		return (NULL);
11109950fde0SConrad Meyer 
11118e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->dma;
11129950fde0SConrad Meyer 	if (src2 != src1 + PAGE_SIZE) {
11139950fde0SConrad Meyer 		hw_desc->u.control.src_page_break = 1;
11149950fde0SConrad Meyer 		hw_desc->next_src_addr = src2;
11159950fde0SConrad Meyer 	}
11169950fde0SConrad Meyer 	if (dst2 != dst1 + PAGE_SIZE) {
11179950fde0SConrad Meyer 		hw_desc->u.control.dest_page_break = 1;
11189950fde0SConrad Meyer 		hw_desc->next_dest_addr = dst2;
11199950fde0SConrad Meyer 	}
11209950fde0SConrad Meyer 
11219950fde0SConrad Meyer 	if (g_ioat_debug_level >= 3)
11229950fde0SConrad Meyer 		dump_descriptor(hw_desc);
11239950fde0SConrad Meyer 
11249950fde0SConrad Meyer 	ioat_submit_single(ioat);
11259950fde0SConrad Meyer 	return (&desc->bus_dmadesc);
11269950fde0SConrad Meyer }
11279950fde0SConrad Meyer 
11289950fde0SConrad Meyer struct bus_dmadesc *
1129bec7ff79SConrad Meyer ioat_copy_crc(bus_dmaengine_t dmaengine, bus_addr_t dst, bus_addr_t src,
1130bec7ff79SConrad Meyer     bus_size_t len, uint32_t *initialseed, bus_addr_t crcptr,
1131bec7ff79SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
1132bec7ff79SConrad Meyer {
1133bec7ff79SConrad Meyer 	struct ioat_crc32_hw_descriptor *hw_desc;
1134bec7ff79SConrad Meyer 	struct ioat_descriptor *desc;
1135bec7ff79SConrad Meyer 	struct ioat_softc *ioat;
1136bec7ff79SConrad Meyer 	uint32_t teststore;
1137bec7ff79SConrad Meyer 	uint8_t op;
1138bec7ff79SConrad Meyer 
1139bec7ff79SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1140520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1141bec7ff79SConrad Meyer 
1142bec7ff79SConrad Meyer 	if ((ioat->capabilities & IOAT_DMACAP_MOVECRC) == 0) {
1143bec7ff79SConrad Meyer 		ioat_log_message(0, "%s: Device lacks MOVECRC capability\n",
1144bec7ff79SConrad Meyer 		    __func__);
1145bec7ff79SConrad Meyer 		return (NULL);
1146bec7ff79SConrad Meyer 	}
1147bec7ff79SConrad Meyer 	if (((src | dst) & (0xffffffull << 40)) != 0) {
1148bec7ff79SConrad Meyer 		ioat_log_message(0, "%s: High 24 bits of src/dst invalid\n",
1149bec7ff79SConrad Meyer 		    __func__);
1150bec7ff79SConrad Meyer 		return (NULL);
1151bec7ff79SConrad Meyer 	}
1152bec7ff79SConrad Meyer 	teststore = (flags & _DMA_CRC_TESTSTORE);
1153bec7ff79SConrad Meyer 	if (teststore == _DMA_CRC_TESTSTORE) {
1154bec7ff79SConrad Meyer 		ioat_log_message(0, "%s: TEST and STORE invalid\n", __func__);
1155bec7ff79SConrad Meyer 		return (NULL);
1156bec7ff79SConrad Meyer 	}
1157bec7ff79SConrad Meyer 	if (teststore == 0 && (flags & DMA_CRC_INLINE) != 0) {
1158bec7ff79SConrad Meyer 		ioat_log_message(0, "%s: INLINE invalid without TEST or STORE\n",
1159bec7ff79SConrad Meyer 		    __func__);
1160bec7ff79SConrad Meyer 		return (NULL);
1161bec7ff79SConrad Meyer 	}
1162bec7ff79SConrad Meyer 
1163bec7ff79SConrad Meyer 	switch (teststore) {
1164bec7ff79SConrad Meyer 	case DMA_CRC_STORE:
1165bec7ff79SConrad Meyer 		op = IOAT_OP_MOVECRC_STORE;
1166bec7ff79SConrad Meyer 		break;
1167bec7ff79SConrad Meyer 	case DMA_CRC_TEST:
1168bec7ff79SConrad Meyer 		op = IOAT_OP_MOVECRC_TEST;
1169bec7ff79SConrad Meyer 		break;
1170bec7ff79SConrad Meyer 	default:
1171bec7ff79SConrad Meyer 		KASSERT(teststore == 0, ("bogus"));
1172bec7ff79SConrad Meyer 		op = IOAT_OP_MOVECRC;
1173bec7ff79SConrad Meyer 		break;
1174bec7ff79SConrad Meyer 	}
1175bec7ff79SConrad Meyer 
1176bec7ff79SConrad Meyer 	if ((flags & DMA_CRC_INLINE) == 0 &&
1177bec7ff79SConrad Meyer 	    (crcptr & (0xffffffull << 40)) != 0) {
1178bec7ff79SConrad Meyer 		ioat_log_message(0,
1179bec7ff79SConrad Meyer 		    "%s: High 24 bits of crcptr invalid\n", __func__);
1180bec7ff79SConrad Meyer 		return (NULL);
1181bec7ff79SConrad Meyer 	}
1182bec7ff79SConrad Meyer 
1183bec7ff79SConrad Meyer 	desc = ioat_op_generic(ioat, op, len, src, dst, callback_fn,
1184bec7ff79SConrad Meyer 	    callback_arg, flags & ~_DMA_CRC_FLAGS);
1185bec7ff79SConrad Meyer 	if (desc == NULL)
1186bec7ff79SConrad Meyer 		return (NULL);
1187bec7ff79SConrad Meyer 
11888e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->crc32;
1189bec7ff79SConrad Meyer 
1190bec7ff79SConrad Meyer 	if ((flags & DMA_CRC_INLINE) == 0)
1191bec7ff79SConrad Meyer 		hw_desc->crc_address = crcptr;
1192bec7ff79SConrad Meyer 	else
1193bec7ff79SConrad Meyer 		hw_desc->u.control.crc_location = 1;
1194bec7ff79SConrad Meyer 
1195bec7ff79SConrad Meyer 	if (initialseed != NULL) {
1196bec7ff79SConrad Meyer 		hw_desc->u.control.use_seed = 1;
1197bec7ff79SConrad Meyer 		hw_desc->seed = *initialseed;
1198bec7ff79SConrad Meyer 	}
1199bec7ff79SConrad Meyer 
1200bec7ff79SConrad Meyer 	if (g_ioat_debug_level >= 3)
1201bec7ff79SConrad Meyer 		dump_descriptor(hw_desc);
1202bec7ff79SConrad Meyer 
1203bec7ff79SConrad Meyer 	ioat_submit_single(ioat);
1204bec7ff79SConrad Meyer 	return (&desc->bus_dmadesc);
1205bec7ff79SConrad Meyer }
1206bec7ff79SConrad Meyer 
1207bec7ff79SConrad Meyer struct bus_dmadesc *
1208bec7ff79SConrad Meyer ioat_crc(bus_dmaengine_t dmaengine, bus_addr_t src, bus_size_t len,
1209bec7ff79SConrad Meyer     uint32_t *initialseed, bus_addr_t crcptr,
1210bec7ff79SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
1211bec7ff79SConrad Meyer {
1212bec7ff79SConrad Meyer 	struct ioat_crc32_hw_descriptor *hw_desc;
1213bec7ff79SConrad Meyer 	struct ioat_descriptor *desc;
1214bec7ff79SConrad Meyer 	struct ioat_softc *ioat;
1215bec7ff79SConrad Meyer 	uint32_t teststore;
1216bec7ff79SConrad Meyer 	uint8_t op;
1217bec7ff79SConrad Meyer 
1218bec7ff79SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1219520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1220bec7ff79SConrad Meyer 
1221bec7ff79SConrad Meyer 	if ((ioat->capabilities & IOAT_DMACAP_CRC) == 0) {
1222bec7ff79SConrad Meyer 		ioat_log_message(0, "%s: Device lacks CRC capability\n",
1223bec7ff79SConrad Meyer 		    __func__);
1224bec7ff79SConrad Meyer 		return (NULL);
1225bec7ff79SConrad Meyer 	}
1226bec7ff79SConrad Meyer 	if ((src & (0xffffffull << 40)) != 0) {
1227bec7ff79SConrad Meyer 		ioat_log_message(0, "%s: High 24 bits of src invalid\n",
1228bec7ff79SConrad Meyer 		    __func__);
1229bec7ff79SConrad Meyer 		return (NULL);
1230bec7ff79SConrad Meyer 	}
1231bec7ff79SConrad Meyer 	teststore = (flags & _DMA_CRC_TESTSTORE);
1232bec7ff79SConrad Meyer 	if (teststore == _DMA_CRC_TESTSTORE) {
1233bec7ff79SConrad Meyer 		ioat_log_message(0, "%s: TEST and STORE invalid\n", __func__);
1234bec7ff79SConrad Meyer 		return (NULL);
1235bec7ff79SConrad Meyer 	}
1236bec7ff79SConrad Meyer 	if (teststore == 0 && (flags & DMA_CRC_INLINE) != 0) {
1237bec7ff79SConrad Meyer 		ioat_log_message(0, "%s: INLINE invalid without TEST or STORE\n",
1238bec7ff79SConrad Meyer 		    __func__);
1239bec7ff79SConrad Meyer 		return (NULL);
1240bec7ff79SConrad Meyer 	}
1241bec7ff79SConrad Meyer 
1242bec7ff79SConrad Meyer 	switch (teststore) {
1243bec7ff79SConrad Meyer 	case DMA_CRC_STORE:
1244bec7ff79SConrad Meyer 		op = IOAT_OP_CRC_STORE;
1245bec7ff79SConrad Meyer 		break;
1246bec7ff79SConrad Meyer 	case DMA_CRC_TEST:
1247bec7ff79SConrad Meyer 		op = IOAT_OP_CRC_TEST;
1248bec7ff79SConrad Meyer 		break;
1249bec7ff79SConrad Meyer 	default:
1250bec7ff79SConrad Meyer 		KASSERT(teststore == 0, ("bogus"));
1251bec7ff79SConrad Meyer 		op = IOAT_OP_CRC;
1252bec7ff79SConrad Meyer 		break;
1253bec7ff79SConrad Meyer 	}
1254bec7ff79SConrad Meyer 
1255bec7ff79SConrad Meyer 	if ((flags & DMA_CRC_INLINE) == 0 &&
1256bec7ff79SConrad Meyer 	    (crcptr & (0xffffffull << 40)) != 0) {
1257bec7ff79SConrad Meyer 		ioat_log_message(0,
1258bec7ff79SConrad Meyer 		    "%s: High 24 bits of crcptr invalid\n", __func__);
1259bec7ff79SConrad Meyer 		return (NULL);
1260bec7ff79SConrad Meyer 	}
1261bec7ff79SConrad Meyer 
1262bec7ff79SConrad Meyer 	desc = ioat_op_generic(ioat, op, len, src, 0, callback_fn,
1263bec7ff79SConrad Meyer 	    callback_arg, flags & ~_DMA_CRC_FLAGS);
1264bec7ff79SConrad Meyer 	if (desc == NULL)
1265bec7ff79SConrad Meyer 		return (NULL);
1266bec7ff79SConrad Meyer 
12678e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->crc32;
1268bec7ff79SConrad Meyer 
1269bec7ff79SConrad Meyer 	if ((flags & DMA_CRC_INLINE) == 0)
1270bec7ff79SConrad Meyer 		hw_desc->crc_address = crcptr;
1271bec7ff79SConrad Meyer 	else
1272bec7ff79SConrad Meyer 		hw_desc->u.control.crc_location = 1;
1273bec7ff79SConrad Meyer 
1274bec7ff79SConrad Meyer 	if (initialseed != NULL) {
1275bec7ff79SConrad Meyer 		hw_desc->u.control.use_seed = 1;
1276bec7ff79SConrad Meyer 		hw_desc->seed = *initialseed;
1277bec7ff79SConrad Meyer 	}
1278bec7ff79SConrad Meyer 
1279bec7ff79SConrad Meyer 	if (g_ioat_debug_level >= 3)
1280bec7ff79SConrad Meyer 		dump_descriptor(hw_desc);
1281bec7ff79SConrad Meyer 
1282bec7ff79SConrad Meyer 	ioat_submit_single(ioat);
1283bec7ff79SConrad Meyer 	return (&desc->bus_dmadesc);
1284bec7ff79SConrad Meyer }
1285bec7ff79SConrad Meyer 
1286bec7ff79SConrad Meyer struct bus_dmadesc *
12872a4fd6b1SConrad Meyer ioat_blockfill(bus_dmaengine_t dmaengine, bus_addr_t dst, uint64_t fillpattern,
12882a4fd6b1SConrad Meyer     bus_size_t len, bus_dmaengine_callback_t callback_fn, void *callback_arg,
12892a4fd6b1SConrad Meyer     uint32_t flags)
12902a4fd6b1SConrad Meyer {
12912a4fd6b1SConrad Meyer 	struct ioat_fill_hw_descriptor *hw_desc;
12922a4fd6b1SConrad Meyer 	struct ioat_descriptor *desc;
12932a4fd6b1SConrad Meyer 	struct ioat_softc *ioat;
12942a4fd6b1SConrad Meyer 
12952a4fd6b1SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1296520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
12972a4fd6b1SConrad Meyer 
12981693d27bSConrad Meyer 	if ((ioat->capabilities & IOAT_DMACAP_BFILL) == 0) {
12991693d27bSConrad Meyer 		ioat_log_message(0, "%s: Device lacks BFILL capability\n",
13001693d27bSConrad Meyer 		    __func__);
13011693d27bSConrad Meyer 		return (NULL);
13021693d27bSConrad Meyer 	}
13031693d27bSConrad Meyer 
13042a4fd6b1SConrad Meyer 	if ((dst & (0xffffull << 48)) != 0) {
13052a4fd6b1SConrad Meyer 		ioat_log_message(0, "%s: High 16 bits of dst invalid\n",
13062a4fd6b1SConrad Meyer 		    __func__);
13072a4fd6b1SConrad Meyer 		return (NULL);
13082a4fd6b1SConrad Meyer 	}
13092a4fd6b1SConrad Meyer 
13102a4fd6b1SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_FILL, len, fillpattern, dst,
13112a4fd6b1SConrad Meyer 	    callback_fn, callback_arg, flags);
13122a4fd6b1SConrad Meyer 	if (desc == NULL)
13132a4fd6b1SConrad Meyer 		return (NULL);
13142a4fd6b1SConrad Meyer 
13158e269d99SConrad Meyer 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->fill;
13162a4fd6b1SConrad Meyer 	if (g_ioat_debug_level >= 3)
13172a4fd6b1SConrad Meyer 		dump_descriptor(hw_desc);
13182a4fd6b1SConrad Meyer 
13192a4fd6b1SConrad Meyer 	ioat_submit_single(ioat);
13202a4fd6b1SConrad Meyer 	return (&desc->bus_dmadesc);
13212a4fd6b1SConrad Meyer }
13222a4fd6b1SConrad Meyer 
1323e974f91cSConrad Meyer /*
1324e974f91cSConrad Meyer  * Ring Management
1325e974f91cSConrad Meyer  */
1326e974f91cSConrad Meyer static inline uint32_t
1327e974f91cSConrad Meyer ioat_get_active(struct ioat_softc *ioat)
1328e974f91cSConrad Meyer {
1329e974f91cSConrad Meyer 
1330e974f91cSConrad Meyer 	return ((ioat->head - ioat->tail) & ((1 << ioat->ring_size_order) - 1));
1331e974f91cSConrad Meyer }
1332e974f91cSConrad Meyer 
1333e974f91cSConrad Meyer static inline uint32_t
1334e974f91cSConrad Meyer ioat_get_ring_space(struct ioat_softc *ioat)
1335e974f91cSConrad Meyer {
1336e974f91cSConrad Meyer 
1337e974f91cSConrad Meyer 	return ((1 << ioat->ring_size_order) - ioat_get_active(ioat) - 1);
1338e974f91cSConrad Meyer }
1339e974f91cSConrad Meyer 
1340bf8553eaSConrad Meyer /*
1341bf8553eaSConrad Meyer  * Reserves space in this IOAT descriptor ring by ensuring enough slots remain
1342bf8553eaSConrad Meyer  * for 'num_descs'.
1343bf8553eaSConrad Meyer  *
1344bf8553eaSConrad Meyer  * If mflags contains M_WAITOK, blocks until enough space is available.
1345bf8553eaSConrad Meyer  *
1346bf8553eaSConrad Meyer  * Returns zero on success, or an errno on error.  If num_descs is beyond the
1347bf8553eaSConrad Meyer  * maximum ring size, returns EINVAl; if allocation would block and mflags
1348bf8553eaSConrad Meyer  * contains M_NOWAIT, returns EAGAIN.
1349bf8553eaSConrad Meyer  *
1350bf8553eaSConrad Meyer  * Must be called with the submit_lock held; returns with the lock held.  The
1351bf8553eaSConrad Meyer  * lock may be dropped to allocate the ring.
1352bf8553eaSConrad Meyer  *
1353bf8553eaSConrad Meyer  * (The submit_lock is needed to add any entries to the ring, so callers are
1354bf8553eaSConrad Meyer  * assured enough room is available.)
1355bf8553eaSConrad Meyer  */
1356e974f91cSConrad Meyer static int
1357bf8553eaSConrad Meyer ioat_reserve_space(struct ioat_softc *ioat, uint32_t num_descs, int mflags)
1358e974f91cSConrad Meyer {
135925ad9585SConrad Meyer 	boolean_t dug;
1360bf8553eaSConrad Meyer 	int error;
1361e974f91cSConrad Meyer 
1362bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1363bf8553eaSConrad Meyer 	error = 0;
136425ad9585SConrad Meyer 	dug = FALSE;
1365e974f91cSConrad Meyer 
1366a0992979SConrad Meyer 	if (num_descs < 1 || num_descs >= (1 << ioat->ring_size_order)) {
1367bf8553eaSConrad Meyer 		error = EINVAL;
1368bf8553eaSConrad Meyer 		goto out;
1369e974f91cSConrad Meyer 	}
137008a6b96aSConrad Meyer 
137108a6b96aSConrad Meyer 	for (;;) {
13725f77bd3eSConrad Meyer 		if (ioat->quiescing) {
13735f77bd3eSConrad Meyer 			error = ENXIO;
13745f77bd3eSConrad Meyer 			goto out;
13755f77bd3eSConrad Meyer 		}
1376bf8553eaSConrad Meyer 
1377bf8553eaSConrad Meyer 		if (ioat_get_ring_space(ioat) >= num_descs)
1378bf8553eaSConrad Meyer 			goto out;
1379bf8553eaSConrad Meyer 
138005e4cffbSConrad Meyer 		CTR3(KTR_IOAT, "%s channel=%u starved (%u)", __func__,
138105e4cffbSConrad Meyer 		    ioat->chan_idx, num_descs);
138205e4cffbSConrad Meyer 
1383a0992979SConrad Meyer 		if (!dug && !ioat->is_submitter_processing) {
138425ad9585SConrad Meyer 			ioat->is_submitter_processing = TRUE;
138525ad9585SConrad Meyer 			mtx_unlock(&ioat->submit_lock);
138625ad9585SConrad Meyer 
138705e4cffbSConrad Meyer 			CTR2(KTR_IOAT, "%s channel=%u attempting to process events",
138805e4cffbSConrad Meyer 			    __func__, ioat->chan_idx);
138925ad9585SConrad Meyer 			ioat_process_events(ioat);
139025ad9585SConrad Meyer 
139125ad9585SConrad Meyer 			mtx_lock(&ioat->submit_lock);
139225ad9585SConrad Meyer 			dug = TRUE;
139325ad9585SConrad Meyer 			KASSERT(ioat->is_submitter_processing == TRUE,
139425ad9585SConrad Meyer 			    ("is_submitter_processing"));
139525ad9585SConrad Meyer 			ioat->is_submitter_processing = FALSE;
139625ad9585SConrad Meyer 			wakeup(&ioat->tail);
139725ad9585SConrad Meyer 			continue;
139825ad9585SConrad Meyer 		}
139925ad9585SConrad Meyer 
1400a0992979SConrad Meyer 		if ((mflags & M_WAITOK) == 0) {
1401a0992979SConrad Meyer 			error = EAGAIN;
1402a0992979SConrad Meyer 			break;
1403a0992979SConrad Meyer 		}
140405e4cffbSConrad Meyer 		CTR2(KTR_IOAT, "%s channel=%u blocking on completions",
140505e4cffbSConrad Meyer 		    __func__, ioat->chan_idx);
1406bf8553eaSConrad Meyer 		msleep(&ioat->tail, &ioat->submit_lock, 0,
1407a0992979SConrad Meyer 		    "ioat_full", 0);
1408bf8553eaSConrad Meyer 		continue;
1409bf8553eaSConrad Meyer 	}
1410bf8553eaSConrad Meyer 
1411bf8553eaSConrad Meyer out:
1412bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
141308a6b96aSConrad Meyer 	KASSERT(!ioat->quiescing || error == ENXIO,
141408a6b96aSConrad Meyer 	    ("reserved during quiesce"));
1415bf8553eaSConrad Meyer 	return (error);
1416bf8553eaSConrad Meyer }
1417bf8553eaSConrad Meyer 
1418bf8553eaSConrad Meyer static void
1419bf8553eaSConrad Meyer ioat_free_ring(struct ioat_softc *ioat, uint32_t size,
14208e269d99SConrad Meyer     struct ioat_descriptor *ring)
1421bf8553eaSConrad Meyer {
1422bf8553eaSConrad Meyer 
1423bf8553eaSConrad Meyer 	free(ring, M_IOAT);
1424e974f91cSConrad Meyer }
1425e974f91cSConrad Meyer 
1426e974f91cSConrad Meyer static struct ioat_descriptor *
1427e974f91cSConrad Meyer ioat_get_ring_entry(struct ioat_softc *ioat, uint32_t index)
1428e974f91cSConrad Meyer {
1429e974f91cSConrad Meyer 
14308e269d99SConrad Meyer 	return (&ioat->ring[index % (1 << ioat->ring_size_order)]);
14318e269d99SConrad Meyer }
14328e269d99SConrad Meyer 
14338e269d99SConrad Meyer static union ioat_hw_descriptor *
14348e269d99SConrad Meyer ioat_get_descriptor(struct ioat_softc *ioat, uint32_t index)
14358e269d99SConrad Meyer {
14368e269d99SConrad Meyer 
14378e269d99SConrad Meyer 	return (&ioat->hw_desc_ring[index % (1 << ioat->ring_size_order)]);
1438e974f91cSConrad Meyer }
1439e974f91cSConrad Meyer 
1440e974f91cSConrad Meyer static void
14418f274637SConrad Meyer ioat_halted_debug(struct ioat_softc *ioat, uint32_t chanerr)
1442e974f91cSConrad Meyer {
14438e269d99SConrad Meyer 	union ioat_hw_descriptor *desc;
14448f274637SConrad Meyer 
144559acd4baSConrad Meyer 	ioat_log_message(0, "Channel halted (%b)\n", (int)chanerr,
144659acd4baSConrad Meyer 	    IOAT_CHANERR_STR);
14478f274637SConrad Meyer 	if (chanerr == 0)
14488f274637SConrad Meyer 		return;
14498f274637SConrad Meyer 
1450faefad9cSConrad Meyer 	mtx_assert(&ioat->cleanup_lock, MA_OWNED);
1451faefad9cSConrad Meyer 
14528e269d99SConrad Meyer 	desc = ioat_get_descriptor(ioat, ioat->tail + 0);
14538e269d99SConrad Meyer 	dump_descriptor(desc);
14548f274637SConrad Meyer 
14558e269d99SConrad Meyer 	desc = ioat_get_descriptor(ioat, ioat->tail + 1);
14568e269d99SConrad Meyer 	dump_descriptor(desc);
14578f274637SConrad Meyer }
14588f274637SConrad Meyer 
14598f274637SConrad Meyer static void
14605ac77963SConrad Meyer ioat_poll_timer_callback(void *arg)
14615ac77963SConrad Meyer {
14625ac77963SConrad Meyer 	struct ioat_softc *ioat;
14635ac77963SConrad Meyer 
14645ac77963SConrad Meyer 	ioat = arg;
14655ac77963SConrad Meyer 	ioat_log_message(3, "%s\n", __func__);
14665ac77963SConrad Meyer 
14675ac77963SConrad Meyer 	ioat_process_events(ioat);
14685ac77963SConrad Meyer }
14695ac77963SConrad Meyer 
1470e974f91cSConrad Meyer /*
1471e974f91cSConrad Meyer  * Support Functions
1472e974f91cSConrad Meyer  */
1473e974f91cSConrad Meyer static void
1474e974f91cSConrad Meyer ioat_submit_single(struct ioat_softc *ioat)
1475e974f91cSConrad Meyer {
1476e974f91cSConrad Meyer 
14775a8af401SConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
14785a8af401SConrad Meyer 
1479466b3540SConrad Meyer 	ioat_get(ioat, IOAT_ACTIVE_DESCR_REF);
1480e974f91cSConrad Meyer 	atomic_add_rel_int(&ioat->head, 1);
1481bf8553eaSConrad Meyer 	atomic_add_rel_int(&ioat->hw_head, 1);
148205e4cffbSConrad Meyer 	CTR5(KTR_IOAT, "%s channel=%u head=%u hw_head=%u tail=%u", __func__,
148305e4cffbSConrad Meyer 	    ioat->chan_idx, ioat->head, ioat->hw_head & UINT16_MAX,
148405e4cffbSConrad Meyer 	    ioat->tail);
1485e974f91cSConrad Meyer 
148601fbbc88SConrad Meyer 	ioat->stats.descriptors_submitted++;
1487e974f91cSConrad Meyer }
1488e974f91cSConrad Meyer 
1489e974f91cSConrad Meyer static int
1490e974f91cSConrad Meyer ioat_reset_hw(struct ioat_softc *ioat)
1491e974f91cSConrad Meyer {
1492e974f91cSConrad Meyer 	uint64_t status;
1493e974f91cSConrad Meyer 	uint32_t chanerr;
1494cea5b880SConrad Meyer 	unsigned timeout;
14955f77bd3eSConrad Meyer 	int error;
14965f77bd3eSConrad Meyer 
1497520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
14986d41e1edSConrad Meyer 
14995f77bd3eSConrad Meyer 	mtx_lock(IOAT_REFLK);
150093f7f84aSConrad Meyer 	while (ioat->resetting && !ioat->destroying)
150193f7f84aSConrad Meyer 		msleep(&ioat->resetting, IOAT_REFLK, 0, "IRH_drain", 0);
150293f7f84aSConrad Meyer 	if (ioat->destroying) {
150393f7f84aSConrad Meyer 		mtx_unlock(IOAT_REFLK);
150493f7f84aSConrad Meyer 		return (ENXIO);
150593f7f84aSConrad Meyer 	}
150693f7f84aSConrad Meyer 	ioat->resetting = TRUE;
150793f7f84aSConrad Meyer 
15085f77bd3eSConrad Meyer 	ioat->quiescing = TRUE;
15095f77bd3eSConrad Meyer 	ioat_drain_locked(ioat);
15105f77bd3eSConrad Meyer 	mtx_unlock(IOAT_REFLK);
1511e974f91cSConrad Meyer 
1512fe8712f8SConrad Meyer 	/*
1513fe8712f8SConrad Meyer 	 * Suspend ioat_process_events while the hardware and softc are in an
1514fe8712f8SConrad Meyer 	 * indeterminate state.
1515fe8712f8SConrad Meyer 	 */
1516fe8712f8SConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
1517fe8712f8SConrad Meyer 	ioat->resetting_cleanup = TRUE;
1518fe8712f8SConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
1519fe8712f8SConrad Meyer 
1520ee5642bbSConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u quiesced and drained", __func__,
1521ee5642bbSConrad Meyer 	    ioat->chan_idx);
1522ee5642bbSConrad Meyer 
1523e974f91cSConrad Meyer 	status = ioat_get_chansts(ioat);
1524e974f91cSConrad Meyer 	if (is_ioat_active(status) || is_ioat_idle(status))
1525e974f91cSConrad Meyer 		ioat_suspend(ioat);
1526e974f91cSConrad Meyer 
1527e974f91cSConrad Meyer 	/* Wait at most 20 ms */
1528e974f91cSConrad Meyer 	for (timeout = 0; (is_ioat_active(status) || is_ioat_idle(status)) &&
1529e974f91cSConrad Meyer 	    timeout < 20; timeout++) {
1530e974f91cSConrad Meyer 		DELAY(1000);
1531e974f91cSConrad Meyer 		status = ioat_get_chansts(ioat);
1532e974f91cSConrad Meyer 	}
15335f77bd3eSConrad Meyer 	if (timeout == 20) {
15345f77bd3eSConrad Meyer 		error = ETIMEDOUT;
15355f77bd3eSConrad Meyer 		goto out;
15365f77bd3eSConrad Meyer 	}
1537e974f91cSConrad Meyer 
1538cea5b880SConrad Meyer 	KASSERT(ioat_get_active(ioat) == 0, ("active after quiesce"));
1539cea5b880SConrad Meyer 
1540e974f91cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
1541e974f91cSConrad Meyer 	ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
1542e974f91cSConrad Meyer 
1543ee5642bbSConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u hardware suspended", __func__,
1544ee5642bbSConrad Meyer 	    ioat->chan_idx);
1545ee5642bbSConrad Meyer 
1546e974f91cSConrad Meyer 	/*
1547e974f91cSConrad Meyer 	 * IOAT v3 workaround - CHANERRMSK_INT with 3E07h to masks out errors
1548e974f91cSConrad Meyer 	 *  that can cause stability issues for IOAT v3.
1549e974f91cSConrad Meyer 	 */
1550e974f91cSConrad Meyer 	pci_write_config(ioat->device, IOAT_CFG_CHANERRMASK_INT_OFFSET, 0x3e07,
1551e974f91cSConrad Meyer 	    4);
1552e974f91cSConrad Meyer 	chanerr = pci_read_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, 4);
1553e974f91cSConrad Meyer 	pci_write_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, chanerr, 4);
1554e974f91cSConrad Meyer 
15550d1a05d9SConrad Meyer 	/*
15560d1a05d9SConrad Meyer 	 * BDXDE and BWD models reset MSI-X registers on device reset.
15570d1a05d9SConrad Meyer 	 * Save/restore their contents manually.
15580d1a05d9SConrad Meyer 	 */
1559f7157235SConrad Meyer 	if (ioat_model_resets_msix(ioat)) {
1560f7157235SConrad Meyer 		ioat_log_message(1, "device resets MSI-X registers; saving\n");
15610d1a05d9SConrad Meyer 		pci_save_state(ioat->device);
1562f7157235SConrad Meyer 	}
15630d1a05d9SConrad Meyer 
1564e974f91cSConrad Meyer 	ioat_reset(ioat);
1565ee5642bbSConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u hardware reset", __func__,
1566ee5642bbSConrad Meyer 	    ioat->chan_idx);
1567e974f91cSConrad Meyer 
1568e974f91cSConrad Meyer 	/* Wait at most 20 ms */
1569e974f91cSConrad Meyer 	for (timeout = 0; ioat_reset_pending(ioat) && timeout < 20; timeout++)
1570e974f91cSConrad Meyer 		DELAY(1000);
15715f77bd3eSConrad Meyer 	if (timeout == 20) {
15725f77bd3eSConrad Meyer 		error = ETIMEDOUT;
15735f77bd3eSConrad Meyer 		goto out;
15745f77bd3eSConrad Meyer 	}
1575e974f91cSConrad Meyer 
1576f7157235SConrad Meyer 	if (ioat_model_resets_msix(ioat)) {
1577f7157235SConrad Meyer 		ioat_log_message(1, "device resets registers; restored\n");
15780d1a05d9SConrad Meyer 		pci_restore_state(ioat->device);
1579f7157235SConrad Meyer 	}
15804253ea50SConrad Meyer 
1581cea5b880SConrad Meyer 	/* Reset attempts to return the hardware to "halted." */
1582cea5b880SConrad Meyer 	status = ioat_get_chansts(ioat);
1583cea5b880SConrad Meyer 	if (is_ioat_active(status) || is_ioat_idle(status)) {
1584cea5b880SConrad Meyer 		/* So this really shouldn't happen... */
1585cea5b880SConrad Meyer 		ioat_log_message(0, "Device is active after a reset?\n");
1586cea5b880SConrad Meyer 		ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
15875f77bd3eSConrad Meyer 		error = 0;
15885f77bd3eSConrad Meyer 		goto out;
1589e974f91cSConrad Meyer 	}
1590e974f91cSConrad Meyer 
1591cea5b880SConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
15925f77bd3eSConrad Meyer 	if (chanerr != 0) {
1593faefad9cSConrad Meyer 		mtx_lock(&ioat->cleanup_lock);
1594faefad9cSConrad Meyer 		ioat_halted_debug(ioat, chanerr);
1595faefad9cSConrad Meyer 		mtx_unlock(&ioat->cleanup_lock);
15965f77bd3eSConrad Meyer 		error = EIO;
15975f77bd3eSConrad Meyer 		goto out;
15985f77bd3eSConrad Meyer 	}
1599cea5b880SConrad Meyer 
1600cea5b880SConrad Meyer 	/*
1601cea5b880SConrad Meyer 	 * Bring device back online after reset.  Writing CHAINADDR brings the
1602cea5b880SConrad Meyer 	 * device back to active.
1603cea5b880SConrad Meyer 	 *
1604cea5b880SConrad Meyer 	 * The internal ring counter resets to zero, so we have to start over
1605cea5b880SConrad Meyer 	 * at zero as well.
1606cea5b880SConrad Meyer 	 */
1607bf8553eaSConrad Meyer 	ioat->tail = ioat->head = ioat->hw_head = 0;
1608cea5b880SConrad Meyer 	ioat->last_seen = 0;
1609fe8712f8SConrad Meyer 	*ioat->comp_update = 0;
1610c114e741SConrad Meyer 	KASSERT(!ioat->is_completion_pending, ("bogus completion_pending"));
1611cea5b880SConrad Meyer 
1612cea5b880SConrad Meyer 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
1613cea5b880SConrad Meyer 	ioat_write_chancmp(ioat, ioat->comp_update_bus_addr);
16148e269d99SConrad Meyer 	ioat_write_chainaddr(ioat, RING_PHYS_ADDR(ioat, 0));
16155f77bd3eSConrad Meyer 	error = 0;
1616ee5642bbSConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u configured channel", __func__,
1617ee5642bbSConrad Meyer 	    ioat->chan_idx);
16185f77bd3eSConrad Meyer 
16195f77bd3eSConrad Meyer out:
16201bbc06b8SConrad Meyer 	/* Enqueues a null operation and ensures it completes. */
1621ee5642bbSConrad Meyer 	if (error == 0) {
16221bbc06b8SConrad Meyer 		error = ioat_start_channel(ioat);
1623ee5642bbSConrad Meyer 		CTR2(KTR_IOAT, "%s channel=%u started channel", __func__,
1624ee5642bbSConrad Meyer 		    ioat->chan_idx);
1625ee5642bbSConrad Meyer 	}
16261bbc06b8SConrad Meyer 
1627fe8712f8SConrad Meyer 	/*
1628fe8712f8SConrad Meyer 	 * Resume completions now that ring state is consistent.
1629fe8712f8SConrad Meyer 	 */
1630fe8712f8SConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
1631fe8712f8SConrad Meyer 	ioat->resetting_cleanup = FALSE;
1632fe8712f8SConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
163393f7f84aSConrad Meyer 
1634fe8712f8SConrad Meyer 	/* Unblock submission of new work */
1635fe8712f8SConrad Meyer 	mtx_lock(IOAT_REFLK);
1636fe8712f8SConrad Meyer 	ioat->quiescing = FALSE;
1637fe8712f8SConrad Meyer 	wakeup(&ioat->quiescing);
1638fe8712f8SConrad Meyer 
1639fe8712f8SConrad Meyer 	ioat->resetting = FALSE;
1640fe8712f8SConrad Meyer 	wakeup(&ioat->resetting);
16411bbc06b8SConrad Meyer 
16421bbc06b8SConrad Meyer 	if (ioat->is_completion_pending)
16431bbc06b8SConrad Meyer 		callout_reset(&ioat->poll_timer, 1, ioat_poll_timer_callback,
16441bbc06b8SConrad Meyer 		    ioat);
1645ee5642bbSConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u reset done", __func__, ioat->chan_idx);
1646fe8712f8SConrad Meyer 	mtx_unlock(IOAT_REFLK);
1647fe8712f8SConrad Meyer 
16485f77bd3eSConrad Meyer 	return (error);
1649cea5b880SConrad Meyer }
1650cea5b880SConrad Meyer 
1651f7157235SConrad Meyer static int
1652faefad9cSConrad Meyer sysctl_handle_chansts(SYSCTL_HANDLER_ARGS)
1653faefad9cSConrad Meyer {
1654faefad9cSConrad Meyer 	struct ioat_softc *ioat;
1655faefad9cSConrad Meyer 	struct sbuf sb;
1656faefad9cSConrad Meyer 	uint64_t status;
1657faefad9cSConrad Meyer 	int error;
1658faefad9cSConrad Meyer 
1659faefad9cSConrad Meyer 	ioat = arg1;
1660faefad9cSConrad Meyer 
1661faefad9cSConrad Meyer 	status = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS;
1662faefad9cSConrad Meyer 
1663faefad9cSConrad Meyer 	sbuf_new_for_sysctl(&sb, NULL, 256, req);
1664faefad9cSConrad Meyer 	switch (status) {
1665faefad9cSConrad Meyer 	case IOAT_CHANSTS_ACTIVE:
1666faefad9cSConrad Meyer 		sbuf_printf(&sb, "ACTIVE");
1667faefad9cSConrad Meyer 		break;
1668faefad9cSConrad Meyer 	case IOAT_CHANSTS_IDLE:
1669faefad9cSConrad Meyer 		sbuf_printf(&sb, "IDLE");
1670faefad9cSConrad Meyer 		break;
1671faefad9cSConrad Meyer 	case IOAT_CHANSTS_SUSPENDED:
1672faefad9cSConrad Meyer 		sbuf_printf(&sb, "SUSPENDED");
1673faefad9cSConrad Meyer 		break;
1674faefad9cSConrad Meyer 	case IOAT_CHANSTS_HALTED:
1675faefad9cSConrad Meyer 		sbuf_printf(&sb, "HALTED");
1676faefad9cSConrad Meyer 		break;
1677faefad9cSConrad Meyer 	case IOAT_CHANSTS_ARMED:
1678faefad9cSConrad Meyer 		sbuf_printf(&sb, "ARMED");
1679faefad9cSConrad Meyer 		break;
1680faefad9cSConrad Meyer 	default:
1681faefad9cSConrad Meyer 		sbuf_printf(&sb, "UNKNOWN");
1682faefad9cSConrad Meyer 		break;
1683faefad9cSConrad Meyer 	}
1684faefad9cSConrad Meyer 	error = sbuf_finish(&sb);
1685faefad9cSConrad Meyer 	sbuf_delete(&sb);
1686faefad9cSConrad Meyer 
1687faefad9cSConrad Meyer 	if (error != 0 || req->newptr == NULL)
1688faefad9cSConrad Meyer 		return (error);
1689faefad9cSConrad Meyer 	return (EINVAL);
1690faefad9cSConrad Meyer }
1691faefad9cSConrad Meyer 
1692faefad9cSConrad Meyer static int
169301fbbc88SConrad Meyer sysctl_handle_dpi(SYSCTL_HANDLER_ARGS)
169401fbbc88SConrad Meyer {
169501fbbc88SConrad Meyer 	struct ioat_softc *ioat;
169601fbbc88SConrad Meyer 	struct sbuf sb;
169701fbbc88SConrad Meyer #define	PRECISION	"1"
169801fbbc88SConrad Meyer 	const uintmax_t factor = 10;
169901fbbc88SConrad Meyer 	uintmax_t rate;
170001fbbc88SConrad Meyer 	int error;
170101fbbc88SConrad Meyer 
170201fbbc88SConrad Meyer 	ioat = arg1;
170301fbbc88SConrad Meyer 	sbuf_new_for_sysctl(&sb, NULL, 16, req);
170401fbbc88SConrad Meyer 
170501fbbc88SConrad Meyer 	if (ioat->stats.interrupts == 0) {
170601fbbc88SConrad Meyer 		sbuf_printf(&sb, "NaN");
170701fbbc88SConrad Meyer 		goto out;
170801fbbc88SConrad Meyer 	}
170901fbbc88SConrad Meyer 	rate = ioat->stats.descriptors_processed * factor /
171001fbbc88SConrad Meyer 	    ioat->stats.interrupts;
171101fbbc88SConrad Meyer 	sbuf_printf(&sb, "%ju.%." PRECISION "ju", rate / factor,
171201fbbc88SConrad Meyer 	    rate % factor);
171301fbbc88SConrad Meyer #undef	PRECISION
171401fbbc88SConrad Meyer out:
171501fbbc88SConrad Meyer 	error = sbuf_finish(&sb);
171601fbbc88SConrad Meyer 	sbuf_delete(&sb);
171701fbbc88SConrad Meyer 	if (error != 0 || req->newptr == NULL)
171801fbbc88SConrad Meyer 		return (error);
171901fbbc88SConrad Meyer 	return (EINVAL);
172001fbbc88SConrad Meyer }
172101fbbc88SConrad Meyer 
172201fbbc88SConrad Meyer static int
1723f7157235SConrad Meyer sysctl_handle_reset(SYSCTL_HANDLER_ARGS)
1724f7157235SConrad Meyer {
1725f7157235SConrad Meyer 	struct ioat_softc *ioat;
1726f7157235SConrad Meyer 	int error, arg;
1727f7157235SConrad Meyer 
1728f7157235SConrad Meyer 	ioat = arg1;
1729f7157235SConrad Meyer 
1730f7157235SConrad Meyer 	arg = 0;
1731f7157235SConrad Meyer 	error = SYSCTL_OUT(req, &arg, sizeof(arg));
1732f7157235SConrad Meyer 	if (error != 0 || req->newptr == NULL)
1733f7157235SConrad Meyer 		return (error);
1734f7157235SConrad Meyer 
1735f7157235SConrad Meyer 	error = SYSCTL_IN(req, &arg, sizeof(arg));
1736f7157235SConrad Meyer 	if (error != 0)
1737f7157235SConrad Meyer 		return (error);
1738f7157235SConrad Meyer 
1739f7157235SConrad Meyer 	if (arg != 0)
1740f7157235SConrad Meyer 		error = ioat_reset_hw(ioat);
1741f7157235SConrad Meyer 
1742f7157235SConrad Meyer 	return (error);
1743f7157235SConrad Meyer }
1744f7157235SConrad Meyer 
1745e974f91cSConrad Meyer static void
1746e974f91cSConrad Meyer dump_descriptor(void *hw_desc)
1747e974f91cSConrad Meyer {
1748e974f91cSConrad Meyer 	int i, j;
1749e974f91cSConrad Meyer 
1750e974f91cSConrad Meyer 	for (i = 0; i < 2; i++) {
1751e974f91cSConrad Meyer 		for (j = 0; j < 8; j++)
1752e974f91cSConrad Meyer 			printf("%08x ", ((uint32_t *)hw_desc)[i * 8 + j]);
1753e974f91cSConrad Meyer 		printf("\n");
1754e974f91cSConrad Meyer 	}
1755e974f91cSConrad Meyer }
1756e974f91cSConrad Meyer 
1757e974f91cSConrad Meyer static void
1758e974f91cSConrad Meyer ioat_setup_sysctl(device_t device)
1759e974f91cSConrad Meyer {
176001fbbc88SConrad Meyer 	struct sysctl_oid_list *par, *statpar, *state, *hammer;
1761f7157235SConrad Meyer 	struct sysctl_ctx_list *ctx;
176201fbbc88SConrad Meyer 	struct sysctl_oid *tree, *tmp;
1763e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1764e974f91cSConrad Meyer 
1765e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
1766f7157235SConrad Meyer 	ctx = device_get_sysctl_ctx(device);
1767f7157235SConrad Meyer 	tree = device_get_sysctl_tree(device);
1768f7157235SConrad Meyer 	par = SYSCTL_CHILDREN(tree);
1769e974f91cSConrad Meyer 
177065e4f8adSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "version", CTLFLAG_RD,
177165e4f8adSConrad Meyer 	    &ioat->version, 0, "HW version (0xMM form)");
177265e4f8adSConrad Meyer 	SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "max_xfer_size", CTLFLAG_RD,
177365e4f8adSConrad Meyer 	    &ioat->max_xfer_size, 0, "HW maximum transfer size");
17745ca9fc2aSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "intrdelay_supported", CTLFLAG_RD,
17755ca9fc2aSConrad Meyer 	    &ioat->intrdelay_supported, 0, "Is INTRDELAY supported");
17765ca9fc2aSConrad Meyer 	SYSCTL_ADD_U16(ctx, par, OID_AUTO, "intrdelay_max", CTLFLAG_RD,
17775ca9fc2aSConrad Meyer 	    &ioat->intrdelay_max, 0,
17785ca9fc2aSConrad Meyer 	    "Maximum configurable INTRDELAY on this channel (microseconds)");
177965e4f8adSConrad Meyer 
178001fbbc88SConrad Meyer 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "state", CTLFLAG_RD, NULL,
178101fbbc88SConrad Meyer 	    "IOAT channel internal state");
178201fbbc88SConrad Meyer 	state = SYSCTL_CHILDREN(tmp);
178301fbbc88SConrad Meyer 
178401fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "ring_size_order", CTLFLAG_RD,
1785bf8553eaSConrad Meyer 	    &ioat->ring_size_order, 0, "SW descriptor ring size order");
178601fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "head", CTLFLAG_RD, &ioat->head,
178701fbbc88SConrad Meyer 	    0, "SW descriptor head pointer index");
178801fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "tail", CTLFLAG_RD, &ioat->tail,
178901fbbc88SConrad Meyer 	    0, "SW descriptor tail pointer index");
179001fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "hw_head", CTLFLAG_RD,
1791bf8553eaSConrad Meyer 	    &ioat->hw_head, 0, "HW DMACOUNT");
1792f7157235SConrad Meyer 
179301fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, state, OID_AUTO, "last_completion", CTLFLAG_RD,
179465e4f8adSConrad Meyer 	    ioat->comp_update, "HW addr of last completion");
179565e4f8adSConrad Meyer 
179625ad9585SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_submitter_processing",
179725ad9585SConrad Meyer 	    CTLFLAG_RD, &ioat->is_submitter_processing, 0,
179825ad9585SConrad Meyer 	    "submitter processing");
179901fbbc88SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_completion_pending",
180001fbbc88SConrad Meyer 	    CTLFLAG_RD, &ioat->is_completion_pending, 0, "completion pending");
180101fbbc88SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_reset_pending", CTLFLAG_RD,
180265e4f8adSConrad Meyer 	    &ioat->is_reset_pending, 0, "reset pending");
180301fbbc88SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_channel_running", CTLFLAG_RD,
180465e4f8adSConrad Meyer 	    &ioat->is_channel_running, 0, "channel running");
180565e4f8adSConrad Meyer 
180601fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, state, OID_AUTO, "chansts",
1807faefad9cSConrad Meyer 	    CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_chansts, "A",
1808faefad9cSConrad Meyer 	    "String of the channel status");
180901fbbc88SConrad Meyer 
18105ca9fc2aSConrad Meyer 	SYSCTL_ADD_U16(ctx, state, OID_AUTO, "intrdelay", CTLFLAG_RD,
18115ca9fc2aSConrad Meyer 	    &ioat->cached_intrdelay, 0,
18125ca9fc2aSConrad Meyer 	    "Current INTRDELAY on this channel (cached, microseconds)");
18135ca9fc2aSConrad Meyer 
181401fbbc88SConrad Meyer 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "hammer", CTLFLAG_RD, NULL,
181501fbbc88SConrad Meyer 	    "Big hammers (mostly for testing)");
181601fbbc88SConrad Meyer 	hammer = SYSCTL_CHILDREN(tmp);
181701fbbc88SConrad Meyer 
181801fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, hammer, OID_AUTO, "force_hw_reset",
181901fbbc88SConrad Meyer 	    CTLTYPE_INT | CTLFLAG_RW, ioat, 0, sysctl_handle_reset, "I",
182001fbbc88SConrad Meyer 	    "Set to non-zero to reset the hardware");
182101fbbc88SConrad Meyer 
182201fbbc88SConrad Meyer 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "stats", CTLFLAG_RD, NULL,
182301fbbc88SConrad Meyer 	    "IOAT channel statistics");
182401fbbc88SConrad Meyer 	statpar = SYSCTL_CHILDREN(tmp);
182501fbbc88SConrad Meyer 
182601fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "interrupts", CTLFLAG_RW,
182701fbbc88SConrad Meyer 	    &ioat->stats.interrupts,
182801fbbc88SConrad Meyer 	    "Number of interrupts processed on this channel");
182901fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "descriptors", CTLFLAG_RW,
183001fbbc88SConrad Meyer 	    &ioat->stats.descriptors_processed,
183101fbbc88SConrad Meyer 	    "Number of descriptors processed on this channel");
183201fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "submitted", CTLFLAG_RW,
183301fbbc88SConrad Meyer 	    &ioat->stats.descriptors_submitted,
183401fbbc88SConrad Meyer 	    "Number of descriptors submitted to this channel");
183501fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "errored", CTLFLAG_RW,
183601fbbc88SConrad Meyer 	    &ioat->stats.descriptors_error,
183701fbbc88SConrad Meyer 	    "Number of descriptors failed by channel errors");
183801fbbc88SConrad Meyer 	SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "halts", CTLFLAG_RW,
183901fbbc88SConrad Meyer 	    &ioat->stats.channel_halts, 0,
184001fbbc88SConrad Meyer 	    "Number of times the channel has halted");
184101fbbc88SConrad Meyer 	SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "last_halt_chanerr", CTLFLAG_RW,
184201fbbc88SConrad Meyer 	    &ioat->stats.last_halt_chanerr, 0,
184301fbbc88SConrad Meyer 	    "The raw CHANERR when the channel was last halted");
184401fbbc88SConrad Meyer 
184501fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, statpar, OID_AUTO, "desc_per_interrupt",
184601fbbc88SConrad Meyer 	    CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_dpi, "A",
184701fbbc88SConrad Meyer 	    "Descriptors per interrupt");
1848e974f91cSConrad Meyer }
1849466b3540SConrad Meyer 
1850466b3540SConrad Meyer static inline struct ioat_softc *
1851466b3540SConrad Meyer ioat_get(struct ioat_softc *ioat, enum ioat_ref_kind kind)
1852466b3540SConrad Meyer {
1853466b3540SConrad Meyer 	uint32_t old;
1854466b3540SConrad Meyer 
1855466b3540SConrad Meyer 	KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus"));
1856466b3540SConrad Meyer 
1857466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refcnt, 1);
1858466b3540SConrad Meyer 	KASSERT(old < UINT32_MAX, ("refcnt overflow"));
1859466b3540SConrad Meyer 
1860466b3540SConrad Meyer #ifdef INVARIANTS
1861466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refkinds[kind], 1);
1862466b3540SConrad Meyer 	KASSERT(old < UINT32_MAX, ("refcnt kind overflow"));
1863466b3540SConrad Meyer #endif
1864466b3540SConrad Meyer 
1865466b3540SConrad Meyer 	return (ioat);
1866466b3540SConrad Meyer }
1867466b3540SConrad Meyer 
1868466b3540SConrad Meyer static inline void
1869466b3540SConrad Meyer ioat_putn(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind)
1870466b3540SConrad Meyer {
1871faefad9cSConrad Meyer 
1872faefad9cSConrad Meyer 	_ioat_putn(ioat, n, kind, FALSE);
1873faefad9cSConrad Meyer }
1874faefad9cSConrad Meyer 
1875faefad9cSConrad Meyer static inline void
1876faefad9cSConrad Meyer ioat_putn_locked(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind)
1877faefad9cSConrad Meyer {
1878faefad9cSConrad Meyer 
1879faefad9cSConrad Meyer 	_ioat_putn(ioat, n, kind, TRUE);
1880faefad9cSConrad Meyer }
1881faefad9cSConrad Meyer 
1882faefad9cSConrad Meyer static inline void
1883faefad9cSConrad Meyer _ioat_putn(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind,
1884faefad9cSConrad Meyer     boolean_t locked)
1885faefad9cSConrad Meyer {
1886466b3540SConrad Meyer 	uint32_t old;
1887466b3540SConrad Meyer 
1888466b3540SConrad Meyer 	KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus"));
1889466b3540SConrad Meyer 
1890466b3540SConrad Meyer 	if (n == 0)
1891466b3540SConrad Meyer 		return;
1892466b3540SConrad Meyer 
1893466b3540SConrad Meyer #ifdef INVARIANTS
1894466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refkinds[kind], -n);
1895466b3540SConrad Meyer 	KASSERT(old >= n, ("refcnt kind underflow"));
1896466b3540SConrad Meyer #endif
1897466b3540SConrad Meyer 
1898466b3540SConrad Meyer 	/* Skip acquiring the lock if resulting refcnt > 0. */
1899466b3540SConrad Meyer 	for (;;) {
1900466b3540SConrad Meyer 		old = ioat->refcnt;
1901466b3540SConrad Meyer 		if (old <= n)
1902466b3540SConrad Meyer 			break;
1903466b3540SConrad Meyer 		if (atomic_cmpset_32(&ioat->refcnt, old, old - n))
1904466b3540SConrad Meyer 			return;
1905466b3540SConrad Meyer 	}
1906466b3540SConrad Meyer 
1907faefad9cSConrad Meyer 	if (locked)
1908faefad9cSConrad Meyer 		mtx_assert(IOAT_REFLK, MA_OWNED);
1909faefad9cSConrad Meyer 	else
1910466b3540SConrad Meyer 		mtx_lock(IOAT_REFLK);
1911faefad9cSConrad Meyer 
1912466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refcnt, -n);
1913466b3540SConrad Meyer 	KASSERT(old >= n, ("refcnt error"));
1914466b3540SConrad Meyer 
1915466b3540SConrad Meyer 	if (old == n)
1916466b3540SConrad Meyer 		wakeup(IOAT_REFLK);
1917faefad9cSConrad Meyer 	if (!locked)
1918466b3540SConrad Meyer 		mtx_unlock(IOAT_REFLK);
1919466b3540SConrad Meyer }
1920466b3540SConrad Meyer 
1921466b3540SConrad Meyer static inline void
1922466b3540SConrad Meyer ioat_put(struct ioat_softc *ioat, enum ioat_ref_kind kind)
1923466b3540SConrad Meyer {
1924466b3540SConrad Meyer 
1925466b3540SConrad Meyer 	ioat_putn(ioat, 1, kind);
1926466b3540SConrad Meyer }
1927466b3540SConrad Meyer 
1928466b3540SConrad Meyer static void
19295f77bd3eSConrad Meyer ioat_drain_locked(struct ioat_softc *ioat)
1930466b3540SConrad Meyer {
1931466b3540SConrad Meyer 
19325f77bd3eSConrad Meyer 	mtx_assert(IOAT_REFLK, MA_OWNED);
1933466b3540SConrad Meyer 	while (ioat->refcnt > 0)
1934466b3540SConrad Meyer 		msleep(IOAT_REFLK, IOAT_REFLK, 0, "ioat_drain", 0);
1935466b3540SConrad Meyer }
19360bb35debSConrad Meyer 
19370bb35debSConrad Meyer #ifdef DDB
19380bb35debSConrad Meyer #define	_db_show_lock(lo)	LOCK_CLASS(lo)->lc_ddb_show(lo)
19390bb35debSConrad Meyer #define	db_show_lock(lk)	_db_show_lock(&(lk)->lock_object)
19400bb35debSConrad Meyer DB_SHOW_COMMAND(ioat, db_show_ioat)
19410bb35debSConrad Meyer {
19420bb35debSConrad Meyer 	struct ioat_softc *sc;
19430bb35debSConrad Meyer 	unsigned idx;
19440bb35debSConrad Meyer 
19450bb35debSConrad Meyer 	if (!have_addr)
19460bb35debSConrad Meyer 		goto usage;
19470bb35debSConrad Meyer 	idx = (unsigned)addr;
194835b7a45eSConrad Meyer 	if (idx >= ioat_channel_index)
19490bb35debSConrad Meyer 		goto usage;
19500bb35debSConrad Meyer 
19510bb35debSConrad Meyer 	sc = ioat_channel[idx];
19520bb35debSConrad Meyer 	db_printf("ioat softc at %p\n", sc);
19530bb35debSConrad Meyer 	if (sc == NULL)
19540bb35debSConrad Meyer 		return;
19550bb35debSConrad Meyer 
19560bb35debSConrad Meyer 	db_printf(" version: %d\n", sc->version);
19570bb35debSConrad Meyer 	db_printf(" chan_idx: %u\n", sc->chan_idx);
19580bb35debSConrad Meyer 	db_printf(" submit_lock: ");
19590bb35debSConrad Meyer 	db_show_lock(&sc->submit_lock);
19600bb35debSConrad Meyer 
19610bb35debSConrad Meyer 	db_printf(" capabilities: %b\n", (int)sc->capabilities,
19620bb35debSConrad Meyer 	    IOAT_DMACAP_STR);
19630bb35debSConrad Meyer 	db_printf(" cached_intrdelay: %u\n", sc->cached_intrdelay);
19640bb35debSConrad Meyer 	db_printf(" *comp_update: 0x%jx\n", (uintmax_t)*sc->comp_update);
19650bb35debSConrad Meyer 
19665ac77963SConrad Meyer 	db_printf(" poll_timer:\n");
19675ac77963SConrad Meyer 	db_printf("  c_time: %ju\n", (uintmax_t)sc->poll_timer.c_time);
19685ac77963SConrad Meyer 	db_printf("  c_arg: %p\n", sc->poll_timer.c_arg);
19695ac77963SConrad Meyer 	db_printf("  c_func: %p\n", sc->poll_timer.c_func);
19705ac77963SConrad Meyer 	db_printf("  c_lock: %p\n", sc->poll_timer.c_lock);
19715ac77963SConrad Meyer 	db_printf("  c_flags: 0x%x\n", (unsigned)sc->poll_timer.c_flags);
19725ac77963SConrad Meyer 
19730bb35debSConrad Meyer 	db_printf(" quiescing: %d\n", (int)sc->quiescing);
19740bb35debSConrad Meyer 	db_printf(" destroying: %d\n", (int)sc->destroying);
197525ad9585SConrad Meyer 	db_printf(" is_submitter_processing: %d\n",
197625ad9585SConrad Meyer 	    (int)sc->is_submitter_processing);
19770bb35debSConrad Meyer 	db_printf(" is_completion_pending: %d\n", (int)sc->is_completion_pending);
19780bb35debSConrad Meyer 	db_printf(" is_reset_pending: %d\n", (int)sc->is_reset_pending);
19790bb35debSConrad Meyer 	db_printf(" is_channel_running: %d\n", (int)sc->is_channel_running);
19800bb35debSConrad Meyer 	db_printf(" intrdelay_supported: %d\n", (int)sc->intrdelay_supported);
198193f7f84aSConrad Meyer 	db_printf(" resetting: %d\n", (int)sc->resetting);
19820bb35debSConrad Meyer 
19830bb35debSConrad Meyer 	db_printf(" head: %u\n", sc->head);
19840bb35debSConrad Meyer 	db_printf(" tail: %u\n", sc->tail);
19850bb35debSConrad Meyer 	db_printf(" hw_head: %u\n", sc->hw_head);
19860bb35debSConrad Meyer 	db_printf(" ring_size_order: %u\n", sc->ring_size_order);
19870bb35debSConrad Meyer 	db_printf(" last_seen: 0x%lx\n", sc->last_seen);
19880bb35debSConrad Meyer 	db_printf(" ring: %p\n", sc->ring);
19898e269d99SConrad Meyer 	db_printf(" descriptors: %p\n", sc->hw_desc_ring);
19908e269d99SConrad Meyer 	db_printf(" descriptors (phys): 0x%jx\n",
19918e269d99SConrad Meyer 	    (uintmax_t)sc->hw_desc_bus_addr);
19920bb35debSConrad Meyer 
199335b7a45eSConrad Meyer 	db_printf("  ring[%u] (tail):\n", sc->tail %
199435b7a45eSConrad Meyer 	    (1 << sc->ring_size_order));
199535b7a45eSConrad Meyer 	db_printf("   id: %u\n", ioat_get_ring_entry(sc, sc->tail)->id);
199635b7a45eSConrad Meyer 	db_printf("   addr: 0x%lx\n",
19978e269d99SConrad Meyer 	    RING_PHYS_ADDR(sc, sc->tail));
199835b7a45eSConrad Meyer 	db_printf("   next: 0x%lx\n",
19998e269d99SConrad Meyer 	     ioat_get_descriptor(sc, sc->tail)->generic.next);
200035b7a45eSConrad Meyer 
200135b7a45eSConrad Meyer 	db_printf("  ring[%u] (head - 1):\n", (sc->head - 1) %
200235b7a45eSConrad Meyer 	    (1 << sc->ring_size_order));
200335b7a45eSConrad Meyer 	db_printf("   id: %u\n", ioat_get_ring_entry(sc, sc->head - 1)->id);
200435b7a45eSConrad Meyer 	db_printf("   addr: 0x%lx\n",
20058e269d99SConrad Meyer 	    RING_PHYS_ADDR(sc, sc->head - 1));
200635b7a45eSConrad Meyer 	db_printf("   next: 0x%lx\n",
20078e269d99SConrad Meyer 	     ioat_get_descriptor(sc, sc->head - 1)->generic.next);
200835b7a45eSConrad Meyer 
200935b7a45eSConrad Meyer 	db_printf("  ring[%u] (head):\n", (sc->head) %
201035b7a45eSConrad Meyer 	    (1 << sc->ring_size_order));
201135b7a45eSConrad Meyer 	db_printf("   id: %u\n", ioat_get_ring_entry(sc, sc->head)->id);
201235b7a45eSConrad Meyer 	db_printf("   addr: 0x%lx\n",
20138e269d99SConrad Meyer 	    RING_PHYS_ADDR(sc, sc->head));
201435b7a45eSConrad Meyer 	db_printf("   next: 0x%lx\n",
20158e269d99SConrad Meyer 	     ioat_get_descriptor(sc, sc->head)->generic.next);
201635b7a45eSConrad Meyer 
201735b7a45eSConrad Meyer 	for (idx = 0; idx < (1 << sc->ring_size_order); idx++)
201835b7a45eSConrad Meyer 		if ((*sc->comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK)
20198e269d99SConrad Meyer 		    == RING_PHYS_ADDR(sc, idx))
202035b7a45eSConrad Meyer 			db_printf("  ring[%u] == hardware tail\n", idx);
202135b7a45eSConrad Meyer 
20220bb35debSConrad Meyer 	db_printf(" cleanup_lock: ");
20230bb35debSConrad Meyer 	db_show_lock(&sc->cleanup_lock);
20240bb35debSConrad Meyer 
20250bb35debSConrad Meyer 	db_printf(" refcnt: %u\n", sc->refcnt);
20260bb35debSConrad Meyer #ifdef INVARIANTS
20270bb35debSConrad Meyer 	CTASSERT(IOAT_NUM_REF_KINDS == 2);
20280bb35debSConrad Meyer 	db_printf(" refkinds: [ENG=%u, DESCR=%u]\n", sc->refkinds[0],
20290bb35debSConrad Meyer 	    sc->refkinds[1]);
20300bb35debSConrad Meyer #endif
20310bb35debSConrad Meyer 	db_printf(" stats:\n");
20320bb35debSConrad Meyer 	db_printf("  interrupts: %lu\n", sc->stats.interrupts);
20330bb35debSConrad Meyer 	db_printf("  descriptors_processed: %lu\n", sc->stats.descriptors_processed);
20340bb35debSConrad Meyer 	db_printf("  descriptors_error: %lu\n", sc->stats.descriptors_error);
20350bb35debSConrad Meyer 	db_printf("  descriptors_submitted: %lu\n", sc->stats.descriptors_submitted);
20360bb35debSConrad Meyer 
20370bb35debSConrad Meyer 	db_printf("  channel_halts: %u\n", sc->stats.channel_halts);
20380bb35debSConrad Meyer 	db_printf("  last_halt_chanerr: %u\n", sc->stats.last_halt_chanerr);
20390bb35debSConrad Meyer 
20400bb35debSConrad Meyer 	if (db_pager_quit)
20410bb35debSConrad Meyer 		return;
20420bb35debSConrad Meyer 
20430bb35debSConrad Meyer 	db_printf(" hw status:\n");
20440bb35debSConrad Meyer 	db_printf("  status: 0x%lx\n", ioat_get_chansts(sc));
20450bb35debSConrad Meyer 	db_printf("  chanctrl: 0x%x\n",
20460bb35debSConrad Meyer 	    (unsigned)ioat_read_2(sc, IOAT_CHANCTRL_OFFSET));
20470bb35debSConrad Meyer 	db_printf("  chancmd: 0x%x\n",
20480bb35debSConrad Meyer 	    (unsigned)ioat_read_1(sc, IOAT_CHANCMD_OFFSET));
20490bb35debSConrad Meyer 	db_printf("  dmacount: 0x%x\n",
20500bb35debSConrad Meyer 	    (unsigned)ioat_read_2(sc, IOAT_DMACOUNT_OFFSET));
20510bb35debSConrad Meyer 	db_printf("  chainaddr: 0x%lx\n",
20520bb35debSConrad Meyer 	    ioat_read_double_4(sc, IOAT_CHAINADDR_OFFSET_LOW));
20530bb35debSConrad Meyer 	db_printf("  chancmp: 0x%lx\n",
20540bb35debSConrad Meyer 	    ioat_read_double_4(sc, IOAT_CHANCMP_OFFSET_LOW));
20550bb35debSConrad Meyer 	db_printf("  chanerr: %b\n",
20560bb35debSConrad Meyer 	    (int)ioat_read_4(sc, IOAT_CHANERR_OFFSET), IOAT_CHANERR_STR);
20570bb35debSConrad Meyer 	return;
20580bb35debSConrad Meyer usage:
20590bb35debSConrad Meyer 	db_printf("usage: show ioat <0-%u>\n", ioat_channel_index);
20600bb35debSConrad Meyer 	return;
20610bb35debSConrad Meyer }
20620bb35debSConrad Meyer #endif /* DDB */
2063