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 <linux/dma_remapping.h>
19 #include <linux/pagemap.h>
20 #include "scif_main.h"
23 /* Used to skip ulimit checks for registrations with SCIF_MAP_KERNEL flag */
24 #define SCIF_MAP_ULIMIT 0x40
26 bool scif_ulimit_check
= 1;
32 * Initialize RMA per EP data structures.
34 void scif_rma_ep_init(struct scif_endpt
*ep
)
36 struct scif_endpt_rma_info
*rma
= &ep
->rma_info
;
38 mutex_init(&rma
->rma_lock
);
39 init_iova_domain(&rma
->iovad
, PAGE_SIZE
, SCIF_IOVA_START_PFN
,
41 spin_lock_init(&rma
->tc_lock
);
42 mutex_init(&rma
->mmn_lock
);
43 INIT_LIST_HEAD(&rma
->reg_list
);
44 INIT_LIST_HEAD(&rma
->remote_reg_list
);
45 atomic_set(&rma
->tw_refcount
, 0);
46 atomic_set(&rma
->tcw_refcount
, 0);
47 atomic_set(&rma
->tcw_total_pages
, 0);
48 atomic_set(&rma
->fence_refcount
, 0);
50 rma
->async_list_del
= 0;
52 INIT_LIST_HEAD(&rma
->mmn_list
);
53 INIT_LIST_HEAD(&rma
->vma_list
);
54 init_waitqueue_head(&rma
->markwq
);
58 * scif_rma_ep_can_uninit:
61 * Returns 1 if an endpoint can be uninitialized and 0 otherwise.
63 int scif_rma_ep_can_uninit(struct scif_endpt
*ep
)
67 mutex_lock(&ep
->rma_info
.rma_lock
);
68 /* Destroy RMA Info only if both lists are empty */
69 if (list_empty(&ep
->rma_info
.reg_list
) &&
70 list_empty(&ep
->rma_info
.remote_reg_list
) &&
71 list_empty(&ep
->rma_info
.mmn_list
) &&
72 !atomic_read(&ep
->rma_info
.tw_refcount
) &&
73 !atomic_read(&ep
->rma_info
.tcw_refcount
) &&
74 !atomic_read(&ep
->rma_info
.fence_refcount
))
76 mutex_unlock(&ep
->rma_info
.rma_lock
);
81 * scif_create_pinned_pages:
82 * @nr_pages: number of pages in window
83 * @prot: read/write protection
85 * Allocate and prepare a set of pinned pages.
87 static struct scif_pinned_pages
*
88 scif_create_pinned_pages(int nr_pages
, int prot
)
90 struct scif_pinned_pages
*pin
;
93 pin
= scif_zalloc(sizeof(*pin
));
97 pin
->pages
= scif_zalloc(nr_pages
* sizeof(*pin
->pages
));
99 goto error_free_pinned_pages
;
102 pin
->magic
= SCIFEP_MAGIC
;
105 error_free_pinned_pages
:
106 scif_free(pin
, sizeof(*pin
));
112 * scif_destroy_pinned_pages:
113 * @pin: A set of pinned pages.
115 * Deallocate resources for pinned pages.
117 static int scif_destroy_pinned_pages(struct scif_pinned_pages
*pin
)
120 int writeable
= pin
->prot
& SCIF_PROT_WRITE
;
121 int kernel
= SCIF_MAP_KERNEL
& pin
->map_flags
;
123 for (j
= 0; j
< pin
->nr_pages
; j
++) {
124 if (pin
->pages
[j
] && !kernel
) {
126 SetPageDirty(pin
->pages
[j
]);
127 put_page(pin
->pages
[j
]);
131 scif_free(pin
->pages
,
132 pin
->nr_pages
* sizeof(*pin
->pages
));
133 scif_free(pin
, sizeof(*pin
));
138 * scif_create_window:
140 * @nr_pages: number of pages
141 * @offset: registration offset
142 * @temp: true if a temporary window is being created
144 * Allocate and prepare a self registration window.
146 struct scif_window
*scif_create_window(struct scif_endpt
*ep
, int nr_pages
,
147 s64 offset
, bool temp
)
149 struct scif_window
*window
;
152 window
= scif_zalloc(sizeof(*window
));
156 window
->dma_addr
= scif_zalloc(nr_pages
* sizeof(*window
->dma_addr
));
157 if (!window
->dma_addr
)
158 goto error_free_window
;
160 window
->num_pages
= scif_zalloc(nr_pages
* sizeof(*window
->num_pages
));
161 if (!window
->num_pages
)
162 goto error_free_window
;
164 window
->offset
= offset
;
165 window
->ep
= (u64
)ep
;
166 window
->magic
= SCIFEP_MAGIC
;
167 window
->reg_state
= OP_IDLE
;
168 init_waitqueue_head(&window
->regwq
);
169 window
->unreg_state
= OP_IDLE
;
170 init_waitqueue_head(&window
->unregwq
);
171 INIT_LIST_HEAD(&window
->list
);
172 window
->type
= SCIF_WINDOW_SELF
;
177 scif_free(window
->dma_addr
,
178 nr_pages
* sizeof(*window
->dma_addr
));
179 scif_free(window
, sizeof(*window
));
185 * scif_destroy_incomplete_window:
187 * @window: registration window
189 * Deallocate resources for self window.
191 static void scif_destroy_incomplete_window(struct scif_endpt
*ep
,
192 struct scif_window
*window
)
195 int nr_pages
= window
->nr_pages
;
196 struct scif_allocmsg
*alloc
= &window
->alloc_handle
;
200 /* Wait for a SCIF_ALLOC_GNT/REJ message */
201 err
= wait_event_timeout(alloc
->allocwq
,
202 alloc
->state
!= OP_IN_PROGRESS
,
203 SCIF_NODE_ALIVE_TIMEOUT
);
204 if (!err
&& scifdev_alive(ep
))
207 mutex_lock(&ep
->rma_info
.rma_lock
);
208 if (alloc
->state
== OP_COMPLETED
) {
209 msg
.uop
= SCIF_FREE_VIRT
;
211 msg
.payload
[0] = ep
->remote_ep
;
212 msg
.payload
[1] = window
->alloc_handle
.vaddr
;
213 msg
.payload
[2] = (u64
)window
;
214 msg
.payload
[3] = SCIF_REGISTER
;
215 _scif_nodeqp_send(ep
->remote_dev
, &msg
);
217 mutex_unlock(&ep
->rma_info
.rma_lock
);
219 scif_free_window_offset(ep
, window
, window
->offset
);
220 scif_free(window
->dma_addr
, nr_pages
* sizeof(*window
->dma_addr
));
221 scif_free(window
->num_pages
, nr_pages
* sizeof(*window
->num_pages
));
222 scif_free(window
, sizeof(*window
));
227 * @remote_dev: SCIF remote device
228 * @window: registration window
230 * Delete any DMA mappings created for a registered self window
232 void scif_unmap_window(struct scif_dev
*remote_dev
, struct scif_window
*window
)
236 if (scif_is_iommu_enabled() && !scifdev_self(remote_dev
)) {
238 dma_unmap_sg(&remote_dev
->sdev
->dev
,
239 window
->st
->sgl
, window
->st
->nents
,
241 sg_free_table(window
->st
);
246 for (j
= 0; j
< window
->nr_contig_chunks
; j
++) {
247 if (window
->dma_addr
[j
]) {
248 scif_unmap_single(window
->dma_addr
[j
],
250 window
->num_pages
[j
] <<
252 window
->dma_addr
[j
] = 0x0;
258 static inline struct mm_struct
*__scif_acquire_mm(void)
260 if (scif_ulimit_check
)
261 return get_task_mm(current
);
265 static inline void __scif_release_mm(struct mm_struct
*mm
)
272 __scif_dec_pinned_vm_lock(struct mm_struct
*mm
,
273 int nr_pages
, bool try_lock
)
275 if (!mm
|| !nr_pages
|| !scif_ulimit_check
)
278 if (!down_write_trylock(&mm
->mmap_sem
)) {
279 dev_err(scif_info
.mdev
.this_device
,
280 "%s %d err\n", __func__
, __LINE__
);
284 down_write(&mm
->mmap_sem
);
286 mm
->pinned_vm
-= nr_pages
;
287 up_write(&mm
->mmap_sem
);
291 static inline int __scif_check_inc_pinned_vm(struct mm_struct
*mm
,
294 unsigned long locked
, lock_limit
;
296 if (!mm
|| !nr_pages
|| !scif_ulimit_check
)
300 locked
+= mm
->pinned_vm
;
301 lock_limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
302 if ((locked
> lock_limit
) && !capable(CAP_IPC_LOCK
)) {
303 dev_err(scif_info
.mdev
.this_device
,
304 "locked(%lu) > lock_limit(%lu)\n",
308 mm
->pinned_vm
= locked
;
313 * scif_destroy_window:
315 * @window: registration window
317 * Deallocate resources for self window.
319 int scif_destroy_window(struct scif_endpt
*ep
, struct scif_window
*window
)
322 struct scif_pinned_pages
*pinned_pages
= window
->pinned_pages
;
323 int nr_pages
= window
->nr_pages
;
326 if (!window
->temp
&& window
->mm
) {
327 __scif_dec_pinned_vm_lock(window
->mm
, window
->nr_pages
, 0);
328 __scif_release_mm(window
->mm
);
332 scif_free_window_offset(ep
, window
, window
->offset
);
333 scif_unmap_window(ep
->remote_dev
, window
);
335 * Decrement references for this set of pinned pages from
338 j
= atomic_sub_return(1, &pinned_pages
->ref_count
);
340 dev_err(scif_info
.mdev
.this_device
,
341 "%s %d incorrect ref count %d\n",
342 __func__
, __LINE__
, j
);
344 * If the ref count for pinned_pages is zero then someone
345 * has already called scif_unpin_pages() for it and we should
346 * destroy the page cache.
349 scif_destroy_pinned_pages(window
->pinned_pages
);
350 scif_free(window
->dma_addr
, nr_pages
* sizeof(*window
->dma_addr
));
351 scif_free(window
->num_pages
, nr_pages
* sizeof(*window
->num_pages
));
353 scif_free(window
, sizeof(*window
));
358 * scif_create_remote_lookup:
359 * @remote_dev: SCIF remote device
360 * @window: remote window
362 * Allocate and prepare lookup entries for the remote
363 * end to copy over the physical addresses.
364 * Returns 0 on success and appropriate errno on failure.
366 static int scif_create_remote_lookup(struct scif_dev
*remote_dev
,
367 struct scif_window
*window
)
370 int nr_pages
= window
->nr_pages
;
371 bool vmalloc_dma_phys
, vmalloc_num_pages
;
375 err
= scif_map_single(&window
->mapped_offset
,
376 window
, remote_dev
, sizeof(*window
));
380 /* Compute the number of lookup entries. 21 == 2MB Shift */
381 window
->nr_lookup
= ALIGN(nr_pages
* PAGE_SIZE
,
382 ((2) * 1024 * 1024)) >> 21;
384 window
->dma_addr_lookup
.lookup
=
385 scif_alloc_coherent(&window
->dma_addr_lookup
.offset
,
386 remote_dev
, window
->nr_lookup
*
387 sizeof(*window
->dma_addr_lookup
.lookup
),
388 GFP_KERNEL
| __GFP_ZERO
);
389 if (!window
->dma_addr_lookup
.lookup
) {
394 window
->num_pages_lookup
.lookup
=
395 scif_alloc_coherent(&window
->num_pages_lookup
.offset
,
396 remote_dev
, window
->nr_lookup
*
397 sizeof(*window
->num_pages_lookup
.lookup
),
398 GFP_KERNEL
| __GFP_ZERO
);
399 if (!window
->num_pages_lookup
.lookup
) {
404 vmalloc_dma_phys
= is_vmalloc_addr(&window
->dma_addr
[0]);
405 vmalloc_num_pages
= is_vmalloc_addr(&window
->num_pages
[0]);
407 /* Now map each of the pages containing physical addresses */
408 for (i
= 0, j
= 0; i
< nr_pages
; i
+= SCIF_NR_ADDR_IN_PAGE
, j
++) {
409 err
= scif_map_page(&window
->dma_addr_lookup
.lookup
[j
],
411 vmalloc_to_page(&window
->dma_addr
[i
]) :
412 virt_to_page(&window
->dma_addr
[i
]),
416 err
= scif_map_page(&window
->num_pages_lookup
.lookup
[j
],
418 vmalloc_to_page(&window
->num_pages
[i
]) :
419 virt_to_page(&window
->num_pages
[i
]),
430 * scif_destroy_remote_lookup:
431 * @remote_dev: SCIF remote device
432 * @window: remote window
434 * Destroy lookup entries used for the remote
435 * end to copy over the physical addresses.
437 static void scif_destroy_remote_lookup(struct scif_dev
*remote_dev
,
438 struct scif_window
*window
)
442 if (window
->nr_lookup
) {
443 struct scif_rma_lookup
*lup
= &window
->dma_addr_lookup
;
444 struct scif_rma_lookup
*npup
= &window
->num_pages_lookup
;
446 for (i
= 0, j
= 0; i
< window
->nr_pages
;
447 i
+= SCIF_NR_ADDR_IN_PAGE
, j
++) {
448 if (lup
->lookup
&& lup
->lookup
[j
])
449 scif_unmap_single(lup
->lookup
[j
],
452 if (npup
->lookup
&& npup
->lookup
[j
])
453 scif_unmap_single(npup
->lookup
[j
],
458 scif_free_coherent(lup
->lookup
, lup
->offset
,
459 remote_dev
, window
->nr_lookup
*
460 sizeof(*lup
->lookup
));
462 scif_free_coherent(npup
->lookup
, npup
->offset
,
463 remote_dev
, window
->nr_lookup
*
464 sizeof(*npup
->lookup
));
465 if (window
->mapped_offset
)
466 scif_unmap_single(window
->mapped_offset
,
467 remote_dev
, sizeof(*window
));
468 window
->nr_lookup
= 0;
473 * scif_create_remote_window:
475 * @nr_pages: number of pages in window
477 * Allocate and prepare a remote registration window.
479 static struct scif_window
*
480 scif_create_remote_window(struct scif_dev
*scifdev
, int nr_pages
)
482 struct scif_window
*window
;
485 window
= scif_zalloc(sizeof(*window
));
489 window
->magic
= SCIFEP_MAGIC
;
490 window
->nr_pages
= nr_pages
;
492 window
->dma_addr
= scif_zalloc(nr_pages
* sizeof(*window
->dma_addr
));
493 if (!window
->dma_addr
)
496 window
->num_pages
= scif_zalloc(nr_pages
*
497 sizeof(*window
->num_pages
));
498 if (!window
->num_pages
)
501 if (scif_create_remote_lookup(scifdev
, window
))
504 window
->type
= SCIF_WINDOW_PEER
;
505 window
->unreg_state
= OP_IDLE
;
506 INIT_LIST_HEAD(&window
->list
);
509 scif_destroy_remote_window(window
);
515 * scif_destroy_remote_window:
517 * @window: remote registration window
519 * Deallocate resources for remote window.
522 scif_destroy_remote_window(struct scif_window
*window
)
524 scif_free(window
->dma_addr
, window
->nr_pages
*
525 sizeof(*window
->dma_addr
));
526 scif_free(window
->num_pages
, window
->nr_pages
*
527 sizeof(*window
->num_pages
));
529 scif_free(window
, sizeof(*window
));
533 * scif_iommu_map: create DMA mappings if the IOMMU is enabled
534 * @remote_dev: SCIF remote device
535 * @window: remote registration window
537 * Map the physical pages using dma_map_sg(..) and then detect the number
538 * of contiguous DMA mappings allocated
540 static int scif_iommu_map(struct scif_dev
*remote_dev
,
541 struct scif_window
*window
)
543 struct scatterlist
*sg
;
545 scif_pinned_pages_t pin
= window
->pinned_pages
;
547 window
->st
= kzalloc(sizeof(*window
->st
), GFP_KERNEL
);
551 err
= sg_alloc_table(window
->st
, window
->nr_pages
, GFP_KERNEL
);
555 for_each_sg(window
->st
->sgl
, sg
, window
->st
->nents
, i
)
556 sg_set_page(sg
, pin
->pages
[i
], PAGE_SIZE
, 0x0);
558 err
= dma_map_sg(&remote_dev
->sdev
->dev
, window
->st
->sgl
,
559 window
->st
->nents
, DMA_BIDIRECTIONAL
);
562 /* Detect contiguous ranges of DMA mappings */
563 sg
= window
->st
->sgl
;
564 for (i
= 0; sg
; i
++) {
567 window
->dma_addr
[i
] = sg_dma_address(sg
);
568 window
->num_pages
[i
] = sg_dma_len(sg
) >> PAGE_SHIFT
;
569 last_da
= sg_dma_address(sg
) + sg_dma_len(sg
);
570 while ((sg
= sg_next(sg
)) && sg_dma_address(sg
) == last_da
) {
571 window
->num_pages
[i
] +=
572 (sg_dma_len(sg
) >> PAGE_SHIFT
);
573 last_da
= window
->dma_addr
[i
] +
576 window
->nr_contig_chunks
++;
583 * @remote_dev: SCIF remote device
584 * @window: self registration window
586 * Map pages of a window into the aperture/PCI.
587 * Also determine addresses required for DMA.
590 scif_map_window(struct scif_dev
*remote_dev
, struct scif_window
*window
)
592 int i
, j
, k
, err
= 0, nr_contig_pages
;
593 scif_pinned_pages_t pin
;
594 phys_addr_t phys_prev
, phys_curr
;
598 pin
= window
->pinned_pages
;
600 if (intel_iommu_enabled
&& !scifdev_self(remote_dev
))
601 return scif_iommu_map(remote_dev
, window
);
603 for (i
= 0, j
= 0; i
< window
->nr_pages
; i
+= nr_contig_pages
, j
++) {
604 phys_prev
= page_to_phys(pin
->pages
[i
]);
607 /* Detect physically contiguous chunks */
608 for (k
= i
+ 1; k
< window
->nr_pages
; k
++) {
609 phys_curr
= page_to_phys(pin
->pages
[k
]);
610 if (phys_curr
!= (phys_prev
+ PAGE_SIZE
))
612 phys_prev
= phys_curr
;
615 window
->num_pages
[j
] = nr_contig_pages
;
616 window
->nr_contig_chunks
++;
617 if (scif_is_mgmt_node()) {
619 * Management node has to deal with SMPT on X100 and
620 * hence the DMA mapping is required
622 err
= scif_map_single(&window
->dma_addr
[j
],
623 phys_to_virt(page_to_phys(
626 nr_contig_pages
<< PAGE_SHIFT
);
630 window
->dma_addr
[j
] = page_to_phys(pin
->pages
[i
]);
637 * scif_send_scif_unregister:
639 * @window: self registration window
641 * Send a SCIF_UNREGISTER message.
643 static int scif_send_scif_unregister(struct scif_endpt
*ep
,
644 struct scif_window
*window
)
648 msg
.uop
= SCIF_UNREGISTER
;
650 msg
.payload
[0] = window
->alloc_handle
.vaddr
;
651 msg
.payload
[1] = (u64
)window
;
652 return scif_nodeqp_send(ep
->remote_dev
, &msg
);
656 * scif_unregister_window:
657 * @window: self registration window
659 * Send an unregistration request and wait for a response.
661 int scif_unregister_window(struct scif_window
*window
)
664 struct scif_endpt
*ep
= (struct scif_endpt
*)window
->ep
;
665 bool send_msg
= false;
668 switch (window
->unreg_state
) {
671 window
->unreg_state
= OP_IN_PROGRESS
;
677 scif_get_window(window
, 1);
678 mutex_unlock(&ep
->rma_info
.rma_lock
);
680 err
= scif_send_scif_unregister(ep
, window
);
682 window
->unreg_state
= OP_COMPLETED
;
686 /* Return ENXIO since unregistration is in progress */
687 mutex_lock(&ep
->rma_info
.rma_lock
);
691 /* Wait for a SCIF_UNREGISTER_(N)ACK message */
692 err
= wait_event_timeout(window
->unregwq
,
693 window
->unreg_state
!= OP_IN_PROGRESS
,
694 SCIF_NODE_ALIVE_TIMEOUT
);
695 if (!err
&& scifdev_alive(ep
))
699 window
->unreg_state
= OP_COMPLETED
;
700 dev_err(scif_info
.mdev
.this_device
,
701 "%s %d err %d\n", __func__
, __LINE__
, err
);
706 mutex_lock(&ep
->rma_info
.rma_lock
);
707 scif_put_window(window
, 1);
712 if (!scifdev_alive(ep
)) {
714 window
->unreg_state
= OP_COMPLETED
;
724 if (window
->unreg_state
== OP_COMPLETED
&& window
->ref_count
)
725 scif_put_window(window
, window
->nr_pages
);
727 if (!window
->ref_count
) {
728 atomic_inc(&ep
->rma_info
.tw_refcount
);
729 list_del_init(&window
->list
);
730 scif_free_window_offset(ep
, window
, window
->offset
);
731 mutex_unlock(&ep
->rma_info
.rma_lock
);
732 if ((!!(window
->pinned_pages
->map_flags
& SCIF_MAP_KERNEL
)) &&
734 scif_drain_dma_intr(ep
->remote_dev
->sdev
,
735 ep
->rma_info
.dma_chan
);
737 if (!__scif_dec_pinned_vm_lock(window
->mm
,
738 window
->nr_pages
, 1)) {
739 __scif_release_mm(window
->mm
);
743 scif_queue_for_cleanup(window
, &scif_info
.rma
);
744 mutex_lock(&ep
->rma_info
.rma_lock
);
750 * scif_send_alloc_request:
752 * @window: self registration window
754 * Send a remote window allocation request
756 static int scif_send_alloc_request(struct scif_endpt
*ep
,
757 struct scif_window
*window
)
760 struct scif_allocmsg
*alloc
= &window
->alloc_handle
;
762 /* Set up the Alloc Handle */
763 alloc
->state
= OP_IN_PROGRESS
;
764 init_waitqueue_head(&alloc
->allocwq
);
766 /* Send out an allocation request */
767 msg
.uop
= SCIF_ALLOC_REQ
;
768 msg
.payload
[1] = window
->nr_pages
;
769 msg
.payload
[2] = (u64
)&window
->alloc_handle
;
770 return _scif_nodeqp_send(ep
->remote_dev
, &msg
);
774 * scif_prep_remote_window:
776 * @window: self registration window
778 * Send a remote window allocation request, wait for an allocation response,
779 * and prepares the remote window by copying over the page lists
781 static int scif_prep_remote_window(struct scif_endpt
*ep
,
782 struct scif_window
*window
)
785 struct scif_window
*remote_window
;
786 struct scif_allocmsg
*alloc
= &window
->alloc_handle
;
787 dma_addr_t
*dma_phys_lookup
, *tmp
, *num_pages_lookup
, *tmp1
;
789 int nr_contig_chunks
, loop_nr_contig_chunks
;
790 int remaining_nr_contig_chunks
, nr_lookup
;
793 map_err
= scif_map_window(ep
->remote_dev
, window
);
795 dev_err(&ep
->remote_dev
->sdev
->dev
,
796 "%s %d map_err %d\n", __func__
, __LINE__
, map_err
);
797 remaining_nr_contig_chunks
= window
->nr_contig_chunks
;
798 nr_contig_chunks
= window
->nr_contig_chunks
;
800 /* Wait for a SCIF_ALLOC_GNT/REJ message */
801 err
= wait_event_timeout(alloc
->allocwq
,
802 alloc
->state
!= OP_IN_PROGRESS
,
803 SCIF_NODE_ALIVE_TIMEOUT
);
804 mutex_lock(&ep
->rma_info
.rma_lock
);
805 /* Synchronize with the thread waking up allocwq */
806 mutex_unlock(&ep
->rma_info
.rma_lock
);
807 if (!err
&& scifdev_alive(ep
))
818 /* Bail out. The remote end rejected this request */
819 if (alloc
->state
== OP_FAILED
)
823 dev_err(&ep
->remote_dev
->sdev
->dev
,
824 "%s %d err %d\n", __func__
, __LINE__
, map_err
);
825 msg
.uop
= SCIF_FREE_VIRT
;
827 msg
.payload
[0] = ep
->remote_ep
;
828 msg
.payload
[1] = window
->alloc_handle
.vaddr
;
829 msg
.payload
[2] = (u64
)window
;
830 msg
.payload
[3] = SCIF_REGISTER
;
831 spin_lock(&ep
->lock
);
832 if (ep
->state
== SCIFEP_CONNECTED
)
833 err
= _scif_nodeqp_send(ep
->remote_dev
, &msg
);
836 spin_unlock(&ep
->lock
);
840 remote_window
= scif_ioremap(alloc
->phys_addr
, sizeof(*window
),
843 /* Compute the number of lookup entries. 21 == 2MB Shift */
844 nr_lookup
= ALIGN(nr_contig_chunks
, SCIF_NR_ADDR_IN_PAGE
)
845 >> ilog2(SCIF_NR_ADDR_IN_PAGE
);
848 scif_ioremap(remote_window
->dma_addr_lookup
.offset
,
850 sizeof(*remote_window
->dma_addr_lookup
.lookup
),
853 scif_ioremap(remote_window
->num_pages_lookup
.offset
,
855 sizeof(*remote_window
->num_pages_lookup
.lookup
),
858 while (remaining_nr_contig_chunks
) {
859 loop_nr_contig_chunks
= min_t(int, remaining_nr_contig_chunks
,
860 (int)SCIF_NR_ADDR_IN_PAGE
);
861 /* #1/2 - Copy physical addresses over to the remote side */
863 /* #2/2 - Copy DMA addresses (addresses that are fed into the
864 * DMA engine) We transfer bus addresses which are then
865 * converted into a MIC physical address on the remote
866 * side if it is a MIC, if the remote node is a mgmt node we
867 * transfer the MIC physical address
869 tmp
= scif_ioremap(dma_phys_lookup
[j
],
870 loop_nr_contig_chunks
*
871 sizeof(*window
->dma_addr
),
873 tmp1
= scif_ioremap(num_pages_lookup
[j
],
874 loop_nr_contig_chunks
*
875 sizeof(*window
->num_pages
),
877 if (scif_is_mgmt_node()) {
878 memcpy_toio((void __force __iomem
*)tmp
,
879 &window
->dma_addr
[i
], loop_nr_contig_chunks
880 * sizeof(*window
->dma_addr
));
881 memcpy_toio((void __force __iomem
*)tmp1
,
882 &window
->num_pages
[i
], loop_nr_contig_chunks
883 * sizeof(*window
->num_pages
));
885 if (scifdev_is_p2p(ep
->remote_dev
)) {
887 * add remote node's base address for this node
888 * to convert it into a MIC address
893 for (m
= 0; m
< loop_nr_contig_chunks
; m
++) {
894 dma_addr
= window
->dma_addr
[i
+ m
] +
895 ep
->remote_dev
->base_addr
;
897 (void __force __iomem
*)&tmp
[m
]);
899 memcpy_toio((void __force __iomem
*)tmp1
,
900 &window
->num_pages
[i
],
901 loop_nr_contig_chunks
902 * sizeof(*window
->num_pages
));
904 /* Mgmt node or loopback - transfer DMA
905 * addresses as is, this is the same as a
906 * MIC physical address (we use the dma_addr
907 * and not the phys_addr array since the
908 * phys_addr is only setup if there is a mmap()
909 * request from the mgmt node)
911 memcpy_toio((void __force __iomem
*)tmp
,
912 &window
->dma_addr
[i
],
913 loop_nr_contig_chunks
*
914 sizeof(*window
->dma_addr
));
915 memcpy_toio((void __force __iomem
*)tmp1
,
916 &window
->num_pages
[i
],
917 loop_nr_contig_chunks
*
918 sizeof(*window
->num_pages
));
921 remaining_nr_contig_chunks
-= loop_nr_contig_chunks
;
922 i
+= loop_nr_contig_chunks
;
924 scif_iounmap(tmp
, loop_nr_contig_chunks
*
925 sizeof(*window
->dma_addr
), ep
->remote_dev
);
926 scif_iounmap(tmp1
, loop_nr_contig_chunks
*
927 sizeof(*window
->num_pages
), ep
->remote_dev
);
930 /* Prepare the remote window for the peer */
931 remote_window
->peer_window
= (u64
)window
;
932 remote_window
->offset
= window
->offset
;
933 remote_window
->prot
= window
->prot
;
934 remote_window
->nr_contig_chunks
= nr_contig_chunks
;
935 remote_window
->ep
= ep
->remote_ep
;
936 scif_iounmap(num_pages_lookup
,
938 sizeof(*remote_window
->num_pages_lookup
.lookup
),
940 scif_iounmap(dma_phys_lookup
,
942 sizeof(*remote_window
->dma_addr_lookup
.lookup
),
944 scif_iounmap(remote_window
, sizeof(*remote_window
), ep
->remote_dev
);
945 window
->peer_window
= alloc
->vaddr
;
950 * scif_send_scif_register:
952 * @window: self registration window
954 * Send a SCIF_REGISTER message if EP is connected and wait for a
955 * SCIF_REGISTER_(N)ACK message else send a SCIF_FREE_VIRT
956 * message so that the peer can free its remote window allocated earlier.
958 static int scif_send_scif_register(struct scif_endpt
*ep
,
959 struct scif_window
*window
)
965 msg
.payload
[0] = ep
->remote_ep
;
966 msg
.payload
[1] = window
->alloc_handle
.vaddr
;
967 msg
.payload
[2] = (u64
)window
;
968 spin_lock(&ep
->lock
);
969 if (ep
->state
== SCIFEP_CONNECTED
) {
970 msg
.uop
= SCIF_REGISTER
;
971 window
->reg_state
= OP_IN_PROGRESS
;
972 err
= _scif_nodeqp_send(ep
->remote_dev
, &msg
);
973 spin_unlock(&ep
->lock
);
976 /* Wait for a SCIF_REGISTER_(N)ACK message */
977 err
= wait_event_timeout(window
->regwq
,
980 SCIF_NODE_ALIVE_TIMEOUT
);
981 if (!err
&& scifdev_alive(ep
))
983 err
= !err
? -ENODEV
: 0;
984 if (window
->reg_state
== OP_FAILED
)
988 msg
.uop
= SCIF_FREE_VIRT
;
989 msg
.payload
[3] = SCIF_REGISTER
;
990 err
= _scif_nodeqp_send(ep
->remote_dev
, &msg
);
991 spin_unlock(&ep
->lock
);
999 * scif_get_window_offset:
1000 * @ep: end point descriptor
1002 * @offset: offset hint
1003 * @num_pages: number of pages
1004 * @out_offset: computed offset returned by reference.
1006 * Compute/Claim a new offset for this EP.
1008 int scif_get_window_offset(struct scif_endpt
*ep
, int flags
, s64 offset
,
1009 int num_pages
, s64
*out_offset
)
1012 struct iova
*iova_ptr
;
1015 if (flags
& SCIF_MAP_FIXED
) {
1016 page_index
= SCIF_IOVA_PFN(offset
);
1017 iova_ptr
= reserve_iova(&ep
->rma_info
.iovad
, page_index
,
1018 page_index
+ num_pages
- 1);
1022 iova_ptr
= alloc_iova(&ep
->rma_info
.iovad
, num_pages
,
1023 SCIF_DMA_63BIT_PFN
- 1, 0);
1028 *out_offset
= (iova_ptr
->pfn_lo
) << PAGE_SHIFT
;
1033 * scif_free_window_offset:
1034 * @ep: end point descriptor
1035 * @window: registration window
1036 * @offset: Offset to be freed
1038 * Free offset for this EP. The callee is supposed to grab
1039 * the RMA mutex before calling this API.
1041 void scif_free_window_offset(struct scif_endpt
*ep
,
1042 struct scif_window
*window
, s64 offset
)
1044 if ((window
&& !window
->offset_freed
) || !window
) {
1045 free_iova(&ep
->rma_info
.iovad
, offset
>> PAGE_SHIFT
);
1047 window
->offset_freed
= true;
1052 * scif_alloc_req: Respond to SCIF_ALLOC_REQ interrupt message
1053 * @msg: Interrupt message
1055 * Remote side is requesting a memory allocation.
1057 void scif_alloc_req(struct scif_dev
*scifdev
, struct scifmsg
*msg
)
1060 struct scif_window
*window
= NULL
;
1061 int nr_pages
= msg
->payload
[1];
1063 window
= scif_create_remote_window(scifdev
, nr_pages
);
1069 /* The peer's allocation request is granted */
1070 msg
->uop
= SCIF_ALLOC_GNT
;
1071 msg
->payload
[0] = (u64
)window
;
1072 msg
->payload
[1] = window
->mapped_offset
;
1073 err
= scif_nodeqp_send(scifdev
, msg
);
1075 scif_destroy_remote_window(window
);
1078 /* The peer's allocation request is rejected */
1079 dev_err(&scifdev
->sdev
->dev
,
1080 "%s %d error %d alloc_ptr %p nr_pages 0x%x\n",
1081 __func__
, __LINE__
, err
, window
, nr_pages
);
1082 msg
->uop
= SCIF_ALLOC_REJ
;
1083 scif_nodeqp_send(scifdev
, msg
);
1087 * scif_alloc_gnt_rej: Respond to SCIF_ALLOC_GNT/REJ interrupt message
1088 * @msg: Interrupt message
1090 * Remote side responded to a memory allocation.
1092 void scif_alloc_gnt_rej(struct scif_dev
*scifdev
, struct scifmsg
*msg
)
1094 struct scif_allocmsg
*handle
= (struct scif_allocmsg
*)msg
->payload
[2];
1095 struct scif_window
*window
= container_of(handle
, struct scif_window
,
1097 struct scif_endpt
*ep
= (struct scif_endpt
*)window
->ep
;
1099 mutex_lock(&ep
->rma_info
.rma_lock
);
1100 handle
->vaddr
= msg
->payload
[0];
1101 handle
->phys_addr
= msg
->payload
[1];
1102 if (msg
->uop
== SCIF_ALLOC_GNT
)
1103 handle
->state
= OP_COMPLETED
;
1105 handle
->state
= OP_FAILED
;
1106 wake_up(&handle
->allocwq
);
1107 mutex_unlock(&ep
->rma_info
.rma_lock
);
1111 * scif_free_virt: Respond to SCIF_FREE_VIRT interrupt message
1112 * @msg: Interrupt message
1114 * Free up memory kmalloc'd earlier.
1116 void scif_free_virt(struct scif_dev
*scifdev
, struct scifmsg
*msg
)
1118 struct scif_window
*window
= (struct scif_window
*)msg
->payload
[1];
1120 scif_destroy_remote_window(window
);
1124 scif_fixup_aper_base(struct scif_dev
*dev
, struct scif_window
*window
)
1127 struct scif_hw_dev
*sdev
= dev
->sdev
;
1128 phys_addr_t apt_base
= 0;
1131 * Add the aperture base if the DMA address is not card relative
1132 * since the DMA addresses need to be an offset into the bar
1134 if (!scifdev_self(dev
) && window
->type
== SCIF_WINDOW_PEER
&&
1135 sdev
->aper
&& !sdev
->card_rel_da
)
1136 apt_base
= sdev
->aper
->pa
;
1140 for (j
= 0; j
< window
->nr_contig_chunks
; j
++) {
1141 if (window
->num_pages
[j
])
1142 window
->dma_addr
[j
] += apt_base
;
1149 * scif_recv_reg: Respond to SCIF_REGISTER interrupt message
1150 * @msg: Interrupt message
1152 * Update remote window list with a new registered window.
1154 void scif_recv_reg(struct scif_dev
*scifdev
, struct scifmsg
*msg
)
1156 struct scif_endpt
*ep
= (struct scif_endpt
*)msg
->payload
[0];
1157 struct scif_window
*window
=
1158 (struct scif_window
*)msg
->payload
[1];
1160 mutex_lock(&ep
->rma_info
.rma_lock
);
1161 spin_lock(&ep
->lock
);
1162 if (ep
->state
== SCIFEP_CONNECTED
) {
1163 msg
->uop
= SCIF_REGISTER_ACK
;
1164 scif_nodeqp_send(ep
->remote_dev
, msg
);
1165 scif_fixup_aper_base(ep
->remote_dev
, window
);
1166 /* No further failures expected. Insert new window */
1167 scif_insert_window(window
, &ep
->rma_info
.remote_reg_list
);
1169 msg
->uop
= SCIF_REGISTER_NACK
;
1170 scif_nodeqp_send(ep
->remote_dev
, msg
);
1172 spin_unlock(&ep
->lock
);
1173 mutex_unlock(&ep
->rma_info
.rma_lock
);
1174 /* free up any lookup resources now that page lists are transferred */
1175 scif_destroy_remote_lookup(ep
->remote_dev
, window
);
1177 * We could not insert the window but we need to
1178 * destroy the window.
1180 if (msg
->uop
== SCIF_REGISTER_NACK
)
1181 scif_destroy_remote_window(window
);
1185 * scif_recv_unreg: Respond to SCIF_UNREGISTER interrupt message
1186 * @msg: Interrupt message
1188 * Remove window from remote registration list;
1190 void scif_recv_unreg(struct scif_dev
*scifdev
, struct scifmsg
*msg
)
1192 struct scif_rma_req req
;
1193 struct scif_window
*window
= NULL
;
1194 struct scif_window
*recv_window
=
1195 (struct scif_window
*)msg
->payload
[0];
1196 struct scif_endpt
*ep
;
1199 ep
= (struct scif_endpt
*)recv_window
->ep
;
1200 req
.out_window
= &window
;
1201 req
.offset
= recv_window
->offset
;
1203 req
.nr_bytes
= recv_window
->nr_pages
<< PAGE_SHIFT
;
1204 req
.type
= SCIF_WINDOW_FULL
;
1205 req
.head
= &ep
->rma_info
.remote_reg_list
;
1206 msg
->payload
[0] = ep
->remote_ep
;
1208 mutex_lock(&ep
->rma_info
.rma_lock
);
1209 /* Does a valid window exist? */
1210 if (scif_query_window(&req
)) {
1211 dev_err(&scifdev
->sdev
->dev
,
1212 "%s %d -ENXIO\n", __func__
, __LINE__
);
1213 msg
->uop
= SCIF_UNREGISTER_ACK
;
1217 if (window
->ref_count
)
1218 scif_put_window(window
, window
->nr_pages
);
1220 dev_err(&scifdev
->sdev
->dev
,
1221 "%s %d ref count should be +ve\n",
1222 __func__
, __LINE__
);
1223 window
->unreg_state
= OP_COMPLETED
;
1224 if (!window
->ref_count
) {
1225 msg
->uop
= SCIF_UNREGISTER_ACK
;
1226 atomic_inc(&ep
->rma_info
.tw_refcount
);
1227 ep
->rma_info
.async_list_del
= 1;
1228 list_del_init(&window
->list
);
1231 /* NACK! There are valid references to this window */
1232 msg
->uop
= SCIF_UNREGISTER_NACK
;
1235 /* The window did not make its way to the list at all. ACK */
1236 msg
->uop
= SCIF_UNREGISTER_ACK
;
1237 scif_destroy_remote_window(recv_window
);
1240 mutex_unlock(&ep
->rma_info
.rma_lock
);
1242 scif_drain_dma_intr(ep
->remote_dev
->sdev
,
1243 ep
->rma_info
.dma_chan
);
1244 scif_nodeqp_send(ep
->remote_dev
, msg
);
1246 scif_queue_for_cleanup(window
, &scif_info
.rma
);
1250 * scif_recv_reg_ack: Respond to SCIF_REGISTER_ACK interrupt message
1251 * @msg: Interrupt message
1253 * Wake up the window waiting to complete registration.
1255 void scif_recv_reg_ack(struct scif_dev
*scifdev
, struct scifmsg
*msg
)
1257 struct scif_window
*window
=
1258 (struct scif_window
*)msg
->payload
[2];
1259 struct scif_endpt
*ep
= (struct scif_endpt
*)window
->ep
;
1261 mutex_lock(&ep
->rma_info
.rma_lock
);
1262 window
->reg_state
= OP_COMPLETED
;
1263 wake_up(&window
->regwq
);
1264 mutex_unlock(&ep
->rma_info
.rma_lock
);
1268 * scif_recv_reg_nack: Respond to SCIF_REGISTER_NACK interrupt message
1269 * @msg: Interrupt message
1271 * Wake up the window waiting to inform it that registration
1272 * cannot be completed.
1274 void scif_recv_reg_nack(struct scif_dev
*scifdev
, struct scifmsg
*msg
)
1276 struct scif_window
*window
=
1277 (struct scif_window
*)msg
->payload
[2];
1278 struct scif_endpt
*ep
= (struct scif_endpt
*)window
->ep
;
1280 mutex_lock(&ep
->rma_info
.rma_lock
);
1281 window
->reg_state
= OP_FAILED
;
1282 wake_up(&window
->regwq
);
1283 mutex_unlock(&ep
->rma_info
.rma_lock
);
1287 * scif_recv_unreg_ack: Respond to SCIF_UNREGISTER_ACK interrupt message
1288 * @msg: Interrupt message
1290 * Wake up the window waiting to complete unregistration.
1292 void scif_recv_unreg_ack(struct scif_dev
*scifdev
, struct scifmsg
*msg
)
1294 struct scif_window
*window
=
1295 (struct scif_window
*)msg
->payload
[1];
1296 struct scif_endpt
*ep
= (struct scif_endpt
*)window
->ep
;
1298 mutex_lock(&ep
->rma_info
.rma_lock
);
1299 window
->unreg_state
= OP_COMPLETED
;
1300 wake_up(&window
->unregwq
);
1301 mutex_unlock(&ep
->rma_info
.rma_lock
);
1305 * scif_recv_unreg_nack: Respond to SCIF_UNREGISTER_NACK interrupt message
1306 * @msg: Interrupt message
1308 * Wake up the window waiting to inform it that unregistration
1309 * cannot be completed immediately.
1311 void scif_recv_unreg_nack(struct scif_dev
*scifdev
, struct scifmsg
*msg
)
1313 struct scif_window
*window
=
1314 (struct scif_window
*)msg
->payload
[1];
1315 struct scif_endpt
*ep
= (struct scif_endpt
*)window
->ep
;
1317 mutex_lock(&ep
->rma_info
.rma_lock
);
1318 window
->unreg_state
= OP_FAILED
;
1319 wake_up(&window
->unregwq
);
1320 mutex_unlock(&ep
->rma_info
.rma_lock
);
1323 int __scif_pin_pages(void *addr
, size_t len
, int *out_prot
,
1324 int map_flags
, scif_pinned_pages_t
*pages
)
1326 struct scif_pinned_pages
*pinned_pages
;
1327 int nr_pages
, err
= 0, i
;
1328 bool vmalloc_addr
= false;
1329 bool try_upgrade
= false;
1330 int prot
= *out_prot
;
1332 struct mm_struct
*mm
= NULL
;
1334 /* Unsupported flags */
1335 if (map_flags
& ~(SCIF_MAP_KERNEL
| SCIF_MAP_ULIMIT
))
1337 ulimit
= !!(map_flags
& SCIF_MAP_ULIMIT
);
1339 /* Unsupported protection requested */
1340 if (prot
& ~(SCIF_PROT_READ
| SCIF_PROT_WRITE
))
1343 /* addr/len must be page aligned. len should be non zero */
1345 (ALIGN((u64
)addr
, PAGE_SIZE
) != (u64
)addr
) ||
1346 (ALIGN((u64
)len
, PAGE_SIZE
) != (u64
)len
))
1351 nr_pages
= len
>> PAGE_SHIFT
;
1353 /* Allocate a set of pinned pages */
1354 pinned_pages
= scif_create_pinned_pages(nr_pages
, prot
);
1358 if (map_flags
& SCIF_MAP_KERNEL
) {
1359 if (is_vmalloc_addr(addr
))
1360 vmalloc_addr
= true;
1362 for (i
= 0; i
< nr_pages
; i
++) {
1364 pinned_pages
->pages
[i
] =
1365 vmalloc_to_page(addr
+ (i
* PAGE_SIZE
));
1367 pinned_pages
->pages
[i
] =
1368 virt_to_page(addr
+ (i
* PAGE_SIZE
));
1370 pinned_pages
->nr_pages
= nr_pages
;
1371 pinned_pages
->map_flags
= SCIF_MAP_KERNEL
;
1374 * SCIF supports registration caching. If a registration has
1375 * been requested with read only permissions, then we try
1376 * to pin the pages with RW permissions so that a subsequent
1377 * transfer with RW permission can hit the cache instead of
1378 * invalidating it. If the upgrade fails with RW then we
1379 * revert back to R permission and retry
1381 if (prot
== SCIF_PROT_READ
)
1383 prot
|= SCIF_PROT_WRITE
;
1386 down_write(&mm
->mmap_sem
);
1388 err
= __scif_check_inc_pinned_vm(mm
, nr_pages
);
1390 up_write(&mm
->mmap_sem
);
1391 pinned_pages
->nr_pages
= 0;
1396 pinned_pages
->nr_pages
= get_user_pages(
1399 !!(prot
& SCIF_PROT_WRITE
),
1401 pinned_pages
->pages
,
1403 up_write(&mm
->mmap_sem
);
1404 if (nr_pages
!= pinned_pages
->nr_pages
) {
1407 __scif_dec_pinned_vm_lock(mm
,
1409 /* Roll back any pinned pages */
1410 for (i
= 0; i
< pinned_pages
->nr_pages
; i
++) {
1411 if (pinned_pages
->pages
[i
])
1413 pinned_pages
->pages
[i
]);
1415 prot
&= ~SCIF_PROT_WRITE
;
1416 try_upgrade
= false;
1420 pinned_pages
->map_flags
= 0;
1423 if (pinned_pages
->nr_pages
< nr_pages
) {
1425 pinned_pages
->nr_pages
= nr_pages
;
1430 atomic_set(&pinned_pages
->ref_count
, 1);
1431 *pages
= pinned_pages
;
1435 __scif_dec_pinned_vm_lock(mm
, nr_pages
, 0);
1436 /* Something went wrong! Rollback */
1438 pinned_pages
->nr_pages
= nr_pages
;
1439 scif_destroy_pinned_pages(pinned_pages
);
1441 dev_dbg(scif_info
.mdev
.this_device
,
1442 "%s %d err %d len 0x%lx\n", __func__
, __LINE__
, err
, len
);
1446 int scif_pin_pages(void *addr
, size_t len
, int prot
,
1447 int map_flags
, scif_pinned_pages_t
*pages
)
1449 return __scif_pin_pages(addr
, len
, &prot
, map_flags
, pages
);
1451 EXPORT_SYMBOL_GPL(scif_pin_pages
);
1453 int scif_unpin_pages(scif_pinned_pages_t pinned_pages
)
1457 if (!pinned_pages
|| SCIFEP_MAGIC
!= pinned_pages
->magic
)
1460 ret
= atomic_sub_return(1, &pinned_pages
->ref_count
);
1462 dev_err(scif_info
.mdev
.this_device
,
1463 "%s %d scif_unpin_pages called without pinning? rc %d\n",
1464 __func__
, __LINE__
, ret
);
1468 * Destroy the window if the ref count for this set of pinned
1469 * pages has dropped to zero. If it is positive then there is
1470 * a valid registered window which is backed by these pages and
1471 * it will be destroyed once all such windows are unregistered.
1474 err
= scif_destroy_pinned_pages(pinned_pages
);
1478 EXPORT_SYMBOL_GPL(scif_unpin_pages
);
1481 scif_insert_local_window(struct scif_window
*window
, struct scif_endpt
*ep
)
1483 mutex_lock(&ep
->rma_info
.rma_lock
);
1484 scif_insert_window(window
, &ep
->rma_info
.reg_list
);
1485 mutex_unlock(&ep
->rma_info
.rma_lock
);
1488 off_t
scif_register_pinned_pages(scif_epd_t epd
,
1489 scif_pinned_pages_t pinned_pages
,
1490 off_t offset
, int map_flags
)
1492 struct scif_endpt
*ep
= (struct scif_endpt
*)epd
;
1493 s64 computed_offset
;
1494 struct scif_window
*window
;
1497 struct device
*spdev
;
1499 /* Unsupported flags */
1500 if (map_flags
& ~SCIF_MAP_FIXED
)
1503 len
= pinned_pages
->nr_pages
<< PAGE_SHIFT
;
1506 * Offset is not page aligned/negative or offset+len
1507 * wraps around with SCIF_MAP_FIXED.
1509 if ((map_flags
& SCIF_MAP_FIXED
) &&
1510 ((ALIGN(offset
, PAGE_SIZE
) != offset
) ||
1512 (len
> LONG_MAX
- offset
)))
1517 err
= scif_verify_epd(ep
);
1521 * It is an error to pass pinned_pages to scif_register_pinned_pages()
1522 * after calling scif_unpin_pages().
1524 if (!atomic_add_unless(&pinned_pages
->ref_count
, 1, 0))
1527 /* Compute the offset for this registration */
1528 err
= scif_get_window_offset(ep
, map_flags
, offset
,
1529 len
, &computed_offset
);
1531 atomic_sub(1, &pinned_pages
->ref_count
);
1535 /* Allocate and prepare self registration window */
1536 window
= scif_create_window(ep
, pinned_pages
->nr_pages
,
1537 computed_offset
, false);
1539 atomic_sub(1, &pinned_pages
->ref_count
);
1540 scif_free_window_offset(ep
, NULL
, computed_offset
);
1544 window
->pinned_pages
= pinned_pages
;
1545 window
->nr_pages
= pinned_pages
->nr_pages
;
1546 window
->prot
= pinned_pages
->prot
;
1548 spdev
= scif_get_peer_dev(ep
->remote_dev
);
1549 if (IS_ERR(spdev
)) {
1550 err
= PTR_ERR(spdev
);
1551 scif_destroy_window(ep
, window
);
1554 err
= scif_send_alloc_request(ep
, window
);
1556 dev_err(&ep
->remote_dev
->sdev
->dev
,
1557 "%s %d err %d\n", __func__
, __LINE__
, err
);
1561 /* Prepare the remote registration window */
1562 err
= scif_prep_remote_window(ep
, window
);
1564 dev_err(&ep
->remote_dev
->sdev
->dev
,
1565 "%s %d err %d\n", __func__
, __LINE__
, err
);
1569 /* Tell the peer about the new window */
1570 err
= scif_send_scif_register(ep
, window
);
1572 dev_err(&ep
->remote_dev
->sdev
->dev
,
1573 "%s %d err %d\n", __func__
, __LINE__
, err
);
1577 scif_put_peer_dev(spdev
);
1578 /* No further failures expected. Insert new window */
1579 scif_insert_local_window(window
, ep
);
1580 return computed_offset
;
1582 scif_destroy_window(ep
, window
);
1583 scif_put_peer_dev(spdev
);
1584 dev_err(&ep
->remote_dev
->sdev
->dev
,
1585 "%s %d err %d\n", __func__
, __LINE__
, err
);
1588 EXPORT_SYMBOL_GPL(scif_register_pinned_pages
);
1590 off_t
scif_register(scif_epd_t epd
, void *addr
, size_t len
, off_t offset
,
1591 int prot
, int map_flags
)
1593 scif_pinned_pages_t pinned_pages
;
1595 struct scif_endpt
*ep
= (struct scif_endpt
*)epd
;
1596 s64 computed_offset
;
1597 struct scif_window
*window
;
1598 struct mm_struct
*mm
= NULL
;
1599 struct device
*spdev
;
1601 dev_dbg(scif_info
.mdev
.this_device
,
1602 "SCIFAPI register: ep %p addr %p len 0x%lx offset 0x%lx prot 0x%x map_flags 0x%x\n",
1603 epd
, addr
, len
, offset
, prot
, map_flags
);
1604 /* Unsupported flags */
1605 if (map_flags
& ~(SCIF_MAP_FIXED
| SCIF_MAP_KERNEL
))
1609 * Offset is not page aligned/negative or offset+len
1610 * wraps around with SCIF_MAP_FIXED.
1612 if ((map_flags
& SCIF_MAP_FIXED
) &&
1613 ((ALIGN(offset
, PAGE_SIZE
) != offset
) ||
1615 (len
> LONG_MAX
- offset
)))
1618 /* Unsupported protection requested */
1619 if (prot
& ~(SCIF_PROT_READ
| SCIF_PROT_WRITE
))
1622 /* addr/len must be page aligned. len should be non zero */
1623 if (!len
|| (ALIGN((u64
)addr
, PAGE_SIZE
) != (u64
)addr
) ||
1624 (ALIGN(len
, PAGE_SIZE
) != len
))
1629 err
= scif_verify_epd(ep
);
1633 /* Compute the offset for this registration */
1634 err
= scif_get_window_offset(ep
, map_flags
, offset
,
1635 len
>> PAGE_SHIFT
, &computed_offset
);
1639 spdev
= scif_get_peer_dev(ep
->remote_dev
);
1640 if (IS_ERR(spdev
)) {
1641 err
= PTR_ERR(spdev
);
1642 scif_free_window_offset(ep
, NULL
, computed_offset
);
1645 /* Allocate and prepare self registration window */
1646 window
= scif_create_window(ep
, len
>> PAGE_SHIFT
,
1647 computed_offset
, false);
1649 scif_free_window_offset(ep
, NULL
, computed_offset
);
1650 scif_put_peer_dev(spdev
);
1654 window
->nr_pages
= len
>> PAGE_SHIFT
;
1656 err
= scif_send_alloc_request(ep
, window
);
1658 scif_destroy_incomplete_window(ep
, window
);
1659 scif_put_peer_dev(spdev
);
1663 if (!(map_flags
& SCIF_MAP_KERNEL
)) {
1664 mm
= __scif_acquire_mm();
1665 map_flags
|= SCIF_MAP_ULIMIT
;
1667 /* Pin down the pages */
1668 err
= __scif_pin_pages(addr
, len
, &prot
,
1669 map_flags
& (SCIF_MAP_KERNEL
| SCIF_MAP_ULIMIT
),
1672 scif_destroy_incomplete_window(ep
, window
);
1673 __scif_release_mm(mm
);
1677 window
->pinned_pages
= pinned_pages
;
1678 window
->prot
= pinned_pages
->prot
;
1681 /* Prepare the remote registration window */
1682 err
= scif_prep_remote_window(ep
, window
);
1684 dev_err(&ep
->remote_dev
->sdev
->dev
,
1685 "%s %d err %ld\n", __func__
, __LINE__
, err
);
1689 /* Tell the peer about the new window */
1690 err
= scif_send_scif_register(ep
, window
);
1692 dev_err(&ep
->remote_dev
->sdev
->dev
,
1693 "%s %d err %ld\n", __func__
, __LINE__
, err
);
1697 scif_put_peer_dev(spdev
);
1698 /* No further failures expected. Insert new window */
1699 scif_insert_local_window(window
, ep
);
1700 dev_dbg(&ep
->remote_dev
->sdev
->dev
,
1701 "SCIFAPI register: ep %p addr %p len 0x%lx computed_offset 0x%llx\n",
1702 epd
, addr
, len
, computed_offset
);
1703 return computed_offset
;
1705 scif_destroy_window(ep
, window
);
1707 scif_put_peer_dev(spdev
);
1708 dev_err(&ep
->remote_dev
->sdev
->dev
,
1709 "%s %d err %ld\n", __func__
, __LINE__
, err
);
1712 EXPORT_SYMBOL_GPL(scif_register
);
1715 scif_unregister(scif_epd_t epd
, off_t offset
, size_t len
)
1717 struct scif_endpt
*ep
= (struct scif_endpt
*)epd
;
1718 struct scif_window
*window
= NULL
;
1719 struct scif_rma_req req
;
1721 struct device
*spdev
;
1723 dev_dbg(scif_info
.mdev
.this_device
,
1724 "SCIFAPI unregister: ep %p offset 0x%lx len 0x%lx\n",
1726 /* len must be page aligned. len should be non zero */
1728 (ALIGN((u64
)len
, PAGE_SIZE
) != (u64
)len
))
1731 /* Offset is not page aligned or offset+len wraps around */
1732 if ((ALIGN(offset
, PAGE_SIZE
) != offset
) ||
1734 (len
> LONG_MAX
- offset
))
1737 err
= scif_verify_epd(ep
);
1742 nr_pages
= len
>> PAGE_SHIFT
;
1744 req
.out_window
= &window
;
1745 req
.offset
= offset
;
1748 req
.type
= SCIF_WINDOW_FULL
;
1749 req
.head
= &ep
->rma_info
.reg_list
;
1751 spdev
= scif_get_peer_dev(ep
->remote_dev
);
1752 if (IS_ERR(spdev
)) {
1753 err
= PTR_ERR(spdev
);
1756 mutex_lock(&ep
->rma_info
.rma_lock
);
1757 /* Does a valid window exist? */
1758 err
= scif_query_window(&req
);
1760 dev_err(&ep
->remote_dev
->sdev
->dev
,
1761 "%s %d err %d\n", __func__
, __LINE__
, err
);
1764 /* Unregister all the windows in this range */
1765 err
= scif_rma_list_unregister(window
, offset
, nr_pages
);
1767 dev_err(&ep
->remote_dev
->sdev
->dev
,
1768 "%s %d err %d\n", __func__
, __LINE__
, err
);
1770 mutex_unlock(&ep
->rma_info
.rma_lock
);
1771 scif_put_peer_dev(spdev
);
1774 EXPORT_SYMBOL_GPL(scif_unregister
);