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)" },
235 { 0x20218086, "SKX IOAT" },
238 MODULE_PNP_INFO("W32:vendor/device;D:#", pci, ioat, pci_ids,
242 * OS <-> Driver linkage functions
245 ioat_probe(device_t device)
250 type = pci_get_devid(device);
251 for (ep = pci_ids; ep < &pci_ids[nitems(pci_ids)]; ep++) {
252 if (ep->type == type) {
253 device_set_desc(device, ep->desc);
261 ioat_attach(device_t device)
263 struct ioat_softc *ioat;
266 ioat = DEVICE2SOFTC(device);
267 ioat->device = device;
269 error = ioat_map_pci_bar(ioat);
273 ioat->version = ioat_read_cbver(ioat);
274 if (ioat->version < IOAT_VER_3_0) {
279 error = ioat3_attach(device);
283 error = pci_enable_busmaster(device);
287 error = ioat_setup_intr(ioat);
291 error = ioat_reset_hw(ioat);
295 ioat_process_events(ioat, FALSE);
296 ioat_setup_sysctl(device);
298 mtx_lock(&ioat_list_mtx);
299 for (i = 0; i < IOAT_MAX_CHANNELS; i++) {
300 if (ioat_channel[i] == NULL)
303 if (i >= IOAT_MAX_CHANNELS) {
304 mtx_unlock(&ioat_list_mtx);
305 device_printf(device, "Too many I/OAT devices in system\n");
310 ioat_channel[i] = ioat;
311 if (i >= ioat_channel_index)
312 ioat_channel_index = i + 1;
313 mtx_unlock(&ioat_list_mtx);
324 ioat_detach(device_t device)
326 struct ioat_softc *ioat;
328 ioat = DEVICE2SOFTC(device);
330 mtx_lock(&ioat_list_mtx);
331 ioat_channel[ioat->chan_idx] = NULL;
332 while (ioat_channel_index > 0 &&
333 ioat_channel[ioat_channel_index - 1] == NULL)
334 ioat_channel_index--;
335 mtx_unlock(&ioat_list_mtx);
338 taskqueue_drain(taskqueue_thread, &ioat->reset_task);
340 mtx_lock(&ioat->submit_lock);
341 ioat->quiescing = TRUE;
342 ioat->destroying = TRUE;
343 wakeup(&ioat->quiescing);
344 wakeup(&ioat->resetting);
346 ioat_drain_locked(ioat);
347 mtx_unlock(&ioat->submit_lock);
348 mtx_lock(&ioat->cleanup_lock);
349 while (ioat_get_active(ioat) > 0)
350 msleep(&ioat->tail, &ioat->cleanup_lock, 0, "ioat_drain", 1);
351 mtx_unlock(&ioat->cleanup_lock);
353 ioat_teardown_intr(ioat);
354 callout_drain(&ioat->poll_timer);
356 pci_disable_busmaster(device);
358 if (ioat->pci_resource != NULL)
359 bus_release_resource(device, SYS_RES_MEMORY,
360 ioat->pci_resource_id, ioat->pci_resource);
362 if (ioat->ring != NULL)
363 ioat_free_ring(ioat, 1 << ioat->ring_size_order, ioat->ring);
365 if (ioat->comp_update != NULL) {
366 bus_dmamap_unload(ioat->comp_update_tag, ioat->comp_update_map);
367 bus_dmamem_free(ioat->comp_update_tag, ioat->comp_update,
368 ioat->comp_update_map);
369 bus_dma_tag_destroy(ioat->comp_update_tag);
372 if (ioat->hw_desc_ring != NULL) {
373 bus_dmamap_unload(ioat->hw_desc_tag, ioat->hw_desc_map);
374 bus_dmamem_free(ioat->hw_desc_tag, ioat->hw_desc_ring,
376 bus_dma_tag_destroy(ioat->hw_desc_tag);
383 ioat_teardown_intr(struct ioat_softc *ioat)
386 if (ioat->tag != NULL)
387 bus_teardown_intr(ioat->device, ioat->res, ioat->tag);
389 if (ioat->res != NULL)
390 bus_release_resource(ioat->device, SYS_RES_IRQ,
391 rman_get_rid(ioat->res), ioat->res);
393 pci_release_msi(ioat->device);
398 ioat_start_channel(struct ioat_softc *ioat)
400 struct ioat_dma_hw_descriptor *hw_desc;
401 struct ioat_descriptor *desc;
402 struct bus_dmadesc *dmadesc;
407 ioat_acquire(&ioat->dmaengine);
409 /* Submit 'NULL' operation manually to avoid quiescing flag */
410 desc = ioat_get_ring_entry(ioat, ioat->head);
411 hw_desc = &ioat_get_descriptor(ioat, ioat->head)->dma;
412 dmadesc = &desc->bus_dmadesc;
414 dmadesc->callback_fn = NULL;
415 dmadesc->callback_arg = NULL;
417 hw_desc->u.control_raw = 0;
418 hw_desc->u.control_generic.op = IOAT_OP_COPY;
419 hw_desc->u.control_generic.completion_update = 1;
421 hw_desc->src_addr = 0;
422 hw_desc->dest_addr = 0;
423 hw_desc->u.control.null = 1;
425 ioat_submit_single(ioat);
426 ioat_release(&ioat->dmaengine);
428 for (i = 0; i < 100; i++) {
430 status = ioat_get_chansts(ioat);
431 if (is_ioat_idle(status))
435 chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
436 ioat_log_message(0, "could not start channel: "
437 "status = %#jx error = %b\n", (uintmax_t)status, (int)chanerr,
443 * Initialize Hardware
446 ioat3_attach(device_t device)
448 struct ioat_softc *ioat;
449 struct ioat_descriptor *ring;
450 struct ioat_dma_hw_descriptor *dma_hw_desc;
453 int i, num_descriptors;
458 ioat = DEVICE2SOFTC(device);
459 ioat->capabilities = ioat_read_dmacapability(ioat);
461 ioat_log_message(0, "Capabilities: %b\n", (int)ioat->capabilities,
464 xfercap = ioat_read_xfercap(ioat);
465 ioat->max_xfer_size = 1 << xfercap;
467 ioat->intrdelay_supported = (ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) &
468 IOAT_INTRDELAY_SUPPORTED) != 0;
469 if (ioat->intrdelay_supported)
470 ioat->intrdelay_max = IOAT_INTRDELAY_US_MASK;
472 /* TODO: need to check DCA here if we ever do XOR/PQ */
474 mtx_init(&ioat->submit_lock, "ioat_submit", NULL, MTX_DEF);
475 mtx_init(&ioat->cleanup_lock, "ioat_cleanup", NULL, MTX_DEF);
476 callout_init(&ioat->poll_timer, 1);
477 TASK_INIT(&ioat->reset_task, 0, ioat_reset_hw_task, ioat);
479 /* Establish lock order for Witness */
480 mtx_lock(&ioat->cleanup_lock);
481 mtx_lock(&ioat->submit_lock);
482 mtx_unlock(&ioat->submit_lock);
483 mtx_unlock(&ioat->cleanup_lock);
485 ioat->is_submitter_processing = FALSE;
487 bus_dma_tag_create(bus_get_dma_tag(ioat->device), sizeof(uint64_t), 0x0,
488 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
489 sizeof(uint64_t), 1, sizeof(uint64_t), 0, NULL, NULL,
490 &ioat->comp_update_tag);
492 error = bus_dmamem_alloc(ioat->comp_update_tag,
493 (void **)&ioat->comp_update, BUS_DMA_ZERO, &ioat->comp_update_map);
494 if (ioat->comp_update == NULL)
497 error = bus_dmamap_load(ioat->comp_update_tag, ioat->comp_update_map,
498 ioat->comp_update, sizeof(uint64_t), ioat_comp_update_map, ioat,
503 ioat->ring_size_order = g_ioat_ring_order;
504 num_descriptors = 1 << ioat->ring_size_order;
505 ringsz = sizeof(struct ioat_dma_hw_descriptor) * num_descriptors;
507 error = bus_dma_tag_create(bus_get_dma_tag(ioat->device),
508 2 * 1024 * 1024, 0x0, (bus_addr_t)BUS_SPACE_MAXADDR_40BIT,
509 BUS_SPACE_MAXADDR, NULL, NULL, ringsz, 1, ringsz, 0, NULL, NULL,
514 error = bus_dmamem_alloc(ioat->hw_desc_tag, &hw_desc,
515 BUS_DMA_ZERO | BUS_DMA_WAITOK, &ioat->hw_desc_map);
519 error = bus_dmamap_load(ioat->hw_desc_tag, ioat->hw_desc_map, hw_desc,
520 ringsz, ioat_dmamap_cb, &ioat->hw_desc_bus_addr, BUS_DMA_WAITOK);
524 ioat->hw_desc_ring = hw_desc;
526 ioat->ring = malloc(num_descriptors * sizeof(*ring), M_IOAT,
530 for (i = 0; i < num_descriptors; i++) {
531 memset(&ring[i].bus_dmadesc, 0, sizeof(ring[i].bus_dmadesc));
535 for (i = 0; i < num_descriptors; i++) {
536 dma_hw_desc = &ioat->hw_desc_ring[i].dma;
537 dma_hw_desc->next = RING_PHYS_ADDR(ioat, i + 1);
543 *ioat->comp_update = 0;
548 ioat_map_pci_bar(struct ioat_softc *ioat)
551 ioat->pci_resource_id = PCIR_BAR(0);
552 ioat->pci_resource = bus_alloc_resource_any(ioat->device,
553 SYS_RES_MEMORY, &ioat->pci_resource_id, RF_ACTIVE);
555 if (ioat->pci_resource == NULL) {
556 ioat_log_message(0, "unable to allocate pci resource\n");
560 ioat->pci_bus_tag = rman_get_bustag(ioat->pci_resource);
561 ioat->pci_bus_handle = rman_get_bushandle(ioat->pci_resource);
566 ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg, int error)
568 struct ioat_softc *ioat = arg;
570 KASSERT(error == 0, ("%s: error:%d", __func__, error));
571 ioat->comp_update_bus_addr = seg[0].ds_addr;
575 ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
579 KASSERT(error == 0, ("%s: error:%d", __func__, error));
581 *baddr = segs->ds_addr;
585 * Interrupt setup and handlers
588 ioat_setup_intr(struct ioat_softc *ioat)
590 uint32_t num_vectors;
593 boolean_t force_legacy_interrupts;
596 force_legacy_interrupts = FALSE;
598 if (!g_force_legacy_interrupts && pci_msix_count(ioat->device) >= 1) {
600 pci_alloc_msix(ioat->device, &num_vectors);
601 if (num_vectors == 1)
607 ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
608 &ioat->rid, RF_ACTIVE);
611 ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
612 &ioat->rid, RF_SHAREABLE | RF_ACTIVE);
614 if (ioat->res == NULL) {
615 ioat_log_message(0, "bus_alloc_resource failed\n");
620 error = bus_setup_intr(ioat->device, ioat->res, INTR_MPSAFE |
621 INTR_TYPE_MISC, NULL, ioat_interrupt_handler, ioat, &ioat->tag);
623 ioat_log_message(0, "bus_setup_intr failed\n");
627 ioat_write_intrctrl(ioat, IOAT_INTRCTRL_MASTER_INT_EN);
632 ioat_model_resets_msix(struct ioat_softc *ioat)
636 pciid = pci_get_devid(ioat->device);
655 ioat_interrupt_handler(void *arg)
657 struct ioat_softc *ioat = arg;
659 ioat->stats.interrupts++;
660 ioat_process_events(ioat, TRUE);
664 chanerr_to_errno(uint32_t chanerr)
669 if ((chanerr & (IOAT_CHANERR_XSADDERR | IOAT_CHANERR_XDADDERR)) != 0)
671 if ((chanerr & (IOAT_CHANERR_RDERR | IOAT_CHANERR_WDERR)) != 0)
673 /* This one is probably our fault: */
674 if ((chanerr & IOAT_CHANERR_NDADDERR) != 0)
680 ioat_process_events(struct ioat_softc *ioat, boolean_t intr)
682 struct ioat_descriptor *desc;
683 struct bus_dmadesc *dmadesc;
684 uint64_t comp_update, status;
685 uint32_t completed, chanerr;
688 mtx_lock(&ioat->cleanup_lock);
691 * Don't run while the hardware is being reset. Reset is responsible
692 * for blocking new work and draining & completing existing work, so
693 * there is nothing to do until new work is queued after reset anyway.
695 if (ioat->resetting_cleanup) {
696 mtx_unlock(&ioat->cleanup_lock);
701 comp_update = *ioat->comp_update;
702 status = comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK;
704 if (status < ioat->hw_desc_bus_addr ||
705 status >= ioat->hw_desc_bus_addr + (1 << ioat->ring_size_order) *
706 sizeof(struct ioat_generic_hw_descriptor))
707 panic("Bogus completion address %jx (channel %u)",
708 (uintmax_t)status, ioat->chan_idx);
710 if (status == ioat->last_seen) {
712 * If we landed in process_events and nothing has been
713 * completed, check for a timeout due to channel halt.
717 CTR4(KTR_IOAT, "%s channel=%u hw_status=0x%lx last_seen=0x%lx",
718 __func__, ioat->chan_idx, comp_update, ioat->last_seen);
720 while (RING_PHYS_ADDR(ioat, ioat->tail - 1) != status) {
721 desc = ioat_get_ring_entry(ioat, ioat->tail);
722 dmadesc = &desc->bus_dmadesc;
723 CTR5(KTR_IOAT, "channel=%u completing desc idx %u (%p) ok cb %p(%p)",
724 ioat->chan_idx, ioat->tail, dmadesc, dmadesc->callback_fn,
725 dmadesc->callback_arg);
727 if (dmadesc->callback_fn != NULL)
728 dmadesc->callback_fn(dmadesc->callback_arg, 0);
733 CTR5(KTR_IOAT, "%s channel=%u head=%u tail=%u active=%u", __func__,
734 ioat->chan_idx, ioat->head, ioat->tail, ioat_get_active(ioat));
736 if (completed != 0) {
737 ioat->last_seen = RING_PHYS_ADDR(ioat, ioat->tail - 1);
738 ioat->stats.descriptors_processed += completed;
743 ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
744 mtx_unlock(&ioat->cleanup_lock);
747 * The device doesn't seem to reliably push suspend/halt statuses to
748 * the channel completion memory address, so poll the device register
749 * here. For performance reasons skip it on interrupts, do it only
750 * on much more rare polling events.
753 comp_update = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS;
754 if (!is_ioat_halted(comp_update) && !is_ioat_suspended(comp_update))
757 ioat->stats.channel_halts++;
760 * Fatal programming error on this DMA channel. Flush any outstanding
761 * work with error status and restart the engine.
763 mtx_lock(&ioat->submit_lock);
764 ioat->quiescing = TRUE;
765 mtx_unlock(&ioat->submit_lock);
768 * This is safe to do here because the submit queue is quiesced. We
769 * know that we will drain all outstanding events, so ioat_reset_hw
770 * can't deadlock. It is necessary to protect other ioat_process_event
771 * threads from racing ioat_reset_hw, reading an indeterminate hw
772 * state, and attempting to continue issuing completions.
774 mtx_lock(&ioat->cleanup_lock);
775 ioat->resetting_cleanup = TRUE;
777 chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
778 if (1 <= g_ioat_debug_level)
779 ioat_halted_debug(ioat, chanerr);
780 ioat->stats.last_halt_chanerr = chanerr;
782 while (ioat_get_active(ioat) > 0) {
783 desc = ioat_get_ring_entry(ioat, ioat->tail);
784 dmadesc = &desc->bus_dmadesc;
785 CTR5(KTR_IOAT, "channel=%u completing desc idx %u (%p) err cb %p(%p)",
786 ioat->chan_idx, ioat->tail, dmadesc, dmadesc->callback_fn,
787 dmadesc->callback_arg);
789 if (dmadesc->callback_fn != NULL)
790 dmadesc->callback_fn(dmadesc->callback_arg,
791 chanerr_to_errno(chanerr));
794 ioat->stats.descriptors_processed++;
795 ioat->stats.descriptors_error++;
797 CTR5(KTR_IOAT, "%s channel=%u head=%u tail=%u active=%u", __func__,
798 ioat->chan_idx, ioat->head, ioat->tail, ioat_get_active(ioat));
800 /* Clear error status */
801 ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
803 mtx_unlock(&ioat->cleanup_lock);
805 ioat_log_message(0, "Resetting channel to recover from error\n");
806 error = taskqueue_enqueue(taskqueue_thread, &ioat->reset_task);
808 ("%s: taskqueue_enqueue failed: %d", __func__, error));
812 ioat_reset_hw_task(void *ctx, int pending __unused)
814 struct ioat_softc *ioat;
818 ioat_log_message(1, "%s: Resetting channel\n", __func__);
820 error = ioat_reset_hw(ioat);
821 KASSERT(error == 0, ("%s: reset failed: %d", __func__, error));
829 ioat_get_nchannels(void)
832 return (ioat_channel_index);
836 ioat_get_dmaengine(uint32_t index, int flags)
838 struct ioat_softc *ioat;
840 KASSERT((flags & ~(M_NOWAIT | M_WAITOK)) == 0,
841 ("invalid flags: 0x%08x", flags));
842 KASSERT((flags & (M_NOWAIT | M_WAITOK)) != (M_NOWAIT | M_WAITOK),
843 ("invalid wait | nowait"));
845 mtx_lock(&ioat_list_mtx);
846 if (index >= ioat_channel_index ||
847 (ioat = ioat_channel[index]) == NULL) {
848 mtx_unlock(&ioat_list_mtx);
851 mtx_lock(&ioat->submit_lock);
852 mtx_unlock(&ioat_list_mtx);
854 if (ioat->destroying) {
855 mtx_unlock(&ioat->submit_lock);
860 if (ioat->quiescing) {
861 if ((flags & M_NOWAIT) != 0) {
863 mtx_unlock(&ioat->submit_lock);
867 while (ioat->quiescing && !ioat->destroying)
868 msleep(&ioat->quiescing, &ioat->submit_lock, 0, "getdma", 0);
870 if (ioat->destroying) {
872 mtx_unlock(&ioat->submit_lock);
876 mtx_unlock(&ioat->submit_lock);
877 return (&ioat->dmaengine);
881 ioat_put_dmaengine(bus_dmaengine_t dmaengine)
883 struct ioat_softc *ioat;
885 ioat = to_ioat_softc(dmaengine);
886 mtx_lock(&ioat->submit_lock);
888 mtx_unlock(&ioat->submit_lock);
892 ioat_get_hwversion(bus_dmaengine_t dmaengine)
894 struct ioat_softc *ioat;
896 ioat = to_ioat_softc(dmaengine);
897 return (ioat->version);
901 ioat_get_max_io_size(bus_dmaengine_t dmaengine)
903 struct ioat_softc *ioat;
905 ioat = to_ioat_softc(dmaengine);
906 return (ioat->max_xfer_size);
910 ioat_get_capabilities(bus_dmaengine_t dmaengine)
912 struct ioat_softc *ioat;
914 ioat = to_ioat_softc(dmaengine);
915 return (ioat->capabilities);
919 ioat_set_interrupt_coalesce(bus_dmaengine_t dmaengine, uint16_t delay)
921 struct ioat_softc *ioat;
923 ioat = to_ioat_softc(dmaengine);
924 if (!ioat->intrdelay_supported)
926 if (delay > ioat->intrdelay_max)
929 ioat_write_2(ioat, IOAT_INTRDELAY_OFFSET, delay);
930 ioat->cached_intrdelay =
931 ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) & IOAT_INTRDELAY_US_MASK;
936 ioat_get_max_coalesce_period(bus_dmaengine_t dmaengine)
938 struct ioat_softc *ioat;
940 ioat = to_ioat_softc(dmaengine);
941 return (ioat->intrdelay_max);
945 ioat_acquire(bus_dmaengine_t dmaengine)
947 struct ioat_softc *ioat;
949 ioat = to_ioat_softc(dmaengine);
950 mtx_lock(&ioat->submit_lock);
951 CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
952 ioat->acq_head = ioat->head;
956 ioat_acquire_reserve(bus_dmaengine_t dmaengine, unsigned n, int mflags)
958 struct ioat_softc *ioat;
961 ioat = to_ioat_softc(dmaengine);
962 ioat_acquire(dmaengine);
964 error = ioat_reserve_space(ioat, n, mflags);
966 ioat_release(dmaengine);
971 ioat_release(bus_dmaengine_t dmaengine)
973 struct ioat_softc *ioat;
975 ioat = to_ioat_softc(dmaengine);
976 CTR3(KTR_IOAT, "%s channel=%u dispatch1 head=%u", __func__,
977 ioat->chan_idx, ioat->head);
978 KFAIL_POINT_CODE(DEBUG_FP, ioat_release, /* do nothing */);
979 CTR3(KTR_IOAT, "%s channel=%u dispatch2 head=%u", __func__,
980 ioat->chan_idx, ioat->head);
982 if (ioat->acq_head != ioat->head) {
983 ioat_write_2(ioat, IOAT_DMACOUNT_OFFSET,
984 (uint16_t)ioat->head);
986 if (!callout_pending(&ioat->poll_timer)) {
987 callout_reset(&ioat->poll_timer, 1,
988 ioat_poll_timer_callback, ioat);
991 mtx_unlock(&ioat->submit_lock);
994 static struct ioat_descriptor *
995 ioat_op_generic(struct ioat_softc *ioat, uint8_t op,
996 uint32_t size, uint64_t src, uint64_t dst,
997 bus_dmaengine_callback_t callback_fn, void *callback_arg,
1000 struct ioat_generic_hw_descriptor *hw_desc;
1001 struct ioat_descriptor *desc;
1004 mtx_assert(&ioat->submit_lock, MA_OWNED);
1006 KASSERT((flags & ~_DMA_GENERIC_FLAGS) == 0,
1007 ("Unrecognized flag(s): %#x", flags & ~_DMA_GENERIC_FLAGS));
1008 if ((flags & DMA_NO_WAIT) != 0)
1013 if (size > ioat->max_xfer_size) {
1014 ioat_log_message(0, "%s: max_xfer_size = %d, requested = %u\n",
1015 __func__, ioat->max_xfer_size, (unsigned)size);
1019 if (ioat_reserve_space(ioat, 1, mflags) != 0)
1022 desc = ioat_get_ring_entry(ioat, ioat->head);
1023 hw_desc = &ioat_get_descriptor(ioat, ioat->head)->generic;
1025 hw_desc->u.control_raw = 0;
1026 hw_desc->u.control_generic.op = op;
1027 hw_desc->u.control_generic.completion_update = 1;
1029 if ((flags & DMA_INT_EN) != 0)
1030 hw_desc->u.control_generic.int_enable = 1;
1031 if ((flags & DMA_FENCE) != 0)
1032 hw_desc->u.control_generic.fence = 1;
1034 hw_desc->size = size;
1035 hw_desc->src_addr = src;
1036 hw_desc->dest_addr = dst;
1038 desc->bus_dmadesc.callback_fn = callback_fn;
1039 desc->bus_dmadesc.callback_arg = callback_arg;
1043 struct bus_dmadesc *
1044 ioat_null(bus_dmaengine_t dmaengine, bus_dmaengine_callback_t callback_fn,
1045 void *callback_arg, uint32_t flags)
1047 struct ioat_dma_hw_descriptor *hw_desc;
1048 struct ioat_descriptor *desc;
1049 struct ioat_softc *ioat;
1051 ioat = to_ioat_softc(dmaengine);
1052 CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1054 desc = ioat_op_generic(ioat, IOAT_OP_COPY, 8, 0, 0, callback_fn,
1055 callback_arg, flags);
1059 hw_desc = &ioat_get_descriptor(ioat, desc->id)->dma;
1060 hw_desc->u.control.null = 1;
1061 ioat_submit_single(ioat);
1062 return (&desc->bus_dmadesc);
1065 struct bus_dmadesc *
1066 ioat_copy(bus_dmaengine_t dmaengine, bus_addr_t dst,
1067 bus_addr_t src, bus_size_t len, bus_dmaengine_callback_t callback_fn,
1068 void *callback_arg, uint32_t flags)
1070 struct ioat_dma_hw_descriptor *hw_desc;
1071 struct ioat_descriptor *desc;
1072 struct ioat_softc *ioat;
1074 ioat = to_ioat_softc(dmaengine);
1076 if (((src | dst) & (0xffffull << 48)) != 0) {
1077 ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n",
1082 desc = ioat_op_generic(ioat, IOAT_OP_COPY, len, src, dst, callback_fn,
1083 callback_arg, flags);
1087 hw_desc = &ioat_get_descriptor(ioat, desc->id)->dma;
1088 if (g_ioat_debug_level >= 3)
1089 dump_descriptor(hw_desc);
1091 ioat_submit_single(ioat);
1092 CTR6(KTR_IOAT, "%s channel=%u desc=%p dest=%lx src=%lx len=%lx",
1093 __func__, ioat->chan_idx, &desc->bus_dmadesc, dst, src, len);
1094 return (&desc->bus_dmadesc);
1097 struct bus_dmadesc *
1098 ioat_copy_8k_aligned(bus_dmaengine_t dmaengine, bus_addr_t dst1,
1099 bus_addr_t dst2, bus_addr_t src1, bus_addr_t src2,
1100 bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
1102 struct ioat_dma_hw_descriptor *hw_desc;
1103 struct ioat_descriptor *desc;
1104 struct ioat_softc *ioat;
1106 ioat = to_ioat_softc(dmaengine);
1107 CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1109 if (((src1 | src2 | dst1 | dst2) & (0xffffull << 48)) != 0) {
1110 ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n",
1114 if (((src1 | src2 | dst1 | dst2) & PAGE_MASK) != 0) {
1115 ioat_log_message(0, "%s: Addresses must be page-aligned\n",
1120 desc = ioat_op_generic(ioat, IOAT_OP_COPY, 2 * PAGE_SIZE, src1, dst1,
1121 callback_fn, callback_arg, flags);
1125 hw_desc = &ioat_get_descriptor(ioat, desc->id)->dma;
1126 if (src2 != src1 + PAGE_SIZE) {
1127 hw_desc->u.control.src_page_break = 1;
1128 hw_desc->next_src_addr = src2;
1130 if (dst2 != dst1 + PAGE_SIZE) {
1131 hw_desc->u.control.dest_page_break = 1;
1132 hw_desc->next_dest_addr = dst2;
1135 if (g_ioat_debug_level >= 3)
1136 dump_descriptor(hw_desc);
1138 ioat_submit_single(ioat);
1139 return (&desc->bus_dmadesc);
1142 struct bus_dmadesc *
1143 ioat_copy_crc(bus_dmaengine_t dmaengine, bus_addr_t dst, bus_addr_t src,
1144 bus_size_t len, uint32_t *initialseed, bus_addr_t crcptr,
1145 bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
1147 struct ioat_crc32_hw_descriptor *hw_desc;
1148 struct ioat_descriptor *desc;
1149 struct ioat_softc *ioat;
1153 ioat = to_ioat_softc(dmaengine);
1154 CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1156 if ((ioat->capabilities & IOAT_DMACAP_MOVECRC) == 0) {
1157 ioat_log_message(0, "%s: Device lacks MOVECRC capability\n",
1161 if (((src | dst) & (0xffffffull << 40)) != 0) {
1162 ioat_log_message(0, "%s: High 24 bits of src/dst invalid\n",
1166 teststore = (flags & _DMA_CRC_TESTSTORE);
1167 if (teststore == _DMA_CRC_TESTSTORE) {
1168 ioat_log_message(0, "%s: TEST and STORE invalid\n", __func__);
1171 if (teststore == 0 && (flags & DMA_CRC_INLINE) != 0) {
1172 ioat_log_message(0, "%s: INLINE invalid without TEST or STORE\n",
1177 switch (teststore) {
1179 op = IOAT_OP_MOVECRC_STORE;
1182 op = IOAT_OP_MOVECRC_TEST;
1185 KASSERT(teststore == 0, ("bogus"));
1186 op = IOAT_OP_MOVECRC;
1190 if ((flags & DMA_CRC_INLINE) == 0 &&
1191 (crcptr & (0xffffffull << 40)) != 0) {
1193 "%s: High 24 bits of crcptr invalid\n", __func__);
1197 desc = ioat_op_generic(ioat, op, len, src, dst, callback_fn,
1198 callback_arg, flags & ~_DMA_CRC_FLAGS);
1202 hw_desc = &ioat_get_descriptor(ioat, desc->id)->crc32;
1204 if ((flags & DMA_CRC_INLINE) == 0)
1205 hw_desc->crc_address = crcptr;
1207 hw_desc->u.control.crc_location = 1;
1209 if (initialseed != NULL) {
1210 hw_desc->u.control.use_seed = 1;
1211 hw_desc->seed = *initialseed;
1214 if (g_ioat_debug_level >= 3)
1215 dump_descriptor(hw_desc);
1217 ioat_submit_single(ioat);
1218 return (&desc->bus_dmadesc);
1221 struct bus_dmadesc *
1222 ioat_crc(bus_dmaengine_t dmaengine, bus_addr_t src, bus_size_t len,
1223 uint32_t *initialseed, bus_addr_t crcptr,
1224 bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
1226 struct ioat_crc32_hw_descriptor *hw_desc;
1227 struct ioat_descriptor *desc;
1228 struct ioat_softc *ioat;
1232 ioat = to_ioat_softc(dmaengine);
1233 CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1235 if ((ioat->capabilities & IOAT_DMACAP_CRC) == 0) {
1236 ioat_log_message(0, "%s: Device lacks CRC capability\n",
1240 if ((src & (0xffffffull << 40)) != 0) {
1241 ioat_log_message(0, "%s: High 24 bits of src invalid\n",
1245 teststore = (flags & _DMA_CRC_TESTSTORE);
1246 if (teststore == _DMA_CRC_TESTSTORE) {
1247 ioat_log_message(0, "%s: TEST and STORE invalid\n", __func__);
1250 if (teststore == 0 && (flags & DMA_CRC_INLINE) != 0) {
1251 ioat_log_message(0, "%s: INLINE invalid without TEST or STORE\n",
1256 switch (teststore) {
1258 op = IOAT_OP_CRC_STORE;
1261 op = IOAT_OP_CRC_TEST;
1264 KASSERT(teststore == 0, ("bogus"));
1269 if ((flags & DMA_CRC_INLINE) == 0 &&
1270 (crcptr & (0xffffffull << 40)) != 0) {
1272 "%s: High 24 bits of crcptr invalid\n", __func__);
1276 desc = ioat_op_generic(ioat, op, len, src, 0, callback_fn,
1277 callback_arg, flags & ~_DMA_CRC_FLAGS);
1281 hw_desc = &ioat_get_descriptor(ioat, desc->id)->crc32;
1283 if ((flags & DMA_CRC_INLINE) == 0)
1284 hw_desc->crc_address = crcptr;
1286 hw_desc->u.control.crc_location = 1;
1288 if (initialseed != NULL) {
1289 hw_desc->u.control.use_seed = 1;
1290 hw_desc->seed = *initialseed;
1293 if (g_ioat_debug_level >= 3)
1294 dump_descriptor(hw_desc);
1296 ioat_submit_single(ioat);
1297 return (&desc->bus_dmadesc);
1300 struct bus_dmadesc *
1301 ioat_blockfill(bus_dmaengine_t dmaengine, bus_addr_t dst, uint64_t fillpattern,
1302 bus_size_t len, bus_dmaengine_callback_t callback_fn, void *callback_arg,
1305 struct ioat_fill_hw_descriptor *hw_desc;
1306 struct ioat_descriptor *desc;
1307 struct ioat_softc *ioat;
1309 ioat = to_ioat_softc(dmaengine);
1310 CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1312 if ((ioat->capabilities & IOAT_DMACAP_BFILL) == 0) {
1313 ioat_log_message(0, "%s: Device lacks BFILL capability\n",
1318 if ((dst & (0xffffull << 48)) != 0) {
1319 ioat_log_message(0, "%s: High 16 bits of dst invalid\n",
1324 desc = ioat_op_generic(ioat, IOAT_OP_FILL, len, fillpattern, dst,
1325 callback_fn, callback_arg, flags);
1329 hw_desc = &ioat_get_descriptor(ioat, desc->id)->fill;
1330 if (g_ioat_debug_level >= 3)
1331 dump_descriptor(hw_desc);
1333 ioat_submit_single(ioat);
1334 return (&desc->bus_dmadesc);
1340 static inline uint32_t
1341 ioat_get_active(struct ioat_softc *ioat)
1344 return ((ioat->head - ioat->tail) & ((1 << ioat->ring_size_order) - 1));
1347 static inline uint32_t
1348 ioat_get_ring_space(struct ioat_softc *ioat)
1351 return ((1 << ioat->ring_size_order) - ioat_get_active(ioat) - 1);
1355 * Reserves space in this IOAT descriptor ring by ensuring enough slots remain
1358 * If mflags contains M_WAITOK, blocks until enough space is available.
1360 * Returns zero on success, or an errno on error. If num_descs is beyond the
1361 * maximum ring size, returns EINVAl; if allocation would block and mflags
1362 * contains M_NOWAIT, returns EAGAIN.
1364 * Must be called with the submit_lock held; returns with the lock held. The
1365 * lock may be dropped to allocate the ring.
1367 * (The submit_lock is needed to add any entries to the ring, so callers are
1368 * assured enough room is available.)
1371 ioat_reserve_space(struct ioat_softc *ioat, uint32_t num_descs, int mflags)
1376 mtx_assert(&ioat->submit_lock, MA_OWNED);
1380 if (num_descs < 1 || num_descs >= (1 << ioat->ring_size_order)) {
1386 if (ioat->quiescing) {
1391 if (ioat_get_ring_space(ioat) >= num_descs)
1394 CTR3(KTR_IOAT, "%s channel=%u starved (%u)", __func__,
1395 ioat->chan_idx, num_descs);
1397 if (!dug && !ioat->is_submitter_processing) {
1398 ioat->is_submitter_processing = TRUE;
1399 mtx_unlock(&ioat->submit_lock);
1401 CTR2(KTR_IOAT, "%s channel=%u attempting to process events",
1402 __func__, ioat->chan_idx);
1403 ioat_process_events(ioat, FALSE);
1405 mtx_lock(&ioat->submit_lock);
1407 KASSERT(ioat->is_submitter_processing == TRUE,
1408 ("is_submitter_processing"));
1409 ioat->is_submitter_processing = FALSE;
1410 wakeup(&ioat->tail);
1414 if ((mflags & M_WAITOK) == 0) {
1418 CTR2(KTR_IOAT, "%s channel=%u blocking on completions",
1419 __func__, ioat->chan_idx);
1420 msleep(&ioat->tail, &ioat->submit_lock, 0,
1426 mtx_assert(&ioat->submit_lock, MA_OWNED);
1427 KASSERT(!ioat->quiescing || error == ENXIO,
1428 ("reserved during quiesce"));
1433 ioat_free_ring(struct ioat_softc *ioat, uint32_t size,
1434 struct ioat_descriptor *ring)
1440 static struct ioat_descriptor *
1441 ioat_get_ring_entry(struct ioat_softc *ioat, uint32_t index)
1444 return (&ioat->ring[index % (1 << ioat->ring_size_order)]);
1447 static union ioat_hw_descriptor *
1448 ioat_get_descriptor(struct ioat_softc *ioat, uint32_t index)
1451 return (&ioat->hw_desc_ring[index % (1 << ioat->ring_size_order)]);
1455 ioat_halted_debug(struct ioat_softc *ioat, uint32_t chanerr)
1457 union ioat_hw_descriptor *desc;
1459 ioat_log_message(0, "Channel halted (%b)\n", (int)chanerr,
1464 mtx_assert(&ioat->cleanup_lock, MA_OWNED);
1466 desc = ioat_get_descriptor(ioat, ioat->tail + 0);
1467 dump_descriptor(desc);
1469 desc = ioat_get_descriptor(ioat, ioat->tail + 1);
1470 dump_descriptor(desc);
1474 ioat_poll_timer_callback(void *arg)
1476 struct ioat_softc *ioat;
1479 ioat_log_message(3, "%s\n", __func__);
1481 ioat_process_events(ioat, FALSE);
1483 mtx_lock(&ioat->submit_lock);
1484 if (ioat_get_active(ioat) > 0)
1485 callout_schedule(&ioat->poll_timer, 1);
1486 mtx_unlock(&ioat->submit_lock);
1493 ioat_submit_single(struct ioat_softc *ioat)
1496 mtx_assert(&ioat->submit_lock, MA_OWNED);
1499 CTR4(KTR_IOAT, "%s channel=%u head=%u tail=%u", __func__,
1500 ioat->chan_idx, ioat->head, ioat->tail);
1502 ioat->stats.descriptors_submitted++;
1506 ioat_reset_hw(struct ioat_softc *ioat)
1513 CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1515 mtx_lock(&ioat->submit_lock);
1516 while (ioat->resetting && !ioat->destroying)
1517 msleep(&ioat->resetting, &ioat->submit_lock, 0, "IRH_drain", 0);
1518 if (ioat->destroying) {
1519 mtx_unlock(&ioat->submit_lock);
1522 ioat->resetting = TRUE;
1523 ioat->quiescing = TRUE;
1524 mtx_unlock(&ioat->submit_lock);
1525 mtx_lock(&ioat->cleanup_lock);
1526 while (ioat_get_active(ioat) > 0)
1527 msleep(&ioat->tail, &ioat->cleanup_lock, 0, "ioat_drain", 1);
1530 * Suspend ioat_process_events while the hardware and softc are in an
1531 * indeterminate state.
1533 ioat->resetting_cleanup = TRUE;
1534 mtx_unlock(&ioat->cleanup_lock);
1536 CTR2(KTR_IOAT, "%s channel=%u quiesced and drained", __func__,
1539 status = ioat_get_chansts(ioat);
1540 if (is_ioat_active(status) || is_ioat_idle(status))
1543 /* Wait at most 20 ms */
1544 for (timeout = 0; (is_ioat_active(status) || is_ioat_idle(status)) &&
1545 timeout < 20; timeout++) {
1547 status = ioat_get_chansts(ioat);
1549 if (timeout == 20) {
1554 KASSERT(ioat_get_active(ioat) == 0, ("active after quiesce"));
1556 chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
1557 ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
1559 CTR2(KTR_IOAT, "%s channel=%u hardware suspended", __func__,
1563 * IOAT v3 workaround - CHANERRMSK_INT with 3E07h to masks out errors
1564 * that can cause stability issues for IOAT v3.
1566 pci_write_config(ioat->device, IOAT_CFG_CHANERRMASK_INT_OFFSET, 0x3e07,
1568 chanerr = pci_read_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, 4);
1569 pci_write_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, chanerr, 4);
1572 * BDXDE and BWD models reset MSI-X registers on device reset.
1573 * Save/restore their contents manually.
1575 if (ioat_model_resets_msix(ioat)) {
1576 ioat_log_message(1, "device resets MSI-X registers; saving\n");
1577 pci_save_state(ioat->device);
1581 CTR2(KTR_IOAT, "%s channel=%u hardware reset", __func__,
1584 /* Wait at most 20 ms */
1585 for (timeout = 0; ioat_reset_pending(ioat) && timeout < 20; timeout++)
1587 if (timeout == 20) {
1592 if (ioat_model_resets_msix(ioat)) {
1593 ioat_log_message(1, "device resets registers; restored\n");
1594 pci_restore_state(ioat->device);
1597 /* Reset attempts to return the hardware to "halted." */
1598 status = ioat_get_chansts(ioat);
1599 if (is_ioat_active(status) || is_ioat_idle(status)) {
1600 /* So this really shouldn't happen... */
1601 ioat_log_message(0, "Device is active after a reset?\n");
1602 ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
1607 chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
1609 mtx_lock(&ioat->cleanup_lock);
1610 ioat_halted_debug(ioat, chanerr);
1611 mtx_unlock(&ioat->cleanup_lock);
1617 * Bring device back online after reset. Writing CHAINADDR brings the
1618 * device back to active.
1620 * The internal ring counter resets to zero, so we have to start over
1623 ioat->tail = ioat->head = 0;
1624 ioat->last_seen = 0;
1625 *ioat->comp_update = 0;
1627 ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
1628 ioat_write_chancmp(ioat, ioat->comp_update_bus_addr);
1629 ioat_write_chainaddr(ioat, RING_PHYS_ADDR(ioat, 0));
1631 CTR2(KTR_IOAT, "%s channel=%u configured channel", __func__,
1635 /* Enqueues a null operation and ensures it completes. */
1637 error = ioat_start_channel(ioat);
1638 CTR2(KTR_IOAT, "%s channel=%u started channel", __func__,
1643 * Resume completions now that ring state is consistent.
1645 mtx_lock(&ioat->cleanup_lock);
1646 ioat->resetting_cleanup = FALSE;
1647 mtx_unlock(&ioat->cleanup_lock);
1649 /* Unblock submission of new work */
1650 mtx_lock(&ioat->submit_lock);
1651 ioat->quiescing = FALSE;
1652 wakeup(&ioat->quiescing);
1654 ioat->resetting = FALSE;
1655 wakeup(&ioat->resetting);
1657 CTR2(KTR_IOAT, "%s channel=%u reset done", __func__, ioat->chan_idx);
1658 mtx_unlock(&ioat->submit_lock);
1664 sysctl_handle_chansts(SYSCTL_HANDLER_ARGS)
1666 struct ioat_softc *ioat;
1673 status = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS;
1675 sbuf_new_for_sysctl(&sb, NULL, 256, req);
1677 case IOAT_CHANSTS_ACTIVE:
1678 sbuf_printf(&sb, "ACTIVE");
1680 case IOAT_CHANSTS_IDLE:
1681 sbuf_printf(&sb, "IDLE");
1683 case IOAT_CHANSTS_SUSPENDED:
1684 sbuf_printf(&sb, "SUSPENDED");
1686 case IOAT_CHANSTS_HALTED:
1687 sbuf_printf(&sb, "HALTED");
1689 case IOAT_CHANSTS_ARMED:
1690 sbuf_printf(&sb, "ARMED");
1693 sbuf_printf(&sb, "UNKNOWN");
1696 error = sbuf_finish(&sb);
1699 if (error != 0 || req->newptr == NULL)
1705 sysctl_handle_dpi(SYSCTL_HANDLER_ARGS)
1707 struct ioat_softc *ioat;
1709 #define PRECISION "1"
1710 const uintmax_t factor = 10;
1715 sbuf_new_for_sysctl(&sb, NULL, 16, req);
1717 if (ioat->stats.interrupts == 0) {
1718 sbuf_printf(&sb, "NaN");
1721 rate = ioat->stats.descriptors_processed * factor /
1722 ioat->stats.interrupts;
1723 sbuf_printf(&sb, "%ju.%." PRECISION "ju", rate / factor,
1727 error = sbuf_finish(&sb);
1729 if (error != 0 || req->newptr == NULL)
1735 sysctl_handle_reset(SYSCTL_HANDLER_ARGS)
1737 struct ioat_softc *ioat;
1743 error = SYSCTL_OUT(req, &arg, sizeof(arg));
1744 if (error != 0 || req->newptr == NULL)
1747 error = SYSCTL_IN(req, &arg, sizeof(arg));
1752 error = ioat_reset_hw(ioat);
1758 dump_descriptor(void *hw_desc)
1762 for (i = 0; i < 2; i++) {
1763 for (j = 0; j < 8; j++)
1764 printf("%08x ", ((uint32_t *)hw_desc)[i * 8 + j]);
1770 ioat_setup_sysctl(device_t device)
1772 struct sysctl_oid_list *par, *statpar, *state, *hammer;
1773 struct sysctl_ctx_list *ctx;
1774 struct sysctl_oid *tree, *tmp;
1775 struct ioat_softc *ioat;
1777 ioat = DEVICE2SOFTC(device);
1778 ctx = device_get_sysctl_ctx(device);
1779 tree = device_get_sysctl_tree(device);
1780 par = SYSCTL_CHILDREN(tree);
1782 SYSCTL_ADD_INT(ctx, par, OID_AUTO, "version", CTLFLAG_RD,
1783 &ioat->version, 0, "HW version (0xMM form)");
1784 SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "max_xfer_size", CTLFLAG_RD,
1785 &ioat->max_xfer_size, 0, "HW maximum transfer size");
1786 SYSCTL_ADD_INT(ctx, par, OID_AUTO, "intrdelay_supported", CTLFLAG_RD,
1787 &ioat->intrdelay_supported, 0, "Is INTRDELAY supported");
1788 SYSCTL_ADD_U16(ctx, par, OID_AUTO, "intrdelay_max", CTLFLAG_RD,
1789 &ioat->intrdelay_max, 0,
1790 "Maximum configurable INTRDELAY on this channel (microseconds)");
1792 tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "state", CTLFLAG_RD, NULL,
1793 "IOAT channel internal state");
1794 state = SYSCTL_CHILDREN(tmp);
1796 SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "ring_size_order", CTLFLAG_RD,
1797 &ioat->ring_size_order, 0, "SW descriptor ring size order");
1798 SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "head", CTLFLAG_RD, &ioat->head,
1799 0, "SW descriptor head pointer index");
1800 SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "tail", CTLFLAG_RD, &ioat->tail,
1801 0, "SW descriptor tail pointer index");
1803 SYSCTL_ADD_UQUAD(ctx, state, OID_AUTO, "last_completion", CTLFLAG_RD,
1804 ioat->comp_update, "HW addr of last completion");
1806 SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_submitter_processing",
1807 CTLFLAG_RD, &ioat->is_submitter_processing, 0,
1808 "submitter processing");
1810 SYSCTL_ADD_PROC(ctx, state, OID_AUTO, "chansts",
1811 CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_chansts, "A",
1812 "String of the channel status");
1814 SYSCTL_ADD_U16(ctx, state, OID_AUTO, "intrdelay", CTLFLAG_RD,
1815 &ioat->cached_intrdelay, 0,
1816 "Current INTRDELAY on this channel (cached, microseconds)");
1818 tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "hammer", CTLFLAG_RD, NULL,
1819 "Big hammers (mostly for testing)");
1820 hammer = SYSCTL_CHILDREN(tmp);
1822 SYSCTL_ADD_PROC(ctx, hammer, OID_AUTO, "force_hw_reset",
1823 CTLTYPE_INT | CTLFLAG_RW, ioat, 0, sysctl_handle_reset, "I",
1824 "Set to non-zero to reset the hardware");
1826 tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "stats", CTLFLAG_RD, NULL,
1827 "IOAT channel statistics");
1828 statpar = SYSCTL_CHILDREN(tmp);
1830 SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "interrupts", CTLFLAG_RW,
1831 &ioat->stats.interrupts,
1832 "Number of interrupts processed on this channel");
1833 SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "descriptors", CTLFLAG_RW,
1834 &ioat->stats.descriptors_processed,
1835 "Number of descriptors processed on this channel");
1836 SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "submitted", CTLFLAG_RW,
1837 &ioat->stats.descriptors_submitted,
1838 "Number of descriptors submitted to this channel");
1839 SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "errored", CTLFLAG_RW,
1840 &ioat->stats.descriptors_error,
1841 "Number of descriptors failed by channel errors");
1842 SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "halts", CTLFLAG_RW,
1843 &ioat->stats.channel_halts, 0,
1844 "Number of times the channel has halted");
1845 SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "last_halt_chanerr", CTLFLAG_RW,
1846 &ioat->stats.last_halt_chanerr, 0,
1847 "The raw CHANERR when the channel was last halted");
1849 SYSCTL_ADD_PROC(ctx, statpar, OID_AUTO, "desc_per_interrupt",
1850 CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_dpi, "A",
1851 "Descriptors per interrupt");
1855 ioat_get(struct ioat_softc *ioat)
1858 mtx_assert(&ioat->submit_lock, MA_OWNED);
1859 KASSERT(ioat->refcnt < UINT32_MAX, ("refcnt overflow"));
1865 ioat_put(struct ioat_softc *ioat)
1868 mtx_assert(&ioat->submit_lock, MA_OWNED);
1869 KASSERT(ioat->refcnt >= 1, ("refcnt error"));
1871 if (--ioat->refcnt == 0)
1872 wakeup(&ioat->refcnt);
1876 ioat_drain_locked(struct ioat_softc *ioat)
1879 mtx_assert(&ioat->submit_lock, MA_OWNED);
1881 while (ioat->refcnt > 0)
1882 msleep(&ioat->refcnt, &ioat->submit_lock, 0, "ioat_drain", 0);
1886 #define _db_show_lock(lo) LOCK_CLASS(lo)->lc_ddb_show(lo)
1887 #define db_show_lock(lk) _db_show_lock(&(lk)->lock_object)
1888 DB_SHOW_COMMAND(ioat, db_show_ioat)
1890 struct ioat_softc *sc;
1895 idx = (unsigned)addr;
1896 if (idx >= ioat_channel_index)
1899 sc = ioat_channel[idx];
1900 db_printf("ioat softc at %p\n", sc);
1904 db_printf(" version: %d\n", sc->version);
1905 db_printf(" chan_idx: %u\n", sc->chan_idx);
1906 db_printf(" submit_lock: ");
1907 db_show_lock(&sc->submit_lock);
1909 db_printf(" capabilities: %b\n", (int)sc->capabilities,
1911 db_printf(" cached_intrdelay: %u\n", sc->cached_intrdelay);
1912 db_printf(" *comp_update: 0x%jx\n", (uintmax_t)*sc->comp_update);
1914 db_printf(" poll_timer:\n");
1915 db_printf(" c_time: %ju\n", (uintmax_t)sc->poll_timer.c_time);
1916 db_printf(" c_arg: %p\n", sc->poll_timer.c_arg);
1917 db_printf(" c_func: %p\n", sc->poll_timer.c_func);
1918 db_printf(" c_lock: %p\n", sc->poll_timer.c_lock);
1919 db_printf(" c_flags: 0x%x\n", (unsigned)sc->poll_timer.c_flags);
1921 db_printf(" quiescing: %d\n", (int)sc->quiescing);
1922 db_printf(" destroying: %d\n", (int)sc->destroying);
1923 db_printf(" is_submitter_processing: %d\n",
1924 (int)sc->is_submitter_processing);
1925 db_printf(" intrdelay_supported: %d\n", (int)sc->intrdelay_supported);
1926 db_printf(" resetting: %d\n", (int)sc->resetting);
1928 db_printf(" head: %u\n", sc->head);
1929 db_printf(" tail: %u\n", sc->tail);
1930 db_printf(" ring_size_order: %u\n", sc->ring_size_order);
1931 db_printf(" last_seen: 0x%lx\n", sc->last_seen);
1932 db_printf(" ring: %p\n", sc->ring);
1933 db_printf(" descriptors: %p\n", sc->hw_desc_ring);
1934 db_printf(" descriptors (phys): 0x%jx\n",
1935 (uintmax_t)sc->hw_desc_bus_addr);
1937 db_printf(" ring[%u] (tail):\n", sc->tail %
1938 (1 << sc->ring_size_order));
1939 db_printf(" id: %u\n", ioat_get_ring_entry(sc, sc->tail)->id);
1940 db_printf(" addr: 0x%lx\n",
1941 RING_PHYS_ADDR(sc, sc->tail));
1942 db_printf(" next: 0x%lx\n",
1943 ioat_get_descriptor(sc, sc->tail)->generic.next);
1945 db_printf(" ring[%u] (head - 1):\n", (sc->head - 1) %
1946 (1 << sc->ring_size_order));
1947 db_printf(" id: %u\n", ioat_get_ring_entry(sc, sc->head - 1)->id);
1948 db_printf(" addr: 0x%lx\n",
1949 RING_PHYS_ADDR(sc, sc->head - 1));
1950 db_printf(" next: 0x%lx\n",
1951 ioat_get_descriptor(sc, sc->head - 1)->generic.next);
1953 db_printf(" ring[%u] (head):\n", (sc->head) %
1954 (1 << sc->ring_size_order));
1955 db_printf(" id: %u\n", ioat_get_ring_entry(sc, sc->head)->id);
1956 db_printf(" addr: 0x%lx\n",
1957 RING_PHYS_ADDR(sc, sc->head));
1958 db_printf(" next: 0x%lx\n",
1959 ioat_get_descriptor(sc, sc->head)->generic.next);
1961 for (idx = 0; idx < (1 << sc->ring_size_order); idx++)
1962 if ((*sc->comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK)
1963 == RING_PHYS_ADDR(sc, idx))
1964 db_printf(" ring[%u] == hardware tail\n", idx);
1966 db_printf(" cleanup_lock: ");
1967 db_show_lock(&sc->cleanup_lock);
1969 db_printf(" refcnt: %u\n", sc->refcnt);
1970 db_printf(" stats:\n");
1971 db_printf(" interrupts: %lu\n", sc->stats.interrupts);
1972 db_printf(" descriptors_processed: %lu\n", sc->stats.descriptors_processed);
1973 db_printf(" descriptors_error: %lu\n", sc->stats.descriptors_error);
1974 db_printf(" descriptors_submitted: %lu\n", sc->stats.descriptors_submitted);
1976 db_printf(" channel_halts: %u\n", sc->stats.channel_halts);
1977 db_printf(" last_halt_chanerr: %u\n", sc->stats.last_halt_chanerr);
1982 db_printf(" hw status:\n");
1983 db_printf(" status: 0x%lx\n", ioat_get_chansts(sc));
1984 db_printf(" chanctrl: 0x%x\n",
1985 (unsigned)ioat_read_2(sc, IOAT_CHANCTRL_OFFSET));
1986 db_printf(" chancmd: 0x%x\n",
1987 (unsigned)ioat_read_1(sc, IOAT_CHANCMD_OFFSET));
1988 db_printf(" dmacount: 0x%x\n",
1989 (unsigned)ioat_read_2(sc, IOAT_DMACOUNT_OFFSET));
1990 db_printf(" chainaddr: 0x%lx\n",
1991 ioat_read_double_4(sc, IOAT_CHAINADDR_OFFSET_LOW));
1992 db_printf(" chancmp: 0x%lx\n",
1993 ioat_read_double_4(sc, IOAT_CHANCMP_OFFSET_LOW));
1994 db_printf(" chanerr: %b\n",
1995 (int)ioat_read_4(sc, IOAT_CHANERR_OFFSET), IOAT_CHANERR_STR);
1998 db_printf("usage: show ioat <0-%u>\n", ioat_channel_index);