// SPDX-License-Identifier: GPL-2.0-or-later /* * rio_cm - RapidIO Channelized Messaging Driver * * Copyright 2013-2016 Integrated Device Technology, Inc. * Copyright (c) 2015, Prodrive Technologies * Copyright (c) 2015, RapidIO Trade Association */ #include <linux/module.h> #include <linux/kernel.h> #include <linux/dma-mapping.h> #include <linux/delay.h> #include <linux/sched.h> #include <linux/rio.h> #include <linux/rio_drv.h> #include <linux/slab.h> #include <linux/idr.h> #include <linux/interrupt.h> #include <linux/cdev.h> #include <linux/fs.h> #include <linux/poll.h> #include <linux/reboot.h> #include <linux/bitops.h> #include <linux/printk.h> #include <linux/rio_cm_cdev.h> #define DRV_NAME … #define DRV_VERSION … #define DRV_AUTHOR … #define DRV_DESC … #define DEV_NAME … /* Debug output filtering masks */ enum { … }; #ifdef DEBUG #define riocm_debug(level, fmt, arg...) … #else #define riocm_debug … #endif #define riocm_warn(fmt, arg...) … #define riocm_error(fmt, arg...) … static int cmbox = …; module_param(cmbox, int, S_IRUGO); MODULE_PARM_DESC(…) …; static int chstart = …; module_param(chstart, int, S_IRUGO); MODULE_PARM_DESC(…) …; #ifdef DEBUG static u32 dbg_level = …; module_param(dbg_level, uint, S_IWUSR | S_IRUGO); MODULE_PARM_DESC(…) …; #endif MODULE_AUTHOR(…); MODULE_DESCRIPTION(…); MODULE_LICENSE(…) …; MODULE_VERSION(…); #define RIOCM_TX_RING_SIZE … #define RIOCM_RX_RING_SIZE … #define RIOCM_CONNECT_TO … #define RIOCM_MAX_CHNUM … #define RIOCM_CHNUM_AUTO … #define RIOCM_MAX_EP_COUNT … enum rio_cm_state { … }; enum rio_cm_pkt_type { … }; enum rio_cm_chop { … }; struct rio_ch_base_bhdr { … } __attribute__((packed)); struct rio_ch_chan_hdr { … } __attribute__((packed)); struct tx_req { … }; struct cm_dev { … }; struct chan_rx_ring { … }; struct rio_channel { … }; struct cm_peer { … }; struct rio_cm_work { … }; struct conn_req { … }; /* * A channel_dev structure represents a CM_CDEV * @cdev Character device * @dev Associated device object */ struct channel_dev { … }; static struct rio_channel *riocm_ch_alloc(u16 ch_num); static void riocm_ch_free(struct kref *ref); static int riocm_post_send(struct cm_dev *cm, struct rio_dev *rdev, void *buffer, size_t len); static int riocm_ch_close(struct rio_channel *ch); static DEFINE_SPINLOCK(idr_lock); static DEFINE_IDR(ch_idr); static LIST_HEAD(cm_dev_list); static DECLARE_RWSEM(rdev_sem); static const struct class dev_class = …; static unsigned int dev_major; static unsigned int dev_minor_base; static dev_t dev_number; static struct channel_dev riocm_cdev; #define is_msg_capable(src_ops, dst_ops) … #define dev_cm_capable(dev) … static int riocm_cmp(struct rio_channel *ch, enum rio_cm_state cmp) { … } static int riocm_cmp_exch(struct rio_channel *ch, enum rio_cm_state cmp, enum rio_cm_state exch) { … } static enum rio_cm_state riocm_exch(struct rio_channel *ch, enum rio_cm_state exch) { … } static struct rio_channel *riocm_get_channel(u16 nr) { … } static void riocm_put_channel(struct rio_channel *ch) { … } static void *riocm_rx_get_msg(struct cm_dev *cm) { … } /* * riocm_rx_fill - fills a ring of receive buffers for given cm device * @cm: cm_dev object * @nent: max number of entries to fill * * Returns: none */ static void riocm_rx_fill(struct cm_dev *cm, int nent) { … } /* * riocm_rx_free - frees all receive buffers associated with given cm device * @cm: cm_dev object * * Returns: none */ static void riocm_rx_free(struct cm_dev *cm) { … } /* * riocm_req_handler - connection request handler * @cm: cm_dev object * @req_data: pointer to the request packet * * Returns: 0 if success, or * -EINVAL if channel is not in correct state, * -ENODEV if cannot find a channel with specified ID, * -ENOMEM if unable to allocate memory to store the request */ static int riocm_req_handler(struct cm_dev *cm, void *req_data) { … } /* * riocm_resp_handler - response to connection request handler * @resp_data: pointer to the response packet * * Returns: 0 if success, or * -EINVAL if channel is not in correct state, * -ENODEV if cannot find a channel with specified ID, */ static int riocm_resp_handler(void *resp_data) { … } /* * riocm_close_handler - channel close request handler * @req_data: pointer to the request packet * * Returns: 0 if success, or * -ENODEV if cannot find a channel with specified ID, * + error codes returned by riocm_ch_close. */ static int riocm_close_handler(void *data) { … } /* * rio_cm_handler - function that services request (non-data) packets * @cm: cm_dev object * @data: pointer to the packet */ static void rio_cm_handler(struct cm_dev *cm, void *data) { … } /* * rio_rx_data_handler - received data packet handler * @cm: cm_dev object * @buf: data packet * * Returns: 0 if success, or * -ENODEV if cannot find a channel with specified ID, * -EIO if channel is not in CONNECTED state, * -ENOMEM if channel RX queue is full (packet discarded) */ static int rio_rx_data_handler(struct cm_dev *cm, void *buf) { … } /* * rio_ibmsg_handler - inbound message packet handler */ static void rio_ibmsg_handler(struct work_struct *work) { … } static void riocm_inb_msg_event(struct rio_mport *mport, void *dev_id, int mbox, int slot) { … } /* * rio_txcq_handler - TX completion handler * @cm: cm_dev object * @slot: TX queue slot * * TX completion handler also ensures that pending request packets are placed * into transmit queue as soon as a free slot becomes available. This is done * to give higher priority to request packets during high intensity data flow. */ static void rio_txcq_handler(struct cm_dev *cm, int slot) { … } static void riocm_outb_msg_event(struct rio_mport *mport, void *dev_id, int mbox, int slot) { … } static int riocm_queue_req(struct cm_dev *cm, struct rio_dev *rdev, void *buffer, size_t len) { … } /* * riocm_post_send - helper function that places packet into msg TX queue * @cm: cm_dev object * @rdev: target RapidIO device object (required by outbound msg interface) * @buffer: pointer to a packet buffer to send * @len: length of data to transfer * @req: request priority flag * * Returns: 0 if success, or error code otherwise. */ static int riocm_post_send(struct cm_dev *cm, struct rio_dev *rdev, void *buffer, size_t len) { … } /* * riocm_ch_send - sends a data packet to a remote device * @ch_id: local channel ID * @buf: pointer to a data buffer to send (including CM header) * @len: length of data to transfer (including CM header) * * ATTN: ASSUMES THAT THE HEADER SPACE IS RESERVED PART OF THE DATA PACKET * * Returns: 0 if success, or * -EINVAL if one or more input parameters is/are not valid, * -ENODEV if cannot find a channel with specified ID, * -EAGAIN if a channel is not in CONNECTED state, * + error codes returned by HW send routine. */ static int riocm_ch_send(u16 ch_id, void *buf, int len) { … } static int riocm_ch_free_rxbuf(struct rio_channel *ch, void *buf) { … } /* * riocm_ch_receive - fetch a data packet received for the specified channel * @ch: local channel ID * @buf: pointer to a packet buffer * @timeout: timeout to wait for incoming packet (in jiffies) * * Returns: 0 and valid buffer pointer if success, or NULL pointer and one of: * -EAGAIN if a channel is not in CONNECTED state, * -ENOMEM if in-use tracking queue is full, * -ETIME if wait timeout expired, * -EINTR if wait was interrupted. */ static int riocm_ch_receive(struct rio_channel *ch, void **buf, long timeout) { … } /* * riocm_ch_connect - sends a connect request to a remote device * @loc_ch: local channel ID * @cm: CM device to send connect request * @peer: target RapidIO device * @rem_ch: remote channel ID * * Returns: 0 if success, or * -EINVAL if the channel is not in IDLE state, * -EAGAIN if no connection request available immediately, * -ETIME if ACK response timeout expired, * -EINTR if wait for response was interrupted. */ static int riocm_ch_connect(u16 loc_ch, struct cm_dev *cm, struct cm_peer *peer, u16 rem_ch) { … } static int riocm_send_ack(struct rio_channel *ch) { … } /* * riocm_ch_accept - accept incoming connection request * @ch_id: channel ID * @new_ch_id: local mport device * @timeout: wait timeout (if 0 non-blocking call, do not wait if connection * request is not available). * * Returns: pointer to new channel struct if success, or error-valued pointer: * -ENODEV - cannot find specified channel or mport, * -EINVAL - the channel is not in IDLE state, * -EAGAIN - no connection request available immediately (timeout=0), * -ENOMEM - unable to allocate new channel, * -ETIME - wait timeout expired, * -EINTR - wait was interrupted. */ static struct rio_channel *riocm_ch_accept(u16 ch_id, u16 *new_ch_id, long timeout) { … } /* * riocm_ch_listen - puts a channel into LISTEN state * @ch_id: channel ID * * Returns: 0 if success, or * -EINVAL if the specified channel does not exists or * is not in CHAN_BOUND state. */ static int riocm_ch_listen(u16 ch_id) { … } /* * riocm_ch_bind - associate a channel object and an mport device * @ch_id: channel ID * @mport_id: local mport device ID * @context: pointer to the additional caller's context * * Returns: 0 if success, or * -ENODEV if cannot find specified mport, * -EINVAL if the specified channel does not exist or * is not in IDLE state. */ static int riocm_ch_bind(u16 ch_id, u8 mport_id, void *context) { … } /* * riocm_ch_alloc - channel object allocation helper routine * @ch_num: channel ID (1 ... RIOCM_MAX_CHNUM, 0 = automatic) * * Return value: pointer to newly created channel object, * or error-valued pointer */ static struct rio_channel *riocm_ch_alloc(u16 ch_num) { … } /* * riocm_ch_create - creates a new channel object and allocates ID for it * @ch_num: channel ID (1 ... RIOCM_MAX_CHNUM, 0 = automatic) * * Allocates and initializes a new channel object. If the parameter ch_num > 0 * and is within the valid range, riocm_ch_create tries to allocate the * specified ID for the new channel. If ch_num = 0, channel ID will be assigned * automatically from the range (chstart ... RIOCM_MAX_CHNUM). * Module parameter 'chstart' defines start of an ID range available for dynamic * allocation. Range below 'chstart' is reserved for pre-defined ID numbers. * Available channel numbers are limited by 16-bit size of channel numbers used * in the packet header. * * Return value: PTR to rio_channel structure if successful (with channel number * updated via pointer) or error-valued pointer if error. */ static struct rio_channel *riocm_ch_create(u16 *ch_num) { … } /* * riocm_ch_free - channel object release routine * @ref: pointer to a channel's kref structure */ static void riocm_ch_free(struct kref *ref) { … } static int riocm_send_close(struct rio_channel *ch) { … } /* * riocm_ch_close - closes a channel object with specified ID (by local request) * @ch: channel to be closed */ static int riocm_ch_close(struct rio_channel *ch) { … } /* * riocm_cdev_open() - Open character device */ static int riocm_cdev_open(struct inode *inode, struct file *filp) { … } /* * riocm_cdev_release() - Release character device */ static int riocm_cdev_release(struct inode *inode, struct file *filp) { … } /* * cm_ep_get_list_size() - Reports number of endpoints in the network */ static int cm_ep_get_list_size(void __user *arg) { … } /* * cm_ep_get_list() - Returns list of attached endpoints */ static int cm_ep_get_list(void __user *arg) { … } /* * cm_mport_get_list() - Returns list of available local mport devices */ static int cm_mport_get_list(void __user *arg) { … } /* * cm_chan_create() - Create a message exchange channel */ static int cm_chan_create(struct file *filp, void __user *arg) { … } /* * cm_chan_close() - Close channel * @filp: Pointer to file object * @arg: Channel to close */ static int cm_chan_close(struct file *filp, void __user *arg) { … } /* * cm_chan_bind() - Bind channel * @arg: Channel number */ static int cm_chan_bind(void __user *arg) { … } /* * cm_chan_listen() - Listen on channel * @arg: Channel number */ static int cm_chan_listen(void __user *arg) { … } /* * cm_chan_accept() - Accept incoming connection * @filp: Pointer to file object * @arg: Channel number */ static int cm_chan_accept(struct file *filp, void __user *arg) { … } /* * cm_chan_connect() - Connect on channel * @arg: Channel information */ static int cm_chan_connect(void __user *arg) { … } /* * cm_chan_msg_send() - Send a message through channel * @arg: Outbound message information */ static int cm_chan_msg_send(void __user *arg) { … } /* * cm_chan_msg_rcv() - Receive a message through channel * @arg: Inbound message information */ static int cm_chan_msg_rcv(void __user *arg) { … } /* * riocm_cdev_ioctl() - IOCTL requests handler */ static long riocm_cdev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) { … } static const struct file_operations riocm_cdev_fops = …; /* * riocm_add_dev - add new remote RapidIO device into channel management core * @dev: device object associated with RapidIO device * @sif: subsystem interface * * Adds the specified RapidIO device (if applicable) into peers list of * the corresponding channel management device (cm_dev). */ static int riocm_add_dev(struct device *dev, struct subsys_interface *sif) { … } /* * riocm_remove_dev - remove remote RapidIO device from channel management core * @dev: device object associated with RapidIO device * @sif: subsystem interface * * Removes the specified RapidIO device (if applicable) from peers list of * the corresponding channel management device (cm_dev). */ static void riocm_remove_dev(struct device *dev, struct subsys_interface *sif) { … } /* * riocm_cdev_add() - Create rio_cm char device * @devno: device number assigned to device (MAJ + MIN) */ static int riocm_cdev_add(dev_t devno) { … } /* * riocm_add_mport - add new local mport device into channel management core * @dev: device object associated with mport * * When a new mport device is added, CM immediately reserves inbound and * outbound RapidIO mailboxes that will be used. */ static int riocm_add_mport(struct device *dev) { … } /* * riocm_remove_mport - remove local mport device from channel management core * @dev: device object associated with mport * * Removes a local mport device from the list of registered devices that provide * channel management services. Returns an error if the specified mport is not * registered with the CM core. */ static void riocm_remove_mport(struct device *dev) { … } static int rio_cm_shutdown(struct notifier_block *nb, unsigned long code, void *unused) { … } /* * riocm_interface handles addition/removal of remote RapidIO devices */ static struct subsys_interface riocm_interface = …; /* * rio_mport_interface handles addition/removal local mport devices */ static struct class_interface rio_mport_interface __refdata = …; static struct notifier_block rio_cm_notifier = …; static int __init riocm_init(void) { … } static void __exit riocm_exit(void) { … } late_initcall(riocm_init); module_exit(riocm_exit);