2 * RapidIO mport character device
4 * Copyright 2014-2015 Integrated Device Technology, Inc.
6 * Copyright 2014-2015 Prodrive Technologies
9 * Copyright (C) 2014 Texas Instruments Incorporated
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/cdev.h>
20 #include <linux/ioctl.h>
21 #include <linux/uaccess.h>
22 #include <linux/list.h>
24 #include <linux/err.h>
25 #include <linux/net.h>
26 #include <linux/poll.h>
27 #include <linux/spinlock.h>
28 #include <linux/sched.h>
29 #include <linux/kfifo.h>
32 #include <linux/slab.h>
33 #include <linux/vmalloc.h>
34 #include <linux/mman.h>
36 #include <linux/dma-mapping.h>
37 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
38 #include <linux/dmaengine.h>
41 #include <linux/rio.h>
42 #include <linux/rio_ids.h>
43 #include <linux/rio_drv.h>
44 #include <linux/rio_mport_cdev.h>
48 #define DRV_NAME "rio_mport"
49 #define DRV_PREFIX DRV_NAME ": "
50 #define DEV_NAME "rio_mport"
51 #define DRV_VERSION "1.0.0"
53 /* Debug output filtering masks */
56 DBG_INIT = BIT(0), /* driver init */
57 DBG_EXIT = BIT(1), /* driver exit */
58 DBG_MPORT = BIT(2), /* mport add/remove */
59 DBG_RDEV = BIT(3), /* RapidIO device add/remove */
60 DBG_DMA = BIT(4), /* DMA transfer messages */
61 DBG_MMAP = BIT(5), /* mapping messages */
62 DBG_IBW = BIT(6), /* inbound window */
63 DBG_EVENT = BIT(7), /* event handling messages */
64 DBG_OBW = BIT(8), /* outbound window messages */
65 DBG_DBELL = BIT(9), /* doorbell messages */
70 #define rmcd_debug(level, fmt, arg...) \
72 if (DBG_##level & dbg_level) \
73 pr_debug(DRV_PREFIX "%s: " fmt "\n", __func__, ##arg); \
76 #define rmcd_debug(level, fmt, arg...) \
77 no_printk(KERN_DEBUG pr_fmt(DRV_PREFIX fmt "\n"), ##arg)
80 #define rmcd_warn(fmt, arg...) \
81 pr_warn(DRV_PREFIX "%s WARNING " fmt "\n", __func__, ##arg)
83 #define rmcd_error(fmt, arg...) \
84 pr_err(DRV_PREFIX "%s ERROR " fmt "\n", __func__, ##arg)
90 MODULE_DESCRIPTION("RapidIO mport character device driver");
91 MODULE_LICENSE("GPL");
92 MODULE_VERSION(DRV_VERSION);
94 static int dma_timeout = 3000; /* DMA transfer timeout in msec */
95 module_param(dma_timeout, int, S_IRUGO);
96 MODULE_PARM_DESC(dma_timeout, "DMA Transfer Timeout in msec (default: 3000)");
99 static u32 dbg_level = DBG_NONE;
100 module_param(dbg_level, uint, S_IWUSR | S_IWGRP | S_IRUGO);
101 MODULE_PARM_DESC(dbg_level, "Debugging output level (default 0 = none)");
105 * An internal DMA coherent buffer
107 struct mport_dma_buf {
117 * Internal memory mapping structure
119 enum rio_mport_map_dir {
125 struct rio_mport_mapping {
126 struct list_head node;
127 struct mport_dev *md;
128 enum rio_mport_map_dir dir;
131 dma_addr_t phys_addr; /* for mmap */
132 void *virt_addr; /* kernel address, for dma_free_coherent */
134 struct kref ref; /* refcount of vmas sharing the mapping */
138 struct rio_mport_dma_map {
145 #define MPORT_MAX_DMA_BUFS 16
146 #define MPORT_EVENT_DEPTH 10
149 * mport_dev driver-specific structure that represents mport device
150 * @active mport device status flag
151 * @node list node to maintain list of registered mports
152 * @cdev character device
153 * @dev associated device object
154 * @mport associated subsystem's master port device object
155 * @buf_mutex lock for buffer handling
156 * @file_mutex - lock for open files list
157 * @file_list - list of open files on given mport
158 * @properties properties of this mport
159 * @portwrites queue of inbound portwrites
160 * @pw_lock lock for port write queue
161 * @mappings queue for memory mappings
162 * @dma_chan DMA channels associated with this device
168 struct list_head node;
171 struct rio_mport *mport;
172 struct mutex buf_mutex;
173 struct mutex file_mutex;
174 struct list_head file_list;
175 struct rio_mport_properties properties;
176 struct list_head doorbells;
178 struct list_head portwrites;
180 struct list_head mappings;
181 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
182 struct dma_chan *dma_chan;
184 struct completion comp;
189 * mport_cdev_priv - data structure specific to individual file object
190 * associated with an open device
191 * @md master port character device object
192 * @async_queue - asynchronous notification queue
193 * @list - file objects tracking list
194 * @db_filters inbound doorbell filters for this descriptor
195 * @pw_filters portwrite filters for this descriptor
196 * @event_fifo event fifo for this descriptor
197 * @event_rx_wait wait queue for this descriptor
198 * @fifo_lock lock for event_fifo
199 * @event_mask event mask for this descriptor
200 * @dmach DMA engine channel allocated for specific file object
202 struct mport_cdev_priv {
203 struct mport_dev *md;
204 struct fasync_struct *async_queue;
205 struct list_head list;
206 struct list_head db_filters;
207 struct list_head pw_filters;
208 struct kfifo event_fifo;
209 wait_queue_head_t event_rx_wait;
210 spinlock_t fifo_lock;
211 u32 event_mask; /* RIO_DOORBELL, RIO_PORTWRITE */
212 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
213 struct dma_chan *dmach;
214 struct list_head async_list;
216 struct mutex dma_lock;
218 struct completion comp;
223 * rio_mport_pw_filter - structure to describe a portwrite filter
224 * md_node node in mport device's list
225 * priv_node node in private file object's list
226 * priv reference to private data
227 * filter actual portwrite filter
229 struct rio_mport_pw_filter {
230 struct list_head md_node;
231 struct list_head priv_node;
232 struct mport_cdev_priv *priv;
233 struct rio_pw_filter filter;
237 * rio_mport_db_filter - structure to describe a doorbell filter
238 * @data_node reference to device node
239 * @priv_node node in private data
240 * @priv reference to private data
241 * @filter actual doorbell filter
243 struct rio_mport_db_filter {
244 struct list_head data_node;
245 struct list_head priv_node;
246 struct mport_cdev_priv *priv;
247 struct rio_doorbell_filter filter;
250 static LIST_HEAD(mport_devs);
251 static DEFINE_MUTEX(mport_devs_lock);
253 #if (0) /* used by commented out portion of poll function : FIXME */
254 static DECLARE_WAIT_QUEUE_HEAD(mport_cdev_wait);
257 static struct class *dev_class;
258 static dev_t dev_number;
260 static void mport_release_mapping(struct kref *ref);
262 static int rio_mport_maint_rd(struct mport_cdev_priv *priv, void __user *arg,
265 struct rio_mport *mport = priv->md->mport;
266 struct rio_mport_maint_io maint_io;
272 if (unlikely(copy_from_user(&maint_io, arg, sizeof(maint_io))))
275 if ((maint_io.offset % 4) ||
276 (maint_io.length == 0) || (maint_io.length % 4) ||
277 (maint_io.length + maint_io.offset) > RIO_MAINT_SPACE_SZ)
280 buffer = vmalloc(maint_io.length);
283 length = maint_io.length/sizeof(u32);
284 offset = maint_io.offset;
286 for (i = 0; i < length; i++) {
288 ret = __rio_local_read_config_32(mport,
291 ret = rio_mport_read_config_32(mport, maint_io.rioid,
292 maint_io.hopcount, offset, &buffer[i]);
299 if (unlikely(copy_to_user((void __user *)(uintptr_t)maint_io.buffer,
300 buffer, maint_io.length)))
307 static int rio_mport_maint_wr(struct mport_cdev_priv *priv, void __user *arg,
310 struct rio_mport *mport = priv->md->mport;
311 struct rio_mport_maint_io maint_io;
315 int ret = -EINVAL, i;
317 if (unlikely(copy_from_user(&maint_io, arg, sizeof(maint_io))))
320 if ((maint_io.offset % 4) ||
321 (maint_io.length == 0) || (maint_io.length % 4) ||
322 (maint_io.length + maint_io.offset) > RIO_MAINT_SPACE_SZ)
325 buffer = vmalloc(maint_io.length);
328 length = maint_io.length;
330 if (unlikely(copy_from_user(buffer,
331 (void __user *)(uintptr_t)maint_io.buffer, length))) {
336 offset = maint_io.offset;
337 length /= sizeof(u32);
339 for (i = 0; i < length; i++) {
341 ret = __rio_local_write_config_32(mport,
344 ret = rio_mport_write_config_32(mport, maint_io.rioid,
360 * Inbound/outbound memory mapping functions
363 rio_mport_create_outbound_mapping(struct mport_dev *md, struct file *filp,
364 u16 rioid, u64 raddr, u32 size,
367 struct rio_mport *mport = md->mport;
368 struct rio_mport_mapping *map;
371 rmcd_debug(OBW, "did=%d ra=0x%llx sz=0x%x", rioid, raddr, size);
373 map = kzalloc(sizeof(*map), GFP_KERNEL);
377 ret = rio_map_outb_region(mport, rioid, raddr, size, 0, paddr);
381 map->dir = MAP_OUTBOUND;
383 map->rio_addr = raddr;
385 map->phys_addr = *paddr;
388 kref_init(&map->ref);
389 list_add_tail(&map->node, &md->mappings);
397 rio_mport_get_outbound_mapping(struct mport_dev *md, struct file *filp,
398 u16 rioid, u64 raddr, u32 size,
401 struct rio_mport_mapping *map;
404 mutex_lock(&md->buf_mutex);
405 list_for_each_entry(map, &md->mappings, node) {
406 if (map->dir != MAP_OUTBOUND)
408 if (rioid == map->rioid &&
409 raddr == map->rio_addr && size == map->size) {
410 *paddr = map->phys_addr;
413 } else if (rioid == map->rioid &&
414 raddr < (map->rio_addr + map->size - 1) &&
415 (raddr + size) > map->rio_addr) {
421 /* If not found, create new */
423 err = rio_mport_create_outbound_mapping(md, filp, rioid, raddr,
425 mutex_unlock(&md->buf_mutex);
429 static int rio_mport_obw_map(struct file *filp, void __user *arg)
431 struct mport_cdev_priv *priv = filp->private_data;
432 struct mport_dev *data = priv->md;
437 if (unlikely(copy_from_user(&map, arg, sizeof(map))))
440 rmcd_debug(OBW, "did=%d ra=0x%llx sz=0x%llx",
441 map.rioid, map.rio_addr, map.length);
443 ret = rio_mport_get_outbound_mapping(data, filp, map.rioid,
444 map.rio_addr, map.length, &paddr);
446 rmcd_error("Failed to set OBW err= %d", ret);
452 if (unlikely(copy_to_user(arg, &map, sizeof(map))))
458 * rio_mport_obw_free() - unmap an OutBound Window from RapidIO address space
460 * @priv: driver private data
461 * @arg: buffer handle returned by allocation routine
463 static int rio_mport_obw_free(struct file *filp, void __user *arg)
465 struct mport_cdev_priv *priv = filp->private_data;
466 struct mport_dev *md = priv->md;
468 struct rio_mport_mapping *map, *_map;
470 if (!md->mport->ops->unmap_outb)
471 return -EPROTONOSUPPORT;
473 if (copy_from_user(&handle, arg, sizeof(handle)))
476 rmcd_debug(OBW, "h=0x%llx", handle);
478 mutex_lock(&md->buf_mutex);
479 list_for_each_entry_safe(map, _map, &md->mappings, node) {
480 if (map->dir == MAP_OUTBOUND && map->phys_addr == handle) {
481 if (map->filp == filp) {
482 rmcd_debug(OBW, "kref_put h=0x%llx", handle);
484 kref_put(&map->ref, mport_release_mapping);
489 mutex_unlock(&md->buf_mutex);
495 * maint_hdid_set() - Set the host Device ID
496 * @priv: driver private data
499 static int maint_hdid_set(struct mport_cdev_priv *priv, void __user *arg)
501 struct mport_dev *md = priv->md;
504 if (copy_from_user(&hdid, arg, sizeof(hdid)))
507 md->mport->host_deviceid = hdid;
508 md->properties.hdid = hdid;
509 rio_local_set_device_id(md->mport, hdid);
511 rmcd_debug(MPORT, "Set host device Id to %d", hdid);
517 * maint_comptag_set() - Set the host Component Tag
518 * @priv: driver private data
519 * @arg: Component Tag
521 static int maint_comptag_set(struct mport_cdev_priv *priv, void __user *arg)
523 struct mport_dev *md = priv->md;
526 if (copy_from_user(&comptag, arg, sizeof(comptag)))
529 rio_local_write_config_32(md->mport, RIO_COMPONENT_TAG_CSR, comptag);
531 rmcd_debug(MPORT, "Set host Component Tag to %d", comptag);
536 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
538 struct mport_dma_req {
539 struct kref refcount;
540 struct list_head node;
542 struct mport_cdev_priv *priv;
543 enum rio_transfer_sync sync;
545 struct page **page_list;
546 unsigned int nr_pages;
547 struct rio_mport_mapping *map;
548 struct dma_chan *dmach;
549 enum dma_data_direction dir;
551 enum dma_status status;
552 struct completion req_comp;
555 static void mport_release_def_dma(struct kref *dma_ref)
557 struct mport_dev *md =
558 container_of(dma_ref, struct mport_dev, dma_ref);
560 rmcd_debug(EXIT, "DMA_%d", md->dma_chan->chan_id);
561 rio_release_dma(md->dma_chan);
565 static void mport_release_dma(struct kref *dma_ref)
567 struct mport_cdev_priv *priv =
568 container_of(dma_ref, struct mport_cdev_priv, dma_ref);
570 rmcd_debug(EXIT, "DMA_%d", priv->dmach->chan_id);
571 complete(&priv->comp);
574 static void dma_req_free(struct kref *ref)
576 struct mport_dma_req *req = container_of(ref, struct mport_dma_req,
578 struct mport_cdev_priv *priv = req->priv;
581 dma_unmap_sg(req->dmach->device->dev,
582 req->sgt.sgl, req->sgt.nents, req->dir);
583 sg_free_table(&req->sgt);
584 if (req->page_list) {
585 for (i = 0; i < req->nr_pages; i++)
586 put_page(req->page_list[i]);
587 kfree(req->page_list);
591 mutex_lock(&req->map->md->buf_mutex);
592 kref_put(&req->map->ref, mport_release_mapping);
593 mutex_unlock(&req->map->md->buf_mutex);
596 kref_put(&priv->dma_ref, mport_release_dma);
601 static void dma_xfer_callback(void *param)
603 struct mport_dma_req *req = (struct mport_dma_req *)param;
604 struct mport_cdev_priv *priv = req->priv;
606 req->status = dma_async_is_tx_complete(priv->dmach, req->cookie,
608 complete(&req->req_comp);
609 kref_put(&req->refcount, dma_req_free);
613 * prep_dma_xfer() - Configure and send request to DMAengine to prepare DMA
615 * Returns pointer to DMA transaction descriptor allocated by DMA driver on
616 * success or ERR_PTR (and/or NULL) if failed. Caller must check returned
617 * non-NULL pointer using IS_ERR macro.
619 static struct dma_async_tx_descriptor
620 *prep_dma_xfer(struct dma_chan *chan, struct rio_transfer_io *transfer,
621 struct sg_table *sgt, int nents, enum dma_transfer_direction dir,
622 enum dma_ctrl_flags flags)
624 struct rio_dma_data tx_data;
626 tx_data.sg = sgt->sgl;
627 tx_data.sg_len = nents;
628 tx_data.rio_addr_u = 0;
629 tx_data.rio_addr = transfer->rio_addr;
630 if (dir == DMA_MEM_TO_DEV) {
631 switch (transfer->method) {
632 case RIO_EXCHANGE_NWRITE:
633 tx_data.wr_type = RDW_ALL_NWRITE;
635 case RIO_EXCHANGE_NWRITE_R_ALL:
636 tx_data.wr_type = RDW_ALL_NWRITE_R;
638 case RIO_EXCHANGE_NWRITE_R:
639 tx_data.wr_type = RDW_LAST_NWRITE_R;
641 case RIO_EXCHANGE_DEFAULT:
642 tx_data.wr_type = RDW_DEFAULT;
645 return ERR_PTR(-EINVAL);
649 return rio_dma_prep_xfer(chan, transfer->rioid, &tx_data, dir, flags);
652 /* Request DMA channel associated with this mport device.
653 * Try to request DMA channel for every new process that opened given
654 * mport. If a new DMA channel is not available use default channel
655 * which is the first DMA channel opened on mport device.
657 static int get_dma_channel(struct mport_cdev_priv *priv)
659 mutex_lock(&priv->dma_lock);
661 priv->dmach = rio_request_mport_dma(priv->md->mport);
663 /* Use default DMA channel if available */
664 if (priv->md->dma_chan) {
665 priv->dmach = priv->md->dma_chan;
666 kref_get(&priv->md->dma_ref);
668 rmcd_error("Failed to get DMA channel");
669 mutex_unlock(&priv->dma_lock);
672 } else if (!priv->md->dma_chan) {
673 /* Register default DMA channel if we do not have one */
674 priv->md->dma_chan = priv->dmach;
675 kref_init(&priv->md->dma_ref);
676 rmcd_debug(DMA, "Register DMA_chan %d as default",
677 priv->dmach->chan_id);
680 kref_init(&priv->dma_ref);
681 init_completion(&priv->comp);
684 kref_get(&priv->dma_ref);
685 mutex_unlock(&priv->dma_lock);
689 static void put_dma_channel(struct mport_cdev_priv *priv)
691 kref_put(&priv->dma_ref, mport_release_dma);
695 * DMA transfer functions
697 static int do_dma_request(struct mport_dma_req *req,
698 struct rio_transfer_io *xfer,
699 enum rio_transfer_sync sync, int nents)
701 struct mport_cdev_priv *priv;
702 struct sg_table *sgt;
703 struct dma_chan *chan;
704 struct dma_async_tx_descriptor *tx;
706 unsigned long tmo = msecs_to_jiffies(dma_timeout);
707 enum dma_transfer_direction dir;
715 dir = (req->dir == DMA_FROM_DEVICE) ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
717 rmcd_debug(DMA, "%s(%d) uses %s for DMA_%s",
718 current->comm, task_pid_nr(current),
719 dev_name(&chan->dev->device),
720 (dir == DMA_DEV_TO_MEM)?"READ":"WRITE");
722 /* Initialize DMA transaction request */
723 tx = prep_dma_xfer(chan, xfer, sgt, nents, dir,
724 DMA_CTRL_ACK | DMA_PREP_INTERRUPT);
727 rmcd_debug(DMA, "prep error for %s A:0x%llx L:0x%llx",
728 (dir == DMA_DEV_TO_MEM)?"READ":"WRITE",
729 xfer->rio_addr, xfer->length);
732 } else if (IS_ERR(tx)) {
734 rmcd_debug(DMA, "prep error %d for %s A:0x%llx L:0x%llx", ret,
735 (dir == DMA_DEV_TO_MEM)?"READ":"WRITE",
736 xfer->rio_addr, xfer->length);
740 tx->callback = dma_xfer_callback;
741 tx->callback_param = req;
743 req->status = DMA_IN_PROGRESS;
744 kref_get(&req->refcount);
746 cookie = dmaengine_submit(tx);
747 req->cookie = cookie;
749 rmcd_debug(DMA, "pid=%d DMA_%s tx_cookie = %d", task_pid_nr(current),
750 (dir == DMA_DEV_TO_MEM)?"READ":"WRITE", cookie);
752 if (dma_submit_error(cookie)) {
753 rmcd_error("submit err=%d (addr:0x%llx len:0x%llx)",
754 cookie, xfer->rio_addr, xfer->length);
755 kref_put(&req->refcount, dma_req_free);
760 dma_async_issue_pending(chan);
762 if (sync == RIO_TRANSFER_ASYNC) {
763 spin_lock(&priv->req_lock);
764 list_add_tail(&req->node, &priv->async_list);
765 spin_unlock(&priv->req_lock);
767 } else if (sync == RIO_TRANSFER_FAF)
770 wret = wait_for_completion_interruptible_timeout(&req->req_comp, tmo);
773 /* Timeout on wait occurred */
774 rmcd_error("%s(%d) timed out waiting for DMA_%s %d",
775 current->comm, task_pid_nr(current),
776 (dir == DMA_DEV_TO_MEM)?"READ":"WRITE", cookie);
778 } else if (wret == -ERESTARTSYS) {
779 /* Wait_for_completion was interrupted by a signal but DMA may
782 rmcd_error("%s(%d) wait for DMA_%s %d was interrupted",
783 current->comm, task_pid_nr(current),
784 (dir == DMA_DEV_TO_MEM)?"READ":"WRITE", cookie);
788 if (req->status != DMA_COMPLETE) {
789 /* DMA transaction completion was signaled with error */
790 rmcd_error("%s(%d) DMA_%s %d completed with status %d (ret=%d)",
791 current->comm, task_pid_nr(current),
792 (dir == DMA_DEV_TO_MEM)?"READ":"WRITE",
793 cookie, req->status, ret);
802 * rio_dma_transfer() - Perform RapidIO DMA data transfer to/from
803 * the remote RapidIO device
804 * @filp: file pointer associated with the call
805 * @transfer_mode: DMA transfer mode
806 * @sync: synchronization mode
807 * @dir: DMA transfer direction (DMA_MEM_TO_DEV = write OR
808 * DMA_DEV_TO_MEM = read)
809 * @xfer: data transfer descriptor structure
812 rio_dma_transfer(struct file *filp, u32 transfer_mode,
813 enum rio_transfer_sync sync, enum dma_data_direction dir,
814 struct rio_transfer_io *xfer)
816 struct mport_cdev_priv *priv = filp->private_data;
817 unsigned long nr_pages = 0;
818 struct page **page_list = NULL;
819 struct mport_dma_req *req;
820 struct mport_dev *md = priv->md;
821 struct dma_chan *chan;
825 if (xfer->length == 0)
827 req = kzalloc(sizeof(*req), GFP_KERNEL);
831 ret = get_dma_channel(priv);
838 kref_init(&req->refcount);
839 init_completion(&req->req_comp);
847 * If parameter loc_addr != NULL, we are transferring data from/to
848 * data buffer allocated in user-space: lock in memory user-space
849 * buffer pages and build an SG table for DMA transfer request
851 * Otherwise (loc_addr == NULL) contiguous kernel-space buffer is
852 * used for DMA data transfers: build single entry SG table using
853 * offset within the internal buffer specified by handle parameter.
855 if (xfer->loc_addr) {
859 offset = lower_32_bits(offset_in_page(xfer->loc_addr));
860 nr_pages = PAGE_ALIGN(xfer->length + offset) >> PAGE_SHIFT;
862 page_list = kmalloc_array(nr_pages,
863 sizeof(*page_list), GFP_KERNEL);
864 if (page_list == NULL) {
869 pinned = get_user_pages_fast(
870 (unsigned long)xfer->loc_addr & PAGE_MASK,
871 nr_pages, dir == DMA_FROM_DEVICE, page_list);
873 if (pinned != nr_pages) {
875 rmcd_error("get_user_pages_unlocked err=%ld",
879 rmcd_error("pinned %ld out of %ld pages",
885 ret = sg_alloc_table_from_pages(&req->sgt, page_list, nr_pages,
886 offset, xfer->length, GFP_KERNEL);
888 rmcd_error("sg_alloc_table failed with err=%d", ret);
892 req->page_list = page_list;
893 req->nr_pages = nr_pages;
896 struct rio_mport_mapping *map;
898 baddr = (dma_addr_t)xfer->handle;
900 mutex_lock(&md->buf_mutex);
901 list_for_each_entry(map, &md->mappings, node) {
902 if (baddr >= map->phys_addr &&
903 baddr < (map->phys_addr + map->size)) {
909 mutex_unlock(&md->buf_mutex);
911 if (req->map == NULL) {
916 if (xfer->length + xfer->offset > map->size) {
921 ret = sg_alloc_table(&req->sgt, 1, GFP_KERNEL);
923 rmcd_error("sg_alloc_table failed for internal buf");
927 sg_set_buf(req->sgt.sgl,
928 map->virt_addr + (baddr - map->phys_addr) +
929 xfer->offset, xfer->length);
932 nents = dma_map_sg(chan->device->dev,
933 req->sgt.sgl, req->sgt.nents, dir);
935 rmcd_error("Failed to map SG list");
940 ret = do_dma_request(req, xfer, sync, nents);
943 if (sync == RIO_TRANSFER_ASYNC)
944 return ret; /* return ASYNC cookie */
946 rmcd_debug(DMA, "do_dma_request failed with err=%d", ret);
950 if (!req->page_list) {
951 for (i = 0; i < nr_pages; i++)
952 put_page(page_list[i]);
956 kref_put(&req->refcount, dma_req_free);
960 static int rio_mport_transfer_ioctl(struct file *filp, void __user *arg)
962 struct mport_cdev_priv *priv = filp->private_data;
963 struct rio_transaction transaction;
964 struct rio_transfer_io *transfer;
965 enum dma_data_direction dir;
968 if (unlikely(copy_from_user(&transaction, arg, sizeof(transaction))))
971 if (transaction.count != 1) /* only single transfer for now */
974 if ((transaction.transfer_mode &
975 priv->md->properties.transfer_mode) == 0)
978 transfer = vmalloc(array_size(sizeof(*transfer), transaction.count));
982 if (unlikely(copy_from_user(transfer,
983 (void __user *)(uintptr_t)transaction.block,
984 transaction.count * sizeof(*transfer)))) {
989 dir = (transaction.dir == RIO_TRANSFER_DIR_READ) ?
990 DMA_FROM_DEVICE : DMA_TO_DEVICE;
991 for (i = 0; i < transaction.count && ret == 0; i++)
992 ret = rio_dma_transfer(filp, transaction.transfer_mode,
993 transaction.sync, dir, &transfer[i]);
995 if (unlikely(copy_to_user((void __user *)(uintptr_t)transaction.block,
997 transaction.count * sizeof(*transfer))))
1006 static int rio_mport_wait_for_async_dma(struct file *filp, void __user *arg)
1008 struct mport_cdev_priv *priv;
1009 struct rio_async_tx_wait w_param;
1010 struct mport_dma_req *req;
1011 dma_cookie_t cookie;
1017 priv = (struct mport_cdev_priv *)filp->private_data;
1019 if (unlikely(copy_from_user(&w_param, arg, sizeof(w_param))))
1022 cookie = w_param.token;
1023 if (w_param.timeout)
1024 tmo = msecs_to_jiffies(w_param.timeout);
1025 else /* Use default DMA timeout */
1026 tmo = msecs_to_jiffies(dma_timeout);
1028 spin_lock(&priv->req_lock);
1029 list_for_each_entry(req, &priv->async_list, node) {
1030 if (req->cookie == cookie) {
1031 list_del(&req->node);
1036 spin_unlock(&priv->req_lock);
1041 wret = wait_for_completion_interruptible_timeout(&req->req_comp, tmo);
1044 /* Timeout on wait occurred */
1045 rmcd_error("%s(%d) timed out waiting for ASYNC DMA_%s",
1046 current->comm, task_pid_nr(current),
1047 (req->dir == DMA_FROM_DEVICE)?"READ":"WRITE");
1050 } else if (wret == -ERESTARTSYS) {
1051 /* Wait_for_completion was interrupted by a signal but DMA may
1052 * be still in progress
1054 rmcd_error("%s(%d) wait for ASYNC DMA_%s was interrupted",
1055 current->comm, task_pid_nr(current),
1056 (req->dir == DMA_FROM_DEVICE)?"READ":"WRITE");
1061 if (req->status != DMA_COMPLETE) {
1062 /* DMA transaction completion signaled with transfer error */
1063 rmcd_error("%s(%d) ASYNC DMA_%s completion with status %d",
1064 current->comm, task_pid_nr(current),
1065 (req->dir == DMA_FROM_DEVICE)?"READ":"WRITE",
1071 if (req->status != DMA_IN_PROGRESS && req->status != DMA_PAUSED)
1072 kref_put(&req->refcount, dma_req_free);
1077 /* Return request back into async queue */
1078 spin_lock(&priv->req_lock);
1079 list_add_tail(&req->node, &priv->async_list);
1080 spin_unlock(&priv->req_lock);
1084 static int rio_mport_create_dma_mapping(struct mport_dev *md, struct file *filp,
1085 u64 size, struct rio_mport_mapping **mapping)
1087 struct rio_mport_mapping *map;
1089 map = kzalloc(sizeof(*map), GFP_KERNEL);
1093 map->virt_addr = dma_alloc_coherent(md->mport->dev.parent, size,
1094 &map->phys_addr, GFP_KERNEL);
1095 if (map->virt_addr == NULL) {
1104 kref_init(&map->ref);
1105 mutex_lock(&md->buf_mutex);
1106 list_add_tail(&map->node, &md->mappings);
1107 mutex_unlock(&md->buf_mutex);
1113 static int rio_mport_alloc_dma(struct file *filp, void __user *arg)
1115 struct mport_cdev_priv *priv = filp->private_data;
1116 struct mport_dev *md = priv->md;
1117 struct rio_dma_mem map;
1118 struct rio_mport_mapping *mapping = NULL;
1121 if (unlikely(copy_from_user(&map, arg, sizeof(map))))
1124 ret = rio_mport_create_dma_mapping(md, filp, map.length, &mapping);
1128 map.dma_handle = mapping->phys_addr;
1130 if (unlikely(copy_to_user(arg, &map, sizeof(map)))) {
1131 mutex_lock(&md->buf_mutex);
1132 kref_put(&mapping->ref, mport_release_mapping);
1133 mutex_unlock(&md->buf_mutex);
1140 static int rio_mport_free_dma(struct file *filp, void __user *arg)
1142 struct mport_cdev_priv *priv = filp->private_data;
1143 struct mport_dev *md = priv->md;
1146 struct rio_mport_mapping *map, *_map;
1148 if (copy_from_user(&handle, arg, sizeof(handle)))
1150 rmcd_debug(EXIT, "filp=%p", filp);
1152 mutex_lock(&md->buf_mutex);
1153 list_for_each_entry_safe(map, _map, &md->mappings, node) {
1154 if (map->dir == MAP_DMA && map->phys_addr == handle &&
1155 map->filp == filp) {
1156 kref_put(&map->ref, mport_release_mapping);
1161 mutex_unlock(&md->buf_mutex);
1163 if (ret == -EFAULT) {
1164 rmcd_debug(DMA, "ERR no matching mapping");
1171 static int rio_mport_transfer_ioctl(struct file *filp, void *arg)
1176 static int rio_mport_wait_for_async_dma(struct file *filp, void __user *arg)
1181 static int rio_mport_alloc_dma(struct file *filp, void __user *arg)
1186 static int rio_mport_free_dma(struct file *filp, void __user *arg)
1190 #endif /* CONFIG_RAPIDIO_DMA_ENGINE */
1193 * Inbound/outbound memory mapping functions
1197 rio_mport_create_inbound_mapping(struct mport_dev *md, struct file *filp,
1198 u64 raddr, u64 size,
1199 struct rio_mport_mapping **mapping)
1201 struct rio_mport *mport = md->mport;
1202 struct rio_mport_mapping *map;
1205 /* rio_map_inb_region() accepts u32 size */
1206 if (size > 0xffffffff)
1209 map = kzalloc(sizeof(*map), GFP_KERNEL);
1213 map->virt_addr = dma_alloc_coherent(mport->dev.parent, size,
1214 &map->phys_addr, GFP_KERNEL);
1215 if (map->virt_addr == NULL) {
1220 if (raddr == RIO_MAP_ANY_ADDR)
1221 raddr = map->phys_addr;
1222 ret = rio_map_inb_region(mport, map->phys_addr, raddr, (u32)size, 0);
1226 map->dir = MAP_INBOUND;
1227 map->rio_addr = raddr;
1231 kref_init(&map->ref);
1232 mutex_lock(&md->buf_mutex);
1233 list_add_tail(&map->node, &md->mappings);
1234 mutex_unlock(&md->buf_mutex);
1239 dma_free_coherent(mport->dev.parent, size,
1240 map->virt_addr, map->phys_addr);
1247 rio_mport_get_inbound_mapping(struct mport_dev *md, struct file *filp,
1248 u64 raddr, u64 size,
1249 struct rio_mport_mapping **mapping)
1251 struct rio_mport_mapping *map;
1254 if (raddr == RIO_MAP_ANY_ADDR)
1257 mutex_lock(&md->buf_mutex);
1258 list_for_each_entry(map, &md->mappings, node) {
1259 if (map->dir != MAP_INBOUND)
1261 if (raddr == map->rio_addr && size == map->size) {
1262 /* allow exact match only */
1266 } else if (raddr < (map->rio_addr + map->size - 1) &&
1267 (raddr + size) > map->rio_addr) {
1272 mutex_unlock(&md->buf_mutex);
1277 /* not found, create new */
1278 return rio_mport_create_inbound_mapping(md, filp, raddr, size, mapping);
1281 static int rio_mport_map_inbound(struct file *filp, void __user *arg)
1283 struct mport_cdev_priv *priv = filp->private_data;
1284 struct mport_dev *md = priv->md;
1285 struct rio_mmap map;
1286 struct rio_mport_mapping *mapping = NULL;
1289 if (!md->mport->ops->map_inb)
1290 return -EPROTONOSUPPORT;
1291 if (unlikely(copy_from_user(&map, arg, sizeof(map))))
1294 rmcd_debug(IBW, "%s filp=%p", dev_name(&priv->md->dev), filp);
1296 ret = rio_mport_get_inbound_mapping(md, filp, map.rio_addr,
1297 map.length, &mapping);
1301 map.handle = mapping->phys_addr;
1302 map.rio_addr = mapping->rio_addr;
1304 if (unlikely(copy_to_user(arg, &map, sizeof(map)))) {
1305 /* Delete mapping if it was created by this request */
1306 if (ret == 0 && mapping->filp == filp) {
1307 mutex_lock(&md->buf_mutex);
1308 kref_put(&mapping->ref, mport_release_mapping);
1309 mutex_unlock(&md->buf_mutex);
1318 * rio_mport_inbound_free() - unmap from RapidIO address space and free
1319 * previously allocated inbound DMA coherent buffer
1320 * @priv: driver private data
1321 * @arg: buffer handle returned by allocation routine
1323 static int rio_mport_inbound_free(struct file *filp, void __user *arg)
1325 struct mport_cdev_priv *priv = filp->private_data;
1326 struct mport_dev *md = priv->md;
1328 struct rio_mport_mapping *map, *_map;
1330 rmcd_debug(IBW, "%s filp=%p", dev_name(&priv->md->dev), filp);
1332 if (!md->mport->ops->unmap_inb)
1333 return -EPROTONOSUPPORT;
1335 if (copy_from_user(&handle, arg, sizeof(handle)))
1338 mutex_lock(&md->buf_mutex);
1339 list_for_each_entry_safe(map, _map, &md->mappings, node) {
1340 if (map->dir == MAP_INBOUND && map->phys_addr == handle) {
1341 if (map->filp == filp) {
1343 kref_put(&map->ref, mport_release_mapping);
1348 mutex_unlock(&md->buf_mutex);
1354 * maint_port_idx_get() - Get the port index of the mport instance
1355 * @priv: driver private data
1358 static int maint_port_idx_get(struct mport_cdev_priv *priv, void __user *arg)
1360 struct mport_dev *md = priv->md;
1361 u32 port_idx = md->mport->index;
1363 rmcd_debug(MPORT, "port_index=%d", port_idx);
1365 if (copy_to_user(arg, &port_idx, sizeof(port_idx)))
1371 static int rio_mport_add_event(struct mport_cdev_priv *priv,
1372 struct rio_event *event)
1376 if (!(priv->event_mask & event->header))
1379 spin_lock(&priv->fifo_lock);
1380 overflow = kfifo_avail(&priv->event_fifo) < sizeof(*event)
1381 || kfifo_in(&priv->event_fifo, (unsigned char *)event,
1382 sizeof(*event)) != sizeof(*event);
1383 spin_unlock(&priv->fifo_lock);
1385 wake_up_interruptible(&priv->event_rx_wait);
1388 dev_warn(&priv->md->dev, DRV_NAME ": event fifo overflow\n");
1395 static void rio_mport_doorbell_handler(struct rio_mport *mport, void *dev_id,
1396 u16 src, u16 dst, u16 info)
1398 struct mport_dev *data = dev_id;
1399 struct mport_cdev_priv *priv;
1400 struct rio_mport_db_filter *db_filter;
1401 struct rio_event event;
1404 event.header = RIO_DOORBELL;
1405 event.u.doorbell.rioid = src;
1406 event.u.doorbell.payload = info;
1409 spin_lock(&data->db_lock);
1410 list_for_each_entry(db_filter, &data->doorbells, data_node) {
1411 if (((db_filter->filter.rioid == RIO_INVALID_DESTID ||
1412 db_filter->filter.rioid == src)) &&
1413 info >= db_filter->filter.low &&
1414 info <= db_filter->filter.high) {
1415 priv = db_filter->priv;
1416 rio_mport_add_event(priv, &event);
1420 spin_unlock(&data->db_lock);
1423 dev_warn(&data->dev,
1424 "%s: spurious DB received from 0x%x, info=0x%04x\n",
1425 __func__, src, info);
1428 static int rio_mport_add_db_filter(struct mport_cdev_priv *priv,
1431 struct mport_dev *md = priv->md;
1432 struct rio_mport_db_filter *db_filter;
1433 struct rio_doorbell_filter filter;
1434 unsigned long flags;
1437 if (copy_from_user(&filter, arg, sizeof(filter)))
1440 if (filter.low > filter.high)
1443 ret = rio_request_inb_dbell(md->mport, md, filter.low, filter.high,
1444 rio_mport_doorbell_handler);
1446 rmcd_error("%s failed to register IBDB, err=%d",
1447 dev_name(&md->dev), ret);
1451 db_filter = kzalloc(sizeof(*db_filter), GFP_KERNEL);
1452 if (db_filter == NULL) {
1453 rio_release_inb_dbell(md->mport, filter.low, filter.high);
1457 db_filter->filter = filter;
1458 db_filter->priv = priv;
1459 spin_lock_irqsave(&md->db_lock, flags);
1460 list_add_tail(&db_filter->priv_node, &priv->db_filters);
1461 list_add_tail(&db_filter->data_node, &md->doorbells);
1462 spin_unlock_irqrestore(&md->db_lock, flags);
1467 static void rio_mport_delete_db_filter(struct rio_mport_db_filter *db_filter)
1469 list_del(&db_filter->data_node);
1470 list_del(&db_filter->priv_node);
1474 static int rio_mport_remove_db_filter(struct mport_cdev_priv *priv,
1477 struct rio_mport_db_filter *db_filter;
1478 struct rio_doorbell_filter filter;
1479 unsigned long flags;
1482 if (copy_from_user(&filter, arg, sizeof(filter)))
1485 if (filter.low > filter.high)
1488 spin_lock_irqsave(&priv->md->db_lock, flags);
1489 list_for_each_entry(db_filter, &priv->db_filters, priv_node) {
1490 if (db_filter->filter.rioid == filter.rioid &&
1491 db_filter->filter.low == filter.low &&
1492 db_filter->filter.high == filter.high) {
1493 rio_mport_delete_db_filter(db_filter);
1498 spin_unlock_irqrestore(&priv->md->db_lock, flags);
1501 rio_release_inb_dbell(priv->md->mport, filter.low, filter.high);
1506 static int rio_mport_match_pw(union rio_pw_msg *msg,
1507 struct rio_pw_filter *filter)
1509 if ((msg->em.comptag & filter->mask) < filter->low ||
1510 (msg->em.comptag & filter->mask) > filter->high)
1515 static int rio_mport_pw_handler(struct rio_mport *mport, void *context,
1516 union rio_pw_msg *msg, int step)
1518 struct mport_dev *md = context;
1519 struct mport_cdev_priv *priv;
1520 struct rio_mport_pw_filter *pw_filter;
1521 struct rio_event event;
1524 event.header = RIO_PORTWRITE;
1525 memcpy(event.u.portwrite.payload, msg->raw, RIO_PW_MSG_SIZE);
1528 spin_lock(&md->pw_lock);
1529 list_for_each_entry(pw_filter, &md->portwrites, md_node) {
1530 if (rio_mport_match_pw(msg, &pw_filter->filter)) {
1531 priv = pw_filter->priv;
1532 rio_mport_add_event(priv, &event);
1536 spin_unlock(&md->pw_lock);
1539 printk_ratelimited(KERN_WARNING DRV_NAME
1540 ": mport%d received spurious PW from 0x%08x\n",
1541 mport->id, msg->em.comptag);
1547 static int rio_mport_add_pw_filter(struct mport_cdev_priv *priv,
1550 struct mport_dev *md = priv->md;
1551 struct rio_mport_pw_filter *pw_filter;
1552 struct rio_pw_filter filter;
1553 unsigned long flags;
1556 if (copy_from_user(&filter, arg, sizeof(filter)))
1559 pw_filter = kzalloc(sizeof(*pw_filter), GFP_KERNEL);
1560 if (pw_filter == NULL)
1563 pw_filter->filter = filter;
1564 pw_filter->priv = priv;
1565 spin_lock_irqsave(&md->pw_lock, flags);
1566 if (list_empty(&md->portwrites))
1568 list_add_tail(&pw_filter->priv_node, &priv->pw_filters);
1569 list_add_tail(&pw_filter->md_node, &md->portwrites);
1570 spin_unlock_irqrestore(&md->pw_lock, flags);
1575 ret = rio_add_mport_pw_handler(md->mport, md,
1576 rio_mport_pw_handler);
1579 "%s: failed to add IB_PW handler, err=%d\n",
1583 rio_pw_enable(md->mport, 1);
1589 static void rio_mport_delete_pw_filter(struct rio_mport_pw_filter *pw_filter)
1591 list_del(&pw_filter->md_node);
1592 list_del(&pw_filter->priv_node);
1596 static int rio_mport_match_pw_filter(struct rio_pw_filter *a,
1597 struct rio_pw_filter *b)
1599 if ((a->mask == b->mask) && (a->low == b->low) && (a->high == b->high))
1604 static int rio_mport_remove_pw_filter(struct mport_cdev_priv *priv,
1607 struct mport_dev *md = priv->md;
1608 struct rio_mport_pw_filter *pw_filter;
1609 struct rio_pw_filter filter;
1610 unsigned long flags;
1614 if (copy_from_user(&filter, arg, sizeof(filter)))
1617 spin_lock_irqsave(&md->pw_lock, flags);
1618 list_for_each_entry(pw_filter, &priv->pw_filters, priv_node) {
1619 if (rio_mport_match_pw_filter(&pw_filter->filter, &filter)) {
1620 rio_mport_delete_pw_filter(pw_filter);
1626 if (list_empty(&md->portwrites))
1628 spin_unlock_irqrestore(&md->pw_lock, flags);
1631 rio_del_mport_pw_handler(md->mport, priv->md,
1632 rio_mport_pw_handler);
1633 rio_pw_enable(md->mport, 0);
1640 * rio_release_dev - release routine for kernel RIO device object
1641 * @dev: kernel device object associated with a RIO device structure
1643 * Frees a RIO device struct associated a RIO device struct.
1644 * The RIO device struct is freed.
1646 static void rio_release_dev(struct device *dev)
1648 struct rio_dev *rdev;
1650 rdev = to_rio_dev(dev);
1651 pr_info(DRV_PREFIX "%s: %s\n", __func__, rio_name(rdev));
1656 static void rio_release_net(struct device *dev)
1658 struct rio_net *net;
1660 net = to_rio_net(dev);
1661 rmcd_debug(RDEV, "net_%d", net->id);
1667 * rio_mport_add_riodev - creates a kernel RIO device object
1669 * Allocates a RIO device data structure and initializes required fields based
1670 * on device's configuration space contents.
1671 * If the device has switch capabilities, then a switch specific portion is
1672 * allocated and configured.
1674 static int rio_mport_add_riodev(struct mport_cdev_priv *priv,
1677 struct mport_dev *md = priv->md;
1678 struct rio_rdev_info dev_info;
1679 struct rio_dev *rdev;
1680 struct rio_switch *rswitch = NULL;
1681 struct rio_mport *mport;
1689 if (copy_from_user(&dev_info, arg, sizeof(dev_info)))
1692 rmcd_debug(RDEV, "name:%s ct:0x%x did:0x%x hc:0x%x", dev_info.name,
1693 dev_info.comptag, dev_info.destid, dev_info.hopcount);
1695 if (bus_find_device_by_name(&rio_bus_type, NULL, dev_info.name)) {
1696 rmcd_debug(RDEV, "device %s already exists", dev_info.name);
1700 size = sizeof(*rdev);
1702 destid = dev_info.destid;
1703 hopcount = dev_info.hopcount;
1705 if (rio_mport_read_config_32(mport, destid, hopcount,
1706 RIO_PEF_CAR, &rval))
1709 if (rval & RIO_PEF_SWITCH) {
1710 rio_mport_read_config_32(mport, destid, hopcount,
1711 RIO_SWP_INFO_CAR, &swpinfo);
1712 size += (RIO_GET_TOTAL_PORTS(swpinfo) *
1713 sizeof(rswitch->nextdev[0])) + sizeof(*rswitch);
1716 rdev = kzalloc(size, GFP_KERNEL);
1720 if (mport->net == NULL) {
1721 struct rio_net *net;
1723 net = rio_alloc_net(mport);
1726 rmcd_debug(RDEV, "failed to allocate net object");
1730 net->id = mport->id;
1732 dev_set_name(&net->dev, "rnet_%d", net->id);
1733 net->dev.parent = &mport->dev;
1734 net->dev.release = rio_release_net;
1735 err = rio_add_net(net);
1737 rmcd_debug(RDEV, "failed to register net, err=%d", err);
1743 rdev->net = mport->net;
1745 rdev->swpinfo = swpinfo;
1746 rio_mport_read_config_32(mport, destid, hopcount,
1747 RIO_DEV_ID_CAR, &rval);
1748 rdev->did = rval >> 16;
1749 rdev->vid = rval & 0xffff;
1750 rio_mport_read_config_32(mport, destid, hopcount, RIO_DEV_INFO_CAR,
1752 rio_mport_read_config_32(mport, destid, hopcount, RIO_ASM_ID_CAR,
1754 rdev->asm_did = rval >> 16;
1755 rdev->asm_vid = rval & 0xffff;
1756 rio_mport_read_config_32(mport, destid, hopcount, RIO_ASM_INFO_CAR,
1758 rdev->asm_rev = rval >> 16;
1760 if (rdev->pef & RIO_PEF_EXT_FEATURES) {
1761 rdev->efptr = rval & 0xffff;
1762 rdev->phys_efptr = rio_mport_get_physefb(mport, 0, destid,
1763 hopcount, &rdev->phys_rmap);
1765 rdev->em_efptr = rio_mport_get_feature(mport, 0, destid,
1766 hopcount, RIO_EFB_ERR_MGMNT);
1769 rio_mport_read_config_32(mport, destid, hopcount, RIO_SRC_OPS_CAR,
1771 rio_mport_read_config_32(mport, destid, hopcount, RIO_DST_OPS_CAR,
1774 rdev->comp_tag = dev_info.comptag;
1775 rdev->destid = destid;
1776 /* hopcount is stored as specified by a caller, regardles of EP or SW */
1777 rdev->hopcount = hopcount;
1779 if (rdev->pef & RIO_PEF_SWITCH) {
1780 rswitch = rdev->rswitch;
1781 rswitch->route_table = NULL;
1784 if (strlen(dev_info.name))
1785 dev_set_name(&rdev->dev, "%s", dev_info.name);
1786 else if (rdev->pef & RIO_PEF_SWITCH)
1787 dev_set_name(&rdev->dev, "%02x:s:%04x", mport->id,
1788 rdev->comp_tag & RIO_CTAG_UDEVID);
1790 dev_set_name(&rdev->dev, "%02x:e:%04x", mport->id,
1791 rdev->comp_tag & RIO_CTAG_UDEVID);
1793 INIT_LIST_HEAD(&rdev->net_list);
1794 rdev->dev.parent = &mport->net->dev;
1795 rio_attach_device(rdev);
1796 rdev->dev.release = rio_release_dev;
1798 if (rdev->dst_ops & RIO_DST_OPS_DOORBELL)
1799 rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE],
1801 err = rio_add_device(rdev);
1812 static int rio_mport_del_riodev(struct mport_cdev_priv *priv, void __user *arg)
1814 struct rio_rdev_info dev_info;
1815 struct rio_dev *rdev = NULL;
1817 struct rio_mport *mport;
1818 struct rio_net *net;
1820 if (copy_from_user(&dev_info, arg, sizeof(dev_info)))
1823 mport = priv->md->mport;
1825 /* If device name is specified, removal by name has priority */
1826 if (strlen(dev_info.name)) {
1827 dev = bus_find_device_by_name(&rio_bus_type, NULL,
1830 rdev = to_rio_dev(dev);
1833 rdev = rio_get_comptag(dev_info.comptag, rdev);
1834 if (rdev && rdev->dev.parent == &mport->net->dev &&
1835 rdev->destid == dev_info.destid &&
1836 rdev->hopcount == dev_info.hopcount)
1843 "device name:%s ct:0x%x did:0x%x hc:0x%x not found",
1844 dev_info.name, dev_info.comptag, dev_info.destid,
1851 rio_del_device(rdev, RIO_DEVICE_SHUTDOWN);
1853 if (list_empty(&net->devices)) {
1862 * Mport cdev management
1866 * mport_cdev_open() - Open character device (mport)
1868 static int mport_cdev_open(struct inode *inode, struct file *filp)
1871 int minor = iminor(inode);
1872 struct mport_dev *chdev;
1873 struct mport_cdev_priv *priv;
1875 /* Test for valid device */
1876 if (minor >= RIO_MAX_MPORTS) {
1877 rmcd_error("Invalid minor device number");
1881 chdev = container_of(inode->i_cdev, struct mport_dev, cdev);
1883 rmcd_debug(INIT, "%s filp=%p", dev_name(&chdev->dev), filp);
1885 if (atomic_read(&chdev->active) == 0)
1888 get_device(&chdev->dev);
1890 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1892 put_device(&chdev->dev);
1898 mutex_lock(&chdev->file_mutex);
1899 list_add_tail(&priv->list, &chdev->file_list);
1900 mutex_unlock(&chdev->file_mutex);
1902 INIT_LIST_HEAD(&priv->db_filters);
1903 INIT_LIST_HEAD(&priv->pw_filters);
1904 spin_lock_init(&priv->fifo_lock);
1905 init_waitqueue_head(&priv->event_rx_wait);
1906 ret = kfifo_alloc(&priv->event_fifo,
1907 sizeof(struct rio_event) * MPORT_EVENT_DEPTH,
1910 dev_err(&chdev->dev, DRV_NAME ": kfifo_alloc failed\n");
1915 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
1916 INIT_LIST_HEAD(&priv->async_list);
1917 spin_lock_init(&priv->req_lock);
1918 mutex_init(&priv->dma_lock);
1921 filp->private_data = priv;
1929 static int mport_cdev_fasync(int fd, struct file *filp, int mode)
1931 struct mport_cdev_priv *priv = filp->private_data;
1933 return fasync_helper(fd, filp, mode, &priv->async_queue);
1936 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
1937 static void mport_cdev_release_dma(struct file *filp)
1939 struct mport_cdev_priv *priv = filp->private_data;
1940 struct mport_dev *md;
1941 struct mport_dma_req *req, *req_next;
1942 unsigned long tmo = msecs_to_jiffies(dma_timeout);
1946 rmcd_debug(EXIT, "from filp=%p %s(%d)",
1947 filp, current->comm, task_pid_nr(current));
1950 rmcd_debug(EXIT, "No DMA channel for filp=%p", filp);
1956 spin_lock(&priv->req_lock);
1957 if (!list_empty(&priv->async_list)) {
1958 rmcd_debug(EXIT, "async list not empty filp=%p %s(%d)",
1959 filp, current->comm, task_pid_nr(current));
1960 list_splice_init(&priv->async_list, &list);
1962 spin_unlock(&priv->req_lock);
1964 if (!list_empty(&list)) {
1965 rmcd_debug(EXIT, "temp list not empty");
1966 list_for_each_entry_safe(req, req_next, &list, node) {
1967 rmcd_debug(EXIT, "free req->filp=%p cookie=%d compl=%s",
1968 req->filp, req->cookie,
1969 completion_done(&req->req_comp)?"yes":"no");
1970 list_del(&req->node);
1971 kref_put(&req->refcount, dma_req_free);
1975 put_dma_channel(priv);
1976 wret = wait_for_completion_interruptible_timeout(&priv->comp, tmo);
1979 rmcd_error("%s(%d) failed waiting for DMA release err=%ld",
1980 current->comm, task_pid_nr(current), wret);
1983 if (priv->dmach != priv->md->dma_chan) {
1984 rmcd_debug(EXIT, "Release DMA channel for filp=%p %s(%d)",
1985 filp, current->comm, task_pid_nr(current));
1986 rio_release_dma(priv->dmach);
1988 rmcd_debug(EXIT, "Adjust default DMA channel refcount");
1989 kref_put(&md->dma_ref, mport_release_def_dma);
1995 #define mport_cdev_release_dma(priv) do {} while (0)
1999 * mport_cdev_release() - Release character device
2001 static int mport_cdev_release(struct inode *inode, struct file *filp)
2003 struct mport_cdev_priv *priv = filp->private_data;
2004 struct mport_dev *chdev;
2005 struct rio_mport_pw_filter *pw_filter, *pw_filter_next;
2006 struct rio_mport_db_filter *db_filter, *db_filter_next;
2007 struct rio_mport_mapping *map, *_map;
2008 unsigned long flags;
2010 rmcd_debug(EXIT, "%s filp=%p", dev_name(&priv->md->dev), filp);
2013 mport_cdev_release_dma(filp);
2015 priv->event_mask = 0;
2017 spin_lock_irqsave(&chdev->pw_lock, flags);
2018 if (!list_empty(&priv->pw_filters)) {
2019 list_for_each_entry_safe(pw_filter, pw_filter_next,
2020 &priv->pw_filters, priv_node)
2021 rio_mport_delete_pw_filter(pw_filter);
2023 spin_unlock_irqrestore(&chdev->pw_lock, flags);
2025 spin_lock_irqsave(&chdev->db_lock, flags);
2026 list_for_each_entry_safe(db_filter, db_filter_next,
2027 &priv->db_filters, priv_node) {
2028 rio_mport_delete_db_filter(db_filter);
2030 spin_unlock_irqrestore(&chdev->db_lock, flags);
2032 kfifo_free(&priv->event_fifo);
2034 mutex_lock(&chdev->buf_mutex);
2035 list_for_each_entry_safe(map, _map, &chdev->mappings, node) {
2036 if (map->filp == filp) {
2037 rmcd_debug(EXIT, "release mapping %p filp=%p",
2038 map->virt_addr, filp);
2039 kref_put(&map->ref, mport_release_mapping);
2042 mutex_unlock(&chdev->buf_mutex);
2044 mport_cdev_fasync(-1, filp, 0);
2045 filp->private_data = NULL;
2046 mutex_lock(&chdev->file_mutex);
2047 list_del(&priv->list);
2048 mutex_unlock(&chdev->file_mutex);
2049 put_device(&chdev->dev);
2055 * mport_cdev_ioctl() - IOCTLs for character device
2057 static long mport_cdev_ioctl(struct file *filp,
2058 unsigned int cmd, unsigned long arg)
2061 struct mport_cdev_priv *data = filp->private_data;
2062 struct mport_dev *md = data->md;
2064 if (atomic_read(&md->active) == 0)
2068 case RIO_MPORT_MAINT_READ_LOCAL:
2069 return rio_mport_maint_rd(data, (void __user *)arg, 1);
2070 case RIO_MPORT_MAINT_WRITE_LOCAL:
2071 return rio_mport_maint_wr(data, (void __user *)arg, 1);
2072 case RIO_MPORT_MAINT_READ_REMOTE:
2073 return rio_mport_maint_rd(data, (void __user *)arg, 0);
2074 case RIO_MPORT_MAINT_WRITE_REMOTE:
2075 return rio_mport_maint_wr(data, (void __user *)arg, 0);
2076 case RIO_MPORT_MAINT_HDID_SET:
2077 return maint_hdid_set(data, (void __user *)arg);
2078 case RIO_MPORT_MAINT_COMPTAG_SET:
2079 return maint_comptag_set(data, (void __user *)arg);
2080 case RIO_MPORT_MAINT_PORT_IDX_GET:
2081 return maint_port_idx_get(data, (void __user *)arg);
2082 case RIO_MPORT_GET_PROPERTIES:
2083 md->properties.hdid = md->mport->host_deviceid;
2084 if (copy_to_user((void __user *)arg, &(md->properties),
2085 sizeof(md->properties)))
2088 case RIO_ENABLE_DOORBELL_RANGE:
2089 return rio_mport_add_db_filter(data, (void __user *)arg);
2090 case RIO_DISABLE_DOORBELL_RANGE:
2091 return rio_mport_remove_db_filter(data, (void __user *)arg);
2092 case RIO_ENABLE_PORTWRITE_RANGE:
2093 return rio_mport_add_pw_filter(data, (void __user *)arg);
2094 case RIO_DISABLE_PORTWRITE_RANGE:
2095 return rio_mport_remove_pw_filter(data, (void __user *)arg);
2096 case RIO_SET_EVENT_MASK:
2097 data->event_mask = (u32)arg;
2099 case RIO_GET_EVENT_MASK:
2100 if (copy_to_user((void __user *)arg, &data->event_mask,
2104 case RIO_MAP_OUTBOUND:
2105 return rio_mport_obw_map(filp, (void __user *)arg);
2106 case RIO_MAP_INBOUND:
2107 return rio_mport_map_inbound(filp, (void __user *)arg);
2108 case RIO_UNMAP_OUTBOUND:
2109 return rio_mport_obw_free(filp, (void __user *)arg);
2110 case RIO_UNMAP_INBOUND:
2111 return rio_mport_inbound_free(filp, (void __user *)arg);
2113 return rio_mport_alloc_dma(filp, (void __user *)arg);
2115 return rio_mport_free_dma(filp, (void __user *)arg);
2116 case RIO_WAIT_FOR_ASYNC:
2117 return rio_mport_wait_for_async_dma(filp, (void __user *)arg);
2119 return rio_mport_transfer_ioctl(filp, (void __user *)arg);
2121 return rio_mport_add_riodev(data, (void __user *)arg);
2123 return rio_mport_del_riodev(data, (void __user *)arg);
2132 * mport_release_mapping - free mapping resources and info structure
2133 * @ref: a pointer to the kref within struct rio_mport_mapping
2135 * NOTE: Shall be called while holding buf_mutex.
2137 static void mport_release_mapping(struct kref *ref)
2139 struct rio_mport_mapping *map =
2140 container_of(ref, struct rio_mport_mapping, ref);
2141 struct rio_mport *mport = map->md->mport;
2143 rmcd_debug(MMAP, "type %d mapping @ %p (phys = %pad) for %s",
2144 map->dir, map->virt_addr,
2145 &map->phys_addr, mport->name);
2147 list_del(&map->node);
2151 rio_unmap_inb_region(mport, map->phys_addr);
2153 dma_free_coherent(mport->dev.parent, map->size,
2154 map->virt_addr, map->phys_addr);
2157 rio_unmap_outb_region(mport, map->rioid, map->rio_addr);
2163 static void mport_mm_open(struct vm_area_struct *vma)
2165 struct rio_mport_mapping *map = vma->vm_private_data;
2167 rmcd_debug(MMAP, "%pad", &map->phys_addr);
2168 kref_get(&map->ref);
2171 static void mport_mm_close(struct vm_area_struct *vma)
2173 struct rio_mport_mapping *map = vma->vm_private_data;
2175 rmcd_debug(MMAP, "%pad", &map->phys_addr);
2176 mutex_lock(&map->md->buf_mutex);
2177 kref_put(&map->ref, mport_release_mapping);
2178 mutex_unlock(&map->md->buf_mutex);
2181 static const struct vm_operations_struct vm_ops = {
2182 .open = mport_mm_open,
2183 .close = mport_mm_close,
2186 static int mport_cdev_mmap(struct file *filp, struct vm_area_struct *vma)
2188 struct mport_cdev_priv *priv = filp->private_data;
2189 struct mport_dev *md;
2190 size_t size = vma->vm_end - vma->vm_start;
2192 unsigned long offset;
2194 struct rio_mport_mapping *map;
2196 rmcd_debug(MMAP, "0x%x bytes at offset 0x%lx",
2197 (unsigned int)size, vma->vm_pgoff);
2200 baddr = ((dma_addr_t)vma->vm_pgoff << PAGE_SHIFT);
2202 mutex_lock(&md->buf_mutex);
2203 list_for_each_entry(map, &md->mappings, node) {
2204 if (baddr >= map->phys_addr &&
2205 baddr < (map->phys_addr + map->size)) {
2210 mutex_unlock(&md->buf_mutex);
2215 offset = baddr - map->phys_addr;
2217 if (size + offset > map->size)
2220 vma->vm_pgoff = offset >> PAGE_SHIFT;
2221 rmcd_debug(MMAP, "MMAP adjusted offset = 0x%lx", vma->vm_pgoff);
2223 if (map->dir == MAP_INBOUND || map->dir == MAP_DMA)
2224 ret = dma_mmap_coherent(md->mport->dev.parent, vma,
2225 map->virt_addr, map->phys_addr, map->size);
2226 else if (map->dir == MAP_OUTBOUND) {
2227 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
2228 ret = vm_iomap_memory(vma, map->phys_addr, map->size);
2230 rmcd_error("Attempt to mmap unsupported mapping type");
2235 vma->vm_private_data = map;
2236 vma->vm_ops = &vm_ops;
2239 rmcd_error("MMAP exit with err=%d", ret);
2245 static __poll_t mport_cdev_poll(struct file *filp, poll_table *wait)
2247 struct mport_cdev_priv *priv = filp->private_data;
2249 poll_wait(filp, &priv->event_rx_wait, wait);
2250 if (kfifo_len(&priv->event_fifo))
2251 return EPOLLIN | EPOLLRDNORM;
2256 static ssize_t mport_read(struct file *filp, char __user *buf, size_t count,
2259 struct mport_cdev_priv *priv = filp->private_data;
2266 if (kfifo_is_empty(&priv->event_fifo) &&
2267 (filp->f_flags & O_NONBLOCK))
2270 if (count % sizeof(struct rio_event))
2273 ret = wait_event_interruptible(priv->event_rx_wait,
2274 kfifo_len(&priv->event_fifo) != 0);
2278 while (ret < count) {
2279 if (kfifo_to_user(&priv->event_fifo, buf,
2280 sizeof(struct rio_event), &copied))
2289 static ssize_t mport_write(struct file *filp, const char __user *buf,
2290 size_t count, loff_t *ppos)
2292 struct mport_cdev_priv *priv = filp->private_data;
2293 struct rio_mport *mport = priv->md->mport;
2294 struct rio_event event;
2300 if (count % sizeof(event))
2304 while ((count - len) >= (int)sizeof(event)) {
2305 if (copy_from_user(&event, buf, sizeof(event)))
2308 if (event.header != RIO_DOORBELL)
2311 ret = rio_mport_send_doorbell(mport,
2312 event.u.doorbell.rioid,
2313 event.u.doorbell.payload);
2317 len += sizeof(event);
2318 buf += sizeof(event);
2324 static const struct file_operations mport_fops = {
2325 .owner = THIS_MODULE,
2326 .open = mport_cdev_open,
2327 .release = mport_cdev_release,
2328 .poll = mport_cdev_poll,
2330 .write = mport_write,
2331 .mmap = mport_cdev_mmap,
2332 .fasync = mport_cdev_fasync,
2333 .unlocked_ioctl = mport_cdev_ioctl
2337 * Character device management
2340 static void mport_device_release(struct device *dev)
2342 struct mport_dev *md;
2344 rmcd_debug(EXIT, "%s", dev_name(dev));
2345 md = container_of(dev, struct mport_dev, dev);
2350 * mport_cdev_add() - Create mport_dev from rio_mport
2351 * @mport: RapidIO master port
2353 static struct mport_dev *mport_cdev_add(struct rio_mport *mport)
2356 struct mport_dev *md;
2357 struct rio_mport_attr attr;
2359 md = kzalloc(sizeof(*md), GFP_KERNEL);
2361 rmcd_error("Unable allocate a device object");
2366 mutex_init(&md->buf_mutex);
2367 mutex_init(&md->file_mutex);
2368 INIT_LIST_HEAD(&md->file_list);
2370 device_initialize(&md->dev);
2371 md->dev.devt = MKDEV(MAJOR(dev_number), mport->id);
2372 md->dev.class = dev_class;
2373 md->dev.parent = &mport->dev;
2374 md->dev.release = mport_device_release;
2375 dev_set_name(&md->dev, DEV_NAME "%d", mport->id);
2376 atomic_set(&md->active, 1);
2378 cdev_init(&md->cdev, &mport_fops);
2379 md->cdev.owner = THIS_MODULE;
2381 ret = cdev_device_add(&md->cdev, &md->dev);
2383 rmcd_error("Failed to register mport %d (err=%d)",
2388 INIT_LIST_HEAD(&md->doorbells);
2389 spin_lock_init(&md->db_lock);
2390 INIT_LIST_HEAD(&md->portwrites);
2391 spin_lock_init(&md->pw_lock);
2392 INIT_LIST_HEAD(&md->mappings);
2394 md->properties.id = mport->id;
2395 md->properties.sys_size = mport->sys_size;
2396 md->properties.hdid = mport->host_deviceid;
2397 md->properties.index = mport->index;
2399 /* The transfer_mode property will be returned through mport query
2402 #ifdef CONFIG_FSL_RIO /* for now: only on Freescale's SoCs */
2403 md->properties.transfer_mode |= RIO_TRANSFER_MODE_MAPPED;
2405 md->properties.transfer_mode |= RIO_TRANSFER_MODE_TRANSFER;
2407 ret = rio_query_mport(mport, &attr);
2409 md->properties.flags = attr.flags;
2410 md->properties.link_speed = attr.link_speed;
2411 md->properties.link_width = attr.link_width;
2412 md->properties.dma_max_sge = attr.dma_max_sge;
2413 md->properties.dma_max_size = attr.dma_max_size;
2414 md->properties.dma_align = attr.dma_align;
2415 md->properties.cap_sys_size = 0;
2416 md->properties.cap_transfer_mode = 0;
2417 md->properties.cap_addr_size = 0;
2419 pr_info(DRV_PREFIX "Failed to obtain info for %s cdev(%d:%d)\n",
2420 mport->name, MAJOR(dev_number), mport->id);
2422 mutex_lock(&mport_devs_lock);
2423 list_add_tail(&md->node, &mport_devs);
2424 mutex_unlock(&mport_devs_lock);
2426 pr_info(DRV_PREFIX "Added %s cdev(%d:%d)\n",
2427 mport->name, MAJOR(dev_number), mport->id);
2432 put_device(&md->dev);
2437 * mport_cdev_terminate_dma() - Stop all active DMA data transfers and release
2438 * associated DMA channels.
2440 static void mport_cdev_terminate_dma(struct mport_dev *md)
2442 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
2443 struct mport_cdev_priv *client;
2445 rmcd_debug(DMA, "%s", dev_name(&md->dev));
2447 mutex_lock(&md->file_mutex);
2448 list_for_each_entry(client, &md->file_list, list) {
2449 if (client->dmach) {
2450 dmaengine_terminate_all(client->dmach);
2451 rio_release_dma(client->dmach);
2454 mutex_unlock(&md->file_mutex);
2457 dmaengine_terminate_all(md->dma_chan);
2458 rio_release_dma(md->dma_chan);
2459 md->dma_chan = NULL;
2466 * mport_cdev_kill_fasync() - Send SIGIO signal to all processes with open
2469 static int mport_cdev_kill_fasync(struct mport_dev *md)
2471 unsigned int files = 0;
2472 struct mport_cdev_priv *client;
2474 mutex_lock(&md->file_mutex);
2475 list_for_each_entry(client, &md->file_list, list) {
2476 if (client->async_queue)
2477 kill_fasync(&client->async_queue, SIGIO, POLL_HUP);
2480 mutex_unlock(&md->file_mutex);
2485 * mport_cdev_remove() - Remove mport character device
2486 * @dev: Mport device to remove
2488 static void mport_cdev_remove(struct mport_dev *md)
2490 struct rio_mport_mapping *map, *_map;
2492 rmcd_debug(EXIT, "Remove %s cdev", md->mport->name);
2493 atomic_set(&md->active, 0);
2494 mport_cdev_terminate_dma(md);
2495 rio_del_mport_pw_handler(md->mport, md, rio_mport_pw_handler);
2496 cdev_device_del(&md->cdev, &md->dev);
2497 mport_cdev_kill_fasync(md);
2499 /* TODO: do we need to give clients some time to close file
2500 * descriptors? Simple wait for XX, or kref?
2504 * Release DMA buffers allocated for the mport device.
2505 * Disable associated inbound Rapidio requests mapping if applicable.
2507 mutex_lock(&md->buf_mutex);
2508 list_for_each_entry_safe(map, _map, &md->mappings, node) {
2509 kref_put(&map->ref, mport_release_mapping);
2511 mutex_unlock(&md->buf_mutex);
2513 if (!list_empty(&md->mappings))
2514 rmcd_warn("WARNING: %s pending mappings on removal",
2517 rio_release_inb_dbell(md->mport, 0, 0x0fff);
2519 put_device(&md->dev);
2523 * RIO rio_mport_interface driver
2527 * mport_add_mport() - Add rio_mport from LDM device struct
2528 * @dev: Linux device model struct
2529 * @class_intf: Linux class_interface
2531 static int mport_add_mport(struct device *dev,
2532 struct class_interface *class_intf)
2534 struct rio_mport *mport = NULL;
2535 struct mport_dev *chdev = NULL;
2537 mport = to_rio_mport(dev);
2541 chdev = mport_cdev_add(mport);
2549 * mport_remove_mport() - Remove rio_mport from global list
2550 * TODO remove device from global mport_dev list
2552 static void mport_remove_mport(struct device *dev,
2553 struct class_interface *class_intf)
2555 struct rio_mport *mport = NULL;
2556 struct mport_dev *chdev;
2559 mport = to_rio_mport(dev);
2560 rmcd_debug(EXIT, "Remove %s", mport->name);
2562 mutex_lock(&mport_devs_lock);
2563 list_for_each_entry(chdev, &mport_devs, node) {
2564 if (chdev->mport->id == mport->id) {
2565 atomic_set(&chdev->active, 0);
2566 list_del(&chdev->node);
2571 mutex_unlock(&mport_devs_lock);
2574 mport_cdev_remove(chdev);
2577 /* the rio_mport_interface is used to handle local mport devices */
2578 static struct class_interface rio_mport_interface __refdata = {
2579 .class = &rio_mport_class,
2580 .add_dev = mport_add_mport,
2581 .remove_dev = mport_remove_mport,
2585 * Linux kernel module
2589 * mport_init - Driver module loading
2591 static int __init mport_init(void)
2595 /* Create device class needed by udev */
2596 dev_class = class_create(THIS_MODULE, DRV_NAME);
2597 if (IS_ERR(dev_class)) {
2598 rmcd_error("Unable to create " DRV_NAME " class");
2599 return PTR_ERR(dev_class);
2602 ret = alloc_chrdev_region(&dev_number, 0, RIO_MAX_MPORTS, DRV_NAME);
2606 rmcd_debug(INIT, "Registered class with major=%d", MAJOR(dev_number));
2608 /* Register to rio_mport_interface */
2609 ret = class_interface_register(&rio_mport_interface);
2611 rmcd_error("class_interface_register() failed, err=%d", ret);
2618 unregister_chrdev_region(dev_number, RIO_MAX_MPORTS);
2620 class_destroy(dev_class);
2625 * mport_exit - Driver module unloading
2627 static void __exit mport_exit(void)
2629 class_interface_unregister(&rio_mport_interface);
2630 class_destroy(dev_class);
2631 unregister_chrdev_region(dev_number, RIO_MAX_MPORTS);
2634 module_init(mport_init);
2635 module_exit(mport_exit);