xref: /openbmc/linux/drivers/dma/ioat/init.c (revision fb6e3d4d)
14fa9c49fSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2c0f28ce6SDave Jiang /*
3c0f28ce6SDave Jiang  * Intel I/OAT DMA Linux driver
4c0f28ce6SDave Jiang  * Copyright(c) 2004 - 2015 Intel Corporation.
5c0f28ce6SDave Jiang  */
6c0f28ce6SDave Jiang 
7c0f28ce6SDave Jiang #include <linux/init.h>
8c0f28ce6SDave Jiang #include <linux/module.h>
9c0f28ce6SDave Jiang #include <linux/slab.h>
10c0f28ce6SDave Jiang #include <linux/pci.h>
11c0f28ce6SDave Jiang #include <linux/interrupt.h>
12c0f28ce6SDave Jiang #include <linux/dmaengine.h>
13c0f28ce6SDave Jiang #include <linux/delay.h>
14c0f28ce6SDave Jiang #include <linux/dma-mapping.h>
15c0f28ce6SDave Jiang #include <linux/workqueue.h>
16c0f28ce6SDave Jiang #include <linux/prefetch.h>
17c0f28ce6SDave Jiang #include <linux/dca.h>
18dd4645ebSDave Jiang #include <linux/sizes.h>
19c0f28ce6SDave Jiang #include "dma.h"
20c0f28ce6SDave Jiang #include "registers.h"
21c0f28ce6SDave Jiang #include "hw.h"
22c0f28ce6SDave Jiang 
23c0f28ce6SDave Jiang #include "../dmaengine.h"
24c0f28ce6SDave Jiang 
25c0f28ce6SDave Jiang MODULE_VERSION(IOAT_DMA_VERSION);
26c0f28ce6SDave Jiang MODULE_LICENSE("Dual BSD/GPL");
27c0f28ce6SDave Jiang MODULE_AUTHOR("Intel Corporation");
28c0f28ce6SDave Jiang 
2901fa2faeSArvind Yadav static const struct pci_device_id ioat_pci_tbl[] = {
30c0f28ce6SDave Jiang 	/* I/OAT v3 platforms */
31c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG0) },
32c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG1) },
33c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG2) },
34c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG3) },
35c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG4) },
36c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG5) },
37c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG6) },
38c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG7) },
39c0f28ce6SDave Jiang 
40c0f28ce6SDave Jiang 	/* I/OAT v3.2 platforms */
41c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF0) },
42c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF1) },
43c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF2) },
44c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF3) },
45c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF4) },
46c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF5) },
47c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF6) },
48c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF7) },
49c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF8) },
50c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF9) },
51c0f28ce6SDave Jiang 
52c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB0) },
53c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB1) },
54c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB2) },
55c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB3) },
56c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB4) },
57c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB5) },
58c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB6) },
59c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB7) },
60c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB8) },
61c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB9) },
62c0f28ce6SDave Jiang 
63c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB0) },
64c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB1) },
65c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB2) },
66c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB3) },
67c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB4) },
68c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB5) },
69c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB6) },
70c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB7) },
71c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB8) },
72c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB9) },
73c0f28ce6SDave Jiang 
74c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW0) },
75c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW1) },
76c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW2) },
77c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW3) },
78c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW4) },
79c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW5) },
80c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW6) },
81c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW7) },
82c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW8) },
83c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW9) },
84c0f28ce6SDave Jiang 
85ab98193dSDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX0) },
86ab98193dSDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX1) },
87ab98193dSDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX2) },
88ab98193dSDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX3) },
89ab98193dSDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX4) },
90ab98193dSDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX5) },
91ab98193dSDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX6) },
92ab98193dSDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX7) },
93ab98193dSDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX8) },
94ab98193dSDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX9) },
95ab98193dSDave Jiang 
961594c18fSDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SKX) },
971594c18fSDave Jiang 
98c0f28ce6SDave Jiang 	/* I/OAT v3.3 platforms */
99c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BWD0) },
100c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BWD1) },
101c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BWD2) },
102c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BWD3) },
103c0f28ce6SDave Jiang 
104c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDXDE0) },
105c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDXDE1) },
106c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDXDE2) },
107c0f28ce6SDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDXDE3) },
108c0f28ce6SDave Jiang 
1094d75873fSDave Jiang 	/* I/OAT v3.4 platforms */
1104d75873fSDave Jiang 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_ICX) },
1114d75873fSDave Jiang 
112c0f28ce6SDave Jiang 	{ 0, }
113c0f28ce6SDave Jiang };
114c0f28ce6SDave Jiang MODULE_DEVICE_TABLE(pci, ioat_pci_tbl);
115c0f28ce6SDave Jiang 
116c0f28ce6SDave Jiang static int ioat_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id);
117c0f28ce6SDave Jiang static void ioat_remove(struct pci_dev *pdev);
118599d49deSDave Jiang static void
119599d49deSDave Jiang ioat_init_channel(struct ioatdma_device *ioat_dma,
120599d49deSDave Jiang 		  struct ioatdma_chan *ioat_chan, int idx);
121ef97bd0fSDave Jiang static void ioat_intr_quirk(struct ioatdma_device *ioat_dma);
122f4d34aa8SRami Rosen static void ioat_enumerate_channels(struct ioatdma_device *ioat_dma);
123ef97bd0fSDave Jiang static int ioat3_dma_self_test(struct ioatdma_device *ioat_dma);
124c0f28ce6SDave Jiang 
125c0f28ce6SDave Jiang static int ioat_dca_enabled = 1;
126c0f28ce6SDave Jiang module_param(ioat_dca_enabled, int, 0644);
127c0f28ce6SDave Jiang MODULE_PARM_DESC(ioat_dca_enabled, "control support of dca service (default: 1)");
128e0100d40SDave Jiang int ioat_pending_level = 7;
129c0f28ce6SDave Jiang module_param(ioat_pending_level, int, 0644);
130c0f28ce6SDave Jiang MODULE_PARM_DESC(ioat_pending_level,
131e0100d40SDave Jiang 		 "high-water mark for pushing ioat descriptors (default: 7)");
132c0f28ce6SDave Jiang static char ioat_interrupt_style[32] = "msix";
133c0f28ce6SDave Jiang module_param_string(ioat_interrupt_style, ioat_interrupt_style,
134c0f28ce6SDave Jiang 		    sizeof(ioat_interrupt_style), 0644);
135c0f28ce6SDave Jiang MODULE_PARM_DESC(ioat_interrupt_style,
136c0f28ce6SDave Jiang 		 "set ioat interrupt style: msix (default), msi, intx");
137c0f28ce6SDave Jiang 
138c0f28ce6SDave Jiang struct kmem_cache *ioat_cache;
139c0f28ce6SDave Jiang struct kmem_cache *ioat_sed_cache;
140c0f28ce6SDave Jiang 
141c0f28ce6SDave Jiang static bool is_jf_ioat(struct pci_dev *pdev)
142c0f28ce6SDave Jiang {
143c0f28ce6SDave Jiang 	switch (pdev->device) {
144c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_JSF0:
145c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_JSF1:
146c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_JSF2:
147c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_JSF3:
148c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_JSF4:
149c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_JSF5:
150c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_JSF6:
151c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_JSF7:
152c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_JSF8:
153c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_JSF9:
154c0f28ce6SDave Jiang 		return true;
155c0f28ce6SDave Jiang 	default:
156c0f28ce6SDave Jiang 		return false;
157c0f28ce6SDave Jiang 	}
158c0f28ce6SDave Jiang }
159c0f28ce6SDave Jiang 
160c0f28ce6SDave Jiang static bool is_snb_ioat(struct pci_dev *pdev)
161c0f28ce6SDave Jiang {
162c0f28ce6SDave Jiang 	switch (pdev->device) {
163c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_SNB0:
164c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_SNB1:
165c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_SNB2:
166c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_SNB3:
167c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_SNB4:
168c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_SNB5:
169c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_SNB6:
170c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_SNB7:
171c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_SNB8:
172c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_SNB9:
173c0f28ce6SDave Jiang 		return true;
174c0f28ce6SDave Jiang 	default:
175c0f28ce6SDave Jiang 		return false;
176c0f28ce6SDave Jiang 	}
177c0f28ce6SDave Jiang }
178c0f28ce6SDave Jiang 
179c0f28ce6SDave Jiang static bool is_ivb_ioat(struct pci_dev *pdev)
180c0f28ce6SDave Jiang {
181c0f28ce6SDave Jiang 	switch (pdev->device) {
182c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_IVB0:
183c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_IVB1:
184c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_IVB2:
185c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_IVB3:
186c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_IVB4:
187c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_IVB5:
188c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_IVB6:
189c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_IVB7:
190c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_IVB8:
191c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_IVB9:
192c0f28ce6SDave Jiang 		return true;
193c0f28ce6SDave Jiang 	default:
194c0f28ce6SDave Jiang 		return false;
195c0f28ce6SDave Jiang 	}
196c0f28ce6SDave Jiang 
197c0f28ce6SDave Jiang }
198c0f28ce6SDave Jiang 
199c0f28ce6SDave Jiang static bool is_hsw_ioat(struct pci_dev *pdev)
200c0f28ce6SDave Jiang {
201c0f28ce6SDave Jiang 	switch (pdev->device) {
202c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_HSW0:
203c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_HSW1:
204c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_HSW2:
205c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_HSW3:
206c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_HSW4:
207c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_HSW5:
208c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_HSW6:
209c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_HSW7:
210c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_HSW8:
211c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_HSW9:
212c0f28ce6SDave Jiang 		return true;
213c0f28ce6SDave Jiang 	default:
214c0f28ce6SDave Jiang 		return false;
215c0f28ce6SDave Jiang 	}
216c0f28ce6SDave Jiang 
217c0f28ce6SDave Jiang }
218c0f28ce6SDave Jiang 
219ab98193dSDave Jiang static bool is_bdx_ioat(struct pci_dev *pdev)
220ab98193dSDave Jiang {
221ab98193dSDave Jiang 	switch (pdev->device) {
222ab98193dSDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_BDX0:
223ab98193dSDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_BDX1:
224ab98193dSDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_BDX2:
225ab98193dSDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_BDX3:
226ab98193dSDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_BDX4:
227ab98193dSDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_BDX5:
228ab98193dSDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_BDX6:
229ab98193dSDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_BDX7:
230ab98193dSDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_BDX8:
231ab98193dSDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_BDX9:
232ab98193dSDave Jiang 		return true;
233ab98193dSDave Jiang 	default:
234ab98193dSDave Jiang 		return false;
235ab98193dSDave Jiang 	}
236ab98193dSDave Jiang }
237ab98193dSDave Jiang 
2381594c18fSDave Jiang static inline bool is_skx_ioat(struct pci_dev *pdev)
2391594c18fSDave Jiang {
2401594c18fSDave Jiang 	return (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_SKX) ? true : false;
2411594c18fSDave Jiang }
2421594c18fSDave Jiang 
243c0f28ce6SDave Jiang static bool is_xeon_cb32(struct pci_dev *pdev)
244c0f28ce6SDave Jiang {
245c0f28ce6SDave Jiang 	return is_jf_ioat(pdev) || is_snb_ioat(pdev) || is_ivb_ioat(pdev) ||
2461594c18fSDave Jiang 		is_hsw_ioat(pdev) || is_bdx_ioat(pdev) || is_skx_ioat(pdev);
247c0f28ce6SDave Jiang }
248c0f28ce6SDave Jiang 
249c0f28ce6SDave Jiang bool is_bwd_ioat(struct pci_dev *pdev)
250c0f28ce6SDave Jiang {
251c0f28ce6SDave Jiang 	switch (pdev->device) {
252c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_BWD0:
253c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_BWD1:
254c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_BWD2:
255c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_BWD3:
256c0f28ce6SDave Jiang 	/* even though not Atom, BDX-DE has same DMA silicon */
257c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_BDXDE0:
258c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_BDXDE1:
259c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_BDXDE2:
260c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_BDXDE3:
261c0f28ce6SDave Jiang 		return true;
262c0f28ce6SDave Jiang 	default:
263c0f28ce6SDave Jiang 		return false;
264c0f28ce6SDave Jiang 	}
265c0f28ce6SDave Jiang }
266c0f28ce6SDave Jiang 
267c0f28ce6SDave Jiang static bool is_bwd_noraid(struct pci_dev *pdev)
268c0f28ce6SDave Jiang {
269c0f28ce6SDave Jiang 	switch (pdev->device) {
270c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_BWD2:
271c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_BWD3:
272c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_BDXDE0:
273c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_BDXDE1:
274c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_BDXDE2:
275c0f28ce6SDave Jiang 	case PCI_DEVICE_ID_INTEL_IOAT_BDXDE3:
276c0f28ce6SDave Jiang 		return true;
277c0f28ce6SDave Jiang 	default:
278c0f28ce6SDave Jiang 		return false;
279c0f28ce6SDave Jiang 	}
280c0f28ce6SDave Jiang 
281c0f28ce6SDave Jiang }
282c0f28ce6SDave Jiang 
283c0f28ce6SDave Jiang /*
284c0f28ce6SDave Jiang  * Perform a IOAT transaction to verify the HW works.
285c0f28ce6SDave Jiang  */
286c0f28ce6SDave Jiang #define IOAT_TEST_SIZE 2000
287c0f28ce6SDave Jiang 
288c0f28ce6SDave Jiang static void ioat_dma_test_callback(void *dma_async_param)
289c0f28ce6SDave Jiang {
290c0f28ce6SDave Jiang 	struct completion *cmp = dma_async_param;
291c0f28ce6SDave Jiang 
292c0f28ce6SDave Jiang 	complete(cmp);
293c0f28ce6SDave Jiang }
294c0f28ce6SDave Jiang 
295c0f28ce6SDave Jiang /**
296c0f28ce6SDave Jiang  * ioat_dma_self_test - Perform a IOAT transaction to verify the HW works.
297c0f28ce6SDave Jiang  * @ioat_dma: dma device to be tested
298c0f28ce6SDave Jiang  */
299599d49deSDave Jiang static int ioat_dma_self_test(struct ioatdma_device *ioat_dma)
300c0f28ce6SDave Jiang {
301c0f28ce6SDave Jiang 	int i;
302c0f28ce6SDave Jiang 	u8 *src;
303c0f28ce6SDave Jiang 	u8 *dest;
304c0f28ce6SDave Jiang 	struct dma_device *dma = &ioat_dma->dma_dev;
305c0f28ce6SDave Jiang 	struct device *dev = &ioat_dma->pdev->dev;
306c0f28ce6SDave Jiang 	struct dma_chan *dma_chan;
307c0f28ce6SDave Jiang 	struct dma_async_tx_descriptor *tx;
308c0f28ce6SDave Jiang 	dma_addr_t dma_dest, dma_src;
309c0f28ce6SDave Jiang 	dma_cookie_t cookie;
310c0f28ce6SDave Jiang 	int err = 0;
311c0f28ce6SDave Jiang 	struct completion cmp;
312c0f28ce6SDave Jiang 	unsigned long tmo;
313c0f28ce6SDave Jiang 	unsigned long flags;
314c0f28ce6SDave Jiang 
3156396bb22SKees Cook 	src = kzalloc(IOAT_TEST_SIZE, GFP_KERNEL);
316c0f28ce6SDave Jiang 	if (!src)
317c0f28ce6SDave Jiang 		return -ENOMEM;
3186396bb22SKees Cook 	dest = kzalloc(IOAT_TEST_SIZE, GFP_KERNEL);
319c0f28ce6SDave Jiang 	if (!dest) {
320c0f28ce6SDave Jiang 		kfree(src);
321c0f28ce6SDave Jiang 		return -ENOMEM;
322c0f28ce6SDave Jiang 	}
323c0f28ce6SDave Jiang 
324c0f28ce6SDave Jiang 	/* Fill in src buffer */
325c0f28ce6SDave Jiang 	for (i = 0; i < IOAT_TEST_SIZE; i++)
326c0f28ce6SDave Jiang 		src[i] = (u8)i;
327c0f28ce6SDave Jiang 
328c0f28ce6SDave Jiang 	/* Start copy, using first DMA channel */
329c0f28ce6SDave Jiang 	dma_chan = container_of(dma->channels.next, struct dma_chan,
330c0f28ce6SDave Jiang 				device_node);
331c0f28ce6SDave Jiang 	if (dma->device_alloc_chan_resources(dma_chan) < 1) {
332c0f28ce6SDave Jiang 		dev_err(dev, "selftest cannot allocate chan resource\n");
333c0f28ce6SDave Jiang 		err = -ENODEV;
334c0f28ce6SDave Jiang 		goto out;
335c0f28ce6SDave Jiang 	}
336c0f28ce6SDave Jiang 
337c0f28ce6SDave Jiang 	dma_src = dma_map_single(dev, src, IOAT_TEST_SIZE, DMA_TO_DEVICE);
338c0f28ce6SDave Jiang 	if (dma_mapping_error(dev, dma_src)) {
339c0f28ce6SDave Jiang 		dev_err(dev, "mapping src buffer failed\n");
340b424d2a0SPan Bian 		err = -ENOMEM;
341c0f28ce6SDave Jiang 		goto free_resources;
342c0f28ce6SDave Jiang 	}
343c0f28ce6SDave Jiang 	dma_dest = dma_map_single(dev, dest, IOAT_TEST_SIZE, DMA_FROM_DEVICE);
344c0f28ce6SDave Jiang 	if (dma_mapping_error(dev, dma_dest)) {
345c0f28ce6SDave Jiang 		dev_err(dev, "mapping dest buffer failed\n");
346b424d2a0SPan Bian 		err = -ENOMEM;
347c0f28ce6SDave Jiang 		goto unmap_src;
348c0f28ce6SDave Jiang 	}
349c0f28ce6SDave Jiang 	flags = DMA_PREP_INTERRUPT;
350c0f28ce6SDave Jiang 	tx = ioat_dma->dma_dev.device_prep_dma_memcpy(dma_chan, dma_dest,
351c0f28ce6SDave Jiang 						      dma_src, IOAT_TEST_SIZE,
352c0f28ce6SDave Jiang 						      flags);
353c0f28ce6SDave Jiang 	if (!tx) {
354c0f28ce6SDave Jiang 		dev_err(dev, "Self-test prep failed, disabling\n");
355c0f28ce6SDave Jiang 		err = -ENODEV;
356c0f28ce6SDave Jiang 		goto unmap_dma;
357c0f28ce6SDave Jiang 	}
358c0f28ce6SDave Jiang 
359c0f28ce6SDave Jiang 	async_tx_ack(tx);
360c0f28ce6SDave Jiang 	init_completion(&cmp);
361c0f28ce6SDave Jiang 	tx->callback = ioat_dma_test_callback;
362c0f28ce6SDave Jiang 	tx->callback_param = &cmp;
363c0f28ce6SDave Jiang 	cookie = tx->tx_submit(tx);
364c0f28ce6SDave Jiang 	if (cookie < 0) {
365c0f28ce6SDave Jiang 		dev_err(dev, "Self-test setup failed, disabling\n");
366c0f28ce6SDave Jiang 		err = -ENODEV;
367c0f28ce6SDave Jiang 		goto unmap_dma;
368c0f28ce6SDave Jiang 	}
369c0f28ce6SDave Jiang 	dma->device_issue_pending(dma_chan);
370c0f28ce6SDave Jiang 
371c0f28ce6SDave Jiang 	tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
372c0f28ce6SDave Jiang 
373c0f28ce6SDave Jiang 	if (tmo == 0 ||
374c0f28ce6SDave Jiang 	    dma->device_tx_status(dma_chan, cookie, NULL)
375c0f28ce6SDave Jiang 					!= DMA_COMPLETE) {
376c0f28ce6SDave Jiang 		dev_err(dev, "Self-test copy timed out, disabling\n");
377c0f28ce6SDave Jiang 		err = -ENODEV;
378c0f28ce6SDave Jiang 		goto unmap_dma;
379c0f28ce6SDave Jiang 	}
380c0f28ce6SDave Jiang 	if (memcmp(src, dest, IOAT_TEST_SIZE)) {
381c0f28ce6SDave Jiang 		dev_err(dev, "Self-test copy failed compare, disabling\n");
382c0f28ce6SDave Jiang 		err = -ENODEV;
3835c9afbdaSChristophe JAILLET 		goto unmap_dma;
384c0f28ce6SDave Jiang 	}
385c0f28ce6SDave Jiang 
386c0f28ce6SDave Jiang unmap_dma:
387c0f28ce6SDave Jiang 	dma_unmap_single(dev, dma_dest, IOAT_TEST_SIZE, DMA_FROM_DEVICE);
388c0f28ce6SDave Jiang unmap_src:
389c0f28ce6SDave Jiang 	dma_unmap_single(dev, dma_src, IOAT_TEST_SIZE, DMA_TO_DEVICE);
390c0f28ce6SDave Jiang free_resources:
391c0f28ce6SDave Jiang 	dma->device_free_chan_resources(dma_chan);
392c0f28ce6SDave Jiang out:
393c0f28ce6SDave Jiang 	kfree(src);
394c0f28ce6SDave Jiang 	kfree(dest);
395c0f28ce6SDave Jiang 	return err;
396c0f28ce6SDave Jiang }
397c0f28ce6SDave Jiang 
398c0f28ce6SDave Jiang /**
399c0f28ce6SDave Jiang  * ioat_dma_setup_interrupts - setup interrupt handler
400c0f28ce6SDave Jiang  * @ioat_dma: ioat dma device
401c0f28ce6SDave Jiang  */
402c0f28ce6SDave Jiang int ioat_dma_setup_interrupts(struct ioatdma_device *ioat_dma)
403c0f28ce6SDave Jiang {
404c0f28ce6SDave Jiang 	struct ioatdma_chan *ioat_chan;
405c0f28ce6SDave Jiang 	struct pci_dev *pdev = ioat_dma->pdev;
406c0f28ce6SDave Jiang 	struct device *dev = &pdev->dev;
407c0f28ce6SDave Jiang 	struct msix_entry *msix;
408c0f28ce6SDave Jiang 	int i, j, msixcnt;
409c0f28ce6SDave Jiang 	int err = -EINVAL;
410c0f28ce6SDave Jiang 	u8 intrctrl = 0;
411c0f28ce6SDave Jiang 
412c0f28ce6SDave Jiang 	if (!strcmp(ioat_interrupt_style, "msix"))
413c0f28ce6SDave Jiang 		goto msix;
414c0f28ce6SDave Jiang 	if (!strcmp(ioat_interrupt_style, "msi"))
415c0f28ce6SDave Jiang 		goto msi;
416c0f28ce6SDave Jiang 	if (!strcmp(ioat_interrupt_style, "intx"))
417c0f28ce6SDave Jiang 		goto intx;
418c0f28ce6SDave Jiang 	dev_err(dev, "invalid ioat_interrupt_style %s\n", ioat_interrupt_style);
419c0f28ce6SDave Jiang 	goto err_no_irq;
420c0f28ce6SDave Jiang 
421c0f28ce6SDave Jiang msix:
422c0f28ce6SDave Jiang 	/* The number of MSI-X vectors should equal the number of channels */
423f4f84fb6SYajun Deng 	msixcnt = ioat_dma->chancnt;
424c0f28ce6SDave Jiang 	for (i = 0; i < msixcnt; i++)
425c0f28ce6SDave Jiang 		ioat_dma->msix_entries[i].entry = i;
426c0f28ce6SDave Jiang 
427c0f28ce6SDave Jiang 	err = pci_enable_msix_exact(pdev, ioat_dma->msix_entries, msixcnt);
428c0f28ce6SDave Jiang 	if (err)
429c0f28ce6SDave Jiang 		goto msi;
430c0f28ce6SDave Jiang 
431c0f28ce6SDave Jiang 	for (i = 0; i < msixcnt; i++) {
432c0f28ce6SDave Jiang 		msix = &ioat_dma->msix_entries[i];
433c0f28ce6SDave Jiang 		ioat_chan = ioat_chan_by_index(ioat_dma, i);
434c0f28ce6SDave Jiang 		err = devm_request_irq(dev, msix->vector,
435c0f28ce6SDave Jiang 				       ioat_dma_do_interrupt_msix, 0,
436c0f28ce6SDave Jiang 				       "ioat-msix", ioat_chan);
437c0f28ce6SDave Jiang 		if (err) {
438c0f28ce6SDave Jiang 			for (j = 0; j < i; j++) {
439c0f28ce6SDave Jiang 				msix = &ioat_dma->msix_entries[j];
440c0f28ce6SDave Jiang 				ioat_chan = ioat_chan_by_index(ioat_dma, j);
441c0f28ce6SDave Jiang 				devm_free_irq(dev, msix->vector, ioat_chan);
442c0f28ce6SDave Jiang 			}
443c0f28ce6SDave Jiang 			goto msi;
444c0f28ce6SDave Jiang 		}
445c0f28ce6SDave Jiang 	}
446c0f28ce6SDave Jiang 	intrctrl |= IOAT_INTRCTRL_MSIX_VECTOR_CONTROL;
447c0f28ce6SDave Jiang 	ioat_dma->irq_mode = IOAT_MSIX;
448c0f28ce6SDave Jiang 	goto done;
449c0f28ce6SDave Jiang 
450c0f28ce6SDave Jiang msi:
451c0f28ce6SDave Jiang 	err = pci_enable_msi(pdev);
452c0f28ce6SDave Jiang 	if (err)
453c0f28ce6SDave Jiang 		goto intx;
454c0f28ce6SDave Jiang 
455c0f28ce6SDave Jiang 	err = devm_request_irq(dev, pdev->irq, ioat_dma_do_interrupt, 0,
456c0f28ce6SDave Jiang 			       "ioat-msi", ioat_dma);
457c0f28ce6SDave Jiang 	if (err) {
458c0f28ce6SDave Jiang 		pci_disable_msi(pdev);
459c0f28ce6SDave Jiang 		goto intx;
460c0f28ce6SDave Jiang 	}
461c0f28ce6SDave Jiang 	ioat_dma->irq_mode = IOAT_MSI;
462c0f28ce6SDave Jiang 	goto done;
463c0f28ce6SDave Jiang 
464c0f28ce6SDave Jiang intx:
465c0f28ce6SDave Jiang 	err = devm_request_irq(dev, pdev->irq, ioat_dma_do_interrupt,
466c0f28ce6SDave Jiang 			       IRQF_SHARED, "ioat-intx", ioat_dma);
467c0f28ce6SDave Jiang 	if (err)
468c0f28ce6SDave Jiang 		goto err_no_irq;
469c0f28ce6SDave Jiang 
470c0f28ce6SDave Jiang 	ioat_dma->irq_mode = IOAT_INTX;
471c0f28ce6SDave Jiang done:
472ef97bd0fSDave Jiang 	if (is_bwd_ioat(pdev))
473ef97bd0fSDave Jiang 		ioat_intr_quirk(ioat_dma);
474c0f28ce6SDave Jiang 	intrctrl |= IOAT_INTRCTRL_MASTER_INT_EN;
475c0f28ce6SDave Jiang 	writeb(intrctrl, ioat_dma->reg_base + IOAT_INTRCTRL_OFFSET);
476c0f28ce6SDave Jiang 	return 0;
477c0f28ce6SDave Jiang 
478c0f28ce6SDave Jiang err_no_irq:
479c0f28ce6SDave Jiang 	/* Disable all interrupt generation */
480c0f28ce6SDave Jiang 	writeb(0, ioat_dma->reg_base + IOAT_INTRCTRL_OFFSET);
481c0f28ce6SDave Jiang 	ioat_dma->irq_mode = IOAT_NOIRQ;
482c0f28ce6SDave Jiang 	dev_err(dev, "no usable interrupts\n");
483c0f28ce6SDave Jiang 	return err;
484c0f28ce6SDave Jiang }
485c0f28ce6SDave Jiang 
486c0f28ce6SDave Jiang static void ioat_disable_interrupts(struct ioatdma_device *ioat_dma)
487c0f28ce6SDave Jiang {
488c0f28ce6SDave Jiang 	/* Disable all interrupt generation */
489c0f28ce6SDave Jiang 	writeb(0, ioat_dma->reg_base + IOAT_INTRCTRL_OFFSET);
490c0f28ce6SDave Jiang }
491c0f28ce6SDave Jiang 
492599d49deSDave Jiang static int ioat_probe(struct ioatdma_device *ioat_dma)
493c0f28ce6SDave Jiang {
494c0f28ce6SDave Jiang 	int err = -ENODEV;
495c0f28ce6SDave Jiang 	struct dma_device *dma = &ioat_dma->dma_dev;
496c0f28ce6SDave Jiang 	struct pci_dev *pdev = ioat_dma->pdev;
497c0f28ce6SDave Jiang 	struct device *dev = &pdev->dev;
498c0f28ce6SDave Jiang 
499679cfbf7SDave Jiang 	ioat_dma->completion_pool = dma_pool_create("completion_pool", dev,
500c0f28ce6SDave Jiang 						    sizeof(u64),
501c0f28ce6SDave Jiang 						    SMP_CACHE_BYTES,
502c0f28ce6SDave Jiang 						    SMP_CACHE_BYTES);
503c0f28ce6SDave Jiang 
504c0f28ce6SDave Jiang 	if (!ioat_dma->completion_pool) {
505c0f28ce6SDave Jiang 		err = -ENOMEM;
506dd4645ebSDave Jiang 		goto err_out;
507c0f28ce6SDave Jiang 	}
508c0f28ce6SDave Jiang 
509ef97bd0fSDave Jiang 	ioat_enumerate_channels(ioat_dma);
510c0f28ce6SDave Jiang 
511c0f28ce6SDave Jiang 	dma_cap_set(DMA_MEMCPY, dma->cap_mask);
512c0f28ce6SDave Jiang 	dma->dev = &pdev->dev;
513c0f28ce6SDave Jiang 
514f4f84fb6SYajun Deng 	if (!ioat_dma->chancnt) {
515c0f28ce6SDave Jiang 		dev_err(dev, "channel enumeration error\n");
516c0f28ce6SDave Jiang 		goto err_setup_interrupts;
517c0f28ce6SDave Jiang 	}
518c0f28ce6SDave Jiang 
519c0f28ce6SDave Jiang 	err = ioat_dma_setup_interrupts(ioat_dma);
520c0f28ce6SDave Jiang 	if (err)
521c0f28ce6SDave Jiang 		goto err_setup_interrupts;
522c0f28ce6SDave Jiang 
523ef97bd0fSDave Jiang 	err = ioat3_dma_self_test(ioat_dma);
524c0f28ce6SDave Jiang 	if (err)
525c0f28ce6SDave Jiang 		goto err_self_test;
526c0f28ce6SDave Jiang 
527c0f28ce6SDave Jiang 	return 0;
528c0f28ce6SDave Jiang 
529c0f28ce6SDave Jiang err_self_test:
530c0f28ce6SDave Jiang 	ioat_disable_interrupts(ioat_dma);
531c0f28ce6SDave Jiang err_setup_interrupts:
532679cfbf7SDave Jiang 	dma_pool_destroy(ioat_dma->completion_pool);
533dd4645ebSDave Jiang err_out:
534c0f28ce6SDave Jiang 	return err;
535c0f28ce6SDave Jiang }
536c0f28ce6SDave Jiang 
537599d49deSDave Jiang static void ioat_dma_remove(struct ioatdma_device *ioat_dma)
538c0f28ce6SDave Jiang {
539c0f28ce6SDave Jiang 	struct dma_device *dma = &ioat_dma->dma_dev;
540c0f28ce6SDave Jiang 
541c0f28ce6SDave Jiang 	ioat_disable_interrupts(ioat_dma);
542c0f28ce6SDave Jiang 
543c0f28ce6SDave Jiang 	ioat_kobject_del(ioat_dma);
544c0f28ce6SDave Jiang 
545c0f28ce6SDave Jiang 	dma_async_device_unregister(dma);
546c0f28ce6SDave Jiang }
547c0f28ce6SDave Jiang 
548c0f28ce6SDave Jiang /**
549c0f28ce6SDave Jiang  * ioat_enumerate_channels - find and initialize the device's channels
550c0f28ce6SDave Jiang  * @ioat_dma: the ioat dma device to be enumerated
551c0f28ce6SDave Jiang  */
552f4d34aa8SRami Rosen static void ioat_enumerate_channels(struct ioatdma_device *ioat_dma)
553c0f28ce6SDave Jiang {
554c0f28ce6SDave Jiang 	struct ioatdma_chan *ioat_chan;
555c0f28ce6SDave Jiang 	struct device *dev = &ioat_dma->pdev->dev;
556c0f28ce6SDave Jiang 	struct dma_device *dma = &ioat_dma->dma_dev;
557c0f28ce6SDave Jiang 	u8 xfercap_log;
558f4f84fb6SYajun Deng 	int chancnt;
559c0f28ce6SDave Jiang 	int i;
560c0f28ce6SDave Jiang 
561c0f28ce6SDave Jiang 	INIT_LIST_HEAD(&dma->channels);
562f4f84fb6SYajun Deng 	chancnt = readb(ioat_dma->reg_base + IOAT_CHANCNT_OFFSET);
563f4f84fb6SYajun Deng 	chancnt &= 0x1f; /* bits [4:0] valid */
564f4f84fb6SYajun Deng 	if (chancnt > ARRAY_SIZE(ioat_dma->idx)) {
565c0f28ce6SDave Jiang 		dev_warn(dev, "(%d) exceeds max supported channels (%zu)\n",
566f4f84fb6SYajun Deng 			 chancnt, ARRAY_SIZE(ioat_dma->idx));
567f4f84fb6SYajun Deng 		chancnt = ARRAY_SIZE(ioat_dma->idx);
568c0f28ce6SDave Jiang 	}
569c0f28ce6SDave Jiang 	xfercap_log = readb(ioat_dma->reg_base + IOAT_XFERCAP_OFFSET);
570c0f28ce6SDave Jiang 	xfercap_log &= 0x1f; /* bits [4:0] valid */
571c0f28ce6SDave Jiang 	if (xfercap_log == 0)
572f4d34aa8SRami Rosen 		return;
573c0f28ce6SDave Jiang 	dev_dbg(dev, "%s: xfercap = %d\n", __func__, 1 << xfercap_log);
574c0f28ce6SDave Jiang 
575f4f84fb6SYajun Deng 	for (i = 0; i < chancnt; i++) {
576bf453a0aSLogan Gunthorpe 		ioat_chan = kzalloc(sizeof(*ioat_chan), GFP_KERNEL);
577c0f28ce6SDave Jiang 		if (!ioat_chan)
578c0f28ce6SDave Jiang 			break;
579c0f28ce6SDave Jiang 
580c0f28ce6SDave Jiang 		ioat_init_channel(ioat_dma, ioat_chan, i);
581c0f28ce6SDave Jiang 		ioat_chan->xfercap_log = xfercap_log;
582c0f28ce6SDave Jiang 		spin_lock_init(&ioat_chan->prep_lock);
583ef97bd0fSDave Jiang 		if (ioat_reset_hw(ioat_chan)) {
584c0f28ce6SDave Jiang 			i = 0;
585c0f28ce6SDave Jiang 			break;
586c0f28ce6SDave Jiang 		}
587c0f28ce6SDave Jiang 	}
588f4f84fb6SYajun Deng 	ioat_dma->chancnt = i;
589c0f28ce6SDave Jiang }
590c0f28ce6SDave Jiang 
591c0f28ce6SDave Jiang /**
592c0f28ce6SDave Jiang  * ioat_free_chan_resources - release all the descriptors
593af49b016SLee Jones  * @c: the channel to be cleaned
594c0f28ce6SDave Jiang  */
595599d49deSDave Jiang static void ioat_free_chan_resources(struct dma_chan *c)
596c0f28ce6SDave Jiang {
597c0f28ce6SDave Jiang 	struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
598c0f28ce6SDave Jiang 	struct ioatdma_device *ioat_dma = ioat_chan->ioat_dma;
599c0f28ce6SDave Jiang 	struct ioat_ring_ent *desc;
600c0f28ce6SDave Jiang 	const int total_descs = 1 << ioat_chan->alloc_order;
601c0f28ce6SDave Jiang 	int descs;
602c0f28ce6SDave Jiang 	int i;
603c0f28ce6SDave Jiang 
604c0f28ce6SDave Jiang 	/* Before freeing channel resources first check
605c0f28ce6SDave Jiang 	 * if they have been previously allocated for this channel.
606c0f28ce6SDave Jiang 	 */
607c0f28ce6SDave Jiang 	if (!ioat_chan->ring)
608c0f28ce6SDave Jiang 		return;
609c0f28ce6SDave Jiang 
610c0f28ce6SDave Jiang 	ioat_stop(ioat_chan);
611bf453a0aSLogan Gunthorpe 
612bf453a0aSLogan Gunthorpe 	if (!test_bit(IOAT_CHAN_DOWN, &ioat_chan->state)) {
613ef97bd0fSDave Jiang 		ioat_reset_hw(ioat_chan);
614c0f28ce6SDave Jiang 
615528314b5SDave Jiang 		/* Put LTR to idle */
616528314b5SDave Jiang 		if (ioat_dma->version >= IOAT_VER_3_4)
617528314b5SDave Jiang 			writeb(IOAT_CHAN_LTR_SWSEL_IDLE,
618bf453a0aSLogan Gunthorpe 			       ioat_chan->reg_base +
619bf453a0aSLogan Gunthorpe 			       IOAT_CHAN_LTR_SWSEL_OFFSET);
620bf453a0aSLogan Gunthorpe 	}
621528314b5SDave Jiang 
622c0f28ce6SDave Jiang 	spin_lock_bh(&ioat_chan->cleanup_lock);
623c0f28ce6SDave Jiang 	spin_lock_bh(&ioat_chan->prep_lock);
624c0f28ce6SDave Jiang 	descs = ioat_ring_space(ioat_chan);
625c0f28ce6SDave Jiang 	dev_dbg(to_dev(ioat_chan), "freeing %d idle descriptors\n", descs);
626c0f28ce6SDave Jiang 	for (i = 0; i < descs; i++) {
627c0f28ce6SDave Jiang 		desc = ioat_get_ring_ent(ioat_chan, ioat_chan->head + i);
628c0f28ce6SDave Jiang 		ioat_free_ring_ent(desc, c);
629c0f28ce6SDave Jiang 	}
630c0f28ce6SDave Jiang 
631c0f28ce6SDave Jiang 	if (descs < total_descs)
632c0f28ce6SDave Jiang 		dev_err(to_dev(ioat_chan), "Freeing %d in use descriptors!\n",
633c0f28ce6SDave Jiang 			total_descs - descs);
634c0f28ce6SDave Jiang 
635c0f28ce6SDave Jiang 	for (i = 0; i < total_descs - descs; i++) {
636c0f28ce6SDave Jiang 		desc = ioat_get_ring_ent(ioat_chan, ioat_chan->tail + i);
637c0f28ce6SDave Jiang 		dump_desc_dbg(ioat_chan, desc);
638c0f28ce6SDave Jiang 		ioat_free_ring_ent(desc, c);
639c0f28ce6SDave Jiang 	}
640c0f28ce6SDave Jiang 
641dd4645ebSDave Jiang 	for (i = 0; i < ioat_chan->desc_chunks; i++) {
642bd2bf302SLeonid Ravich 		dma_free_coherent(to_dev(ioat_chan), IOAT_CHUNK_SIZE,
643dd4645ebSDave Jiang 				  ioat_chan->descs[i].virt,
644dd4645ebSDave Jiang 				  ioat_chan->descs[i].hw);
645dd4645ebSDave Jiang 		ioat_chan->descs[i].virt = NULL;
646dd4645ebSDave Jiang 		ioat_chan->descs[i].hw = 0;
647dd4645ebSDave Jiang 	}
648dd4645ebSDave Jiang 	ioat_chan->desc_chunks = 0;
649dd4645ebSDave Jiang 
650c0f28ce6SDave Jiang 	kfree(ioat_chan->ring);
651c0f28ce6SDave Jiang 	ioat_chan->ring = NULL;
652c0f28ce6SDave Jiang 	ioat_chan->alloc_order = 0;
653679cfbf7SDave Jiang 	dma_pool_free(ioat_dma->completion_pool, ioat_chan->completion,
654c0f28ce6SDave Jiang 		      ioat_chan->completion_dma);
655c0f28ce6SDave Jiang 	spin_unlock_bh(&ioat_chan->prep_lock);
656c0f28ce6SDave Jiang 	spin_unlock_bh(&ioat_chan->cleanup_lock);
657c0f28ce6SDave Jiang 
658c0f28ce6SDave Jiang 	ioat_chan->last_completion = 0;
659c0f28ce6SDave Jiang 	ioat_chan->completion_dma = 0;
660c0f28ce6SDave Jiang 	ioat_chan->dmacount = 0;
661c0f28ce6SDave Jiang }
662c0f28ce6SDave Jiang 
663c0f28ce6SDave Jiang /* ioat_alloc_chan_resources - allocate/initialize ioat descriptor ring
664c0f28ce6SDave Jiang  * @chan: channel to be initialized
665c0f28ce6SDave Jiang  */
666599d49deSDave Jiang static int ioat_alloc_chan_resources(struct dma_chan *c)
667c0f28ce6SDave Jiang {
668c0f28ce6SDave Jiang 	struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
669c0f28ce6SDave Jiang 	struct ioat_ring_ent **ring;
670c0f28ce6SDave Jiang 	u64 status;
671c0f28ce6SDave Jiang 	int order;
672c0f28ce6SDave Jiang 	int i = 0;
673c0f28ce6SDave Jiang 	u32 chanerr;
674c0f28ce6SDave Jiang 
675c0f28ce6SDave Jiang 	/* have we already been set up? */
676c0f28ce6SDave Jiang 	if (ioat_chan->ring)
677c0f28ce6SDave Jiang 		return 1 << ioat_chan->alloc_order;
678c0f28ce6SDave Jiang 
679c0f28ce6SDave Jiang 	/* Setup register to interrupt and write completion status on error */
680c0f28ce6SDave Jiang 	writew(IOAT_CHANCTRL_RUN, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
681c0f28ce6SDave Jiang 
682c0f28ce6SDave Jiang 	/* allocate a completion writeback area */
683c0f28ce6SDave Jiang 	/* doing 2 32bit writes to mmio since 1 64b write doesn't work */
684c0f28ce6SDave Jiang 	ioat_chan->completion =
685305697faSJulia Lawall 		dma_pool_zalloc(ioat_chan->ioat_dma->completion_pool,
68621d25f6aSKrister Johansen 				GFP_NOWAIT, &ioat_chan->completion_dma);
687c0f28ce6SDave Jiang 	if (!ioat_chan->completion)
688c0f28ce6SDave Jiang 		return -ENOMEM;
689c0f28ce6SDave Jiang 
690c0f28ce6SDave Jiang 	writel(((u64)ioat_chan->completion_dma) & 0x00000000FFFFFFFF,
691c0f28ce6SDave Jiang 	       ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
692c0f28ce6SDave Jiang 	writel(((u64)ioat_chan->completion_dma) >> 32,
693c0f28ce6SDave Jiang 	       ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
694c0f28ce6SDave Jiang 
695cd60cd96SDave Jiang 	order = IOAT_MAX_ORDER;
69621d25f6aSKrister Johansen 	ring = ioat_alloc_ring(c, order, GFP_NOWAIT);
697c0f28ce6SDave Jiang 	if (!ring)
698c0f28ce6SDave Jiang 		return -ENOMEM;
699c0f28ce6SDave Jiang 
700c0f28ce6SDave Jiang 	spin_lock_bh(&ioat_chan->cleanup_lock);
701c0f28ce6SDave Jiang 	spin_lock_bh(&ioat_chan->prep_lock);
702c0f28ce6SDave Jiang 	ioat_chan->ring = ring;
703c0f28ce6SDave Jiang 	ioat_chan->head = 0;
704c0f28ce6SDave Jiang 	ioat_chan->issued = 0;
705c0f28ce6SDave Jiang 	ioat_chan->tail = 0;
706c0f28ce6SDave Jiang 	ioat_chan->alloc_order = order;
707c0f28ce6SDave Jiang 	set_bit(IOAT_RUN, &ioat_chan->state);
708c0f28ce6SDave Jiang 	spin_unlock_bh(&ioat_chan->prep_lock);
709c0f28ce6SDave Jiang 	spin_unlock_bh(&ioat_chan->cleanup_lock);
710c0f28ce6SDave Jiang 
711528314b5SDave Jiang 	/* Setting up LTR values for 3.4 or later */
712528314b5SDave Jiang 	if (ioat_chan->ioat_dma->version >= IOAT_VER_3_4) {
713528314b5SDave Jiang 		u32 lat_val;
714528314b5SDave Jiang 
715528314b5SDave Jiang 		lat_val = IOAT_CHAN_LTR_ACTIVE_SNVAL |
716528314b5SDave Jiang 			IOAT_CHAN_LTR_ACTIVE_SNLATSCALE |
717528314b5SDave Jiang 			IOAT_CHAN_LTR_ACTIVE_SNREQMNT;
718528314b5SDave Jiang 		writel(lat_val, ioat_chan->reg_base +
719528314b5SDave Jiang 				IOAT_CHAN_LTR_ACTIVE_OFFSET);
720528314b5SDave Jiang 
721528314b5SDave Jiang 		lat_val = IOAT_CHAN_LTR_IDLE_SNVAL |
722528314b5SDave Jiang 			  IOAT_CHAN_LTR_IDLE_SNLATSCALE |
723528314b5SDave Jiang 			  IOAT_CHAN_LTR_IDLE_SNREQMNT;
724528314b5SDave Jiang 		writel(lat_val, ioat_chan->reg_base +
725528314b5SDave Jiang 				IOAT_CHAN_LTR_IDLE_OFFSET);
726528314b5SDave Jiang 
727528314b5SDave Jiang 		/* Select to active */
728528314b5SDave Jiang 		writeb(IOAT_CHAN_LTR_SWSEL_ACTIVE,
729528314b5SDave Jiang 		       ioat_chan->reg_base +
730528314b5SDave Jiang 		       IOAT_CHAN_LTR_SWSEL_OFFSET);
731528314b5SDave Jiang 	}
732528314b5SDave Jiang 
733c0f28ce6SDave Jiang 	ioat_start_null_desc(ioat_chan);
734c0f28ce6SDave Jiang 
735c0f28ce6SDave Jiang 	/* check that we got off the ground */
736c0f28ce6SDave Jiang 	do {
737c0f28ce6SDave Jiang 		udelay(1);
738c0f28ce6SDave Jiang 		status = ioat_chansts(ioat_chan);
739c0f28ce6SDave Jiang 	} while (i++ < 20 && !is_ioat_active(status) && !is_ioat_idle(status));
740c0f28ce6SDave Jiang 
741c0f28ce6SDave Jiang 	if (is_ioat_active(status) || is_ioat_idle(status))
742c0f28ce6SDave Jiang 		return 1 << ioat_chan->alloc_order;
743c0f28ce6SDave Jiang 
744c0f28ce6SDave Jiang 	chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
745c0f28ce6SDave Jiang 
746c0f28ce6SDave Jiang 	dev_WARN(to_dev(ioat_chan),
747c0f28ce6SDave Jiang 		 "failed to start channel chanerr: %#x\n", chanerr);
748c0f28ce6SDave Jiang 	ioat_free_chan_resources(c);
749c0f28ce6SDave Jiang 	return -EFAULT;
750c0f28ce6SDave Jiang }
751c0f28ce6SDave Jiang 
752c0f28ce6SDave Jiang /* common channel initialization */
753599d49deSDave Jiang static void
754c0f28ce6SDave Jiang ioat_init_channel(struct ioatdma_device *ioat_dma,
755c0f28ce6SDave Jiang 		  struct ioatdma_chan *ioat_chan, int idx)
756c0f28ce6SDave Jiang {
757c0f28ce6SDave Jiang 	struct dma_device *dma = &ioat_dma->dma_dev;
758c0f28ce6SDave Jiang 
759c0f28ce6SDave Jiang 	ioat_chan->ioat_dma = ioat_dma;
760c0f28ce6SDave Jiang 	ioat_chan->reg_base = ioat_dma->reg_base + (0x80 * (idx + 1));
761c0f28ce6SDave Jiang 	spin_lock_init(&ioat_chan->cleanup_lock);
762c0f28ce6SDave Jiang 	ioat_chan->dma_chan.device = dma;
763c0f28ce6SDave Jiang 	dma_cookie_init(&ioat_chan->dma_chan);
764c0f28ce6SDave Jiang 	list_add_tail(&ioat_chan->dma_chan.device_node, &dma->channels);
765c0f28ce6SDave Jiang 	ioat_dma->idx[idx] = ioat_chan;
766bcdc4bd3SKees Cook 	timer_setup(&ioat_chan->timer, ioat_timer_event, 0);
7673b8040deSAllen Pais 	tasklet_setup(&ioat_chan->cleanup_task, ioat_cleanup_event);
768c0f28ce6SDave Jiang }
769c0f28ce6SDave Jiang 
770c0f28ce6SDave Jiang #define IOAT_NUM_SRC_TEST 6 /* must be <= 8 */
771c0f28ce6SDave Jiang static int ioat_xor_val_self_test(struct ioatdma_device *ioat_dma)
772c0f28ce6SDave Jiang {
773c0f28ce6SDave Jiang 	int i, src_idx;
774c0f28ce6SDave Jiang 	struct page *dest;
775c0f28ce6SDave Jiang 	struct page *xor_srcs[IOAT_NUM_SRC_TEST];
776c0f28ce6SDave Jiang 	struct page *xor_val_srcs[IOAT_NUM_SRC_TEST + 1];
777c0f28ce6SDave Jiang 	dma_addr_t dma_srcs[IOAT_NUM_SRC_TEST + 1];
778c0f28ce6SDave Jiang 	dma_addr_t dest_dma;
779c0f28ce6SDave Jiang 	struct dma_async_tx_descriptor *tx;
780c0f28ce6SDave Jiang 	struct dma_chan *dma_chan;
781c0f28ce6SDave Jiang 	dma_cookie_t cookie;
782c0f28ce6SDave Jiang 	u8 cmp_byte = 0;
783c0f28ce6SDave Jiang 	u32 cmp_word;
784c0f28ce6SDave Jiang 	u32 xor_val_result;
785c0f28ce6SDave Jiang 	int err = 0;
786c0f28ce6SDave Jiang 	struct completion cmp;
787c0f28ce6SDave Jiang 	unsigned long tmo;
788c0f28ce6SDave Jiang 	struct device *dev = &ioat_dma->pdev->dev;
789c0f28ce6SDave Jiang 	struct dma_device *dma = &ioat_dma->dma_dev;
790c0f28ce6SDave Jiang 	u8 op = 0;
791c0f28ce6SDave Jiang 
792c0f28ce6SDave Jiang 	dev_dbg(dev, "%s\n", __func__);
793c0f28ce6SDave Jiang 
794c0f28ce6SDave Jiang 	if (!dma_has_cap(DMA_XOR, dma->cap_mask))
795c0f28ce6SDave Jiang 		return 0;
796c0f28ce6SDave Jiang 
797c0f28ce6SDave Jiang 	for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
798c0f28ce6SDave Jiang 		xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
799c0f28ce6SDave Jiang 		if (!xor_srcs[src_idx]) {
800c0f28ce6SDave Jiang 			while (src_idx--)
801c0f28ce6SDave Jiang 				__free_page(xor_srcs[src_idx]);
802c0f28ce6SDave Jiang 			return -ENOMEM;
803c0f28ce6SDave Jiang 		}
804c0f28ce6SDave Jiang 	}
805c0f28ce6SDave Jiang 
806c0f28ce6SDave Jiang 	dest = alloc_page(GFP_KERNEL);
807c0f28ce6SDave Jiang 	if (!dest) {
808c0f28ce6SDave Jiang 		while (src_idx--)
809c0f28ce6SDave Jiang 			__free_page(xor_srcs[src_idx]);
810c0f28ce6SDave Jiang 		return -ENOMEM;
811c0f28ce6SDave Jiang 	}
812c0f28ce6SDave Jiang 
813c0f28ce6SDave Jiang 	/* Fill in src buffers */
814c0f28ce6SDave Jiang 	for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
815c0f28ce6SDave Jiang 		u8 *ptr = page_address(xor_srcs[src_idx]);
816c0f28ce6SDave Jiang 
817c0f28ce6SDave Jiang 		for (i = 0; i < PAGE_SIZE; i++)
818c0f28ce6SDave Jiang 			ptr[i] = (1 << src_idx);
819c0f28ce6SDave Jiang 	}
820c0f28ce6SDave Jiang 
821c0f28ce6SDave Jiang 	for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++)
822c0f28ce6SDave Jiang 		cmp_byte ^= (u8) (1 << src_idx);
823c0f28ce6SDave Jiang 
824c0f28ce6SDave Jiang 	cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
825c0f28ce6SDave Jiang 			(cmp_byte << 8) | cmp_byte;
826c0f28ce6SDave Jiang 
827c0f28ce6SDave Jiang 	memset(page_address(dest), 0, PAGE_SIZE);
828c0f28ce6SDave Jiang 
829c0f28ce6SDave Jiang 	dma_chan = container_of(dma->channels.next, struct dma_chan,
830c0f28ce6SDave Jiang 				device_node);
831c0f28ce6SDave Jiang 	if (dma->device_alloc_chan_resources(dma_chan) < 1) {
832c0f28ce6SDave Jiang 		err = -ENODEV;
833c0f28ce6SDave Jiang 		goto out;
834c0f28ce6SDave Jiang 	}
835c0f28ce6SDave Jiang 
836c0f28ce6SDave Jiang 	/* test xor */
837c0f28ce6SDave Jiang 	op = IOAT_OP_XOR;
838c0f28ce6SDave Jiang 
839c0f28ce6SDave Jiang 	dest_dma = dma_map_page(dev, dest, 0, PAGE_SIZE, DMA_FROM_DEVICE);
8407393fca9SPan Bian 	if (dma_mapping_error(dev, dest_dma)) {
8417393fca9SPan Bian 		err = -ENOMEM;
8422eab9b1aSDave Jiang 		goto free_resources;
8437393fca9SPan Bian 	}
844c0f28ce6SDave Jiang 
845c0f28ce6SDave Jiang 	for (i = 0; i < IOAT_NUM_SRC_TEST; i++) {
846c0f28ce6SDave Jiang 		dma_srcs[i] = dma_map_page(dev, xor_srcs[i], 0, PAGE_SIZE,
847c0f28ce6SDave Jiang 					   DMA_TO_DEVICE);
8487393fca9SPan Bian 		if (dma_mapping_error(dev, dma_srcs[i])) {
8497393fca9SPan Bian 			err = -ENOMEM;
850c0f28ce6SDave Jiang 			goto dma_unmap;
851c0f28ce6SDave Jiang 		}
8527393fca9SPan Bian 	}
853c0f28ce6SDave Jiang 	tx = dma->device_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
854c0f28ce6SDave Jiang 				      IOAT_NUM_SRC_TEST, PAGE_SIZE,
855c0f28ce6SDave Jiang 				      DMA_PREP_INTERRUPT);
856c0f28ce6SDave Jiang 
857c0f28ce6SDave Jiang 	if (!tx) {
858c0f28ce6SDave Jiang 		dev_err(dev, "Self-test xor prep failed\n");
859c0f28ce6SDave Jiang 		err = -ENODEV;
860c0f28ce6SDave Jiang 		goto dma_unmap;
861c0f28ce6SDave Jiang 	}
862c0f28ce6SDave Jiang 
863c0f28ce6SDave Jiang 	async_tx_ack(tx);
864c0f28ce6SDave Jiang 	init_completion(&cmp);
8653372de58SDave Jiang 	tx->callback = ioat_dma_test_callback;
866c0f28ce6SDave Jiang 	tx->callback_param = &cmp;
867c0f28ce6SDave Jiang 	cookie = tx->tx_submit(tx);
868c0f28ce6SDave Jiang 	if (cookie < 0) {
869c0f28ce6SDave Jiang 		dev_err(dev, "Self-test xor setup failed\n");
870c0f28ce6SDave Jiang 		err = -ENODEV;
871c0f28ce6SDave Jiang 		goto dma_unmap;
872c0f28ce6SDave Jiang 	}
873c0f28ce6SDave Jiang 	dma->device_issue_pending(dma_chan);
874c0f28ce6SDave Jiang 
875c0f28ce6SDave Jiang 	tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
876c0f28ce6SDave Jiang 
877c0f28ce6SDave Jiang 	if (tmo == 0 ||
878c0f28ce6SDave Jiang 	    dma->device_tx_status(dma_chan, cookie, NULL) != DMA_COMPLETE) {
879c0f28ce6SDave Jiang 		dev_err(dev, "Self-test xor timed out\n");
880c0f28ce6SDave Jiang 		err = -ENODEV;
881c0f28ce6SDave Jiang 		goto dma_unmap;
882c0f28ce6SDave Jiang 	}
883c0f28ce6SDave Jiang 
884c0f28ce6SDave Jiang 	for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
885c0f28ce6SDave Jiang 		dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
886c0f28ce6SDave Jiang 
887c0f28ce6SDave Jiang 	dma_sync_single_for_cpu(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
888c0f28ce6SDave Jiang 	for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
889c0f28ce6SDave Jiang 		u32 *ptr = page_address(dest);
890c0f28ce6SDave Jiang 
891c0f28ce6SDave Jiang 		if (ptr[i] != cmp_word) {
892c0f28ce6SDave Jiang 			dev_err(dev, "Self-test xor failed compare\n");
893c0f28ce6SDave Jiang 			err = -ENODEV;
894c0f28ce6SDave Jiang 			goto free_resources;
895c0f28ce6SDave Jiang 		}
896c0f28ce6SDave Jiang 	}
897c0f28ce6SDave Jiang 	dma_sync_single_for_device(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
898c0f28ce6SDave Jiang 
899c0f28ce6SDave Jiang 	dma_unmap_page(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
900c0f28ce6SDave Jiang 
901c0f28ce6SDave Jiang 	/* skip validate if the capability is not present */
902c0f28ce6SDave Jiang 	if (!dma_has_cap(DMA_XOR_VAL, dma_chan->device->cap_mask))
903c0f28ce6SDave Jiang 		goto free_resources;
904c0f28ce6SDave Jiang 
905c0f28ce6SDave Jiang 	op = IOAT_OP_XOR_VAL;
906c0f28ce6SDave Jiang 
907c0f28ce6SDave Jiang 	/* validate the sources with the destintation page */
908c0f28ce6SDave Jiang 	for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
909c0f28ce6SDave Jiang 		xor_val_srcs[i] = xor_srcs[i];
910c0f28ce6SDave Jiang 	xor_val_srcs[i] = dest;
911c0f28ce6SDave Jiang 
912c0f28ce6SDave Jiang 	xor_val_result = 1;
913c0f28ce6SDave Jiang 
914c0f28ce6SDave Jiang 	for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++) {
915c0f28ce6SDave Jiang 		dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
916c0f28ce6SDave Jiang 					   DMA_TO_DEVICE);
9177393fca9SPan Bian 		if (dma_mapping_error(dev, dma_srcs[i])) {
9187393fca9SPan Bian 			err = -ENOMEM;
919c0f28ce6SDave Jiang 			goto dma_unmap;
920c0f28ce6SDave Jiang 		}
9217393fca9SPan Bian 	}
922c0f28ce6SDave Jiang 	tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
923c0f28ce6SDave Jiang 					  IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
924c0f28ce6SDave Jiang 					  &xor_val_result, DMA_PREP_INTERRUPT);
925c0f28ce6SDave Jiang 	if (!tx) {
926c0f28ce6SDave Jiang 		dev_err(dev, "Self-test zero prep failed\n");
927c0f28ce6SDave Jiang 		err = -ENODEV;
928c0f28ce6SDave Jiang 		goto dma_unmap;
929c0f28ce6SDave Jiang 	}
930c0f28ce6SDave Jiang 
931c0f28ce6SDave Jiang 	async_tx_ack(tx);
932c0f28ce6SDave Jiang 	init_completion(&cmp);
9333372de58SDave Jiang 	tx->callback = ioat_dma_test_callback;
934c0f28ce6SDave Jiang 	tx->callback_param = &cmp;
935c0f28ce6SDave Jiang 	cookie = tx->tx_submit(tx);
936c0f28ce6SDave Jiang 	if (cookie < 0) {
937c0f28ce6SDave Jiang 		dev_err(dev, "Self-test zero setup failed\n");
938c0f28ce6SDave Jiang 		err = -ENODEV;
939c0f28ce6SDave Jiang 		goto dma_unmap;
940c0f28ce6SDave Jiang 	}
941c0f28ce6SDave Jiang 	dma->device_issue_pending(dma_chan);
942c0f28ce6SDave Jiang 
943c0f28ce6SDave Jiang 	tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
944c0f28ce6SDave Jiang 
945c0f28ce6SDave Jiang 	if (tmo == 0 ||
946c0f28ce6SDave Jiang 	    dma->device_tx_status(dma_chan, cookie, NULL) != DMA_COMPLETE) {
947c0f28ce6SDave Jiang 		dev_err(dev, "Self-test validate timed out\n");
948c0f28ce6SDave Jiang 		err = -ENODEV;
949c0f28ce6SDave Jiang 		goto dma_unmap;
950c0f28ce6SDave Jiang 	}
951c0f28ce6SDave Jiang 
952c0f28ce6SDave Jiang 	for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
953c0f28ce6SDave Jiang 		dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
954c0f28ce6SDave Jiang 
955c0f28ce6SDave Jiang 	if (xor_val_result != 0) {
956c0f28ce6SDave Jiang 		dev_err(dev, "Self-test validate failed compare\n");
957c0f28ce6SDave Jiang 		err = -ENODEV;
958c0f28ce6SDave Jiang 		goto free_resources;
959c0f28ce6SDave Jiang 	}
960c0f28ce6SDave Jiang 
961c0f28ce6SDave Jiang 	memset(page_address(dest), 0, PAGE_SIZE);
962c0f28ce6SDave Jiang 
963c0f28ce6SDave Jiang 	/* test for non-zero parity sum */
964c0f28ce6SDave Jiang 	op = IOAT_OP_XOR_VAL;
965c0f28ce6SDave Jiang 
966c0f28ce6SDave Jiang 	xor_val_result = 0;
967c0f28ce6SDave Jiang 	for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++) {
968c0f28ce6SDave Jiang 		dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
969c0f28ce6SDave Jiang 					   DMA_TO_DEVICE);
9707393fca9SPan Bian 		if (dma_mapping_error(dev, dma_srcs[i])) {
9717393fca9SPan Bian 			err = -ENOMEM;
972c0f28ce6SDave Jiang 			goto dma_unmap;
973c0f28ce6SDave Jiang 		}
9747393fca9SPan Bian 	}
975c0f28ce6SDave Jiang 	tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
976c0f28ce6SDave Jiang 					  IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
977c0f28ce6SDave Jiang 					  &xor_val_result, DMA_PREP_INTERRUPT);
978c0f28ce6SDave Jiang 	if (!tx) {
979c0f28ce6SDave Jiang 		dev_err(dev, "Self-test 2nd zero prep failed\n");
980c0f28ce6SDave Jiang 		err = -ENODEV;
981c0f28ce6SDave Jiang 		goto dma_unmap;
982c0f28ce6SDave Jiang 	}
983c0f28ce6SDave Jiang 
984c0f28ce6SDave Jiang 	async_tx_ack(tx);
985c0f28ce6SDave Jiang 	init_completion(&cmp);
9863372de58SDave Jiang 	tx->callback = ioat_dma_test_callback;
987c0f28ce6SDave Jiang 	tx->callback_param = &cmp;
988c0f28ce6SDave Jiang 	cookie = tx->tx_submit(tx);
989c0f28ce6SDave Jiang 	if (cookie < 0) {
990c0f28ce6SDave Jiang 		dev_err(dev, "Self-test  2nd zero setup failed\n");
991c0f28ce6SDave Jiang 		err = -ENODEV;
992c0f28ce6SDave Jiang 		goto dma_unmap;
993c0f28ce6SDave Jiang 	}
994c0f28ce6SDave Jiang 	dma->device_issue_pending(dma_chan);
995c0f28ce6SDave Jiang 
996c0f28ce6SDave Jiang 	tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
997c0f28ce6SDave Jiang 
998c0f28ce6SDave Jiang 	if (tmo == 0 ||
999c0f28ce6SDave Jiang 	    dma->device_tx_status(dma_chan, cookie, NULL) != DMA_COMPLETE) {
1000c0f28ce6SDave Jiang 		dev_err(dev, "Self-test 2nd validate timed out\n");
1001c0f28ce6SDave Jiang 		err = -ENODEV;
1002c0f28ce6SDave Jiang 		goto dma_unmap;
1003c0f28ce6SDave Jiang 	}
1004c0f28ce6SDave Jiang 
1005c0f28ce6SDave Jiang 	if (xor_val_result != SUM_CHECK_P_RESULT) {
1006c0f28ce6SDave Jiang 		dev_err(dev, "Self-test validate failed compare\n");
1007c0f28ce6SDave Jiang 		err = -ENODEV;
1008c0f28ce6SDave Jiang 		goto dma_unmap;
1009c0f28ce6SDave Jiang 	}
1010c0f28ce6SDave Jiang 
1011c0f28ce6SDave Jiang 	for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1012c0f28ce6SDave Jiang 		dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
1013c0f28ce6SDave Jiang 
1014c0f28ce6SDave Jiang 	goto free_resources;
1015c0f28ce6SDave Jiang dma_unmap:
1016c0f28ce6SDave Jiang 	if (op == IOAT_OP_XOR) {
1017e4734b3fSChristoph Hellwig 		while (--i >= 0)
1018c0f28ce6SDave Jiang 			dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE,
1019c0f28ce6SDave Jiang 				       DMA_TO_DEVICE);
1020e4734b3fSChristoph Hellwig 		dma_unmap_page(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
1021c0f28ce6SDave Jiang 	} else if (op == IOAT_OP_XOR_VAL) {
1022e4734b3fSChristoph Hellwig 		while (--i >= 0)
1023c0f28ce6SDave Jiang 			dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE,
1024c0f28ce6SDave Jiang 				       DMA_TO_DEVICE);
1025c0f28ce6SDave Jiang 	}
1026c0f28ce6SDave Jiang free_resources:
1027c0f28ce6SDave Jiang 	dma->device_free_chan_resources(dma_chan);
1028c0f28ce6SDave Jiang out:
1029c0f28ce6SDave Jiang 	src_idx = IOAT_NUM_SRC_TEST;
1030c0f28ce6SDave Jiang 	while (src_idx--)
1031c0f28ce6SDave Jiang 		__free_page(xor_srcs[src_idx]);
1032c0f28ce6SDave Jiang 	__free_page(dest);
1033c0f28ce6SDave Jiang 	return err;
1034c0f28ce6SDave Jiang }
1035c0f28ce6SDave Jiang 
1036c0f28ce6SDave Jiang static int ioat3_dma_self_test(struct ioatdma_device *ioat_dma)
1037c0f28ce6SDave Jiang {
103864f1d0ffSDave Jiang 	int rc;
1039c0f28ce6SDave Jiang 
104064f1d0ffSDave Jiang 	rc = ioat_dma_self_test(ioat_dma);
1041c0f28ce6SDave Jiang 	if (rc)
1042c0f28ce6SDave Jiang 		return rc;
1043c0f28ce6SDave Jiang 
1044c0f28ce6SDave Jiang 	rc = ioat_xor_val_self_test(ioat_dma);
1045c0f28ce6SDave Jiang 
104664f1d0ffSDave Jiang 	return rc;
1047c0f28ce6SDave Jiang }
1048c0f28ce6SDave Jiang 
10493372de58SDave Jiang static void ioat_intr_quirk(struct ioatdma_device *ioat_dma)
1050c0f28ce6SDave Jiang {
1051c0f28ce6SDave Jiang 	struct dma_device *dma;
1052c0f28ce6SDave Jiang 	struct dma_chan *c;
1053c0f28ce6SDave Jiang 	struct ioatdma_chan *ioat_chan;
1054c0f28ce6SDave Jiang 	u32 errmask;
1055c0f28ce6SDave Jiang 
1056c0f28ce6SDave Jiang 	dma = &ioat_dma->dma_dev;
1057c0f28ce6SDave Jiang 
1058c0f28ce6SDave Jiang 	/*
1059c0f28ce6SDave Jiang 	 * if we have descriptor write back error status, we mask the
1060c0f28ce6SDave Jiang 	 * error interrupts
1061c0f28ce6SDave Jiang 	 */
1062c0f28ce6SDave Jiang 	if (ioat_dma->cap & IOAT_CAP_DWBES) {
1063c0f28ce6SDave Jiang 		list_for_each_entry(c, &dma->channels, device_node) {
1064c0f28ce6SDave Jiang 			ioat_chan = to_ioat_chan(c);
1065c0f28ce6SDave Jiang 			errmask = readl(ioat_chan->reg_base +
1066c0f28ce6SDave Jiang 					IOAT_CHANERR_MASK_OFFSET);
1067c0f28ce6SDave Jiang 			errmask |= IOAT_CHANERR_XOR_P_OR_CRC_ERR |
1068c0f28ce6SDave Jiang 				   IOAT_CHANERR_XOR_Q_ERR;
1069c0f28ce6SDave Jiang 			writel(errmask, ioat_chan->reg_base +
1070c0f28ce6SDave Jiang 					IOAT_CHANERR_MASK_OFFSET);
1071c0f28ce6SDave Jiang 		}
1072c0f28ce6SDave Jiang 	}
1073c0f28ce6SDave Jiang }
1074c0f28ce6SDave Jiang 
1075599d49deSDave Jiang static int ioat3_dma_probe(struct ioatdma_device *ioat_dma, int dca)
1076c0f28ce6SDave Jiang {
1077c0f28ce6SDave Jiang 	struct pci_dev *pdev = ioat_dma->pdev;
1078c0f28ce6SDave Jiang 	int dca_en = system_has_dca_enabled(pdev);
1079c0f28ce6SDave Jiang 	struct dma_device *dma;
1080c0f28ce6SDave Jiang 	struct dma_chan *c;
1081c0f28ce6SDave Jiang 	struct ioatdma_chan *ioat_chan;
1082c0f28ce6SDave Jiang 	int err;
1083511deae0SDave Jiang 	u16 val16;
1084c0f28ce6SDave Jiang 
1085c0f28ce6SDave Jiang 	dma = &ioat_dma->dma_dev;
1086c0f28ce6SDave Jiang 	dma->device_prep_dma_memcpy = ioat_dma_prep_memcpy_lock;
1087c0f28ce6SDave Jiang 	dma->device_issue_pending = ioat_issue_pending;
1088c0f28ce6SDave Jiang 	dma->device_alloc_chan_resources = ioat_alloc_chan_resources;
1089c0f28ce6SDave Jiang 	dma->device_free_chan_resources = ioat_free_chan_resources;
1090c0f28ce6SDave Jiang 
1091c0f28ce6SDave Jiang 	dma_cap_set(DMA_INTERRUPT, dma->cap_mask);
1092c0f28ce6SDave Jiang 	dma->device_prep_dma_interrupt = ioat_prep_interrupt_lock;
1093c0f28ce6SDave Jiang 
1094c0f28ce6SDave Jiang 	ioat_dma->cap = readl(ioat_dma->reg_base + IOAT_DMA_CAP_OFFSET);
1095c0f28ce6SDave Jiang 
1096c0f28ce6SDave Jiang 	if (is_xeon_cb32(pdev) || is_bwd_noraid(pdev))
1097c0f28ce6SDave Jiang 		ioat_dma->cap &=
1098c0f28ce6SDave Jiang 			~(IOAT_CAP_XOR | IOAT_CAP_PQ | IOAT_CAP_RAID16SS);
1099c0f28ce6SDave Jiang 
1100c0f28ce6SDave Jiang 	/* dca is incompatible with raid operations */
1101c0f28ce6SDave Jiang 	if (dca_en && (ioat_dma->cap & (IOAT_CAP_XOR|IOAT_CAP_PQ)))
1102c0f28ce6SDave Jiang 		ioat_dma->cap &= ~(IOAT_CAP_XOR|IOAT_CAP_PQ);
1103c0f28ce6SDave Jiang 
1104c0f28ce6SDave Jiang 	if (ioat_dma->cap & IOAT_CAP_XOR) {
1105c0f28ce6SDave Jiang 		dma->max_xor = 8;
1106c0f28ce6SDave Jiang 
1107c0f28ce6SDave Jiang 		dma_cap_set(DMA_XOR, dma->cap_mask);
1108c0f28ce6SDave Jiang 		dma->device_prep_dma_xor = ioat_prep_xor;
1109c0f28ce6SDave Jiang 
1110c0f28ce6SDave Jiang 		dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
1111c0f28ce6SDave Jiang 		dma->device_prep_dma_xor_val = ioat_prep_xor_val;
1112c0f28ce6SDave Jiang 	}
1113c0f28ce6SDave Jiang 
1114c0f28ce6SDave Jiang 	if (ioat_dma->cap & IOAT_CAP_PQ) {
1115c0f28ce6SDave Jiang 
1116c0f28ce6SDave Jiang 		dma->device_prep_dma_pq = ioat_prep_pq;
1117c0f28ce6SDave Jiang 		dma->device_prep_dma_pq_val = ioat_prep_pq_val;
1118c0f28ce6SDave Jiang 		dma_cap_set(DMA_PQ, dma->cap_mask);
1119c0f28ce6SDave Jiang 		dma_cap_set(DMA_PQ_VAL, dma->cap_mask);
1120c0f28ce6SDave Jiang 
1121c0f28ce6SDave Jiang 		if (ioat_dma->cap & IOAT_CAP_RAID16SS)
1122c0f28ce6SDave Jiang 			dma_set_maxpq(dma, 16, 0);
1123c0f28ce6SDave Jiang 		else
1124c0f28ce6SDave Jiang 			dma_set_maxpq(dma, 8, 0);
1125c0f28ce6SDave Jiang 
1126c0f28ce6SDave Jiang 		if (!(ioat_dma->cap & IOAT_CAP_XOR)) {
1127c0f28ce6SDave Jiang 			dma->device_prep_dma_xor = ioat_prep_pqxor;
1128c0f28ce6SDave Jiang 			dma->device_prep_dma_xor_val = ioat_prep_pqxor_val;
1129c0f28ce6SDave Jiang 			dma_cap_set(DMA_XOR, dma->cap_mask);
1130c0f28ce6SDave Jiang 			dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
1131c0f28ce6SDave Jiang 
1132c0f28ce6SDave Jiang 			if (ioat_dma->cap & IOAT_CAP_RAID16SS)
1133c0f28ce6SDave Jiang 				dma->max_xor = 16;
1134c0f28ce6SDave Jiang 			else
1135c0f28ce6SDave Jiang 				dma->max_xor = 8;
1136c0f28ce6SDave Jiang 		}
1137c0f28ce6SDave Jiang 	}
1138c0f28ce6SDave Jiang 
1139c0f28ce6SDave Jiang 	dma->device_tx_status = ioat_tx_status;
1140c0f28ce6SDave Jiang 
1141c0f28ce6SDave Jiang 	/* starting with CB3.3 super extended descriptors are supported */
1142c0f28ce6SDave Jiang 	if (ioat_dma->cap & IOAT_CAP_RAID16SS) {
1143c0f28ce6SDave Jiang 		char pool_name[14];
1144c0f28ce6SDave Jiang 		int i;
1145c0f28ce6SDave Jiang 
1146c0f28ce6SDave Jiang 		for (i = 0; i < MAX_SED_POOLS; i++) {
1147c0f28ce6SDave Jiang 			snprintf(pool_name, 14, "ioat_hw%d_sed", i);
1148c0f28ce6SDave Jiang 
1149c0f28ce6SDave Jiang 			/* allocate SED DMA pool */
1150c0f28ce6SDave Jiang 			ioat_dma->sed_hw_pool[i] = dmam_pool_create(pool_name,
1151c0f28ce6SDave Jiang 					&pdev->dev,
1152c0f28ce6SDave Jiang 					SED_SIZE * (i + 1), 64, 0);
1153c0f28ce6SDave Jiang 			if (!ioat_dma->sed_hw_pool[i])
1154c0f28ce6SDave Jiang 				return -ENOMEM;
1155c0f28ce6SDave Jiang 
1156c0f28ce6SDave Jiang 		}
1157c0f28ce6SDave Jiang 	}
1158c0f28ce6SDave Jiang 
1159c0f28ce6SDave Jiang 	if (!(ioat_dma->cap & (IOAT_CAP_XOR | IOAT_CAP_PQ)))
1160c0f28ce6SDave Jiang 		dma_cap_set(DMA_PRIVATE, dma->cap_mask);
1161c0f28ce6SDave Jiang 
1162c0f28ce6SDave Jiang 	err = ioat_probe(ioat_dma);
1163c0f28ce6SDave Jiang 	if (err)
1164c0f28ce6SDave Jiang 		return err;
1165c0f28ce6SDave Jiang 
1166c0f28ce6SDave Jiang 	list_for_each_entry(c, &dma->channels, device_node) {
1167c0f28ce6SDave Jiang 		ioat_chan = to_ioat_chan(c);
1168c0f28ce6SDave Jiang 		writel(IOAT_DMA_DCA_ANY_CPU,
1169c0f28ce6SDave Jiang 		       ioat_chan->reg_base + IOAT_DCACTRL_OFFSET);
1170c0f28ce6SDave Jiang 	}
1171c0f28ce6SDave Jiang 
1172*fb6e3d4dSNikita Shubin 	err = dma_async_device_register(&ioat_dma->dma_dev);
1173c0f28ce6SDave Jiang 	if (err)
1174*fb6e3d4dSNikita Shubin 		goto err_disable_interrupts;
1175c0f28ce6SDave Jiang 
1176c0f28ce6SDave Jiang 	ioat_kobject_add(ioat_dma, &ioat_ktype);
1177c0f28ce6SDave Jiang 
1178c0f28ce6SDave Jiang 	if (dca)
11793372de58SDave Jiang 		ioat_dma->dca = ioat_dca_init(pdev, ioat_dma->reg_base);
1180c0f28ce6SDave Jiang 
1181511deae0SDave Jiang 	/* disable relaxed ordering */
11828f6707d0SBjorn Helgaas 	err = pcie_capability_read_word(pdev, PCI_EXP_DEVCTL, &val16);
1183*fb6e3d4dSNikita Shubin 	if (err) {
1184*fb6e3d4dSNikita Shubin 		err = pcibios_err_to_errno(err);
1185*fb6e3d4dSNikita Shubin 		goto err_disable_interrupts;
1186*fb6e3d4dSNikita Shubin 	}
1187511deae0SDave Jiang 
1188511deae0SDave Jiang 	/* clear relaxed ordering enable */
11898f6707d0SBjorn Helgaas 	val16 &= ~PCI_EXP_DEVCTL_RELAX_EN;
11908f6707d0SBjorn Helgaas 	err = pcie_capability_write_word(pdev, PCI_EXP_DEVCTL, val16);
1191*fb6e3d4dSNikita Shubin 	if (err) {
1192*fb6e3d4dSNikita Shubin 		err = pcibios_err_to_errno(err);
1193*fb6e3d4dSNikita Shubin 		goto err_disable_interrupts;
1194*fb6e3d4dSNikita Shubin 	}
1195511deae0SDave Jiang 
1196e0100d40SDave Jiang 	if (ioat_dma->cap & IOAT_CAP_DPS)
1197e0100d40SDave Jiang 		writeb(ioat_pending_level + 1,
1198e0100d40SDave Jiang 		       ioat_dma->reg_base + IOAT_PREFETCH_LIMIT_OFFSET);
1199e0100d40SDave Jiang 
1200c0f28ce6SDave Jiang 	return 0;
1201*fb6e3d4dSNikita Shubin 
1202*fb6e3d4dSNikita Shubin err_disable_interrupts:
1203*fb6e3d4dSNikita Shubin 	ioat_disable_interrupts(ioat_dma);
1204*fb6e3d4dSNikita Shubin 	dma_pool_destroy(ioat_dma->completion_pool);
1205*fb6e3d4dSNikita Shubin 	return err;
1206c0f28ce6SDave Jiang }
1207c0f28ce6SDave Jiang 
1208ad4a7b50SDave Jiang static void ioat_shutdown(struct pci_dev *pdev)
1209ad4a7b50SDave Jiang {
1210ad4a7b50SDave Jiang 	struct ioatdma_device *ioat_dma = pci_get_drvdata(pdev);
1211ad4a7b50SDave Jiang 	struct ioatdma_chan *ioat_chan;
1212ad4a7b50SDave Jiang 	int i;
1213ad4a7b50SDave Jiang 
1214ad4a7b50SDave Jiang 	if (!ioat_dma)
1215ad4a7b50SDave Jiang 		return;
1216ad4a7b50SDave Jiang 
1217ad4a7b50SDave Jiang 	for (i = 0; i < IOAT_MAX_CHANS; i++) {
1218ad4a7b50SDave Jiang 		ioat_chan = ioat_dma->idx[i];
1219ad4a7b50SDave Jiang 		if (!ioat_chan)
1220ad4a7b50SDave Jiang 			continue;
1221ad4a7b50SDave Jiang 
1222ad4a7b50SDave Jiang 		spin_lock_bh(&ioat_chan->prep_lock);
1223ad4a7b50SDave Jiang 		set_bit(IOAT_CHAN_DOWN, &ioat_chan->state);
1224ad4a7b50SDave Jiang 		spin_unlock_bh(&ioat_chan->prep_lock);
1225cfb03be6SWaiman Long 		/*
1226cfb03be6SWaiman Long 		 * Synchronization rule for del_timer_sync():
1227cfb03be6SWaiman Long 		 *  - The caller must not hold locks which would prevent
1228cfb03be6SWaiman Long 		 *    completion of the timer's handler.
1229cfb03be6SWaiman Long 		 * So prep_lock cannot be held before calling it.
1230cfb03be6SWaiman Long 		 */
1231cfb03be6SWaiman Long 		del_timer_sync(&ioat_chan->timer);
1232cfb03be6SWaiman Long 
1233ad4a7b50SDave Jiang 		/* this should quiesce then reset */
1234ad4a7b50SDave Jiang 		ioat_reset_hw(ioat_chan);
1235ad4a7b50SDave Jiang 	}
1236ad4a7b50SDave Jiang 
1237ad4a7b50SDave Jiang 	ioat_disable_interrupts(ioat_dma);
1238ad4a7b50SDave Jiang }
1239ad4a7b50SDave Jiang 
1240184ff2aaSVinod Koul static void ioat_resume(struct ioatdma_device *ioat_dma)
12414222a907SDave Jiang {
12424222a907SDave Jiang 	struct ioatdma_chan *ioat_chan;
12434222a907SDave Jiang 	u32 chanerr;
12444222a907SDave Jiang 	int i;
12454222a907SDave Jiang 
12464222a907SDave Jiang 	for (i = 0; i < IOAT_MAX_CHANS; i++) {
12474222a907SDave Jiang 		ioat_chan = ioat_dma->idx[i];
12484222a907SDave Jiang 		if (!ioat_chan)
12494222a907SDave Jiang 			continue;
12504222a907SDave Jiang 
12514222a907SDave Jiang 		spin_lock_bh(&ioat_chan->prep_lock);
12524222a907SDave Jiang 		clear_bit(IOAT_CHAN_DOWN, &ioat_chan->state);
12534222a907SDave Jiang 		spin_unlock_bh(&ioat_chan->prep_lock);
12544222a907SDave Jiang 
12554222a907SDave Jiang 		chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
12564222a907SDave Jiang 		writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
12574222a907SDave Jiang 
12584222a907SDave Jiang 		/* no need to reset as shutdown already did that */
12594222a907SDave Jiang 	}
12604222a907SDave Jiang }
12614222a907SDave Jiang 
1262c0f28ce6SDave Jiang #define DRV_NAME "ioatdma"
1263c0f28ce6SDave Jiang 
12644222a907SDave Jiang static pci_ers_result_t ioat_pcie_error_detected(struct pci_dev *pdev,
126516d79cd4SLuc Van Oostenryck 						 pci_channel_state_t error)
12664222a907SDave Jiang {
12674222a907SDave Jiang 	dev_dbg(&pdev->dev, "%s: PCIe AER error %d\n", DRV_NAME, error);
12684222a907SDave Jiang 
12694222a907SDave Jiang 	/* quiesce and block I/O */
12704222a907SDave Jiang 	ioat_shutdown(pdev);
12714222a907SDave Jiang 
12724222a907SDave Jiang 	return PCI_ERS_RESULT_NEED_RESET;
12734222a907SDave Jiang }
12744222a907SDave Jiang 
12754222a907SDave Jiang static pci_ers_result_t ioat_pcie_error_slot_reset(struct pci_dev *pdev)
12764222a907SDave Jiang {
12774222a907SDave Jiang 	pci_ers_result_t result = PCI_ERS_RESULT_RECOVERED;
12784222a907SDave Jiang 
12794222a907SDave Jiang 	dev_dbg(&pdev->dev, "%s post reset handling\n", DRV_NAME);
12804222a907SDave Jiang 
12814222a907SDave Jiang 	if (pci_enable_device_mem(pdev) < 0) {
12824222a907SDave Jiang 		dev_err(&pdev->dev,
12834222a907SDave Jiang 			"Failed to enable PCIe device after reset.\n");
12844222a907SDave Jiang 		result = PCI_ERS_RESULT_DISCONNECT;
12854222a907SDave Jiang 	} else {
12864222a907SDave Jiang 		pci_set_master(pdev);
12874222a907SDave Jiang 		pci_restore_state(pdev);
12884222a907SDave Jiang 		pci_save_state(pdev);
12894222a907SDave Jiang 		pci_wake_from_d3(pdev, false);
12904222a907SDave Jiang 	}
12914222a907SDave Jiang 
12924222a907SDave Jiang 	return result;
12934222a907SDave Jiang }
12944222a907SDave Jiang 
12954222a907SDave Jiang static void ioat_pcie_error_resume(struct pci_dev *pdev)
12964222a907SDave Jiang {
12974222a907SDave Jiang 	struct ioatdma_device *ioat_dma = pci_get_drvdata(pdev);
12984222a907SDave Jiang 
12994222a907SDave Jiang 	dev_dbg(&pdev->dev, "%s: AER handling resuming\n", DRV_NAME);
13004222a907SDave Jiang 
13014222a907SDave Jiang 	/* initialize and bring everything back */
13024222a907SDave Jiang 	ioat_resume(ioat_dma);
13034222a907SDave Jiang }
13044222a907SDave Jiang 
13054222a907SDave Jiang static const struct pci_error_handlers ioat_err_handler = {
13064222a907SDave Jiang 	.error_detected = ioat_pcie_error_detected,
13074222a907SDave Jiang 	.slot_reset = ioat_pcie_error_slot_reset,
13084222a907SDave Jiang 	.resume = ioat_pcie_error_resume,
13094222a907SDave Jiang };
13104222a907SDave Jiang 
1311c0f28ce6SDave Jiang static struct pci_driver ioat_pci_driver = {
1312c0f28ce6SDave Jiang 	.name		= DRV_NAME,
1313c0f28ce6SDave Jiang 	.id_table	= ioat_pci_tbl,
1314c0f28ce6SDave Jiang 	.probe		= ioat_pci_probe,
1315c0f28ce6SDave Jiang 	.remove		= ioat_remove,
1316ad4a7b50SDave Jiang 	.shutdown	= ioat_shutdown,
13174222a907SDave Jiang 	.err_handler	= &ioat_err_handler,
1318c0f28ce6SDave Jiang };
1319c0f28ce6SDave Jiang 
1320bf453a0aSLogan Gunthorpe static void release_ioatdma(struct dma_device *device)
1321bf453a0aSLogan Gunthorpe {
1322bf453a0aSLogan Gunthorpe 	struct ioatdma_device *d = to_ioatdma_device(device);
1323bf453a0aSLogan Gunthorpe 	int i;
1324bf453a0aSLogan Gunthorpe 
1325bf453a0aSLogan Gunthorpe 	for (i = 0; i < IOAT_MAX_CHANS; i++)
1326bf453a0aSLogan Gunthorpe 		kfree(d->idx[i]);
1327bf453a0aSLogan Gunthorpe 
1328bf453a0aSLogan Gunthorpe 	dma_pool_destroy(d->completion_pool);
1329bf453a0aSLogan Gunthorpe 	kfree(d);
1330bf453a0aSLogan Gunthorpe }
1331bf453a0aSLogan Gunthorpe 
1332c0f28ce6SDave Jiang static struct ioatdma_device *
1333c0f28ce6SDave Jiang alloc_ioatdma(struct pci_dev *pdev, void __iomem *iobase)
1334c0f28ce6SDave Jiang {
1335bf453a0aSLogan Gunthorpe 	struct ioatdma_device *d = kzalloc(sizeof(*d), GFP_KERNEL);
1336c0f28ce6SDave Jiang 
1337c0f28ce6SDave Jiang 	if (!d)
1338c0f28ce6SDave Jiang 		return NULL;
1339c0f28ce6SDave Jiang 	d->pdev = pdev;
1340c0f28ce6SDave Jiang 	d->reg_base = iobase;
1341bf453a0aSLogan Gunthorpe 	d->dma_dev.device_release = release_ioatdma;
1342c0f28ce6SDave Jiang 	return d;
1343c0f28ce6SDave Jiang }
1344c0f28ce6SDave Jiang 
1345c0f28ce6SDave Jiang static int ioat_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1346c0f28ce6SDave Jiang {
1347c0f28ce6SDave Jiang 	void __iomem * const *iomap;
1348c0f28ce6SDave Jiang 	struct device *dev = &pdev->dev;
1349c0f28ce6SDave Jiang 	struct ioatdma_device *device;
1350ab9501ecSNikita Shubin 	u8 version;
1351c0f28ce6SDave Jiang 	int err;
1352c0f28ce6SDave Jiang 
1353c0f28ce6SDave Jiang 	err = pcim_enable_device(pdev);
1354c0f28ce6SDave Jiang 	if (err)
1355c0f28ce6SDave Jiang 		return err;
1356c0f28ce6SDave Jiang 
1357c0f28ce6SDave Jiang 	err = pcim_iomap_regions(pdev, 1 << IOAT_MMIO_BAR, DRV_NAME);
1358c0f28ce6SDave Jiang 	if (err)
1359c0f28ce6SDave Jiang 		return err;
1360c0f28ce6SDave Jiang 	iomap = pcim_iomap_table(pdev);
1361c0f28ce6SDave Jiang 	if (!iomap)
1362c0f28ce6SDave Jiang 		return -ENOMEM;
1363c0f28ce6SDave Jiang 
1364ab9501ecSNikita Shubin 	version = readb(iomap[IOAT_MMIO_BAR] + IOAT_VER_OFFSET);
1365ab9501ecSNikita Shubin 	if (version < IOAT_VER_3_0)
1366ab9501ecSNikita Shubin 		return -ENODEV;
1367ab9501ecSNikita Shubin 
13680c5afef7SQing Wang 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
1369c0f28ce6SDave Jiang 	if (err)
1370c0f28ce6SDave Jiang 		return err;
1371c0f28ce6SDave Jiang 
1372c0f28ce6SDave Jiang 	device = alloc_ioatdma(pdev, iomap[IOAT_MMIO_BAR]);
1373c0f28ce6SDave Jiang 	if (!device)
1374c0f28ce6SDave Jiang 		return -ENOMEM;
1375c0f28ce6SDave Jiang 	pci_set_master(pdev);
1376c0f28ce6SDave Jiang 	pci_set_drvdata(pdev, device);
1377c0f28ce6SDave Jiang 
1378ab9501ecSNikita Shubin 	device->version = version;
137911e31e28SDave Jiang 	if (device->version >= IOAT_VER_3_4)
138011e31e28SDave Jiang 		ioat_dca_enabled = 0;
1381ab9501ecSNikita Shubin 
138234a31f0aSDave Jiang 	if (is_skx_ioat(pdev))
138334a31f0aSDave Jiang 		device->version = IOAT_VER_3_2;
1384c0f28ce6SDave Jiang 
1385ab9501ecSNikita Shubin 	err = ioat3_dma_probe(device, ioat_dca_enabled);
1386c0f28ce6SDave Jiang 	if (err) {
1387c0f28ce6SDave Jiang 		dev_err(dev, "Intel(R) I/OAT DMA Engine init failed\n");
1388c0f28ce6SDave Jiang 		return -ENODEV;
1389c0f28ce6SDave Jiang 	}
1390c0f28ce6SDave Jiang 
1391c0f28ce6SDave Jiang 	return 0;
1392c0f28ce6SDave Jiang }
1393c0f28ce6SDave Jiang 
1394c0f28ce6SDave Jiang static void ioat_remove(struct pci_dev *pdev)
1395c0f28ce6SDave Jiang {
1396c0f28ce6SDave Jiang 	struct ioatdma_device *device = pci_get_drvdata(pdev);
1397c0f28ce6SDave Jiang 
1398c0f28ce6SDave Jiang 	if (!device)
1399c0f28ce6SDave Jiang 		return;
1400c0f28ce6SDave Jiang 
1401bf453a0aSLogan Gunthorpe 	ioat_shutdown(pdev);
1402bf453a0aSLogan Gunthorpe 
1403c0f28ce6SDave Jiang 	dev_err(&pdev->dev, "Removing dma and dca services\n");
1404c0f28ce6SDave Jiang 	if (device->dca) {
1405c0f28ce6SDave Jiang 		unregister_dca_provider(device->dca, &pdev->dev);
1406c0f28ce6SDave Jiang 		free_dca_provider(device->dca);
1407c0f28ce6SDave Jiang 		device->dca = NULL;
1408c0f28ce6SDave Jiang 	}
14094222a907SDave Jiang 
1410c0f28ce6SDave Jiang 	ioat_dma_remove(device);
1411c0f28ce6SDave Jiang }
1412c0f28ce6SDave Jiang 
1413c0f28ce6SDave Jiang static int __init ioat_init_module(void)
1414c0f28ce6SDave Jiang {
1415c0f28ce6SDave Jiang 	int err = -ENOMEM;
1416c0f28ce6SDave Jiang 
1417c0f28ce6SDave Jiang 	pr_info("%s: Intel(R) QuickData Technology Driver %s\n",
1418c0f28ce6SDave Jiang 		DRV_NAME, IOAT_DMA_VERSION);
1419c0f28ce6SDave Jiang 
1420c0f28ce6SDave Jiang 	ioat_cache = kmem_cache_create("ioat", sizeof(struct ioat_ring_ent),
1421c0f28ce6SDave Jiang 					0, SLAB_HWCACHE_ALIGN, NULL);
1422c0f28ce6SDave Jiang 	if (!ioat_cache)
1423c0f28ce6SDave Jiang 		return -ENOMEM;
1424c0f28ce6SDave Jiang 
1425c0f28ce6SDave Jiang 	ioat_sed_cache = KMEM_CACHE(ioat_sed_ent, 0);
1426c0f28ce6SDave Jiang 	if (!ioat_sed_cache)
1427c0f28ce6SDave Jiang 		goto err_ioat_cache;
1428c0f28ce6SDave Jiang 
1429c0f28ce6SDave Jiang 	err = pci_register_driver(&ioat_pci_driver);
1430c0f28ce6SDave Jiang 	if (err)
1431c0f28ce6SDave Jiang 		goto err_ioat3_cache;
1432c0f28ce6SDave Jiang 
1433c0f28ce6SDave Jiang 	return 0;
1434c0f28ce6SDave Jiang 
1435c0f28ce6SDave Jiang  err_ioat3_cache:
1436c0f28ce6SDave Jiang 	kmem_cache_destroy(ioat_sed_cache);
1437c0f28ce6SDave Jiang 
1438c0f28ce6SDave Jiang  err_ioat_cache:
1439c0f28ce6SDave Jiang 	kmem_cache_destroy(ioat_cache);
1440c0f28ce6SDave Jiang 
1441c0f28ce6SDave Jiang 	return err;
1442c0f28ce6SDave Jiang }
1443c0f28ce6SDave Jiang module_init(ioat_init_module);
1444c0f28ce6SDave Jiang 
1445c0f28ce6SDave Jiang static void __exit ioat_exit_module(void)
1446c0f28ce6SDave Jiang {
1447c0f28ce6SDave Jiang 	pci_unregister_driver(&ioat_pci_driver);
1448c0f28ce6SDave Jiang 	kmem_cache_destroy(ioat_cache);
1449c0f28ce6SDave Jiang }
1450c0f28ce6SDave Jiang module_exit(ioat_exit_module);
1451