1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * rio_cm - RapidIO Channelized Messaging Driver
5 * Copyright 2013-2016 Integrated Device Technology, Inc.
6 * Copyright (c) 2015, Prodrive Technologies
7 * Copyright (c) 2015, RapidIO Trade Association
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/delay.h>
14 #include <linux/sched.h>
15 #include <linux/rio.h>
16 #include <linux/rio_drv.h>
17 #include <linux/slab.h>
18 #include <linux/idr.h>
19 #include <linux/interrupt.h>
20 #include <linux/cdev.h>
22 #include <linux/poll.h>
23 #include <linux/reboot.h>
24 #include <linux/bitops.h>
25 #include <linux/printk.h>
26 #include <linux/rio_cm_cdev.h>
28 #define DRV_NAME "rio_cm"
29 #define DRV_VERSION "1.0.0"
31 #define DRV_DESC "RapidIO Channelized Messaging Driver"
32 #define DEV_NAME "rio_cm"
34 /* Debug output filtering masks */
37 DBG_INIT = BIT(0), /* driver init */
38 DBG_EXIT = BIT(1), /* driver exit */
39 DBG_MPORT = BIT(2), /* mport add/remove */
40 DBG_RDEV = BIT(3), /* RapidIO device add/remove */
41 DBG_CHOP = BIT(4), /* channel operations */
42 DBG_WAIT = BIT(5), /* waiting for events */
43 DBG_TX = BIT(6), /* message TX */
44 DBG_TX_EVENT = BIT(7), /* message TX event */
45 DBG_RX_DATA = BIT(8), /* inbound data messages */
46 DBG_RX_CMD = BIT(9), /* inbound REQ/ACK/NACK messages */
51 #define riocm_debug(level, fmt, arg...) \
53 if (DBG_##level & dbg_level) \
54 pr_debug(DRV_NAME ": %s " fmt "\n", \
58 #define riocm_debug(level, fmt, arg...) \
59 no_printk(KERN_DEBUG pr_fmt(DRV_NAME fmt "\n"), ##arg)
62 #define riocm_warn(fmt, arg...) \
63 pr_warn(DRV_NAME ": %s WARNING " fmt "\n", __func__, ##arg)
65 #define riocm_error(fmt, arg...) \
66 pr_err(DRV_NAME ": %s ERROR " fmt "\n", __func__, ##arg)
70 module_param(cmbox, int, S_IRUGO);
71 MODULE_PARM_DESC(cmbox, "RapidIO Mailbox number (default 1)");
73 static int chstart = 256;
74 module_param(chstart, int, S_IRUGO);
75 MODULE_PARM_DESC(chstart,
76 "Start channel number for dynamic allocation (default 256)");
79 static u32 dbg_level = DBG_NONE;
80 module_param(dbg_level, uint, S_IWUSR | S_IRUGO);
81 MODULE_PARM_DESC(dbg_level, "Debugging output level (default 0 = none)");
84 MODULE_AUTHOR(DRV_AUTHOR);
85 MODULE_DESCRIPTION(DRV_DESC);
86 MODULE_LICENSE("GPL");
87 MODULE_VERSION(DRV_VERSION);
89 #define RIOCM_TX_RING_SIZE 128
90 #define RIOCM_RX_RING_SIZE 128
91 #define RIOCM_CONNECT_TO 3 /* connect response TO (in sec) */
93 #define RIOCM_MAX_CHNUM 0xffff /* Use full range of u16 field */
94 #define RIOCM_CHNUM_AUTO 0
95 #define RIOCM_MAX_EP_COUNT 0x10000 /* Max number of endpoints */
107 enum rio_cm_pkt_type {
119 struct rio_ch_base_bhdr {
122 #define RIO_HDR_LETTER_MASK 0xffff0000
123 #define RIO_HDR_MBOX_MASK 0x0000ffff
127 } __attribute__((__packed__));
129 struct rio_ch_chan_hdr {
130 struct rio_ch_base_bhdr bhdr;
136 } __attribute__((__packed__));
139 struct list_head node;
140 struct rio_dev *rdev;
146 struct list_head list;
147 struct rio_mport *mport;
148 void *rx_buf[RIOCM_RX_RING_SIZE];
150 struct mutex rx_lock;
152 void *tx_buf[RIOCM_TX_RING_SIZE];
156 struct list_head tx_reqs;
159 struct list_head peers;
161 struct workqueue_struct *rx_wq;
162 struct work_struct rx_work;
165 struct chan_rx_ring {
166 void *buf[RIOCM_RX_RING_SIZE];
171 /* Tracking RX buffers reported to upper level */
172 void *inuse[RIOCM_RX_RING_SIZE];
177 u16 id; /* local channel ID */
178 struct kref ref; /* channel refcount */
180 struct cm_dev *cmdev; /* associated CM device object */
181 struct rio_dev *rdev; /* remote RapidIO device */
182 enum rio_cm_state state;
186 u32 loc_destid; /* local destID */
187 u32 rem_destid; /* remote destID */
188 u16 rem_channel; /* remote channel ID */
189 struct list_head accept_queue;
190 struct list_head ch_node;
191 struct completion comp;
192 struct completion comp_close;
193 struct chan_rx_ring rx_ring;
197 struct list_head node;
198 struct rio_dev *rdev;
202 struct work_struct work;
208 struct list_head node;
209 u32 destid; /* requester destID */
210 u16 chan; /* requester channel ID */
211 struct cm_dev *cmdev;
215 * A channel_dev structure represents a CM_CDEV
216 * @cdev Character device
217 * @dev Associated device object
224 static struct rio_channel *riocm_ch_alloc(u16 ch_num);
225 static void riocm_ch_free(struct kref *ref);
226 static int riocm_post_send(struct cm_dev *cm, struct rio_dev *rdev,
227 void *buffer, size_t len);
228 static int riocm_ch_close(struct rio_channel *ch);
230 static DEFINE_SPINLOCK(idr_lock);
231 static DEFINE_IDR(ch_idr);
233 static LIST_HEAD(cm_dev_list);
234 static DECLARE_RWSEM(rdev_sem);
236 static const struct class dev_class = {
239 static unsigned int dev_major;
240 static unsigned int dev_minor_base;
241 static dev_t dev_number;
242 static struct channel_dev riocm_cdev;
244 #define is_msg_capable(src_ops, dst_ops) \
245 ((src_ops & RIO_SRC_OPS_DATA_MSG) && \
246 (dst_ops & RIO_DST_OPS_DATA_MSG))
247 #define dev_cm_capable(dev) \
248 is_msg_capable(dev->src_ops, dev->dst_ops)
250 static int riocm_cmp(struct rio_channel *ch, enum rio_cm_state cmp)
254 spin_lock_bh(&ch->lock);
255 ret = (ch->state == cmp);
256 spin_unlock_bh(&ch->lock);
260 static int riocm_cmp_exch(struct rio_channel *ch,
261 enum rio_cm_state cmp, enum rio_cm_state exch)
265 spin_lock_bh(&ch->lock);
266 ret = (ch->state == cmp);
269 spin_unlock_bh(&ch->lock);
273 static enum rio_cm_state riocm_exch(struct rio_channel *ch,
274 enum rio_cm_state exch)
276 enum rio_cm_state old;
278 spin_lock_bh(&ch->lock);
281 spin_unlock_bh(&ch->lock);
285 static struct rio_channel *riocm_get_channel(u16 nr)
287 struct rio_channel *ch;
289 spin_lock_bh(&idr_lock);
290 ch = idr_find(&ch_idr, nr);
293 spin_unlock_bh(&idr_lock);
297 static void riocm_put_channel(struct rio_channel *ch)
299 kref_put(&ch->ref, riocm_ch_free);
302 static void *riocm_rx_get_msg(struct cm_dev *cm)
307 msg = rio_get_inb_message(cm->mport, cmbox);
309 for (i = 0; i < RIOCM_RX_RING_SIZE; i++) {
310 if (cm->rx_buf[i] == msg) {
311 cm->rx_buf[i] = NULL;
317 if (i == RIOCM_RX_RING_SIZE)
318 riocm_warn("no record for buffer 0x%p", msg);
325 * riocm_rx_fill - fills a ring of receive buffers for given cm device
327 * @nent: max number of entries to fill
331 static void riocm_rx_fill(struct cm_dev *cm, int nent)
335 if (cm->rx_slots == 0)
338 for (i = 0; i < RIOCM_RX_RING_SIZE && cm->rx_slots && nent; i++) {
339 if (cm->rx_buf[i] == NULL) {
340 cm->rx_buf[i] = kmalloc(RIO_MAX_MSG_SIZE, GFP_KERNEL);
341 if (cm->rx_buf[i] == NULL)
343 rio_add_inb_buffer(cm->mport, cmbox, cm->rx_buf[i]);
351 * riocm_rx_free - frees all receive buffers associated with given cm device
356 static void riocm_rx_free(struct cm_dev *cm)
360 for (i = 0; i < RIOCM_RX_RING_SIZE; i++) {
361 if (cm->rx_buf[i] != NULL) {
362 kfree(cm->rx_buf[i]);
363 cm->rx_buf[i] = NULL;
369 * riocm_req_handler - connection request handler
371 * @req_data: pointer to the request packet
373 * Returns: 0 if success, or
374 * -EINVAL if channel is not in correct state,
375 * -ENODEV if cannot find a channel with specified ID,
376 * -ENOMEM if unable to allocate memory to store the request
378 static int riocm_req_handler(struct cm_dev *cm, void *req_data)
380 struct rio_channel *ch;
381 struct conn_req *req;
382 struct rio_ch_chan_hdr *hh = req_data;
385 chnum = ntohs(hh->dst_ch);
387 ch = riocm_get_channel(chnum);
392 if (ch->state != RIO_CM_LISTEN) {
393 riocm_debug(RX_CMD, "channel %d is not in listen state", chnum);
394 riocm_put_channel(ch);
398 req = kzalloc(sizeof(*req), GFP_KERNEL);
400 riocm_put_channel(ch);
404 req->destid = ntohl(hh->bhdr.src_id);
405 req->chan = ntohs(hh->src_ch);
408 spin_lock_bh(&ch->lock);
409 list_add_tail(&req->node, &ch->accept_queue);
410 spin_unlock_bh(&ch->lock);
412 riocm_put_channel(ch);
418 * riocm_resp_handler - response to connection request handler
419 * @resp_data: pointer to the response packet
421 * Returns: 0 if success, or
422 * -EINVAL if channel is not in correct state,
423 * -ENODEV if cannot find a channel with specified ID,
425 static int riocm_resp_handler(void *resp_data)
427 struct rio_channel *ch;
428 struct rio_ch_chan_hdr *hh = resp_data;
431 chnum = ntohs(hh->dst_ch);
432 ch = riocm_get_channel(chnum);
436 if (ch->state != RIO_CM_CONNECT) {
437 riocm_put_channel(ch);
441 riocm_exch(ch, RIO_CM_CONNECTED);
442 ch->rem_channel = ntohs(hh->src_ch);
444 riocm_put_channel(ch);
450 * riocm_close_handler - channel close request handler
451 * @req_data: pointer to the request packet
453 * Returns: 0 if success, or
454 * -ENODEV if cannot find a channel with specified ID,
455 * + error codes returned by riocm_ch_close.
457 static int riocm_close_handler(void *data)
459 struct rio_channel *ch;
460 struct rio_ch_chan_hdr *hh = data;
463 riocm_debug(RX_CMD, "for ch=%d", ntohs(hh->dst_ch));
465 spin_lock_bh(&idr_lock);
466 ch = idr_find(&ch_idr, ntohs(hh->dst_ch));
468 spin_unlock_bh(&idr_lock);
471 idr_remove(&ch_idr, ch->id);
472 spin_unlock_bh(&idr_lock);
474 riocm_exch(ch, RIO_CM_DISCONNECT);
476 ret = riocm_ch_close(ch);
478 riocm_debug(RX_CMD, "riocm_ch_close() returned %d", ret);
484 * rio_cm_handler - function that services request (non-data) packets
486 * @data: pointer to the packet
488 static void rio_cm_handler(struct cm_dev *cm, void *data)
490 struct rio_ch_chan_hdr *hdr;
492 if (!rio_mport_is_running(cm->mport))
497 riocm_debug(RX_CMD, "OP=%x for ch=%d from %d",
498 hdr->ch_op, ntohs(hdr->dst_ch), ntohs(hdr->src_ch));
500 switch (hdr->ch_op) {
502 riocm_req_handler(cm, data);
505 riocm_resp_handler(data);
508 riocm_close_handler(data);
511 riocm_error("Invalid packet header");
519 * rio_rx_data_handler - received data packet handler
523 * Returns: 0 if success, or
524 * -ENODEV if cannot find a channel with specified ID,
525 * -EIO if channel is not in CONNECTED state,
526 * -ENOMEM if channel RX queue is full (packet discarded)
528 static int rio_rx_data_handler(struct cm_dev *cm, void *buf)
530 struct rio_ch_chan_hdr *hdr;
531 struct rio_channel *ch;
535 riocm_debug(RX_DATA, "for ch=%d", ntohs(hdr->dst_ch));
537 ch = riocm_get_channel(ntohs(hdr->dst_ch));
539 /* Discard data message for non-existing channel */
544 /* Place pointer to the buffer into channel's RX queue */
545 spin_lock(&ch->lock);
547 if (ch->state != RIO_CM_CONNECTED) {
548 /* Channel is not ready to receive data, discard a packet */
549 riocm_debug(RX_DATA, "ch=%d is in wrong state=%d",
551 spin_unlock(&ch->lock);
553 riocm_put_channel(ch);
557 if (ch->rx_ring.count == RIOCM_RX_RING_SIZE) {
558 /* If RX ring is full, discard a packet */
559 riocm_debug(RX_DATA, "ch=%d is full", ch->id);
560 spin_unlock(&ch->lock);
562 riocm_put_channel(ch);
566 ch->rx_ring.buf[ch->rx_ring.head] = buf;
569 ch->rx_ring.head %= RIOCM_RX_RING_SIZE;
573 spin_unlock(&ch->lock);
574 riocm_put_channel(ch);
580 * rio_ibmsg_handler - inbound message packet handler
582 static void rio_ibmsg_handler(struct work_struct *work)
584 struct cm_dev *cm = container_of(work, struct cm_dev, rx_work);
586 struct rio_ch_chan_hdr *hdr;
588 if (!rio_mport_is_running(cm->mport))
592 mutex_lock(&cm->rx_lock);
593 data = riocm_rx_get_msg(cm);
595 riocm_rx_fill(cm, 1);
596 mutex_unlock(&cm->rx_lock);
603 if (hdr->bhdr.type != RIO_CM_CHAN) {
604 /* For now simply discard packets other than channel */
605 riocm_error("Unsupported TYPE code (0x%x). Msg dropped",
611 /* Process a channel message */
612 if (hdr->ch_op == CM_DATA_MSG)
613 rio_rx_data_handler(cm, data);
615 rio_cm_handler(cm, data);
619 static void riocm_inb_msg_event(struct rio_mport *mport, void *dev_id,
622 struct cm_dev *cm = dev_id;
624 if (rio_mport_is_running(cm->mport) && !work_pending(&cm->rx_work))
625 queue_work(cm->rx_wq, &cm->rx_work);
629 * rio_txcq_handler - TX completion handler
631 * @slot: TX queue slot
633 * TX completion handler also ensures that pending request packets are placed
634 * into transmit queue as soon as a free slot becomes available. This is done
635 * to give higher priority to request packets during high intensity data flow.
637 static void rio_txcq_handler(struct cm_dev *cm, int slot)
641 /* ATTN: Add TX completion notification if/when direct buffer
642 * transfer is implemented. At this moment only correct tracking
643 * of tx_count is important.
645 riocm_debug(TX_EVENT, "for mport_%d slot %d tx_cnt %d",
646 cm->mport->id, slot, cm->tx_cnt);
648 spin_lock(&cm->tx_lock);
649 ack_slot = cm->tx_ack_slot;
651 if (ack_slot == slot)
652 riocm_debug(TX_EVENT, "slot == ack_slot");
654 while (cm->tx_cnt && ((ack_slot != slot) ||
655 (cm->tx_cnt == RIOCM_TX_RING_SIZE))) {
657 cm->tx_buf[ack_slot] = NULL;
659 ack_slot &= (RIOCM_TX_RING_SIZE - 1);
663 if (cm->tx_cnt < 0 || cm->tx_cnt > RIOCM_TX_RING_SIZE)
664 riocm_error("tx_cnt %d out of sync", cm->tx_cnt);
666 WARN_ON((cm->tx_cnt < 0) || (cm->tx_cnt > RIOCM_TX_RING_SIZE));
668 cm->tx_ack_slot = ack_slot;
671 * If there are pending requests, insert them into transmit queue
673 if (!list_empty(&cm->tx_reqs) && (cm->tx_cnt < RIOCM_TX_RING_SIZE)) {
674 struct tx_req *req, *_req;
677 list_for_each_entry_safe(req, _req, &cm->tx_reqs, node) {
678 list_del(&req->node);
679 cm->tx_buf[cm->tx_slot] = req->buffer;
680 rc = rio_add_outb_message(cm->mport, req->rdev, cmbox,
681 req->buffer, req->len);
687 cm->tx_slot &= (RIOCM_TX_RING_SIZE - 1);
688 if (cm->tx_cnt == RIOCM_TX_RING_SIZE)
693 spin_unlock(&cm->tx_lock);
696 static void riocm_outb_msg_event(struct rio_mport *mport, void *dev_id,
699 struct cm_dev *cm = dev_id;
701 if (cm && rio_mport_is_running(cm->mport))
702 rio_txcq_handler(cm, slot);
705 static int riocm_queue_req(struct cm_dev *cm, struct rio_dev *rdev,
706 void *buffer, size_t len)
711 treq = kzalloc(sizeof(*treq), GFP_KERNEL);
716 treq->buffer = buffer;
719 spin_lock_irqsave(&cm->tx_lock, flags);
720 list_add_tail(&treq->node, &cm->tx_reqs);
721 spin_unlock_irqrestore(&cm->tx_lock, flags);
726 * riocm_post_send - helper function that places packet into msg TX queue
728 * @rdev: target RapidIO device object (required by outbound msg interface)
729 * @buffer: pointer to a packet buffer to send
730 * @len: length of data to transfer
731 * @req: request priority flag
733 * Returns: 0 if success, or error code otherwise.
735 static int riocm_post_send(struct cm_dev *cm, struct rio_dev *rdev,
736 void *buffer, size_t len)
741 spin_lock_irqsave(&cm->tx_lock, flags);
743 if (cm->mport == NULL) {
748 if (cm->tx_cnt == RIOCM_TX_RING_SIZE) {
749 riocm_debug(TX, "Tx Queue is full");
754 cm->tx_buf[cm->tx_slot] = buffer;
755 rc = rio_add_outb_message(cm->mport, rdev, cmbox, buffer, len);
757 riocm_debug(TX, "Add buf@%p destid=%x tx_slot=%d tx_cnt=%d",
758 buffer, rdev->destid, cm->tx_slot, cm->tx_cnt);
762 cm->tx_slot &= (RIOCM_TX_RING_SIZE - 1);
765 spin_unlock_irqrestore(&cm->tx_lock, flags);
770 * riocm_ch_send - sends a data packet to a remote device
771 * @ch_id: local channel ID
772 * @buf: pointer to a data buffer to send (including CM header)
773 * @len: length of data to transfer (including CM header)
775 * ATTN: ASSUMES THAT THE HEADER SPACE IS RESERVED PART OF THE DATA PACKET
777 * Returns: 0 if success, or
778 * -EINVAL if one or more input parameters is/are not valid,
779 * -ENODEV if cannot find a channel with specified ID,
780 * -EAGAIN if a channel is not in CONNECTED state,
781 * + error codes returned by HW send routine.
783 static int riocm_ch_send(u16 ch_id, void *buf, int len)
785 struct rio_channel *ch;
786 struct rio_ch_chan_hdr *hdr;
789 if (buf == NULL || ch_id == 0 || len == 0 || len > RIO_MAX_MSG_SIZE)
792 ch = riocm_get_channel(ch_id);
794 riocm_error("%s(%d) ch_%d not found", current->comm,
795 task_pid_nr(current), ch_id);
799 if (!riocm_cmp(ch, RIO_CM_CONNECTED)) {
805 * Fill buffer header section with corresponding channel data
809 hdr->bhdr.src_id = htonl(ch->loc_destid);
810 hdr->bhdr.dst_id = htonl(ch->rem_destid);
811 hdr->bhdr.src_mbox = cmbox;
812 hdr->bhdr.dst_mbox = cmbox;
813 hdr->bhdr.type = RIO_CM_CHAN;
814 hdr->ch_op = CM_DATA_MSG;
815 hdr->dst_ch = htons(ch->rem_channel);
816 hdr->src_ch = htons(ch->id);
817 hdr->msg_len = htons((u16)len);
819 /* ATTN: the function call below relies on the fact that underlying
820 * HW-specific add_outb_message() routine copies TX data into its own
821 * internal transfer buffer (true for all RIONET compatible mport
822 * drivers). Must be reviewed if mport driver uses the buffer directly.
825 ret = riocm_post_send(ch->cmdev, ch->rdev, buf, len);
827 riocm_debug(TX, "ch %d send_err=%d", ch->id, ret);
829 riocm_put_channel(ch);
833 static int riocm_ch_free_rxbuf(struct rio_channel *ch, void *buf)
835 int i, ret = -EINVAL;
837 spin_lock_bh(&ch->lock);
839 for (i = 0; i < RIOCM_RX_RING_SIZE; i++) {
840 if (ch->rx_ring.inuse[i] == buf) {
841 ch->rx_ring.inuse[i] = NULL;
842 ch->rx_ring.inuse_cnt--;
848 spin_unlock_bh(&ch->lock);
857 * riocm_ch_receive - fetch a data packet received for the specified channel
858 * @ch: local channel ID
859 * @buf: pointer to a packet buffer
860 * @timeout: timeout to wait for incoming packet (in jiffies)
862 * Returns: 0 and valid buffer pointer if success, or NULL pointer and one of:
863 * -EAGAIN if a channel is not in CONNECTED state,
864 * -ENOMEM if in-use tracking queue is full,
865 * -ETIME if wait timeout expired,
866 * -EINTR if wait was interrupted.
868 static int riocm_ch_receive(struct rio_channel *ch, void **buf, long timeout)
874 if (!riocm_cmp(ch, RIO_CM_CONNECTED)) {
879 if (ch->rx_ring.inuse_cnt == RIOCM_RX_RING_SIZE) {
880 /* If we do not have entries to track buffers given to upper
881 * layer, reject request.
887 wret = wait_for_completion_interruptible_timeout(&ch->comp, timeout);
889 riocm_debug(WAIT, "wait on %d returned %ld", ch->id, wret);
893 else if (wret == -ERESTARTSYS)
896 ret = riocm_cmp(ch, RIO_CM_CONNECTED) ? 0 : -ECONNRESET;
901 spin_lock_bh(&ch->lock);
903 rxmsg = ch->rx_ring.buf[ch->rx_ring.tail];
904 ch->rx_ring.buf[ch->rx_ring.tail] = NULL;
907 ch->rx_ring.tail %= RIOCM_RX_RING_SIZE;
910 for (i = 0; i < RIOCM_RX_RING_SIZE; i++) {
911 if (ch->rx_ring.inuse[i] == NULL) {
912 ch->rx_ring.inuse[i] = rxmsg;
913 ch->rx_ring.inuse_cnt++;
920 /* We have no entry to store pending message: drop it */
925 spin_unlock_bh(&ch->lock);
932 * riocm_ch_connect - sends a connect request to a remote device
933 * @loc_ch: local channel ID
934 * @cm: CM device to send connect request
935 * @peer: target RapidIO device
936 * @rem_ch: remote channel ID
938 * Returns: 0 if success, or
939 * -EINVAL if the channel is not in IDLE state,
940 * -EAGAIN if no connection request available immediately,
941 * -ETIME if ACK response timeout expired,
942 * -EINTR if wait for response was interrupted.
944 static int riocm_ch_connect(u16 loc_ch, struct cm_dev *cm,
945 struct cm_peer *peer, u16 rem_ch)
947 struct rio_channel *ch = NULL;
948 struct rio_ch_chan_hdr *hdr;
952 ch = riocm_get_channel(loc_ch);
956 if (!riocm_cmp_exch(ch, RIO_CM_IDLE, RIO_CM_CONNECT)) {
962 ch->rdev = peer->rdev;
964 ch->loc_destid = cm->mport->host_deviceid;
965 ch->rem_channel = rem_ch;
968 * Send connect request to the remote RapidIO device
971 hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
977 hdr->bhdr.src_id = htonl(ch->loc_destid);
978 hdr->bhdr.dst_id = htonl(peer->rdev->destid);
979 hdr->bhdr.src_mbox = cmbox;
980 hdr->bhdr.dst_mbox = cmbox;
981 hdr->bhdr.type = RIO_CM_CHAN;
982 hdr->ch_op = CM_CONN_REQ;
983 hdr->dst_ch = htons(rem_ch);
984 hdr->src_ch = htons(loc_ch);
986 /* ATTN: the function call below relies on the fact that underlying
987 * HW-specific add_outb_message() routine copies TX data into its
988 * internal transfer buffer. Must be reviewed if mport driver uses
989 * this buffer directly.
991 ret = riocm_post_send(cm, peer->rdev, hdr, sizeof(*hdr));
996 ret = riocm_queue_req(cm, peer->rdev, hdr, sizeof(*hdr));
1002 riocm_cmp_exch(ch, RIO_CM_CONNECT, RIO_CM_IDLE);
1006 /* Wait for connect response from the remote device */
1007 wret = wait_for_completion_interruptible_timeout(&ch->comp,
1008 RIOCM_CONNECT_TO * HZ);
1009 riocm_debug(WAIT, "wait on %d returns %ld", ch->id, wret);
1013 else if (wret == -ERESTARTSYS)
1016 ret = riocm_cmp(ch, RIO_CM_CONNECTED) ? 0 : -1;
1019 riocm_put_channel(ch);
1023 static int riocm_send_ack(struct rio_channel *ch)
1025 struct rio_ch_chan_hdr *hdr;
1028 hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
1032 hdr->bhdr.src_id = htonl(ch->loc_destid);
1033 hdr->bhdr.dst_id = htonl(ch->rem_destid);
1034 hdr->dst_ch = htons(ch->rem_channel);
1035 hdr->src_ch = htons(ch->id);
1036 hdr->bhdr.src_mbox = cmbox;
1037 hdr->bhdr.dst_mbox = cmbox;
1038 hdr->bhdr.type = RIO_CM_CHAN;
1039 hdr->ch_op = CM_CONN_ACK;
1041 /* ATTN: the function call below relies on the fact that underlying
1042 * add_outb_message() routine copies TX data into its internal transfer
1043 * buffer. Review if switching to direct buffer version.
1045 ret = riocm_post_send(ch->cmdev, ch->rdev, hdr, sizeof(*hdr));
1047 if (ret == -EBUSY && !riocm_queue_req(ch->cmdev,
1048 ch->rdev, hdr, sizeof(*hdr)))
1053 riocm_error("send ACK to ch_%d on %s failed (ret=%d)",
1054 ch->id, rio_name(ch->rdev), ret);
1059 * riocm_ch_accept - accept incoming connection request
1060 * @ch_id: channel ID
1061 * @new_ch_id: local mport device
1062 * @timeout: wait timeout (if 0 non-blocking call, do not wait if connection
1063 * request is not available).
1065 * Returns: pointer to new channel struct if success, or error-valued pointer:
1066 * -ENODEV - cannot find specified channel or mport,
1067 * -EINVAL - the channel is not in IDLE state,
1068 * -EAGAIN - no connection request available immediately (timeout=0),
1069 * -ENOMEM - unable to allocate new channel,
1070 * -ETIME - wait timeout expired,
1071 * -EINTR - wait was interrupted.
1073 static struct rio_channel *riocm_ch_accept(u16 ch_id, u16 *new_ch_id,
1076 struct rio_channel *ch;
1077 struct rio_channel *new_ch;
1078 struct conn_req *req;
1079 struct cm_peer *peer;
1084 ch = riocm_get_channel(ch_id);
1086 return ERR_PTR(-EINVAL);
1088 if (!riocm_cmp(ch, RIO_CM_LISTEN)) {
1093 /* Don't sleep if this is a non blocking call */
1095 if (!try_wait_for_completion(&ch->comp)) {
1100 riocm_debug(WAIT, "on %d", ch->id);
1102 wret = wait_for_completion_interruptible_timeout(&ch->comp,
1107 } else if (wret == -ERESTARTSYS) {
1113 spin_lock_bh(&ch->lock);
1115 if (ch->state != RIO_CM_LISTEN) {
1117 } else if (list_empty(&ch->accept_queue)) {
1118 riocm_debug(WAIT, "on %d accept_queue is empty on completion",
1123 spin_unlock_bh(&ch->lock);
1126 riocm_debug(WAIT, "on %d returns %d", ch->id, err);
1130 /* Create new channel for this connection */
1131 new_ch = riocm_ch_alloc(RIOCM_CHNUM_AUTO);
1133 if (IS_ERR(new_ch)) {
1134 riocm_error("failed to get channel for new req (%ld)",
1140 spin_lock_bh(&ch->lock);
1142 req = list_first_entry(&ch->accept_queue, struct conn_req, node);
1143 list_del(&req->node);
1144 new_ch->cmdev = ch->cmdev;
1145 new_ch->loc_destid = ch->loc_destid;
1146 new_ch->rem_destid = req->destid;
1147 new_ch->rem_channel = req->chan;
1149 spin_unlock_bh(&ch->lock);
1150 riocm_put_channel(ch);
1154 down_read(&rdev_sem);
1155 /* Find requester's device object */
1156 list_for_each_entry(peer, &new_ch->cmdev->peers, node) {
1157 if (peer->rdev->destid == new_ch->rem_destid) {
1158 riocm_debug(RX_CMD, "found matching device(%s)",
1159 rio_name(peer->rdev));
1167 /* If peer device object not found, simply ignore the request */
1169 goto err_put_new_ch;
1172 new_ch->rdev = peer->rdev;
1173 new_ch->state = RIO_CM_CONNECTED;
1174 spin_lock_init(&new_ch->lock);
1176 /* Acknowledge the connection request. */
1177 riocm_send_ack(new_ch);
1179 *new_ch_id = new_ch->id;
1183 spin_lock_bh(&idr_lock);
1184 idr_remove(&ch_idr, new_ch->id);
1185 spin_unlock_bh(&idr_lock);
1186 riocm_put_channel(new_ch);
1190 riocm_put_channel(ch);
1192 return ERR_PTR(err);
1196 * riocm_ch_listen - puts a channel into LISTEN state
1197 * @ch_id: channel ID
1199 * Returns: 0 if success, or
1200 * -EINVAL if the specified channel does not exists or
1201 * is not in CHAN_BOUND state.
1203 static int riocm_ch_listen(u16 ch_id)
1205 struct rio_channel *ch = NULL;
1208 riocm_debug(CHOP, "(ch_%d)", ch_id);
1210 ch = riocm_get_channel(ch_id);
1213 if (!riocm_cmp_exch(ch, RIO_CM_CHAN_BOUND, RIO_CM_LISTEN))
1215 riocm_put_channel(ch);
1220 * riocm_ch_bind - associate a channel object and an mport device
1221 * @ch_id: channel ID
1222 * @mport_id: local mport device ID
1223 * @context: pointer to the additional caller's context
1225 * Returns: 0 if success, or
1226 * -ENODEV if cannot find specified mport,
1227 * -EINVAL if the specified channel does not exist or
1228 * is not in IDLE state.
1230 static int riocm_ch_bind(u16 ch_id, u8 mport_id, void *context)
1232 struct rio_channel *ch = NULL;
1236 riocm_debug(CHOP, "ch_%d to mport_%d", ch_id, mport_id);
1238 /* Find matching cm_dev object */
1239 down_read(&rdev_sem);
1240 list_for_each_entry(cm, &cm_dev_list, list) {
1241 if ((cm->mport->id == mport_id) &&
1242 rio_mport_is_running(cm->mport)) {
1251 ch = riocm_get_channel(ch_id);
1257 spin_lock_bh(&ch->lock);
1258 if (ch->state != RIO_CM_IDLE) {
1259 spin_unlock_bh(&ch->lock);
1265 ch->loc_destid = cm->mport->host_deviceid;
1266 ch->context = context;
1267 ch->state = RIO_CM_CHAN_BOUND;
1268 spin_unlock_bh(&ch->lock);
1270 riocm_put_channel(ch);
1277 * riocm_ch_alloc - channel object allocation helper routine
1278 * @ch_num: channel ID (1 ... RIOCM_MAX_CHNUM, 0 = automatic)
1280 * Return value: pointer to newly created channel object,
1281 * or error-valued pointer
1283 static struct rio_channel *riocm_ch_alloc(u16 ch_num)
1287 struct rio_channel *ch;
1289 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
1291 return ERR_PTR(-ENOMEM);
1294 /* If requested, try to obtain the specified channel ID */
1298 /* Obtain channel ID from the dynamic allocation range */
1300 end = RIOCM_MAX_CHNUM + 1;
1303 idr_preload(GFP_KERNEL);
1304 spin_lock_bh(&idr_lock);
1305 id = idr_alloc_cyclic(&ch_idr, ch, start, end, GFP_NOWAIT);
1306 spin_unlock_bh(&idr_lock);
1311 return ERR_PTR(id == -ENOSPC ? -EBUSY : id);
1315 ch->state = RIO_CM_IDLE;
1316 spin_lock_init(&ch->lock);
1317 INIT_LIST_HEAD(&ch->accept_queue);
1318 INIT_LIST_HEAD(&ch->ch_node);
1319 init_completion(&ch->comp);
1320 init_completion(&ch->comp_close);
1321 kref_init(&ch->ref);
1322 ch->rx_ring.head = 0;
1323 ch->rx_ring.tail = 0;
1324 ch->rx_ring.count = 0;
1325 ch->rx_ring.inuse_cnt = 0;
1331 * riocm_ch_create - creates a new channel object and allocates ID for it
1332 * @ch_num: channel ID (1 ... RIOCM_MAX_CHNUM, 0 = automatic)
1334 * Allocates and initializes a new channel object. If the parameter ch_num > 0
1335 * and is within the valid range, riocm_ch_create tries to allocate the
1336 * specified ID for the new channel. If ch_num = 0, channel ID will be assigned
1337 * automatically from the range (chstart ... RIOCM_MAX_CHNUM).
1338 * Module parameter 'chstart' defines start of an ID range available for dynamic
1339 * allocation. Range below 'chstart' is reserved for pre-defined ID numbers.
1340 * Available channel numbers are limited by 16-bit size of channel numbers used
1341 * in the packet header.
1343 * Return value: PTR to rio_channel structure if successful (with channel number
1344 * updated via pointer) or error-valued pointer if error.
1346 static struct rio_channel *riocm_ch_create(u16 *ch_num)
1348 struct rio_channel *ch = NULL;
1350 ch = riocm_ch_alloc(*ch_num);
1353 riocm_debug(CHOP, "Failed to allocate channel %d (err=%ld)",
1354 *ch_num, PTR_ERR(ch));
1362 * riocm_ch_free - channel object release routine
1363 * @ref: pointer to a channel's kref structure
1365 static void riocm_ch_free(struct kref *ref)
1367 struct rio_channel *ch = container_of(ref, struct rio_channel, ref);
1370 riocm_debug(CHOP, "(ch_%d)", ch->id);
1372 if (ch->rx_ring.inuse_cnt) {
1374 i < RIOCM_RX_RING_SIZE && ch->rx_ring.inuse_cnt; i++) {
1375 if (ch->rx_ring.inuse[i] != NULL) {
1376 kfree(ch->rx_ring.inuse[i]);
1377 ch->rx_ring.inuse_cnt--;
1382 if (ch->rx_ring.count)
1383 for (i = 0; i < RIOCM_RX_RING_SIZE && ch->rx_ring.count; i++) {
1384 if (ch->rx_ring.buf[i] != NULL) {
1385 kfree(ch->rx_ring.buf[i]);
1386 ch->rx_ring.count--;
1390 complete(&ch->comp_close);
1393 static int riocm_send_close(struct rio_channel *ch)
1395 struct rio_ch_chan_hdr *hdr;
1399 * Send CH_CLOSE notification to the remote RapidIO device
1402 hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
1406 hdr->bhdr.src_id = htonl(ch->loc_destid);
1407 hdr->bhdr.dst_id = htonl(ch->rem_destid);
1408 hdr->bhdr.src_mbox = cmbox;
1409 hdr->bhdr.dst_mbox = cmbox;
1410 hdr->bhdr.type = RIO_CM_CHAN;
1411 hdr->ch_op = CM_CONN_CLOSE;
1412 hdr->dst_ch = htons(ch->rem_channel);
1413 hdr->src_ch = htons(ch->id);
1415 /* ATTN: the function call below relies on the fact that underlying
1416 * add_outb_message() routine copies TX data into its internal transfer
1417 * buffer. Needs to be reviewed if switched to direct buffer mode.
1419 ret = riocm_post_send(ch->cmdev, ch->rdev, hdr, sizeof(*hdr));
1421 if (ret == -EBUSY && !riocm_queue_req(ch->cmdev, ch->rdev,
1427 riocm_error("ch(%d) send CLOSE failed (ret=%d)", ch->id, ret);
1433 * riocm_ch_close - closes a channel object with specified ID (by local request)
1434 * @ch: channel to be closed
1436 static int riocm_ch_close(struct rio_channel *ch)
1438 unsigned long tmo = msecs_to_jiffies(3000);
1439 enum rio_cm_state state;
1443 riocm_debug(CHOP, "ch_%d by %s(%d)",
1444 ch->id, current->comm, task_pid_nr(current));
1446 state = riocm_exch(ch, RIO_CM_DESTROYING);
1447 if (state == RIO_CM_CONNECTED)
1448 riocm_send_close(ch);
1450 complete_all(&ch->comp);
1452 riocm_put_channel(ch);
1453 wret = wait_for_completion_interruptible_timeout(&ch->comp_close, tmo);
1455 riocm_debug(WAIT, "wait on %d returns %ld", ch->id, wret);
1458 /* Timeout on wait occurred */
1459 riocm_debug(CHOP, "%s(%d) timed out waiting for ch %d",
1460 current->comm, task_pid_nr(current), ch->id);
1462 } else if (wret == -ERESTARTSYS) {
1463 /* Wait_for_completion was interrupted by a signal */
1464 riocm_debug(CHOP, "%s(%d) wait for ch %d was interrupted",
1465 current->comm, task_pid_nr(current), ch->id);
1470 riocm_debug(CHOP, "ch_%d resources released", ch->id);
1473 riocm_debug(CHOP, "failed to release ch_%d resources", ch->id);
1480 * riocm_cdev_open() - Open character device
1482 static int riocm_cdev_open(struct inode *inode, struct file *filp)
1484 riocm_debug(INIT, "by %s(%d) filp=%p ",
1485 current->comm, task_pid_nr(current), filp);
1487 if (list_empty(&cm_dev_list))
1494 * riocm_cdev_release() - Release character device
1496 static int riocm_cdev_release(struct inode *inode, struct file *filp)
1498 struct rio_channel *ch, *_c;
1502 riocm_debug(EXIT, "by %s(%d) filp=%p",
1503 current->comm, task_pid_nr(current), filp);
1505 /* Check if there are channels associated with this file descriptor */
1506 spin_lock_bh(&idr_lock);
1507 idr_for_each_entry(&ch_idr, ch, i) {
1508 if (ch && ch->filp == filp) {
1509 riocm_debug(EXIT, "ch_%d not released by %s(%d)",
1510 ch->id, current->comm,
1511 task_pid_nr(current));
1512 idr_remove(&ch_idr, ch->id);
1513 list_add(&ch->ch_node, &list);
1516 spin_unlock_bh(&idr_lock);
1518 if (!list_empty(&list)) {
1519 list_for_each_entry_safe(ch, _c, &list, ch_node) {
1520 list_del(&ch->ch_node);
1529 * cm_ep_get_list_size() - Reports number of endpoints in the network
1531 static int cm_ep_get_list_size(void __user *arg)
1533 u32 __user *p = arg;
1538 if (get_user(mport_id, p))
1540 if (mport_id >= RIO_MAX_MPORTS)
1543 /* Find a matching cm_dev object */
1544 down_read(&rdev_sem);
1545 list_for_each_entry(cm, &cm_dev_list, list) {
1546 if (cm->mport->id == mport_id) {
1549 if (copy_to_user(arg, &count, sizeof(u32)))
1560 * cm_ep_get_list() - Returns list of attached endpoints
1562 static int cm_ep_get_list(void __user *arg)
1565 struct cm_peer *peer;
1573 if (copy_from_user(&info, arg, sizeof(info)))
1576 if (info[1] >= RIO_MAX_MPORTS || info[0] > RIOCM_MAX_EP_COUNT)
1579 /* Find a matching cm_dev object */
1580 down_read(&rdev_sem);
1581 list_for_each_entry(cm, &cm_dev_list, list)
1582 if (cm->mport->id == (u8)info[1])
1589 nent = min(info[0], cm->npeers);
1590 buf = kcalloc(nent + 2, sizeof(u32), GFP_KERNEL);
1596 entry_ptr = (u32 *)((uintptr_t)buf + 2*sizeof(u32));
1598 list_for_each_entry(peer, &cm->peers, node) {
1599 *entry_ptr = (u32)peer->rdev->destid;
1606 ((u32 *)buf)[0] = i; /* report an updated number of entries */
1607 ((u32 *)buf)[1] = info[1]; /* put back an mport ID */
1608 if (copy_to_user(arg, buf, sizeof(u32) * (info[0] + 2)))
1616 * cm_mport_get_list() - Returns list of available local mport devices
1618 static int cm_mport_get_list(void __user *arg)
1627 if (copy_from_user(&entries, arg, sizeof(entries)))
1629 if (entries == 0 || entries > RIO_MAX_MPORTS)
1631 buf = kcalloc(entries + 1, sizeof(u32), GFP_KERNEL);
1635 /* Scan all registered cm_dev objects */
1636 entry_ptr = (u32 *)((uintptr_t)buf + sizeof(u32));
1637 down_read(&rdev_sem);
1638 list_for_each_entry(cm, &cm_dev_list, list) {
1639 if (count++ < entries) {
1640 *entry_ptr = (cm->mport->id << 16) |
1641 cm->mport->host_deviceid;
1647 *((u32 *)buf) = count; /* report a real number of entries */
1648 if (copy_to_user(arg, buf, sizeof(u32) * (count + 1)))
1656 * cm_chan_create() - Create a message exchange channel
1658 static int cm_chan_create(struct file *filp, void __user *arg)
1660 u16 __user *p = arg;
1662 struct rio_channel *ch;
1664 if (get_user(ch_num, p))
1667 riocm_debug(CHOP, "ch_%d requested by %s(%d)",
1668 ch_num, current->comm, task_pid_nr(current));
1669 ch = riocm_ch_create(&ch_num);
1674 riocm_debug(CHOP, "ch_%d created by %s(%d)",
1675 ch_num, current->comm, task_pid_nr(current));
1676 return put_user(ch_num, p);
1680 * cm_chan_close() - Close channel
1681 * @filp: Pointer to file object
1682 * @arg: Channel to close
1684 static int cm_chan_close(struct file *filp, void __user *arg)
1686 u16 __user *p = arg;
1688 struct rio_channel *ch;
1690 if (get_user(ch_num, p))
1693 riocm_debug(CHOP, "ch_%d by %s(%d)",
1694 ch_num, current->comm, task_pid_nr(current));
1696 spin_lock_bh(&idr_lock);
1697 ch = idr_find(&ch_idr, ch_num);
1699 spin_unlock_bh(&idr_lock);
1702 if (ch->filp != filp) {
1703 spin_unlock_bh(&idr_lock);
1706 idr_remove(&ch_idr, ch->id);
1707 spin_unlock_bh(&idr_lock);
1709 return riocm_ch_close(ch);
1713 * cm_chan_bind() - Bind channel
1714 * @arg: Channel number
1716 static int cm_chan_bind(void __user *arg)
1718 struct rio_cm_channel chan;
1720 if (copy_from_user(&chan, arg, sizeof(chan)))
1722 if (chan.mport_id >= RIO_MAX_MPORTS)
1725 return riocm_ch_bind(chan.id, chan.mport_id, NULL);
1729 * cm_chan_listen() - Listen on channel
1730 * @arg: Channel number
1732 static int cm_chan_listen(void __user *arg)
1734 u16 __user *p = arg;
1737 if (get_user(ch_num, p))
1740 return riocm_ch_listen(ch_num);
1744 * cm_chan_accept() - Accept incoming connection
1745 * @filp: Pointer to file object
1746 * @arg: Channel number
1748 static int cm_chan_accept(struct file *filp, void __user *arg)
1750 struct rio_cm_accept param;
1752 struct rio_channel *ch;
1754 if (copy_from_user(¶m, arg, sizeof(param)))
1757 riocm_debug(CHOP, "on ch_%d by %s(%d)",
1758 param.ch_num, current->comm, task_pid_nr(current));
1760 accept_to = param.wait_to ?
1761 msecs_to_jiffies(param.wait_to) : 0;
1763 ch = riocm_ch_accept(param.ch_num, ¶m.ch_num, accept_to);
1768 riocm_debug(CHOP, "new ch_%d for %s(%d)",
1769 ch->id, current->comm, task_pid_nr(current));
1771 if (copy_to_user(arg, ¶m, sizeof(param)))
1777 * cm_chan_connect() - Connect on channel
1778 * @arg: Channel information
1780 static int cm_chan_connect(void __user *arg)
1782 struct rio_cm_channel chan;
1784 struct cm_peer *peer;
1787 if (copy_from_user(&chan, arg, sizeof(chan)))
1789 if (chan.mport_id >= RIO_MAX_MPORTS)
1792 down_read(&rdev_sem);
1794 /* Find matching cm_dev object */
1795 list_for_each_entry(cm, &cm_dev_list, list) {
1796 if (cm->mport->id == chan.mport_id) {
1805 if (chan.remote_destid >= RIO_ANY_DESTID(cm->mport->sys_size)) {
1810 /* Find corresponding RapidIO endpoint device object */
1813 list_for_each_entry(peer, &cm->peers, node) {
1814 if (peer->rdev->destid == chan.remote_destid) {
1825 return riocm_ch_connect(chan.id, cm, peer, chan.remote_channel);
1832 * cm_chan_msg_send() - Send a message through channel
1833 * @arg: Outbound message information
1835 static int cm_chan_msg_send(void __user *arg)
1837 struct rio_cm_msg msg;
1841 if (copy_from_user(&msg, arg, sizeof(msg)))
1843 if (msg.size > RIO_MAX_MSG_SIZE)
1846 buf = memdup_user((void __user *)(uintptr_t)msg.msg, msg.size);
1848 return PTR_ERR(buf);
1850 ret = riocm_ch_send(msg.ch_num, buf, msg.size);
1857 * cm_chan_msg_rcv() - Receive a message through channel
1858 * @arg: Inbound message information
1860 static int cm_chan_msg_rcv(void __user *arg)
1862 struct rio_cm_msg msg;
1863 struct rio_channel *ch;
1866 int ret = 0, msg_size;
1868 if (copy_from_user(&msg, arg, sizeof(msg)))
1871 if (msg.ch_num == 0 || msg.size == 0)
1874 ch = riocm_get_channel(msg.ch_num);
1878 rxto = msg.rxto ? msecs_to_jiffies(msg.rxto) : MAX_SCHEDULE_TIMEOUT;
1880 ret = riocm_ch_receive(ch, &buf, rxto);
1884 msg_size = min(msg.size, (u16)(RIO_MAX_MSG_SIZE));
1886 if (copy_to_user((void __user *)(uintptr_t)msg.msg, buf, msg_size))
1889 riocm_ch_free_rxbuf(ch, buf);
1891 riocm_put_channel(ch);
1896 * riocm_cdev_ioctl() - IOCTL requests handler
1899 riocm_cdev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1902 case RIO_CM_EP_GET_LIST_SIZE:
1903 return cm_ep_get_list_size((void __user *)arg);
1904 case RIO_CM_EP_GET_LIST:
1905 return cm_ep_get_list((void __user *)arg);
1906 case RIO_CM_CHAN_CREATE:
1907 return cm_chan_create(filp, (void __user *)arg);
1908 case RIO_CM_CHAN_CLOSE:
1909 return cm_chan_close(filp, (void __user *)arg);
1910 case RIO_CM_CHAN_BIND:
1911 return cm_chan_bind((void __user *)arg);
1912 case RIO_CM_CHAN_LISTEN:
1913 return cm_chan_listen((void __user *)arg);
1914 case RIO_CM_CHAN_ACCEPT:
1915 return cm_chan_accept(filp, (void __user *)arg);
1916 case RIO_CM_CHAN_CONNECT:
1917 return cm_chan_connect((void __user *)arg);
1918 case RIO_CM_CHAN_SEND:
1919 return cm_chan_msg_send((void __user *)arg);
1920 case RIO_CM_CHAN_RECEIVE:
1921 return cm_chan_msg_rcv((void __user *)arg);
1922 case RIO_CM_MPORT_GET_LIST:
1923 return cm_mport_get_list((void __user *)arg);
1931 static const struct file_operations riocm_cdev_fops = {
1932 .owner = THIS_MODULE,
1933 .open = riocm_cdev_open,
1934 .release = riocm_cdev_release,
1935 .unlocked_ioctl = riocm_cdev_ioctl,
1939 * riocm_add_dev - add new remote RapidIO device into channel management core
1940 * @dev: device object associated with RapidIO device
1941 * @sif: subsystem interface
1943 * Adds the specified RapidIO device (if applicable) into peers list of
1944 * the corresponding channel management device (cm_dev).
1946 static int riocm_add_dev(struct device *dev, struct subsys_interface *sif)
1948 struct cm_peer *peer;
1949 struct rio_dev *rdev = to_rio_dev(dev);
1952 /* Check if the remote device has capabilities required to support CM */
1953 if (!dev_cm_capable(rdev))
1956 riocm_debug(RDEV, "(%s)", rio_name(rdev));
1958 peer = kmalloc(sizeof(*peer), GFP_KERNEL);
1962 /* Find a corresponding cm_dev object */
1963 down_write(&rdev_sem);
1964 list_for_each_entry(cm, &cm_dev_list, list) {
1965 if (cm->mport == rdev->net->hport)
1969 up_write(&rdev_sem);
1975 list_add_tail(&peer->node, &cm->peers);
1978 up_write(&rdev_sem);
1983 * riocm_remove_dev - remove remote RapidIO device from channel management core
1984 * @dev: device object associated with RapidIO device
1985 * @sif: subsystem interface
1987 * Removes the specified RapidIO device (if applicable) from peers list of
1988 * the corresponding channel management device (cm_dev).
1990 static void riocm_remove_dev(struct device *dev, struct subsys_interface *sif)
1992 struct rio_dev *rdev = to_rio_dev(dev);
1994 struct cm_peer *peer;
1995 struct rio_channel *ch, *_c;
2000 /* Check if the remote device has capabilities required to support CM */
2001 if (!dev_cm_capable(rdev))
2004 riocm_debug(RDEV, "(%s)", rio_name(rdev));
2006 /* Find matching cm_dev object */
2007 down_write(&rdev_sem);
2008 list_for_each_entry(cm, &cm_dev_list, list) {
2009 if (cm->mport == rdev->net->hport) {
2016 up_write(&rdev_sem);
2020 /* Remove remote device from the list of peers */
2022 list_for_each_entry(peer, &cm->peers, node) {
2023 if (peer->rdev == rdev) {
2024 riocm_debug(RDEV, "removing peer %s", rio_name(rdev));
2026 list_del(&peer->node);
2033 up_write(&rdev_sem);
2039 * Release channels associated with this peer
2042 spin_lock_bh(&idr_lock);
2043 idr_for_each_entry(&ch_idr, ch, i) {
2044 if (ch && ch->rdev == rdev) {
2045 if (atomic_read(&rdev->state) != RIO_DEVICE_SHUTDOWN)
2046 riocm_exch(ch, RIO_CM_DISCONNECT);
2047 idr_remove(&ch_idr, ch->id);
2048 list_add(&ch->ch_node, &list);
2051 spin_unlock_bh(&idr_lock);
2053 if (!list_empty(&list)) {
2054 list_for_each_entry_safe(ch, _c, &list, ch_node) {
2055 list_del(&ch->ch_node);
2062 * riocm_cdev_add() - Create rio_cm char device
2063 * @devno: device number assigned to device (MAJ + MIN)
2065 static int riocm_cdev_add(dev_t devno)
2069 cdev_init(&riocm_cdev.cdev, &riocm_cdev_fops);
2070 riocm_cdev.cdev.owner = THIS_MODULE;
2071 ret = cdev_add(&riocm_cdev.cdev, devno, 1);
2073 riocm_error("Cannot register a device with error %d", ret);
2077 riocm_cdev.dev = device_create(&dev_class, NULL, devno, NULL, DEV_NAME);
2078 if (IS_ERR(riocm_cdev.dev)) {
2079 cdev_del(&riocm_cdev.cdev);
2080 return PTR_ERR(riocm_cdev.dev);
2083 riocm_debug(MPORT, "Added %s cdev(%d:%d)",
2084 DEV_NAME, MAJOR(devno), MINOR(devno));
2090 * riocm_add_mport - add new local mport device into channel management core
2091 * @dev: device object associated with mport
2093 * When a new mport device is added, CM immediately reserves inbound and
2094 * outbound RapidIO mailboxes that will be used.
2096 static int riocm_add_mport(struct device *dev)
2101 struct rio_mport *mport = to_rio_mport(dev);
2103 riocm_debug(MPORT, "add mport %s", mport->name);
2105 cm = kzalloc(sizeof(*cm), GFP_KERNEL);
2111 rc = rio_request_outb_mbox(mport, cm, cmbox,
2112 RIOCM_TX_RING_SIZE, riocm_outb_msg_event);
2114 riocm_error("failed to allocate OBMBOX_%d on %s",
2115 cmbox, mport->name);
2120 rc = rio_request_inb_mbox(mport, cm, cmbox,
2121 RIOCM_RX_RING_SIZE, riocm_inb_msg_event);
2123 riocm_error("failed to allocate IBMBOX_%d on %s",
2124 cmbox, mport->name);
2125 rio_release_outb_mbox(mport, cmbox);
2130 cm->rx_wq = create_workqueue(DRV_NAME "/rxq");
2132 rio_release_inb_mbox(mport, cmbox);
2133 rio_release_outb_mbox(mport, cmbox);
2139 * Allocate and register inbound messaging buffers to be ready
2140 * to receive channel and system management requests
2142 for (i = 0; i < RIOCM_RX_RING_SIZE; i++)
2143 cm->rx_buf[i] = NULL;
2145 cm->rx_slots = RIOCM_RX_RING_SIZE;
2146 mutex_init(&cm->rx_lock);
2147 riocm_rx_fill(cm, RIOCM_RX_RING_SIZE);
2148 INIT_WORK(&cm->rx_work, rio_ibmsg_handler);
2152 cm->tx_ack_slot = 0;
2153 spin_lock_init(&cm->tx_lock);
2155 INIT_LIST_HEAD(&cm->peers);
2157 INIT_LIST_HEAD(&cm->tx_reqs);
2159 down_write(&rdev_sem);
2160 list_add_tail(&cm->list, &cm_dev_list);
2161 up_write(&rdev_sem);
2167 * riocm_remove_mport - remove local mport device from channel management core
2168 * @dev: device object associated with mport
2170 * Removes a local mport device from the list of registered devices that provide
2171 * channel management services. Returns an error if the specified mport is not
2172 * registered with the CM core.
2174 static void riocm_remove_mport(struct device *dev)
2176 struct rio_mport *mport = to_rio_mport(dev);
2178 struct cm_peer *peer, *temp;
2179 struct rio_channel *ch, *_c;
2184 riocm_debug(MPORT, "%s", mport->name);
2186 /* Find a matching cm_dev object */
2187 down_write(&rdev_sem);
2188 list_for_each_entry(cm, &cm_dev_list, list) {
2189 if (cm->mport == mport) {
2190 list_del(&cm->list);
2195 up_write(&rdev_sem);
2199 flush_workqueue(cm->rx_wq);
2200 destroy_workqueue(cm->rx_wq);
2202 /* Release channels bound to this mport */
2203 spin_lock_bh(&idr_lock);
2204 idr_for_each_entry(&ch_idr, ch, i) {
2205 if (ch->cmdev == cm) {
2206 riocm_debug(RDEV, "%s drop ch_%d",
2207 mport->name, ch->id);
2208 idr_remove(&ch_idr, ch->id);
2209 list_add(&ch->ch_node, &list);
2212 spin_unlock_bh(&idr_lock);
2214 if (!list_empty(&list)) {
2215 list_for_each_entry_safe(ch, _c, &list, ch_node) {
2216 list_del(&ch->ch_node);
2221 rio_release_inb_mbox(mport, cmbox);
2222 rio_release_outb_mbox(mport, cmbox);
2224 /* Remove and free peer entries */
2225 if (!list_empty(&cm->peers))
2226 riocm_debug(RDEV, "ATTN: peer list not empty");
2227 list_for_each_entry_safe(peer, temp, &cm->peers, node) {
2228 riocm_debug(RDEV, "removing peer %s", rio_name(peer->rdev));
2229 list_del(&peer->node);
2235 riocm_debug(MPORT, "%s done", mport->name);
2238 static int rio_cm_shutdown(struct notifier_block *nb, unsigned long code,
2241 struct rio_channel *ch;
2245 riocm_debug(EXIT, ".");
2248 * If there are any channels left in connected state send
2249 * close notification to the connection partner.
2250 * First build a list of channels that require a closing
2251 * notification because function riocm_send_close() should
2252 * be called outside of spinlock protected code.
2254 spin_lock_bh(&idr_lock);
2255 idr_for_each_entry(&ch_idr, ch, i) {
2256 if (ch->state == RIO_CM_CONNECTED) {
2257 riocm_debug(EXIT, "close ch %d", ch->id);
2258 idr_remove(&ch_idr, ch->id);
2259 list_add(&ch->ch_node, &list);
2262 spin_unlock_bh(&idr_lock);
2264 list_for_each_entry(ch, &list, ch_node)
2265 riocm_send_close(ch);
2271 * riocm_interface handles addition/removal of remote RapidIO devices
2273 static struct subsys_interface riocm_interface = {
2275 .subsys = &rio_bus_type,
2276 .add_dev = riocm_add_dev,
2277 .remove_dev = riocm_remove_dev,
2281 * rio_mport_interface handles addition/removal local mport devices
2283 static struct class_interface rio_mport_interface __refdata = {
2284 .class = &rio_mport_class,
2285 .add_dev = riocm_add_mport,
2286 .remove_dev = riocm_remove_mport,
2289 static struct notifier_block rio_cm_notifier = {
2290 .notifier_call = rio_cm_shutdown,
2293 static int __init riocm_init(void)
2297 /* Create device class needed by udev */
2298 ret = class_register(&dev_class);
2300 riocm_error("Cannot create " DRV_NAME " class");
2304 ret = alloc_chrdev_region(&dev_number, 0, 1, DRV_NAME);
2306 class_unregister(&dev_class);
2310 dev_major = MAJOR(dev_number);
2311 dev_minor_base = MINOR(dev_number);
2312 riocm_debug(INIT, "Registered class with %d major", dev_major);
2315 * Register as rapidio_port class interface to get notifications about
2316 * mport additions and removals.
2318 ret = class_interface_register(&rio_mport_interface);
2320 riocm_error("class_interface_register error: %d", ret);
2325 * Register as RapidIO bus interface to get notifications about
2326 * addition/removal of remote RapidIO devices.
2328 ret = subsys_interface_register(&riocm_interface);
2330 riocm_error("subsys_interface_register error: %d", ret);
2334 ret = register_reboot_notifier(&rio_cm_notifier);
2336 riocm_error("failed to register reboot notifier (err=%d)", ret);
2340 ret = riocm_cdev_add(dev_number);
2342 unregister_reboot_notifier(&rio_cm_notifier);
2349 subsys_interface_unregister(&riocm_interface);
2351 class_interface_unregister(&rio_mport_interface);
2353 unregister_chrdev_region(dev_number, 1);
2354 class_unregister(&dev_class);
2358 static void __exit riocm_exit(void)
2360 riocm_debug(EXIT, "enter");
2361 unregister_reboot_notifier(&rio_cm_notifier);
2362 subsys_interface_unregister(&riocm_interface);
2363 class_interface_unregister(&rio_mport_interface);
2364 idr_destroy(&ch_idr);
2366 device_unregister(riocm_cdev.dev);
2367 cdev_del(&(riocm_cdev.cdev));
2369 class_unregister(&dev_class);
2370 unregister_chrdev_region(dev_number, 1);
2373 late_initcall(riocm_init);
2374 module_exit(riocm_exit);