1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * RapidIO mport character device
5 * Copyright 2014-2015 Integrated Device Technology, Inc.
6 * Alexandre Bounine <alexandre.bounine@idt.com>
7 * Copyright 2014-2015 Prodrive Technologies
8 * Andre van Herk <andre.van.herk@prodrive-technologies.com>
9 * Jerry Jacobs <jerry.jacobs@prodrive-technologies.com>
10 * Copyright (C) 2014 Texas Instruments Incorporated
11 * Aurelien Jacquiot <a-jacquiot@ti.com>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/cdev.h>
16 #include <linux/ioctl.h>
17 #include <linux/uaccess.h>
18 #include <linux/list.h>
20 #include <linux/err.h>
21 #include <linux/net.h>
22 #include <linux/poll.h>
23 #include <linux/spinlock.h>
24 #include <linux/sched.h>
25 #include <linux/kfifo.h>
28 #include <linux/slab.h>
29 #include <linux/vmalloc.h>
30 #include <linux/mman.h>
32 #include <linux/dma-mapping.h>
33 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
34 #include <linux/dmaengine.h>
37 #include <linux/rio.h>
38 #include <linux/rio_ids.h>
39 #include <linux/rio_drv.h>
40 #include <linux/rio_mport_cdev.h>
44 #define DRV_NAME "rio_mport"
45 #define DRV_PREFIX DRV_NAME ": "
46 #define DEV_NAME "rio_mport"
47 #define DRV_VERSION "1.0.0"
49 /* Debug output filtering masks */
52 DBG_INIT
= BIT(0), /* driver init */
53 DBG_EXIT
= BIT(1), /* driver exit */
54 DBG_MPORT
= BIT(2), /* mport add/remove */
55 DBG_RDEV
= BIT(3), /* RapidIO device add/remove */
56 DBG_DMA
= BIT(4), /* DMA transfer messages */
57 DBG_MMAP
= BIT(5), /* mapping messages */
58 DBG_IBW
= BIT(6), /* inbound window */
59 DBG_EVENT
= BIT(7), /* event handling messages */
60 DBG_OBW
= BIT(8), /* outbound window messages */
61 DBG_DBELL
= BIT(9), /* doorbell messages */
66 #define rmcd_debug(level, fmt, arg...) \
68 if (DBG_##level & dbg_level) \
69 pr_debug(DRV_PREFIX "%s: " fmt "\n", __func__, ##arg); \
72 #define rmcd_debug(level, fmt, arg...) \
73 no_printk(KERN_DEBUG pr_fmt(DRV_PREFIX fmt "\n"), ##arg)
76 #define rmcd_warn(fmt, arg...) \
77 pr_warn(DRV_PREFIX "%s WARNING " fmt "\n", __func__, ##arg)
79 #define rmcd_error(fmt, arg...) \
80 pr_err(DRV_PREFIX "%s ERROR " fmt "\n", __func__, ##arg)
82 MODULE_AUTHOR("Jerry Jacobs <jerry.jacobs@prodrive-technologies.com>");
83 MODULE_AUTHOR("Aurelien Jacquiot <a-jacquiot@ti.com>");
84 MODULE_AUTHOR("Alexandre Bounine <alexandre.bounine@idt.com>");
85 MODULE_AUTHOR("Andre van Herk <andre.van.herk@prodrive-technologies.com>");
86 MODULE_DESCRIPTION("RapidIO mport character device driver");
87 MODULE_LICENSE("GPL");
88 MODULE_VERSION(DRV_VERSION
);
90 static int dma_timeout
= 3000; /* DMA transfer timeout in msec */
91 module_param(dma_timeout
, int, S_IRUGO
);
92 MODULE_PARM_DESC(dma_timeout
, "DMA Transfer Timeout in msec (default: 3000)");
95 static u32 dbg_level
= DBG_NONE
;
96 module_param(dbg_level
, uint
, S_IWUSR
| S_IWGRP
| S_IRUGO
);
97 MODULE_PARM_DESC(dbg_level
, "Debugging output level (default 0 = none)");
101 * An internal DMA coherent buffer
103 struct mport_dma_buf
{
113 * Internal memory mapping structure
115 enum rio_mport_map_dir
{
121 struct rio_mport_mapping
{
122 struct list_head node
;
123 struct mport_dev
*md
;
124 enum rio_mport_map_dir dir
;
127 dma_addr_t phys_addr
; /* for mmap */
128 void *virt_addr
; /* kernel address, for dma_free_coherent */
130 struct kref ref
; /* refcount of vmas sharing the mapping */
134 struct rio_mport_dma_map
{
141 #define MPORT_MAX_DMA_BUFS 16
142 #define MPORT_EVENT_DEPTH 10
145 * mport_dev driver-specific structure that represents mport device
146 * @active mport device status flag
147 * @node list node to maintain list of registered mports
148 * @cdev character device
149 * @dev associated device object
150 * @mport associated subsystem's master port device object
151 * @buf_mutex lock for buffer handling
152 * @file_mutex - lock for open files list
153 * @file_list - list of open files on given mport
154 * @properties properties of this mport
155 * @portwrites queue of inbound portwrites
156 * @pw_lock lock for port write queue
157 * @mappings queue for memory mappings
158 * @dma_chan DMA channels associated with this device
164 struct list_head node
;
167 struct rio_mport
*mport
;
168 struct mutex buf_mutex
;
169 struct mutex file_mutex
;
170 struct list_head file_list
;
171 struct rio_mport_properties properties
;
172 struct list_head doorbells
;
174 struct list_head portwrites
;
176 struct list_head mappings
;
177 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
178 struct dma_chan
*dma_chan
;
180 struct completion comp
;
185 * mport_cdev_priv - data structure specific to individual file object
186 * associated with an open device
187 * @md master port character device object
188 * @async_queue - asynchronous notification queue
189 * @list - file objects tracking list
190 * @db_filters inbound doorbell filters for this descriptor
191 * @pw_filters portwrite filters for this descriptor
192 * @event_fifo event fifo for this descriptor
193 * @event_rx_wait wait queue for this descriptor
194 * @fifo_lock lock for event_fifo
195 * @event_mask event mask for this descriptor
196 * @dmach DMA engine channel allocated for specific file object
198 struct mport_cdev_priv
{
199 struct mport_dev
*md
;
200 struct fasync_struct
*async_queue
;
201 struct list_head list
;
202 struct list_head db_filters
;
203 struct list_head pw_filters
;
204 struct kfifo event_fifo
;
205 wait_queue_head_t event_rx_wait
;
206 spinlock_t fifo_lock
;
207 u32 event_mask
; /* RIO_DOORBELL, RIO_PORTWRITE */
208 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
209 struct dma_chan
*dmach
;
210 struct list_head async_list
;
212 struct mutex dma_lock
;
214 struct completion comp
;
219 * rio_mport_pw_filter - structure to describe a portwrite filter
220 * md_node node in mport device's list
221 * priv_node node in private file object's list
222 * priv reference to private data
223 * filter actual portwrite filter
225 struct rio_mport_pw_filter
{
226 struct list_head md_node
;
227 struct list_head priv_node
;
228 struct mport_cdev_priv
*priv
;
229 struct rio_pw_filter filter
;
233 * rio_mport_db_filter - structure to describe a doorbell filter
234 * @data_node reference to device node
235 * @priv_node node in private data
236 * @priv reference to private data
237 * @filter actual doorbell filter
239 struct rio_mport_db_filter
{
240 struct list_head data_node
;
241 struct list_head priv_node
;
242 struct mport_cdev_priv
*priv
;
243 struct rio_doorbell_filter filter
;
246 static LIST_HEAD(mport_devs
);
247 static DEFINE_MUTEX(mport_devs_lock
);
249 #if (0) /* used by commented out portion of poll function : FIXME */
250 static DECLARE_WAIT_QUEUE_HEAD(mport_cdev_wait
);
253 static const struct class dev_class
= {
256 static dev_t dev_number
;
258 static void mport_release_mapping(struct kref
*ref
);
260 static int rio_mport_maint_rd(struct mport_cdev_priv
*priv
, void __user
*arg
,
263 struct rio_mport
*mport
= priv
->md
->mport
;
264 struct rio_mport_maint_io maint_io
;
270 if (unlikely(copy_from_user(&maint_io
, arg
, sizeof(maint_io
))))
273 if ((maint_io
.offset
% 4) ||
274 (maint_io
.length
== 0) || (maint_io
.length
% 4) ||
275 (maint_io
.length
+ maint_io
.offset
) > RIO_MAINT_SPACE_SZ
)
278 buffer
= vmalloc(maint_io
.length
);
281 length
= maint_io
.length
/sizeof(u32
);
282 offset
= maint_io
.offset
;
284 for (i
= 0; i
< length
; i
++) {
286 ret
= __rio_local_read_config_32(mport
,
289 ret
= rio_mport_read_config_32(mport
, maint_io
.rioid
,
290 maint_io
.hopcount
, offset
, &buffer
[i
]);
297 if (unlikely(copy_to_user((void __user
*)(uintptr_t)maint_io
.buffer
,
298 buffer
, maint_io
.length
)))
305 static int rio_mport_maint_wr(struct mport_cdev_priv
*priv
, void __user
*arg
,
308 struct rio_mport
*mport
= priv
->md
->mport
;
309 struct rio_mport_maint_io maint_io
;
313 int ret
= -EINVAL
, i
;
315 if (unlikely(copy_from_user(&maint_io
, arg
, sizeof(maint_io
))))
318 if ((maint_io
.offset
% 4) ||
319 (maint_io
.length
== 0) || (maint_io
.length
% 4) ||
320 (maint_io
.length
+ maint_io
.offset
) > RIO_MAINT_SPACE_SZ
)
323 buffer
= vmalloc(maint_io
.length
);
326 length
= maint_io
.length
;
328 if (unlikely(copy_from_user(buffer
,
329 (void __user
*)(uintptr_t)maint_io
.buffer
, length
))) {
334 offset
= maint_io
.offset
;
335 length
/= sizeof(u32
);
337 for (i
= 0; i
< length
; i
++) {
339 ret
= __rio_local_write_config_32(mport
,
342 ret
= rio_mport_write_config_32(mport
, maint_io
.rioid
,
358 * Inbound/outbound memory mapping functions
361 rio_mport_create_outbound_mapping(struct mport_dev
*md
, struct file
*filp
,
362 u16 rioid
, u64 raddr
, u32 size
,
365 struct rio_mport
*mport
= md
->mport
;
366 struct rio_mport_mapping
*map
;
369 rmcd_debug(OBW
, "did=%d ra=0x%llx sz=0x%x", rioid
, raddr
, size
);
371 map
= kzalloc(sizeof(*map
), GFP_KERNEL
);
375 ret
= rio_map_outb_region(mport
, rioid
, raddr
, size
, 0, paddr
);
379 map
->dir
= MAP_OUTBOUND
;
381 map
->rio_addr
= raddr
;
383 map
->phys_addr
= *paddr
;
386 kref_init(&map
->ref
);
387 list_add_tail(&map
->node
, &md
->mappings
);
395 rio_mport_get_outbound_mapping(struct mport_dev
*md
, struct file
*filp
,
396 u16 rioid
, u64 raddr
, u32 size
,
399 struct rio_mport_mapping
*map
;
402 mutex_lock(&md
->buf_mutex
);
403 list_for_each_entry(map
, &md
->mappings
, node
) {
404 if (map
->dir
!= MAP_OUTBOUND
)
406 if (rioid
== map
->rioid
&&
407 raddr
== map
->rio_addr
&& size
== map
->size
) {
408 *paddr
= map
->phys_addr
;
411 } else if (rioid
== map
->rioid
&&
412 raddr
< (map
->rio_addr
+ map
->size
- 1) &&
413 (raddr
+ size
) > map
->rio_addr
) {
419 /* If not found, create new */
421 err
= rio_mport_create_outbound_mapping(md
, filp
, rioid
, raddr
,
423 mutex_unlock(&md
->buf_mutex
);
427 static int rio_mport_obw_map(struct file
*filp
, void __user
*arg
)
429 struct mport_cdev_priv
*priv
= filp
->private_data
;
430 struct mport_dev
*data
= priv
->md
;
435 if (unlikely(copy_from_user(&map
, arg
, sizeof(map
))))
438 rmcd_debug(OBW
, "did=%d ra=0x%llx sz=0x%llx",
439 map
.rioid
, map
.rio_addr
, map
.length
);
441 ret
= rio_mport_get_outbound_mapping(data
, filp
, map
.rioid
,
442 map
.rio_addr
, map
.length
, &paddr
);
444 rmcd_error("Failed to set OBW err= %d", ret
);
450 if (unlikely(copy_to_user(arg
, &map
, sizeof(map
))))
456 * rio_mport_obw_free() - unmap an OutBound Window from RapidIO address space
458 * @priv: driver private data
459 * @arg: buffer handle returned by allocation routine
461 static int rio_mport_obw_free(struct file
*filp
, void __user
*arg
)
463 struct mport_cdev_priv
*priv
= filp
->private_data
;
464 struct mport_dev
*md
= priv
->md
;
466 struct rio_mport_mapping
*map
, *_map
;
468 if (!md
->mport
->ops
->unmap_outb
)
469 return -EPROTONOSUPPORT
;
471 if (copy_from_user(&handle
, arg
, sizeof(handle
)))
474 rmcd_debug(OBW
, "h=0x%llx", handle
);
476 mutex_lock(&md
->buf_mutex
);
477 list_for_each_entry_safe(map
, _map
, &md
->mappings
, node
) {
478 if (map
->dir
== MAP_OUTBOUND
&& map
->phys_addr
== handle
) {
479 if (map
->filp
== filp
) {
480 rmcd_debug(OBW
, "kref_put h=0x%llx", handle
);
482 kref_put(&map
->ref
, mport_release_mapping
);
487 mutex_unlock(&md
->buf_mutex
);
493 * maint_hdid_set() - Set the host Device ID
494 * @priv: driver private data
497 static int maint_hdid_set(struct mport_cdev_priv
*priv
, void __user
*arg
)
499 struct mport_dev
*md
= priv
->md
;
502 if (copy_from_user(&hdid
, arg
, sizeof(hdid
)))
505 md
->mport
->host_deviceid
= hdid
;
506 md
->properties
.hdid
= hdid
;
507 rio_local_set_device_id(md
->mport
, hdid
);
509 rmcd_debug(MPORT
, "Set host device Id to %d", hdid
);
515 * maint_comptag_set() - Set the host Component Tag
516 * @priv: driver private data
517 * @arg: Component Tag
519 static int maint_comptag_set(struct mport_cdev_priv
*priv
, void __user
*arg
)
521 struct mport_dev
*md
= priv
->md
;
524 if (copy_from_user(&comptag
, arg
, sizeof(comptag
)))
527 rio_local_write_config_32(md
->mport
, RIO_COMPONENT_TAG_CSR
, comptag
);
529 rmcd_debug(MPORT
, "Set host Component Tag to %d", comptag
);
534 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
536 struct mport_dma_req
{
537 struct kref refcount
;
538 struct list_head node
;
540 struct mport_cdev_priv
*priv
;
541 enum rio_transfer_sync sync
;
543 struct page
**page_list
;
544 unsigned int nr_pages
;
545 struct rio_mport_mapping
*map
;
546 struct dma_chan
*dmach
;
547 enum dma_data_direction dir
;
549 enum dma_status status
;
550 struct completion req_comp
;
553 static void mport_release_def_dma(struct kref
*dma_ref
)
555 struct mport_dev
*md
=
556 container_of(dma_ref
, struct mport_dev
, dma_ref
);
558 rmcd_debug(EXIT
, "DMA_%d", md
->dma_chan
->chan_id
);
559 rio_release_dma(md
->dma_chan
);
563 static void mport_release_dma(struct kref
*dma_ref
)
565 struct mport_cdev_priv
*priv
=
566 container_of(dma_ref
, struct mport_cdev_priv
, dma_ref
);
568 rmcd_debug(EXIT
, "DMA_%d", priv
->dmach
->chan_id
);
569 complete(&priv
->comp
);
572 static void dma_req_free(struct kref
*ref
)
574 struct mport_dma_req
*req
= container_of(ref
, struct mport_dma_req
,
576 struct mport_cdev_priv
*priv
= req
->priv
;
578 dma_unmap_sg(req
->dmach
->device
->dev
,
579 req
->sgt
.sgl
, req
->sgt
.nents
, req
->dir
);
580 sg_free_table(&req
->sgt
);
581 if (req
->page_list
) {
582 unpin_user_pages(req
->page_list
, req
->nr_pages
);
583 kfree(req
->page_list
);
587 mutex_lock(&req
->map
->md
->buf_mutex
);
588 kref_put(&req
->map
->ref
, mport_release_mapping
);
589 mutex_unlock(&req
->map
->md
->buf_mutex
);
592 kref_put(&priv
->dma_ref
, mport_release_dma
);
597 static void dma_xfer_callback(void *param
)
599 struct mport_dma_req
*req
= (struct mport_dma_req
*)param
;
600 struct mport_cdev_priv
*priv
= req
->priv
;
602 req
->status
= dma_async_is_tx_complete(priv
->dmach
, req
->cookie
,
604 complete(&req
->req_comp
);
605 kref_put(&req
->refcount
, dma_req_free
);
609 * prep_dma_xfer() - Configure and send request to DMAengine to prepare DMA
611 * Returns pointer to DMA transaction descriptor allocated by DMA driver on
612 * success or ERR_PTR (and/or NULL) if failed. Caller must check returned
613 * non-NULL pointer using IS_ERR macro.
615 static struct dma_async_tx_descriptor
616 *prep_dma_xfer(struct dma_chan
*chan
, struct rio_transfer_io
*transfer
,
617 struct sg_table
*sgt
, int nents
, enum dma_transfer_direction dir
,
618 enum dma_ctrl_flags flags
)
620 struct rio_dma_data tx_data
;
622 tx_data
.sg
= sgt
->sgl
;
623 tx_data
.sg_len
= nents
;
624 tx_data
.rio_addr_u
= 0;
625 tx_data
.rio_addr
= transfer
->rio_addr
;
626 if (dir
== DMA_MEM_TO_DEV
) {
627 switch (transfer
->method
) {
628 case RIO_EXCHANGE_NWRITE
:
629 tx_data
.wr_type
= RDW_ALL_NWRITE
;
631 case RIO_EXCHANGE_NWRITE_R_ALL
:
632 tx_data
.wr_type
= RDW_ALL_NWRITE_R
;
634 case RIO_EXCHANGE_NWRITE_R
:
635 tx_data
.wr_type
= RDW_LAST_NWRITE_R
;
637 case RIO_EXCHANGE_DEFAULT
:
638 tx_data
.wr_type
= RDW_DEFAULT
;
641 return ERR_PTR(-EINVAL
);
645 return rio_dma_prep_xfer(chan
, transfer
->rioid
, &tx_data
, dir
, flags
);
648 /* Request DMA channel associated with this mport device.
649 * Try to request DMA channel for every new process that opened given
650 * mport. If a new DMA channel is not available use default channel
651 * which is the first DMA channel opened on mport device.
653 static int get_dma_channel(struct mport_cdev_priv
*priv
)
655 mutex_lock(&priv
->dma_lock
);
657 priv
->dmach
= rio_request_mport_dma(priv
->md
->mport
);
659 /* Use default DMA channel if available */
660 if (priv
->md
->dma_chan
) {
661 priv
->dmach
= priv
->md
->dma_chan
;
662 kref_get(&priv
->md
->dma_ref
);
664 rmcd_error("Failed to get DMA channel");
665 mutex_unlock(&priv
->dma_lock
);
668 } else if (!priv
->md
->dma_chan
) {
669 /* Register default DMA channel if we do not have one */
670 priv
->md
->dma_chan
= priv
->dmach
;
671 kref_init(&priv
->md
->dma_ref
);
672 rmcd_debug(DMA
, "Register DMA_chan %d as default",
673 priv
->dmach
->chan_id
);
676 kref_init(&priv
->dma_ref
);
677 init_completion(&priv
->comp
);
680 kref_get(&priv
->dma_ref
);
681 mutex_unlock(&priv
->dma_lock
);
685 static void put_dma_channel(struct mport_cdev_priv
*priv
)
687 kref_put(&priv
->dma_ref
, mport_release_dma
);
691 * DMA transfer functions
693 static int do_dma_request(struct mport_dma_req
*req
,
694 struct rio_transfer_io
*xfer
,
695 enum rio_transfer_sync sync
, int nents
)
697 struct mport_cdev_priv
*priv
;
698 struct sg_table
*sgt
;
699 struct dma_chan
*chan
;
700 struct dma_async_tx_descriptor
*tx
;
702 unsigned long tmo
= msecs_to_jiffies(dma_timeout
);
703 enum dma_transfer_direction dir
;
711 dir
= (req
->dir
== DMA_FROM_DEVICE
) ? DMA_DEV_TO_MEM
: DMA_MEM_TO_DEV
;
713 rmcd_debug(DMA
, "%s(%d) uses %s for DMA_%s",
714 current
->comm
, task_pid_nr(current
),
715 dev_name(&chan
->dev
->device
),
716 (dir
== DMA_DEV_TO_MEM
)?"READ":"WRITE");
718 /* Initialize DMA transaction request */
719 tx
= prep_dma_xfer(chan
, xfer
, sgt
, nents
, dir
,
720 DMA_CTRL_ACK
| DMA_PREP_INTERRUPT
);
723 rmcd_debug(DMA
, "prep error for %s A:0x%llx L:0x%llx",
724 (dir
== DMA_DEV_TO_MEM
)?"READ":"WRITE",
725 xfer
->rio_addr
, xfer
->length
);
728 } else if (IS_ERR(tx
)) {
730 rmcd_debug(DMA
, "prep error %d for %s A:0x%llx L:0x%llx", ret
,
731 (dir
== DMA_DEV_TO_MEM
)?"READ":"WRITE",
732 xfer
->rio_addr
, xfer
->length
);
736 tx
->callback
= dma_xfer_callback
;
737 tx
->callback_param
= req
;
739 req
->status
= DMA_IN_PROGRESS
;
740 kref_get(&req
->refcount
);
742 cookie
= dmaengine_submit(tx
);
743 req
->cookie
= cookie
;
745 rmcd_debug(DMA
, "pid=%d DMA_%s tx_cookie = %d", task_pid_nr(current
),
746 (dir
== DMA_DEV_TO_MEM
)?"READ":"WRITE", cookie
);
748 if (dma_submit_error(cookie
)) {
749 rmcd_error("submit err=%d (addr:0x%llx len:0x%llx)",
750 cookie
, xfer
->rio_addr
, xfer
->length
);
751 kref_put(&req
->refcount
, dma_req_free
);
756 dma_async_issue_pending(chan
);
758 if (sync
== RIO_TRANSFER_ASYNC
) {
759 spin_lock(&priv
->req_lock
);
760 list_add_tail(&req
->node
, &priv
->async_list
);
761 spin_unlock(&priv
->req_lock
);
763 } else if (sync
== RIO_TRANSFER_FAF
)
766 wret
= wait_for_completion_interruptible_timeout(&req
->req_comp
, tmo
);
769 /* Timeout on wait occurred */
770 rmcd_error("%s(%d) timed out waiting for DMA_%s %d",
771 current
->comm
, task_pid_nr(current
),
772 (dir
== DMA_DEV_TO_MEM
)?"READ":"WRITE", cookie
);
774 } else if (wret
== -ERESTARTSYS
) {
775 /* Wait_for_completion was interrupted by a signal but DMA may
778 rmcd_error("%s(%d) wait for DMA_%s %d was interrupted",
779 current
->comm
, task_pid_nr(current
),
780 (dir
== DMA_DEV_TO_MEM
)?"READ":"WRITE", cookie
);
784 if (req
->status
!= DMA_COMPLETE
) {
785 /* DMA transaction completion was signaled with error */
786 rmcd_error("%s(%d) DMA_%s %d completed with status %d (ret=%d)",
787 current
->comm
, task_pid_nr(current
),
788 (dir
== DMA_DEV_TO_MEM
)?"READ":"WRITE",
789 cookie
, req
->status
, ret
);
798 * rio_dma_transfer() - Perform RapidIO DMA data transfer to/from
799 * the remote RapidIO device
800 * @filp: file pointer associated with the call
801 * @transfer_mode: DMA transfer mode
802 * @sync: synchronization mode
803 * @dir: DMA transfer direction (DMA_MEM_TO_DEV = write OR
804 * DMA_DEV_TO_MEM = read)
805 * @xfer: data transfer descriptor structure
808 rio_dma_transfer(struct file
*filp
, u32 transfer_mode
,
809 enum rio_transfer_sync sync
, enum dma_data_direction dir
,
810 struct rio_transfer_io
*xfer
)
812 struct mport_cdev_priv
*priv
= filp
->private_data
;
813 unsigned long nr_pages
= 0;
814 struct page
**page_list
= NULL
;
815 struct mport_dma_req
*req
;
816 struct mport_dev
*md
= priv
->md
;
817 struct dma_chan
*chan
;
821 if (xfer
->length
== 0)
823 req
= kzalloc(sizeof(*req
), GFP_KERNEL
);
827 ret
= get_dma_channel(priv
);
834 kref_init(&req
->refcount
);
835 init_completion(&req
->req_comp
);
843 * If parameter loc_addr != NULL, we are transferring data from/to
844 * data buffer allocated in user-space: lock in memory user-space
845 * buffer pages and build an SG table for DMA transfer request
847 * Otherwise (loc_addr == NULL) contiguous kernel-space buffer is
848 * used for DMA data transfers: build single entry SG table using
849 * offset within the internal buffer specified by handle parameter.
851 if (xfer
->loc_addr
) {
855 offset
= lower_32_bits(offset_in_page(xfer
->loc_addr
));
856 nr_pages
= PAGE_ALIGN(xfer
->length
+ offset
) >> PAGE_SHIFT
;
858 page_list
= kmalloc_array(nr_pages
,
859 sizeof(*page_list
), GFP_KERNEL
);
860 if (page_list
== NULL
) {
865 pinned
= pin_user_pages_fast(
866 (unsigned long)xfer
->loc_addr
& PAGE_MASK
,
868 dir
== DMA_FROM_DEVICE
? FOLL_WRITE
: 0,
871 if (pinned
!= nr_pages
) {
873 rmcd_error("pin_user_pages_fast err=%ld",
877 rmcd_error("pinned %ld out of %ld pages",
880 * Set nr_pages up to mean "how many pages to unpin, in
889 ret
= sg_alloc_table_from_pages(&req
->sgt
, page_list
, nr_pages
,
890 offset
, xfer
->length
, GFP_KERNEL
);
892 rmcd_error("sg_alloc_table failed with err=%d", ret
);
896 req
->page_list
= page_list
;
897 req
->nr_pages
= nr_pages
;
900 struct rio_mport_mapping
*map
;
902 baddr
= (dma_addr_t
)xfer
->handle
;
904 mutex_lock(&md
->buf_mutex
);
905 list_for_each_entry(map
, &md
->mappings
, node
) {
906 if (baddr
>= map
->phys_addr
&&
907 baddr
< (map
->phys_addr
+ map
->size
)) {
913 mutex_unlock(&md
->buf_mutex
);
915 if (req
->map
== NULL
) {
920 if (xfer
->length
+ xfer
->offset
> req
->map
->size
) {
925 ret
= sg_alloc_table(&req
->sgt
, 1, GFP_KERNEL
);
927 rmcd_error("sg_alloc_table failed for internal buf");
931 sg_set_buf(req
->sgt
.sgl
,
932 req
->map
->virt_addr
+ (baddr
- req
->map
->phys_addr
) +
933 xfer
->offset
, xfer
->length
);
936 nents
= dma_map_sg(chan
->device
->dev
,
937 req
->sgt
.sgl
, req
->sgt
.nents
, dir
);
939 rmcd_error("Failed to map SG list");
944 ret
= do_dma_request(req
, xfer
, sync
, nents
);
947 if (sync
== RIO_TRANSFER_ASYNC
)
948 return ret
; /* return ASYNC cookie */
950 rmcd_debug(DMA
, "do_dma_request failed with err=%d", ret
);
954 if (!req
->page_list
) {
955 unpin_user_pages(page_list
, nr_pages
);
959 kref_put(&req
->refcount
, dma_req_free
);
963 static int rio_mport_transfer_ioctl(struct file
*filp
, void __user
*arg
)
965 struct mport_cdev_priv
*priv
= filp
->private_data
;
966 struct rio_transaction transaction
;
967 struct rio_transfer_io
*transfer
;
968 enum dma_data_direction dir
;
972 if (unlikely(copy_from_user(&transaction
, arg
, sizeof(transaction
))))
975 if (transaction
.count
!= 1) /* only single transfer for now */
978 if ((transaction
.transfer_mode
&
979 priv
->md
->properties
.transfer_mode
) == 0)
982 size
= array_size(sizeof(*transfer
), transaction
.count
);
983 transfer
= vmalloc(size
);
987 if (unlikely(copy_from_user(transfer
,
988 (void __user
*)(uintptr_t)transaction
.block
,
994 dir
= (transaction
.dir
== RIO_TRANSFER_DIR_READ
) ?
995 DMA_FROM_DEVICE
: DMA_TO_DEVICE
;
996 for (i
= 0; i
< transaction
.count
&& ret
== 0; i
++)
997 ret
= rio_dma_transfer(filp
, transaction
.transfer_mode
,
998 transaction
.sync
, dir
, &transfer
[i
]);
1000 if (unlikely(copy_to_user((void __user
*)(uintptr_t)transaction
.block
,
1010 static int rio_mport_wait_for_async_dma(struct file
*filp
, void __user
*arg
)
1012 struct mport_cdev_priv
*priv
;
1013 struct rio_async_tx_wait w_param
;
1014 struct mport_dma_req
*req
;
1015 dma_cookie_t cookie
;
1021 priv
= (struct mport_cdev_priv
*)filp
->private_data
;
1023 if (unlikely(copy_from_user(&w_param
, arg
, sizeof(w_param
))))
1026 cookie
= w_param
.token
;
1027 if (w_param
.timeout
)
1028 tmo
= msecs_to_jiffies(w_param
.timeout
);
1029 else /* Use default DMA timeout */
1030 tmo
= msecs_to_jiffies(dma_timeout
);
1032 spin_lock(&priv
->req_lock
);
1033 list_for_each_entry(req
, &priv
->async_list
, node
) {
1034 if (req
->cookie
== cookie
) {
1035 list_del(&req
->node
);
1040 spin_unlock(&priv
->req_lock
);
1045 wret
= wait_for_completion_interruptible_timeout(&req
->req_comp
, tmo
);
1048 /* Timeout on wait occurred */
1049 rmcd_error("%s(%d) timed out waiting for ASYNC DMA_%s",
1050 current
->comm
, task_pid_nr(current
),
1051 (req
->dir
== DMA_FROM_DEVICE
)?"READ":"WRITE");
1054 } else if (wret
== -ERESTARTSYS
) {
1055 /* Wait_for_completion was interrupted by a signal but DMA may
1056 * be still in progress
1058 rmcd_error("%s(%d) wait for ASYNC DMA_%s was interrupted",
1059 current
->comm
, task_pid_nr(current
),
1060 (req
->dir
== DMA_FROM_DEVICE
)?"READ":"WRITE");
1065 if (req
->status
!= DMA_COMPLETE
) {
1066 /* DMA transaction completion signaled with transfer error */
1067 rmcd_error("%s(%d) ASYNC DMA_%s completion with status %d",
1068 current
->comm
, task_pid_nr(current
),
1069 (req
->dir
== DMA_FROM_DEVICE
)?"READ":"WRITE",
1075 if (req
->status
!= DMA_IN_PROGRESS
&& req
->status
!= DMA_PAUSED
)
1076 kref_put(&req
->refcount
, dma_req_free
);
1081 /* Return request back into async queue */
1082 spin_lock(&priv
->req_lock
);
1083 list_add_tail(&req
->node
, &priv
->async_list
);
1084 spin_unlock(&priv
->req_lock
);
1088 static int rio_mport_create_dma_mapping(struct mport_dev
*md
, struct file
*filp
,
1089 u64 size
, struct rio_mport_mapping
**mapping
)
1091 struct rio_mport_mapping
*map
;
1093 map
= kzalloc(sizeof(*map
), GFP_KERNEL
);
1097 map
->virt_addr
= dma_alloc_coherent(md
->mport
->dev
.parent
, size
,
1098 &map
->phys_addr
, GFP_KERNEL
);
1099 if (map
->virt_addr
== NULL
) {
1108 kref_init(&map
->ref
);
1109 mutex_lock(&md
->buf_mutex
);
1110 list_add_tail(&map
->node
, &md
->mappings
);
1111 mutex_unlock(&md
->buf_mutex
);
1117 static int rio_mport_alloc_dma(struct file
*filp
, void __user
*arg
)
1119 struct mport_cdev_priv
*priv
= filp
->private_data
;
1120 struct mport_dev
*md
= priv
->md
;
1121 struct rio_dma_mem map
;
1122 struct rio_mport_mapping
*mapping
= NULL
;
1125 if (unlikely(copy_from_user(&map
, arg
, sizeof(map
))))
1128 ret
= rio_mport_create_dma_mapping(md
, filp
, map
.length
, &mapping
);
1132 map
.dma_handle
= mapping
->phys_addr
;
1134 if (unlikely(copy_to_user(arg
, &map
, sizeof(map
)))) {
1135 mutex_lock(&md
->buf_mutex
);
1136 kref_put(&mapping
->ref
, mport_release_mapping
);
1137 mutex_unlock(&md
->buf_mutex
);
1144 static int rio_mport_free_dma(struct file
*filp
, void __user
*arg
)
1146 struct mport_cdev_priv
*priv
= filp
->private_data
;
1147 struct mport_dev
*md
= priv
->md
;
1150 struct rio_mport_mapping
*map
, *_map
;
1152 if (copy_from_user(&handle
, arg
, sizeof(handle
)))
1154 rmcd_debug(EXIT
, "filp=%p", filp
);
1156 mutex_lock(&md
->buf_mutex
);
1157 list_for_each_entry_safe(map
, _map
, &md
->mappings
, node
) {
1158 if (map
->dir
== MAP_DMA
&& map
->phys_addr
== handle
&&
1159 map
->filp
== filp
) {
1160 kref_put(&map
->ref
, mport_release_mapping
);
1165 mutex_unlock(&md
->buf_mutex
);
1167 if (ret
== -EFAULT
) {
1168 rmcd_debug(DMA
, "ERR no matching mapping");
1175 static int rio_mport_transfer_ioctl(struct file
*filp
, void *arg
)
1180 static int rio_mport_wait_for_async_dma(struct file
*filp
, void __user
*arg
)
1185 static int rio_mport_alloc_dma(struct file
*filp
, void __user
*arg
)
1190 static int rio_mport_free_dma(struct file
*filp
, void __user
*arg
)
1194 #endif /* CONFIG_RAPIDIO_DMA_ENGINE */
1197 * Inbound/outbound memory mapping functions
1201 rio_mport_create_inbound_mapping(struct mport_dev
*md
, struct file
*filp
,
1202 u64 raddr
, u64 size
,
1203 struct rio_mport_mapping
**mapping
)
1205 struct rio_mport
*mport
= md
->mport
;
1206 struct rio_mport_mapping
*map
;
1209 /* rio_map_inb_region() accepts u32 size */
1210 if (size
> 0xffffffff)
1213 map
= kzalloc(sizeof(*map
), GFP_KERNEL
);
1217 map
->virt_addr
= dma_alloc_coherent(mport
->dev
.parent
, size
,
1218 &map
->phys_addr
, GFP_KERNEL
);
1219 if (map
->virt_addr
== NULL
) {
1224 if (raddr
== RIO_MAP_ANY_ADDR
)
1225 raddr
= map
->phys_addr
;
1226 ret
= rio_map_inb_region(mport
, map
->phys_addr
, raddr
, (u32
)size
, 0);
1230 map
->dir
= MAP_INBOUND
;
1231 map
->rio_addr
= raddr
;
1235 kref_init(&map
->ref
);
1236 mutex_lock(&md
->buf_mutex
);
1237 list_add_tail(&map
->node
, &md
->mappings
);
1238 mutex_unlock(&md
->buf_mutex
);
1243 dma_free_coherent(mport
->dev
.parent
, size
,
1244 map
->virt_addr
, map
->phys_addr
);
1251 rio_mport_get_inbound_mapping(struct mport_dev
*md
, struct file
*filp
,
1252 u64 raddr
, u64 size
,
1253 struct rio_mport_mapping
**mapping
)
1255 struct rio_mport_mapping
*map
;
1258 if (raddr
== RIO_MAP_ANY_ADDR
)
1261 mutex_lock(&md
->buf_mutex
);
1262 list_for_each_entry(map
, &md
->mappings
, node
) {
1263 if (map
->dir
!= MAP_INBOUND
)
1265 if (raddr
== map
->rio_addr
&& size
== map
->size
) {
1266 /* allow exact match only */
1270 } else if (raddr
< (map
->rio_addr
+ map
->size
- 1) &&
1271 (raddr
+ size
) > map
->rio_addr
) {
1276 mutex_unlock(&md
->buf_mutex
);
1281 /* not found, create new */
1282 return rio_mport_create_inbound_mapping(md
, filp
, raddr
, size
, mapping
);
1285 static int rio_mport_map_inbound(struct file
*filp
, void __user
*arg
)
1287 struct mport_cdev_priv
*priv
= filp
->private_data
;
1288 struct mport_dev
*md
= priv
->md
;
1289 struct rio_mmap map
;
1290 struct rio_mport_mapping
*mapping
= NULL
;
1293 if (!md
->mport
->ops
->map_inb
)
1294 return -EPROTONOSUPPORT
;
1295 if (unlikely(copy_from_user(&map
, arg
, sizeof(map
))))
1298 rmcd_debug(IBW
, "%s filp=%p", dev_name(&priv
->md
->dev
), filp
);
1300 ret
= rio_mport_get_inbound_mapping(md
, filp
, map
.rio_addr
,
1301 map
.length
, &mapping
);
1305 map
.handle
= mapping
->phys_addr
;
1306 map
.rio_addr
= mapping
->rio_addr
;
1308 if (unlikely(copy_to_user(arg
, &map
, sizeof(map
)))) {
1309 /* Delete mapping if it was created by this request */
1310 if (ret
== 0 && mapping
->filp
== filp
) {
1311 mutex_lock(&md
->buf_mutex
);
1312 kref_put(&mapping
->ref
, mport_release_mapping
);
1313 mutex_unlock(&md
->buf_mutex
);
1322 * rio_mport_inbound_free() - unmap from RapidIO address space and free
1323 * previously allocated inbound DMA coherent buffer
1324 * @priv: driver private data
1325 * @arg: buffer handle returned by allocation routine
1327 static int rio_mport_inbound_free(struct file
*filp
, void __user
*arg
)
1329 struct mport_cdev_priv
*priv
= filp
->private_data
;
1330 struct mport_dev
*md
= priv
->md
;
1332 struct rio_mport_mapping
*map
, *_map
;
1334 rmcd_debug(IBW
, "%s filp=%p", dev_name(&priv
->md
->dev
), filp
);
1336 if (!md
->mport
->ops
->unmap_inb
)
1337 return -EPROTONOSUPPORT
;
1339 if (copy_from_user(&handle
, arg
, sizeof(handle
)))
1342 mutex_lock(&md
->buf_mutex
);
1343 list_for_each_entry_safe(map
, _map
, &md
->mappings
, node
) {
1344 if (map
->dir
== MAP_INBOUND
&& map
->phys_addr
== handle
) {
1345 if (map
->filp
== filp
) {
1347 kref_put(&map
->ref
, mport_release_mapping
);
1352 mutex_unlock(&md
->buf_mutex
);
1358 * maint_port_idx_get() - Get the port index of the mport instance
1359 * @priv: driver private data
1362 static int maint_port_idx_get(struct mport_cdev_priv
*priv
, void __user
*arg
)
1364 struct mport_dev
*md
= priv
->md
;
1365 u32 port_idx
= md
->mport
->index
;
1367 rmcd_debug(MPORT
, "port_index=%d", port_idx
);
1369 if (copy_to_user(arg
, &port_idx
, sizeof(port_idx
)))
1375 static int rio_mport_add_event(struct mport_cdev_priv
*priv
,
1376 struct rio_event
*event
)
1380 if (!(priv
->event_mask
& event
->header
))
1383 spin_lock(&priv
->fifo_lock
);
1384 overflow
= kfifo_avail(&priv
->event_fifo
) < sizeof(*event
)
1385 || kfifo_in(&priv
->event_fifo
, (unsigned char *)event
,
1386 sizeof(*event
)) != sizeof(*event
);
1387 spin_unlock(&priv
->fifo_lock
);
1389 wake_up_interruptible(&priv
->event_rx_wait
);
1392 dev_warn(&priv
->md
->dev
, DRV_NAME
": event fifo overflow\n");
1399 static void rio_mport_doorbell_handler(struct rio_mport
*mport
, void *dev_id
,
1400 u16 src
, u16 dst
, u16 info
)
1402 struct mport_dev
*data
= dev_id
;
1403 struct mport_cdev_priv
*priv
;
1404 struct rio_mport_db_filter
*db_filter
;
1405 struct rio_event event
;
1408 event
.header
= RIO_DOORBELL
;
1409 event
.u
.doorbell
.rioid
= src
;
1410 event
.u
.doorbell
.payload
= info
;
1413 spin_lock(&data
->db_lock
);
1414 list_for_each_entry(db_filter
, &data
->doorbells
, data_node
) {
1415 if (((db_filter
->filter
.rioid
== RIO_INVALID_DESTID
||
1416 db_filter
->filter
.rioid
== src
)) &&
1417 info
>= db_filter
->filter
.low
&&
1418 info
<= db_filter
->filter
.high
) {
1419 priv
= db_filter
->priv
;
1420 rio_mport_add_event(priv
, &event
);
1424 spin_unlock(&data
->db_lock
);
1427 dev_warn(&data
->dev
,
1428 "%s: spurious DB received from 0x%x, info=0x%04x\n",
1429 __func__
, src
, info
);
1432 static int rio_mport_add_db_filter(struct mport_cdev_priv
*priv
,
1435 struct mport_dev
*md
= priv
->md
;
1436 struct rio_mport_db_filter
*db_filter
;
1437 struct rio_doorbell_filter filter
;
1438 unsigned long flags
;
1441 if (copy_from_user(&filter
, arg
, sizeof(filter
)))
1444 if (filter
.low
> filter
.high
)
1447 ret
= rio_request_inb_dbell(md
->mport
, md
, filter
.low
, filter
.high
,
1448 rio_mport_doorbell_handler
);
1450 rmcd_error("%s failed to register IBDB, err=%d",
1451 dev_name(&md
->dev
), ret
);
1455 db_filter
= kzalloc(sizeof(*db_filter
), GFP_KERNEL
);
1456 if (db_filter
== NULL
) {
1457 rio_release_inb_dbell(md
->mport
, filter
.low
, filter
.high
);
1461 db_filter
->filter
= filter
;
1462 db_filter
->priv
= priv
;
1463 spin_lock_irqsave(&md
->db_lock
, flags
);
1464 list_add_tail(&db_filter
->priv_node
, &priv
->db_filters
);
1465 list_add_tail(&db_filter
->data_node
, &md
->doorbells
);
1466 spin_unlock_irqrestore(&md
->db_lock
, flags
);
1471 static void rio_mport_delete_db_filter(struct rio_mport_db_filter
*db_filter
)
1473 list_del(&db_filter
->data_node
);
1474 list_del(&db_filter
->priv_node
);
1478 static int rio_mport_remove_db_filter(struct mport_cdev_priv
*priv
,
1481 struct rio_mport_db_filter
*db_filter
;
1482 struct rio_doorbell_filter filter
;
1483 unsigned long flags
;
1486 if (copy_from_user(&filter
, arg
, sizeof(filter
)))
1489 if (filter
.low
> filter
.high
)
1492 spin_lock_irqsave(&priv
->md
->db_lock
, flags
);
1493 list_for_each_entry(db_filter
, &priv
->db_filters
, priv_node
) {
1494 if (db_filter
->filter
.rioid
== filter
.rioid
&&
1495 db_filter
->filter
.low
== filter
.low
&&
1496 db_filter
->filter
.high
== filter
.high
) {
1497 rio_mport_delete_db_filter(db_filter
);
1502 spin_unlock_irqrestore(&priv
->md
->db_lock
, flags
);
1505 rio_release_inb_dbell(priv
->md
->mport
, filter
.low
, filter
.high
);
1510 static int rio_mport_match_pw(union rio_pw_msg
*msg
,
1511 struct rio_pw_filter
*filter
)
1513 if ((msg
->em
.comptag
& filter
->mask
) < filter
->low
||
1514 (msg
->em
.comptag
& filter
->mask
) > filter
->high
)
1519 static int rio_mport_pw_handler(struct rio_mport
*mport
, void *context
,
1520 union rio_pw_msg
*msg
, int step
)
1522 struct mport_dev
*md
= context
;
1523 struct mport_cdev_priv
*priv
;
1524 struct rio_mport_pw_filter
*pw_filter
;
1525 struct rio_event event
;
1528 event
.header
= RIO_PORTWRITE
;
1529 memcpy(event
.u
.portwrite
.payload
, msg
->raw
, RIO_PW_MSG_SIZE
);
1532 spin_lock(&md
->pw_lock
);
1533 list_for_each_entry(pw_filter
, &md
->portwrites
, md_node
) {
1534 if (rio_mport_match_pw(msg
, &pw_filter
->filter
)) {
1535 priv
= pw_filter
->priv
;
1536 rio_mport_add_event(priv
, &event
);
1540 spin_unlock(&md
->pw_lock
);
1543 printk_ratelimited(KERN_WARNING DRV_NAME
1544 ": mport%d received spurious PW from 0x%08x\n",
1545 mport
->id
, msg
->em
.comptag
);
1551 static int rio_mport_add_pw_filter(struct mport_cdev_priv
*priv
,
1554 struct mport_dev
*md
= priv
->md
;
1555 struct rio_mport_pw_filter
*pw_filter
;
1556 struct rio_pw_filter filter
;
1557 unsigned long flags
;
1560 if (copy_from_user(&filter
, arg
, sizeof(filter
)))
1563 pw_filter
= kzalloc(sizeof(*pw_filter
), GFP_KERNEL
);
1564 if (pw_filter
== NULL
)
1567 pw_filter
->filter
= filter
;
1568 pw_filter
->priv
= priv
;
1569 spin_lock_irqsave(&md
->pw_lock
, flags
);
1570 if (list_empty(&md
->portwrites
))
1572 list_add_tail(&pw_filter
->priv_node
, &priv
->pw_filters
);
1573 list_add_tail(&pw_filter
->md_node
, &md
->portwrites
);
1574 spin_unlock_irqrestore(&md
->pw_lock
, flags
);
1579 ret
= rio_add_mport_pw_handler(md
->mport
, md
,
1580 rio_mport_pw_handler
);
1583 "%s: failed to add IB_PW handler, err=%d\n",
1587 rio_pw_enable(md
->mport
, 1);
1593 static void rio_mport_delete_pw_filter(struct rio_mport_pw_filter
*pw_filter
)
1595 list_del(&pw_filter
->md_node
);
1596 list_del(&pw_filter
->priv_node
);
1600 static int rio_mport_match_pw_filter(struct rio_pw_filter
*a
,
1601 struct rio_pw_filter
*b
)
1603 if ((a
->mask
== b
->mask
) && (a
->low
== b
->low
) && (a
->high
== b
->high
))
1608 static int rio_mport_remove_pw_filter(struct mport_cdev_priv
*priv
,
1611 struct mport_dev
*md
= priv
->md
;
1612 struct rio_mport_pw_filter
*pw_filter
;
1613 struct rio_pw_filter filter
;
1614 unsigned long flags
;
1618 if (copy_from_user(&filter
, arg
, sizeof(filter
)))
1621 spin_lock_irqsave(&md
->pw_lock
, flags
);
1622 list_for_each_entry(pw_filter
, &priv
->pw_filters
, priv_node
) {
1623 if (rio_mport_match_pw_filter(&pw_filter
->filter
, &filter
)) {
1624 rio_mport_delete_pw_filter(pw_filter
);
1630 if (list_empty(&md
->portwrites
))
1632 spin_unlock_irqrestore(&md
->pw_lock
, flags
);
1635 rio_del_mport_pw_handler(md
->mport
, priv
->md
,
1636 rio_mport_pw_handler
);
1637 rio_pw_enable(md
->mport
, 0);
1644 * rio_release_dev - release routine for kernel RIO device object
1645 * @dev: kernel device object associated with a RIO device structure
1647 * Frees a RIO device struct associated a RIO device struct.
1648 * The RIO device struct is freed.
1650 static void rio_release_dev(struct device
*dev
)
1652 struct rio_dev
*rdev
;
1654 rdev
= to_rio_dev(dev
);
1655 pr_info(DRV_PREFIX
"%s: %s\n", __func__
, rio_name(rdev
));
1660 static void rio_release_net(struct device
*dev
)
1662 struct rio_net
*net
;
1664 net
= to_rio_net(dev
);
1665 rmcd_debug(RDEV
, "net_%d", net
->id
);
1671 * rio_mport_add_riodev - creates a kernel RIO device object
1673 * Allocates a RIO device data structure and initializes required fields based
1674 * on device's configuration space contents.
1675 * If the device has switch capabilities, then a switch specific portion is
1676 * allocated and configured.
1678 static int rio_mport_add_riodev(struct mport_cdev_priv
*priv
,
1681 struct mport_dev
*md
= priv
->md
;
1682 struct rio_rdev_info dev_info
;
1683 struct rio_dev
*rdev
;
1684 struct rio_switch
*rswitch
= NULL
;
1685 struct rio_mport
*mport
;
1694 if (copy_from_user(&dev_info
, arg
, sizeof(dev_info
)))
1696 dev_info
.name
[sizeof(dev_info
.name
) - 1] = '\0';
1698 rmcd_debug(RDEV
, "name:%s ct:0x%x did:0x%x hc:0x%x", dev_info
.name
,
1699 dev_info
.comptag
, dev_info
.destid
, dev_info
.hopcount
);
1701 dev
= bus_find_device_by_name(&rio_bus_type
, NULL
, dev_info
.name
);
1703 rmcd_debug(RDEV
, "device %s already exists", dev_info
.name
);
1708 size
= sizeof(*rdev
);
1710 destid
= dev_info
.destid
;
1711 hopcount
= dev_info
.hopcount
;
1713 if (rio_mport_read_config_32(mport
, destid
, hopcount
,
1714 RIO_PEF_CAR
, &rval
))
1717 if (rval
& RIO_PEF_SWITCH
) {
1718 rio_mport_read_config_32(mport
, destid
, hopcount
,
1719 RIO_SWP_INFO_CAR
, &swpinfo
);
1720 size
+= struct_size(rswitch
, nextdev
, RIO_GET_TOTAL_PORTS(swpinfo
));
1723 rdev
= kzalloc(size
, GFP_KERNEL
);
1727 if (mport
->net
== NULL
) {
1728 struct rio_net
*net
;
1730 net
= rio_alloc_net(mport
);
1733 rmcd_debug(RDEV
, "failed to allocate net object");
1737 net
->id
= mport
->id
;
1739 dev_set_name(&net
->dev
, "rnet_%d", net
->id
);
1740 net
->dev
.parent
= &mport
->dev
;
1741 net
->dev
.release
= rio_release_net
;
1742 err
= rio_add_net(net
);
1744 rmcd_debug(RDEV
, "failed to register net, err=%d", err
);
1750 rdev
->net
= mport
->net
;
1752 rdev
->swpinfo
= swpinfo
;
1753 rio_mport_read_config_32(mport
, destid
, hopcount
,
1754 RIO_DEV_ID_CAR
, &rval
);
1755 rdev
->did
= rval
>> 16;
1756 rdev
->vid
= rval
& 0xffff;
1757 rio_mport_read_config_32(mport
, destid
, hopcount
, RIO_DEV_INFO_CAR
,
1759 rio_mport_read_config_32(mport
, destid
, hopcount
, RIO_ASM_ID_CAR
,
1761 rdev
->asm_did
= rval
>> 16;
1762 rdev
->asm_vid
= rval
& 0xffff;
1763 rio_mport_read_config_32(mport
, destid
, hopcount
, RIO_ASM_INFO_CAR
,
1765 rdev
->asm_rev
= rval
>> 16;
1767 if (rdev
->pef
& RIO_PEF_EXT_FEATURES
) {
1768 rdev
->efptr
= rval
& 0xffff;
1769 rdev
->phys_efptr
= rio_mport_get_physefb(mport
, 0, destid
,
1770 hopcount
, &rdev
->phys_rmap
);
1772 rdev
->em_efptr
= rio_mport_get_feature(mport
, 0, destid
,
1773 hopcount
, RIO_EFB_ERR_MGMNT
);
1776 rio_mport_read_config_32(mport
, destid
, hopcount
, RIO_SRC_OPS_CAR
,
1778 rio_mport_read_config_32(mport
, destid
, hopcount
, RIO_DST_OPS_CAR
,
1781 rdev
->comp_tag
= dev_info
.comptag
;
1782 rdev
->destid
= destid
;
1783 /* hopcount is stored as specified by a caller, regardles of EP or SW */
1784 rdev
->hopcount
= hopcount
;
1786 if (rdev
->pef
& RIO_PEF_SWITCH
) {
1787 rswitch
= rdev
->rswitch
;
1788 rswitch
->route_table
= NULL
;
1791 if (strlen(dev_info
.name
))
1792 dev_set_name(&rdev
->dev
, "%s", dev_info
.name
);
1793 else if (rdev
->pef
& RIO_PEF_SWITCH
)
1794 dev_set_name(&rdev
->dev
, "%02x:s:%04x", mport
->id
,
1795 rdev
->comp_tag
& RIO_CTAG_UDEVID
);
1797 dev_set_name(&rdev
->dev
, "%02x:e:%04x", mport
->id
,
1798 rdev
->comp_tag
& RIO_CTAG_UDEVID
);
1800 INIT_LIST_HEAD(&rdev
->net_list
);
1801 rdev
->dev
.parent
= &mport
->net
->dev
;
1802 rio_attach_device(rdev
);
1803 rdev
->dev
.release
= rio_release_dev
;
1805 if (rdev
->dst_ops
& RIO_DST_OPS_DOORBELL
)
1806 rio_init_dbell_res(&rdev
->riores
[RIO_DOORBELL_RESOURCE
],
1808 err
= rio_add_device(rdev
);
1810 put_device(&rdev
->dev
);
1822 static int rio_mport_del_riodev(struct mport_cdev_priv
*priv
, void __user
*arg
)
1824 struct rio_rdev_info dev_info
;
1825 struct rio_dev
*rdev
= NULL
;
1827 struct rio_mport
*mport
;
1828 struct rio_net
*net
;
1830 if (copy_from_user(&dev_info
, arg
, sizeof(dev_info
)))
1832 dev_info
.name
[sizeof(dev_info
.name
) - 1] = '\0';
1834 mport
= priv
->md
->mport
;
1836 /* If device name is specified, removal by name has priority */
1837 if (strlen(dev_info
.name
)) {
1838 dev
= bus_find_device_by_name(&rio_bus_type
, NULL
,
1841 rdev
= to_rio_dev(dev
);
1844 rdev
= rio_get_comptag(dev_info
.comptag
, rdev
);
1845 if (rdev
&& rdev
->dev
.parent
== &mport
->net
->dev
&&
1846 rdev
->destid
== dev_info
.destid
&&
1847 rdev
->hopcount
== dev_info
.hopcount
)
1854 "device name:%s ct:0x%x did:0x%x hc:0x%x not found",
1855 dev_info
.name
, dev_info
.comptag
, dev_info
.destid
,
1862 rio_del_device(rdev
, RIO_DEVICE_SHUTDOWN
);
1864 if (list_empty(&net
->devices
)) {
1873 * Mport cdev management
1877 * mport_cdev_open() - Open character device (mport)
1879 static int mport_cdev_open(struct inode
*inode
, struct file
*filp
)
1882 int minor
= iminor(inode
);
1883 struct mport_dev
*chdev
;
1884 struct mport_cdev_priv
*priv
;
1886 /* Test for valid device */
1887 if (minor
>= RIO_MAX_MPORTS
) {
1888 rmcd_error("Invalid minor device number");
1892 chdev
= container_of(inode
->i_cdev
, struct mport_dev
, cdev
);
1894 rmcd_debug(INIT
, "%s filp=%p", dev_name(&chdev
->dev
), filp
);
1896 if (atomic_read(&chdev
->active
) == 0)
1899 get_device(&chdev
->dev
);
1901 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
1903 put_device(&chdev
->dev
);
1909 INIT_LIST_HEAD(&priv
->db_filters
);
1910 INIT_LIST_HEAD(&priv
->pw_filters
);
1911 spin_lock_init(&priv
->fifo_lock
);
1912 init_waitqueue_head(&priv
->event_rx_wait
);
1913 ret
= kfifo_alloc(&priv
->event_fifo
,
1914 sizeof(struct rio_event
) * MPORT_EVENT_DEPTH
,
1917 put_device(&chdev
->dev
);
1918 dev_err(&chdev
->dev
, DRV_NAME
": kfifo_alloc failed\n");
1923 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
1924 INIT_LIST_HEAD(&priv
->async_list
);
1925 spin_lock_init(&priv
->req_lock
);
1926 mutex_init(&priv
->dma_lock
);
1928 mutex_lock(&chdev
->file_mutex
);
1929 list_add_tail(&priv
->list
, &chdev
->file_list
);
1930 mutex_unlock(&chdev
->file_mutex
);
1932 filp
->private_data
= priv
;
1940 static int mport_cdev_fasync(int fd
, struct file
*filp
, int mode
)
1942 struct mport_cdev_priv
*priv
= filp
->private_data
;
1944 return fasync_helper(fd
, filp
, mode
, &priv
->async_queue
);
1947 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
1948 static void mport_cdev_release_dma(struct file
*filp
)
1950 struct mport_cdev_priv
*priv
= filp
->private_data
;
1951 struct mport_dev
*md
;
1952 struct mport_dma_req
*req
, *req_next
;
1953 unsigned long tmo
= msecs_to_jiffies(dma_timeout
);
1957 rmcd_debug(EXIT
, "from filp=%p %s(%d)",
1958 filp
, current
->comm
, task_pid_nr(current
));
1961 rmcd_debug(EXIT
, "No DMA channel for filp=%p", filp
);
1967 spin_lock(&priv
->req_lock
);
1968 if (!list_empty(&priv
->async_list
)) {
1969 rmcd_debug(EXIT
, "async list not empty filp=%p %s(%d)",
1970 filp
, current
->comm
, task_pid_nr(current
));
1971 list_splice_init(&priv
->async_list
, &list
);
1973 spin_unlock(&priv
->req_lock
);
1975 if (!list_empty(&list
)) {
1976 rmcd_debug(EXIT
, "temp list not empty");
1977 list_for_each_entry_safe(req
, req_next
, &list
, node
) {
1978 rmcd_debug(EXIT
, "free req->filp=%p cookie=%d compl=%s",
1979 req
->filp
, req
->cookie
,
1980 completion_done(&req
->req_comp
)?"yes":"no");
1981 list_del(&req
->node
);
1982 kref_put(&req
->refcount
, dma_req_free
);
1986 put_dma_channel(priv
);
1987 wret
= wait_for_completion_interruptible_timeout(&priv
->comp
, tmo
);
1990 rmcd_error("%s(%d) failed waiting for DMA release err=%ld",
1991 current
->comm
, task_pid_nr(current
), wret
);
1994 if (priv
->dmach
!= priv
->md
->dma_chan
) {
1995 rmcd_debug(EXIT
, "Release DMA channel for filp=%p %s(%d)",
1996 filp
, current
->comm
, task_pid_nr(current
));
1997 rio_release_dma(priv
->dmach
);
1999 rmcd_debug(EXIT
, "Adjust default DMA channel refcount");
2000 kref_put(&md
->dma_ref
, mport_release_def_dma
);
2006 #define mport_cdev_release_dma(priv) do {} while (0)
2010 * mport_cdev_release() - Release character device
2012 static int mport_cdev_release(struct inode
*inode
, struct file
*filp
)
2014 struct mport_cdev_priv
*priv
= filp
->private_data
;
2015 struct mport_dev
*chdev
;
2016 struct rio_mport_pw_filter
*pw_filter
, *pw_filter_next
;
2017 struct rio_mport_db_filter
*db_filter
, *db_filter_next
;
2018 struct rio_mport_mapping
*map
, *_map
;
2019 unsigned long flags
;
2021 rmcd_debug(EXIT
, "%s filp=%p", dev_name(&priv
->md
->dev
), filp
);
2024 mport_cdev_release_dma(filp
);
2026 priv
->event_mask
= 0;
2028 spin_lock_irqsave(&chdev
->pw_lock
, flags
);
2029 if (!list_empty(&priv
->pw_filters
)) {
2030 list_for_each_entry_safe(pw_filter
, pw_filter_next
,
2031 &priv
->pw_filters
, priv_node
)
2032 rio_mport_delete_pw_filter(pw_filter
);
2034 spin_unlock_irqrestore(&chdev
->pw_lock
, flags
);
2036 spin_lock_irqsave(&chdev
->db_lock
, flags
);
2037 list_for_each_entry_safe(db_filter
, db_filter_next
,
2038 &priv
->db_filters
, priv_node
) {
2039 rio_mport_delete_db_filter(db_filter
);
2041 spin_unlock_irqrestore(&chdev
->db_lock
, flags
);
2043 kfifo_free(&priv
->event_fifo
);
2045 mutex_lock(&chdev
->buf_mutex
);
2046 list_for_each_entry_safe(map
, _map
, &chdev
->mappings
, node
) {
2047 if (map
->filp
== filp
) {
2048 rmcd_debug(EXIT
, "release mapping %p filp=%p",
2049 map
->virt_addr
, filp
);
2050 kref_put(&map
->ref
, mport_release_mapping
);
2053 mutex_unlock(&chdev
->buf_mutex
);
2055 mport_cdev_fasync(-1, filp
, 0);
2056 filp
->private_data
= NULL
;
2057 mutex_lock(&chdev
->file_mutex
);
2058 list_del(&priv
->list
);
2059 mutex_unlock(&chdev
->file_mutex
);
2060 put_device(&chdev
->dev
);
2066 * mport_cdev_ioctl() - IOCTLs for character device
2068 static long mport_cdev_ioctl(struct file
*filp
,
2069 unsigned int cmd
, unsigned long arg
)
2072 struct mport_cdev_priv
*data
= filp
->private_data
;
2073 struct mport_dev
*md
= data
->md
;
2075 if (atomic_read(&md
->active
) == 0)
2079 case RIO_MPORT_MAINT_READ_LOCAL
:
2080 return rio_mport_maint_rd(data
, (void __user
*)arg
, 1);
2081 case RIO_MPORT_MAINT_WRITE_LOCAL
:
2082 return rio_mport_maint_wr(data
, (void __user
*)arg
, 1);
2083 case RIO_MPORT_MAINT_READ_REMOTE
:
2084 return rio_mport_maint_rd(data
, (void __user
*)arg
, 0);
2085 case RIO_MPORT_MAINT_WRITE_REMOTE
:
2086 return rio_mport_maint_wr(data
, (void __user
*)arg
, 0);
2087 case RIO_MPORT_MAINT_HDID_SET
:
2088 return maint_hdid_set(data
, (void __user
*)arg
);
2089 case RIO_MPORT_MAINT_COMPTAG_SET
:
2090 return maint_comptag_set(data
, (void __user
*)arg
);
2091 case RIO_MPORT_MAINT_PORT_IDX_GET
:
2092 return maint_port_idx_get(data
, (void __user
*)arg
);
2093 case RIO_MPORT_GET_PROPERTIES
:
2094 md
->properties
.hdid
= md
->mport
->host_deviceid
;
2095 if (copy_to_user((void __user
*)arg
, &(md
->properties
),
2096 sizeof(md
->properties
)))
2099 case RIO_ENABLE_DOORBELL_RANGE
:
2100 return rio_mport_add_db_filter(data
, (void __user
*)arg
);
2101 case RIO_DISABLE_DOORBELL_RANGE
:
2102 return rio_mport_remove_db_filter(data
, (void __user
*)arg
);
2103 case RIO_ENABLE_PORTWRITE_RANGE
:
2104 return rio_mport_add_pw_filter(data
, (void __user
*)arg
);
2105 case RIO_DISABLE_PORTWRITE_RANGE
:
2106 return rio_mport_remove_pw_filter(data
, (void __user
*)arg
);
2107 case RIO_SET_EVENT_MASK
:
2108 data
->event_mask
= (u32
)arg
;
2110 case RIO_GET_EVENT_MASK
:
2111 if (copy_to_user((void __user
*)arg
, &data
->event_mask
,
2115 case RIO_MAP_OUTBOUND
:
2116 return rio_mport_obw_map(filp
, (void __user
*)arg
);
2117 case RIO_MAP_INBOUND
:
2118 return rio_mport_map_inbound(filp
, (void __user
*)arg
);
2119 case RIO_UNMAP_OUTBOUND
:
2120 return rio_mport_obw_free(filp
, (void __user
*)arg
);
2121 case RIO_UNMAP_INBOUND
:
2122 return rio_mport_inbound_free(filp
, (void __user
*)arg
);
2124 return rio_mport_alloc_dma(filp
, (void __user
*)arg
);
2126 return rio_mport_free_dma(filp
, (void __user
*)arg
);
2127 case RIO_WAIT_FOR_ASYNC
:
2128 return rio_mport_wait_for_async_dma(filp
, (void __user
*)arg
);
2130 return rio_mport_transfer_ioctl(filp
, (void __user
*)arg
);
2132 return rio_mport_add_riodev(data
, (void __user
*)arg
);
2134 return rio_mport_del_riodev(data
, (void __user
*)arg
);
2143 * mport_release_mapping - free mapping resources and info structure
2144 * @ref: a pointer to the kref within struct rio_mport_mapping
2146 * NOTE: Shall be called while holding buf_mutex.
2148 static void mport_release_mapping(struct kref
*ref
)
2150 struct rio_mport_mapping
*map
=
2151 container_of(ref
, struct rio_mport_mapping
, ref
);
2152 struct rio_mport
*mport
= map
->md
->mport
;
2154 rmcd_debug(MMAP
, "type %d mapping @ %p (phys = %pad) for %s",
2155 map
->dir
, map
->virt_addr
,
2156 &map
->phys_addr
, mport
->name
);
2158 list_del(&map
->node
);
2162 rio_unmap_inb_region(mport
, map
->phys_addr
);
2165 dma_free_coherent(mport
->dev
.parent
, map
->size
,
2166 map
->virt_addr
, map
->phys_addr
);
2169 rio_unmap_outb_region(mport
, map
->rioid
, map
->rio_addr
);
2175 static void mport_mm_open(struct vm_area_struct
*vma
)
2177 struct rio_mport_mapping
*map
= vma
->vm_private_data
;
2179 rmcd_debug(MMAP
, "%pad", &map
->phys_addr
);
2180 kref_get(&map
->ref
);
2183 static void mport_mm_close(struct vm_area_struct
*vma
)
2185 struct rio_mport_mapping
*map
= vma
->vm_private_data
;
2187 rmcd_debug(MMAP
, "%pad", &map
->phys_addr
);
2188 mutex_lock(&map
->md
->buf_mutex
);
2189 kref_put(&map
->ref
, mport_release_mapping
);
2190 mutex_unlock(&map
->md
->buf_mutex
);
2193 static const struct vm_operations_struct vm_ops
= {
2194 .open
= mport_mm_open
,
2195 .close
= mport_mm_close
,
2198 static int mport_cdev_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
2200 struct mport_cdev_priv
*priv
= filp
->private_data
;
2201 struct mport_dev
*md
;
2202 size_t size
= vma
->vm_end
- vma
->vm_start
;
2204 unsigned long offset
;
2206 struct rio_mport_mapping
*map
;
2208 rmcd_debug(MMAP
, "0x%x bytes at offset 0x%lx",
2209 (unsigned int)size
, vma
->vm_pgoff
);
2212 baddr
= ((dma_addr_t
)vma
->vm_pgoff
<< PAGE_SHIFT
);
2214 mutex_lock(&md
->buf_mutex
);
2215 list_for_each_entry(map
, &md
->mappings
, node
) {
2216 if (baddr
>= map
->phys_addr
&&
2217 baddr
< (map
->phys_addr
+ map
->size
)) {
2222 mutex_unlock(&md
->buf_mutex
);
2227 offset
= baddr
- map
->phys_addr
;
2229 if (size
+ offset
> map
->size
)
2232 vma
->vm_pgoff
= offset
>> PAGE_SHIFT
;
2233 rmcd_debug(MMAP
, "MMAP adjusted offset = 0x%lx", vma
->vm_pgoff
);
2235 if (map
->dir
== MAP_INBOUND
|| map
->dir
== MAP_DMA
)
2236 ret
= dma_mmap_coherent(md
->mport
->dev
.parent
, vma
,
2237 map
->virt_addr
, map
->phys_addr
, map
->size
);
2238 else if (map
->dir
== MAP_OUTBOUND
) {
2239 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
2240 ret
= vm_iomap_memory(vma
, map
->phys_addr
, map
->size
);
2242 rmcd_error("Attempt to mmap unsupported mapping type");
2247 vma
->vm_private_data
= map
;
2248 vma
->vm_ops
= &vm_ops
;
2251 rmcd_error("MMAP exit with err=%d", ret
);
2257 static __poll_t
mport_cdev_poll(struct file
*filp
, poll_table
*wait
)
2259 struct mport_cdev_priv
*priv
= filp
->private_data
;
2261 poll_wait(filp
, &priv
->event_rx_wait
, wait
);
2262 if (kfifo_len(&priv
->event_fifo
))
2263 return EPOLLIN
| EPOLLRDNORM
;
2268 static ssize_t
mport_read(struct file
*filp
, char __user
*buf
, size_t count
,
2271 struct mport_cdev_priv
*priv
= filp
->private_data
;
2278 if (kfifo_is_empty(&priv
->event_fifo
) &&
2279 (filp
->f_flags
& O_NONBLOCK
))
2282 if (count
% sizeof(struct rio_event
))
2285 ret
= wait_event_interruptible(priv
->event_rx_wait
,
2286 kfifo_len(&priv
->event_fifo
) != 0);
2290 while (ret
< count
) {
2291 if (kfifo_to_user(&priv
->event_fifo
, buf
,
2292 sizeof(struct rio_event
), &copied
))
2301 static ssize_t
mport_write(struct file
*filp
, const char __user
*buf
,
2302 size_t count
, loff_t
*ppos
)
2304 struct mport_cdev_priv
*priv
= filp
->private_data
;
2305 struct rio_mport
*mport
= priv
->md
->mport
;
2306 struct rio_event event
;
2312 if (count
% sizeof(event
))
2316 while ((count
- len
) >= (int)sizeof(event
)) {
2317 if (copy_from_user(&event
, buf
, sizeof(event
)))
2320 if (event
.header
!= RIO_DOORBELL
)
2323 ret
= rio_mport_send_doorbell(mport
,
2324 event
.u
.doorbell
.rioid
,
2325 event
.u
.doorbell
.payload
);
2329 len
+= sizeof(event
);
2330 buf
+= sizeof(event
);
2336 static const struct file_operations mport_fops
= {
2337 .owner
= THIS_MODULE
,
2338 .open
= mport_cdev_open
,
2339 .release
= mport_cdev_release
,
2340 .poll
= mport_cdev_poll
,
2342 .write
= mport_write
,
2343 .mmap
= mport_cdev_mmap
,
2344 .fasync
= mport_cdev_fasync
,
2345 .unlocked_ioctl
= mport_cdev_ioctl
2349 * Character device management
2352 static void mport_device_release(struct device
*dev
)
2354 struct mport_dev
*md
;
2356 rmcd_debug(EXIT
, "%s", dev_name(dev
));
2357 md
= container_of(dev
, struct mport_dev
, dev
);
2362 * mport_cdev_add() - Create mport_dev from rio_mport
2363 * @mport: RapidIO master port
2365 static struct mport_dev
*mport_cdev_add(struct rio_mport
*mport
)
2368 struct mport_dev
*md
;
2369 struct rio_mport_attr attr
;
2371 md
= kzalloc(sizeof(*md
), GFP_KERNEL
);
2373 rmcd_error("Unable allocate a device object");
2378 mutex_init(&md
->buf_mutex
);
2379 mutex_init(&md
->file_mutex
);
2380 INIT_LIST_HEAD(&md
->file_list
);
2382 device_initialize(&md
->dev
);
2383 md
->dev
.devt
= MKDEV(MAJOR(dev_number
), mport
->id
);
2384 md
->dev
.class = &dev_class
;
2385 md
->dev
.parent
= &mport
->dev
;
2386 md
->dev
.release
= mport_device_release
;
2387 dev_set_name(&md
->dev
, DEV_NAME
"%d", mport
->id
);
2388 atomic_set(&md
->active
, 1);
2390 cdev_init(&md
->cdev
, &mport_fops
);
2391 md
->cdev
.owner
= THIS_MODULE
;
2393 INIT_LIST_HEAD(&md
->doorbells
);
2394 spin_lock_init(&md
->db_lock
);
2395 INIT_LIST_HEAD(&md
->portwrites
);
2396 spin_lock_init(&md
->pw_lock
);
2397 INIT_LIST_HEAD(&md
->mappings
);
2399 md
->properties
.id
= mport
->id
;
2400 md
->properties
.sys_size
= mport
->sys_size
;
2401 md
->properties
.hdid
= mport
->host_deviceid
;
2402 md
->properties
.index
= mport
->index
;
2404 /* The transfer_mode property will be returned through mport query
2407 #ifdef CONFIG_FSL_RIO /* for now: only on Freescale's SoCs */
2408 md
->properties
.transfer_mode
|= RIO_TRANSFER_MODE_MAPPED
;
2410 md
->properties
.transfer_mode
|= RIO_TRANSFER_MODE_TRANSFER
;
2413 ret
= cdev_device_add(&md
->cdev
, &md
->dev
);
2415 rmcd_error("Failed to register mport %d (err=%d)",
2419 ret
= rio_query_mport(mport
, &attr
);
2421 md
->properties
.flags
= attr
.flags
;
2422 md
->properties
.link_speed
= attr
.link_speed
;
2423 md
->properties
.link_width
= attr
.link_width
;
2424 md
->properties
.dma_max_sge
= attr
.dma_max_sge
;
2425 md
->properties
.dma_max_size
= attr
.dma_max_size
;
2426 md
->properties
.dma_align
= attr
.dma_align
;
2427 md
->properties
.cap_sys_size
= 0;
2428 md
->properties
.cap_transfer_mode
= 0;
2429 md
->properties
.cap_addr_size
= 0;
2431 pr_info(DRV_PREFIX
"Failed to obtain info for %s cdev(%d:%d)\n",
2432 mport
->name
, MAJOR(dev_number
), mport
->id
);
2434 mutex_lock(&mport_devs_lock
);
2435 list_add_tail(&md
->node
, &mport_devs
);
2436 mutex_unlock(&mport_devs_lock
);
2438 pr_info(DRV_PREFIX
"Added %s cdev(%d:%d)\n",
2439 mport
->name
, MAJOR(dev_number
), mport
->id
);
2444 put_device(&md
->dev
);
2449 * mport_cdev_terminate_dma() - Stop all active DMA data transfers and release
2450 * associated DMA channels.
2452 static void mport_cdev_terminate_dma(struct mport_dev
*md
)
2454 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
2455 struct mport_cdev_priv
*client
;
2457 rmcd_debug(DMA
, "%s", dev_name(&md
->dev
));
2459 mutex_lock(&md
->file_mutex
);
2460 list_for_each_entry(client
, &md
->file_list
, list
) {
2461 if (client
->dmach
) {
2462 dmaengine_terminate_all(client
->dmach
);
2463 rio_release_dma(client
->dmach
);
2466 mutex_unlock(&md
->file_mutex
);
2469 dmaengine_terminate_all(md
->dma_chan
);
2470 rio_release_dma(md
->dma_chan
);
2471 md
->dma_chan
= NULL
;
2478 * mport_cdev_kill_fasync() - Send SIGIO signal to all processes with open
2481 static int mport_cdev_kill_fasync(struct mport_dev
*md
)
2483 unsigned int files
= 0;
2484 struct mport_cdev_priv
*client
;
2486 mutex_lock(&md
->file_mutex
);
2487 list_for_each_entry(client
, &md
->file_list
, list
) {
2488 if (client
->async_queue
)
2489 kill_fasync(&client
->async_queue
, SIGIO
, POLL_HUP
);
2492 mutex_unlock(&md
->file_mutex
);
2497 * mport_cdev_remove() - Remove mport character device
2498 * @dev: Mport device to remove
2500 static void mport_cdev_remove(struct mport_dev
*md
)
2502 struct rio_mport_mapping
*map
, *_map
;
2504 rmcd_debug(EXIT
, "Remove %s cdev", md
->mport
->name
);
2505 atomic_set(&md
->active
, 0);
2506 mport_cdev_terminate_dma(md
);
2507 rio_del_mport_pw_handler(md
->mport
, md
, rio_mport_pw_handler
);
2508 cdev_device_del(&md
->cdev
, &md
->dev
);
2509 mport_cdev_kill_fasync(md
);
2511 /* TODO: do we need to give clients some time to close file
2512 * descriptors? Simple wait for XX, or kref?
2516 * Release DMA buffers allocated for the mport device.
2517 * Disable associated inbound Rapidio requests mapping if applicable.
2519 mutex_lock(&md
->buf_mutex
);
2520 list_for_each_entry_safe(map
, _map
, &md
->mappings
, node
) {
2521 kref_put(&map
->ref
, mport_release_mapping
);
2523 mutex_unlock(&md
->buf_mutex
);
2525 if (!list_empty(&md
->mappings
))
2526 rmcd_warn("WARNING: %s pending mappings on removal",
2529 rio_release_inb_dbell(md
->mport
, 0, 0x0fff);
2531 put_device(&md
->dev
);
2535 * RIO rio_mport_interface driver
2539 * mport_add_mport() - Add rio_mport from LDM device struct
2540 * @dev: Linux device model struct
2542 static int mport_add_mport(struct device
*dev
)
2544 struct rio_mport
*mport
= NULL
;
2545 struct mport_dev
*chdev
= NULL
;
2547 mport
= to_rio_mport(dev
);
2551 chdev
= mport_cdev_add(mport
);
2559 * mport_remove_mport() - Remove rio_mport from global list
2560 * TODO remove device from global mport_dev list
2562 static void mport_remove_mport(struct device
*dev
)
2564 struct rio_mport
*mport
= NULL
;
2565 struct mport_dev
*chdev
;
2568 mport
= to_rio_mport(dev
);
2569 rmcd_debug(EXIT
, "Remove %s", mport
->name
);
2571 mutex_lock(&mport_devs_lock
);
2572 list_for_each_entry(chdev
, &mport_devs
, node
) {
2573 if (chdev
->mport
->id
== mport
->id
) {
2574 atomic_set(&chdev
->active
, 0);
2575 list_del(&chdev
->node
);
2580 mutex_unlock(&mport_devs_lock
);
2583 mport_cdev_remove(chdev
);
2586 /* the rio_mport_interface is used to handle local mport devices */
2587 static struct class_interface rio_mport_interface __refdata
= {
2588 .class = &rio_mport_class
,
2589 .add_dev
= mport_add_mport
,
2590 .remove_dev
= mport_remove_mport
,
2594 * Linux kernel module
2598 * mport_init - Driver module loading
2600 static int __init
mport_init(void)
2604 /* Create device class needed by udev */
2605 ret
= class_register(&dev_class
);
2607 rmcd_error("Unable to create " DRV_NAME
" class");
2611 ret
= alloc_chrdev_region(&dev_number
, 0, RIO_MAX_MPORTS
, DRV_NAME
);
2615 rmcd_debug(INIT
, "Registered class with major=%d", MAJOR(dev_number
));
2617 /* Register to rio_mport_interface */
2618 ret
= class_interface_register(&rio_mport_interface
);
2620 rmcd_error("class_interface_register() failed, err=%d", ret
);
2627 unregister_chrdev_region(dev_number
, RIO_MAX_MPORTS
);
2629 class_unregister(&dev_class
);
2634 * mport_exit - Driver module unloading
2636 static void __exit
mport_exit(void)
2638 class_interface_unregister(&rio_mport_interface
);
2639 class_unregister(&dev_class
);
2640 unregister_chrdev_region(dev_number
, RIO_MAX_MPORTS
);
2643 module_init(mport_init
);
2644 module_exit(mport_exit
);