blob: 820817e97e626a498561a9e5f0f3a61f22ffc9fa [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:
217 return true;
218 default:
219 return false;
220 }
221}
222
Dave Jiangd3023982013-04-10 16:44:32 -0700223static bool is_bwd_noraid(struct pci_dev *pdev)
224{
225 switch (pdev->device) {
226 case PCI_DEVICE_ID_INTEL_IOAT_BWD2:
227 case PCI_DEVICE_ID_INTEL_IOAT_BWD3:
228 return true;
229 default:
230 return false;
231 }
232
233}
234
Dave Jiang7727eaa2013-04-15 10:25:56 -0700235static void pq16_set_src(struct ioat_raw_descriptor *desc[3],
Paul Bollee6a5fa62013-07-20 20:05:05 +0200236 dma_addr_t addr, u32 offset, u8 coef, unsigned idx)
Dave Jiang7727eaa2013-04-15 10:25:56 -0700237{
238 struct ioat_pq_descriptor *pq = (struct ioat_pq_descriptor *)desc[0];
239 struct ioat_pq16a_descriptor *pq16 =
240 (struct ioat_pq16a_descriptor *)desc[1];
241 struct ioat_raw_descriptor *raw = desc[pq16_idx_to_desc[idx]];
242
243 raw->field[pq16_idx_to_field[idx]] = addr + offset;
244
245 if (idx < 8)
246 pq->coef[idx] = coef;
247 else
248 pq16->coef[idx - 8] = coef;
249}
250
Fengguang Wue6a30fe2013-04-16 13:41:26 +0800251static struct ioat_sed_ent *
Dave Jiang7727eaa2013-04-15 10:25:56 -0700252ioat3_alloc_sed(struct ioatdma_device *device, unsigned int hw_pool)
253{
254 struct ioat_sed_ent *sed;
255 gfp_t flags = __GFP_ZERO | GFP_ATOMIC;
256
Dan Williams59056e82013-11-13 10:57:18 -0800257 sed = kmem_cache_alloc(ioat3_sed_cache, flags);
Dave Jiang7727eaa2013-04-15 10:25:56 -0700258 if (!sed)
259 return NULL;
260
261 sed->hw_pool = hw_pool;
262 sed->hw = dma_pool_alloc(device->sed_hw_pool[hw_pool],
263 flags, &sed->dma);
264 if (!sed->hw) {
Dan Williams59056e82013-11-13 10:57:18 -0800265 kmem_cache_free(ioat3_sed_cache, sed);
Dave Jiang7727eaa2013-04-15 10:25:56 -0700266 return NULL;
267 }
268
269 return sed;
270}
271
Fengguang Wue6a30fe2013-04-16 13:41:26 +0800272static void ioat3_free_sed(struct ioatdma_device *device, struct ioat_sed_ent *sed)
Dave Jiang7727eaa2013-04-15 10:25:56 -0700273{
274 if (!sed)
275 return;
276
277 dma_pool_free(device->sed_hw_pool[sed->hw_pool], sed->hw, sed->dma);
Dan Williams59056e82013-11-13 10:57:18 -0800278 kmem_cache_free(ioat3_sed_cache, sed);
Dan Williamsbf40a682009-09-08 17:42:55 -0700279}
280
Dan Williamsb094ad32009-09-08 17:42:57 -0700281static bool desc_has_ext(struct ioat_ring_ent *desc)
282{
283 struct ioat_dma_descriptor *hw = desc->hw;
Dan Williamsbf40a682009-09-08 17:42:55 -0700284
Dan Williamsb094ad32009-09-08 17:42:57 -0700285 if (hw->ctl_f.op == IOAT_OP_XOR ||
286 hw->ctl_f.op == IOAT_OP_XOR_VAL) {
287 struct ioat_xor_descriptor *xor = desc->xor;
288
289 if (src_cnt_to_sw(xor->ctl_f.src_cnt) > 5)
290 return true;
Dan Williamsd69d235b2009-09-08 17:42:59 -0700291 } else if (hw->ctl_f.op == IOAT_OP_PQ ||
292 hw->ctl_f.op == IOAT_OP_PQ_VAL) {
293 struct ioat_pq_descriptor *pq = desc->pq;
294
295 if (src_cnt_to_sw(pq->ctl_f.src_cnt) > 3)
296 return true;
Dan Williamsb094ad32009-09-08 17:42:57 -0700297 }
298
299 return false;
300}
301
Dave Jiang3f09ede2013-03-26 15:43:09 -0700302static u64 ioat3_get_current_completion(struct ioat_chan_common *chan)
303{
304 u64 phys_complete;
305 u64 completion;
306
307 completion = *chan->completion;
308 phys_complete = ioat_chansts_to_addr(completion);
309
310 dev_dbg(to_dev(chan), "%s: phys_complete: %#llx\n", __func__,
311 (unsigned long long) phys_complete);
312
313 return phys_complete;
314}
315
316static bool ioat3_cleanup_preamble(struct ioat_chan_common *chan,
317 u64 *phys_complete)
318{
319 *phys_complete = ioat3_get_current_completion(chan);
320 if (*phys_complete == chan->last_completion)
321 return false;
322
323 clear_bit(IOAT_COMPLETION_ACK, &chan->state);
324 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
325
326 return true;
327}
328
Dave Jiang75c6f0a2013-04-10 16:44:39 -0700329static void
330desc_get_errstat(struct ioat2_dma_chan *ioat, struct ioat_ring_ent *desc)
331{
332 struct ioat_dma_descriptor *hw = desc->hw;
333
334 switch (hw->ctl_f.op) {
335 case IOAT_OP_PQ_VAL:
336 case IOAT_OP_PQ_VAL_16S:
337 {
338 struct ioat_pq_descriptor *pq = desc->pq;
339
340 /* check if there's error written */
341 if (!pq->dwbes_f.wbes)
342 return;
343
344 /* need to set a chanerr var for checking to clear later */
345
346 if (pq->dwbes_f.p_val_err)
347 *desc->result |= SUM_CHECK_P_RESULT;
348
349 if (pq->dwbes_f.q_val_err)
350 *desc->result |= SUM_CHECK_Q_RESULT;
351
352 return;
353 }
354 default:
355 return;
356 }
357}
358
Dan Williamsb094ad32009-09-08 17:42:57 -0700359/**
360 * __cleanup - reclaim used descriptors
361 * @ioat: channel (ring) to clean
362 *
363 * The difference from the dma_v2.c __cleanup() is that this routine
364 * handles extended descriptors and dma-unmapping raid operations.
365 */
Dan Williams27502932012-03-23 13:36:42 -0700366static void __cleanup(struct ioat2_dma_chan *ioat, dma_addr_t phys_complete)
Dan Williamsbf40a682009-09-08 17:42:55 -0700367{
368 struct ioat_chan_common *chan = &ioat->base;
Dave Jiang7727eaa2013-04-15 10:25:56 -0700369 struct ioatdma_device *device = chan->device;
Dan Williamsbf40a682009-09-08 17:42:55 -0700370 struct ioat_ring_ent *desc;
371 bool seen_current = false;
Dan Williams074cc472010-05-01 15:22:55 -0700372 int idx = ioat->tail, i;
Dan Williamsbf40a682009-09-08 17:42:55 -0700373 u16 active;
Dan Williamsbf40a682009-09-08 17:42:55 -0700374
375 dev_dbg(to_dev(chan), "%s: head: %#x tail: %#x issued: %#x\n",
376 __func__, ioat->head, ioat->tail, ioat->issued);
377
Dave Jiang3f09ede2013-03-26 15:43:09 -0700378 /*
379 * At restart of the channel, the completion address and the
380 * channel status will be 0 due to starting a new chain. Since
381 * it's new chain and the first descriptor "fails", there is
382 * nothing to clean up. We do not want to reap the entire submitted
383 * chain due to this 0 address value and then BUG.
384 */
385 if (!phys_complete)
386 return;
387
Dan Williamsbf40a682009-09-08 17:42:55 -0700388 active = ioat2_ring_active(ioat);
389 for (i = 0; i < active && !seen_current; i++) {
390 struct dma_async_tx_descriptor *tx;
391
Dan Williams074cc472010-05-01 15:22:55 -0700392 smp_read_barrier_depends();
393 prefetch(ioat2_get_ring_ent(ioat, idx + i + 1));
394 desc = ioat2_get_ring_ent(ioat, idx + i);
Dan Williamsbf40a682009-09-08 17:42:55 -0700395 dump_desc_dbg(ioat, desc);
Dave Jiang75c6f0a2013-04-10 16:44:39 -0700396
397 /* set err stat if we are using dwbes */
398 if (device->cap & IOAT_CAP_DWBES)
399 desc_get_errstat(ioat, desc);
400
Dan Williamsbf40a682009-09-08 17:42:55 -0700401 tx = &desc->txd;
402 if (tx->cookie) {
Russell King - ARM Linuxf7fbce02012-03-06 22:35:07 +0000403 dma_cookie_complete(tx);
Dan Williamsd38a8c62013-10-18 19:35:23 +0200404 dma_descriptor_unmap(tx);
Dan Williamsbf40a682009-09-08 17:42:55 -0700405 if (tx->callback) {
406 tx->callback(tx->callback_param);
407 tx->callback = NULL;
408 }
409 }
410
411 if (tx->phys == phys_complete)
412 seen_current = true;
Dan Williamsb094ad32009-09-08 17:42:57 -0700413
414 /* skip extended descriptors */
415 if (desc_has_ext(desc)) {
416 BUG_ON(i + 1 >= active);
417 i++;
418 }
Dave Jiang7727eaa2013-04-15 10:25:56 -0700419
420 /* cleanup super extended descriptors */
421 if (desc->sed) {
422 ioat3_free_sed(device, desc->sed);
423 desc->sed = NULL;
424 }
Dan Williamsbf40a682009-09-08 17:42:55 -0700425 }
Dan Williams074cc472010-05-01 15:22:55 -0700426 smp_mb(); /* finish all descriptor reads before incrementing tail */
427 ioat->tail = idx + i;
Dan Williamsaa75db02010-03-03 21:21:10 -0700428 BUG_ON(active && !seen_current); /* no active descs have written a completion? */
Dan Williamsbf40a682009-09-08 17:42:55 -0700429 chan->last_completion = phys_complete;
Dan Williamsb9cc9862010-03-03 21:21:13 -0700430
Dan Williams074cc472010-05-01 15:22:55 -0700431 if (active - i == 0) {
Dan Williamsbf40a682009-09-08 17:42:55 -0700432 dev_dbg(to_dev(chan), "%s: cancel completion timeout\n",
433 __func__);
434 clear_bit(IOAT_COMPLETION_PENDING, &chan->state);
435 mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
436 }
Dan Williamsb9cc9862010-03-03 21:21:13 -0700437 /* 5 microsecond delay per pending descriptor */
Dan Williams074cc472010-05-01 15:22:55 -0700438 writew(min((5 * (active - i)), IOAT_INTRDELAY_MASK),
Dan Williamsb9cc9862010-03-03 21:21:13 -0700439 chan->device->reg_base + IOAT_INTRDELAY_OFFSET);
Dan Williamsbf40a682009-09-08 17:42:55 -0700440}
441
Dan Williams074cc472010-05-01 15:22:55 -0700442static void ioat3_cleanup(struct ioat2_dma_chan *ioat)
Dan Williamsbf40a682009-09-08 17:42:55 -0700443{
444 struct ioat_chan_common *chan = &ioat->base;
Dave Jiang3f09ede2013-03-26 15:43:09 -0700445 u64 phys_complete;
Dan Williamsbf40a682009-09-08 17:42:55 -0700446
Dan Williamsb9cc9862010-03-03 21:21:13 -0700447 spin_lock_bh(&chan->cleanup_lock);
Dave Jiang3f09ede2013-03-26 15:43:09 -0700448
449 if (ioat3_cleanup_preamble(chan, &phys_complete))
Dan Williams074cc472010-05-01 15:22:55 -0700450 __cleanup(ioat, phys_complete);
Dave Jiang3f09ede2013-03-26 15:43:09 -0700451
452 if (is_ioat_halted(*chan->completion)) {
453 u32 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
454
455 if (chanerr & IOAT_CHANERR_HANDLE_MASK) {
456 mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
457 ioat3_eh(ioat);
458 }
459 }
460
Dan Williamsb9cc9862010-03-03 21:21:13 -0700461 spin_unlock_bh(&chan->cleanup_lock);
462}
463
Dan Williamsaa4d72a2010-03-03 21:21:13 -0700464static void ioat3_cleanup_event(unsigned long data)
Dan Williamsbf40a682009-09-08 17:42:55 -0700465{
Dan Williamsaa4d72a2010-03-03 21:21:13 -0700466 struct ioat2_dma_chan *ioat = to_ioat2_chan((void *) data);
Dan Williamsbf40a682009-09-08 17:42:55 -0700467
Dan Williams074cc472010-05-01 15:22:55 -0700468 ioat3_cleanup(ioat);
Dan Williams773d9e22010-03-03 11:47:42 -0700469 writew(IOAT_CHANCTRL_RUN, ioat->base.reg_base + IOAT_CHANCTRL_OFFSET);
Dan Williamsbf40a682009-09-08 17:42:55 -0700470}
471
472static void ioat3_restart_channel(struct ioat2_dma_chan *ioat)
473{
474 struct ioat_chan_common *chan = &ioat->base;
Dave Jiang3f09ede2013-03-26 15:43:09 -0700475 u64 phys_complete;
Dan Williamsbf40a682009-09-08 17:42:55 -0700476
Dan Williamsb372ec22010-03-03 11:47:42 -0700477 ioat2_quiesce(chan, 0);
Dave Jiang3f09ede2013-03-26 15:43:09 -0700478 if (ioat3_cleanup_preamble(chan, &phys_complete))
Dan Williamsbf40a682009-09-08 17:42:55 -0700479 __cleanup(ioat, phys_complete);
480
481 __ioat2_restart_chan(ioat);
482}
483
Dave Jiang3f09ede2013-03-26 15:43:09 -0700484static void ioat3_eh(struct ioat2_dma_chan *ioat)
485{
486 struct ioat_chan_common *chan = &ioat->base;
487 struct pci_dev *pdev = to_pdev(chan);
488 struct ioat_dma_descriptor *hw;
489 u64 phys_complete;
490 struct ioat_ring_ent *desc;
491 u32 err_handled = 0;
492 u32 chanerr_int;
493 u32 chanerr;
494
495 /* cleanup so tail points to descriptor that caused the error */
496 if (ioat3_cleanup_preamble(chan, &phys_complete))
497 __cleanup(ioat, phys_complete);
498
499 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
500 pci_read_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, &chanerr_int);
501
502 dev_dbg(to_dev(chan), "%s: error = %x:%x\n",
503 __func__, chanerr, chanerr_int);
504
505 desc = ioat2_get_ring_ent(ioat, ioat->tail);
506 hw = desc->hw;
507 dump_desc_dbg(ioat, desc);
508
509 switch (hw->ctl_f.op) {
510 case IOAT_OP_XOR_VAL:
511 if (chanerr & IOAT_CHANERR_XOR_P_OR_CRC_ERR) {
512 *desc->result |= SUM_CHECK_P_RESULT;
513 err_handled |= IOAT_CHANERR_XOR_P_OR_CRC_ERR;
514 }
515 break;
516 case IOAT_OP_PQ_VAL:
Dave Jiang7727eaa2013-04-15 10:25:56 -0700517 case IOAT_OP_PQ_VAL_16S:
Dave Jiang3f09ede2013-03-26 15:43:09 -0700518 if (chanerr & IOAT_CHANERR_XOR_P_OR_CRC_ERR) {
519 *desc->result |= SUM_CHECK_P_RESULT;
520 err_handled |= IOAT_CHANERR_XOR_P_OR_CRC_ERR;
521 }
522 if (chanerr & IOAT_CHANERR_XOR_Q_ERR) {
523 *desc->result |= SUM_CHECK_Q_RESULT;
524 err_handled |= IOAT_CHANERR_XOR_Q_ERR;
525 }
526 break;
527 }
528
529 /* fault on unhandled error or spurious halt */
530 if (chanerr ^ err_handled || chanerr == 0) {
531 dev_err(to_dev(chan), "%s: fatal error (%x:%x)\n",
532 __func__, chanerr, err_handled);
533 BUG();
534 }
535
536 writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
537 pci_write_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, chanerr_int);
538
539 /* mark faulting descriptor as complete */
540 *chan->completion = desc->txd.phys;
541
542 spin_lock_bh(&ioat->prep_lock);
543 ioat3_restart_channel(ioat);
544 spin_unlock_bh(&ioat->prep_lock);
545}
546
Dave Jiang4dec23d2013-02-07 14:38:32 -0700547static void check_active(struct ioat2_dma_chan *ioat)
Dan Williamsbf40a682009-09-08 17:42:55 -0700548{
Dan Williamsbf40a682009-09-08 17:42:55 -0700549 struct ioat_chan_common *chan = &ioat->base;
550
Dave Jiang4dec23d2013-02-07 14:38:32 -0700551 if (ioat2_ring_active(ioat)) {
552 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
553 return;
554 }
Dan Williamsbf40a682009-09-08 17:42:55 -0700555
Dave Jiang4dec23d2013-02-07 14:38:32 -0700556 if (test_and_clear_bit(IOAT_CHAN_ACTIVE, &chan->state))
557 mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
558 else if (ioat->alloc_order > ioat_get_alloc_order()) {
Dan Williamsbf40a682009-09-08 17:42:55 -0700559 /* if the ring is idle, empty, and oversized try to step
560 * down the size
561 */
Dave Jiang4dec23d2013-02-07 14:38:32 -0700562 reshape_ring(ioat, ioat->alloc_order - 1);
Dan Williamsbf40a682009-09-08 17:42:55 -0700563
564 /* keep shrinking until we get back to our minimum
565 * default size
566 */
567 if (ioat->alloc_order > ioat_get_alloc_order())
568 mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
569 }
Dave Jiang4dec23d2013-02-07 14:38:32 -0700570
571}
572
Fengguang Wua20702b2013-02-13 09:40:03 +0800573static void ioat3_timer_event(unsigned long data)
Dave Jiang4dec23d2013-02-07 14:38:32 -0700574{
575 struct ioat2_dma_chan *ioat = to_ioat2_chan((void *) data);
576 struct ioat_chan_common *chan = &ioat->base;
577 dma_addr_t phys_complete;
578 u64 status;
579
580 status = ioat_chansts(chan);
581
582 /* when halted due to errors check for channel
583 * programming errors before advancing the completion state
584 */
585 if (is_ioat_halted(status)) {
586 u32 chanerr;
587
588 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
589 dev_err(to_dev(chan), "%s: Channel halted (%x)\n",
590 __func__, chanerr);
591 if (test_bit(IOAT_RUN, &chan->state))
592 BUG_ON(is_ioat_bug(chanerr));
593 else /* we never got off the ground */
594 return;
595 }
596
597 /* if we haven't made progress and we have already
598 * acknowledged a pending completion once, then be more
599 * forceful with a restart
600 */
601 spin_lock_bh(&chan->cleanup_lock);
602 if (ioat_cleanup_preamble(chan, &phys_complete))
603 __cleanup(ioat, phys_complete);
604 else if (test_bit(IOAT_COMPLETION_ACK, &chan->state)) {
605 spin_lock_bh(&ioat->prep_lock);
606 ioat3_restart_channel(ioat);
607 spin_unlock_bh(&ioat->prep_lock);
608 spin_unlock_bh(&chan->cleanup_lock);
609 return;
610 } else {
611 set_bit(IOAT_COMPLETION_ACK, &chan->state);
612 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
613 }
614
615
616 if (ioat2_ring_active(ioat))
617 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
618 else {
619 spin_lock_bh(&ioat->prep_lock);
620 check_active(ioat);
621 spin_unlock_bh(&ioat->prep_lock);
622 }
623 spin_unlock_bh(&chan->cleanup_lock);
Dan Williamsbf40a682009-09-08 17:42:55 -0700624}
625
626static enum dma_status
Linus Walleij07934482010-03-26 16:50:49 -0700627ioat3_tx_status(struct dma_chan *c, dma_cookie_t cookie,
628 struct dma_tx_state *txstate)
Dan Williamsbf40a682009-09-08 17:42:55 -0700629{
630 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000631 enum dma_status ret;
Dan Williamsbf40a682009-09-08 17:42:55 -0700632
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000633 ret = dma_cookie_status(c, cookie, txstate);
Vinod Koul2f16f802013-10-16 20:48:52 +0530634 if (ret == DMA_COMPLETE)
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000635 return ret;
Dan Williamsbf40a682009-09-08 17:42:55 -0700636
Dan Williams074cc472010-05-01 15:22:55 -0700637 ioat3_cleanup(ioat);
Dan Williamsbf40a682009-09-08 17:42:55 -0700638
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000639 return dma_cookie_status(c, cookie, txstate);
Dan Williamsbf40a682009-09-08 17:42:55 -0700640}
641
642static struct dma_async_tx_descriptor *
Dan Williamsb094ad32009-09-08 17:42:57 -0700643__ioat3_prep_xor_lock(struct dma_chan *c, enum sum_check_flags *result,
644 dma_addr_t dest, dma_addr_t *src, unsigned int src_cnt,
645 size_t len, unsigned long flags)
646{
647 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
648 struct ioat_ring_ent *compl_desc;
649 struct ioat_ring_ent *desc;
650 struct ioat_ring_ent *ext;
651 size_t total_len = len;
652 struct ioat_xor_descriptor *xor;
653 struct ioat_xor_ext_descriptor *xor_ex = NULL;
654 struct ioat_dma_descriptor *hw;
Dan Williams074cc472010-05-01 15:22:55 -0700655 int num_descs, with_ext, idx, i;
Dan Williamsb094ad32009-09-08 17:42:57 -0700656 u32 offset = 0;
Dan Williamsb094ad32009-09-08 17:42:57 -0700657 u8 op = result ? IOAT_OP_XOR_VAL : IOAT_OP_XOR;
658
659 BUG_ON(src_cnt < 2);
660
661 num_descs = ioat2_xferlen_to_descs(ioat, len);
662 /* we need 2x the number of descriptors to cover greater than 5
663 * sources
664 */
665 if (src_cnt > 5) {
666 with_ext = 1;
667 num_descs *= 2;
668 } else
669 with_ext = 0;
670
671 /* completion writes from the raid engine may pass completion
672 * writes from the legacy engine, so we need one extra null
673 * (legacy) descriptor to ensure all completion writes arrive in
674 * order.
675 */
Dan Williams074cc472010-05-01 15:22:55 -0700676 if (likely(num_descs) && ioat2_check_space_lock(ioat, num_descs+1) == 0)
677 idx = ioat->head;
Dan Williamsb094ad32009-09-08 17:42:57 -0700678 else
679 return NULL;
Dan Williamscdef57d2009-09-21 09:22:29 -0700680 i = 0;
681 do {
Dan Williamsb094ad32009-09-08 17:42:57 -0700682 struct ioat_raw_descriptor *descs[2];
683 size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
684 int s;
685
686 desc = ioat2_get_ring_ent(ioat, idx + i);
687 xor = desc->xor;
688
689 /* save a branch by unconditionally retrieving the
690 * extended descriptor xor_set_src() knows to not write
691 * to it in the single descriptor case
692 */
693 ext = ioat2_get_ring_ent(ioat, idx + i + 1);
694 xor_ex = ext->xor_ex;
695
696 descs[0] = (struct ioat_raw_descriptor *) xor;
697 descs[1] = (struct ioat_raw_descriptor *) xor_ex;
698 for (s = 0; s < src_cnt; s++)
699 xor_set_src(descs, src[s], offset, s);
700 xor->size = xfer_size;
701 xor->dst_addr = dest + offset;
702 xor->ctl = 0;
703 xor->ctl_f.op = op;
704 xor->ctl_f.src_cnt = src_cnt_to_hw(src_cnt);
705
706 len -= xfer_size;
707 offset += xfer_size;
708 dump_desc_dbg(ioat, desc);
Dan Williamscdef57d2009-09-21 09:22:29 -0700709 } while ((i += 1 + with_ext) < num_descs);
Dan Williamsb094ad32009-09-08 17:42:57 -0700710
711 /* last xor descriptor carries the unmap parameters and fence bit */
712 desc->txd.flags = flags;
713 desc->len = total_len;
714 if (result)
715 desc->result = result;
716 xor->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
717
718 /* completion descriptor carries interrupt bit */
719 compl_desc = ioat2_get_ring_ent(ioat, idx + i);
720 compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
721 hw = compl_desc->hw;
722 hw->ctl = 0;
723 hw->ctl_f.null = 1;
724 hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
725 hw->ctl_f.compl_write = 1;
726 hw->size = NULL_DESC_BUFFER_SIZE;
727 dump_desc_dbg(ioat, compl_desc);
728
729 /* we leave the channel locked to ensure in order submission */
Dan Williams49954c12009-11-19 17:11:03 -0700730 return &compl_desc->txd;
Dan Williamsb094ad32009-09-08 17:42:57 -0700731}
732
733static struct dma_async_tx_descriptor *
734ioat3_prep_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
735 unsigned int src_cnt, size_t len, unsigned long flags)
736{
737 return __ioat3_prep_xor_lock(chan, NULL, dest, src, src_cnt, len, flags);
738}
739
740struct dma_async_tx_descriptor *
741ioat3_prep_xor_val(struct dma_chan *chan, dma_addr_t *src,
742 unsigned int src_cnt, size_t len,
743 enum sum_check_flags *result, unsigned long flags)
744{
745 /* the cleanup routine only sets bits on validate failure, it
746 * does not clear bits on validate success... so clear it here
747 */
748 *result = 0;
749
750 return __ioat3_prep_xor_lock(chan, result, src[0], &src[1],
751 src_cnt - 1, len, flags);
752}
753
Dan Williamsd69d235b2009-09-08 17:42:59 -0700754static void
755dump_pq_desc_dbg(struct ioat2_dma_chan *ioat, struct ioat_ring_ent *desc, struct ioat_ring_ent *ext)
756{
757 struct device *dev = to_dev(&ioat->base);
758 struct ioat_pq_descriptor *pq = desc->pq;
759 struct ioat_pq_ext_descriptor *pq_ex = ext ? ext->pq_ex : NULL;
760 struct ioat_raw_descriptor *descs[] = { (void *) pq, (void *) pq_ex };
761 int src_cnt = src_cnt_to_sw(pq->ctl_f.src_cnt);
762 int i;
763
764 dev_dbg(dev, "desc[%d]: (%#llx->%#llx) flags: %#x"
Dave Jiang7727eaa2013-04-15 10:25:56 -0700765 " sz: %#10.8x ctl: %#x (op: %#x int: %d compl: %d pq: '%s%s'"
766 " src_cnt: %d)\n",
Dan Williamsd69d235b2009-09-08 17:42:59 -0700767 desc_id(desc), (unsigned long long) desc->txd.phys,
768 (unsigned long long) (pq_ex ? pq_ex->next : pq->next),
769 desc->txd.flags, pq->size, pq->ctl, pq->ctl_f.op, pq->ctl_f.int_en,
770 pq->ctl_f.compl_write,
771 pq->ctl_f.p_disable ? "" : "p", pq->ctl_f.q_disable ? "" : "q",
772 pq->ctl_f.src_cnt);
773 for (i = 0; i < src_cnt; i++)
774 dev_dbg(dev, "\tsrc[%d]: %#llx coef: %#x\n", i,
775 (unsigned long long) pq_get_src(descs, i), pq->coef[i]);
776 dev_dbg(dev, "\tP: %#llx\n", pq->p_addr);
777 dev_dbg(dev, "\tQ: %#llx\n", pq->q_addr);
Dave Jiang50f9f972013-03-04 10:59:54 -0700778 dev_dbg(dev, "\tNEXT: %#llx\n", pq->next);
Dan Williamsd69d235b2009-09-08 17:42:59 -0700779}
780
Dave Jiang7727eaa2013-04-15 10:25:56 -0700781static void dump_pq16_desc_dbg(struct ioat2_dma_chan *ioat,
782 struct ioat_ring_ent *desc)
783{
784 struct device *dev = to_dev(&ioat->base);
785 struct ioat_pq_descriptor *pq = desc->pq;
786 struct ioat_raw_descriptor *descs[] = { (void *)pq,
787 (void *)pq,
788 (void *)pq };
789 int src_cnt = src16_cnt_to_sw(pq->ctl_f.src_cnt);
790 int i;
791
792 if (desc->sed) {
793 descs[1] = (void *)desc->sed->hw;
794 descs[2] = (void *)desc->sed->hw + 64;
795 }
796
797 dev_dbg(dev, "desc[%d]: (%#llx->%#llx) flags: %#x"
798 " sz: %#x ctl: %#x (op: %#x int: %d compl: %d pq: '%s%s'"
799 " src_cnt: %d)\n",
800 desc_id(desc), (unsigned long long) desc->txd.phys,
801 (unsigned long long) pq->next,
802 desc->txd.flags, pq->size, pq->ctl,
803 pq->ctl_f.op, pq->ctl_f.int_en,
804 pq->ctl_f.compl_write,
805 pq->ctl_f.p_disable ? "" : "p", pq->ctl_f.q_disable ? "" : "q",
806 pq->ctl_f.src_cnt);
807 for (i = 0; i < src_cnt; i++) {
808 dev_dbg(dev, "\tsrc[%d]: %#llx coef: %#x\n", i,
809 (unsigned long long) pq16_get_src(descs, i),
810 pq->coef[i]);
811 }
812 dev_dbg(dev, "\tP: %#llx\n", pq->p_addr);
813 dev_dbg(dev, "\tQ: %#llx\n", pq->q_addr);
814}
815
Dan Williamsd69d235b2009-09-08 17:42:59 -0700816static struct dma_async_tx_descriptor *
817__ioat3_prep_pq_lock(struct dma_chan *c, enum sum_check_flags *result,
818 const dma_addr_t *dst, const dma_addr_t *src,
819 unsigned int src_cnt, const unsigned char *scf,
820 size_t len, unsigned long flags)
821{
822 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
823 struct ioat_chan_common *chan = &ioat->base;
Dave Jiange0884772013-04-10 16:44:20 -0700824 struct ioatdma_device *device = chan->device;
Dan Williamsd69d235b2009-09-08 17:42:59 -0700825 struct ioat_ring_ent *compl_desc;
826 struct ioat_ring_ent *desc;
827 struct ioat_ring_ent *ext;
828 size_t total_len = len;
829 struct ioat_pq_descriptor *pq;
830 struct ioat_pq_ext_descriptor *pq_ex = NULL;
831 struct ioat_dma_descriptor *hw;
832 u32 offset = 0;
Dan Williamsd69d235b2009-09-08 17:42:59 -0700833 u8 op = result ? IOAT_OP_PQ_VAL : IOAT_OP_PQ;
Dan Williams074cc472010-05-01 15:22:55 -0700834 int i, s, idx, with_ext, num_descs;
Dave Jiange0884772013-04-10 16:44:20 -0700835 int cb32 = (device->version < IOAT_VER_3_3) ? 1 : 0;
Dan Williamsd69d235b2009-09-08 17:42:59 -0700836
837 dev_dbg(to_dev(chan), "%s\n", __func__);
838 /* the engine requires at least two sources (we provide
839 * at least 1 implied source in the DMA_PREP_CONTINUE case)
840 */
841 BUG_ON(src_cnt + dmaf_continue(flags) < 2);
842
843 num_descs = ioat2_xferlen_to_descs(ioat, len);
844 /* we need 2x the number of descriptors to cover greater than 3
Dan Williamscd788092009-12-17 13:52:39 -0700845 * sources (we need 1 extra source in the q-only continuation
846 * case and 3 extra sources in the p+q continuation case.
Dan Williamsd69d235b2009-09-08 17:42:59 -0700847 */
Dan Williamscd788092009-12-17 13:52:39 -0700848 if (src_cnt + dmaf_p_disabled_continue(flags) > 3 ||
849 (dmaf_continue(flags) && !dmaf_p_disabled_continue(flags))) {
Dan Williamsd69d235b2009-09-08 17:42:59 -0700850 with_ext = 1;
851 num_descs *= 2;
852 } else
853 with_ext = 0;
854
855 /* completion writes from the raid engine may pass completion
856 * writes from the legacy engine, so we need one extra null
857 * (legacy) descriptor to ensure all completion writes arrive in
858 * order.
859 */
860 if (likely(num_descs) &&
Dave Jiange0884772013-04-10 16:44:20 -0700861 ioat2_check_space_lock(ioat, num_descs + cb32) == 0)
Dan Williams074cc472010-05-01 15:22:55 -0700862 idx = ioat->head;
Dan Williamsd69d235b2009-09-08 17:42:59 -0700863 else
864 return NULL;
Dan Williamscdef57d2009-09-21 09:22:29 -0700865 i = 0;
866 do {
Dan Williamsd69d235b2009-09-08 17:42:59 -0700867 struct ioat_raw_descriptor *descs[2];
868 size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
869
870 desc = ioat2_get_ring_ent(ioat, idx + i);
871 pq = desc->pq;
872
873 /* save a branch by unconditionally retrieving the
874 * extended descriptor pq_set_src() knows to not write
875 * to it in the single descriptor case
876 */
877 ext = ioat2_get_ring_ent(ioat, idx + i + with_ext);
878 pq_ex = ext->pq_ex;
879
880 descs[0] = (struct ioat_raw_descriptor *) pq;
881 descs[1] = (struct ioat_raw_descriptor *) pq_ex;
882
883 for (s = 0; s < src_cnt; s++)
884 pq_set_src(descs, src[s], offset, scf[s], s);
885
886 /* see the comment for dma_maxpq in include/linux/dmaengine.h */
887 if (dmaf_p_disabled_continue(flags))
888 pq_set_src(descs, dst[1], offset, 1, s++);
889 else if (dmaf_continue(flags)) {
890 pq_set_src(descs, dst[0], offset, 0, s++);
891 pq_set_src(descs, dst[1], offset, 1, s++);
892 pq_set_src(descs, dst[1], offset, 0, s++);
893 }
894 pq->size = xfer_size;
895 pq->p_addr = dst[0] + offset;
896 pq->q_addr = dst[1] + offset;
897 pq->ctl = 0;
898 pq->ctl_f.op = op;
Dave Jiang75c6f0a2013-04-10 16:44:39 -0700899 /* we turn on descriptor write back error status */
900 if (device->cap & IOAT_CAP_DWBES)
901 pq->ctl_f.wb_en = result ? 1 : 0;
Dan Williamsd69d235b2009-09-08 17:42:59 -0700902 pq->ctl_f.src_cnt = src_cnt_to_hw(s);
903 pq->ctl_f.p_disable = !!(flags & DMA_PREP_PQ_DISABLE_P);
904 pq->ctl_f.q_disable = !!(flags & DMA_PREP_PQ_DISABLE_Q);
905
906 len -= xfer_size;
907 offset += xfer_size;
Dan Williamscdef57d2009-09-21 09:22:29 -0700908 } while ((i += 1 + with_ext) < num_descs);
Dan Williamsd69d235b2009-09-08 17:42:59 -0700909
910 /* last pq descriptor carries the unmap parameters and fence bit */
911 desc->txd.flags = flags;
912 desc->len = total_len;
913 if (result)
914 desc->result = result;
915 pq->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
916 dump_pq_desc_dbg(ioat, desc, ext);
917
Dave Jiange0884772013-04-10 16:44:20 -0700918 if (!cb32) {
919 pq->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
920 pq->ctl_f.compl_write = 1;
921 compl_desc = desc;
922 } else {
923 /* completion descriptor carries interrupt bit */
924 compl_desc = ioat2_get_ring_ent(ioat, idx + i);
925 compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
926 hw = compl_desc->hw;
927 hw->ctl = 0;
928 hw->ctl_f.null = 1;
929 hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
930 hw->ctl_f.compl_write = 1;
931 hw->size = NULL_DESC_BUFFER_SIZE;
932 dump_desc_dbg(ioat, compl_desc);
933 }
934
Dan Williamsd69d235b2009-09-08 17:42:59 -0700935
936 /* we leave the channel locked to ensure in order submission */
Dan Williams49954c12009-11-19 17:11:03 -0700937 return &compl_desc->txd;
Dan Williamsd69d235b2009-09-08 17:42:59 -0700938}
939
940static struct dma_async_tx_descriptor *
Dave Jiang7727eaa2013-04-15 10:25:56 -0700941__ioat3_prep_pq16_lock(struct dma_chan *c, enum sum_check_flags *result,
942 const dma_addr_t *dst, const dma_addr_t *src,
943 unsigned int src_cnt, const unsigned char *scf,
944 size_t len, unsigned long flags)
945{
946 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
947 struct ioat_chan_common *chan = &ioat->base;
948 struct ioatdma_device *device = chan->device;
949 struct ioat_ring_ent *desc;
950 size_t total_len = len;
951 struct ioat_pq_descriptor *pq;
952 u32 offset = 0;
953 u8 op;
954 int i, s, idx, num_descs;
955
Dave Jiang7727eaa2013-04-15 10:25:56 -0700956 /* this function is only called with 9-16 sources */
957 op = result ? IOAT_OP_PQ_VAL_16S : IOAT_OP_PQ_16S;
958
959 dev_dbg(to_dev(chan), "%s\n", __func__);
960
961 num_descs = ioat2_xferlen_to_descs(ioat, len);
962
963 /*
964 * 16 source pq is only available on cb3.3 and has no completion
965 * write hw bug.
966 */
967 if (num_descs && ioat2_check_space_lock(ioat, num_descs) == 0)
968 idx = ioat->head;
969 else
970 return NULL;
971
972 i = 0;
973
974 do {
975 struct ioat_raw_descriptor *descs[4];
976 size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
977
978 desc = ioat2_get_ring_ent(ioat, idx + i);
979 pq = desc->pq;
980
981 descs[0] = (struct ioat_raw_descriptor *) pq;
982
Dan Williams5d48b9b2013-11-13 10:15:42 -0800983 desc->sed = ioat3_alloc_sed(device, (src_cnt-2) >> 3);
Dave Jiang7727eaa2013-04-15 10:25:56 -0700984 if (!desc->sed) {
985 dev_err(to_dev(chan),
986 "%s: no free sed entries\n", __func__);
987 return NULL;
988 }
989
990 pq->sed_addr = desc->sed->dma;
991 desc->sed->parent = desc;
992
993 descs[1] = (struct ioat_raw_descriptor *)desc->sed->hw;
994 descs[2] = (void *)descs[1] + 64;
995
996 for (s = 0; s < src_cnt; s++)
997 pq16_set_src(descs, src[s], offset, scf[s], s);
998
999 /* see the comment for dma_maxpq in include/linux/dmaengine.h */
1000 if (dmaf_p_disabled_continue(flags))
1001 pq16_set_src(descs, dst[1], offset, 1, s++);
1002 else if (dmaf_continue(flags)) {
1003 pq16_set_src(descs, dst[0], offset, 0, s++);
1004 pq16_set_src(descs, dst[1], offset, 1, s++);
1005 pq16_set_src(descs, dst[1], offset, 0, s++);
1006 }
1007
1008 pq->size = xfer_size;
1009 pq->p_addr = dst[0] + offset;
1010 pq->q_addr = dst[1] + offset;
1011 pq->ctl = 0;
1012 pq->ctl_f.op = op;
1013 pq->ctl_f.src_cnt = src16_cnt_to_hw(s);
Dave Jiang75c6f0a2013-04-10 16:44:39 -07001014 /* we turn on descriptor write back error status */
1015 if (device->cap & IOAT_CAP_DWBES)
1016 pq->ctl_f.wb_en = result ? 1 : 0;
Dave Jiang7727eaa2013-04-15 10:25:56 -07001017 pq->ctl_f.p_disable = !!(flags & DMA_PREP_PQ_DISABLE_P);
1018 pq->ctl_f.q_disable = !!(flags & DMA_PREP_PQ_DISABLE_Q);
1019
1020 len -= xfer_size;
1021 offset += xfer_size;
1022 } while (++i < num_descs);
1023
1024 /* last pq descriptor carries the unmap parameters and fence bit */
1025 desc->txd.flags = flags;
1026 desc->len = total_len;
1027 if (result)
1028 desc->result = result;
1029 pq->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
1030
1031 /* with cb3.3 we should be able to do completion w/o a null desc */
1032 pq->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
1033 pq->ctl_f.compl_write = 1;
1034
1035 dump_pq16_desc_dbg(ioat, desc);
1036
1037 /* we leave the channel locked to ensure in order submission */
1038 return &desc->txd;
1039}
1040
Dan Williams21e96c72013-11-13 10:37:36 -08001041static int src_cnt_flags(unsigned int src_cnt, unsigned long flags)
1042{
1043 if (dmaf_p_disabled_continue(flags))
1044 return src_cnt + 1;
1045 else if (dmaf_continue(flags))
1046 return src_cnt + 3;
1047 else
1048 return src_cnt;
1049}
1050
Dave Jiang7727eaa2013-04-15 10:25:56 -07001051static struct dma_async_tx_descriptor *
Dan Williamsd69d235b2009-09-08 17:42:59 -07001052ioat3_prep_pq(struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
1053 unsigned int src_cnt, const unsigned char *scf, size_t len,
1054 unsigned long flags)
1055{
Dan Williamsde581b62009-11-19 17:08:45 -07001056 /* specify valid address for disabled result */
1057 if (flags & DMA_PREP_PQ_DISABLE_P)
1058 dst[0] = dst[1];
1059 if (flags & DMA_PREP_PQ_DISABLE_Q)
1060 dst[1] = dst[0];
1061
Dan Williamsd69d235b2009-09-08 17:42:59 -07001062 /* handle the single source multiply case from the raid6
1063 * recovery path
1064 */
Dan Williamsde581b62009-11-19 17:08:45 -07001065 if ((flags & DMA_PREP_PQ_DISABLE_P) && src_cnt == 1) {
Dan Williamsd69d235b2009-09-08 17:42:59 -07001066 dma_addr_t single_source[2];
1067 unsigned char single_source_coef[2];
1068
1069 BUG_ON(flags & DMA_PREP_PQ_DISABLE_Q);
1070 single_source[0] = src[0];
1071 single_source[1] = src[0];
1072 single_source_coef[0] = scf[0];
1073 single_source_coef[1] = 0;
1074
Dan Williams21e96c72013-11-13 10:37:36 -08001075 return src_cnt_flags(src_cnt, flags) > 8 ?
Dave Jiang7727eaa2013-04-15 10:25:56 -07001076 __ioat3_prep_pq16_lock(chan, NULL, dst, single_source,
1077 2, single_source_coef, len,
1078 flags) :
1079 __ioat3_prep_pq_lock(chan, NULL, dst, single_source, 2,
1080 single_source_coef, len, flags);
1081
1082 } else {
Dan Williams21e96c72013-11-13 10:37:36 -08001083 return src_cnt_flags(src_cnt, flags) > 8 ?
Dave Jiang7727eaa2013-04-15 10:25:56 -07001084 __ioat3_prep_pq16_lock(chan, NULL, dst, src, src_cnt,
1085 scf, len, flags) :
1086 __ioat3_prep_pq_lock(chan, NULL, dst, src, src_cnt,
1087 scf, len, flags);
1088 }
Dan Williamsd69d235b2009-09-08 17:42:59 -07001089}
1090
1091struct dma_async_tx_descriptor *
1092ioat3_prep_pq_val(struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
1093 unsigned int src_cnt, const unsigned char *scf, size_t len,
1094 enum sum_check_flags *pqres, unsigned long flags)
1095{
Dan Williamsde581b62009-11-19 17:08:45 -07001096 /* specify valid address for disabled result */
1097 if (flags & DMA_PREP_PQ_DISABLE_P)
1098 pq[0] = pq[1];
1099 if (flags & DMA_PREP_PQ_DISABLE_Q)
1100 pq[1] = pq[0];
1101
Dan Williamsd69d235b2009-09-08 17:42:59 -07001102 /* the cleanup routine only sets bits on validate failure, it
1103 * does not clear bits on validate success... so clear it here
1104 */
1105 *pqres = 0;
1106
Dan Williams21e96c72013-11-13 10:37:36 -08001107 return src_cnt_flags(src_cnt, flags) > 8 ?
Dave Jiang7727eaa2013-04-15 10:25:56 -07001108 __ioat3_prep_pq16_lock(chan, pqres, pq, src, src_cnt, scf, len,
1109 flags) :
1110 __ioat3_prep_pq_lock(chan, pqres, pq, src, src_cnt, scf, len,
1111 flags);
Dan Williamsd69d235b2009-09-08 17:42:59 -07001112}
1113
Dan Williamsae786622009-09-08 17:43:00 -07001114static struct dma_async_tx_descriptor *
1115ioat3_prep_pqxor(struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
1116 unsigned int src_cnt, size_t len, unsigned long flags)
1117{
1118 unsigned char scf[src_cnt];
1119 dma_addr_t pq[2];
1120
1121 memset(scf, 0, src_cnt);
Dan Williamsae786622009-09-08 17:43:00 -07001122 pq[0] = dst;
Dan Williamsde581b62009-11-19 17:08:45 -07001123 flags |= DMA_PREP_PQ_DISABLE_Q;
1124 pq[1] = dst; /* specify valid address for disabled result */
Dan Williamsae786622009-09-08 17:43:00 -07001125
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, NULL, pq, src, src_cnt, scf, len,
1128 flags) :
1129 __ioat3_prep_pq_lock(chan, NULL, pq, src, src_cnt, scf, len,
1130 flags);
Dan Williamsae786622009-09-08 17:43:00 -07001131}
1132
1133struct dma_async_tx_descriptor *
1134ioat3_prep_pqxor_val(struct dma_chan *chan, dma_addr_t *src,
1135 unsigned int src_cnt, size_t len,
1136 enum sum_check_flags *result, unsigned long flags)
1137{
1138 unsigned char scf[src_cnt];
1139 dma_addr_t pq[2];
1140
1141 /* the cleanup routine only sets bits on validate failure, it
1142 * does not clear bits on validate success... so clear it here
1143 */
1144 *result = 0;
1145
1146 memset(scf, 0, src_cnt);
Dan Williamsae786622009-09-08 17:43:00 -07001147 pq[0] = src[0];
Dan Williamsde581b62009-11-19 17:08:45 -07001148 flags |= DMA_PREP_PQ_DISABLE_Q;
1149 pq[1] = pq[0]; /* specify valid address for disabled result */
Dan Williamsae786622009-09-08 17:43:00 -07001150
Dan Williams21e96c72013-11-13 10:37:36 -08001151 return src_cnt_flags(src_cnt, flags) > 8 ?
Dave Jiang7727eaa2013-04-15 10:25:56 -07001152 __ioat3_prep_pq16_lock(chan, result, pq, &src[1], src_cnt - 1,
1153 scf, len, flags) :
1154 __ioat3_prep_pq_lock(chan, result, pq, &src[1], src_cnt - 1,
1155 scf, len, flags);
Dan Williamsae786622009-09-08 17:43:00 -07001156}
1157
Dan Williams58c86492009-09-08 17:43:00 -07001158static struct dma_async_tx_descriptor *
1159ioat3_prep_interrupt_lock(struct dma_chan *c, unsigned long flags)
1160{
1161 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
1162 struct ioat_ring_ent *desc;
1163 struct ioat_dma_descriptor *hw;
Dan Williams58c86492009-09-08 17:43:00 -07001164
Dan Williams074cc472010-05-01 15:22:55 -07001165 if (ioat2_check_space_lock(ioat, 1) == 0)
1166 desc = ioat2_get_ring_ent(ioat, ioat->head);
Dan Williams58c86492009-09-08 17:43:00 -07001167 else
1168 return NULL;
1169
1170 hw = desc->hw;
1171 hw->ctl = 0;
1172 hw->ctl_f.null = 1;
1173 hw->ctl_f.int_en = 1;
1174 hw->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
1175 hw->ctl_f.compl_write = 1;
1176 hw->size = NULL_DESC_BUFFER_SIZE;
1177 hw->src_addr = 0;
1178 hw->dst_addr = 0;
1179
1180 desc->txd.flags = flags;
1181 desc->len = 1;
1182
1183 dump_desc_dbg(ioat, desc);
1184
1185 /* we leave the channel locked to ensure in order submission */
1186 return &desc->txd;
1187}
1188
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08001189static void ioat3_dma_test_callback(void *dma_async_param)
Dan Williams9de6fc72009-09-08 17:42:58 -07001190{
1191 struct completion *cmp = dma_async_param;
1192
1193 complete(cmp);
1194}
1195
1196#define IOAT_NUM_SRC_TEST 6 /* must be <= 8 */
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08001197static int ioat_xor_val_self_test(struct ioatdma_device *device)
Dan Williams9de6fc72009-09-08 17:42:58 -07001198{
1199 int i, src_idx;
1200 struct page *dest;
1201 struct page *xor_srcs[IOAT_NUM_SRC_TEST];
1202 struct page *xor_val_srcs[IOAT_NUM_SRC_TEST + 1];
1203 dma_addr_t dma_srcs[IOAT_NUM_SRC_TEST + 1];
Bartlomiej Zolnierkiewicz48a9db42013-07-03 15:05:06 -07001204 dma_addr_t dest_dma;
Dan Williams9de6fc72009-09-08 17:42:58 -07001205 struct dma_async_tx_descriptor *tx;
1206 struct dma_chan *dma_chan;
1207 dma_cookie_t cookie;
1208 u8 cmp_byte = 0;
1209 u32 cmp_word;
1210 u32 xor_val_result;
1211 int err = 0;
1212 struct completion cmp;
1213 unsigned long tmo;
1214 struct device *dev = &device->pdev->dev;
1215 struct dma_device *dma = &device->common;
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001216 u8 op = 0;
Dan Williams9de6fc72009-09-08 17:42:58 -07001217
1218 dev_dbg(dev, "%s\n", __func__);
1219
1220 if (!dma_has_cap(DMA_XOR, dma->cap_mask))
1221 return 0;
1222
1223 for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
1224 xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
1225 if (!xor_srcs[src_idx]) {
1226 while (src_idx--)
1227 __free_page(xor_srcs[src_idx]);
1228 return -ENOMEM;
1229 }
1230 }
1231
1232 dest = alloc_page(GFP_KERNEL);
1233 if (!dest) {
1234 while (src_idx--)
1235 __free_page(xor_srcs[src_idx]);
1236 return -ENOMEM;
1237 }
1238
1239 /* Fill in src buffers */
1240 for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
1241 u8 *ptr = page_address(xor_srcs[src_idx]);
1242 for (i = 0; i < PAGE_SIZE; i++)
1243 ptr[i] = (1 << src_idx);
1244 }
1245
1246 for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++)
1247 cmp_byte ^= (u8) (1 << src_idx);
1248
1249 cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
1250 (cmp_byte << 8) | cmp_byte;
1251
1252 memset(page_address(dest), 0, PAGE_SIZE);
1253
1254 dma_chan = container_of(dma->channels.next, struct dma_chan,
1255 device_node);
1256 if (dma->device_alloc_chan_resources(dma_chan) < 1) {
1257 err = -ENODEV;
1258 goto out;
1259 }
1260
1261 /* test xor */
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001262 op = IOAT_OP_XOR;
1263
Dan Williams9de6fc72009-09-08 17:42:58 -07001264 dest_dma = dma_map_page(dev, dest, 0, PAGE_SIZE, DMA_FROM_DEVICE);
1265 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
1266 dma_srcs[i] = dma_map_page(dev, xor_srcs[i], 0, PAGE_SIZE,
1267 DMA_TO_DEVICE);
1268 tx = dma->device_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
1269 IOAT_NUM_SRC_TEST, PAGE_SIZE,
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +02001270 DMA_PREP_INTERRUPT);
Dan Williams9de6fc72009-09-08 17:42:58 -07001271
1272 if (!tx) {
1273 dev_err(dev, "Self-test xor prep failed\n");
1274 err = -ENODEV;
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001275 goto dma_unmap;
Dan Williams9de6fc72009-09-08 17:42:58 -07001276 }
1277
1278 async_tx_ack(tx);
1279 init_completion(&cmp);
1280 tx->callback = ioat3_dma_test_callback;
1281 tx->callback_param = &cmp;
1282 cookie = tx->tx_submit(tx);
1283 if (cookie < 0) {
1284 dev_err(dev, "Self-test xor setup failed\n");
1285 err = -ENODEV;
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001286 goto dma_unmap;
Dan Williams9de6fc72009-09-08 17:42:58 -07001287 }
1288 dma->device_issue_pending(dma_chan);
1289
1290 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
1291
Vinod Koul2f16f802013-10-16 20:48:52 +05301292 if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_COMPLETE) {
Dan Williams9de6fc72009-09-08 17:42:58 -07001293 dev_err(dev, "Self-test xor timed out\n");
1294 err = -ENODEV;
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001295 goto dma_unmap;
Dan Williams9de6fc72009-09-08 17:42:58 -07001296 }
1297
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001298 dma_unmap_page(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
1299 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
1300 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
1301
Dan Williams9de6fc72009-09-08 17:42:58 -07001302 dma_sync_single_for_cpu(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
1303 for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
1304 u32 *ptr = page_address(dest);
1305 if (ptr[i] != cmp_word) {
1306 dev_err(dev, "Self-test xor failed compare\n");
1307 err = -ENODEV;
1308 goto free_resources;
1309 }
1310 }
Shuah Khanac498982012-10-25 10:22:32 -06001311 dma_sync_single_for_device(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
Dan Williams9de6fc72009-09-08 17:42:58 -07001312
1313 /* skip validate if the capability is not present */
1314 if (!dma_has_cap(DMA_XOR_VAL, dma_chan->device->cap_mask))
1315 goto free_resources;
1316
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001317 op = IOAT_OP_XOR_VAL;
1318
Dan Williams9de6fc72009-09-08 17:42:58 -07001319 /* validate the sources with the destintation page */
1320 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
1321 xor_val_srcs[i] = xor_srcs[i];
1322 xor_val_srcs[i] = dest;
1323
1324 xor_val_result = 1;
1325
1326 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1327 dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
1328 DMA_TO_DEVICE);
1329 tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
1330 IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +02001331 &xor_val_result, DMA_PREP_INTERRUPT);
Dan Williams9de6fc72009-09-08 17:42:58 -07001332 if (!tx) {
1333 dev_err(dev, "Self-test zero prep failed\n");
1334 err = -ENODEV;
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001335 goto dma_unmap;
Dan Williams9de6fc72009-09-08 17:42:58 -07001336 }
1337
1338 async_tx_ack(tx);
1339 init_completion(&cmp);
1340 tx->callback = ioat3_dma_test_callback;
1341 tx->callback_param = &cmp;
1342 cookie = tx->tx_submit(tx);
1343 if (cookie < 0) {
1344 dev_err(dev, "Self-test zero setup failed\n");
1345 err = -ENODEV;
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001346 goto dma_unmap;
Dan Williams9de6fc72009-09-08 17:42:58 -07001347 }
1348 dma->device_issue_pending(dma_chan);
1349
1350 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
1351
Vinod Koul2f16f802013-10-16 20:48:52 +05301352 if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_COMPLETE) {
Dan Williams9de6fc72009-09-08 17:42:58 -07001353 dev_err(dev, "Self-test validate timed out\n");
1354 err = -ENODEV;
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001355 goto dma_unmap;
Dan Williams9de6fc72009-09-08 17:42:58 -07001356 }
1357
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001358 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1359 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
1360
Dan Williams9de6fc72009-09-08 17:42:58 -07001361 if (xor_val_result != 0) {
1362 dev_err(dev, "Self-test validate failed compare\n");
1363 err = -ENODEV;
1364 goto free_resources;
1365 }
1366
Dave Jiangac7d6312013-11-06 08:50:09 -07001367 memset(page_address(dest), 0, PAGE_SIZE);
1368
Dan Williams9de6fc72009-09-08 17:42:58 -07001369 /* test for non-zero parity sum */
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001370 op = IOAT_OP_XOR_VAL;
1371
Dan Williams9de6fc72009-09-08 17:42:58 -07001372 xor_val_result = 0;
1373 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1374 dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
1375 DMA_TO_DEVICE);
1376 tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
1377 IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +02001378 &xor_val_result, DMA_PREP_INTERRUPT);
Dan Williams9de6fc72009-09-08 17:42:58 -07001379 if (!tx) {
1380 dev_err(dev, "Self-test 2nd zero prep failed\n");
1381 err = -ENODEV;
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001382 goto dma_unmap;
Dan Williams9de6fc72009-09-08 17:42:58 -07001383 }
1384
1385 async_tx_ack(tx);
1386 init_completion(&cmp);
1387 tx->callback = ioat3_dma_test_callback;
1388 tx->callback_param = &cmp;
1389 cookie = tx->tx_submit(tx);
1390 if (cookie < 0) {
1391 dev_err(dev, "Self-test 2nd zero setup failed\n");
1392 err = -ENODEV;
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001393 goto dma_unmap;
Dan Williams9de6fc72009-09-08 17:42:58 -07001394 }
1395 dma->device_issue_pending(dma_chan);
1396
1397 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
1398
Vinod Koul2f16f802013-10-16 20:48:52 +05301399 if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_COMPLETE) {
Dan Williams9de6fc72009-09-08 17:42:58 -07001400 dev_err(dev, "Self-test 2nd validate timed out\n");
1401 err = -ENODEV;
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001402 goto dma_unmap;
Dan Williams9de6fc72009-09-08 17:42:58 -07001403 }
1404
1405 if (xor_val_result != SUM_CHECK_P_RESULT) {
1406 dev_err(dev, "Self-test validate failed compare\n");
1407 err = -ENODEV;
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001408 goto dma_unmap;
Dan Williams9de6fc72009-09-08 17:42:58 -07001409 }
1410
Bartlomiej Zolnierkiewicz7369f562012-11-05 10:00:19 +00001411 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1412 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
1413
1414 goto free_resources;
1415dma_unmap:
1416 if (op == IOAT_OP_XOR) {
1417 dma_unmap_page(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
1418 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
1419 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE,
1420 DMA_TO_DEVICE);
1421 } else if (op == IOAT_OP_XOR_VAL) {
1422 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1423 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE,
1424 DMA_TO_DEVICE);
Bartlomiej Zolnierkiewicz48a9db42013-07-03 15:05:06 -07001425 }
Dan Williams9de6fc72009-09-08 17:42:58 -07001426free_resources:
1427 dma->device_free_chan_resources(dma_chan);
1428out:
1429 src_idx = IOAT_NUM_SRC_TEST;
1430 while (src_idx--)
1431 __free_page(xor_srcs[src_idx]);
1432 __free_page(dest);
1433 return err;
1434}
1435
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08001436static int ioat3_dma_self_test(struct ioatdma_device *device)
Dan Williams9de6fc72009-09-08 17:42:58 -07001437{
1438 int rc = ioat_dma_self_test(device);
1439
1440 if (rc)
1441 return rc;
1442
1443 rc = ioat_xor_val_self_test(device);
1444 if (rc)
1445 return rc;
1446
1447 return 0;
1448}
1449
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001450static int ioat3_irq_reinit(struct ioatdma_device *device)
1451{
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001452 struct pci_dev *pdev = device->pdev;
Dan Williams779e5612013-11-13 16:30:43 -08001453 int irq = pdev->irq, i;
1454
1455 if (!is_bwd_ioat(pdev))
1456 return 0;
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001457
1458 switch (device->irq_mode) {
1459 case IOAT_MSIX:
Dan Williams779e5612013-11-13 16:30:43 -08001460 for (i = 0; i < device->common.chancnt; i++) {
1461 struct msix_entry *msix = &device->msix_entries[i];
1462 struct ioat_chan_common *chan;
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001463
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001464 chan = ioat_chan_by_index(device, i);
1465 devm_free_irq(&pdev->dev, msix->vector, chan);
1466 }
1467
1468 pci_disable_msix(pdev);
1469 break;
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001470 case IOAT_MSI:
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001471 pci_disable_msi(pdev);
Dan Williams779e5612013-11-13 16:30:43 -08001472 /* fall through */
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001473 case IOAT_INTX:
Dan Williams779e5612013-11-13 16:30:43 -08001474 devm_free_irq(&pdev->dev, irq, device);
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001475 break;
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001476 default:
1477 return 0;
1478 }
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001479 device->irq_mode = IOAT_NOIRQ;
1480
Dan Williams779e5612013-11-13 16:30:43 -08001481 return ioat_dma_setup_interrupts(device);
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001482}
1483
Dan Williamsa6d52d72009-12-19 15:36:02 -07001484static int ioat3_reset_hw(struct ioat_chan_common *chan)
1485{
1486 /* throw away whatever the channel was doing and get it
1487 * initialized, with ioat3 specific workarounds
1488 */
1489 struct ioatdma_device *device = chan->device;
1490 struct pci_dev *pdev = device->pdev;
1491 u32 chanerr;
1492 u16 dev_id;
1493 int err;
1494
1495 ioat2_quiesce(chan, msecs_to_jiffies(100));
1496
1497 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
1498 writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
1499
Dave Jiang6ead7e42013-03-26 15:42:59 -07001500 if (device->version < IOAT_VER_3_3) {
1501 /* clear any pending errors */
1502 err = pci_read_config_dword(pdev,
1503 IOAT_PCI_CHANERR_INT_OFFSET, &chanerr);
1504 if (err) {
1505 dev_err(&pdev->dev,
1506 "channel error register unreachable\n");
1507 return err;
1508 }
1509 pci_write_config_dword(pdev,
1510 IOAT_PCI_CHANERR_INT_OFFSET, chanerr);
Dan Williamsa6d52d72009-12-19 15:36:02 -07001511
Dave Jiang6ead7e42013-03-26 15:42:59 -07001512 /* Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
1513 * (workaround for spurious config parity error after restart)
1514 */
1515 pci_read_config_word(pdev, IOAT_PCI_DEVICE_ID_OFFSET, &dev_id);
1516 if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0) {
1517 pci_write_config_dword(pdev,
1518 IOAT_PCI_DMAUNCERRSTS_OFFSET,
1519 0x10);
1520 }
1521 }
Dan Williamsa6d52d72009-12-19 15:36:02 -07001522
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001523 err = ioat2_reset_sync(chan, msecs_to_jiffies(200));
Dan Williams779e5612013-11-13 16:30:43 -08001524 if (!err)
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001525 err = ioat3_irq_reinit(device);
Dave Jiang1a363062012-12-03 16:08:37 -07001526
Dan Williams779e5612013-11-13 16:30:43 -08001527 if (err)
1528 dev_err(&pdev->dev, "Failed to reset: %d\n", err);
1529
Dave Jiang8a52b9f2013-03-26 15:42:47 -07001530 return err;
Dave Jiang570727b2013-03-25 14:37:31 -07001531}
1532
Dave Jiang75c6f0a2013-04-10 16:44:39 -07001533static void ioat3_intr_quirk(struct ioatdma_device *device)
1534{
1535 struct dma_device *dma;
1536 struct dma_chan *c;
1537 struct ioat_chan_common *chan;
1538 u32 errmask;
1539
1540 dma = &device->common;
1541
1542 /*
1543 * if we have descriptor write back error status, we mask the
1544 * error interrupts
1545 */
1546 if (device->cap & IOAT_CAP_DWBES) {
1547 list_for_each_entry(c, &dma->channels, device_node) {
1548 chan = to_chan_common(c);
1549 errmask = readl(chan->reg_base +
1550 IOAT_CHANERR_MASK_OFFSET);
1551 errmask |= IOAT_CHANERR_XOR_P_OR_CRC_ERR |
1552 IOAT_CHANERR_XOR_Q_ERR;
1553 writel(errmask, chan->reg_base +
1554 IOAT_CHANERR_MASK_OFFSET);
1555 }
1556 }
1557}
1558
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08001559int ioat3_dma_probe(struct ioatdma_device *device, int dca)
Dan Williamsbf40a682009-09-08 17:42:55 -07001560{
1561 struct pci_dev *pdev = device->pdev;
Dan Williams228c4f52009-11-19 17:07:10 -07001562 int dca_en = system_has_dca_enabled(pdev);
Dan Williamsbf40a682009-09-08 17:42:55 -07001563 struct dma_device *dma;
1564 struct dma_chan *c;
1565 struct ioat_chan_common *chan;
Dan Williamse3232712009-09-08 17:43:02 -07001566 bool is_raid_device = false;
Dan Williamsbf40a682009-09-08 17:42:55 -07001567 int err;
Dan Williamsbf40a682009-09-08 17:42:55 -07001568
1569 device->enumerate_channels = ioat2_enumerate_channels;
Dan Williamsa6d52d72009-12-19 15:36:02 -07001570 device->reset_hw = ioat3_reset_hw;
Dan Williams9de6fc72009-09-08 17:42:58 -07001571 device->self_test = ioat3_dma_self_test;
Dave Jiang75c6f0a2013-04-10 16:44:39 -07001572 device->intr_quirk = ioat3_intr_quirk;
Dan Williamsbf40a682009-09-08 17:42:55 -07001573 dma = &device->common;
1574 dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy_lock;
1575 dma->device_issue_pending = ioat2_issue_pending;
1576 dma->device_alloc_chan_resources = ioat2_alloc_chan_resources;
1577 dma->device_free_chan_resources = ioat2_free_chan_resources;
Dan Williams58c86492009-09-08 17:43:00 -07001578
1579 dma_cap_set(DMA_INTERRUPT, dma->cap_mask);
1580 dma->device_prep_dma_interrupt = ioat3_prep_interrupt_lock;
1581
Dave Jiang75c6f0a2013-04-10 16:44:39 -07001582 device->cap = readl(device->reg_base + IOAT_DMA_CAP_OFFSET);
Dan Williams228c4f52009-11-19 17:07:10 -07001583
Brice Goglinc4dcf0e2013-08-02 21:18:03 +02001584 if (is_xeon_cb32(pdev) || is_bwd_noraid(pdev))
Dave Jiang75c6f0a2013-04-10 16:44:39 -07001585 device->cap &= ~(IOAT_CAP_XOR | IOAT_CAP_PQ | IOAT_CAP_RAID16SS);
Dave Jiangd3023982013-04-10 16:44:32 -07001586
Dan Williams228c4f52009-11-19 17:07:10 -07001587 /* dca is incompatible with raid operations */
Dave Jiang75c6f0a2013-04-10 16:44:39 -07001588 if (dca_en && (device->cap & (IOAT_CAP_XOR|IOAT_CAP_PQ)))
1589 device->cap &= ~(IOAT_CAP_XOR|IOAT_CAP_PQ);
Dan Williams228c4f52009-11-19 17:07:10 -07001590
Dave Jiang75c6f0a2013-04-10 16:44:39 -07001591 if (device->cap & IOAT_CAP_XOR) {
Dan Williamse3232712009-09-08 17:43:02 -07001592 is_raid_device = true;
Dan Williamsb094ad32009-09-08 17:42:57 -07001593 dma->max_xor = 8;
Dan Williamsb094ad32009-09-08 17:42:57 -07001594
1595 dma_cap_set(DMA_XOR, dma->cap_mask);
1596 dma->device_prep_dma_xor = ioat3_prep_xor;
1597
1598 dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
1599 dma->device_prep_dma_xor_val = ioat3_prep_xor_val;
1600 }
Dave Jiangeceec442013-03-26 15:43:15 -07001601
Dave Jiang75c6f0a2013-04-10 16:44:39 -07001602 if (device->cap & IOAT_CAP_PQ) {
Dan Williamse3232712009-09-08 17:43:02 -07001603 is_raid_device = true;
Dave Jiang7727eaa2013-04-15 10:25:56 -07001604
Dave Jiang75c6f0a2013-04-10 16:44:39 -07001605 dma->device_prep_dma_pq = ioat3_prep_pq;
1606 dma->device_prep_dma_pq_val = ioat3_prep_pq_val;
1607 dma_cap_set(DMA_PQ, dma->cap_mask);
1608 dma_cap_set(DMA_PQ_VAL, dma->cap_mask);
1609
1610 if (device->cap & IOAT_CAP_RAID16SS) {
Dave Jiang7727eaa2013-04-15 10:25:56 -07001611 dma_set_maxpq(dma, 16, 0);
Dave Jiang7727eaa2013-04-15 10:25:56 -07001612 } else {
1613 dma_set_maxpq(dma, 8, 0);
Dave Jiang7727eaa2013-04-15 10:25:56 -07001614 }
Dan Williamsd69d235b2009-09-08 17:42:59 -07001615
Dave Jiang75c6f0a2013-04-10 16:44:39 -07001616 if (!(device->cap & IOAT_CAP_XOR)) {
1617 dma->device_prep_dma_xor = ioat3_prep_pqxor;
1618 dma->device_prep_dma_xor_val = ioat3_prep_pqxor_val;
1619 dma_cap_set(DMA_XOR, dma->cap_mask);
1620 dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
Dan Williamsd69d235b2009-09-08 17:42:59 -07001621
Dave Jiang75c6f0a2013-04-10 16:44:39 -07001622 if (device->cap & IOAT_CAP_RAID16SS) {
Dave Jiang7727eaa2013-04-15 10:25:56 -07001623 dma->max_xor = 16;
Dave Jiang7727eaa2013-04-15 10:25:56 -07001624 } else {
1625 dma->max_xor = 8;
Dave Jiang7727eaa2013-04-15 10:25:56 -07001626 }
Dan Williamsae786622009-09-08 17:43:00 -07001627 }
Dan Williamsd69d235b2009-09-08 17:42:59 -07001628 }
Dave Jiangeceec442013-03-26 15:43:15 -07001629
Dave Jiang9a37f642013-02-26 09:20:36 -07001630 dma->device_tx_status = ioat3_tx_status;
1631 device->cleanup_fn = ioat3_cleanup_event;
1632 device->timer_fn = ioat3_timer_event;
Dan Williamsbf40a682009-09-08 17:42:55 -07001633
Dave Jiang7727eaa2013-04-15 10:25:56 -07001634 /* starting with CB3.3 super extended descriptors are supported */
Dave Jiang75c6f0a2013-04-10 16:44:39 -07001635 if (device->cap & IOAT_CAP_RAID16SS) {
Dave Jiang7727eaa2013-04-15 10:25:56 -07001636 char pool_name[14];
1637 int i;
1638
Dave Jiang7727eaa2013-04-15 10:25:56 -07001639 for (i = 0; i < MAX_SED_POOLS; i++) {
1640 snprintf(pool_name, 14, "ioat_hw%d_sed", i);
1641
1642 /* allocate SED DMA pool */
Dan Williams59056e82013-11-13 10:57:18 -08001643 device->sed_hw_pool[i] = dmam_pool_create(pool_name,
Dave Jiang7727eaa2013-04-15 10:25:56 -07001644 &pdev->dev,
1645 SED_SIZE * (i + 1), 64, 0);
1646 if (!device->sed_hw_pool[i])
Dan Williams59056e82013-11-13 10:57:18 -08001647 return -ENOMEM;
Dave Jiang7727eaa2013-04-15 10:25:56 -07001648
1649 }
1650 }
1651
Dan Williamsbf40a682009-09-08 17:42:55 -07001652 err = ioat_probe(device);
1653 if (err)
1654 return err;
1655 ioat_set_tcp_copy_break(262144);
1656
1657 list_for_each_entry(c, &dma->channels, device_node) {
1658 chan = to_chan_common(c);
1659 writel(IOAT_DMA_DCA_ANY_CPU,
1660 chan->reg_base + IOAT_DCACTRL_OFFSET);
1661 }
1662
1663 err = ioat_register(device);
1664 if (err)
1665 return err;
Dan Williams5669e312009-09-08 17:42:56 -07001666
1667 ioat_kobject_add(device, &ioat2_ktype);
1668
Dan Williamsbf40a682009-09-08 17:42:55 -07001669 if (dca)
1670 device->dca = ioat3_dca_init(pdev, device->reg_base);
1671
1672 return 0;
Dan Williamsbf40a682009-09-08 17:42:55 -07001673}