2 * Intel MIC Platform Software Stack (MPSS)
4 * Copyright(c) 2015 Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2, as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
18 #include "scif_main.h"
22 * struct scif_dma_comp_cb - SCIF DMA completion callback
24 * @dma_completion_func: DMA completion callback
25 * @cb_cookie: DMA completion callback cookie
26 * @temp_buf: Temporary buffer
27 * @temp_buf_to_free: Temporary buffer to be freed
28 * @is_cache: Is a kmem_cache allocated buffer
29 * @dst_offset: Destination registration offset
30 * @dst_window: Destination registration window
31 * @len: Length of the temp buffer
32 * @temp_phys: DMA address of the temp buffer
33 * @sdev: The SCIF device
34 * @header_padding: padding for cache line alignment
36 struct scif_dma_comp_cb
{
37 void (*dma_completion_func
)(void *cookie
);
43 struct scif_window
*dst_window
;
46 struct scif_dev
*sdev
;
51 * struct scif_copy_work - Work for DMA copy
53 * @src_offset: Starting source offset
54 * @dst_offset: Starting destination offset
55 * @src_window: Starting src registered window
56 * @dst_window: Starting dst registered window
57 * @loopback: true if this is a loopback DMA transfer
58 * @len: Length of the transfer
59 * @comp_cb: DMA copy completion callback
60 * @remote_dev: The remote SCIF peer device
61 * @fence_type: polling or interrupt based
62 * @ordered: is this a tail byte ordered DMA transfer
64 struct scif_copy_work
{
67 struct scif_window
*src_window
;
68 struct scif_window
*dst_window
;
71 struct scif_dma_comp_cb
*comp_cb
;
72 struct scif_dev
*remote_dev
;
78 * scif_reserve_dma_chan:
79 * @ep: Endpoint Descriptor.
81 * This routine reserves a DMA channel for a particular
82 * endpoint. All DMA transfers for an endpoint are always
83 * programmed on the same DMA channel.
85 int scif_reserve_dma_chan(struct scif_endpt
*ep
)
88 struct scif_dev
*scifdev
;
89 struct scif_hw_dev
*sdev
;
90 struct dma_chan
*chan
;
92 /* Loopback DMAs are not supported on the management node */
93 if (!scif_info
.nodeid
&& scifdev_self(ep
->remote_dev
))
96 scifdev
= &scif_dev
[0];
98 scifdev
= ep
->remote_dev
;
100 if (!sdev
->num_dma_ch
)
102 chan
= sdev
->dma_ch
[scifdev
->dma_ch_idx
];
103 scifdev
->dma_ch_idx
= (scifdev
->dma_ch_idx
+ 1) % sdev
->num_dma_ch
;
104 mutex_lock(&ep
->rma_info
.rma_lock
);
105 ep
->rma_info
.dma_chan
= chan
;
106 mutex_unlock(&ep
->rma_info
.rma_lock
);
110 #ifdef CONFIG_MMU_NOTIFIER
112 * scif_rma_destroy_tcw:
114 * This routine destroys temporary cached windows
117 void __scif_rma_destroy_tcw(struct scif_mmu_notif
*mmn
,
120 struct list_head
*item
, *tmp
;
121 struct scif_window
*window
;
122 u64 start_va
, end_va
;
123 u64 end
= start
+ len
;
128 list_for_each_safe(item
, tmp
, &mmn
->tc_reg_list
) {
129 window
= list_entry(item
, struct scif_window
, list
);
132 start_va
= window
->va_for_temp
;
133 end_va
= start_va
+ (window
->nr_pages
<< PAGE_SHIFT
);
134 if (start
< start_va
&& end
<= start_va
)
138 __scif_rma_destroy_tcw_helper(window
);
142 static void scif_rma_destroy_tcw(struct scif_mmu_notif
*mmn
, u64 start
, u64 len
)
144 struct scif_endpt
*ep
= mmn
->ep
;
146 spin_lock(&ep
->rma_info
.tc_lock
);
147 __scif_rma_destroy_tcw(mmn
, start
, len
);
148 spin_unlock(&ep
->rma_info
.tc_lock
);
151 static void scif_rma_destroy_tcw_ep(struct scif_endpt
*ep
)
153 struct list_head
*item
, *tmp
;
154 struct scif_mmu_notif
*mmn
;
156 list_for_each_safe(item
, tmp
, &ep
->rma_info
.mmn_list
) {
157 mmn
= list_entry(item
, struct scif_mmu_notif
, list
);
158 scif_rma_destroy_tcw(mmn
, 0, ULONG_MAX
);
162 static void __scif_rma_destroy_tcw_ep(struct scif_endpt
*ep
)
164 struct list_head
*item
, *tmp
;
165 struct scif_mmu_notif
*mmn
;
167 spin_lock(&ep
->rma_info
.tc_lock
);
168 list_for_each_safe(item
, tmp
, &ep
->rma_info
.mmn_list
) {
169 mmn
= list_entry(item
, struct scif_mmu_notif
, list
);
170 __scif_rma_destroy_tcw(mmn
, 0, ULONG_MAX
);
172 spin_unlock(&ep
->rma_info
.tc_lock
);
175 static bool scif_rma_tc_can_cache(struct scif_endpt
*ep
, size_t cur_bytes
)
177 if ((cur_bytes
>> PAGE_SHIFT
) > scif_info
.rma_tc_limit
)
179 if ((atomic_read(&ep
->rma_info
.tcw_total_pages
)
180 + (cur_bytes
>> PAGE_SHIFT
)) >
181 scif_info
.rma_tc_limit
) {
182 dev_info(scif_info
.mdev
.this_device
,
183 "%s %d total=%d, current=%zu reached max\n",
185 atomic_read(&ep
->rma_info
.tcw_total_pages
),
186 (1 + (cur_bytes
>> PAGE_SHIFT
)));
187 scif_rma_destroy_tcw_invalid();
188 __scif_rma_destroy_tcw_ep(ep
);
193 static void scif_mmu_notifier_release(struct mmu_notifier
*mn
,
194 struct mm_struct
*mm
)
196 struct scif_mmu_notif
*mmn
;
198 mmn
= container_of(mn
, struct scif_mmu_notif
, ep_mmu_notifier
);
199 scif_rma_destroy_tcw(mmn
, 0, ULONG_MAX
);
200 schedule_work(&scif_info
.misc_work
);
203 static int scif_mmu_notifier_invalidate_range_start(struct mmu_notifier
*mn
,
204 const struct mmu_notifier_range
*range
)
206 struct scif_mmu_notif
*mmn
;
208 mmn
= container_of(mn
, struct scif_mmu_notif
, ep_mmu_notifier
);
209 scif_rma_destroy_tcw(mmn
, range
->start
, range
->end
- range
->start
);
214 static void scif_mmu_notifier_invalidate_range_end(struct mmu_notifier
*mn
,
215 const struct mmu_notifier_range
*range
)
218 * Nothing to do here, everything needed was done in
219 * invalidate_range_start.
223 static const struct mmu_notifier_ops scif_mmu_notifier_ops
= {
224 .release
= scif_mmu_notifier_release
,
225 .clear_flush_young
= NULL
,
226 .invalidate_range_start
= scif_mmu_notifier_invalidate_range_start
,
227 .invalidate_range_end
= scif_mmu_notifier_invalidate_range_end
};
229 static void scif_ep_unregister_mmu_notifier(struct scif_endpt
*ep
)
231 struct scif_endpt_rma_info
*rma
= &ep
->rma_info
;
232 struct scif_mmu_notif
*mmn
= NULL
;
233 struct list_head
*item
, *tmp
;
235 mutex_lock(&ep
->rma_info
.mmn_lock
);
236 list_for_each_safe(item
, tmp
, &rma
->mmn_list
) {
237 mmn
= list_entry(item
, struct scif_mmu_notif
, list
);
238 mmu_notifier_unregister(&mmn
->ep_mmu_notifier
, mmn
->mm
);
242 mutex_unlock(&ep
->rma_info
.mmn_lock
);
245 static void scif_init_mmu_notifier(struct scif_mmu_notif
*mmn
,
246 struct mm_struct
*mm
, struct scif_endpt
*ep
)
250 mmn
->ep_mmu_notifier
.ops
= &scif_mmu_notifier_ops
;
251 INIT_LIST_HEAD(&mmn
->list
);
252 INIT_LIST_HEAD(&mmn
->tc_reg_list
);
255 static struct scif_mmu_notif
*
256 scif_find_mmu_notifier(struct mm_struct
*mm
, struct scif_endpt_rma_info
*rma
)
258 struct scif_mmu_notif
*mmn
;
260 list_for_each_entry(mmn
, &rma
->mmn_list
, list
)
266 static struct scif_mmu_notif
*
267 scif_add_mmu_notifier(struct mm_struct
*mm
, struct scif_endpt
*ep
)
269 struct scif_mmu_notif
*mmn
270 = kzalloc(sizeof(*mmn
), GFP_KERNEL
);
273 return ERR_PTR(-ENOMEM
);
275 scif_init_mmu_notifier(mmn
, current
->mm
, ep
);
276 if (mmu_notifier_register(&mmn
->ep_mmu_notifier
, current
->mm
)) {
278 return ERR_PTR(-EBUSY
);
280 list_add(&mmn
->list
, &ep
->rma_info
.mmn_list
);
285 * Called from the misc thread to destroy temporary cached windows and
286 * unregister the MMU notifier for the SCIF endpoint.
288 void scif_mmu_notif_handler(struct work_struct
*work
)
290 struct list_head
*pos
, *tmpq
;
291 struct scif_endpt
*ep
;
293 scif_rma_destroy_tcw_invalid();
294 spin_lock(&scif_info
.rmalock
);
295 list_for_each_safe(pos
, tmpq
, &scif_info
.mmu_notif_cleanup
) {
296 ep
= list_entry(pos
, struct scif_endpt
, mmu_list
);
297 list_del(&ep
->mmu_list
);
298 spin_unlock(&scif_info
.rmalock
);
299 scif_rma_destroy_tcw_ep(ep
);
300 scif_ep_unregister_mmu_notifier(ep
);
303 spin_unlock(&scif_info
.rmalock
);
306 static bool scif_is_set_reg_cache(int flags
)
308 return !!(flags
& SCIF_RMA_USECACHE
);
311 static struct scif_mmu_notif
*
312 scif_find_mmu_notifier(struct mm_struct
*mm
,
313 struct scif_endpt_rma_info
*rma
)
318 static struct scif_mmu_notif
*
319 scif_add_mmu_notifier(struct mm_struct
*mm
, struct scif_endpt
*ep
)
324 void scif_mmu_notif_handler(struct work_struct
*work
)
328 static bool scif_is_set_reg_cache(int flags
)
333 static bool scif_rma_tc_can_cache(struct scif_endpt
*ep
, size_t cur_bytes
)
340 * scif_register_temp:
341 * @epd: End Point Descriptor.
342 * @addr: virtual address to/from which to copy
343 * @len: length of range to copy
344 * @out_offset: computed offset returned by reference.
345 * @out_window: allocated registered window returned by reference.
347 * Create a temporary registered window. The peer will not know about this
348 * window. This API is used for scif_vreadfrom()/scif_vwriteto() API's.
351 scif_register_temp(scif_epd_t epd
, unsigned long addr
, size_t len
, int prot
,
352 off_t
*out_offset
, struct scif_window
**out_window
)
354 struct scif_endpt
*ep
= (struct scif_endpt
*)epd
;
356 scif_pinned_pages_t pinned_pages
;
359 aligned_len
= ALIGN(len
, PAGE_SIZE
);
361 err
= __scif_pin_pages((void *)(addr
& PAGE_MASK
),
362 aligned_len
, &prot
, 0, &pinned_pages
);
366 pinned_pages
->prot
= prot
;
368 /* Compute the offset for this registration */
369 err
= scif_get_window_offset(ep
, 0, 0,
370 aligned_len
>> PAGE_SHIFT
,
375 /* Allocate and prepare self registration window */
376 *out_window
= scif_create_window(ep
, aligned_len
>> PAGE_SHIFT
,
379 scif_free_window_offset(ep
, NULL
, *out_offset
);
384 (*out_window
)->pinned_pages
= pinned_pages
;
385 (*out_window
)->nr_pages
= pinned_pages
->nr_pages
;
386 (*out_window
)->prot
= pinned_pages
->prot
;
388 (*out_window
)->va_for_temp
= addr
& PAGE_MASK
;
389 err
= scif_map_window(ep
->remote_dev
, *out_window
);
391 /* Something went wrong! Rollback */
392 scif_destroy_window(ep
, *out_window
);
395 *out_offset
|= (addr
- (*out_window
)->va_for_temp
);
400 dev_err(&ep
->remote_dev
->sdev
->dev
,
401 "%s %d err %d\n", __func__
, __LINE__
, err
);
402 scif_unpin_pages(pinned_pages
);
406 #define SCIF_DMA_TO (3 * HZ)
409 * scif_sync_dma - Program a DMA without an interrupt descriptor
411 * @dev - The address of the pointer to the device instance used
412 * for DMA registration.
413 * @chan - DMA channel to be used.
414 * @sync_wait: Wait for DMA to complete?
416 * Return 0 on success and -errno on error.
418 static int scif_sync_dma(struct scif_hw_dev
*sdev
, struct dma_chan
*chan
,
422 struct dma_async_tx_descriptor
*tx
= NULL
;
423 enum dma_ctrl_flags flags
= DMA_PREP_FENCE
;
425 struct dma_device
*ddev
;
429 dev_err(&sdev
->dev
, "%s %d err %d\n",
430 __func__
, __LINE__
, err
);
435 tx
= ddev
->device_prep_dma_memcpy(chan
, 0, 0, 0, flags
);
438 dev_err(&sdev
->dev
, "%s %d err %d\n",
439 __func__
, __LINE__
, err
);
442 cookie
= tx
->tx_submit(tx
);
444 if (dma_submit_error(cookie
)) {
446 dev_err(&sdev
->dev
, "%s %d err %d\n",
447 __func__
, __LINE__
, err
);
451 dma_async_issue_pending(chan
);
453 if (dma_sync_wait(chan
, cookie
) == DMA_COMPLETE
) {
457 dev_err(&sdev
->dev
, "%s %d err %d\n",
458 __func__
, __LINE__
, err
);
465 static void scif_dma_callback(void *arg
)
467 struct completion
*done
= (struct completion
*)arg
;
472 #define SCIF_DMA_SYNC_WAIT true
473 #define SCIF_DMA_POLL BIT(0)
474 #define SCIF_DMA_INTR BIT(1)
477 * scif_async_dma - Program a DMA with an interrupt descriptor
479 * @dev - The address of the pointer to the device instance used
480 * for DMA registration.
481 * @chan - DMA channel to be used.
482 * Return 0 on success and -errno on error.
484 static int scif_async_dma(struct scif_hw_dev
*sdev
, struct dma_chan
*chan
)
487 struct dma_device
*ddev
;
488 struct dma_async_tx_descriptor
*tx
= NULL
;
489 enum dma_ctrl_flags flags
= DMA_PREP_INTERRUPT
| DMA_PREP_FENCE
;
490 DECLARE_COMPLETION_ONSTACK(done_wait
);
492 enum dma_status status
;
496 dev_err(&sdev
->dev
, "%s %d err %d\n",
497 __func__
, __LINE__
, err
);
502 tx
= ddev
->device_prep_dma_memcpy(chan
, 0, 0, 0, flags
);
505 dev_err(&sdev
->dev
, "%s %d err %d\n",
506 __func__
, __LINE__
, err
);
509 reinit_completion(&done_wait
);
510 tx
->callback
= scif_dma_callback
;
511 tx
->callback_param
= &done_wait
;
512 cookie
= tx
->tx_submit(tx
);
514 if (dma_submit_error(cookie
)) {
516 dev_err(&sdev
->dev
, "%s %d err %d\n",
517 __func__
, __LINE__
, err
);
520 dma_async_issue_pending(chan
);
522 err
= wait_for_completion_timeout(&done_wait
, SCIF_DMA_TO
);
525 dev_err(&sdev
->dev
, "%s %d err %d\n",
526 __func__
, __LINE__
, err
);
530 status
= dma_async_is_tx_complete(chan
, cookie
, NULL
, NULL
);
531 if (status
!= DMA_COMPLETE
) {
533 dev_err(&sdev
->dev
, "%s %d err %d\n",
534 __func__
, __LINE__
, err
);
542 * scif_drain_dma_poll - Drain all outstanding DMA operations for a particular
543 * DMA channel via polling.
545 * @sdev - The SCIF device
546 * @chan - DMA channel
547 * Return 0 on success and -errno on error.
549 static int scif_drain_dma_poll(struct scif_hw_dev
*sdev
, struct dma_chan
*chan
)
553 return scif_sync_dma(sdev
, chan
, SCIF_DMA_SYNC_WAIT
);
557 * scif_drain_dma_intr - Drain all outstanding DMA operations for a particular
558 * DMA channel via interrupt based blocking wait.
560 * @sdev - The SCIF device
561 * @chan - DMA channel
562 * Return 0 on success and -errno on error.
564 int scif_drain_dma_intr(struct scif_hw_dev
*sdev
, struct dma_chan
*chan
)
568 return scif_async_dma(sdev
, chan
);
572 * scif_rma_destroy_windows:
574 * This routine destroys all windows queued for cleanup
576 void scif_rma_destroy_windows(void)
578 struct list_head
*item
, *tmp
;
579 struct scif_window
*window
;
580 struct scif_endpt
*ep
;
581 struct dma_chan
*chan
;
585 spin_lock(&scif_info
.rmalock
);
586 list_for_each_safe(item
, tmp
, &scif_info
.rma
) {
587 window
= list_entry(item
, struct scif_window
,
589 ep
= (struct scif_endpt
*)window
->ep
;
590 chan
= ep
->rma_info
.dma_chan
;
592 list_del_init(&window
->list
);
593 spin_unlock(&scif_info
.rmalock
);
594 if (!chan
|| !scifdev_alive(ep
) ||
595 !scif_drain_dma_intr(ep
->remote_dev
->sdev
,
596 ep
->rma_info
.dma_chan
))
597 /* Remove window from global list */
598 window
->unreg_state
= OP_COMPLETED
;
600 dev_warn(&ep
->remote_dev
->sdev
->dev
,
601 "DMA engine hung?\n");
602 if (window
->unreg_state
== OP_COMPLETED
) {
603 if (window
->type
== SCIF_WINDOW_SELF
)
604 scif_destroy_window(ep
, window
);
606 scif_destroy_remote_window(window
);
607 atomic_dec(&ep
->rma_info
.tw_refcount
);
611 spin_unlock(&scif_info
.rmalock
);
615 * scif_rma_destroy_tcw:
617 * This routine destroys temporary cached registered windows
618 * which have been queued for cleanup.
620 void scif_rma_destroy_tcw_invalid(void)
622 struct list_head
*item
, *tmp
;
623 struct scif_window
*window
;
624 struct scif_endpt
*ep
;
625 struct dma_chan
*chan
;
629 spin_lock(&scif_info
.rmalock
);
630 list_for_each_safe(item
, tmp
, &scif_info
.rma_tc
) {
631 window
= list_entry(item
, struct scif_window
, list
);
632 ep
= (struct scif_endpt
*)window
->ep
;
633 chan
= ep
->rma_info
.dma_chan
;
634 list_del_init(&window
->list
);
635 spin_unlock(&scif_info
.rmalock
);
636 mutex_lock(&ep
->rma_info
.rma_lock
);
637 if (!chan
|| !scifdev_alive(ep
) ||
638 !scif_drain_dma_intr(ep
->remote_dev
->sdev
,
639 ep
->rma_info
.dma_chan
)) {
640 atomic_sub(window
->nr_pages
,
641 &ep
->rma_info
.tcw_total_pages
);
642 scif_destroy_window(ep
, window
);
643 atomic_dec(&ep
->rma_info
.tcw_refcount
);
645 dev_warn(&ep
->remote_dev
->sdev
->dev
,
646 "DMA engine hung?\n");
648 mutex_unlock(&ep
->rma_info
.rma_lock
);
651 spin_unlock(&scif_info
.rmalock
);
655 void *_get_local_va(off_t off
, struct scif_window
*window
, size_t len
)
657 int page_nr
= (off
- window
->offset
) >> PAGE_SHIFT
;
658 off_t page_off
= off
& ~PAGE_MASK
;
661 if (window
->type
== SCIF_WINDOW_SELF
) {
662 struct page
**pages
= window
->pinned_pages
->pages
;
664 va
= page_address(pages
[page_nr
]) + page_off
;
670 void *ioremap_remote(off_t off
, struct scif_window
*window
,
671 size_t len
, struct scif_dev
*dev
,
672 struct scif_window_iter
*iter
)
674 dma_addr_t phys
= scif_off_to_dma_addr(window
, off
, NULL
, iter
);
677 * If the DMA address is not card relative then we need the DMA
678 * addresses to be an offset into the bar. The aperture base was already
679 * added so subtract it here since scif_ioremap is going to add it again
681 if (!scifdev_self(dev
) && window
->type
== SCIF_WINDOW_PEER
&&
682 dev
->sdev
->aper
&& !dev
->sdev
->card_rel_da
)
683 phys
= phys
- dev
->sdev
->aper
->pa
;
684 return scif_ioremap(phys
, len
, dev
);
688 iounmap_remote(void *virt
, size_t size
, struct scif_copy_work
*work
)
690 scif_iounmap(virt
, size
, work
->remote_dev
);
694 * Takes care of ordering issue caused by
695 * 1. Hardware: Only in the case of cpu copy from mgmt node to card
696 * because of WC memory.
697 * 2. Software: If memcpy reorders copy instructions for optimization.
698 * This could happen at both mgmt node and card.
701 scif_ordered_memcpy_toio(char *dst
, const char *src
, size_t count
)
706 memcpy_toio((void __iomem __force
*)dst
, src
, --count
);
707 /* Order the last byte with the previous stores */
709 *(dst
+ count
) = *(src
+ count
);
712 static inline void scif_unaligned_cpy_toio(char *dst
, const char *src
,
713 size_t count
, bool ordered
)
716 scif_ordered_memcpy_toio(dst
, src
, count
);
718 memcpy_toio((void __iomem __force
*)dst
, src
, count
);
722 void scif_ordered_memcpy_fromio(char *dst
, const char *src
, size_t count
)
727 memcpy_fromio(dst
, (void __iomem __force
*)src
, --count
);
728 /* Order the last byte with the previous loads */
730 *(dst
+ count
) = *(src
+ count
);
733 static inline void scif_unaligned_cpy_fromio(char *dst
, const char *src
,
734 size_t count
, bool ordered
)
737 scif_ordered_memcpy_fromio(dst
, src
, count
);
739 memcpy_fromio(dst
, (void __iomem __force
*)src
, count
);
742 #define SCIF_RMA_ERROR_CODE (~(dma_addr_t)0x0)
745 * scif_off_to_dma_addr:
746 * Obtain the dma_addr given the window and the offset.
747 * @window: Registered window.
748 * @off: Window offset.
749 * @nr_bytes: Return the number of contiguous bytes till next DMA addr index.
750 * @index: Return the index of the dma_addr array found.
751 * @start_off: start offset of index of the dma addr array found.
752 * The nr_bytes provides the callee an estimate of the maximum possible
753 * DMA xfer possible while the index/start_off provide faster lookups
754 * for the next iteration.
756 dma_addr_t
scif_off_to_dma_addr(struct scif_window
*window
, s64 off
,
757 size_t *nr_bytes
, struct scif_window_iter
*iter
)
763 if (window
->nr_pages
== window
->nr_contig_chunks
) {
764 page_nr
= (off
- window
->offset
) >> PAGE_SHIFT
;
765 page_off
= off
& ~PAGE_MASK
;
768 *nr_bytes
= PAGE_SIZE
- page_off
;
769 return window
->dma_addr
[page_nr
] | page_off
;
773 start
= iter
->offset
;
776 start
= window
->offset
;
778 for (; i
< window
->nr_contig_chunks
; i
++) {
779 end
= start
+ (window
->num_pages
[i
] << PAGE_SHIFT
);
780 if (off
>= start
&& off
< end
) {
783 iter
->offset
= start
;
786 *nr_bytes
= end
- off
;
787 return (window
->dma_addr
[i
] + (off
- start
));
789 start
+= (window
->num_pages
[i
] << PAGE_SHIFT
);
791 dev_err(scif_info
.mdev
.this_device
,
792 "%s %d BUG. Addr not found? window %p off 0x%llx\n",
793 __func__
, __LINE__
, window
, off
);
794 return SCIF_RMA_ERROR_CODE
;
798 * Copy between rma window and temporary buffer
800 static void scif_rma_local_cpu_copy(s64 offset
, struct scif_window
*window
,
801 u8
*temp
, size_t rem_len
, bool to_temp
)
808 offset_in_page
= offset
& ~PAGE_MASK
;
809 loop_len
= PAGE_SIZE
- offset_in_page
;
811 if (rem_len
< loop_len
)
814 window_virt
= _get_local_va(offset
, window
, loop_len
);
818 memcpy(temp
, window_virt
, loop_len
);
820 memcpy(window_virt
, temp
, loop_len
);
826 end_offset
= window
->offset
+
827 (window
->nr_pages
<< PAGE_SHIFT
);
829 if (offset
== end_offset
) {
830 window
= list_next_entry(window
, list
);
831 end_offset
= window
->offset
+
832 (window
->nr_pages
<< PAGE_SHIFT
);
834 loop_len
= min(PAGE_SIZE
, rem_len
);
835 window_virt
= _get_local_va(offset
, window
, loop_len
);
839 memcpy(temp
, window_virt
, loop_len
);
841 memcpy(window_virt
, temp
, loop_len
);
849 * scif_rma_completion_cb:
852 * RMA interrupt completion callback.
854 static void scif_rma_completion_cb(void *data
)
856 struct scif_dma_comp_cb
*comp_cb
= data
;
858 /* Free DMA Completion CB. */
859 if (comp_cb
->dst_window
)
860 scif_rma_local_cpu_copy(comp_cb
->dst_offset
,
863 comp_cb
->header_padding
,
864 comp_cb
->len
, false);
865 scif_unmap_single(comp_cb
->temp_phys
, comp_cb
->sdev
,
866 SCIF_KMEM_UNALIGNED_BUF_SIZE
);
867 if (comp_cb
->is_cache
)
868 kmem_cache_free(unaligned_cache
,
869 comp_cb
->temp_buf_to_free
);
871 kfree(comp_cb
->temp_buf_to_free
);
874 /* Copies between temporary buffer and offsets provided in work */
876 scif_rma_list_dma_copy_unaligned(struct scif_copy_work
*work
,
877 u8
*temp
, struct dma_chan
*chan
,
880 struct scif_dma_comp_cb
*comp_cb
= work
->comp_cb
;
881 dma_addr_t window_dma_addr
, temp_dma_addr
;
882 dma_addr_t temp_phys
= comp_cb
->temp_phys
;
883 size_t loop_len
, nr_contig_bytes
= 0, remaining_len
= work
->len
;
884 int offset_in_ca
, ret
= 0;
885 s64 end_offset
, offset
;
886 struct scif_window
*window
;
887 void *window_virt_addr
;
889 struct dma_async_tx_descriptor
*tx
;
890 struct dma_device
*dev
= chan
->device
;
894 offset
= work
->dst_offset
;
895 window
= work
->dst_window
;
897 offset
= work
->src_offset
;
898 window
= work
->src_window
;
901 offset_in_ca
= offset
& (L1_CACHE_BYTES
- 1);
903 loop_len
= L1_CACHE_BYTES
- offset_in_ca
;
904 loop_len
= min(loop_len
, remaining_len
);
905 window_virt_addr
= ioremap_remote(offset
, window
,
909 if (!window_virt_addr
)
912 scif_unaligned_cpy_toio(window_virt_addr
, temp
,
915 !(remaining_len
- loop_len
));
917 scif_unaligned_cpy_fromio(temp
, window_virt_addr
,
918 loop_len
, work
->ordered
&&
919 !(remaining_len
- loop_len
));
920 iounmap_remote(window_virt_addr
, loop_len
, work
);
924 temp_phys
+= loop_len
;
925 remaining_len
-= loop_len
;
928 offset_in_ca
= offset
& ~PAGE_MASK
;
929 end_offset
= window
->offset
+
930 (window
->nr_pages
<< PAGE_SHIFT
);
932 tail_len
= remaining_len
& (L1_CACHE_BYTES
- 1);
933 remaining_len
-= tail_len
;
934 while (remaining_len
) {
935 if (offset
== end_offset
) {
936 window
= list_next_entry(window
, list
);
937 end_offset
= window
->offset
+
938 (window
->nr_pages
<< PAGE_SHIFT
);
940 if (scif_is_mgmt_node())
941 temp_dma_addr
= temp_phys
;
943 /* Fix if we ever enable IOMMU on the card */
944 temp_dma_addr
= (dma_addr_t
)virt_to_phys(temp
);
945 window_dma_addr
= scif_off_to_dma_addr(window
, offset
,
948 loop_len
= min(nr_contig_bytes
, remaining_len
);
950 if (work
->ordered
&& !tail_len
&&
951 !(remaining_len
- loop_len
) &&
952 loop_len
!= L1_CACHE_BYTES
) {
954 * Break up the last chunk of the transfer into
955 * two steps. if there is no tail to guarantee
956 * DMA ordering. SCIF_DMA_POLLING inserts
957 * a status update descriptor in step 1 which
958 * acts as a double sided synchronization fence
959 * for the DMA engine to ensure that the last
960 * cache line in step 2 is updated last.
962 /* Step 1) DMA: Body Length - L1_CACHE_BYTES. */
964 dev
->device_prep_dma_memcpy(chan
,
974 cookie
= tx
->tx_submit(tx
);
975 if (dma_submit_error(cookie
)) {
979 dma_async_issue_pending(chan
);
980 offset
+= (loop_len
- L1_CACHE_BYTES
);
981 temp_dma_addr
+= (loop_len
- L1_CACHE_BYTES
);
982 window_dma_addr
+= (loop_len
- L1_CACHE_BYTES
);
983 remaining_len
-= (loop_len
- L1_CACHE_BYTES
);
984 loop_len
= remaining_len
;
986 /* Step 2) DMA: L1_CACHE_BYTES */
988 dev
->device_prep_dma_memcpy(chan
,
996 cookie
= tx
->tx_submit(tx
);
997 if (dma_submit_error(cookie
)) {
1001 dma_async_issue_pending(chan
);
1004 dev
->device_prep_dma_memcpy(chan
,
1012 cookie
= tx
->tx_submit(tx
);
1013 if (dma_submit_error(cookie
)) {
1017 dma_async_issue_pending(chan
);
1020 tx
= dev
->device_prep_dma_memcpy(chan
, temp_dma_addr
,
1021 window_dma_addr
, loop_len
, 0);
1026 cookie
= tx
->tx_submit(tx
);
1027 if (dma_submit_error(cookie
)) {
1031 dma_async_issue_pending(chan
);
1035 temp_phys
+= loop_len
;
1036 remaining_len
-= loop_len
;
1040 if (offset
== end_offset
) {
1041 window
= list_next_entry(window
, list
);
1042 end_offset
= window
->offset
+
1043 (window
->nr_pages
<< PAGE_SHIFT
);
1045 window_virt_addr
= ioremap_remote(offset
, window
, tail_len
,
1048 if (!window_virt_addr
)
1051 * The CPU copy for the tail bytes must be initiated only once
1052 * previous DMA transfers for this endpoint have completed
1053 * to guarantee ordering.
1055 if (work
->ordered
) {
1056 struct scif_dev
*rdev
= work
->remote_dev
;
1058 ret
= scif_drain_dma_intr(rdev
->sdev
, chan
);
1063 scif_unaligned_cpy_toio(window_virt_addr
, temp
,
1064 tail_len
, work
->ordered
);
1066 scif_unaligned_cpy_fromio(temp
, window_virt_addr
,
1067 tail_len
, work
->ordered
);
1068 iounmap_remote(window_virt_addr
, tail_len
, work
);
1070 tx
= dev
->device_prep_dma_memcpy(chan
, 0, 0, 0, DMA_PREP_INTERRUPT
);
1075 tx
->callback
= &scif_rma_completion_cb
;
1076 tx
->callback_param
= comp_cb
;
1077 cookie
= tx
->tx_submit(tx
);
1079 if (dma_submit_error(cookie
)) {
1083 dma_async_issue_pending(chan
);
1086 dev_err(scif_info
.mdev
.this_device
,
1087 "%s %d Desc Prog Failed ret %d\n",
1088 __func__
, __LINE__
, ret
);
1093 * _scif_rma_list_dma_copy_aligned:
1095 * Traverse all the windows and perform DMA copy.
1097 static int _scif_rma_list_dma_copy_aligned(struct scif_copy_work
*work
,
1098 struct dma_chan
*chan
)
1100 dma_addr_t src_dma_addr
, dst_dma_addr
;
1101 size_t loop_len
, remaining_len
, src_contig_bytes
= 0;
1102 size_t dst_contig_bytes
= 0;
1103 struct scif_window_iter src_win_iter
;
1104 struct scif_window_iter dst_win_iter
;
1105 s64 end_src_offset
, end_dst_offset
;
1106 struct scif_window
*src_window
= work
->src_window
;
1107 struct scif_window
*dst_window
= work
->dst_window
;
1108 s64 src_offset
= work
->src_offset
, dst_offset
= work
->dst_offset
;
1110 struct dma_async_tx_descriptor
*tx
;
1111 struct dma_device
*dev
= chan
->device
;
1112 dma_cookie_t cookie
;
1114 remaining_len
= work
->len
;
1116 scif_init_window_iter(src_window
, &src_win_iter
);
1117 scif_init_window_iter(dst_window
, &dst_win_iter
);
1118 end_src_offset
= src_window
->offset
+
1119 (src_window
->nr_pages
<< PAGE_SHIFT
);
1120 end_dst_offset
= dst_window
->offset
+
1121 (dst_window
->nr_pages
<< PAGE_SHIFT
);
1122 while (remaining_len
) {
1123 if (src_offset
== end_src_offset
) {
1124 src_window
= list_next_entry(src_window
, list
);
1125 end_src_offset
= src_window
->offset
+
1126 (src_window
->nr_pages
<< PAGE_SHIFT
);
1127 scif_init_window_iter(src_window
, &src_win_iter
);
1129 if (dst_offset
== end_dst_offset
) {
1130 dst_window
= list_next_entry(dst_window
, list
);
1131 end_dst_offset
= dst_window
->offset
+
1132 (dst_window
->nr_pages
<< PAGE_SHIFT
);
1133 scif_init_window_iter(dst_window
, &dst_win_iter
);
1136 /* compute dma addresses for transfer */
1137 src_dma_addr
= scif_off_to_dma_addr(src_window
, src_offset
,
1140 dst_dma_addr
= scif_off_to_dma_addr(dst_window
, dst_offset
,
1143 loop_len
= min(src_contig_bytes
, dst_contig_bytes
);
1144 loop_len
= min(loop_len
, remaining_len
);
1145 if (work
->ordered
&& !(remaining_len
- loop_len
)) {
1147 * Break up the last chunk of the transfer into two
1148 * steps to ensure that the last byte in step 2 is
1151 /* Step 1) DMA: Body Length - 1 */
1152 tx
= dev
->device_prep_dma_memcpy(chan
, dst_dma_addr
,
1160 cookie
= tx
->tx_submit(tx
);
1161 if (dma_submit_error(cookie
)) {
1165 src_offset
+= (loop_len
- 1);
1166 dst_offset
+= (loop_len
- 1);
1167 src_dma_addr
+= (loop_len
- 1);
1168 dst_dma_addr
+= (loop_len
- 1);
1169 remaining_len
-= (loop_len
- 1);
1170 loop_len
= remaining_len
;
1172 /* Step 2) DMA: 1 BYTES */
1173 tx
= dev
->device_prep_dma_memcpy(chan
, dst_dma_addr
,
1174 src_dma_addr
, loop_len
, 0);
1179 cookie
= tx
->tx_submit(tx
);
1180 if (dma_submit_error(cookie
)) {
1184 dma_async_issue_pending(chan
);
1186 tx
= dev
->device_prep_dma_memcpy(chan
, dst_dma_addr
,
1187 src_dma_addr
, loop_len
, 0);
1192 cookie
= tx
->tx_submit(tx
);
1193 if (dma_submit_error(cookie
)) {
1198 src_offset
+= loop_len
;
1199 dst_offset
+= loop_len
;
1200 remaining_len
-= loop_len
;
1204 dev_err(scif_info
.mdev
.this_device
,
1205 "%s %d Desc Prog Failed ret %d\n",
1206 __func__
, __LINE__
, ret
);
1211 * scif_rma_list_dma_copy_aligned:
1213 * Traverse all the windows and perform DMA copy.
1215 static int scif_rma_list_dma_copy_aligned(struct scif_copy_work
*work
,
1216 struct dma_chan
*chan
)
1218 dma_addr_t src_dma_addr
, dst_dma_addr
;
1219 size_t loop_len
, remaining_len
, tail_len
, src_contig_bytes
= 0;
1220 size_t dst_contig_bytes
= 0;
1222 s64 end_src_offset
, end_dst_offset
;
1223 struct scif_window_iter src_win_iter
;
1224 struct scif_window_iter dst_win_iter
;
1225 void *src_virt
, *dst_virt
;
1226 struct scif_window
*src_window
= work
->src_window
;
1227 struct scif_window
*dst_window
= work
->dst_window
;
1228 s64 src_offset
= work
->src_offset
, dst_offset
= work
->dst_offset
;
1230 struct dma_async_tx_descriptor
*tx
;
1231 struct dma_device
*dev
= chan
->device
;
1232 dma_cookie_t cookie
;
1234 remaining_len
= work
->len
;
1235 scif_init_window_iter(src_window
, &src_win_iter
);
1236 scif_init_window_iter(dst_window
, &dst_win_iter
);
1238 src_cache_off
= src_offset
& (L1_CACHE_BYTES
- 1);
1239 if (src_cache_off
!= 0) {
1241 loop_len
= L1_CACHE_BYTES
- src_cache_off
;
1242 loop_len
= min(loop_len
, remaining_len
);
1243 src_dma_addr
= __scif_off_to_dma_addr(src_window
, src_offset
);
1244 dst_dma_addr
= __scif_off_to_dma_addr(dst_window
, dst_offset
);
1245 if (src_window
->type
== SCIF_WINDOW_SELF
)
1246 src_virt
= _get_local_va(src_offset
, src_window
,
1249 src_virt
= ioremap_remote(src_offset
, src_window
,
1251 work
->remote_dev
, NULL
);
1254 if (dst_window
->type
== SCIF_WINDOW_SELF
)
1255 dst_virt
= _get_local_va(dst_offset
, dst_window
,
1258 dst_virt
= ioremap_remote(dst_offset
, dst_window
,
1260 work
->remote_dev
, NULL
);
1262 if (src_window
->type
!= SCIF_WINDOW_SELF
)
1263 iounmap_remote(src_virt
, loop_len
, work
);
1266 if (src_window
->type
== SCIF_WINDOW_SELF
)
1267 scif_unaligned_cpy_toio(dst_virt
, src_virt
, loop_len
,
1268 remaining_len
== loop_len
?
1269 work
->ordered
: false);
1271 scif_unaligned_cpy_fromio(dst_virt
, src_virt
, loop_len
,
1272 remaining_len
== loop_len
?
1273 work
->ordered
: false);
1274 if (src_window
->type
!= SCIF_WINDOW_SELF
)
1275 iounmap_remote(src_virt
, loop_len
, work
);
1276 if (dst_window
->type
!= SCIF_WINDOW_SELF
)
1277 iounmap_remote(dst_virt
, loop_len
, work
);
1278 src_offset
+= loop_len
;
1279 dst_offset
+= loop_len
;
1280 remaining_len
-= loop_len
;
1283 end_src_offset
= src_window
->offset
+
1284 (src_window
->nr_pages
<< PAGE_SHIFT
);
1285 end_dst_offset
= dst_window
->offset
+
1286 (dst_window
->nr_pages
<< PAGE_SHIFT
);
1287 tail_len
= remaining_len
& (L1_CACHE_BYTES
- 1);
1288 remaining_len
-= tail_len
;
1289 while (remaining_len
) {
1290 if (src_offset
== end_src_offset
) {
1291 src_window
= list_next_entry(src_window
, list
);
1292 end_src_offset
= src_window
->offset
+
1293 (src_window
->nr_pages
<< PAGE_SHIFT
);
1294 scif_init_window_iter(src_window
, &src_win_iter
);
1296 if (dst_offset
== end_dst_offset
) {
1297 dst_window
= list_next_entry(dst_window
, list
);
1298 end_dst_offset
= dst_window
->offset
+
1299 (dst_window
->nr_pages
<< PAGE_SHIFT
);
1300 scif_init_window_iter(dst_window
, &dst_win_iter
);
1303 /* compute dma addresses for transfer */
1304 src_dma_addr
= scif_off_to_dma_addr(src_window
, src_offset
,
1307 dst_dma_addr
= scif_off_to_dma_addr(dst_window
, dst_offset
,
1310 loop_len
= min(src_contig_bytes
, dst_contig_bytes
);
1311 loop_len
= min(loop_len
, remaining_len
);
1312 if (work
->ordered
&& !tail_len
&&
1313 !(remaining_len
- loop_len
)) {
1315 * Break up the last chunk of the transfer into two
1316 * steps. if there is no tail to gurantee DMA ordering.
1317 * Passing SCIF_DMA_POLLING inserts a status update
1318 * descriptor in step 1 which acts as a double sided
1319 * synchronization fence for the DMA engine to ensure
1320 * that the last cache line in step 2 is updated last.
1322 /* Step 1) DMA: Body Length - L1_CACHE_BYTES. */
1323 tx
= dev
->device_prep_dma_memcpy(chan
, dst_dma_addr
,
1332 cookie
= tx
->tx_submit(tx
);
1333 if (dma_submit_error(cookie
)) {
1337 dma_async_issue_pending(chan
);
1338 src_offset
+= (loop_len
- L1_CACHE_BYTES
);
1339 dst_offset
+= (loop_len
- L1_CACHE_BYTES
);
1340 src_dma_addr
+= (loop_len
- L1_CACHE_BYTES
);
1341 dst_dma_addr
+= (loop_len
- L1_CACHE_BYTES
);
1342 remaining_len
-= (loop_len
- L1_CACHE_BYTES
);
1343 loop_len
= remaining_len
;
1345 /* Step 2) DMA: L1_CACHE_BYTES */
1346 tx
= dev
->device_prep_dma_memcpy(chan
, dst_dma_addr
,
1353 cookie
= tx
->tx_submit(tx
);
1354 if (dma_submit_error(cookie
)) {
1358 dma_async_issue_pending(chan
);
1360 tx
= dev
->device_prep_dma_memcpy(chan
, dst_dma_addr
,
1367 cookie
= tx
->tx_submit(tx
);
1368 if (dma_submit_error(cookie
)) {
1372 dma_async_issue_pending(chan
);
1374 src_offset
+= loop_len
;
1375 dst_offset
+= loop_len
;
1376 remaining_len
-= loop_len
;
1378 remaining_len
= tail_len
;
1379 if (remaining_len
) {
1380 loop_len
= remaining_len
;
1381 if (src_offset
== end_src_offset
)
1382 src_window
= list_next_entry(src_window
, list
);
1383 if (dst_offset
== end_dst_offset
)
1384 dst_window
= list_next_entry(dst_window
, list
);
1386 src_dma_addr
= __scif_off_to_dma_addr(src_window
, src_offset
);
1387 dst_dma_addr
= __scif_off_to_dma_addr(dst_window
, dst_offset
);
1389 * The CPU copy for the tail bytes must be initiated only once
1390 * previous DMA transfers for this endpoint have completed to
1391 * guarantee ordering.
1393 if (work
->ordered
) {
1394 struct scif_dev
*rdev
= work
->remote_dev
;
1396 ret
= scif_drain_dma_poll(rdev
->sdev
, chan
);
1400 if (src_window
->type
== SCIF_WINDOW_SELF
)
1401 src_virt
= _get_local_va(src_offset
, src_window
,
1404 src_virt
= ioremap_remote(src_offset
, src_window
,
1406 work
->remote_dev
, NULL
);
1410 if (dst_window
->type
== SCIF_WINDOW_SELF
)
1411 dst_virt
= _get_local_va(dst_offset
, dst_window
,
1414 dst_virt
= ioremap_remote(dst_offset
, dst_window
,
1416 work
->remote_dev
, NULL
);
1418 if (src_window
->type
!= SCIF_WINDOW_SELF
)
1419 iounmap_remote(src_virt
, loop_len
, work
);
1423 if (src_window
->type
== SCIF_WINDOW_SELF
)
1424 scif_unaligned_cpy_toio(dst_virt
, src_virt
, loop_len
,
1427 scif_unaligned_cpy_fromio(dst_virt
, src_virt
,
1428 loop_len
, work
->ordered
);
1429 if (src_window
->type
!= SCIF_WINDOW_SELF
)
1430 iounmap_remote(src_virt
, loop_len
, work
);
1432 if (dst_window
->type
!= SCIF_WINDOW_SELF
)
1433 iounmap_remote(dst_virt
, loop_len
, work
);
1434 remaining_len
-= loop_len
;
1438 dev_err(scif_info
.mdev
.this_device
,
1439 "%s %d Desc Prog Failed ret %d\n",
1440 __func__
, __LINE__
, ret
);
1445 * scif_rma_list_cpu_copy:
1447 * Traverse all the windows and perform CPU copy.
1449 static int scif_rma_list_cpu_copy(struct scif_copy_work
*work
)
1451 void *src_virt
, *dst_virt
;
1452 size_t loop_len
, remaining_len
;
1453 int src_page_off
, dst_page_off
;
1454 s64 src_offset
= work
->src_offset
, dst_offset
= work
->dst_offset
;
1455 struct scif_window
*src_window
= work
->src_window
;
1456 struct scif_window
*dst_window
= work
->dst_window
;
1457 s64 end_src_offset
, end_dst_offset
;
1459 struct scif_window_iter src_win_iter
;
1460 struct scif_window_iter dst_win_iter
;
1462 remaining_len
= work
->len
;
1464 scif_init_window_iter(src_window
, &src_win_iter
);
1465 scif_init_window_iter(dst_window
, &dst_win_iter
);
1466 while (remaining_len
) {
1467 src_page_off
= src_offset
& ~PAGE_MASK
;
1468 dst_page_off
= dst_offset
& ~PAGE_MASK
;
1469 loop_len
= min(PAGE_SIZE
-
1470 max(src_page_off
, dst_page_off
),
1473 if (src_window
->type
== SCIF_WINDOW_SELF
)
1474 src_virt
= _get_local_va(src_offset
, src_window
,
1477 src_virt
= ioremap_remote(src_offset
, src_window
,
1486 if (dst_window
->type
== SCIF_WINDOW_SELF
)
1487 dst_virt
= _get_local_va(dst_offset
, dst_window
,
1490 dst_virt
= ioremap_remote(dst_offset
, dst_window
,
1495 if (src_window
->type
== SCIF_WINDOW_PEER
)
1496 iounmap_remote(src_virt
, loop_len
, work
);
1501 if (work
->loopback
) {
1502 memcpy(dst_virt
, src_virt
, loop_len
);
1504 if (src_window
->type
== SCIF_WINDOW_SELF
)
1505 memcpy_toio((void __iomem __force
*)dst_virt
,
1506 src_virt
, loop_len
);
1508 memcpy_fromio(dst_virt
,
1509 (void __iomem __force
*)src_virt
,
1512 if (src_window
->type
== SCIF_WINDOW_PEER
)
1513 iounmap_remote(src_virt
, loop_len
, work
);
1515 if (dst_window
->type
== SCIF_WINDOW_PEER
)
1516 iounmap_remote(dst_virt
, loop_len
, work
);
1518 src_offset
+= loop_len
;
1519 dst_offset
+= loop_len
;
1520 remaining_len
-= loop_len
;
1521 if (remaining_len
) {
1522 end_src_offset
= src_window
->offset
+
1523 (src_window
->nr_pages
<< PAGE_SHIFT
);
1524 end_dst_offset
= dst_window
->offset
+
1525 (dst_window
->nr_pages
<< PAGE_SHIFT
);
1526 if (src_offset
== end_src_offset
) {
1527 src_window
= list_next_entry(src_window
, list
);
1528 scif_init_window_iter(src_window
,
1531 if (dst_offset
== end_dst_offset
) {
1532 dst_window
= list_next_entry(dst_window
, list
);
1533 scif_init_window_iter(dst_window
,
1542 static int scif_rma_list_dma_copy_wrapper(struct scif_endpt
*epd
,
1543 struct scif_copy_work
*work
,
1544 struct dma_chan
*chan
, off_t loffset
)
1546 int src_cache_off
, dst_cache_off
;
1547 s64 src_offset
= work
->src_offset
, dst_offset
= work
->dst_offset
;
1549 bool src_local
= true;
1550 struct scif_dma_comp_cb
*comp_cb
;
1553 if (is_dma_copy_aligned(chan
->device
, 1, 1, 1))
1554 return _scif_rma_list_dma_copy_aligned(work
, chan
);
1556 src_cache_off
= src_offset
& (L1_CACHE_BYTES
- 1);
1557 dst_cache_off
= dst_offset
& (L1_CACHE_BYTES
- 1);
1559 if (dst_cache_off
== src_cache_off
)
1560 return scif_rma_list_dma_copy_aligned(work
, chan
);
1563 return scif_rma_list_cpu_copy(work
);
1564 src_local
= work
->src_window
->type
== SCIF_WINDOW_SELF
;
1566 /* Allocate dma_completion cb */
1567 comp_cb
= kzalloc(sizeof(*comp_cb
), GFP_KERNEL
);
1571 work
->comp_cb
= comp_cb
;
1572 comp_cb
->cb_cookie
= comp_cb
;
1573 comp_cb
->dma_completion_func
= &scif_rma_completion_cb
;
1575 if (work
->len
+ (L1_CACHE_BYTES
<< 1) < SCIF_KMEM_UNALIGNED_BUF_SIZE
) {
1576 comp_cb
->is_cache
= false;
1577 /* Allocate padding bytes to align to a cache line */
1578 temp
= kmalloc(work
->len
+ (L1_CACHE_BYTES
<< 1),
1582 comp_cb
->temp_buf_to_free
= temp
;
1583 /* kmalloc(..) does not guarantee cache line alignment */
1584 if (!IS_ALIGNED((u64
)temp
, L1_CACHE_BYTES
))
1585 temp
= PTR_ALIGN(temp
, L1_CACHE_BYTES
);
1587 comp_cb
->is_cache
= true;
1588 temp
= kmem_cache_alloc(unaligned_cache
, GFP_KERNEL
);
1591 comp_cb
->temp_buf_to_free
= temp
;
1595 temp
+= dst_cache_off
;
1596 scif_rma_local_cpu_copy(work
->src_offset
, work
->src_window
,
1597 temp
, work
->len
, true);
1599 comp_cb
->dst_window
= work
->dst_window
;
1600 comp_cb
->dst_offset
= work
->dst_offset
;
1601 work
->src_offset
= work
->src_offset
- src_cache_off
;
1602 comp_cb
->len
= work
->len
;
1603 work
->len
= ALIGN(work
->len
+ src_cache_off
, L1_CACHE_BYTES
);
1604 comp_cb
->header_padding
= src_cache_off
;
1606 comp_cb
->temp_buf
= temp
;
1608 err
= scif_map_single(&comp_cb
->temp_phys
, temp
,
1609 work
->remote_dev
, SCIF_KMEM_UNALIGNED_BUF_SIZE
);
1612 comp_cb
->sdev
= work
->remote_dev
;
1613 if (scif_rma_list_dma_copy_unaligned(work
, temp
, chan
, src_local
) < 0)
1616 work
->fence_type
= SCIF_DMA_INTR
;
1619 if (comp_cb
->is_cache
)
1620 kmem_cache_free(unaligned_cache
, comp_cb
->temp_buf_to_free
);
1622 kfree(comp_cb
->temp_buf_to_free
);
1631 * @epd: end point descriptor.
1632 * @loffset: offset in local registered address space to/from which to copy
1633 * @addr: user virtual address to/from which to copy
1634 * @len: length of range to copy
1635 * @roffset: offset in remote registered address space to/from which to copy
1637 * @dir: LOCAL->REMOTE or vice versa.
1638 * @last_chunk: true if this is the last chunk of a larger transfer
1640 * Validate parameters, check if src/dst registered ranges requested for copy
1641 * are valid and initiate either CPU or DMA copy.
1643 static int scif_rma_copy(scif_epd_t epd
, off_t loffset
, unsigned long addr
,
1644 size_t len
, off_t roffset
, int flags
,
1645 enum scif_rma_dir dir
, bool last_chunk
)
1647 struct scif_endpt
*ep
= (struct scif_endpt
*)epd
;
1648 struct scif_rma_req remote_req
;
1649 struct scif_rma_req req
;
1650 struct scif_window
*local_window
= NULL
;
1651 struct scif_window
*remote_window
= NULL
;
1652 struct scif_copy_work copy_work
;
1655 struct dma_chan
*chan
;
1656 struct scif_mmu_notif
*mmn
= NULL
;
1658 struct device
*spdev
;
1660 err
= scif_verify_epd(ep
);
1664 if (flags
&& !(flags
& (SCIF_RMA_USECPU
| SCIF_RMA_USECACHE
|
1665 SCIF_RMA_SYNC
| SCIF_RMA_ORDERED
)))
1668 loopback
= scifdev_self(ep
->remote_dev
) ? true : false;
1669 copy_work
.fence_type
= ((flags
& SCIF_RMA_SYNC
) && last_chunk
) ?
1671 copy_work
.ordered
= !!((flags
& SCIF_RMA_ORDERED
) && last_chunk
);
1673 /* Use CPU for Mgmt node <-> Mgmt node copies */
1674 if (loopback
&& scif_is_mgmt_node()) {
1675 flags
|= SCIF_RMA_USECPU
;
1676 copy_work
.fence_type
= 0x0;
1679 cache
= scif_is_set_reg_cache(flags
);
1681 remote_req
.out_window
= &remote_window
;
1682 remote_req
.offset
= roffset
;
1683 remote_req
.nr_bytes
= len
;
1685 * If transfer is from local to remote then the remote window
1686 * must be writeable and vice versa.
1688 remote_req
.prot
= dir
== SCIF_LOCAL_TO_REMOTE
? VM_WRITE
: VM_READ
;
1689 remote_req
.type
= SCIF_WINDOW_PARTIAL
;
1690 remote_req
.head
= &ep
->rma_info
.remote_reg_list
;
1692 spdev
= scif_get_peer_dev(ep
->remote_dev
);
1693 if (IS_ERR(spdev
)) {
1694 err
= PTR_ERR(spdev
);
1698 if (addr
&& cache
) {
1699 mutex_lock(&ep
->rma_info
.mmn_lock
);
1700 mmn
= scif_find_mmu_notifier(current
->mm
, &ep
->rma_info
);
1702 mmn
= scif_add_mmu_notifier(current
->mm
, ep
);
1703 mutex_unlock(&ep
->rma_info
.mmn_lock
);
1705 scif_put_peer_dev(spdev
);
1706 return PTR_ERR(mmn
);
1708 cache
= cache
&& !scif_rma_tc_can_cache(ep
, len
);
1710 mutex_lock(&ep
->rma_info
.rma_lock
);
1712 req
.out_window
= &local_window
;
1713 req
.nr_bytes
= ALIGN(len
+ (addr
& ~PAGE_MASK
),
1715 req
.va_for_temp
= addr
& PAGE_MASK
;
1716 req
.prot
= (dir
== SCIF_LOCAL_TO_REMOTE
?
1717 VM_READ
: VM_WRITE
| VM_READ
);
1718 /* Does a valid local window exist? */
1720 spin_lock(&ep
->rma_info
.tc_lock
);
1721 req
.head
= &mmn
->tc_reg_list
;
1722 err
= scif_query_tcw(ep
, &req
);
1723 spin_unlock(&ep
->rma_info
.tc_lock
);
1726 err
= scif_register_temp(epd
, req
.va_for_temp
,
1727 req
.nr_bytes
, req
.prot
,
1728 &loffset
, &local_window
);
1730 mutex_unlock(&ep
->rma_info
.rma_lock
);
1735 atomic_inc(&ep
->rma_info
.tcw_refcount
);
1736 atomic_add_return(local_window
->nr_pages
,
1737 &ep
->rma_info
.tcw_total_pages
);
1739 spin_lock(&ep
->rma_info
.tc_lock
);
1740 scif_insert_tcw(local_window
,
1742 spin_unlock(&ep
->rma_info
.tc_lock
);
1746 loffset
= local_window
->offset
+
1747 (addr
- local_window
->va_for_temp
);
1749 req
.out_window
= &local_window
;
1750 req
.offset
= loffset
;
1752 * If transfer is from local to remote then the self window
1753 * must be readable and vice versa.
1755 req
.prot
= dir
== SCIF_LOCAL_TO_REMOTE
? VM_READ
: VM_WRITE
;
1757 req
.type
= SCIF_WINDOW_PARTIAL
;
1758 req
.head
= &ep
->rma_info
.reg_list
;
1759 /* Does a valid local window exist? */
1760 err
= scif_query_window(&req
);
1762 mutex_unlock(&ep
->rma_info
.rma_lock
);
1767 /* Does a valid remote window exist? */
1768 err
= scif_query_window(&remote_req
);
1770 mutex_unlock(&ep
->rma_info
.rma_lock
);
1775 * Prepare copy_work for submitting work to the DMA kernel thread
1776 * or CPU copy routine.
1778 copy_work
.len
= len
;
1779 copy_work
.loopback
= loopback
;
1780 copy_work
.remote_dev
= ep
->remote_dev
;
1781 if (dir
== SCIF_LOCAL_TO_REMOTE
) {
1782 copy_work
.src_offset
= loffset
;
1783 copy_work
.src_window
= local_window
;
1784 copy_work
.dst_offset
= roffset
;
1785 copy_work
.dst_window
= remote_window
;
1787 copy_work
.src_offset
= roffset
;
1788 copy_work
.src_window
= remote_window
;
1789 copy_work
.dst_offset
= loffset
;
1790 copy_work
.dst_window
= local_window
;
1793 if (flags
& SCIF_RMA_USECPU
) {
1794 scif_rma_list_cpu_copy(©_work
);
1796 chan
= ep
->rma_info
.dma_chan
;
1797 err
= scif_rma_list_dma_copy_wrapper(epd
, ©_work
,
1801 atomic_inc(&ep
->rma_info
.tw_refcount
);
1803 mutex_unlock(&ep
->rma_info
.rma_lock
);
1806 struct scif_dev
*rdev
= ep
->remote_dev
;
1808 if (copy_work
.fence_type
== SCIF_DMA_POLL
)
1809 err
= scif_drain_dma_poll(rdev
->sdev
,
1810 ep
->rma_info
.dma_chan
);
1811 else if (copy_work
.fence_type
== SCIF_DMA_INTR
)
1812 err
= scif_drain_dma_intr(rdev
->sdev
,
1813 ep
->rma_info
.dma_chan
);
1817 scif_queue_for_cleanup(local_window
, &scif_info
.rma
);
1818 scif_put_peer_dev(spdev
);
1822 if (addr
&& local_window
&& !cache
)
1823 scif_destroy_window(ep
, local_window
);
1824 dev_err(scif_info
.mdev
.this_device
,
1825 "%s %d err %d len 0x%lx\n",
1826 __func__
, __LINE__
, err
, len
);
1828 scif_put_peer_dev(spdev
);
1832 int scif_readfrom(scif_epd_t epd
, off_t loffset
, size_t len
,
1833 off_t roffset
, int flags
)
1837 dev_dbg(scif_info
.mdev
.this_device
,
1838 "SCIFAPI readfrom: ep %p loffset 0x%lx len 0x%lx offset 0x%lx flags 0x%x\n",
1839 epd
, loffset
, len
, roffset
, flags
);
1840 if (scif_unaligned(loffset
, roffset
)) {
1841 while (len
> SCIF_MAX_UNALIGNED_BUF_SIZE
) {
1842 err
= scif_rma_copy(epd
, loffset
, 0x0,
1843 SCIF_MAX_UNALIGNED_BUF_SIZE
,
1845 SCIF_REMOTE_TO_LOCAL
, false);
1848 loffset
+= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1849 roffset
+= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1850 len
-= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1853 err
= scif_rma_copy(epd
, loffset
, 0x0, len
,
1854 roffset
, flags
, SCIF_REMOTE_TO_LOCAL
, true);
1858 EXPORT_SYMBOL_GPL(scif_readfrom
);
1860 int scif_writeto(scif_epd_t epd
, off_t loffset
, size_t len
,
1861 off_t roffset
, int flags
)
1865 dev_dbg(scif_info
.mdev
.this_device
,
1866 "SCIFAPI writeto: ep %p loffset 0x%lx len 0x%lx roffset 0x%lx flags 0x%x\n",
1867 epd
, loffset
, len
, roffset
, flags
);
1868 if (scif_unaligned(loffset
, roffset
)) {
1869 while (len
> SCIF_MAX_UNALIGNED_BUF_SIZE
) {
1870 err
= scif_rma_copy(epd
, loffset
, 0x0,
1871 SCIF_MAX_UNALIGNED_BUF_SIZE
,
1873 SCIF_LOCAL_TO_REMOTE
, false);
1876 loffset
+= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1877 roffset
+= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1878 len
-= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1881 err
= scif_rma_copy(epd
, loffset
, 0x0, len
,
1882 roffset
, flags
, SCIF_LOCAL_TO_REMOTE
, true);
1886 EXPORT_SYMBOL_GPL(scif_writeto
);
1888 int scif_vreadfrom(scif_epd_t epd
, void *addr
, size_t len
,
1889 off_t roffset
, int flags
)
1893 dev_dbg(scif_info
.mdev
.this_device
,
1894 "SCIFAPI vreadfrom: ep %p addr %p len 0x%lx roffset 0x%lx flags 0x%x\n",
1895 epd
, addr
, len
, roffset
, flags
);
1896 if (scif_unaligned((off_t __force
)addr
, roffset
)) {
1897 if (len
> SCIF_MAX_UNALIGNED_BUF_SIZE
)
1898 flags
&= ~SCIF_RMA_USECACHE
;
1900 while (len
> SCIF_MAX_UNALIGNED_BUF_SIZE
) {
1901 err
= scif_rma_copy(epd
, 0, (u64
)addr
,
1902 SCIF_MAX_UNALIGNED_BUF_SIZE
,
1904 SCIF_REMOTE_TO_LOCAL
, false);
1907 addr
+= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1908 roffset
+= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1909 len
-= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1912 err
= scif_rma_copy(epd
, 0, (u64
)addr
, len
,
1913 roffset
, flags
, SCIF_REMOTE_TO_LOCAL
, true);
1917 EXPORT_SYMBOL_GPL(scif_vreadfrom
);
1919 int scif_vwriteto(scif_epd_t epd
, void *addr
, size_t len
,
1920 off_t roffset
, int flags
)
1924 dev_dbg(scif_info
.mdev
.this_device
,
1925 "SCIFAPI vwriteto: ep %p addr %p len 0x%lx roffset 0x%lx flags 0x%x\n",
1926 epd
, addr
, len
, roffset
, flags
);
1927 if (scif_unaligned((off_t __force
)addr
, roffset
)) {
1928 if (len
> SCIF_MAX_UNALIGNED_BUF_SIZE
)
1929 flags
&= ~SCIF_RMA_USECACHE
;
1931 while (len
> SCIF_MAX_UNALIGNED_BUF_SIZE
) {
1932 err
= scif_rma_copy(epd
, 0, (u64
)addr
,
1933 SCIF_MAX_UNALIGNED_BUF_SIZE
,
1935 SCIF_LOCAL_TO_REMOTE
, false);
1938 addr
+= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1939 roffset
+= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1940 len
-= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1943 err
= scif_rma_copy(epd
, 0, (u64
)addr
, len
,
1944 roffset
, flags
, SCIF_LOCAL_TO_REMOTE
, true);
1948 EXPORT_SYMBOL_GPL(scif_vwriteto
);