Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * rio_cm - RapidIO Channelized Messaging Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright 2013-2016 Integrated Device Technology, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * Copyright (c) 2015, Prodrive Technologies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * Copyright (c) 2015, RapidIO Trade Association
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/dma-mapping.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/rio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/rio_drv.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/cdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/printk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/rio_cm_cdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #define DRV_NAME        "rio_cm"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #define DRV_VERSION     "1.0.0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #define DRV_AUTHOR      "Alexandre Bounine <alexandre.bounine@idt.com>"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #define DRV_DESC        "RapidIO Channelized Messaging Driver"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #define DEV_NAME	"rio_cm"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) /* Debug output filtering masks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 	DBG_NONE	= 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 	DBG_INIT	= BIT(0), /* driver init */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 	DBG_EXIT	= BIT(1), /* driver exit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 	DBG_MPORT	= BIT(2), /* mport add/remove */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 	DBG_RDEV	= BIT(3), /* RapidIO device add/remove */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 	DBG_CHOP	= BIT(4), /* channel operations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 	DBG_WAIT	= BIT(5), /* waiting for events */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 	DBG_TX		= BIT(6), /* message TX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 	DBG_TX_EVENT	= BIT(7), /* message TX event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 	DBG_RX_DATA	= BIT(8), /* inbound data messages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	DBG_RX_CMD	= BIT(9), /* inbound REQ/ACK/NACK messages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 	DBG_ALL		= ~0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) #ifdef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) #define riocm_debug(level, fmt, arg...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 		if (DBG_##level & dbg_level) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 			pr_debug(DRV_NAME ": %s " fmt "\n", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 				__func__, ##arg); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	} while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) #define riocm_debug(level, fmt, arg...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 		no_printk(KERN_DEBUG pr_fmt(DRV_NAME fmt "\n"), ##arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) #define riocm_warn(fmt, arg...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	pr_warn(DRV_NAME ": %s WARNING " fmt "\n", __func__, ##arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) #define riocm_error(fmt, arg...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	pr_err(DRV_NAME ": %s ERROR " fmt "\n", __func__, ##arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) static int cmbox = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) module_param(cmbox, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) MODULE_PARM_DESC(cmbox, "RapidIO Mailbox number (default 1)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) static int chstart = 256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) module_param(chstart, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) MODULE_PARM_DESC(chstart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 		 "Start channel number for dynamic allocation (default 256)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) #ifdef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) static u32 dbg_level = DBG_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) module_param(dbg_level, uint, S_IWUSR | S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) MODULE_PARM_DESC(dbg_level, "Debugging output level (default 0 = none)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) MODULE_AUTHOR(DRV_AUTHOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) MODULE_DESCRIPTION(DRV_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) MODULE_VERSION(DRV_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) #define RIOCM_TX_RING_SIZE	128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) #define RIOCM_RX_RING_SIZE	128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) #define RIOCM_CONNECT_TO	3 /* connect response TO (in sec) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) #define RIOCM_MAX_CHNUM		0xffff /* Use full range of u16 field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) #define RIOCM_CHNUM_AUTO	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) #define RIOCM_MAX_EP_COUNT	0x10000 /* Max number of endpoints */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) enum rio_cm_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	RIO_CM_IDLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	RIO_CM_CONNECT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	RIO_CM_CONNECTED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	RIO_CM_DISCONNECT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	RIO_CM_CHAN_BOUND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	RIO_CM_LISTEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	RIO_CM_DESTROYING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) enum rio_cm_pkt_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	RIO_CM_SYS	= 0xaa,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	RIO_CM_CHAN	= 0x55,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) enum rio_cm_chop {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	CM_CONN_REQ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	CM_CONN_ACK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	CM_CONN_CLOSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	CM_DATA_MSG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) struct rio_ch_base_bhdr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	u32 src_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	u32 dst_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) #define RIO_HDR_LETTER_MASK 0xffff0000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) #define RIO_HDR_MBOX_MASK   0x0000ffff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	u8  src_mbox;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	u8  dst_mbox;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	u8  type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) } __attribute__((__packed__));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) struct rio_ch_chan_hdr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	struct rio_ch_base_bhdr bhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	u8 ch_op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	u16 dst_ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	u16 src_ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	u16 msg_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	u16 rsrvd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) } __attribute__((__packed__));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) struct tx_req {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	struct rio_dev   *rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	void		 *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	size_t		 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) struct cm_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	struct list_head	list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	struct rio_mport	*mport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	void			*rx_buf[RIOCM_RX_RING_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	int			rx_slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	struct mutex		rx_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	void			*tx_buf[RIOCM_TX_RING_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	int			tx_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	int			tx_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	int			tx_ack_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	struct list_head	tx_reqs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	spinlock_t		tx_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	struct list_head	peers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	u32			npeers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	struct workqueue_struct *rx_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	struct work_struct	rx_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) struct chan_rx_ring {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	void	*buf[RIOCM_RX_RING_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	int	head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	int	tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	int	count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	/* Tracking RX buffers reported to upper level */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	void	*inuse[RIOCM_RX_RING_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	int	inuse_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) struct rio_channel {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	u16			id;	/* local channel ID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	struct kref		ref;	/* channel refcount */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	struct file		*filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	struct cm_dev		*cmdev;	/* associated CM device object */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	struct rio_dev		*rdev;	/* remote RapidIO device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	enum rio_cm_state	state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	spinlock_t		lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	void			*context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	u32			loc_destid;	/* local destID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	u32			rem_destid;	/* remote destID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	u16			rem_channel;	/* remote channel ID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	struct list_head	accept_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	struct list_head	ch_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	struct completion	comp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	struct completion	comp_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	struct chan_rx_ring	rx_ring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) struct cm_peer {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	struct rio_dev *rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) struct rio_cm_work {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	struct work_struct work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	struct cm_dev *cm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	void *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) struct conn_req {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	u32 destid;	/* requester destID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	u16 chan;	/* requester channel ID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	struct cm_dev *cmdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215)  * A channel_dev structure represents a CM_CDEV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216)  * @cdev	Character device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217)  * @dev		Associated device object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) struct channel_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	struct cdev	cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	struct device	*dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) static struct rio_channel *riocm_ch_alloc(u16 ch_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) static void riocm_ch_free(struct kref *ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) static int riocm_post_send(struct cm_dev *cm, struct rio_dev *rdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 			   void *buffer, size_t len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) static int riocm_ch_close(struct rio_channel *ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) static DEFINE_SPINLOCK(idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) static DEFINE_IDR(ch_idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) static LIST_HEAD(cm_dev_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) static DECLARE_RWSEM(rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) static struct class *dev_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) static unsigned int dev_major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) static unsigned int dev_minor_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) static dev_t dev_number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) static struct channel_dev riocm_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) #define is_msg_capable(src_ops, dst_ops)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 			((src_ops & RIO_SRC_OPS_DATA_MSG) &&	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 			 (dst_ops & RIO_DST_OPS_DATA_MSG))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) #define dev_cm_capable(dev) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	is_msg_capable(dev->src_ops, dev->dst_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) static int riocm_cmp(struct rio_channel *ch, enum rio_cm_state cmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	spin_lock_bh(&ch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	ret = (ch->state == cmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	spin_unlock_bh(&ch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) static int riocm_cmp_exch(struct rio_channel *ch,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 			   enum rio_cm_state cmp, enum rio_cm_state exch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	spin_lock_bh(&ch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	ret = (ch->state == cmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 		ch->state = exch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	spin_unlock_bh(&ch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) static enum rio_cm_state riocm_exch(struct rio_channel *ch,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 				    enum rio_cm_state exch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	enum rio_cm_state old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	spin_lock_bh(&ch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	old = ch->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	ch->state = exch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	spin_unlock_bh(&ch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	return old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) static struct rio_channel *riocm_get_channel(u16 nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	struct rio_channel *ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	spin_lock_bh(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	ch = idr_find(&ch_idr, nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	if (ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 		kref_get(&ch->ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	spin_unlock_bh(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	return ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) static void riocm_put_channel(struct rio_channel *ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	kref_put(&ch->ref, riocm_ch_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) static void *riocm_rx_get_msg(struct cm_dev *cm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	void *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	msg = rio_get_inb_message(cm->mport, cmbox);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	if (msg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 		for (i = 0; i < RIOCM_RX_RING_SIZE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 			if (cm->rx_buf[i] == msg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 				cm->rx_buf[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 				cm->rx_slots++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 		if (i == RIOCM_RX_RING_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 			riocm_warn("no record for buffer 0x%p", msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	return msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323)  * riocm_rx_fill - fills a ring of receive buffers for given cm device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324)  * @cm: cm_dev object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325)  * @nent: max number of entries to fill
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327)  * Returns: none
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) static void riocm_rx_fill(struct cm_dev *cm, int nent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	if (cm->rx_slots == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	for (i = 0; i < RIOCM_RX_RING_SIZE && cm->rx_slots && nent; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 		if (cm->rx_buf[i] == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 			cm->rx_buf[i] = kmalloc(RIO_MAX_MSG_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 			if (cm->rx_buf[i] == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 			rio_add_inb_buffer(cm->mport, cmbox, cm->rx_buf[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 			cm->rx_slots--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 			nent--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349)  * riocm_rx_free - frees all receive buffers associated with given cm device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350)  * @cm: cm_dev object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352)  * Returns: none
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) static void riocm_rx_free(struct cm_dev *cm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	for (i = 0; i < RIOCM_RX_RING_SIZE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		if (cm->rx_buf[i] != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 			kfree(cm->rx_buf[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 			cm->rx_buf[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367)  * riocm_req_handler - connection request handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368)  * @cm: cm_dev object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369)  * @req_data: pointer to the request packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371)  * Returns: 0 if success, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372)  *          -EINVAL if channel is not in correct state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373)  *          -ENODEV if cannot find a channel with specified ID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374)  *          -ENOMEM if unable to allocate memory to store the request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) static int riocm_req_handler(struct cm_dev *cm, void *req_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	struct rio_channel *ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	struct conn_req *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	struct rio_ch_chan_hdr *hh = req_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	u16 chnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	chnum = ntohs(hh->dst_ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	ch = riocm_get_channel(chnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	if (!ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	if (ch->state != RIO_CM_LISTEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 		riocm_debug(RX_CMD, "channel %d is not in listen state", chnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		riocm_put_channel(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	req = kzalloc(sizeof(*req), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	if (!req) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 		riocm_put_channel(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	req->destid = ntohl(hh->bhdr.src_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	req->chan = ntohs(hh->src_ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	req->cmdev = cm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	spin_lock_bh(&ch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	list_add_tail(&req->node, &ch->accept_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	spin_unlock_bh(&ch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	complete(&ch->comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	riocm_put_channel(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416)  * riocm_resp_handler - response to connection request handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417)  * @resp_data: pointer to the response packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419)  * Returns: 0 if success, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420)  *          -EINVAL if channel is not in correct state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421)  *          -ENODEV if cannot find a channel with specified ID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) static int riocm_resp_handler(void *resp_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	struct rio_channel *ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	struct rio_ch_chan_hdr *hh = resp_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	u16 chnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	chnum = ntohs(hh->dst_ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	ch = riocm_get_channel(chnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	if (!ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	if (ch->state != RIO_CM_CONNECT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 		riocm_put_channel(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	riocm_exch(ch, RIO_CM_CONNECTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	ch->rem_channel = ntohs(hh->src_ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	complete(&ch->comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	riocm_put_channel(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448)  * riocm_close_handler - channel close request handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449)  * @req_data: pointer to the request packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451)  * Returns: 0 if success, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452)  *          -ENODEV if cannot find a channel with specified ID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453)  *            + error codes returned by riocm_ch_close.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) static int riocm_close_handler(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	struct rio_channel *ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	struct rio_ch_chan_hdr *hh = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	riocm_debug(RX_CMD, "for ch=%d", ntohs(hh->dst_ch));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	spin_lock_bh(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	ch = idr_find(&ch_idr, ntohs(hh->dst_ch));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	if (!ch) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 		spin_unlock_bh(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	idr_remove(&ch_idr, ch->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	spin_unlock_bh(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	riocm_exch(ch, RIO_CM_DISCONNECT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	ret = riocm_ch_close(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 		riocm_debug(RX_CMD, "riocm_ch_close() returned %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482)  * rio_cm_handler - function that services request (non-data) packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483)  * @cm: cm_dev object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484)  * @data: pointer to the packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) static void rio_cm_handler(struct cm_dev *cm, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	struct rio_ch_chan_hdr *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	if (!rio_mport_is_running(cm->mport))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	hdr = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	riocm_debug(RX_CMD, "OP=%x for ch=%d from %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 		    hdr->ch_op, ntohs(hdr->dst_ch), ntohs(hdr->src_ch));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	switch (hdr->ch_op) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	case CM_CONN_REQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 		riocm_req_handler(cm, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	case CM_CONN_ACK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 		riocm_resp_handler(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	case CM_CONN_CLOSE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 		riocm_close_handler(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 		riocm_error("Invalid packet header");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517)  * rio_rx_data_handler - received data packet handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518)  * @cm: cm_dev object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519)  * @buf: data packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521)  * Returns: 0 if success, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522)  *          -ENODEV if cannot find a channel with specified ID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523)  *          -EIO if channel is not in CONNECTED state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524)  *          -ENOMEM if channel RX queue is full (packet discarded)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) static int rio_rx_data_handler(struct cm_dev *cm, void *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	struct rio_ch_chan_hdr *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	struct rio_channel *ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	hdr = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	riocm_debug(RX_DATA, "for ch=%d", ntohs(hdr->dst_ch));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	ch = riocm_get_channel(ntohs(hdr->dst_ch));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	if (!ch) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		/* Discard data message for non-existing channel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	/* Place pointer to the buffer into channel's RX queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	spin_lock(&ch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	if (ch->state != RIO_CM_CONNECTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		/* Channel is not ready to receive data, discard a packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		riocm_debug(RX_DATA, "ch=%d is in wrong state=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 			    ch->id, ch->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		spin_unlock(&ch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 		kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 		riocm_put_channel(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	if (ch->rx_ring.count == RIOCM_RX_RING_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		/* If RX ring is full, discard a packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		riocm_debug(RX_DATA, "ch=%d is full", ch->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		spin_unlock(&ch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 		kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 		riocm_put_channel(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	ch->rx_ring.buf[ch->rx_ring.head] = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	ch->rx_ring.head++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	ch->rx_ring.count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	ch->rx_ring.head %= RIOCM_RX_RING_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	complete(&ch->comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	spin_unlock(&ch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	riocm_put_channel(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578)  * rio_ibmsg_handler - inbound message packet handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) static void rio_ibmsg_handler(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	struct cm_dev *cm = container_of(work, struct cm_dev, rx_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	void *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	struct rio_ch_chan_hdr *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	if (!rio_mport_is_running(cm->mport))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 		mutex_lock(&cm->rx_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 		data = riocm_rx_get_msg(cm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		if (data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 			riocm_rx_fill(cm, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		mutex_unlock(&cm->rx_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 		if (data == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		hdr = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 		if (hdr->bhdr.type != RIO_CM_CHAN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 			/* For now simply discard packets other than channel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 			riocm_error("Unsupported TYPE code (0x%x). Msg dropped",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 				    hdr->bhdr.type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 			kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 		/* Process a channel message */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 		if (hdr->ch_op == CM_DATA_MSG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 			rio_rx_data_handler(cm, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 			rio_cm_handler(cm, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) static void riocm_inb_msg_event(struct rio_mport *mport, void *dev_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 				int mbox, int slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	struct cm_dev *cm = dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	if (rio_mport_is_running(cm->mport) && !work_pending(&cm->rx_work))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		queue_work(cm->rx_wq, &cm->rx_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627)  * rio_txcq_handler - TX completion handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628)  * @cm: cm_dev object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629)  * @slot: TX queue slot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631)  * TX completion handler also ensures that pending request packets are placed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632)  * into transmit queue as soon as a free slot becomes available. This is done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633)  * to give higher priority to request packets during high intensity data flow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) static void rio_txcq_handler(struct cm_dev *cm, int slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	int ack_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	/* ATTN: Add TX completion notification if/when direct buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	 * transfer is implemented. At this moment only correct tracking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	 * of tx_count is important.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	riocm_debug(TX_EVENT, "for mport_%d slot %d tx_cnt %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 		    cm->mport->id, slot, cm->tx_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	spin_lock(&cm->tx_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	ack_slot = cm->tx_ack_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	if (ack_slot == slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 		riocm_debug(TX_EVENT, "slot == ack_slot");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	while (cm->tx_cnt && ((ack_slot != slot) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	       (cm->tx_cnt == RIOCM_TX_RING_SIZE))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 		cm->tx_buf[ack_slot] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 		++ack_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		ack_slot &= (RIOCM_TX_RING_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 		cm->tx_cnt--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	if (cm->tx_cnt < 0 || cm->tx_cnt > RIOCM_TX_RING_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 		riocm_error("tx_cnt %d out of sync", cm->tx_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	WARN_ON((cm->tx_cnt < 0) || (cm->tx_cnt > RIOCM_TX_RING_SIZE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	cm->tx_ack_slot = ack_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	 * If there are pending requests, insert them into transmit queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	if (!list_empty(&cm->tx_reqs) && (cm->tx_cnt < RIOCM_TX_RING_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 		struct tx_req *req, *_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		list_for_each_entry_safe(req, _req, &cm->tx_reqs, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 			list_del(&req->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 			cm->tx_buf[cm->tx_slot] = req->buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 			rc = rio_add_outb_message(cm->mport, req->rdev, cmbox,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 						  req->buffer, req->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 			kfree(req->buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 			kfree(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 			++cm->tx_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 			++cm->tx_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 			cm->tx_slot &= (RIOCM_TX_RING_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 			if (cm->tx_cnt == RIOCM_TX_RING_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	spin_unlock(&cm->tx_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) static void riocm_outb_msg_event(struct rio_mport *mport, void *dev_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 				 int mbox, int slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	struct cm_dev *cm = dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	if (cm && rio_mport_is_running(cm->mport))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 		rio_txcq_handler(cm, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) static int riocm_queue_req(struct cm_dev *cm, struct rio_dev *rdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 			   void *buffer, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	struct tx_req *treq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	treq = kzalloc(sizeof(*treq), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	if (treq == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	treq->rdev = rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	treq->buffer = buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	treq->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	spin_lock_irqsave(&cm->tx_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	list_add_tail(&treq->node, &cm->tx_reqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	spin_unlock_irqrestore(&cm->tx_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724)  * riocm_post_send - helper function that places packet into msg TX queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725)  * @cm: cm_dev object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726)  * @rdev: target RapidIO device object (required by outbound msg interface)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727)  * @buffer: pointer to a packet buffer to send
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728)  * @len: length of data to transfer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729)  * @req: request priority flag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731)  * Returns: 0 if success, or error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) static int riocm_post_send(struct cm_dev *cm, struct rio_dev *rdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 			   void *buffer, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	spin_lock_irqsave(&cm->tx_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	if (cm->mport == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		rc = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	if (cm->tx_cnt == RIOCM_TX_RING_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		riocm_debug(TX, "Tx Queue is full");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		rc = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	cm->tx_buf[cm->tx_slot] = buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	rc = rio_add_outb_message(cm->mport, rdev, cmbox, buffer, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	riocm_debug(TX, "Add buf@%p destid=%x tx_slot=%d tx_cnt=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		 buffer, rdev->destid, cm->tx_slot, cm->tx_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	++cm->tx_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	++cm->tx_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	cm->tx_slot &= (RIOCM_TX_RING_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	spin_unlock_irqrestore(&cm->tx_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768)  * riocm_ch_send - sends a data packet to a remote device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769)  * @ch_id: local channel ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770)  * @buf: pointer to a data buffer to send (including CM header)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771)  * @len: length of data to transfer (including CM header)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773)  * ATTN: ASSUMES THAT THE HEADER SPACE IS RESERVED PART OF THE DATA PACKET
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775)  * Returns: 0 if success, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776)  *          -EINVAL if one or more input parameters is/are not valid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777)  *          -ENODEV if cannot find a channel with specified ID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778)  *          -EAGAIN if a channel is not in CONNECTED state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779)  *	    + error codes returned by HW send routine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) static int riocm_ch_send(u16 ch_id, void *buf, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	struct rio_channel *ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	struct rio_ch_chan_hdr *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	if (buf == NULL || ch_id == 0 || len == 0 || len > RIO_MAX_MSG_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	ch = riocm_get_channel(ch_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	if (!ch) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 		riocm_error("%s(%d) ch_%d not found", current->comm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 			    task_pid_nr(current), ch_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	if (!riocm_cmp(ch, RIO_CM_CONNECTED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	 * Fill buffer header section with corresponding channel data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	hdr = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	hdr->bhdr.src_id = htonl(ch->loc_destid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	hdr->bhdr.dst_id = htonl(ch->rem_destid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	hdr->bhdr.src_mbox = cmbox;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	hdr->bhdr.dst_mbox = cmbox;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	hdr->bhdr.type = RIO_CM_CHAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	hdr->ch_op = CM_DATA_MSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	hdr->dst_ch = htons(ch->rem_channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	hdr->src_ch = htons(ch->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	hdr->msg_len = htons((u16)len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	/* ATTN: the function call below relies on the fact that underlying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	 * HW-specific add_outb_message() routine copies TX data into its own
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	 * internal transfer buffer (true for all RIONET compatible mport
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	 * drivers). Must be reviewed if mport driver uses the buffer directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	ret = riocm_post_send(ch->cmdev, ch->rdev, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		riocm_debug(TX, "ch %d send_err=%d", ch->id, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	riocm_put_channel(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) static int riocm_ch_free_rxbuf(struct rio_channel *ch, void *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	int i, ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	spin_lock_bh(&ch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	for (i = 0; i < RIOCM_RX_RING_SIZE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		if (ch->rx_ring.inuse[i] == buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 			ch->rx_ring.inuse[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 			ch->rx_ring.inuse_cnt--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	spin_unlock_bh(&ch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 		kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855)  * riocm_ch_receive - fetch a data packet received for the specified channel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856)  * @ch: local channel ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857)  * @buf: pointer to a packet buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858)  * @timeout: timeout to wait for incoming packet (in jiffies)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860)  * Returns: 0 and valid buffer pointer if success, or NULL pointer and one of:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861)  *          -EAGAIN if a channel is not in CONNECTED state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862)  *          -ENOMEM if in-use tracking queue is full,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863)  *          -ETIME if wait timeout expired,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864)  *	    -EINTR if wait was interrupted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) static int riocm_ch_receive(struct rio_channel *ch, void **buf, long timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	void *rxmsg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	int i, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	long wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	if (!riocm_cmp(ch, RIO_CM_CONNECTED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	if (ch->rx_ring.inuse_cnt == RIOCM_RX_RING_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		/* If we do not have entries to track buffers given to upper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		 * layer, reject request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	wret = wait_for_completion_interruptible_timeout(&ch->comp, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	riocm_debug(WAIT, "wait on %d returned %ld", ch->id, wret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	if (!wret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 		ret = -ETIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	else if (wret == -ERESTARTSYS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		ret = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		ret = riocm_cmp(ch, RIO_CM_CONNECTED) ? 0 : -ECONNRESET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	spin_lock_bh(&ch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	rxmsg = ch->rx_ring.buf[ch->rx_ring.tail];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	ch->rx_ring.buf[ch->rx_ring.tail] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	ch->rx_ring.count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	ch->rx_ring.tail++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	ch->rx_ring.tail %= RIOCM_RX_RING_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	for (i = 0; i < RIOCM_RX_RING_SIZE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		if (ch->rx_ring.inuse[i] == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 			ch->rx_ring.inuse[i] = rxmsg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 			ch->rx_ring.inuse_cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		/* We have no entry to store pending message: drop it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 		kfree(rxmsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 		rxmsg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	spin_unlock_bh(&ch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	*buf = rxmsg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930)  * riocm_ch_connect - sends a connect request to a remote device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931)  * @loc_ch: local channel ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932)  * @cm: CM device to send connect request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933)  * @peer: target RapidIO device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934)  * @rem_ch: remote channel ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936)  * Returns: 0 if success, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937)  *          -EINVAL if the channel is not in IDLE state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938)  *          -EAGAIN if no connection request available immediately,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939)  *          -ETIME if ACK response timeout expired,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940)  *          -EINTR if wait for response was interrupted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) static int riocm_ch_connect(u16 loc_ch, struct cm_dev *cm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 			    struct cm_peer *peer, u16 rem_ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	struct rio_channel *ch = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	struct rio_ch_chan_hdr *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	long wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	ch = riocm_get_channel(loc_ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	if (!ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	if (!riocm_cmp_exch(ch, RIO_CM_IDLE, RIO_CM_CONNECT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		goto conn_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	ch->cmdev = cm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	ch->rdev = peer->rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	ch->context = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	ch->loc_destid = cm->mport->host_deviceid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	ch->rem_channel = rem_ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	 * Send connect request to the remote RapidIO device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	if (hdr == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		goto conn_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	hdr->bhdr.src_id = htonl(ch->loc_destid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	hdr->bhdr.dst_id = htonl(peer->rdev->destid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	hdr->bhdr.src_mbox = cmbox;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	hdr->bhdr.dst_mbox = cmbox;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	hdr->bhdr.type = RIO_CM_CHAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	hdr->ch_op = CM_CONN_REQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	hdr->dst_ch = htons(rem_ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	hdr->src_ch = htons(loc_ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	/* ATTN: the function call below relies on the fact that underlying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	 * HW-specific add_outb_message() routine copies TX data into its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	 * internal transfer buffer. Must be reviewed if mport driver uses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	 * this buffer directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	ret = riocm_post_send(cm, peer->rdev, hdr, sizeof(*hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	if (ret != -EBUSY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 		kfree(hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 		ret = riocm_queue_req(cm, peer->rdev, hdr, sizeof(*hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 			kfree(hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 		riocm_cmp_exch(ch, RIO_CM_CONNECT, RIO_CM_IDLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 		goto conn_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	/* Wait for connect response from the remote device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	wret = wait_for_completion_interruptible_timeout(&ch->comp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 							 RIOCM_CONNECT_TO * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	riocm_debug(WAIT, "wait on %d returns %ld", ch->id, wret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	if (!wret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		ret = -ETIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	else if (wret == -ERESTARTSYS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		ret = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		ret = riocm_cmp(ch, RIO_CM_CONNECTED) ? 0 : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) conn_done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	riocm_put_channel(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) static int riocm_send_ack(struct rio_channel *ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	struct rio_ch_chan_hdr *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	if (hdr == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	hdr->bhdr.src_id = htonl(ch->loc_destid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	hdr->bhdr.dst_id = htonl(ch->rem_destid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	hdr->dst_ch = htons(ch->rem_channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	hdr->src_ch = htons(ch->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	hdr->bhdr.src_mbox = cmbox;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	hdr->bhdr.dst_mbox = cmbox;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	hdr->bhdr.type = RIO_CM_CHAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	hdr->ch_op = CM_CONN_ACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	/* ATTN: the function call below relies on the fact that underlying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	 * add_outb_message() routine copies TX data into its internal transfer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	 * buffer. Review if switching to direct buffer version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	ret = riocm_post_send(ch->cmdev, ch->rdev, hdr, sizeof(*hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	if (ret == -EBUSY && !riocm_queue_req(ch->cmdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 					      ch->rdev, hdr, sizeof(*hdr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	kfree(hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 		riocm_error("send ACK to ch_%d on %s failed (ret=%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 			    ch->id, rio_name(ch->rdev), ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057)  * riocm_ch_accept - accept incoming connection request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058)  * @ch_id: channel ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059)  * @new_ch_id: local mport device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060)  * @timeout: wait timeout (if 0 non-blocking call, do not wait if connection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061)  *           request is not available).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063)  * Returns: pointer to new channel struct if success, or error-valued pointer:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064)  *          -ENODEV - cannot find specified channel or mport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065)  *          -EINVAL - the channel is not in IDLE state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066)  *          -EAGAIN - no connection request available immediately (timeout=0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067)  *          -ENOMEM - unable to allocate new channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068)  *          -ETIME - wait timeout expired,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069)  *          -EINTR - wait was interrupted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) static struct rio_channel *riocm_ch_accept(u16 ch_id, u16 *new_ch_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 					   long timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	struct rio_channel *ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	struct rio_channel *new_ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	struct conn_req *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	struct cm_peer *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	int found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	long wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	ch = riocm_get_channel(ch_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	if (!ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	if (!riocm_cmp(ch, RIO_CM_LISTEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 		goto err_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	/* Don't sleep if this is a non blocking call */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	if (!timeout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 		if (!try_wait_for_completion(&ch->comp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 			err = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 			goto err_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 		riocm_debug(WAIT, "on %d", ch->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		wret = wait_for_completion_interruptible_timeout(&ch->comp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 								 timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		if (!wret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 			err = -ETIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 			goto err_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 		} else if (wret == -ERESTARTSYS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 			err = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 			goto err_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	spin_lock_bh(&ch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	if (ch->state != RIO_CM_LISTEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 		err = -ECANCELED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	} else if (list_empty(&ch->accept_queue)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		riocm_debug(WAIT, "on %d accept_queue is empty on completion",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 			    ch->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	spin_unlock_bh(&ch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 		riocm_debug(WAIT, "on %d returns %d", ch->id, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 		goto err_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	/* Create new channel for this connection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	new_ch = riocm_ch_alloc(RIOCM_CHNUM_AUTO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	if (IS_ERR(new_ch)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 		riocm_error("failed to get channel for new req (%ld)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 			PTR_ERR(new_ch));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 		goto err_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	spin_lock_bh(&ch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	req = list_first_entry(&ch->accept_queue, struct conn_req, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	list_del(&req->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	new_ch->cmdev = ch->cmdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	new_ch->loc_destid = ch->loc_destid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	new_ch->rem_destid = req->destid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	new_ch->rem_channel = req->chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	spin_unlock_bh(&ch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	riocm_put_channel(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	ch = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	kfree(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	down_read(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	/* Find requester's device object */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	list_for_each_entry(peer, &new_ch->cmdev->peers, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 		if (peer->rdev->destid == new_ch->rem_destid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 			riocm_debug(RX_CMD, "found matching device(%s)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 				    rio_name(peer->rdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 			found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	up_read(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	if (!found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		/* If peer device object not found, simply ignore the request */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 		err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 		goto err_put_new_ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	new_ch->rdev = peer->rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	new_ch->state = RIO_CM_CONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	spin_lock_init(&new_ch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	/* Acknowledge the connection request. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	riocm_send_ack(new_ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	*new_ch_id = new_ch->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	return new_ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) err_put_new_ch:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	spin_lock_bh(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	idr_remove(&ch_idr, new_ch->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	spin_unlock_bh(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	riocm_put_channel(new_ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) err_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	if (ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 		riocm_put_channel(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	*new_ch_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194)  * riocm_ch_listen - puts a channel into LISTEN state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195)  * @ch_id: channel ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197)  * Returns: 0 if success, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198)  *          -EINVAL if the specified channel does not exists or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199)  *                  is not in CHAN_BOUND state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) static int riocm_ch_listen(u16 ch_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	struct rio_channel *ch = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	riocm_debug(CHOP, "(ch_%d)", ch_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	ch = riocm_get_channel(ch_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	if (!ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	if (!riocm_cmp_exch(ch, RIO_CM_CHAN_BOUND, RIO_CM_LISTEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	riocm_put_channel(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218)  * riocm_ch_bind - associate a channel object and an mport device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219)  * @ch_id: channel ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220)  * @mport_id: local mport device ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221)  * @context: pointer to the additional caller's context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223)  * Returns: 0 if success, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224)  *          -ENODEV if cannot find specified mport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225)  *          -EINVAL if the specified channel does not exist or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226)  *                  is not in IDLE state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) static int riocm_ch_bind(u16 ch_id, u8 mport_id, void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	struct rio_channel *ch = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	struct cm_dev *cm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	int rc = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	riocm_debug(CHOP, "ch_%d to mport_%d", ch_id, mport_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	/* Find matching cm_dev object */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	down_read(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	list_for_each_entry(cm, &cm_dev_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 		if ((cm->mport->id == mport_id) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 		     rio_mport_is_running(cm->mport)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 			rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	ch = riocm_get_channel(ch_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	if (!ch) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	spin_lock_bh(&ch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	if (ch->state != RIO_CM_IDLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 		spin_unlock_bh(&ch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 		goto err_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	ch->cmdev = cm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	ch->loc_destid = cm->mport->host_deviceid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 	ch->context = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	ch->state = RIO_CM_CHAN_BOUND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 	spin_unlock_bh(&ch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) err_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	riocm_put_channel(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	up_read(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275)  * riocm_ch_alloc - channel object allocation helper routine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276)  * @ch_num: channel ID (1 ... RIOCM_MAX_CHNUM, 0 = automatic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278)  * Return value: pointer to newly created channel object,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279)  *               or error-valued pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) static struct rio_channel *riocm_ch_alloc(u16 ch_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	int start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	struct rio_channel *ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	ch = kzalloc(sizeof(*ch), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	if (!ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	if (ch_num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 		/* If requested, try to obtain the specified channel ID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 		start = ch_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 		end = ch_num + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 		/* Obtain channel ID from the dynamic allocation range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 		start = chstart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 		end = RIOCM_MAX_CHNUM + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	idr_preload(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	spin_lock_bh(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	id = idr_alloc_cyclic(&ch_idr, ch, start, end, GFP_NOWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	spin_unlock_bh(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	idr_preload_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	if (id < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 		kfree(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 		return ERR_PTR(id == -ENOSPC ? -EBUSY : id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	ch->id = (u16)id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	ch->state = RIO_CM_IDLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	spin_lock_init(&ch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	INIT_LIST_HEAD(&ch->accept_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 	INIT_LIST_HEAD(&ch->ch_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	init_completion(&ch->comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	init_completion(&ch->comp_close);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 	kref_init(&ch->ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	ch->rx_ring.head = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	ch->rx_ring.tail = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	ch->rx_ring.count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	ch->rx_ring.inuse_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	return ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329)  * riocm_ch_create - creates a new channel object and allocates ID for it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330)  * @ch_num: channel ID (1 ... RIOCM_MAX_CHNUM, 0 = automatic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332)  * Allocates and initializes a new channel object. If the parameter ch_num > 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333)  * and is within the valid range, riocm_ch_create tries to allocate the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334)  * specified ID for the new channel. If ch_num = 0, channel ID will be assigned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335)  * automatically from the range (chstart ... RIOCM_MAX_CHNUM).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336)  * Module parameter 'chstart' defines start of an ID range available for dynamic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337)  * allocation. Range below 'chstart' is reserved for pre-defined ID numbers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338)  * Available channel numbers are limited by 16-bit size of channel numbers used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339)  * in the packet header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341)  * Return value: PTR to rio_channel structure if successful (with channel number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342)  *               updated via pointer) or error-valued pointer if error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) static struct rio_channel *riocm_ch_create(u16 *ch_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 	struct rio_channel *ch = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	ch = riocm_ch_alloc(*ch_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 	if (IS_ERR(ch))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 		riocm_debug(CHOP, "Failed to allocate channel %d (err=%ld)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 			    *ch_num, PTR_ERR(ch));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 		*ch_num = ch->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	return ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360)  * riocm_ch_free - channel object release routine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361)  * @ref: pointer to a channel's kref structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) static void riocm_ch_free(struct kref *ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	struct rio_channel *ch = container_of(ref, struct rio_channel, ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	riocm_debug(CHOP, "(ch_%d)", ch->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	if (ch->rx_ring.inuse_cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 		for (i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 		     i < RIOCM_RX_RING_SIZE && ch->rx_ring.inuse_cnt; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 			if (ch->rx_ring.inuse[i] != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 				kfree(ch->rx_ring.inuse[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 				ch->rx_ring.inuse_cnt--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	if (ch->rx_ring.count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 		for (i = 0; i < RIOCM_RX_RING_SIZE && ch->rx_ring.count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 			if (ch->rx_ring.buf[i] != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 				kfree(ch->rx_ring.buf[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 				ch->rx_ring.count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 	complete(&ch->comp_close);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) static int riocm_send_close(struct rio_channel *ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	struct rio_ch_chan_hdr *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 	 * Send CH_CLOSE notification to the remote RapidIO device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 	hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	if (hdr == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	hdr->bhdr.src_id = htonl(ch->loc_destid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	hdr->bhdr.dst_id = htonl(ch->rem_destid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	hdr->bhdr.src_mbox = cmbox;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	hdr->bhdr.dst_mbox = cmbox;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	hdr->bhdr.type = RIO_CM_CHAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	hdr->ch_op = CM_CONN_CLOSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	hdr->dst_ch = htons(ch->rem_channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	hdr->src_ch = htons(ch->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	/* ATTN: the function call below relies on the fact that underlying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	 * add_outb_message() routine copies TX data into its internal transfer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	 * buffer. Needs to be reviewed if switched to direct buffer mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	ret = riocm_post_send(ch->cmdev, ch->rdev, hdr, sizeof(*hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	if (ret == -EBUSY && !riocm_queue_req(ch->cmdev, ch->rdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 					      hdr, sizeof(*hdr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	kfree(hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 		riocm_error("ch(%d) send CLOSE failed (ret=%d)", ch->id, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431)  * riocm_ch_close - closes a channel object with specified ID (by local request)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432)  * @ch: channel to be closed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) static int riocm_ch_close(struct rio_channel *ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	unsigned long tmo = msecs_to_jiffies(3000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	enum rio_cm_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 	long wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 	riocm_debug(CHOP, "ch_%d by %s(%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 		    ch->id, current->comm, task_pid_nr(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	state = riocm_exch(ch, RIO_CM_DESTROYING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	if (state == RIO_CM_CONNECTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 		riocm_send_close(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	complete_all(&ch->comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	riocm_put_channel(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	wret = wait_for_completion_interruptible_timeout(&ch->comp_close, tmo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	riocm_debug(WAIT, "wait on %d returns %ld", ch->id, wret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	if (wret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 		/* Timeout on wait occurred */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 		riocm_debug(CHOP, "%s(%d) timed out waiting for ch %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 		       current->comm, task_pid_nr(current), ch->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 		ret = -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	} else if (wret == -ERESTARTSYS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 		/* Wait_for_completion was interrupted by a signal */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 		riocm_debug(CHOP, "%s(%d) wait for ch %d was interrupted",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 			current->comm, task_pid_nr(current), ch->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 		ret = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 		riocm_debug(CHOP, "ch_%d resources released", ch->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 		kfree(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 		riocm_debug(CHOP, "failed to release ch_%d resources", ch->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478)  * riocm_cdev_open() - Open character device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) static int riocm_cdev_open(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	riocm_debug(INIT, "by %s(%d) filp=%p ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 		    current->comm, task_pid_nr(current), filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	if (list_empty(&cm_dev_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492)  * riocm_cdev_release() - Release character device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) static int riocm_cdev_release(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	struct rio_channel *ch, *_c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 	riocm_debug(EXIT, "by %s(%d) filp=%p",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 		    current->comm, task_pid_nr(current), filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	/* Check if there are channels associated with this file descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	spin_lock_bh(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 	idr_for_each_entry(&ch_idr, ch, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 		if (ch && ch->filp == filp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 			riocm_debug(EXIT, "ch_%d not released by %s(%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 				    ch->id, current->comm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 				    task_pid_nr(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 			idr_remove(&ch_idr, ch->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 			list_add(&ch->ch_node, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	spin_unlock_bh(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 	if (!list_empty(&list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 		list_for_each_entry_safe(ch, _c, &list, ch_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 			list_del(&ch->ch_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 			riocm_ch_close(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527)  * cm_ep_get_list_size() - Reports number of endpoints in the network
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) static int cm_ep_get_list_size(void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 	u32 __user *p = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	u32 mport_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	u32 count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	struct cm_dev *cm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	if (get_user(mport_id, p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	if (mport_id >= RIO_MAX_MPORTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	/* Find a matching cm_dev object */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 	down_read(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	list_for_each_entry(cm, &cm_dev_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 		if (cm->mport->id == mport_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 			count = cm->npeers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 			up_read(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 			if (copy_to_user(arg, &count, sizeof(u32)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 				return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	up_read(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558)  * cm_ep_get_list() - Returns list of attached endpoints
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) static int cm_ep_get_list(void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	struct cm_dev *cm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 	struct cm_peer *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	u32 info[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	u32 nent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	u32 *entry_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	u32 i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 	if (copy_from_user(&info, arg, sizeof(info)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	if (info[1] >= RIO_MAX_MPORTS || info[0] > RIOCM_MAX_EP_COUNT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	/* Find a matching cm_dev object */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	down_read(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	list_for_each_entry(cm, &cm_dev_list, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 		if (cm->mport->id == (u8)info[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 			goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 	up_read(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	nent = min(info[0], cm->npeers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	buf = kcalloc(nent + 2, sizeof(u32), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 	if (!buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 		up_read(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	entry_ptr = (u32 *)((uintptr_t)buf + 2*sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	list_for_each_entry(peer, &cm->peers, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 		*entry_ptr = (u32)peer->rdev->destid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 		entry_ptr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 		if (++i == nent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 	up_read(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 	((u32 *)buf)[0] = i; /* report an updated number of entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 	((u32 *)buf)[1] = info[1]; /* put back an mport ID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 	if (copy_to_user(arg, buf, sizeof(u32) * (info[0] + 2)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614)  * cm_mport_get_list() - Returns list of available local mport devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) static int cm_mport_get_list(void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 	u32 entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	struct cm_dev *cm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 	u32 *entry_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 	if (copy_from_user(&entries, arg, sizeof(entries)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 	if (entries == 0 || entries > RIO_MAX_MPORTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	buf = kcalloc(entries + 1, sizeof(u32), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 	/* Scan all registered cm_dev objects */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 	entry_ptr = (u32 *)((uintptr_t)buf + sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 	down_read(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 	list_for_each_entry(cm, &cm_dev_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 		if (count++ < entries) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 			*entry_ptr = (cm->mport->id << 16) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 				      cm->mport->host_deviceid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 			entry_ptr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 	up_read(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 	*((u32 *)buf) = count; /* report a real number of entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 	if (copy_to_user(arg, buf, sizeof(u32) * (count + 1)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654)  * cm_chan_create() - Create a message exchange channel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) static int cm_chan_create(struct file *filp, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	u16 __user *p = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 	u16 ch_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 	struct rio_channel *ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	if (get_user(ch_num, p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 	riocm_debug(CHOP, "ch_%d requested by %s(%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 		    ch_num, current->comm, task_pid_nr(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 	ch = riocm_ch_create(&ch_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 	if (IS_ERR(ch))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 		return PTR_ERR(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 	ch->filp = filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 	riocm_debug(CHOP, "ch_%d created by %s(%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 		    ch_num, current->comm, task_pid_nr(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 	return put_user(ch_num, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678)  * cm_chan_close() - Close channel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679)  * @filp:	Pointer to file object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680)  * @arg:	Channel to close
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) static int cm_chan_close(struct file *filp, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	u16 __user *p = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	u16 ch_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 	struct rio_channel *ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 	if (get_user(ch_num, p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 	riocm_debug(CHOP, "ch_%d by %s(%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 		    ch_num, current->comm, task_pid_nr(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	spin_lock_bh(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 	ch = idr_find(&ch_idr, ch_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	if (!ch) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 		spin_unlock_bh(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 	if (ch->filp != filp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 		spin_unlock_bh(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 	idr_remove(&ch_idr, ch->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 	spin_unlock_bh(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 	return riocm_ch_close(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711)  * cm_chan_bind() - Bind channel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712)  * @arg:	Channel number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) static int cm_chan_bind(void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 	struct rio_cm_channel chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 	if (copy_from_user(&chan, arg, sizeof(chan)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 	if (chan.mport_id >= RIO_MAX_MPORTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 	return riocm_ch_bind(chan.id, chan.mport_id, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727)  * cm_chan_listen() - Listen on channel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728)  * @arg:	Channel number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) static int cm_chan_listen(void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 	u16 __user *p = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 	u16 ch_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 	if (get_user(ch_num, p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 	return riocm_ch_listen(ch_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742)  * cm_chan_accept() - Accept incoming connection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743)  * @filp:	Pointer to file object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744)  * @arg:	Channel number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) static int cm_chan_accept(struct file *filp, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 	struct rio_cm_accept param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 	long accept_to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 	struct rio_channel *ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	if (copy_from_user(&param, arg, sizeof(param)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	riocm_debug(CHOP, "on ch_%d by %s(%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 		    param.ch_num, current->comm, task_pid_nr(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 	accept_to = param.wait_to ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 			msecs_to_jiffies(param.wait_to) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 	ch = riocm_ch_accept(param.ch_num, &param.ch_num, accept_to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 	if (IS_ERR(ch))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 		return PTR_ERR(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 	ch->filp = filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 	riocm_debug(CHOP, "new ch_%d for %s(%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 		    ch->id, current->comm, task_pid_nr(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 	if (copy_to_user(arg, &param, sizeof(param)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775)  * cm_chan_connect() - Connect on channel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776)  * @arg:	Channel information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) static int cm_chan_connect(void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 	struct rio_cm_channel chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	struct cm_dev *cm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	struct cm_peer *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 	int ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	if (copy_from_user(&chan, arg, sizeof(chan)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	if (chan.mport_id >= RIO_MAX_MPORTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 	down_read(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	/* Find matching cm_dev object */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 	list_for_each_entry(cm, &cm_dev_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 		if (cm->mport->id == chan.mport_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 	if (chan.remote_destid >= RIO_ANY_DESTID(cm->mport->sys_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 	/* Find corresponding RapidIO endpoint device object */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 	ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 	list_for_each_entry(peer, &cm->peers, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 		if (peer->rdev->destid == chan.remote_destid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 	up_read(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 	return riocm_ch_connect(chan.id, cm, peer, chan.remote_channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 	up_read(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830)  * cm_chan_msg_send() - Send a message through channel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831)  * @arg:	Outbound message information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) static int cm_chan_msg_send(void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 	struct rio_cm_msg msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 	void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 	if (copy_from_user(&msg, arg, sizeof(msg)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 	if (msg.size > RIO_MAX_MSG_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	buf = memdup_user((void __user *)(uintptr_t)msg.msg, msg.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 	if (IS_ERR(buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 		return PTR_ERR(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 	ret = riocm_ch_send(msg.ch_num, buf, msg.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855)  * cm_chan_msg_rcv() - Receive a message through channel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856)  * @arg:	Inbound message information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) static int cm_chan_msg_rcv(void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 	struct rio_cm_msg msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 	struct rio_channel *ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 	void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 	long rxto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 	int ret = 0, msg_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 	if (copy_from_user(&msg, arg, sizeof(msg)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	if (msg.ch_num == 0 || msg.size == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 	ch = riocm_get_channel(msg.ch_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 	if (!ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 	rxto = msg.rxto ? msecs_to_jiffies(msg.rxto) : MAX_SCHEDULE_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 	ret = riocm_ch_receive(ch, &buf, rxto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 	msg_size = min(msg.size, (u16)(RIO_MAX_MSG_SIZE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 	if (copy_to_user((void __user *)(uintptr_t)msg.msg, buf, msg_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 	riocm_ch_free_rxbuf(ch, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 	riocm_put_channel(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894)  * riocm_cdev_ioctl() - IOCTL requests handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) static long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) riocm_cdev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 	case RIO_CM_EP_GET_LIST_SIZE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 		return cm_ep_get_list_size((void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 	case RIO_CM_EP_GET_LIST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 		return cm_ep_get_list((void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 	case RIO_CM_CHAN_CREATE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 		return cm_chan_create(filp, (void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 	case RIO_CM_CHAN_CLOSE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 		return cm_chan_close(filp, (void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	case RIO_CM_CHAN_BIND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 		return cm_chan_bind((void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 	case RIO_CM_CHAN_LISTEN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 		return cm_chan_listen((void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 	case RIO_CM_CHAN_ACCEPT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 		return cm_chan_accept(filp, (void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 	case RIO_CM_CHAN_CONNECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 		return cm_chan_connect((void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 	case RIO_CM_CHAN_SEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 		return cm_chan_msg_send((void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 	case RIO_CM_CHAN_RECEIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 		return cm_chan_msg_rcv((void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 	case RIO_CM_MPORT_GET_LIST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 		return cm_mport_get_list((void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) static const struct file_operations riocm_cdev_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 	.open		= riocm_cdev_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 	.release	= riocm_cdev_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 	.unlocked_ioctl = riocm_cdev_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937)  * riocm_add_dev - add new remote RapidIO device into channel management core
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938)  * @dev: device object associated with RapidIO device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939)  * @sif: subsystem interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941)  * Adds the specified RapidIO device (if applicable) into peers list of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942)  * the corresponding channel management device (cm_dev).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) static int riocm_add_dev(struct device *dev, struct subsys_interface *sif)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 	struct cm_peer *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 	struct rio_dev *rdev = to_rio_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 	struct cm_dev *cm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 	/* Check if the remote device has capabilities required to support CM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 	if (!dev_cm_capable(rdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 	riocm_debug(RDEV, "(%s)", rio_name(rdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 	peer = kmalloc(sizeof(*peer), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 	if (!peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 	/* Find a corresponding cm_dev object */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 	down_write(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	list_for_each_entry(cm, &cm_dev_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 		if (cm->mport == rdev->net->hport)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 			goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 	up_write(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 	kfree(peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 	return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 	peer->rdev = rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 	list_add_tail(&peer->node, &cm->peers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 	cm->npeers++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 	up_write(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981)  * riocm_remove_dev - remove remote RapidIO device from channel management core
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982)  * @dev: device object associated with RapidIO device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983)  * @sif: subsystem interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985)  * Removes the specified RapidIO device (if applicable) from peers list of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986)  * the corresponding channel management device (cm_dev).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) static void riocm_remove_dev(struct device *dev, struct subsys_interface *sif)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 	struct rio_dev *rdev = to_rio_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 	struct cm_dev *cm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 	struct cm_peer *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 	struct rio_channel *ch, *_c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	bool found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 	LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 	/* Check if the remote device has capabilities required to support CM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 	if (!dev_cm_capable(rdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 	riocm_debug(RDEV, "(%s)", rio_name(rdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 	/* Find matching cm_dev object */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 	down_write(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 	list_for_each_entry(cm, &cm_dev_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 		if (cm->mport == rdev->net->hport) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 			found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 	if (!found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 		up_write(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 	/* Remove remote device from the list of peers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 	found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 	list_for_each_entry(peer, &cm->peers, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 		if (peer->rdev == rdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 			riocm_debug(RDEV, "removing peer %s", rio_name(rdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 			found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 			list_del(&peer->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 			cm->npeers--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 			kfree(peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 	up_write(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 	if (!found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 	 * Release channels associated with this peer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 	spin_lock_bh(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 	idr_for_each_entry(&ch_idr, ch, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 		if (ch && ch->rdev == rdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 			if (atomic_read(&rdev->state) != RIO_DEVICE_SHUTDOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 				riocm_exch(ch, RIO_CM_DISCONNECT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 			idr_remove(&ch_idr, ch->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 			list_add(&ch->ch_node, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 	spin_unlock_bh(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 	if (!list_empty(&list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 		list_for_each_entry_safe(ch, _c, &list, ch_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 			list_del(&ch->ch_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 			riocm_ch_close(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060)  * riocm_cdev_add() - Create rio_cm char device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061)  * @devno: device number assigned to device (MAJ + MIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) static int riocm_cdev_add(dev_t devno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 	cdev_init(&riocm_cdev.cdev, &riocm_cdev_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 	riocm_cdev.cdev.owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 	ret = cdev_add(&riocm_cdev.cdev, devno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 		riocm_error("Cannot register a device with error %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 	riocm_cdev.dev = device_create(dev_class, NULL, devno, NULL, DEV_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 	if (IS_ERR(riocm_cdev.dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 		cdev_del(&riocm_cdev.cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 		return PTR_ERR(riocm_cdev.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 	riocm_debug(MPORT, "Added %s cdev(%d:%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 		    DEV_NAME, MAJOR(devno), MINOR(devno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088)  * riocm_add_mport - add new local mport device into channel management core
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089)  * @dev: device object associated with mport
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090)  * @class_intf: class interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092)  * When a new mport device is added, CM immediately reserves inbound and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093)  * outbound RapidIO mailboxes that will be used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) static int riocm_add_mport(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 			   struct class_interface *class_intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 	struct cm_dev *cm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 	struct rio_mport *mport = to_rio_mport(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 	riocm_debug(MPORT, "add mport %s", mport->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 	cm = kzalloc(sizeof(*cm), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 	if (!cm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 	cm->mport = mport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 	rc = rio_request_outb_mbox(mport, cm, cmbox,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 				   RIOCM_TX_RING_SIZE, riocm_outb_msg_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 		riocm_error("failed to allocate OBMBOX_%d on %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 			    cmbox, mport->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 		kfree(cm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 	rc = rio_request_inb_mbox(mport, cm, cmbox,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 				  RIOCM_RX_RING_SIZE, riocm_inb_msg_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 		riocm_error("failed to allocate IBMBOX_%d on %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 			    cmbox, mport->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 		rio_release_outb_mbox(mport, cmbox);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 		kfree(cm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 	cm->rx_wq = create_workqueue(DRV_NAME "/rxq");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 	if (!cm->rx_wq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 		rio_release_inb_mbox(mport, cmbox);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 		rio_release_outb_mbox(mport, cmbox);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 		kfree(cm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 	 * Allocate and register inbound messaging buffers to be ready
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 	 * to receive channel and system management requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 	for (i = 0; i < RIOCM_RX_RING_SIZE; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 		cm->rx_buf[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 	cm->rx_slots = RIOCM_RX_RING_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 	mutex_init(&cm->rx_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 	riocm_rx_fill(cm, RIOCM_RX_RING_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 	INIT_WORK(&cm->rx_work, rio_ibmsg_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 	cm->tx_slot = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 	cm->tx_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 	cm->tx_ack_slot = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 	spin_lock_init(&cm->tx_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 	INIT_LIST_HEAD(&cm->peers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 	cm->npeers = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 	INIT_LIST_HEAD(&cm->tx_reqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 	down_write(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 	list_add_tail(&cm->list, &cm_dev_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 	up_write(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167)  * riocm_remove_mport - remove local mport device from channel management core
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168)  * @dev: device object associated with mport
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169)  * @class_intf: class interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171)  * Removes a local mport device from the list of registered devices that provide
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172)  * channel management services. Returns an error if the specified mport is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173)  * registered with the CM core.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) static void riocm_remove_mport(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 			       struct class_interface *class_intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 	struct rio_mport *mport = to_rio_mport(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 	struct cm_dev *cm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 	struct cm_peer *peer, *temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 	struct rio_channel *ch, *_c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 	bool found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 	LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 	riocm_debug(MPORT, "%s", mport->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 	/* Find a matching cm_dev object */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 	down_write(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 	list_for_each_entry(cm, &cm_dev_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 		if (cm->mport == mport) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 			list_del(&cm->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 			found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 	up_write(&rdev_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 	if (!found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 	flush_workqueue(cm->rx_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 	destroy_workqueue(cm->rx_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 	/* Release channels bound to this mport */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 	spin_lock_bh(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 	idr_for_each_entry(&ch_idr, ch, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 		if (ch->cmdev == cm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 			riocm_debug(RDEV, "%s drop ch_%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 				    mport->name, ch->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 			idr_remove(&ch_idr, ch->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 			list_add(&ch->ch_node, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 	spin_unlock_bh(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 	if (!list_empty(&list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 		list_for_each_entry_safe(ch, _c, &list, ch_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 			list_del(&ch->ch_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 			riocm_ch_close(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 	rio_release_inb_mbox(mport, cmbox);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 	rio_release_outb_mbox(mport, cmbox);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 	/* Remove and free peer entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 	if (!list_empty(&cm->peers))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 		riocm_debug(RDEV, "ATTN: peer list not empty");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 	list_for_each_entry_safe(peer, temp, &cm->peers, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 		riocm_debug(RDEV, "removing peer %s", rio_name(peer->rdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 		list_del(&peer->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 		kfree(peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 	riocm_rx_free(cm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 	kfree(cm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 	riocm_debug(MPORT, "%s done", mport->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) static int rio_cm_shutdown(struct notifier_block *nb, unsigned long code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 	void *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 	struct rio_channel *ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 	LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 	riocm_debug(EXIT, ".");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 	 * If there are any channels left in connected state send
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 	 * close notification to the connection partner.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 	 * First build a list of channels that require a closing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 	 * notification because function riocm_send_close() should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 	 * be called outside of spinlock protected code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 	spin_lock_bh(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 	idr_for_each_entry(&ch_idr, ch, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 		if (ch->state == RIO_CM_CONNECTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 			riocm_debug(EXIT, "close ch %d", ch->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 			idr_remove(&ch_idr, ch->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 			list_add(&ch->ch_node, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 	spin_unlock_bh(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 	list_for_each_entry(ch, &list, ch_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 		riocm_send_close(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 	return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273)  * riocm_interface handles addition/removal of remote RapidIO devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) static struct subsys_interface riocm_interface = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 	.name		= "rio_cm",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 	.subsys		= &rio_bus_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 	.add_dev	= riocm_add_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 	.remove_dev	= riocm_remove_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283)  * rio_mport_interface handles addition/removal local mport devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) static struct class_interface rio_mport_interface __refdata = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 	.class = &rio_mport_class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 	.add_dev = riocm_add_mport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 	.remove_dev = riocm_remove_mport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) static struct notifier_block rio_cm_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 	.notifier_call = rio_cm_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) static int __init riocm_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 	/* Create device class needed by udev */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 	dev_class = class_create(THIS_MODULE, DRV_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 	if (IS_ERR(dev_class)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 		riocm_error("Cannot create " DRV_NAME " class");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 		return PTR_ERR(dev_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 	ret = alloc_chrdev_region(&dev_number, 0, 1, DRV_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 		class_destroy(dev_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 	dev_major = MAJOR(dev_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 	dev_minor_base = MINOR(dev_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 	riocm_debug(INIT, "Registered class with %d major", dev_major);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 	 * Register as rapidio_port class interface to get notifications about
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 	 * mport additions and removals.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 	ret = class_interface_register(&rio_mport_interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 		riocm_error("class_interface_register error: %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 		goto err_reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 	 * Register as RapidIO bus interface to get notifications about
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 	 * addition/removal of remote RapidIO devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) 	ret = subsys_interface_register(&riocm_interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) 		riocm_error("subsys_interface_register error: %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 		goto err_cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 	ret = register_reboot_notifier(&rio_cm_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 		riocm_error("failed to register reboot notifier (err=%d)", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 		goto err_sif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 	ret = riocm_cdev_add(dev_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 		unregister_reboot_notifier(&rio_cm_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 		goto err_sif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) err_sif:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 	subsys_interface_unregister(&riocm_interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) err_cl:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 	class_interface_unregister(&rio_mport_interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) err_reg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 	unregister_chrdev_region(dev_number, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 	class_destroy(dev_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) static void __exit riocm_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 	riocm_debug(EXIT, "enter");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 	unregister_reboot_notifier(&rio_cm_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 	subsys_interface_unregister(&riocm_interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 	class_interface_unregister(&rio_mport_interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 	idr_destroy(&ch_idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 	device_unregister(riocm_cdev.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 	cdev_del(&(riocm_cdev.cdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 	class_destroy(dev_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 	unregister_chrdev_region(dev_number, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) late_initcall(riocm_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) module_exit(riocm_exit);