2 * Copyright (C) 2012 Intel Corporation
4 * Copyright (C) 2018 Alexander Motin <mav@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
38 #include <sys/ioccom.h>
39 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
46 #include <sys/sysctl.h>
47 #include <sys/taskqueue.h>
49 #include <dev/pci/pcireg.h>
50 #include <dev/pci/pcivar.h>
51 #include <machine/bus.h>
52 #include <machine/resource.h>
53 #include <machine/stdarg.h>
61 #include "ioat_internal.h"
63 #ifndef BUS_SPACE_MAXADDR_40BIT
64 #define BUS_SPACE_MAXADDR_40BIT 0xFFFFFFFFFFULL
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,
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, boolean_t intr);
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 *,
88 static struct ioat_descriptor *ioat_get_ring_entry(struct ioat_softc *,
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,
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 void ioat_get(struct ioat_softc *);
101 static void ioat_put(struct ioat_softc *);
102 static void ioat_drain_locked(struct ioat_softc *);
104 #define ioat_log_message(v, ...) do { \
105 if ((v) <= g_ioat_debug_level) { \
106 device_printf(ioat->device, __VA_ARGS__); \
110 MALLOC_DEFINE(M_IOAT, "ioat", "ioat driver memory allocations");
111 SYSCTL_NODE(_hw, OID_AUTO, ioat, CTLFLAG_RD, 0, "ioat node");
113 static int g_force_legacy_interrupts;
114 SYSCTL_INT(_hw_ioat, OID_AUTO, force_legacy_interrupts, CTLFLAG_RDTUN,
115 &g_force_legacy_interrupts, 0, "Set to non-zero to force MSI-X disabled");
117 int g_ioat_debug_level = 0;
118 SYSCTL_INT(_hw_ioat, OID_AUTO, debug_level, CTLFLAG_RWTUN, &g_ioat_debug_level,
119 0, "Set log level (0-3) for ioat(4). Higher is more verbose.");
121 unsigned g_ioat_ring_order = 13;
122 SYSCTL_UINT(_hw_ioat, OID_AUTO, ring_order, CTLFLAG_RDTUN, &g_ioat_ring_order,
123 0, "Set IOAT ring order. (1 << this) == ring size.");
126 * OS <-> Driver interface structures
128 static device_method_t ioat_pci_methods[] = {
129 /* Device interface */
130 DEVMETHOD(device_probe, ioat_probe),
131 DEVMETHOD(device_attach, ioat_attach),
132 DEVMETHOD(device_detach, ioat_detach),
136 static driver_t ioat_pci_driver = {
139 sizeof(struct ioat_softc),
142 static devclass_t ioat_devclass;
143 DRIVER_MODULE(ioat, pci, ioat_pci_driver, ioat_devclass, 0, 0);
144 MODULE_VERSION(ioat, 1);
147 * Private data structures
149 static struct ioat_softc *ioat_channel[IOAT_MAX_CHANNELS];
150 static unsigned ioat_channel_index = 0;
151 SYSCTL_UINT(_hw_ioat, OID_AUTO, channels, CTLFLAG_RD, &ioat_channel_index, 0,
152 "Number of IOAT channels attached");
153 static struct mtx ioat_list_mtx;
154 MTX_SYSINIT(ioat_list_mtx, &ioat_list_mtx, "ioat list mtx", MTX_DEF);
161 { 0x34308086, "TBG IOAT Ch0" },
162 { 0x34318086, "TBG IOAT Ch1" },
163 { 0x34328086, "TBG IOAT Ch2" },
164 { 0x34338086, "TBG IOAT Ch3" },
165 { 0x34298086, "TBG IOAT Ch4" },
166 { 0x342a8086, "TBG IOAT Ch5" },
167 { 0x342b8086, "TBG IOAT Ch6" },
168 { 0x342c8086, "TBG IOAT Ch7" },
170 { 0x37108086, "JSF IOAT Ch0" },
171 { 0x37118086, "JSF IOAT Ch1" },
172 { 0x37128086, "JSF IOAT Ch2" },
173 { 0x37138086, "JSF IOAT Ch3" },
174 { 0x37148086, "JSF IOAT Ch4" },
175 { 0x37158086, "JSF IOAT Ch5" },
176 { 0x37168086, "JSF IOAT Ch6" },
177 { 0x37178086, "JSF IOAT Ch7" },
178 { 0x37188086, "JSF IOAT Ch0 (RAID)" },
179 { 0x37198086, "JSF IOAT Ch1 (RAID)" },
181 { 0x3c208086, "SNB IOAT Ch0" },
182 { 0x3c218086, "SNB IOAT Ch1" },
183 { 0x3c228086, "SNB IOAT Ch2" },
184 { 0x3c238086, "SNB IOAT Ch3" },
185 { 0x3c248086, "SNB IOAT Ch4" },
186 { 0x3c258086, "SNB IOAT Ch5" },
187 { 0x3c268086, "SNB IOAT Ch6" },
188 { 0x3c278086, "SNB IOAT Ch7" },
189 { 0x3c2e8086, "SNB IOAT Ch0 (RAID)" },
190 { 0x3c2f8086, "SNB IOAT Ch1 (RAID)" },
192 { 0x0e208086, "IVB IOAT Ch0" },
193 { 0x0e218086, "IVB IOAT Ch1" },
194 { 0x0e228086, "IVB IOAT Ch2" },
195 { 0x0e238086, "IVB IOAT Ch3" },
196 { 0x0e248086, "IVB IOAT Ch4" },
197 { 0x0e258086, "IVB IOAT Ch5" },
198 { 0x0e268086, "IVB IOAT Ch6" },
199 { 0x0e278086, "IVB IOAT Ch7" },
200 { 0x0e2e8086, "IVB IOAT Ch0 (RAID)" },
201 { 0x0e2f8086, "IVB IOAT Ch1 (RAID)" },
203 { 0x2f208086, "HSW IOAT Ch0" },
204 { 0x2f218086, "HSW IOAT Ch1" },
205 { 0x2f228086, "HSW IOAT Ch2" },
206 { 0x2f238086, "HSW IOAT Ch3" },
207 { 0x2f248086, "HSW IOAT Ch4" },
208 { 0x2f258086, "HSW IOAT Ch5" },
209 { 0x2f268086, "HSW IOAT Ch6" },
210 { 0x2f278086, "HSW IOAT Ch7" },
211 { 0x2f2e8086, "HSW IOAT Ch0 (RAID)" },
212 { 0x2f2f8086, "HSW IOAT Ch1 (RAID)" },
214 { 0x0c508086, "BWD IOAT Ch0" },
215 { 0x0c518086, "BWD IOAT Ch1" },
216 { 0x0c528086, "BWD IOAT Ch2" },
217 { 0x0c538086, "BWD IOAT Ch3" },
219 { 0x6f508086, "BDXDE IOAT Ch0" },
220 { 0x6f518086, "BDXDE IOAT Ch1" },
221 { 0x6f528086, "BDXDE IOAT Ch2" },
222 { 0x6f538086, "BDXDE IOAT Ch3" },
224 { 0x6f208086, "BDX IOAT Ch0" },
225 { 0x6f218086, "BDX IOAT Ch1" },
226 { 0x6f228086, "BDX IOAT Ch2" },
227 { 0x6f238086, "BDX IOAT Ch3" },
228 { 0x6f248086, "BDX IOAT Ch4" },
229 { 0x6f258086, "BDX IOAT Ch5" },
230 { 0x6f268086, "BDX IOAT Ch6" },
231 { 0x6f278086, "BDX IOAT Ch7" },
232 { 0x6f2e8086, "BDX IOAT Ch0 (RAID)" },
233 { 0x6f2f8086, "BDX IOAT Ch1 (RAID)" },
234 { 0x20218086, "SKX IOAT" },
239 * OS <-> Driver linkage functions
242 ioat_probe(device_t device)
247 type = pci_get_devid(device);
248 for (ep = pci_ids; ep->type; ep++) {
249 if (ep->type == type) {
250 device_set_desc(device, ep->desc);
258 ioat_attach(device_t device)
260 struct ioat_softc *ioat;
263 ioat = DEVICE2SOFTC(device);
264 ioat->device = device;
266 error = ioat_map_pci_bar(ioat);
270 ioat->version = ioat_read_cbver(ioat);
271 if (ioat->version < IOAT_VER_3_0) {
276 error = ioat3_attach(device);
280 error = pci_enable_busmaster(device);
284 error = ioat_setup_intr(ioat);
288 error = ioat_reset_hw(ioat);
292 ioat_process_events(ioat, FALSE);
293 ioat_setup_sysctl(device);
295 mtx_lock(&ioat_list_mtx);
296 for (i = 0; i < IOAT_MAX_CHANNELS; i++) {
297 if (ioat_channel[i] == NULL)
300 if (i >= IOAT_MAX_CHANNELS) {
301 mtx_unlock(&ioat_list_mtx);
302 device_printf(device, "Too many I/OAT devices in system\n");
307 ioat_channel[i] = ioat;
308 if (i >= ioat_channel_index)
309 ioat_channel_index = i + 1;
310 mtx_unlock(&ioat_list_mtx);
321 ioat_detach(device_t device)
323 struct ioat_softc *ioat;
325 ioat = DEVICE2SOFTC(device);
327 mtx_lock(&ioat_list_mtx);
328 ioat_channel[ioat->chan_idx] = NULL;
329 while (ioat_channel_index > 0 &&
330 ioat_channel[ioat_channel_index - 1] == NULL)
331 ioat_channel_index--;
332 mtx_unlock(&ioat_list_mtx);
335 taskqueue_drain(taskqueue_thread, &ioat->reset_task);
337 mtx_lock(&ioat->submit_lock);
338 ioat->quiescing = TRUE;
339 ioat->destroying = TRUE;
340 wakeup(&ioat->quiescing);
341 wakeup(&ioat->resetting);
343 ioat_drain_locked(ioat);
344 mtx_unlock(&ioat->submit_lock);
345 mtx_lock(&ioat->cleanup_lock);
346 while (ioat_get_active(ioat) > 0)
347 msleep(&ioat->tail, &ioat->cleanup_lock, 0, "ioat_drain", 1);
348 mtx_unlock(&ioat->cleanup_lock);
350 ioat_teardown_intr(ioat);
351 callout_drain(&ioat->poll_timer);
353 pci_disable_busmaster(device);
355 if (ioat->pci_resource != NULL)
356 bus_release_resource(device, SYS_RES_MEMORY,
357 ioat->pci_resource_id, ioat->pci_resource);
359 if (ioat->ring != NULL)
360 ioat_free_ring(ioat, 1 << ioat->ring_size_order, ioat->ring);
362 if (ioat->comp_update != NULL) {
363 bus_dmamap_unload(ioat->comp_update_tag, ioat->comp_update_map);
364 bus_dmamem_free(ioat->comp_update_tag, ioat->comp_update,
365 ioat->comp_update_map);
366 bus_dma_tag_destroy(ioat->comp_update_tag);
369 if (ioat->hw_desc_ring != NULL) {
370 bus_dmamap_unload(ioat->hw_desc_tag, ioat->hw_desc_map);
371 bus_dmamem_free(ioat->hw_desc_tag, ioat->hw_desc_ring,
373 bus_dma_tag_destroy(ioat->hw_desc_tag);
380 ioat_teardown_intr(struct ioat_softc *ioat)
383 if (ioat->tag != NULL)
384 bus_teardown_intr(ioat->device, ioat->res, ioat->tag);
386 if (ioat->res != NULL)
387 bus_release_resource(ioat->device, SYS_RES_IRQ,
388 rman_get_rid(ioat->res), ioat->res);
390 pci_release_msi(ioat->device);
395 ioat_start_channel(struct ioat_softc *ioat)
397 struct ioat_dma_hw_descriptor *hw_desc;
398 struct ioat_descriptor *desc;
399 struct bus_dmadesc *dmadesc;
404 ioat_acquire(&ioat->dmaengine);
406 /* Submit 'NULL' operation manually to avoid quiescing flag */
407 desc = ioat_get_ring_entry(ioat, ioat->head);
408 hw_desc = &ioat_get_descriptor(ioat, ioat->head)->dma;
409 dmadesc = &desc->bus_dmadesc;
411 dmadesc->callback_fn = NULL;
412 dmadesc->callback_arg = NULL;
414 hw_desc->u.control_raw = 0;
415 hw_desc->u.control_generic.op = IOAT_OP_COPY;
416 hw_desc->u.control_generic.completion_update = 1;
418 hw_desc->src_addr = 0;
419 hw_desc->dest_addr = 0;
420 hw_desc->u.control.null = 1;
422 ioat_submit_single(ioat);
423 ioat_release(&ioat->dmaengine);
425 for (i = 0; i < 100; i++) {
427 status = ioat_get_chansts(ioat);
428 if (is_ioat_idle(status))
432 chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
433 ioat_log_message(0, "could not start channel: "
434 "status = %#jx error = %b\n", (uintmax_t)status, (int)chanerr,
440 * Initialize Hardware
443 ioat3_attach(device_t device)
445 struct ioat_softc *ioat;
446 struct ioat_descriptor *ring;
447 struct ioat_dma_hw_descriptor *dma_hw_desc;
450 int i, num_descriptors;
455 ioat = DEVICE2SOFTC(device);
456 ioat->capabilities = ioat_read_dmacapability(ioat);
458 ioat_log_message(0, "Capabilities: %b\n", (int)ioat->capabilities,
461 xfercap = ioat_read_xfercap(ioat);
462 ioat->max_xfer_size = 1 << xfercap;
464 ioat->intrdelay_supported = (ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) &
465 IOAT_INTRDELAY_SUPPORTED) != 0;
466 if (ioat->intrdelay_supported)
467 ioat->intrdelay_max = IOAT_INTRDELAY_US_MASK;
469 /* TODO: need to check DCA here if we ever do XOR/PQ */
471 mtx_init(&ioat->submit_lock, "ioat_submit", NULL, MTX_DEF);
472 mtx_init(&ioat->cleanup_lock, "ioat_cleanup", NULL, MTX_DEF);
473 callout_init(&ioat->poll_timer, 1);
474 TASK_INIT(&ioat->reset_task, 0, ioat_reset_hw_task, ioat);
476 /* Establish lock order for Witness */
477 mtx_lock(&ioat->cleanup_lock);
478 mtx_lock(&ioat->submit_lock);
479 mtx_unlock(&ioat->submit_lock);
480 mtx_unlock(&ioat->cleanup_lock);
482 ioat->is_submitter_processing = FALSE;
484 bus_dma_tag_create(bus_get_dma_tag(ioat->device), sizeof(uint64_t), 0x0,
485 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
486 sizeof(uint64_t), 1, sizeof(uint64_t), 0, NULL, NULL,
487 &ioat->comp_update_tag);
489 error = bus_dmamem_alloc(ioat->comp_update_tag,
490 (void **)&ioat->comp_update, BUS_DMA_ZERO, &ioat->comp_update_map);
491 if (ioat->comp_update == NULL)
494 error = bus_dmamap_load(ioat->comp_update_tag, ioat->comp_update_map,
495 ioat->comp_update, sizeof(uint64_t), ioat_comp_update_map, ioat,
500 ioat->ring_size_order = g_ioat_ring_order;
501 num_descriptors = 1 << ioat->ring_size_order;
502 ringsz = sizeof(struct ioat_dma_hw_descriptor) * num_descriptors;
504 error = bus_dma_tag_create(bus_get_dma_tag(ioat->device),
505 2 * 1024 * 1024, 0x0, (bus_addr_t)BUS_SPACE_MAXADDR_40BIT,
506 BUS_SPACE_MAXADDR, NULL, NULL, ringsz, 1, ringsz, 0, NULL, NULL,
511 error = bus_dmamem_alloc(ioat->hw_desc_tag, &hw_desc,
512 BUS_DMA_ZERO | BUS_DMA_WAITOK, &ioat->hw_desc_map);
516 error = bus_dmamap_load(ioat->hw_desc_tag, ioat->hw_desc_map, hw_desc,
517 ringsz, ioat_dmamap_cb, &ioat->hw_desc_bus_addr, BUS_DMA_WAITOK);
521 ioat->hw_desc_ring = hw_desc;
523 ioat->ring = malloc(num_descriptors * sizeof(*ring), M_IOAT,
527 for (i = 0; i < num_descriptors; i++) {
528 memset(&ring[i].bus_dmadesc, 0, sizeof(ring[i].bus_dmadesc));
532 for (i = 0; i < num_descriptors; i++) {
533 dma_hw_desc = &ioat->hw_desc_ring[i].dma;
534 dma_hw_desc->next = RING_PHYS_ADDR(ioat, i + 1);
540 *ioat->comp_update = 0;
545 ioat_map_pci_bar(struct ioat_softc *ioat)
548 ioat->pci_resource_id = PCIR_BAR(0);
549 ioat->pci_resource = bus_alloc_resource_any(ioat->device,
550 SYS_RES_MEMORY, &ioat->pci_resource_id, RF_ACTIVE);
552 if (ioat->pci_resource == NULL) {
553 ioat_log_message(0, "unable to allocate pci resource\n");
557 ioat->pci_bus_tag = rman_get_bustag(ioat->pci_resource);
558 ioat->pci_bus_handle = rman_get_bushandle(ioat->pci_resource);
563 ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg, int error)
565 struct ioat_softc *ioat = arg;
567 KASSERT(error == 0, ("%s: error:%d", __func__, error));
568 ioat->comp_update_bus_addr = seg[0].ds_addr;
572 ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
576 KASSERT(error == 0, ("%s: error:%d", __func__, error));
578 *baddr = segs->ds_addr;
582 * Interrupt setup and handlers
585 ioat_setup_intr(struct ioat_softc *ioat)
587 uint32_t num_vectors;
590 boolean_t force_legacy_interrupts;
593 force_legacy_interrupts = FALSE;
595 if (!g_force_legacy_interrupts && pci_msix_count(ioat->device) >= 1) {
597 pci_alloc_msix(ioat->device, &num_vectors);
598 if (num_vectors == 1)
604 ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
605 &ioat->rid, RF_ACTIVE);
608 ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
609 &ioat->rid, RF_SHAREABLE | RF_ACTIVE);
611 if (ioat->res == NULL) {
612 ioat_log_message(0, "bus_alloc_resource failed\n");
617 error = bus_setup_intr(ioat->device, ioat->res, INTR_MPSAFE |
618 INTR_TYPE_MISC, NULL, ioat_interrupt_handler, ioat, &ioat->tag);
620 ioat_log_message(0, "bus_setup_intr failed\n");
624 ioat_write_intrctrl(ioat, IOAT_INTRCTRL_MASTER_INT_EN);
629 ioat_model_resets_msix(struct ioat_softc *ioat)
633 pciid = pci_get_devid(ioat->device);
652 ioat_interrupt_handler(void *arg)
654 struct ioat_softc *ioat = arg;
656 ioat->stats.interrupts++;
657 ioat_process_events(ioat, TRUE);
661 chanerr_to_errno(uint32_t chanerr)
666 if ((chanerr & (IOAT_CHANERR_XSADDERR | IOAT_CHANERR_XDADDERR)) != 0)
668 if ((chanerr & (IOAT_CHANERR_RDERR | IOAT_CHANERR_WDERR)) != 0)
670 /* This one is probably our fault: */
671 if ((chanerr & IOAT_CHANERR_NDADDERR) != 0)
677 ioat_process_events(struct ioat_softc *ioat, boolean_t intr)
679 struct ioat_descriptor *desc;
680 struct bus_dmadesc *dmadesc;
681 uint64_t comp_update, status;
682 uint32_t completed, chanerr;
685 mtx_lock(&ioat->cleanup_lock);
688 * Don't run while the hardware is being reset. Reset is responsible
689 * for blocking new work and draining & completing existing work, so
690 * there is nothing to do until new work is queued after reset anyway.
692 if (ioat->resetting_cleanup) {
693 mtx_unlock(&ioat->cleanup_lock);
698 comp_update = *ioat->comp_update;
699 status = comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK;
701 if (status < ioat->hw_desc_bus_addr ||
702 status >= ioat->hw_desc_bus_addr + (1 << ioat->ring_size_order) *
703 sizeof(struct ioat_generic_hw_descriptor))
704 panic("Bogus completion address %jx (channel %u)",
705 (uintmax_t)status, ioat->chan_idx);
707 if (status == ioat->last_seen) {
709 * If we landed in process_events and nothing has been
710 * completed, check for a timeout due to channel halt.
714 CTR4(KTR_IOAT, "%s channel=%u hw_status=0x%lx last_seen=0x%lx",
715 __func__, ioat->chan_idx, comp_update, ioat->last_seen);
717 while (RING_PHYS_ADDR(ioat, ioat->tail - 1) != status) {
718 desc = ioat_get_ring_entry(ioat, ioat->tail);
719 dmadesc = &desc->bus_dmadesc;
720 CTR5(KTR_IOAT, "channel=%u completing desc idx %u (%p) ok cb %p(%p)",
721 ioat->chan_idx, ioat->tail, dmadesc, dmadesc->callback_fn,
722 dmadesc->callback_arg);
724 if (dmadesc->callback_fn != NULL)
725 dmadesc->callback_fn(dmadesc->callback_arg, 0);
730 CTR5(KTR_IOAT, "%s channel=%u head=%u tail=%u active=%u", __func__,
731 ioat->chan_idx, ioat->head, ioat->tail, ioat_get_active(ioat));
733 if (completed != 0) {
734 ioat->last_seen = RING_PHYS_ADDR(ioat, ioat->tail - 1);
735 ioat->stats.descriptors_processed += completed;
740 ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
741 mtx_unlock(&ioat->cleanup_lock);
744 * The device doesn't seem to reliably push suspend/halt statuses to
745 * the channel completion memory address, so poll the device register
746 * here. For performance reasons skip it on interrupts, do it only
747 * on much more rare polling events.
750 comp_update = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS;
751 if (!is_ioat_halted(comp_update) && !is_ioat_suspended(comp_update))
754 ioat->stats.channel_halts++;
757 * Fatal programming error on this DMA channel. Flush any outstanding
758 * work with error status and restart the engine.
760 mtx_lock(&ioat->submit_lock);
761 ioat->quiescing = TRUE;
762 mtx_unlock(&ioat->submit_lock);
765 * This is safe to do here because the submit queue is quiesced. We
766 * know that we will drain all outstanding events, so ioat_reset_hw
767 * can't deadlock. It is necessary to protect other ioat_process_event
768 * threads from racing ioat_reset_hw, reading an indeterminate hw
769 * state, and attempting to continue issuing completions.
771 mtx_lock(&ioat->cleanup_lock);
772 ioat->resetting_cleanup = TRUE;
774 chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
775 if (1 <= g_ioat_debug_level)
776 ioat_halted_debug(ioat, chanerr);
777 ioat->stats.last_halt_chanerr = chanerr;
779 while (ioat_get_active(ioat) > 0) {
780 desc = ioat_get_ring_entry(ioat, ioat->tail);
781 dmadesc = &desc->bus_dmadesc;
782 CTR5(KTR_IOAT, "channel=%u completing desc idx %u (%p) err cb %p(%p)",
783 ioat->chan_idx, ioat->tail, dmadesc, dmadesc->callback_fn,
784 dmadesc->callback_arg);
786 if (dmadesc->callback_fn != NULL)
787 dmadesc->callback_fn(dmadesc->callback_arg,
788 chanerr_to_errno(chanerr));
791 ioat->stats.descriptors_processed++;
792 ioat->stats.descriptors_error++;
794 CTR5(KTR_IOAT, "%s channel=%u head=%u tail=%u active=%u", __func__,
795 ioat->chan_idx, ioat->head, ioat->tail, ioat_get_active(ioat));
797 /* Clear error status */
798 ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
800 mtx_unlock(&ioat->cleanup_lock);
802 ioat_log_message(0, "Resetting channel to recover from error\n");
803 error = taskqueue_enqueue(taskqueue_thread, &ioat->reset_task);
805 ("%s: taskqueue_enqueue failed: %d", __func__, error));
809 ioat_reset_hw_task(void *ctx, int pending __unused)
811 struct ioat_softc *ioat;
815 ioat_log_message(1, "%s: Resetting channel\n", __func__);
817 error = ioat_reset_hw(ioat);
818 KASSERT(error == 0, ("%s: reset failed: %d", __func__, error));
826 ioat_get_nchannels(void)
829 return (ioat_channel_index);
833 ioat_get_dmaengine(uint32_t index, int flags)
835 struct ioat_softc *ioat;
837 KASSERT((flags & ~(M_NOWAIT | M_WAITOK)) == 0,
838 ("invalid flags: 0x%08x", flags));
839 KASSERT((flags & (M_NOWAIT | M_WAITOK)) != (M_NOWAIT | M_WAITOK),
840 ("invalid wait | nowait"));
842 mtx_lock(&ioat_list_mtx);
843 if (index >= ioat_channel_index ||
844 (ioat = ioat_channel[index]) == NULL) {
845 mtx_unlock(&ioat_list_mtx);
848 mtx_lock(&ioat->submit_lock);
849 mtx_unlock(&ioat_list_mtx);
851 if (ioat->destroying) {
852 mtx_unlock(&ioat->submit_lock);
857 if (ioat->quiescing) {
858 if ((flags & M_NOWAIT) != 0) {
860 mtx_unlock(&ioat->submit_lock);
864 while (ioat->quiescing && !ioat->destroying)
865 msleep(&ioat->quiescing, &ioat->submit_lock, 0, "getdma", 0);
867 if (ioat->destroying) {
869 mtx_unlock(&ioat->submit_lock);
873 mtx_unlock(&ioat->submit_lock);
874 return (&ioat->dmaengine);
878 ioat_put_dmaengine(bus_dmaengine_t dmaengine)
880 struct ioat_softc *ioat;
882 ioat = to_ioat_softc(dmaengine);
883 mtx_lock(&ioat->submit_lock);
885 mtx_unlock(&ioat->submit_lock);
889 ioat_get_hwversion(bus_dmaengine_t dmaengine)
891 struct ioat_softc *ioat;
893 ioat = to_ioat_softc(dmaengine);
894 return (ioat->version);
898 ioat_get_max_io_size(bus_dmaengine_t dmaengine)
900 struct ioat_softc *ioat;
902 ioat = to_ioat_softc(dmaengine);
903 return (ioat->max_xfer_size);
907 ioat_get_capabilities(bus_dmaengine_t dmaengine)
909 struct ioat_softc *ioat;
911 ioat = to_ioat_softc(dmaengine);
912 return (ioat->capabilities);
916 ioat_set_interrupt_coalesce(bus_dmaengine_t dmaengine, uint16_t delay)
918 struct ioat_softc *ioat;
920 ioat = to_ioat_softc(dmaengine);
921 if (!ioat->intrdelay_supported)
923 if (delay > ioat->intrdelay_max)
926 ioat_write_2(ioat, IOAT_INTRDELAY_OFFSET, delay);
927 ioat->cached_intrdelay =
928 ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) & IOAT_INTRDELAY_US_MASK;
933 ioat_get_max_coalesce_period(bus_dmaengine_t dmaengine)
935 struct ioat_softc *ioat;
937 ioat = to_ioat_softc(dmaengine);
938 return (ioat->intrdelay_max);
942 ioat_acquire(bus_dmaengine_t dmaengine)
944 struct ioat_softc *ioat;
946 ioat = to_ioat_softc(dmaengine);
947 mtx_lock(&ioat->submit_lock);
948 CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
949 ioat->acq_head = ioat->head;
953 ioat_acquire_reserve(bus_dmaengine_t dmaengine, unsigned n, int mflags)
955 struct ioat_softc *ioat;
958 ioat = to_ioat_softc(dmaengine);
959 ioat_acquire(dmaengine);
961 error = ioat_reserve_space(ioat, n, mflags);
963 ioat_release(dmaengine);
968 ioat_release(bus_dmaengine_t dmaengine)
970 struct ioat_softc *ioat;
972 ioat = to_ioat_softc(dmaengine);
973 CTR3(KTR_IOAT, "%s channel=%u dispatch1 head=%u", __func__,
974 ioat->chan_idx, ioat->head);
975 KFAIL_POINT_CODE(DEBUG_FP, ioat_release, /* do nothing */);
976 CTR3(KTR_IOAT, "%s channel=%u dispatch2 head=%u", __func__,
977 ioat->chan_idx, ioat->head);
979 if (ioat->acq_head != ioat->head) {
980 ioat_write_2(ioat, IOAT_DMACOUNT_OFFSET,
981 (uint16_t)ioat->head);
983 if (!callout_pending(&ioat->poll_timer)) {
984 callout_reset(&ioat->poll_timer, 1,
985 ioat_poll_timer_callback, ioat);
988 mtx_unlock(&ioat->submit_lock);
991 static struct ioat_descriptor *
992 ioat_op_generic(struct ioat_softc *ioat, uint8_t op,
993 uint32_t size, uint64_t src, uint64_t dst,
994 bus_dmaengine_callback_t callback_fn, void *callback_arg,
997 struct ioat_generic_hw_descriptor *hw_desc;
998 struct ioat_descriptor *desc;
1001 mtx_assert(&ioat->submit_lock, MA_OWNED);
1003 KASSERT((flags & ~_DMA_GENERIC_FLAGS) == 0,
1004 ("Unrecognized flag(s): %#x", flags & ~_DMA_GENERIC_FLAGS));
1005 if ((flags & DMA_NO_WAIT) != 0)
1010 if (size > ioat->max_xfer_size) {
1011 ioat_log_message(0, "%s: max_xfer_size = %d, requested = %u\n",
1012 __func__, ioat->max_xfer_size, (unsigned)size);
1016 if (ioat_reserve_space(ioat, 1, mflags) != 0)
1019 desc = ioat_get_ring_entry(ioat, ioat->head);
1020 hw_desc = &ioat_get_descriptor(ioat, ioat->head)->generic;
1022 hw_desc->u.control_raw = 0;
1023 hw_desc->u.control_generic.op = op;
1024 hw_desc->u.control_generic.completion_update = 1;
1026 if ((flags & DMA_INT_EN) != 0)
1027 hw_desc->u.control_generic.int_enable = 1;
1028 if ((flags & DMA_FENCE) != 0)
1029 hw_desc->u.control_generic.fence = 1;
1031 hw_desc->size = size;
1032 hw_desc->src_addr = src;
1033 hw_desc->dest_addr = dst;
1035 desc->bus_dmadesc.callback_fn = callback_fn;
1036 desc->bus_dmadesc.callback_arg = callback_arg;
1040 struct bus_dmadesc *
1041 ioat_null(bus_dmaengine_t dmaengine, bus_dmaengine_callback_t callback_fn,
1042 void *callback_arg, uint32_t flags)
1044 struct ioat_dma_hw_descriptor *hw_desc;
1045 struct ioat_descriptor *desc;
1046 struct ioat_softc *ioat;
1048 ioat = to_ioat_softc(dmaengine);
1049 CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1051 desc = ioat_op_generic(ioat, IOAT_OP_COPY, 8, 0, 0, callback_fn,
1052 callback_arg, flags);
1056 hw_desc = &ioat_get_descriptor(ioat, desc->id)->dma;
1057 hw_desc->u.control.null = 1;
1058 ioat_submit_single(ioat);
1059 return (&desc->bus_dmadesc);
1062 struct bus_dmadesc *
1063 ioat_copy(bus_dmaengine_t dmaengine, bus_addr_t dst,
1064 bus_addr_t src, bus_size_t len, bus_dmaengine_callback_t callback_fn,
1065 void *callback_arg, uint32_t flags)
1067 struct ioat_dma_hw_descriptor *hw_desc;
1068 struct ioat_descriptor *desc;
1069 struct ioat_softc *ioat;
1071 ioat = to_ioat_softc(dmaengine);
1073 if (((src | dst) & (0xffffull << 48)) != 0) {
1074 ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n",
1079 desc = ioat_op_generic(ioat, IOAT_OP_COPY, len, src, dst, callback_fn,
1080 callback_arg, flags);
1084 hw_desc = &ioat_get_descriptor(ioat, desc->id)->dma;
1085 if (g_ioat_debug_level >= 3)
1086 dump_descriptor(hw_desc);
1088 ioat_submit_single(ioat);
1089 CTR6(KTR_IOAT, "%s channel=%u desc=%p dest=%lx src=%lx len=%lx",
1090 __func__, ioat->chan_idx, &desc->bus_dmadesc, dst, src, len);
1091 return (&desc->bus_dmadesc);
1094 struct bus_dmadesc *
1095 ioat_copy_8k_aligned(bus_dmaengine_t dmaengine, bus_addr_t dst1,
1096 bus_addr_t dst2, bus_addr_t src1, bus_addr_t src2,
1097 bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
1099 struct ioat_dma_hw_descriptor *hw_desc;
1100 struct ioat_descriptor *desc;
1101 struct ioat_softc *ioat;
1103 ioat = to_ioat_softc(dmaengine);
1104 CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1106 if (((src1 | src2 | dst1 | dst2) & (0xffffull << 48)) != 0) {
1107 ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n",
1111 if (((src1 | src2 | dst1 | dst2) & PAGE_MASK) != 0) {
1112 ioat_log_message(0, "%s: Addresses must be page-aligned\n",
1117 desc = ioat_op_generic(ioat, IOAT_OP_COPY, 2 * PAGE_SIZE, src1, dst1,
1118 callback_fn, callback_arg, flags);
1122 hw_desc = &ioat_get_descriptor(ioat, desc->id)->dma;
1123 if (src2 != src1 + PAGE_SIZE) {
1124 hw_desc->u.control.src_page_break = 1;
1125 hw_desc->next_src_addr = src2;
1127 if (dst2 != dst1 + PAGE_SIZE) {
1128 hw_desc->u.control.dest_page_break = 1;
1129 hw_desc->next_dest_addr = dst2;
1132 if (g_ioat_debug_level >= 3)
1133 dump_descriptor(hw_desc);
1135 ioat_submit_single(ioat);
1136 return (&desc->bus_dmadesc);
1139 struct bus_dmadesc *
1140 ioat_copy_crc(bus_dmaengine_t dmaengine, bus_addr_t dst, bus_addr_t src,
1141 bus_size_t len, uint32_t *initialseed, bus_addr_t crcptr,
1142 bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
1144 struct ioat_crc32_hw_descriptor *hw_desc;
1145 struct ioat_descriptor *desc;
1146 struct ioat_softc *ioat;
1150 ioat = to_ioat_softc(dmaengine);
1151 CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1153 if ((ioat->capabilities & IOAT_DMACAP_MOVECRC) == 0) {
1154 ioat_log_message(0, "%s: Device lacks MOVECRC capability\n",
1158 if (((src | dst) & (0xffffffull << 40)) != 0) {
1159 ioat_log_message(0, "%s: High 24 bits of src/dst invalid\n",
1163 teststore = (flags & _DMA_CRC_TESTSTORE);
1164 if (teststore == _DMA_CRC_TESTSTORE) {
1165 ioat_log_message(0, "%s: TEST and STORE invalid\n", __func__);
1168 if (teststore == 0 && (flags & DMA_CRC_INLINE) != 0) {
1169 ioat_log_message(0, "%s: INLINE invalid without TEST or STORE\n",
1174 switch (teststore) {
1176 op = IOAT_OP_MOVECRC_STORE;
1179 op = IOAT_OP_MOVECRC_TEST;
1182 KASSERT(teststore == 0, ("bogus"));
1183 op = IOAT_OP_MOVECRC;
1187 if ((flags & DMA_CRC_INLINE) == 0 &&
1188 (crcptr & (0xffffffull << 40)) != 0) {
1190 "%s: High 24 bits of crcptr invalid\n", __func__);
1194 desc = ioat_op_generic(ioat, op, len, src, dst, callback_fn,
1195 callback_arg, flags & ~_DMA_CRC_FLAGS);
1199 hw_desc = &ioat_get_descriptor(ioat, desc->id)->crc32;
1201 if ((flags & DMA_CRC_INLINE) == 0)
1202 hw_desc->crc_address = crcptr;
1204 hw_desc->u.control.crc_location = 1;
1206 if (initialseed != NULL) {
1207 hw_desc->u.control.use_seed = 1;
1208 hw_desc->seed = *initialseed;
1211 if (g_ioat_debug_level >= 3)
1212 dump_descriptor(hw_desc);
1214 ioat_submit_single(ioat);
1215 return (&desc->bus_dmadesc);
1218 struct bus_dmadesc *
1219 ioat_crc(bus_dmaengine_t dmaengine, bus_addr_t src, bus_size_t len,
1220 uint32_t *initialseed, bus_addr_t crcptr,
1221 bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
1223 struct ioat_crc32_hw_descriptor *hw_desc;
1224 struct ioat_descriptor *desc;
1225 struct ioat_softc *ioat;
1229 ioat = to_ioat_softc(dmaengine);
1230 CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1232 if ((ioat->capabilities & IOAT_DMACAP_CRC) == 0) {
1233 ioat_log_message(0, "%s: Device lacks CRC capability\n",
1237 if ((src & (0xffffffull << 40)) != 0) {
1238 ioat_log_message(0, "%s: High 24 bits of src invalid\n",
1242 teststore = (flags & _DMA_CRC_TESTSTORE);
1243 if (teststore == _DMA_CRC_TESTSTORE) {
1244 ioat_log_message(0, "%s: TEST and STORE invalid\n", __func__);
1247 if (teststore == 0 && (flags & DMA_CRC_INLINE) != 0) {
1248 ioat_log_message(0, "%s: INLINE invalid without TEST or STORE\n",
1253 switch (teststore) {
1255 op = IOAT_OP_CRC_STORE;
1258 op = IOAT_OP_CRC_TEST;
1261 KASSERT(teststore == 0, ("bogus"));
1266 if ((flags & DMA_CRC_INLINE) == 0 &&
1267 (crcptr & (0xffffffull << 40)) != 0) {
1269 "%s: High 24 bits of crcptr invalid\n", __func__);
1273 desc = ioat_op_generic(ioat, op, len, src, 0, callback_fn,
1274 callback_arg, flags & ~_DMA_CRC_FLAGS);
1278 hw_desc = &ioat_get_descriptor(ioat, desc->id)->crc32;
1280 if ((flags & DMA_CRC_INLINE) == 0)
1281 hw_desc->crc_address = crcptr;
1283 hw_desc->u.control.crc_location = 1;
1285 if (initialseed != NULL) {
1286 hw_desc->u.control.use_seed = 1;
1287 hw_desc->seed = *initialseed;
1290 if (g_ioat_debug_level >= 3)
1291 dump_descriptor(hw_desc);
1293 ioat_submit_single(ioat);
1294 return (&desc->bus_dmadesc);
1297 struct bus_dmadesc *
1298 ioat_blockfill(bus_dmaengine_t dmaengine, bus_addr_t dst, uint64_t fillpattern,
1299 bus_size_t len, bus_dmaengine_callback_t callback_fn, void *callback_arg,
1302 struct ioat_fill_hw_descriptor *hw_desc;
1303 struct ioat_descriptor *desc;
1304 struct ioat_softc *ioat;
1306 ioat = to_ioat_softc(dmaengine);
1307 CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1309 if ((ioat->capabilities & IOAT_DMACAP_BFILL) == 0) {
1310 ioat_log_message(0, "%s: Device lacks BFILL capability\n",
1315 if ((dst & (0xffffull << 48)) != 0) {
1316 ioat_log_message(0, "%s: High 16 bits of dst invalid\n",
1321 desc = ioat_op_generic(ioat, IOAT_OP_FILL, len, fillpattern, dst,
1322 callback_fn, callback_arg, flags);
1326 hw_desc = &ioat_get_descriptor(ioat, desc->id)->fill;
1327 if (g_ioat_debug_level >= 3)
1328 dump_descriptor(hw_desc);
1330 ioat_submit_single(ioat);
1331 return (&desc->bus_dmadesc);
1337 static inline uint32_t
1338 ioat_get_active(struct ioat_softc *ioat)
1341 return ((ioat->head - ioat->tail) & ((1 << ioat->ring_size_order) - 1));
1344 static inline uint32_t
1345 ioat_get_ring_space(struct ioat_softc *ioat)
1348 return ((1 << ioat->ring_size_order) - ioat_get_active(ioat) - 1);
1352 * Reserves space in this IOAT descriptor ring by ensuring enough slots remain
1355 * If mflags contains M_WAITOK, blocks until enough space is available.
1357 * Returns zero on success, or an errno on error. If num_descs is beyond the
1358 * maximum ring size, returns EINVAl; if allocation would block and mflags
1359 * contains M_NOWAIT, returns EAGAIN.
1361 * Must be called with the submit_lock held; returns with the lock held. The
1362 * lock may be dropped to allocate the ring.
1364 * (The submit_lock is needed to add any entries to the ring, so callers are
1365 * assured enough room is available.)
1368 ioat_reserve_space(struct ioat_softc *ioat, uint32_t num_descs, int mflags)
1373 mtx_assert(&ioat->submit_lock, MA_OWNED);
1377 if (num_descs < 1 || num_descs >= (1 << ioat->ring_size_order)) {
1383 if (ioat->quiescing) {
1388 if (ioat_get_ring_space(ioat) >= num_descs)
1391 CTR3(KTR_IOAT, "%s channel=%u starved (%u)", __func__,
1392 ioat->chan_idx, num_descs);
1394 if (!dug && !ioat->is_submitter_processing) {
1395 ioat->is_submitter_processing = TRUE;
1396 mtx_unlock(&ioat->submit_lock);
1398 CTR2(KTR_IOAT, "%s channel=%u attempting to process events",
1399 __func__, ioat->chan_idx);
1400 ioat_process_events(ioat, FALSE);
1402 mtx_lock(&ioat->submit_lock);
1404 KASSERT(ioat->is_submitter_processing == TRUE,
1405 ("is_submitter_processing"));
1406 ioat->is_submitter_processing = FALSE;
1407 wakeup(&ioat->tail);
1411 if ((mflags & M_WAITOK) == 0) {
1415 CTR2(KTR_IOAT, "%s channel=%u blocking on completions",
1416 __func__, ioat->chan_idx);
1417 msleep(&ioat->tail, &ioat->submit_lock, 0,
1423 mtx_assert(&ioat->submit_lock, MA_OWNED);
1424 KASSERT(!ioat->quiescing || error == ENXIO,
1425 ("reserved during quiesce"));
1430 ioat_free_ring(struct ioat_softc *ioat, uint32_t size,
1431 struct ioat_descriptor *ring)
1437 static struct ioat_descriptor *
1438 ioat_get_ring_entry(struct ioat_softc *ioat, uint32_t index)
1441 return (&ioat->ring[index % (1 << ioat->ring_size_order)]);
1444 static union ioat_hw_descriptor *
1445 ioat_get_descriptor(struct ioat_softc *ioat, uint32_t index)
1448 return (&ioat->hw_desc_ring[index % (1 << ioat->ring_size_order)]);
1452 ioat_halted_debug(struct ioat_softc *ioat, uint32_t chanerr)
1454 union ioat_hw_descriptor *desc;
1456 ioat_log_message(0, "Channel halted (%b)\n", (int)chanerr,
1461 mtx_assert(&ioat->cleanup_lock, MA_OWNED);
1463 desc = ioat_get_descriptor(ioat, ioat->tail + 0);
1464 dump_descriptor(desc);
1466 desc = ioat_get_descriptor(ioat, ioat->tail + 1);
1467 dump_descriptor(desc);
1471 ioat_poll_timer_callback(void *arg)
1473 struct ioat_softc *ioat;
1476 ioat_log_message(3, "%s\n", __func__);
1478 ioat_process_events(ioat, FALSE);
1480 mtx_lock(&ioat->submit_lock);
1481 if (ioat_get_active(ioat) > 0)
1482 callout_schedule(&ioat->poll_timer, 1);
1483 mtx_unlock(&ioat->submit_lock);
1490 ioat_submit_single(struct ioat_softc *ioat)
1493 mtx_assert(&ioat->submit_lock, MA_OWNED);
1496 CTR4(KTR_IOAT, "%s channel=%u head=%u tail=%u", __func__,
1497 ioat->chan_idx, ioat->head, ioat->tail);
1499 ioat->stats.descriptors_submitted++;
1503 ioat_reset_hw(struct ioat_softc *ioat)
1510 CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1512 mtx_lock(&ioat->submit_lock);
1513 while (ioat->resetting && !ioat->destroying)
1514 msleep(&ioat->resetting, &ioat->submit_lock, 0, "IRH_drain", 0);
1515 if (ioat->destroying) {
1516 mtx_unlock(&ioat->submit_lock);
1519 ioat->resetting = TRUE;
1520 ioat->quiescing = TRUE;
1521 mtx_unlock(&ioat->submit_lock);
1522 mtx_lock(&ioat->cleanup_lock);
1523 while (ioat_get_active(ioat) > 0)
1524 msleep(&ioat->tail, &ioat->cleanup_lock, 0, "ioat_drain", 1);
1527 * Suspend ioat_process_events while the hardware and softc are in an
1528 * indeterminate state.
1530 ioat->resetting_cleanup = TRUE;
1531 mtx_unlock(&ioat->cleanup_lock);
1533 CTR2(KTR_IOAT, "%s channel=%u quiesced and drained", __func__,
1536 status = ioat_get_chansts(ioat);
1537 if (is_ioat_active(status) || is_ioat_idle(status))
1540 /* Wait at most 20 ms */
1541 for (timeout = 0; (is_ioat_active(status) || is_ioat_idle(status)) &&
1542 timeout < 20; timeout++) {
1544 status = ioat_get_chansts(ioat);
1546 if (timeout == 20) {
1551 KASSERT(ioat_get_active(ioat) == 0, ("active after quiesce"));
1553 chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
1554 ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
1556 CTR2(KTR_IOAT, "%s channel=%u hardware suspended", __func__,
1560 * IOAT v3 workaround - CHANERRMSK_INT with 3E07h to masks out errors
1561 * that can cause stability issues for IOAT v3.
1563 pci_write_config(ioat->device, IOAT_CFG_CHANERRMASK_INT_OFFSET, 0x3e07,
1565 chanerr = pci_read_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, 4);
1566 pci_write_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, chanerr, 4);
1569 * BDXDE and BWD models reset MSI-X registers on device reset.
1570 * Save/restore their contents manually.
1572 if (ioat_model_resets_msix(ioat)) {
1573 ioat_log_message(1, "device resets MSI-X registers; saving\n");
1574 pci_save_state(ioat->device);
1578 CTR2(KTR_IOAT, "%s channel=%u hardware reset", __func__,
1581 /* Wait at most 20 ms */
1582 for (timeout = 0; ioat_reset_pending(ioat) && timeout < 20; timeout++)
1584 if (timeout == 20) {
1589 if (ioat_model_resets_msix(ioat)) {
1590 ioat_log_message(1, "device resets registers; restored\n");
1591 pci_restore_state(ioat->device);
1594 /* Reset attempts to return the hardware to "halted." */
1595 status = ioat_get_chansts(ioat);
1596 if (is_ioat_active(status) || is_ioat_idle(status)) {
1597 /* So this really shouldn't happen... */
1598 ioat_log_message(0, "Device is active after a reset?\n");
1599 ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
1604 chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
1606 mtx_lock(&ioat->cleanup_lock);
1607 ioat_halted_debug(ioat, chanerr);
1608 mtx_unlock(&ioat->cleanup_lock);
1614 * Bring device back online after reset. Writing CHAINADDR brings the
1615 * device back to active.
1617 * The internal ring counter resets to zero, so we have to start over
1620 ioat->tail = ioat->head = 0;
1621 ioat->last_seen = 0;
1622 *ioat->comp_update = 0;
1624 ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
1625 ioat_write_chancmp(ioat, ioat->comp_update_bus_addr);
1626 ioat_write_chainaddr(ioat, RING_PHYS_ADDR(ioat, 0));
1628 CTR2(KTR_IOAT, "%s channel=%u configured channel", __func__,
1632 /* Enqueues a null operation and ensures it completes. */
1634 error = ioat_start_channel(ioat);
1635 CTR2(KTR_IOAT, "%s channel=%u started channel", __func__,
1640 * Resume completions now that ring state is consistent.
1642 mtx_lock(&ioat->cleanup_lock);
1643 ioat->resetting_cleanup = FALSE;
1644 mtx_unlock(&ioat->cleanup_lock);
1646 /* Unblock submission of new work */
1647 mtx_lock(&ioat->submit_lock);
1648 ioat->quiescing = FALSE;
1649 wakeup(&ioat->quiescing);
1651 ioat->resetting = FALSE;
1652 wakeup(&ioat->resetting);
1654 CTR2(KTR_IOAT, "%s channel=%u reset done", __func__, ioat->chan_idx);
1655 mtx_unlock(&ioat->submit_lock);
1661 sysctl_handle_chansts(SYSCTL_HANDLER_ARGS)
1663 struct ioat_softc *ioat;
1670 status = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS;
1672 sbuf_new_for_sysctl(&sb, NULL, 256, req);
1674 case IOAT_CHANSTS_ACTIVE:
1675 sbuf_printf(&sb, "ACTIVE");
1677 case IOAT_CHANSTS_IDLE:
1678 sbuf_printf(&sb, "IDLE");
1680 case IOAT_CHANSTS_SUSPENDED:
1681 sbuf_printf(&sb, "SUSPENDED");
1683 case IOAT_CHANSTS_HALTED:
1684 sbuf_printf(&sb, "HALTED");
1686 case IOAT_CHANSTS_ARMED:
1687 sbuf_printf(&sb, "ARMED");
1690 sbuf_printf(&sb, "UNKNOWN");
1693 error = sbuf_finish(&sb);
1696 if (error != 0 || req->newptr == NULL)
1702 sysctl_handle_dpi(SYSCTL_HANDLER_ARGS)
1704 struct ioat_softc *ioat;
1706 #define PRECISION "1"
1707 const uintmax_t factor = 10;
1712 sbuf_new_for_sysctl(&sb, NULL, 16, req);
1714 if (ioat->stats.interrupts == 0) {
1715 sbuf_printf(&sb, "NaN");
1718 rate = ioat->stats.descriptors_processed * factor /
1719 ioat->stats.interrupts;
1720 sbuf_printf(&sb, "%ju.%." PRECISION "ju", rate / factor,
1724 error = sbuf_finish(&sb);
1726 if (error != 0 || req->newptr == NULL)
1732 sysctl_handle_reset(SYSCTL_HANDLER_ARGS)
1734 struct ioat_softc *ioat;
1740 error = SYSCTL_OUT(req, &arg, sizeof(arg));
1741 if (error != 0 || req->newptr == NULL)
1744 error = SYSCTL_IN(req, &arg, sizeof(arg));
1749 error = ioat_reset_hw(ioat);
1755 dump_descriptor(void *hw_desc)
1759 for (i = 0; i < 2; i++) {
1760 for (j = 0; j < 8; j++)
1761 printf("%08x ", ((uint32_t *)hw_desc)[i * 8 + j]);
1767 ioat_setup_sysctl(device_t device)
1769 struct sysctl_oid_list *par, *statpar, *state, *hammer;
1770 struct sysctl_ctx_list *ctx;
1771 struct sysctl_oid *tree, *tmp;
1772 struct ioat_softc *ioat;
1774 ioat = DEVICE2SOFTC(device);
1775 ctx = device_get_sysctl_ctx(device);
1776 tree = device_get_sysctl_tree(device);
1777 par = SYSCTL_CHILDREN(tree);
1779 SYSCTL_ADD_INT(ctx, par, OID_AUTO, "version", CTLFLAG_RD,
1780 &ioat->version, 0, "HW version (0xMM form)");
1781 SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "max_xfer_size", CTLFLAG_RD,
1782 &ioat->max_xfer_size, 0, "HW maximum transfer size");
1783 SYSCTL_ADD_INT(ctx, par, OID_AUTO, "intrdelay_supported", CTLFLAG_RD,
1784 &ioat->intrdelay_supported, 0, "Is INTRDELAY supported");
1785 SYSCTL_ADD_U16(ctx, par, OID_AUTO, "intrdelay_max", CTLFLAG_RD,
1786 &ioat->intrdelay_max, 0,
1787 "Maximum configurable INTRDELAY on this channel (microseconds)");
1789 tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "state", CTLFLAG_RD, NULL,
1790 "IOAT channel internal state");
1791 state = SYSCTL_CHILDREN(tmp);
1793 SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "ring_size_order", CTLFLAG_RD,
1794 &ioat->ring_size_order, 0, "SW descriptor ring size order");
1795 SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "head", CTLFLAG_RD, &ioat->head,
1796 0, "SW descriptor head pointer index");
1797 SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "tail", CTLFLAG_RD, &ioat->tail,
1798 0, "SW descriptor tail pointer index");
1800 SYSCTL_ADD_UQUAD(ctx, state, OID_AUTO, "last_completion", CTLFLAG_RD,
1801 ioat->comp_update, "HW addr of last completion");
1803 SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_submitter_processing",
1804 CTLFLAG_RD, &ioat->is_submitter_processing, 0,
1805 "submitter processing");
1807 SYSCTL_ADD_PROC(ctx, state, OID_AUTO, "chansts",
1808 CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_chansts, "A",
1809 "String of the channel status");
1811 SYSCTL_ADD_U16(ctx, state, OID_AUTO, "intrdelay", CTLFLAG_RD,
1812 &ioat->cached_intrdelay, 0,
1813 "Current INTRDELAY on this channel (cached, microseconds)");
1815 tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "hammer", CTLFLAG_RD, NULL,
1816 "Big hammers (mostly for testing)");
1817 hammer = SYSCTL_CHILDREN(tmp);
1819 SYSCTL_ADD_PROC(ctx, hammer, OID_AUTO, "force_hw_reset",
1820 CTLTYPE_INT | CTLFLAG_RW, ioat, 0, sysctl_handle_reset, "I",
1821 "Set to non-zero to reset the hardware");
1823 tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "stats", CTLFLAG_RD, NULL,
1824 "IOAT channel statistics");
1825 statpar = SYSCTL_CHILDREN(tmp);
1827 SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "interrupts", CTLFLAG_RW,
1828 &ioat->stats.interrupts,
1829 "Number of interrupts processed on this channel");
1830 SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "descriptors", CTLFLAG_RW,
1831 &ioat->stats.descriptors_processed,
1832 "Number of descriptors processed on this channel");
1833 SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "submitted", CTLFLAG_RW,
1834 &ioat->stats.descriptors_submitted,
1835 "Number of descriptors submitted to this channel");
1836 SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "errored", CTLFLAG_RW,
1837 &ioat->stats.descriptors_error,
1838 "Number of descriptors failed by channel errors");
1839 SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "halts", CTLFLAG_RW,
1840 &ioat->stats.channel_halts, 0,
1841 "Number of times the channel has halted");
1842 SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "last_halt_chanerr", CTLFLAG_RW,
1843 &ioat->stats.last_halt_chanerr, 0,
1844 "The raw CHANERR when the channel was last halted");
1846 SYSCTL_ADD_PROC(ctx, statpar, OID_AUTO, "desc_per_interrupt",
1847 CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_dpi, "A",
1848 "Descriptors per interrupt");
1852 ioat_get(struct ioat_softc *ioat)
1855 mtx_assert(&ioat->submit_lock, MA_OWNED);
1856 KASSERT(ioat->refcnt < UINT32_MAX, ("refcnt overflow"));
1862 ioat_put(struct ioat_softc *ioat)
1865 mtx_assert(&ioat->submit_lock, MA_OWNED);
1866 KASSERT(ioat->refcnt >= 1, ("refcnt error"));
1868 if (--ioat->refcnt == 0)
1869 wakeup(&ioat->refcnt);
1873 ioat_drain_locked(struct ioat_softc *ioat)
1876 mtx_assert(&ioat->submit_lock, MA_OWNED);
1878 while (ioat->refcnt > 0)
1879 msleep(&ioat->refcnt, &ioat->submit_lock, 0, "ioat_drain", 0);
1883 #define _db_show_lock(lo) LOCK_CLASS(lo)->lc_ddb_show(lo)
1884 #define db_show_lock(lk) _db_show_lock(&(lk)->lock_object)
1885 DB_SHOW_COMMAND(ioat, db_show_ioat)
1887 struct ioat_softc *sc;
1892 idx = (unsigned)addr;
1893 if (idx >= ioat_channel_index)
1896 sc = ioat_channel[idx];
1897 db_printf("ioat softc at %p\n", sc);
1901 db_printf(" version: %d\n", sc->version);
1902 db_printf(" chan_idx: %u\n", sc->chan_idx);
1903 db_printf(" submit_lock: ");
1904 db_show_lock(&sc->submit_lock);
1906 db_printf(" capabilities: %b\n", (int)sc->capabilities,
1908 db_printf(" cached_intrdelay: %u\n", sc->cached_intrdelay);
1909 db_printf(" *comp_update: 0x%jx\n", (uintmax_t)*sc->comp_update);
1911 db_printf(" poll_timer:\n");
1912 db_printf(" c_time: %ju\n", (uintmax_t)sc->poll_timer.c_time);
1913 db_printf(" c_arg: %p\n", sc->poll_timer.c_arg);
1914 db_printf(" c_func: %p\n", sc->poll_timer.c_func);
1915 db_printf(" c_lock: %p\n", sc->poll_timer.c_lock);
1916 db_printf(" c_flags: 0x%x\n", (unsigned)sc->poll_timer.c_flags);
1918 db_printf(" quiescing: %d\n", (int)sc->quiescing);
1919 db_printf(" destroying: %d\n", (int)sc->destroying);
1920 db_printf(" is_submitter_processing: %d\n",
1921 (int)sc->is_submitter_processing);
1922 db_printf(" intrdelay_supported: %d\n", (int)sc->intrdelay_supported);
1923 db_printf(" resetting: %d\n", (int)sc->resetting);
1925 db_printf(" head: %u\n", sc->head);
1926 db_printf(" tail: %u\n", sc->tail);
1927 db_printf(" ring_size_order: %u\n", sc->ring_size_order);
1928 db_printf(" last_seen: 0x%lx\n", sc->last_seen);
1929 db_printf(" ring: %p\n", sc->ring);
1930 db_printf(" descriptors: %p\n", sc->hw_desc_ring);
1931 db_printf(" descriptors (phys): 0x%jx\n",
1932 (uintmax_t)sc->hw_desc_bus_addr);
1934 db_printf(" ring[%u] (tail):\n", sc->tail %
1935 (1 << sc->ring_size_order));
1936 db_printf(" id: %u\n", ioat_get_ring_entry(sc, sc->tail)->id);
1937 db_printf(" addr: 0x%lx\n",
1938 RING_PHYS_ADDR(sc, sc->tail));
1939 db_printf(" next: 0x%lx\n",
1940 ioat_get_descriptor(sc, sc->tail)->generic.next);
1942 db_printf(" ring[%u] (head - 1):\n", (sc->head - 1) %
1943 (1 << sc->ring_size_order));
1944 db_printf(" id: %u\n", ioat_get_ring_entry(sc, sc->head - 1)->id);
1945 db_printf(" addr: 0x%lx\n",
1946 RING_PHYS_ADDR(sc, sc->head - 1));
1947 db_printf(" next: 0x%lx\n",
1948 ioat_get_descriptor(sc, sc->head - 1)->generic.next);
1950 db_printf(" ring[%u] (head):\n", (sc->head) %
1951 (1 << sc->ring_size_order));
1952 db_printf(" id: %u\n", ioat_get_ring_entry(sc, sc->head)->id);
1953 db_printf(" addr: 0x%lx\n",
1954 RING_PHYS_ADDR(sc, sc->head));
1955 db_printf(" next: 0x%lx\n",
1956 ioat_get_descriptor(sc, sc->head)->generic.next);
1958 for (idx = 0; idx < (1 << sc->ring_size_order); idx++)
1959 if ((*sc->comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK)
1960 == RING_PHYS_ADDR(sc, idx))
1961 db_printf(" ring[%u] == hardware tail\n", idx);
1963 db_printf(" cleanup_lock: ");
1964 db_show_lock(&sc->cleanup_lock);
1966 db_printf(" refcnt: %u\n", sc->refcnt);
1967 db_printf(" stats:\n");
1968 db_printf(" interrupts: %lu\n", sc->stats.interrupts);
1969 db_printf(" descriptors_processed: %lu\n", sc->stats.descriptors_processed);
1970 db_printf(" descriptors_error: %lu\n", sc->stats.descriptors_error);
1971 db_printf(" descriptors_submitted: %lu\n", sc->stats.descriptors_submitted);
1973 db_printf(" channel_halts: %u\n", sc->stats.channel_halts);
1974 db_printf(" last_halt_chanerr: %u\n", sc->stats.last_halt_chanerr);
1979 db_printf(" hw status:\n");
1980 db_printf(" status: 0x%lx\n", ioat_get_chansts(sc));
1981 db_printf(" chanctrl: 0x%x\n",
1982 (unsigned)ioat_read_2(sc, IOAT_CHANCTRL_OFFSET));
1983 db_printf(" chancmd: 0x%x\n",
1984 (unsigned)ioat_read_1(sc, IOAT_CHANCMD_OFFSET));
1985 db_printf(" dmacount: 0x%x\n",
1986 (unsigned)ioat_read_2(sc, IOAT_DMACOUNT_OFFSET));
1987 db_printf(" chainaddr: 0x%lx\n",
1988 ioat_read_double_4(sc, IOAT_CHAINADDR_OFFSET_LOW));
1989 db_printf(" chancmp: 0x%lx\n",
1990 ioat_read_double_4(sc, IOAT_CHANCMP_OFFSET_LOW));
1991 db_printf(" chanerr: %b\n",
1992 (int)ioat_read_4(sc, IOAT_CHANERR_OFFSET), IOAT_CHANERR_STR);
1995 db_printf("usage: show ioat <0-%u>\n", ioat_channel_index);