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
,
118 struct scif_endpt
*ep
,
121 struct list_head
*item
, *tmp
;
122 struct scif_window
*window
;
123 u64 start_va
, end_va
;
124 u64 end
= start
+ len
;
129 list_for_each_safe(item
, tmp
, &mmn
->tc_reg_list
) {
130 window
= list_entry(item
, struct scif_window
, list
);
131 ep
= (struct scif_endpt
*)window
->ep
;
134 start_va
= window
->va_for_temp
;
135 end_va
= start_va
+ (window
->nr_pages
<< PAGE_SHIFT
);
136 if (start
< start_va
&& end
<= start_va
)
140 __scif_rma_destroy_tcw_helper(window
);
144 static void scif_rma_destroy_tcw(struct scif_mmu_notif
*mmn
, u64 start
, u64 len
)
146 struct scif_endpt
*ep
= mmn
->ep
;
148 spin_lock(&ep
->rma_info
.tc_lock
);
149 __scif_rma_destroy_tcw(mmn
, ep
, start
, len
);
150 spin_unlock(&ep
->rma_info
.tc_lock
);
153 static void scif_rma_destroy_tcw_ep(struct scif_endpt
*ep
)
155 struct list_head
*item
, *tmp
;
156 struct scif_mmu_notif
*mmn
;
158 list_for_each_safe(item
, tmp
, &ep
->rma_info
.mmn_list
) {
159 mmn
= list_entry(item
, struct scif_mmu_notif
, list
);
160 scif_rma_destroy_tcw(mmn
, 0, ULONG_MAX
);
164 static void __scif_rma_destroy_tcw_ep(struct scif_endpt
*ep
)
166 struct list_head
*item
, *tmp
;
167 struct scif_mmu_notif
*mmn
;
169 spin_lock(&ep
->rma_info
.tc_lock
);
170 list_for_each_safe(item
, tmp
, &ep
->rma_info
.mmn_list
) {
171 mmn
= list_entry(item
, struct scif_mmu_notif
, list
);
172 __scif_rma_destroy_tcw(mmn
, ep
, 0, ULONG_MAX
);
174 spin_unlock(&ep
->rma_info
.tc_lock
);
177 static bool scif_rma_tc_can_cache(struct scif_endpt
*ep
, size_t cur_bytes
)
179 if ((cur_bytes
>> PAGE_SHIFT
) > scif_info
.rma_tc_limit
)
181 if ((atomic_read(&ep
->rma_info
.tcw_total_pages
)
182 + (cur_bytes
>> PAGE_SHIFT
)) >
183 scif_info
.rma_tc_limit
) {
184 dev_info(scif_info
.mdev
.this_device
,
185 "%s %d total=%d, current=%zu reached max\n",
187 atomic_read(&ep
->rma_info
.tcw_total_pages
),
188 (1 + (cur_bytes
>> PAGE_SHIFT
)));
189 scif_rma_destroy_tcw_invalid();
190 __scif_rma_destroy_tcw_ep(ep
);
195 static void scif_mmu_notifier_release(struct mmu_notifier
*mn
,
196 struct mm_struct
*mm
)
198 struct scif_mmu_notif
*mmn
;
200 mmn
= container_of(mn
, struct scif_mmu_notif
, ep_mmu_notifier
);
201 scif_rma_destroy_tcw(mmn
, 0, ULONG_MAX
);
202 schedule_work(&scif_info
.misc_work
);
205 static void scif_mmu_notifier_invalidate_page(struct mmu_notifier
*mn
,
206 struct mm_struct
*mm
,
207 unsigned long address
)
209 struct scif_mmu_notif
*mmn
;
211 mmn
= container_of(mn
, struct scif_mmu_notif
, ep_mmu_notifier
);
212 scif_rma_destroy_tcw(mmn
, address
, PAGE_SIZE
);
215 static void scif_mmu_notifier_invalidate_range_start(struct mmu_notifier
*mn
,
216 struct mm_struct
*mm
,
220 struct scif_mmu_notif
*mmn
;
222 mmn
= container_of(mn
, struct scif_mmu_notif
, ep_mmu_notifier
);
223 scif_rma_destroy_tcw(mmn
, start
, end
- start
);
226 static void scif_mmu_notifier_invalidate_range_end(struct mmu_notifier
*mn
,
227 struct mm_struct
*mm
,
232 * Nothing to do here, everything needed was done in
233 * invalidate_range_start.
237 static const struct mmu_notifier_ops scif_mmu_notifier_ops
= {
238 .release
= scif_mmu_notifier_release
,
239 .clear_flush_young
= NULL
,
240 .invalidate_page
= scif_mmu_notifier_invalidate_page
,
241 .invalidate_range_start
= scif_mmu_notifier_invalidate_range_start
,
242 .invalidate_range_end
= scif_mmu_notifier_invalidate_range_end
};
244 static void scif_ep_unregister_mmu_notifier(struct scif_endpt
*ep
)
246 struct scif_endpt_rma_info
*rma
= &ep
->rma_info
;
247 struct scif_mmu_notif
*mmn
= NULL
;
248 struct list_head
*item
, *tmp
;
250 mutex_lock(&ep
->rma_info
.mmn_lock
);
251 list_for_each_safe(item
, tmp
, &rma
->mmn_list
) {
252 mmn
= list_entry(item
, struct scif_mmu_notif
, list
);
253 mmu_notifier_unregister(&mmn
->ep_mmu_notifier
, mmn
->mm
);
257 mutex_unlock(&ep
->rma_info
.mmn_lock
);
260 static void scif_init_mmu_notifier(struct scif_mmu_notif
*mmn
,
261 struct mm_struct
*mm
, struct scif_endpt
*ep
)
265 mmn
->ep_mmu_notifier
.ops
= &scif_mmu_notifier_ops
;
266 INIT_LIST_HEAD(&mmn
->list
);
267 INIT_LIST_HEAD(&mmn
->tc_reg_list
);
270 static struct scif_mmu_notif
*
271 scif_find_mmu_notifier(struct mm_struct
*mm
, struct scif_endpt_rma_info
*rma
)
273 struct scif_mmu_notif
*mmn
;
275 list_for_each_entry(mmn
, &rma
->mmn_list
, list
)
281 static struct scif_mmu_notif
*
282 scif_add_mmu_notifier(struct mm_struct
*mm
, struct scif_endpt
*ep
)
284 struct scif_mmu_notif
*mmn
285 = kzalloc(sizeof(*mmn
), GFP_KERNEL
);
288 return ERR_PTR(-ENOMEM
);
290 scif_init_mmu_notifier(mmn
, current
->mm
, ep
);
291 if (mmu_notifier_register(&mmn
->ep_mmu_notifier
, current
->mm
)) {
293 return ERR_PTR(-EBUSY
);
295 list_add(&mmn
->list
, &ep
->rma_info
.mmn_list
);
300 * Called from the misc thread to destroy temporary cached windows and
301 * unregister the MMU notifier for the SCIF endpoint.
303 void scif_mmu_notif_handler(struct work_struct
*work
)
305 struct list_head
*pos
, *tmpq
;
306 struct scif_endpt
*ep
;
308 scif_rma_destroy_tcw_invalid();
309 spin_lock(&scif_info
.rmalock
);
310 list_for_each_safe(pos
, tmpq
, &scif_info
.mmu_notif_cleanup
) {
311 ep
= list_entry(pos
, struct scif_endpt
, mmu_list
);
312 list_del(&ep
->mmu_list
);
313 spin_unlock(&scif_info
.rmalock
);
314 scif_rma_destroy_tcw_ep(ep
);
315 scif_ep_unregister_mmu_notifier(ep
);
318 spin_unlock(&scif_info
.rmalock
);
321 static bool scif_is_set_reg_cache(int flags
)
323 return !!(flags
& SCIF_RMA_USECACHE
);
326 static struct scif_mmu_notif
*
327 scif_find_mmu_notifier(struct mm_struct
*mm
,
328 struct scif_endpt_rma_info
*rma
)
333 static struct scif_mmu_notif
*
334 scif_add_mmu_notifier(struct mm_struct
*mm
, struct scif_endpt
*ep
)
339 void scif_mmu_notif_handler(struct work_struct
*work
)
343 static bool scif_is_set_reg_cache(int flags
)
348 static bool scif_rma_tc_can_cache(struct scif_endpt
*ep
, size_t cur_bytes
)
355 * scif_register_temp:
356 * @epd: End Point Descriptor.
357 * @addr: virtual address to/from which to copy
358 * @len: length of range to copy
359 * @out_offset: computed offset returned by reference.
360 * @out_window: allocated registered window returned by reference.
362 * Create a temporary registered window. The peer will not know about this
363 * window. This API is used for scif_vreadfrom()/scif_vwriteto() API's.
366 scif_register_temp(scif_epd_t epd
, unsigned long addr
, size_t len
, int prot
,
367 off_t
*out_offset
, struct scif_window
**out_window
)
369 struct scif_endpt
*ep
= (struct scif_endpt
*)epd
;
371 scif_pinned_pages_t pinned_pages
;
374 aligned_len
= ALIGN(len
, PAGE_SIZE
);
376 err
= __scif_pin_pages((void *)(addr
& PAGE_MASK
),
377 aligned_len
, &prot
, 0, &pinned_pages
);
381 pinned_pages
->prot
= prot
;
383 /* Compute the offset for this registration */
384 err
= scif_get_window_offset(ep
, 0, 0,
385 aligned_len
>> PAGE_SHIFT
,
390 /* Allocate and prepare self registration window */
391 *out_window
= scif_create_window(ep
, aligned_len
>> PAGE_SHIFT
,
394 scif_free_window_offset(ep
, NULL
, *out_offset
);
399 (*out_window
)->pinned_pages
= pinned_pages
;
400 (*out_window
)->nr_pages
= pinned_pages
->nr_pages
;
401 (*out_window
)->prot
= pinned_pages
->prot
;
403 (*out_window
)->va_for_temp
= addr
& PAGE_MASK
;
404 err
= scif_map_window(ep
->remote_dev
, *out_window
);
406 /* Something went wrong! Rollback */
407 scif_destroy_window(ep
, *out_window
);
410 *out_offset
|= (addr
- (*out_window
)->va_for_temp
);
415 dev_err(&ep
->remote_dev
->sdev
->dev
,
416 "%s %d err %d\n", __func__
, __LINE__
, err
);
417 scif_unpin_pages(pinned_pages
);
421 #define SCIF_DMA_TO (3 * HZ)
424 * scif_sync_dma - Program a DMA without an interrupt descriptor
426 * @dev - The address of the pointer to the device instance used
427 * for DMA registration.
428 * @chan - DMA channel to be used.
429 * @sync_wait: Wait for DMA to complete?
431 * Return 0 on success and -errno on error.
433 static int scif_sync_dma(struct scif_hw_dev
*sdev
, struct dma_chan
*chan
,
437 struct dma_async_tx_descriptor
*tx
= NULL
;
438 enum dma_ctrl_flags flags
= DMA_PREP_FENCE
;
440 struct dma_device
*ddev
;
444 dev_err(&sdev
->dev
, "%s %d err %d\n",
445 __func__
, __LINE__
, err
);
450 tx
= ddev
->device_prep_dma_memcpy(chan
, 0, 0, 0, flags
);
453 dev_err(&sdev
->dev
, "%s %d err %d\n",
454 __func__
, __LINE__
, err
);
457 cookie
= tx
->tx_submit(tx
);
459 if (dma_submit_error(cookie
)) {
461 dev_err(&sdev
->dev
, "%s %d err %d\n",
462 __func__
, __LINE__
, err
);
466 dma_async_issue_pending(chan
);
468 if (dma_sync_wait(chan
, cookie
) == DMA_COMPLETE
) {
472 dev_err(&sdev
->dev
, "%s %d err %d\n",
473 __func__
, __LINE__
, err
);
480 static void scif_dma_callback(void *arg
)
482 struct completion
*done
= (struct completion
*)arg
;
487 #define SCIF_DMA_SYNC_WAIT true
488 #define SCIF_DMA_POLL BIT(0)
489 #define SCIF_DMA_INTR BIT(1)
492 * scif_async_dma - Program a DMA with an interrupt descriptor
494 * @dev - The address of the pointer to the device instance used
495 * for DMA registration.
496 * @chan - DMA channel to be used.
497 * Return 0 on success and -errno on error.
499 static int scif_async_dma(struct scif_hw_dev
*sdev
, struct dma_chan
*chan
)
502 struct dma_device
*ddev
;
503 struct dma_async_tx_descriptor
*tx
= NULL
;
504 enum dma_ctrl_flags flags
= DMA_PREP_INTERRUPT
| DMA_PREP_FENCE
;
505 DECLARE_COMPLETION_ONSTACK(done_wait
);
507 enum dma_status status
;
511 dev_err(&sdev
->dev
, "%s %d err %d\n",
512 __func__
, __LINE__
, err
);
517 tx
= ddev
->device_prep_dma_memcpy(chan
, 0, 0, 0, flags
);
520 dev_err(&sdev
->dev
, "%s %d err %d\n",
521 __func__
, __LINE__
, err
);
524 reinit_completion(&done_wait
);
525 tx
->callback
= scif_dma_callback
;
526 tx
->callback_param
= &done_wait
;
527 cookie
= tx
->tx_submit(tx
);
529 if (dma_submit_error(cookie
)) {
531 dev_err(&sdev
->dev
, "%s %d err %d\n",
532 __func__
, __LINE__
, err
);
535 dma_async_issue_pending(chan
);
537 err
= wait_for_completion_timeout(&done_wait
, SCIF_DMA_TO
);
540 dev_err(&sdev
->dev
, "%s %d err %d\n",
541 __func__
, __LINE__
, err
);
545 status
= dma_async_is_tx_complete(chan
, cookie
, NULL
, NULL
);
546 if (status
!= DMA_COMPLETE
) {
548 dev_err(&sdev
->dev
, "%s %d err %d\n",
549 __func__
, __LINE__
, err
);
557 * scif_drain_dma_poll - Drain all outstanding DMA operations for a particular
558 * DMA channel via polling.
560 * @sdev - The SCIF device
561 * @chan - DMA channel
562 * Return 0 on success and -errno on error.
564 static int scif_drain_dma_poll(struct scif_hw_dev
*sdev
, struct dma_chan
*chan
)
568 return scif_sync_dma(sdev
, chan
, SCIF_DMA_SYNC_WAIT
);
572 * scif_drain_dma_intr - Drain all outstanding DMA operations for a particular
573 * DMA channel via interrupt based blocking wait.
575 * @sdev - The SCIF device
576 * @chan - DMA channel
577 * Return 0 on success and -errno on error.
579 int scif_drain_dma_intr(struct scif_hw_dev
*sdev
, struct dma_chan
*chan
)
583 return scif_async_dma(sdev
, chan
);
587 * scif_rma_destroy_windows:
589 * This routine destroys all windows queued for cleanup
591 void scif_rma_destroy_windows(void)
593 struct list_head
*item
, *tmp
;
594 struct scif_window
*window
;
595 struct scif_endpt
*ep
;
596 struct dma_chan
*chan
;
600 spin_lock(&scif_info
.rmalock
);
601 list_for_each_safe(item
, tmp
, &scif_info
.rma
) {
602 window
= list_entry(item
, struct scif_window
,
604 ep
= (struct scif_endpt
*)window
->ep
;
605 chan
= ep
->rma_info
.dma_chan
;
607 list_del_init(&window
->list
);
608 spin_unlock(&scif_info
.rmalock
);
609 if (!chan
|| !scifdev_alive(ep
) ||
610 !scif_drain_dma_intr(ep
->remote_dev
->sdev
,
611 ep
->rma_info
.dma_chan
))
612 /* Remove window from global list */
613 window
->unreg_state
= OP_COMPLETED
;
615 dev_warn(&ep
->remote_dev
->sdev
->dev
,
616 "DMA engine hung?\n");
617 if (window
->unreg_state
== OP_COMPLETED
) {
618 if (window
->type
== SCIF_WINDOW_SELF
)
619 scif_destroy_window(ep
, window
);
621 scif_destroy_remote_window(window
);
622 atomic_dec(&ep
->rma_info
.tw_refcount
);
626 spin_unlock(&scif_info
.rmalock
);
630 * scif_rma_destroy_tcw:
632 * This routine destroys temporary cached registered windows
633 * which have been queued for cleanup.
635 void scif_rma_destroy_tcw_invalid(void)
637 struct list_head
*item
, *tmp
;
638 struct scif_window
*window
;
639 struct scif_endpt
*ep
;
640 struct dma_chan
*chan
;
644 spin_lock(&scif_info
.rmalock
);
645 list_for_each_safe(item
, tmp
, &scif_info
.rma_tc
) {
646 window
= list_entry(item
, struct scif_window
, list
);
647 ep
= (struct scif_endpt
*)window
->ep
;
648 chan
= ep
->rma_info
.dma_chan
;
649 list_del_init(&window
->list
);
650 spin_unlock(&scif_info
.rmalock
);
651 mutex_lock(&ep
->rma_info
.rma_lock
);
652 if (!chan
|| !scifdev_alive(ep
) ||
653 !scif_drain_dma_intr(ep
->remote_dev
->sdev
,
654 ep
->rma_info
.dma_chan
)) {
655 atomic_sub(window
->nr_pages
,
656 &ep
->rma_info
.tcw_total_pages
);
657 scif_destroy_window(ep
, window
);
658 atomic_dec(&ep
->rma_info
.tcw_refcount
);
660 dev_warn(&ep
->remote_dev
->sdev
->dev
,
661 "DMA engine hung?\n");
663 mutex_unlock(&ep
->rma_info
.rma_lock
);
666 spin_unlock(&scif_info
.rmalock
);
670 void *_get_local_va(off_t off
, struct scif_window
*window
, size_t len
)
672 int page_nr
= (off
- window
->offset
) >> PAGE_SHIFT
;
673 off_t page_off
= off
& ~PAGE_MASK
;
676 if (window
->type
== SCIF_WINDOW_SELF
) {
677 struct page
**pages
= window
->pinned_pages
->pages
;
679 va
= page_address(pages
[page_nr
]) + page_off
;
685 void *ioremap_remote(off_t off
, struct scif_window
*window
,
686 size_t len
, struct scif_dev
*dev
,
687 struct scif_window_iter
*iter
)
689 dma_addr_t phys
= scif_off_to_dma_addr(window
, off
, NULL
, iter
);
692 * If the DMA address is not card relative then we need the DMA
693 * addresses to be an offset into the bar. The aperture base was already
694 * added so subtract it here since scif_ioremap is going to add it again
696 if (!scifdev_self(dev
) && window
->type
== SCIF_WINDOW_PEER
&&
697 dev
->sdev
->aper
&& !dev
->sdev
->card_rel_da
)
698 phys
= phys
- dev
->sdev
->aper
->pa
;
699 return scif_ioremap(phys
, len
, dev
);
703 iounmap_remote(void *virt
, size_t size
, struct scif_copy_work
*work
)
705 scif_iounmap(virt
, size
, work
->remote_dev
);
709 * Takes care of ordering issue caused by
710 * 1. Hardware: Only in the case of cpu copy from mgmt node to card
711 * because of WC memory.
712 * 2. Software: If memcpy reorders copy instructions for optimization.
713 * This could happen at both mgmt node and card.
716 scif_ordered_memcpy_toio(char *dst
, const char *src
, size_t count
)
721 memcpy_toio((void __iomem __force
*)dst
, src
, --count
);
722 /* Order the last byte with the previous stores */
724 *(dst
+ count
) = *(src
+ count
);
727 static inline void scif_unaligned_cpy_toio(char *dst
, const char *src
,
728 size_t count
, bool ordered
)
731 scif_ordered_memcpy_toio(dst
, src
, count
);
733 memcpy_toio((void __iomem __force
*)dst
, src
, count
);
737 void scif_ordered_memcpy_fromio(char *dst
, const char *src
, size_t count
)
742 memcpy_fromio(dst
, (void __iomem __force
*)src
, --count
);
743 /* Order the last byte with the previous loads */
745 *(dst
+ count
) = *(src
+ count
);
748 static inline void scif_unaligned_cpy_fromio(char *dst
, const char *src
,
749 size_t count
, bool ordered
)
752 scif_ordered_memcpy_fromio(dst
, src
, count
);
754 memcpy_fromio(dst
, (void __iomem __force
*)src
, count
);
757 #define SCIF_RMA_ERROR_CODE (~(dma_addr_t)0x0)
760 * scif_off_to_dma_addr:
761 * Obtain the dma_addr given the window and the offset.
762 * @window: Registered window.
763 * @off: Window offset.
764 * @nr_bytes: Return the number of contiguous bytes till next DMA addr index.
765 * @index: Return the index of the dma_addr array found.
766 * @start_off: start offset of index of the dma addr array found.
767 * The nr_bytes provides the callee an estimate of the maximum possible
768 * DMA xfer possible while the index/start_off provide faster lookups
769 * for the next iteration.
771 dma_addr_t
scif_off_to_dma_addr(struct scif_window
*window
, s64 off
,
772 size_t *nr_bytes
, struct scif_window_iter
*iter
)
778 if (window
->nr_pages
== window
->nr_contig_chunks
) {
779 page_nr
= (off
- window
->offset
) >> PAGE_SHIFT
;
780 page_off
= off
& ~PAGE_MASK
;
783 *nr_bytes
= PAGE_SIZE
- page_off
;
784 return window
->dma_addr
[page_nr
] | page_off
;
788 start
= iter
->offset
;
791 start
= window
->offset
;
793 for (; i
< window
->nr_contig_chunks
; i
++) {
794 end
= start
+ (window
->num_pages
[i
] << PAGE_SHIFT
);
795 if (off
>= start
&& off
< end
) {
798 iter
->offset
= start
;
801 *nr_bytes
= end
- off
;
802 return (window
->dma_addr
[i
] + (off
- start
));
804 start
+= (window
->num_pages
[i
] << PAGE_SHIFT
);
806 dev_err(scif_info
.mdev
.this_device
,
807 "%s %d BUG. Addr not found? window %p off 0x%llx\n",
808 __func__
, __LINE__
, window
, off
);
809 return SCIF_RMA_ERROR_CODE
;
813 * Copy between rma window and temporary buffer
815 static void scif_rma_local_cpu_copy(s64 offset
, struct scif_window
*window
,
816 u8
*temp
, size_t rem_len
, bool to_temp
)
823 offset_in_page
= offset
& ~PAGE_MASK
;
824 loop_len
= PAGE_SIZE
- offset_in_page
;
826 if (rem_len
< loop_len
)
829 window_virt
= _get_local_va(offset
, window
, loop_len
);
833 memcpy(temp
, window_virt
, loop_len
);
835 memcpy(window_virt
, temp
, loop_len
);
841 end_offset
= window
->offset
+
842 (window
->nr_pages
<< PAGE_SHIFT
);
844 if (offset
== end_offset
) {
845 window
= list_next_entry(window
, list
);
846 end_offset
= window
->offset
+
847 (window
->nr_pages
<< PAGE_SHIFT
);
849 loop_len
= min(PAGE_SIZE
, rem_len
);
850 window_virt
= _get_local_va(offset
, window
, loop_len
);
854 memcpy(temp
, window_virt
, loop_len
);
856 memcpy(window_virt
, temp
, loop_len
);
864 * scif_rma_completion_cb:
867 * RMA interrupt completion callback.
869 static void scif_rma_completion_cb(void *data
)
871 struct scif_dma_comp_cb
*comp_cb
= data
;
873 /* Free DMA Completion CB. */
874 if (comp_cb
->dst_window
)
875 scif_rma_local_cpu_copy(comp_cb
->dst_offset
,
878 comp_cb
->header_padding
,
879 comp_cb
->len
, false);
880 scif_unmap_single(comp_cb
->temp_phys
, comp_cb
->sdev
,
881 SCIF_KMEM_UNALIGNED_BUF_SIZE
);
882 if (comp_cb
->is_cache
)
883 kmem_cache_free(unaligned_cache
,
884 comp_cb
->temp_buf_to_free
);
886 kfree(comp_cb
->temp_buf_to_free
);
889 /* Copies between temporary buffer and offsets provided in work */
891 scif_rma_list_dma_copy_unaligned(struct scif_copy_work
*work
,
892 u8
*temp
, struct dma_chan
*chan
,
895 struct scif_dma_comp_cb
*comp_cb
= work
->comp_cb
;
896 dma_addr_t window_dma_addr
, temp_dma_addr
;
897 dma_addr_t temp_phys
= comp_cb
->temp_phys
;
898 size_t loop_len
, nr_contig_bytes
= 0, remaining_len
= work
->len
;
899 int offset_in_ca
, ret
= 0;
900 s64 end_offset
, offset
;
901 struct scif_window
*window
;
902 void *window_virt_addr
;
904 struct dma_async_tx_descriptor
*tx
;
905 struct dma_device
*dev
= chan
->device
;
909 offset
= work
->dst_offset
;
910 window
= work
->dst_window
;
912 offset
= work
->src_offset
;
913 window
= work
->src_window
;
916 offset_in_ca
= offset
& (L1_CACHE_BYTES
- 1);
918 loop_len
= L1_CACHE_BYTES
- offset_in_ca
;
919 loop_len
= min(loop_len
, remaining_len
);
920 window_virt_addr
= ioremap_remote(offset
, window
,
924 if (!window_virt_addr
)
927 scif_unaligned_cpy_toio(window_virt_addr
, temp
,
930 !(remaining_len
- loop_len
));
932 scif_unaligned_cpy_fromio(temp
, window_virt_addr
,
933 loop_len
, work
->ordered
&&
934 !(remaining_len
- loop_len
));
935 iounmap_remote(window_virt_addr
, loop_len
, work
);
939 temp_phys
+= loop_len
;
940 remaining_len
-= loop_len
;
943 offset_in_ca
= offset
& ~PAGE_MASK
;
944 end_offset
= window
->offset
+
945 (window
->nr_pages
<< PAGE_SHIFT
);
947 tail_len
= remaining_len
& (L1_CACHE_BYTES
- 1);
948 remaining_len
-= tail_len
;
949 while (remaining_len
) {
950 if (offset
== end_offset
) {
951 window
= list_next_entry(window
, list
);
952 end_offset
= window
->offset
+
953 (window
->nr_pages
<< PAGE_SHIFT
);
955 if (scif_is_mgmt_node())
956 temp_dma_addr
= temp_phys
;
958 /* Fix if we ever enable IOMMU on the card */
959 temp_dma_addr
= (dma_addr_t
)virt_to_phys(temp
);
960 window_dma_addr
= scif_off_to_dma_addr(window
, offset
,
963 loop_len
= min(nr_contig_bytes
, remaining_len
);
965 if (work
->ordered
&& !tail_len
&&
966 !(remaining_len
- loop_len
) &&
967 loop_len
!= L1_CACHE_BYTES
) {
969 * Break up the last chunk of the transfer into
970 * two steps. if there is no tail to guarantee
971 * DMA ordering. SCIF_DMA_POLLING inserts
972 * a status update descriptor in step 1 which
973 * acts as a double sided synchronization fence
974 * for the DMA engine to ensure that the last
975 * cache line in step 2 is updated last.
977 /* Step 1) DMA: Body Length - L1_CACHE_BYTES. */
979 dev
->device_prep_dma_memcpy(chan
,
989 cookie
= tx
->tx_submit(tx
);
990 if (dma_submit_error(cookie
)) {
994 dma_async_issue_pending(chan
);
995 offset
+= (loop_len
- L1_CACHE_BYTES
);
996 temp_dma_addr
+= (loop_len
- L1_CACHE_BYTES
);
997 window_dma_addr
+= (loop_len
- L1_CACHE_BYTES
);
998 remaining_len
-= (loop_len
- L1_CACHE_BYTES
);
999 loop_len
= remaining_len
;
1001 /* Step 2) DMA: L1_CACHE_BYTES */
1003 dev
->device_prep_dma_memcpy(chan
,
1011 cookie
= tx
->tx_submit(tx
);
1012 if (dma_submit_error(cookie
)) {
1016 dma_async_issue_pending(chan
);
1019 dev
->device_prep_dma_memcpy(chan
,
1027 cookie
= tx
->tx_submit(tx
);
1028 if (dma_submit_error(cookie
)) {
1032 dma_async_issue_pending(chan
);
1035 tx
= dev
->device_prep_dma_memcpy(chan
, temp_dma_addr
,
1036 window_dma_addr
, loop_len
, 0);
1041 cookie
= tx
->tx_submit(tx
);
1042 if (dma_submit_error(cookie
)) {
1046 dma_async_issue_pending(chan
);
1052 temp_phys
+= loop_len
;
1053 remaining_len
-= loop_len
;
1057 if (offset
== end_offset
) {
1058 window
= list_next_entry(window
, list
);
1059 end_offset
= window
->offset
+
1060 (window
->nr_pages
<< PAGE_SHIFT
);
1062 window_virt_addr
= ioremap_remote(offset
, window
, tail_len
,
1065 if (!window_virt_addr
)
1068 * The CPU copy for the tail bytes must be initiated only once
1069 * previous DMA transfers for this endpoint have completed
1070 * to guarantee ordering.
1072 if (work
->ordered
) {
1073 struct scif_dev
*rdev
= work
->remote_dev
;
1075 ret
= scif_drain_dma_intr(rdev
->sdev
, chan
);
1080 scif_unaligned_cpy_toio(window_virt_addr
, temp
,
1081 tail_len
, work
->ordered
);
1083 scif_unaligned_cpy_fromio(temp
, window_virt_addr
,
1084 tail_len
, work
->ordered
);
1085 iounmap_remote(window_virt_addr
, tail_len
, work
);
1087 tx
= dev
->device_prep_dma_memcpy(chan
, 0, 0, 0, DMA_PREP_INTERRUPT
);
1092 tx
->callback
= &scif_rma_completion_cb
;
1093 tx
->callback_param
= comp_cb
;
1094 cookie
= tx
->tx_submit(tx
);
1096 if (dma_submit_error(cookie
)) {
1100 dma_async_issue_pending(chan
);
1103 dev_err(scif_info
.mdev
.this_device
,
1104 "%s %d Desc Prog Failed ret %d\n",
1105 __func__
, __LINE__
, ret
);
1110 * _scif_rma_list_dma_copy_aligned:
1112 * Traverse all the windows and perform DMA copy.
1114 static int _scif_rma_list_dma_copy_aligned(struct scif_copy_work
*work
,
1115 struct dma_chan
*chan
)
1117 dma_addr_t src_dma_addr
, dst_dma_addr
;
1118 size_t loop_len
, remaining_len
, src_contig_bytes
= 0;
1119 size_t dst_contig_bytes
= 0;
1120 struct scif_window_iter src_win_iter
;
1121 struct scif_window_iter dst_win_iter
;
1122 s64 end_src_offset
, end_dst_offset
;
1123 struct scif_window
*src_window
= work
->src_window
;
1124 struct scif_window
*dst_window
= work
->dst_window
;
1125 s64 src_offset
= work
->src_offset
, dst_offset
= work
->dst_offset
;
1127 struct dma_async_tx_descriptor
*tx
;
1128 struct dma_device
*dev
= chan
->device
;
1129 dma_cookie_t cookie
;
1131 remaining_len
= work
->len
;
1133 scif_init_window_iter(src_window
, &src_win_iter
);
1134 scif_init_window_iter(dst_window
, &dst_win_iter
);
1135 end_src_offset
= src_window
->offset
+
1136 (src_window
->nr_pages
<< PAGE_SHIFT
);
1137 end_dst_offset
= dst_window
->offset
+
1138 (dst_window
->nr_pages
<< PAGE_SHIFT
);
1139 while (remaining_len
) {
1140 if (src_offset
== end_src_offset
) {
1141 src_window
= list_next_entry(src_window
, list
);
1142 end_src_offset
= src_window
->offset
+
1143 (src_window
->nr_pages
<< PAGE_SHIFT
);
1144 scif_init_window_iter(src_window
, &src_win_iter
);
1146 if (dst_offset
== end_dst_offset
) {
1147 dst_window
= list_next_entry(dst_window
, list
);
1148 end_dst_offset
= dst_window
->offset
+
1149 (dst_window
->nr_pages
<< PAGE_SHIFT
);
1150 scif_init_window_iter(dst_window
, &dst_win_iter
);
1153 /* compute dma addresses for transfer */
1154 src_dma_addr
= scif_off_to_dma_addr(src_window
, src_offset
,
1157 dst_dma_addr
= scif_off_to_dma_addr(dst_window
, dst_offset
,
1160 loop_len
= min(src_contig_bytes
, dst_contig_bytes
);
1161 loop_len
= min(loop_len
, remaining_len
);
1162 if (work
->ordered
&& !(remaining_len
- loop_len
)) {
1164 * Break up the last chunk of the transfer into two
1165 * steps to ensure that the last byte in step 2 is
1168 /* Step 1) DMA: Body Length - 1 */
1169 tx
= dev
->device_prep_dma_memcpy(chan
, dst_dma_addr
,
1177 cookie
= tx
->tx_submit(tx
);
1178 if (dma_submit_error(cookie
)) {
1182 src_offset
+= (loop_len
- 1);
1183 dst_offset
+= (loop_len
- 1);
1184 src_dma_addr
+= (loop_len
- 1);
1185 dst_dma_addr
+= (loop_len
- 1);
1186 remaining_len
-= (loop_len
- 1);
1187 loop_len
= remaining_len
;
1189 /* Step 2) DMA: 1 BYTES */
1190 tx
= dev
->device_prep_dma_memcpy(chan
, dst_dma_addr
,
1191 src_dma_addr
, loop_len
, 0);
1196 cookie
= tx
->tx_submit(tx
);
1197 if (dma_submit_error(cookie
)) {
1201 dma_async_issue_pending(chan
);
1203 tx
= dev
->device_prep_dma_memcpy(chan
, dst_dma_addr
,
1204 src_dma_addr
, loop_len
, 0);
1209 cookie
= tx
->tx_submit(tx
);
1210 if (dma_submit_error(cookie
)) {
1215 src_offset
+= loop_len
;
1216 dst_offset
+= loop_len
;
1217 remaining_len
-= loop_len
;
1221 dev_err(scif_info
.mdev
.this_device
,
1222 "%s %d Desc Prog Failed ret %d\n",
1223 __func__
, __LINE__
, ret
);
1228 * scif_rma_list_dma_copy_aligned:
1230 * Traverse all the windows and perform DMA copy.
1232 static int scif_rma_list_dma_copy_aligned(struct scif_copy_work
*work
,
1233 struct dma_chan
*chan
)
1235 dma_addr_t src_dma_addr
, dst_dma_addr
;
1236 size_t loop_len
, remaining_len
, tail_len
, src_contig_bytes
= 0;
1237 size_t dst_contig_bytes
= 0;
1239 s64 end_src_offset
, end_dst_offset
;
1240 struct scif_window_iter src_win_iter
;
1241 struct scif_window_iter dst_win_iter
;
1242 void *src_virt
, *dst_virt
;
1243 struct scif_window
*src_window
= work
->src_window
;
1244 struct scif_window
*dst_window
= work
->dst_window
;
1245 s64 src_offset
= work
->src_offset
, dst_offset
= work
->dst_offset
;
1247 struct dma_async_tx_descriptor
*tx
;
1248 struct dma_device
*dev
= chan
->device
;
1249 dma_cookie_t cookie
;
1251 remaining_len
= work
->len
;
1252 scif_init_window_iter(src_window
, &src_win_iter
);
1253 scif_init_window_iter(dst_window
, &dst_win_iter
);
1255 src_cache_off
= src_offset
& (L1_CACHE_BYTES
- 1);
1256 if (src_cache_off
!= 0) {
1258 loop_len
= L1_CACHE_BYTES
- src_cache_off
;
1259 loop_len
= min(loop_len
, remaining_len
);
1260 src_dma_addr
= __scif_off_to_dma_addr(src_window
, src_offset
);
1261 dst_dma_addr
= __scif_off_to_dma_addr(dst_window
, dst_offset
);
1262 if (src_window
->type
== SCIF_WINDOW_SELF
)
1263 src_virt
= _get_local_va(src_offset
, src_window
,
1266 src_virt
= ioremap_remote(src_offset
, src_window
,
1268 work
->remote_dev
, NULL
);
1271 if (dst_window
->type
== SCIF_WINDOW_SELF
)
1272 dst_virt
= _get_local_va(dst_offset
, dst_window
,
1275 dst_virt
= ioremap_remote(dst_offset
, dst_window
,
1277 work
->remote_dev
, NULL
);
1279 if (src_window
->type
!= SCIF_WINDOW_SELF
)
1280 iounmap_remote(src_virt
, loop_len
, work
);
1283 if (src_window
->type
== SCIF_WINDOW_SELF
)
1284 scif_unaligned_cpy_toio(dst_virt
, src_virt
, loop_len
,
1285 remaining_len
== loop_len
?
1286 work
->ordered
: false);
1288 scif_unaligned_cpy_fromio(dst_virt
, src_virt
, loop_len
,
1289 remaining_len
== loop_len
?
1290 work
->ordered
: false);
1291 if (src_window
->type
!= SCIF_WINDOW_SELF
)
1292 iounmap_remote(src_virt
, loop_len
, work
);
1293 if (dst_window
->type
!= SCIF_WINDOW_SELF
)
1294 iounmap_remote(dst_virt
, loop_len
, work
);
1295 src_offset
+= loop_len
;
1296 dst_offset
+= loop_len
;
1297 remaining_len
-= loop_len
;
1300 end_src_offset
= src_window
->offset
+
1301 (src_window
->nr_pages
<< PAGE_SHIFT
);
1302 end_dst_offset
= dst_window
->offset
+
1303 (dst_window
->nr_pages
<< PAGE_SHIFT
);
1304 tail_len
= remaining_len
& (L1_CACHE_BYTES
- 1);
1305 remaining_len
-= tail_len
;
1306 while (remaining_len
) {
1307 if (src_offset
== end_src_offset
) {
1308 src_window
= list_next_entry(src_window
, list
);
1309 end_src_offset
= src_window
->offset
+
1310 (src_window
->nr_pages
<< PAGE_SHIFT
);
1311 scif_init_window_iter(src_window
, &src_win_iter
);
1313 if (dst_offset
== end_dst_offset
) {
1314 dst_window
= list_next_entry(dst_window
, list
);
1315 end_dst_offset
= dst_window
->offset
+
1316 (dst_window
->nr_pages
<< PAGE_SHIFT
);
1317 scif_init_window_iter(dst_window
, &dst_win_iter
);
1320 /* compute dma addresses for transfer */
1321 src_dma_addr
= scif_off_to_dma_addr(src_window
, src_offset
,
1324 dst_dma_addr
= scif_off_to_dma_addr(dst_window
, dst_offset
,
1327 loop_len
= min(src_contig_bytes
, dst_contig_bytes
);
1328 loop_len
= min(loop_len
, remaining_len
);
1329 if (work
->ordered
&& !tail_len
&&
1330 !(remaining_len
- loop_len
)) {
1332 * Break up the last chunk of the transfer into two
1333 * steps. if there is no tail to gurantee DMA ordering.
1334 * Passing SCIF_DMA_POLLING inserts a status update
1335 * descriptor in step 1 which acts as a double sided
1336 * synchronization fence for the DMA engine to ensure
1337 * that the last cache line in step 2 is updated last.
1339 /* Step 1) DMA: Body Length - L1_CACHE_BYTES. */
1340 tx
= dev
->device_prep_dma_memcpy(chan
, dst_dma_addr
,
1349 cookie
= tx
->tx_submit(tx
);
1350 if (dma_submit_error(cookie
)) {
1354 dma_async_issue_pending(chan
);
1355 src_offset
+= (loop_len
- L1_CACHE_BYTES
);
1356 dst_offset
+= (loop_len
- L1_CACHE_BYTES
);
1357 src_dma_addr
+= (loop_len
- L1_CACHE_BYTES
);
1358 dst_dma_addr
+= (loop_len
- L1_CACHE_BYTES
);
1359 remaining_len
-= (loop_len
- L1_CACHE_BYTES
);
1360 loop_len
= remaining_len
;
1362 /* Step 2) DMA: L1_CACHE_BYTES */
1363 tx
= dev
->device_prep_dma_memcpy(chan
, dst_dma_addr
,
1370 cookie
= tx
->tx_submit(tx
);
1371 if (dma_submit_error(cookie
)) {
1375 dma_async_issue_pending(chan
);
1377 tx
= dev
->device_prep_dma_memcpy(chan
, dst_dma_addr
,
1384 cookie
= tx
->tx_submit(tx
);
1385 if (dma_submit_error(cookie
)) {
1389 dma_async_issue_pending(chan
);
1391 src_offset
+= loop_len
;
1392 dst_offset
+= loop_len
;
1393 remaining_len
-= loop_len
;
1395 remaining_len
= tail_len
;
1396 if (remaining_len
) {
1397 loop_len
= remaining_len
;
1398 if (src_offset
== end_src_offset
)
1399 src_window
= list_next_entry(src_window
, list
);
1400 if (dst_offset
== end_dst_offset
)
1401 dst_window
= list_next_entry(dst_window
, list
);
1403 src_dma_addr
= __scif_off_to_dma_addr(src_window
, src_offset
);
1404 dst_dma_addr
= __scif_off_to_dma_addr(dst_window
, dst_offset
);
1406 * The CPU copy for the tail bytes must be initiated only once
1407 * previous DMA transfers for this endpoint have completed to
1408 * guarantee ordering.
1410 if (work
->ordered
) {
1411 struct scif_dev
*rdev
= work
->remote_dev
;
1413 ret
= scif_drain_dma_poll(rdev
->sdev
, chan
);
1417 if (src_window
->type
== SCIF_WINDOW_SELF
)
1418 src_virt
= _get_local_va(src_offset
, src_window
,
1421 src_virt
= ioremap_remote(src_offset
, src_window
,
1423 work
->remote_dev
, NULL
);
1427 if (dst_window
->type
== SCIF_WINDOW_SELF
)
1428 dst_virt
= _get_local_va(dst_offset
, dst_window
,
1431 dst_virt
= ioremap_remote(dst_offset
, dst_window
,
1433 work
->remote_dev
, NULL
);
1435 if (src_window
->type
!= SCIF_WINDOW_SELF
)
1436 iounmap_remote(src_virt
, loop_len
, work
);
1440 if (src_window
->type
== SCIF_WINDOW_SELF
)
1441 scif_unaligned_cpy_toio(dst_virt
, src_virt
, loop_len
,
1444 scif_unaligned_cpy_fromio(dst_virt
, src_virt
,
1445 loop_len
, work
->ordered
);
1446 if (src_window
->type
!= SCIF_WINDOW_SELF
)
1447 iounmap_remote(src_virt
, loop_len
, work
);
1449 if (dst_window
->type
!= SCIF_WINDOW_SELF
)
1450 iounmap_remote(dst_virt
, loop_len
, work
);
1451 remaining_len
-= loop_len
;
1455 dev_err(scif_info
.mdev
.this_device
,
1456 "%s %d Desc Prog Failed ret %d\n",
1457 __func__
, __LINE__
, ret
);
1462 * scif_rma_list_cpu_copy:
1464 * Traverse all the windows and perform CPU copy.
1466 static int scif_rma_list_cpu_copy(struct scif_copy_work
*work
)
1468 void *src_virt
, *dst_virt
;
1469 size_t loop_len
, remaining_len
;
1470 int src_page_off
, dst_page_off
;
1471 s64 src_offset
= work
->src_offset
, dst_offset
= work
->dst_offset
;
1472 struct scif_window
*src_window
= work
->src_window
;
1473 struct scif_window
*dst_window
= work
->dst_window
;
1474 s64 end_src_offset
, end_dst_offset
;
1476 struct scif_window_iter src_win_iter
;
1477 struct scif_window_iter dst_win_iter
;
1479 remaining_len
= work
->len
;
1481 scif_init_window_iter(src_window
, &src_win_iter
);
1482 scif_init_window_iter(dst_window
, &dst_win_iter
);
1483 while (remaining_len
) {
1484 src_page_off
= src_offset
& ~PAGE_MASK
;
1485 dst_page_off
= dst_offset
& ~PAGE_MASK
;
1486 loop_len
= min(PAGE_SIZE
-
1487 max(src_page_off
, dst_page_off
),
1490 if (src_window
->type
== SCIF_WINDOW_SELF
)
1491 src_virt
= _get_local_va(src_offset
, src_window
,
1494 src_virt
= ioremap_remote(src_offset
, src_window
,
1503 if (dst_window
->type
== SCIF_WINDOW_SELF
)
1504 dst_virt
= _get_local_va(dst_offset
, dst_window
,
1507 dst_virt
= ioremap_remote(dst_offset
, dst_window
,
1512 if (src_window
->type
== SCIF_WINDOW_PEER
)
1513 iounmap_remote(src_virt
, loop_len
, work
);
1518 if (work
->loopback
) {
1519 memcpy(dst_virt
, src_virt
, loop_len
);
1521 if (src_window
->type
== SCIF_WINDOW_SELF
)
1522 memcpy_toio((void __iomem __force
*)dst_virt
,
1523 src_virt
, loop_len
);
1525 memcpy_fromio(dst_virt
,
1526 (void __iomem __force
*)src_virt
,
1529 if (src_window
->type
== SCIF_WINDOW_PEER
)
1530 iounmap_remote(src_virt
, loop_len
, work
);
1532 if (dst_window
->type
== SCIF_WINDOW_PEER
)
1533 iounmap_remote(dst_virt
, loop_len
, work
);
1535 src_offset
+= loop_len
;
1536 dst_offset
+= loop_len
;
1537 remaining_len
-= loop_len
;
1538 if (remaining_len
) {
1539 end_src_offset
= src_window
->offset
+
1540 (src_window
->nr_pages
<< PAGE_SHIFT
);
1541 end_dst_offset
= dst_window
->offset
+
1542 (dst_window
->nr_pages
<< PAGE_SHIFT
);
1543 if (src_offset
== end_src_offset
) {
1544 src_window
= list_next_entry(src_window
, list
);
1545 scif_init_window_iter(src_window
,
1548 if (dst_offset
== end_dst_offset
) {
1549 dst_window
= list_next_entry(dst_window
, list
);
1550 scif_init_window_iter(dst_window
,
1559 static int scif_rma_list_dma_copy_wrapper(struct scif_endpt
*epd
,
1560 struct scif_copy_work
*work
,
1561 struct dma_chan
*chan
, off_t loffset
)
1563 int src_cache_off
, dst_cache_off
;
1564 s64 src_offset
= work
->src_offset
, dst_offset
= work
->dst_offset
;
1566 bool src_local
= true, dst_local
= false;
1567 struct scif_dma_comp_cb
*comp_cb
;
1568 dma_addr_t src_dma_addr
, dst_dma_addr
;
1571 if (is_dma_copy_aligned(chan
->device
, 1, 1, 1))
1572 return _scif_rma_list_dma_copy_aligned(work
, chan
);
1574 src_cache_off
= src_offset
& (L1_CACHE_BYTES
- 1);
1575 dst_cache_off
= dst_offset
& (L1_CACHE_BYTES
- 1);
1577 if (dst_cache_off
== src_cache_off
)
1578 return scif_rma_list_dma_copy_aligned(work
, chan
);
1581 return scif_rma_list_cpu_copy(work
);
1582 src_dma_addr
= __scif_off_to_dma_addr(work
->src_window
, src_offset
);
1583 dst_dma_addr
= __scif_off_to_dma_addr(work
->dst_window
, dst_offset
);
1584 src_local
= work
->src_window
->type
== SCIF_WINDOW_SELF
;
1585 dst_local
= work
->dst_window
->type
== SCIF_WINDOW_SELF
;
1587 dst_local
= dst_local
;
1588 /* Allocate dma_completion cb */
1589 comp_cb
= kzalloc(sizeof(*comp_cb
), GFP_KERNEL
);
1593 work
->comp_cb
= comp_cb
;
1594 comp_cb
->cb_cookie
= comp_cb
;
1595 comp_cb
->dma_completion_func
= &scif_rma_completion_cb
;
1597 if (work
->len
+ (L1_CACHE_BYTES
<< 1) < SCIF_KMEM_UNALIGNED_BUF_SIZE
) {
1598 comp_cb
->is_cache
= false;
1599 /* Allocate padding bytes to align to a cache line */
1600 temp
= kmalloc(work
->len
+ (L1_CACHE_BYTES
<< 1),
1604 comp_cb
->temp_buf_to_free
= temp
;
1605 /* kmalloc(..) does not guarantee cache line alignment */
1606 if (!IS_ALIGNED((u64
)temp
, L1_CACHE_BYTES
))
1607 temp
= PTR_ALIGN(temp
, L1_CACHE_BYTES
);
1609 comp_cb
->is_cache
= true;
1610 temp
= kmem_cache_alloc(unaligned_cache
, GFP_KERNEL
);
1613 comp_cb
->temp_buf_to_free
= temp
;
1617 temp
+= dst_cache_off
;
1618 scif_rma_local_cpu_copy(work
->src_offset
, work
->src_window
,
1619 temp
, work
->len
, true);
1621 comp_cb
->dst_window
= work
->dst_window
;
1622 comp_cb
->dst_offset
= work
->dst_offset
;
1623 work
->src_offset
= work
->src_offset
- src_cache_off
;
1624 comp_cb
->len
= work
->len
;
1625 work
->len
= ALIGN(work
->len
+ src_cache_off
, L1_CACHE_BYTES
);
1626 comp_cb
->header_padding
= src_cache_off
;
1628 comp_cb
->temp_buf
= temp
;
1630 err
= scif_map_single(&comp_cb
->temp_phys
, temp
,
1631 work
->remote_dev
, SCIF_KMEM_UNALIGNED_BUF_SIZE
);
1634 comp_cb
->sdev
= work
->remote_dev
;
1635 if (scif_rma_list_dma_copy_unaligned(work
, temp
, chan
, src_local
) < 0)
1638 work
->fence_type
= SCIF_DMA_INTR
;
1641 if (comp_cb
->is_cache
)
1642 kmem_cache_free(unaligned_cache
, comp_cb
->temp_buf_to_free
);
1644 kfree(comp_cb
->temp_buf_to_free
);
1653 * @epd: end point descriptor.
1654 * @loffset: offset in local registered address space to/from which to copy
1655 * @addr: user virtual address to/from which to copy
1656 * @len: length of range to copy
1657 * @roffset: offset in remote registered address space to/from which to copy
1659 * @dir: LOCAL->REMOTE or vice versa.
1660 * @last_chunk: true if this is the last chunk of a larger transfer
1662 * Validate parameters, check if src/dst registered ranges requested for copy
1663 * are valid and initiate either CPU or DMA copy.
1665 static int scif_rma_copy(scif_epd_t epd
, off_t loffset
, unsigned long addr
,
1666 size_t len
, off_t roffset
, int flags
,
1667 enum scif_rma_dir dir
, bool last_chunk
)
1669 struct scif_endpt
*ep
= (struct scif_endpt
*)epd
;
1670 struct scif_rma_req remote_req
;
1671 struct scif_rma_req req
;
1672 struct scif_window
*local_window
= NULL
;
1673 struct scif_window
*remote_window
= NULL
;
1674 struct scif_copy_work copy_work
;
1677 struct dma_chan
*chan
;
1678 struct scif_mmu_notif
*mmn
= NULL
;
1680 struct device
*spdev
;
1682 err
= scif_verify_epd(ep
);
1686 if (flags
&& !(flags
& (SCIF_RMA_USECPU
| SCIF_RMA_USECACHE
|
1687 SCIF_RMA_SYNC
| SCIF_RMA_ORDERED
)))
1690 loopback
= scifdev_self(ep
->remote_dev
) ? true : false;
1691 copy_work
.fence_type
= ((flags
& SCIF_RMA_SYNC
) && last_chunk
) ?
1693 copy_work
.ordered
= !!((flags
& SCIF_RMA_ORDERED
) && last_chunk
);
1695 /* Use CPU for Mgmt node <-> Mgmt node copies */
1696 if (loopback
&& scif_is_mgmt_node()) {
1697 flags
|= SCIF_RMA_USECPU
;
1698 copy_work
.fence_type
= 0x0;
1701 cache
= scif_is_set_reg_cache(flags
);
1703 remote_req
.out_window
= &remote_window
;
1704 remote_req
.offset
= roffset
;
1705 remote_req
.nr_bytes
= len
;
1707 * If transfer is from local to remote then the remote window
1708 * must be writeable and vice versa.
1710 remote_req
.prot
= dir
== SCIF_LOCAL_TO_REMOTE
? VM_WRITE
: VM_READ
;
1711 remote_req
.type
= SCIF_WINDOW_PARTIAL
;
1712 remote_req
.head
= &ep
->rma_info
.remote_reg_list
;
1714 spdev
= scif_get_peer_dev(ep
->remote_dev
);
1715 if (IS_ERR(spdev
)) {
1716 err
= PTR_ERR(spdev
);
1720 if (addr
&& cache
) {
1721 mutex_lock(&ep
->rma_info
.mmn_lock
);
1722 mmn
= scif_find_mmu_notifier(current
->mm
, &ep
->rma_info
);
1724 mmn
= scif_add_mmu_notifier(current
->mm
, ep
);
1725 mutex_unlock(&ep
->rma_info
.mmn_lock
);
1727 scif_put_peer_dev(spdev
);
1728 return PTR_ERR(mmn
);
1730 cache
= cache
&& !scif_rma_tc_can_cache(ep
, len
);
1732 mutex_lock(&ep
->rma_info
.rma_lock
);
1734 req
.out_window
= &local_window
;
1735 req
.nr_bytes
= ALIGN(len
+ (addr
& ~PAGE_MASK
),
1737 req
.va_for_temp
= addr
& PAGE_MASK
;
1738 req
.prot
= (dir
== SCIF_LOCAL_TO_REMOTE
?
1739 VM_READ
: VM_WRITE
| VM_READ
);
1740 /* Does a valid local window exist? */
1742 spin_lock(&ep
->rma_info
.tc_lock
);
1743 req
.head
= &mmn
->tc_reg_list
;
1744 err
= scif_query_tcw(ep
, &req
);
1745 spin_unlock(&ep
->rma_info
.tc_lock
);
1748 err
= scif_register_temp(epd
, req
.va_for_temp
,
1749 req
.nr_bytes
, req
.prot
,
1750 &loffset
, &local_window
);
1752 mutex_unlock(&ep
->rma_info
.rma_lock
);
1757 atomic_inc(&ep
->rma_info
.tcw_refcount
);
1758 atomic_add_return(local_window
->nr_pages
,
1759 &ep
->rma_info
.tcw_total_pages
);
1761 spin_lock(&ep
->rma_info
.tc_lock
);
1762 scif_insert_tcw(local_window
,
1764 spin_unlock(&ep
->rma_info
.tc_lock
);
1768 loffset
= local_window
->offset
+
1769 (addr
- local_window
->va_for_temp
);
1771 req
.out_window
= &local_window
;
1772 req
.offset
= loffset
;
1774 * If transfer is from local to remote then the self window
1775 * must be readable and vice versa.
1777 req
.prot
= dir
== SCIF_LOCAL_TO_REMOTE
? VM_READ
: VM_WRITE
;
1779 req
.type
= SCIF_WINDOW_PARTIAL
;
1780 req
.head
= &ep
->rma_info
.reg_list
;
1781 /* Does a valid local window exist? */
1782 err
= scif_query_window(&req
);
1784 mutex_unlock(&ep
->rma_info
.rma_lock
);
1789 /* Does a valid remote window exist? */
1790 err
= scif_query_window(&remote_req
);
1792 mutex_unlock(&ep
->rma_info
.rma_lock
);
1797 * Prepare copy_work for submitting work to the DMA kernel thread
1798 * or CPU copy routine.
1800 copy_work
.len
= len
;
1801 copy_work
.loopback
= loopback
;
1802 copy_work
.remote_dev
= ep
->remote_dev
;
1803 if (dir
== SCIF_LOCAL_TO_REMOTE
) {
1804 copy_work
.src_offset
= loffset
;
1805 copy_work
.src_window
= local_window
;
1806 copy_work
.dst_offset
= roffset
;
1807 copy_work
.dst_window
= remote_window
;
1809 copy_work
.src_offset
= roffset
;
1810 copy_work
.src_window
= remote_window
;
1811 copy_work
.dst_offset
= loffset
;
1812 copy_work
.dst_window
= local_window
;
1815 if (flags
& SCIF_RMA_USECPU
) {
1816 scif_rma_list_cpu_copy(©_work
);
1818 chan
= ep
->rma_info
.dma_chan
;
1819 err
= scif_rma_list_dma_copy_wrapper(epd
, ©_work
,
1823 atomic_inc(&ep
->rma_info
.tw_refcount
);
1825 mutex_unlock(&ep
->rma_info
.rma_lock
);
1828 struct scif_dev
*rdev
= ep
->remote_dev
;
1830 if (copy_work
.fence_type
== SCIF_DMA_POLL
)
1831 err
= scif_drain_dma_poll(rdev
->sdev
,
1832 ep
->rma_info
.dma_chan
);
1833 else if (copy_work
.fence_type
== SCIF_DMA_INTR
)
1834 err
= scif_drain_dma_intr(rdev
->sdev
,
1835 ep
->rma_info
.dma_chan
);
1839 scif_queue_for_cleanup(local_window
, &scif_info
.rma
);
1840 scif_put_peer_dev(spdev
);
1844 if (addr
&& local_window
&& !cache
)
1845 scif_destroy_window(ep
, local_window
);
1846 dev_err(scif_info
.mdev
.this_device
,
1847 "%s %d err %d len 0x%lx\n",
1848 __func__
, __LINE__
, err
, len
);
1850 scif_put_peer_dev(spdev
);
1854 int scif_readfrom(scif_epd_t epd
, off_t loffset
, size_t len
,
1855 off_t roffset
, int flags
)
1859 dev_dbg(scif_info
.mdev
.this_device
,
1860 "SCIFAPI readfrom: ep %p loffset 0x%lx len 0x%lx offset 0x%lx flags 0x%x\n",
1861 epd
, loffset
, len
, roffset
, flags
);
1862 if (scif_unaligned(loffset
, roffset
)) {
1863 while (len
> SCIF_MAX_UNALIGNED_BUF_SIZE
) {
1864 err
= scif_rma_copy(epd
, loffset
, 0x0,
1865 SCIF_MAX_UNALIGNED_BUF_SIZE
,
1867 SCIF_REMOTE_TO_LOCAL
, false);
1870 loffset
+= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1871 roffset
+= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1872 len
-= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1875 err
= scif_rma_copy(epd
, loffset
, 0x0, len
,
1876 roffset
, flags
, SCIF_REMOTE_TO_LOCAL
, true);
1880 EXPORT_SYMBOL_GPL(scif_readfrom
);
1882 int scif_writeto(scif_epd_t epd
, off_t loffset
, size_t len
,
1883 off_t roffset
, int flags
)
1887 dev_dbg(scif_info
.mdev
.this_device
,
1888 "SCIFAPI writeto: ep %p loffset 0x%lx len 0x%lx roffset 0x%lx flags 0x%x\n",
1889 epd
, loffset
, len
, roffset
, flags
);
1890 if (scif_unaligned(loffset
, roffset
)) {
1891 while (len
> SCIF_MAX_UNALIGNED_BUF_SIZE
) {
1892 err
= scif_rma_copy(epd
, loffset
, 0x0,
1893 SCIF_MAX_UNALIGNED_BUF_SIZE
,
1895 SCIF_LOCAL_TO_REMOTE
, false);
1898 loffset
+= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1899 roffset
+= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1900 len
-= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1903 err
= scif_rma_copy(epd
, loffset
, 0x0, len
,
1904 roffset
, flags
, SCIF_LOCAL_TO_REMOTE
, true);
1908 EXPORT_SYMBOL_GPL(scif_writeto
);
1910 int scif_vreadfrom(scif_epd_t epd
, void *addr
, size_t len
,
1911 off_t roffset
, int flags
)
1915 dev_dbg(scif_info
.mdev
.this_device
,
1916 "SCIFAPI vreadfrom: ep %p addr %p len 0x%lx roffset 0x%lx flags 0x%x\n",
1917 epd
, addr
, len
, roffset
, flags
);
1918 if (scif_unaligned((off_t __force
)addr
, roffset
)) {
1919 if (len
> SCIF_MAX_UNALIGNED_BUF_SIZE
)
1920 flags
&= ~SCIF_RMA_USECACHE
;
1922 while (len
> SCIF_MAX_UNALIGNED_BUF_SIZE
) {
1923 err
= scif_rma_copy(epd
, 0, (u64
)addr
,
1924 SCIF_MAX_UNALIGNED_BUF_SIZE
,
1926 SCIF_REMOTE_TO_LOCAL
, false);
1929 addr
+= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1930 roffset
+= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1931 len
-= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1934 err
= scif_rma_copy(epd
, 0, (u64
)addr
, len
,
1935 roffset
, flags
, SCIF_REMOTE_TO_LOCAL
, true);
1939 EXPORT_SYMBOL_GPL(scif_vreadfrom
);
1941 int scif_vwriteto(scif_epd_t epd
, void *addr
, size_t len
,
1942 off_t roffset
, int flags
)
1946 dev_dbg(scif_info
.mdev
.this_device
,
1947 "SCIFAPI vwriteto: ep %p addr %p len 0x%lx roffset 0x%lx flags 0x%x\n",
1948 epd
, addr
, len
, roffset
, flags
);
1949 if (scif_unaligned((off_t __force
)addr
, roffset
)) {
1950 if (len
> SCIF_MAX_UNALIGNED_BUF_SIZE
)
1951 flags
&= ~SCIF_RMA_USECACHE
;
1953 while (len
> SCIF_MAX_UNALIGNED_BUF_SIZE
) {
1954 err
= scif_rma_copy(epd
, 0, (u64
)addr
,
1955 SCIF_MAX_UNALIGNED_BUF_SIZE
,
1957 SCIF_LOCAL_TO_REMOTE
, false);
1960 addr
+= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1961 roffset
+= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1962 len
-= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1965 err
= scif_rma_copy(epd
, 0, (u64
)addr
, len
,
1966 roffset
, flags
, SCIF_LOCAL_TO_REMOTE
, true);
1970 EXPORT_SYMBOL_GPL(scif_vwriteto
);