This source file includes following definitions.
- scif_recv_munmap
- __scif_zap_mmaps
- _scif_zap_mmaps
- scif_zap_mmaps
- __scif_cleanup_rma_for_zombies
- scif_cleanup_rma_for_zombies
- scif_insert_vma
- scif_delete_vma
- scif_get_phys
- scif_get_pages
- scif_put_pages
- scif_rma_list_mmap
- scif_rma_list_munmap
- vma_pvt_release
- scif_vma_open
- scif_munmap
- scif_mmap
1
2
3
4
5
6
7
8
9 #include "scif_main.h"
10
11
12
13
14
15
16
17 struct scif_vma_info {
18 struct vm_area_struct *vma;
19 struct list_head list;
20 };
21
22 void scif_recv_munmap(struct scif_dev *scifdev, struct scifmsg *msg)
23 {
24 struct scif_rma_req req;
25 struct scif_window *window = NULL;
26 struct scif_window *recv_window =
27 (struct scif_window *)msg->payload[0];
28 struct scif_endpt *ep;
29
30 ep = (struct scif_endpt *)recv_window->ep;
31 req.out_window = &window;
32 req.offset = recv_window->offset;
33 req.prot = recv_window->prot;
34 req.nr_bytes = recv_window->nr_pages << PAGE_SHIFT;
35 req.type = SCIF_WINDOW_FULL;
36 req.head = &ep->rma_info.reg_list;
37 msg->payload[0] = ep->remote_ep;
38
39 mutex_lock(&ep->rma_info.rma_lock);
40
41 if (scif_query_window(&req)) {
42 dev_err(&scifdev->sdev->dev,
43 "%s %d -ENXIO\n", __func__, __LINE__);
44 msg->uop = SCIF_UNREGISTER_ACK;
45 goto error;
46 }
47
48 scif_put_window(window, window->nr_pages);
49
50 if (!window->ref_count) {
51 atomic_inc(&ep->rma_info.tw_refcount);
52 ep->rma_info.async_list_del = 1;
53 list_del_init(&window->list);
54 scif_free_window_offset(ep, window, window->offset);
55 }
56 error:
57 mutex_unlock(&ep->rma_info.rma_lock);
58 if (window && !window->ref_count)
59 scif_queue_for_cleanup(window, &scif_info.rma);
60 }
61
62
63
64
65
66 static void __scif_zap_mmaps(struct scif_endpt *ep)
67 {
68 struct list_head *item;
69 struct scif_vma_info *info;
70 struct vm_area_struct *vma;
71 unsigned long size;
72
73 spin_lock(&ep->lock);
74 list_for_each(item, &ep->rma_info.vma_list) {
75 info = list_entry(item, struct scif_vma_info, list);
76 vma = info->vma;
77 size = vma->vm_end - vma->vm_start;
78 zap_vma_ptes(vma, vma->vm_start, size);
79 dev_dbg(scif_info.mdev.this_device,
80 "%s ep %p zap vma %p size 0x%lx\n",
81 __func__, ep, info->vma, size);
82 }
83 spin_unlock(&ep->lock);
84 }
85
86
87
88
89
90 static void _scif_zap_mmaps(int node, struct list_head *head)
91 {
92 struct scif_endpt *ep;
93 struct list_head *item;
94
95 mutex_lock(&scif_info.connlock);
96 list_for_each(item, head) {
97 ep = list_entry(item, struct scif_endpt, list);
98 if (ep->remote_dev->node == node)
99 __scif_zap_mmaps(ep);
100 }
101 mutex_unlock(&scif_info.connlock);
102 }
103
104
105
106
107
108 void scif_zap_mmaps(int node)
109 {
110 _scif_zap_mmaps(node, &scif_info.connected);
111 _scif_zap_mmaps(node, &scif_info.disconnected);
112 }
113
114
115
116
117
118
119
120
121
122 static void __scif_cleanup_rma_for_zombies(struct scif_endpt *ep)
123 {
124 struct list_head *pos, *tmp;
125 struct scif_window *window;
126
127 list_for_each_safe(pos, tmp, &ep->rma_info.remote_reg_list) {
128 window = list_entry(pos, struct scif_window, list);
129 if (window->ref_count)
130 scif_put_window(window, window->nr_pages);
131 else
132 dev_err(scif_info.mdev.this_device,
133 "%s %d unexpected\n",
134 __func__, __LINE__);
135 if (!window->ref_count) {
136 atomic_inc(&ep->rma_info.tw_refcount);
137 list_del_init(&window->list);
138 scif_queue_for_cleanup(window, &scif_info.rma);
139 }
140 }
141 }
142
143
144 void scif_cleanup_rma_for_zombies(int node)
145 {
146 struct scif_endpt *ep;
147 struct list_head *item;
148
149 mutex_lock(&scif_info.eplock);
150 list_for_each(item, &scif_info.zombie) {
151 ep = list_entry(item, struct scif_endpt, list);
152 if (ep->remote_dev && ep->remote_dev->node == node)
153 __scif_cleanup_rma_for_zombies(ep);
154 }
155 mutex_unlock(&scif_info.eplock);
156 flush_work(&scif_info.misc_work);
157 }
158
159
160 static int scif_insert_vma(struct scif_endpt *ep, struct vm_area_struct *vma)
161 {
162 struct scif_vma_info *info;
163 int err = 0;
164
165 info = kzalloc(sizeof(*info), GFP_KERNEL);
166 if (!info) {
167 err = -ENOMEM;
168 goto done;
169 }
170 info->vma = vma;
171 spin_lock(&ep->lock);
172 list_add_tail(&info->list, &ep->rma_info.vma_list);
173 spin_unlock(&ep->lock);
174 done:
175 return err;
176 }
177
178
179 static void scif_delete_vma(struct scif_endpt *ep, struct vm_area_struct *vma)
180 {
181 struct list_head *item;
182 struct scif_vma_info *info;
183
184 spin_lock(&ep->lock);
185 list_for_each(item, &ep->rma_info.vma_list) {
186 info = list_entry(item, struct scif_vma_info, list);
187 if (info->vma == vma) {
188 list_del(&info->list);
189 kfree(info);
190 break;
191 }
192 }
193 spin_unlock(&ep->lock);
194 }
195
196 static phys_addr_t scif_get_phys(phys_addr_t phys, struct scif_endpt *ep)
197 {
198 struct scif_dev *scifdev = (struct scif_dev *)ep->remote_dev;
199 struct scif_hw_dev *sdev = scifdev->sdev;
200 phys_addr_t out_phys, apt_base = 0;
201
202
203
204
205
206 if (!scifdev_self(scifdev) && sdev->aper && sdev->card_rel_da)
207 apt_base = sdev->aper->pa;
208 out_phys = apt_base + phys;
209 return out_phys;
210 }
211
212 int scif_get_pages(scif_epd_t epd, off_t offset, size_t len,
213 struct scif_range **pages)
214 {
215 struct scif_endpt *ep = (struct scif_endpt *)epd;
216 struct scif_rma_req req;
217 struct scif_window *window = NULL;
218 int nr_pages, err, i;
219
220 dev_dbg(scif_info.mdev.this_device,
221 "SCIFAPI get_pinned_pages: ep %p offset 0x%lx len 0x%lx\n",
222 ep, offset, len);
223 err = scif_verify_epd(ep);
224 if (err)
225 return err;
226
227 if (!len || (offset < 0) ||
228 (offset + len < offset) ||
229 (ALIGN(offset, PAGE_SIZE) != offset) ||
230 (ALIGN(len, PAGE_SIZE) != len))
231 return -EINVAL;
232
233 nr_pages = len >> PAGE_SHIFT;
234
235 req.out_window = &window;
236 req.offset = offset;
237 req.prot = 0;
238 req.nr_bytes = len;
239 req.type = SCIF_WINDOW_SINGLE;
240 req.head = &ep->rma_info.remote_reg_list;
241
242 mutex_lock(&ep->rma_info.rma_lock);
243
244 err = scif_query_window(&req);
245 if (err) {
246 dev_err(&ep->remote_dev->sdev->dev,
247 "%s %d err %d\n", __func__, __LINE__, err);
248 goto error;
249 }
250
251
252 *pages = kzalloc(sizeof(**pages), GFP_KERNEL);
253 if (!*pages) {
254 err = -ENOMEM;
255 goto error;
256 }
257
258
259 (*pages)->phys_addr = scif_zalloc(nr_pages * sizeof(dma_addr_t));
260 if (!((*pages)->phys_addr)) {
261 err = -ENOMEM;
262 goto error;
263 }
264
265 if (scif_is_mgmt_node() && !scifdev_self(ep->remote_dev)) {
266
267 ((*pages)->va = scif_zalloc(nr_pages * sizeof(void *)));
268 if (!(*pages)->va) {
269 err = -ENOMEM;
270 goto error;
271 }
272 }
273
274 (*pages)->cookie = window;
275 (*pages)->nr_pages = nr_pages;
276 (*pages)->prot_flags = window->prot;
277
278 for (i = 0; i < nr_pages; i++) {
279 (*pages)->phys_addr[i] =
280 __scif_off_to_dma_addr(window, offset +
281 (i * PAGE_SIZE));
282 (*pages)->phys_addr[i] = scif_get_phys((*pages)->phys_addr[i],
283 ep);
284 if (scif_is_mgmt_node() && !scifdev_self(ep->remote_dev))
285 (*pages)->va[i] =
286 ep->remote_dev->sdev->aper->va +
287 (*pages)->phys_addr[i] -
288 ep->remote_dev->sdev->aper->pa;
289 }
290
291 scif_get_window(window, nr_pages);
292 error:
293 mutex_unlock(&ep->rma_info.rma_lock);
294 if (err) {
295 if (*pages) {
296 scif_free((*pages)->phys_addr,
297 nr_pages * sizeof(dma_addr_t));
298 scif_free((*pages)->va,
299 nr_pages * sizeof(void *));
300 kfree(*pages);
301 *pages = NULL;
302 }
303 dev_err(&ep->remote_dev->sdev->dev,
304 "%s %d err %d\n", __func__, __LINE__, err);
305 }
306 return err;
307 }
308 EXPORT_SYMBOL_GPL(scif_get_pages);
309
310 int scif_put_pages(struct scif_range *pages)
311 {
312 struct scif_endpt *ep;
313 struct scif_window *window;
314 struct scifmsg msg;
315
316 if (!pages || !pages->cookie)
317 return -EINVAL;
318
319 window = pages->cookie;
320
321 if (!window || window->magic != SCIFEP_MAGIC)
322 return -EINVAL;
323
324 ep = (struct scif_endpt *)window->ep;
325
326
327
328
329
330
331 if (ep->state != SCIFEP_CONNECTED && ep->state != SCIFEP_DISCONNECTED)
332 return -ENOTCONN;
333
334 mutex_lock(&ep->rma_info.rma_lock);
335
336 scif_put_window(window, pages->nr_pages);
337
338
339 if (!window->ref_count) {
340 list_del(&window->list);
341 mutex_unlock(&ep->rma_info.rma_lock);
342 scif_drain_dma_intr(ep->remote_dev->sdev,
343 ep->rma_info.dma_chan);
344
345 msg.uop = SCIF_MUNMAP;
346 msg.src = ep->port;
347 msg.payload[0] = window->peer_window;
348
349 scif_nodeqp_send(ep->remote_dev, &msg);
350
351 scif_destroy_remote_window(window);
352 } else {
353 mutex_unlock(&ep->rma_info.rma_lock);
354 }
355
356 scif_free(pages->phys_addr, pages->nr_pages * sizeof(dma_addr_t));
357 scif_free(pages->va, pages->nr_pages * sizeof(void *));
358 kfree(pages);
359 return 0;
360 }
361 EXPORT_SYMBOL_GPL(scif_put_pages);
362
363
364
365
366
367
368
369
370
371
372 static int scif_rma_list_mmap(struct scif_window *start_window, s64 offset,
373 int nr_pages, struct vm_area_struct *vma)
374 {
375 s64 end_offset, loop_offset = offset;
376 struct scif_window *window = start_window;
377 int loop_nr_pages, nr_pages_left = nr_pages;
378 struct scif_endpt *ep = (struct scif_endpt *)start_window->ep;
379 struct list_head *head = &ep->rma_info.remote_reg_list;
380 int i, err = 0;
381 dma_addr_t phys_addr;
382 struct scif_window_iter src_win_iter;
383 size_t contig_bytes = 0;
384
385 might_sleep();
386 list_for_each_entry_from(window, head, list) {
387 end_offset = window->offset +
388 (window->nr_pages << PAGE_SHIFT);
389 loop_nr_pages = min_t(int,
390 (end_offset - loop_offset) >> PAGE_SHIFT,
391 nr_pages_left);
392 scif_init_window_iter(window, &src_win_iter);
393 for (i = 0; i < loop_nr_pages; i++) {
394 phys_addr = scif_off_to_dma_addr(window, loop_offset,
395 &contig_bytes,
396 &src_win_iter);
397 phys_addr = scif_get_phys(phys_addr, ep);
398 err = remap_pfn_range(vma,
399 vma->vm_start +
400 loop_offset - offset,
401 phys_addr >> PAGE_SHIFT,
402 PAGE_SIZE,
403 vma->vm_page_prot);
404 if (err)
405 goto error;
406 loop_offset += PAGE_SIZE;
407 }
408 nr_pages_left -= loop_nr_pages;
409 if (!nr_pages_left)
410 break;
411 }
412
413
414
415
416
417
418 loop_offset = offset;
419 nr_pages_left = nr_pages;
420 window = start_window;
421 head = &ep->rma_info.remote_reg_list;
422 list_for_each_entry_from(window, head, list) {
423 end_offset = window->offset +
424 (window->nr_pages << PAGE_SHIFT);
425 loop_nr_pages = min_t(int,
426 (end_offset - loop_offset) >> PAGE_SHIFT,
427 nr_pages_left);
428 scif_get_window(window, loop_nr_pages);
429 nr_pages_left -= loop_nr_pages;
430 loop_offset += (loop_nr_pages << PAGE_SHIFT);
431 if (!nr_pages_left)
432 break;
433 }
434 error:
435 if (err)
436 dev_err(scif_info.mdev.this_device,
437 "%s %d err %d\n", __func__, __LINE__, err);
438 return err;
439 }
440
441
442
443
444
445
446
447
448
449 static void scif_rma_list_munmap(struct scif_window *start_window,
450 s64 offset, int nr_pages)
451 {
452 struct scifmsg msg;
453 s64 loop_offset = offset, end_offset;
454 int loop_nr_pages, nr_pages_left = nr_pages;
455 struct scif_endpt *ep = (struct scif_endpt *)start_window->ep;
456 struct list_head *head = &ep->rma_info.remote_reg_list;
457 struct scif_window *window = start_window, *_window;
458
459 msg.uop = SCIF_MUNMAP;
460 msg.src = ep->port;
461 loop_offset = offset;
462 nr_pages_left = nr_pages;
463 list_for_each_entry_safe_from(window, _window, head, list) {
464 end_offset = window->offset +
465 (window->nr_pages << PAGE_SHIFT);
466 loop_nr_pages = min_t(int,
467 (end_offset - loop_offset) >> PAGE_SHIFT,
468 nr_pages_left);
469 scif_put_window(window, loop_nr_pages);
470 if (!window->ref_count) {
471 struct scif_dev *rdev = ep->remote_dev;
472
473 scif_drain_dma_intr(rdev->sdev,
474 ep->rma_info.dma_chan);
475
476 msg.payload[0] = window->peer_window;
477
478 scif_nodeqp_send(ep->remote_dev, &msg);
479 list_del(&window->list);
480
481 scif_destroy_remote_window(window);
482 }
483 nr_pages_left -= loop_nr_pages;
484 loop_offset += (loop_nr_pages << PAGE_SHIFT);
485 if (!nr_pages_left)
486 break;
487 }
488 }
489
490
491
492
493
494 struct vma_pvt {
495 struct scif_endpt *ep;
496 s64 offset;
497 bool valid_offset;
498
499
500
501
502 struct kref ref;
503 };
504
505 static void vma_pvt_release(struct kref *ref)
506 {
507 struct vma_pvt *vmapvt = container_of(ref, struct vma_pvt, ref);
508
509 kfree(vmapvt);
510 }
511
512
513
514
515
516
517
518
519
520
521
522
523
524 static void scif_vma_open(struct vm_area_struct *vma)
525 {
526 struct vma_pvt *vmapvt = vma->vm_private_data;
527
528 dev_dbg(scif_info.mdev.this_device,
529 "SCIFAPI vma open: vma_start 0x%lx vma_end 0x%lx\n",
530 vma->vm_start, vma->vm_end);
531 scif_insert_vma(vmapvt->ep, vma);
532 kref_get(&vmapvt->ref);
533 }
534
535
536
537
538
539
540
541
542 static void scif_munmap(struct vm_area_struct *vma)
543 {
544 struct scif_endpt *ep;
545 struct vma_pvt *vmapvt = vma->vm_private_data;
546 int nr_pages = vma_pages(vma);
547 s64 offset;
548 struct scif_rma_req req;
549 struct scif_window *window = NULL;
550 int err;
551
552 might_sleep();
553 dev_dbg(scif_info.mdev.this_device,
554 "SCIFAPI munmap: vma_start 0x%lx vma_end 0x%lx\n",
555 vma->vm_start, vma->vm_end);
556 ep = vmapvt->ep;
557 offset = vmapvt->valid_offset ? vmapvt->offset :
558 (vma->vm_pgoff) << PAGE_SHIFT;
559 dev_dbg(scif_info.mdev.this_device,
560 "SCIFAPI munmap: ep %p nr_pages 0x%x offset 0x%llx\n",
561 ep, nr_pages, offset);
562 req.out_window = &window;
563 req.offset = offset;
564 req.nr_bytes = vma->vm_end - vma->vm_start;
565 req.prot = vma->vm_flags & (VM_READ | VM_WRITE);
566 req.type = SCIF_WINDOW_PARTIAL;
567 req.head = &ep->rma_info.remote_reg_list;
568
569 mutex_lock(&ep->rma_info.rma_lock);
570
571 err = scif_query_window(&req);
572 if (err)
573 dev_err(scif_info.mdev.this_device,
574 "%s %d err %d\n", __func__, __LINE__, err);
575 else
576 scif_rma_list_munmap(window, offset, nr_pages);
577
578 mutex_unlock(&ep->rma_info.rma_lock);
579
580
581
582
583 vma->vm_ops = NULL;
584 vma->vm_private_data = NULL;
585 kref_put(&vmapvt->ref, vma_pvt_release);
586 scif_delete_vma(ep, vma);
587 }
588
589 static const struct vm_operations_struct scif_vm_ops = {
590 .open = scif_vma_open,
591 .close = scif_munmap,
592 };
593
594
595
596
597
598
599
600
601
602 int scif_mmap(struct vm_area_struct *vma, scif_epd_t epd)
603 {
604 struct scif_rma_req req;
605 struct scif_window *window = NULL;
606 struct scif_endpt *ep = (struct scif_endpt *)epd;
607 s64 start_offset = vma->vm_pgoff << PAGE_SHIFT;
608 int nr_pages = vma_pages(vma);
609 int err;
610 struct vma_pvt *vmapvt;
611
612 dev_dbg(scif_info.mdev.this_device,
613 "SCIFAPI mmap: ep %p start_offset 0x%llx nr_pages 0x%x\n",
614 ep, start_offset, nr_pages);
615 err = scif_verify_epd(ep);
616 if (err)
617 return err;
618
619 might_sleep();
620
621 err = scif_insert_vma(ep, vma);
622 if (err)
623 return err;
624
625 vmapvt = kzalloc(sizeof(*vmapvt), GFP_KERNEL);
626 if (!vmapvt) {
627 scif_delete_vma(ep, vma);
628 return -ENOMEM;
629 }
630
631 vmapvt->ep = ep;
632 kref_init(&vmapvt->ref);
633
634 req.out_window = &window;
635 req.offset = start_offset;
636 req.nr_bytes = vma->vm_end - vma->vm_start;
637 req.prot = vma->vm_flags & (VM_READ | VM_WRITE);
638 req.type = SCIF_WINDOW_PARTIAL;
639 req.head = &ep->rma_info.remote_reg_list;
640
641 mutex_lock(&ep->rma_info.rma_lock);
642
643 err = scif_query_window(&req);
644 if (err) {
645 dev_err(&ep->remote_dev->sdev->dev,
646 "%s %d err %d\n", __func__, __LINE__, err);
647 goto error_unlock;
648 }
649
650
651 if (!scifdev_self(ep->remote_dev))
652 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
653
654
655
656
657
658
659
660
661
662
663
664
665
666 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
667
668 if (!scifdev_self(ep->remote_dev))
669 vma->vm_flags |= VM_IO | VM_PFNMAP;
670
671
672 err = scif_rma_list_mmap(window, start_offset, nr_pages, vma);
673 if (err) {
674 dev_err(&ep->remote_dev->sdev->dev,
675 "%s %d err %d\n", __func__, __LINE__, err);
676 goto error_unlock;
677 }
678
679 vma->vm_ops = &scif_vm_ops;
680 vma->vm_private_data = vmapvt;
681 error_unlock:
682 mutex_unlock(&ep->rma_info.rma_lock);
683 if (err) {
684 kfree(vmapvt);
685 dev_err(&ep->remote_dev->sdev->dev,
686 "%s %d err %d\n", __func__, __LINE__, err);
687 scif_delete_vma(ep, vma);
688 }
689 return err;
690 }