xref: /freebsd/sys/dev/ioat/ioat.c (revision a5d223e641705cbe537d23e5c023395a929ab8da)
1 /*-
2  * Copyright (C) 2012 Intel Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_ddb.h"
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/conf.h>
36 #include <sys/fail.h>
37 #include <sys/ioccom.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/rman.h>
44 #include <sys/sbuf.h>
45 #include <sys/sysctl.h>
46 #include <sys/taskqueue.h>
47 #include <sys/time.h>
48 #include <dev/pci/pcireg.h>
49 #include <dev/pci/pcivar.h>
50 #include <machine/bus.h>
51 #include <machine/resource.h>
52 #include <machine/stdarg.h>
53 
54 #ifdef DDB
55 #include <ddb/ddb.h>
56 #endif
57 
58 #include "ioat.h"
59 #include "ioat_hw.h"
60 #include "ioat_internal.h"
61 
62 #ifndef	BUS_SPACE_MAXADDR_40BIT
63 #define	BUS_SPACE_MAXADDR_40BIT	0xFFFFFFFFFFULL
64 #endif
65 #define	IOAT_REFLK	(&ioat->submit_lock)
66 
67 static int ioat_probe(device_t device);
68 static int ioat_attach(device_t device);
69 static int ioat_detach(device_t device);
70 static int ioat_setup_intr(struct ioat_softc *ioat);
71 static int ioat_teardown_intr(struct ioat_softc *ioat);
72 static int ioat3_attach(device_t device);
73 static int ioat_start_channel(struct ioat_softc *ioat);
74 static int ioat_map_pci_bar(struct ioat_softc *ioat);
75 static void ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg,
76     int error);
77 static void ioat_interrupt_handler(void *arg);
78 static boolean_t ioat_model_resets_msix(struct ioat_softc *ioat);
79 static int chanerr_to_errno(uint32_t);
80 static void ioat_process_events(struct ioat_softc *ioat);
81 static inline uint32_t ioat_get_active(struct ioat_softc *ioat);
82 static inline uint32_t ioat_get_ring_space(struct ioat_softc *ioat);
83 static void ioat_free_ring(struct ioat_softc *, uint32_t size,
84     struct ioat_descriptor *);
85 static int ioat_reserve_space(struct ioat_softc *, uint32_t, int mflags);
86 static union ioat_hw_descriptor *ioat_get_descriptor(struct ioat_softc *,
87     uint32_t index);
88 static struct ioat_descriptor *ioat_get_ring_entry(struct ioat_softc *,
89     uint32_t index);
90 static void ioat_halted_debug(struct ioat_softc *, uint32_t);
91 static void ioat_poll_timer_callback(void *arg);
92 static void dump_descriptor(void *hw_desc);
93 static void ioat_submit_single(struct ioat_softc *ioat);
94 static void ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg,
95     int error);
96 static int ioat_reset_hw(struct ioat_softc *ioat);
97 static void ioat_reset_hw_task(void *, int);
98 static void ioat_setup_sysctl(device_t device);
99 static int sysctl_handle_reset(SYSCTL_HANDLER_ARGS);
100 static inline struct ioat_softc *ioat_get(struct ioat_softc *,
101     enum ioat_ref_kind);
102 static inline void ioat_put(struct ioat_softc *, enum ioat_ref_kind);
103 static inline void _ioat_putn(struct ioat_softc *, uint32_t,
104     enum ioat_ref_kind, boolean_t);
105 static inline void ioat_putn(struct ioat_softc *, uint32_t,
106     enum ioat_ref_kind);
107 static inline void ioat_putn_locked(struct ioat_softc *, uint32_t,
108     enum ioat_ref_kind);
109 static void ioat_drain_locked(struct ioat_softc *);
110 
111 #define	ioat_log_message(v, ...) do {					\
112 	if ((v) <= g_ioat_debug_level) {				\
113 		device_printf(ioat->device, __VA_ARGS__);		\
114 	}								\
115 } while (0)
116 
117 MALLOC_DEFINE(M_IOAT, "ioat", "ioat driver memory allocations");
118 SYSCTL_NODE(_hw, OID_AUTO, ioat, CTLFLAG_RD, 0, "ioat node");
119 
120 static int g_force_legacy_interrupts;
121 SYSCTL_INT(_hw_ioat, OID_AUTO, force_legacy_interrupts, CTLFLAG_RDTUN,
122     &g_force_legacy_interrupts, 0, "Set to non-zero to force MSI-X disabled");
123 
124 int g_ioat_debug_level = 0;
125 SYSCTL_INT(_hw_ioat, OID_AUTO, debug_level, CTLFLAG_RWTUN, &g_ioat_debug_level,
126     0, "Set log level (0-3) for ioat(4). Higher is more verbose.");
127 
128 unsigned g_ioat_ring_order = 13;
129 SYSCTL_UINT(_hw_ioat, OID_AUTO, ring_order, CTLFLAG_RDTUN, &g_ioat_ring_order,
130     0, "Set IOAT ring order.  (1 << this) == ring size.");
131 
132 /*
133  * OS <-> Driver interface structures
134  */
135 static device_method_t ioat_pci_methods[] = {
136 	/* Device interface */
137 	DEVMETHOD(device_probe,     ioat_probe),
138 	DEVMETHOD(device_attach,    ioat_attach),
139 	DEVMETHOD(device_detach,    ioat_detach),
140 	DEVMETHOD_END
141 };
142 
143 static driver_t ioat_pci_driver = {
144 	"ioat",
145 	ioat_pci_methods,
146 	sizeof(struct ioat_softc),
147 };
148 
149 static devclass_t ioat_devclass;
150 DRIVER_MODULE(ioat, pci, ioat_pci_driver, ioat_devclass, 0, 0);
151 MODULE_VERSION(ioat, 1);
152 
153 /*
154  * Private data structures
155  */
156 static struct ioat_softc *ioat_channel[IOAT_MAX_CHANNELS];
157 static unsigned ioat_channel_index = 0;
158 SYSCTL_UINT(_hw_ioat, OID_AUTO, channels, CTLFLAG_RD, &ioat_channel_index, 0,
159     "Number of IOAT channels attached");
160 
161 static struct _pcsid
162 {
163 	u_int32_t   type;
164 	const char  *desc;
165 } pci_ids[] = {
166 	{ 0x34308086, "TBG IOAT Ch0" },
167 	{ 0x34318086, "TBG IOAT Ch1" },
168 	{ 0x34328086, "TBG IOAT Ch2" },
169 	{ 0x34338086, "TBG IOAT Ch3" },
170 	{ 0x34298086, "TBG IOAT Ch4" },
171 	{ 0x342a8086, "TBG IOAT Ch5" },
172 	{ 0x342b8086, "TBG IOAT Ch6" },
173 	{ 0x342c8086, "TBG IOAT Ch7" },
174 
175 	{ 0x37108086, "JSF IOAT Ch0" },
176 	{ 0x37118086, "JSF IOAT Ch1" },
177 	{ 0x37128086, "JSF IOAT Ch2" },
178 	{ 0x37138086, "JSF IOAT Ch3" },
179 	{ 0x37148086, "JSF IOAT Ch4" },
180 	{ 0x37158086, "JSF IOAT Ch5" },
181 	{ 0x37168086, "JSF IOAT Ch6" },
182 	{ 0x37178086, "JSF IOAT Ch7" },
183 	{ 0x37188086, "JSF IOAT Ch0 (RAID)" },
184 	{ 0x37198086, "JSF IOAT Ch1 (RAID)" },
185 
186 	{ 0x3c208086, "SNB IOAT Ch0" },
187 	{ 0x3c218086, "SNB IOAT Ch1" },
188 	{ 0x3c228086, "SNB IOAT Ch2" },
189 	{ 0x3c238086, "SNB IOAT Ch3" },
190 	{ 0x3c248086, "SNB IOAT Ch4" },
191 	{ 0x3c258086, "SNB IOAT Ch5" },
192 	{ 0x3c268086, "SNB IOAT Ch6" },
193 	{ 0x3c278086, "SNB IOAT Ch7" },
194 	{ 0x3c2e8086, "SNB IOAT Ch0 (RAID)" },
195 	{ 0x3c2f8086, "SNB IOAT Ch1 (RAID)" },
196 
197 	{ 0x0e208086, "IVB IOAT Ch0" },
198 	{ 0x0e218086, "IVB IOAT Ch1" },
199 	{ 0x0e228086, "IVB IOAT Ch2" },
200 	{ 0x0e238086, "IVB IOAT Ch3" },
201 	{ 0x0e248086, "IVB IOAT Ch4" },
202 	{ 0x0e258086, "IVB IOAT Ch5" },
203 	{ 0x0e268086, "IVB IOAT Ch6" },
204 	{ 0x0e278086, "IVB IOAT Ch7" },
205 	{ 0x0e2e8086, "IVB IOAT Ch0 (RAID)" },
206 	{ 0x0e2f8086, "IVB IOAT Ch1 (RAID)" },
207 
208 	{ 0x2f208086, "HSW IOAT Ch0" },
209 	{ 0x2f218086, "HSW IOAT Ch1" },
210 	{ 0x2f228086, "HSW IOAT Ch2" },
211 	{ 0x2f238086, "HSW IOAT Ch3" },
212 	{ 0x2f248086, "HSW IOAT Ch4" },
213 	{ 0x2f258086, "HSW IOAT Ch5" },
214 	{ 0x2f268086, "HSW IOAT Ch6" },
215 	{ 0x2f278086, "HSW IOAT Ch7" },
216 	{ 0x2f2e8086, "HSW IOAT Ch0 (RAID)" },
217 	{ 0x2f2f8086, "HSW IOAT Ch1 (RAID)" },
218 
219 	{ 0x0c508086, "BWD IOAT Ch0" },
220 	{ 0x0c518086, "BWD IOAT Ch1" },
221 	{ 0x0c528086, "BWD IOAT Ch2" },
222 	{ 0x0c538086, "BWD IOAT Ch3" },
223 
224 	{ 0x6f508086, "BDXDE IOAT Ch0" },
225 	{ 0x6f518086, "BDXDE IOAT Ch1" },
226 	{ 0x6f528086, "BDXDE IOAT Ch2" },
227 	{ 0x6f538086, "BDXDE IOAT Ch3" },
228 
229 	{ 0x6f208086, "BDX IOAT Ch0" },
230 	{ 0x6f218086, "BDX IOAT Ch1" },
231 	{ 0x6f228086, "BDX IOAT Ch2" },
232 	{ 0x6f238086, "BDX IOAT Ch3" },
233 	{ 0x6f248086, "BDX IOAT Ch4" },
234 	{ 0x6f258086, "BDX IOAT Ch5" },
235 	{ 0x6f268086, "BDX IOAT Ch6" },
236 	{ 0x6f278086, "BDX IOAT Ch7" },
237 	{ 0x6f2e8086, "BDX IOAT Ch0 (RAID)" },
238 	{ 0x6f2f8086, "BDX IOAT Ch1 (RAID)" },
239 
240 	{ 0x00000000, NULL           }
241 };
242 
243 /*
244  * OS <-> Driver linkage functions
245  */
246 static int
247 ioat_probe(device_t device)
248 {
249 	struct _pcsid *ep;
250 	u_int32_t type;
251 
252 	type = pci_get_devid(device);
253 	for (ep = pci_ids; ep->type; ep++) {
254 		if (ep->type == type) {
255 			device_set_desc(device, ep->desc);
256 			return (0);
257 		}
258 	}
259 	return (ENXIO);
260 }
261 
262 static int
263 ioat_attach(device_t device)
264 {
265 	struct ioat_softc *ioat;
266 	int error;
267 
268 	ioat = DEVICE2SOFTC(device);
269 	ioat->device = device;
270 
271 	error = ioat_map_pci_bar(ioat);
272 	if (error != 0)
273 		goto err;
274 
275 	ioat->version = ioat_read_cbver(ioat);
276 	if (ioat->version < IOAT_VER_3_0) {
277 		error = ENODEV;
278 		goto err;
279 	}
280 
281 	error = ioat3_attach(device);
282 	if (error != 0)
283 		goto err;
284 
285 	error = pci_enable_busmaster(device);
286 	if (error != 0)
287 		goto err;
288 
289 	error = ioat_setup_intr(ioat);
290 	if (error != 0)
291 		goto err;
292 
293 	error = ioat_reset_hw(ioat);
294 	if (error != 0)
295 		goto err;
296 
297 	ioat_process_events(ioat);
298 	ioat_setup_sysctl(device);
299 
300 	ioat->chan_idx = ioat_channel_index;
301 	ioat_channel[ioat_channel_index++] = ioat;
302 	ioat_test_attach();
303 
304 err:
305 	if (error != 0)
306 		ioat_detach(device);
307 	return (error);
308 }
309 
310 static int
311 ioat_detach(device_t device)
312 {
313 	struct ioat_softc *ioat;
314 
315 	ioat = DEVICE2SOFTC(device);
316 
317 	ioat_test_detach();
318 	taskqueue_drain(taskqueue_thread, &ioat->reset_task);
319 
320 	mtx_lock(IOAT_REFLK);
321 	ioat->quiescing = TRUE;
322 	ioat->destroying = TRUE;
323 	wakeup(&ioat->quiescing);
324 	wakeup(&ioat->resetting);
325 
326 	ioat_channel[ioat->chan_idx] = NULL;
327 
328 	ioat_drain_locked(ioat);
329 	mtx_unlock(IOAT_REFLK);
330 
331 	ioat_teardown_intr(ioat);
332 	callout_drain(&ioat->poll_timer);
333 
334 	pci_disable_busmaster(device);
335 
336 	if (ioat->pci_resource != NULL)
337 		bus_release_resource(device, SYS_RES_MEMORY,
338 		    ioat->pci_resource_id, ioat->pci_resource);
339 
340 	if (ioat->ring != NULL)
341 		ioat_free_ring(ioat, 1 << ioat->ring_size_order, ioat->ring);
342 
343 	if (ioat->comp_update != NULL) {
344 		bus_dmamap_unload(ioat->comp_update_tag, ioat->comp_update_map);
345 		bus_dmamem_free(ioat->comp_update_tag, ioat->comp_update,
346 		    ioat->comp_update_map);
347 		bus_dma_tag_destroy(ioat->comp_update_tag);
348 	}
349 
350 	if (ioat->hw_desc_ring != NULL) {
351 		bus_dmamap_unload(ioat->hw_desc_tag, ioat->hw_desc_map);
352 		bus_dmamem_free(ioat->hw_desc_tag, ioat->hw_desc_ring,
353 		    ioat->hw_desc_map);
354 		bus_dma_tag_destroy(ioat->hw_desc_tag);
355 	}
356 
357 	return (0);
358 }
359 
360 static int
361 ioat_teardown_intr(struct ioat_softc *ioat)
362 {
363 
364 	if (ioat->tag != NULL)
365 		bus_teardown_intr(ioat->device, ioat->res, ioat->tag);
366 
367 	if (ioat->res != NULL)
368 		bus_release_resource(ioat->device, SYS_RES_IRQ,
369 		    rman_get_rid(ioat->res), ioat->res);
370 
371 	pci_release_msi(ioat->device);
372 	return (0);
373 }
374 
375 static int
376 ioat_start_channel(struct ioat_softc *ioat)
377 {
378 	struct ioat_dma_hw_descriptor *hw_desc;
379 	struct ioat_descriptor *desc;
380 	struct bus_dmadesc *dmadesc;
381 	uint64_t status;
382 	uint32_t chanerr;
383 	int i;
384 
385 	ioat_acquire(&ioat->dmaengine);
386 
387 	/* Submit 'NULL' operation manually to avoid quiescing flag */
388 	desc = ioat_get_ring_entry(ioat, ioat->head);
389 	hw_desc = &ioat_get_descriptor(ioat, ioat->head)->dma;
390 	dmadesc = &desc->bus_dmadesc;
391 
392 	dmadesc->callback_fn = NULL;
393 	dmadesc->callback_arg = NULL;
394 
395 	hw_desc->u.control_raw = 0;
396 	hw_desc->u.control_generic.op = IOAT_OP_COPY;
397 	hw_desc->u.control_generic.completion_update = 1;
398 	hw_desc->size = 8;
399 	hw_desc->src_addr = 0;
400 	hw_desc->dest_addr = 0;
401 	hw_desc->u.control.null = 1;
402 
403 	ioat_submit_single(ioat);
404 	ioat_release(&ioat->dmaengine);
405 
406 	for (i = 0; i < 100; i++) {
407 		DELAY(1);
408 		status = ioat_get_chansts(ioat);
409 		if (is_ioat_idle(status))
410 			return (0);
411 	}
412 
413 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
414 	ioat_log_message(0, "could not start channel: "
415 	    "status = %#jx error = %b\n", (uintmax_t)status, (int)chanerr,
416 	    IOAT_CHANERR_STR);
417 	return (ENXIO);
418 }
419 
420 /*
421  * Initialize Hardware
422  */
423 static int
424 ioat3_attach(device_t device)
425 {
426 	struct ioat_softc *ioat;
427 	struct ioat_descriptor *ring;
428 	struct ioat_dma_hw_descriptor *dma_hw_desc;
429 	void *hw_desc;
430 	size_t ringsz;
431 	int i, num_descriptors;
432 	int error;
433 	uint8_t xfercap;
434 
435 	error = 0;
436 	ioat = DEVICE2SOFTC(device);
437 	ioat->capabilities = ioat_read_dmacapability(ioat);
438 
439 	ioat_log_message(0, "Capabilities: %b\n", (int)ioat->capabilities,
440 	    IOAT_DMACAP_STR);
441 
442 	xfercap = ioat_read_xfercap(ioat);
443 	ioat->max_xfer_size = 1 << xfercap;
444 
445 	ioat->intrdelay_supported = (ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) &
446 	    IOAT_INTRDELAY_SUPPORTED) != 0;
447 	if (ioat->intrdelay_supported)
448 		ioat->intrdelay_max = IOAT_INTRDELAY_US_MASK;
449 
450 	/* TODO: need to check DCA here if we ever do XOR/PQ */
451 
452 	mtx_init(&ioat->submit_lock, "ioat_submit", NULL, MTX_DEF);
453 	mtx_init(&ioat->cleanup_lock, "ioat_cleanup", NULL, MTX_DEF);
454 	callout_init(&ioat->poll_timer, 1);
455 	TASK_INIT(&ioat->reset_task, 0, ioat_reset_hw_task, ioat);
456 
457 	/* Establish lock order for Witness */
458 	mtx_lock(&ioat->submit_lock);
459 	mtx_lock(&ioat->cleanup_lock);
460 	mtx_unlock(&ioat->cleanup_lock);
461 	mtx_unlock(&ioat->submit_lock);
462 
463 	ioat->is_submitter_processing = FALSE;
464 	ioat->is_completion_pending = FALSE;
465 	ioat->is_reset_pending = FALSE;
466 	ioat->is_channel_running = FALSE;
467 
468 	bus_dma_tag_create(bus_get_dma_tag(ioat->device), sizeof(uint64_t), 0x0,
469 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
470 	    sizeof(uint64_t), 1, sizeof(uint64_t), 0, NULL, NULL,
471 	    &ioat->comp_update_tag);
472 
473 	error = bus_dmamem_alloc(ioat->comp_update_tag,
474 	    (void **)&ioat->comp_update, BUS_DMA_ZERO, &ioat->comp_update_map);
475 	if (ioat->comp_update == NULL)
476 		return (ENOMEM);
477 
478 	error = bus_dmamap_load(ioat->comp_update_tag, ioat->comp_update_map,
479 	    ioat->comp_update, sizeof(uint64_t), ioat_comp_update_map, ioat,
480 	    0);
481 	if (error != 0)
482 		return (error);
483 
484 	ioat->ring_size_order = g_ioat_ring_order;
485 	num_descriptors = 1 << ioat->ring_size_order;
486 	ringsz = sizeof(struct ioat_dma_hw_descriptor) * num_descriptors;
487 
488 	error = bus_dma_tag_create(bus_get_dma_tag(ioat->device),
489 	    2 * 1024 * 1024, 0x0, BUS_SPACE_MAXADDR_40BIT, BUS_SPACE_MAXADDR,
490 	    NULL, NULL, ringsz, 1, ringsz, 0, NULL, NULL, &ioat->hw_desc_tag);
491 	if (error != 0)
492 		return (error);
493 
494 	error = bus_dmamem_alloc(ioat->hw_desc_tag, &hw_desc,
495 	    BUS_DMA_ZERO | BUS_DMA_WAITOK, &ioat->hw_desc_map);
496 	if (error != 0)
497 		return (error);
498 
499 	error = bus_dmamap_load(ioat->hw_desc_tag, ioat->hw_desc_map, hw_desc,
500 	    ringsz, ioat_dmamap_cb, &ioat->hw_desc_bus_addr, BUS_DMA_WAITOK);
501 	if (error)
502 		return (error);
503 
504 	ioat->hw_desc_ring = hw_desc;
505 
506 	ioat->ring = malloc(num_descriptors * sizeof(*ring), M_IOAT,
507 	    M_ZERO | M_WAITOK);
508 
509 	ring = ioat->ring;
510 	for (i = 0; i < num_descriptors; i++) {
511 		memset(&ring[i].bus_dmadesc, 0, sizeof(ring[i].bus_dmadesc));
512 		ring[i].id = i;
513 	}
514 
515 	for (i = 0; i < num_descriptors; i++) {
516 		dma_hw_desc = &ioat->hw_desc_ring[i].dma;
517 		dma_hw_desc->next = RING_PHYS_ADDR(ioat, i + 1);
518 	}
519 
520 	ioat->head = ioat->hw_head = 0;
521 	ioat->tail = 0;
522 	ioat->last_seen = 0;
523 	*ioat->comp_update = 0;
524 	return (0);
525 }
526 
527 static int
528 ioat_map_pci_bar(struct ioat_softc *ioat)
529 {
530 
531 	ioat->pci_resource_id = PCIR_BAR(0);
532 	ioat->pci_resource = bus_alloc_resource_any(ioat->device,
533 	    SYS_RES_MEMORY, &ioat->pci_resource_id, RF_ACTIVE);
534 
535 	if (ioat->pci_resource == NULL) {
536 		ioat_log_message(0, "unable to allocate pci resource\n");
537 		return (ENODEV);
538 	}
539 
540 	ioat->pci_bus_tag = rman_get_bustag(ioat->pci_resource);
541 	ioat->pci_bus_handle = rman_get_bushandle(ioat->pci_resource);
542 	return (0);
543 }
544 
545 static void
546 ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg, int error)
547 {
548 	struct ioat_softc *ioat = arg;
549 
550 	KASSERT(error == 0, ("%s: error:%d", __func__, error));
551 	ioat->comp_update_bus_addr = seg[0].ds_addr;
552 }
553 
554 static void
555 ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
556 {
557 	bus_addr_t *baddr;
558 
559 	KASSERT(error == 0, ("%s: error:%d", __func__, error));
560 	baddr = arg;
561 	*baddr = segs->ds_addr;
562 }
563 
564 /*
565  * Interrupt setup and handlers
566  */
567 static int
568 ioat_setup_intr(struct ioat_softc *ioat)
569 {
570 	uint32_t num_vectors;
571 	int error;
572 	boolean_t use_msix;
573 	boolean_t force_legacy_interrupts;
574 
575 	use_msix = FALSE;
576 	force_legacy_interrupts = FALSE;
577 
578 	if (!g_force_legacy_interrupts && pci_msix_count(ioat->device) >= 1) {
579 		num_vectors = 1;
580 		pci_alloc_msix(ioat->device, &num_vectors);
581 		if (num_vectors == 1)
582 			use_msix = TRUE;
583 	}
584 
585 	if (use_msix) {
586 		ioat->rid = 1;
587 		ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
588 		    &ioat->rid, RF_ACTIVE);
589 	} else {
590 		ioat->rid = 0;
591 		ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
592 		    &ioat->rid, RF_SHAREABLE | RF_ACTIVE);
593 	}
594 	if (ioat->res == NULL) {
595 		ioat_log_message(0, "bus_alloc_resource failed\n");
596 		return (ENOMEM);
597 	}
598 
599 	ioat->tag = NULL;
600 	error = bus_setup_intr(ioat->device, ioat->res, INTR_MPSAFE |
601 	    INTR_TYPE_MISC, NULL, ioat_interrupt_handler, ioat, &ioat->tag);
602 	if (error != 0) {
603 		ioat_log_message(0, "bus_setup_intr failed\n");
604 		return (error);
605 	}
606 
607 	ioat_write_intrctrl(ioat, IOAT_INTRCTRL_MASTER_INT_EN);
608 	return (0);
609 }
610 
611 static boolean_t
612 ioat_model_resets_msix(struct ioat_softc *ioat)
613 {
614 	u_int32_t pciid;
615 
616 	pciid = pci_get_devid(ioat->device);
617 	switch (pciid) {
618 		/* BWD: */
619 	case 0x0c508086:
620 	case 0x0c518086:
621 	case 0x0c528086:
622 	case 0x0c538086:
623 		/* BDXDE: */
624 	case 0x6f508086:
625 	case 0x6f518086:
626 	case 0x6f528086:
627 	case 0x6f538086:
628 		return (TRUE);
629 	}
630 
631 	return (FALSE);
632 }
633 
634 static void
635 ioat_interrupt_handler(void *arg)
636 {
637 	struct ioat_softc *ioat = arg;
638 
639 	ioat->stats.interrupts++;
640 	ioat_process_events(ioat);
641 }
642 
643 static int
644 chanerr_to_errno(uint32_t chanerr)
645 {
646 
647 	if (chanerr == 0)
648 		return (0);
649 	if ((chanerr & (IOAT_CHANERR_XSADDERR | IOAT_CHANERR_XDADDERR)) != 0)
650 		return (EFAULT);
651 	if ((chanerr & (IOAT_CHANERR_RDERR | IOAT_CHANERR_WDERR)) != 0)
652 		return (EIO);
653 	/* This one is probably our fault: */
654 	if ((chanerr & IOAT_CHANERR_NDADDERR) != 0)
655 		return (EIO);
656 	return (EIO);
657 }
658 
659 static void
660 ioat_process_events(struct ioat_softc *ioat)
661 {
662 	struct ioat_descriptor *desc;
663 	struct bus_dmadesc *dmadesc;
664 	uint64_t comp_update, status;
665 	uint32_t completed, chanerr;
666 	boolean_t pending;
667 	int error;
668 
669 	mtx_lock(&ioat->cleanup_lock);
670 
671 	/*
672 	 * Don't run while the hardware is being reset.  Reset is responsible
673 	 * for blocking new work and draining & completing existing work, so
674 	 * there is nothing to do until new work is queued after reset anyway.
675 	 */
676 	if (ioat->resetting_cleanup) {
677 		mtx_unlock(&ioat->cleanup_lock);
678 		return;
679 	}
680 
681 	completed = 0;
682 	comp_update = *ioat->comp_update;
683 	status = comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK;
684 
685 	if (status < ioat->hw_desc_bus_addr ||
686 	    status >= ioat->hw_desc_bus_addr + (1 << ioat->ring_size_order) *
687 	    sizeof(struct ioat_generic_hw_descriptor))
688 		panic("Bogus completion address %jx (channel %u)",
689 		    (uintmax_t)status, ioat->chan_idx);
690 
691 	if (status == ioat->last_seen) {
692 		/*
693 		 * If we landed in process_events and nothing has been
694 		 * completed, check for a timeout due to channel halt.
695 		 */
696 		goto out;
697 	}
698 	CTR4(KTR_IOAT, "%s channel=%u hw_status=0x%lx last_seen=0x%lx",
699 	    __func__, ioat->chan_idx, comp_update, ioat->last_seen);
700 
701 	while (RING_PHYS_ADDR(ioat, ioat->tail - 1) != status) {
702 		desc = ioat_get_ring_entry(ioat, ioat->tail);
703 		dmadesc = &desc->bus_dmadesc;
704 		CTR5(KTR_IOAT, "channel=%u completing desc idx %u (%p) ok  cb %p(%p)",
705 		    ioat->chan_idx, ioat->tail, dmadesc, dmadesc->callback_fn,
706 		    dmadesc->callback_arg);
707 
708 		if (dmadesc->callback_fn != NULL)
709 			dmadesc->callback_fn(dmadesc->callback_arg, 0);
710 
711 		completed++;
712 		ioat->tail++;
713 	}
714 	CTR5(KTR_IOAT, "%s channel=%u head=%u tail=%u active=%u", __func__,
715 	    ioat->chan_idx, ioat->head, ioat->tail, ioat_get_active(ioat));
716 
717 	if (completed != 0) {
718 		ioat->last_seen = RING_PHYS_ADDR(ioat, ioat->tail - 1);
719 		ioat->stats.descriptors_processed += completed;
720 	}
721 
722 out:
723 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
724 
725 	/* Perform a racy check first; only take the locks if it passes. */
726 	pending = (ioat_get_active(ioat) != 0);
727 	if (!pending && ioat->is_completion_pending) {
728 		mtx_unlock(&ioat->cleanup_lock);
729 		mtx_lock(&ioat->submit_lock);
730 		mtx_lock(&ioat->cleanup_lock);
731 
732 		pending = (ioat_get_active(ioat) != 0);
733 		if (!pending && ioat->is_completion_pending) {
734 			ioat->is_completion_pending = FALSE;
735 			callout_stop(&ioat->poll_timer);
736 		}
737 		mtx_unlock(&ioat->submit_lock);
738 	}
739 	mtx_unlock(&ioat->cleanup_lock);
740 
741 	if (pending)
742 		callout_reset(&ioat->poll_timer, 1, ioat_poll_timer_callback,
743 		    ioat);
744 
745 	if (completed != 0) {
746 		ioat_putn(ioat, completed, IOAT_ACTIVE_DESCR_REF);
747 		wakeup(&ioat->tail);
748 	}
749 
750 	/*
751 	 * The device doesn't seem to reliably push suspend/halt statuses to
752 	 * the channel completion memory address, so poll the device register
753 	 * here.
754 	 */
755 	comp_update = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS;
756 	if (!is_ioat_halted(comp_update) && !is_ioat_suspended(comp_update))
757 		return;
758 
759 	ioat->stats.channel_halts++;
760 
761 	/*
762 	 * Fatal programming error on this DMA channel.  Flush any outstanding
763 	 * work with error status and restart the engine.
764 	 */
765 	mtx_lock(&ioat->submit_lock);
766 	mtx_lock(&ioat->cleanup_lock);
767 	ioat->quiescing = TRUE;
768 
769 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
770 	if (1 <= g_ioat_debug_level)
771 		ioat_halted_debug(ioat, chanerr);
772 	ioat->stats.last_halt_chanerr = chanerr;
773 
774 	while (ioat_get_active(ioat) > 0) {
775 		desc = ioat_get_ring_entry(ioat, ioat->tail);
776 		dmadesc = &desc->bus_dmadesc;
777 		CTR5(KTR_IOAT, "channel=%u completing desc idx %u (%p) err cb %p(%p)",
778 		    ioat->chan_idx, ioat->tail, dmadesc, dmadesc->callback_fn,
779 		    dmadesc->callback_arg);
780 
781 		if (dmadesc->callback_fn != NULL)
782 			dmadesc->callback_fn(dmadesc->callback_arg,
783 			    chanerr_to_errno(chanerr));
784 
785 		ioat_putn_locked(ioat, 1, IOAT_ACTIVE_DESCR_REF);
786 		ioat->tail++;
787 		ioat->stats.descriptors_processed++;
788 		ioat->stats.descriptors_error++;
789 	}
790 	CTR5(KTR_IOAT, "%s channel=%u head=%u tail=%u active=%u", __func__,
791 	    ioat->chan_idx, ioat->head, ioat->tail, ioat_get_active(ioat));
792 
793 	if (ioat->is_completion_pending) {
794 		ioat->is_completion_pending = FALSE;
795 		callout_stop(&ioat->poll_timer);
796 	}
797 
798 	/* Clear error status */
799 	ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
800 
801 	mtx_unlock(&ioat->cleanup_lock);
802 	mtx_unlock(&ioat->submit_lock);
803 
804 	ioat_log_message(0, "Resetting channel to recover from error\n");
805 	error = taskqueue_enqueue(taskqueue_thread, &ioat->reset_task);
806 	KASSERT(error == 0,
807 	    ("%s: taskqueue_enqueue failed: %d", __func__, error));
808 }
809 
810 static void
811 ioat_reset_hw_task(void *ctx, int pending __unused)
812 {
813 	struct ioat_softc *ioat;
814 	int error;
815 
816 	ioat = ctx;
817 	ioat_log_message(1, "%s: Resetting channel\n", __func__);
818 
819 	error = ioat_reset_hw(ioat);
820 	KASSERT(error == 0, ("%s: reset failed: %d", __func__, error));
821 	(void)error;
822 }
823 
824 /*
825  * User API functions
826  */
827 unsigned
828 ioat_get_nchannels(void)
829 {
830 
831 	return (ioat_channel_index);
832 }
833 
834 bus_dmaengine_t
835 ioat_get_dmaengine(uint32_t index, int flags)
836 {
837 	struct ioat_softc *ioat;
838 
839 	KASSERT((flags & ~(M_NOWAIT | M_WAITOK)) == 0,
840 	    ("invalid flags: 0x%08x", flags));
841 	KASSERT((flags & (M_NOWAIT | M_WAITOK)) != (M_NOWAIT | M_WAITOK),
842 	    ("invalid wait | nowait"));
843 
844 	if (index >= ioat_channel_index)
845 		return (NULL);
846 
847 	ioat = ioat_channel[index];
848 	if (ioat == NULL || ioat->destroying)
849 		return (NULL);
850 
851 	if (ioat->quiescing) {
852 		if ((flags & M_NOWAIT) != 0)
853 			return (NULL);
854 
855 		mtx_lock(IOAT_REFLK);
856 		while (ioat->quiescing && !ioat->destroying)
857 			msleep(&ioat->quiescing, IOAT_REFLK, 0, "getdma", 0);
858 		mtx_unlock(IOAT_REFLK);
859 
860 		if (ioat->destroying)
861 			return (NULL);
862 	}
863 
864 	/*
865 	 * There's a race here between the quiescing check and HW reset or
866 	 * module destroy.
867 	 */
868 	return (&ioat_get(ioat, IOAT_DMAENGINE_REF)->dmaengine);
869 }
870 
871 void
872 ioat_put_dmaengine(bus_dmaengine_t dmaengine)
873 {
874 	struct ioat_softc *ioat;
875 
876 	ioat = to_ioat_softc(dmaengine);
877 	ioat_put(ioat, IOAT_DMAENGINE_REF);
878 }
879 
880 int
881 ioat_get_hwversion(bus_dmaengine_t dmaengine)
882 {
883 	struct ioat_softc *ioat;
884 
885 	ioat = to_ioat_softc(dmaengine);
886 	return (ioat->version);
887 }
888 
889 size_t
890 ioat_get_max_io_size(bus_dmaengine_t dmaengine)
891 {
892 	struct ioat_softc *ioat;
893 
894 	ioat = to_ioat_softc(dmaengine);
895 	return (ioat->max_xfer_size);
896 }
897 
898 uint32_t
899 ioat_get_capabilities(bus_dmaengine_t dmaengine)
900 {
901 	struct ioat_softc *ioat;
902 
903 	ioat = to_ioat_softc(dmaengine);
904 	return (ioat->capabilities);
905 }
906 
907 int
908 ioat_set_interrupt_coalesce(bus_dmaengine_t dmaengine, uint16_t delay)
909 {
910 	struct ioat_softc *ioat;
911 
912 	ioat = to_ioat_softc(dmaengine);
913 	if (!ioat->intrdelay_supported)
914 		return (ENODEV);
915 	if (delay > ioat->intrdelay_max)
916 		return (ERANGE);
917 
918 	ioat_write_2(ioat, IOAT_INTRDELAY_OFFSET, delay);
919 	ioat->cached_intrdelay =
920 	    ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) & IOAT_INTRDELAY_US_MASK;
921 	return (0);
922 }
923 
924 uint16_t
925 ioat_get_max_coalesce_period(bus_dmaengine_t dmaengine)
926 {
927 	struct ioat_softc *ioat;
928 
929 	ioat = to_ioat_softc(dmaengine);
930 	return (ioat->intrdelay_max);
931 }
932 
933 void
934 ioat_acquire(bus_dmaengine_t dmaengine)
935 {
936 	struct ioat_softc *ioat;
937 
938 	ioat = to_ioat_softc(dmaengine);
939 	mtx_lock(&ioat->submit_lock);
940 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
941 }
942 
943 int
944 ioat_acquire_reserve(bus_dmaengine_t dmaengine, unsigned n, int mflags)
945 {
946 	struct ioat_softc *ioat;
947 	int error;
948 
949 	ioat = to_ioat_softc(dmaengine);
950 	ioat_acquire(dmaengine);
951 
952 	error = ioat_reserve_space(ioat, n, mflags);
953 	if (error != 0)
954 		ioat_release(dmaengine);
955 	return (error);
956 }
957 
958 void
959 ioat_release(bus_dmaengine_t dmaengine)
960 {
961 	struct ioat_softc *ioat;
962 
963 	ioat = to_ioat_softc(dmaengine);
964 	CTR4(KTR_IOAT, "%s channel=%u dispatch1 hw_head=%u head=%u", __func__,
965 	    ioat->chan_idx, ioat->hw_head & UINT16_MAX, ioat->head);
966 	KFAIL_POINT_CODE(DEBUG_FP, ioat_release, /* do nothing */);
967 	CTR4(KTR_IOAT, "%s channel=%u dispatch2 hw_head=%u head=%u", __func__,
968 	    ioat->chan_idx, ioat->hw_head & UINT16_MAX, ioat->head);
969 
970 	ioat_write_2(ioat, IOAT_DMACOUNT_OFFSET, (uint16_t)ioat->hw_head);
971 
972 	if (!ioat->is_completion_pending) {
973 		ioat->is_completion_pending = TRUE;
974 		callout_reset(&ioat->poll_timer, 1, ioat_poll_timer_callback,
975 		    ioat);
976 	}
977 	mtx_unlock(&ioat->submit_lock);
978 }
979 
980 static struct ioat_descriptor *
981 ioat_op_generic(struct ioat_softc *ioat, uint8_t op,
982     uint32_t size, uint64_t src, uint64_t dst,
983     bus_dmaengine_callback_t callback_fn, void *callback_arg,
984     uint32_t flags)
985 {
986 	struct ioat_generic_hw_descriptor *hw_desc;
987 	struct ioat_descriptor *desc;
988 	int mflags;
989 
990 	mtx_assert(&ioat->submit_lock, MA_OWNED);
991 
992 	KASSERT((flags & ~_DMA_GENERIC_FLAGS) == 0,
993 	    ("Unrecognized flag(s): %#x", flags & ~_DMA_GENERIC_FLAGS));
994 	if ((flags & DMA_NO_WAIT) != 0)
995 		mflags = M_NOWAIT;
996 	else
997 		mflags = M_WAITOK;
998 
999 	if (size > ioat->max_xfer_size) {
1000 		ioat_log_message(0, "%s: max_xfer_size = %d, requested = %u\n",
1001 		    __func__, ioat->max_xfer_size, (unsigned)size);
1002 		return (NULL);
1003 	}
1004 
1005 	if (ioat_reserve_space(ioat, 1, mflags) != 0)
1006 		return (NULL);
1007 
1008 	desc = ioat_get_ring_entry(ioat, ioat->head);
1009 	hw_desc = &ioat_get_descriptor(ioat, ioat->head)->generic;
1010 
1011 	hw_desc->u.control_raw = 0;
1012 	hw_desc->u.control_generic.op = op;
1013 	hw_desc->u.control_generic.completion_update = 1;
1014 
1015 	if ((flags & DMA_INT_EN) != 0)
1016 		hw_desc->u.control_generic.int_enable = 1;
1017 	if ((flags & DMA_FENCE) != 0)
1018 		hw_desc->u.control_generic.fence = 1;
1019 
1020 	hw_desc->size = size;
1021 	hw_desc->src_addr = src;
1022 	hw_desc->dest_addr = dst;
1023 
1024 	desc->bus_dmadesc.callback_fn = callback_fn;
1025 	desc->bus_dmadesc.callback_arg = callback_arg;
1026 	return (desc);
1027 }
1028 
1029 struct bus_dmadesc *
1030 ioat_null(bus_dmaengine_t dmaengine, bus_dmaengine_callback_t callback_fn,
1031     void *callback_arg, uint32_t flags)
1032 {
1033 	struct ioat_dma_hw_descriptor *hw_desc;
1034 	struct ioat_descriptor *desc;
1035 	struct ioat_softc *ioat;
1036 
1037 	ioat = to_ioat_softc(dmaengine);
1038 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1039 
1040 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, 8, 0, 0, callback_fn,
1041 	    callback_arg, flags);
1042 	if (desc == NULL)
1043 		return (NULL);
1044 
1045 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->dma;
1046 	hw_desc->u.control.null = 1;
1047 	ioat_submit_single(ioat);
1048 	return (&desc->bus_dmadesc);
1049 }
1050 
1051 struct bus_dmadesc *
1052 ioat_copy(bus_dmaengine_t dmaengine, bus_addr_t dst,
1053     bus_addr_t src, bus_size_t len, bus_dmaengine_callback_t callback_fn,
1054     void *callback_arg, uint32_t flags)
1055 {
1056 	struct ioat_dma_hw_descriptor *hw_desc;
1057 	struct ioat_descriptor *desc;
1058 	struct ioat_softc *ioat;
1059 
1060 	ioat = to_ioat_softc(dmaengine);
1061 
1062 	if (((src | dst) & (0xffffull << 48)) != 0) {
1063 		ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n",
1064 		    __func__);
1065 		return (NULL);
1066 	}
1067 
1068 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, len, src, dst, callback_fn,
1069 	    callback_arg, flags);
1070 	if (desc == NULL)
1071 		return (NULL);
1072 
1073 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->dma;
1074 	if (g_ioat_debug_level >= 3)
1075 		dump_descriptor(hw_desc);
1076 
1077 	ioat_submit_single(ioat);
1078 	CTR6(KTR_IOAT, "%s channel=%u desc=%p dest=%lx src=%lx len=%lx",
1079 	    __func__, ioat->chan_idx, &desc->bus_dmadesc, dst, src, len);
1080 	return (&desc->bus_dmadesc);
1081 }
1082 
1083 struct bus_dmadesc *
1084 ioat_copy_8k_aligned(bus_dmaengine_t dmaengine, bus_addr_t dst1,
1085     bus_addr_t dst2, bus_addr_t src1, bus_addr_t src2,
1086     bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
1087 {
1088 	struct ioat_dma_hw_descriptor *hw_desc;
1089 	struct ioat_descriptor *desc;
1090 	struct ioat_softc *ioat;
1091 
1092 	ioat = to_ioat_softc(dmaengine);
1093 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1094 
1095 	if (((src1 | src2 | dst1 | dst2) & (0xffffull << 48)) != 0) {
1096 		ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n",
1097 		    __func__);
1098 		return (NULL);
1099 	}
1100 	if (((src1 | src2 | dst1 | dst2) & PAGE_MASK) != 0) {
1101 		ioat_log_message(0, "%s: Addresses must be page-aligned\n",
1102 		    __func__);
1103 		return (NULL);
1104 	}
1105 
1106 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, 2 * PAGE_SIZE, src1, dst1,
1107 	    callback_fn, callback_arg, flags);
1108 	if (desc == NULL)
1109 		return (NULL);
1110 
1111 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->dma;
1112 	if (src2 != src1 + PAGE_SIZE) {
1113 		hw_desc->u.control.src_page_break = 1;
1114 		hw_desc->next_src_addr = src2;
1115 	}
1116 	if (dst2 != dst1 + PAGE_SIZE) {
1117 		hw_desc->u.control.dest_page_break = 1;
1118 		hw_desc->next_dest_addr = dst2;
1119 	}
1120 
1121 	if (g_ioat_debug_level >= 3)
1122 		dump_descriptor(hw_desc);
1123 
1124 	ioat_submit_single(ioat);
1125 	return (&desc->bus_dmadesc);
1126 }
1127 
1128 struct bus_dmadesc *
1129 ioat_copy_crc(bus_dmaengine_t dmaengine, bus_addr_t dst, bus_addr_t src,
1130     bus_size_t len, uint32_t *initialseed, bus_addr_t crcptr,
1131     bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
1132 {
1133 	struct ioat_crc32_hw_descriptor *hw_desc;
1134 	struct ioat_descriptor *desc;
1135 	struct ioat_softc *ioat;
1136 	uint32_t teststore;
1137 	uint8_t op;
1138 
1139 	ioat = to_ioat_softc(dmaengine);
1140 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1141 
1142 	if ((ioat->capabilities & IOAT_DMACAP_MOVECRC) == 0) {
1143 		ioat_log_message(0, "%s: Device lacks MOVECRC capability\n",
1144 		    __func__);
1145 		return (NULL);
1146 	}
1147 	if (((src | dst) & (0xffffffull << 40)) != 0) {
1148 		ioat_log_message(0, "%s: High 24 bits of src/dst invalid\n",
1149 		    __func__);
1150 		return (NULL);
1151 	}
1152 	teststore = (flags & _DMA_CRC_TESTSTORE);
1153 	if (teststore == _DMA_CRC_TESTSTORE) {
1154 		ioat_log_message(0, "%s: TEST and STORE invalid\n", __func__);
1155 		return (NULL);
1156 	}
1157 	if (teststore == 0 && (flags & DMA_CRC_INLINE) != 0) {
1158 		ioat_log_message(0, "%s: INLINE invalid without TEST or STORE\n",
1159 		    __func__);
1160 		return (NULL);
1161 	}
1162 
1163 	switch (teststore) {
1164 	case DMA_CRC_STORE:
1165 		op = IOAT_OP_MOVECRC_STORE;
1166 		break;
1167 	case DMA_CRC_TEST:
1168 		op = IOAT_OP_MOVECRC_TEST;
1169 		break;
1170 	default:
1171 		KASSERT(teststore == 0, ("bogus"));
1172 		op = IOAT_OP_MOVECRC;
1173 		break;
1174 	}
1175 
1176 	if ((flags & DMA_CRC_INLINE) == 0 &&
1177 	    (crcptr & (0xffffffull << 40)) != 0) {
1178 		ioat_log_message(0,
1179 		    "%s: High 24 bits of crcptr invalid\n", __func__);
1180 		return (NULL);
1181 	}
1182 
1183 	desc = ioat_op_generic(ioat, op, len, src, dst, callback_fn,
1184 	    callback_arg, flags & ~_DMA_CRC_FLAGS);
1185 	if (desc == NULL)
1186 		return (NULL);
1187 
1188 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->crc32;
1189 
1190 	if ((flags & DMA_CRC_INLINE) == 0)
1191 		hw_desc->crc_address = crcptr;
1192 	else
1193 		hw_desc->u.control.crc_location = 1;
1194 
1195 	if (initialseed != NULL) {
1196 		hw_desc->u.control.use_seed = 1;
1197 		hw_desc->seed = *initialseed;
1198 	}
1199 
1200 	if (g_ioat_debug_level >= 3)
1201 		dump_descriptor(hw_desc);
1202 
1203 	ioat_submit_single(ioat);
1204 	return (&desc->bus_dmadesc);
1205 }
1206 
1207 struct bus_dmadesc *
1208 ioat_crc(bus_dmaengine_t dmaengine, bus_addr_t src, bus_size_t len,
1209     uint32_t *initialseed, bus_addr_t crcptr,
1210     bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
1211 {
1212 	struct ioat_crc32_hw_descriptor *hw_desc;
1213 	struct ioat_descriptor *desc;
1214 	struct ioat_softc *ioat;
1215 	uint32_t teststore;
1216 	uint8_t op;
1217 
1218 	ioat = to_ioat_softc(dmaengine);
1219 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1220 
1221 	if ((ioat->capabilities & IOAT_DMACAP_CRC) == 0) {
1222 		ioat_log_message(0, "%s: Device lacks CRC capability\n",
1223 		    __func__);
1224 		return (NULL);
1225 	}
1226 	if ((src & (0xffffffull << 40)) != 0) {
1227 		ioat_log_message(0, "%s: High 24 bits of src invalid\n",
1228 		    __func__);
1229 		return (NULL);
1230 	}
1231 	teststore = (flags & _DMA_CRC_TESTSTORE);
1232 	if (teststore == _DMA_CRC_TESTSTORE) {
1233 		ioat_log_message(0, "%s: TEST and STORE invalid\n", __func__);
1234 		return (NULL);
1235 	}
1236 	if (teststore == 0 && (flags & DMA_CRC_INLINE) != 0) {
1237 		ioat_log_message(0, "%s: INLINE invalid without TEST or STORE\n",
1238 		    __func__);
1239 		return (NULL);
1240 	}
1241 
1242 	switch (teststore) {
1243 	case DMA_CRC_STORE:
1244 		op = IOAT_OP_CRC_STORE;
1245 		break;
1246 	case DMA_CRC_TEST:
1247 		op = IOAT_OP_CRC_TEST;
1248 		break;
1249 	default:
1250 		KASSERT(teststore == 0, ("bogus"));
1251 		op = IOAT_OP_CRC;
1252 		break;
1253 	}
1254 
1255 	if ((flags & DMA_CRC_INLINE) == 0 &&
1256 	    (crcptr & (0xffffffull << 40)) != 0) {
1257 		ioat_log_message(0,
1258 		    "%s: High 24 bits of crcptr invalid\n", __func__);
1259 		return (NULL);
1260 	}
1261 
1262 	desc = ioat_op_generic(ioat, op, len, src, 0, callback_fn,
1263 	    callback_arg, flags & ~_DMA_CRC_FLAGS);
1264 	if (desc == NULL)
1265 		return (NULL);
1266 
1267 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->crc32;
1268 
1269 	if ((flags & DMA_CRC_INLINE) == 0)
1270 		hw_desc->crc_address = crcptr;
1271 	else
1272 		hw_desc->u.control.crc_location = 1;
1273 
1274 	if (initialseed != NULL) {
1275 		hw_desc->u.control.use_seed = 1;
1276 		hw_desc->seed = *initialseed;
1277 	}
1278 
1279 	if (g_ioat_debug_level >= 3)
1280 		dump_descriptor(hw_desc);
1281 
1282 	ioat_submit_single(ioat);
1283 	return (&desc->bus_dmadesc);
1284 }
1285 
1286 struct bus_dmadesc *
1287 ioat_blockfill(bus_dmaengine_t dmaengine, bus_addr_t dst, uint64_t fillpattern,
1288     bus_size_t len, bus_dmaengine_callback_t callback_fn, void *callback_arg,
1289     uint32_t flags)
1290 {
1291 	struct ioat_fill_hw_descriptor *hw_desc;
1292 	struct ioat_descriptor *desc;
1293 	struct ioat_softc *ioat;
1294 
1295 	ioat = to_ioat_softc(dmaengine);
1296 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1297 
1298 	if ((ioat->capabilities & IOAT_DMACAP_BFILL) == 0) {
1299 		ioat_log_message(0, "%s: Device lacks BFILL capability\n",
1300 		    __func__);
1301 		return (NULL);
1302 	}
1303 
1304 	if ((dst & (0xffffull << 48)) != 0) {
1305 		ioat_log_message(0, "%s: High 16 bits of dst invalid\n",
1306 		    __func__);
1307 		return (NULL);
1308 	}
1309 
1310 	desc = ioat_op_generic(ioat, IOAT_OP_FILL, len, fillpattern, dst,
1311 	    callback_fn, callback_arg, flags);
1312 	if (desc == NULL)
1313 		return (NULL);
1314 
1315 	hw_desc = &ioat_get_descriptor(ioat, desc->id)->fill;
1316 	if (g_ioat_debug_level >= 3)
1317 		dump_descriptor(hw_desc);
1318 
1319 	ioat_submit_single(ioat);
1320 	return (&desc->bus_dmadesc);
1321 }
1322 
1323 /*
1324  * Ring Management
1325  */
1326 static inline uint32_t
1327 ioat_get_active(struct ioat_softc *ioat)
1328 {
1329 
1330 	return ((ioat->head - ioat->tail) & ((1 << ioat->ring_size_order) - 1));
1331 }
1332 
1333 static inline uint32_t
1334 ioat_get_ring_space(struct ioat_softc *ioat)
1335 {
1336 
1337 	return ((1 << ioat->ring_size_order) - ioat_get_active(ioat) - 1);
1338 }
1339 
1340 /*
1341  * Reserves space in this IOAT descriptor ring by ensuring enough slots remain
1342  * for 'num_descs'.
1343  *
1344  * If mflags contains M_WAITOK, blocks until enough space is available.
1345  *
1346  * Returns zero on success, or an errno on error.  If num_descs is beyond the
1347  * maximum ring size, returns EINVAl; if allocation would block and mflags
1348  * contains M_NOWAIT, returns EAGAIN.
1349  *
1350  * Must be called with the submit_lock held; returns with the lock held.  The
1351  * lock may be dropped to allocate the ring.
1352  *
1353  * (The submit_lock is needed to add any entries to the ring, so callers are
1354  * assured enough room is available.)
1355  */
1356 static int
1357 ioat_reserve_space(struct ioat_softc *ioat, uint32_t num_descs, int mflags)
1358 {
1359 	boolean_t dug;
1360 	int error;
1361 
1362 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1363 	error = 0;
1364 	dug = FALSE;
1365 
1366 	if (num_descs < 1 || num_descs >= (1 << ioat->ring_size_order)) {
1367 		error = EINVAL;
1368 		goto out;
1369 	}
1370 
1371 	for (;;) {
1372 		if (ioat->quiescing) {
1373 			error = ENXIO;
1374 			goto out;
1375 		}
1376 
1377 		if (ioat_get_ring_space(ioat) >= num_descs)
1378 			goto out;
1379 
1380 		CTR3(KTR_IOAT, "%s channel=%u starved (%u)", __func__,
1381 		    ioat->chan_idx, num_descs);
1382 
1383 		if (!dug && !ioat->is_submitter_processing) {
1384 			ioat->is_submitter_processing = TRUE;
1385 			mtx_unlock(&ioat->submit_lock);
1386 
1387 			CTR2(KTR_IOAT, "%s channel=%u attempting to process events",
1388 			    __func__, ioat->chan_idx);
1389 			ioat_process_events(ioat);
1390 
1391 			mtx_lock(&ioat->submit_lock);
1392 			dug = TRUE;
1393 			KASSERT(ioat->is_submitter_processing == TRUE,
1394 			    ("is_submitter_processing"));
1395 			ioat->is_submitter_processing = FALSE;
1396 			wakeup(&ioat->tail);
1397 			continue;
1398 		}
1399 
1400 		if ((mflags & M_WAITOK) == 0) {
1401 			error = EAGAIN;
1402 			break;
1403 		}
1404 		CTR2(KTR_IOAT, "%s channel=%u blocking on completions",
1405 		    __func__, ioat->chan_idx);
1406 		msleep(&ioat->tail, &ioat->submit_lock, 0,
1407 		    "ioat_full", 0);
1408 		continue;
1409 	}
1410 
1411 out:
1412 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1413 	KASSERT(!ioat->quiescing || error == ENXIO,
1414 	    ("reserved during quiesce"));
1415 	return (error);
1416 }
1417 
1418 static void
1419 ioat_free_ring(struct ioat_softc *ioat, uint32_t size,
1420     struct ioat_descriptor *ring)
1421 {
1422 
1423 	free(ring, M_IOAT);
1424 }
1425 
1426 static struct ioat_descriptor *
1427 ioat_get_ring_entry(struct ioat_softc *ioat, uint32_t index)
1428 {
1429 
1430 	return (&ioat->ring[index % (1 << ioat->ring_size_order)]);
1431 }
1432 
1433 static union ioat_hw_descriptor *
1434 ioat_get_descriptor(struct ioat_softc *ioat, uint32_t index)
1435 {
1436 
1437 	return (&ioat->hw_desc_ring[index % (1 << ioat->ring_size_order)]);
1438 }
1439 
1440 static void
1441 ioat_halted_debug(struct ioat_softc *ioat, uint32_t chanerr)
1442 {
1443 	union ioat_hw_descriptor *desc;
1444 
1445 	ioat_log_message(0, "Channel halted (%b)\n", (int)chanerr,
1446 	    IOAT_CHANERR_STR);
1447 	if (chanerr == 0)
1448 		return;
1449 
1450 	mtx_assert(&ioat->cleanup_lock, MA_OWNED);
1451 
1452 	desc = ioat_get_descriptor(ioat, ioat->tail + 0);
1453 	dump_descriptor(desc);
1454 
1455 	desc = ioat_get_descriptor(ioat, ioat->tail + 1);
1456 	dump_descriptor(desc);
1457 }
1458 
1459 static void
1460 ioat_poll_timer_callback(void *arg)
1461 {
1462 	struct ioat_softc *ioat;
1463 
1464 	ioat = arg;
1465 	ioat_log_message(3, "%s\n", __func__);
1466 
1467 	ioat_process_events(ioat);
1468 }
1469 
1470 /*
1471  * Support Functions
1472  */
1473 static void
1474 ioat_submit_single(struct ioat_softc *ioat)
1475 {
1476 
1477 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1478 
1479 	ioat_get(ioat, IOAT_ACTIVE_DESCR_REF);
1480 	atomic_add_rel_int(&ioat->head, 1);
1481 	atomic_add_rel_int(&ioat->hw_head, 1);
1482 	CTR5(KTR_IOAT, "%s channel=%u head=%u hw_head=%u tail=%u", __func__,
1483 	    ioat->chan_idx, ioat->head, ioat->hw_head & UINT16_MAX,
1484 	    ioat->tail);
1485 
1486 	ioat->stats.descriptors_submitted++;
1487 }
1488 
1489 static int
1490 ioat_reset_hw(struct ioat_softc *ioat)
1491 {
1492 	uint64_t status;
1493 	uint32_t chanerr;
1494 	unsigned timeout;
1495 	int error;
1496 
1497 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1498 
1499 	mtx_lock(IOAT_REFLK);
1500 	while (ioat->resetting && !ioat->destroying)
1501 		msleep(&ioat->resetting, IOAT_REFLK, 0, "IRH_drain", 0);
1502 	if (ioat->destroying) {
1503 		mtx_unlock(IOAT_REFLK);
1504 		return (ENXIO);
1505 	}
1506 	ioat->resetting = TRUE;
1507 
1508 	ioat->quiescing = TRUE;
1509 	ioat_drain_locked(ioat);
1510 	mtx_unlock(IOAT_REFLK);
1511 
1512 	/*
1513 	 * Suspend ioat_process_events while the hardware and softc are in an
1514 	 * indeterminate state.
1515 	 */
1516 	mtx_lock(&ioat->cleanup_lock);
1517 	ioat->resetting_cleanup = TRUE;
1518 	mtx_unlock(&ioat->cleanup_lock);
1519 
1520 	CTR2(KTR_IOAT, "%s channel=%u quiesced and drained", __func__,
1521 	    ioat->chan_idx);
1522 
1523 	status = ioat_get_chansts(ioat);
1524 	if (is_ioat_active(status) || is_ioat_idle(status))
1525 		ioat_suspend(ioat);
1526 
1527 	/* Wait at most 20 ms */
1528 	for (timeout = 0; (is_ioat_active(status) || is_ioat_idle(status)) &&
1529 	    timeout < 20; timeout++) {
1530 		DELAY(1000);
1531 		status = ioat_get_chansts(ioat);
1532 	}
1533 	if (timeout == 20) {
1534 		error = ETIMEDOUT;
1535 		goto out;
1536 	}
1537 
1538 	KASSERT(ioat_get_active(ioat) == 0, ("active after quiesce"));
1539 
1540 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
1541 	ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
1542 
1543 	CTR2(KTR_IOAT, "%s channel=%u hardware suspended", __func__,
1544 	    ioat->chan_idx);
1545 
1546 	/*
1547 	 * IOAT v3 workaround - CHANERRMSK_INT with 3E07h to masks out errors
1548 	 *  that can cause stability issues for IOAT v3.
1549 	 */
1550 	pci_write_config(ioat->device, IOAT_CFG_CHANERRMASK_INT_OFFSET, 0x3e07,
1551 	    4);
1552 	chanerr = pci_read_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, 4);
1553 	pci_write_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, chanerr, 4);
1554 
1555 	/*
1556 	 * BDXDE and BWD models reset MSI-X registers on device reset.
1557 	 * Save/restore their contents manually.
1558 	 */
1559 	if (ioat_model_resets_msix(ioat)) {
1560 		ioat_log_message(1, "device resets MSI-X registers; saving\n");
1561 		pci_save_state(ioat->device);
1562 	}
1563 
1564 	ioat_reset(ioat);
1565 	CTR2(KTR_IOAT, "%s channel=%u hardware reset", __func__,
1566 	    ioat->chan_idx);
1567 
1568 	/* Wait at most 20 ms */
1569 	for (timeout = 0; ioat_reset_pending(ioat) && timeout < 20; timeout++)
1570 		DELAY(1000);
1571 	if (timeout == 20) {
1572 		error = ETIMEDOUT;
1573 		goto out;
1574 	}
1575 
1576 	if (ioat_model_resets_msix(ioat)) {
1577 		ioat_log_message(1, "device resets registers; restored\n");
1578 		pci_restore_state(ioat->device);
1579 	}
1580 
1581 	/* Reset attempts to return the hardware to "halted." */
1582 	status = ioat_get_chansts(ioat);
1583 	if (is_ioat_active(status) || is_ioat_idle(status)) {
1584 		/* So this really shouldn't happen... */
1585 		ioat_log_message(0, "Device is active after a reset?\n");
1586 		ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
1587 		error = 0;
1588 		goto out;
1589 	}
1590 
1591 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
1592 	if (chanerr != 0) {
1593 		mtx_lock(&ioat->cleanup_lock);
1594 		ioat_halted_debug(ioat, chanerr);
1595 		mtx_unlock(&ioat->cleanup_lock);
1596 		error = EIO;
1597 		goto out;
1598 	}
1599 
1600 	/*
1601 	 * Bring device back online after reset.  Writing CHAINADDR brings the
1602 	 * device back to active.
1603 	 *
1604 	 * The internal ring counter resets to zero, so we have to start over
1605 	 * at zero as well.
1606 	 */
1607 	ioat->tail = ioat->head = ioat->hw_head = 0;
1608 	ioat->last_seen = 0;
1609 	*ioat->comp_update = 0;
1610 	KASSERT(!ioat->is_completion_pending, ("bogus completion_pending"));
1611 
1612 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
1613 	ioat_write_chancmp(ioat, ioat->comp_update_bus_addr);
1614 	ioat_write_chainaddr(ioat, RING_PHYS_ADDR(ioat, 0));
1615 	error = 0;
1616 	CTR2(KTR_IOAT, "%s channel=%u configured channel", __func__,
1617 	    ioat->chan_idx);
1618 
1619 out:
1620 	/* Enqueues a null operation and ensures it completes. */
1621 	if (error == 0) {
1622 		error = ioat_start_channel(ioat);
1623 		CTR2(KTR_IOAT, "%s channel=%u started channel", __func__,
1624 		    ioat->chan_idx);
1625 	}
1626 
1627 	/*
1628 	 * Resume completions now that ring state is consistent.
1629 	 */
1630 	mtx_lock(&ioat->cleanup_lock);
1631 	ioat->resetting_cleanup = FALSE;
1632 	mtx_unlock(&ioat->cleanup_lock);
1633 
1634 	/* Unblock submission of new work */
1635 	mtx_lock(IOAT_REFLK);
1636 	ioat->quiescing = FALSE;
1637 	wakeup(&ioat->quiescing);
1638 
1639 	ioat->resetting = FALSE;
1640 	wakeup(&ioat->resetting);
1641 
1642 	if (ioat->is_completion_pending)
1643 		callout_reset(&ioat->poll_timer, 1, ioat_poll_timer_callback,
1644 		    ioat);
1645 	CTR2(KTR_IOAT, "%s channel=%u reset done", __func__, ioat->chan_idx);
1646 	mtx_unlock(IOAT_REFLK);
1647 
1648 	return (error);
1649 }
1650 
1651 static int
1652 sysctl_handle_chansts(SYSCTL_HANDLER_ARGS)
1653 {
1654 	struct ioat_softc *ioat;
1655 	struct sbuf sb;
1656 	uint64_t status;
1657 	int error;
1658 
1659 	ioat = arg1;
1660 
1661 	status = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS;
1662 
1663 	sbuf_new_for_sysctl(&sb, NULL, 256, req);
1664 	switch (status) {
1665 	case IOAT_CHANSTS_ACTIVE:
1666 		sbuf_printf(&sb, "ACTIVE");
1667 		break;
1668 	case IOAT_CHANSTS_IDLE:
1669 		sbuf_printf(&sb, "IDLE");
1670 		break;
1671 	case IOAT_CHANSTS_SUSPENDED:
1672 		sbuf_printf(&sb, "SUSPENDED");
1673 		break;
1674 	case IOAT_CHANSTS_HALTED:
1675 		sbuf_printf(&sb, "HALTED");
1676 		break;
1677 	case IOAT_CHANSTS_ARMED:
1678 		sbuf_printf(&sb, "ARMED");
1679 		break;
1680 	default:
1681 		sbuf_printf(&sb, "UNKNOWN");
1682 		break;
1683 	}
1684 	error = sbuf_finish(&sb);
1685 	sbuf_delete(&sb);
1686 
1687 	if (error != 0 || req->newptr == NULL)
1688 		return (error);
1689 	return (EINVAL);
1690 }
1691 
1692 static int
1693 sysctl_handle_dpi(SYSCTL_HANDLER_ARGS)
1694 {
1695 	struct ioat_softc *ioat;
1696 	struct sbuf sb;
1697 #define	PRECISION	"1"
1698 	const uintmax_t factor = 10;
1699 	uintmax_t rate;
1700 	int error;
1701 
1702 	ioat = arg1;
1703 	sbuf_new_for_sysctl(&sb, NULL, 16, req);
1704 
1705 	if (ioat->stats.interrupts == 0) {
1706 		sbuf_printf(&sb, "NaN");
1707 		goto out;
1708 	}
1709 	rate = ioat->stats.descriptors_processed * factor /
1710 	    ioat->stats.interrupts;
1711 	sbuf_printf(&sb, "%ju.%." PRECISION "ju", rate / factor,
1712 	    rate % factor);
1713 #undef	PRECISION
1714 out:
1715 	error = sbuf_finish(&sb);
1716 	sbuf_delete(&sb);
1717 	if (error != 0 || req->newptr == NULL)
1718 		return (error);
1719 	return (EINVAL);
1720 }
1721 
1722 static int
1723 sysctl_handle_reset(SYSCTL_HANDLER_ARGS)
1724 {
1725 	struct ioat_softc *ioat;
1726 	int error, arg;
1727 
1728 	ioat = arg1;
1729 
1730 	arg = 0;
1731 	error = SYSCTL_OUT(req, &arg, sizeof(arg));
1732 	if (error != 0 || req->newptr == NULL)
1733 		return (error);
1734 
1735 	error = SYSCTL_IN(req, &arg, sizeof(arg));
1736 	if (error != 0)
1737 		return (error);
1738 
1739 	if (arg != 0)
1740 		error = ioat_reset_hw(ioat);
1741 
1742 	return (error);
1743 }
1744 
1745 static void
1746 dump_descriptor(void *hw_desc)
1747 {
1748 	int i, j;
1749 
1750 	for (i = 0; i < 2; i++) {
1751 		for (j = 0; j < 8; j++)
1752 			printf("%08x ", ((uint32_t *)hw_desc)[i * 8 + j]);
1753 		printf("\n");
1754 	}
1755 }
1756 
1757 static void
1758 ioat_setup_sysctl(device_t device)
1759 {
1760 	struct sysctl_oid_list *par, *statpar, *state, *hammer;
1761 	struct sysctl_ctx_list *ctx;
1762 	struct sysctl_oid *tree, *tmp;
1763 	struct ioat_softc *ioat;
1764 
1765 	ioat = DEVICE2SOFTC(device);
1766 	ctx = device_get_sysctl_ctx(device);
1767 	tree = device_get_sysctl_tree(device);
1768 	par = SYSCTL_CHILDREN(tree);
1769 
1770 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "version", CTLFLAG_RD,
1771 	    &ioat->version, 0, "HW version (0xMM form)");
1772 	SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "max_xfer_size", CTLFLAG_RD,
1773 	    &ioat->max_xfer_size, 0, "HW maximum transfer size");
1774 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "intrdelay_supported", CTLFLAG_RD,
1775 	    &ioat->intrdelay_supported, 0, "Is INTRDELAY supported");
1776 	SYSCTL_ADD_U16(ctx, par, OID_AUTO, "intrdelay_max", CTLFLAG_RD,
1777 	    &ioat->intrdelay_max, 0,
1778 	    "Maximum configurable INTRDELAY on this channel (microseconds)");
1779 
1780 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "state", CTLFLAG_RD, NULL,
1781 	    "IOAT channel internal state");
1782 	state = SYSCTL_CHILDREN(tmp);
1783 
1784 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "ring_size_order", CTLFLAG_RD,
1785 	    &ioat->ring_size_order, 0, "SW descriptor ring size order");
1786 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "head", CTLFLAG_RD, &ioat->head,
1787 	    0, "SW descriptor head pointer index");
1788 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "tail", CTLFLAG_RD, &ioat->tail,
1789 	    0, "SW descriptor tail pointer index");
1790 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "hw_head", CTLFLAG_RD,
1791 	    &ioat->hw_head, 0, "HW DMACOUNT");
1792 
1793 	SYSCTL_ADD_UQUAD(ctx, state, OID_AUTO, "last_completion", CTLFLAG_RD,
1794 	    ioat->comp_update, "HW addr of last completion");
1795 
1796 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_submitter_processing",
1797 	    CTLFLAG_RD, &ioat->is_submitter_processing, 0,
1798 	    "submitter processing");
1799 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_completion_pending",
1800 	    CTLFLAG_RD, &ioat->is_completion_pending, 0, "completion pending");
1801 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_reset_pending", CTLFLAG_RD,
1802 	    &ioat->is_reset_pending, 0, "reset pending");
1803 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_channel_running", CTLFLAG_RD,
1804 	    &ioat->is_channel_running, 0, "channel running");
1805 
1806 	SYSCTL_ADD_PROC(ctx, state, OID_AUTO, "chansts",
1807 	    CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_chansts, "A",
1808 	    "String of the channel status");
1809 
1810 	SYSCTL_ADD_U16(ctx, state, OID_AUTO, "intrdelay", CTLFLAG_RD,
1811 	    &ioat->cached_intrdelay, 0,
1812 	    "Current INTRDELAY on this channel (cached, microseconds)");
1813 
1814 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "hammer", CTLFLAG_RD, NULL,
1815 	    "Big hammers (mostly for testing)");
1816 	hammer = SYSCTL_CHILDREN(tmp);
1817 
1818 	SYSCTL_ADD_PROC(ctx, hammer, OID_AUTO, "force_hw_reset",
1819 	    CTLTYPE_INT | CTLFLAG_RW, ioat, 0, sysctl_handle_reset, "I",
1820 	    "Set to non-zero to reset the hardware");
1821 
1822 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "stats", CTLFLAG_RD, NULL,
1823 	    "IOAT channel statistics");
1824 	statpar = SYSCTL_CHILDREN(tmp);
1825 
1826 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "interrupts", CTLFLAG_RW,
1827 	    &ioat->stats.interrupts,
1828 	    "Number of interrupts processed on this channel");
1829 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "descriptors", CTLFLAG_RW,
1830 	    &ioat->stats.descriptors_processed,
1831 	    "Number of descriptors processed on this channel");
1832 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "submitted", CTLFLAG_RW,
1833 	    &ioat->stats.descriptors_submitted,
1834 	    "Number of descriptors submitted to this channel");
1835 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "errored", CTLFLAG_RW,
1836 	    &ioat->stats.descriptors_error,
1837 	    "Number of descriptors failed by channel errors");
1838 	SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "halts", CTLFLAG_RW,
1839 	    &ioat->stats.channel_halts, 0,
1840 	    "Number of times the channel has halted");
1841 	SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "last_halt_chanerr", CTLFLAG_RW,
1842 	    &ioat->stats.last_halt_chanerr, 0,
1843 	    "The raw CHANERR when the channel was last halted");
1844 
1845 	SYSCTL_ADD_PROC(ctx, statpar, OID_AUTO, "desc_per_interrupt",
1846 	    CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_dpi, "A",
1847 	    "Descriptors per interrupt");
1848 }
1849 
1850 static inline struct ioat_softc *
1851 ioat_get(struct ioat_softc *ioat, enum ioat_ref_kind kind)
1852 {
1853 	uint32_t old;
1854 
1855 	KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus"));
1856 
1857 	old = atomic_fetchadd_32(&ioat->refcnt, 1);
1858 	KASSERT(old < UINT32_MAX, ("refcnt overflow"));
1859 
1860 #ifdef INVARIANTS
1861 	old = atomic_fetchadd_32(&ioat->refkinds[kind], 1);
1862 	KASSERT(old < UINT32_MAX, ("refcnt kind overflow"));
1863 #endif
1864 
1865 	return (ioat);
1866 }
1867 
1868 static inline void
1869 ioat_putn(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind)
1870 {
1871 
1872 	_ioat_putn(ioat, n, kind, FALSE);
1873 }
1874 
1875 static inline void
1876 ioat_putn_locked(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind)
1877 {
1878 
1879 	_ioat_putn(ioat, n, kind, TRUE);
1880 }
1881 
1882 static inline void
1883 _ioat_putn(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind,
1884     boolean_t locked)
1885 {
1886 	uint32_t old;
1887 
1888 	KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus"));
1889 
1890 	if (n == 0)
1891 		return;
1892 
1893 #ifdef INVARIANTS
1894 	old = atomic_fetchadd_32(&ioat->refkinds[kind], -n);
1895 	KASSERT(old >= n, ("refcnt kind underflow"));
1896 #endif
1897 
1898 	/* Skip acquiring the lock if resulting refcnt > 0. */
1899 	for (;;) {
1900 		old = ioat->refcnt;
1901 		if (old <= n)
1902 			break;
1903 		if (atomic_cmpset_32(&ioat->refcnt, old, old - n))
1904 			return;
1905 	}
1906 
1907 	if (locked)
1908 		mtx_assert(IOAT_REFLK, MA_OWNED);
1909 	else
1910 		mtx_lock(IOAT_REFLK);
1911 
1912 	old = atomic_fetchadd_32(&ioat->refcnt, -n);
1913 	KASSERT(old >= n, ("refcnt error"));
1914 
1915 	if (old == n)
1916 		wakeup(IOAT_REFLK);
1917 	if (!locked)
1918 		mtx_unlock(IOAT_REFLK);
1919 }
1920 
1921 static inline void
1922 ioat_put(struct ioat_softc *ioat, enum ioat_ref_kind kind)
1923 {
1924 
1925 	ioat_putn(ioat, 1, kind);
1926 }
1927 
1928 static void
1929 ioat_drain_locked(struct ioat_softc *ioat)
1930 {
1931 
1932 	mtx_assert(IOAT_REFLK, MA_OWNED);
1933 	while (ioat->refcnt > 0)
1934 		msleep(IOAT_REFLK, IOAT_REFLK, 0, "ioat_drain", 0);
1935 }
1936 
1937 #ifdef DDB
1938 #define	_db_show_lock(lo)	LOCK_CLASS(lo)->lc_ddb_show(lo)
1939 #define	db_show_lock(lk)	_db_show_lock(&(lk)->lock_object)
1940 DB_SHOW_COMMAND(ioat, db_show_ioat)
1941 {
1942 	struct ioat_softc *sc;
1943 	unsigned idx;
1944 
1945 	if (!have_addr)
1946 		goto usage;
1947 	idx = (unsigned)addr;
1948 	if (idx >= ioat_channel_index)
1949 		goto usage;
1950 
1951 	sc = ioat_channel[idx];
1952 	db_printf("ioat softc at %p\n", sc);
1953 	if (sc == NULL)
1954 		return;
1955 
1956 	db_printf(" version: %d\n", sc->version);
1957 	db_printf(" chan_idx: %u\n", sc->chan_idx);
1958 	db_printf(" submit_lock: ");
1959 	db_show_lock(&sc->submit_lock);
1960 
1961 	db_printf(" capabilities: %b\n", (int)sc->capabilities,
1962 	    IOAT_DMACAP_STR);
1963 	db_printf(" cached_intrdelay: %u\n", sc->cached_intrdelay);
1964 	db_printf(" *comp_update: 0x%jx\n", (uintmax_t)*sc->comp_update);
1965 
1966 	db_printf(" poll_timer:\n");
1967 	db_printf("  c_time: %ju\n", (uintmax_t)sc->poll_timer.c_time);
1968 	db_printf("  c_arg: %p\n", sc->poll_timer.c_arg);
1969 	db_printf("  c_func: %p\n", sc->poll_timer.c_func);
1970 	db_printf("  c_lock: %p\n", sc->poll_timer.c_lock);
1971 	db_printf("  c_flags: 0x%x\n", (unsigned)sc->poll_timer.c_flags);
1972 
1973 	db_printf(" quiescing: %d\n", (int)sc->quiescing);
1974 	db_printf(" destroying: %d\n", (int)sc->destroying);
1975 	db_printf(" is_submitter_processing: %d\n",
1976 	    (int)sc->is_submitter_processing);
1977 	db_printf(" is_completion_pending: %d\n", (int)sc->is_completion_pending);
1978 	db_printf(" is_reset_pending: %d\n", (int)sc->is_reset_pending);
1979 	db_printf(" is_channel_running: %d\n", (int)sc->is_channel_running);
1980 	db_printf(" intrdelay_supported: %d\n", (int)sc->intrdelay_supported);
1981 	db_printf(" resetting: %d\n", (int)sc->resetting);
1982 
1983 	db_printf(" head: %u\n", sc->head);
1984 	db_printf(" tail: %u\n", sc->tail);
1985 	db_printf(" hw_head: %u\n", sc->hw_head);
1986 	db_printf(" ring_size_order: %u\n", sc->ring_size_order);
1987 	db_printf(" last_seen: 0x%lx\n", sc->last_seen);
1988 	db_printf(" ring: %p\n", sc->ring);
1989 	db_printf(" descriptors: %p\n", sc->hw_desc_ring);
1990 	db_printf(" descriptors (phys): 0x%jx\n",
1991 	    (uintmax_t)sc->hw_desc_bus_addr);
1992 
1993 	db_printf("  ring[%u] (tail):\n", sc->tail %
1994 	    (1 << sc->ring_size_order));
1995 	db_printf("   id: %u\n", ioat_get_ring_entry(sc, sc->tail)->id);
1996 	db_printf("   addr: 0x%lx\n",
1997 	    RING_PHYS_ADDR(sc, sc->tail));
1998 	db_printf("   next: 0x%lx\n",
1999 	     ioat_get_descriptor(sc, sc->tail)->generic.next);
2000 
2001 	db_printf("  ring[%u] (head - 1):\n", (sc->head - 1) %
2002 	    (1 << sc->ring_size_order));
2003 	db_printf("   id: %u\n", ioat_get_ring_entry(sc, sc->head - 1)->id);
2004 	db_printf("   addr: 0x%lx\n",
2005 	    RING_PHYS_ADDR(sc, sc->head - 1));
2006 	db_printf("   next: 0x%lx\n",
2007 	     ioat_get_descriptor(sc, sc->head - 1)->generic.next);
2008 
2009 	db_printf("  ring[%u] (head):\n", (sc->head) %
2010 	    (1 << sc->ring_size_order));
2011 	db_printf("   id: %u\n", ioat_get_ring_entry(sc, sc->head)->id);
2012 	db_printf("   addr: 0x%lx\n",
2013 	    RING_PHYS_ADDR(sc, sc->head));
2014 	db_printf("   next: 0x%lx\n",
2015 	     ioat_get_descriptor(sc, sc->head)->generic.next);
2016 
2017 	for (idx = 0; idx < (1 << sc->ring_size_order); idx++)
2018 		if ((*sc->comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK)
2019 		    == RING_PHYS_ADDR(sc, idx))
2020 			db_printf("  ring[%u] == hardware tail\n", idx);
2021 
2022 	db_printf(" cleanup_lock: ");
2023 	db_show_lock(&sc->cleanup_lock);
2024 
2025 	db_printf(" refcnt: %u\n", sc->refcnt);
2026 #ifdef INVARIANTS
2027 	CTASSERT(IOAT_NUM_REF_KINDS == 2);
2028 	db_printf(" refkinds: [ENG=%u, DESCR=%u]\n", sc->refkinds[0],
2029 	    sc->refkinds[1]);
2030 #endif
2031 	db_printf(" stats:\n");
2032 	db_printf("  interrupts: %lu\n", sc->stats.interrupts);
2033 	db_printf("  descriptors_processed: %lu\n", sc->stats.descriptors_processed);
2034 	db_printf("  descriptors_error: %lu\n", sc->stats.descriptors_error);
2035 	db_printf("  descriptors_submitted: %lu\n", sc->stats.descriptors_submitted);
2036 
2037 	db_printf("  channel_halts: %u\n", sc->stats.channel_halts);
2038 	db_printf("  last_halt_chanerr: %u\n", sc->stats.last_halt_chanerr);
2039 
2040 	if (db_pager_quit)
2041 		return;
2042 
2043 	db_printf(" hw status:\n");
2044 	db_printf("  status: 0x%lx\n", ioat_get_chansts(sc));
2045 	db_printf("  chanctrl: 0x%x\n",
2046 	    (unsigned)ioat_read_2(sc, IOAT_CHANCTRL_OFFSET));
2047 	db_printf("  chancmd: 0x%x\n",
2048 	    (unsigned)ioat_read_1(sc, IOAT_CHANCMD_OFFSET));
2049 	db_printf("  dmacount: 0x%x\n",
2050 	    (unsigned)ioat_read_2(sc, IOAT_DMACOUNT_OFFSET));
2051 	db_printf("  chainaddr: 0x%lx\n",
2052 	    ioat_read_double_4(sc, IOAT_CHAINADDR_OFFSET_LOW));
2053 	db_printf("  chancmp: 0x%lx\n",
2054 	    ioat_read_double_4(sc, IOAT_CHANCMP_OFFSET_LOW));
2055 	db_printf("  chanerr: %b\n",
2056 	    (int)ioat_read_4(sc, IOAT_CHANERR_OFFSET), IOAT_CHANERR_STR);
2057 	return;
2058 usage:
2059 	db_printf("usage: show ioat <0-%u>\n", ioat_channel_index);
2060 	return;
2061 }
2062 #endif /* DDB */
2063