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 <linux/sched/mm.h>
21 #include <linux/sched/signal.h>
23 #include "scif_main.h"
26 /* Used to skip ulimit checks for registrations with SCIF_MAP_KERNEL flag */
27 #define SCIF_MAP_ULIMIT 0x40
29 bool scif_ulimit_check
= 1;
35 * Initialize RMA per EP data structures.
37 void scif_rma_ep_init(struct scif_endpt
*ep
)
39 struct scif_endpt_rma_info
*rma
= &ep
->rma_info
;
41 mutex_init(&rma
->rma_lock
);
42 init_iova_domain(&rma
->iovad
, PAGE_SIZE
, SCIF_IOVA_START_PFN
);
43 spin_lock_init(&rma
->tc_lock
);
44 mutex_init(&rma
->mmn_lock
);
45 INIT_LIST_HEAD(&rma
->reg_list
);
46 INIT_LIST_HEAD(&rma
->remote_reg_list
);
47 atomic_set(&rma
->tw_refcount
, 0);
48 atomic_set(&rma
->tcw_refcount
, 0);
49 atomic_set(&rma
->tcw_total_pages
, 0);
50 atomic_set(&rma
->fence_refcount
, 0);
52 rma
->async_list_del
= 0;
54 INIT_LIST_HEAD(&rma
->mmn_list
);
55 INIT_LIST_HEAD(&rma
->vma_list
);
56 init_waitqueue_head(&rma
->markwq
);
60 * scif_rma_ep_can_uninit:
63 * Returns 1 if an endpoint can be uninitialized and 0 otherwise.
65 int scif_rma_ep_can_uninit(struct scif_endpt
*ep
)
69 mutex_lock(&ep
->rma_info
.rma_lock
);
70 /* Destroy RMA Info only if both lists are empty */
71 if (list_empty(&ep
->rma_info
.reg_list
) &&
72 list_empty(&ep
->rma_info
.remote_reg_list
) &&
73 list_empty(&ep
->rma_info
.mmn_list
) &&
74 !atomic_read(&ep
->rma_info
.tw_refcount
) &&
75 !atomic_read(&ep
->rma_info
.tcw_refcount
) &&
76 !atomic_read(&ep
->rma_info
.fence_refcount
))
78 mutex_unlock(&ep
->rma_info
.rma_lock
);
83 * scif_create_pinned_pages:
84 * @nr_pages: number of pages in window
85 * @prot: read/write protection
87 * Allocate and prepare a set of pinned pages.
89 static struct scif_pinned_pages
*
90 scif_create_pinned_pages(int nr_pages
, int prot
)
92 struct scif_pinned_pages
*pin
;
95 pin
= scif_zalloc(sizeof(*pin
));
99 pin
->pages
= scif_zalloc(nr_pages
* sizeof(*pin
->pages
));
101 goto error_free_pinned_pages
;
104 pin
->magic
= SCIFEP_MAGIC
;
107 error_free_pinned_pages
:
108 scif_free(pin
, sizeof(*pin
));
114 * scif_destroy_pinned_pages:
115 * @pin: A set of pinned pages.
117 * Deallocate resources for pinned pages.
119 static int scif_destroy_pinned_pages(struct scif_pinned_pages
*pin
)
122 int writeable
= pin
->prot
& SCIF_PROT_WRITE
;
123 int kernel
= SCIF_MAP_KERNEL
& pin
->map_flags
;
125 for (j
= 0; j
< pin
->nr_pages
; j
++) {
126 if (pin
->pages
[j
] && !kernel
) {
128 SetPageDirty(pin
->pages
[j
]);
129 put_page(pin
->pages
[j
]);
133 scif_free(pin
->pages
,
134 pin
->nr_pages
* sizeof(*pin
->pages
));
135 scif_free(pin
, sizeof(*pin
));
140 * scif_create_window:
142 * @nr_pages: number of pages
143 * @offset: registration offset
144 * @temp: true if a temporary window is being created
146 * Allocate and prepare a self registration window.
148 struct scif_window
*scif_create_window(struct scif_endpt
*ep
, int nr_pages
,
149 s64 offset
, bool temp
)
151 struct scif_window
*window
;
154 window
= scif_zalloc(sizeof(*window
));
158 window
->dma_addr
= scif_zalloc(nr_pages
* sizeof(*window
->dma_addr
));
159 if (!window
->dma_addr
)
160 goto error_free_window
;
162 window
->num_pages
= scif_zalloc(nr_pages
* sizeof(*window
->num_pages
));
163 if (!window
->num_pages
)
164 goto error_free_window
;
166 window
->offset
= offset
;
167 window
->ep
= (u64
)ep
;
168 window
->magic
= SCIFEP_MAGIC
;
169 window
->reg_state
= OP_IDLE
;
170 init_waitqueue_head(&window
->regwq
);
171 window
->unreg_state
= OP_IDLE
;
172 init_waitqueue_head(&window
->unregwq
);
173 INIT_LIST_HEAD(&window
->list
);
174 window
->type
= SCIF_WINDOW_SELF
;
179 scif_free(window
->dma_addr
,
180 nr_pages
* sizeof(*window
->dma_addr
));
181 scif_free(window
, sizeof(*window
));
187 * scif_destroy_incomplete_window:
189 * @window: registration window
191 * Deallocate resources for self window.
193 static void scif_destroy_incomplete_window(struct scif_endpt
*ep
,
194 struct scif_window
*window
)
197 int nr_pages
= window
->nr_pages
;
198 struct scif_allocmsg
*alloc
= &window
->alloc_handle
;
202 /* Wait for a SCIF_ALLOC_GNT/REJ message */
203 err
= wait_event_timeout(alloc
->allocwq
,
204 alloc
->state
!= OP_IN_PROGRESS
,
205 SCIF_NODE_ALIVE_TIMEOUT
);
206 if (!err
&& scifdev_alive(ep
))
209 mutex_lock(&ep
->rma_info
.rma_lock
);
210 if (alloc
->state
== OP_COMPLETED
) {
211 msg
.uop
= SCIF_FREE_VIRT
;
213 msg
.payload
[0] = ep
->remote_ep
;
214 msg
.payload
[1] = window
->alloc_handle
.vaddr
;
215 msg
.payload
[2] = (u64
)window
;
216 msg
.payload
[3] = SCIF_REGISTER
;
217 _scif_nodeqp_send(ep
->remote_dev
, &msg
);
219 mutex_unlock(&ep
->rma_info
.rma_lock
);
221 scif_free_window_offset(ep
, window
, window
->offset
);
222 scif_free(window
->dma_addr
, nr_pages
* sizeof(*window
->dma_addr
));
223 scif_free(window
->num_pages
, nr_pages
* sizeof(*window
->num_pages
));
224 scif_free(window
, sizeof(*window
));
229 * @remote_dev: SCIF remote device
230 * @window: registration window
232 * Delete any DMA mappings created for a registered self window
234 void scif_unmap_window(struct scif_dev
*remote_dev
, struct scif_window
*window
)
238 if (scif_is_iommu_enabled() && !scifdev_self(remote_dev
)) {
240 dma_unmap_sg(&remote_dev
->sdev
->dev
,
241 window
->st
->sgl
, window
->st
->nents
,
243 sg_free_table(window
->st
);
248 for (j
= 0; j
< window
->nr_contig_chunks
; j
++) {
249 if (window
->dma_addr
[j
]) {
250 scif_unmap_single(window
->dma_addr
[j
],
252 window
->num_pages
[j
] <<
254 window
->dma_addr
[j
] = 0x0;
260 static inline struct mm_struct
*__scif_acquire_mm(void)
262 if (scif_ulimit_check
)
263 return get_task_mm(current
);
267 static inline void __scif_release_mm(struct mm_struct
*mm
)
274 __scif_dec_pinned_vm_lock(struct mm_struct
*mm
,
275 int nr_pages
, bool try_lock
)
277 if (!mm
|| !nr_pages
|| !scif_ulimit_check
)
280 if (!down_write_trylock(&mm
->mmap_sem
)) {
281 dev_err(scif_info
.mdev
.this_device
,
282 "%s %d err\n", __func__
, __LINE__
);
286 down_write(&mm
->mmap_sem
);
288 mm
->pinned_vm
-= nr_pages
;
289 up_write(&mm
->mmap_sem
);
293 static inline int __scif_check_inc_pinned_vm(struct mm_struct
*mm
,
296 unsigned long locked
, lock_limit
;
298 if (!mm
|| !nr_pages
|| !scif_ulimit_check
)
302 locked
+= mm
->pinned_vm
;
303 lock_limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
304 if ((locked
> lock_limit
) && !capable(CAP_IPC_LOCK
)) {
305 dev_err(scif_info
.mdev
.this_device
,
306 "locked(%lu) > lock_limit(%lu)\n",
310 mm
->pinned_vm
= locked
;
315 * scif_destroy_window:
317 * @window: registration window
319 * Deallocate resources for self window.
321 int scif_destroy_window(struct scif_endpt
*ep
, struct scif_window
*window
)
324 struct scif_pinned_pages
*pinned_pages
= window
->pinned_pages
;
325 int nr_pages
= window
->nr_pages
;
328 if (!window
->temp
&& window
->mm
) {
329 __scif_dec_pinned_vm_lock(window
->mm
, window
->nr_pages
, 0);
330 __scif_release_mm(window
->mm
);
334 scif_free_window_offset(ep
, window
, window
->offset
);
335 scif_unmap_window(ep
->remote_dev
, window
);
337 * Decrement references for this set of pinned pages from
340 j
= atomic_sub_return(1, &pinned_pages
->ref_count
);
342 dev_err(scif_info
.mdev
.this_device
,
343 "%s %d incorrect ref count %d\n",
344 __func__
, __LINE__
, j
);
346 * If the ref count for pinned_pages is zero then someone
347 * has already called scif_unpin_pages() for it and we should
348 * destroy the page cache.
351 scif_destroy_pinned_pages(window
->pinned_pages
);
352 scif_free(window
->dma_addr
, nr_pages
* sizeof(*window
->dma_addr
));
353 scif_free(window
->num_pages
, nr_pages
* sizeof(*window
->num_pages
));
355 scif_free(window
, sizeof(*window
));
360 * scif_create_remote_lookup:
361 * @remote_dev: SCIF remote device
362 * @window: remote window
364 * Allocate and prepare lookup entries for the remote
365 * end to copy over the physical addresses.
366 * Returns 0 on success and appropriate errno on failure.
368 static int scif_create_remote_lookup(struct scif_dev
*remote_dev
,
369 struct scif_window
*window
)
372 int nr_pages
= window
->nr_pages
;
373 bool vmalloc_dma_phys
, vmalloc_num_pages
;
377 err
= scif_map_single(&window
->mapped_offset
,
378 window
, remote_dev
, sizeof(*window
));
382 /* Compute the number of lookup entries. 21 == 2MB Shift */
383 window
->nr_lookup
= ALIGN(nr_pages
* PAGE_SIZE
,
384 ((2) * 1024 * 1024)) >> 21;
386 window
->dma_addr_lookup
.lookup
=
387 scif_alloc_coherent(&window
->dma_addr_lookup
.offset
,
388 remote_dev
, window
->nr_lookup
*
389 sizeof(*window
->dma_addr_lookup
.lookup
),
390 GFP_KERNEL
| __GFP_ZERO
);
391 if (!window
->dma_addr_lookup
.lookup
) {
396 window
->num_pages_lookup
.lookup
=
397 scif_alloc_coherent(&window
->num_pages_lookup
.offset
,
398 remote_dev
, window
->nr_lookup
*
399 sizeof(*window
->num_pages_lookup
.lookup
),
400 GFP_KERNEL
| __GFP_ZERO
);
401 if (!window
->num_pages_lookup
.lookup
) {
406 vmalloc_dma_phys
= is_vmalloc_addr(&window
->dma_addr
[0]);
407 vmalloc_num_pages
= is_vmalloc_addr(&window
->num_pages
[0]);
409 /* Now map each of the pages containing physical addresses */
410 for (i
= 0, j
= 0; i
< nr_pages
; i
+= SCIF_NR_ADDR_IN_PAGE
, j
++) {
411 err
= scif_map_page(&window
->dma_addr_lookup
.lookup
[j
],
413 vmalloc_to_page(&window
->dma_addr
[i
]) :
414 virt_to_page(&window
->dma_addr
[i
]),
418 err
= scif_map_page(&window
->num_pages_lookup
.lookup
[j
],
420 vmalloc_to_page(&window
->num_pages
[i
]) :
421 virt_to_page(&window
->num_pages
[i
]),
432 * scif_destroy_remote_lookup:
433 * @remote_dev: SCIF remote device
434 * @window: remote window
436 * Destroy lookup entries used for the remote
437 * end to copy over the physical addresses.
439 static void scif_destroy_remote_lookup(struct scif_dev
*remote_dev
,
440 struct scif_window
*window
)
444 if (window
->nr_lookup
) {
445 struct scif_rma_lookup
*lup
= &window
->dma_addr_lookup
;
446 struct scif_rma_lookup
*npup
= &window
->num_pages_lookup
;
448 for (i
= 0, j
= 0; i
< window
->nr_pages
;
449 i
+= SCIF_NR_ADDR_IN_PAGE
, j
++) {
450 if (lup
->lookup
&& lup
->lookup
[j
])
451 scif_unmap_single(lup
->lookup
[j
],
454 if (npup
->lookup
&& npup
->lookup
[j
])
455 scif_unmap_single(npup
->lookup
[j
],
460 scif_free_coherent(lup
->lookup
, lup
->offset
,
461 remote_dev
, window
->nr_lookup
*
462 sizeof(*lup
->lookup
));
464 scif_free_coherent(npup
->lookup
, npup
->offset
,
465 remote_dev
, window
->nr_lookup
*
466 sizeof(*npup
->lookup
));
467 if (window
->mapped_offset
)
468 scif_unmap_single(window
->mapped_offset
,
469 remote_dev
, sizeof(*window
));
470 window
->nr_lookup
= 0;
475 * scif_create_remote_window:
477 * @nr_pages: number of pages in window
479 * Allocate and prepare a remote registration window.
481 static struct scif_window
*
482 scif_create_remote_window(struct scif_dev
*scifdev
, int nr_pages
)
484 struct scif_window
*window
;
487 window
= scif_zalloc(sizeof(*window
));
491 window
->magic
= SCIFEP_MAGIC
;
492 window
->nr_pages
= nr_pages
;
494 window
->dma_addr
= scif_zalloc(nr_pages
* sizeof(*window
->dma_addr
));
495 if (!window
->dma_addr
)
498 window
->num_pages
= scif_zalloc(nr_pages
*
499 sizeof(*window
->num_pages
));
500 if (!window
->num_pages
)
503 if (scif_create_remote_lookup(scifdev
, window
))
506 window
->type
= SCIF_WINDOW_PEER
;
507 window
->unreg_state
= OP_IDLE
;
508 INIT_LIST_HEAD(&window
->list
);
511 scif_destroy_remote_window(window
);
517 * scif_destroy_remote_window:
519 * @window: remote registration window
521 * Deallocate resources for remote window.
524 scif_destroy_remote_window(struct scif_window
*window
)
526 scif_free(window
->dma_addr
, window
->nr_pages
*
527 sizeof(*window
->dma_addr
));
528 scif_free(window
->num_pages
, window
->nr_pages
*
529 sizeof(*window
->num_pages
));
531 scif_free(window
, sizeof(*window
));
535 * scif_iommu_map: create DMA mappings if the IOMMU is enabled
536 * @remote_dev: SCIF remote device
537 * @window: remote registration window
539 * Map the physical pages using dma_map_sg(..) and then detect the number
540 * of contiguous DMA mappings allocated
542 static int scif_iommu_map(struct scif_dev
*remote_dev
,
543 struct scif_window
*window
)
545 struct scatterlist
*sg
;
547 scif_pinned_pages_t pin
= window
->pinned_pages
;
549 window
->st
= kzalloc(sizeof(*window
->st
), GFP_KERNEL
);
553 err
= sg_alloc_table(window
->st
, window
->nr_pages
, GFP_KERNEL
);
557 for_each_sg(window
->st
->sgl
, sg
, window
->st
->nents
, i
)
558 sg_set_page(sg
, pin
->pages
[i
], PAGE_SIZE
, 0x0);
560 err
= dma_map_sg(&remote_dev
->sdev
->dev
, window
->st
->sgl
,
561 window
->st
->nents
, DMA_BIDIRECTIONAL
);
564 /* Detect contiguous ranges of DMA mappings */
565 sg
= window
->st
->sgl
;
566 for (i
= 0; sg
; i
++) {
569 window
->dma_addr
[i
] = sg_dma_address(sg
);
570 window
->num_pages
[i
] = sg_dma_len(sg
) >> PAGE_SHIFT
;
571 last_da
= sg_dma_address(sg
) + sg_dma_len(sg
);
572 while ((sg
= sg_next(sg
)) && sg_dma_address(sg
) == last_da
) {
573 window
->num_pages
[i
] +=
574 (sg_dma_len(sg
) >> PAGE_SHIFT
);
575 last_da
= window
->dma_addr
[i
] +
578 window
->nr_contig_chunks
++;
585 * @remote_dev: SCIF remote device
586 * @window: self registration window
588 * Map pages of a window into the aperture/PCI.
589 * Also determine addresses required for DMA.
592 scif_map_window(struct scif_dev
*remote_dev
, struct scif_window
*window
)
594 int i
, j
, k
, err
= 0, nr_contig_pages
;
595 scif_pinned_pages_t pin
;
596 phys_addr_t phys_prev
, phys_curr
;
600 pin
= window
->pinned_pages
;
602 if (intel_iommu_enabled
&& !scifdev_self(remote_dev
))
603 return scif_iommu_map(remote_dev
, window
);
605 for (i
= 0, j
= 0; i
< window
->nr_pages
; i
+= nr_contig_pages
, j
++) {
606 phys_prev
= page_to_phys(pin
->pages
[i
]);
609 /* Detect physically contiguous chunks */
610 for (k
= i
+ 1; k
< window
->nr_pages
; k
++) {
611 phys_curr
= page_to_phys(pin
->pages
[k
]);
612 if (phys_curr
!= (phys_prev
+ PAGE_SIZE
))
614 phys_prev
= phys_curr
;
617 window
->num_pages
[j
] = nr_contig_pages
;
618 window
->nr_contig_chunks
++;
619 if (scif_is_mgmt_node()) {
621 * Management node has to deal with SMPT on X100 and
622 * hence the DMA mapping is required
624 err
= scif_map_single(&window
->dma_addr
[j
],
625 phys_to_virt(page_to_phys(
628 nr_contig_pages
<< PAGE_SHIFT
);
632 window
->dma_addr
[j
] = page_to_phys(pin
->pages
[i
]);
639 * scif_send_scif_unregister:
641 * @window: self registration window
643 * Send a SCIF_UNREGISTER message.
645 static int scif_send_scif_unregister(struct scif_endpt
*ep
,
646 struct scif_window
*window
)
650 msg
.uop
= SCIF_UNREGISTER
;
652 msg
.payload
[0] = window
->alloc_handle
.vaddr
;
653 msg
.payload
[1] = (u64
)window
;
654 return scif_nodeqp_send(ep
->remote_dev
, &msg
);
658 * scif_unregister_window:
659 * @window: self registration window
661 * Send an unregistration request and wait for a response.
663 int scif_unregister_window(struct scif_window
*window
)
666 struct scif_endpt
*ep
= (struct scif_endpt
*)window
->ep
;
667 bool send_msg
= false;
670 switch (window
->unreg_state
) {
673 window
->unreg_state
= OP_IN_PROGRESS
;
679 scif_get_window(window
, 1);
680 mutex_unlock(&ep
->rma_info
.rma_lock
);
682 err
= scif_send_scif_unregister(ep
, window
);
684 window
->unreg_state
= OP_COMPLETED
;
688 /* Return ENXIO since unregistration is in progress */
689 mutex_lock(&ep
->rma_info
.rma_lock
);
693 /* Wait for a SCIF_UNREGISTER_(N)ACK message */
694 err
= wait_event_timeout(window
->unregwq
,
695 window
->unreg_state
!= OP_IN_PROGRESS
,
696 SCIF_NODE_ALIVE_TIMEOUT
);
697 if (!err
&& scifdev_alive(ep
))
701 window
->unreg_state
= OP_COMPLETED
;
702 dev_err(scif_info
.mdev
.this_device
,
703 "%s %d err %d\n", __func__
, __LINE__
, err
);
708 mutex_lock(&ep
->rma_info
.rma_lock
);
709 scif_put_window(window
, 1);
714 if (!scifdev_alive(ep
)) {
716 window
->unreg_state
= OP_COMPLETED
;
726 if (window
->unreg_state
== OP_COMPLETED
&& window
->ref_count
)
727 scif_put_window(window
, window
->nr_pages
);
729 if (!window
->ref_count
) {
730 atomic_inc(&ep
->rma_info
.tw_refcount
);
731 list_del_init(&window
->list
);
732 scif_free_window_offset(ep
, window
, window
->offset
);
733 mutex_unlock(&ep
->rma_info
.rma_lock
);
734 if ((!!(window
->pinned_pages
->map_flags
& SCIF_MAP_KERNEL
)) &&
736 scif_drain_dma_intr(ep
->remote_dev
->sdev
,
737 ep
->rma_info
.dma_chan
);
739 if (!__scif_dec_pinned_vm_lock(window
->mm
,
740 window
->nr_pages
, 1)) {
741 __scif_release_mm(window
->mm
);
745 scif_queue_for_cleanup(window
, &scif_info
.rma
);
746 mutex_lock(&ep
->rma_info
.rma_lock
);
752 * scif_send_alloc_request:
754 * @window: self registration window
756 * Send a remote window allocation request
758 static int scif_send_alloc_request(struct scif_endpt
*ep
,
759 struct scif_window
*window
)
762 struct scif_allocmsg
*alloc
= &window
->alloc_handle
;
764 /* Set up the Alloc Handle */
765 alloc
->state
= OP_IN_PROGRESS
;
766 init_waitqueue_head(&alloc
->allocwq
);
768 /* Send out an allocation request */
769 msg
.uop
= SCIF_ALLOC_REQ
;
770 msg
.payload
[1] = window
->nr_pages
;
771 msg
.payload
[2] = (u64
)&window
->alloc_handle
;
772 return _scif_nodeqp_send(ep
->remote_dev
, &msg
);
776 * scif_prep_remote_window:
778 * @window: self registration window
780 * Send a remote window allocation request, wait for an allocation response,
781 * and prepares the remote window by copying over the page lists
783 static int scif_prep_remote_window(struct scif_endpt
*ep
,
784 struct scif_window
*window
)
787 struct scif_window
*remote_window
;
788 struct scif_allocmsg
*alloc
= &window
->alloc_handle
;
789 dma_addr_t
*dma_phys_lookup
, *tmp
, *num_pages_lookup
, *tmp1
;
791 int nr_contig_chunks
, loop_nr_contig_chunks
;
792 int remaining_nr_contig_chunks
, nr_lookup
;
795 map_err
= scif_map_window(ep
->remote_dev
, window
);
797 dev_err(&ep
->remote_dev
->sdev
->dev
,
798 "%s %d map_err %d\n", __func__
, __LINE__
, map_err
);
799 remaining_nr_contig_chunks
= window
->nr_contig_chunks
;
800 nr_contig_chunks
= window
->nr_contig_chunks
;
802 /* Wait for a SCIF_ALLOC_GNT/REJ message */
803 err
= wait_event_timeout(alloc
->allocwq
,
804 alloc
->state
!= OP_IN_PROGRESS
,
805 SCIF_NODE_ALIVE_TIMEOUT
);
806 mutex_lock(&ep
->rma_info
.rma_lock
);
807 /* Synchronize with the thread waking up allocwq */
808 mutex_unlock(&ep
->rma_info
.rma_lock
);
809 if (!err
&& scifdev_alive(ep
))
820 /* Bail out. The remote end rejected this request */
821 if (alloc
->state
== OP_FAILED
)
825 dev_err(&ep
->remote_dev
->sdev
->dev
,
826 "%s %d err %d\n", __func__
, __LINE__
, map_err
);
827 msg
.uop
= SCIF_FREE_VIRT
;
829 msg
.payload
[0] = ep
->remote_ep
;
830 msg
.payload
[1] = window
->alloc_handle
.vaddr
;
831 msg
.payload
[2] = (u64
)window
;
832 msg
.payload
[3] = SCIF_REGISTER
;
833 spin_lock(&ep
->lock
);
834 if (ep
->state
== SCIFEP_CONNECTED
)
835 err
= _scif_nodeqp_send(ep
->remote_dev
, &msg
);
838 spin_unlock(&ep
->lock
);
842 remote_window
= scif_ioremap(alloc
->phys_addr
, sizeof(*window
),
845 /* Compute the number of lookup entries. 21 == 2MB Shift */
846 nr_lookup
= ALIGN(nr_contig_chunks
, SCIF_NR_ADDR_IN_PAGE
)
847 >> ilog2(SCIF_NR_ADDR_IN_PAGE
);
850 scif_ioremap(remote_window
->dma_addr_lookup
.offset
,
852 sizeof(*remote_window
->dma_addr_lookup
.lookup
),
855 scif_ioremap(remote_window
->num_pages_lookup
.offset
,
857 sizeof(*remote_window
->num_pages_lookup
.lookup
),
860 while (remaining_nr_contig_chunks
) {
861 loop_nr_contig_chunks
= min_t(int, remaining_nr_contig_chunks
,
862 (int)SCIF_NR_ADDR_IN_PAGE
);
863 /* #1/2 - Copy physical addresses over to the remote side */
865 /* #2/2 - Copy DMA addresses (addresses that are fed into the
866 * DMA engine) We transfer bus addresses which are then
867 * converted into a MIC physical address on the remote
868 * side if it is a MIC, if the remote node is a mgmt node we
869 * transfer the MIC physical address
871 tmp
= scif_ioremap(dma_phys_lookup
[j
],
872 loop_nr_contig_chunks
*
873 sizeof(*window
->dma_addr
),
875 tmp1
= scif_ioremap(num_pages_lookup
[j
],
876 loop_nr_contig_chunks
*
877 sizeof(*window
->num_pages
),
879 if (scif_is_mgmt_node()) {
880 memcpy_toio((void __force __iomem
*)tmp
,
881 &window
->dma_addr
[i
], loop_nr_contig_chunks
882 * sizeof(*window
->dma_addr
));
883 memcpy_toio((void __force __iomem
*)tmp1
,
884 &window
->num_pages
[i
], loop_nr_contig_chunks
885 * sizeof(*window
->num_pages
));
887 if (scifdev_is_p2p(ep
->remote_dev
)) {
889 * add remote node's base address for this node
890 * to convert it into a MIC address
895 for (m
= 0; m
< loop_nr_contig_chunks
; m
++) {
896 dma_addr
= window
->dma_addr
[i
+ m
] +
897 ep
->remote_dev
->base_addr
;
899 (void __force __iomem
*)&tmp
[m
]);
901 memcpy_toio((void __force __iomem
*)tmp1
,
902 &window
->num_pages
[i
],
903 loop_nr_contig_chunks
904 * sizeof(*window
->num_pages
));
906 /* Mgmt node or loopback - transfer DMA
907 * addresses as is, this is the same as a
908 * MIC physical address (we use the dma_addr
909 * and not the phys_addr array since the
910 * phys_addr is only setup if there is a mmap()
911 * request from the mgmt node)
913 memcpy_toio((void __force __iomem
*)tmp
,
914 &window
->dma_addr
[i
],
915 loop_nr_contig_chunks
*
916 sizeof(*window
->dma_addr
));
917 memcpy_toio((void __force __iomem
*)tmp1
,
918 &window
->num_pages
[i
],
919 loop_nr_contig_chunks
*
920 sizeof(*window
->num_pages
));
923 remaining_nr_contig_chunks
-= loop_nr_contig_chunks
;
924 i
+= loop_nr_contig_chunks
;
926 scif_iounmap(tmp
, loop_nr_contig_chunks
*
927 sizeof(*window
->dma_addr
), ep
->remote_dev
);
928 scif_iounmap(tmp1
, loop_nr_contig_chunks
*
929 sizeof(*window
->num_pages
), ep
->remote_dev
);
932 /* Prepare the remote window for the peer */
933 remote_window
->peer_window
= (u64
)window
;
934 remote_window
->offset
= window
->offset
;
935 remote_window
->prot
= window
->prot
;
936 remote_window
->nr_contig_chunks
= nr_contig_chunks
;
937 remote_window
->ep
= ep
->remote_ep
;
938 scif_iounmap(num_pages_lookup
,
940 sizeof(*remote_window
->num_pages_lookup
.lookup
),
942 scif_iounmap(dma_phys_lookup
,
944 sizeof(*remote_window
->dma_addr_lookup
.lookup
),
946 scif_iounmap(remote_window
, sizeof(*remote_window
), ep
->remote_dev
);
947 window
->peer_window
= alloc
->vaddr
;
952 * scif_send_scif_register:
954 * @window: self registration window
956 * Send a SCIF_REGISTER message if EP is connected and wait for a
957 * SCIF_REGISTER_(N)ACK message else send a SCIF_FREE_VIRT
958 * message so that the peer can free its remote window allocated earlier.
960 static int scif_send_scif_register(struct scif_endpt
*ep
,
961 struct scif_window
*window
)
967 msg
.payload
[0] = ep
->remote_ep
;
968 msg
.payload
[1] = window
->alloc_handle
.vaddr
;
969 msg
.payload
[2] = (u64
)window
;
970 spin_lock(&ep
->lock
);
971 if (ep
->state
== SCIFEP_CONNECTED
) {
972 msg
.uop
= SCIF_REGISTER
;
973 window
->reg_state
= OP_IN_PROGRESS
;
974 err
= _scif_nodeqp_send(ep
->remote_dev
, &msg
);
975 spin_unlock(&ep
->lock
);
978 /* Wait for a SCIF_REGISTER_(N)ACK message */
979 err
= wait_event_timeout(window
->regwq
,
982 SCIF_NODE_ALIVE_TIMEOUT
);
983 if (!err
&& scifdev_alive(ep
))
985 err
= !err
? -ENODEV
: 0;
986 if (window
->reg_state
== OP_FAILED
)
990 msg
.uop
= SCIF_FREE_VIRT
;
991 msg
.payload
[3] = SCIF_REGISTER
;
992 err
= _scif_nodeqp_send(ep
->remote_dev
, &msg
);
993 spin_unlock(&ep
->lock
);
1001 * scif_get_window_offset:
1002 * @ep: end point descriptor
1004 * @offset: offset hint
1005 * @num_pages: number of pages
1006 * @out_offset: computed offset returned by reference.
1008 * Compute/Claim a new offset for this EP.
1010 int scif_get_window_offset(struct scif_endpt
*ep
, int flags
, s64 offset
,
1011 int num_pages
, s64
*out_offset
)
1014 struct iova
*iova_ptr
;
1017 if (flags
& SCIF_MAP_FIXED
) {
1018 page_index
= SCIF_IOVA_PFN(offset
);
1019 iova_ptr
= reserve_iova(&ep
->rma_info
.iovad
, page_index
,
1020 page_index
+ num_pages
- 1);
1024 iova_ptr
= alloc_iova(&ep
->rma_info
.iovad
, num_pages
,
1025 SCIF_DMA_63BIT_PFN
- 1, 0);
1030 *out_offset
= (iova_ptr
->pfn_lo
) << PAGE_SHIFT
;
1035 * scif_free_window_offset:
1036 * @ep: end point descriptor
1037 * @window: registration window
1038 * @offset: Offset to be freed
1040 * Free offset for this EP. The callee is supposed to grab
1041 * the RMA mutex before calling this API.
1043 void scif_free_window_offset(struct scif_endpt
*ep
,
1044 struct scif_window
*window
, s64 offset
)
1046 if ((window
&& !window
->offset_freed
) || !window
) {
1047 free_iova(&ep
->rma_info
.iovad
, offset
>> PAGE_SHIFT
);
1049 window
->offset_freed
= true;
1054 * scif_alloc_req: Respond to SCIF_ALLOC_REQ interrupt message
1055 * @msg: Interrupt message
1057 * Remote side is requesting a memory allocation.
1059 void scif_alloc_req(struct scif_dev
*scifdev
, struct scifmsg
*msg
)
1062 struct scif_window
*window
= NULL
;
1063 int nr_pages
= msg
->payload
[1];
1065 window
= scif_create_remote_window(scifdev
, nr_pages
);
1071 /* The peer's allocation request is granted */
1072 msg
->uop
= SCIF_ALLOC_GNT
;
1073 msg
->payload
[0] = (u64
)window
;
1074 msg
->payload
[1] = window
->mapped_offset
;
1075 err
= scif_nodeqp_send(scifdev
, msg
);
1077 scif_destroy_remote_window(window
);
1080 /* The peer's allocation request is rejected */
1081 dev_err(&scifdev
->sdev
->dev
,
1082 "%s %d error %d alloc_ptr %p nr_pages 0x%x\n",
1083 __func__
, __LINE__
, err
, window
, nr_pages
);
1084 msg
->uop
= SCIF_ALLOC_REJ
;
1085 scif_nodeqp_send(scifdev
, msg
);
1089 * scif_alloc_gnt_rej: Respond to SCIF_ALLOC_GNT/REJ interrupt message
1090 * @msg: Interrupt message
1092 * Remote side responded to a memory allocation.
1094 void scif_alloc_gnt_rej(struct scif_dev
*scifdev
, struct scifmsg
*msg
)
1096 struct scif_allocmsg
*handle
= (struct scif_allocmsg
*)msg
->payload
[2];
1097 struct scif_window
*window
= container_of(handle
, struct scif_window
,
1099 struct scif_endpt
*ep
= (struct scif_endpt
*)window
->ep
;
1101 mutex_lock(&ep
->rma_info
.rma_lock
);
1102 handle
->vaddr
= msg
->payload
[0];
1103 handle
->phys_addr
= msg
->payload
[1];
1104 if (msg
->uop
== SCIF_ALLOC_GNT
)
1105 handle
->state
= OP_COMPLETED
;
1107 handle
->state
= OP_FAILED
;
1108 wake_up(&handle
->allocwq
);
1109 mutex_unlock(&ep
->rma_info
.rma_lock
);
1113 * scif_free_virt: Respond to SCIF_FREE_VIRT interrupt message
1114 * @msg: Interrupt message
1116 * Free up memory kmalloc'd earlier.
1118 void scif_free_virt(struct scif_dev
*scifdev
, struct scifmsg
*msg
)
1120 struct scif_window
*window
= (struct scif_window
*)msg
->payload
[1];
1122 scif_destroy_remote_window(window
);
1126 scif_fixup_aper_base(struct scif_dev
*dev
, struct scif_window
*window
)
1129 struct scif_hw_dev
*sdev
= dev
->sdev
;
1130 phys_addr_t apt_base
= 0;
1133 * Add the aperture base if the DMA address is not card relative
1134 * since the DMA addresses need to be an offset into the bar
1136 if (!scifdev_self(dev
) && window
->type
== SCIF_WINDOW_PEER
&&
1137 sdev
->aper
&& !sdev
->card_rel_da
)
1138 apt_base
= sdev
->aper
->pa
;
1142 for (j
= 0; j
< window
->nr_contig_chunks
; j
++) {
1143 if (window
->num_pages
[j
])
1144 window
->dma_addr
[j
] += apt_base
;
1151 * scif_recv_reg: Respond to SCIF_REGISTER interrupt message
1152 * @msg: Interrupt message
1154 * Update remote window list with a new registered window.
1156 void scif_recv_reg(struct scif_dev
*scifdev
, struct scifmsg
*msg
)
1158 struct scif_endpt
*ep
= (struct scif_endpt
*)msg
->payload
[0];
1159 struct scif_window
*window
=
1160 (struct scif_window
*)msg
->payload
[1];
1162 mutex_lock(&ep
->rma_info
.rma_lock
);
1163 spin_lock(&ep
->lock
);
1164 if (ep
->state
== SCIFEP_CONNECTED
) {
1165 msg
->uop
= SCIF_REGISTER_ACK
;
1166 scif_nodeqp_send(ep
->remote_dev
, msg
);
1167 scif_fixup_aper_base(ep
->remote_dev
, window
);
1168 /* No further failures expected. Insert new window */
1169 scif_insert_window(window
, &ep
->rma_info
.remote_reg_list
);
1171 msg
->uop
= SCIF_REGISTER_NACK
;
1172 scif_nodeqp_send(ep
->remote_dev
, msg
);
1174 spin_unlock(&ep
->lock
);
1175 mutex_unlock(&ep
->rma_info
.rma_lock
);
1176 /* free up any lookup resources now that page lists are transferred */
1177 scif_destroy_remote_lookup(ep
->remote_dev
, window
);
1179 * We could not insert the window but we need to
1180 * destroy the window.
1182 if (msg
->uop
== SCIF_REGISTER_NACK
)
1183 scif_destroy_remote_window(window
);
1187 * scif_recv_unreg: Respond to SCIF_UNREGISTER interrupt message
1188 * @msg: Interrupt message
1190 * Remove window from remote registration list;
1192 void scif_recv_unreg(struct scif_dev
*scifdev
, struct scifmsg
*msg
)
1194 struct scif_rma_req req
;
1195 struct scif_window
*window
= NULL
;
1196 struct scif_window
*recv_window
=
1197 (struct scif_window
*)msg
->payload
[0];
1198 struct scif_endpt
*ep
;
1201 ep
= (struct scif_endpt
*)recv_window
->ep
;
1202 req
.out_window
= &window
;
1203 req
.offset
= recv_window
->offset
;
1205 req
.nr_bytes
= recv_window
->nr_pages
<< PAGE_SHIFT
;
1206 req
.type
= SCIF_WINDOW_FULL
;
1207 req
.head
= &ep
->rma_info
.remote_reg_list
;
1208 msg
->payload
[0] = ep
->remote_ep
;
1210 mutex_lock(&ep
->rma_info
.rma_lock
);
1211 /* Does a valid window exist? */
1212 if (scif_query_window(&req
)) {
1213 dev_err(&scifdev
->sdev
->dev
,
1214 "%s %d -ENXIO\n", __func__
, __LINE__
);
1215 msg
->uop
= SCIF_UNREGISTER_ACK
;
1219 if (window
->ref_count
)
1220 scif_put_window(window
, window
->nr_pages
);
1222 dev_err(&scifdev
->sdev
->dev
,
1223 "%s %d ref count should be +ve\n",
1224 __func__
, __LINE__
);
1225 window
->unreg_state
= OP_COMPLETED
;
1226 if (!window
->ref_count
) {
1227 msg
->uop
= SCIF_UNREGISTER_ACK
;
1228 atomic_inc(&ep
->rma_info
.tw_refcount
);
1229 ep
->rma_info
.async_list_del
= 1;
1230 list_del_init(&window
->list
);
1233 /* NACK! There are valid references to this window */
1234 msg
->uop
= SCIF_UNREGISTER_NACK
;
1237 /* The window did not make its way to the list at all. ACK */
1238 msg
->uop
= SCIF_UNREGISTER_ACK
;
1239 scif_destroy_remote_window(recv_window
);
1242 mutex_unlock(&ep
->rma_info
.rma_lock
);
1244 scif_drain_dma_intr(ep
->remote_dev
->sdev
,
1245 ep
->rma_info
.dma_chan
);
1246 scif_nodeqp_send(ep
->remote_dev
, msg
);
1248 scif_queue_for_cleanup(window
, &scif_info
.rma
);
1252 * scif_recv_reg_ack: Respond to SCIF_REGISTER_ACK interrupt message
1253 * @msg: Interrupt message
1255 * Wake up the window waiting to complete registration.
1257 void scif_recv_reg_ack(struct scif_dev
*scifdev
, struct scifmsg
*msg
)
1259 struct scif_window
*window
=
1260 (struct scif_window
*)msg
->payload
[2];
1261 struct scif_endpt
*ep
= (struct scif_endpt
*)window
->ep
;
1263 mutex_lock(&ep
->rma_info
.rma_lock
);
1264 window
->reg_state
= OP_COMPLETED
;
1265 wake_up(&window
->regwq
);
1266 mutex_unlock(&ep
->rma_info
.rma_lock
);
1270 * scif_recv_reg_nack: Respond to SCIF_REGISTER_NACK interrupt message
1271 * @msg: Interrupt message
1273 * Wake up the window waiting to inform it that registration
1274 * cannot be completed.
1276 void scif_recv_reg_nack(struct scif_dev
*scifdev
, struct scifmsg
*msg
)
1278 struct scif_window
*window
=
1279 (struct scif_window
*)msg
->payload
[2];
1280 struct scif_endpt
*ep
= (struct scif_endpt
*)window
->ep
;
1282 mutex_lock(&ep
->rma_info
.rma_lock
);
1283 window
->reg_state
= OP_FAILED
;
1284 wake_up(&window
->regwq
);
1285 mutex_unlock(&ep
->rma_info
.rma_lock
);
1289 * scif_recv_unreg_ack: Respond to SCIF_UNREGISTER_ACK interrupt message
1290 * @msg: Interrupt message
1292 * Wake up the window waiting to complete unregistration.
1294 void scif_recv_unreg_ack(struct scif_dev
*scifdev
, struct scifmsg
*msg
)
1296 struct scif_window
*window
=
1297 (struct scif_window
*)msg
->payload
[1];
1298 struct scif_endpt
*ep
= (struct scif_endpt
*)window
->ep
;
1300 mutex_lock(&ep
->rma_info
.rma_lock
);
1301 window
->unreg_state
= OP_COMPLETED
;
1302 wake_up(&window
->unregwq
);
1303 mutex_unlock(&ep
->rma_info
.rma_lock
);
1307 * scif_recv_unreg_nack: Respond to SCIF_UNREGISTER_NACK interrupt message
1308 * @msg: Interrupt message
1310 * Wake up the window waiting to inform it that unregistration
1311 * cannot be completed immediately.
1313 void scif_recv_unreg_nack(struct scif_dev
*scifdev
, struct scifmsg
*msg
)
1315 struct scif_window
*window
=
1316 (struct scif_window
*)msg
->payload
[1];
1317 struct scif_endpt
*ep
= (struct scif_endpt
*)window
->ep
;
1319 mutex_lock(&ep
->rma_info
.rma_lock
);
1320 window
->unreg_state
= OP_FAILED
;
1321 wake_up(&window
->unregwq
);
1322 mutex_unlock(&ep
->rma_info
.rma_lock
);
1325 int __scif_pin_pages(void *addr
, size_t len
, int *out_prot
,
1326 int map_flags
, scif_pinned_pages_t
*pages
)
1328 struct scif_pinned_pages
*pinned_pages
;
1329 int nr_pages
, err
= 0, i
;
1330 bool vmalloc_addr
= false;
1331 bool try_upgrade
= false;
1332 int prot
= *out_prot
;
1334 struct mm_struct
*mm
= NULL
;
1336 /* Unsupported flags */
1337 if (map_flags
& ~(SCIF_MAP_KERNEL
| SCIF_MAP_ULIMIT
))
1339 ulimit
= !!(map_flags
& SCIF_MAP_ULIMIT
);
1341 /* Unsupported protection requested */
1342 if (prot
& ~(SCIF_PROT_READ
| SCIF_PROT_WRITE
))
1345 /* addr/len must be page aligned. len should be non zero */
1347 (ALIGN((u64
)addr
, PAGE_SIZE
) != (u64
)addr
) ||
1348 (ALIGN((u64
)len
, PAGE_SIZE
) != (u64
)len
))
1353 nr_pages
= len
>> PAGE_SHIFT
;
1355 /* Allocate a set of pinned pages */
1356 pinned_pages
= scif_create_pinned_pages(nr_pages
, prot
);
1360 if (map_flags
& SCIF_MAP_KERNEL
) {
1361 if (is_vmalloc_addr(addr
))
1362 vmalloc_addr
= true;
1364 for (i
= 0; i
< nr_pages
; i
++) {
1366 pinned_pages
->pages
[i
] =
1367 vmalloc_to_page(addr
+ (i
* PAGE_SIZE
));
1369 pinned_pages
->pages
[i
] =
1370 virt_to_page(addr
+ (i
* PAGE_SIZE
));
1372 pinned_pages
->nr_pages
= nr_pages
;
1373 pinned_pages
->map_flags
= SCIF_MAP_KERNEL
;
1376 * SCIF supports registration caching. If a registration has
1377 * been requested with read only permissions, then we try
1378 * to pin the pages with RW permissions so that a subsequent
1379 * transfer with RW permission can hit the cache instead of
1380 * invalidating it. If the upgrade fails with RW then we
1381 * revert back to R permission and retry
1383 if (prot
== SCIF_PROT_READ
)
1385 prot
|= SCIF_PROT_WRITE
;
1388 down_write(&mm
->mmap_sem
);
1390 err
= __scif_check_inc_pinned_vm(mm
, nr_pages
);
1392 up_write(&mm
->mmap_sem
);
1393 pinned_pages
->nr_pages
= 0;
1398 pinned_pages
->nr_pages
= get_user_pages(
1401 (prot
& SCIF_PROT_WRITE
) ? FOLL_WRITE
: 0,
1402 pinned_pages
->pages
,
1404 up_write(&mm
->mmap_sem
);
1405 if (nr_pages
!= pinned_pages
->nr_pages
) {
1408 __scif_dec_pinned_vm_lock(mm
,
1410 /* Roll back any pinned pages */
1411 for (i
= 0; i
< pinned_pages
->nr_pages
; i
++) {
1412 if (pinned_pages
->pages
[i
])
1414 pinned_pages
->pages
[i
]);
1416 prot
&= ~SCIF_PROT_WRITE
;
1417 try_upgrade
= false;
1421 pinned_pages
->map_flags
= 0;
1424 if (pinned_pages
->nr_pages
< nr_pages
) {
1426 pinned_pages
->nr_pages
= nr_pages
;
1431 atomic_set(&pinned_pages
->ref_count
, 1);
1432 *pages
= pinned_pages
;
1436 __scif_dec_pinned_vm_lock(mm
, nr_pages
, 0);
1437 /* Something went wrong! Rollback */
1439 pinned_pages
->nr_pages
= nr_pages
;
1440 scif_destroy_pinned_pages(pinned_pages
);
1442 dev_dbg(scif_info
.mdev
.this_device
,
1443 "%s %d err %d len 0x%lx\n", __func__
, __LINE__
, err
, len
);
1447 int scif_pin_pages(void *addr
, size_t len
, int prot
,
1448 int map_flags
, scif_pinned_pages_t
*pages
)
1450 return __scif_pin_pages(addr
, len
, &prot
, map_flags
, pages
);
1452 EXPORT_SYMBOL_GPL(scif_pin_pages
);
1454 int scif_unpin_pages(scif_pinned_pages_t pinned_pages
)
1458 if (!pinned_pages
|| SCIFEP_MAGIC
!= pinned_pages
->magic
)
1461 ret
= atomic_sub_return(1, &pinned_pages
->ref_count
);
1463 dev_err(scif_info
.mdev
.this_device
,
1464 "%s %d scif_unpin_pages called without pinning? rc %d\n",
1465 __func__
, __LINE__
, ret
);
1469 * Destroy the window if the ref count for this set of pinned
1470 * pages has dropped to zero. If it is positive then there is
1471 * a valid registered window which is backed by these pages and
1472 * it will be destroyed once all such windows are unregistered.
1475 err
= scif_destroy_pinned_pages(pinned_pages
);
1479 EXPORT_SYMBOL_GPL(scif_unpin_pages
);
1482 scif_insert_local_window(struct scif_window
*window
, struct scif_endpt
*ep
)
1484 mutex_lock(&ep
->rma_info
.rma_lock
);
1485 scif_insert_window(window
, &ep
->rma_info
.reg_list
);
1486 mutex_unlock(&ep
->rma_info
.rma_lock
);
1489 off_t
scif_register_pinned_pages(scif_epd_t epd
,
1490 scif_pinned_pages_t pinned_pages
,
1491 off_t offset
, int map_flags
)
1493 struct scif_endpt
*ep
= (struct scif_endpt
*)epd
;
1494 s64 computed_offset
;
1495 struct scif_window
*window
;
1498 struct device
*spdev
;
1500 /* Unsupported flags */
1501 if (map_flags
& ~SCIF_MAP_FIXED
)
1504 len
= pinned_pages
->nr_pages
<< PAGE_SHIFT
;
1507 * Offset is not page aligned/negative or offset+len
1508 * wraps around with SCIF_MAP_FIXED.
1510 if ((map_flags
& SCIF_MAP_FIXED
) &&
1511 ((ALIGN(offset
, PAGE_SIZE
) != offset
) ||
1513 (len
> LONG_MAX
- offset
)))
1518 err
= scif_verify_epd(ep
);
1522 * It is an error to pass pinned_pages to scif_register_pinned_pages()
1523 * after calling scif_unpin_pages().
1525 if (!atomic_add_unless(&pinned_pages
->ref_count
, 1, 0))
1528 /* Compute the offset for this registration */
1529 err
= scif_get_window_offset(ep
, map_flags
, offset
,
1530 len
, &computed_offset
);
1532 atomic_sub(1, &pinned_pages
->ref_count
);
1536 /* Allocate and prepare self registration window */
1537 window
= scif_create_window(ep
, pinned_pages
->nr_pages
,
1538 computed_offset
, false);
1540 atomic_sub(1, &pinned_pages
->ref_count
);
1541 scif_free_window_offset(ep
, NULL
, computed_offset
);
1545 window
->pinned_pages
= pinned_pages
;
1546 window
->nr_pages
= pinned_pages
->nr_pages
;
1547 window
->prot
= pinned_pages
->prot
;
1549 spdev
= scif_get_peer_dev(ep
->remote_dev
);
1550 if (IS_ERR(spdev
)) {
1551 err
= PTR_ERR(spdev
);
1552 scif_destroy_window(ep
, window
);
1555 err
= scif_send_alloc_request(ep
, window
);
1557 dev_err(&ep
->remote_dev
->sdev
->dev
,
1558 "%s %d err %d\n", __func__
, __LINE__
, err
);
1562 /* Prepare the remote registration window */
1563 err
= scif_prep_remote_window(ep
, window
);
1565 dev_err(&ep
->remote_dev
->sdev
->dev
,
1566 "%s %d err %d\n", __func__
, __LINE__
, err
);
1570 /* Tell the peer about the new window */
1571 err
= scif_send_scif_register(ep
, window
);
1573 dev_err(&ep
->remote_dev
->sdev
->dev
,
1574 "%s %d err %d\n", __func__
, __LINE__
, err
);
1578 scif_put_peer_dev(spdev
);
1579 /* No further failures expected. Insert new window */
1580 scif_insert_local_window(window
, ep
);
1581 return computed_offset
;
1583 scif_destroy_window(ep
, window
);
1584 scif_put_peer_dev(spdev
);
1585 dev_err(&ep
->remote_dev
->sdev
->dev
,
1586 "%s %d err %d\n", __func__
, __LINE__
, err
);
1589 EXPORT_SYMBOL_GPL(scif_register_pinned_pages
);
1591 off_t
scif_register(scif_epd_t epd
, void *addr
, size_t len
, off_t offset
,
1592 int prot
, int map_flags
)
1594 scif_pinned_pages_t pinned_pages
;
1596 struct scif_endpt
*ep
= (struct scif_endpt
*)epd
;
1597 s64 computed_offset
;
1598 struct scif_window
*window
;
1599 struct mm_struct
*mm
= NULL
;
1600 struct device
*spdev
;
1602 dev_dbg(scif_info
.mdev
.this_device
,
1603 "SCIFAPI register: ep %p addr %p len 0x%lx offset 0x%lx prot 0x%x map_flags 0x%x\n",
1604 epd
, addr
, len
, offset
, prot
, map_flags
);
1605 /* Unsupported flags */
1606 if (map_flags
& ~(SCIF_MAP_FIXED
| SCIF_MAP_KERNEL
))
1610 * Offset is not page aligned/negative or offset+len
1611 * wraps around with SCIF_MAP_FIXED.
1613 if ((map_flags
& SCIF_MAP_FIXED
) &&
1614 ((ALIGN(offset
, PAGE_SIZE
) != offset
) ||
1616 (len
> LONG_MAX
- offset
)))
1619 /* Unsupported protection requested */
1620 if (prot
& ~(SCIF_PROT_READ
| SCIF_PROT_WRITE
))
1623 /* addr/len must be page aligned. len should be non zero */
1624 if (!len
|| (ALIGN((u64
)addr
, PAGE_SIZE
) != (u64
)addr
) ||
1625 (ALIGN(len
, PAGE_SIZE
) != len
))
1630 err
= scif_verify_epd(ep
);
1634 /* Compute the offset for this registration */
1635 err
= scif_get_window_offset(ep
, map_flags
, offset
,
1636 len
>> PAGE_SHIFT
, &computed_offset
);
1640 spdev
= scif_get_peer_dev(ep
->remote_dev
);
1641 if (IS_ERR(spdev
)) {
1642 err
= PTR_ERR(spdev
);
1643 scif_free_window_offset(ep
, NULL
, computed_offset
);
1646 /* Allocate and prepare self registration window */
1647 window
= scif_create_window(ep
, len
>> PAGE_SHIFT
,
1648 computed_offset
, false);
1650 scif_free_window_offset(ep
, NULL
, computed_offset
);
1651 scif_put_peer_dev(spdev
);
1655 window
->nr_pages
= len
>> PAGE_SHIFT
;
1657 err
= scif_send_alloc_request(ep
, window
);
1659 scif_destroy_incomplete_window(ep
, window
);
1660 scif_put_peer_dev(spdev
);
1664 if (!(map_flags
& SCIF_MAP_KERNEL
)) {
1665 mm
= __scif_acquire_mm();
1666 map_flags
|= SCIF_MAP_ULIMIT
;
1668 /* Pin down the pages */
1669 err
= __scif_pin_pages(addr
, len
, &prot
,
1670 map_flags
& (SCIF_MAP_KERNEL
| SCIF_MAP_ULIMIT
),
1673 scif_destroy_incomplete_window(ep
, window
);
1674 __scif_release_mm(mm
);
1678 window
->pinned_pages
= pinned_pages
;
1679 window
->prot
= pinned_pages
->prot
;
1682 /* Prepare the remote registration window */
1683 err
= scif_prep_remote_window(ep
, window
);
1685 dev_err(&ep
->remote_dev
->sdev
->dev
,
1686 "%s %d err %ld\n", __func__
, __LINE__
, err
);
1690 /* Tell the peer about the new window */
1691 err
= scif_send_scif_register(ep
, window
);
1693 dev_err(&ep
->remote_dev
->sdev
->dev
,
1694 "%s %d err %ld\n", __func__
, __LINE__
, err
);
1698 scif_put_peer_dev(spdev
);
1699 /* No further failures expected. Insert new window */
1700 scif_insert_local_window(window
, ep
);
1701 dev_dbg(&ep
->remote_dev
->sdev
->dev
,
1702 "SCIFAPI register: ep %p addr %p len 0x%lx computed_offset 0x%llx\n",
1703 epd
, addr
, len
, computed_offset
);
1704 return computed_offset
;
1706 scif_destroy_window(ep
, window
);
1708 scif_put_peer_dev(spdev
);
1709 dev_err(&ep
->remote_dev
->sdev
->dev
,
1710 "%s %d err %ld\n", __func__
, __LINE__
, err
);
1713 EXPORT_SYMBOL_GPL(scif_register
);
1716 scif_unregister(scif_epd_t epd
, off_t offset
, size_t len
)
1718 struct scif_endpt
*ep
= (struct scif_endpt
*)epd
;
1719 struct scif_window
*window
= NULL
;
1720 struct scif_rma_req req
;
1722 struct device
*spdev
;
1724 dev_dbg(scif_info
.mdev
.this_device
,
1725 "SCIFAPI unregister: ep %p offset 0x%lx len 0x%lx\n",
1727 /* len must be page aligned. len should be non zero */
1729 (ALIGN((u64
)len
, PAGE_SIZE
) != (u64
)len
))
1732 /* Offset is not page aligned or offset+len wraps around */
1733 if ((ALIGN(offset
, PAGE_SIZE
) != offset
) ||
1735 (len
> LONG_MAX
- offset
))
1738 err
= scif_verify_epd(ep
);
1743 nr_pages
= len
>> PAGE_SHIFT
;
1745 req
.out_window
= &window
;
1746 req
.offset
= offset
;
1749 req
.type
= SCIF_WINDOW_FULL
;
1750 req
.head
= &ep
->rma_info
.reg_list
;
1752 spdev
= scif_get_peer_dev(ep
->remote_dev
);
1753 if (IS_ERR(spdev
)) {
1754 err
= PTR_ERR(spdev
);
1757 mutex_lock(&ep
->rma_info
.rma_lock
);
1758 /* Does a valid window exist? */
1759 err
= scif_query_window(&req
);
1761 dev_err(&ep
->remote_dev
->sdev
->dev
,
1762 "%s %d err %d\n", __func__
, __LINE__
, err
);
1765 /* Unregister all the windows in this range */
1766 err
= scif_rma_list_unregister(window
, offset
, nr_pages
);
1768 dev_err(&ep
->remote_dev
->sdev
->dev
,
1769 "%s %d err %d\n", __func__
, __LINE__
, err
);
1771 mutex_unlock(&ep
->rma_info
.rma_lock
);
1772 scif_put_peer_dev(spdev
);
1775 EXPORT_SYMBOL_GPL(scif_unregister
);