blob: 77a6dcf25b98b818b9d83ecbbf0d99edc3783056 [file] [log] [blame]
Dan Williamsbf40a682009-09-08 17:42:55 -07001/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2004 - 2009 Intel Corporation. All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License,
11 * version 2, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * The full GNU General Public License is included in this distribution in
23 * the file called "COPYING".
24 *
25 * BSD LICENSE
26 *
27 * Copyright(c) 2004-2009 Intel Corporation. All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions are met:
31 *
32 * * Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * * Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in
36 * the documentation and/or other materials provided with the
37 * distribution.
38 * * Neither the name of Intel Corporation nor the names of its
39 * contributors may be used to endorse or promote products derived
40 * from this software without specific prior written permission.
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
46 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
47 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
48 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
49 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
50 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
52 * POSSIBILITY OF SUCH DAMAGE.
53 */
54
55/*
56 * Support routines for v3+ hardware
57 */
Dave Jiang7727eaa2013-04-15 10:25:56 -070058#include <linux/module.h>
Dan Williamsbf40a682009-09-08 17:42:55 -070059#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090060#include <linux/gfp.h>
Dan Williamsbf40a682009-09-08 17:42:55 -070061#include <linux/dmaengine.h>
62#include <linux/dma-mapping.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040063#include <linux/prefetch.h>
Vinod Koul949ff5b2012-03-13 11:58:12 +053064#include "../dmaengine.h"
Dan Williamsbf40a682009-09-08 17:42:55 -070065#include "registers.h"
66#include "hw.h"
67#include "dma.h"
68#include "dma_v2.h"
69
Dan Williams59056e82013-11-13 10:57:18 -080070extern struct kmem_cache *ioat3_sed_cache;
71
Dan Williamsb094ad32009-09-08 17:42:57 -070072/* ioat hardware assumes at least two sources for raid operations */
73#define src_cnt_to_sw(x) ((x) + 2)
74#define src_cnt_to_hw(x) ((x) - 2)
Dave Jiang7727eaa2013-04-15 10:25:56 -070075#define ndest_to_sw(x) ((x) + 1)
76#define ndest_to_hw(x) ((x) - 1)
77#define src16_cnt_to_sw(x) ((x) + 9)
78#define src16_cnt_to_hw(x) ((x) - 9)
Dan Williamsb094ad32009-09-08 17:42:57 -070079
80/* provide a lookup table for setting the source address in the base or
Dan Williamsd69d235b2009-09-08 17:42:59 -070081 * extended descriptor of an xor or pq descriptor
Dan Williamsb094ad32009-09-08 17:42:57 -070082 */
Dan Williamsd0b0c8c2011-07-22 14:20:46 -070083static const u8 xor_idx_to_desc = 0xe0;
Andi Kleen9b487ce2011-06-07 15:26:33 -070084static const u8 xor_idx_to_field[] = { 1, 4, 5, 6, 7, 0, 1, 2 };
85static const u8 pq_idx_to_desc = 0xf8;
Dave Jiang7727eaa2013-04-15 10:25:56 -070086static const u8 pq16_idx_to_desc[] = { 0, 0, 1, 1, 1, 1, 1, 1, 1,
87 2, 2, 2, 2, 2, 2, 2 };
Andi Kleen9b487ce2011-06-07 15:26:33 -070088static const u8 pq_idx_to_field[] = { 1, 4, 5, 0, 1, 2, 4, 5 };
Dave Jiang7727eaa2013-04-15 10:25:56 -070089static const u8 pq16_idx_to_field[] = { 1, 4, 1, 2, 3, 4, 5, 6, 7,
90 0, 1, 2, 3, 4, 5, 6 };
91
Dave Jiang3f09ede2013-03-26 15:43:09 -070092static void ioat3_eh(struct ioat2_dma_chan *ioat);
93
Dan Williamsb094ad32009-09-08 17:42:57 -070094static void xor_set_src(struct ioat_raw_descriptor *descs[2],
95 dma_addr_t addr, u32 offset, int idx)
96{
97 struct ioat_raw_descriptor *raw = descs[xor_idx_to_desc >> idx & 1];
98
99 raw->field[xor_idx_to_field[idx]] = addr + offset;
100}
101
Dan Williamsd69d235b2009-09-08 17:42:59 -0700102static dma_addr_t pq_get_src(struct ioat_raw_descriptor *descs[2], int idx)
103{
104 struct ioat_raw_descriptor *raw = descs[pq_idx_to_desc >> idx & 1];
105
106 return raw->field[pq_idx_to_field[idx]];
107}
108
Dave Jiang7727eaa2013-04-15 10:25:56 -0700109static dma_addr_t pq16_get_src(struct ioat_raw_descriptor *desc[3], int idx)
110{
111 struct ioat_raw_descriptor *raw = desc[pq16_idx_to_desc[idx]];
112
113 return raw->field[pq16_idx_to_field[idx]];
114}
115
Dan Williamsd69d235b2009-09-08 17:42:59 -0700116static void pq_set_src(struct ioat_raw_descriptor *descs[2],
117 dma_addr_t addr, u32 offset, u8 coef, int idx)
118{
119 struct ioat_pq_descriptor *pq = (struct ioat_pq_descriptor *) descs[0];
120 struct ioat_raw_descriptor *raw = descs[pq_idx_to_desc >> idx & 1];
121
122 raw->field[pq_idx_to_field[idx]] = addr + offset;
123 pq->coef[idx] = coef;
124}
125
Dave Jiang8a52b9f2013-03-26 15:42:47 -0700126static bool is_jf_ioat(struct pci_dev *pdev)
127{
128 switch (pdev->device) {
129 case PCI_DEVICE_ID_INTEL_IOAT_JSF0:
130 case PCI_DEVICE_ID_INTEL_IOAT_JSF1:
131 case PCI_DEVICE_ID_INTEL_IOAT_JSF2:
132 case PCI_DEVICE_ID_INTEL_IOAT_JSF3:
133 case PCI_DEVICE_ID_INTEL_IOAT_JSF4:
134 case PCI_DEVICE_ID_INTEL_IOAT_JSF5:
135 case PCI_DEVICE_ID_INTEL_IOAT_JSF6:
136 case PCI_DEVICE_ID_INTEL_IOAT_JSF7:
137 case PCI_DEVICE_ID_INTEL_IOAT_JSF8:
138 case PCI_DEVICE_ID_INTEL_IOAT_JSF9:
139 return true;
140 default:
141 return false;
142 }
143}
144
145static bool is_snb_ioat(struct pci_dev *pdev)
146{
147 switch (pdev->device) {
148 case PCI_DEVICE_ID_INTEL_IOAT_SNB0:
149 case PCI_DEVICE_ID_INTEL_IOAT_SNB1:
150 case PCI_DEVICE_ID_INTEL_IOAT_SNB2:
151 case PCI_DEVICE_ID_INTEL_IOAT_SNB3:
152 case PCI_DEVICE_ID_INTEL_IOAT_SNB4:
153 case PCI_DEVICE_ID_INTEL_IOAT_SNB5:
154 case PCI_DEVICE_ID_INTEL_IOAT_SNB6:
155 case PCI_DEVICE_ID_INTEL_IOAT_SNB7:
156 case PCI_DEVICE_ID_INTEL_IOAT_SNB8:
157 case PCI_DEVICE_ID_INTEL_IOAT_SNB9:
158 return true;
159 default:
160 return false;
161 }
162}
163
164static bool is_ivb_ioat(struct pci_dev *pdev)
165{
166 switch (pdev->device) {
167 case PCI_DEVICE_ID_INTEL_IOAT_IVB0:
168 case PCI_DEVICE_ID_INTEL_IOAT_IVB1:
169 case PCI_DEVICE_ID_INTEL_IOAT_IVB2:
170 case PCI_DEVICE_ID_INTEL_IOAT_IVB3:
171 case PCI_DEVICE_ID_INTEL_IOAT_IVB4:
172 case PCI_DEVICE_ID_INTEL_IOAT_IVB5:
173 case PCI_DEVICE_ID_INTEL_IOAT_IVB6:
174 case PCI_DEVICE_ID_INTEL_IOAT_IVB7:
175 case PCI_DEVICE_ID_INTEL_IOAT_IVB8:
176 case PCI_DEVICE_ID_INTEL_IOAT_IVB9:
177 return true;
178 default:
179 return false;
180 }
181
182}
183
184static bool is_hsw_ioat(struct pci_dev *pdev)
185{
186 switch (pdev->device) {
187 case PCI_DEVICE_ID_INTEL_IOAT_HSW0:
188 case PCI_DEVICE_ID_INTEL_IOAT_HSW1:
189 case PCI_DEVICE_ID_INTEL_IOAT_HSW2:
190 case PCI_DEVICE_ID_INTEL_IOAT_HSW3:
191 case PCI_DEVICE_ID_INTEL_IOAT_HSW4:
192 case PCI_DEVICE_ID_INTEL_IOAT_HSW5:
193 case PCI_DEVICE_ID_INTEL_IOAT_HSW6:
194 case PCI_DEVICE_ID_INTEL_IOAT_HSW7:
195 case PCI_DEVICE_ID_INTEL_IOAT_HSW8:
196 case PCI_DEVICE_ID_INTEL_IOAT_HSW9:
197 return true;
198 default:
199 return false;
200 }
201
202}
203
204static bool is_xeon_cb32(struct pci_dev *pdev)
205{
206 return is_jf_ioat(pdev) || is_snb_ioat(pdev) || is_ivb_ioat(pdev) ||
207 is_hsw_ioat(pdev);
208}
209
210static bool is_bwd_ioat(struct pci_dev *pdev)
211{
212 switch (pdev->device) {
213 case PCI_DEVICE_ID_INTEL_IOAT_BWD0:
214 case PCI_DEVICE_ID_INTEL_IOAT_BWD1:
215 case PCI_DEVICE_ID_INTEL_IOAT_BWD2:
216 case PCI_DEVICE_ID_INTEL_IOAT_BWD3:
Dave Jiang68a8cc92015-01-30 15:06:01 -0700217 /* even though not Atom, BDX-DE has same DMA silicon */
218 case PCI_DEVICE_ID_INTEL_IOAT_BDXDE0:
219 case PCI_DEVICE_ID_INTEL_IOAT_BDXDE1:
220 case PCI_DEVICE_ID_INTEL_IOAT_BDXDE2:
221 case PCI_DEVICE_ID_INTEL_IOAT_BDXDE3:
Dave Jiang8a52b9f2013-03-26 15:42:47 -0700222 return true;
223 default:
224 return false;
225 }
226}
227
Dave Jiangd3023982013-04-10 16:44:32 -0700228static bool is_bwd_noraid(struct pci_dev *pdev)
229{
230 switch (pdev->device) {
231 case PCI_DEVICE_ID_INTEL_IOAT_BWD2:
232 case PCI_DEVICE_ID_INTEL_IOAT_BWD3:
233 return true;
234 default:
235 return false;
236 }
237
238}
239
Dave Jiang7727eaa2013-04-15 10:25:56 -0700240static void pq16_set_src(struct ioat_raw_descriptor *desc[3],
Paul Bollee6a5fa62013-07-20 20:05:05 +0200241 dma_addr_t addr, u32 offset, u8 coef, unsigned idx)
Dave Jiang7727eaa2013-04-15 10:25:56 -0700242{
243 struct ioat_pq_descriptor *pq = (struct ioat_pq_descriptor *)desc[0];
244 struct ioat_pq16a_descriptor *pq16 =
245 (struct ioat_pq16a_descriptor *)desc[1];
246 struct ioat_raw_descriptor *raw = desc[pq16_idx_to_desc[idx]];
247
248 raw->field[pq16_idx_to_field[idx]] = addr + offset;
249
250 if (idx < 8)
251 pq->coef[idx] = coef;
252 else
253 pq16->coef[idx - 8] = coef;
254}
255
Fengguang Wue6a30fe2013-04-16 13:41:26 +0800256static struct ioat_sed_ent *
Dave Jiang7727eaa2013-04-15 10:25:56 -0700257ioat3_alloc_sed(struct ioatdma_device *device, unsigned int hw_pool)
258{
259 struct ioat_sed_ent *sed;
260 gfp_t flags = __GFP_ZERO | GFP_ATOMIC;
261
Dan Williams59056e82013-11-13 10:57:18 -0800262 sed = kmem_cache_alloc(ioat3_sed_cache, flags);
Dave Jiang7727eaa2013-04-15 10:25:56 -0700263 if (!sed)
264 return NULL;
265
266 sed->hw_pool = hw_pool;
267 sed->hw = dma_pool_alloc(device->sed_hw_pool[hw_pool],
268 flags, &sed->dma);
269 if (!sed->hw) {
Dan Williams59056e82013-11-13 10:57:18 -0800270 kmem_cache_free(ioat3_sed_cache, sed);
Dave Jiang7727eaa2013-04-15 10:25:56 -0700271 return NULL;
272 }
273
274 return sed;
275}
276
Fengguang Wue6a30fe2013-04-16 13:41:26 +0800277static void ioat3_free_sed(struct ioatdma_device *device, struct ioat_sed_ent *sed)
Dave Jiang7727eaa2013-04-15 10:25:56 -0700278{
279 if (!sed)
280 return;
281
282 dma_pool_free(device->sed_hw_pool[sed->hw_pool], sed->hw, sed->dma);
Dan Williams59056e82013-11-13 10:57:18 -0800283 kmem_cache_free(ioat3_sed_cache, sed);
Dan Williamsbf40a682009-09-08 17:42:55 -0700284}
285
Dan Williamsb094ad32009-09-08 17:42:57 -0700286static bool desc_has_ext(struct ioat_ring_ent *desc)
287{
288 struct ioat_dma_descriptor *hw = desc->hw;
Dan Williamsbf40a682009-09-08 17:42:55 -0700289
Dan Williamsb094ad32009-09-08 17:42:57 -0700290 if (hw->ctl_f.op == IOAT_OP_XOR ||
291 hw->ctl_f.op == IOAT_OP_XOR_VAL) {
292 struct ioat_xor_descriptor *xor = desc->xor;
293
294 if (src_cnt_to_sw(xor->ctl_f.src_cnt) > 5)
295 return true;
Dan Williamsd69d235b2009-09-08 17:42:59 -0700296 } else if (hw->ctl_f.op == IOAT_OP_PQ ||
297 hw->ctl_f.op == IOAT_OP_PQ_VAL) {
298 struct ioat_pq_descriptor *pq = desc->pq;
299
300 if (src_cnt_to_sw(pq->ctl_f.src_cnt) > 3)
301 return true;
Dan Williamsb094ad32009-09-08 17:42:57 -0700302 }
303
304 return false;
305}
306
Dave Jiang3f09ede2013-03-26 15:43:09 -0700307static u64 ioat3_get_current_completion(struct ioat_chan_common *chan)
308{
309 u64 phys_complete;
310 u64 completion;
311
312 completion = *chan->completion;
313 phys_complete = ioat_chansts_to_addr(completion);
314
315 dev_dbg(to_dev(chan), "%s: phys_complete: %#llx\n", __func__,
316 (unsigned long long) phys_complete);
317
318 return phys_complete;
319}
320
321static bool ioat3_cleanup_preamble(struct ioat_chan_common *chan,
322 u64 *phys_complete)
323{
324 *phys_complete = ioat3_get_current_completion(chan);
325 if (*phys_complete == chan->last_completion)
326 return false;
327
328 clear_bit(IOAT_COMPLETION_ACK, &chan->state);
329 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
330
331 return true;
332}
333
Dave Jiang75c6f0a2013-04-10 16:44:39 -0700334static void
335desc_get_errstat(struct ioat2_dma_chan *ioat, struct ioat_ring_ent *desc)
336{
337 struct ioat_dma_descriptor *hw = desc->hw;
338
339 switch (hw->ctl_f.op) {
340 case IOAT_OP_PQ_VAL:
341 case IOAT_OP_PQ_VAL_16S:
342 {
343 struct ioat_pq_descriptor *pq = desc->pq;
344
345 /* check if there's error written */
346 if (!pq->dwbes_f.wbes)
347 return;
348
349 /* need to set a chanerr var for checking to clear later */
350
351 if (pq->dwbes_f.p_val_err)
352 *desc->result |= SUM_CHECK_P_RESULT;
353
354 if (pq->dwbes_f.q_val_err)
355 *desc->result |= SUM_CHECK_Q_RESULT;
356
357 return;
358 }
359 default:
360 return;
361 }
362}
363
Dan Williamsb094ad32009-09-08 17:42:57 -0700364/**
365 * __cleanup - reclaim used descriptors
366 * @ioat: channel (ring) to clean
367 *
368 * The difference from the dma_v2.c __cleanup() is that this routine
369 * handles extended descriptors and dma-unmapping raid operations.
370 */
Dan Williams27502932012-03-23 13:36:42 -0700371static void __cleanup(struct ioat2_dma_chan *ioat, dma_addr_t phys_complete)
Dan Williamsbf40a682009-09-08 17:42:55 -0700372{
373 struct ioat_chan_common *chan = &ioat->base;
Dave Jiang7727eaa2013-04-15 10:25:56 -0700374 struct ioatdma_device *device = chan->device;
Dan Williamsbf40a682009-09-08 17:42:55 -0700375 struct ioat_ring_ent *desc;
376 bool seen_current = false;
Dan Williams074cc472010-05-01 15:22:55 -0700377 int idx = ioat->tail, i;
Dan Williamsbf40a682009-09-08 17:42:55 -0700378 u16 active;
Dan Williamsbf40a682009-09-08 17:42:55 -0700379
380 dev_dbg(to_dev(chan), "%s: head: %#x tail: %#x issued: %#x\n",
381 __func__, ioat->head, ioat->tail, ioat->issued);
382
Dave Jiang3f09ede2013-03-26 15:43:09 -0700383 /*
384 * At restart of the channel, the completion address and the
385 * channel status will be 0 due to starting a new chain. Since
386 * it's new chain and the first descriptor "fails", there is
387 * nothing to clean up. We do not want to reap the entire submitted
388 * chain due to this 0 address value and then BUG.
389 */
390 if (!phys_complete)
391 return;
392
Dan Williamsbf40a682009-09-08 17:42:55 -0700393 active = ioat2_ring_active(ioat);
394 for (i = 0; i < active && !seen_current; i++) {
395 struct dma_async_tx_descriptor *tx;
396
Dan Williams074cc472010-05-01 15:22:55 -0700397 smp_read_barrier_depends();
398 prefetch(ioat2_get_ring_ent(ioat, idx + i + 1));
399 desc = ioat2_get_ring_ent(ioat, idx + i);
Dan Williamsbf40a682009-09-08 17:42:55 -0700400 dump_desc_dbg(ioat, desc);
Dave Jiang75c6f0a2013-04-10 16:44:39 -0700401
402 /* set err stat if we are using dwbes */
403 if (device->cap & IOAT_CAP_DWBES)
404 desc_get_errstat(ioat, desc);
405
Dan Williamsbf40a682009-09-08 17:42:55 -0700406 tx = &desc->txd;
407 if (tx->cookie) {
Russell King - ARM Linuxf7fbce02012-03-06 22:35:07 +0000408 dma_cookie_complete(tx);
Dan Williamsd38a8c62013-10-18 19:35:23 +0200409 dma_descriptor_unmap(tx);
Dan Williamsbf40a682009-09-08 17:42:55 -0700410 if (tx->callback) {
411 tx->callback(tx->callback_param);
412 tx->callback = NULL;
413 }
414 }
415
416 if (tx->phys == phys_complete)
417 seen_current = true;
Dan Williamsb094ad32009-09-08 17:42:57 -0700418
419 /* skip extended descriptors */
420 if (desc_has_ext(desc)) {
421 BUG_ON(i + 1 >= active);
422 i++;
423 }
Dave Jiang7727eaa2013-04-15 10:25:56 -0700424
425 /* cleanup super extended descriptors */
426 if (desc->sed) {
427 ioat3_free_sed(device, desc->sed);
428 desc->sed = NULL;
429 }
Dan Williamsbf40a682009-09-08 17:42:55 -0700430 }
Dan Williams074cc472010-05-01 15:22:55 -0700431 smp_mb(); /* finish all descriptor reads before incrementing tail */
432 ioat->tail = idx + i;
Dan Williamsaa75db02010-03-03 21:21:10 -0700433 BUG_ON(active && !seen_current); /* no active descs have written a completion? */
Dan Williamsbf40a682009-09-08 17:42:55 -0700434 chan->last_completion = phys_complete;
Dan Williamsb9cc9862010-03-03 21:21:13 -0700435
Dan Williams074cc472010-05-01 15:22:55 -0700436 if (active - i == 0) {
Dan Williamsbf40a682009-09-08 17:42:55 -0700437 dev_dbg(to_dev(chan), "%s: cancel completion timeout\n",
438 __func__);
439 clear_bit(IOAT_COMPLETION_PENDING, &chan->state);
440 mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
441 }
Dan Williamsb9cc9862010-03-03 21:21:13 -0700442 /* 5 microsecond delay per pending descriptor */
Dan Williams074cc472010-05-01 15:22:55 -0700443 writew(min((5 * (active - i)), IOAT_INTRDELAY_MASK),
Dan Williamsb9cc9862010-03-03 21:21:13 -0700444 chan->device->reg_base + IOAT_INTRDELAY_OFFSET);
Dan Williamsbf40a682009-09-08 17:42:55 -0700445}
446
Dan Williams074cc472010-05-01 15:22:55 -0700447static void ioat3_cleanup(struct ioat2_dma_chan *ioat)
Dan Williamsbf40a682009-09-08 17:42:55 -0700448{
449 struct ioat_chan_common *chan = &ioat->base;
Dave Jiang3f09ede2013-03-26 15:43:09 -0700450 u64 phys_complete;
Dan Williamsbf40a682009-09-08 17:42:55 -0700451
Dan Williamsb9cc9862010-03-03 21:21:13 -0700452 spin_lock_bh(&chan->cleanup_lock);
Dave Jiang3f09ede2013-03-26 15:43:09 -0700453
454 if (ioat3_cleanup_preamble(chan, &phys_complete))
Dan Williams074cc472010-05-01 15:22:55 -0700455 __cleanup(ioat, phys_complete);
Dave Jiang3f09ede2013-03-26 15:43:09 -0700456
457 if (is_ioat_halted(*chan->completion)) {
458 u32 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
459
460 if (chanerr & IOAT_CHANERR_HANDLE_MASK) {
461 mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
462 ioat3_eh(ioat);
463 }
464 }
465
Dan Williamsb9cc9862010-03-03 21:21:13 -0700466 spin_unlock_bh(&chan->cleanup_lock);
467}
468
Dan Williamsaa4d72a2010-03-03 21:21:13 -0700469static void ioat3_cleanup_event(unsigned long data)
Dan Williamsbf40a682009-09-08 17:42:55 -0700470{
Dan Williamsaa4d72a2010-03-03 21:21:13 -0700471 struct ioat2_dma_chan *ioat = to_ioat2_chan((void *) data);
Dan Williamsda87ca42014-02-19 16:19:35 -0800472 struct ioat_chan_common *chan = &ioat->base;
Dan Williamsbf40a682009-09-08 17:42:55 -0700473
Dan Williams074cc472010-05-01 15:22:55 -0700474 ioat3_cleanup(ioat);
Dan Williamsda87ca42014-02-19 16:19:35 -0800475 if (!test_bit(IOAT_RUN, &chan->state))
476 return;
Dan Williams773d9e22010-03-03 11:47:42 -0700477 writew(IOAT_CHANCTRL_RUN, ioat->base.reg_base + IOAT_CHANCTRL_OFFSET);
Dan Williamsbf40a682009-09-08 17:42:55 -0700478}
479
480static void ioat3_restart_channel(struct ioat2_dma_chan *ioat)
481{
482 struct ioat_chan_common *chan = &ioat->base;
Dave Jiang3f09ede2013-03-26 15:43:09 -0700483 u64 phys_complete;
Dan Williamsbf40a682009-09-08 17:42:55 -0700484
Dan Williamsb372ec22010-03-03 11:47:42 -0700485 ioat2_quiesce(chan, 0);
Dave Jiang3f09ede2013-03-26 15:43:09 -0700486 if (ioat3_cleanup_preamble(chan, &phys_complete))
Dan Williamsbf40a682009-09-08 17:42:55 -0700487 __cleanup(ioat, phys_complete);
488
489 __ioat2_restart_chan(ioat);
490}
491
Dave Jiang3f09ede2013-03-26 15:43:09 -0700492static void ioat3_eh(struct ioat2_dma_chan *ioat)
493{
494 struct ioat_chan_common *chan = &ioat->base;
495 struct pci_dev *pdev = to_pdev(chan);
496 struct ioat_dma_descriptor *hw;
Dave Jiangabf538a2014-12-11 09:13:42 -0700497 struct dma_async_tx_descriptor *tx;
Dave Jiang3f09ede2013-03-26 15:43:09 -0700498 u64 phys_complete;
499 struct ioat_ring_ent *desc;
500 u32 err_handled = 0;
501 u32 chanerr_int;
502 u32 chanerr;
503
504 /* cleanup so tail points to descriptor that caused the error */
505 if (ioat3_cleanup_preamble(chan, &phys_complete))
506 __cleanup(ioat, phys_complete);
507
508 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
509 pci_read_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, &chanerr_int);
510
511 dev_dbg(to_dev(chan), "%s: error = %x:%x\n",
512 __func__, chanerr, chanerr_int);
513
514 desc = ioat2_get_ring_ent(ioat, ioat->tail);
515 hw = desc->hw;
516 dump_desc_dbg(ioat, desc);
517
518 switch (hw->ctl_f.op) {
519 case IOAT_OP_XOR_VAL:
520 if (chanerr & IOAT_CHANERR_XOR_P_OR_CRC_ERR) {
521 *desc->result |= SUM_CHECK_P_RESULT;
522 err_handled |= IOAT_CHANERR_XOR_P_OR_CRC_ERR;
523 }
524 break;
525 case IOAT_OP_PQ_VAL:
Dave Jiang7727eaa2013-04-15 10:25:56 -0700526 case IOAT_OP_PQ_VAL_16S:
Dave Jiang3f09ede2013-03-26 15:43:09 -0700527 if (chanerr & IOAT_CHANERR_XOR_P_OR_CRC_ERR) {
528 *desc->result |= SUM_CHECK_P_RESULT;
529 err_handled |= IOAT_CHANERR_XOR_P_OR_CRC_ERR;
530 }
531 if (chanerr & IOAT_CHANERR_XOR_Q_ERR) {
532 *desc->result |= SUM_CHECK_Q_RESULT;
533 err_handled |= IOAT_CHANERR_XOR_Q_ERR;
534 }
535 break;
536 }
537
538 /* fault on unhandled error or spurious halt */
539 if (chanerr ^ err_handled || chanerr == 0) {
540 dev_err(to_dev(chan), "%s: fatal error (%x:%x)\n",
541 __func__, chanerr, err_handled);
542 BUG();
Dave Jiangabf538a2014-12-11 09:13:42 -0700543 } else { /* cleanup the faulty descriptor */
544 tx = &desc->txd;
545 if (tx->cookie) {
546 dma_cookie_complete(tx);
547 dma_descriptor_unmap(tx);
548 if (tx->callback) {
549 tx->callback(tx->callback_param);
550 tx->callback = NULL;
551 }
552 }
Dave Jiang3f09ede2013-03-26 15:43:09 -0700553 }
554
555 writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
556 pci_write_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, chanerr_int);
557
558 /* mark faulting descriptor as complete */
559 *chan->completion = desc->txd.phys;
560
561 spin_lock_bh(&ioat->prep_lock);
562 ioat3_restart_channel(ioat);
563 spin_unlock_bh(&ioat->prep_lock);
564}
565
Dave Jiang4dec23d2013-02-07 14:38:32 -0700566static void check_active(struct ioat2_dma_chan *ioat)
Dan Williamsbf40a682009-09-08 17:42:55 -0700567{
Dan Williamsbf40a682009-09-08 17:42:55 -0700568 struct ioat_chan_common *chan = &ioat->base;
569
Dave Jiang4dec23d2013-02-07 14:38:32 -0700570 if (ioat2_ring_active(ioat)) {
571 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
572 return;
573 }
Dan Williamsbf40a682009-09-08 17:42:55 -0700574
Dave Jiang4dec23d2013-02-07 14:38:32 -0700575 if (test_and_clear_bit(IOAT_CHAN_ACTIVE, &chan->state))
576 mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
577 else if (ioat->alloc_order > ioat_get_alloc_order()) {
Dan Williamsbf40a682009-09-08 17:42:55 -0700578 /* if the ring is idle, empty, and oversized try to step
579 * down the size
580 */
Dave Jiang4dec23d2013-02-07 14:38:32 -0700581 reshape_ring(ioat, ioat->alloc_order - 1);
Dan Williamsbf40a682009-09-08 17:42:55 -0700582
583 /* keep shrinking until we get back to our minimum
584 * default size
585 */
586 if (ioat->alloc_order > ioat_get_alloc_order())
587 mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
588 }
Dave Jiang4dec23d2013-02-07 14:38:32 -0700589
590}
591
Fengguang Wua20702b2013-02-13 09:40:03 +0800592static void ioat3_timer_event(unsigned long data)
Dave Jiang4dec23d2013-02-07 14:38:32 -0700593{
594 struct ioat2_dma_chan *ioat = to_ioat2_chan((void *) data);
595 struct ioat_chan_common *chan = &ioat->base;
596 dma_addr_t phys_complete;
597 u64 status;
598
599 status = ioat_chansts(chan);
600
601 /* when halted due to errors check for channel
602 * programming errors before advancing the completion state
603 */
604 if (is_ioat_halted(status)) {
605 u32 chanerr;
606
607 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
608 dev_err(to_dev(chan), "%s: Channel halted (%x)\n",
609 __func__, chanerr);
610 if (test_bit(IOAT_RUN, &chan->state))
611 BUG_ON(is_ioat_bug(chanerr));
612 else /* we never got off the ground */
613 return;
614 }
615
616 /* if we haven't made progress and we have already
617 * acknowledged a pending completion once, then be more
618 * forceful with a restart
619 */
620 spin_lock_bh(&chan->cleanup_lock);
621 if (ioat_cleanup_preamble(chan, &phys_complete))
622 __cleanup(ioat, phys_complete);
623 else if (test_bit(IOAT_COMPLETION_ACK, &chan->state)) {
624 spin_lock_bh(&ioat->prep_lock);
625 ioat3_restart_channel(ioat);
626 spin_unlock_bh(&ioat->prep_lock);
627 spin_unlock_bh(&chan->cleanup_lock);
628 return;
629 } else {
630 set_bit(IOAT_COMPLETION_ACK, &chan->state);
631 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
632 }
633
634
635 if (ioat2_ring_active(ioat))
636 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
637 else {
638 spin_lock_bh(&ioat->prep_lock);
639 check_active(ioat);
640 spin_unlock_bh(&ioat->prep_lock);
641 }
642 spin_unlock_bh(&chan->cleanup_lock);
Dan Williamsbf40a682009-09-08 17:42:55 -0700643}
644
645static enum dma_status
Linus Walleij07934482010-03-26 16:50:49 -0700646ioat3_tx_status(struct dma_chan *c, dma_cookie_t cookie,
647 struct dma_tx_state *txstate)
Dan Williamsbf40a682009-09-08 17:42:55 -0700648{
649 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000650 enum dma_status ret;
Dan Williamsbf40a682009-09-08 17:42:55 -0700651
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000652 ret = dma_cookie_status(c, cookie, txstate);
Vinod Koul2f16f802013-10-16 20:48:52 +0530653 if (ret == DMA_COMPLETE)
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000654 return ret;
Dan Williamsbf40a682009-09-08 17:42:55 -0700655
Dan Williams074cc472010-05-01 15:22:55 -0700656 ioat3_cleanup(ioat);
Dan Williamsbf40a682009-09-08 17:42:55 -0700657
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000658 return dma_cookie_status(c, cookie, txstate);
Dan Williamsbf40a682009-09-08 17:42:55 -0700659}
660
661static struct dma_async_tx_descriptor *
Dan Williamsb094ad32009-09-08 17:42:57 -0700662__ioat3_prep_xor_lock(struct dma_chan *c, enum sum_check_flags *result,
663 dma_addr_t dest, dma_addr_t *src, unsigned int src_cnt,
664 size_t len, unsigned long flags)
665{
666 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
667 struct ioat_ring_ent *compl_desc;
668 struct ioat_ring_ent *desc;
669 struct ioat_ring_ent *ext;
670 size_t total_len = len;
671 struct ioat_xor_descriptor *xor;
672 struct ioat_xor_ext_descriptor *xor_ex = NULL;
673 struct ioat_dma_descriptor *hw;
Dan Williams074cc472010-05-01 15:22:55 -0700674 int num_descs, with_ext, idx, i;
Dan Williamsb094ad32009-09-08 17:42:57 -0700675 u32 offset = 0;
Dan Williamsb094ad32009-09-08 17:42:57 -0700676 u8 op = result ? IOAT_OP_XOR_VAL : IOAT_OP_XOR;
677
678 BUG_ON(src_cnt < 2);
679
680 num_descs = ioat2_xferlen_to_descs(ioat, len);
681 /* we need 2x the number of descriptors to cover greater than 5
682 * sources
683 */
684 if (src_cnt > 5) {
685 with_ext = 1;
686 num_descs *= 2;
687 } else
688 with_ext = 0;
689
690 /* completion writes from the raid engine may pass completion
691 * writes from the legacy engine, so we need one extra null
692 * (legacy) descriptor to ensure all completion writes arrive in
693 * order.
694 */
Dan Williams074cc472010-05-01 15:22:55 -0700695 if (likely(num_descs) && ioat2_check_space_lock(ioat, num_descs+1) == 0)
696 idx = ioat->head;
Dan Williamsb094ad32009-09-08 17:42:57 -0700697 else
698 return NULL;
Dan Williamscdef57d2009-09-21 09:22:29 -0700699 i = 0;
700 do {
Dan Williamsb094ad32009-09-08 17:42:57 -0700701 struct ioat_raw_descriptor *descs[2];
702 size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
703 int s;
704
705 desc = ioat2_get_ring_ent(ioat, idx + i);
706 xor = desc->xor;
707
708 /* save a branch by unconditionally retrieving the
709 * extended descriptor xor_set_src() knows to not write
710 * to it in the single descriptor case
711 */
712 ext = ioat2_get_ring_ent(ioat, idx + i + 1);
713 xor_ex = ext->xor_ex;
714
715 descs[0] = (struct ioat_raw_descriptor *) xor;
716 descs[1] = (struct ioat_raw_descriptor *) xor_ex;
717 for (s = 0; s < src_cnt; s++)
718 xor_set_src(descs, src[s], offset, s);
719 xor->size = xfer_size;
720 xor->dst_addr = dest + offset;
721 xor->ctl = 0;
722 xor->ctl_f.op = op;
723 xor->ctl_f.src_cnt = src_cnt_to_hw(src_cnt);
724
725 len -= xfer_size;
726 offset += xfer_size;
727 dump_desc_dbg(ioat, desc);
Dan Williamscdef57d2009-09-21 09:22:29 -0700728 } while ((i += 1 + with_ext) < num_descs);
Dan Williamsb094ad32009-09-08 17:42:57 -0700729
730 /* last xor descriptor carries the unmap parameters and fence bit */
731 desc->txd.flags = flags;
732 desc->len = total_len;
733 if (result)
734 desc->result = result;
735 xor->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
736
737 /* completion descriptor carries interrupt bit */
738 compl_desc = ioat2_get_ring_ent(ioat, idx + i);
739 compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
740 hw = compl_desc->hw;
741 hw->ctl = 0;
742 hw->ctl_f.null = 1;
743 hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
744 hw->ctl_f.compl_write = 1;
745 hw->size = NULL_DESC_BUFFER_SIZE;
746 dump_desc_dbg(ioat, compl_desc);
747
748 /* we leave the channel locked to ensure in order submission */
Dan Williams49954c12009-11-19 17:11:03 -0700749 return &compl_desc->txd;
Dan Williamsb094ad32009-09-08 17:42:57 -0700750}
751
752static struct dma_async_tx_descriptor *
753ioat3_prep_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
754 unsigned int src_cnt, size_t len, unsigned long flags)
755{
756 return __ioat3_prep_xor_lock(chan, NULL, dest, src, src_cnt, len, flags);
757}
758
Rashika8d1d3272013-12-16 15:40:40 +0530759static struct dma_async_tx_descriptor *
Dan Williamsb094ad32009-09-08 17:42:57 -0700760ioat3_prep_xor_val(struct dma_chan *chan, dma_addr_t *src,
761 unsigned int src_cnt, size_t len,
762 enum sum_check_flags *result, unsigned long flags)
763{
764 /* the cleanup routine only sets bits on validate failure, it
765 * does not clear bits on validate success... so clear it here
766 */
767 *result = 0;
768
769 return __ioat3_prep_xor_lock(chan, result, src[0], &src[1],
770 src_cnt - 1, len, flags);
771}
772
Dan Williamsd69d235b2009-09-08 17:42:59 -0700773static void
774dump_pq_desc_dbg(struct ioat2_dma_chan *ioat, struct ioat_ring_ent *desc, struct ioat_ring_ent *ext)
775{
776 struct device *dev = to_dev(&ioat->base);
777 struct ioat_pq_descriptor *pq = desc->pq;
778 struct ioat_pq_ext_descriptor *pq_ex = ext ? ext->pq_ex : NULL;
779 struct ioat_raw_descriptor *descs[] = { (void *) pq, (void *) pq_ex };
780 int src_cnt = src_cnt_to_sw(pq->ctl_f.src_cnt);
781 int i;
782
783 dev_dbg(dev, "desc[%d]: (%#llx->%#llx) flags: %#x"
Dave Jiang7727eaa2013-04-15 10:25:56 -0700784 " sz: %#10.8x ctl: %#x (op: %#x int: %d compl: %d pq: '%s%s'"
785 " src_cnt: %d)\n",
Dan Williamsd69d235b2009-09-08 17:42:59 -0700786 desc_id(desc), (unsigned long long) desc->txd.phys,
787 (unsigned long long) (pq_ex ? pq_ex->next : pq->next),
788 desc->txd.flags, pq->size, pq->ctl, pq->ctl_f.op, pq->ctl_f.int_en,
789 pq->ctl_f.compl_write,
790 pq->ctl_f.p_disable ? "" : "p", pq->ctl_f.q_disable ? "" : "q",
791 pq->ctl_f.src_cnt);
792 for (i = 0; i < src_cnt; i++)
793 dev_dbg(dev, "\tsrc[%d]: %#llx coef: %#x\n", i,
794 (unsigned long long) pq_get_src(descs, i), pq->coef[i]);
795 dev_dbg(dev, "\tP: %#llx\n", pq->p_addr);
796 dev_dbg(dev, "\tQ: %#llx\n", pq->q_addr);
Dave Jiang50f9f972013-03-04 10:59:54 -0700797 dev_dbg(dev, "\tNEXT: %#llx\n", pq->next);
Dan Williamsd69d235b2009-09-08 17:42:59 -0700798}
799
Dave Jiang7727eaa2013-04-15 10:25:56 -0700800static void dump_pq16_desc_dbg(struct ioat2_dma_chan *ioat,
801 struct ioat_ring_ent *desc)
802{
803 struct device *dev = to_dev(&ioat->base);
804 struct ioat_pq_descriptor *pq = desc->pq;
805 struct ioat_raw_descriptor *descs[] = { (void *)pq,
806 (void *)pq,
807 (void *)pq };
808 int src_cnt = src16_cnt_to_sw(pq->ctl_f.src_cnt);
809 int i;
810
811 if (desc->sed) {
812 descs[1] = (void *)desc->sed->hw;
813 descs[2] = (void *)desc->sed->hw + 64;
814 }
815
816 dev_dbg(dev, "desc[%d]: (%#llx->%#llx) flags: %#x"
817 " sz: %#x ctl: %#x (op: %#x int: %d compl: %d pq: '%s%s'"
818 " src_cnt: %d)\n",
819 desc_id(desc), (unsigned long long) desc->txd.phys,
820 (unsigned long long) pq->next,
821 desc->txd.flags, pq->size, pq->ctl,
822 pq->ctl_f.op, pq->ctl_f.int_en,
823 pq->ctl_f.compl_write,
824 pq->ctl_f.p_disable ? "" : "p", pq->ctl_f.q_disable ? "" : "q",
825 pq->ctl_f.src_cnt);
826 for (i = 0; i < src_cnt; i++) {
827 dev_dbg(dev, "\tsrc[%d]: %#llx coef: %#x\n", i,
828 (unsigned long long) pq16_get_src(descs, i),
829 pq->coef[i]);
830 }
831 dev_dbg(dev, "\tP: %#llx\n", pq->p_addr);
832 dev_dbg(dev, "\tQ: %#llx\n", pq->q_addr);
833}
834
Dan Williamsd69d235b2009-09-08 17:42:59 -0700835static struct dma_async_tx_descriptor *
836__ioat3_prep_pq_lock(struct dma_chan *c, enum sum_check_flags *result,
837 const dma_addr_t *dst, const dma_addr_t *src,
838 unsigned int src_cnt, const unsigned char *scf,
839 size_t len, unsigned long flags)
840{
841 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
842 struct ioat_chan_common *chan = &ioat->base;
Dave Jiange0884772013-04-10 16:44:20 -0700843 struct ioatdma_device *device = chan->device;
Dan Williamsd69d235b2009-09-08 17:42:59 -0700844 struct ioat_ring_ent *compl_desc;
845 struct ioat_ring_ent *desc;
846 struct ioat_ring_ent *ext;
847 size_t total_len = len;
848 struct ioat_pq_descriptor *pq;
849 struct ioat_pq_ext_descriptor *pq_ex = NULL;
850 struct ioat_dma_descriptor *hw;
851 u32 offset = 0;
Dan Williamsd69d235b2009-09-08 17:42:59 -0700852 u8 op = result ? IOAT_OP_PQ_VAL : IOAT_OP_PQ;
Dan Williams074cc472010-05-01 15:22:55 -0700853 int i, s, idx, with_ext, num_descs;
Dave Jiange0884772013-04-10 16:44:20 -0700854 int cb32 = (device->version < IOAT_VER_3_3) ? 1 : 0;
Dan Williamsd69d235b2009-09-08 17:42:59 -0700855
856 dev_dbg(to_dev(chan), "%s\n", __func__);
857 /* the engine requires at least two sources (we provide
858 * at least 1 implied source in the DMA_PREP_CONTINUE case)
859 */
860 BUG_ON(src_cnt + dmaf_continue(flags) < 2);
861
862 num_descs = ioat2_xferlen_to_descs(ioat, len);
863 /* we need 2x the number of descriptors to cover greater than 3
Dan Williamscd788092009-12-17 13:52:39 -0700864 * sources (we need 1 extra source in the q-only continuation
865 * case and 3 extra sources in the p+q continuation case.
Dan Williamsd69d235b2009-09-08 17:42:59 -0700866 */
Dan Williamscd788092009-12-17 13:52:39 -0700867 if (src_cnt + dmaf_p_disabled_continue(flags) > 3 ||
868 (dmaf_continue(flags) && !dmaf_p_disabled_continue(flags))) {
Dan Williamsd69d235b2009-09-08 17:42:59 -0700869 with_ext = 1;
870 num_descs *= 2;
871 } else
872 with_ext = 0;
873
874 /* completion writes from the raid engine may pass completion
875 * writes from the legacy engine, so we need one extra null
876 * (legacy) descriptor to ensure all completion writes arrive in
877 * order.
878 */
879 if (likely(num_descs) &&
Dave Jiange0884772013-04-10 16:44:20 -0700880 ioat2_check_space_lock(ioat, num_descs + cb32) == 0)
Dan Williams074cc472010-05-01 15:22:55 -0700881 idx = ioat->head;
Dan Williamsd69d235b2009-09-08 17:42:59 -0700882 else
883 return NULL;
Dan Williamscdef57d2009-09-21 09:22:29 -0700884 i = 0;
885 do {
Dan Williamsd69d235b2009-09-08 17:42:59 -0700886 struct ioat_raw_descriptor *descs[2];
887 size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
888
889 desc = ioat2_get_ring_ent(ioat, idx + i);
890 pq = desc->pq;
891
892 /* save a branch by unconditionally retrieving the
893 * extended descriptor pq_set_src() knows to not write
894 * to it in the single descriptor case
895 */
896 ext = ioat2_get_ring_ent(ioat, idx + i + with_ext);
897 pq_ex = ext->pq_ex;
898
899 descs[0] = (struct ioat_raw_descriptor *) pq;
900 descs[1] = (struct ioat_raw_descriptor *) pq_ex;
901
902 for (s = 0; s < src_cnt; s++)
903 pq_set_src(descs, src[s], offset, scf[s], s);
904
905 /* see the comment for dma_maxpq in include/linux/dmaengine.h */
906 if (dmaf_p_disabled_continue(flags))
907 pq_set_src(descs, dst[1], offset, 1, s++);
908 else if (dmaf_continue(flags)) {
909 pq_set_src(descs, dst[0], offset, 0, s++);
910 pq_set_src(descs, dst[1], offset, 1, s++);
911 pq_set_src(descs, dst[1], offset, 0, s++);
912 }
913 pq->size = xfer_size;
914 pq->p_addr = dst[0] + offset;
915 pq->q_addr = dst[1] + offset;
916 pq->ctl = 0;
917 pq->ctl_f.op = op;
Dave Jiang75c6f0a2013-04-10 16:44:39 -0700918 /* we turn on descriptor write back error status */
919 if (device->cap & IOAT_CAP_DWBES)
920 pq->ctl_f.wb_en = result ? 1 : 0;
Dan Williamsd69d235b2009-09-08 17:42:59 -0700921 pq->ctl_f.src_cnt = src_cnt_to_hw(s);
922 pq->ctl_f.p_disable = !!(flags & DMA_PREP_PQ_DISABLE_P);
923 pq->ctl_f.q_disable = !!(flags & DMA_PREP_PQ_DISABLE_Q);
924
925 len -= xfer_size;
926 offset += xfer_size;
Dan Williamscdef57d2009-09-21 09:22:29 -0700927 } while ((i += 1 + with_ext) < num_descs);
Dan Williamsd69d235b2009-09-08 17:42:59 -0700928
929 /* last pq descriptor carries the unmap parameters and fence bit */
930 desc->txd.flags = flags;
931 desc->len = total_len;
932 if (result)
933 desc->result = result;
934 pq->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
935 dump_pq_desc_dbg(ioat, desc, ext);
936
Dave Jiange0884772013-04-10 16:44:20 -0700937 if (!cb32) {
938 pq->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
939 pq->ctl_f.compl_write = 1;
940 compl_desc = desc;
941 } else {
942 /* completion descriptor carries interrupt bit */
943 compl_desc = ioat2_get_ring_ent(ioat, idx + i);
944 compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
945 hw = compl_desc->hw;
946 hw->ctl = 0;
947 hw->ctl_f.null = 1;
948 hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
949 hw->ctl_f.compl_write = 1;
950 hw->size = NULL_DESC_BUFFER_SIZE;
951 dump_desc_dbg(ioat, compl_desc);
952 }
953
Dan Williamsd69d235b2009-09-08 17:42:59 -0700954
955 /* we leave the channel locked to ensure in order submission */
Dan Williams49954c12009-11-19 17:11:03 -0700956 return &compl_desc->txd;
Dan Williamsd69d235b2009-09-08 17:42:59 -0700957}
958
959static struct dma_async_tx_descriptor *
Dave Jiang7727eaa2013-04-15 10:25:56 -0700960__ioat3_prep_pq16_lock(struct dma_chan *c, enum sum_check_flags *result,
961 const dma_addr_t *dst, const dma_addr_t *src,
962 unsigned int src_cnt, const unsigned char *scf,
963 size_t len, unsigned long flags)
964{
965 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
966 struct ioat_chan_common *chan = &ioat->base;
967 struct ioatdma_device *device = chan->device;
968 struct ioat_ring_ent *desc;
969 size_t total_len = len;
970 struct ioat_pq_descriptor *pq;
971 u32 offset = 0;
972 u8 op;
973 int i, s, idx, num_descs;
974
Dave Jiang7727eaa2013-04-15 10:25:56 -0700975 /* this function is only called with 9-16 sources */
976 op = result ? IOAT_OP_PQ_VAL_16S : IOAT_OP_PQ_16S;
977
978 dev_dbg(to_dev(chan), "%s\n", __func__);
979
980 num_descs = ioat2_xferlen_to_descs(ioat, len);
981
982 /*
983 * 16 source pq is only available on cb3.3 and has no completion
984 * write hw bug.
985 */
986 if (num_descs && ioat2_check_space_lock(ioat, num_descs) == 0)
987 idx = ioat->head;
988 else
989 return NULL;
990
991 i = 0;
992
993 do {
994 struct ioat_raw_descriptor *descs[4];
995 size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
996
997 desc = ioat2_get_ring_ent(ioat, idx + i);
998 pq = desc->pq;
999
1000 descs[0] = (struct ioat_raw_descriptor *) pq;
1001
Dan Williams5d48b9b2013-11-13 10:15:42 -08001002 desc->sed = ioat3_alloc_sed(device, (src_cnt-2) >> 3);
Dave Jiang7727eaa2013-04-15 10:25:56 -07001003 if (!desc->sed) {
1004 dev_err(to_dev(chan),
1005 "%s: no free sed entries\n", __func__);
1006 return NULL;
1007 }
1008
1009 pq->sed_addr = desc->sed->dma;
1010 desc->sed->parent = desc;
1011
1012 descs[1] = (struct ioat_raw_descriptor *)desc->sed->hw;
1013 descs[2] = (void *)descs[1] + 64;
1014
1015 for (s = 0; s < src_cnt; s++)
1016 pq16_set_src(descs, src[s], offset, scf[s], s);
1017
1018 /* see the comment for dma_maxpq in include/linux/dmaengine.h */
1019 if (dmaf_p_disabled_continue(flags))
1020 pq16_set_src(descs, dst[1], offset, 1, s++);
1021 else if (dmaf_continue(flags)) {
1022 pq16_set_src(descs, dst[0], offset, 0, s++);
1023 pq16_set_src(descs, dst[1], offset, 1, s++);
1024 pq16_set_src(descs, dst[1], offset, 0, s++);
1025 }
1026
1027 pq->size = xfer_size;
1028 pq->p_addr = dst[0] + offset;
1029 pq->q_addr = dst[1] + offset;
1030 pq->ctl = 0;
1031 pq->ctl_f.op = op;
1032 pq->ctl_f.src_cnt = src16_cnt_to_hw(s);
Dave Jiang75c6f0a2013-04-10 16:44:39 -07001033 /* we turn on descriptor write back error status */
1034 if (device->cap & IOAT_CAP_DWBES)
1035 pq->ctl_f.wb_en = result ? 1 : 0;
Dave Jiang7727eaa2013-04-15 10:25:56 -07001036 pq->ctl_f.p_disable = !!(flags & DMA_PREP_PQ_DISABLE_P);
1037 pq->ctl_f.q_disable = !!(flags & DMA_PREP_PQ_DISABLE_Q);
1038
1039 len -= xfer_size;
1040 offset += xfer_size;
1041 } while (++i < num_descs);
1042
1043 /* last pq descriptor carries the unmap parameters and fence bit */
1044 desc->txd.flags = flags;
1045 desc->len = total_len;
1046 if (result)
1047 desc->result = result;
1048 pq->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
1049
1050 /* with cb3.3 we should be able to do completion w/o a null desc */
1051 pq->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
1052 pq->ctl_f.compl_write = 1;
1053
1054 dump_pq16_desc_dbg(ioat, desc);
1055
1056 /* we leave the channel locked to ensure in order submission */
1057 return &desc->txd;
1058}
1059
Dan Williams21e96c72013-11-13 10:37:36 -08001060static int src_cnt_flags(unsigned int src_cnt, unsigned long flags)
1061{
1062 if (dmaf_p_disabled_continue(flags))
1063 return src_cnt + 1;
1064 else if (dmaf_continue(flags))
1065 return src_cnt + 3;
1066 else
1067 return src_cnt;
1068}
1069
Dave Jiang7727eaa2013-04-15 10:25:56 -07001070static struct dma_async_tx_descriptor *
Dan Williamsd69d235b2009-09-08 17:42:59 -07001071ioat3_prep_pq(struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
1072 unsigned int src_cnt, const unsigned char *scf, size_t len,
1073 unsigned long flags)
1074{
Dan Williamsde581b62009-11-19 17:08:45 -07001075 /* specify valid address for disabled result */
1076 if (flags & DMA_PREP_PQ_DISABLE_P)
1077 dst[0] = dst[1];
1078 if (flags & DMA_PREP_PQ_DISABLE_Q)
1079 dst[1] = dst[0];
1080
Dan Williamsd69d235b2009-09-08 17:42:59 -07001081 /* handle the single source multiply case from the raid6
1082 * recovery path
1083 */
Dan Williamsde581b62009-11-19 17:08:45 -07001084 if ((flags & DMA_PREP_PQ_DISABLE_P) && src_cnt == 1) {
Dan Williamsd69d235b2009-09-08 17:42:59 -07001085 dma_addr_t single_source[2];
1086 unsigned char single_source_coef[2];
1087
1088 BUG_ON(flags & DMA_PREP_PQ_DISABLE_Q);
1089 single_source[0] = src[0];
1090 single_source[1] = src[0];
1091 single_source_coef[0] = scf[0];
1092 single_source_coef[1] = 0;
1093
Dan Williams21e96c72013-11-13 10:37:36 -08001094 return src_cnt_flags(src_cnt, flags) > 8 ?
Dave Jiang7727eaa2013-04-15 10:25:56 -07001095 __ioat3_prep_pq16_lock(chan, NULL, dst, single_source,
1096 2, single_source_coef, len,
1097 flags) :
1098 __ioat3_prep_pq_lock(chan, NULL, dst, single_source, 2,
1099 single_source_coef, len, flags);
1100
1101 } else {
Dan Williams21e96c72013-11-13 10:37:36 -08001102 return src_cnt_flags(src_cnt, flags) > 8 ?
Dave Jiang7727eaa2013-04-15 10:25:56 -07001103 __ioat3_prep_pq16_lock(chan, NULL, dst, src, src_cnt,
1104 scf, len, flags) :
1105 __ioat3_prep_pq_lock(chan, NULL, dst, src, src_cnt,
1106 scf, len, flags);
1107 }
Dan Williamsd69d235b2009-09-08 17:42:59 -07001108}
1109
Rashika8d1d3272013-12-16 15:40:40 +05301110static struct dma_async_tx_descriptor *
Dan Williamsd69d235b2009-09-08 17:42:59 -07001111ioat3_prep_pq_val(struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
1112 unsigned int src_cnt, const unsigned char *scf, size_t len,
1113 enum sum_check_flags *pqres, unsigned long flags)
1114{
Dan Williamsde581b62009-11-19 17:08:45 -07001115 /* specify valid address for disabled result */
1116 if (flags & DMA_PREP_PQ_DISABLE_P)
1117 pq[0] = pq[1];
1118 if (flags & DMA_PREP_PQ_DISABLE_Q)
1119 pq[1] = pq[0];
1120
Dan Williamsd69d235b2009-09-08 17:42:59 -07001121 /* the cleanup routine only sets bits on validate failure, it
1122 * does not clear bits on validate success... so clear it here
1123 */
1124 *pqres = 0;
1125
Dan Williams21e96c72013-11-13 10:37:36 -08001126 return src_cnt_flags(src_cnt, flags) > 8 ?
Dave Jiang7727eaa2013-04-15 10:25:56 -07001127 __ioat3_prep_pq16_lock(chan, pqres, pq, src, src_cnt, scf, len,
1128 flags) :
1129 __ioat3_prep_pq_lock(chan, pqres, pq, src, src_cnt, scf, len,
1130 flags);
Dan Williamsd69d235b2009-09-08 17:42:59 -07001131}
1132
Dan Williamsae786622009-09-08 17:43:00 -07001133static struct dma_async_tx_descriptor *
1134ioat3_prep_pqxor(struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
1135 unsigned int src_cnt, size_t len, unsigned long flags)
1136{
1137 unsigned char scf[src_cnt];
1138 dma_addr_t pq[2];
1139
1140 memset(scf, 0, src_cnt);
Dan Williamsae786622009-09-08 17:43:00 -07001141 pq[0] = dst;
Dan Williamsde581b62009-11-19 17:08:45 -07001142 flags |= DMA_PREP_PQ_DISABLE_Q;
1143 pq[1] = dst; /* specify valid address for disabled result */
Dan Williamsae786622009-09-08 17:43:00 -07001144
Dan Williams21e96c72013-11-13 10:37:36 -08001145 return src_cnt_flags(src_cnt, flags) > 8 ?
Dave Jiang7727eaa2013-04-15 10:25:56 -07001146 __ioat3_prep_pq16_lock(chan, NULL, pq, src, src_cnt, scf, len,
1147 flags) :
1148 __ioat3_prep_pq_lock(chan, NULL, pq, src, src_cnt, scf, len,
1149 flags);
Dan Williamsae786622009-09-08 17:43:00 -07001150}
1151
Rashika8d1d3272013-12-16 15:40:40 +05301152static struct dma_async_tx_descriptor *
Dan Williamsae786622009-09-08 17:43:00 -07001153ioat3_prep_pqxor_val(struct dma_chan *chan, dma_addr_t *src,
1154 unsigned int src_cnt, size_t len,
1155 enum sum_check_flags *result, unsigned long flags)
1156{
1157 unsigned char scf[src_cnt];
1158 dma_addr_t pq[2];
1159
1160 /* the cleanup routine only sets bits on validate failure, it
1161 * does not clear bits on validate success... so clear it here
1162 */
1163 *result = 0;
1164
1165 memset(scf, 0, src_cnt);
Dan Williamsae786622009-09-08 17:43:00 -07001166 pq[0] = src[0];
Dan Williamsde581b62009-11-19 17:08:45 -07001167 flags |= DMA_PREP_PQ_DISABLE_Q;
1168 pq[1] = pq[0]; /* specify valid address for disabled result */
Dan Williamsae786622009-09-08 17:43:00 -07001169
Dan Williams21e96c72013-11-13 10:37:36 -08001170 return src_cnt_flags(src_cnt, flags) > 8 ?
Dave Jiang7727eaa2013-04-15 10:25:56 -07001171 __ioat3_prep_pq16_lock(chan, result, pq, &src[1], src_cnt - 1,
1172 scf, len, flags) :
1173 __ioat3_prep_pq_lock(chan, result, pq, &src[1], src_cnt - 1,
1174 scf, len, flags);
Dan Williamsae786622009-09-08 17:43:00 -07001175}
1176
Dan Williams58c86492009-09-08 17:43:00 -07001177static struct dma_async_tx_descriptor *
1178ioat3_prep_interrupt_lock(struct dma_chan *c, unsigned long flags)
1179{
1180 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
1181 struct ioat_ring_ent *desc;
1182 struct ioat_dma_descriptor *hw;
Dan Williams58c86492009-09-08 17:43:00 -07001183
Dan Williams074cc472010-05-01 15:22:55 -07001184 if (ioat2_check_space_lock(ioat, 1) == 0)
1185 desc = ioat2_get_ring_ent(ioat, ioat->head);
Dan Williams58c86492009-09-08 17:43:00 -07001186 else
1187 return NULL;
1188
1189 hw = desc->hw;
1190 hw->ctl = 0;
1191 hw->ctl_f.null = 1;
1192 hw->ctl_f.int_en = 1;
1193 hw->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
1194 hw->ctl_f.compl_write = 1;
1195 hw->size = NULL_DESC_BUFFER_SIZE;
1196 hw->src_addr = 0;
1197 hw->dst_addr = 0;
1198
1199 desc->txd.flags = flags;
1200 desc->len = 1;
1201
1202 dump_desc_dbg(ioat, desc);
1203
1204 /* we leave the channel locked to ensure in order submission */
1205 return &desc->txd;
1206}
1207
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08001208static void ioat3_dma_test_callback(void *dma_async_param)
Dan Williams9de6fc72009-09-08 17:42:58 -07001209{
1210 struct completion *cmp = dma_async_param;
1211
1212 complete(cmp);
1213}
1214
1215#define IOAT_NUM_SRC_TEST 6 /* must be <= 8 */
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08001216static int ioat_xor_val_self_test(struct ioatdma_device *device)
Dan Williams9de6fc72009-09-08 17:42:58 -07001217{
1218 int i, src_idx;
1219 struct page *dest;
1220 struct page *xor_srcs[IOAT_NUM_SRC_TEST];
1221 struct page *xor_val_srcs[IOAT_NUM_SRC_TEST + 1];
1222 dma_addr_t dma_srcs[IOAT_NUM_SRC_TEST + 1];
Bartlomiej Zolnierkiewicz48a9db42013-07-03 15:05:06 -07001223 dma_addr_t dest_dma;
Dan Williams9de6fc72009-09-08 17:42:58 -07001224 struct dma_async_tx_descriptor *tx;
1225 struct dma_chan *dma_chan;
1226 dma_cookie_t cookie;
1227 u8 cmp_byte = 0;
1228 u32 cmp_word;
1229 u32 xor_val_result;
1230 int err = 0;
1231 struct completion cmp;
1232 unsigned long tmo;
1233 struct device *dev = &device->pdev->dev;
1234 struct dma_device *dma = &device->common;
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001235 u8 op = 0;
Dan Williams9de6fc72009-09-08 17:42:58 -07001236
1237 dev_dbg(dev, "%s\n", __func__);
1238
1239 if (!dma_has_cap(DMA_XOR, dma->cap_mask))
1240 return 0;
1241
1242 for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
1243 xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
1244 if (!xor_srcs[src_idx]) {
1245 while (src_idx--)
1246 __free_page(xor_srcs[src_idx]);
1247 return -ENOMEM;
1248 }
1249 }
1250
1251 dest = alloc_page(GFP_KERNEL);
1252 if (!dest) {
1253 while (src_idx--)
1254 __free_page(xor_srcs[src_idx]);
1255 return -ENOMEM;
1256 }
1257
1258 /* Fill in src buffers */
1259 for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
1260 u8 *ptr = page_address(xor_srcs[src_idx]);
1261 for (i = 0; i < PAGE_SIZE; i++)
1262 ptr[i] = (1 << src_idx);
1263 }
1264
1265 for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++)
1266 cmp_byte ^= (u8) (1 << src_idx);
1267
1268 cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
1269 (cmp_byte << 8) | cmp_byte;
1270
1271 memset(page_address(dest), 0, PAGE_SIZE);
1272
1273 dma_chan = container_of(dma->channels.next, struct dma_chan,
1274 device_node);
1275 if (dma->device_alloc_chan_resources(dma_chan) < 1) {
1276 err = -ENODEV;
1277 goto out;
1278 }
1279
1280 /* test xor */
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001281 op = IOAT_OP_XOR;
1282
Dan Williams9de6fc72009-09-08 17:42:58 -07001283 dest_dma = dma_map_page(dev, dest, 0, PAGE_SIZE, DMA_FROM_DEVICE);
Prarit Bhargava4ff2fd82014-10-23 07:38:29 -04001284 if (dma_mapping_error(dev, dest_dma))
1285 goto dma_unmap;
1286
Dan Williams9de6fc72009-09-08 17:42:58 -07001287 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
Prarit Bhargava4ff2fd82014-10-23 07:38:29 -04001288 dma_srcs[i] = DMA_ERROR_CODE;
1289 for (i = 0; i < IOAT_NUM_SRC_TEST; i++) {
Dan Williams9de6fc72009-09-08 17:42:58 -07001290 dma_srcs[i] = dma_map_page(dev, xor_srcs[i], 0, PAGE_SIZE,
1291 DMA_TO_DEVICE);
Prarit Bhargava4ff2fd82014-10-23 07:38:29 -04001292 if (dma_mapping_error(dev, dma_srcs[i]))
1293 goto dma_unmap;
1294 }
Dan Williams9de6fc72009-09-08 17:42:58 -07001295 tx = dma->device_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
1296 IOAT_NUM_SRC_TEST, PAGE_SIZE,
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +02001297 DMA_PREP_INTERRUPT);
Dan Williams9de6fc72009-09-08 17:42:58 -07001298
1299 if (!tx) {
1300 dev_err(dev, "Self-test xor prep failed\n");
1301 err = -ENODEV;
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001302 goto dma_unmap;
Dan Williams9de6fc72009-09-08 17:42:58 -07001303 }
1304
1305 async_tx_ack(tx);
1306 init_completion(&cmp);
1307 tx->callback = ioat3_dma_test_callback;
1308 tx->callback_param = &cmp;
1309 cookie = tx->tx_submit(tx);
1310 if (cookie < 0) {
1311 dev_err(dev, "Self-test xor setup failed\n");
1312 err = -ENODEV;
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001313 goto dma_unmap;
Dan Williams9de6fc72009-09-08 17:42:58 -07001314 }
1315 dma->device_issue_pending(dma_chan);
1316
1317 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
1318
Nicholas Mc Guire12385f42015-01-08 15:16:35 +01001319 if (tmo == 0 ||
1320 dma->device_tx_status(dma_chan, cookie, NULL) != DMA_COMPLETE) {
Dan Williams9de6fc72009-09-08 17:42:58 -07001321 dev_err(dev, "Self-test xor timed out\n");
1322 err = -ENODEV;
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001323 goto dma_unmap;
Dan Williams9de6fc72009-09-08 17:42:58 -07001324 }
1325
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001326 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
1327 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
1328
Dan Williams9de6fc72009-09-08 17:42:58 -07001329 dma_sync_single_for_cpu(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
1330 for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
1331 u32 *ptr = page_address(dest);
1332 if (ptr[i] != cmp_word) {
1333 dev_err(dev, "Self-test xor failed compare\n");
1334 err = -ENODEV;
1335 goto free_resources;
1336 }
1337 }
Shuah Khanac498982012-10-25 10:22:32 -06001338 dma_sync_single_for_device(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
Dan Williams9de6fc72009-09-08 17:42:58 -07001339
Prarit Bhargava4ff2fd82014-10-23 07:38:29 -04001340 dma_unmap_page(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
1341
Dan Williams9de6fc72009-09-08 17:42:58 -07001342 /* skip validate if the capability is not present */
1343 if (!dma_has_cap(DMA_XOR_VAL, dma_chan->device->cap_mask))
1344 goto free_resources;
1345
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001346 op = IOAT_OP_XOR_VAL;
1347
Dan Williams9de6fc72009-09-08 17:42:58 -07001348 /* validate the sources with the destintation page */
1349 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
1350 xor_val_srcs[i] = xor_srcs[i];
1351 xor_val_srcs[i] = dest;
1352
1353 xor_val_result = 1;
1354
1355 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
Prarit Bhargava4ff2fd82014-10-23 07:38:29 -04001356 dma_srcs[i] = DMA_ERROR_CODE;
1357 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++) {
Dan Williams9de6fc72009-09-08 17:42:58 -07001358 dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
1359 DMA_TO_DEVICE);
Prarit Bhargava4ff2fd82014-10-23 07:38:29 -04001360 if (dma_mapping_error(dev, dma_srcs[i]))
1361 goto dma_unmap;
1362 }
Dan Williams9de6fc72009-09-08 17:42:58 -07001363 tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
1364 IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +02001365 &xor_val_result, DMA_PREP_INTERRUPT);
Dan Williams9de6fc72009-09-08 17:42:58 -07001366 if (!tx) {
1367 dev_err(dev, "Self-test zero prep failed\n");
1368 err = -ENODEV;
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001369 goto dma_unmap;
Dan Williams9de6fc72009-09-08 17:42:58 -07001370 }
1371
1372 async_tx_ack(tx);
1373 init_completion(&cmp);
1374 tx->callback = ioat3_dma_test_callback;
1375 tx->callback_param = &cmp;
1376 cookie = tx->tx_submit(tx);
1377 if (cookie < 0) {
1378 dev_err(dev, "Self-test zero setup failed\n");
1379 err = -ENODEV;
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001380 goto dma_unmap;
Dan Williams9de6fc72009-09-08 17:42:58 -07001381 }
1382 dma->device_issue_pending(dma_chan);
1383
1384 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
1385
Nicholas Mc Guire12385f42015-01-08 15:16:35 +01001386 if (tmo == 0 ||
1387 dma->device_tx_status(dma_chan, cookie, NULL) != DMA_COMPLETE) {
Dan Williams9de6fc72009-09-08 17:42:58 -07001388 dev_err(dev, "Self-test validate timed out\n");
1389 err = -ENODEV;
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001390 goto dma_unmap;
Dan Williams9de6fc72009-09-08 17:42:58 -07001391 }
1392
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001393 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1394 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
1395
Dan Williams9de6fc72009-09-08 17:42:58 -07001396 if (xor_val_result != 0) {
1397 dev_err(dev, "Self-test validate failed compare\n");
1398 err = -ENODEV;
1399 goto free_resources;
1400 }
1401
Dave Jiangac7d6312013-11-06 08:50:09 -07001402 memset(page_address(dest), 0, PAGE_SIZE);
1403
Dan Williams9de6fc72009-09-08 17:42:58 -07001404 /* test for non-zero parity sum */
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001405 op = IOAT_OP_XOR_VAL;
1406
Dan Williams9de6fc72009-09-08 17:42:58 -07001407 xor_val_result = 0;
1408 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
Prarit Bhargava4ff2fd82014-10-23 07:38:29 -04001409 dma_srcs[i] = DMA_ERROR_CODE;
1410 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++) {
Dan Williams9de6fc72009-09-08 17:42:58 -07001411 dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
1412 DMA_TO_DEVICE);
Prarit Bhargava4ff2fd82014-10-23 07:38:29 -04001413 if (dma_mapping_error(dev, dma_srcs[i]))
1414 goto dma_unmap;
1415 }
Dan Williams9de6fc72009-09-08 17:42:58 -07001416 tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
1417 IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +02001418 &xor_val_result, DMA_PREP_INTERRUPT);
Dan Williams9de6fc72009-09-08 17:42:58 -07001419 if (!tx) {
1420 dev_err(dev, "Self-test 2nd zero prep failed\n");
1421 err = -ENODEV;
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001422 goto dma_unmap;
Dan Williams9de6fc72009-09-08 17:42:58 -07001423 }
1424
1425 async_tx_ack(tx);
1426 init_completion(&cmp);
1427 tx->callback = ioat3_dma_test_callback;
1428 tx->callback_param = &cmp;
1429 cookie = tx->tx_submit(tx);
1430 if (cookie < 0) {
1431 dev_err(dev, "Self-test 2nd zero setup failed\n");
1432 err = -ENODEV;
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001433 goto dma_unmap;
Dan Williams9de6fc72009-09-08 17:42:58 -07001434 }
1435 dma->device_issue_pending(dma_chan);
1436
1437 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
1438
Nicholas Mc Guire12385f42015-01-08 15:16:35 +01001439 if (tmo == 0 ||
1440 dma->device_tx_status(dma_chan, cookie, NULL) != DMA_COMPLETE) {
Dan Williams9de6fc72009-09-08 17:42:58 -07001441 dev_err(dev, "Self-test 2nd validate timed out\n");
1442 err = -ENODEV;
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001443 goto dma_unmap;
Dan Williams9de6fc72009-09-08 17:42:58 -07001444 }
1445
1446 if (xor_val_result != SUM_CHECK_P_RESULT) {
1447 dev_err(dev, "Self-test validate failed compare\n");
1448 err = -ENODEV;
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001449 goto dma_unmap;
Dan Williams9de6fc72009-09-08 17:42:58 -07001450 }
1451
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001452 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1453 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
1454
1455 goto free_resources;
1456dma_unmap:
1457 if (op == IOAT_OP_XOR) {
Prarit Bhargava4ff2fd82014-10-23 07:38:29 -04001458 if (dest_dma != DMA_ERROR_CODE)
1459 dma_unmap_page(dev, dest_dma, PAGE_SIZE,
1460 DMA_FROM_DEVICE);
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001461 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
Prarit Bhargava4ff2fd82014-10-23 07:38:29 -04001462 if (dma_srcs[i] != DMA_ERROR_CODE)
1463 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE,
1464 DMA_TO_DEVICE);
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001465 } else if (op == IOAT_OP_XOR_VAL) {
1466 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
Prarit Bhargava4ff2fd82014-10-23 07:38:29 -04001467 if (dma_srcs[i] != DMA_ERROR_CODE)
1468 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE,
1469 DMA_TO_DEVICE);
Bartlomiej Zolnierkiewicz48a9db42013-07-03 15:05:06 -07001470 }
Dan Williams9de6fc72009-09-08 17:42:58 -07001471free_resources:
1472 dma->device_free_chan_resources(dma_chan);
1473out:
1474 src_idx = IOAT_NUM_SRC_TEST;
1475 while (src_idx--)
1476 __free_page(xor_srcs[src_idx]);
1477 __free_page(dest);
1478 return err;
1479}
1480
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08001481static int ioat3_dma_self_test(struct ioatdma_device *device)
Dan Williams9de6fc72009-09-08 17:42:58 -07001482{
1483 int rc = ioat_dma_self_test(device);
1484
1485 if (rc)
1486 return rc;
1487
1488 rc = ioat_xor_val_self_test(device);
1489 if (rc)
1490 return rc;
1491
1492 return 0;
1493}
1494
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001495static int ioat3_irq_reinit(struct ioatdma_device *device)
1496{
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001497 struct pci_dev *pdev = device->pdev;
Dan Williams779e5612013-11-13 16:30:43 -08001498 int irq = pdev->irq, i;
1499
1500 if (!is_bwd_ioat(pdev))
1501 return 0;
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001502
1503 switch (device->irq_mode) {
1504 case IOAT_MSIX:
Dan Williams779e5612013-11-13 16:30:43 -08001505 for (i = 0; i < device->common.chancnt; i++) {
1506 struct msix_entry *msix = &device->msix_entries[i];
1507 struct ioat_chan_common *chan;
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001508
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001509 chan = ioat_chan_by_index(device, i);
1510 devm_free_irq(&pdev->dev, msix->vector, chan);
1511 }
1512
1513 pci_disable_msix(pdev);
1514 break;
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001515 case IOAT_MSI:
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001516 pci_disable_msi(pdev);
Dan Williams779e5612013-11-13 16:30:43 -08001517 /* fall through */
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001518 case IOAT_INTX:
Dan Williams779e5612013-11-13 16:30:43 -08001519 devm_free_irq(&pdev->dev, irq, device);
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001520 break;
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001521 default:
1522 return 0;
1523 }
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001524 device->irq_mode = IOAT_NOIRQ;
1525
Dan Williams779e5612013-11-13 16:30:43 -08001526 return ioat_dma_setup_interrupts(device);
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001527}
1528
Dan Williamsa6d52d72009-12-19 15:36:02 -07001529static int ioat3_reset_hw(struct ioat_chan_common *chan)
1530{
1531 /* throw away whatever the channel was doing and get it
1532 * initialized, with ioat3 specific workarounds
1533 */
1534 struct ioatdma_device *device = chan->device;
1535 struct pci_dev *pdev = device->pdev;
1536 u32 chanerr;
1537 u16 dev_id;
1538 int err;
1539
1540 ioat2_quiesce(chan, msecs_to_jiffies(100));
1541
1542 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
1543 writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
1544
Dave Jiang6ead7e42013-03-26 15:42:59 -07001545 if (device->version < IOAT_VER_3_3) {
1546 /* clear any pending errors */
1547 err = pci_read_config_dword(pdev,
1548 IOAT_PCI_CHANERR_INT_OFFSET, &chanerr);
1549 if (err) {
1550 dev_err(&pdev->dev,
1551 "channel error register unreachable\n");
1552 return err;
1553 }
1554 pci_write_config_dword(pdev,
1555 IOAT_PCI_CHANERR_INT_OFFSET, chanerr);
Dan Williamsa6d52d72009-12-19 15:36:02 -07001556
Dave Jiang6ead7e42013-03-26 15:42:59 -07001557 /* Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
1558 * (workaround for spurious config parity error after restart)
1559 */
1560 pci_read_config_word(pdev, IOAT_PCI_DEVICE_ID_OFFSET, &dev_id);
1561 if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0) {
1562 pci_write_config_dword(pdev,
1563 IOAT_PCI_DMAUNCERRSTS_OFFSET,
1564 0x10);
1565 }
1566 }
Dan Williamsa6d52d72009-12-19 15:36:02 -07001567
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001568 err = ioat2_reset_sync(chan, msecs_to_jiffies(200));
Dan Williams779e5612013-11-13 16:30:43 -08001569 if (!err)
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001570 err = ioat3_irq_reinit(device);
Dave Jiang1a363062012-12-03 16:08:37 -07001571
Dan Williams779e5612013-11-13 16:30:43 -08001572 if (err)
1573 dev_err(&pdev->dev, "Failed to reset: %d\n", err);
1574
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001575 return err;
Dave Jiang570727b2013-03-25 14:37:31 -07001576}
1577
Dave Jiang75c6f0a2013-04-10 16:44:39 -07001578static void ioat3_intr_quirk(struct ioatdma_device *device)
1579{
1580 struct dma_device *dma;
1581 struct dma_chan *c;
1582 struct ioat_chan_common *chan;
1583 u32 errmask;
1584
1585 dma = &device->common;
1586
1587 /*
1588 * if we have descriptor write back error status, we mask the
1589 * error interrupts
1590 */
1591 if (device->cap & IOAT_CAP_DWBES) {
1592 list_for_each_entry(c, &dma->channels, device_node) {
1593 chan = to_chan_common(c);
1594 errmask = readl(chan->reg_base +
1595 IOAT_CHANERR_MASK_OFFSET);
1596 errmask |= IOAT_CHANERR_XOR_P_OR_CRC_ERR |
1597 IOAT_CHANERR_XOR_Q_ERR;
1598 writel(errmask, chan->reg_base +
1599 IOAT_CHANERR_MASK_OFFSET);
1600 }
1601 }
1602}
1603
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08001604int ioat3_dma_probe(struct ioatdma_device *device, int dca)
Dan Williamsbf40a682009-09-08 17:42:55 -07001605{
1606 struct pci_dev *pdev = device->pdev;
Dan Williams228c4f52009-11-19 17:07:10 -07001607 int dca_en = system_has_dca_enabled(pdev);
Dan Williamsbf40a682009-09-08 17:42:55 -07001608 struct dma_device *dma;
1609 struct dma_chan *c;
1610 struct ioat_chan_common *chan;
Dan Williamse3232712009-09-08 17:43:02 -07001611 bool is_raid_device = false;
Dan Williamsbf40a682009-09-08 17:42:55 -07001612 int err;
Dan Williamsbf40a682009-09-08 17:42:55 -07001613
1614 device->enumerate_channels = ioat2_enumerate_channels;
Dan Williamsa6d52d72009-12-19 15:36:02 -07001615 device->reset_hw = ioat3_reset_hw;
Dan Williams9de6fc72009-09-08 17:42:58 -07001616 device->self_test = ioat3_dma_self_test;
Dave Jiang75c6f0a2013-04-10 16:44:39 -07001617 device->intr_quirk = ioat3_intr_quirk;
Dan Williamsbf40a682009-09-08 17:42:55 -07001618 dma = &device->common;
1619 dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy_lock;
1620 dma->device_issue_pending = ioat2_issue_pending;
1621 dma->device_alloc_chan_resources = ioat2_alloc_chan_resources;
1622 dma->device_free_chan_resources = ioat2_free_chan_resources;
Dan Williams58c86492009-09-08 17:43:00 -07001623
1624 dma_cap_set(DMA_INTERRUPT, dma->cap_mask);
1625 dma->device_prep_dma_interrupt = ioat3_prep_interrupt_lock;
1626
Dave Jiang75c6f0a2013-04-10 16:44:39 -07001627 device->cap = readl(device->reg_base + IOAT_DMA_CAP_OFFSET);
Dan Williams228c4f52009-11-19 17:07:10 -07001628
Brice Goglinc4dcf0e2013-08-02 21:18:03 +02001629 if (is_xeon_cb32(pdev) || is_bwd_noraid(pdev))
Dave Jiang75c6f0a2013-04-10 16:44:39 -07001630 device->cap &= ~(IOAT_CAP_XOR | IOAT_CAP_PQ | IOAT_CAP_RAID16SS);
Dave Jiangd3023982013-04-10 16:44:32 -07001631
Dan Williams228c4f52009-11-19 17:07:10 -07001632 /* dca is incompatible with raid operations */
Dave Jiang75c6f0a2013-04-10 16:44:39 -07001633 if (dca_en && (device->cap & (IOAT_CAP_XOR|IOAT_CAP_PQ)))
1634 device->cap &= ~(IOAT_CAP_XOR|IOAT_CAP_PQ);
Dan Williams228c4f52009-11-19 17:07:10 -07001635
Dave Jiang75c6f0a2013-04-10 16:44:39 -07001636 if (device->cap & IOAT_CAP_XOR) {
Dan Williamse3232712009-09-08 17:43:02 -07001637 is_raid_device = true;
Dan Williamsb094ad32009-09-08 17:42:57 -07001638 dma->max_xor = 8;
Dan Williamsb094ad32009-09-08 17:42:57 -07001639
1640 dma_cap_set(DMA_XOR, dma->cap_mask);
1641 dma->device_prep_dma_xor = ioat3_prep_xor;
1642
1643 dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
1644 dma->device_prep_dma_xor_val = ioat3_prep_xor_val;
1645 }
Dave Jiangeceec442013-03-26 15:43:15 -07001646
Dave Jiang75c6f0a2013-04-10 16:44:39 -07001647 if (device->cap & IOAT_CAP_PQ) {
Dan Williamse3232712009-09-08 17:43:02 -07001648 is_raid_device = true;
Dave Jiang7727eaa2013-04-15 10:25:56 -07001649
Dave Jiang75c6f0a2013-04-10 16:44:39 -07001650 dma->device_prep_dma_pq = ioat3_prep_pq;
1651 dma->device_prep_dma_pq_val = ioat3_prep_pq_val;
1652 dma_cap_set(DMA_PQ, dma->cap_mask);
1653 dma_cap_set(DMA_PQ_VAL, dma->cap_mask);
1654
1655 if (device->cap & IOAT_CAP_RAID16SS) {
Dave Jiang7727eaa2013-04-15 10:25:56 -07001656 dma_set_maxpq(dma, 16, 0);
Dave Jiang7727eaa2013-04-15 10:25:56 -07001657 } else {
1658 dma_set_maxpq(dma, 8, 0);
Dave Jiang7727eaa2013-04-15 10:25:56 -07001659 }
Dan Williamsd69d235b2009-09-08 17:42:59 -07001660
Dave Jiang75c6f0a2013-04-10 16:44:39 -07001661 if (!(device->cap & IOAT_CAP_XOR)) {
1662 dma->device_prep_dma_xor = ioat3_prep_pqxor;
1663 dma->device_prep_dma_xor_val = ioat3_prep_pqxor_val;
1664 dma_cap_set(DMA_XOR, dma->cap_mask);
1665 dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
Dan Williamsd69d235b2009-09-08 17:42:59 -07001666
Dave Jiang75c6f0a2013-04-10 16:44:39 -07001667 if (device->cap & IOAT_CAP_RAID16SS) {
Dave Jiang7727eaa2013-04-15 10:25:56 -07001668 dma->max_xor = 16;
Dave Jiang7727eaa2013-04-15 10:25:56 -07001669 } else {
1670 dma->max_xor = 8;
Dave Jiang7727eaa2013-04-15 10:25:56 -07001671 }
Dan Williamsae786622009-09-08 17:43:00 -07001672 }
Dan Williamsd69d235b2009-09-08 17:42:59 -07001673 }
Dave Jiangeceec442013-03-26 15:43:15 -07001674
Dave Jiang9a37f642013-02-26 09:20:36 -07001675 dma->device_tx_status = ioat3_tx_status;
1676 device->cleanup_fn = ioat3_cleanup_event;
1677 device->timer_fn = ioat3_timer_event;
Dan Williamsbf40a682009-09-08 17:42:55 -07001678
Dave Jiang7727eaa2013-04-15 10:25:56 -07001679 /* starting with CB3.3 super extended descriptors are supported */
Dave Jiang75c6f0a2013-04-10 16:44:39 -07001680 if (device->cap & IOAT_CAP_RAID16SS) {
Dave Jiang7727eaa2013-04-15 10:25:56 -07001681 char pool_name[14];
1682 int i;
1683
Dave Jiang7727eaa2013-04-15 10:25:56 -07001684 for (i = 0; i < MAX_SED_POOLS; i++) {
1685 snprintf(pool_name, 14, "ioat_hw%d_sed", i);
1686
1687 /* allocate SED DMA pool */
Dan Williams59056e82013-11-13 10:57:18 -08001688 device->sed_hw_pool[i] = dmam_pool_create(pool_name,
Dave Jiang7727eaa2013-04-15 10:25:56 -07001689 &pdev->dev,
1690 SED_SIZE * (i + 1), 64, 0);
1691 if (!device->sed_hw_pool[i])
Dan Williams59056e82013-11-13 10:57:18 -08001692 return -ENOMEM;
Dave Jiang7727eaa2013-04-15 10:25:56 -07001693
1694 }
1695 }
1696
Dan Williamsbf40a682009-09-08 17:42:55 -07001697 err = ioat_probe(device);
1698 if (err)
1699 return err;
Dan Williamsbf40a682009-09-08 17:42:55 -07001700
1701 list_for_each_entry(c, &dma->channels, device_node) {
1702 chan = to_chan_common(c);
1703 writel(IOAT_DMA_DCA_ANY_CPU,
1704 chan->reg_base + IOAT_DCACTRL_OFFSET);
1705 }
1706
1707 err = ioat_register(device);
1708 if (err)
1709 return err;
Dan Williams5669e312009-09-08 17:42:56 -07001710
1711 ioat_kobject_add(device, &ioat2_ktype);
1712
Dan Williamsbf40a682009-09-08 17:42:55 -07001713 if (dca)
1714 device->dca = ioat3_dca_init(pdev, device->reg_base);
1715
1716 return 0;
Dan Williamsbf40a682009-09-08 17:42:55 -07001717}