2 * Intel I/OAT DMA Linux driver
3 * Copyright(c) 2004 - 2009 Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
24 * This driver supports an Intel I/OAT DMA engine (versions >= 2), which
25 * does asynchronous data movement and checksumming operations.
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/pci.h>
32 #include <linux/interrupt.h>
33 #include <linux/dmaengine.h>
34 #include <linux/delay.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/workqueue.h>
37 #include <linux/i7300_idle.h>
40 #include "registers.h"
43 int ioat_ring_alloc_order
= 8;
44 module_param(ioat_ring_alloc_order
, int, 0644);
45 MODULE_PARM_DESC(ioat_ring_alloc_order
,
46 "ioat2+: allocate 2^n descriptors per channel"
47 " (default: 8 max: 16)");
48 static int ioat_ring_max_alloc_order
= IOAT_MAX_ORDER
;
49 module_param(ioat_ring_max_alloc_order
, int, 0644);
50 MODULE_PARM_DESC(ioat_ring_max_alloc_order
,
51 "ioat2+: upper limit for ring size (default: 16)");
53 void __ioat2_issue_pending(struct ioat2_dma_chan
*ioat
)
55 struct ioat_chan_common
*chan
= &ioat
->base
;
57 ioat
->dmacount
+= ioat2_ring_pending(ioat
);
58 ioat
->issued
= ioat
->head
;
59 /* make descriptor updates globally visible before notifying channel */
61 writew(ioat
->dmacount
, chan
->reg_base
+ IOAT_CHAN_DMACOUNT_OFFSET
);
63 "%s: head: %#x tail: %#x issued: %#x count: %#x\n",
64 __func__
, ioat
->head
, ioat
->tail
, ioat
->issued
, ioat
->dmacount
);
67 void ioat2_issue_pending(struct dma_chan
*c
)
69 struct ioat2_dma_chan
*ioat
= to_ioat2_chan(c
);
71 if (ioat2_ring_pending(ioat
)) {
72 spin_lock_bh(&ioat
->ring_lock
);
73 __ioat2_issue_pending(ioat
);
74 spin_unlock_bh(&ioat
->ring_lock
);
79 * ioat2_update_pending - log pending descriptors
80 * @ioat: ioat2+ channel
82 * Check if the number of unsubmitted descriptors has exceeded the
83 * watermark. Called with ring_lock held
85 static void ioat2_update_pending(struct ioat2_dma_chan
*ioat
)
87 if (ioat2_ring_pending(ioat
) > ioat_pending_level
)
88 __ioat2_issue_pending(ioat
);
91 static void __ioat2_start_null_desc(struct ioat2_dma_chan
*ioat
)
93 struct ioat_ring_ent
*desc
;
94 struct ioat_dma_descriptor
*hw
;
97 if (ioat2_ring_space(ioat
) < 1) {
98 dev_err(to_dev(&ioat
->base
),
99 "Unable to start null desc - ring full\n");
103 dev_dbg(to_dev(&ioat
->base
), "%s: head: %#x tail: %#x issued: %#x\n",
104 __func__
, ioat
->head
, ioat
->tail
, ioat
->issued
);
105 idx
= ioat2_desc_alloc(ioat
, 1);
106 desc
= ioat2_get_ring_ent(ioat
, idx
);
111 hw
->ctl_f
.int_en
= 1;
112 hw
->ctl_f
.compl_write
= 1;
113 /* set size to non-zero value (channel returns error when size is 0) */
114 hw
->size
= NULL_DESC_BUFFER_SIZE
;
117 async_tx_ack(&desc
->txd
);
118 ioat2_set_chainaddr(ioat
, desc
->txd
.phys
);
119 dump_desc_dbg(ioat
, desc
);
120 __ioat2_issue_pending(ioat
);
123 static void ioat2_start_null_desc(struct ioat2_dma_chan
*ioat
)
125 spin_lock_bh(&ioat
->ring_lock
);
126 __ioat2_start_null_desc(ioat
);
127 spin_unlock_bh(&ioat
->ring_lock
);
130 static void __cleanup(struct ioat2_dma_chan
*ioat
, unsigned long phys_complete
)
132 struct ioat_chan_common
*chan
= &ioat
->base
;
133 struct dma_async_tx_descriptor
*tx
;
134 struct ioat_ring_ent
*desc
;
135 bool seen_current
= false;
139 dev_dbg(to_dev(chan
), "%s: head: %#x tail: %#x issued: %#x\n",
140 __func__
, ioat
->head
, ioat
->tail
, ioat
->issued
);
142 active
= ioat2_ring_active(ioat
);
143 for (i
= 0; i
< active
&& !seen_current
; i
++) {
144 prefetch(ioat2_get_ring_ent(ioat
, ioat
->tail
+ i
+ 1));
145 desc
= ioat2_get_ring_ent(ioat
, ioat
->tail
+ i
);
147 dump_desc_dbg(ioat
, desc
);
149 ioat_dma_unmap(chan
, tx
->flags
, desc
->len
, desc
->hw
);
150 chan
->completed_cookie
= tx
->cookie
;
153 tx
->callback(tx
->callback_param
);
158 if (tx
->phys
== phys_complete
)
162 BUG_ON(active
&& !seen_current
); /* no active descs have written a completion? */
164 chan
->last_completion
= phys_complete
;
165 if (ioat
->head
== ioat
->tail
) {
166 dev_dbg(to_dev(chan
), "%s: cancel completion timeout\n",
168 clear_bit(IOAT_COMPLETION_PENDING
, &chan
->state
);
169 mod_timer(&chan
->timer
, jiffies
+ IDLE_TIMEOUT
);
174 * ioat2_cleanup - clean finished descriptors (advance tail pointer)
175 * @chan: ioat channel to be cleaned up
177 static void ioat2_cleanup(struct ioat2_dma_chan
*ioat
)
179 struct ioat_chan_common
*chan
= &ioat
->base
;
180 unsigned long phys_complete
;
182 prefetch(chan
->completion
);
184 if (!spin_trylock_bh(&chan
->cleanup_lock
))
187 if (!ioat_cleanup_preamble(chan
, &phys_complete
)) {
188 spin_unlock_bh(&chan
->cleanup_lock
);
192 if (!spin_trylock_bh(&ioat
->ring_lock
)) {
193 spin_unlock_bh(&chan
->cleanup_lock
);
197 __cleanup(ioat
, phys_complete
);
199 spin_unlock_bh(&ioat
->ring_lock
);
200 spin_unlock_bh(&chan
->cleanup_lock
);
203 void ioat2_cleanup_event(unsigned long data
)
205 struct ioat2_dma_chan
*ioat
= to_ioat2_chan((void *) data
);
208 writew(IOAT_CHANCTRL_RUN
, ioat
->base
.reg_base
+ IOAT_CHANCTRL_OFFSET
);
211 void __ioat2_restart_chan(struct ioat2_dma_chan
*ioat
)
213 struct ioat_chan_common
*chan
= &ioat
->base
;
215 /* set the tail to be re-issued */
216 ioat
->issued
= ioat
->tail
;
218 set_bit(IOAT_COMPLETION_PENDING
, &chan
->state
);
219 mod_timer(&chan
->timer
, jiffies
+ COMPLETION_TIMEOUT
);
221 dev_dbg(to_dev(chan
),
222 "%s: head: %#x tail: %#x issued: %#x count: %#x\n",
223 __func__
, ioat
->head
, ioat
->tail
, ioat
->issued
, ioat
->dmacount
);
225 if (ioat2_ring_pending(ioat
)) {
226 struct ioat_ring_ent
*desc
;
228 desc
= ioat2_get_ring_ent(ioat
, ioat
->tail
);
229 ioat2_set_chainaddr(ioat
, desc
->txd
.phys
);
230 __ioat2_issue_pending(ioat
);
232 __ioat2_start_null_desc(ioat
);
235 int ioat2_quiesce(struct ioat_chan_common
*chan
, unsigned long tmo
)
237 unsigned long end
= jiffies
+ tmo
;
241 status
= ioat_chansts(chan
);
242 if (is_ioat_active(status
) || is_ioat_idle(status
))
244 while (is_ioat_active(status
) || is_ioat_idle(status
)) {
245 if (tmo
&& time_after(jiffies
, end
)) {
249 status
= ioat_chansts(chan
);
256 int ioat2_reset_sync(struct ioat_chan_common
*chan
, unsigned long tmo
)
258 unsigned long end
= jiffies
+ tmo
;
262 while (ioat_reset_pending(chan
)) {
263 if (end
&& time_after(jiffies
, end
)) {
273 static void ioat2_restart_channel(struct ioat2_dma_chan
*ioat
)
275 struct ioat_chan_common
*chan
= &ioat
->base
;
276 unsigned long phys_complete
;
278 ioat2_quiesce(chan
, 0);
279 if (ioat_cleanup_preamble(chan
, &phys_complete
))
280 __cleanup(ioat
, phys_complete
);
282 __ioat2_restart_chan(ioat
);
285 void ioat2_timer_event(unsigned long data
)
287 struct ioat2_dma_chan
*ioat
= to_ioat2_chan((void *) data
);
288 struct ioat_chan_common
*chan
= &ioat
->base
;
290 spin_lock_bh(&chan
->cleanup_lock
);
291 if (test_bit(IOAT_COMPLETION_PENDING
, &chan
->state
)) {
292 unsigned long phys_complete
;
295 spin_lock_bh(&ioat
->ring_lock
);
296 status
= ioat_chansts(chan
);
298 /* when halted due to errors check for channel
299 * programming errors before advancing the completion state
301 if (is_ioat_halted(status
)) {
304 chanerr
= readl(chan
->reg_base
+ IOAT_CHANERR_OFFSET
);
305 dev_err(to_dev(chan
), "%s: Channel halted (%x)\n",
307 BUG_ON(is_ioat_bug(chanerr
));
310 /* if we haven't made progress and we have already
311 * acknowledged a pending completion once, then be more
312 * forceful with a restart
314 if (ioat_cleanup_preamble(chan
, &phys_complete
))
315 __cleanup(ioat
, phys_complete
);
316 else if (test_bit(IOAT_COMPLETION_ACK
, &chan
->state
))
317 ioat2_restart_channel(ioat
);
319 set_bit(IOAT_COMPLETION_ACK
, &chan
->state
);
320 mod_timer(&chan
->timer
, jiffies
+ COMPLETION_TIMEOUT
);
322 spin_unlock_bh(&ioat
->ring_lock
);
326 /* if the ring is idle, empty, and oversized try to step
329 spin_lock_bh(&ioat
->ring_lock
);
330 active
= ioat2_ring_active(ioat
);
331 if (active
== 0 && ioat
->alloc_order
> ioat_get_alloc_order())
332 reshape_ring(ioat
, ioat
->alloc_order
-1);
333 spin_unlock_bh(&ioat
->ring_lock
);
335 /* keep shrinking until we get back to our minimum
338 if (ioat
->alloc_order
> ioat_get_alloc_order())
339 mod_timer(&chan
->timer
, jiffies
+ IDLE_TIMEOUT
);
341 spin_unlock_bh(&chan
->cleanup_lock
);
344 static int ioat2_reset_hw(struct ioat_chan_common
*chan
)
346 /* throw away whatever the channel was doing and get it initialized */
349 ioat2_quiesce(chan
, msecs_to_jiffies(100));
351 chanerr
= readl(chan
->reg_base
+ IOAT_CHANERR_OFFSET
);
352 writel(chanerr
, chan
->reg_base
+ IOAT_CHANERR_OFFSET
);
354 return ioat2_reset_sync(chan
, msecs_to_jiffies(200));
358 * ioat2_enumerate_channels - find and initialize the device's channels
359 * @device: the device to be enumerated
361 int ioat2_enumerate_channels(struct ioatdma_device
*device
)
363 struct ioat2_dma_chan
*ioat
;
364 struct device
*dev
= &device
->pdev
->dev
;
365 struct dma_device
*dma
= &device
->common
;
369 INIT_LIST_HEAD(&dma
->channels
);
370 dma
->chancnt
= readb(device
->reg_base
+ IOAT_CHANCNT_OFFSET
);
371 dma
->chancnt
&= 0x1f; /* bits [4:0] valid */
372 if (dma
->chancnt
> ARRAY_SIZE(device
->idx
)) {
373 dev_warn(dev
, "(%d) exceeds max supported channels (%zu)\n",
374 dma
->chancnt
, ARRAY_SIZE(device
->idx
));
375 dma
->chancnt
= ARRAY_SIZE(device
->idx
);
377 xfercap_log
= readb(device
->reg_base
+ IOAT_XFERCAP_OFFSET
);
378 xfercap_log
&= 0x1f; /* bits [4:0] valid */
379 if (xfercap_log
== 0)
381 dev_dbg(dev
, "%s: xfercap = %d\n", __func__
, 1 << xfercap_log
);
383 /* FIXME which i/oat version is i7300? */
384 #ifdef CONFIG_I7300_IDLE_IOAT_CHANNEL
385 if (i7300_idle_platform_probe(NULL
, NULL
, 1) == 0)
388 for (i
= 0; i
< dma
->chancnt
; i
++) {
389 ioat
= devm_kzalloc(dev
, sizeof(*ioat
), GFP_KERNEL
);
393 ioat_init_channel(device
, &ioat
->base
, i
);
394 ioat
->xfercap_log
= xfercap_log
;
395 spin_lock_init(&ioat
->ring_lock
);
396 if (device
->reset_hw(&ioat
->base
)) {
405 static dma_cookie_t
ioat2_tx_submit_unlock(struct dma_async_tx_descriptor
*tx
)
407 struct dma_chan
*c
= tx
->chan
;
408 struct ioat2_dma_chan
*ioat
= to_ioat2_chan(c
);
409 struct ioat_chan_common
*chan
= &ioat
->base
;
410 dma_cookie_t cookie
= c
->cookie
;
417 dev_dbg(to_dev(&ioat
->base
), "%s: cookie: %d\n", __func__
, cookie
);
419 if (!test_and_set_bit(IOAT_COMPLETION_PENDING
, &chan
->state
))
420 mod_timer(&chan
->timer
, jiffies
+ COMPLETION_TIMEOUT
);
421 ioat2_update_pending(ioat
);
422 spin_unlock_bh(&ioat
->ring_lock
);
427 static struct ioat_ring_ent
*ioat2_alloc_ring_ent(struct dma_chan
*chan
, gfp_t flags
)
429 struct ioat_dma_descriptor
*hw
;
430 struct ioat_ring_ent
*desc
;
431 struct ioatdma_device
*dma
;
434 dma
= to_ioatdma_device(chan
->device
);
435 hw
= pci_pool_alloc(dma
->dma_pool
, flags
, &phys
);
438 memset(hw
, 0, sizeof(*hw
));
440 desc
= kmem_cache_alloc(ioat2_cache
, flags
);
442 pci_pool_free(dma
->dma_pool
, hw
, phys
);
445 memset(desc
, 0, sizeof(*desc
));
447 dma_async_tx_descriptor_init(&desc
->txd
, chan
);
448 desc
->txd
.tx_submit
= ioat2_tx_submit_unlock
;
450 desc
->txd
.phys
= phys
;
454 static void ioat2_free_ring_ent(struct ioat_ring_ent
*desc
, struct dma_chan
*chan
)
456 struct ioatdma_device
*dma
;
458 dma
= to_ioatdma_device(chan
->device
);
459 pci_pool_free(dma
->dma_pool
, desc
->hw
, desc
->txd
.phys
);
460 kmem_cache_free(ioat2_cache
, desc
);
463 static struct ioat_ring_ent
**ioat2_alloc_ring(struct dma_chan
*c
, int order
, gfp_t flags
)
465 struct ioat_ring_ent
**ring
;
466 int descs
= 1 << order
;
469 if (order
> ioat_get_max_alloc_order())
472 /* allocate the array to hold the software ring */
473 ring
= kcalloc(descs
, sizeof(*ring
), flags
);
476 for (i
= 0; i
< descs
; i
++) {
477 ring
[i
] = ioat2_alloc_ring_ent(c
, flags
);
480 ioat2_free_ring_ent(ring
[i
], c
);
484 set_desc_id(ring
[i
], i
);
488 for (i
= 0; i
< descs
-1; i
++) {
489 struct ioat_ring_ent
*next
= ring
[i
+1];
490 struct ioat_dma_descriptor
*hw
= ring
[i
]->hw
;
492 hw
->next
= next
->txd
.phys
;
494 ring
[i
]->hw
->next
= ring
[0]->txd
.phys
;
499 /* ioat2_alloc_chan_resources - allocate/initialize ioat2 descriptor ring
500 * @chan: channel to be initialized
502 int ioat2_alloc_chan_resources(struct dma_chan
*c
)
504 struct ioat2_dma_chan
*ioat
= to_ioat2_chan(c
);
505 struct ioat_chan_common
*chan
= &ioat
->base
;
506 struct ioat_ring_ent
**ring
;
509 /* have we already been set up? */
511 return 1 << ioat
->alloc_order
;
513 /* Setup register to interrupt and write completion status on error */
514 writew(IOAT_CHANCTRL_RUN
, chan
->reg_base
+ IOAT_CHANCTRL_OFFSET
);
516 /* allocate a completion writeback area */
517 /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
518 chan
->completion
= pci_pool_alloc(chan
->device
->completion_pool
,
519 GFP_KERNEL
, &chan
->completion_dma
);
520 if (!chan
->completion
)
523 memset(chan
->completion
, 0, sizeof(*chan
->completion
));
524 writel(((u64
) chan
->completion_dma
) & 0x00000000FFFFFFFF,
525 chan
->reg_base
+ IOAT_CHANCMP_OFFSET_LOW
);
526 writel(((u64
) chan
->completion_dma
) >> 32,
527 chan
->reg_base
+ IOAT_CHANCMP_OFFSET_HIGH
);
529 order
= ioat_get_alloc_order();
530 ring
= ioat2_alloc_ring(c
, order
, GFP_KERNEL
);
534 spin_lock_bh(&ioat
->ring_lock
);
539 ioat
->alloc_order
= order
;
540 spin_unlock_bh(&ioat
->ring_lock
);
542 tasklet_enable(&chan
->cleanup_task
);
543 ioat2_start_null_desc(ioat
);
545 return 1 << ioat
->alloc_order
;
548 bool reshape_ring(struct ioat2_dma_chan
*ioat
, int order
)
550 /* reshape differs from normal ring allocation in that we want
551 * to allocate a new software ring while only
552 * extending/truncating the hardware ring
554 struct ioat_chan_common
*chan
= &ioat
->base
;
555 struct dma_chan
*c
= &chan
->common
;
556 const u16 curr_size
= ioat2_ring_mask(ioat
) + 1;
557 const u16 active
= ioat2_ring_active(ioat
);
558 const u16 new_size
= 1 << order
;
559 struct ioat_ring_ent
**ring
;
562 if (order
> ioat_get_max_alloc_order())
565 /* double check that we have at least 1 free descriptor */
566 if (active
== curr_size
)
569 /* when shrinking, verify that we can hold the current active
570 * set in the new ring
572 if (active
>= new_size
)
575 /* allocate the array to hold the software ring */
576 ring
= kcalloc(new_size
, sizeof(*ring
), GFP_NOWAIT
);
580 /* allocate/trim descriptors as needed */
581 if (new_size
> curr_size
) {
582 /* copy current descriptors to the new ring */
583 for (i
= 0; i
< curr_size
; i
++) {
584 u16 curr_idx
= (ioat
->tail
+i
) & (curr_size
-1);
585 u16 new_idx
= (ioat
->tail
+i
) & (new_size
-1);
587 ring
[new_idx
] = ioat
->ring
[curr_idx
];
588 set_desc_id(ring
[new_idx
], new_idx
);
591 /* add new descriptors to the ring */
592 for (i
= curr_size
; i
< new_size
; i
++) {
593 u16 new_idx
= (ioat
->tail
+i
) & (new_size
-1);
595 ring
[new_idx
] = ioat2_alloc_ring_ent(c
, GFP_NOWAIT
);
596 if (!ring
[new_idx
]) {
598 u16 new_idx
= (ioat
->tail
+i
) & (new_size
-1);
600 ioat2_free_ring_ent(ring
[new_idx
], c
);
605 set_desc_id(ring
[new_idx
], new_idx
);
608 /* hw link new descriptors */
609 for (i
= curr_size
-1; i
< new_size
; i
++) {
610 u16 new_idx
= (ioat
->tail
+i
) & (new_size
-1);
611 struct ioat_ring_ent
*next
= ring
[(new_idx
+1) & (new_size
-1)];
612 struct ioat_dma_descriptor
*hw
= ring
[new_idx
]->hw
;
614 hw
->next
= next
->txd
.phys
;
617 struct ioat_dma_descriptor
*hw
;
618 struct ioat_ring_ent
*next
;
620 /* copy current descriptors to the new ring, dropping the
621 * removed descriptors
623 for (i
= 0; i
< new_size
; i
++) {
624 u16 curr_idx
= (ioat
->tail
+i
) & (curr_size
-1);
625 u16 new_idx
= (ioat
->tail
+i
) & (new_size
-1);
627 ring
[new_idx
] = ioat
->ring
[curr_idx
];
628 set_desc_id(ring
[new_idx
], new_idx
);
631 /* free deleted descriptors */
632 for (i
= new_size
; i
< curr_size
; i
++) {
633 struct ioat_ring_ent
*ent
;
635 ent
= ioat2_get_ring_ent(ioat
, ioat
->tail
+i
);
636 ioat2_free_ring_ent(ent
, c
);
639 /* fix up hardware ring */
640 hw
= ring
[(ioat
->tail
+new_size
-1) & (new_size
-1)]->hw
;
641 next
= ring
[(ioat
->tail
+new_size
) & (new_size
-1)];
642 hw
->next
= next
->txd
.phys
;
645 dev_dbg(to_dev(chan
), "%s: allocated %d descriptors\n",
650 ioat
->alloc_order
= order
;
656 * ioat2_alloc_and_lock - common descriptor alloc boilerplate for ioat2,3 ops
657 * @idx: gets starting descriptor index on successful allocation
658 * @ioat: ioat2,3 channel (ring) to operate on
659 * @num_descs: allocation length
661 int ioat2_alloc_and_lock(u16
*idx
, struct ioat2_dma_chan
*ioat
, int num_descs
)
663 struct ioat_chan_common
*chan
= &ioat
->base
;
665 spin_lock_bh(&ioat
->ring_lock
);
666 /* never allow the last descriptor to be consumed, we need at
667 * least one free at all times to allow for on-the-fly ring
670 while (unlikely(ioat2_ring_space(ioat
) <= num_descs
)) {
671 if (reshape_ring(ioat
, ioat
->alloc_order
+ 1) &&
672 ioat2_ring_space(ioat
) > num_descs
)
675 if (printk_ratelimit())
676 dev_dbg(to_dev(chan
),
677 "%s: ring full! num_descs: %d (%x:%x:%x)\n",
678 __func__
, num_descs
, ioat
->head
, ioat
->tail
,
680 spin_unlock_bh(&ioat
->ring_lock
);
682 /* progress reclaim in the allocation failure case we
683 * may be called under bh_disabled so we need to trigger
684 * the timer event directly
686 spin_lock_bh(&chan
->cleanup_lock
);
687 if (jiffies
> chan
->timer
.expires
&&
688 timer_pending(&chan
->timer
)) {
689 struct ioatdma_device
*device
= chan
->device
;
691 mod_timer(&chan
->timer
, jiffies
+ COMPLETION_TIMEOUT
);
692 spin_unlock_bh(&chan
->cleanup_lock
);
693 device
->timer_fn((unsigned long) &chan
->common
);
695 spin_unlock_bh(&chan
->cleanup_lock
);
699 dev_dbg(to_dev(chan
), "%s: num_descs: %d (%x:%x:%x)\n",
700 __func__
, num_descs
, ioat
->head
, ioat
->tail
, ioat
->issued
);
702 *idx
= ioat2_desc_alloc(ioat
, num_descs
);
703 return 0; /* with ioat->ring_lock held */
706 struct dma_async_tx_descriptor
*
707 ioat2_dma_prep_memcpy_lock(struct dma_chan
*c
, dma_addr_t dma_dest
,
708 dma_addr_t dma_src
, size_t len
, unsigned long flags
)
710 struct ioat2_dma_chan
*ioat
= to_ioat2_chan(c
);
711 struct ioat_dma_descriptor
*hw
;
712 struct ioat_ring_ent
*desc
;
713 dma_addr_t dst
= dma_dest
;
714 dma_addr_t src
= dma_src
;
715 size_t total_len
= len
;
720 num_descs
= ioat2_xferlen_to_descs(ioat
, len
);
721 if (likely(num_descs
) &&
722 ioat2_alloc_and_lock(&idx
, ioat
, num_descs
) == 0)
728 size_t copy
= min_t(size_t, len
, 1 << ioat
->xfercap_log
);
730 desc
= ioat2_get_ring_ent(ioat
, idx
+ i
);
741 dump_desc_dbg(ioat
, desc
);
742 } while (++i
< num_descs
);
744 desc
->txd
.flags
= flags
;
745 desc
->len
= total_len
;
746 hw
->ctl_f
.int_en
= !!(flags
& DMA_PREP_INTERRUPT
);
747 hw
->ctl_f
.fence
= !!(flags
& DMA_PREP_FENCE
);
748 hw
->ctl_f
.compl_write
= 1;
749 dump_desc_dbg(ioat
, desc
);
750 /* we leave the channel locked to ensure in order submission */
756 * ioat2_free_chan_resources - release all the descriptors
757 * @chan: the channel to be cleaned
759 void ioat2_free_chan_resources(struct dma_chan
*c
)
761 struct ioat2_dma_chan
*ioat
= to_ioat2_chan(c
);
762 struct ioat_chan_common
*chan
= &ioat
->base
;
763 struct ioatdma_device
*device
= chan
->device
;
764 struct ioat_ring_ent
*desc
;
765 const u16 total_descs
= 1 << ioat
->alloc_order
;
769 /* Before freeing channel resources first check
770 * if they have been previously allocated for this channel.
775 tasklet_disable(&chan
->cleanup_task
);
776 del_timer_sync(&chan
->timer
);
777 device
->cleanup_fn((unsigned long) c
);
778 device
->reset_hw(chan
);
780 spin_lock_bh(&ioat
->ring_lock
);
781 descs
= ioat2_ring_space(ioat
);
782 dev_dbg(to_dev(chan
), "freeing %d idle descriptors\n", descs
);
783 for (i
= 0; i
< descs
; i
++) {
784 desc
= ioat2_get_ring_ent(ioat
, ioat
->head
+ i
);
785 ioat2_free_ring_ent(desc
, c
);
788 if (descs
< total_descs
)
789 dev_err(to_dev(chan
), "Freeing %d in use descriptors!\n",
790 total_descs
- descs
);
792 for (i
= 0; i
< total_descs
- descs
; i
++) {
793 desc
= ioat2_get_ring_ent(ioat
, ioat
->tail
+ i
);
794 dump_desc_dbg(ioat
, desc
);
795 ioat2_free_ring_ent(desc
, c
);
800 ioat
->alloc_order
= 0;
801 pci_pool_free(device
->completion_pool
, chan
->completion
,
802 chan
->completion_dma
);
803 spin_unlock_bh(&ioat
->ring_lock
);
805 chan
->last_completion
= 0;
806 chan
->completion_dma
= 0;
810 static ssize_t
ring_size_show(struct dma_chan
*c
, char *page
)
812 struct ioat2_dma_chan
*ioat
= to_ioat2_chan(c
);
814 return sprintf(page
, "%d\n", (1 << ioat
->alloc_order
) & ~1);
816 static struct ioat_sysfs_entry ring_size_attr
= __ATTR_RO(ring_size
);
818 static ssize_t
ring_active_show(struct dma_chan
*c
, char *page
)
820 struct ioat2_dma_chan
*ioat
= to_ioat2_chan(c
);
822 /* ...taken outside the lock, no need to be precise */
823 return sprintf(page
, "%d\n", ioat2_ring_active(ioat
));
825 static struct ioat_sysfs_entry ring_active_attr
= __ATTR_RO(ring_active
);
827 static struct attribute
*ioat2_attrs
[] = {
828 &ring_size_attr
.attr
,
829 &ring_active_attr
.attr
,
831 &ioat_version_attr
.attr
,
835 struct kobj_type ioat2_ktype
= {
836 .sysfs_ops
= &ioat_sysfs_ops
,
837 .default_attrs
= ioat2_attrs
,
840 int __devinit
ioat2_dma_probe(struct ioatdma_device
*device
, int dca
)
842 struct pci_dev
*pdev
= device
->pdev
;
843 struct dma_device
*dma
;
845 struct ioat_chan_common
*chan
;
848 device
->enumerate_channels
= ioat2_enumerate_channels
;
849 device
->reset_hw
= ioat2_reset_hw
;
850 device
->cleanup_fn
= ioat2_cleanup_event
;
851 device
->timer_fn
= ioat2_timer_event
;
852 device
->self_test
= ioat_dma_self_test
;
853 dma
= &device
->common
;
854 dma
->device_prep_dma_memcpy
= ioat2_dma_prep_memcpy_lock
;
855 dma
->device_issue_pending
= ioat2_issue_pending
;
856 dma
->device_alloc_chan_resources
= ioat2_alloc_chan_resources
;
857 dma
->device_free_chan_resources
= ioat2_free_chan_resources
;
858 dma
->device_is_tx_complete
= ioat_is_dma_complete
;
860 err
= ioat_probe(device
);
863 ioat_set_tcp_copy_break(2048);
865 list_for_each_entry(c
, &dma
->channels
, device_node
) {
866 chan
= to_chan_common(c
);
867 writel(IOAT_DCACTRL_CMPL_WRITE_ENABLE
| IOAT_DMA_DCA_ANY_CPU
,
868 chan
->reg_base
+ IOAT_DCACTRL_OFFSET
);
871 err
= ioat_register(device
);
875 ioat_kobject_add(device
, &ioat2_ktype
);
878 device
->dca
= ioat2_dca_init(pdev
, device
->reg_base
);