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