xref: /freebsd/sys/dev/ioat/ioat.c (revision c2b69205acdd876613e850250e723a85bb75341e)
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 
30e974f91cSConrad Meyer #include <sys/param.h>
31e974f91cSConrad Meyer #include <sys/systm.h>
32e974f91cSConrad Meyer #include <sys/bus.h>
33e974f91cSConrad Meyer #include <sys/conf.h>
34e974f91cSConrad Meyer #include <sys/ioccom.h>
35e974f91cSConrad Meyer #include <sys/kernel.h>
36e974f91cSConrad Meyer #include <sys/lock.h>
37e974f91cSConrad Meyer #include <sys/malloc.h>
38e974f91cSConrad Meyer #include <sys/module.h>
39e974f91cSConrad Meyer #include <sys/mutex.h>
40e974f91cSConrad Meyer #include <sys/rman.h>
41faefad9cSConrad Meyer #include <sys/sbuf.h>
42e974f91cSConrad Meyer #include <sys/sysctl.h>
43e974f91cSConrad Meyer #include <sys/time.h>
44e974f91cSConrad Meyer #include <dev/pci/pcireg.h>
45e974f91cSConrad Meyer #include <dev/pci/pcivar.h>
46e974f91cSConrad Meyer #include <machine/bus.h>
47e974f91cSConrad Meyer #include <machine/resource.h>
48e974f91cSConrad Meyer #include <machine/stdarg.h>
49e974f91cSConrad Meyer 
50e974f91cSConrad Meyer #include "ioat.h"
51e974f91cSConrad Meyer #include "ioat_hw.h"
52e974f91cSConrad Meyer #include "ioat_internal.h"
53e974f91cSConrad Meyer 
54fe720f5aSConrad Meyer #define	IOAT_INTR_TIMO	(hz / 10)
55466b3540SConrad Meyer #define	IOAT_REFLK	(&ioat->submit_lock)
56fe720f5aSConrad Meyer 
57e974f91cSConrad Meyer static int ioat_probe(device_t device);
58e974f91cSConrad Meyer static int ioat_attach(device_t device);
59e974f91cSConrad Meyer static int ioat_detach(device_t device);
604253ea50SConrad Meyer static int ioat_setup_intr(struct ioat_softc *ioat);
614253ea50SConrad Meyer static int ioat_teardown_intr(struct ioat_softc *ioat);
62e974f91cSConrad Meyer static int ioat3_attach(device_t device);
63cea5b880SConrad Meyer static int ioat_start_channel(struct ioat_softc *ioat);
64e974f91cSConrad Meyer static int ioat_map_pci_bar(struct ioat_softc *ioat);
65e974f91cSConrad Meyer static void ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg,
66e974f91cSConrad Meyer     int error);
67e974f91cSConrad Meyer static void ioat_interrupt_handler(void *arg);
680d1a05d9SConrad Meyer static boolean_t ioat_model_resets_msix(struct ioat_softc *ioat);
69faefad9cSConrad Meyer static int chanerr_to_errno(uint32_t);
70e974f91cSConrad Meyer static void ioat_process_events(struct ioat_softc *ioat);
71e974f91cSConrad Meyer static inline uint32_t ioat_get_active(struct ioat_softc *ioat);
72e974f91cSConrad Meyer static inline uint32_t ioat_get_ring_space(struct ioat_softc *ioat);
73bf8553eaSConrad Meyer static void ioat_free_ring(struct ioat_softc *, uint32_t size,
74bf8553eaSConrad Meyer     struct ioat_descriptor **);
75e974f91cSConrad Meyer static void ioat_free_ring_entry(struct ioat_softc *ioat,
76e974f91cSConrad Meyer     struct ioat_descriptor *desc);
77bf8553eaSConrad Meyer static struct ioat_descriptor *ioat_alloc_ring_entry(struct ioat_softc *,
78bf8553eaSConrad Meyer     int mflags);
79bf8553eaSConrad Meyer static int ioat_reserve_space(struct ioat_softc *, uint32_t, int mflags);
80e974f91cSConrad Meyer static struct ioat_descriptor *ioat_get_ring_entry(struct ioat_softc *ioat,
81e974f91cSConrad Meyer     uint32_t index);
82bf8553eaSConrad Meyer static struct ioat_descriptor **ioat_prealloc_ring(struct ioat_softc *,
83bf8553eaSConrad Meyer     uint32_t size, boolean_t need_dscr, int mflags);
84bf8553eaSConrad Meyer static int ring_grow(struct ioat_softc *, uint32_t oldorder,
85bf8553eaSConrad Meyer     struct ioat_descriptor **);
86bf8553eaSConrad Meyer static int ring_shrink(struct ioat_softc *, uint32_t oldorder,
87bf8553eaSConrad Meyer     struct ioat_descriptor **);
88faefad9cSConrad Meyer static void ioat_halted_debug(struct ioat_softc *, uint32_t);
89e974f91cSConrad Meyer static void ioat_timer_callback(void *arg);
90e974f91cSConrad Meyer static void dump_descriptor(void *hw_desc);
91e974f91cSConrad Meyer static void ioat_submit_single(struct ioat_softc *ioat);
92e974f91cSConrad Meyer static void ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg,
93e974f91cSConrad Meyer     int error);
94e974f91cSConrad Meyer static int ioat_reset_hw(struct ioat_softc *ioat);
95e974f91cSConrad Meyer static void ioat_setup_sysctl(device_t device);
96f7157235SConrad Meyer static int sysctl_handle_reset(SYSCTL_HANDLER_ARGS);
97466b3540SConrad Meyer static inline struct ioat_softc *ioat_get(struct ioat_softc *,
98466b3540SConrad Meyer     enum ioat_ref_kind);
99466b3540SConrad Meyer static inline void ioat_put(struct ioat_softc *, enum ioat_ref_kind);
100faefad9cSConrad Meyer static inline void _ioat_putn(struct ioat_softc *, uint32_t,
101faefad9cSConrad Meyer     enum ioat_ref_kind, boolean_t);
102466b3540SConrad Meyer static inline void ioat_putn(struct ioat_softc *, uint32_t,
103466b3540SConrad Meyer     enum ioat_ref_kind);
104faefad9cSConrad Meyer static inline void ioat_putn_locked(struct ioat_softc *, uint32_t,
105faefad9cSConrad Meyer     enum ioat_ref_kind);
1065f77bd3eSConrad Meyer static void ioat_drain_locked(struct ioat_softc *);
107e974f91cSConrad Meyer 
1081c25420eSConrad Meyer #define	ioat_log_message(v, ...) do {					\
1091c25420eSConrad Meyer 	if ((v) <= g_ioat_debug_level) {				\
1101c25420eSConrad Meyer 		device_printf(ioat->device, __VA_ARGS__);		\
1111c25420eSConrad Meyer 	}								\
1121c25420eSConrad Meyer } while (0)
1131c25420eSConrad Meyer 
114e974f91cSConrad Meyer MALLOC_DEFINE(M_IOAT, "ioat", "ioat driver memory allocations");
115e974f91cSConrad Meyer SYSCTL_NODE(_hw, OID_AUTO, ioat, CTLFLAG_RD, 0, "ioat node");
116e974f91cSConrad Meyer 
117e974f91cSConrad Meyer static int g_force_legacy_interrupts;
118e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, force_legacy_interrupts, CTLFLAG_RDTUN,
119e974f91cSConrad Meyer     &g_force_legacy_interrupts, 0, "Set to non-zero to force MSI-X disabled");
120e974f91cSConrad Meyer 
1211c25420eSConrad Meyer int g_ioat_debug_level = 0;
122e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, debug_level, CTLFLAG_RWTUN, &g_ioat_debug_level,
123e974f91cSConrad Meyer     0, "Set log level (0-3) for ioat(4). Higher is more verbose.");
124e974f91cSConrad Meyer 
125e974f91cSConrad Meyer /*
126e974f91cSConrad Meyer  * OS <-> Driver interface structures
127e974f91cSConrad Meyer  */
128e974f91cSConrad Meyer static device_method_t ioat_pci_methods[] = {
129e974f91cSConrad Meyer 	/* Device interface */
130e974f91cSConrad Meyer 	DEVMETHOD(device_probe,     ioat_probe),
131e974f91cSConrad Meyer 	DEVMETHOD(device_attach,    ioat_attach),
132e974f91cSConrad Meyer 	DEVMETHOD(device_detach,    ioat_detach),
133e974f91cSConrad Meyer 	{ 0, 0 }
134e974f91cSConrad Meyer };
135e974f91cSConrad Meyer 
136e974f91cSConrad Meyer static driver_t ioat_pci_driver = {
137e974f91cSConrad Meyer 	"ioat",
138e974f91cSConrad Meyer 	ioat_pci_methods,
139e974f91cSConrad Meyer 	sizeof(struct ioat_softc),
140e974f91cSConrad Meyer };
141e974f91cSConrad Meyer 
142e974f91cSConrad Meyer static devclass_t ioat_devclass;
143e974f91cSConrad Meyer DRIVER_MODULE(ioat, pci, ioat_pci_driver, ioat_devclass, 0, 0);
144*c2b69205SConrad Meyer MODULE_VERSION(ioat, 1);
145e974f91cSConrad Meyer 
146e974f91cSConrad Meyer /*
147e974f91cSConrad Meyer  * Private data structures
148e974f91cSConrad Meyer  */
149e974f91cSConrad Meyer static struct ioat_softc *ioat_channel[IOAT_MAX_CHANNELS];
150e974f91cSConrad Meyer static int ioat_channel_index = 0;
151e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, channels, CTLFLAG_RD, &ioat_channel_index, 0,
152e974f91cSConrad Meyer     "Number of IOAT channels attached");
153e974f91cSConrad Meyer 
154e974f91cSConrad Meyer static struct _pcsid
155e974f91cSConrad Meyer {
156e974f91cSConrad Meyer 	u_int32_t   type;
157e974f91cSConrad Meyer 	const char  *desc;
158e974f91cSConrad Meyer } pci_ids[] = {
159e974f91cSConrad Meyer 	{ 0x34308086, "TBG IOAT Ch0" },
160e974f91cSConrad Meyer 	{ 0x34318086, "TBG IOAT Ch1" },
161e974f91cSConrad Meyer 	{ 0x34328086, "TBG IOAT Ch2" },
162e974f91cSConrad Meyer 	{ 0x34338086, "TBG IOAT Ch3" },
163e974f91cSConrad Meyer 	{ 0x34298086, "TBG IOAT Ch4" },
164e974f91cSConrad Meyer 	{ 0x342a8086, "TBG IOAT Ch5" },
165e974f91cSConrad Meyer 	{ 0x342b8086, "TBG IOAT Ch6" },
166e974f91cSConrad Meyer 	{ 0x342c8086, "TBG IOAT Ch7" },
167e974f91cSConrad Meyer 
168e974f91cSConrad Meyer 	{ 0x37108086, "JSF IOAT Ch0" },
169e974f91cSConrad Meyer 	{ 0x37118086, "JSF IOAT Ch1" },
170e974f91cSConrad Meyer 	{ 0x37128086, "JSF IOAT Ch2" },
171e974f91cSConrad Meyer 	{ 0x37138086, "JSF IOAT Ch3" },
172e974f91cSConrad Meyer 	{ 0x37148086, "JSF IOAT Ch4" },
173e974f91cSConrad Meyer 	{ 0x37158086, "JSF IOAT Ch5" },
174e974f91cSConrad Meyer 	{ 0x37168086, "JSF IOAT Ch6" },
175e974f91cSConrad Meyer 	{ 0x37178086, "JSF IOAT Ch7" },
176e974f91cSConrad Meyer 	{ 0x37188086, "JSF IOAT Ch0 (RAID)" },
177e974f91cSConrad Meyer 	{ 0x37198086, "JSF IOAT Ch1 (RAID)" },
178e974f91cSConrad Meyer 
179e974f91cSConrad Meyer 	{ 0x3c208086, "SNB IOAT Ch0" },
180e974f91cSConrad Meyer 	{ 0x3c218086, "SNB IOAT Ch1" },
181e974f91cSConrad Meyer 	{ 0x3c228086, "SNB IOAT Ch2" },
182e974f91cSConrad Meyer 	{ 0x3c238086, "SNB IOAT Ch3" },
183e974f91cSConrad Meyer 	{ 0x3c248086, "SNB IOAT Ch4" },
184e974f91cSConrad Meyer 	{ 0x3c258086, "SNB IOAT Ch5" },
185e974f91cSConrad Meyer 	{ 0x3c268086, "SNB IOAT Ch6" },
186e974f91cSConrad Meyer 	{ 0x3c278086, "SNB IOAT Ch7" },
187e974f91cSConrad Meyer 	{ 0x3c2e8086, "SNB IOAT Ch0 (RAID)" },
188e974f91cSConrad Meyer 	{ 0x3c2f8086, "SNB IOAT Ch1 (RAID)" },
189e974f91cSConrad Meyer 
190e974f91cSConrad Meyer 	{ 0x0e208086, "IVB IOAT Ch0" },
191e974f91cSConrad Meyer 	{ 0x0e218086, "IVB IOAT Ch1" },
192e974f91cSConrad Meyer 	{ 0x0e228086, "IVB IOAT Ch2" },
193e974f91cSConrad Meyer 	{ 0x0e238086, "IVB IOAT Ch3" },
194e974f91cSConrad Meyer 	{ 0x0e248086, "IVB IOAT Ch4" },
195e974f91cSConrad Meyer 	{ 0x0e258086, "IVB IOAT Ch5" },
196e974f91cSConrad Meyer 	{ 0x0e268086, "IVB IOAT Ch6" },
197e974f91cSConrad Meyer 	{ 0x0e278086, "IVB IOAT Ch7" },
198e974f91cSConrad Meyer 	{ 0x0e2e8086, "IVB IOAT Ch0 (RAID)" },
199e974f91cSConrad Meyer 	{ 0x0e2f8086, "IVB IOAT Ch1 (RAID)" },
200e974f91cSConrad Meyer 
201e974f91cSConrad Meyer 	{ 0x2f208086, "HSW IOAT Ch0" },
202e974f91cSConrad Meyer 	{ 0x2f218086, "HSW IOAT Ch1" },
203e974f91cSConrad Meyer 	{ 0x2f228086, "HSW IOAT Ch2" },
204e974f91cSConrad Meyer 	{ 0x2f238086, "HSW IOAT Ch3" },
205e974f91cSConrad Meyer 	{ 0x2f248086, "HSW IOAT Ch4" },
206e974f91cSConrad Meyer 	{ 0x2f258086, "HSW IOAT Ch5" },
207e974f91cSConrad Meyer 	{ 0x2f268086, "HSW IOAT Ch6" },
208e974f91cSConrad Meyer 	{ 0x2f278086, "HSW IOAT Ch7" },
209e974f91cSConrad Meyer 	{ 0x2f2e8086, "HSW IOAT Ch0 (RAID)" },
210e974f91cSConrad Meyer 	{ 0x2f2f8086, "HSW IOAT Ch1 (RAID)" },
211e974f91cSConrad Meyer 
212e974f91cSConrad Meyer 	{ 0x0c508086, "BWD IOAT Ch0" },
213e974f91cSConrad Meyer 	{ 0x0c518086, "BWD IOAT Ch1" },
214e974f91cSConrad Meyer 	{ 0x0c528086, "BWD IOAT Ch2" },
215e974f91cSConrad Meyer 	{ 0x0c538086, "BWD IOAT Ch3" },
216e974f91cSConrad Meyer 
217e974f91cSConrad Meyer 	{ 0x6f508086, "BDXDE IOAT Ch0" },
218e974f91cSConrad Meyer 	{ 0x6f518086, "BDXDE IOAT Ch1" },
219e974f91cSConrad Meyer 	{ 0x6f528086, "BDXDE IOAT Ch2" },
220e974f91cSConrad Meyer 	{ 0x6f538086, "BDXDE IOAT Ch3" },
221e974f91cSConrad Meyer 
222e974f91cSConrad Meyer 	{ 0x00000000, NULL           }
223e974f91cSConrad Meyer };
224e974f91cSConrad Meyer 
225e974f91cSConrad Meyer /*
226e974f91cSConrad Meyer  * OS <-> Driver linkage functions
227e974f91cSConrad Meyer  */
228e974f91cSConrad Meyer static int
229e974f91cSConrad Meyer ioat_probe(device_t device)
230e974f91cSConrad Meyer {
231e974f91cSConrad Meyer 	struct _pcsid *ep;
232e974f91cSConrad Meyer 	u_int32_t type;
233e974f91cSConrad Meyer 
234e974f91cSConrad Meyer 	type = pci_get_devid(device);
235e974f91cSConrad Meyer 	for (ep = pci_ids; ep->type; ep++) {
236e974f91cSConrad Meyer 		if (ep->type == type) {
237e974f91cSConrad Meyer 			device_set_desc(device, ep->desc);
238e974f91cSConrad Meyer 			return (0);
239e974f91cSConrad Meyer 		}
240e974f91cSConrad Meyer 	}
241e974f91cSConrad Meyer 	return (ENXIO);
242e974f91cSConrad Meyer }
243e974f91cSConrad Meyer 
244e974f91cSConrad Meyer static int
245e974f91cSConrad Meyer ioat_attach(device_t device)
246e974f91cSConrad Meyer {
247e974f91cSConrad Meyer 	struct ioat_softc *ioat;
248e974f91cSConrad Meyer 	int error;
249e974f91cSConrad Meyer 
250e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
251e974f91cSConrad Meyer 	ioat->device = device;
252e974f91cSConrad Meyer 
253e974f91cSConrad Meyer 	error = ioat_map_pci_bar(ioat);
254e974f91cSConrad Meyer 	if (error != 0)
255e974f91cSConrad Meyer 		goto err;
256e974f91cSConrad Meyer 
257e974f91cSConrad Meyer 	ioat->version = ioat_read_cbver(ioat);
258e974f91cSConrad Meyer 	if (ioat->version < IOAT_VER_3_0) {
259e974f91cSConrad Meyer 		error = ENODEV;
260e974f91cSConrad Meyer 		goto err;
261e974f91cSConrad Meyer 	}
262e974f91cSConrad Meyer 
263e974f91cSConrad Meyer 	error = ioat3_attach(device);
264e974f91cSConrad Meyer 	if (error != 0)
265e974f91cSConrad Meyer 		goto err;
266e974f91cSConrad Meyer 
267e974f91cSConrad Meyer 	error = pci_enable_busmaster(device);
268e974f91cSConrad Meyer 	if (error != 0)
269e974f91cSConrad Meyer 		goto err;
270e974f91cSConrad Meyer 
271466b3540SConrad Meyer 	error = ioat_setup_intr(ioat);
272466b3540SConrad Meyer 	if (error != 0)
273466b3540SConrad Meyer 		goto err;
274466b3540SConrad Meyer 
275cea5b880SConrad Meyer 	error = ioat_reset_hw(ioat);
2767afbb263SConrad Meyer 	if (error != 0)
277466b3540SConrad Meyer 		goto err;
2787afbb263SConrad Meyer 
2797afbb263SConrad Meyer 	ioat_process_events(ioat);
2807afbb263SConrad Meyer 	ioat_setup_sysctl(device);
2817afbb263SConrad Meyer 
2825f77bd3eSConrad Meyer 	ioat->chan_idx = ioat_channel_index;
283e974f91cSConrad Meyer 	ioat_channel[ioat_channel_index++] = ioat;
2847afbb263SConrad Meyer 	ioat_test_attach();
285e974f91cSConrad Meyer 
286e974f91cSConrad Meyer err:
287e974f91cSConrad Meyer 	if (error != 0)
288e974f91cSConrad Meyer 		ioat_detach(device);
289e974f91cSConrad Meyer 	return (error);
290e974f91cSConrad Meyer }
291e974f91cSConrad Meyer 
292e974f91cSConrad Meyer static int
293e974f91cSConrad Meyer ioat_detach(device_t device)
294e974f91cSConrad Meyer {
295e974f91cSConrad Meyer 	struct ioat_softc *ioat;
296e974f91cSConrad Meyer 
297e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
2987afbb263SConrad Meyer 
2997afbb263SConrad Meyer 	ioat_test_detach();
3005f77bd3eSConrad Meyer 
3015f77bd3eSConrad Meyer 	mtx_lock(IOAT_REFLK);
3025f77bd3eSConrad Meyer 	ioat->quiescing = TRUE;
3035f77bd3eSConrad Meyer 	ioat_channel[ioat->chan_idx] = NULL;
3045f77bd3eSConrad Meyer 
3055f77bd3eSConrad Meyer 	ioat_drain_locked(ioat);
3065f77bd3eSConrad Meyer 	mtx_unlock(IOAT_REFLK);
307fe720f5aSConrad Meyer 
308fe720f5aSConrad Meyer 	ioat_teardown_intr(ioat);
309e974f91cSConrad Meyer 	callout_drain(&ioat->timer);
310e974f91cSConrad Meyer 
311e974f91cSConrad Meyer 	pci_disable_busmaster(device);
312e974f91cSConrad Meyer 
313e974f91cSConrad Meyer 	if (ioat->pci_resource != NULL)
314e974f91cSConrad Meyer 		bus_release_resource(device, SYS_RES_MEMORY,
315e974f91cSConrad Meyer 		    ioat->pci_resource_id, ioat->pci_resource);
316e974f91cSConrad Meyer 
317bf8553eaSConrad Meyer 	if (ioat->ring != NULL)
318bf8553eaSConrad Meyer 		ioat_free_ring(ioat, 1 << ioat->ring_size_order, ioat->ring);
319e974f91cSConrad Meyer 
320e974f91cSConrad Meyer 	if (ioat->comp_update != NULL) {
321e974f91cSConrad Meyer 		bus_dmamap_unload(ioat->comp_update_tag, ioat->comp_update_map);
322e974f91cSConrad Meyer 		bus_dmamem_free(ioat->comp_update_tag, ioat->comp_update,
323e974f91cSConrad Meyer 		    ioat->comp_update_map);
324e974f91cSConrad Meyer 		bus_dma_tag_destroy(ioat->comp_update_tag);
325e974f91cSConrad Meyer 	}
326e974f91cSConrad Meyer 
327e974f91cSConrad Meyer 	bus_dma_tag_destroy(ioat->hw_desc_tag);
328e974f91cSConrad Meyer 
3294253ea50SConrad Meyer 	return (0);
3304253ea50SConrad Meyer }
3314253ea50SConrad Meyer 
3324253ea50SConrad Meyer static int
3334253ea50SConrad Meyer ioat_teardown_intr(struct ioat_softc *ioat)
3344253ea50SConrad Meyer {
3354253ea50SConrad Meyer 
336e974f91cSConrad Meyer 	if (ioat->tag != NULL)
3374253ea50SConrad Meyer 		bus_teardown_intr(ioat->device, ioat->res, ioat->tag);
338e974f91cSConrad Meyer 
339e974f91cSConrad Meyer 	if (ioat->res != NULL)
3404253ea50SConrad Meyer 		bus_release_resource(ioat->device, SYS_RES_IRQ,
341e974f91cSConrad Meyer 		    rman_get_rid(ioat->res), ioat->res);
342e974f91cSConrad Meyer 
3434253ea50SConrad Meyer 	pci_release_msi(ioat->device);
344e974f91cSConrad Meyer 	return (0);
345e974f91cSConrad Meyer }
346e974f91cSConrad Meyer 
347e974f91cSConrad Meyer static int
348cea5b880SConrad Meyer ioat_start_channel(struct ioat_softc *ioat)
349e974f91cSConrad Meyer {
350e974f91cSConrad Meyer 	uint64_t status;
351e974f91cSConrad Meyer 	uint32_t chanerr;
352e974f91cSConrad Meyer 	int i;
353e974f91cSConrad Meyer 
354e974f91cSConrad Meyer 	ioat_acquire(&ioat->dmaengine);
355e974f91cSConrad Meyer 	ioat_null(&ioat->dmaengine, NULL, NULL, 0);
356e974f91cSConrad Meyer 	ioat_release(&ioat->dmaengine);
357e974f91cSConrad Meyer 
358e974f91cSConrad Meyer 	for (i = 0; i < 100; i++) {
359e974f91cSConrad Meyer 		DELAY(1);
360e974f91cSConrad Meyer 		status = ioat_get_chansts(ioat);
361e974f91cSConrad Meyer 		if (is_ioat_idle(status))
362e974f91cSConrad Meyer 			return (0);
363e974f91cSConrad Meyer 	}
364e974f91cSConrad Meyer 
365e974f91cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
366e974f91cSConrad Meyer 	ioat_log_message(0, "could not start channel: "
36759acd4baSConrad Meyer 	    "status = %#jx error = %b\n", (uintmax_t)status, (int)chanerr,
36859acd4baSConrad Meyer 	    IOAT_CHANERR_STR);
369e974f91cSConrad Meyer 	return (ENXIO);
370e974f91cSConrad Meyer }
371e974f91cSConrad Meyer 
372e974f91cSConrad Meyer /*
373e974f91cSConrad Meyer  * Initialize Hardware
374e974f91cSConrad Meyer  */
375e974f91cSConrad Meyer static int
376e974f91cSConrad Meyer ioat3_attach(device_t device)
377e974f91cSConrad Meyer {
378e974f91cSConrad Meyer 	struct ioat_softc *ioat;
379e974f91cSConrad Meyer 	struct ioat_descriptor **ring;
380e974f91cSConrad Meyer 	struct ioat_descriptor *next;
381e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *dma_hw_desc;
382e974f91cSConrad Meyer 	int i, num_descriptors;
383e974f91cSConrad Meyer 	int error;
384e974f91cSConrad Meyer 	uint8_t xfercap;
385e974f91cSConrad Meyer 
386e974f91cSConrad Meyer 	error = 0;
387e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
3881693d27bSConrad Meyer 	ioat->capabilities = ioat_read_dmacapability(ioat);
3891693d27bSConrad Meyer 
3901693d27bSConrad Meyer 	ioat_log_message(1, "Capabilities: %b\n", (int)ioat->capabilities,
3911693d27bSConrad Meyer 	    IOAT_DMACAP_STR);
392e974f91cSConrad Meyer 
393e974f91cSConrad Meyer 	xfercap = ioat_read_xfercap(ioat);
394e974f91cSConrad Meyer 	ioat->max_xfer_size = 1 << xfercap;
395e974f91cSConrad Meyer 
396e974f91cSConrad Meyer 	/* TODO: need to check DCA here if we ever do XOR/PQ */
397e974f91cSConrad Meyer 
398e974f91cSConrad Meyer 	mtx_init(&ioat->submit_lock, "ioat_submit", NULL, MTX_DEF);
399faefad9cSConrad Meyer 	mtx_init(&ioat->cleanup_lock, "ioat_cleanup", NULL, MTX_DEF);
4007afbb263SConrad Meyer 	callout_init(&ioat->timer, 1);
401e974f91cSConrad Meyer 
402faefad9cSConrad Meyer 	/* Establish lock order for Witness */
403faefad9cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
404faefad9cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
405faefad9cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
406faefad9cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
407faefad9cSConrad Meyer 
408e974f91cSConrad Meyer 	ioat->is_resize_pending = FALSE;
409e974f91cSConrad Meyer 	ioat->is_completion_pending = FALSE;
410e974f91cSConrad Meyer 	ioat->is_reset_pending = FALSE;
411e974f91cSConrad Meyer 	ioat->is_channel_running = FALSE;
412e974f91cSConrad Meyer 
413e974f91cSConrad Meyer 	bus_dma_tag_create(bus_get_dma_tag(ioat->device), sizeof(uint64_t), 0x0,
414e974f91cSConrad Meyer 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
415e974f91cSConrad Meyer 	    sizeof(uint64_t), 1, sizeof(uint64_t), 0, NULL, NULL,
416e974f91cSConrad Meyer 	    &ioat->comp_update_tag);
417e974f91cSConrad Meyer 
418e974f91cSConrad Meyer 	error = bus_dmamem_alloc(ioat->comp_update_tag,
419e974f91cSConrad Meyer 	    (void **)&ioat->comp_update, BUS_DMA_ZERO, &ioat->comp_update_map);
420e974f91cSConrad Meyer 	if (ioat->comp_update == NULL)
421e974f91cSConrad Meyer 		return (ENOMEM);
422e974f91cSConrad Meyer 
423e974f91cSConrad Meyer 	error = bus_dmamap_load(ioat->comp_update_tag, ioat->comp_update_map,
424e974f91cSConrad Meyer 	    ioat->comp_update, sizeof(uint64_t), ioat_comp_update_map, ioat,
425e974f91cSConrad Meyer 	    0);
426e974f91cSConrad Meyer 	if (error != 0)
427e974f91cSConrad Meyer 		return (error);
428e974f91cSConrad Meyer 
429e974f91cSConrad Meyer 	ioat->ring_size_order = IOAT_MIN_ORDER;
430e974f91cSConrad Meyer 
431e974f91cSConrad Meyer 	num_descriptors = 1 << ioat->ring_size_order;
432e974f91cSConrad Meyer 
433e974f91cSConrad Meyer 	bus_dma_tag_create(bus_get_dma_tag(ioat->device), 0x40, 0x0,
434e974f91cSConrad Meyer 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
435e974f91cSConrad Meyer 	    sizeof(struct ioat_dma_hw_descriptor), 1,
436e974f91cSConrad Meyer 	    sizeof(struct ioat_dma_hw_descriptor), 0, NULL, NULL,
437e974f91cSConrad Meyer 	    &ioat->hw_desc_tag);
438e974f91cSConrad Meyer 
439e974f91cSConrad Meyer 	ioat->ring = malloc(num_descriptors * sizeof(*ring), M_IOAT,
440bf8553eaSConrad Meyer 	    M_ZERO | M_WAITOK);
441e974f91cSConrad Meyer 	if (ioat->ring == NULL)
442e974f91cSConrad Meyer 		return (ENOMEM);
443e974f91cSConrad Meyer 
444e974f91cSConrad Meyer 	ring = ioat->ring;
445e974f91cSConrad Meyer 	for (i = 0; i < num_descriptors; i++) {
446bf8553eaSConrad Meyer 		ring[i] = ioat_alloc_ring_entry(ioat, M_WAITOK);
447e974f91cSConrad Meyer 		if (ring[i] == NULL)
448e974f91cSConrad Meyer 			return (ENOMEM);
449e974f91cSConrad Meyer 
450e974f91cSConrad Meyer 		ring[i]->id = i;
451e974f91cSConrad Meyer 	}
452e974f91cSConrad Meyer 
453e974f91cSConrad Meyer 	for (i = 0; i < num_descriptors - 1; i++) {
454e974f91cSConrad Meyer 		next = ring[i + 1];
455e974f91cSConrad Meyer 		dma_hw_desc = ring[i]->u.dma;
456e974f91cSConrad Meyer 
457e974f91cSConrad Meyer 		dma_hw_desc->next = next->hw_desc_bus_addr;
458e974f91cSConrad Meyer 	}
459e974f91cSConrad Meyer 
460e974f91cSConrad Meyer 	ring[i]->u.dma->next = ring[0]->hw_desc_bus_addr;
461e974f91cSConrad Meyer 
462bf8553eaSConrad Meyer 	ioat->head = ioat->hw_head = 0;
463e974f91cSConrad Meyer 	ioat->tail = 0;
464e974f91cSConrad Meyer 	ioat->last_seen = 0;
465e974f91cSConrad Meyer 	return (0);
466e974f91cSConrad Meyer }
467e974f91cSConrad Meyer 
468e974f91cSConrad Meyer static int
469e974f91cSConrad Meyer ioat_map_pci_bar(struct ioat_softc *ioat)
470e974f91cSConrad Meyer {
471e974f91cSConrad Meyer 
472e974f91cSConrad Meyer 	ioat->pci_resource_id = PCIR_BAR(0);
473e88e14b9SConrad Meyer 	ioat->pci_resource = bus_alloc_resource_any(ioat->device,
474e88e14b9SConrad Meyer 	    SYS_RES_MEMORY, &ioat->pci_resource_id, RF_ACTIVE);
475e974f91cSConrad Meyer 
476e974f91cSConrad Meyer 	if (ioat->pci_resource == NULL) {
477e974f91cSConrad Meyer 		ioat_log_message(0, "unable to allocate pci resource\n");
478e974f91cSConrad Meyer 		return (ENODEV);
479e974f91cSConrad Meyer 	}
480e974f91cSConrad Meyer 
481e974f91cSConrad Meyer 	ioat->pci_bus_tag = rman_get_bustag(ioat->pci_resource);
482e974f91cSConrad Meyer 	ioat->pci_bus_handle = rman_get_bushandle(ioat->pci_resource);
483e974f91cSConrad Meyer 	return (0);
484e974f91cSConrad Meyer }
485e974f91cSConrad Meyer 
486e974f91cSConrad Meyer static void
487e974f91cSConrad Meyer ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg, int error)
488e974f91cSConrad Meyer {
489e974f91cSConrad Meyer 	struct ioat_softc *ioat = arg;
490e974f91cSConrad Meyer 
491cea5b880SConrad Meyer 	KASSERT(error == 0, ("%s: error:%d", __func__, error));
492e974f91cSConrad Meyer 	ioat->comp_update_bus_addr = seg[0].ds_addr;
493e974f91cSConrad Meyer }
494e974f91cSConrad Meyer 
495e974f91cSConrad Meyer static void
496e974f91cSConrad Meyer ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
497e974f91cSConrad Meyer {
498e974f91cSConrad Meyer 	bus_addr_t *baddr;
499e974f91cSConrad Meyer 
500cea5b880SConrad Meyer 	KASSERT(error == 0, ("%s: error:%d", __func__, error));
501e974f91cSConrad Meyer 	baddr = arg;
502e974f91cSConrad Meyer 	*baddr = segs->ds_addr;
503e974f91cSConrad Meyer }
504e974f91cSConrad Meyer 
505e974f91cSConrad Meyer /*
506e974f91cSConrad Meyer  * Interrupt setup and handlers
507e974f91cSConrad Meyer  */
508e974f91cSConrad Meyer static int
5094253ea50SConrad Meyer ioat_setup_intr(struct ioat_softc *ioat)
510e974f91cSConrad Meyer {
511e974f91cSConrad Meyer 	uint32_t num_vectors;
512e974f91cSConrad Meyer 	int error;
513e974f91cSConrad Meyer 	boolean_t use_msix;
514e974f91cSConrad Meyer 	boolean_t force_legacy_interrupts;
515e974f91cSConrad Meyer 
516e974f91cSConrad Meyer 	use_msix = FALSE;
517e974f91cSConrad Meyer 	force_legacy_interrupts = FALSE;
518e974f91cSConrad Meyer 
519e974f91cSConrad Meyer 	if (!g_force_legacy_interrupts && pci_msix_count(ioat->device) >= 1) {
520e974f91cSConrad Meyer 		num_vectors = 1;
521e974f91cSConrad Meyer 		pci_alloc_msix(ioat->device, &num_vectors);
522e974f91cSConrad Meyer 		if (num_vectors == 1)
523e974f91cSConrad Meyer 			use_msix = TRUE;
524e974f91cSConrad Meyer 	}
525e974f91cSConrad Meyer 
526e974f91cSConrad Meyer 	if (use_msix) {
527e974f91cSConrad Meyer 		ioat->rid = 1;
528e974f91cSConrad Meyer 		ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
529e974f91cSConrad Meyer 		    &ioat->rid, RF_ACTIVE);
530e974f91cSConrad Meyer 	} else {
531e974f91cSConrad Meyer 		ioat->rid = 0;
532e974f91cSConrad Meyer 		ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
533e974f91cSConrad Meyer 		    &ioat->rid, RF_SHAREABLE | RF_ACTIVE);
534e974f91cSConrad Meyer 	}
535e974f91cSConrad Meyer 	if (ioat->res == NULL) {
536e974f91cSConrad Meyer 		ioat_log_message(0, "bus_alloc_resource failed\n");
537e974f91cSConrad Meyer 		return (ENOMEM);
538e974f91cSConrad Meyer 	}
539e974f91cSConrad Meyer 
540e974f91cSConrad Meyer 	ioat->tag = NULL;
541e974f91cSConrad Meyer 	error = bus_setup_intr(ioat->device, ioat->res, INTR_MPSAFE |
542e974f91cSConrad Meyer 	    INTR_TYPE_MISC, NULL, ioat_interrupt_handler, ioat, &ioat->tag);
543e974f91cSConrad Meyer 	if (error != 0) {
544e974f91cSConrad Meyer 		ioat_log_message(0, "bus_setup_intr failed\n");
545e974f91cSConrad Meyer 		return (error);
546e974f91cSConrad Meyer 	}
547e974f91cSConrad Meyer 
548e974f91cSConrad Meyer 	ioat_write_intrctrl(ioat, IOAT_INTRCTRL_MASTER_INT_EN);
549e974f91cSConrad Meyer 	return (0);
550e974f91cSConrad Meyer }
551e974f91cSConrad Meyer 
5524253ea50SConrad Meyer static boolean_t
5530d1a05d9SConrad Meyer ioat_model_resets_msix(struct ioat_softc *ioat)
5544253ea50SConrad Meyer {
5554253ea50SConrad Meyer 	u_int32_t pciid;
5564253ea50SConrad Meyer 
5574253ea50SConrad Meyer 	pciid = pci_get_devid(ioat->device);
5584253ea50SConrad Meyer 	switch (pciid) {
5590d1a05d9SConrad Meyer 		/* BWD: */
5600d1a05d9SConrad Meyer 	case 0x0c508086:
5610d1a05d9SConrad Meyer 	case 0x0c518086:
5620d1a05d9SConrad Meyer 	case 0x0c528086:
5630d1a05d9SConrad Meyer 	case 0x0c538086:
5640d1a05d9SConrad Meyer 		/* BDXDE: */
5654253ea50SConrad Meyer 	case 0x6f508086:
5664253ea50SConrad Meyer 	case 0x6f518086:
5674253ea50SConrad Meyer 	case 0x6f528086:
5684253ea50SConrad Meyer 	case 0x6f538086:
5694253ea50SConrad Meyer 		return (TRUE);
5704253ea50SConrad Meyer 	}
5714253ea50SConrad Meyer 
5724253ea50SConrad Meyer 	return (FALSE);
5734253ea50SConrad Meyer }
5744253ea50SConrad Meyer 
575e974f91cSConrad Meyer static void
576e974f91cSConrad Meyer ioat_interrupt_handler(void *arg)
577e974f91cSConrad Meyer {
578e974f91cSConrad Meyer 	struct ioat_softc *ioat = arg;
579e974f91cSConrad Meyer 
580e974f91cSConrad Meyer 	ioat_process_events(ioat);
581e974f91cSConrad Meyer }
582e974f91cSConrad Meyer 
583faefad9cSConrad Meyer static int
584faefad9cSConrad Meyer chanerr_to_errno(uint32_t chanerr)
585faefad9cSConrad Meyer {
586faefad9cSConrad Meyer 
587faefad9cSConrad Meyer 	if (chanerr == 0)
588faefad9cSConrad Meyer 		return (0);
589faefad9cSConrad Meyer 	if ((chanerr & (IOAT_CHANERR_XSADDERR | IOAT_CHANERR_XDADDERR)) != 0)
590faefad9cSConrad Meyer 		return (EFAULT);
591faefad9cSConrad Meyer 	if ((chanerr & (IOAT_CHANERR_RDERR | IOAT_CHANERR_WDERR)) != 0)
592faefad9cSConrad Meyer 		return (EIO);
593faefad9cSConrad Meyer 	/* This one is probably our fault: */
594faefad9cSConrad Meyer 	if ((chanerr & IOAT_CHANERR_NDADDERR) != 0)
595faefad9cSConrad Meyer 		return (EIO);
596faefad9cSConrad Meyer 	return (EIO);
597faefad9cSConrad Meyer }
598faefad9cSConrad Meyer 
599e974f91cSConrad Meyer static void
600e974f91cSConrad Meyer ioat_process_events(struct ioat_softc *ioat)
601e974f91cSConrad Meyer {
602e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
603e974f91cSConrad Meyer 	struct bus_dmadesc *dmadesc;
604e974f91cSConrad Meyer 	uint64_t comp_update, status;
605faefad9cSConrad Meyer 	uint32_t completed, chanerr;
606faefad9cSConrad Meyer 	int error;
607e974f91cSConrad Meyer 
608e974f91cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
609e974f91cSConrad Meyer 
610e974f91cSConrad Meyer 	completed = 0;
611e974f91cSConrad Meyer 	comp_update = *ioat->comp_update;
612e974f91cSConrad Meyer 	status = comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK;
613e974f91cSConrad Meyer 
61443fc1847SConrad Meyer 	CTR0(KTR_IOAT, __func__);
615e974f91cSConrad Meyer 
6164becebdfSConrad Meyer 	if (status == ioat->last_seen)
6174becebdfSConrad Meyer 		goto out;
618e974f91cSConrad Meyer 
619e974f91cSConrad Meyer 	while (1) {
620e974f91cSConrad Meyer 		desc = ioat_get_ring_entry(ioat, ioat->tail);
621e974f91cSConrad Meyer 		dmadesc = &desc->bus_dmadesc;
62243fc1847SConrad Meyer 		CTR1(KTR_IOAT, "completing desc %d", ioat->tail);
623e974f91cSConrad Meyer 
624faefad9cSConrad Meyer 		if (dmadesc->callback_fn != NULL)
625faefad9cSConrad Meyer 			dmadesc->callback_fn(dmadesc->callback_arg, 0);
626e974f91cSConrad Meyer 
627466b3540SConrad Meyer 		completed++;
628e974f91cSConrad Meyer 		ioat->tail++;
629e974f91cSConrad Meyer 		if (desc->hw_desc_bus_addr == status)
630e974f91cSConrad Meyer 			break;
631e974f91cSConrad Meyer 	}
632e974f91cSConrad Meyer 
633e974f91cSConrad Meyer 	ioat->last_seen = desc->hw_desc_bus_addr;
634e974f91cSConrad Meyer 
635e974f91cSConrad Meyer 	if (ioat->head == ioat->tail) {
636e974f91cSConrad Meyer 		ioat->is_completion_pending = FALSE;
637fe720f5aSConrad Meyer 		callout_reset(&ioat->timer, IOAT_INTR_TIMO,
638fe720f5aSConrad Meyer 		    ioat_timer_callback, ioat);
639e974f91cSConrad Meyer 	}
640e974f91cSConrad Meyer 
6414becebdfSConrad Meyer out:
642e974f91cSConrad Meyer 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
643e974f91cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
644466b3540SConrad Meyer 
645466b3540SConrad Meyer 	ioat_putn(ioat, completed, IOAT_ACTIVE_DESCR_REF);
646bf8553eaSConrad Meyer 	wakeup(&ioat->tail);
647faefad9cSConrad Meyer 
648faefad9cSConrad Meyer 	if (!is_ioat_halted(comp_update))
649faefad9cSConrad Meyer 		return;
650faefad9cSConrad Meyer 
651faefad9cSConrad Meyer 	/*
652faefad9cSConrad Meyer 	 * Fatal programming error on this DMA channel.  Flush any outstanding
653faefad9cSConrad Meyer 	 * work with error status and restart the engine.
654faefad9cSConrad Meyer 	 */
655faefad9cSConrad Meyer 	ioat_log_message(0, "Channel halted due to fatal programming error\n");
656faefad9cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
657faefad9cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
658faefad9cSConrad Meyer 	ioat->quiescing = TRUE;
659faefad9cSConrad Meyer 
660faefad9cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
661faefad9cSConrad Meyer 	ioat_halted_debug(ioat, chanerr);
662faefad9cSConrad Meyer 
663faefad9cSConrad Meyer 	while (ioat_get_active(ioat) > 0) {
664faefad9cSConrad Meyer 		desc = ioat_get_ring_entry(ioat, ioat->tail);
665faefad9cSConrad Meyer 		dmadesc = &desc->bus_dmadesc;
666faefad9cSConrad Meyer 		CTR1(KTR_IOAT, "completing err desc %d", ioat->tail);
667faefad9cSConrad Meyer 
668faefad9cSConrad Meyer 		if (dmadesc->callback_fn != NULL)
669faefad9cSConrad Meyer 			dmadesc->callback_fn(dmadesc->callback_arg,
670faefad9cSConrad Meyer 			    chanerr_to_errno(chanerr));
671faefad9cSConrad Meyer 
672faefad9cSConrad Meyer 		ioat_putn_locked(ioat, 1, IOAT_ACTIVE_DESCR_REF);
673faefad9cSConrad Meyer 		ioat->tail++;
674faefad9cSConrad Meyer 	}
675faefad9cSConrad Meyer 
676faefad9cSConrad Meyer 	/* Clear error status */
677faefad9cSConrad Meyer 	ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
678faefad9cSConrad Meyer 
679faefad9cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
680faefad9cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
681faefad9cSConrad Meyer 
682faefad9cSConrad Meyer 	ioat_log_message(0, "Resetting channel to recover from error\n");
683faefad9cSConrad Meyer 	error = ioat_reset_hw(ioat);
684faefad9cSConrad Meyer 	KASSERT(error == 0, ("%s: reset failed: %d", __func__, error));
685e974f91cSConrad Meyer }
686e974f91cSConrad Meyer 
687e974f91cSConrad Meyer /*
688e974f91cSConrad Meyer  * User API functions
689e974f91cSConrad Meyer  */
690e974f91cSConrad Meyer bus_dmaengine_t
691e974f91cSConrad Meyer ioat_get_dmaengine(uint32_t index)
692e974f91cSConrad Meyer {
6935f77bd3eSConrad Meyer 	struct ioat_softc *sc;
694e974f91cSConrad Meyer 
695466b3540SConrad Meyer 	if (index >= ioat_channel_index)
696e974f91cSConrad Meyer 		return (NULL);
6975f77bd3eSConrad Meyer 
6985f77bd3eSConrad Meyer 	sc = ioat_channel[index];
6995f77bd3eSConrad Meyer 	if (sc == NULL || sc->quiescing)
7005f77bd3eSConrad Meyer 		return (NULL);
7015f77bd3eSConrad Meyer 
7025f77bd3eSConrad Meyer 	return (&ioat_get(sc, IOAT_DMAENGINE_REF)->dmaengine);
703466b3540SConrad Meyer }
704466b3540SConrad Meyer 
705466b3540SConrad Meyer void
706466b3540SConrad Meyer ioat_put_dmaengine(bus_dmaengine_t dmaengine)
707466b3540SConrad Meyer {
708466b3540SConrad Meyer 	struct ioat_softc *ioat;
709466b3540SConrad Meyer 
710466b3540SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
711466b3540SConrad Meyer 	ioat_put(ioat, IOAT_DMAENGINE_REF);
712e974f91cSConrad Meyer }
713e974f91cSConrad Meyer 
714e974f91cSConrad Meyer void
715e974f91cSConrad Meyer ioat_acquire(bus_dmaengine_t dmaengine)
716e974f91cSConrad Meyer {
717e974f91cSConrad Meyer 	struct ioat_softc *ioat;
718e974f91cSConrad Meyer 
719e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
720e974f91cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
72143fc1847SConrad Meyer 	CTR0(KTR_IOAT, __func__);
722e974f91cSConrad Meyer }
723e974f91cSConrad Meyer 
724e974f91cSConrad Meyer void
725e974f91cSConrad Meyer ioat_release(bus_dmaengine_t dmaengine)
726e974f91cSConrad Meyer {
727e974f91cSConrad Meyer 	struct ioat_softc *ioat;
728e974f91cSConrad Meyer 
729e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
73043fc1847SConrad Meyer 	CTR0(KTR_IOAT, __func__);
731bf8553eaSConrad Meyer 	ioat_write_2(ioat, IOAT_DMACOUNT_OFFSET, (uint16_t)ioat->hw_head);
732e974f91cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
733e974f91cSConrad Meyer }
734e974f91cSConrad Meyer 
7359e3bbf26SConrad Meyer static struct ioat_descriptor *
7369e3bbf26SConrad Meyer ioat_op_generic(struct ioat_softc *ioat, uint8_t op,
7379e3bbf26SConrad Meyer     uint32_t size, uint64_t src, uint64_t dst,
7389e3bbf26SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg,
7399e3bbf26SConrad Meyer     uint32_t flags)
740e974f91cSConrad Meyer {
7419e3bbf26SConrad Meyer 	struct ioat_generic_hw_descriptor *hw_desc;
742e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
743bf8553eaSConrad Meyer 	int mflags;
744e974f91cSConrad Meyer 
7459e3bbf26SConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
7469e3bbf26SConrad Meyer 
747e974f91cSConrad Meyer 	KASSERT((flags & ~DMA_ALL_FLAGS) == 0, ("Unrecognized flag(s): %#x",
748e974f91cSConrad Meyer 		flags & ~DMA_ALL_FLAGS));
749bf8553eaSConrad Meyer 	if ((flags & DMA_NO_WAIT) != 0)
750bf8553eaSConrad Meyer 		mflags = M_NOWAIT;
751bf8553eaSConrad Meyer 	else
752bf8553eaSConrad Meyer 		mflags = M_WAITOK;
753e974f91cSConrad Meyer 
7549e3bbf26SConrad Meyer 	if (size > ioat->max_xfer_size) {
7559e3bbf26SConrad Meyer 		ioat_log_message(0, "%s: max_xfer_size = %d, requested = %u\n",
7569e3bbf26SConrad Meyer 		    __func__, ioat->max_xfer_size, (unsigned)size);
7579e3bbf26SConrad Meyer 		return (NULL);
7589e3bbf26SConrad Meyer 	}
759e974f91cSConrad Meyer 
760bf8553eaSConrad Meyer 	if (ioat_reserve_space(ioat, 1, mflags) != 0)
761e974f91cSConrad Meyer 		return (NULL);
762e974f91cSConrad Meyer 
763e974f91cSConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->head);
7649e3bbf26SConrad Meyer 	hw_desc = desc->u.generic;
765e974f91cSConrad Meyer 
766e974f91cSConrad Meyer 	hw_desc->u.control_raw = 0;
7679e3bbf26SConrad Meyer 	hw_desc->u.control_generic.op = op;
7689e3bbf26SConrad Meyer 	hw_desc->u.control_generic.completion_update = 1;
769e974f91cSConrad Meyer 
770e974f91cSConrad Meyer 	if ((flags & DMA_INT_EN) != 0)
7719e3bbf26SConrad Meyer 		hw_desc->u.control_generic.int_enable = 1;
772e974f91cSConrad Meyer 
7739e3bbf26SConrad Meyer 	hw_desc->size = size;
7749e3bbf26SConrad Meyer 	hw_desc->src_addr = src;
7759e3bbf26SConrad Meyer 	hw_desc->dest_addr = dst;
776e974f91cSConrad Meyer 
777e974f91cSConrad Meyer 	desc->bus_dmadesc.callback_fn = callback_fn;
778e974f91cSConrad Meyer 	desc->bus_dmadesc.callback_arg = callback_arg;
7799e3bbf26SConrad Meyer 	return (desc);
7809e3bbf26SConrad Meyer }
781e974f91cSConrad Meyer 
7829e3bbf26SConrad Meyer struct bus_dmadesc *
7839e3bbf26SConrad Meyer ioat_null(bus_dmaengine_t dmaengine, bus_dmaengine_callback_t callback_fn,
7849e3bbf26SConrad Meyer     void *callback_arg, uint32_t flags)
7859e3bbf26SConrad Meyer {
7869e3bbf26SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
7879e3bbf26SConrad Meyer 	struct ioat_descriptor *desc;
7889e3bbf26SConrad Meyer 	struct ioat_softc *ioat;
7899e3bbf26SConrad Meyer 
7909e3bbf26SConrad Meyer 	CTR0(KTR_IOAT, __func__);
7919e3bbf26SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
7929e3bbf26SConrad Meyer 
7939e3bbf26SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, 8, 0, 0, callback_fn,
7949e3bbf26SConrad Meyer 	    callback_arg, flags);
7959e3bbf26SConrad Meyer 	if (desc == NULL)
7969e3bbf26SConrad Meyer 		return (NULL);
7979e3bbf26SConrad Meyer 
7989e3bbf26SConrad Meyer 	hw_desc = desc->u.dma;
7999e3bbf26SConrad Meyer 	hw_desc->u.control.null = 1;
800e974f91cSConrad Meyer 	ioat_submit_single(ioat);
801e974f91cSConrad Meyer 	return (&desc->bus_dmadesc);
802e974f91cSConrad Meyer }
803e974f91cSConrad Meyer 
804e974f91cSConrad Meyer struct bus_dmadesc *
805e974f91cSConrad Meyer ioat_copy(bus_dmaengine_t dmaengine, bus_addr_t dst,
806e974f91cSConrad Meyer     bus_addr_t src, bus_size_t len, bus_dmaengine_callback_t callback_fn,
807e974f91cSConrad Meyer     void *callback_arg, uint32_t flags)
808e974f91cSConrad Meyer {
809e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
8109e3bbf26SConrad Meyer 	struct ioat_descriptor *desc;
811e974f91cSConrad Meyer 	struct ioat_softc *ioat;
812e974f91cSConrad Meyer 
8139e3bbf26SConrad Meyer 	CTR0(KTR_IOAT, __func__);
814e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
815e974f91cSConrad Meyer 
8169e3bbf26SConrad Meyer 	if (((src | dst) & (0xffffull << 48)) != 0) {
8179e3bbf26SConrad Meyer 		ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n",
8189e3bbf26SConrad Meyer 		    __func__);
819e974f91cSConrad Meyer 		return (NULL);
820e974f91cSConrad Meyer 	}
821e974f91cSConrad Meyer 
8229e3bbf26SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, len, src, dst, callback_fn,
8239e3bbf26SConrad Meyer 	    callback_arg, flags);
8249e3bbf26SConrad Meyer 	if (desc == NULL)
825e974f91cSConrad Meyer 		return (NULL);
826e974f91cSConrad Meyer 
827e974f91cSConrad Meyer 	hw_desc = desc->u.dma;
828e974f91cSConrad Meyer 	if (g_ioat_debug_level >= 3)
829e974f91cSConrad Meyer 		dump_descriptor(hw_desc);
830e974f91cSConrad Meyer 
831e974f91cSConrad Meyer 	ioat_submit_single(ioat);
832e974f91cSConrad Meyer 	return (&desc->bus_dmadesc);
833e974f91cSConrad Meyer }
834e974f91cSConrad Meyer 
8352a4fd6b1SConrad Meyer struct bus_dmadesc *
8362a4fd6b1SConrad Meyer ioat_blockfill(bus_dmaengine_t dmaengine, bus_addr_t dst, uint64_t fillpattern,
8372a4fd6b1SConrad Meyer     bus_size_t len, bus_dmaengine_callback_t callback_fn, void *callback_arg,
8382a4fd6b1SConrad Meyer     uint32_t flags)
8392a4fd6b1SConrad Meyer {
8402a4fd6b1SConrad Meyer 	struct ioat_fill_hw_descriptor *hw_desc;
8412a4fd6b1SConrad Meyer 	struct ioat_descriptor *desc;
8422a4fd6b1SConrad Meyer 	struct ioat_softc *ioat;
8432a4fd6b1SConrad Meyer 
8442a4fd6b1SConrad Meyer 	CTR0(KTR_IOAT, __func__);
8452a4fd6b1SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
8462a4fd6b1SConrad Meyer 
8471693d27bSConrad Meyer 	if ((ioat->capabilities & IOAT_DMACAP_BFILL) == 0) {
8481693d27bSConrad Meyer 		ioat_log_message(0, "%s: Device lacks BFILL capability\n",
8491693d27bSConrad Meyer 		    __func__);
8501693d27bSConrad Meyer 		return (NULL);
8511693d27bSConrad Meyer 	}
8521693d27bSConrad Meyer 
8532a4fd6b1SConrad Meyer 	if ((dst & (0xffffull << 48)) != 0) {
8542a4fd6b1SConrad Meyer 		ioat_log_message(0, "%s: High 16 bits of dst invalid\n",
8552a4fd6b1SConrad Meyer 		    __func__);
8562a4fd6b1SConrad Meyer 		return (NULL);
8572a4fd6b1SConrad Meyer 	}
8582a4fd6b1SConrad Meyer 
8592a4fd6b1SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_FILL, len, fillpattern, dst,
8602a4fd6b1SConrad Meyer 	    callback_fn, callback_arg, flags);
8612a4fd6b1SConrad Meyer 	if (desc == NULL)
8622a4fd6b1SConrad Meyer 		return (NULL);
8632a4fd6b1SConrad Meyer 
8642a4fd6b1SConrad Meyer 	hw_desc = desc->u.fill;
8652a4fd6b1SConrad Meyer 	if (g_ioat_debug_level >= 3)
8662a4fd6b1SConrad Meyer 		dump_descriptor(hw_desc);
8672a4fd6b1SConrad Meyer 
8682a4fd6b1SConrad Meyer 	ioat_submit_single(ioat);
8692a4fd6b1SConrad Meyer 	return (&desc->bus_dmadesc);
8702a4fd6b1SConrad Meyer }
8712a4fd6b1SConrad Meyer 
872e974f91cSConrad Meyer /*
873e974f91cSConrad Meyer  * Ring Management
874e974f91cSConrad Meyer  */
875e974f91cSConrad Meyer static inline uint32_t
876e974f91cSConrad Meyer ioat_get_active(struct ioat_softc *ioat)
877e974f91cSConrad Meyer {
878e974f91cSConrad Meyer 
879e974f91cSConrad Meyer 	return ((ioat->head - ioat->tail) & ((1 << ioat->ring_size_order) - 1));
880e974f91cSConrad Meyer }
881e974f91cSConrad Meyer 
882e974f91cSConrad Meyer static inline uint32_t
883e974f91cSConrad Meyer ioat_get_ring_space(struct ioat_softc *ioat)
884e974f91cSConrad Meyer {
885e974f91cSConrad Meyer 
886e974f91cSConrad Meyer 	return ((1 << ioat->ring_size_order) - ioat_get_active(ioat) - 1);
887e974f91cSConrad Meyer }
888e974f91cSConrad Meyer 
889e974f91cSConrad Meyer static struct ioat_descriptor *
890bf8553eaSConrad Meyer ioat_alloc_ring_entry(struct ioat_softc *ioat, int mflags)
891e974f91cSConrad Meyer {
8929e3bbf26SConrad Meyer 	struct ioat_generic_hw_descriptor *hw_desc;
893e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
894bf8553eaSConrad Meyer 	int error, busdmaflag;
895e974f91cSConrad Meyer 
896f46011aeSConrad Meyer 	error = ENOMEM;
897f46011aeSConrad Meyer 	hw_desc = NULL;
898f46011aeSConrad Meyer 
899bf8553eaSConrad Meyer 	if ((mflags & M_WAITOK) != 0)
900bf8553eaSConrad Meyer 		busdmaflag = BUS_DMA_WAITOK;
901bf8553eaSConrad Meyer 	else
902bf8553eaSConrad Meyer 		busdmaflag = BUS_DMA_NOWAIT;
903bf8553eaSConrad Meyer 
904bf8553eaSConrad Meyer 	desc = malloc(sizeof(*desc), M_IOAT, mflags);
905e974f91cSConrad Meyer 	if (desc == NULL)
906f46011aeSConrad Meyer 		goto out;
907e974f91cSConrad Meyer 
908f46011aeSConrad Meyer 	bus_dmamem_alloc(ioat->hw_desc_tag, (void **)&hw_desc,
909bf8553eaSConrad Meyer 	    BUS_DMA_ZERO | busdmaflag, &ioat->hw_desc_map);
910f46011aeSConrad Meyer 	if (hw_desc == NULL)
911f46011aeSConrad Meyer 		goto out;
912e974f91cSConrad Meyer 
913faefad9cSConrad Meyer 	memset(&desc->bus_dmadesc, 0, sizeof(desc->bus_dmadesc));
9149e3bbf26SConrad Meyer 	desc->u.generic = hw_desc;
915f46011aeSConrad Meyer 
916f46011aeSConrad Meyer 	error = bus_dmamap_load(ioat->hw_desc_tag, ioat->hw_desc_map, hw_desc,
917f46011aeSConrad Meyer 	    sizeof(*hw_desc), ioat_dmamap_cb, &desc->hw_desc_bus_addr,
918bf8553eaSConrad Meyer 	    busdmaflag);
919f46011aeSConrad Meyer 	if (error)
920f46011aeSConrad Meyer 		goto out;
921f46011aeSConrad Meyer 
922f46011aeSConrad Meyer out:
923f46011aeSConrad Meyer 	if (error) {
924f46011aeSConrad Meyer 		ioat_free_ring_entry(ioat, desc);
925f46011aeSConrad Meyer 		return (NULL);
926f46011aeSConrad Meyer 	}
927e974f91cSConrad Meyer 	return (desc);
928e974f91cSConrad Meyer }
929e974f91cSConrad Meyer 
930e974f91cSConrad Meyer static void
931e974f91cSConrad Meyer ioat_free_ring_entry(struct ioat_softc *ioat, struct ioat_descriptor *desc)
932e974f91cSConrad Meyer {
933e974f91cSConrad Meyer 
934e974f91cSConrad Meyer 	if (desc == NULL)
935e974f91cSConrad Meyer 		return;
936e974f91cSConrad Meyer 
9379e3bbf26SConrad Meyer 	if (desc->u.generic)
9389e3bbf26SConrad Meyer 		bus_dmamem_free(ioat->hw_desc_tag, desc->u.generic,
939e974f91cSConrad Meyer 		    ioat->hw_desc_map);
940e974f91cSConrad Meyer 	free(desc, M_IOAT);
941e974f91cSConrad Meyer }
942e974f91cSConrad Meyer 
943bf8553eaSConrad Meyer /*
944bf8553eaSConrad Meyer  * Reserves space in this IOAT descriptor ring by ensuring enough slots remain
945bf8553eaSConrad Meyer  * for 'num_descs'.
946bf8553eaSConrad Meyer  *
947bf8553eaSConrad Meyer  * If mflags contains M_WAITOK, blocks until enough space is available.
948bf8553eaSConrad Meyer  *
949bf8553eaSConrad Meyer  * Returns zero on success, or an errno on error.  If num_descs is beyond the
950bf8553eaSConrad Meyer  * maximum ring size, returns EINVAl; if allocation would block and mflags
951bf8553eaSConrad Meyer  * contains M_NOWAIT, returns EAGAIN.
952bf8553eaSConrad Meyer  *
953bf8553eaSConrad Meyer  * Must be called with the submit_lock held; returns with the lock held.  The
954bf8553eaSConrad Meyer  * lock may be dropped to allocate the ring.
955bf8553eaSConrad Meyer  *
956bf8553eaSConrad Meyer  * (The submit_lock is needed to add any entries to the ring, so callers are
957bf8553eaSConrad Meyer  * assured enough room is available.)
958bf8553eaSConrad Meyer  */
959e974f91cSConrad Meyer static int
960bf8553eaSConrad Meyer ioat_reserve_space(struct ioat_softc *ioat, uint32_t num_descs, int mflags)
961e974f91cSConrad Meyer {
962bf8553eaSConrad Meyer 	struct ioat_descriptor **new_ring;
963bf8553eaSConrad Meyer 	uint32_t order;
964bf8553eaSConrad Meyer 	int error;
965e974f91cSConrad Meyer 
966bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
967bf8553eaSConrad Meyer 	error = 0;
968e974f91cSConrad Meyer 
969bf8553eaSConrad Meyer 	if (num_descs < 1 || num_descs > (1 << IOAT_MAX_ORDER)) {
970bf8553eaSConrad Meyer 		error = EINVAL;
971bf8553eaSConrad Meyer 		goto out;
972e974f91cSConrad Meyer 	}
9735f77bd3eSConrad Meyer 	if (ioat->quiescing) {
9745f77bd3eSConrad Meyer 		error = ENXIO;
9755f77bd3eSConrad Meyer 		goto out;
9765f77bd3eSConrad Meyer 	}
977bf8553eaSConrad Meyer 
978bf8553eaSConrad Meyer 	for (;;) {
979bf8553eaSConrad Meyer 		if (ioat_get_ring_space(ioat) >= num_descs)
980bf8553eaSConrad Meyer 			goto out;
981bf8553eaSConrad Meyer 
982bf8553eaSConrad Meyer 		order = ioat->ring_size_order;
983bf8553eaSConrad Meyer 		if (ioat->is_resize_pending || order == IOAT_MAX_ORDER) {
984bf8553eaSConrad Meyer 			if ((mflags & M_WAITOK) != 0) {
985bf8553eaSConrad Meyer 				msleep(&ioat->tail, &ioat->submit_lock, 0,
986bf8553eaSConrad Meyer 				    "ioat_rsz", 0);
987bf8553eaSConrad Meyer 				continue;
988bf8553eaSConrad Meyer 			}
989bf8553eaSConrad Meyer 
990bf8553eaSConrad Meyer 			error = EAGAIN;
991bf8553eaSConrad Meyer 			break;
992bf8553eaSConrad Meyer 		}
993bf8553eaSConrad Meyer 
994bf8553eaSConrad Meyer 		ioat->is_resize_pending = TRUE;
995bf8553eaSConrad Meyer 		for (;;) {
996bf8553eaSConrad Meyer 			mtx_unlock(&ioat->submit_lock);
997bf8553eaSConrad Meyer 
998bf8553eaSConrad Meyer 			new_ring = ioat_prealloc_ring(ioat, 1 << (order + 1),
999bf8553eaSConrad Meyer 			    TRUE, mflags);
1000bf8553eaSConrad Meyer 
1001bf8553eaSConrad Meyer 			mtx_lock(&ioat->submit_lock);
1002bf8553eaSConrad Meyer 			KASSERT(ioat->ring_size_order == order,
1003bf8553eaSConrad Meyer 			    ("is_resize_pending should protect order"));
1004bf8553eaSConrad Meyer 
1005bf8553eaSConrad Meyer 			if (new_ring == NULL) {
1006bf8553eaSConrad Meyer 				KASSERT((mflags & M_WAITOK) == 0,
1007bf8553eaSConrad Meyer 				    ("allocation failed"));
1008bf8553eaSConrad Meyer 				error = EAGAIN;
1009bf8553eaSConrad Meyer 				break;
1010bf8553eaSConrad Meyer 			}
1011bf8553eaSConrad Meyer 
1012bf8553eaSConrad Meyer 			error = ring_grow(ioat, order, new_ring);
1013bf8553eaSConrad Meyer 			if (error == 0)
1014bf8553eaSConrad Meyer 				break;
1015bf8553eaSConrad Meyer 		}
1016bf8553eaSConrad Meyer 		ioat->is_resize_pending = FALSE;
1017bf8553eaSConrad Meyer 		wakeup(&ioat->tail);
1018bf8553eaSConrad Meyer 		if (error)
1019bf8553eaSConrad Meyer 			break;
1020bf8553eaSConrad Meyer 	}
1021bf8553eaSConrad Meyer 
1022bf8553eaSConrad Meyer out:
1023bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1024bf8553eaSConrad Meyer 	return (error);
1025bf8553eaSConrad Meyer }
1026bf8553eaSConrad Meyer 
1027bf8553eaSConrad Meyer static struct ioat_descriptor **
1028bf8553eaSConrad Meyer ioat_prealloc_ring(struct ioat_softc *ioat, uint32_t size, boolean_t need_dscr,
1029bf8553eaSConrad Meyer     int mflags)
1030bf8553eaSConrad Meyer {
1031bf8553eaSConrad Meyer 	struct ioat_descriptor **ring;
1032bf8553eaSConrad Meyer 	uint32_t i;
1033bf8553eaSConrad Meyer 	int error;
1034bf8553eaSConrad Meyer 
1035bf8553eaSConrad Meyer 	KASSERT(size > 0 && powerof2(size), ("bogus size"));
1036bf8553eaSConrad Meyer 
1037bf8553eaSConrad Meyer 	ring = malloc(size * sizeof(*ring), M_IOAT, M_ZERO | mflags);
1038bf8553eaSConrad Meyer 	if (ring == NULL)
1039bf8553eaSConrad Meyer 		return (NULL);
1040bf8553eaSConrad Meyer 
1041bf8553eaSConrad Meyer 	if (need_dscr) {
1042bf8553eaSConrad Meyer 		error = ENOMEM;
1043bf8553eaSConrad Meyer 		for (i = size / 2; i < size; i++) {
1044bf8553eaSConrad Meyer 			ring[i] = ioat_alloc_ring_entry(ioat, mflags);
1045bf8553eaSConrad Meyer 			if (ring[i] == NULL)
1046bf8553eaSConrad Meyer 				goto out;
1047bf8553eaSConrad Meyer 			ring[i]->id = i;
1048bf8553eaSConrad Meyer 		}
1049bf8553eaSConrad Meyer 	}
1050bf8553eaSConrad Meyer 	error = 0;
1051bf8553eaSConrad Meyer 
1052bf8553eaSConrad Meyer out:
1053bf8553eaSConrad Meyer 	if (error != 0 && ring != NULL) {
1054bf8553eaSConrad Meyer 		ioat_free_ring(ioat, size, ring);
1055bf8553eaSConrad Meyer 		ring = NULL;
1056bf8553eaSConrad Meyer 	}
1057bf8553eaSConrad Meyer 	return (ring);
1058bf8553eaSConrad Meyer }
1059bf8553eaSConrad Meyer 
1060bf8553eaSConrad Meyer static void
1061bf8553eaSConrad Meyer ioat_free_ring(struct ioat_softc *ioat, uint32_t size,
1062bf8553eaSConrad Meyer     struct ioat_descriptor **ring)
1063bf8553eaSConrad Meyer {
1064bf8553eaSConrad Meyer 	uint32_t i;
1065bf8553eaSConrad Meyer 
1066bf8553eaSConrad Meyer 	for (i = 0; i < size; i++) {
1067bf8553eaSConrad Meyer 		if (ring[i] != NULL)
1068bf8553eaSConrad Meyer 			ioat_free_ring_entry(ioat, ring[i]);
1069bf8553eaSConrad Meyer 	}
1070bf8553eaSConrad Meyer 	free(ring, M_IOAT);
1071e974f91cSConrad Meyer }
1072e974f91cSConrad Meyer 
1073e974f91cSConrad Meyer static struct ioat_descriptor *
1074e974f91cSConrad Meyer ioat_get_ring_entry(struct ioat_softc *ioat, uint32_t index)
1075e974f91cSConrad Meyer {
1076e974f91cSConrad Meyer 
1077e974f91cSConrad Meyer 	return (ioat->ring[index % (1 << ioat->ring_size_order)]);
1078e974f91cSConrad Meyer }
1079e974f91cSConrad Meyer 
1080bf8553eaSConrad Meyer static int
1081bf8553eaSConrad Meyer ring_grow(struct ioat_softc *ioat, uint32_t oldorder,
1082bf8553eaSConrad Meyer     struct ioat_descriptor **newring)
1083e974f91cSConrad Meyer {
1084bf8553eaSConrad Meyer 	struct ioat_descriptor *tmp, *next;
1085e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *hw;
1086bf8553eaSConrad Meyer 	uint32_t oldsize, newsize, head, tail, i, end;
1087bf8553eaSConrad Meyer 	int error;
1088e974f91cSConrad Meyer 
1089bf8553eaSConrad Meyer 	CTR0(KTR_IOAT, __func__);
1090e974f91cSConrad Meyer 
1091bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1092bf8553eaSConrad Meyer 
1093bf8553eaSConrad Meyer 	if (oldorder != ioat->ring_size_order || oldorder >= IOAT_MAX_ORDER) {
1094bf8553eaSConrad Meyer 		error = EINVAL;
1095bf8553eaSConrad Meyer 		goto out;
1096bf8553eaSConrad Meyer 	}
1097bf8553eaSConrad Meyer 
1098bf8553eaSConrad Meyer 	oldsize = (1 << oldorder);
1099bf8553eaSConrad Meyer 	newsize = (1 << (oldorder + 1));
1100bf8553eaSConrad Meyer 
1101bf8553eaSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
1102bf8553eaSConrad Meyer 
1103bf8553eaSConrad Meyer 	head = ioat->head & (oldsize - 1);
1104bf8553eaSConrad Meyer 	tail = ioat->tail & (oldsize - 1);
1105bf8553eaSConrad Meyer 
1106bf8553eaSConrad Meyer 	/* Copy old descriptors to new ring */
1107bf8553eaSConrad Meyer 	for (i = 0; i < oldsize; i++)
1108bf8553eaSConrad Meyer 		newring[i] = ioat->ring[i];
1109e974f91cSConrad Meyer 
1110e974f91cSConrad Meyer 	/*
1111bf8553eaSConrad Meyer 	 * If head has wrapped but tail hasn't, we must swap some descriptors
1112bf8553eaSConrad Meyer 	 * around so that tail can increment directly to head.
1113e974f91cSConrad Meyer 	 */
1114bf8553eaSConrad Meyer 	if (head < tail) {
1115bf8553eaSConrad Meyer 		for (i = 0; i <= head; i++) {
1116bf8553eaSConrad Meyer 			tmp = newring[oldsize + i];
1117e974f91cSConrad Meyer 
1118bf8553eaSConrad Meyer 			newring[oldsize + i] = newring[i];
1119bf8553eaSConrad Meyer 			newring[oldsize + i]->id = oldsize + i;
1120e974f91cSConrad Meyer 
1121bf8553eaSConrad Meyer 			newring[i] = tmp;
1122bf8553eaSConrad Meyer 			newring[i]->id = i;
1123bf8553eaSConrad Meyer 		}
1124bf8553eaSConrad Meyer 		head += oldsize;
1125e974f91cSConrad Meyer 	}
1126e974f91cSConrad Meyer 
1127bf8553eaSConrad Meyer 	KASSERT(head >= tail, ("invariants"));
1128e974f91cSConrad Meyer 
1129bf8553eaSConrad Meyer 	/* Head didn't wrap; we only need to link in oldsize..newsize */
1130bf8553eaSConrad Meyer 	if (head < oldsize) {
1131bf8553eaSConrad Meyer 		i = oldsize - 1;
1132bf8553eaSConrad Meyer 		end = newsize;
1133e974f91cSConrad Meyer 	} else {
1134bf8553eaSConrad Meyer 		/* Head did wrap; link newhead..newsize and 0..oldhead */
1135bf8553eaSConrad Meyer 		i = head;
1136bf8553eaSConrad Meyer 		end = newsize + (head - oldsize) + 1;
1137bf8553eaSConrad Meyer 	}
1138bf8553eaSConrad Meyer 
1139e974f91cSConrad Meyer 	/*
1140bf8553eaSConrad Meyer 	 * Fix up hardware ring, being careful not to trample the active
1141bf8553eaSConrad Meyer 	 * section (tail -> head).
1142e974f91cSConrad Meyer 	 */
1143bf8553eaSConrad Meyer 	for (; i < end; i++) {
1144bf8553eaSConrad Meyer 		KASSERT((i & (newsize - 1)) < tail ||
1145bf8553eaSConrad Meyer 		    (i & (newsize - 1)) >= head, ("trampling snake"));
1146e974f91cSConrad Meyer 
1147bf8553eaSConrad Meyer 		next = newring[(i + 1) & (newsize - 1)];
1148bf8553eaSConrad Meyer 		hw = newring[i & (newsize - 1)]->u.dma;
1149e974f91cSConrad Meyer 		hw->next = next->hw_desc_bus_addr;
1150e974f91cSConrad Meyer 	}
1151e974f91cSConrad Meyer 
1152e974f91cSConrad Meyer 	free(ioat->ring, M_IOAT);
1153bf8553eaSConrad Meyer 	ioat->ring = newring;
1154bf8553eaSConrad Meyer 	ioat->ring_size_order = oldorder + 1;
1155bf8553eaSConrad Meyer 	ioat->tail = tail;
1156bf8553eaSConrad Meyer 	ioat->head = head;
1157bf8553eaSConrad Meyer 	error = 0;
1158e974f91cSConrad Meyer 
1159bf8553eaSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
1160bf8553eaSConrad Meyer out:
1161bf8553eaSConrad Meyer 	if (error)
1162bf8553eaSConrad Meyer 		ioat_free_ring(ioat, (1 << (oldorder + 1)), newring);
1163bf8553eaSConrad Meyer 	return (error);
1164bf8553eaSConrad Meyer }
1165bf8553eaSConrad Meyer 
1166bf8553eaSConrad Meyer static int
1167bf8553eaSConrad Meyer ring_shrink(struct ioat_softc *ioat, uint32_t oldorder,
1168bf8553eaSConrad Meyer     struct ioat_descriptor **newring)
1169bf8553eaSConrad Meyer {
1170bf8553eaSConrad Meyer 	struct ioat_dma_hw_descriptor *hw;
1171bf8553eaSConrad Meyer 	struct ioat_descriptor *ent, *next;
1172bf8553eaSConrad Meyer 	uint32_t oldsize, newsize, current_idx, new_idx, i;
1173bf8553eaSConrad Meyer 	int error;
1174bf8553eaSConrad Meyer 
1175bf8553eaSConrad Meyer 	CTR0(KTR_IOAT, __func__);
1176bf8553eaSConrad Meyer 
1177bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1178bf8553eaSConrad Meyer 
1179bf8553eaSConrad Meyer 	if (oldorder != ioat->ring_size_order || oldorder <= IOAT_MIN_ORDER) {
1180bf8553eaSConrad Meyer 		error = EINVAL;
1181bf8553eaSConrad Meyer 		goto out_unlocked;
1182bf8553eaSConrad Meyer 	}
1183bf8553eaSConrad Meyer 
1184bf8553eaSConrad Meyer 	oldsize = (1 << oldorder);
1185bf8553eaSConrad Meyer 	newsize = (1 << (oldorder - 1));
1186bf8553eaSConrad Meyer 
1187bf8553eaSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
1188bf8553eaSConrad Meyer 
1189bf8553eaSConrad Meyer 	/* Can't shrink below current active set! */
1190bf8553eaSConrad Meyer 	if (ioat_get_active(ioat) >= newsize) {
1191bf8553eaSConrad Meyer 		error = ENOMEM;
1192bf8553eaSConrad Meyer 		goto out;
1193bf8553eaSConrad Meyer 	}
1194bf8553eaSConrad Meyer 
1195bf8553eaSConrad Meyer 	/*
1196bf8553eaSConrad Meyer 	 * Copy current descriptors to the new ring, dropping the removed
1197bf8553eaSConrad Meyer 	 * descriptors.
1198bf8553eaSConrad Meyer 	 */
1199bf8553eaSConrad Meyer 	for (i = 0; i < newsize; i++) {
1200bf8553eaSConrad Meyer 		current_idx = (ioat->tail + i) & (oldsize - 1);
1201bf8553eaSConrad Meyer 		new_idx = (ioat->tail + i) & (newsize - 1);
1202bf8553eaSConrad Meyer 
1203bf8553eaSConrad Meyer 		newring[new_idx] = ioat->ring[current_idx];
1204bf8553eaSConrad Meyer 		newring[new_idx]->id = new_idx;
1205bf8553eaSConrad Meyer 	}
1206bf8553eaSConrad Meyer 
1207bf8553eaSConrad Meyer 	/* Free deleted descriptors */
1208bf8553eaSConrad Meyer 	for (i = newsize; i < oldsize; i++) {
1209bf8553eaSConrad Meyer 		ent = ioat_get_ring_entry(ioat, ioat->tail + i);
1210bf8553eaSConrad Meyer 		ioat_free_ring_entry(ioat, ent);
1211bf8553eaSConrad Meyer 	}
1212bf8553eaSConrad Meyer 
1213bf8553eaSConrad Meyer 	/* Fix up hardware ring. */
1214bf8553eaSConrad Meyer 	hw = newring[(ioat->tail + newsize - 1) & (newsize - 1)]->u.dma;
1215bf8553eaSConrad Meyer 	next = newring[(ioat->tail + newsize) & (newsize - 1)];
1216bf8553eaSConrad Meyer 	hw->next = next->hw_desc_bus_addr;
1217bf8553eaSConrad Meyer 
1218bf8553eaSConrad Meyer 	free(ioat->ring, M_IOAT);
1219bf8553eaSConrad Meyer 	ioat->ring = newring;
1220bf8553eaSConrad Meyer 	ioat->ring_size_order = oldorder - 1;
1221bf8553eaSConrad Meyer 	error = 0;
1222bf8553eaSConrad Meyer 
1223bf8553eaSConrad Meyer out:
1224bf8553eaSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
1225bf8553eaSConrad Meyer out_unlocked:
1226bf8553eaSConrad Meyer 	if (error)
1227bf8553eaSConrad Meyer 		ioat_free_ring(ioat, (1 << (oldorder - 1)), newring);
1228bf8553eaSConrad Meyer 	return (error);
1229e974f91cSConrad Meyer }
1230e974f91cSConrad Meyer 
1231e974f91cSConrad Meyer static void
12328f274637SConrad Meyer ioat_halted_debug(struct ioat_softc *ioat, uint32_t chanerr)
1233e974f91cSConrad Meyer {
1234e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
12358f274637SConrad Meyer 
123659acd4baSConrad Meyer 	ioat_log_message(0, "Channel halted (%b)\n", (int)chanerr,
123759acd4baSConrad Meyer 	    IOAT_CHANERR_STR);
12388f274637SConrad Meyer 	if (chanerr == 0)
12398f274637SConrad Meyer 		return;
12408f274637SConrad Meyer 
1241faefad9cSConrad Meyer 	mtx_assert(&ioat->cleanup_lock, MA_OWNED);
1242faefad9cSConrad Meyer 
12438f274637SConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->tail + 0);
12448f274637SConrad Meyer 	dump_descriptor(desc->u.raw);
12458f274637SConrad Meyer 
12468f274637SConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->tail + 1);
12478f274637SConrad Meyer 	dump_descriptor(desc->u.raw);
12488f274637SConrad Meyer }
12498f274637SConrad Meyer 
12508f274637SConrad Meyer static void
12518f274637SConrad Meyer ioat_timer_callback(void *arg)
12528f274637SConrad Meyer {
1253bf8553eaSConrad Meyer 	struct ioat_descriptor **newring;
1254e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1255faefad9cSConrad Meyer 	uint32_t order;
1256e974f91cSConrad Meyer 
1257e974f91cSConrad Meyer 	ioat = arg;
1258fe720f5aSConrad Meyer 	ioat_log_message(1, "%s\n", __func__);
1259e974f91cSConrad Meyer 
1260e974f91cSConrad Meyer 	if (ioat->is_completion_pending) {
1261e974f91cSConrad Meyer 		ioat_process_events(ioat);
1262faefad9cSConrad Meyer 		return;
1263faefad9cSConrad Meyer 	}
1264faefad9cSConrad Meyer 
1265faefad9cSConrad Meyer 	/* Slowly scale the ring down if idle. */
1266e974f91cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
1267bf8553eaSConrad Meyer 	order = ioat->ring_size_order;
1268bf8553eaSConrad Meyer 	if (ioat->is_resize_pending || order == IOAT_MIN_ORDER) {
1269bf8553eaSConrad Meyer 		mtx_unlock(&ioat->submit_lock);
1270bf8553eaSConrad Meyer 		goto out;
1271bf8553eaSConrad Meyer 	}
1272bf8553eaSConrad Meyer 	ioat->is_resize_pending = TRUE;
1273e974f91cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
1274e974f91cSConrad Meyer 
1275bf8553eaSConrad Meyer 	newring = ioat_prealloc_ring(ioat, 1 << (order - 1), FALSE,
1276bf8553eaSConrad Meyer 	    M_NOWAIT);
1277bf8553eaSConrad Meyer 
1278bf8553eaSConrad Meyer 	mtx_lock(&ioat->submit_lock);
1279bf8553eaSConrad Meyer 	KASSERT(ioat->ring_size_order == order,
1280bf8553eaSConrad Meyer 	    ("resize_pending protects order"));
1281bf8553eaSConrad Meyer 
1282bf8553eaSConrad Meyer 	if (newring != NULL)
1283bf8553eaSConrad Meyer 		ring_shrink(ioat, order, newring);
1284bf8553eaSConrad Meyer 
1285bf8553eaSConrad Meyer 	ioat->is_resize_pending = FALSE;
1286bf8553eaSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
1287bf8553eaSConrad Meyer 
1288bf8553eaSConrad Meyer out:
1289e974f91cSConrad Meyer 	if (ioat->ring_size_order > IOAT_MIN_ORDER)
1290bf8553eaSConrad Meyer 		callout_reset(&ioat->timer, 10 * hz,
1291e974f91cSConrad Meyer 		    ioat_timer_callback, ioat);
1292e974f91cSConrad Meyer }
1293e974f91cSConrad Meyer 
1294e974f91cSConrad Meyer /*
1295e974f91cSConrad Meyer  * Support Functions
1296e974f91cSConrad Meyer  */
1297e974f91cSConrad Meyer static void
1298e974f91cSConrad Meyer ioat_submit_single(struct ioat_softc *ioat)
1299e974f91cSConrad Meyer {
1300e974f91cSConrad Meyer 
1301466b3540SConrad Meyer 	ioat_get(ioat, IOAT_ACTIVE_DESCR_REF);
1302e974f91cSConrad Meyer 	atomic_add_rel_int(&ioat->head, 1);
1303bf8553eaSConrad Meyer 	atomic_add_rel_int(&ioat->hw_head, 1);
1304e974f91cSConrad Meyer 
1305e974f91cSConrad Meyer 	if (!ioat->is_completion_pending) {
1306e974f91cSConrad Meyer 		ioat->is_completion_pending = TRUE;
1307fe720f5aSConrad Meyer 		callout_reset(&ioat->timer, IOAT_INTR_TIMO,
1308fe720f5aSConrad Meyer 		    ioat_timer_callback, ioat);
1309e974f91cSConrad Meyer 	}
1310e974f91cSConrad Meyer }
1311e974f91cSConrad Meyer 
1312e974f91cSConrad Meyer static int
1313e974f91cSConrad Meyer ioat_reset_hw(struct ioat_softc *ioat)
1314e974f91cSConrad Meyer {
1315e974f91cSConrad Meyer 	uint64_t status;
1316e974f91cSConrad Meyer 	uint32_t chanerr;
1317cea5b880SConrad Meyer 	unsigned timeout;
13185f77bd3eSConrad Meyer 	int error;
13195f77bd3eSConrad Meyer 
13205f77bd3eSConrad Meyer 	mtx_lock(IOAT_REFLK);
13215f77bd3eSConrad Meyer 	ioat->quiescing = TRUE;
13225f77bd3eSConrad Meyer 	ioat_drain_locked(ioat);
13235f77bd3eSConrad Meyer 	mtx_unlock(IOAT_REFLK);
1324e974f91cSConrad Meyer 
1325e974f91cSConrad Meyer 	status = ioat_get_chansts(ioat);
1326e974f91cSConrad Meyer 	if (is_ioat_active(status) || is_ioat_idle(status))
1327e974f91cSConrad Meyer 		ioat_suspend(ioat);
1328e974f91cSConrad Meyer 
1329e974f91cSConrad Meyer 	/* Wait at most 20 ms */
1330e974f91cSConrad Meyer 	for (timeout = 0; (is_ioat_active(status) || is_ioat_idle(status)) &&
1331e974f91cSConrad Meyer 	    timeout < 20; timeout++) {
1332e974f91cSConrad Meyer 		DELAY(1000);
1333e974f91cSConrad Meyer 		status = ioat_get_chansts(ioat);
1334e974f91cSConrad Meyer 	}
13355f77bd3eSConrad Meyer 	if (timeout == 20) {
13365f77bd3eSConrad Meyer 		error = ETIMEDOUT;
13375f77bd3eSConrad Meyer 		goto out;
13385f77bd3eSConrad Meyer 	}
1339e974f91cSConrad Meyer 
1340cea5b880SConrad Meyer 	KASSERT(ioat_get_active(ioat) == 0, ("active after quiesce"));
1341cea5b880SConrad Meyer 
1342e974f91cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
1343e974f91cSConrad Meyer 	ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
1344e974f91cSConrad Meyer 
1345e974f91cSConrad Meyer 	/*
1346e974f91cSConrad Meyer 	 * IOAT v3 workaround - CHANERRMSK_INT with 3E07h to masks out errors
1347e974f91cSConrad Meyer 	 *  that can cause stability issues for IOAT v3.
1348e974f91cSConrad Meyer 	 */
1349e974f91cSConrad Meyer 	pci_write_config(ioat->device, IOAT_CFG_CHANERRMASK_INT_OFFSET, 0x3e07,
1350e974f91cSConrad Meyer 	    4);
1351e974f91cSConrad Meyer 	chanerr = pci_read_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, 4);
1352e974f91cSConrad Meyer 	pci_write_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, chanerr, 4);
1353e974f91cSConrad Meyer 
13540d1a05d9SConrad Meyer 	/*
13550d1a05d9SConrad Meyer 	 * BDXDE and BWD models reset MSI-X registers on device reset.
13560d1a05d9SConrad Meyer 	 * Save/restore their contents manually.
13570d1a05d9SConrad Meyer 	 */
1358f7157235SConrad Meyer 	if (ioat_model_resets_msix(ioat)) {
1359f7157235SConrad Meyer 		ioat_log_message(1, "device resets MSI-X registers; saving\n");
13600d1a05d9SConrad Meyer 		pci_save_state(ioat->device);
1361f7157235SConrad Meyer 	}
13620d1a05d9SConrad Meyer 
1363e974f91cSConrad Meyer 	ioat_reset(ioat);
1364e974f91cSConrad Meyer 
1365e974f91cSConrad Meyer 	/* Wait at most 20 ms */
1366e974f91cSConrad Meyer 	for (timeout = 0; ioat_reset_pending(ioat) && timeout < 20; timeout++)
1367e974f91cSConrad Meyer 		DELAY(1000);
13685f77bd3eSConrad Meyer 	if (timeout == 20) {
13695f77bd3eSConrad Meyer 		error = ETIMEDOUT;
13705f77bd3eSConrad Meyer 		goto out;
13715f77bd3eSConrad Meyer 	}
1372e974f91cSConrad Meyer 
1373f7157235SConrad Meyer 	if (ioat_model_resets_msix(ioat)) {
1374f7157235SConrad Meyer 		ioat_log_message(1, "device resets registers; restored\n");
13750d1a05d9SConrad Meyer 		pci_restore_state(ioat->device);
1376f7157235SConrad Meyer 	}
13774253ea50SConrad Meyer 
1378cea5b880SConrad Meyer 	/* Reset attempts to return the hardware to "halted." */
1379cea5b880SConrad Meyer 	status = ioat_get_chansts(ioat);
1380cea5b880SConrad Meyer 	if (is_ioat_active(status) || is_ioat_idle(status)) {
1381cea5b880SConrad Meyer 		/* So this really shouldn't happen... */
1382cea5b880SConrad Meyer 		ioat_log_message(0, "Device is active after a reset?\n");
1383cea5b880SConrad Meyer 		ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
13845f77bd3eSConrad Meyer 		error = 0;
13855f77bd3eSConrad Meyer 		goto out;
1386e974f91cSConrad Meyer 	}
1387e974f91cSConrad Meyer 
1388cea5b880SConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
13895f77bd3eSConrad Meyer 	if (chanerr != 0) {
1390faefad9cSConrad Meyer 		mtx_lock(&ioat->cleanup_lock);
1391faefad9cSConrad Meyer 		ioat_halted_debug(ioat, chanerr);
1392faefad9cSConrad Meyer 		mtx_unlock(&ioat->cleanup_lock);
13935f77bd3eSConrad Meyer 		error = EIO;
13945f77bd3eSConrad Meyer 		goto out;
13955f77bd3eSConrad Meyer 	}
1396cea5b880SConrad Meyer 
1397cea5b880SConrad Meyer 	/*
1398cea5b880SConrad Meyer 	 * Bring device back online after reset.  Writing CHAINADDR brings the
1399cea5b880SConrad Meyer 	 * device back to active.
1400cea5b880SConrad Meyer 	 *
1401cea5b880SConrad Meyer 	 * The internal ring counter resets to zero, so we have to start over
1402cea5b880SConrad Meyer 	 * at zero as well.
1403cea5b880SConrad Meyer 	 */
1404bf8553eaSConrad Meyer 	ioat->tail = ioat->head = ioat->hw_head = 0;
1405cea5b880SConrad Meyer 	ioat->last_seen = 0;
1406cea5b880SConrad Meyer 
1407cea5b880SConrad Meyer 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
1408cea5b880SConrad Meyer 	ioat_write_chancmp(ioat, ioat->comp_update_bus_addr);
1409cea5b880SConrad Meyer 	ioat_write_chainaddr(ioat, ioat->ring[0]->hw_desc_bus_addr);
14105f77bd3eSConrad Meyer 	error = 0;
14115f77bd3eSConrad Meyer 
14125f77bd3eSConrad Meyer out:
14135f77bd3eSConrad Meyer 	mtx_lock(IOAT_REFLK);
14145f77bd3eSConrad Meyer 	ioat->quiescing = FALSE;
14155f77bd3eSConrad Meyer 	mtx_unlock(IOAT_REFLK);
14165f77bd3eSConrad Meyer 
14175f77bd3eSConrad Meyer 	if (error == 0)
14185f77bd3eSConrad Meyer 		error = ioat_start_channel(ioat);
14195f77bd3eSConrad Meyer 
14205f77bd3eSConrad Meyer 	return (error);
1421cea5b880SConrad Meyer }
1422cea5b880SConrad Meyer 
1423f7157235SConrad Meyer static int
1424faefad9cSConrad Meyer sysctl_handle_chansts(SYSCTL_HANDLER_ARGS)
1425faefad9cSConrad Meyer {
1426faefad9cSConrad Meyer 	struct ioat_softc *ioat;
1427faefad9cSConrad Meyer 	struct sbuf sb;
1428faefad9cSConrad Meyer 	uint64_t status;
1429faefad9cSConrad Meyer 	int error;
1430faefad9cSConrad Meyer 
1431faefad9cSConrad Meyer 	ioat = arg1;
1432faefad9cSConrad Meyer 
1433faefad9cSConrad Meyer 	status = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS;
1434faefad9cSConrad Meyer 
1435faefad9cSConrad Meyer 	sbuf_new_for_sysctl(&sb, NULL, 256, req);
1436faefad9cSConrad Meyer 	switch (status) {
1437faefad9cSConrad Meyer 	case IOAT_CHANSTS_ACTIVE:
1438faefad9cSConrad Meyer 		sbuf_printf(&sb, "ACTIVE");
1439faefad9cSConrad Meyer 		break;
1440faefad9cSConrad Meyer 	case IOAT_CHANSTS_IDLE:
1441faefad9cSConrad Meyer 		sbuf_printf(&sb, "IDLE");
1442faefad9cSConrad Meyer 		break;
1443faefad9cSConrad Meyer 	case IOAT_CHANSTS_SUSPENDED:
1444faefad9cSConrad Meyer 		sbuf_printf(&sb, "SUSPENDED");
1445faefad9cSConrad Meyer 		break;
1446faefad9cSConrad Meyer 	case IOAT_CHANSTS_HALTED:
1447faefad9cSConrad Meyer 		sbuf_printf(&sb, "HALTED");
1448faefad9cSConrad Meyer 		break;
1449faefad9cSConrad Meyer 	case IOAT_CHANSTS_ARMED:
1450faefad9cSConrad Meyer 		sbuf_printf(&sb, "ARMED");
1451faefad9cSConrad Meyer 		break;
1452faefad9cSConrad Meyer 	default:
1453faefad9cSConrad Meyer 		sbuf_printf(&sb, "UNKNOWN");
1454faefad9cSConrad Meyer 		break;
1455faefad9cSConrad Meyer 	}
1456faefad9cSConrad Meyer 	error = sbuf_finish(&sb);
1457faefad9cSConrad Meyer 	sbuf_delete(&sb);
1458faefad9cSConrad Meyer 
1459faefad9cSConrad Meyer 	if (error != 0 || req->newptr == NULL)
1460faefad9cSConrad Meyer 		return (error);
1461faefad9cSConrad Meyer 	return (EINVAL);
1462faefad9cSConrad Meyer }
1463faefad9cSConrad Meyer 
1464faefad9cSConrad Meyer static int
1465faefad9cSConrad Meyer sysctl_handle_error(SYSCTL_HANDLER_ARGS)
1466faefad9cSConrad Meyer {
1467faefad9cSConrad Meyer 	struct ioat_descriptor *desc;
1468faefad9cSConrad Meyer 	struct ioat_softc *ioat;
1469faefad9cSConrad Meyer 	int error, arg;
1470faefad9cSConrad Meyer 
1471faefad9cSConrad Meyer 	ioat = arg1;
1472faefad9cSConrad Meyer 
1473faefad9cSConrad Meyer 	arg = 0;
1474faefad9cSConrad Meyer 	error = SYSCTL_OUT(req, &arg, sizeof(arg));
1475faefad9cSConrad Meyer 	if (error != 0 || req->newptr == NULL)
1476faefad9cSConrad Meyer 		return (error);
1477faefad9cSConrad Meyer 
1478faefad9cSConrad Meyer 	error = SYSCTL_IN(req, &arg, sizeof(arg));
1479faefad9cSConrad Meyer 	if (error != 0)
1480faefad9cSConrad Meyer 		return (error);
1481faefad9cSConrad Meyer 
1482faefad9cSConrad Meyer 	if (arg != 0) {
1483faefad9cSConrad Meyer 		ioat_acquire(&ioat->dmaengine);
1484faefad9cSConrad Meyer 		desc = ioat_op_generic(ioat, IOAT_OP_COPY, 1,
1485faefad9cSConrad Meyer 		    0xffff000000000000ull, 0xffff000000000000ull, NULL, NULL,
1486faefad9cSConrad Meyer 		    0);
1487faefad9cSConrad Meyer 		if (desc == NULL)
1488faefad9cSConrad Meyer 			error = ENOMEM;
1489faefad9cSConrad Meyer 		else
1490faefad9cSConrad Meyer 			ioat_submit_single(ioat);
1491faefad9cSConrad Meyer 		ioat_release(&ioat->dmaengine);
1492faefad9cSConrad Meyer 	}
1493faefad9cSConrad Meyer 	return (error);
1494faefad9cSConrad Meyer }
1495faefad9cSConrad Meyer 
1496faefad9cSConrad Meyer static int
1497f7157235SConrad Meyer sysctl_handle_reset(SYSCTL_HANDLER_ARGS)
1498f7157235SConrad Meyer {
1499f7157235SConrad Meyer 	struct ioat_softc *ioat;
1500f7157235SConrad Meyer 	int error, arg;
1501f7157235SConrad Meyer 
1502f7157235SConrad Meyer 	ioat = arg1;
1503f7157235SConrad Meyer 
1504f7157235SConrad Meyer 	arg = 0;
1505f7157235SConrad Meyer 	error = SYSCTL_OUT(req, &arg, sizeof(arg));
1506f7157235SConrad Meyer 	if (error != 0 || req->newptr == NULL)
1507f7157235SConrad Meyer 		return (error);
1508f7157235SConrad Meyer 
1509f7157235SConrad Meyer 	error = SYSCTL_IN(req, &arg, sizeof(arg));
1510f7157235SConrad Meyer 	if (error != 0)
1511f7157235SConrad Meyer 		return (error);
1512f7157235SConrad Meyer 
1513f7157235SConrad Meyer 	if (arg != 0)
1514f7157235SConrad Meyer 		error = ioat_reset_hw(ioat);
1515f7157235SConrad Meyer 
1516f7157235SConrad Meyer 	return (error);
1517f7157235SConrad Meyer }
1518f7157235SConrad Meyer 
1519e974f91cSConrad Meyer static void
1520e974f91cSConrad Meyer dump_descriptor(void *hw_desc)
1521e974f91cSConrad Meyer {
1522e974f91cSConrad Meyer 	int i, j;
1523e974f91cSConrad Meyer 
1524e974f91cSConrad Meyer 	for (i = 0; i < 2; i++) {
1525e974f91cSConrad Meyer 		for (j = 0; j < 8; j++)
1526e974f91cSConrad Meyer 			printf("%08x ", ((uint32_t *)hw_desc)[i * 8 + j]);
1527e974f91cSConrad Meyer 		printf("\n");
1528e974f91cSConrad Meyer 	}
1529e974f91cSConrad Meyer }
1530e974f91cSConrad Meyer 
1531e974f91cSConrad Meyer static void
1532e974f91cSConrad Meyer ioat_setup_sysctl(device_t device)
1533e974f91cSConrad Meyer {
1534f7157235SConrad Meyer 	struct sysctl_oid_list *par;
1535f7157235SConrad Meyer 	struct sysctl_ctx_list *ctx;
1536f7157235SConrad Meyer 	struct sysctl_oid *tree;
1537e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1538e974f91cSConrad Meyer 
1539e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
1540f7157235SConrad Meyer 	ctx = device_get_sysctl_ctx(device);
1541f7157235SConrad Meyer 	tree = device_get_sysctl_tree(device);
1542f7157235SConrad Meyer 	par = SYSCTL_CHILDREN(tree);
1543e974f91cSConrad Meyer 
154465e4f8adSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "version", CTLFLAG_RD,
154565e4f8adSConrad Meyer 	    &ioat->version, 0, "HW version (0xMM form)");
154665e4f8adSConrad Meyer 	SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "max_xfer_size", CTLFLAG_RD,
154765e4f8adSConrad Meyer 	    &ioat->max_xfer_size, 0, "HW maximum transfer size");
154865e4f8adSConrad Meyer 
1549f7157235SConrad Meyer 	SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "ring_size_order", CTLFLAG_RD,
1550bf8553eaSConrad Meyer 	    &ioat->ring_size_order, 0, "SW descriptor ring size order");
1551f7157235SConrad Meyer 	SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "head", CTLFLAG_RD, &ioat->head, 0,
1552bf8553eaSConrad Meyer 	    "SW descriptor head pointer index");
1553f7157235SConrad Meyer 	SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "tail", CTLFLAG_RD, &ioat->tail, 0,
1554bf8553eaSConrad Meyer 	    "SW descriptor tail pointer index");
1555bf8553eaSConrad Meyer 	SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "hw_head", CTLFLAG_RD,
1556bf8553eaSConrad Meyer 	    &ioat->hw_head, 0, "HW DMACOUNT");
1557f7157235SConrad Meyer 
155865e4f8adSConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, par, OID_AUTO, "last_completion", CTLFLAG_RD,
155965e4f8adSConrad Meyer 	    ioat->comp_update, "HW addr of last completion");
156065e4f8adSConrad Meyer 
156165e4f8adSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "is_resize_pending", CTLFLAG_RD,
156265e4f8adSConrad Meyer 	    &ioat->is_resize_pending, 0, "resize pending");
156365e4f8adSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "is_completion_pending", CTLFLAG_RD,
156465e4f8adSConrad Meyer 	    &ioat->is_completion_pending, 0, "completion pending");
156565e4f8adSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "is_reset_pending", CTLFLAG_RD,
156665e4f8adSConrad Meyer 	    &ioat->is_reset_pending, 0, "reset pending");
156765e4f8adSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "is_channel_running", CTLFLAG_RD,
156865e4f8adSConrad Meyer 	    &ioat->is_channel_running, 0, "channel running");
156965e4f8adSConrad Meyer 
1570f7157235SConrad Meyer 	SYSCTL_ADD_PROC(ctx, par, OID_AUTO, "force_hw_reset",
1571f7157235SConrad Meyer 	    CTLTYPE_INT | CTLFLAG_RW, ioat, 0, sysctl_handle_reset, "I",
1572f7157235SConrad Meyer 	    "Set to non-zero to reset the hardware");
1573faefad9cSConrad Meyer 	SYSCTL_ADD_PROC(ctx, par, OID_AUTO, "force_hw_error",
1574faefad9cSConrad Meyer 	    CTLTYPE_INT | CTLFLAG_RW, ioat, 0, sysctl_handle_error, "I",
1575faefad9cSConrad Meyer 	    "Set to non-zero to inject a recoverable hardware error");
1576faefad9cSConrad Meyer 	SYSCTL_ADD_PROC(ctx, par, OID_AUTO, "chansts",
1577faefad9cSConrad Meyer 	    CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_chansts, "A",
1578faefad9cSConrad Meyer 	    "String of the channel status");
1579e974f91cSConrad Meyer }
1580466b3540SConrad Meyer 
1581466b3540SConrad Meyer static inline struct ioat_softc *
1582466b3540SConrad Meyer ioat_get(struct ioat_softc *ioat, enum ioat_ref_kind kind)
1583466b3540SConrad Meyer {
1584466b3540SConrad Meyer 	uint32_t old;
1585466b3540SConrad Meyer 
1586466b3540SConrad Meyer 	KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus"));
1587466b3540SConrad Meyer 
1588466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refcnt, 1);
1589466b3540SConrad Meyer 	KASSERT(old < UINT32_MAX, ("refcnt overflow"));
1590466b3540SConrad Meyer 
1591466b3540SConrad Meyer #ifdef INVARIANTS
1592466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refkinds[kind], 1);
1593466b3540SConrad Meyer 	KASSERT(old < UINT32_MAX, ("refcnt kind overflow"));
1594466b3540SConrad Meyer #endif
1595466b3540SConrad Meyer 
1596466b3540SConrad Meyer 	return (ioat);
1597466b3540SConrad Meyer }
1598466b3540SConrad Meyer 
1599466b3540SConrad Meyer static inline void
1600466b3540SConrad Meyer ioat_putn(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind)
1601466b3540SConrad Meyer {
1602faefad9cSConrad Meyer 
1603faefad9cSConrad Meyer 	_ioat_putn(ioat, n, kind, FALSE);
1604faefad9cSConrad Meyer }
1605faefad9cSConrad Meyer 
1606faefad9cSConrad Meyer static inline void
1607faefad9cSConrad Meyer ioat_putn_locked(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind)
1608faefad9cSConrad Meyer {
1609faefad9cSConrad Meyer 
1610faefad9cSConrad Meyer 	_ioat_putn(ioat, n, kind, TRUE);
1611faefad9cSConrad Meyer }
1612faefad9cSConrad Meyer 
1613faefad9cSConrad Meyer static inline void
1614faefad9cSConrad Meyer _ioat_putn(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind,
1615faefad9cSConrad Meyer     boolean_t locked)
1616faefad9cSConrad Meyer {
1617466b3540SConrad Meyer 	uint32_t old;
1618466b3540SConrad Meyer 
1619466b3540SConrad Meyer 	KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus"));
1620466b3540SConrad Meyer 
1621466b3540SConrad Meyer 	if (n == 0)
1622466b3540SConrad Meyer 		return;
1623466b3540SConrad Meyer 
1624466b3540SConrad Meyer #ifdef INVARIANTS
1625466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refkinds[kind], -n);
1626466b3540SConrad Meyer 	KASSERT(old >= n, ("refcnt kind underflow"));
1627466b3540SConrad Meyer #endif
1628466b3540SConrad Meyer 
1629466b3540SConrad Meyer 	/* Skip acquiring the lock if resulting refcnt > 0. */
1630466b3540SConrad Meyer 	for (;;) {
1631466b3540SConrad Meyer 		old = ioat->refcnt;
1632466b3540SConrad Meyer 		if (old <= n)
1633466b3540SConrad Meyer 			break;
1634466b3540SConrad Meyer 		if (atomic_cmpset_32(&ioat->refcnt, old, old - n))
1635466b3540SConrad Meyer 			return;
1636466b3540SConrad Meyer 	}
1637466b3540SConrad Meyer 
1638faefad9cSConrad Meyer 	if (locked)
1639faefad9cSConrad Meyer 		mtx_assert(IOAT_REFLK, MA_OWNED);
1640faefad9cSConrad Meyer 	else
1641466b3540SConrad Meyer 		mtx_lock(IOAT_REFLK);
1642faefad9cSConrad Meyer 
1643466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refcnt, -n);
1644466b3540SConrad Meyer 	KASSERT(old >= n, ("refcnt error"));
1645466b3540SConrad Meyer 
1646466b3540SConrad Meyer 	if (old == n)
1647466b3540SConrad Meyer 		wakeup(IOAT_REFLK);
1648faefad9cSConrad Meyer 	if (!locked)
1649466b3540SConrad Meyer 		mtx_unlock(IOAT_REFLK);
1650466b3540SConrad Meyer }
1651466b3540SConrad Meyer 
1652466b3540SConrad Meyer static inline void
1653466b3540SConrad Meyer ioat_put(struct ioat_softc *ioat, enum ioat_ref_kind kind)
1654466b3540SConrad Meyer {
1655466b3540SConrad Meyer 
1656466b3540SConrad Meyer 	ioat_putn(ioat, 1, kind);
1657466b3540SConrad Meyer }
1658466b3540SConrad Meyer 
1659466b3540SConrad Meyer static void
16605f77bd3eSConrad Meyer ioat_drain_locked(struct ioat_softc *ioat)
1661466b3540SConrad Meyer {
1662466b3540SConrad Meyer 
16635f77bd3eSConrad Meyer 	mtx_assert(IOAT_REFLK, MA_OWNED);
1664466b3540SConrad Meyer 	while (ioat->refcnt > 0)
1665466b3540SConrad Meyer 		msleep(IOAT_REFLK, IOAT_REFLK, 0, "ioat_drain", 0);
1666466b3540SConrad Meyer }
1667