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 void scif_mmu_notifier_invalidate_page(struct mmu_notifier
*mn
,
204 struct mm_struct
*mm
,
205 unsigned long address
)
207 struct scif_mmu_notif
*mmn
;
209 mmn
= container_of(mn
, struct scif_mmu_notif
, ep_mmu_notifier
);
210 scif_rma_destroy_tcw(mmn
, address
, PAGE_SIZE
);
213 static void scif_mmu_notifier_invalidate_range_start(struct mmu_notifier
*mn
,
214 struct mm_struct
*mm
,
218 struct scif_mmu_notif
*mmn
;
220 mmn
= container_of(mn
, struct scif_mmu_notif
, ep_mmu_notifier
);
221 scif_rma_destroy_tcw(mmn
, start
, end
- start
);
224 static void scif_mmu_notifier_invalidate_range_end(struct mmu_notifier
*mn
,
225 struct mm_struct
*mm
,
230 * Nothing to do here, everything needed was done in
231 * invalidate_range_start.
235 static const struct mmu_notifier_ops scif_mmu_notifier_ops
= {
236 .release
= scif_mmu_notifier_release
,
237 .clear_flush_young
= NULL
,
238 .invalidate_page
= scif_mmu_notifier_invalidate_page
,
239 .invalidate_range_start
= scif_mmu_notifier_invalidate_range_start
,
240 .invalidate_range_end
= scif_mmu_notifier_invalidate_range_end
};
242 static void scif_ep_unregister_mmu_notifier(struct scif_endpt
*ep
)
244 struct scif_endpt_rma_info
*rma
= &ep
->rma_info
;
245 struct scif_mmu_notif
*mmn
= NULL
;
246 struct list_head
*item
, *tmp
;
248 mutex_lock(&ep
->rma_info
.mmn_lock
);
249 list_for_each_safe(item
, tmp
, &rma
->mmn_list
) {
250 mmn
= list_entry(item
, struct scif_mmu_notif
, list
);
251 mmu_notifier_unregister(&mmn
->ep_mmu_notifier
, mmn
->mm
);
255 mutex_unlock(&ep
->rma_info
.mmn_lock
);
258 static void scif_init_mmu_notifier(struct scif_mmu_notif
*mmn
,
259 struct mm_struct
*mm
, struct scif_endpt
*ep
)
263 mmn
->ep_mmu_notifier
.ops
= &scif_mmu_notifier_ops
;
264 INIT_LIST_HEAD(&mmn
->list
);
265 INIT_LIST_HEAD(&mmn
->tc_reg_list
);
268 static struct scif_mmu_notif
*
269 scif_find_mmu_notifier(struct mm_struct
*mm
, struct scif_endpt_rma_info
*rma
)
271 struct scif_mmu_notif
*mmn
;
273 list_for_each_entry(mmn
, &rma
->mmn_list
, list
)
279 static struct scif_mmu_notif
*
280 scif_add_mmu_notifier(struct mm_struct
*mm
, struct scif_endpt
*ep
)
282 struct scif_mmu_notif
*mmn
283 = kzalloc(sizeof(*mmn
), GFP_KERNEL
);
286 return ERR_PTR(-ENOMEM
);
288 scif_init_mmu_notifier(mmn
, current
->mm
, ep
);
289 if (mmu_notifier_register(&mmn
->ep_mmu_notifier
, current
->mm
)) {
291 return ERR_PTR(-EBUSY
);
293 list_add(&mmn
->list
, &ep
->rma_info
.mmn_list
);
298 * Called from the misc thread to destroy temporary cached windows and
299 * unregister the MMU notifier for the SCIF endpoint.
301 void scif_mmu_notif_handler(struct work_struct
*work
)
303 struct list_head
*pos
, *tmpq
;
304 struct scif_endpt
*ep
;
306 scif_rma_destroy_tcw_invalid();
307 spin_lock(&scif_info
.rmalock
);
308 list_for_each_safe(pos
, tmpq
, &scif_info
.mmu_notif_cleanup
) {
309 ep
= list_entry(pos
, struct scif_endpt
, mmu_list
);
310 list_del(&ep
->mmu_list
);
311 spin_unlock(&scif_info
.rmalock
);
312 scif_rma_destroy_tcw_ep(ep
);
313 scif_ep_unregister_mmu_notifier(ep
);
316 spin_unlock(&scif_info
.rmalock
);
319 static bool scif_is_set_reg_cache(int flags
)
321 return !!(flags
& SCIF_RMA_USECACHE
);
324 static struct scif_mmu_notif
*
325 scif_find_mmu_notifier(struct mm_struct
*mm
,
326 struct scif_endpt_rma_info
*rma
)
331 static struct scif_mmu_notif
*
332 scif_add_mmu_notifier(struct mm_struct
*mm
, struct scif_endpt
*ep
)
337 void scif_mmu_notif_handler(struct work_struct
*work
)
341 static bool scif_is_set_reg_cache(int flags
)
346 static bool scif_rma_tc_can_cache(struct scif_endpt
*ep
, size_t cur_bytes
)
353 * scif_register_temp:
354 * @epd: End Point Descriptor.
355 * @addr: virtual address to/from which to copy
356 * @len: length of range to copy
357 * @out_offset: computed offset returned by reference.
358 * @out_window: allocated registered window returned by reference.
360 * Create a temporary registered window. The peer will not know about this
361 * window. This API is used for scif_vreadfrom()/scif_vwriteto() API's.
364 scif_register_temp(scif_epd_t epd
, unsigned long addr
, size_t len
, int prot
,
365 off_t
*out_offset
, struct scif_window
**out_window
)
367 struct scif_endpt
*ep
= (struct scif_endpt
*)epd
;
369 scif_pinned_pages_t pinned_pages
;
372 aligned_len
= ALIGN(len
, PAGE_SIZE
);
374 err
= __scif_pin_pages((void *)(addr
& PAGE_MASK
),
375 aligned_len
, &prot
, 0, &pinned_pages
);
379 pinned_pages
->prot
= prot
;
381 /* Compute the offset for this registration */
382 err
= scif_get_window_offset(ep
, 0, 0,
383 aligned_len
>> PAGE_SHIFT
,
388 /* Allocate and prepare self registration window */
389 *out_window
= scif_create_window(ep
, aligned_len
>> PAGE_SHIFT
,
392 scif_free_window_offset(ep
, NULL
, *out_offset
);
397 (*out_window
)->pinned_pages
= pinned_pages
;
398 (*out_window
)->nr_pages
= pinned_pages
->nr_pages
;
399 (*out_window
)->prot
= pinned_pages
->prot
;
401 (*out_window
)->va_for_temp
= addr
& PAGE_MASK
;
402 err
= scif_map_window(ep
->remote_dev
, *out_window
);
404 /* Something went wrong! Rollback */
405 scif_destroy_window(ep
, *out_window
);
408 *out_offset
|= (addr
- (*out_window
)->va_for_temp
);
413 dev_err(&ep
->remote_dev
->sdev
->dev
,
414 "%s %d err %d\n", __func__
, __LINE__
, err
);
415 scif_unpin_pages(pinned_pages
);
419 #define SCIF_DMA_TO (3 * HZ)
422 * scif_sync_dma - Program a DMA without an interrupt descriptor
424 * @dev - The address of the pointer to the device instance used
425 * for DMA registration.
426 * @chan - DMA channel to be used.
427 * @sync_wait: Wait for DMA to complete?
429 * Return 0 on success and -errno on error.
431 static int scif_sync_dma(struct scif_hw_dev
*sdev
, struct dma_chan
*chan
,
435 struct dma_async_tx_descriptor
*tx
= NULL
;
436 enum dma_ctrl_flags flags
= DMA_PREP_FENCE
;
438 struct dma_device
*ddev
;
442 dev_err(&sdev
->dev
, "%s %d err %d\n",
443 __func__
, __LINE__
, err
);
448 tx
= ddev
->device_prep_dma_memcpy(chan
, 0, 0, 0, flags
);
451 dev_err(&sdev
->dev
, "%s %d err %d\n",
452 __func__
, __LINE__
, err
);
455 cookie
= tx
->tx_submit(tx
);
457 if (dma_submit_error(cookie
)) {
459 dev_err(&sdev
->dev
, "%s %d err %d\n",
460 __func__
, __LINE__
, err
);
464 dma_async_issue_pending(chan
);
466 if (dma_sync_wait(chan
, cookie
) == DMA_COMPLETE
) {
470 dev_err(&sdev
->dev
, "%s %d err %d\n",
471 __func__
, __LINE__
, err
);
478 static void scif_dma_callback(void *arg
)
480 struct completion
*done
= (struct completion
*)arg
;
485 #define SCIF_DMA_SYNC_WAIT true
486 #define SCIF_DMA_POLL BIT(0)
487 #define SCIF_DMA_INTR BIT(1)
490 * scif_async_dma - Program a DMA with an interrupt descriptor
492 * @dev - The address of the pointer to the device instance used
493 * for DMA registration.
494 * @chan - DMA channel to be used.
495 * Return 0 on success and -errno on error.
497 static int scif_async_dma(struct scif_hw_dev
*sdev
, struct dma_chan
*chan
)
500 struct dma_device
*ddev
;
501 struct dma_async_tx_descriptor
*tx
= NULL
;
502 enum dma_ctrl_flags flags
= DMA_PREP_INTERRUPT
| DMA_PREP_FENCE
;
503 DECLARE_COMPLETION_ONSTACK(done_wait
);
505 enum dma_status status
;
509 dev_err(&sdev
->dev
, "%s %d err %d\n",
510 __func__
, __LINE__
, err
);
515 tx
= ddev
->device_prep_dma_memcpy(chan
, 0, 0, 0, flags
);
518 dev_err(&sdev
->dev
, "%s %d err %d\n",
519 __func__
, __LINE__
, err
);
522 reinit_completion(&done_wait
);
523 tx
->callback
= scif_dma_callback
;
524 tx
->callback_param
= &done_wait
;
525 cookie
= tx
->tx_submit(tx
);
527 if (dma_submit_error(cookie
)) {
529 dev_err(&sdev
->dev
, "%s %d err %d\n",
530 __func__
, __LINE__
, err
);
533 dma_async_issue_pending(chan
);
535 err
= wait_for_completion_timeout(&done_wait
, SCIF_DMA_TO
);
538 dev_err(&sdev
->dev
, "%s %d err %d\n",
539 __func__
, __LINE__
, err
);
543 status
= dma_async_is_tx_complete(chan
, cookie
, NULL
, NULL
);
544 if (status
!= DMA_COMPLETE
) {
546 dev_err(&sdev
->dev
, "%s %d err %d\n",
547 __func__
, __LINE__
, err
);
555 * scif_drain_dma_poll - Drain all outstanding DMA operations for a particular
556 * DMA channel via polling.
558 * @sdev - The SCIF device
559 * @chan - DMA channel
560 * Return 0 on success and -errno on error.
562 static int scif_drain_dma_poll(struct scif_hw_dev
*sdev
, struct dma_chan
*chan
)
566 return scif_sync_dma(sdev
, chan
, SCIF_DMA_SYNC_WAIT
);
570 * scif_drain_dma_intr - Drain all outstanding DMA operations for a particular
571 * DMA channel via interrupt based blocking wait.
573 * @sdev - The SCIF device
574 * @chan - DMA channel
575 * Return 0 on success and -errno on error.
577 int scif_drain_dma_intr(struct scif_hw_dev
*sdev
, struct dma_chan
*chan
)
581 return scif_async_dma(sdev
, chan
);
585 * scif_rma_destroy_windows:
587 * This routine destroys all windows queued for cleanup
589 void scif_rma_destroy_windows(void)
591 struct list_head
*item
, *tmp
;
592 struct scif_window
*window
;
593 struct scif_endpt
*ep
;
594 struct dma_chan
*chan
;
598 spin_lock(&scif_info
.rmalock
);
599 list_for_each_safe(item
, tmp
, &scif_info
.rma
) {
600 window
= list_entry(item
, struct scif_window
,
602 ep
= (struct scif_endpt
*)window
->ep
;
603 chan
= ep
->rma_info
.dma_chan
;
605 list_del_init(&window
->list
);
606 spin_unlock(&scif_info
.rmalock
);
607 if (!chan
|| !scifdev_alive(ep
) ||
608 !scif_drain_dma_intr(ep
->remote_dev
->sdev
,
609 ep
->rma_info
.dma_chan
))
610 /* Remove window from global list */
611 window
->unreg_state
= OP_COMPLETED
;
613 dev_warn(&ep
->remote_dev
->sdev
->dev
,
614 "DMA engine hung?\n");
615 if (window
->unreg_state
== OP_COMPLETED
) {
616 if (window
->type
== SCIF_WINDOW_SELF
)
617 scif_destroy_window(ep
, window
);
619 scif_destroy_remote_window(window
);
620 atomic_dec(&ep
->rma_info
.tw_refcount
);
624 spin_unlock(&scif_info
.rmalock
);
628 * scif_rma_destroy_tcw:
630 * This routine destroys temporary cached registered windows
631 * which have been queued for cleanup.
633 void scif_rma_destroy_tcw_invalid(void)
635 struct list_head
*item
, *tmp
;
636 struct scif_window
*window
;
637 struct scif_endpt
*ep
;
638 struct dma_chan
*chan
;
642 spin_lock(&scif_info
.rmalock
);
643 list_for_each_safe(item
, tmp
, &scif_info
.rma_tc
) {
644 window
= list_entry(item
, struct scif_window
, list
);
645 ep
= (struct scif_endpt
*)window
->ep
;
646 chan
= ep
->rma_info
.dma_chan
;
647 list_del_init(&window
->list
);
648 spin_unlock(&scif_info
.rmalock
);
649 mutex_lock(&ep
->rma_info
.rma_lock
);
650 if (!chan
|| !scifdev_alive(ep
) ||
651 !scif_drain_dma_intr(ep
->remote_dev
->sdev
,
652 ep
->rma_info
.dma_chan
)) {
653 atomic_sub(window
->nr_pages
,
654 &ep
->rma_info
.tcw_total_pages
);
655 scif_destroy_window(ep
, window
);
656 atomic_dec(&ep
->rma_info
.tcw_refcount
);
658 dev_warn(&ep
->remote_dev
->sdev
->dev
,
659 "DMA engine hung?\n");
661 mutex_unlock(&ep
->rma_info
.rma_lock
);
664 spin_unlock(&scif_info
.rmalock
);
668 void *_get_local_va(off_t off
, struct scif_window
*window
, size_t len
)
670 int page_nr
= (off
- window
->offset
) >> PAGE_SHIFT
;
671 off_t page_off
= off
& ~PAGE_MASK
;
674 if (window
->type
== SCIF_WINDOW_SELF
) {
675 struct page
**pages
= window
->pinned_pages
->pages
;
677 va
= page_address(pages
[page_nr
]) + page_off
;
683 void *ioremap_remote(off_t off
, struct scif_window
*window
,
684 size_t len
, struct scif_dev
*dev
,
685 struct scif_window_iter
*iter
)
687 dma_addr_t phys
= scif_off_to_dma_addr(window
, off
, NULL
, iter
);
690 * If the DMA address is not card relative then we need the DMA
691 * addresses to be an offset into the bar. The aperture base was already
692 * added so subtract it here since scif_ioremap is going to add it again
694 if (!scifdev_self(dev
) && window
->type
== SCIF_WINDOW_PEER
&&
695 dev
->sdev
->aper
&& !dev
->sdev
->card_rel_da
)
696 phys
= phys
- dev
->sdev
->aper
->pa
;
697 return scif_ioremap(phys
, len
, dev
);
701 iounmap_remote(void *virt
, size_t size
, struct scif_copy_work
*work
)
703 scif_iounmap(virt
, size
, work
->remote_dev
);
707 * Takes care of ordering issue caused by
708 * 1. Hardware: Only in the case of cpu copy from mgmt node to card
709 * because of WC memory.
710 * 2. Software: If memcpy reorders copy instructions for optimization.
711 * This could happen at both mgmt node and card.
714 scif_ordered_memcpy_toio(char *dst
, const char *src
, size_t count
)
719 memcpy_toio((void __iomem __force
*)dst
, src
, --count
);
720 /* Order the last byte with the previous stores */
722 *(dst
+ count
) = *(src
+ count
);
725 static inline void scif_unaligned_cpy_toio(char *dst
, const char *src
,
726 size_t count
, bool ordered
)
729 scif_ordered_memcpy_toio(dst
, src
, count
);
731 memcpy_toio((void __iomem __force
*)dst
, src
, count
);
735 void scif_ordered_memcpy_fromio(char *dst
, const char *src
, size_t count
)
740 memcpy_fromio(dst
, (void __iomem __force
*)src
, --count
);
741 /* Order the last byte with the previous loads */
743 *(dst
+ count
) = *(src
+ count
);
746 static inline void scif_unaligned_cpy_fromio(char *dst
, const char *src
,
747 size_t count
, bool ordered
)
750 scif_ordered_memcpy_fromio(dst
, src
, count
);
752 memcpy_fromio(dst
, (void __iomem __force
*)src
, count
);
755 #define SCIF_RMA_ERROR_CODE (~(dma_addr_t)0x0)
758 * scif_off_to_dma_addr:
759 * Obtain the dma_addr given the window and the offset.
760 * @window: Registered window.
761 * @off: Window offset.
762 * @nr_bytes: Return the number of contiguous bytes till next DMA addr index.
763 * @index: Return the index of the dma_addr array found.
764 * @start_off: start offset of index of the dma addr array found.
765 * The nr_bytes provides the callee an estimate of the maximum possible
766 * DMA xfer possible while the index/start_off provide faster lookups
767 * for the next iteration.
769 dma_addr_t
scif_off_to_dma_addr(struct scif_window
*window
, s64 off
,
770 size_t *nr_bytes
, struct scif_window_iter
*iter
)
776 if (window
->nr_pages
== window
->nr_contig_chunks
) {
777 page_nr
= (off
- window
->offset
) >> PAGE_SHIFT
;
778 page_off
= off
& ~PAGE_MASK
;
781 *nr_bytes
= PAGE_SIZE
- page_off
;
782 return window
->dma_addr
[page_nr
] | page_off
;
786 start
= iter
->offset
;
789 start
= window
->offset
;
791 for (; i
< window
->nr_contig_chunks
; i
++) {
792 end
= start
+ (window
->num_pages
[i
] << PAGE_SHIFT
);
793 if (off
>= start
&& off
< end
) {
796 iter
->offset
= start
;
799 *nr_bytes
= end
- off
;
800 return (window
->dma_addr
[i
] + (off
- start
));
802 start
+= (window
->num_pages
[i
] << PAGE_SHIFT
);
804 dev_err(scif_info
.mdev
.this_device
,
805 "%s %d BUG. Addr not found? window %p off 0x%llx\n",
806 __func__
, __LINE__
, window
, off
);
807 return SCIF_RMA_ERROR_CODE
;
811 * Copy between rma window and temporary buffer
813 static void scif_rma_local_cpu_copy(s64 offset
, struct scif_window
*window
,
814 u8
*temp
, size_t rem_len
, bool to_temp
)
821 offset_in_page
= offset
& ~PAGE_MASK
;
822 loop_len
= PAGE_SIZE
- offset_in_page
;
824 if (rem_len
< loop_len
)
827 window_virt
= _get_local_va(offset
, window
, loop_len
);
831 memcpy(temp
, window_virt
, loop_len
);
833 memcpy(window_virt
, temp
, loop_len
);
839 end_offset
= window
->offset
+
840 (window
->nr_pages
<< PAGE_SHIFT
);
842 if (offset
== end_offset
) {
843 window
= list_next_entry(window
, list
);
844 end_offset
= window
->offset
+
845 (window
->nr_pages
<< PAGE_SHIFT
);
847 loop_len
= min(PAGE_SIZE
, rem_len
);
848 window_virt
= _get_local_va(offset
, window
, loop_len
);
852 memcpy(temp
, window_virt
, loop_len
);
854 memcpy(window_virt
, temp
, loop_len
);
862 * scif_rma_completion_cb:
865 * RMA interrupt completion callback.
867 static void scif_rma_completion_cb(void *data
)
869 struct scif_dma_comp_cb
*comp_cb
= data
;
871 /* Free DMA Completion CB. */
872 if (comp_cb
->dst_window
)
873 scif_rma_local_cpu_copy(comp_cb
->dst_offset
,
876 comp_cb
->header_padding
,
877 comp_cb
->len
, false);
878 scif_unmap_single(comp_cb
->temp_phys
, comp_cb
->sdev
,
879 SCIF_KMEM_UNALIGNED_BUF_SIZE
);
880 if (comp_cb
->is_cache
)
881 kmem_cache_free(unaligned_cache
,
882 comp_cb
->temp_buf_to_free
);
884 kfree(comp_cb
->temp_buf_to_free
);
887 /* Copies between temporary buffer and offsets provided in work */
889 scif_rma_list_dma_copy_unaligned(struct scif_copy_work
*work
,
890 u8
*temp
, struct dma_chan
*chan
,
893 struct scif_dma_comp_cb
*comp_cb
= work
->comp_cb
;
894 dma_addr_t window_dma_addr
, temp_dma_addr
;
895 dma_addr_t temp_phys
= comp_cb
->temp_phys
;
896 size_t loop_len
, nr_contig_bytes
= 0, remaining_len
= work
->len
;
897 int offset_in_ca
, ret
= 0;
898 s64 end_offset
, offset
;
899 struct scif_window
*window
;
900 void *window_virt_addr
;
902 struct dma_async_tx_descriptor
*tx
;
903 struct dma_device
*dev
= chan
->device
;
907 offset
= work
->dst_offset
;
908 window
= work
->dst_window
;
910 offset
= work
->src_offset
;
911 window
= work
->src_window
;
914 offset_in_ca
= offset
& (L1_CACHE_BYTES
- 1);
916 loop_len
= L1_CACHE_BYTES
- offset_in_ca
;
917 loop_len
= min(loop_len
, remaining_len
);
918 window_virt_addr
= ioremap_remote(offset
, window
,
922 if (!window_virt_addr
)
925 scif_unaligned_cpy_toio(window_virt_addr
, temp
,
928 !(remaining_len
- loop_len
));
930 scif_unaligned_cpy_fromio(temp
, window_virt_addr
,
931 loop_len
, work
->ordered
&&
932 !(remaining_len
- loop_len
));
933 iounmap_remote(window_virt_addr
, loop_len
, work
);
937 temp_phys
+= loop_len
;
938 remaining_len
-= loop_len
;
941 offset_in_ca
= offset
& ~PAGE_MASK
;
942 end_offset
= window
->offset
+
943 (window
->nr_pages
<< PAGE_SHIFT
);
945 tail_len
= remaining_len
& (L1_CACHE_BYTES
- 1);
946 remaining_len
-= tail_len
;
947 while (remaining_len
) {
948 if (offset
== end_offset
) {
949 window
= list_next_entry(window
, list
);
950 end_offset
= window
->offset
+
951 (window
->nr_pages
<< PAGE_SHIFT
);
953 if (scif_is_mgmt_node())
954 temp_dma_addr
= temp_phys
;
956 /* Fix if we ever enable IOMMU on the card */
957 temp_dma_addr
= (dma_addr_t
)virt_to_phys(temp
);
958 window_dma_addr
= scif_off_to_dma_addr(window
, offset
,
961 loop_len
= min(nr_contig_bytes
, remaining_len
);
963 if (work
->ordered
&& !tail_len
&&
964 !(remaining_len
- loop_len
) &&
965 loop_len
!= L1_CACHE_BYTES
) {
967 * Break up the last chunk of the transfer into
968 * two steps. if there is no tail to guarantee
969 * DMA ordering. SCIF_DMA_POLLING inserts
970 * a status update descriptor in step 1 which
971 * acts as a double sided synchronization fence
972 * for the DMA engine to ensure that the last
973 * cache line in step 2 is updated last.
975 /* Step 1) DMA: Body Length - L1_CACHE_BYTES. */
977 dev
->device_prep_dma_memcpy(chan
,
987 cookie
= tx
->tx_submit(tx
);
988 if (dma_submit_error(cookie
)) {
992 dma_async_issue_pending(chan
);
993 offset
+= (loop_len
- L1_CACHE_BYTES
);
994 temp_dma_addr
+= (loop_len
- L1_CACHE_BYTES
);
995 window_dma_addr
+= (loop_len
- L1_CACHE_BYTES
);
996 remaining_len
-= (loop_len
- L1_CACHE_BYTES
);
997 loop_len
= remaining_len
;
999 /* Step 2) DMA: L1_CACHE_BYTES */
1001 dev
->device_prep_dma_memcpy(chan
,
1009 cookie
= tx
->tx_submit(tx
);
1010 if (dma_submit_error(cookie
)) {
1014 dma_async_issue_pending(chan
);
1017 dev
->device_prep_dma_memcpy(chan
,
1025 cookie
= tx
->tx_submit(tx
);
1026 if (dma_submit_error(cookie
)) {
1030 dma_async_issue_pending(chan
);
1033 tx
= dev
->device_prep_dma_memcpy(chan
, temp_dma_addr
,
1034 window_dma_addr
, loop_len
, 0);
1039 cookie
= tx
->tx_submit(tx
);
1040 if (dma_submit_error(cookie
)) {
1044 dma_async_issue_pending(chan
);
1050 temp_phys
+= loop_len
;
1051 remaining_len
-= loop_len
;
1055 if (offset
== end_offset
) {
1056 window
= list_next_entry(window
, list
);
1057 end_offset
= window
->offset
+
1058 (window
->nr_pages
<< PAGE_SHIFT
);
1060 window_virt_addr
= ioremap_remote(offset
, window
, tail_len
,
1063 if (!window_virt_addr
)
1066 * The CPU copy for the tail bytes must be initiated only once
1067 * previous DMA transfers for this endpoint have completed
1068 * to guarantee ordering.
1070 if (work
->ordered
) {
1071 struct scif_dev
*rdev
= work
->remote_dev
;
1073 ret
= scif_drain_dma_intr(rdev
->sdev
, chan
);
1078 scif_unaligned_cpy_toio(window_virt_addr
, temp
,
1079 tail_len
, work
->ordered
);
1081 scif_unaligned_cpy_fromio(temp
, window_virt_addr
,
1082 tail_len
, work
->ordered
);
1083 iounmap_remote(window_virt_addr
, tail_len
, work
);
1085 tx
= dev
->device_prep_dma_memcpy(chan
, 0, 0, 0, DMA_PREP_INTERRUPT
);
1090 tx
->callback
= &scif_rma_completion_cb
;
1091 tx
->callback_param
= comp_cb
;
1092 cookie
= tx
->tx_submit(tx
);
1094 if (dma_submit_error(cookie
)) {
1098 dma_async_issue_pending(chan
);
1101 dev_err(scif_info
.mdev
.this_device
,
1102 "%s %d Desc Prog Failed ret %d\n",
1103 __func__
, __LINE__
, ret
);
1108 * _scif_rma_list_dma_copy_aligned:
1110 * Traverse all the windows and perform DMA copy.
1112 static int _scif_rma_list_dma_copy_aligned(struct scif_copy_work
*work
,
1113 struct dma_chan
*chan
)
1115 dma_addr_t src_dma_addr
, dst_dma_addr
;
1116 size_t loop_len
, remaining_len
, src_contig_bytes
= 0;
1117 size_t dst_contig_bytes
= 0;
1118 struct scif_window_iter src_win_iter
;
1119 struct scif_window_iter dst_win_iter
;
1120 s64 end_src_offset
, end_dst_offset
;
1121 struct scif_window
*src_window
= work
->src_window
;
1122 struct scif_window
*dst_window
= work
->dst_window
;
1123 s64 src_offset
= work
->src_offset
, dst_offset
= work
->dst_offset
;
1125 struct dma_async_tx_descriptor
*tx
;
1126 struct dma_device
*dev
= chan
->device
;
1127 dma_cookie_t cookie
;
1129 remaining_len
= work
->len
;
1131 scif_init_window_iter(src_window
, &src_win_iter
);
1132 scif_init_window_iter(dst_window
, &dst_win_iter
);
1133 end_src_offset
= src_window
->offset
+
1134 (src_window
->nr_pages
<< PAGE_SHIFT
);
1135 end_dst_offset
= dst_window
->offset
+
1136 (dst_window
->nr_pages
<< PAGE_SHIFT
);
1137 while (remaining_len
) {
1138 if (src_offset
== end_src_offset
) {
1139 src_window
= list_next_entry(src_window
, list
);
1140 end_src_offset
= src_window
->offset
+
1141 (src_window
->nr_pages
<< PAGE_SHIFT
);
1142 scif_init_window_iter(src_window
, &src_win_iter
);
1144 if (dst_offset
== end_dst_offset
) {
1145 dst_window
= list_next_entry(dst_window
, list
);
1146 end_dst_offset
= dst_window
->offset
+
1147 (dst_window
->nr_pages
<< PAGE_SHIFT
);
1148 scif_init_window_iter(dst_window
, &dst_win_iter
);
1151 /* compute dma addresses for transfer */
1152 src_dma_addr
= scif_off_to_dma_addr(src_window
, src_offset
,
1155 dst_dma_addr
= scif_off_to_dma_addr(dst_window
, dst_offset
,
1158 loop_len
= min(src_contig_bytes
, dst_contig_bytes
);
1159 loop_len
= min(loop_len
, remaining_len
);
1160 if (work
->ordered
&& !(remaining_len
- loop_len
)) {
1162 * Break up the last chunk of the transfer into two
1163 * steps to ensure that the last byte in step 2 is
1166 /* Step 1) DMA: Body Length - 1 */
1167 tx
= dev
->device_prep_dma_memcpy(chan
, dst_dma_addr
,
1175 cookie
= tx
->tx_submit(tx
);
1176 if (dma_submit_error(cookie
)) {
1180 src_offset
+= (loop_len
- 1);
1181 dst_offset
+= (loop_len
- 1);
1182 src_dma_addr
+= (loop_len
- 1);
1183 dst_dma_addr
+= (loop_len
- 1);
1184 remaining_len
-= (loop_len
- 1);
1185 loop_len
= remaining_len
;
1187 /* Step 2) DMA: 1 BYTES */
1188 tx
= dev
->device_prep_dma_memcpy(chan
, dst_dma_addr
,
1189 src_dma_addr
, loop_len
, 0);
1194 cookie
= tx
->tx_submit(tx
);
1195 if (dma_submit_error(cookie
)) {
1199 dma_async_issue_pending(chan
);
1201 tx
= dev
->device_prep_dma_memcpy(chan
, dst_dma_addr
,
1202 src_dma_addr
, loop_len
, 0);
1207 cookie
= tx
->tx_submit(tx
);
1208 if (dma_submit_error(cookie
)) {
1213 src_offset
+= loop_len
;
1214 dst_offset
+= loop_len
;
1215 remaining_len
-= loop_len
;
1219 dev_err(scif_info
.mdev
.this_device
,
1220 "%s %d Desc Prog Failed ret %d\n",
1221 __func__
, __LINE__
, ret
);
1226 * scif_rma_list_dma_copy_aligned:
1228 * Traverse all the windows and perform DMA copy.
1230 static int scif_rma_list_dma_copy_aligned(struct scif_copy_work
*work
,
1231 struct dma_chan
*chan
)
1233 dma_addr_t src_dma_addr
, dst_dma_addr
;
1234 size_t loop_len
, remaining_len
, tail_len
, src_contig_bytes
= 0;
1235 size_t dst_contig_bytes
= 0;
1237 s64 end_src_offset
, end_dst_offset
;
1238 struct scif_window_iter src_win_iter
;
1239 struct scif_window_iter dst_win_iter
;
1240 void *src_virt
, *dst_virt
;
1241 struct scif_window
*src_window
= work
->src_window
;
1242 struct scif_window
*dst_window
= work
->dst_window
;
1243 s64 src_offset
= work
->src_offset
, dst_offset
= work
->dst_offset
;
1245 struct dma_async_tx_descriptor
*tx
;
1246 struct dma_device
*dev
= chan
->device
;
1247 dma_cookie_t cookie
;
1249 remaining_len
= work
->len
;
1250 scif_init_window_iter(src_window
, &src_win_iter
);
1251 scif_init_window_iter(dst_window
, &dst_win_iter
);
1253 src_cache_off
= src_offset
& (L1_CACHE_BYTES
- 1);
1254 if (src_cache_off
!= 0) {
1256 loop_len
= L1_CACHE_BYTES
- src_cache_off
;
1257 loop_len
= min(loop_len
, remaining_len
);
1258 src_dma_addr
= __scif_off_to_dma_addr(src_window
, src_offset
);
1259 dst_dma_addr
= __scif_off_to_dma_addr(dst_window
, dst_offset
);
1260 if (src_window
->type
== SCIF_WINDOW_SELF
)
1261 src_virt
= _get_local_va(src_offset
, src_window
,
1264 src_virt
= ioremap_remote(src_offset
, src_window
,
1266 work
->remote_dev
, NULL
);
1269 if (dst_window
->type
== SCIF_WINDOW_SELF
)
1270 dst_virt
= _get_local_va(dst_offset
, dst_window
,
1273 dst_virt
= ioremap_remote(dst_offset
, dst_window
,
1275 work
->remote_dev
, NULL
);
1277 if (src_window
->type
!= SCIF_WINDOW_SELF
)
1278 iounmap_remote(src_virt
, loop_len
, work
);
1281 if (src_window
->type
== SCIF_WINDOW_SELF
)
1282 scif_unaligned_cpy_toio(dst_virt
, src_virt
, loop_len
,
1283 remaining_len
== loop_len
?
1284 work
->ordered
: false);
1286 scif_unaligned_cpy_fromio(dst_virt
, src_virt
, loop_len
,
1287 remaining_len
== loop_len
?
1288 work
->ordered
: false);
1289 if (src_window
->type
!= SCIF_WINDOW_SELF
)
1290 iounmap_remote(src_virt
, loop_len
, work
);
1291 if (dst_window
->type
!= SCIF_WINDOW_SELF
)
1292 iounmap_remote(dst_virt
, loop_len
, work
);
1293 src_offset
+= loop_len
;
1294 dst_offset
+= loop_len
;
1295 remaining_len
-= loop_len
;
1298 end_src_offset
= src_window
->offset
+
1299 (src_window
->nr_pages
<< PAGE_SHIFT
);
1300 end_dst_offset
= dst_window
->offset
+
1301 (dst_window
->nr_pages
<< PAGE_SHIFT
);
1302 tail_len
= remaining_len
& (L1_CACHE_BYTES
- 1);
1303 remaining_len
-= tail_len
;
1304 while (remaining_len
) {
1305 if (src_offset
== end_src_offset
) {
1306 src_window
= list_next_entry(src_window
, list
);
1307 end_src_offset
= src_window
->offset
+
1308 (src_window
->nr_pages
<< PAGE_SHIFT
);
1309 scif_init_window_iter(src_window
, &src_win_iter
);
1311 if (dst_offset
== end_dst_offset
) {
1312 dst_window
= list_next_entry(dst_window
, list
);
1313 end_dst_offset
= dst_window
->offset
+
1314 (dst_window
->nr_pages
<< PAGE_SHIFT
);
1315 scif_init_window_iter(dst_window
, &dst_win_iter
);
1318 /* compute dma addresses for transfer */
1319 src_dma_addr
= scif_off_to_dma_addr(src_window
, src_offset
,
1322 dst_dma_addr
= scif_off_to_dma_addr(dst_window
, dst_offset
,
1325 loop_len
= min(src_contig_bytes
, dst_contig_bytes
);
1326 loop_len
= min(loop_len
, remaining_len
);
1327 if (work
->ordered
&& !tail_len
&&
1328 !(remaining_len
- loop_len
)) {
1330 * Break up the last chunk of the transfer into two
1331 * steps. if there is no tail to gurantee DMA ordering.
1332 * Passing SCIF_DMA_POLLING inserts a status update
1333 * descriptor in step 1 which acts as a double sided
1334 * synchronization fence for the DMA engine to ensure
1335 * that the last cache line in step 2 is updated last.
1337 /* Step 1) DMA: Body Length - L1_CACHE_BYTES. */
1338 tx
= dev
->device_prep_dma_memcpy(chan
, dst_dma_addr
,
1347 cookie
= tx
->tx_submit(tx
);
1348 if (dma_submit_error(cookie
)) {
1352 dma_async_issue_pending(chan
);
1353 src_offset
+= (loop_len
- L1_CACHE_BYTES
);
1354 dst_offset
+= (loop_len
- L1_CACHE_BYTES
);
1355 src_dma_addr
+= (loop_len
- L1_CACHE_BYTES
);
1356 dst_dma_addr
+= (loop_len
- L1_CACHE_BYTES
);
1357 remaining_len
-= (loop_len
- L1_CACHE_BYTES
);
1358 loop_len
= remaining_len
;
1360 /* Step 2) DMA: L1_CACHE_BYTES */
1361 tx
= dev
->device_prep_dma_memcpy(chan
, dst_dma_addr
,
1368 cookie
= tx
->tx_submit(tx
);
1369 if (dma_submit_error(cookie
)) {
1373 dma_async_issue_pending(chan
);
1375 tx
= dev
->device_prep_dma_memcpy(chan
, dst_dma_addr
,
1382 cookie
= tx
->tx_submit(tx
);
1383 if (dma_submit_error(cookie
)) {
1387 dma_async_issue_pending(chan
);
1389 src_offset
+= loop_len
;
1390 dst_offset
+= loop_len
;
1391 remaining_len
-= loop_len
;
1393 remaining_len
= tail_len
;
1394 if (remaining_len
) {
1395 loop_len
= remaining_len
;
1396 if (src_offset
== end_src_offset
)
1397 src_window
= list_next_entry(src_window
, list
);
1398 if (dst_offset
== end_dst_offset
)
1399 dst_window
= list_next_entry(dst_window
, list
);
1401 src_dma_addr
= __scif_off_to_dma_addr(src_window
, src_offset
);
1402 dst_dma_addr
= __scif_off_to_dma_addr(dst_window
, dst_offset
);
1404 * The CPU copy for the tail bytes must be initiated only once
1405 * previous DMA transfers for this endpoint have completed to
1406 * guarantee ordering.
1408 if (work
->ordered
) {
1409 struct scif_dev
*rdev
= work
->remote_dev
;
1411 ret
= scif_drain_dma_poll(rdev
->sdev
, chan
);
1415 if (src_window
->type
== SCIF_WINDOW_SELF
)
1416 src_virt
= _get_local_va(src_offset
, src_window
,
1419 src_virt
= ioremap_remote(src_offset
, src_window
,
1421 work
->remote_dev
, NULL
);
1425 if (dst_window
->type
== SCIF_WINDOW_SELF
)
1426 dst_virt
= _get_local_va(dst_offset
, dst_window
,
1429 dst_virt
= ioremap_remote(dst_offset
, dst_window
,
1431 work
->remote_dev
, NULL
);
1433 if (src_window
->type
!= SCIF_WINDOW_SELF
)
1434 iounmap_remote(src_virt
, loop_len
, work
);
1438 if (src_window
->type
== SCIF_WINDOW_SELF
)
1439 scif_unaligned_cpy_toio(dst_virt
, src_virt
, loop_len
,
1442 scif_unaligned_cpy_fromio(dst_virt
, src_virt
,
1443 loop_len
, work
->ordered
);
1444 if (src_window
->type
!= SCIF_WINDOW_SELF
)
1445 iounmap_remote(src_virt
, loop_len
, work
);
1447 if (dst_window
->type
!= SCIF_WINDOW_SELF
)
1448 iounmap_remote(dst_virt
, loop_len
, work
);
1449 remaining_len
-= loop_len
;
1453 dev_err(scif_info
.mdev
.this_device
,
1454 "%s %d Desc Prog Failed ret %d\n",
1455 __func__
, __LINE__
, ret
);
1460 * scif_rma_list_cpu_copy:
1462 * Traverse all the windows and perform CPU copy.
1464 static int scif_rma_list_cpu_copy(struct scif_copy_work
*work
)
1466 void *src_virt
, *dst_virt
;
1467 size_t loop_len
, remaining_len
;
1468 int src_page_off
, dst_page_off
;
1469 s64 src_offset
= work
->src_offset
, dst_offset
= work
->dst_offset
;
1470 struct scif_window
*src_window
= work
->src_window
;
1471 struct scif_window
*dst_window
= work
->dst_window
;
1472 s64 end_src_offset
, end_dst_offset
;
1474 struct scif_window_iter src_win_iter
;
1475 struct scif_window_iter dst_win_iter
;
1477 remaining_len
= work
->len
;
1479 scif_init_window_iter(src_window
, &src_win_iter
);
1480 scif_init_window_iter(dst_window
, &dst_win_iter
);
1481 while (remaining_len
) {
1482 src_page_off
= src_offset
& ~PAGE_MASK
;
1483 dst_page_off
= dst_offset
& ~PAGE_MASK
;
1484 loop_len
= min(PAGE_SIZE
-
1485 max(src_page_off
, dst_page_off
),
1488 if (src_window
->type
== SCIF_WINDOW_SELF
)
1489 src_virt
= _get_local_va(src_offset
, src_window
,
1492 src_virt
= ioremap_remote(src_offset
, src_window
,
1501 if (dst_window
->type
== SCIF_WINDOW_SELF
)
1502 dst_virt
= _get_local_va(dst_offset
, dst_window
,
1505 dst_virt
= ioremap_remote(dst_offset
, dst_window
,
1510 if (src_window
->type
== SCIF_WINDOW_PEER
)
1511 iounmap_remote(src_virt
, loop_len
, work
);
1516 if (work
->loopback
) {
1517 memcpy(dst_virt
, src_virt
, loop_len
);
1519 if (src_window
->type
== SCIF_WINDOW_SELF
)
1520 memcpy_toio((void __iomem __force
*)dst_virt
,
1521 src_virt
, loop_len
);
1523 memcpy_fromio(dst_virt
,
1524 (void __iomem __force
*)src_virt
,
1527 if (src_window
->type
== SCIF_WINDOW_PEER
)
1528 iounmap_remote(src_virt
, loop_len
, work
);
1530 if (dst_window
->type
== SCIF_WINDOW_PEER
)
1531 iounmap_remote(dst_virt
, loop_len
, work
);
1533 src_offset
+= loop_len
;
1534 dst_offset
+= loop_len
;
1535 remaining_len
-= loop_len
;
1536 if (remaining_len
) {
1537 end_src_offset
= src_window
->offset
+
1538 (src_window
->nr_pages
<< PAGE_SHIFT
);
1539 end_dst_offset
= dst_window
->offset
+
1540 (dst_window
->nr_pages
<< PAGE_SHIFT
);
1541 if (src_offset
== end_src_offset
) {
1542 src_window
= list_next_entry(src_window
, list
);
1543 scif_init_window_iter(src_window
,
1546 if (dst_offset
== end_dst_offset
) {
1547 dst_window
= list_next_entry(dst_window
, list
);
1548 scif_init_window_iter(dst_window
,
1557 static int scif_rma_list_dma_copy_wrapper(struct scif_endpt
*epd
,
1558 struct scif_copy_work
*work
,
1559 struct dma_chan
*chan
, off_t loffset
)
1561 int src_cache_off
, dst_cache_off
;
1562 s64 src_offset
= work
->src_offset
, dst_offset
= work
->dst_offset
;
1564 bool src_local
= true, dst_local
= false;
1565 struct scif_dma_comp_cb
*comp_cb
;
1566 dma_addr_t src_dma_addr
, dst_dma_addr
;
1569 if (is_dma_copy_aligned(chan
->device
, 1, 1, 1))
1570 return _scif_rma_list_dma_copy_aligned(work
, chan
);
1572 src_cache_off
= src_offset
& (L1_CACHE_BYTES
- 1);
1573 dst_cache_off
= dst_offset
& (L1_CACHE_BYTES
- 1);
1575 if (dst_cache_off
== src_cache_off
)
1576 return scif_rma_list_dma_copy_aligned(work
, chan
);
1579 return scif_rma_list_cpu_copy(work
);
1580 src_dma_addr
= __scif_off_to_dma_addr(work
->src_window
, src_offset
);
1581 dst_dma_addr
= __scif_off_to_dma_addr(work
->dst_window
, dst_offset
);
1582 src_local
= work
->src_window
->type
== SCIF_WINDOW_SELF
;
1583 dst_local
= work
->dst_window
->type
== SCIF_WINDOW_SELF
;
1585 dst_local
= dst_local
;
1586 /* Allocate dma_completion cb */
1587 comp_cb
= kzalloc(sizeof(*comp_cb
), GFP_KERNEL
);
1591 work
->comp_cb
= comp_cb
;
1592 comp_cb
->cb_cookie
= comp_cb
;
1593 comp_cb
->dma_completion_func
= &scif_rma_completion_cb
;
1595 if (work
->len
+ (L1_CACHE_BYTES
<< 1) < SCIF_KMEM_UNALIGNED_BUF_SIZE
) {
1596 comp_cb
->is_cache
= false;
1597 /* Allocate padding bytes to align to a cache line */
1598 temp
= kmalloc(work
->len
+ (L1_CACHE_BYTES
<< 1),
1602 comp_cb
->temp_buf_to_free
= temp
;
1603 /* kmalloc(..) does not guarantee cache line alignment */
1604 if (!IS_ALIGNED((u64
)temp
, L1_CACHE_BYTES
))
1605 temp
= PTR_ALIGN(temp
, L1_CACHE_BYTES
);
1607 comp_cb
->is_cache
= true;
1608 temp
= kmem_cache_alloc(unaligned_cache
, GFP_KERNEL
);
1611 comp_cb
->temp_buf_to_free
= temp
;
1615 temp
+= dst_cache_off
;
1616 scif_rma_local_cpu_copy(work
->src_offset
, work
->src_window
,
1617 temp
, work
->len
, true);
1619 comp_cb
->dst_window
= work
->dst_window
;
1620 comp_cb
->dst_offset
= work
->dst_offset
;
1621 work
->src_offset
= work
->src_offset
- src_cache_off
;
1622 comp_cb
->len
= work
->len
;
1623 work
->len
= ALIGN(work
->len
+ src_cache_off
, L1_CACHE_BYTES
);
1624 comp_cb
->header_padding
= src_cache_off
;
1626 comp_cb
->temp_buf
= temp
;
1628 err
= scif_map_single(&comp_cb
->temp_phys
, temp
,
1629 work
->remote_dev
, SCIF_KMEM_UNALIGNED_BUF_SIZE
);
1632 comp_cb
->sdev
= work
->remote_dev
;
1633 if (scif_rma_list_dma_copy_unaligned(work
, temp
, chan
, src_local
) < 0)
1636 work
->fence_type
= SCIF_DMA_INTR
;
1639 if (comp_cb
->is_cache
)
1640 kmem_cache_free(unaligned_cache
, comp_cb
->temp_buf_to_free
);
1642 kfree(comp_cb
->temp_buf_to_free
);
1651 * @epd: end point descriptor.
1652 * @loffset: offset in local registered address space to/from which to copy
1653 * @addr: user virtual address to/from which to copy
1654 * @len: length of range to copy
1655 * @roffset: offset in remote registered address space to/from which to copy
1657 * @dir: LOCAL->REMOTE or vice versa.
1658 * @last_chunk: true if this is the last chunk of a larger transfer
1660 * Validate parameters, check if src/dst registered ranges requested for copy
1661 * are valid and initiate either CPU or DMA copy.
1663 static int scif_rma_copy(scif_epd_t epd
, off_t loffset
, unsigned long addr
,
1664 size_t len
, off_t roffset
, int flags
,
1665 enum scif_rma_dir dir
, bool last_chunk
)
1667 struct scif_endpt
*ep
= (struct scif_endpt
*)epd
;
1668 struct scif_rma_req remote_req
;
1669 struct scif_rma_req req
;
1670 struct scif_window
*local_window
= NULL
;
1671 struct scif_window
*remote_window
= NULL
;
1672 struct scif_copy_work copy_work
;
1675 struct dma_chan
*chan
;
1676 struct scif_mmu_notif
*mmn
= NULL
;
1678 struct device
*spdev
;
1680 err
= scif_verify_epd(ep
);
1684 if (flags
&& !(flags
& (SCIF_RMA_USECPU
| SCIF_RMA_USECACHE
|
1685 SCIF_RMA_SYNC
| SCIF_RMA_ORDERED
)))
1688 loopback
= scifdev_self(ep
->remote_dev
) ? true : false;
1689 copy_work
.fence_type
= ((flags
& SCIF_RMA_SYNC
) && last_chunk
) ?
1691 copy_work
.ordered
= !!((flags
& SCIF_RMA_ORDERED
) && last_chunk
);
1693 /* Use CPU for Mgmt node <-> Mgmt node copies */
1694 if (loopback
&& scif_is_mgmt_node()) {
1695 flags
|= SCIF_RMA_USECPU
;
1696 copy_work
.fence_type
= 0x0;
1699 cache
= scif_is_set_reg_cache(flags
);
1701 remote_req
.out_window
= &remote_window
;
1702 remote_req
.offset
= roffset
;
1703 remote_req
.nr_bytes
= len
;
1705 * If transfer is from local to remote then the remote window
1706 * must be writeable and vice versa.
1708 remote_req
.prot
= dir
== SCIF_LOCAL_TO_REMOTE
? VM_WRITE
: VM_READ
;
1709 remote_req
.type
= SCIF_WINDOW_PARTIAL
;
1710 remote_req
.head
= &ep
->rma_info
.remote_reg_list
;
1712 spdev
= scif_get_peer_dev(ep
->remote_dev
);
1713 if (IS_ERR(spdev
)) {
1714 err
= PTR_ERR(spdev
);
1718 if (addr
&& cache
) {
1719 mutex_lock(&ep
->rma_info
.mmn_lock
);
1720 mmn
= scif_find_mmu_notifier(current
->mm
, &ep
->rma_info
);
1722 mmn
= scif_add_mmu_notifier(current
->mm
, ep
);
1723 mutex_unlock(&ep
->rma_info
.mmn_lock
);
1725 scif_put_peer_dev(spdev
);
1726 return PTR_ERR(mmn
);
1728 cache
= cache
&& !scif_rma_tc_can_cache(ep
, len
);
1730 mutex_lock(&ep
->rma_info
.rma_lock
);
1732 req
.out_window
= &local_window
;
1733 req
.nr_bytes
= ALIGN(len
+ (addr
& ~PAGE_MASK
),
1735 req
.va_for_temp
= addr
& PAGE_MASK
;
1736 req
.prot
= (dir
== SCIF_LOCAL_TO_REMOTE
?
1737 VM_READ
: VM_WRITE
| VM_READ
);
1738 /* Does a valid local window exist? */
1740 spin_lock(&ep
->rma_info
.tc_lock
);
1741 req
.head
= &mmn
->tc_reg_list
;
1742 err
= scif_query_tcw(ep
, &req
);
1743 spin_unlock(&ep
->rma_info
.tc_lock
);
1746 err
= scif_register_temp(epd
, req
.va_for_temp
,
1747 req
.nr_bytes
, req
.prot
,
1748 &loffset
, &local_window
);
1750 mutex_unlock(&ep
->rma_info
.rma_lock
);
1755 atomic_inc(&ep
->rma_info
.tcw_refcount
);
1756 atomic_add_return(local_window
->nr_pages
,
1757 &ep
->rma_info
.tcw_total_pages
);
1759 spin_lock(&ep
->rma_info
.tc_lock
);
1760 scif_insert_tcw(local_window
,
1762 spin_unlock(&ep
->rma_info
.tc_lock
);
1766 loffset
= local_window
->offset
+
1767 (addr
- local_window
->va_for_temp
);
1769 req
.out_window
= &local_window
;
1770 req
.offset
= loffset
;
1772 * If transfer is from local to remote then the self window
1773 * must be readable and vice versa.
1775 req
.prot
= dir
== SCIF_LOCAL_TO_REMOTE
? VM_READ
: VM_WRITE
;
1777 req
.type
= SCIF_WINDOW_PARTIAL
;
1778 req
.head
= &ep
->rma_info
.reg_list
;
1779 /* Does a valid local window exist? */
1780 err
= scif_query_window(&req
);
1782 mutex_unlock(&ep
->rma_info
.rma_lock
);
1787 /* Does a valid remote window exist? */
1788 err
= scif_query_window(&remote_req
);
1790 mutex_unlock(&ep
->rma_info
.rma_lock
);
1795 * Prepare copy_work for submitting work to the DMA kernel thread
1796 * or CPU copy routine.
1798 copy_work
.len
= len
;
1799 copy_work
.loopback
= loopback
;
1800 copy_work
.remote_dev
= ep
->remote_dev
;
1801 if (dir
== SCIF_LOCAL_TO_REMOTE
) {
1802 copy_work
.src_offset
= loffset
;
1803 copy_work
.src_window
= local_window
;
1804 copy_work
.dst_offset
= roffset
;
1805 copy_work
.dst_window
= remote_window
;
1807 copy_work
.src_offset
= roffset
;
1808 copy_work
.src_window
= remote_window
;
1809 copy_work
.dst_offset
= loffset
;
1810 copy_work
.dst_window
= local_window
;
1813 if (flags
& SCIF_RMA_USECPU
) {
1814 scif_rma_list_cpu_copy(©_work
);
1816 chan
= ep
->rma_info
.dma_chan
;
1817 err
= scif_rma_list_dma_copy_wrapper(epd
, ©_work
,
1821 atomic_inc(&ep
->rma_info
.tw_refcount
);
1823 mutex_unlock(&ep
->rma_info
.rma_lock
);
1826 struct scif_dev
*rdev
= ep
->remote_dev
;
1828 if (copy_work
.fence_type
== SCIF_DMA_POLL
)
1829 err
= scif_drain_dma_poll(rdev
->sdev
,
1830 ep
->rma_info
.dma_chan
);
1831 else if (copy_work
.fence_type
== SCIF_DMA_INTR
)
1832 err
= scif_drain_dma_intr(rdev
->sdev
,
1833 ep
->rma_info
.dma_chan
);
1837 scif_queue_for_cleanup(local_window
, &scif_info
.rma
);
1838 scif_put_peer_dev(spdev
);
1842 if (addr
&& local_window
&& !cache
)
1843 scif_destroy_window(ep
, local_window
);
1844 dev_err(scif_info
.mdev
.this_device
,
1845 "%s %d err %d len 0x%lx\n",
1846 __func__
, __LINE__
, err
, len
);
1848 scif_put_peer_dev(spdev
);
1852 int scif_readfrom(scif_epd_t epd
, off_t loffset
, size_t len
,
1853 off_t roffset
, int flags
)
1857 dev_dbg(scif_info
.mdev
.this_device
,
1858 "SCIFAPI readfrom: ep %p loffset 0x%lx len 0x%lx offset 0x%lx flags 0x%x\n",
1859 epd
, loffset
, len
, roffset
, flags
);
1860 if (scif_unaligned(loffset
, roffset
)) {
1861 while (len
> SCIF_MAX_UNALIGNED_BUF_SIZE
) {
1862 err
= scif_rma_copy(epd
, loffset
, 0x0,
1863 SCIF_MAX_UNALIGNED_BUF_SIZE
,
1865 SCIF_REMOTE_TO_LOCAL
, false);
1868 loffset
+= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1869 roffset
+= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1870 len
-= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1873 err
= scif_rma_copy(epd
, loffset
, 0x0, len
,
1874 roffset
, flags
, SCIF_REMOTE_TO_LOCAL
, true);
1878 EXPORT_SYMBOL_GPL(scif_readfrom
);
1880 int scif_writeto(scif_epd_t epd
, off_t loffset
, size_t len
,
1881 off_t roffset
, int flags
)
1885 dev_dbg(scif_info
.mdev
.this_device
,
1886 "SCIFAPI writeto: ep %p loffset 0x%lx len 0x%lx roffset 0x%lx flags 0x%x\n",
1887 epd
, loffset
, len
, roffset
, flags
);
1888 if (scif_unaligned(loffset
, roffset
)) {
1889 while (len
> SCIF_MAX_UNALIGNED_BUF_SIZE
) {
1890 err
= scif_rma_copy(epd
, loffset
, 0x0,
1891 SCIF_MAX_UNALIGNED_BUF_SIZE
,
1893 SCIF_LOCAL_TO_REMOTE
, false);
1896 loffset
+= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1897 roffset
+= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1898 len
-= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1901 err
= scif_rma_copy(epd
, loffset
, 0x0, len
,
1902 roffset
, flags
, SCIF_LOCAL_TO_REMOTE
, true);
1906 EXPORT_SYMBOL_GPL(scif_writeto
);
1908 int scif_vreadfrom(scif_epd_t epd
, void *addr
, size_t len
,
1909 off_t roffset
, int flags
)
1913 dev_dbg(scif_info
.mdev
.this_device
,
1914 "SCIFAPI vreadfrom: ep %p addr %p len 0x%lx roffset 0x%lx flags 0x%x\n",
1915 epd
, addr
, len
, roffset
, flags
);
1916 if (scif_unaligned((off_t __force
)addr
, roffset
)) {
1917 if (len
> SCIF_MAX_UNALIGNED_BUF_SIZE
)
1918 flags
&= ~SCIF_RMA_USECACHE
;
1920 while (len
> SCIF_MAX_UNALIGNED_BUF_SIZE
) {
1921 err
= scif_rma_copy(epd
, 0, (u64
)addr
,
1922 SCIF_MAX_UNALIGNED_BUF_SIZE
,
1924 SCIF_REMOTE_TO_LOCAL
, false);
1927 addr
+= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1928 roffset
+= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1929 len
-= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1932 err
= scif_rma_copy(epd
, 0, (u64
)addr
, len
,
1933 roffset
, flags
, SCIF_REMOTE_TO_LOCAL
, true);
1937 EXPORT_SYMBOL_GPL(scif_vreadfrom
);
1939 int scif_vwriteto(scif_epd_t epd
, void *addr
, size_t len
,
1940 off_t roffset
, int flags
)
1944 dev_dbg(scif_info
.mdev
.this_device
,
1945 "SCIFAPI vwriteto: ep %p addr %p len 0x%lx roffset 0x%lx flags 0x%x\n",
1946 epd
, addr
, len
, roffset
, flags
);
1947 if (scif_unaligned((off_t __force
)addr
, roffset
)) {
1948 if (len
> SCIF_MAX_UNALIGNED_BUF_SIZE
)
1949 flags
&= ~SCIF_RMA_USECACHE
;
1951 while (len
> SCIF_MAX_UNALIGNED_BUF_SIZE
) {
1952 err
= scif_rma_copy(epd
, 0, (u64
)addr
,
1953 SCIF_MAX_UNALIGNED_BUF_SIZE
,
1955 SCIF_LOCAL_TO_REMOTE
, false);
1958 addr
+= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1959 roffset
+= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1960 len
-= SCIF_MAX_UNALIGNED_BUF_SIZE
;
1963 err
= scif_rma_copy(epd
, 0, (u64
)addr
, len
,
1964 roffset
, flags
, SCIF_LOCAL_TO_REMOTE
, true);
1968 EXPORT_SYMBOL_GPL(scif_vwriteto
);