1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel MIC Platform Software Stack (MPSS)
5 * Copyright(c) 2015 Intel Corporation.
9 #include <linux/intel-iommu.h>
10 #include <linux/pagemap.h>
11 #include <linux/sched/mm.h>
12 #include <linux/sched/signal.h>
14 #include "scif_main.h"
17 /* Used to skip ulimit checks for registrations with SCIF_MAP_KERNEL flag */
18 #define SCIF_MAP_ULIMIT 0x40
20 bool scif_ulimit_check = 1;
26 * Initialize RMA per EP data structures.
28 void scif_rma_ep_init(struct scif_endpt *ep)
30 struct scif_endpt_rma_info *rma = &ep->rma_info;
32 mutex_init(&rma->rma_lock);
33 init_iova_domain(&rma->iovad, PAGE_SIZE, SCIF_IOVA_START_PFN);
34 spin_lock_init(&rma->tc_lock);
35 mutex_init(&rma->mmn_lock);
36 INIT_LIST_HEAD(&rma->reg_list);
37 INIT_LIST_HEAD(&rma->remote_reg_list);
38 atomic_set(&rma->tw_refcount, 0);
39 atomic_set(&rma->tcw_refcount, 0);
40 atomic_set(&rma->tcw_total_pages, 0);
41 atomic_set(&rma->fence_refcount, 0);
43 rma->async_list_del = 0;
45 INIT_LIST_HEAD(&rma->mmn_list);
46 INIT_LIST_HEAD(&rma->vma_list);
47 init_waitqueue_head(&rma->markwq);
51 * scif_rma_ep_can_uninit:
54 * Returns 1 if an endpoint can be uninitialized and 0 otherwise.
56 int scif_rma_ep_can_uninit(struct scif_endpt *ep)
60 mutex_lock(&ep->rma_info.rma_lock);
61 /* Destroy RMA Info only if both lists are empty */
62 if (list_empty(&ep->rma_info.reg_list) &&
63 list_empty(&ep->rma_info.remote_reg_list) &&
64 list_empty(&ep->rma_info.mmn_list) &&
65 !atomic_read(&ep->rma_info.tw_refcount) &&
66 !atomic_read(&ep->rma_info.tcw_refcount) &&
67 !atomic_read(&ep->rma_info.fence_refcount))
69 mutex_unlock(&ep->rma_info.rma_lock);
74 * scif_create_pinned_pages:
75 * @nr_pages: number of pages in window
76 * @prot: read/write protection
78 * Allocate and prepare a set of pinned pages.
80 static struct scif_pinned_pages *
81 scif_create_pinned_pages(int nr_pages, int prot)
83 struct scif_pinned_pages *pin;
86 pin = scif_zalloc(sizeof(*pin));
90 pin->pages = scif_zalloc(nr_pages * sizeof(*pin->pages));
92 goto error_free_pinned_pages;
95 pin->magic = SCIFEP_MAGIC;
98 error_free_pinned_pages:
99 scif_free(pin, sizeof(*pin));
105 * scif_destroy_pinned_pages:
106 * @pin: A set of pinned pages.
108 * Deallocate resources for pinned pages.
110 static int scif_destroy_pinned_pages(struct scif_pinned_pages *pin)
113 int writeable = pin->prot & SCIF_PROT_WRITE;
114 int kernel = SCIF_MAP_KERNEL & pin->map_flags;
117 for (j = 0; j < pin->nr_pages; j++) {
118 if (pin->pages[j] && !kernel) {
120 set_page_dirty_lock(pin->pages[j]);
121 put_page(pin->pages[j]);
125 unpin_user_pages_dirty_lock(pin->pages, pin->nr_pages,
127 scif_free(pin->pages,
128 pin->nr_pages * sizeof(*pin->pages));
129 scif_free(pin, sizeof(*pin));
134 * scif_create_window:
136 * @nr_pages: number of pages
137 * @offset: registration offset
138 * @temp: true if a temporary window is being created
140 * Allocate and prepare a self registration window.
142 struct scif_window *scif_create_window(struct scif_endpt *ep, int nr_pages,
143 s64 offset, bool temp)
145 struct scif_window *window;
148 window = scif_zalloc(sizeof(*window));
152 window->dma_addr = scif_zalloc(nr_pages * sizeof(*window->dma_addr));
153 if (!window->dma_addr)
154 goto error_free_window;
156 window->num_pages = scif_zalloc(nr_pages * sizeof(*window->num_pages));
157 if (!window->num_pages)
158 goto error_free_window;
160 window->offset = offset;
161 window->ep = (u64)ep;
162 window->magic = SCIFEP_MAGIC;
163 window->reg_state = OP_IDLE;
164 init_waitqueue_head(&window->regwq);
165 window->unreg_state = OP_IDLE;
166 init_waitqueue_head(&window->unregwq);
167 INIT_LIST_HEAD(&window->list);
168 window->type = SCIF_WINDOW_SELF;
173 scif_free(window->dma_addr,
174 nr_pages * sizeof(*window->dma_addr));
175 scif_free(window, sizeof(*window));
181 * scif_destroy_incomplete_window:
183 * @window: registration window
185 * Deallocate resources for self window.
187 static void scif_destroy_incomplete_window(struct scif_endpt *ep,
188 struct scif_window *window)
191 int nr_pages = window->nr_pages;
192 struct scif_allocmsg *alloc = &window->alloc_handle;
196 /* Wait for a SCIF_ALLOC_GNT/REJ message */
197 err = wait_event_timeout(alloc->allocwq,
198 alloc->state != OP_IN_PROGRESS,
199 SCIF_NODE_ALIVE_TIMEOUT);
200 if (!err && scifdev_alive(ep))
203 mutex_lock(&ep->rma_info.rma_lock);
204 if (alloc->state == OP_COMPLETED) {
205 msg.uop = SCIF_FREE_VIRT;
207 msg.payload[0] = ep->remote_ep;
208 msg.payload[1] = window->alloc_handle.vaddr;
209 msg.payload[2] = (u64)window;
210 msg.payload[3] = SCIF_REGISTER;
211 _scif_nodeqp_send(ep->remote_dev, &msg);
213 mutex_unlock(&ep->rma_info.rma_lock);
215 scif_free_window_offset(ep, window, window->offset);
216 scif_free(window->dma_addr, nr_pages * sizeof(*window->dma_addr));
217 scif_free(window->num_pages, nr_pages * sizeof(*window->num_pages));
218 scif_free(window, sizeof(*window));
223 * @remote_dev: SCIF remote device
224 * @window: registration window
226 * Delete any DMA mappings created for a registered self window
228 void scif_unmap_window(struct scif_dev *remote_dev, struct scif_window *window)
232 if (scif_is_iommu_enabled() && !scifdev_self(remote_dev)) {
234 dma_unmap_sg(&remote_dev->sdev->dev,
235 window->st->sgl, window->st->nents,
237 sg_free_table(window->st);
242 for (j = 0; j < window->nr_contig_chunks; j++) {
243 if (window->dma_addr[j]) {
244 scif_unmap_single(window->dma_addr[j],
246 window->num_pages[j] <<
248 window->dma_addr[j] = 0x0;
254 static inline struct mm_struct *__scif_acquire_mm(void)
256 if (scif_ulimit_check)
257 return get_task_mm(current);
261 static inline void __scif_release_mm(struct mm_struct *mm)
268 __scif_dec_pinned_vm_lock(struct mm_struct *mm,
271 if (!mm || !nr_pages || !scif_ulimit_check)
274 atomic64_sub(nr_pages, &mm->pinned_vm);
278 static inline int __scif_check_inc_pinned_vm(struct mm_struct *mm,
281 unsigned long locked, lock_limit;
283 if (!mm || !nr_pages || !scif_ulimit_check)
286 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
287 locked = atomic64_add_return(nr_pages, &mm->pinned_vm);
289 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
290 atomic64_sub(nr_pages, &mm->pinned_vm);
291 dev_err(scif_info.mdev.this_device,
292 "locked(%lu) > lock_limit(%lu)\n",
300 * scif_destroy_window:
302 * @window: registration window
304 * Deallocate resources for self window.
306 int scif_destroy_window(struct scif_endpt *ep, struct scif_window *window)
309 struct scif_pinned_pages *pinned_pages = window->pinned_pages;
310 int nr_pages = window->nr_pages;
313 if (!window->temp && window->mm) {
314 __scif_dec_pinned_vm_lock(window->mm, window->nr_pages);
315 __scif_release_mm(window->mm);
319 scif_free_window_offset(ep, window, window->offset);
320 scif_unmap_window(ep->remote_dev, window);
322 * Decrement references for this set of pinned pages from
325 j = atomic_sub_return(1, &pinned_pages->ref_count);
327 dev_err(scif_info.mdev.this_device,
328 "%s %d incorrect ref count %d\n",
329 __func__, __LINE__, j);
331 * If the ref count for pinned_pages is zero then someone
332 * has already called scif_unpin_pages() for it and we should
333 * destroy the page cache.
336 scif_destroy_pinned_pages(window->pinned_pages);
337 scif_free(window->dma_addr, nr_pages * sizeof(*window->dma_addr));
338 scif_free(window->num_pages, nr_pages * sizeof(*window->num_pages));
340 scif_free(window, sizeof(*window));
345 * scif_create_remote_lookup:
346 * @remote_dev: SCIF remote device
347 * @window: remote window
349 * Allocate and prepare lookup entries for the remote
350 * end to copy over the physical addresses.
351 * Returns 0 on success and appropriate errno on failure.
353 static int scif_create_remote_lookup(struct scif_dev *remote_dev,
354 struct scif_window *window)
357 int nr_pages = window->nr_pages;
358 bool vmalloc_dma_phys, vmalloc_num_pages;
362 err = scif_map_single(&window->mapped_offset,
363 window, remote_dev, sizeof(*window));
367 /* Compute the number of lookup entries. 21 == 2MB Shift */
368 window->nr_lookup = ALIGN(nr_pages * PAGE_SIZE,
369 ((2) * 1024 * 1024)) >> 21;
371 window->dma_addr_lookup.lookup =
372 scif_alloc_coherent(&window->dma_addr_lookup.offset,
373 remote_dev, window->nr_lookup *
374 sizeof(*window->dma_addr_lookup.lookup),
375 GFP_KERNEL | __GFP_ZERO);
376 if (!window->dma_addr_lookup.lookup) {
381 window->num_pages_lookup.lookup =
382 scif_alloc_coherent(&window->num_pages_lookup.offset,
383 remote_dev, window->nr_lookup *
384 sizeof(*window->num_pages_lookup.lookup),
385 GFP_KERNEL | __GFP_ZERO);
386 if (!window->num_pages_lookup.lookup) {
391 vmalloc_dma_phys = is_vmalloc_addr(&window->dma_addr[0]);
392 vmalloc_num_pages = is_vmalloc_addr(&window->num_pages[0]);
394 /* Now map each of the pages containing physical addresses */
395 for (i = 0, j = 0; i < nr_pages; i += SCIF_NR_ADDR_IN_PAGE, j++) {
396 err = scif_map_page(&window->dma_addr_lookup.lookup[j],
398 vmalloc_to_page(&window->dma_addr[i]) :
399 virt_to_page(&window->dma_addr[i]),
403 err = scif_map_page(&window->num_pages_lookup.lookup[j],
405 vmalloc_to_page(&window->num_pages[i]) :
406 virt_to_page(&window->num_pages[i]),
417 * scif_destroy_remote_lookup:
418 * @remote_dev: SCIF remote device
419 * @window: remote window
421 * Destroy lookup entries used for the remote
422 * end to copy over the physical addresses.
424 static void scif_destroy_remote_lookup(struct scif_dev *remote_dev,
425 struct scif_window *window)
429 if (window->nr_lookup) {
430 struct scif_rma_lookup *lup = &window->dma_addr_lookup;
431 struct scif_rma_lookup *npup = &window->num_pages_lookup;
433 for (i = 0, j = 0; i < window->nr_pages;
434 i += SCIF_NR_ADDR_IN_PAGE, j++) {
435 if (lup->lookup && lup->lookup[j])
436 scif_unmap_single(lup->lookup[j],
439 if (npup->lookup && npup->lookup[j])
440 scif_unmap_single(npup->lookup[j],
445 scif_free_coherent(lup->lookup, lup->offset,
446 remote_dev, window->nr_lookup *
447 sizeof(*lup->lookup));
449 scif_free_coherent(npup->lookup, npup->offset,
450 remote_dev, window->nr_lookup *
451 sizeof(*npup->lookup));
452 if (window->mapped_offset)
453 scif_unmap_single(window->mapped_offset,
454 remote_dev, sizeof(*window));
455 window->nr_lookup = 0;
460 * scif_create_remote_window:
461 * @scifdev: SCIF device
462 * @nr_pages: number of pages in window
464 * Allocate and prepare a remote registration window.
466 static struct scif_window *
467 scif_create_remote_window(struct scif_dev *scifdev, int nr_pages)
469 struct scif_window *window;
472 window = scif_zalloc(sizeof(*window));
476 window->magic = SCIFEP_MAGIC;
477 window->nr_pages = nr_pages;
479 window->dma_addr = scif_zalloc(nr_pages * sizeof(*window->dma_addr));
480 if (!window->dma_addr)
483 window->num_pages = scif_zalloc(nr_pages *
484 sizeof(*window->num_pages));
485 if (!window->num_pages)
488 if (scif_create_remote_lookup(scifdev, window))
491 window->type = SCIF_WINDOW_PEER;
492 window->unreg_state = OP_IDLE;
493 INIT_LIST_HEAD(&window->list);
496 scif_destroy_remote_window(window);
502 * scif_destroy_remote_window:
503 * @window: remote registration window
505 * Deallocate resources for remote window.
508 scif_destroy_remote_window(struct scif_window *window)
510 scif_free(window->dma_addr, window->nr_pages *
511 sizeof(*window->dma_addr));
512 scif_free(window->num_pages, window->nr_pages *
513 sizeof(*window->num_pages));
515 scif_free(window, sizeof(*window));
519 * scif_iommu_map: create DMA mappings if the IOMMU is enabled
520 * @remote_dev: SCIF remote device
521 * @window: remote registration window
523 * Map the physical pages using dma_map_sg(..) and then detect the number
524 * of contiguous DMA mappings allocated
526 static int scif_iommu_map(struct scif_dev *remote_dev,
527 struct scif_window *window)
529 struct scatterlist *sg;
531 scif_pinned_pages_t pin = window->pinned_pages;
533 window->st = kzalloc(sizeof(*window->st), GFP_KERNEL);
537 err = sg_alloc_table(window->st, window->nr_pages, GFP_KERNEL);
541 for_each_sg(window->st->sgl, sg, window->st->nents, i)
542 sg_set_page(sg, pin->pages[i], PAGE_SIZE, 0x0);
544 err = dma_map_sg(&remote_dev->sdev->dev, window->st->sgl,
545 window->st->nents, DMA_BIDIRECTIONAL);
548 /* Detect contiguous ranges of DMA mappings */
549 sg = window->st->sgl;
550 for (i = 0; sg; i++) {
553 window->dma_addr[i] = sg_dma_address(sg);
554 window->num_pages[i] = sg_dma_len(sg) >> PAGE_SHIFT;
555 last_da = sg_dma_address(sg) + sg_dma_len(sg);
556 while ((sg = sg_next(sg)) && sg_dma_address(sg) == last_da) {
557 window->num_pages[i] +=
558 (sg_dma_len(sg) >> PAGE_SHIFT);
559 last_da = window->dma_addr[i] +
562 window->nr_contig_chunks++;
569 * @remote_dev: SCIF remote device
570 * @window: self registration window
572 * Map pages of a window into the aperture/PCI.
573 * Also determine addresses required for DMA.
576 scif_map_window(struct scif_dev *remote_dev, struct scif_window *window)
578 int i, j, k, err = 0, nr_contig_pages;
579 scif_pinned_pages_t pin;
580 phys_addr_t phys_prev, phys_curr;
584 pin = window->pinned_pages;
586 if (intel_iommu_enabled && !scifdev_self(remote_dev))
587 return scif_iommu_map(remote_dev, window);
589 for (i = 0, j = 0; i < window->nr_pages; i += nr_contig_pages, j++) {
590 phys_prev = page_to_phys(pin->pages[i]);
593 /* Detect physically contiguous chunks */
594 for (k = i + 1; k < window->nr_pages; k++) {
595 phys_curr = page_to_phys(pin->pages[k]);
596 if (phys_curr != (phys_prev + PAGE_SIZE))
598 phys_prev = phys_curr;
601 window->num_pages[j] = nr_contig_pages;
602 window->nr_contig_chunks++;
603 if (scif_is_mgmt_node()) {
605 * Management node has to deal with SMPT on X100 and
606 * hence the DMA mapping is required
608 err = scif_map_single(&window->dma_addr[j],
609 phys_to_virt(page_to_phys(
612 nr_contig_pages << PAGE_SHIFT);
616 window->dma_addr[j] = page_to_phys(pin->pages[i]);
623 * scif_send_scif_unregister:
625 * @window: self registration window
627 * Send a SCIF_UNREGISTER message.
629 static int scif_send_scif_unregister(struct scif_endpt *ep,
630 struct scif_window *window)
634 msg.uop = SCIF_UNREGISTER;
636 msg.payload[0] = window->alloc_handle.vaddr;
637 msg.payload[1] = (u64)window;
638 return scif_nodeqp_send(ep->remote_dev, &msg);
642 * scif_unregister_window:
643 * @window: self registration window
645 * Send an unregistration request and wait for a response.
647 int scif_unregister_window(struct scif_window *window)
650 struct scif_endpt *ep = (struct scif_endpt *)window->ep;
651 bool send_msg = false;
654 switch (window->unreg_state) {
657 window->unreg_state = OP_IN_PROGRESS;
663 scif_get_window(window, 1);
664 mutex_unlock(&ep->rma_info.rma_lock);
666 err = scif_send_scif_unregister(ep, window);
668 window->unreg_state = OP_COMPLETED;
672 /* Return ENXIO since unregistration is in progress */
673 mutex_lock(&ep->rma_info.rma_lock);
677 /* Wait for a SCIF_UNREGISTER_(N)ACK message */
678 err = wait_event_timeout(window->unregwq,
679 window->unreg_state != OP_IN_PROGRESS,
680 SCIF_NODE_ALIVE_TIMEOUT);
681 if (!err && scifdev_alive(ep))
685 window->unreg_state = OP_COMPLETED;
686 dev_err(scif_info.mdev.this_device,
687 "%s %d err %d\n", __func__, __LINE__, err);
692 mutex_lock(&ep->rma_info.rma_lock);
693 scif_put_window(window, 1);
698 if (!scifdev_alive(ep)) {
700 window->unreg_state = OP_COMPLETED;
710 if (window->unreg_state == OP_COMPLETED && window->ref_count)
711 scif_put_window(window, window->nr_pages);
713 if (!window->ref_count) {
714 atomic_inc(&ep->rma_info.tw_refcount);
715 list_del_init(&window->list);
716 scif_free_window_offset(ep, window, window->offset);
717 mutex_unlock(&ep->rma_info.rma_lock);
718 if ((!!(window->pinned_pages->map_flags & SCIF_MAP_KERNEL)) &&
720 scif_drain_dma_intr(ep->remote_dev->sdev,
721 ep->rma_info.dma_chan);
723 if (!__scif_dec_pinned_vm_lock(window->mm,
725 __scif_release_mm(window->mm);
729 scif_queue_for_cleanup(window, &scif_info.rma);
730 mutex_lock(&ep->rma_info.rma_lock);
736 * scif_send_alloc_request:
738 * @window: self registration window
740 * Send a remote window allocation request
742 static int scif_send_alloc_request(struct scif_endpt *ep,
743 struct scif_window *window)
746 struct scif_allocmsg *alloc = &window->alloc_handle;
748 /* Set up the Alloc Handle */
749 alloc->state = OP_IN_PROGRESS;
750 init_waitqueue_head(&alloc->allocwq);
752 /* Send out an allocation request */
753 msg.uop = SCIF_ALLOC_REQ;
754 msg.payload[1] = window->nr_pages;
755 msg.payload[2] = (u64)&window->alloc_handle;
756 return _scif_nodeqp_send(ep->remote_dev, &msg);
760 * scif_prep_remote_window:
762 * @window: self registration window
764 * Send a remote window allocation request, wait for an allocation response,
765 * and prepares the remote window by copying over the page lists
767 static int scif_prep_remote_window(struct scif_endpt *ep,
768 struct scif_window *window)
771 struct scif_window *remote_window;
772 struct scif_allocmsg *alloc = &window->alloc_handle;
773 dma_addr_t *dma_phys_lookup, *tmp, *num_pages_lookup, *tmp1;
775 int nr_contig_chunks, loop_nr_contig_chunks;
776 int remaining_nr_contig_chunks, nr_lookup;
779 map_err = scif_map_window(ep->remote_dev, window);
781 dev_err(&ep->remote_dev->sdev->dev,
782 "%s %d map_err %d\n", __func__, __LINE__, map_err);
783 remaining_nr_contig_chunks = window->nr_contig_chunks;
784 nr_contig_chunks = window->nr_contig_chunks;
786 /* Wait for a SCIF_ALLOC_GNT/REJ message */
787 err = wait_event_timeout(alloc->allocwq,
788 alloc->state != OP_IN_PROGRESS,
789 SCIF_NODE_ALIVE_TIMEOUT);
790 mutex_lock(&ep->rma_info.rma_lock);
791 /* Synchronize with the thread waking up allocwq */
792 mutex_unlock(&ep->rma_info.rma_lock);
793 if (!err && scifdev_alive(ep))
804 /* Bail out. The remote end rejected this request */
805 if (alloc->state == OP_FAILED)
809 dev_err(&ep->remote_dev->sdev->dev,
810 "%s %d err %d\n", __func__, __LINE__, map_err);
811 msg.uop = SCIF_FREE_VIRT;
813 msg.payload[0] = ep->remote_ep;
814 msg.payload[1] = window->alloc_handle.vaddr;
815 msg.payload[2] = (u64)window;
816 msg.payload[3] = SCIF_REGISTER;
817 spin_lock(&ep->lock);
818 if (ep->state == SCIFEP_CONNECTED)
819 err = _scif_nodeqp_send(ep->remote_dev, &msg);
822 spin_unlock(&ep->lock);
826 remote_window = scif_ioremap(alloc->phys_addr, sizeof(*window),
829 /* Compute the number of lookup entries. 21 == 2MB Shift */
830 nr_lookup = ALIGN(nr_contig_chunks, SCIF_NR_ADDR_IN_PAGE)
831 >> ilog2(SCIF_NR_ADDR_IN_PAGE);
834 scif_ioremap(remote_window->dma_addr_lookup.offset,
836 sizeof(*remote_window->dma_addr_lookup.lookup),
839 scif_ioremap(remote_window->num_pages_lookup.offset,
841 sizeof(*remote_window->num_pages_lookup.lookup),
844 while (remaining_nr_contig_chunks) {
845 loop_nr_contig_chunks = min_t(int, remaining_nr_contig_chunks,
846 (int)SCIF_NR_ADDR_IN_PAGE);
847 /* #1/2 - Copy physical addresses over to the remote side */
849 /* #2/2 - Copy DMA addresses (addresses that are fed into the
850 * DMA engine) We transfer bus addresses which are then
851 * converted into a MIC physical address on the remote
852 * side if it is a MIC, if the remote node is a mgmt node we
853 * transfer the MIC physical address
855 tmp = scif_ioremap(dma_phys_lookup[j],
856 loop_nr_contig_chunks *
857 sizeof(*window->dma_addr),
859 tmp1 = scif_ioremap(num_pages_lookup[j],
860 loop_nr_contig_chunks *
861 sizeof(*window->num_pages),
863 if (scif_is_mgmt_node()) {
864 memcpy_toio((void __force __iomem *)tmp,
865 &window->dma_addr[i], loop_nr_contig_chunks
866 * sizeof(*window->dma_addr));
867 memcpy_toio((void __force __iomem *)tmp1,
868 &window->num_pages[i], loop_nr_contig_chunks
869 * sizeof(*window->num_pages));
871 if (scifdev_is_p2p(ep->remote_dev)) {
873 * add remote node's base address for this node
874 * to convert it into a MIC address
879 for (m = 0; m < loop_nr_contig_chunks; m++) {
880 dma_addr = window->dma_addr[i + m] +
881 ep->remote_dev->base_addr;
883 (void __force __iomem *)&tmp[m]);
885 memcpy_toio((void __force __iomem *)tmp1,
886 &window->num_pages[i],
887 loop_nr_contig_chunks
888 * sizeof(*window->num_pages));
890 /* Mgmt node or loopback - transfer DMA
891 * addresses as is, this is the same as a
892 * MIC physical address (we use the dma_addr
893 * and not the phys_addr array since the
894 * phys_addr is only setup if there is a mmap()
895 * request from the mgmt node)
897 memcpy_toio((void __force __iomem *)tmp,
898 &window->dma_addr[i],
899 loop_nr_contig_chunks *
900 sizeof(*window->dma_addr));
901 memcpy_toio((void __force __iomem *)tmp1,
902 &window->num_pages[i],
903 loop_nr_contig_chunks *
904 sizeof(*window->num_pages));
907 remaining_nr_contig_chunks -= loop_nr_contig_chunks;
908 i += loop_nr_contig_chunks;
910 scif_iounmap(tmp, loop_nr_contig_chunks *
911 sizeof(*window->dma_addr), ep->remote_dev);
912 scif_iounmap(tmp1, loop_nr_contig_chunks *
913 sizeof(*window->num_pages), ep->remote_dev);
916 /* Prepare the remote window for the peer */
917 remote_window->peer_window = (u64)window;
918 remote_window->offset = window->offset;
919 remote_window->prot = window->prot;
920 remote_window->nr_contig_chunks = nr_contig_chunks;
921 remote_window->ep = ep->remote_ep;
922 scif_iounmap(num_pages_lookup,
924 sizeof(*remote_window->num_pages_lookup.lookup),
926 scif_iounmap(dma_phys_lookup,
928 sizeof(*remote_window->dma_addr_lookup.lookup),
930 scif_iounmap(remote_window, sizeof(*remote_window), ep->remote_dev);
931 window->peer_window = alloc->vaddr;
936 * scif_send_scif_register:
938 * @window: self registration window
940 * Send a SCIF_REGISTER message if EP is connected and wait for a
941 * SCIF_REGISTER_(N)ACK message else send a SCIF_FREE_VIRT
942 * message so that the peer can free its remote window allocated earlier.
944 static int scif_send_scif_register(struct scif_endpt *ep,
945 struct scif_window *window)
951 msg.payload[0] = ep->remote_ep;
952 msg.payload[1] = window->alloc_handle.vaddr;
953 msg.payload[2] = (u64)window;
954 spin_lock(&ep->lock);
955 if (ep->state == SCIFEP_CONNECTED) {
956 msg.uop = SCIF_REGISTER;
957 window->reg_state = OP_IN_PROGRESS;
958 err = _scif_nodeqp_send(ep->remote_dev, &msg);
959 spin_unlock(&ep->lock);
962 /* Wait for a SCIF_REGISTER_(N)ACK message */
963 err = wait_event_timeout(window->regwq,
966 SCIF_NODE_ALIVE_TIMEOUT);
967 if (!err && scifdev_alive(ep))
969 err = !err ? -ENODEV : 0;
970 if (window->reg_state == OP_FAILED)
974 msg.uop = SCIF_FREE_VIRT;
975 msg.payload[3] = SCIF_REGISTER;
976 err = _scif_nodeqp_send(ep->remote_dev, &msg);
977 spin_unlock(&ep->lock);
985 * scif_get_window_offset:
986 * @ep: end point descriptor
988 * @offset: offset hint
989 * @num_pages: number of pages
990 * @out_offset: computed offset returned by reference.
992 * Compute/Claim a new offset for this EP.
994 int scif_get_window_offset(struct scif_endpt *ep, int flags, s64 offset,
995 int num_pages, s64 *out_offset)
998 struct iova *iova_ptr;
1001 if (flags & SCIF_MAP_FIXED) {
1002 page_index = SCIF_IOVA_PFN(offset);
1003 iova_ptr = reserve_iova(&ep->rma_info.iovad, page_index,
1004 page_index + num_pages - 1);
1008 iova_ptr = alloc_iova(&ep->rma_info.iovad, num_pages,
1009 SCIF_DMA_63BIT_PFN - 1, 0);
1014 *out_offset = (iova_ptr->pfn_lo) << PAGE_SHIFT;
1019 * scif_free_window_offset:
1020 * @ep: end point descriptor
1021 * @window: registration window
1022 * @offset: Offset to be freed
1024 * Free offset for this EP. The callee is supposed to grab
1025 * the RMA mutex before calling this API.
1027 void scif_free_window_offset(struct scif_endpt *ep,
1028 struct scif_window *window, s64 offset)
1030 if ((window && !window->offset_freed) || !window) {
1031 free_iova(&ep->rma_info.iovad, offset >> PAGE_SHIFT);
1033 window->offset_freed = true;
1038 * scif_alloc_req: Respond to SCIF_ALLOC_REQ interrupt message
1039 * @scifdev: SCIF device
1040 * @msg: Interrupt message
1042 * Remote side is requesting a memory allocation.
1044 void scif_alloc_req(struct scif_dev *scifdev, struct scifmsg *msg)
1047 struct scif_window *window = NULL;
1048 int nr_pages = msg->payload[1];
1050 window = scif_create_remote_window(scifdev, nr_pages);
1056 /* The peer's allocation request is granted */
1057 msg->uop = SCIF_ALLOC_GNT;
1058 msg->payload[0] = (u64)window;
1059 msg->payload[1] = window->mapped_offset;
1060 err = scif_nodeqp_send(scifdev, msg);
1062 scif_destroy_remote_window(window);
1065 /* The peer's allocation request is rejected */
1066 dev_err(&scifdev->sdev->dev,
1067 "%s %d error %d alloc_ptr %p nr_pages 0x%x\n",
1068 __func__, __LINE__, err, window, nr_pages);
1069 msg->uop = SCIF_ALLOC_REJ;
1070 scif_nodeqp_send(scifdev, msg);
1074 * scif_alloc_gnt_rej: Respond to SCIF_ALLOC_GNT/REJ interrupt message
1075 * @scifdev: SCIF device
1076 * @msg: Interrupt message
1078 * Remote side responded to a memory allocation.
1080 void scif_alloc_gnt_rej(struct scif_dev *scifdev, struct scifmsg *msg)
1082 struct scif_allocmsg *handle = (struct scif_allocmsg *)msg->payload[2];
1083 struct scif_window *window = container_of(handle, struct scif_window,
1085 struct scif_endpt *ep = (struct scif_endpt *)window->ep;
1087 mutex_lock(&ep->rma_info.rma_lock);
1088 handle->vaddr = msg->payload[0];
1089 handle->phys_addr = msg->payload[1];
1090 if (msg->uop == SCIF_ALLOC_GNT)
1091 handle->state = OP_COMPLETED;
1093 handle->state = OP_FAILED;
1094 wake_up(&handle->allocwq);
1095 mutex_unlock(&ep->rma_info.rma_lock);
1099 * scif_free_virt: Respond to SCIF_FREE_VIRT interrupt message
1100 * @scifdev: SCIF device
1101 * @msg: Interrupt message
1103 * Free up memory kmalloc'd earlier.
1105 void scif_free_virt(struct scif_dev *scifdev, struct scifmsg *msg)
1107 struct scif_window *window = (struct scif_window *)msg->payload[1];
1109 scif_destroy_remote_window(window);
1113 scif_fixup_aper_base(struct scif_dev *dev, struct scif_window *window)
1116 struct scif_hw_dev *sdev = dev->sdev;
1117 phys_addr_t apt_base = 0;
1120 * Add the aperture base if the DMA address is not card relative
1121 * since the DMA addresses need to be an offset into the bar
1123 if (!scifdev_self(dev) && window->type == SCIF_WINDOW_PEER &&
1124 sdev->aper && !sdev->card_rel_da)
1125 apt_base = sdev->aper->pa;
1129 for (j = 0; j < window->nr_contig_chunks; j++) {
1130 if (window->num_pages[j])
1131 window->dma_addr[j] += apt_base;
1138 * scif_recv_reg: Respond to SCIF_REGISTER interrupt message
1139 * @scifdev: SCIF device
1140 * @msg: Interrupt message
1142 * Update remote window list with a new registered window.
1144 void scif_recv_reg(struct scif_dev *scifdev, struct scifmsg *msg)
1146 struct scif_endpt *ep = (struct scif_endpt *)msg->payload[0];
1147 struct scif_window *window =
1148 (struct scif_window *)msg->payload[1];
1150 mutex_lock(&ep->rma_info.rma_lock);
1151 spin_lock(&ep->lock);
1152 if (ep->state == SCIFEP_CONNECTED) {
1153 msg->uop = SCIF_REGISTER_ACK;
1154 scif_nodeqp_send(ep->remote_dev, msg);
1155 scif_fixup_aper_base(ep->remote_dev, window);
1156 /* No further failures expected. Insert new window */
1157 scif_insert_window(window, &ep->rma_info.remote_reg_list);
1159 msg->uop = SCIF_REGISTER_NACK;
1160 scif_nodeqp_send(ep->remote_dev, msg);
1162 spin_unlock(&ep->lock);
1163 mutex_unlock(&ep->rma_info.rma_lock);
1164 /* free up any lookup resources now that page lists are transferred */
1165 scif_destroy_remote_lookup(ep->remote_dev, window);
1167 * We could not insert the window but we need to
1168 * destroy the window.
1170 if (msg->uop == SCIF_REGISTER_NACK)
1171 scif_destroy_remote_window(window);
1175 * scif_recv_unreg: Respond to SCIF_UNREGISTER interrupt message
1176 * @scifdev: SCIF device
1177 * @msg: Interrupt message
1179 * Remove window from remote registration list;
1181 void scif_recv_unreg(struct scif_dev *scifdev, struct scifmsg *msg)
1183 struct scif_rma_req req;
1184 struct scif_window *window = NULL;
1185 struct scif_window *recv_window =
1186 (struct scif_window *)msg->payload[0];
1187 struct scif_endpt *ep;
1190 ep = (struct scif_endpt *)recv_window->ep;
1191 req.out_window = &window;
1192 req.offset = recv_window->offset;
1194 req.nr_bytes = recv_window->nr_pages << PAGE_SHIFT;
1195 req.type = SCIF_WINDOW_FULL;
1196 req.head = &ep->rma_info.remote_reg_list;
1197 msg->payload[0] = ep->remote_ep;
1199 mutex_lock(&ep->rma_info.rma_lock);
1200 /* Does a valid window exist? */
1201 if (scif_query_window(&req)) {
1202 dev_err(&scifdev->sdev->dev,
1203 "%s %d -ENXIO\n", __func__, __LINE__);
1204 msg->uop = SCIF_UNREGISTER_ACK;
1208 if (window->ref_count)
1209 scif_put_window(window, window->nr_pages);
1211 dev_err(&scifdev->sdev->dev,
1212 "%s %d ref count should be +ve\n",
1213 __func__, __LINE__);
1214 window->unreg_state = OP_COMPLETED;
1215 if (!window->ref_count) {
1216 msg->uop = SCIF_UNREGISTER_ACK;
1217 atomic_inc(&ep->rma_info.tw_refcount);
1218 ep->rma_info.async_list_del = 1;
1219 list_del_init(&window->list);
1222 /* NACK! There are valid references to this window */
1223 msg->uop = SCIF_UNREGISTER_NACK;
1226 /* The window did not make its way to the list at all. ACK */
1227 msg->uop = SCIF_UNREGISTER_ACK;
1228 scif_destroy_remote_window(recv_window);
1231 mutex_unlock(&ep->rma_info.rma_lock);
1233 scif_drain_dma_intr(ep->remote_dev->sdev,
1234 ep->rma_info.dma_chan);
1235 scif_nodeqp_send(ep->remote_dev, msg);
1237 scif_queue_for_cleanup(window, &scif_info.rma);
1241 * scif_recv_reg_ack: Respond to SCIF_REGISTER_ACK interrupt message
1242 * @scifdev: SCIF device
1243 * @msg: Interrupt message
1245 * Wake up the window waiting to complete registration.
1247 void scif_recv_reg_ack(struct scif_dev *scifdev, struct scifmsg *msg)
1249 struct scif_window *window =
1250 (struct scif_window *)msg->payload[2];
1251 struct scif_endpt *ep = (struct scif_endpt *)window->ep;
1253 mutex_lock(&ep->rma_info.rma_lock);
1254 window->reg_state = OP_COMPLETED;
1255 wake_up(&window->regwq);
1256 mutex_unlock(&ep->rma_info.rma_lock);
1260 * scif_recv_reg_nack: Respond to SCIF_REGISTER_NACK interrupt message
1261 * @scifdev: SCIF device
1262 * @msg: Interrupt message
1264 * Wake up the window waiting to inform it that registration
1265 * cannot be completed.
1267 void scif_recv_reg_nack(struct scif_dev *scifdev, struct scifmsg *msg)
1269 struct scif_window *window =
1270 (struct scif_window *)msg->payload[2];
1271 struct scif_endpt *ep = (struct scif_endpt *)window->ep;
1273 mutex_lock(&ep->rma_info.rma_lock);
1274 window->reg_state = OP_FAILED;
1275 wake_up(&window->regwq);
1276 mutex_unlock(&ep->rma_info.rma_lock);
1280 * scif_recv_unreg_ack: Respond to SCIF_UNREGISTER_ACK interrupt message
1281 * @scifdev: SCIF device
1282 * @msg: Interrupt message
1284 * Wake up the window waiting to complete unregistration.
1286 void scif_recv_unreg_ack(struct scif_dev *scifdev, struct scifmsg *msg)
1288 struct scif_window *window =
1289 (struct scif_window *)msg->payload[1];
1290 struct scif_endpt *ep = (struct scif_endpt *)window->ep;
1292 mutex_lock(&ep->rma_info.rma_lock);
1293 window->unreg_state = OP_COMPLETED;
1294 wake_up(&window->unregwq);
1295 mutex_unlock(&ep->rma_info.rma_lock);
1299 * scif_recv_unreg_nack: Respond to SCIF_UNREGISTER_NACK interrupt message
1300 * @scifdev: SCIF device
1301 * @msg: Interrupt message
1303 * Wake up the window waiting to inform it that unregistration
1304 * cannot be completed immediately.
1306 void scif_recv_unreg_nack(struct scif_dev *scifdev, struct scifmsg *msg)
1308 struct scif_window *window =
1309 (struct scif_window *)msg->payload[1];
1310 struct scif_endpt *ep = (struct scif_endpt *)window->ep;
1312 mutex_lock(&ep->rma_info.rma_lock);
1313 window->unreg_state = OP_FAILED;
1314 wake_up(&window->unregwq);
1315 mutex_unlock(&ep->rma_info.rma_lock);
1318 int __scif_pin_pages(void *addr, size_t len, int *out_prot,
1319 int map_flags, scif_pinned_pages_t *pages)
1321 struct scif_pinned_pages *pinned_pages;
1322 int nr_pages, err = 0, i;
1323 bool vmalloc_addr = false;
1324 bool try_upgrade = false;
1325 int prot = *out_prot;
1327 struct mm_struct *mm = NULL;
1329 /* Unsupported flags */
1330 if (map_flags & ~(SCIF_MAP_KERNEL | SCIF_MAP_ULIMIT))
1332 ulimit = !!(map_flags & SCIF_MAP_ULIMIT);
1334 /* Unsupported protection requested */
1335 if (prot & ~(SCIF_PROT_READ | SCIF_PROT_WRITE))
1338 /* addr/len must be page aligned. len should be non zero */
1340 (ALIGN((u64)addr, PAGE_SIZE) != (u64)addr) ||
1341 (ALIGN((u64)len, PAGE_SIZE) != (u64)len))
1346 nr_pages = len >> PAGE_SHIFT;
1348 /* Allocate a set of pinned pages */
1349 pinned_pages = scif_create_pinned_pages(nr_pages, prot);
1353 if (map_flags & SCIF_MAP_KERNEL) {
1354 if (is_vmalloc_addr(addr))
1355 vmalloc_addr = true;
1357 for (i = 0; i < nr_pages; i++) {
1359 pinned_pages->pages[i] =
1360 vmalloc_to_page(addr + (i * PAGE_SIZE));
1362 pinned_pages->pages[i] =
1363 virt_to_page(addr + (i * PAGE_SIZE));
1365 pinned_pages->nr_pages = nr_pages;
1366 pinned_pages->map_flags = SCIF_MAP_KERNEL;
1369 * SCIF supports registration caching. If a registration has
1370 * been requested with read only permissions, then we try
1371 * to pin the pages with RW permissions so that a subsequent
1372 * transfer with RW permission can hit the cache instead of
1373 * invalidating it. If the upgrade fails with RW then we
1374 * revert back to R permission and retry
1376 if (prot == SCIF_PROT_READ)
1378 prot |= SCIF_PROT_WRITE;
1382 err = __scif_check_inc_pinned_vm(mm, nr_pages);
1384 pinned_pages->nr_pages = 0;
1389 pinned_pages->nr_pages = pin_user_pages_fast(
1392 (prot & SCIF_PROT_WRITE) ? FOLL_WRITE : 0,
1393 pinned_pages->pages);
1394 if (nr_pages != pinned_pages->nr_pages) {
1397 __scif_dec_pinned_vm_lock(mm, nr_pages);
1398 /* Roll back any pinned pages */
1399 unpin_user_pages(pinned_pages->pages,
1400 pinned_pages->nr_pages);
1401 prot &= ~SCIF_PROT_WRITE;
1402 try_upgrade = false;
1406 pinned_pages->map_flags = 0;
1409 if (pinned_pages->nr_pages < nr_pages) {
1411 pinned_pages->nr_pages = nr_pages;
1416 atomic_set(&pinned_pages->ref_count, 1);
1417 *pages = pinned_pages;
1421 __scif_dec_pinned_vm_lock(mm, nr_pages);
1422 /* Something went wrong! Rollback */
1424 pinned_pages->nr_pages = nr_pages;
1425 scif_destroy_pinned_pages(pinned_pages);
1427 dev_dbg(scif_info.mdev.this_device,
1428 "%s %d err %d len 0x%lx\n", __func__, __LINE__, err, len);
1432 int scif_pin_pages(void *addr, size_t len, int prot,
1433 int map_flags, scif_pinned_pages_t *pages)
1435 return __scif_pin_pages(addr, len, &prot, map_flags, pages);
1437 EXPORT_SYMBOL_GPL(scif_pin_pages);
1439 int scif_unpin_pages(scif_pinned_pages_t pinned_pages)
1443 if (!pinned_pages || SCIFEP_MAGIC != pinned_pages->magic)
1446 ret = atomic_sub_return(1, &pinned_pages->ref_count);
1448 dev_err(scif_info.mdev.this_device,
1449 "%s %d scif_unpin_pages called without pinning? rc %d\n",
1450 __func__, __LINE__, ret);
1454 * Destroy the window if the ref count for this set of pinned
1455 * pages has dropped to zero. If it is positive then there is
1456 * a valid registered window which is backed by these pages and
1457 * it will be destroyed once all such windows are unregistered.
1460 err = scif_destroy_pinned_pages(pinned_pages);
1464 EXPORT_SYMBOL_GPL(scif_unpin_pages);
1467 scif_insert_local_window(struct scif_window *window, struct scif_endpt *ep)
1469 mutex_lock(&ep->rma_info.rma_lock);
1470 scif_insert_window(window, &ep->rma_info.reg_list);
1471 mutex_unlock(&ep->rma_info.rma_lock);
1474 off_t scif_register_pinned_pages(scif_epd_t epd,
1475 scif_pinned_pages_t pinned_pages,
1476 off_t offset, int map_flags)
1478 struct scif_endpt *ep = (struct scif_endpt *)epd;
1479 s64 computed_offset;
1480 struct scif_window *window;
1483 struct device *spdev;
1485 /* Unsupported flags */
1486 if (map_flags & ~SCIF_MAP_FIXED)
1489 len = pinned_pages->nr_pages << PAGE_SHIFT;
1492 * Offset is not page aligned/negative or offset+len
1493 * wraps around with SCIF_MAP_FIXED.
1495 if ((map_flags & SCIF_MAP_FIXED) &&
1496 ((ALIGN(offset, PAGE_SIZE) != offset) ||
1498 (len > LONG_MAX - offset)))
1503 err = scif_verify_epd(ep);
1507 * It is an error to pass pinned_pages to scif_register_pinned_pages()
1508 * after calling scif_unpin_pages().
1510 if (!atomic_add_unless(&pinned_pages->ref_count, 1, 0))
1513 /* Compute the offset for this registration */
1514 err = scif_get_window_offset(ep, map_flags, offset,
1515 len, &computed_offset);
1517 atomic_sub(1, &pinned_pages->ref_count);
1521 /* Allocate and prepare self registration window */
1522 window = scif_create_window(ep, pinned_pages->nr_pages,
1523 computed_offset, false);
1525 atomic_sub(1, &pinned_pages->ref_count);
1526 scif_free_window_offset(ep, NULL, computed_offset);
1530 window->pinned_pages = pinned_pages;
1531 window->nr_pages = pinned_pages->nr_pages;
1532 window->prot = pinned_pages->prot;
1534 spdev = scif_get_peer_dev(ep->remote_dev);
1535 if (IS_ERR(spdev)) {
1536 err = PTR_ERR(spdev);
1537 scif_destroy_window(ep, window);
1540 err = scif_send_alloc_request(ep, window);
1542 dev_err(&ep->remote_dev->sdev->dev,
1543 "%s %d err %d\n", __func__, __LINE__, err);
1547 /* Prepare the remote registration window */
1548 err = scif_prep_remote_window(ep, window);
1550 dev_err(&ep->remote_dev->sdev->dev,
1551 "%s %d err %d\n", __func__, __LINE__, err);
1555 /* Tell the peer about the new window */
1556 err = scif_send_scif_register(ep, window);
1558 dev_err(&ep->remote_dev->sdev->dev,
1559 "%s %d err %d\n", __func__, __LINE__, err);
1563 scif_put_peer_dev(spdev);
1564 /* No further failures expected. Insert new window */
1565 scif_insert_local_window(window, ep);
1566 return computed_offset;
1568 scif_destroy_window(ep, window);
1569 scif_put_peer_dev(spdev);
1570 dev_err(&ep->remote_dev->sdev->dev,
1571 "%s %d err %d\n", __func__, __LINE__, err);
1574 EXPORT_SYMBOL_GPL(scif_register_pinned_pages);
1576 off_t scif_register(scif_epd_t epd, void *addr, size_t len, off_t offset,
1577 int prot, int map_flags)
1579 scif_pinned_pages_t pinned_pages;
1581 struct scif_endpt *ep = (struct scif_endpt *)epd;
1582 s64 computed_offset;
1583 struct scif_window *window;
1584 struct mm_struct *mm = NULL;
1585 struct device *spdev;
1587 dev_dbg(scif_info.mdev.this_device,
1588 "SCIFAPI register: ep %p addr %p len 0x%lx offset 0x%lx prot 0x%x map_flags 0x%x\n",
1589 epd, addr, len, offset, prot, map_flags);
1590 /* Unsupported flags */
1591 if (map_flags & ~(SCIF_MAP_FIXED | SCIF_MAP_KERNEL))
1595 * Offset is not page aligned/negative or offset+len
1596 * wraps around with SCIF_MAP_FIXED.
1598 if ((map_flags & SCIF_MAP_FIXED) &&
1599 ((ALIGN(offset, PAGE_SIZE) != offset) ||
1601 (len > LONG_MAX - offset)))
1604 /* Unsupported protection requested */
1605 if (prot & ~(SCIF_PROT_READ | SCIF_PROT_WRITE))
1608 /* addr/len must be page aligned. len should be non zero */
1609 if (!len || (ALIGN((u64)addr, PAGE_SIZE) != (u64)addr) ||
1610 (ALIGN(len, PAGE_SIZE) != len))
1615 err = scif_verify_epd(ep);
1619 /* Compute the offset for this registration */
1620 err = scif_get_window_offset(ep, map_flags, offset,
1621 len >> PAGE_SHIFT, &computed_offset);
1625 spdev = scif_get_peer_dev(ep->remote_dev);
1626 if (IS_ERR(spdev)) {
1627 err = PTR_ERR(spdev);
1628 scif_free_window_offset(ep, NULL, computed_offset);
1631 /* Allocate and prepare self registration window */
1632 window = scif_create_window(ep, len >> PAGE_SHIFT,
1633 computed_offset, false);
1635 scif_free_window_offset(ep, NULL, computed_offset);
1636 scif_put_peer_dev(spdev);
1640 window->nr_pages = len >> PAGE_SHIFT;
1642 err = scif_send_alloc_request(ep, window);
1644 scif_destroy_incomplete_window(ep, window);
1645 scif_put_peer_dev(spdev);
1649 if (!(map_flags & SCIF_MAP_KERNEL)) {
1650 mm = __scif_acquire_mm();
1651 map_flags |= SCIF_MAP_ULIMIT;
1653 /* Pin down the pages */
1654 err = __scif_pin_pages(addr, len, &prot,
1655 map_flags & (SCIF_MAP_KERNEL | SCIF_MAP_ULIMIT),
1658 scif_destroy_incomplete_window(ep, window);
1659 __scif_release_mm(mm);
1663 window->pinned_pages = pinned_pages;
1664 window->prot = pinned_pages->prot;
1667 /* Prepare the remote registration window */
1668 err = scif_prep_remote_window(ep, window);
1670 dev_err(&ep->remote_dev->sdev->dev,
1671 "%s %d err %ld\n", __func__, __LINE__, err);
1675 /* Tell the peer about the new window */
1676 err = scif_send_scif_register(ep, window);
1678 dev_err(&ep->remote_dev->sdev->dev,
1679 "%s %d err %ld\n", __func__, __LINE__, err);
1683 scif_put_peer_dev(spdev);
1684 /* No further failures expected. Insert new window */
1685 scif_insert_local_window(window, ep);
1686 dev_dbg(&ep->remote_dev->sdev->dev,
1687 "SCIFAPI register: ep %p addr %p len 0x%lx computed_offset 0x%llx\n",
1688 epd, addr, len, computed_offset);
1689 return computed_offset;
1691 scif_destroy_window(ep, window);
1693 scif_put_peer_dev(spdev);
1694 dev_err(&ep->remote_dev->sdev->dev,
1695 "%s %d err %ld\n", __func__, __LINE__, err);
1698 EXPORT_SYMBOL_GPL(scif_register);
1701 scif_unregister(scif_epd_t epd, off_t offset, size_t len)
1703 struct scif_endpt *ep = (struct scif_endpt *)epd;
1704 struct scif_window *window = NULL;
1705 struct scif_rma_req req;
1707 struct device *spdev;
1709 dev_dbg(scif_info.mdev.this_device,
1710 "SCIFAPI unregister: ep %p offset 0x%lx len 0x%lx\n",
1712 /* len must be page aligned. len should be non zero */
1714 (ALIGN((u64)len, PAGE_SIZE) != (u64)len))
1717 /* Offset is not page aligned or offset+len wraps around */
1718 if ((ALIGN(offset, PAGE_SIZE) != offset) ||
1720 (len > LONG_MAX - offset))
1723 err = scif_verify_epd(ep);
1728 nr_pages = len >> PAGE_SHIFT;
1730 req.out_window = &window;
1731 req.offset = offset;
1734 req.type = SCIF_WINDOW_FULL;
1735 req.head = &ep->rma_info.reg_list;
1737 spdev = scif_get_peer_dev(ep->remote_dev);
1738 if (IS_ERR(spdev)) {
1739 err = PTR_ERR(spdev);
1742 mutex_lock(&ep->rma_info.rma_lock);
1743 /* Does a valid window exist? */
1744 err = scif_query_window(&req);
1746 dev_err(&ep->remote_dev->sdev->dev,
1747 "%s %d err %d\n", __func__, __LINE__, err);
1750 /* Unregister all the windows in this range */
1751 err = scif_rma_list_unregister(window, offset, nr_pages);
1753 dev_err(&ep->remote_dev->sdev->dev,
1754 "%s %d err %d\n", __func__, __LINE__, err);
1756 mutex_unlock(&ep->rma_info.rma_lock);
1757 scif_put_peer_dev(spdev);
1760 EXPORT_SYMBOL_GPL(scif_unregister);