blob: 66369204896adf62a59f5113627e32d6fee29015 [file] [log] [blame]
Dave Jiangc0f28ce2015-08-11 08:48:43 -07001/*
2 * Intel I/OAT DMA Linux driver
3 * Copyright(c) 2004 - 2015 Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * The full GNU General Public License is included in this distribution in
15 * the file called "COPYING".
16 *
17 */
18
19#include <linux/init.h>
20#include <linux/module.h>
21#include <linux/slab.h>
22#include <linux/pci.h>
23#include <linux/interrupt.h>
24#include <linux/dmaengine.h>
25#include <linux/delay.h>
26#include <linux/dma-mapping.h>
27#include <linux/workqueue.h>
28#include <linux/prefetch.h>
29#include <linux/dca.h>
Dave Jiang4222a902015-08-26 13:17:30 -070030#include <linux/aer.h>
Dave Jiangc0f28ce2015-08-11 08:48:43 -070031#include "dma.h"
32#include "registers.h"
33#include "hw.h"
34
35#include "../dmaengine.h"
36
37MODULE_VERSION(IOAT_DMA_VERSION);
38MODULE_LICENSE("Dual BSD/GPL");
39MODULE_AUTHOR("Intel Corporation");
40
41static struct pci_device_id ioat_pci_tbl[] = {
42 /* I/OAT v3 platforms */
43 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG0) },
44 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG1) },
45 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG2) },
46 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG3) },
47 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG4) },
48 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG5) },
49 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG6) },
50 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG7) },
51
52 /* I/OAT v3.2 platforms */
53 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF0) },
54 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF1) },
55 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF2) },
56 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF3) },
57 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF4) },
58 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF5) },
59 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF6) },
60 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF7) },
61 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF8) },
62 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF9) },
63
64 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB0) },
65 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB1) },
66 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB2) },
67 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB3) },
68 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB4) },
69 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB5) },
70 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB6) },
71 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB7) },
72 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB8) },
73 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB9) },
74
75 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB0) },
76 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB1) },
77 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB2) },
78 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB3) },
79 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB4) },
80 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB5) },
81 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB6) },
82 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB7) },
83 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB8) },
84 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB9) },
85
86 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW0) },
87 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW1) },
88 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW2) },
89 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW3) },
90 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW4) },
91 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW5) },
92 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW6) },
93 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW7) },
94 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW8) },
95 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW9) },
96
Dave Jiangab981932015-08-26 14:16:27 -070097 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX0) },
98 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX1) },
99 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX2) },
100 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX3) },
101 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX4) },
102 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX5) },
103 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX6) },
104 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX7) },
105 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX8) },
106 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX9) },
107
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700108 /* I/OAT v3.3 platforms */
109 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BWD0) },
110 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BWD1) },
111 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BWD2) },
112 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BWD3) },
113
114 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDXDE0) },
115 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDXDE1) },
116 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDXDE2) },
117 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDXDE3) },
118
119 { 0, }
120};
121MODULE_DEVICE_TABLE(pci, ioat_pci_tbl);
122
123static int ioat_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id);
124static void ioat_remove(struct pci_dev *pdev);
Dave Jiang599d49d2015-08-11 08:48:49 -0700125static void
126ioat_init_channel(struct ioatdma_device *ioat_dma,
127 struct ioatdma_chan *ioat_chan, int idx);
Dave Jiangef97bd0f2015-08-11 08:49:00 -0700128static void ioat_intr_quirk(struct ioatdma_device *ioat_dma);
129static int ioat_enumerate_channels(struct ioatdma_device *ioat_dma);
130static int ioat3_dma_self_test(struct ioatdma_device *ioat_dma);
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700131
132static int ioat_dca_enabled = 1;
133module_param(ioat_dca_enabled, int, 0644);
134MODULE_PARM_DESC(ioat_dca_enabled, "control support of dca service (default: 1)");
135int ioat_pending_level = 4;
136module_param(ioat_pending_level, int, 0644);
137MODULE_PARM_DESC(ioat_pending_level,
138 "high-water mark for pushing ioat descriptors (default: 4)");
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700139static char ioat_interrupt_style[32] = "msix";
140module_param_string(ioat_interrupt_style, ioat_interrupt_style,
141 sizeof(ioat_interrupt_style), 0644);
142MODULE_PARM_DESC(ioat_interrupt_style,
143 "set ioat interrupt style: msix (default), msi, intx");
144
145struct kmem_cache *ioat_cache;
146struct kmem_cache *ioat_sed_cache;
147
148static bool is_jf_ioat(struct pci_dev *pdev)
149{
150 switch (pdev->device) {
151 case PCI_DEVICE_ID_INTEL_IOAT_JSF0:
152 case PCI_DEVICE_ID_INTEL_IOAT_JSF1:
153 case PCI_DEVICE_ID_INTEL_IOAT_JSF2:
154 case PCI_DEVICE_ID_INTEL_IOAT_JSF3:
155 case PCI_DEVICE_ID_INTEL_IOAT_JSF4:
156 case PCI_DEVICE_ID_INTEL_IOAT_JSF5:
157 case PCI_DEVICE_ID_INTEL_IOAT_JSF6:
158 case PCI_DEVICE_ID_INTEL_IOAT_JSF7:
159 case PCI_DEVICE_ID_INTEL_IOAT_JSF8:
160 case PCI_DEVICE_ID_INTEL_IOAT_JSF9:
161 return true;
162 default:
163 return false;
164 }
165}
166
167static bool is_snb_ioat(struct pci_dev *pdev)
168{
169 switch (pdev->device) {
170 case PCI_DEVICE_ID_INTEL_IOAT_SNB0:
171 case PCI_DEVICE_ID_INTEL_IOAT_SNB1:
172 case PCI_DEVICE_ID_INTEL_IOAT_SNB2:
173 case PCI_DEVICE_ID_INTEL_IOAT_SNB3:
174 case PCI_DEVICE_ID_INTEL_IOAT_SNB4:
175 case PCI_DEVICE_ID_INTEL_IOAT_SNB5:
176 case PCI_DEVICE_ID_INTEL_IOAT_SNB6:
177 case PCI_DEVICE_ID_INTEL_IOAT_SNB7:
178 case PCI_DEVICE_ID_INTEL_IOAT_SNB8:
179 case PCI_DEVICE_ID_INTEL_IOAT_SNB9:
180 return true;
181 default:
182 return false;
183 }
184}
185
186static bool is_ivb_ioat(struct pci_dev *pdev)
187{
188 switch (pdev->device) {
189 case PCI_DEVICE_ID_INTEL_IOAT_IVB0:
190 case PCI_DEVICE_ID_INTEL_IOAT_IVB1:
191 case PCI_DEVICE_ID_INTEL_IOAT_IVB2:
192 case PCI_DEVICE_ID_INTEL_IOAT_IVB3:
193 case PCI_DEVICE_ID_INTEL_IOAT_IVB4:
194 case PCI_DEVICE_ID_INTEL_IOAT_IVB5:
195 case PCI_DEVICE_ID_INTEL_IOAT_IVB6:
196 case PCI_DEVICE_ID_INTEL_IOAT_IVB7:
197 case PCI_DEVICE_ID_INTEL_IOAT_IVB8:
198 case PCI_DEVICE_ID_INTEL_IOAT_IVB9:
199 return true;
200 default:
201 return false;
202 }
203
204}
205
206static bool is_hsw_ioat(struct pci_dev *pdev)
207{
208 switch (pdev->device) {
209 case PCI_DEVICE_ID_INTEL_IOAT_HSW0:
210 case PCI_DEVICE_ID_INTEL_IOAT_HSW1:
211 case PCI_DEVICE_ID_INTEL_IOAT_HSW2:
212 case PCI_DEVICE_ID_INTEL_IOAT_HSW3:
213 case PCI_DEVICE_ID_INTEL_IOAT_HSW4:
214 case PCI_DEVICE_ID_INTEL_IOAT_HSW5:
215 case PCI_DEVICE_ID_INTEL_IOAT_HSW6:
216 case PCI_DEVICE_ID_INTEL_IOAT_HSW7:
217 case PCI_DEVICE_ID_INTEL_IOAT_HSW8:
218 case PCI_DEVICE_ID_INTEL_IOAT_HSW9:
219 return true;
220 default:
221 return false;
222 }
223
224}
225
Dave Jiangab981932015-08-26 14:16:27 -0700226static bool is_bdx_ioat(struct pci_dev *pdev)
227{
228 switch (pdev->device) {
229 case PCI_DEVICE_ID_INTEL_IOAT_BDX0:
230 case PCI_DEVICE_ID_INTEL_IOAT_BDX1:
231 case PCI_DEVICE_ID_INTEL_IOAT_BDX2:
232 case PCI_DEVICE_ID_INTEL_IOAT_BDX3:
233 case PCI_DEVICE_ID_INTEL_IOAT_BDX4:
234 case PCI_DEVICE_ID_INTEL_IOAT_BDX5:
235 case PCI_DEVICE_ID_INTEL_IOAT_BDX6:
236 case PCI_DEVICE_ID_INTEL_IOAT_BDX7:
237 case PCI_DEVICE_ID_INTEL_IOAT_BDX8:
238 case PCI_DEVICE_ID_INTEL_IOAT_BDX9:
239 return true;
240 default:
241 return false;
242 }
243}
244
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700245static bool is_xeon_cb32(struct pci_dev *pdev)
246{
247 return is_jf_ioat(pdev) || is_snb_ioat(pdev) || is_ivb_ioat(pdev) ||
Dave Jiangab981932015-08-26 14:16:27 -0700248 is_hsw_ioat(pdev) || is_bdx_ioat(pdev);
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700249}
250
251bool is_bwd_ioat(struct pci_dev *pdev)
252{
253 switch (pdev->device) {
254 case PCI_DEVICE_ID_INTEL_IOAT_BWD0:
255 case PCI_DEVICE_ID_INTEL_IOAT_BWD1:
256 case PCI_DEVICE_ID_INTEL_IOAT_BWD2:
257 case PCI_DEVICE_ID_INTEL_IOAT_BWD3:
258 /* even though not Atom, BDX-DE has same DMA silicon */
259 case PCI_DEVICE_ID_INTEL_IOAT_BDXDE0:
260 case PCI_DEVICE_ID_INTEL_IOAT_BDXDE1:
261 case PCI_DEVICE_ID_INTEL_IOAT_BDXDE2:
262 case PCI_DEVICE_ID_INTEL_IOAT_BDXDE3:
263 return true;
264 default:
265 return false;
266 }
267}
268
269static bool is_bwd_noraid(struct pci_dev *pdev)
270{
271 switch (pdev->device) {
272 case PCI_DEVICE_ID_INTEL_IOAT_BWD2:
273 case PCI_DEVICE_ID_INTEL_IOAT_BWD3:
274 case PCI_DEVICE_ID_INTEL_IOAT_BDXDE0:
275 case PCI_DEVICE_ID_INTEL_IOAT_BDXDE1:
276 case PCI_DEVICE_ID_INTEL_IOAT_BDXDE2:
277 case PCI_DEVICE_ID_INTEL_IOAT_BDXDE3:
278 return true;
279 default:
280 return false;
281 }
282
283}
284
285/*
286 * Perform a IOAT transaction to verify the HW works.
287 */
288#define IOAT_TEST_SIZE 2000
289
290static void ioat_dma_test_callback(void *dma_async_param)
291{
292 struct completion *cmp = dma_async_param;
293
294 complete(cmp);
295}
296
297/**
298 * ioat_dma_self_test - Perform a IOAT transaction to verify the HW works.
299 * @ioat_dma: dma device to be tested
300 */
Dave Jiang599d49d2015-08-11 08:48:49 -0700301static int ioat_dma_self_test(struct ioatdma_device *ioat_dma)
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700302{
303 int i;
304 u8 *src;
305 u8 *dest;
306 struct dma_device *dma = &ioat_dma->dma_dev;
307 struct device *dev = &ioat_dma->pdev->dev;
308 struct dma_chan *dma_chan;
309 struct dma_async_tx_descriptor *tx;
310 dma_addr_t dma_dest, dma_src;
311 dma_cookie_t cookie;
312 int err = 0;
313 struct completion cmp;
314 unsigned long tmo;
315 unsigned long flags;
316
317 src = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
318 if (!src)
319 return -ENOMEM;
320 dest = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
321 if (!dest) {
322 kfree(src);
323 return -ENOMEM;
324 }
325
326 /* Fill in src buffer */
327 for (i = 0; i < IOAT_TEST_SIZE; i++)
328 src[i] = (u8)i;
329
330 /* Start copy, using first DMA channel */
331 dma_chan = container_of(dma->channels.next, struct dma_chan,
332 device_node);
333 if (dma->device_alloc_chan_resources(dma_chan) < 1) {
334 dev_err(dev, "selftest cannot allocate chan resource\n");
335 err = -ENODEV;
336 goto out;
337 }
338
339 dma_src = dma_map_single(dev, src, IOAT_TEST_SIZE, DMA_TO_DEVICE);
340 if (dma_mapping_error(dev, dma_src)) {
341 dev_err(dev, "mapping src buffer failed\n");
342 goto free_resources;
343 }
344 dma_dest = dma_map_single(dev, dest, IOAT_TEST_SIZE, DMA_FROM_DEVICE);
345 if (dma_mapping_error(dev, dma_dest)) {
346 dev_err(dev, "mapping dest buffer failed\n");
347 goto unmap_src;
348 }
349 flags = DMA_PREP_INTERRUPT;
350 tx = ioat_dma->dma_dev.device_prep_dma_memcpy(dma_chan, dma_dest,
351 dma_src, IOAT_TEST_SIZE,
352 flags);
353 if (!tx) {
354 dev_err(dev, "Self-test prep failed, disabling\n");
355 err = -ENODEV;
356 goto unmap_dma;
357 }
358
359 async_tx_ack(tx);
360 init_completion(&cmp);
361 tx->callback = ioat_dma_test_callback;
362 tx->callback_param = &cmp;
363 cookie = tx->tx_submit(tx);
364 if (cookie < 0) {
365 dev_err(dev, "Self-test setup failed, disabling\n");
366 err = -ENODEV;
367 goto unmap_dma;
368 }
369 dma->device_issue_pending(dma_chan);
370
371 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
372
373 if (tmo == 0 ||
374 dma->device_tx_status(dma_chan, cookie, NULL)
375 != DMA_COMPLETE) {
376 dev_err(dev, "Self-test copy timed out, disabling\n");
377 err = -ENODEV;
378 goto unmap_dma;
379 }
380 if (memcmp(src, dest, IOAT_TEST_SIZE)) {
381 dev_err(dev, "Self-test copy failed compare, disabling\n");
382 err = -ENODEV;
383 goto free_resources;
384 }
385
386unmap_dma:
387 dma_unmap_single(dev, dma_dest, IOAT_TEST_SIZE, DMA_FROM_DEVICE);
388unmap_src:
389 dma_unmap_single(dev, dma_src, IOAT_TEST_SIZE, DMA_TO_DEVICE);
390free_resources:
391 dma->device_free_chan_resources(dma_chan);
392out:
393 kfree(src);
394 kfree(dest);
395 return err;
396}
397
398/**
399 * ioat_dma_setup_interrupts - setup interrupt handler
400 * @ioat_dma: ioat dma device
401 */
402int ioat_dma_setup_interrupts(struct ioatdma_device *ioat_dma)
403{
404 struct ioatdma_chan *ioat_chan;
405 struct pci_dev *pdev = ioat_dma->pdev;
406 struct device *dev = &pdev->dev;
407 struct msix_entry *msix;
408 int i, j, msixcnt;
409 int err = -EINVAL;
410 u8 intrctrl = 0;
411
412 if (!strcmp(ioat_interrupt_style, "msix"))
413 goto msix;
414 if (!strcmp(ioat_interrupt_style, "msi"))
415 goto msi;
416 if (!strcmp(ioat_interrupt_style, "intx"))
417 goto intx;
418 dev_err(dev, "invalid ioat_interrupt_style %s\n", ioat_interrupt_style);
419 goto err_no_irq;
420
421msix:
422 /* The number of MSI-X vectors should equal the number of channels */
423 msixcnt = ioat_dma->dma_dev.chancnt;
424 for (i = 0; i < msixcnt; i++)
425 ioat_dma->msix_entries[i].entry = i;
426
427 err = pci_enable_msix_exact(pdev, ioat_dma->msix_entries, msixcnt);
428 if (err)
429 goto msi;
430
431 for (i = 0; i < msixcnt; i++) {
432 msix = &ioat_dma->msix_entries[i];
433 ioat_chan = ioat_chan_by_index(ioat_dma, i);
434 err = devm_request_irq(dev, msix->vector,
435 ioat_dma_do_interrupt_msix, 0,
436 "ioat-msix", ioat_chan);
437 if (err) {
438 for (j = 0; j < i; j++) {
439 msix = &ioat_dma->msix_entries[j];
440 ioat_chan = ioat_chan_by_index(ioat_dma, j);
441 devm_free_irq(dev, msix->vector, ioat_chan);
442 }
443 goto msi;
444 }
445 }
446 intrctrl |= IOAT_INTRCTRL_MSIX_VECTOR_CONTROL;
447 ioat_dma->irq_mode = IOAT_MSIX;
448 goto done;
449
450msi:
451 err = pci_enable_msi(pdev);
452 if (err)
453 goto intx;
454
455 err = devm_request_irq(dev, pdev->irq, ioat_dma_do_interrupt, 0,
456 "ioat-msi", ioat_dma);
457 if (err) {
458 pci_disable_msi(pdev);
459 goto intx;
460 }
461 ioat_dma->irq_mode = IOAT_MSI;
462 goto done;
463
464intx:
465 err = devm_request_irq(dev, pdev->irq, ioat_dma_do_interrupt,
466 IRQF_SHARED, "ioat-intx", ioat_dma);
467 if (err)
468 goto err_no_irq;
469
470 ioat_dma->irq_mode = IOAT_INTX;
471done:
Dave Jiangef97bd0f2015-08-11 08:49:00 -0700472 if (is_bwd_ioat(pdev))
473 ioat_intr_quirk(ioat_dma);
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700474 intrctrl |= IOAT_INTRCTRL_MASTER_INT_EN;
475 writeb(intrctrl, ioat_dma->reg_base + IOAT_INTRCTRL_OFFSET);
476 return 0;
477
478err_no_irq:
479 /* Disable all interrupt generation */
480 writeb(0, ioat_dma->reg_base + IOAT_INTRCTRL_OFFSET);
481 ioat_dma->irq_mode = IOAT_NOIRQ;
482 dev_err(dev, "no usable interrupts\n");
483 return err;
484}
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700485
486static void ioat_disable_interrupts(struct ioatdma_device *ioat_dma)
487{
488 /* Disable all interrupt generation */
489 writeb(0, ioat_dma->reg_base + IOAT_INTRCTRL_OFFSET);
490}
491
Dave Jiang599d49d2015-08-11 08:48:49 -0700492static int ioat_probe(struct ioatdma_device *ioat_dma)
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700493{
494 int err = -ENODEV;
495 struct dma_device *dma = &ioat_dma->dma_dev;
496 struct pci_dev *pdev = ioat_dma->pdev;
497 struct device *dev = &pdev->dev;
498
499 /* DMA coherent memory pool for DMA descriptor allocations */
Dave Jiang679cfbf2016-02-10 15:00:21 -0700500 ioat_dma->dma_pool = dma_pool_create("dma_desc_pool", dev,
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700501 sizeof(struct ioat_dma_descriptor),
502 64, 0);
503 if (!ioat_dma->dma_pool) {
504 err = -ENOMEM;
505 goto err_dma_pool;
506 }
507
Dave Jiang679cfbf2016-02-10 15:00:21 -0700508 ioat_dma->completion_pool = dma_pool_create("completion_pool", dev,
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700509 sizeof(u64),
510 SMP_CACHE_BYTES,
511 SMP_CACHE_BYTES);
512
513 if (!ioat_dma->completion_pool) {
514 err = -ENOMEM;
515 goto err_completion_pool;
516 }
517
Dave Jiangef97bd0f2015-08-11 08:49:00 -0700518 ioat_enumerate_channels(ioat_dma);
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700519
520 dma_cap_set(DMA_MEMCPY, dma->cap_mask);
521 dma->dev = &pdev->dev;
522
523 if (!dma->chancnt) {
524 dev_err(dev, "channel enumeration error\n");
525 goto err_setup_interrupts;
526 }
527
528 err = ioat_dma_setup_interrupts(ioat_dma);
529 if (err)
530 goto err_setup_interrupts;
531
Dave Jiangef97bd0f2015-08-11 08:49:00 -0700532 err = ioat3_dma_self_test(ioat_dma);
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700533 if (err)
534 goto err_self_test;
535
536 return 0;
537
538err_self_test:
539 ioat_disable_interrupts(ioat_dma);
540err_setup_interrupts:
Dave Jiang679cfbf2016-02-10 15:00:21 -0700541 dma_pool_destroy(ioat_dma->completion_pool);
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700542err_completion_pool:
Dave Jiang679cfbf2016-02-10 15:00:21 -0700543 dma_pool_destroy(ioat_dma->dma_pool);
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700544err_dma_pool:
545 return err;
546}
547
Dave Jiang599d49d2015-08-11 08:48:49 -0700548static int ioat_register(struct ioatdma_device *ioat_dma)
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700549{
550 int err = dma_async_device_register(&ioat_dma->dma_dev);
551
552 if (err) {
553 ioat_disable_interrupts(ioat_dma);
Dave Jiang679cfbf2016-02-10 15:00:21 -0700554 dma_pool_destroy(ioat_dma->completion_pool);
555 dma_pool_destroy(ioat_dma->dma_pool);
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700556 }
557
558 return err;
559}
560
Dave Jiang599d49d2015-08-11 08:48:49 -0700561static void ioat_dma_remove(struct ioatdma_device *ioat_dma)
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700562{
563 struct dma_device *dma = &ioat_dma->dma_dev;
564
565 ioat_disable_interrupts(ioat_dma);
566
567 ioat_kobject_del(ioat_dma);
568
569 dma_async_device_unregister(dma);
570
Dave Jiang679cfbf2016-02-10 15:00:21 -0700571 dma_pool_destroy(ioat_dma->dma_pool);
572 dma_pool_destroy(ioat_dma->completion_pool);
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700573
574 INIT_LIST_HEAD(&dma->channels);
575}
576
577/**
578 * ioat_enumerate_channels - find and initialize the device's channels
579 * @ioat_dma: the ioat dma device to be enumerated
580 */
Dave Jiang599d49d2015-08-11 08:48:49 -0700581static int ioat_enumerate_channels(struct ioatdma_device *ioat_dma)
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700582{
583 struct ioatdma_chan *ioat_chan;
584 struct device *dev = &ioat_dma->pdev->dev;
585 struct dma_device *dma = &ioat_dma->dma_dev;
586 u8 xfercap_log;
587 int i;
588
589 INIT_LIST_HEAD(&dma->channels);
590 dma->chancnt = readb(ioat_dma->reg_base + IOAT_CHANCNT_OFFSET);
591 dma->chancnt &= 0x1f; /* bits [4:0] valid */
592 if (dma->chancnt > ARRAY_SIZE(ioat_dma->idx)) {
593 dev_warn(dev, "(%d) exceeds max supported channels (%zu)\n",
594 dma->chancnt, ARRAY_SIZE(ioat_dma->idx));
595 dma->chancnt = ARRAY_SIZE(ioat_dma->idx);
596 }
597 xfercap_log = readb(ioat_dma->reg_base + IOAT_XFERCAP_OFFSET);
598 xfercap_log &= 0x1f; /* bits [4:0] valid */
599 if (xfercap_log == 0)
600 return 0;
601 dev_dbg(dev, "%s: xfercap = %d\n", __func__, 1 << xfercap_log);
602
603 for (i = 0; i < dma->chancnt; i++) {
604 ioat_chan = devm_kzalloc(dev, sizeof(*ioat_chan), GFP_KERNEL);
605 if (!ioat_chan)
606 break;
607
608 ioat_init_channel(ioat_dma, ioat_chan, i);
609 ioat_chan->xfercap_log = xfercap_log;
610 spin_lock_init(&ioat_chan->prep_lock);
Dave Jiangef97bd0f2015-08-11 08:49:00 -0700611 if (ioat_reset_hw(ioat_chan)) {
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700612 i = 0;
613 break;
614 }
615 }
616 dma->chancnt = i;
617 return i;
618}
619
620/**
621 * ioat_free_chan_resources - release all the descriptors
622 * @chan: the channel to be cleaned
623 */
Dave Jiang599d49d2015-08-11 08:48:49 -0700624static void ioat_free_chan_resources(struct dma_chan *c)
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700625{
626 struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
627 struct ioatdma_device *ioat_dma = ioat_chan->ioat_dma;
628 struct ioat_ring_ent *desc;
629 const int total_descs = 1 << ioat_chan->alloc_order;
630 int descs;
631 int i;
632
633 /* Before freeing channel resources first check
634 * if they have been previously allocated for this channel.
635 */
636 if (!ioat_chan->ring)
637 return;
638
639 ioat_stop(ioat_chan);
Dave Jiangef97bd0f2015-08-11 08:49:00 -0700640 ioat_reset_hw(ioat_chan);
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700641
642 spin_lock_bh(&ioat_chan->cleanup_lock);
643 spin_lock_bh(&ioat_chan->prep_lock);
644 descs = ioat_ring_space(ioat_chan);
645 dev_dbg(to_dev(ioat_chan), "freeing %d idle descriptors\n", descs);
646 for (i = 0; i < descs; i++) {
647 desc = ioat_get_ring_ent(ioat_chan, ioat_chan->head + i);
648 ioat_free_ring_ent(desc, c);
649 }
650
651 if (descs < total_descs)
652 dev_err(to_dev(ioat_chan), "Freeing %d in use descriptors!\n",
653 total_descs - descs);
654
655 for (i = 0; i < total_descs - descs; i++) {
656 desc = ioat_get_ring_ent(ioat_chan, ioat_chan->tail + i);
657 dump_desc_dbg(ioat_chan, desc);
658 ioat_free_ring_ent(desc, c);
659 }
660
661 kfree(ioat_chan->ring);
662 ioat_chan->ring = NULL;
663 ioat_chan->alloc_order = 0;
Dave Jiang679cfbf2016-02-10 15:00:21 -0700664 dma_pool_free(ioat_dma->completion_pool, ioat_chan->completion,
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700665 ioat_chan->completion_dma);
666 spin_unlock_bh(&ioat_chan->prep_lock);
667 spin_unlock_bh(&ioat_chan->cleanup_lock);
668
669 ioat_chan->last_completion = 0;
670 ioat_chan->completion_dma = 0;
671 ioat_chan->dmacount = 0;
672}
673
674/* ioat_alloc_chan_resources - allocate/initialize ioat descriptor ring
675 * @chan: channel to be initialized
676 */
Dave Jiang599d49d2015-08-11 08:48:49 -0700677static int ioat_alloc_chan_resources(struct dma_chan *c)
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700678{
679 struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
680 struct ioat_ring_ent **ring;
681 u64 status;
682 int order;
683 int i = 0;
684 u32 chanerr;
685
686 /* have we already been set up? */
687 if (ioat_chan->ring)
688 return 1 << ioat_chan->alloc_order;
689
690 /* Setup register to interrupt and write completion status on error */
691 writew(IOAT_CHANCTRL_RUN, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
692
693 /* allocate a completion writeback area */
694 /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
695 ioat_chan->completion =
Dave Jiang679cfbf2016-02-10 15:00:21 -0700696 dma_pool_alloc(ioat_chan->ioat_dma->completion_pool,
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700697 GFP_KERNEL, &ioat_chan->completion_dma);
698 if (!ioat_chan->completion)
699 return -ENOMEM;
700
701 memset(ioat_chan->completion, 0, sizeof(*ioat_chan->completion));
702 writel(((u64)ioat_chan->completion_dma) & 0x00000000FFFFFFFF,
703 ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
704 writel(((u64)ioat_chan->completion_dma) >> 32,
705 ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
706
Dave Jiangcd60cd92016-02-10 15:00:26 -0700707 order = IOAT_MAX_ORDER;
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700708 ring = ioat_alloc_ring(c, order, GFP_KERNEL);
709 if (!ring)
710 return -ENOMEM;
711
712 spin_lock_bh(&ioat_chan->cleanup_lock);
713 spin_lock_bh(&ioat_chan->prep_lock);
714 ioat_chan->ring = ring;
715 ioat_chan->head = 0;
716 ioat_chan->issued = 0;
717 ioat_chan->tail = 0;
718 ioat_chan->alloc_order = order;
719 set_bit(IOAT_RUN, &ioat_chan->state);
720 spin_unlock_bh(&ioat_chan->prep_lock);
721 spin_unlock_bh(&ioat_chan->cleanup_lock);
722
723 ioat_start_null_desc(ioat_chan);
724
725 /* check that we got off the ground */
726 do {
727 udelay(1);
728 status = ioat_chansts(ioat_chan);
729 } while (i++ < 20 && !is_ioat_active(status) && !is_ioat_idle(status));
730
731 if (is_ioat_active(status) || is_ioat_idle(status))
732 return 1 << ioat_chan->alloc_order;
733
734 chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
735
736 dev_WARN(to_dev(ioat_chan),
737 "failed to start channel chanerr: %#x\n", chanerr);
738 ioat_free_chan_resources(c);
739 return -EFAULT;
740}
741
742/* common channel initialization */
Dave Jiang599d49d2015-08-11 08:48:49 -0700743static void
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700744ioat_init_channel(struct ioatdma_device *ioat_dma,
745 struct ioatdma_chan *ioat_chan, int idx)
746{
747 struct dma_device *dma = &ioat_dma->dma_dev;
748 struct dma_chan *c = &ioat_chan->dma_chan;
749 unsigned long data = (unsigned long) c;
750
751 ioat_chan->ioat_dma = ioat_dma;
752 ioat_chan->reg_base = ioat_dma->reg_base + (0x80 * (idx + 1));
753 spin_lock_init(&ioat_chan->cleanup_lock);
754 ioat_chan->dma_chan.device = dma;
755 dma_cookie_init(&ioat_chan->dma_chan);
756 list_add_tail(&ioat_chan->dma_chan.device_node, &dma->channels);
757 ioat_dma->idx[idx] = ioat_chan;
758 init_timer(&ioat_chan->timer);
Dave Jiangef97bd0f2015-08-11 08:49:00 -0700759 ioat_chan->timer.function = ioat_timer_event;
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700760 ioat_chan->timer.data = data;
Dave Jiangef97bd0f2015-08-11 08:49:00 -0700761 tasklet_init(&ioat_chan->cleanup_task, ioat_cleanup_event, data);
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700762}
763
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700764#define IOAT_NUM_SRC_TEST 6 /* must be <= 8 */
765static int ioat_xor_val_self_test(struct ioatdma_device *ioat_dma)
766{
767 int i, src_idx;
768 struct page *dest;
769 struct page *xor_srcs[IOAT_NUM_SRC_TEST];
770 struct page *xor_val_srcs[IOAT_NUM_SRC_TEST + 1];
771 dma_addr_t dma_srcs[IOAT_NUM_SRC_TEST + 1];
772 dma_addr_t dest_dma;
773 struct dma_async_tx_descriptor *tx;
774 struct dma_chan *dma_chan;
775 dma_cookie_t cookie;
776 u8 cmp_byte = 0;
777 u32 cmp_word;
778 u32 xor_val_result;
779 int err = 0;
780 struct completion cmp;
781 unsigned long tmo;
782 struct device *dev = &ioat_dma->pdev->dev;
783 struct dma_device *dma = &ioat_dma->dma_dev;
784 u8 op = 0;
785
786 dev_dbg(dev, "%s\n", __func__);
787
788 if (!dma_has_cap(DMA_XOR, dma->cap_mask))
789 return 0;
790
791 for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
792 xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
793 if (!xor_srcs[src_idx]) {
794 while (src_idx--)
795 __free_page(xor_srcs[src_idx]);
796 return -ENOMEM;
797 }
798 }
799
800 dest = alloc_page(GFP_KERNEL);
801 if (!dest) {
802 while (src_idx--)
803 __free_page(xor_srcs[src_idx]);
804 return -ENOMEM;
805 }
806
807 /* Fill in src buffers */
808 for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
809 u8 *ptr = page_address(xor_srcs[src_idx]);
810
811 for (i = 0; i < PAGE_SIZE; i++)
812 ptr[i] = (1 << src_idx);
813 }
814
815 for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++)
816 cmp_byte ^= (u8) (1 << src_idx);
817
818 cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
819 (cmp_byte << 8) | cmp_byte;
820
821 memset(page_address(dest), 0, PAGE_SIZE);
822
823 dma_chan = container_of(dma->channels.next, struct dma_chan,
824 device_node);
825 if (dma->device_alloc_chan_resources(dma_chan) < 1) {
826 err = -ENODEV;
827 goto out;
828 }
829
830 /* test xor */
831 op = IOAT_OP_XOR;
832
833 dest_dma = dma_map_page(dev, dest, 0, PAGE_SIZE, DMA_FROM_DEVICE);
834 if (dma_mapping_error(dev, dest_dma))
835 goto dma_unmap;
836
837 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
838 dma_srcs[i] = DMA_ERROR_CODE;
839 for (i = 0; i < IOAT_NUM_SRC_TEST; i++) {
840 dma_srcs[i] = dma_map_page(dev, xor_srcs[i], 0, PAGE_SIZE,
841 DMA_TO_DEVICE);
842 if (dma_mapping_error(dev, dma_srcs[i]))
843 goto dma_unmap;
844 }
845 tx = dma->device_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
846 IOAT_NUM_SRC_TEST, PAGE_SIZE,
847 DMA_PREP_INTERRUPT);
848
849 if (!tx) {
850 dev_err(dev, "Self-test xor prep failed\n");
851 err = -ENODEV;
852 goto dma_unmap;
853 }
854
855 async_tx_ack(tx);
856 init_completion(&cmp);
Dave Jiang3372de52015-08-11 08:48:55 -0700857 tx->callback = ioat_dma_test_callback;
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700858 tx->callback_param = &cmp;
859 cookie = tx->tx_submit(tx);
860 if (cookie < 0) {
861 dev_err(dev, "Self-test xor setup failed\n");
862 err = -ENODEV;
863 goto dma_unmap;
864 }
865 dma->device_issue_pending(dma_chan);
866
867 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
868
869 if (tmo == 0 ||
870 dma->device_tx_status(dma_chan, cookie, NULL) != DMA_COMPLETE) {
871 dev_err(dev, "Self-test xor timed out\n");
872 err = -ENODEV;
873 goto dma_unmap;
874 }
875
876 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
877 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
878
879 dma_sync_single_for_cpu(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
880 for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
881 u32 *ptr = page_address(dest);
882
883 if (ptr[i] != cmp_word) {
884 dev_err(dev, "Self-test xor failed compare\n");
885 err = -ENODEV;
886 goto free_resources;
887 }
888 }
889 dma_sync_single_for_device(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
890
891 dma_unmap_page(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
892
893 /* skip validate if the capability is not present */
894 if (!dma_has_cap(DMA_XOR_VAL, dma_chan->device->cap_mask))
895 goto free_resources;
896
897 op = IOAT_OP_XOR_VAL;
898
899 /* validate the sources with the destintation page */
900 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
901 xor_val_srcs[i] = xor_srcs[i];
902 xor_val_srcs[i] = dest;
903
904 xor_val_result = 1;
905
906 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
907 dma_srcs[i] = DMA_ERROR_CODE;
908 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++) {
909 dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
910 DMA_TO_DEVICE);
911 if (dma_mapping_error(dev, dma_srcs[i]))
912 goto dma_unmap;
913 }
914 tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
915 IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
916 &xor_val_result, DMA_PREP_INTERRUPT);
917 if (!tx) {
918 dev_err(dev, "Self-test zero prep failed\n");
919 err = -ENODEV;
920 goto dma_unmap;
921 }
922
923 async_tx_ack(tx);
924 init_completion(&cmp);
Dave Jiang3372de52015-08-11 08:48:55 -0700925 tx->callback = ioat_dma_test_callback;
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700926 tx->callback_param = &cmp;
927 cookie = tx->tx_submit(tx);
928 if (cookie < 0) {
929 dev_err(dev, "Self-test zero setup failed\n");
930 err = -ENODEV;
931 goto dma_unmap;
932 }
933 dma->device_issue_pending(dma_chan);
934
935 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
936
937 if (tmo == 0 ||
938 dma->device_tx_status(dma_chan, cookie, NULL) != DMA_COMPLETE) {
939 dev_err(dev, "Self-test validate timed out\n");
940 err = -ENODEV;
941 goto dma_unmap;
942 }
943
944 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
945 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
946
947 if (xor_val_result != 0) {
948 dev_err(dev, "Self-test validate failed compare\n");
949 err = -ENODEV;
950 goto free_resources;
951 }
952
953 memset(page_address(dest), 0, PAGE_SIZE);
954
955 /* test for non-zero parity sum */
956 op = IOAT_OP_XOR_VAL;
957
958 xor_val_result = 0;
959 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
960 dma_srcs[i] = DMA_ERROR_CODE;
961 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++) {
962 dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
963 DMA_TO_DEVICE);
964 if (dma_mapping_error(dev, dma_srcs[i]))
965 goto dma_unmap;
966 }
967 tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
968 IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
969 &xor_val_result, DMA_PREP_INTERRUPT);
970 if (!tx) {
971 dev_err(dev, "Self-test 2nd zero prep failed\n");
972 err = -ENODEV;
973 goto dma_unmap;
974 }
975
976 async_tx_ack(tx);
977 init_completion(&cmp);
Dave Jiang3372de52015-08-11 08:48:55 -0700978 tx->callback = ioat_dma_test_callback;
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700979 tx->callback_param = &cmp;
980 cookie = tx->tx_submit(tx);
981 if (cookie < 0) {
982 dev_err(dev, "Self-test 2nd zero setup failed\n");
983 err = -ENODEV;
984 goto dma_unmap;
985 }
986 dma->device_issue_pending(dma_chan);
987
988 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
989
990 if (tmo == 0 ||
991 dma->device_tx_status(dma_chan, cookie, NULL) != DMA_COMPLETE) {
992 dev_err(dev, "Self-test 2nd validate timed out\n");
993 err = -ENODEV;
994 goto dma_unmap;
995 }
996
997 if (xor_val_result != SUM_CHECK_P_RESULT) {
998 dev_err(dev, "Self-test validate failed compare\n");
999 err = -ENODEV;
1000 goto dma_unmap;
1001 }
1002
1003 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1004 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
1005
1006 goto free_resources;
1007dma_unmap:
1008 if (op == IOAT_OP_XOR) {
1009 if (dest_dma != DMA_ERROR_CODE)
1010 dma_unmap_page(dev, dest_dma, PAGE_SIZE,
1011 DMA_FROM_DEVICE);
1012 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
1013 if (dma_srcs[i] != DMA_ERROR_CODE)
1014 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE,
1015 DMA_TO_DEVICE);
1016 } else if (op == IOAT_OP_XOR_VAL) {
1017 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1018 if (dma_srcs[i] != DMA_ERROR_CODE)
1019 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE,
1020 DMA_TO_DEVICE);
1021 }
1022free_resources:
1023 dma->device_free_chan_resources(dma_chan);
1024out:
1025 src_idx = IOAT_NUM_SRC_TEST;
1026 while (src_idx--)
1027 __free_page(xor_srcs[src_idx]);
1028 __free_page(dest);
1029 return err;
1030}
1031
1032static int ioat3_dma_self_test(struct ioatdma_device *ioat_dma)
1033{
Dave Jiang64f1d0f2015-08-20 08:44:14 -07001034 int rc;
Dave Jiangc0f28ce2015-08-11 08:48:43 -07001035
Dave Jiang64f1d0f2015-08-20 08:44:14 -07001036 rc = ioat_dma_self_test(ioat_dma);
Dave Jiangc0f28ce2015-08-11 08:48:43 -07001037 if (rc)
1038 return rc;
1039
1040 rc = ioat_xor_val_self_test(ioat_dma);
Dave Jiangc0f28ce2015-08-11 08:48:43 -07001041
Dave Jiang64f1d0f2015-08-20 08:44:14 -07001042 return rc;
Dave Jiangc0f28ce2015-08-11 08:48:43 -07001043}
1044
Dave Jiang3372de52015-08-11 08:48:55 -07001045static void ioat_intr_quirk(struct ioatdma_device *ioat_dma)
Dave Jiangc0f28ce2015-08-11 08:48:43 -07001046{
1047 struct dma_device *dma;
1048 struct dma_chan *c;
1049 struct ioatdma_chan *ioat_chan;
1050 u32 errmask;
1051
1052 dma = &ioat_dma->dma_dev;
1053
1054 /*
1055 * if we have descriptor write back error status, we mask the
1056 * error interrupts
1057 */
1058 if (ioat_dma->cap & IOAT_CAP_DWBES) {
1059 list_for_each_entry(c, &dma->channels, device_node) {
1060 ioat_chan = to_ioat_chan(c);
1061 errmask = readl(ioat_chan->reg_base +
1062 IOAT_CHANERR_MASK_OFFSET);
1063 errmask |= IOAT_CHANERR_XOR_P_OR_CRC_ERR |
1064 IOAT_CHANERR_XOR_Q_ERR;
1065 writel(errmask, ioat_chan->reg_base +
1066 IOAT_CHANERR_MASK_OFFSET);
1067 }
1068 }
1069}
1070
Dave Jiang599d49d2015-08-11 08:48:49 -07001071static int ioat3_dma_probe(struct ioatdma_device *ioat_dma, int dca)
Dave Jiangc0f28ce2015-08-11 08:48:43 -07001072{
1073 struct pci_dev *pdev = ioat_dma->pdev;
1074 int dca_en = system_has_dca_enabled(pdev);
1075 struct dma_device *dma;
1076 struct dma_chan *c;
1077 struct ioatdma_chan *ioat_chan;
1078 bool is_raid_device = false;
1079 int err;
1080
Dave Jiangc0f28ce2015-08-11 08:48:43 -07001081 dma = &ioat_dma->dma_dev;
1082 dma->device_prep_dma_memcpy = ioat_dma_prep_memcpy_lock;
1083 dma->device_issue_pending = ioat_issue_pending;
1084 dma->device_alloc_chan_resources = ioat_alloc_chan_resources;
1085 dma->device_free_chan_resources = ioat_free_chan_resources;
1086
1087 dma_cap_set(DMA_INTERRUPT, dma->cap_mask);
1088 dma->device_prep_dma_interrupt = ioat_prep_interrupt_lock;
1089
1090 ioat_dma->cap = readl(ioat_dma->reg_base + IOAT_DMA_CAP_OFFSET);
1091
1092 if (is_xeon_cb32(pdev) || is_bwd_noraid(pdev))
1093 ioat_dma->cap &=
1094 ~(IOAT_CAP_XOR | IOAT_CAP_PQ | IOAT_CAP_RAID16SS);
1095
1096 /* dca is incompatible with raid operations */
1097 if (dca_en && (ioat_dma->cap & (IOAT_CAP_XOR|IOAT_CAP_PQ)))
1098 ioat_dma->cap &= ~(IOAT_CAP_XOR|IOAT_CAP_PQ);
1099
1100 if (ioat_dma->cap & IOAT_CAP_XOR) {
1101 is_raid_device = true;
1102 dma->max_xor = 8;
1103
1104 dma_cap_set(DMA_XOR, dma->cap_mask);
1105 dma->device_prep_dma_xor = ioat_prep_xor;
1106
1107 dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
1108 dma->device_prep_dma_xor_val = ioat_prep_xor_val;
1109 }
1110
1111 if (ioat_dma->cap & IOAT_CAP_PQ) {
1112 is_raid_device = true;
1113
1114 dma->device_prep_dma_pq = ioat_prep_pq;
1115 dma->device_prep_dma_pq_val = ioat_prep_pq_val;
1116 dma_cap_set(DMA_PQ, dma->cap_mask);
1117 dma_cap_set(DMA_PQ_VAL, dma->cap_mask);
1118
1119 if (ioat_dma->cap & IOAT_CAP_RAID16SS)
1120 dma_set_maxpq(dma, 16, 0);
1121 else
1122 dma_set_maxpq(dma, 8, 0);
1123
1124 if (!(ioat_dma->cap & IOAT_CAP_XOR)) {
1125 dma->device_prep_dma_xor = ioat_prep_pqxor;
1126 dma->device_prep_dma_xor_val = ioat_prep_pqxor_val;
1127 dma_cap_set(DMA_XOR, dma->cap_mask);
1128 dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
1129
1130 if (ioat_dma->cap & IOAT_CAP_RAID16SS)
1131 dma->max_xor = 16;
1132 else
1133 dma->max_xor = 8;
1134 }
1135 }
1136
1137 dma->device_tx_status = ioat_tx_status;
Dave Jiangc0f28ce2015-08-11 08:48:43 -07001138
1139 /* starting with CB3.3 super extended descriptors are supported */
1140 if (ioat_dma->cap & IOAT_CAP_RAID16SS) {
1141 char pool_name[14];
1142 int i;
1143
1144 for (i = 0; i < MAX_SED_POOLS; i++) {
1145 snprintf(pool_name, 14, "ioat_hw%d_sed", i);
1146
1147 /* allocate SED DMA pool */
1148 ioat_dma->sed_hw_pool[i] = dmam_pool_create(pool_name,
1149 &pdev->dev,
1150 SED_SIZE * (i + 1), 64, 0);
1151 if (!ioat_dma->sed_hw_pool[i])
1152 return -ENOMEM;
1153
1154 }
1155 }
1156
1157 if (!(ioat_dma->cap & (IOAT_CAP_XOR | IOAT_CAP_PQ)))
1158 dma_cap_set(DMA_PRIVATE, dma->cap_mask);
1159
1160 err = ioat_probe(ioat_dma);
1161 if (err)
1162 return err;
1163
1164 list_for_each_entry(c, &dma->channels, device_node) {
1165 ioat_chan = to_ioat_chan(c);
1166 writel(IOAT_DMA_DCA_ANY_CPU,
1167 ioat_chan->reg_base + IOAT_DCACTRL_OFFSET);
1168 }
1169
1170 err = ioat_register(ioat_dma);
1171 if (err)
1172 return err;
1173
1174 ioat_kobject_add(ioat_dma, &ioat_ktype);
1175
1176 if (dca)
Dave Jiang3372de52015-08-11 08:48:55 -07001177 ioat_dma->dca = ioat_dca_init(pdev, ioat_dma->reg_base);
Dave Jiangc0f28ce2015-08-11 08:48:43 -07001178
1179 return 0;
1180}
1181
Dave Jiangad4a7b52015-08-26 13:17:24 -07001182static void ioat_shutdown(struct pci_dev *pdev)
1183{
1184 struct ioatdma_device *ioat_dma = pci_get_drvdata(pdev);
1185 struct ioatdma_chan *ioat_chan;
1186 int i;
1187
1188 if (!ioat_dma)
1189 return;
1190
1191 for (i = 0; i < IOAT_MAX_CHANS; i++) {
1192 ioat_chan = ioat_dma->idx[i];
1193 if (!ioat_chan)
1194 continue;
1195
1196 spin_lock_bh(&ioat_chan->prep_lock);
1197 set_bit(IOAT_CHAN_DOWN, &ioat_chan->state);
1198 del_timer_sync(&ioat_chan->timer);
1199 spin_unlock_bh(&ioat_chan->prep_lock);
1200 /* this should quiesce then reset */
1201 ioat_reset_hw(ioat_chan);
1202 }
1203
1204 ioat_disable_interrupts(ioat_dma);
1205}
1206
Dave Jiang4222a902015-08-26 13:17:30 -07001207void ioat_resume(struct ioatdma_device *ioat_dma)
1208{
1209 struct ioatdma_chan *ioat_chan;
1210 u32 chanerr;
1211 int i;
1212
1213 for (i = 0; i < IOAT_MAX_CHANS; i++) {
1214 ioat_chan = ioat_dma->idx[i];
1215 if (!ioat_chan)
1216 continue;
1217
1218 spin_lock_bh(&ioat_chan->prep_lock);
1219 clear_bit(IOAT_CHAN_DOWN, &ioat_chan->state);
1220 spin_unlock_bh(&ioat_chan->prep_lock);
1221
1222 chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
1223 writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
1224
1225 /* no need to reset as shutdown already did that */
1226 }
1227}
1228
Dave Jiangc0f28ce2015-08-11 08:48:43 -07001229#define DRV_NAME "ioatdma"
1230
Dave Jiang4222a902015-08-26 13:17:30 -07001231static pci_ers_result_t ioat_pcie_error_detected(struct pci_dev *pdev,
1232 enum pci_channel_state error)
1233{
1234 dev_dbg(&pdev->dev, "%s: PCIe AER error %d\n", DRV_NAME, error);
1235
1236 /* quiesce and block I/O */
1237 ioat_shutdown(pdev);
1238
1239 return PCI_ERS_RESULT_NEED_RESET;
1240}
1241
1242static pci_ers_result_t ioat_pcie_error_slot_reset(struct pci_dev *pdev)
1243{
1244 pci_ers_result_t result = PCI_ERS_RESULT_RECOVERED;
1245 int err;
1246
1247 dev_dbg(&pdev->dev, "%s post reset handling\n", DRV_NAME);
1248
1249 if (pci_enable_device_mem(pdev) < 0) {
1250 dev_err(&pdev->dev,
1251 "Failed to enable PCIe device after reset.\n");
1252 result = PCI_ERS_RESULT_DISCONNECT;
1253 } else {
1254 pci_set_master(pdev);
1255 pci_restore_state(pdev);
1256 pci_save_state(pdev);
1257 pci_wake_from_d3(pdev, false);
1258 }
1259
1260 err = pci_cleanup_aer_uncorrect_error_status(pdev);
1261 if (err) {
1262 dev_err(&pdev->dev,
1263 "AER uncorrect error status clear failed: %#x\n", err);
1264 }
1265
1266 return result;
1267}
1268
1269static void ioat_pcie_error_resume(struct pci_dev *pdev)
1270{
1271 struct ioatdma_device *ioat_dma = pci_get_drvdata(pdev);
1272
1273 dev_dbg(&pdev->dev, "%s: AER handling resuming\n", DRV_NAME);
1274
1275 /* initialize and bring everything back */
1276 ioat_resume(ioat_dma);
1277}
1278
1279static const struct pci_error_handlers ioat_err_handler = {
1280 .error_detected = ioat_pcie_error_detected,
1281 .slot_reset = ioat_pcie_error_slot_reset,
1282 .resume = ioat_pcie_error_resume,
1283};
1284
Dave Jiangc0f28ce2015-08-11 08:48:43 -07001285static struct pci_driver ioat_pci_driver = {
1286 .name = DRV_NAME,
1287 .id_table = ioat_pci_tbl,
1288 .probe = ioat_pci_probe,
1289 .remove = ioat_remove,
Dave Jiangad4a7b52015-08-26 13:17:24 -07001290 .shutdown = ioat_shutdown,
Dave Jiang4222a902015-08-26 13:17:30 -07001291 .err_handler = &ioat_err_handler,
Dave Jiangc0f28ce2015-08-11 08:48:43 -07001292};
1293
1294static struct ioatdma_device *
1295alloc_ioatdma(struct pci_dev *pdev, void __iomem *iobase)
1296{
1297 struct device *dev = &pdev->dev;
1298 struct ioatdma_device *d = devm_kzalloc(dev, sizeof(*d), GFP_KERNEL);
1299
1300 if (!d)
1301 return NULL;
1302 d->pdev = pdev;
1303 d->reg_base = iobase;
1304 return d;
1305}
1306
1307static int ioat_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1308{
1309 void __iomem * const *iomap;
1310 struct device *dev = &pdev->dev;
1311 struct ioatdma_device *device;
1312 int err;
1313
1314 err = pcim_enable_device(pdev);
1315 if (err)
1316 return err;
1317
1318 err = pcim_iomap_regions(pdev, 1 << IOAT_MMIO_BAR, DRV_NAME);
1319 if (err)
1320 return err;
1321 iomap = pcim_iomap_table(pdev);
1322 if (!iomap)
1323 return -ENOMEM;
1324
1325 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1326 if (err)
1327 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1328 if (err)
1329 return err;
1330
1331 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1332 if (err)
1333 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1334 if (err)
1335 return err;
1336
1337 device = alloc_ioatdma(pdev, iomap[IOAT_MMIO_BAR]);
1338 if (!device)
1339 return -ENOMEM;
1340 pci_set_master(pdev);
1341 pci_set_drvdata(pdev, device);
1342
1343 device->version = readb(device->reg_base + IOAT_VER_OFFSET);
Dave Jiang4222a902015-08-26 13:17:30 -07001344 if (device->version >= IOAT_VER_3_0) {
Dave Jiangc0f28ce2015-08-11 08:48:43 -07001345 err = ioat3_dma_probe(device, ioat_dca_enabled);
Dave Jiang4222a902015-08-26 13:17:30 -07001346
1347 if (device->version >= IOAT_VER_3_3)
1348 pci_enable_pcie_error_reporting(pdev);
1349 } else
Dave Jiangc0f28ce2015-08-11 08:48:43 -07001350 return -ENODEV;
1351
1352 if (err) {
1353 dev_err(dev, "Intel(R) I/OAT DMA Engine init failed\n");
Dave Jiang4222a902015-08-26 13:17:30 -07001354 pci_disable_pcie_error_reporting(pdev);
Dave Jiangc0f28ce2015-08-11 08:48:43 -07001355 return -ENODEV;
1356 }
1357
1358 return 0;
1359}
1360
1361static void ioat_remove(struct pci_dev *pdev)
1362{
1363 struct ioatdma_device *device = pci_get_drvdata(pdev);
1364
1365 if (!device)
1366 return;
1367
1368 dev_err(&pdev->dev, "Removing dma and dca services\n");
1369 if (device->dca) {
1370 unregister_dca_provider(device->dca, &pdev->dev);
1371 free_dca_provider(device->dca);
1372 device->dca = NULL;
1373 }
Dave Jiang4222a902015-08-26 13:17:30 -07001374
1375 pci_disable_pcie_error_reporting(pdev);
Dave Jiangc0f28ce2015-08-11 08:48:43 -07001376 ioat_dma_remove(device);
1377}
1378
1379static int __init ioat_init_module(void)
1380{
1381 int err = -ENOMEM;
1382
1383 pr_info("%s: Intel(R) QuickData Technology Driver %s\n",
1384 DRV_NAME, IOAT_DMA_VERSION);
1385
1386 ioat_cache = kmem_cache_create("ioat", sizeof(struct ioat_ring_ent),
1387 0, SLAB_HWCACHE_ALIGN, NULL);
1388 if (!ioat_cache)
1389 return -ENOMEM;
1390
1391 ioat_sed_cache = KMEM_CACHE(ioat_sed_ent, 0);
1392 if (!ioat_sed_cache)
1393 goto err_ioat_cache;
1394
1395 err = pci_register_driver(&ioat_pci_driver);
1396 if (err)
1397 goto err_ioat3_cache;
1398
1399 return 0;
1400
1401 err_ioat3_cache:
1402 kmem_cache_destroy(ioat_sed_cache);
1403
1404 err_ioat_cache:
1405 kmem_cache_destroy(ioat_cache);
1406
1407 return err;
1408}
1409module_init(ioat_init_module);
1410
1411static void __exit ioat_exit_module(void)
1412{
1413 pci_unregister_driver(&ioat_pci_driver);
1414 kmem_cache_destroy(ioat_cache);
1415}
1416module_exit(ioat_exit_module);