]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ioat/ioat.c
MFC r347990:
[FreeBSD/FreeBSD.git] / sys / dev / ioat / ioat.c
1 /*-
2  * Copyright (C) 2012 Intel Corporation
3  * All rights reserved.
4  * Copyright (C) 2018 Alexander Motin <mav@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include "opt_ddb.h"
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/conf.h>
37 #include <sys/fail.h>
38 #include <sys/ioccom.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/rman.h>
45 #include <sys/sbuf.h>
46 #include <sys/sysctl.h>
47 #include <sys/taskqueue.h>
48 #include <sys/time.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>
54
55 #ifdef DDB
56 #include <ddb/ddb.h>
57 #endif
58
59 #include "ioat.h"
60 #include "ioat_hw.h"
61 #include "ioat_internal.h"
62
63 #ifndef BUS_SPACE_MAXADDR_40BIT
64 #define BUS_SPACE_MAXADDR_40BIT 0xFFFFFFFFFFULL
65 #endif
66
67 static int ioat_probe(device_t device);
68 static int ioat_attach(device_t device);
69 static int ioat_detach(device_t device);
70 static int ioat_setup_intr(struct ioat_softc *ioat);
71 static int ioat_teardown_intr(struct ioat_softc *ioat);
72 static int ioat3_attach(device_t device);
73 static int ioat_start_channel(struct ioat_softc *ioat);
74 static int ioat_map_pci_bar(struct ioat_softc *ioat);
75 static void ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg,
76     int error);
77 static void ioat_interrupt_handler(void *arg);
78 static boolean_t ioat_model_resets_msix(struct ioat_softc *ioat);
79 static int chanerr_to_errno(uint32_t);
80 static void ioat_process_events(struct ioat_softc *ioat, 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 *,
87     uint32_t index);
88 static struct ioat_descriptor *ioat_get_ring_entry(struct ioat_softc *,
89     uint32_t index);
90 static void ioat_halted_debug(struct ioat_softc *, uint32_t);
91 static void ioat_poll_timer_callback(void *arg);
92 static void dump_descriptor(void *hw_desc);
93 static void ioat_submit_single(struct ioat_softc *ioat);
94 static void ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg,
95     int error);
96 static int ioat_reset_hw(struct ioat_softc *ioat);
97 static void ioat_reset_hw_task(void *, int);
98 static void ioat_setup_sysctl(device_t device);
99 static int sysctl_handle_reset(SYSCTL_HANDLER_ARGS);
100 static void ioat_get(struct ioat_softc *);
101 static void ioat_put(struct ioat_softc *);
102 static void ioat_drain_locked(struct ioat_softc *);
103
104 #define ioat_log_message(v, ...) do {                                   \
105         if ((v) <= g_ioat_debug_level) {                                \
106                 device_printf(ioat->device, __VA_ARGS__);               \
107         }                                                               \
108 } while (0)
109
110 MALLOC_DEFINE(M_IOAT, "ioat", "ioat driver memory allocations");
111 SYSCTL_NODE(_hw, OID_AUTO, ioat, CTLFLAG_RD, 0, "ioat node");
112
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");
116
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.");
120
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.");
124
125 /*
126  * OS <-> Driver interface structures
127  */
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),
133         DEVMETHOD_END
134 };
135
136 static driver_t ioat_pci_driver = {
137         "ioat",
138         ioat_pci_methods,
139         sizeof(struct ioat_softc),
140 };
141
142 static devclass_t ioat_devclass;
143 DRIVER_MODULE(ioat, pci, ioat_pci_driver, ioat_devclass, 0, 0);
144 MODULE_VERSION(ioat, 1);
145
146 /*
147  * Private data structures
148  */
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);
155
156 static struct _pcsid
157 {
158         u_int32_t   type;
159         const char  *desc;
160 } pci_ids[] = {
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" },
169
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)" },
180
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)" },
191
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)" },
202
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)" },
213
214         { 0x0c508086, "BWD IOAT Ch0" },
215         { 0x0c518086, "BWD IOAT Ch1" },
216         { 0x0c528086, "BWD IOAT Ch2" },
217         { 0x0c538086, "BWD IOAT Ch3" },
218
219         { 0x6f508086, "BDXDE IOAT Ch0" },
220         { 0x6f518086, "BDXDE IOAT Ch1" },
221         { 0x6f528086, "BDXDE IOAT Ch2" },
222         { 0x6f538086, "BDXDE IOAT Ch3" },
223
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" },
235         { 0x00000000, NULL }
236 };
237
238 /*
239  * OS <-> Driver linkage functions
240  */
241 static int
242 ioat_probe(device_t device)
243 {
244         struct _pcsid *ep;
245         u_int32_t type;
246
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);
251                         return (0);
252                 }
253         }
254         return (ENXIO);
255 }
256
257 static int
258 ioat_attach(device_t device)
259 {
260         struct ioat_softc *ioat;
261         int error, i;
262
263         ioat = DEVICE2SOFTC(device);
264         ioat->device = device;
265
266         error = ioat_map_pci_bar(ioat);
267         if (error != 0)
268                 goto err;
269
270         ioat->version = ioat_read_cbver(ioat);
271         if (ioat->version < IOAT_VER_3_0) {
272                 error = ENODEV;
273                 goto err;
274         }
275
276         error = ioat3_attach(device);
277         if (error != 0)
278                 goto err;
279
280         error = pci_enable_busmaster(device);
281         if (error != 0)
282                 goto err;
283
284         error = ioat_setup_intr(ioat);
285         if (error != 0)
286                 goto err;
287
288         error = ioat_reset_hw(ioat);
289         if (error != 0)
290                 goto err;
291
292         ioat_process_events(ioat, FALSE);
293         ioat_setup_sysctl(device);
294
295         mtx_lock(&ioat_list_mtx);
296         for (i = 0; i < IOAT_MAX_CHANNELS; i++) {
297                 if (ioat_channel[i] == NULL)
298                         break;
299         }
300         if (i >= IOAT_MAX_CHANNELS) {
301                 mtx_unlock(&ioat_list_mtx);
302                 device_printf(device, "Too many I/OAT devices in system\n");
303                 error = ENXIO;
304                 goto err;
305         }
306         ioat->chan_idx = i;
307         ioat_channel[i] = ioat;
308         if (i >= ioat_channel_index)
309                 ioat_channel_index = i + 1;
310         mtx_unlock(&ioat_list_mtx);
311
312         ioat_test_attach();
313
314 err:
315         if (error != 0)
316                 ioat_detach(device);
317         return (error);
318 }
319
320 static int
321 ioat_detach(device_t device)
322 {
323         struct ioat_softc *ioat;
324
325         ioat = DEVICE2SOFTC(device);
326
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);
333
334         ioat_test_detach();
335         taskqueue_drain(taskqueue_thread, &ioat->reset_task);
336
337         mtx_lock(&ioat->submit_lock);
338         ioat->quiescing = TRUE;
339         ioat->destroying = TRUE;
340         wakeup(&ioat->quiescing);
341         wakeup(&ioat->resetting);
342
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);
349
350         ioat_teardown_intr(ioat);
351         callout_drain(&ioat->poll_timer);
352
353         pci_disable_busmaster(device);
354
355         if (ioat->pci_resource != NULL)
356                 bus_release_resource(device, SYS_RES_MEMORY,
357                     ioat->pci_resource_id, ioat->pci_resource);
358
359         if (ioat->ring != NULL)
360                 ioat_free_ring(ioat, 1 << ioat->ring_size_order, ioat->ring);
361
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);
367         }
368
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,
372                     ioat->hw_desc_map);
373                 bus_dma_tag_destroy(ioat->hw_desc_tag);
374         }
375
376         return (0);
377 }
378
379 static int
380 ioat_teardown_intr(struct ioat_softc *ioat)
381 {
382
383         if (ioat->tag != NULL)
384                 bus_teardown_intr(ioat->device, ioat->res, ioat->tag);
385
386         if (ioat->res != NULL)
387                 bus_release_resource(ioat->device, SYS_RES_IRQ,
388                     rman_get_rid(ioat->res), ioat->res);
389
390         pci_release_msi(ioat->device);
391         return (0);
392 }
393
394 static int
395 ioat_start_channel(struct ioat_softc *ioat)
396 {
397         struct ioat_dma_hw_descriptor *hw_desc;
398         struct ioat_descriptor *desc;
399         struct bus_dmadesc *dmadesc;
400         uint64_t status;
401         uint32_t chanerr;
402         int i;
403
404         ioat_acquire(&ioat->dmaengine);
405
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;
410
411         dmadesc->callback_fn = NULL;
412         dmadesc->callback_arg = NULL;
413
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;
417         hw_desc->size = 8;
418         hw_desc->src_addr = 0;
419         hw_desc->dest_addr = 0;
420         hw_desc->u.control.null = 1;
421
422         ioat_submit_single(ioat);
423         ioat_release(&ioat->dmaengine);
424
425         for (i = 0; i < 100; i++) {
426                 DELAY(1);
427                 status = ioat_get_chansts(ioat);
428                 if (is_ioat_idle(status))
429                         return (0);
430         }
431
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,
435             IOAT_CHANERR_STR);
436         return (ENXIO);
437 }
438
439 /*
440  * Initialize Hardware
441  */
442 static int
443 ioat3_attach(device_t device)
444 {
445         struct ioat_softc *ioat;
446         struct ioat_descriptor *ring;
447         struct ioat_dma_hw_descriptor *dma_hw_desc;
448         void *hw_desc;
449         size_t ringsz;
450         int i, num_descriptors;
451         int error;
452         uint8_t xfercap;
453
454         error = 0;
455         ioat = DEVICE2SOFTC(device);
456         ioat->capabilities = ioat_read_dmacapability(ioat);
457
458         ioat_log_message(0, "Capabilities: %b\n", (int)ioat->capabilities,
459             IOAT_DMACAP_STR);
460
461         xfercap = ioat_read_xfercap(ioat);
462         ioat->max_xfer_size = 1 << xfercap;
463
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;
468
469         /* TODO: need to check DCA here if we ever do XOR/PQ */
470
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);
475
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);
481
482         ioat->is_submitter_processing = FALSE;
483
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);
488
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)
492                 return (ENOMEM);
493
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,
496             0);
497         if (error != 0)
498                 return (error);
499
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;
503
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,
507             &ioat->hw_desc_tag);
508         if (error != 0)
509                 return (error);
510
511         error = bus_dmamem_alloc(ioat->hw_desc_tag, &hw_desc,
512             BUS_DMA_ZERO | BUS_DMA_WAITOK, &ioat->hw_desc_map);
513         if (error != 0)
514                 return (error);
515
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);
518         if (error)
519                 return (error);
520
521         ioat->hw_desc_ring = hw_desc;
522
523         ioat->ring = malloc(num_descriptors * sizeof(*ring), M_IOAT,
524             M_ZERO | M_WAITOK);
525
526         ring = ioat->ring;
527         for (i = 0; i < num_descriptors; i++) {
528                 memset(&ring[i].bus_dmadesc, 0, sizeof(ring[i].bus_dmadesc));
529                 ring[i].id = i;
530         }
531
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);
535         }
536
537         ioat->head = 0;
538         ioat->tail = 0;
539         ioat->last_seen = 0;
540         *ioat->comp_update = 0;
541         return (0);
542 }
543
544 static int
545 ioat_map_pci_bar(struct ioat_softc *ioat)
546 {
547
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);
551
552         if (ioat->pci_resource == NULL) {
553                 ioat_log_message(0, "unable to allocate pci resource\n");
554                 return (ENODEV);
555         }
556
557         ioat->pci_bus_tag = rman_get_bustag(ioat->pci_resource);
558         ioat->pci_bus_handle = rman_get_bushandle(ioat->pci_resource);
559         return (0);
560 }
561
562 static void
563 ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg, int error)
564 {
565         struct ioat_softc *ioat = arg;
566
567         KASSERT(error == 0, ("%s: error:%d", __func__, error));
568         ioat->comp_update_bus_addr = seg[0].ds_addr;
569 }
570
571 static void
572 ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
573 {
574         bus_addr_t *baddr;
575
576         KASSERT(error == 0, ("%s: error:%d", __func__, error));
577         baddr = arg;
578         *baddr = segs->ds_addr;
579 }
580
581 /*
582  * Interrupt setup and handlers
583  */
584 static int
585 ioat_setup_intr(struct ioat_softc *ioat)
586 {
587         uint32_t num_vectors;
588         int error;
589         boolean_t use_msix;
590         boolean_t force_legacy_interrupts;
591
592         use_msix = FALSE;
593         force_legacy_interrupts = FALSE;
594
595         if (!g_force_legacy_interrupts && pci_msix_count(ioat->device) >= 1) {
596                 num_vectors = 1;
597                 pci_alloc_msix(ioat->device, &num_vectors);
598                 if (num_vectors == 1)
599                         use_msix = TRUE;
600         }
601
602         if (use_msix) {
603                 ioat->rid = 1;
604                 ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
605                     &ioat->rid, RF_ACTIVE);
606         } else {
607                 ioat->rid = 0;
608                 ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
609                     &ioat->rid, RF_SHAREABLE | RF_ACTIVE);
610         }
611         if (ioat->res == NULL) {
612                 ioat_log_message(0, "bus_alloc_resource failed\n");
613                 return (ENOMEM);
614         }
615
616         ioat->tag = NULL;
617         error = bus_setup_intr(ioat->device, ioat->res, INTR_MPSAFE |
618             INTR_TYPE_MISC, NULL, ioat_interrupt_handler, ioat, &ioat->tag);
619         if (error != 0) {
620                 ioat_log_message(0, "bus_setup_intr failed\n");
621                 return (error);
622         }
623
624         ioat_write_intrctrl(ioat, IOAT_INTRCTRL_MASTER_INT_EN);
625         return (0);
626 }
627
628 static boolean_t
629 ioat_model_resets_msix(struct ioat_softc *ioat)
630 {
631         u_int32_t pciid;
632
633         pciid = pci_get_devid(ioat->device);
634         switch (pciid) {
635                 /* BWD: */
636         case 0x0c508086:
637         case 0x0c518086:
638         case 0x0c528086:
639         case 0x0c538086:
640                 /* BDXDE: */
641         case 0x6f508086:
642         case 0x6f518086:
643         case 0x6f528086:
644         case 0x6f538086:
645                 return (TRUE);
646         }
647
648         return (FALSE);
649 }
650
651 static void
652 ioat_interrupt_handler(void *arg)
653 {
654         struct ioat_softc *ioat = arg;
655
656         ioat->stats.interrupts++;
657         ioat_process_events(ioat, TRUE);
658 }
659
660 static int
661 chanerr_to_errno(uint32_t chanerr)
662 {
663
664         if (chanerr == 0)
665                 return (0);
666         if ((chanerr & (IOAT_CHANERR_XSADDERR | IOAT_CHANERR_XDADDERR)) != 0)
667                 return (EFAULT);
668         if ((chanerr & (IOAT_CHANERR_RDERR | IOAT_CHANERR_WDERR)) != 0)
669                 return (EIO);
670         /* This one is probably our fault: */
671         if ((chanerr & IOAT_CHANERR_NDADDERR) != 0)
672                 return (EIO);
673         return (EIO);
674 }
675
676 static void
677 ioat_process_events(struct ioat_softc *ioat, boolean_t intr)
678 {
679         struct ioat_descriptor *desc;
680         struct bus_dmadesc *dmadesc;
681         uint64_t comp_update, status;
682         uint32_t completed, chanerr;
683         int error;
684
685         mtx_lock(&ioat->cleanup_lock);
686
687         /*
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.
691          */
692         if (ioat->resetting_cleanup) {
693                 mtx_unlock(&ioat->cleanup_lock);
694                 return;
695         }
696
697         completed = 0;
698         comp_update = *ioat->comp_update;
699         status = comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK;
700
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);
706
707         if (status == ioat->last_seen) {
708                 /*
709                  * If we landed in process_events and nothing has been
710                  * completed, check for a timeout due to channel halt.
711                  */
712                 goto out;
713         }
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);
716
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);
723
724                 if (dmadesc->callback_fn != NULL)
725                         dmadesc->callback_fn(dmadesc->callback_arg, 0);
726
727                 completed++;
728                 ioat->tail++;
729         }
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));
732
733         if (completed != 0) {
734                 ioat->last_seen = RING_PHYS_ADDR(ioat, ioat->tail - 1);
735                 ioat->stats.descriptors_processed += completed;
736                 wakeup(&ioat->tail);
737         }
738
739 out:
740         ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
741         mtx_unlock(&ioat->cleanup_lock);
742
743         /*
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.
748          */
749         if (!intr)
750                 comp_update = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS;
751         if (!is_ioat_halted(comp_update) && !is_ioat_suspended(comp_update))
752                 return;
753
754         ioat->stats.channel_halts++;
755
756         /*
757          * Fatal programming error on this DMA channel.  Flush any outstanding
758          * work with error status and restart the engine.
759          */
760         mtx_lock(&ioat->submit_lock);
761         ioat->quiescing = TRUE;
762         mtx_unlock(&ioat->submit_lock);
763
764         /*
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.
770          */
771         mtx_lock(&ioat->cleanup_lock);
772         ioat->resetting_cleanup = TRUE;
773
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;
778
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);
785
786                 if (dmadesc->callback_fn != NULL)
787                         dmadesc->callback_fn(dmadesc->callback_arg,
788                             chanerr_to_errno(chanerr));
789
790                 ioat->tail++;
791                 ioat->stats.descriptors_processed++;
792                 ioat->stats.descriptors_error++;
793         }
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));
796
797         /* Clear error status */
798         ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
799
800         mtx_unlock(&ioat->cleanup_lock);
801
802         ioat_log_message(0, "Resetting channel to recover from error\n");
803         error = taskqueue_enqueue(taskqueue_thread, &ioat->reset_task);
804         KASSERT(error == 0,
805             ("%s: taskqueue_enqueue failed: %d", __func__, error));
806 }
807
808 static void
809 ioat_reset_hw_task(void *ctx, int pending __unused)
810 {
811         struct ioat_softc *ioat;
812         int error;
813
814         ioat = ctx;
815         ioat_log_message(1, "%s: Resetting channel\n", __func__);
816
817         error = ioat_reset_hw(ioat);
818         KASSERT(error == 0, ("%s: reset failed: %d", __func__, error));
819         (void)error;
820 }
821
822 /*
823  * User API functions
824  */
825 unsigned
826 ioat_get_nchannels(void)
827 {
828
829         return (ioat_channel_index);
830 }
831
832 bus_dmaengine_t
833 ioat_get_dmaengine(uint32_t index, int flags)
834 {
835         struct ioat_softc *ioat;
836
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"));
841
842         mtx_lock(&ioat_list_mtx);
843         if (index >= ioat_channel_index ||
844             (ioat = ioat_channel[index]) == NULL) {
845                 mtx_unlock(&ioat_list_mtx);
846                 return (NULL);
847         }
848         mtx_lock(&ioat->submit_lock);
849         mtx_unlock(&ioat_list_mtx);
850
851         if (ioat->destroying) {
852                 mtx_unlock(&ioat->submit_lock);
853                 return (NULL);
854         }
855
856         ioat_get(ioat);
857         if (ioat->quiescing) {
858                 if ((flags & M_NOWAIT) != 0) {
859                         ioat_put(ioat);
860                         mtx_unlock(&ioat->submit_lock);
861                         return (NULL);
862                 }
863
864                 while (ioat->quiescing && !ioat->destroying)
865                         msleep(&ioat->quiescing, &ioat->submit_lock, 0, "getdma", 0);
866
867                 if (ioat->destroying) {
868                         ioat_put(ioat);
869                         mtx_unlock(&ioat->submit_lock);
870                         return (NULL);
871                 }
872         }
873         mtx_unlock(&ioat->submit_lock);
874         return (&ioat->dmaengine);
875 }
876
877 void
878 ioat_put_dmaengine(bus_dmaengine_t dmaengine)
879 {
880         struct ioat_softc *ioat;
881
882         ioat = to_ioat_softc(dmaengine);
883         mtx_lock(&ioat->submit_lock);
884         ioat_put(ioat);
885         mtx_unlock(&ioat->submit_lock);
886 }
887
888 int
889 ioat_get_hwversion(bus_dmaengine_t dmaengine)
890 {
891         struct ioat_softc *ioat;
892
893         ioat = to_ioat_softc(dmaengine);
894         return (ioat->version);
895 }
896
897 size_t
898 ioat_get_max_io_size(bus_dmaengine_t dmaengine)
899 {
900         struct ioat_softc *ioat;
901
902         ioat = to_ioat_softc(dmaengine);
903         return (ioat->max_xfer_size);
904 }
905
906 uint32_t
907 ioat_get_capabilities(bus_dmaengine_t dmaengine)
908 {
909         struct ioat_softc *ioat;
910
911         ioat = to_ioat_softc(dmaengine);
912         return (ioat->capabilities);
913 }
914
915 int
916 ioat_set_interrupt_coalesce(bus_dmaengine_t dmaengine, uint16_t delay)
917 {
918         struct ioat_softc *ioat;
919
920         ioat = to_ioat_softc(dmaengine);
921         if (!ioat->intrdelay_supported)
922                 return (ENODEV);
923         if (delay > ioat->intrdelay_max)
924                 return (ERANGE);
925
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;
929         return (0);
930 }
931
932 uint16_t
933 ioat_get_max_coalesce_period(bus_dmaengine_t dmaengine)
934 {
935         struct ioat_softc *ioat;
936
937         ioat = to_ioat_softc(dmaengine);
938         return (ioat->intrdelay_max);
939 }
940
941 void
942 ioat_acquire(bus_dmaengine_t dmaengine)
943 {
944         struct ioat_softc *ioat;
945
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;
950 }
951
952 int
953 ioat_acquire_reserve(bus_dmaengine_t dmaengine, unsigned n, int mflags)
954 {
955         struct ioat_softc *ioat;
956         int error;
957
958         ioat = to_ioat_softc(dmaengine);
959         ioat_acquire(dmaengine);
960
961         error = ioat_reserve_space(ioat, n, mflags);
962         if (error != 0)
963                 ioat_release(dmaengine);
964         return (error);
965 }
966
967 void
968 ioat_release(bus_dmaengine_t dmaengine)
969 {
970         struct ioat_softc *ioat;
971
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);
978
979         if (ioat->acq_head != ioat->head) {
980                 ioat_write_2(ioat, IOAT_DMACOUNT_OFFSET,
981                     (uint16_t)ioat->head);
982
983                 if (!callout_pending(&ioat->poll_timer)) {
984                         callout_reset(&ioat->poll_timer, 1,
985                             ioat_poll_timer_callback, ioat);
986                 }
987         }
988         mtx_unlock(&ioat->submit_lock);
989 }
990
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,
995     uint32_t flags)
996 {
997         struct ioat_generic_hw_descriptor *hw_desc;
998         struct ioat_descriptor *desc;
999         int mflags;
1000
1001         mtx_assert(&ioat->submit_lock, MA_OWNED);
1002
1003         KASSERT((flags & ~_DMA_GENERIC_FLAGS) == 0,
1004             ("Unrecognized flag(s): %#x", flags & ~_DMA_GENERIC_FLAGS));
1005         if ((flags & DMA_NO_WAIT) != 0)
1006                 mflags = M_NOWAIT;
1007         else
1008                 mflags = M_WAITOK;
1009
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);
1013                 return (NULL);
1014         }
1015
1016         if (ioat_reserve_space(ioat, 1, mflags) != 0)
1017                 return (NULL);
1018
1019         desc = ioat_get_ring_entry(ioat, ioat->head);
1020         hw_desc = &ioat_get_descriptor(ioat, ioat->head)->generic;
1021
1022         hw_desc->u.control_raw = 0;
1023         hw_desc->u.control_generic.op = op;
1024         hw_desc->u.control_generic.completion_update = 1;
1025
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;
1030
1031         hw_desc->size = size;
1032         hw_desc->src_addr = src;
1033         hw_desc->dest_addr = dst;
1034
1035         desc->bus_dmadesc.callback_fn = callback_fn;
1036         desc->bus_dmadesc.callback_arg = callback_arg;
1037         return (desc);
1038 }
1039
1040 struct bus_dmadesc *
1041 ioat_null(bus_dmaengine_t dmaengine, bus_dmaengine_callback_t callback_fn,
1042     void *callback_arg, uint32_t flags)
1043 {
1044         struct ioat_dma_hw_descriptor *hw_desc;
1045         struct ioat_descriptor *desc;
1046         struct ioat_softc *ioat;
1047
1048         ioat = to_ioat_softc(dmaengine);
1049         CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1050
1051         desc = ioat_op_generic(ioat, IOAT_OP_COPY, 8, 0, 0, callback_fn,
1052             callback_arg, flags);
1053         if (desc == NULL)
1054                 return (NULL);
1055
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);
1060 }
1061
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)
1066 {
1067         struct ioat_dma_hw_descriptor *hw_desc;
1068         struct ioat_descriptor *desc;
1069         struct ioat_softc *ioat;
1070
1071         ioat = to_ioat_softc(dmaengine);
1072
1073         if (((src | dst) & (0xffffull << 48)) != 0) {
1074                 ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n",
1075                     __func__);
1076                 return (NULL);
1077         }
1078
1079         desc = ioat_op_generic(ioat, IOAT_OP_COPY, len, src, dst, callback_fn,
1080             callback_arg, flags);
1081         if (desc == NULL)
1082                 return (NULL);
1083
1084         hw_desc = &ioat_get_descriptor(ioat, desc->id)->dma;
1085         if (g_ioat_debug_level >= 3)
1086                 dump_descriptor(hw_desc);
1087
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);
1092 }
1093
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)
1098 {
1099         struct ioat_dma_hw_descriptor *hw_desc;
1100         struct ioat_descriptor *desc;
1101         struct ioat_softc *ioat;
1102
1103         ioat = to_ioat_softc(dmaengine);
1104         CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1105
1106         if (((src1 | src2 | dst1 | dst2) & (0xffffull << 48)) != 0) {
1107                 ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n",
1108                     __func__);
1109                 return (NULL);
1110         }
1111         if (((src1 | src2 | dst1 | dst2) & PAGE_MASK) != 0) {
1112                 ioat_log_message(0, "%s: Addresses must be page-aligned\n",
1113                     __func__);
1114                 return (NULL);
1115         }
1116
1117         desc = ioat_op_generic(ioat, IOAT_OP_COPY, 2 * PAGE_SIZE, src1, dst1,
1118             callback_fn, callback_arg, flags);
1119         if (desc == NULL)
1120                 return (NULL);
1121
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;
1126         }
1127         if (dst2 != dst1 + PAGE_SIZE) {
1128                 hw_desc->u.control.dest_page_break = 1;
1129                 hw_desc->next_dest_addr = dst2;
1130         }
1131
1132         if (g_ioat_debug_level >= 3)
1133                 dump_descriptor(hw_desc);
1134
1135         ioat_submit_single(ioat);
1136         return (&desc->bus_dmadesc);
1137 }
1138
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)
1143 {
1144         struct ioat_crc32_hw_descriptor *hw_desc;
1145         struct ioat_descriptor *desc;
1146         struct ioat_softc *ioat;
1147         uint32_t teststore;
1148         uint8_t op;
1149
1150         ioat = to_ioat_softc(dmaengine);
1151         CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1152
1153         if ((ioat->capabilities & IOAT_DMACAP_MOVECRC) == 0) {
1154                 ioat_log_message(0, "%s: Device lacks MOVECRC capability\n",
1155                     __func__);
1156                 return (NULL);
1157         }
1158         if (((src | dst) & (0xffffffull << 40)) != 0) {
1159                 ioat_log_message(0, "%s: High 24 bits of src/dst invalid\n",
1160                     __func__);
1161                 return (NULL);
1162         }
1163         teststore = (flags & _DMA_CRC_TESTSTORE);
1164         if (teststore == _DMA_CRC_TESTSTORE) {
1165                 ioat_log_message(0, "%s: TEST and STORE invalid\n", __func__);
1166                 return (NULL);
1167         }
1168         if (teststore == 0 && (flags & DMA_CRC_INLINE) != 0) {
1169                 ioat_log_message(0, "%s: INLINE invalid without TEST or STORE\n",
1170                     __func__);
1171                 return (NULL);
1172         }
1173
1174         switch (teststore) {
1175         case DMA_CRC_STORE:
1176                 op = IOAT_OP_MOVECRC_STORE;
1177                 break;
1178         case DMA_CRC_TEST:
1179                 op = IOAT_OP_MOVECRC_TEST;
1180                 break;
1181         default:
1182                 KASSERT(teststore == 0, ("bogus"));
1183                 op = IOAT_OP_MOVECRC;
1184                 break;
1185         }
1186
1187         if ((flags & DMA_CRC_INLINE) == 0 &&
1188             (crcptr & (0xffffffull << 40)) != 0) {
1189                 ioat_log_message(0,
1190                     "%s: High 24 bits of crcptr invalid\n", __func__);
1191                 return (NULL);
1192         }
1193
1194         desc = ioat_op_generic(ioat, op, len, src, dst, callback_fn,
1195             callback_arg, flags & ~_DMA_CRC_FLAGS);
1196         if (desc == NULL)
1197                 return (NULL);
1198
1199         hw_desc = &ioat_get_descriptor(ioat, desc->id)->crc32;
1200
1201         if ((flags & DMA_CRC_INLINE) == 0)
1202                 hw_desc->crc_address = crcptr;
1203         else
1204                 hw_desc->u.control.crc_location = 1;
1205
1206         if (initialseed != NULL) {
1207                 hw_desc->u.control.use_seed = 1;
1208                 hw_desc->seed = *initialseed;
1209         }
1210
1211         if (g_ioat_debug_level >= 3)
1212                 dump_descriptor(hw_desc);
1213
1214         ioat_submit_single(ioat);
1215         return (&desc->bus_dmadesc);
1216 }
1217
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)
1222 {
1223         struct ioat_crc32_hw_descriptor *hw_desc;
1224         struct ioat_descriptor *desc;
1225         struct ioat_softc *ioat;
1226         uint32_t teststore;
1227         uint8_t op;
1228
1229         ioat = to_ioat_softc(dmaengine);
1230         CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1231
1232         if ((ioat->capabilities & IOAT_DMACAP_CRC) == 0) {
1233                 ioat_log_message(0, "%s: Device lacks CRC capability\n",
1234                     __func__);
1235                 return (NULL);
1236         }
1237         if ((src & (0xffffffull << 40)) != 0) {
1238                 ioat_log_message(0, "%s: High 24 bits of src invalid\n",
1239                     __func__);
1240                 return (NULL);
1241         }
1242         teststore = (flags & _DMA_CRC_TESTSTORE);
1243         if (teststore == _DMA_CRC_TESTSTORE) {
1244                 ioat_log_message(0, "%s: TEST and STORE invalid\n", __func__);
1245                 return (NULL);
1246         }
1247         if (teststore == 0 && (flags & DMA_CRC_INLINE) != 0) {
1248                 ioat_log_message(0, "%s: INLINE invalid without TEST or STORE\n",
1249                     __func__);
1250                 return (NULL);
1251         }
1252
1253         switch (teststore) {
1254         case DMA_CRC_STORE:
1255                 op = IOAT_OP_CRC_STORE;
1256                 break;
1257         case DMA_CRC_TEST:
1258                 op = IOAT_OP_CRC_TEST;
1259                 break;
1260         default:
1261                 KASSERT(teststore == 0, ("bogus"));
1262                 op = IOAT_OP_CRC;
1263                 break;
1264         }
1265
1266         if ((flags & DMA_CRC_INLINE) == 0 &&
1267             (crcptr & (0xffffffull << 40)) != 0) {
1268                 ioat_log_message(0,
1269                     "%s: High 24 bits of crcptr invalid\n", __func__);
1270                 return (NULL);
1271         }
1272
1273         desc = ioat_op_generic(ioat, op, len, src, 0, callback_fn,
1274             callback_arg, flags & ~_DMA_CRC_FLAGS);
1275         if (desc == NULL)
1276                 return (NULL);
1277
1278         hw_desc = &ioat_get_descriptor(ioat, desc->id)->crc32;
1279
1280         if ((flags & DMA_CRC_INLINE) == 0)
1281                 hw_desc->crc_address = crcptr;
1282         else
1283                 hw_desc->u.control.crc_location = 1;
1284
1285         if (initialseed != NULL) {
1286                 hw_desc->u.control.use_seed = 1;
1287                 hw_desc->seed = *initialseed;
1288         }
1289
1290         if (g_ioat_debug_level >= 3)
1291                 dump_descriptor(hw_desc);
1292
1293         ioat_submit_single(ioat);
1294         return (&desc->bus_dmadesc);
1295 }
1296
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,
1300     uint32_t flags)
1301 {
1302         struct ioat_fill_hw_descriptor *hw_desc;
1303         struct ioat_descriptor *desc;
1304         struct ioat_softc *ioat;
1305
1306         ioat = to_ioat_softc(dmaengine);
1307         CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1308
1309         if ((ioat->capabilities & IOAT_DMACAP_BFILL) == 0) {
1310                 ioat_log_message(0, "%s: Device lacks BFILL capability\n",
1311                     __func__);
1312                 return (NULL);
1313         }
1314
1315         if ((dst & (0xffffull << 48)) != 0) {
1316                 ioat_log_message(0, "%s: High 16 bits of dst invalid\n",
1317                     __func__);
1318                 return (NULL);
1319         }
1320
1321         desc = ioat_op_generic(ioat, IOAT_OP_FILL, len, fillpattern, dst,
1322             callback_fn, callback_arg, flags);
1323         if (desc == NULL)
1324                 return (NULL);
1325
1326         hw_desc = &ioat_get_descriptor(ioat, desc->id)->fill;
1327         if (g_ioat_debug_level >= 3)
1328                 dump_descriptor(hw_desc);
1329
1330         ioat_submit_single(ioat);
1331         return (&desc->bus_dmadesc);
1332 }
1333
1334 /*
1335  * Ring Management
1336  */
1337 static inline uint32_t
1338 ioat_get_active(struct ioat_softc *ioat)
1339 {
1340
1341         return ((ioat->head - ioat->tail) & ((1 << ioat->ring_size_order) - 1));
1342 }
1343
1344 static inline uint32_t
1345 ioat_get_ring_space(struct ioat_softc *ioat)
1346 {
1347
1348         return ((1 << ioat->ring_size_order) - ioat_get_active(ioat) - 1);
1349 }
1350
1351 /*
1352  * Reserves space in this IOAT descriptor ring by ensuring enough slots remain
1353  * for 'num_descs'.
1354  *
1355  * If mflags contains M_WAITOK, blocks until enough space is available.
1356  *
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.
1360  *
1361  * Must be called with the submit_lock held; returns with the lock held.  The
1362  * lock may be dropped to allocate the ring.
1363  *
1364  * (The submit_lock is needed to add any entries to the ring, so callers are
1365  * assured enough room is available.)
1366  */
1367 static int
1368 ioat_reserve_space(struct ioat_softc *ioat, uint32_t num_descs, int mflags)
1369 {
1370         boolean_t dug;
1371         int error;
1372
1373         mtx_assert(&ioat->submit_lock, MA_OWNED);
1374         error = 0;
1375         dug = FALSE;
1376
1377         if (num_descs < 1 || num_descs >= (1 << ioat->ring_size_order)) {
1378                 error = EINVAL;
1379                 goto out;
1380         }
1381
1382         for (;;) {
1383                 if (ioat->quiescing) {
1384                         error = ENXIO;
1385                         goto out;
1386                 }
1387
1388                 if (ioat_get_ring_space(ioat) >= num_descs)
1389                         goto out;
1390
1391                 CTR3(KTR_IOAT, "%s channel=%u starved (%u)", __func__,
1392                     ioat->chan_idx, num_descs);
1393
1394                 if (!dug && !ioat->is_submitter_processing) {
1395                         ioat->is_submitter_processing = TRUE;
1396                         mtx_unlock(&ioat->submit_lock);
1397
1398                         CTR2(KTR_IOAT, "%s channel=%u attempting to process events",
1399                             __func__, ioat->chan_idx);
1400                         ioat_process_events(ioat, FALSE);
1401
1402                         mtx_lock(&ioat->submit_lock);
1403                         dug = TRUE;
1404                         KASSERT(ioat->is_submitter_processing == TRUE,
1405                             ("is_submitter_processing"));
1406                         ioat->is_submitter_processing = FALSE;
1407                         wakeup(&ioat->tail);
1408                         continue;
1409                 }
1410
1411                 if ((mflags & M_WAITOK) == 0) {
1412                         error = EAGAIN;
1413                         break;
1414                 }
1415                 CTR2(KTR_IOAT, "%s channel=%u blocking on completions",
1416                     __func__, ioat->chan_idx);
1417                 msleep(&ioat->tail, &ioat->submit_lock, 0,
1418                     "ioat_full", 0);
1419                 continue;
1420         }
1421
1422 out:
1423         mtx_assert(&ioat->submit_lock, MA_OWNED);
1424         KASSERT(!ioat->quiescing || error == ENXIO,
1425             ("reserved during quiesce"));
1426         return (error);
1427 }
1428
1429 static void
1430 ioat_free_ring(struct ioat_softc *ioat, uint32_t size,
1431     struct ioat_descriptor *ring)
1432 {
1433
1434         free(ring, M_IOAT);
1435 }
1436
1437 static struct ioat_descriptor *
1438 ioat_get_ring_entry(struct ioat_softc *ioat, uint32_t index)
1439 {
1440
1441         return (&ioat->ring[index % (1 << ioat->ring_size_order)]);
1442 }
1443
1444 static union ioat_hw_descriptor *
1445 ioat_get_descriptor(struct ioat_softc *ioat, uint32_t index)
1446 {
1447
1448         return (&ioat->hw_desc_ring[index % (1 << ioat->ring_size_order)]);
1449 }
1450
1451 static void
1452 ioat_halted_debug(struct ioat_softc *ioat, uint32_t chanerr)
1453 {
1454         union ioat_hw_descriptor *desc;
1455
1456         ioat_log_message(0, "Channel halted (%b)\n", (int)chanerr,
1457             IOAT_CHANERR_STR);
1458         if (chanerr == 0)
1459                 return;
1460
1461         mtx_assert(&ioat->cleanup_lock, MA_OWNED);
1462
1463         desc = ioat_get_descriptor(ioat, ioat->tail + 0);
1464         dump_descriptor(desc);
1465
1466         desc = ioat_get_descriptor(ioat, ioat->tail + 1);
1467         dump_descriptor(desc);
1468 }
1469
1470 static void
1471 ioat_poll_timer_callback(void *arg)
1472 {
1473         struct ioat_softc *ioat;
1474
1475         ioat = arg;
1476         ioat_log_message(3, "%s\n", __func__);
1477
1478         ioat_process_events(ioat, FALSE);
1479
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);
1484 }
1485
1486 /*
1487  * Support Functions
1488  */
1489 static void
1490 ioat_submit_single(struct ioat_softc *ioat)
1491 {
1492
1493         mtx_assert(&ioat->submit_lock, MA_OWNED);
1494
1495         ioat->head++;
1496         CTR4(KTR_IOAT, "%s channel=%u head=%u tail=%u", __func__,
1497             ioat->chan_idx, ioat->head, ioat->tail);
1498
1499         ioat->stats.descriptors_submitted++;
1500 }
1501
1502 static int
1503 ioat_reset_hw(struct ioat_softc *ioat)
1504 {
1505         uint64_t status;
1506         uint32_t chanerr;
1507         unsigned timeout;
1508         int error;
1509
1510         CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1511
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);
1517                 return (ENXIO);
1518         }
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);
1525
1526         /*
1527          * Suspend ioat_process_events while the hardware and softc are in an
1528          * indeterminate state.
1529          */
1530         ioat->resetting_cleanup = TRUE;
1531         mtx_unlock(&ioat->cleanup_lock);
1532
1533         CTR2(KTR_IOAT, "%s channel=%u quiesced and drained", __func__,
1534             ioat->chan_idx);
1535
1536         status = ioat_get_chansts(ioat);
1537         if (is_ioat_active(status) || is_ioat_idle(status))
1538                 ioat_suspend(ioat);
1539
1540         /* Wait at most 20 ms */
1541         for (timeout = 0; (is_ioat_active(status) || is_ioat_idle(status)) &&
1542             timeout < 20; timeout++) {
1543                 DELAY(1000);
1544                 status = ioat_get_chansts(ioat);
1545         }
1546         if (timeout == 20) {
1547                 error = ETIMEDOUT;
1548                 goto out;
1549         }
1550
1551         KASSERT(ioat_get_active(ioat) == 0, ("active after quiesce"));
1552
1553         chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
1554         ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
1555
1556         CTR2(KTR_IOAT, "%s channel=%u hardware suspended", __func__,
1557             ioat->chan_idx);
1558
1559         /*
1560          * IOAT v3 workaround - CHANERRMSK_INT with 3E07h to masks out errors
1561          *  that can cause stability issues for IOAT v3.
1562          */
1563         pci_write_config(ioat->device, IOAT_CFG_CHANERRMASK_INT_OFFSET, 0x3e07,
1564             4);
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);
1567
1568         /*
1569          * BDXDE and BWD models reset MSI-X registers on device reset.
1570          * Save/restore their contents manually.
1571          */
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);
1575         }
1576
1577         ioat_reset(ioat);
1578         CTR2(KTR_IOAT, "%s channel=%u hardware reset", __func__,
1579             ioat->chan_idx);
1580
1581         /* Wait at most 20 ms */
1582         for (timeout = 0; ioat_reset_pending(ioat) && timeout < 20; timeout++)
1583                 DELAY(1000);
1584         if (timeout == 20) {
1585                 error = ETIMEDOUT;
1586                 goto out;
1587         }
1588
1589         if (ioat_model_resets_msix(ioat)) {
1590                 ioat_log_message(1, "device resets registers; restored\n");
1591                 pci_restore_state(ioat->device);
1592         }
1593
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);
1600                 error = 0;
1601                 goto out;
1602         }
1603
1604         chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
1605         if (chanerr != 0) {
1606                 mtx_lock(&ioat->cleanup_lock);
1607                 ioat_halted_debug(ioat, chanerr);
1608                 mtx_unlock(&ioat->cleanup_lock);
1609                 error = EIO;
1610                 goto out;
1611         }
1612
1613         /*
1614          * Bring device back online after reset.  Writing CHAINADDR brings the
1615          * device back to active.
1616          *
1617          * The internal ring counter resets to zero, so we have to start over
1618          * at zero as well.
1619          */
1620         ioat->tail = ioat->head = 0;
1621         ioat->last_seen = 0;
1622         *ioat->comp_update = 0;
1623
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));
1627         error = 0;
1628         CTR2(KTR_IOAT, "%s channel=%u configured channel", __func__,
1629             ioat->chan_idx);
1630
1631 out:
1632         /* Enqueues a null operation and ensures it completes. */
1633         if (error == 0) {
1634                 error = ioat_start_channel(ioat);
1635                 CTR2(KTR_IOAT, "%s channel=%u started channel", __func__,
1636                     ioat->chan_idx);
1637         }
1638
1639         /*
1640          * Resume completions now that ring state is consistent.
1641          */
1642         mtx_lock(&ioat->cleanup_lock);
1643         ioat->resetting_cleanup = FALSE;
1644         mtx_unlock(&ioat->cleanup_lock);
1645
1646         /* Unblock submission of new work */
1647         mtx_lock(&ioat->submit_lock);
1648         ioat->quiescing = FALSE;
1649         wakeup(&ioat->quiescing);
1650
1651         ioat->resetting = FALSE;
1652         wakeup(&ioat->resetting);
1653
1654         CTR2(KTR_IOAT, "%s channel=%u reset done", __func__, ioat->chan_idx);
1655         mtx_unlock(&ioat->submit_lock);
1656
1657         return (error);
1658 }
1659
1660 static int
1661 sysctl_handle_chansts(SYSCTL_HANDLER_ARGS)
1662 {
1663         struct ioat_softc *ioat;
1664         struct sbuf sb;
1665         uint64_t status;
1666         int error;
1667
1668         ioat = arg1;
1669
1670         status = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS;
1671
1672         sbuf_new_for_sysctl(&sb, NULL, 256, req);
1673         switch (status) {
1674         case IOAT_CHANSTS_ACTIVE:
1675                 sbuf_printf(&sb, "ACTIVE");
1676                 break;
1677         case IOAT_CHANSTS_IDLE:
1678                 sbuf_printf(&sb, "IDLE");
1679                 break;
1680         case IOAT_CHANSTS_SUSPENDED:
1681                 sbuf_printf(&sb, "SUSPENDED");
1682                 break;
1683         case IOAT_CHANSTS_HALTED:
1684                 sbuf_printf(&sb, "HALTED");
1685                 break;
1686         case IOAT_CHANSTS_ARMED:
1687                 sbuf_printf(&sb, "ARMED");
1688                 break;
1689         default:
1690                 sbuf_printf(&sb, "UNKNOWN");
1691                 break;
1692         }
1693         error = sbuf_finish(&sb);
1694         sbuf_delete(&sb);
1695
1696         if (error != 0 || req->newptr == NULL)
1697                 return (error);
1698         return (EINVAL);
1699 }
1700
1701 static int
1702 sysctl_handle_dpi(SYSCTL_HANDLER_ARGS)
1703 {
1704         struct ioat_softc *ioat;
1705         struct sbuf sb;
1706 #define PRECISION       "1"
1707         const uintmax_t factor = 10;
1708         uintmax_t rate;
1709         int error;
1710
1711         ioat = arg1;
1712         sbuf_new_for_sysctl(&sb, NULL, 16, req);
1713
1714         if (ioat->stats.interrupts == 0) {
1715                 sbuf_printf(&sb, "NaN");
1716                 goto out;
1717         }
1718         rate = ioat->stats.descriptors_processed * factor /
1719             ioat->stats.interrupts;
1720         sbuf_printf(&sb, "%ju.%." PRECISION "ju", rate / factor,
1721             rate % factor);
1722 #undef  PRECISION
1723 out:
1724         error = sbuf_finish(&sb);
1725         sbuf_delete(&sb);
1726         if (error != 0 || req->newptr == NULL)
1727                 return (error);
1728         return (EINVAL);
1729 }
1730
1731 static int
1732 sysctl_handle_reset(SYSCTL_HANDLER_ARGS)
1733 {
1734         struct ioat_softc *ioat;
1735         int error, arg;
1736
1737         ioat = arg1;
1738
1739         arg = 0;
1740         error = SYSCTL_OUT(req, &arg, sizeof(arg));
1741         if (error != 0 || req->newptr == NULL)
1742                 return (error);
1743
1744         error = SYSCTL_IN(req, &arg, sizeof(arg));
1745         if (error != 0)
1746                 return (error);
1747
1748         if (arg != 0)
1749                 error = ioat_reset_hw(ioat);
1750
1751         return (error);
1752 }
1753
1754 static void
1755 dump_descriptor(void *hw_desc)
1756 {
1757         int i, j;
1758
1759         for (i = 0; i < 2; i++) {
1760                 for (j = 0; j < 8; j++)
1761                         printf("%08x ", ((uint32_t *)hw_desc)[i * 8 + j]);
1762                 printf("\n");
1763         }
1764 }
1765
1766 static void
1767 ioat_setup_sysctl(device_t device)
1768 {
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;
1773
1774         ioat = DEVICE2SOFTC(device);
1775         ctx = device_get_sysctl_ctx(device);
1776         tree = device_get_sysctl_tree(device);
1777         par = SYSCTL_CHILDREN(tree);
1778
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)");
1788
1789         tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "state", CTLFLAG_RD, NULL,
1790             "IOAT channel internal state");
1791         state = SYSCTL_CHILDREN(tmp);
1792
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");
1799
1800         SYSCTL_ADD_UQUAD(ctx, state, OID_AUTO, "last_completion", CTLFLAG_RD,
1801             ioat->comp_update, "HW addr of last completion");
1802
1803         SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_submitter_processing",
1804             CTLFLAG_RD, &ioat->is_submitter_processing, 0,
1805             "submitter processing");
1806
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");
1810
1811         SYSCTL_ADD_U16(ctx, state, OID_AUTO, "intrdelay", CTLFLAG_RD,
1812             &ioat->cached_intrdelay, 0,
1813             "Current INTRDELAY on this channel (cached, microseconds)");
1814
1815         tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "hammer", CTLFLAG_RD, NULL,
1816             "Big hammers (mostly for testing)");
1817         hammer = SYSCTL_CHILDREN(tmp);
1818
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");
1822
1823         tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "stats", CTLFLAG_RD, NULL,
1824             "IOAT channel statistics");
1825         statpar = SYSCTL_CHILDREN(tmp);
1826
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");
1845
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");
1849 }
1850
1851 static void
1852 ioat_get(struct ioat_softc *ioat)
1853 {
1854
1855         mtx_assert(&ioat->submit_lock, MA_OWNED);
1856         KASSERT(ioat->refcnt < UINT32_MAX, ("refcnt overflow"));
1857
1858         ioat->refcnt++;
1859 }
1860
1861 static void
1862 ioat_put(struct ioat_softc *ioat)
1863 {
1864
1865         mtx_assert(&ioat->submit_lock, MA_OWNED);
1866         KASSERT(ioat->refcnt >= 1, ("refcnt error"));
1867
1868         if (--ioat->refcnt == 0)
1869                 wakeup(&ioat->refcnt);
1870 }
1871
1872 static void
1873 ioat_drain_locked(struct ioat_softc *ioat)
1874 {
1875
1876         mtx_assert(&ioat->submit_lock, MA_OWNED);
1877
1878         while (ioat->refcnt > 0)
1879                 msleep(&ioat->refcnt, &ioat->submit_lock, 0, "ioat_drain", 0);
1880 }
1881
1882 #ifdef DDB
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)
1886 {
1887         struct ioat_softc *sc;
1888         unsigned idx;
1889
1890         if (!have_addr)
1891                 goto usage;
1892         idx = (unsigned)addr;
1893         if (idx >= ioat_channel_index)
1894                 goto usage;
1895
1896         sc = ioat_channel[idx];
1897         db_printf("ioat softc at %p\n", sc);
1898         if (sc == NULL)
1899                 return;
1900
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);
1905
1906         db_printf(" capabilities: %b\n", (int)sc->capabilities,
1907             IOAT_DMACAP_STR);
1908         db_printf(" cached_intrdelay: %u\n", sc->cached_intrdelay);
1909         db_printf(" *comp_update: 0x%jx\n", (uintmax_t)*sc->comp_update);
1910
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);
1917
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);
1924
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);
1933
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);
1941
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);
1949
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);
1957
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);
1962
1963         db_printf(" cleanup_lock: ");
1964         db_show_lock(&sc->cleanup_lock);
1965
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);
1972
1973         db_printf("  channel_halts: %u\n", sc->stats.channel_halts);
1974         db_printf("  last_halt_chanerr: %u\n", sc->stats.last_halt_chanerr);
1975
1976         if (db_pager_quit)
1977                 return;
1978
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);
1993         return;
1994 usage:
1995         db_printf("usage: show ioat <0-%u>\n", ioat_channel_index);
1996         return;
1997 }
1998 #endif /* DDB */