Lines Matching +full:cm +full:- +full:poll +full:- +full:mode
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * rio_cm - RapidIO Channelized Messaging Driver
5 * Copyright 2013-2016 Integrated Device Technology, Inc.
12 #include <linux/dma-mapping.h>
22 #include <linux/poll.h>
180 struct cm_dev *cmdev; /* associated CM device object */
203 struct cm_dev *cm; member
226 static int riocm_post_send(struct cm_dev *cm, struct rio_dev *rdev,
246 is_msg_capable(dev->src_ops, dev->dst_ops)
252 spin_lock_bh(&ch->lock); in riocm_cmp()
253 ret = (ch->state == cmp); in riocm_cmp()
254 spin_unlock_bh(&ch->lock); in riocm_cmp()
263 spin_lock_bh(&ch->lock); in riocm_cmp_exch()
264 ret = (ch->state == cmp); in riocm_cmp_exch()
266 ch->state = exch; in riocm_cmp_exch()
267 spin_unlock_bh(&ch->lock); in riocm_cmp_exch()
276 spin_lock_bh(&ch->lock); in riocm_exch()
277 old = ch->state; in riocm_exch()
278 ch->state = exch; in riocm_exch()
279 spin_unlock_bh(&ch->lock); in riocm_exch()
290 kref_get(&ch->ref); in riocm_get_channel()
297 kref_put(&ch->ref, riocm_ch_free); in riocm_put_channel()
300 static void *riocm_rx_get_msg(struct cm_dev *cm) in riocm_rx_get_msg() argument
305 msg = rio_get_inb_message(cm->mport, cmbox); in riocm_rx_get_msg()
308 if (cm->rx_buf[i] == msg) { in riocm_rx_get_msg()
309 cm->rx_buf[i] = NULL; in riocm_rx_get_msg()
310 cm->rx_slots++; in riocm_rx_get_msg()
323 * riocm_rx_fill - fills a ring of receive buffers for given cm device
324 * @cm: cm_dev object
329 static void riocm_rx_fill(struct cm_dev *cm, int nent) in riocm_rx_fill() argument
333 if (cm->rx_slots == 0) in riocm_rx_fill()
336 for (i = 0; i < RIOCM_RX_RING_SIZE && cm->rx_slots && nent; i++) { in riocm_rx_fill()
337 if (cm->rx_buf[i] == NULL) { in riocm_rx_fill()
338 cm->rx_buf[i] = kmalloc(RIO_MAX_MSG_SIZE, GFP_KERNEL); in riocm_rx_fill()
339 if (cm->rx_buf[i] == NULL) in riocm_rx_fill()
341 rio_add_inb_buffer(cm->mport, cmbox, cm->rx_buf[i]); in riocm_rx_fill()
342 cm->rx_slots--; in riocm_rx_fill()
343 nent--; in riocm_rx_fill()
349 * riocm_rx_free - frees all receive buffers associated with given cm device
350 * @cm: cm_dev object
354 static void riocm_rx_free(struct cm_dev *cm) in riocm_rx_free() argument
359 if (cm->rx_buf[i] != NULL) { in riocm_rx_free()
360 kfree(cm->rx_buf[i]); in riocm_rx_free()
361 cm->rx_buf[i] = NULL; in riocm_rx_free()
367 * riocm_req_handler - connection request handler
368 * @cm: cm_dev object
372 * -EINVAL if channel is not in correct state,
373 * -ENODEV if cannot find a channel with specified ID,
374 * -ENOMEM if unable to allocate memory to store the request
376 static int riocm_req_handler(struct cm_dev *cm, void *req_data) in riocm_req_handler() argument
383 chnum = ntohs(hh->dst_ch); in riocm_req_handler()
388 return -ENODEV; in riocm_req_handler()
390 if (ch->state != RIO_CM_LISTEN) { in riocm_req_handler()
393 return -EINVAL; in riocm_req_handler()
399 return -ENOMEM; in riocm_req_handler()
402 req->destid = ntohl(hh->bhdr.src_id); in riocm_req_handler()
403 req->chan = ntohs(hh->src_ch); in riocm_req_handler()
404 req->cmdev = cm; in riocm_req_handler()
406 spin_lock_bh(&ch->lock); in riocm_req_handler()
407 list_add_tail(&req->node, &ch->accept_queue); in riocm_req_handler()
408 spin_unlock_bh(&ch->lock); in riocm_req_handler()
409 complete(&ch->comp); in riocm_req_handler()
416 * riocm_resp_handler - response to connection request handler
420 * -EINVAL if channel is not in correct state,
421 * -ENODEV if cannot find a channel with specified ID,
429 chnum = ntohs(hh->dst_ch); in riocm_resp_handler()
432 return -ENODEV; in riocm_resp_handler()
434 if (ch->state != RIO_CM_CONNECT) { in riocm_resp_handler()
436 return -EINVAL; in riocm_resp_handler()
440 ch->rem_channel = ntohs(hh->src_ch); in riocm_resp_handler()
441 complete(&ch->comp); in riocm_resp_handler()
448 * riocm_close_handler - channel close request handler
452 * -ENODEV if cannot find a channel with specified ID,
461 riocm_debug(RX_CMD, "for ch=%d", ntohs(hh->dst_ch)); in riocm_close_handler()
464 ch = idr_find(&ch_idr, ntohs(hh->dst_ch)); in riocm_close_handler()
467 return -ENODEV; in riocm_close_handler()
469 idr_remove(&ch_idr, ch->id); in riocm_close_handler()
482 * rio_cm_handler - function that services request (non-data) packets
483 * @cm: cm_dev object
486 static void rio_cm_handler(struct cm_dev *cm, void *data) in rio_cm_handler() argument
490 if (!rio_mport_is_running(cm->mport)) in rio_cm_handler()
496 hdr->ch_op, ntohs(hdr->dst_ch), ntohs(hdr->src_ch)); in rio_cm_handler()
498 switch (hdr->ch_op) { in rio_cm_handler()
500 riocm_req_handler(cm, data); in rio_cm_handler()
517 * rio_rx_data_handler - received data packet handler
518 * @cm: cm_dev object
522 * -ENODEV if cannot find a channel with specified ID,
523 * -EIO if channel is not in CONNECTED state,
524 * -ENOMEM if channel RX queue is full (packet discarded)
526 static int rio_rx_data_handler(struct cm_dev *cm, void *buf) in rio_rx_data_handler() argument
533 riocm_debug(RX_DATA, "for ch=%d", ntohs(hdr->dst_ch)); in rio_rx_data_handler()
535 ch = riocm_get_channel(ntohs(hdr->dst_ch)); in rio_rx_data_handler()
537 /* Discard data message for non-existing channel */ in rio_rx_data_handler()
539 return -ENODEV; in rio_rx_data_handler()
543 spin_lock(&ch->lock); in rio_rx_data_handler()
545 if (ch->state != RIO_CM_CONNECTED) { in rio_rx_data_handler()
548 ch->id, ch->state); in rio_rx_data_handler()
549 spin_unlock(&ch->lock); in rio_rx_data_handler()
552 return -EIO; in rio_rx_data_handler()
555 if (ch->rx_ring.count == RIOCM_RX_RING_SIZE) { in rio_rx_data_handler()
557 riocm_debug(RX_DATA, "ch=%d is full", ch->id); in rio_rx_data_handler()
558 spin_unlock(&ch->lock); in rio_rx_data_handler()
561 return -ENOMEM; in rio_rx_data_handler()
564 ch->rx_ring.buf[ch->rx_ring.head] = buf; in rio_rx_data_handler()
565 ch->rx_ring.head++; in rio_rx_data_handler()
566 ch->rx_ring.count++; in rio_rx_data_handler()
567 ch->rx_ring.head %= RIOCM_RX_RING_SIZE; in rio_rx_data_handler()
569 complete(&ch->comp); in rio_rx_data_handler()
571 spin_unlock(&ch->lock); in rio_rx_data_handler()
578 * rio_ibmsg_handler - inbound message packet handler
582 struct cm_dev *cm = container_of(work, struct cm_dev, rx_work); in rio_ibmsg_handler() local
586 if (!rio_mport_is_running(cm->mport)) in rio_ibmsg_handler()
590 mutex_lock(&cm->rx_lock); in rio_ibmsg_handler()
591 data = riocm_rx_get_msg(cm); in rio_ibmsg_handler()
593 riocm_rx_fill(cm, 1); in rio_ibmsg_handler()
594 mutex_unlock(&cm->rx_lock); in rio_ibmsg_handler()
601 if (hdr->bhdr.type != RIO_CM_CHAN) { in rio_ibmsg_handler()
604 hdr->bhdr.type); in rio_ibmsg_handler()
610 if (hdr->ch_op == CM_DATA_MSG) in rio_ibmsg_handler()
611 rio_rx_data_handler(cm, data); in rio_ibmsg_handler()
613 rio_cm_handler(cm, data); in rio_ibmsg_handler()
620 struct cm_dev *cm = dev_id; in riocm_inb_msg_event() local
622 if (rio_mport_is_running(cm->mport) && !work_pending(&cm->rx_work)) in riocm_inb_msg_event()
623 queue_work(cm->rx_wq, &cm->rx_work); in riocm_inb_msg_event()
627 * rio_txcq_handler - TX completion handler
628 * @cm: cm_dev object
635 static void rio_txcq_handler(struct cm_dev *cm, int slot) in rio_txcq_handler() argument
644 cm->mport->id, slot, cm->tx_cnt); in rio_txcq_handler()
646 spin_lock(&cm->tx_lock); in rio_txcq_handler()
647 ack_slot = cm->tx_ack_slot; in rio_txcq_handler()
652 while (cm->tx_cnt && ((ack_slot != slot) || in rio_txcq_handler()
653 (cm->tx_cnt == RIOCM_TX_RING_SIZE))) { in rio_txcq_handler()
655 cm->tx_buf[ack_slot] = NULL; in rio_txcq_handler()
657 ack_slot &= (RIOCM_TX_RING_SIZE - 1); in rio_txcq_handler()
658 cm->tx_cnt--; in rio_txcq_handler()
661 if (cm->tx_cnt < 0 || cm->tx_cnt > RIOCM_TX_RING_SIZE) in rio_txcq_handler()
662 riocm_error("tx_cnt %d out of sync", cm->tx_cnt); in rio_txcq_handler()
664 WARN_ON((cm->tx_cnt < 0) || (cm->tx_cnt > RIOCM_TX_RING_SIZE)); in rio_txcq_handler()
666 cm->tx_ack_slot = ack_slot; in rio_txcq_handler()
671 if (!list_empty(&cm->tx_reqs) && (cm->tx_cnt < RIOCM_TX_RING_SIZE)) { in rio_txcq_handler()
675 list_for_each_entry_safe(req, _req, &cm->tx_reqs, node) { in rio_txcq_handler()
676 list_del(&req->node); in rio_txcq_handler()
677 cm->tx_buf[cm->tx_slot] = req->buffer; in rio_txcq_handler()
678 rc = rio_add_outb_message(cm->mport, req->rdev, cmbox, in rio_txcq_handler()
679 req->buffer, req->len); in rio_txcq_handler()
680 kfree(req->buffer); in rio_txcq_handler()
683 ++cm->tx_cnt; in rio_txcq_handler()
684 ++cm->tx_slot; in rio_txcq_handler()
685 cm->tx_slot &= (RIOCM_TX_RING_SIZE - 1); in rio_txcq_handler()
686 if (cm->tx_cnt == RIOCM_TX_RING_SIZE) in rio_txcq_handler()
691 spin_unlock(&cm->tx_lock); in rio_txcq_handler()
697 struct cm_dev *cm = dev_id; in riocm_outb_msg_event() local
699 if (cm && rio_mport_is_running(cm->mport)) in riocm_outb_msg_event()
700 rio_txcq_handler(cm, slot); in riocm_outb_msg_event()
703 static int riocm_queue_req(struct cm_dev *cm, struct rio_dev *rdev, in riocm_queue_req() argument
711 return -ENOMEM; in riocm_queue_req()
713 treq->rdev = rdev; in riocm_queue_req()
714 treq->buffer = buffer; in riocm_queue_req()
715 treq->len = len; in riocm_queue_req()
717 spin_lock_irqsave(&cm->tx_lock, flags); in riocm_queue_req()
718 list_add_tail(&treq->node, &cm->tx_reqs); in riocm_queue_req()
719 spin_unlock_irqrestore(&cm->tx_lock, flags); in riocm_queue_req()
724 * riocm_post_send - helper function that places packet into msg TX queue
725 * @cm: cm_dev object
733 static int riocm_post_send(struct cm_dev *cm, struct rio_dev *rdev, in riocm_post_send() argument
739 spin_lock_irqsave(&cm->tx_lock, flags); in riocm_post_send()
741 if (cm->mport == NULL) { in riocm_post_send()
742 rc = -ENODEV; in riocm_post_send()
746 if (cm->tx_cnt == RIOCM_TX_RING_SIZE) { in riocm_post_send()
748 rc = -EBUSY; in riocm_post_send()
752 cm->tx_buf[cm->tx_slot] = buffer; in riocm_post_send()
753 rc = rio_add_outb_message(cm->mport, rdev, cmbox, buffer, len); in riocm_post_send()
756 buffer, rdev->destid, cm->tx_slot, cm->tx_cnt); in riocm_post_send()
758 ++cm->tx_cnt; in riocm_post_send()
759 ++cm->tx_slot; in riocm_post_send()
760 cm->tx_slot &= (RIOCM_TX_RING_SIZE - 1); in riocm_post_send()
763 spin_unlock_irqrestore(&cm->tx_lock, flags); in riocm_post_send()
768 * riocm_ch_send - sends a data packet to a remote device
770 * @buf: pointer to a data buffer to send (including CM header)
771 * @len: length of data to transfer (including CM header)
776 * -EINVAL if one or more input parameters is/are not valid,
777 * -ENODEV if cannot find a channel with specified ID,
778 * -EAGAIN if a channel is not in CONNECTED state,
788 return -EINVAL; in riocm_ch_send()
792 riocm_error("%s(%d) ch_%d not found", current->comm, in riocm_ch_send()
794 return -ENODEV; in riocm_ch_send()
798 ret = -EAGAIN; in riocm_ch_send()
807 hdr->bhdr.src_id = htonl(ch->loc_destid); in riocm_ch_send()
808 hdr->bhdr.dst_id = htonl(ch->rem_destid); in riocm_ch_send()
809 hdr->bhdr.src_mbox = cmbox; in riocm_ch_send()
810 hdr->bhdr.dst_mbox = cmbox; in riocm_ch_send()
811 hdr->bhdr.type = RIO_CM_CHAN; in riocm_ch_send()
812 hdr->ch_op = CM_DATA_MSG; in riocm_ch_send()
813 hdr->dst_ch = htons(ch->rem_channel); in riocm_ch_send()
814 hdr->src_ch = htons(ch->id); in riocm_ch_send()
815 hdr->msg_len = htons((u16)len); in riocm_ch_send()
818 * HW-specific add_outb_message() routine copies TX data into its own in riocm_ch_send()
823 ret = riocm_post_send(ch->cmdev, ch->rdev, buf, len); in riocm_ch_send()
825 riocm_debug(TX, "ch %d send_err=%d", ch->id, ret); in riocm_ch_send()
833 int i, ret = -EINVAL; in riocm_ch_free_rxbuf()
835 spin_lock_bh(&ch->lock); in riocm_ch_free_rxbuf()
838 if (ch->rx_ring.inuse[i] == buf) { in riocm_ch_free_rxbuf()
839 ch->rx_ring.inuse[i] = NULL; in riocm_ch_free_rxbuf()
840 ch->rx_ring.inuse_cnt--; in riocm_ch_free_rxbuf()
846 spin_unlock_bh(&ch->lock); in riocm_ch_free_rxbuf()
855 * riocm_ch_receive - fetch a data packet received for the specified channel
861 * -EAGAIN if a channel is not in CONNECTED state,
862 * -ENOMEM if in-use tracking queue is full,
863 * -ETIME if wait timeout expired,
864 * -EINTR if wait was interrupted.
873 ret = -EAGAIN; in riocm_ch_receive()
877 if (ch->rx_ring.inuse_cnt == RIOCM_RX_RING_SIZE) { in riocm_ch_receive()
881 ret = -ENOMEM; in riocm_ch_receive()
885 wret = wait_for_completion_interruptible_timeout(&ch->comp, timeout); in riocm_ch_receive()
887 riocm_debug(WAIT, "wait on %d returned %ld", ch->id, wret); in riocm_ch_receive()
890 ret = -ETIME; in riocm_ch_receive()
891 else if (wret == -ERESTARTSYS) in riocm_ch_receive()
892 ret = -EINTR; in riocm_ch_receive()
894 ret = riocm_cmp(ch, RIO_CM_CONNECTED) ? 0 : -ECONNRESET; in riocm_ch_receive()
899 spin_lock_bh(&ch->lock); in riocm_ch_receive()
901 rxmsg = ch->rx_ring.buf[ch->rx_ring.tail]; in riocm_ch_receive()
902 ch->rx_ring.buf[ch->rx_ring.tail] = NULL; in riocm_ch_receive()
903 ch->rx_ring.count--; in riocm_ch_receive()
904 ch->rx_ring.tail++; in riocm_ch_receive()
905 ch->rx_ring.tail %= RIOCM_RX_RING_SIZE; in riocm_ch_receive()
906 ret = -ENOMEM; in riocm_ch_receive()
909 if (ch->rx_ring.inuse[i] == NULL) { in riocm_ch_receive()
910 ch->rx_ring.inuse[i] = rxmsg; in riocm_ch_receive()
911 ch->rx_ring.inuse_cnt++; in riocm_ch_receive()
923 spin_unlock_bh(&ch->lock); in riocm_ch_receive()
930 * riocm_ch_connect - sends a connect request to a remote device
932 * @cm: CM device to send connect request
937 * -EINVAL if the channel is not in IDLE state,
938 * -EAGAIN if no connection request available immediately,
939 * -ETIME if ACK response timeout expired,
940 * -EINTR if wait for response was interrupted.
942 static int riocm_ch_connect(u16 loc_ch, struct cm_dev *cm, in riocm_ch_connect() argument
952 return -ENODEV; in riocm_ch_connect()
955 ret = -EINVAL; in riocm_ch_connect()
959 ch->cmdev = cm; in riocm_ch_connect()
960 ch->rdev = peer->rdev; in riocm_ch_connect()
961 ch->context = NULL; in riocm_ch_connect()
962 ch->loc_destid = cm->mport->host_deviceid; in riocm_ch_connect()
963 ch->rem_channel = rem_ch; in riocm_ch_connect()
971 ret = -ENOMEM; in riocm_ch_connect()
975 hdr->bhdr.src_id = htonl(ch->loc_destid); in riocm_ch_connect()
976 hdr->bhdr.dst_id = htonl(peer->rdev->destid); in riocm_ch_connect()
977 hdr->bhdr.src_mbox = cmbox; in riocm_ch_connect()
978 hdr->bhdr.dst_mbox = cmbox; in riocm_ch_connect()
979 hdr->bhdr.type = RIO_CM_CHAN; in riocm_ch_connect()
980 hdr->ch_op = CM_CONN_REQ; in riocm_ch_connect()
981 hdr->dst_ch = htons(rem_ch); in riocm_ch_connect()
982 hdr->src_ch = htons(loc_ch); in riocm_ch_connect()
985 * HW-specific add_outb_message() routine copies TX data into its in riocm_ch_connect()
989 ret = riocm_post_send(cm, peer->rdev, hdr, sizeof(*hdr)); in riocm_ch_connect()
991 if (ret != -EBUSY) { in riocm_ch_connect()
994 ret = riocm_queue_req(cm, peer->rdev, hdr, sizeof(*hdr)); in riocm_ch_connect()
1005 wret = wait_for_completion_interruptible_timeout(&ch->comp, in riocm_ch_connect()
1007 riocm_debug(WAIT, "wait on %d returns %ld", ch->id, wret); in riocm_ch_connect()
1010 ret = -ETIME; in riocm_ch_connect()
1011 else if (wret == -ERESTARTSYS) in riocm_ch_connect()
1012 ret = -EINTR; in riocm_ch_connect()
1014 ret = riocm_cmp(ch, RIO_CM_CONNECTED) ? 0 : -1; in riocm_ch_connect()
1028 return -ENOMEM; in riocm_send_ack()
1030 hdr->bhdr.src_id = htonl(ch->loc_destid); in riocm_send_ack()
1031 hdr->bhdr.dst_id = htonl(ch->rem_destid); in riocm_send_ack()
1032 hdr->dst_ch = htons(ch->rem_channel); in riocm_send_ack()
1033 hdr->src_ch = htons(ch->id); in riocm_send_ack()
1034 hdr->bhdr.src_mbox = cmbox; in riocm_send_ack()
1035 hdr->bhdr.dst_mbox = cmbox; in riocm_send_ack()
1036 hdr->bhdr.type = RIO_CM_CHAN; in riocm_send_ack()
1037 hdr->ch_op = CM_CONN_ACK; in riocm_send_ack()
1043 ret = riocm_post_send(ch->cmdev, ch->rdev, hdr, sizeof(*hdr)); in riocm_send_ack()
1045 if (ret == -EBUSY && !riocm_queue_req(ch->cmdev, in riocm_send_ack()
1046 ch->rdev, hdr, sizeof(*hdr))) in riocm_send_ack()
1052 ch->id, rio_name(ch->rdev), ret); in riocm_send_ack()
1057 * riocm_ch_accept - accept incoming connection request
1060 * @timeout: wait timeout (if 0 non-blocking call, do not wait if connection
1063 * Returns: pointer to new channel struct if success, or error-valued pointer:
1064 * -ENODEV - cannot find specified channel or mport,
1065 * -EINVAL - the channel is not in IDLE state,
1066 * -EAGAIN - no connection request available immediately (timeout=0),
1067 * -ENOMEM - unable to allocate new channel,
1068 * -ETIME - wait timeout expired,
1069 * -EINTR - wait was interrupted.
1084 return ERR_PTR(-EINVAL); in riocm_ch_accept()
1087 err = -EINVAL; in riocm_ch_accept()
1093 if (!try_wait_for_completion(&ch->comp)) { in riocm_ch_accept()
1094 err = -EAGAIN; in riocm_ch_accept()
1098 riocm_debug(WAIT, "on %d", ch->id); in riocm_ch_accept()
1100 wret = wait_for_completion_interruptible_timeout(&ch->comp, in riocm_ch_accept()
1103 err = -ETIME; in riocm_ch_accept()
1105 } else if (wret == -ERESTARTSYS) { in riocm_ch_accept()
1106 err = -EINTR; in riocm_ch_accept()
1111 spin_lock_bh(&ch->lock); in riocm_ch_accept()
1113 if (ch->state != RIO_CM_LISTEN) { in riocm_ch_accept()
1114 err = -ECANCELED; in riocm_ch_accept()
1115 } else if (list_empty(&ch->accept_queue)) { in riocm_ch_accept()
1117 ch->id); in riocm_ch_accept()
1118 err = -EIO; in riocm_ch_accept()
1121 spin_unlock_bh(&ch->lock); in riocm_ch_accept()
1124 riocm_debug(WAIT, "on %d returns %d", ch->id, err); in riocm_ch_accept()
1134 err = -ENOMEM; in riocm_ch_accept()
1138 spin_lock_bh(&ch->lock); in riocm_ch_accept()
1140 req = list_first_entry(&ch->accept_queue, struct conn_req, node); in riocm_ch_accept()
1141 list_del(&req->node); in riocm_ch_accept()
1142 new_ch->cmdev = ch->cmdev; in riocm_ch_accept()
1143 new_ch->loc_destid = ch->loc_destid; in riocm_ch_accept()
1144 new_ch->rem_destid = req->destid; in riocm_ch_accept()
1145 new_ch->rem_channel = req->chan; in riocm_ch_accept()
1147 spin_unlock_bh(&ch->lock); in riocm_ch_accept()
1154 list_for_each_entry(peer, &new_ch->cmdev->peers, node) { in riocm_ch_accept()
1155 if (peer->rdev->destid == new_ch->rem_destid) { in riocm_ch_accept()
1157 rio_name(peer->rdev)); in riocm_ch_accept()
1166 err = -ENODEV; in riocm_ch_accept()
1170 new_ch->rdev = peer->rdev; in riocm_ch_accept()
1171 new_ch->state = RIO_CM_CONNECTED; in riocm_ch_accept()
1172 spin_lock_init(&new_ch->lock); in riocm_ch_accept()
1177 *new_ch_id = new_ch->id; in riocm_ch_accept()
1182 idr_remove(&ch_idr, new_ch->id); in riocm_ch_accept()
1194 * riocm_ch_listen - puts a channel into LISTEN state
1198 * -EINVAL if the specified channel does not exists or
1210 return -EINVAL; in riocm_ch_listen()
1212 ret = -EINVAL; in riocm_ch_listen()
1218 * riocm_ch_bind - associate a channel object and an mport device
1224 * -ENODEV if cannot find specified mport,
1225 * -EINVAL if the specified channel does not exist or
1231 struct cm_dev *cm; in riocm_ch_bind() local
1232 int rc = -ENODEV; in riocm_ch_bind()
1238 list_for_each_entry(cm, &cm_dev_list, list) { in riocm_ch_bind()
1239 if ((cm->mport->id == mport_id) && in riocm_ch_bind()
1240 rio_mport_is_running(cm->mport)) { in riocm_ch_bind()
1251 rc = -EINVAL; in riocm_ch_bind()
1255 spin_lock_bh(&ch->lock); in riocm_ch_bind()
1256 if (ch->state != RIO_CM_IDLE) { in riocm_ch_bind()
1257 spin_unlock_bh(&ch->lock); in riocm_ch_bind()
1258 rc = -EINVAL; in riocm_ch_bind()
1262 ch->cmdev = cm; in riocm_ch_bind()
1263 ch->loc_destid = cm->mport->host_deviceid; in riocm_ch_bind()
1264 ch->context = context; in riocm_ch_bind()
1265 ch->state = RIO_CM_CHAN_BOUND; in riocm_ch_bind()
1266 spin_unlock_bh(&ch->lock); in riocm_ch_bind()
1275 * riocm_ch_alloc - channel object allocation helper routine
1279 * or error-valued pointer
1289 return ERR_PTR(-ENOMEM); in riocm_ch_alloc()
1309 return ERR_PTR(id == -ENOSPC ? -EBUSY : id); in riocm_ch_alloc()
1312 ch->id = (u16)id; in riocm_ch_alloc()
1313 ch->state = RIO_CM_IDLE; in riocm_ch_alloc()
1314 spin_lock_init(&ch->lock); in riocm_ch_alloc()
1315 INIT_LIST_HEAD(&ch->accept_queue); in riocm_ch_alloc()
1316 INIT_LIST_HEAD(&ch->ch_node); in riocm_ch_alloc()
1317 init_completion(&ch->comp); in riocm_ch_alloc()
1318 init_completion(&ch->comp_close); in riocm_ch_alloc()
1319 kref_init(&ch->ref); in riocm_ch_alloc()
1320 ch->rx_ring.head = 0; in riocm_ch_alloc()
1321 ch->rx_ring.tail = 0; in riocm_ch_alloc()
1322 ch->rx_ring.count = 0; in riocm_ch_alloc()
1323 ch->rx_ring.inuse_cnt = 0; in riocm_ch_alloc()
1329 * riocm_ch_create - creates a new channel object and allocates ID for it
1337 * allocation. Range below 'chstart' is reserved for pre-defined ID numbers.
1338 * Available channel numbers are limited by 16-bit size of channel numbers used
1342 * updated via pointer) or error-valued pointer if error.
1354 *ch_num = ch->id; in riocm_ch_create()
1360 * riocm_ch_free - channel object release routine
1368 riocm_debug(CHOP, "(ch_%d)", ch->id); in riocm_ch_free()
1370 if (ch->rx_ring.inuse_cnt) { in riocm_ch_free()
1372 i < RIOCM_RX_RING_SIZE && ch->rx_ring.inuse_cnt; i++) { in riocm_ch_free()
1373 if (ch->rx_ring.inuse[i] != NULL) { in riocm_ch_free()
1374 kfree(ch->rx_ring.inuse[i]); in riocm_ch_free()
1375 ch->rx_ring.inuse_cnt--; in riocm_ch_free()
1380 if (ch->rx_ring.count) in riocm_ch_free()
1381 for (i = 0; i < RIOCM_RX_RING_SIZE && ch->rx_ring.count; i++) { in riocm_ch_free()
1382 if (ch->rx_ring.buf[i] != NULL) { in riocm_ch_free()
1383 kfree(ch->rx_ring.buf[i]); in riocm_ch_free()
1384 ch->rx_ring.count--; in riocm_ch_free()
1388 complete(&ch->comp_close); in riocm_ch_free()
1402 return -ENOMEM; in riocm_send_close()
1404 hdr->bhdr.src_id = htonl(ch->loc_destid); in riocm_send_close()
1405 hdr->bhdr.dst_id = htonl(ch->rem_destid); in riocm_send_close()
1406 hdr->bhdr.src_mbox = cmbox; in riocm_send_close()
1407 hdr->bhdr.dst_mbox = cmbox; in riocm_send_close()
1408 hdr->bhdr.type = RIO_CM_CHAN; in riocm_send_close()
1409 hdr->ch_op = CM_CONN_CLOSE; in riocm_send_close()
1410 hdr->dst_ch = htons(ch->rem_channel); in riocm_send_close()
1411 hdr->src_ch = htons(ch->id); in riocm_send_close()
1415 * buffer. Needs to be reviewed if switched to direct buffer mode. in riocm_send_close()
1417 ret = riocm_post_send(ch->cmdev, ch->rdev, hdr, sizeof(*hdr)); in riocm_send_close()
1419 if (ret == -EBUSY && !riocm_queue_req(ch->cmdev, ch->rdev, in riocm_send_close()
1425 riocm_error("ch(%d) send CLOSE failed (ret=%d)", ch->id, ret); in riocm_send_close()
1431 * riocm_ch_close - closes a channel object with specified ID (by local request)
1442 ch->id, current->comm, task_pid_nr(current)); in riocm_ch_close()
1448 complete_all(&ch->comp); in riocm_ch_close()
1451 wret = wait_for_completion_interruptible_timeout(&ch->comp_close, tmo); in riocm_ch_close()
1453 riocm_debug(WAIT, "wait on %d returns %ld", ch->id, wret); in riocm_ch_close()
1458 current->comm, task_pid_nr(current), ch->id); in riocm_ch_close()
1459 ret = -ETIMEDOUT; in riocm_ch_close()
1460 } else if (wret == -ERESTARTSYS) { in riocm_ch_close()
1463 current->comm, task_pid_nr(current), ch->id); in riocm_ch_close()
1464 ret = -EINTR; in riocm_ch_close()
1468 riocm_debug(CHOP, "ch_%d resources released", ch->id); in riocm_ch_close()
1471 riocm_debug(CHOP, "failed to release ch_%d resources", ch->id); in riocm_ch_close()
1478 * riocm_cdev_open() - Open character device
1483 current->comm, task_pid_nr(current), filp); in riocm_cdev_open()
1486 return -ENODEV; in riocm_cdev_open()
1492 * riocm_cdev_release() - Release character device
1501 current->comm, task_pid_nr(current), filp); in riocm_cdev_release()
1506 if (ch && ch->filp == filp) { in riocm_cdev_release()
1508 ch->id, current->comm, in riocm_cdev_release()
1510 idr_remove(&ch_idr, ch->id); in riocm_cdev_release()
1511 list_add(&ch->ch_node, &list); in riocm_cdev_release()
1518 list_del(&ch->ch_node); in riocm_cdev_release()
1527 * cm_ep_get_list_size() - Reports number of endpoints in the network
1534 struct cm_dev *cm; in cm_ep_get_list_size() local
1537 return -EFAULT; in cm_ep_get_list_size()
1539 return -EINVAL; in cm_ep_get_list_size()
1543 list_for_each_entry(cm, &cm_dev_list, list) { in cm_ep_get_list_size()
1544 if (cm->mport->id == mport_id) { in cm_ep_get_list_size()
1545 count = cm->npeers; in cm_ep_get_list_size()
1548 return -EFAULT; in cm_ep_get_list_size()
1554 return -ENODEV; in cm_ep_get_list_size()
1558 * cm_ep_get_list() - Returns list of attached endpoints
1562 struct cm_dev *cm; in cm_ep_get_list() local
1572 return -EFAULT; in cm_ep_get_list()
1575 return -EINVAL; in cm_ep_get_list()
1579 list_for_each_entry(cm, &cm_dev_list, list) in cm_ep_get_list()
1580 if (cm->mport->id == (u8)info[1]) in cm_ep_get_list()
1584 return -ENODEV; in cm_ep_get_list()
1587 nent = min(info[0], cm->npeers); in cm_ep_get_list()
1591 return -ENOMEM; in cm_ep_get_list()
1596 list_for_each_entry(peer, &cm->peers, node) { in cm_ep_get_list()
1597 *entry_ptr = (u32)peer->rdev->destid; in cm_ep_get_list()
1607 ret = -EFAULT; in cm_ep_get_list()
1614 * cm_mport_get_list() - Returns list of available local mport devices
1621 struct cm_dev *cm; in cm_mport_get_list() local
1626 return -EFAULT; in cm_mport_get_list()
1628 return -EINVAL; in cm_mport_get_list()
1631 return -ENOMEM; in cm_mport_get_list()
1636 list_for_each_entry(cm, &cm_dev_list, list) { in cm_mport_get_list()
1638 *entry_ptr = (cm->mport->id << 16) | in cm_mport_get_list()
1639 cm->mport->host_deviceid; in cm_mport_get_list()
1647 ret = -EFAULT; in cm_mport_get_list()
1654 * cm_chan_create() - Create a message exchange channel
1663 return -EFAULT; in cm_chan_create()
1666 ch_num, current->comm, task_pid_nr(current)); in cm_chan_create()
1671 ch->filp = filp; in cm_chan_create()
1673 ch_num, current->comm, task_pid_nr(current)); in cm_chan_create()
1678 * cm_chan_close() - Close channel
1689 return -EFAULT; in cm_chan_close()
1692 ch_num, current->comm, task_pid_nr(current)); in cm_chan_close()
1700 if (ch->filp != filp) { in cm_chan_close()
1702 return -EINVAL; in cm_chan_close()
1704 idr_remove(&ch_idr, ch->id); in cm_chan_close()
1711 * cm_chan_bind() - Bind channel
1719 return -EFAULT; in cm_chan_bind()
1721 return -EINVAL; in cm_chan_bind()
1727 * cm_chan_listen() - Listen on channel
1736 return -EFAULT; in cm_chan_listen()
1742 * cm_chan_accept() - Accept incoming connection
1753 return -EFAULT; in cm_chan_accept()
1756 param.ch_num, current->comm, task_pid_nr(current)); in cm_chan_accept()
1764 ch->filp = filp; in cm_chan_accept()
1767 ch->id, current->comm, task_pid_nr(current)); in cm_chan_accept()
1770 return -EFAULT; in cm_chan_accept()
1775 * cm_chan_connect() - Connect on channel
1781 struct cm_dev *cm; in cm_chan_connect() local
1783 int ret = -ENODEV; in cm_chan_connect()
1786 return -EFAULT; in cm_chan_connect()
1788 return -EINVAL; in cm_chan_connect()
1793 list_for_each_entry(cm, &cm_dev_list, list) { in cm_chan_connect()
1794 if (cm->mport->id == chan.mport_id) { in cm_chan_connect()
1803 if (chan.remote_destid >= RIO_ANY_DESTID(cm->mport->sys_size)) { in cm_chan_connect()
1804 ret = -EINVAL; in cm_chan_connect()
1809 ret = -ENODEV; in cm_chan_connect()
1811 list_for_each_entry(peer, &cm->peers, node) { in cm_chan_connect()
1812 if (peer->rdev->destid == chan.remote_destid) { in cm_chan_connect()
1823 return riocm_ch_connect(chan.id, cm, peer, chan.remote_channel); in cm_chan_connect()
1830 * cm_chan_msg_send() - Send a message through channel
1840 return -EFAULT; in cm_chan_msg_send()
1842 return -EINVAL; in cm_chan_msg_send()
1855 * cm_chan_msg_rcv() - Receive a message through channel
1867 return -EFAULT; in cm_chan_msg_rcv()
1870 return -EINVAL; in cm_chan_msg_rcv()
1874 return -ENODEV; in cm_chan_msg_rcv()
1885 ret = -EFAULT; in cm_chan_msg_rcv()
1894 * riocm_cdev_ioctl() - IOCTL requests handler
1926 return -EINVAL; in riocm_cdev_ioctl()
1937 * riocm_add_dev - add new remote RapidIO device into channel management core
1948 struct cm_dev *cm; in riocm_add_dev() local
1950 /* Check if the remote device has capabilities required to support CM */ in riocm_add_dev()
1958 return -ENOMEM; in riocm_add_dev()
1962 list_for_each_entry(cm, &cm_dev_list, list) { in riocm_add_dev()
1963 if (cm->mport == rdev->net->hport) in riocm_add_dev()
1969 return -ENODEV; in riocm_add_dev()
1972 peer->rdev = rdev; in riocm_add_dev()
1973 list_add_tail(&peer->node, &cm->peers); in riocm_add_dev()
1974 cm->npeers++; in riocm_add_dev()
1981 * riocm_remove_dev - remove remote RapidIO device from channel management core
1991 struct cm_dev *cm; in riocm_remove_dev() local
1998 /* Check if the remote device has capabilities required to support CM */ in riocm_remove_dev()
2006 list_for_each_entry(cm, &cm_dev_list, list) { in riocm_remove_dev()
2007 if (cm->mport == rdev->net->hport) { in riocm_remove_dev()
2020 list_for_each_entry(peer, &cm->peers, node) { in riocm_remove_dev()
2021 if (peer->rdev == rdev) { in riocm_remove_dev()
2024 list_del(&peer->node); in riocm_remove_dev()
2025 cm->npeers--; in riocm_remove_dev()
2042 if (ch && ch->rdev == rdev) { in riocm_remove_dev()
2043 if (atomic_read(&rdev->state) != RIO_DEVICE_SHUTDOWN) in riocm_remove_dev()
2045 idr_remove(&ch_idr, ch->id); in riocm_remove_dev()
2046 list_add(&ch->ch_node, &list); in riocm_remove_dev()
2053 list_del(&ch->ch_node); in riocm_remove_dev()
2060 * riocm_cdev_add() - Create rio_cm char device
2088 * riocm_add_mport - add new local mport device into channel management core
2092 * When a new mport device is added, CM immediately reserves inbound and
2100 struct cm_dev *cm; in riocm_add_mport() local
2103 riocm_debug(MPORT, "add mport %s", mport->name); in riocm_add_mport()
2105 cm = kzalloc(sizeof(*cm), GFP_KERNEL); in riocm_add_mport()
2106 if (!cm) in riocm_add_mport()
2107 return -ENOMEM; in riocm_add_mport()
2109 cm->mport = mport; in riocm_add_mport()
2111 rc = rio_request_outb_mbox(mport, cm, cmbox, in riocm_add_mport()
2115 cmbox, mport->name); in riocm_add_mport()
2116 kfree(cm); in riocm_add_mport()
2117 return -ENODEV; in riocm_add_mport()
2120 rc = rio_request_inb_mbox(mport, cm, cmbox, in riocm_add_mport()
2124 cmbox, mport->name); in riocm_add_mport()
2126 kfree(cm); in riocm_add_mport()
2127 return -ENODEV; in riocm_add_mport()
2130 cm->rx_wq = create_workqueue(DRV_NAME "/rxq"); in riocm_add_mport()
2131 if (!cm->rx_wq) { in riocm_add_mport()
2134 kfree(cm); in riocm_add_mport()
2135 return -ENOMEM; in riocm_add_mport()
2143 cm->rx_buf[i] = NULL; in riocm_add_mport()
2145 cm->rx_slots = RIOCM_RX_RING_SIZE; in riocm_add_mport()
2146 mutex_init(&cm->rx_lock); in riocm_add_mport()
2147 riocm_rx_fill(cm, RIOCM_RX_RING_SIZE); in riocm_add_mport()
2148 INIT_WORK(&cm->rx_work, rio_ibmsg_handler); in riocm_add_mport()
2150 cm->tx_slot = 0; in riocm_add_mport()
2151 cm->tx_cnt = 0; in riocm_add_mport()
2152 cm->tx_ack_slot = 0; in riocm_add_mport()
2153 spin_lock_init(&cm->tx_lock); in riocm_add_mport()
2155 INIT_LIST_HEAD(&cm->peers); in riocm_add_mport()
2156 cm->npeers = 0; in riocm_add_mport()
2157 INIT_LIST_HEAD(&cm->tx_reqs); in riocm_add_mport()
2160 list_add_tail(&cm->list, &cm_dev_list); in riocm_add_mport()
2167 * riocm_remove_mport - remove local mport device from channel management core
2173 * registered with the CM core.
2179 struct cm_dev *cm; in riocm_remove_mport() local
2186 riocm_debug(MPORT, "%s", mport->name); in riocm_remove_mport()
2190 list_for_each_entry(cm, &cm_dev_list, list) { in riocm_remove_mport()
2191 if (cm->mport == mport) { in riocm_remove_mport()
2192 list_del(&cm->list); in riocm_remove_mport()
2201 flush_workqueue(cm->rx_wq); in riocm_remove_mport()
2202 destroy_workqueue(cm->rx_wq); in riocm_remove_mport()
2207 if (ch->cmdev == cm) { in riocm_remove_mport()
2209 mport->name, ch->id); in riocm_remove_mport()
2210 idr_remove(&ch_idr, ch->id); in riocm_remove_mport()
2211 list_add(&ch->ch_node, &list); in riocm_remove_mport()
2218 list_del(&ch->ch_node); in riocm_remove_mport()
2227 if (!list_empty(&cm->peers)) in riocm_remove_mport()
2229 list_for_each_entry_safe(peer, temp, &cm->peers, node) { in riocm_remove_mport()
2230 riocm_debug(RDEV, "removing peer %s", rio_name(peer->rdev)); in riocm_remove_mport()
2231 list_del(&peer->node); in riocm_remove_mport()
2235 riocm_rx_free(cm); in riocm_remove_mport()
2236 kfree(cm); in riocm_remove_mport()
2237 riocm_debug(MPORT, "%s done", mport->name); in riocm_remove_mport()
2258 if (ch->state == RIO_CM_CONNECTED) { in rio_cm_shutdown()
2259 riocm_debug(EXIT, "close ch %d", ch->id); in rio_cm_shutdown()
2260 idr_remove(&ch_idr, ch->id); in rio_cm_shutdown()
2261 list_add(&ch->ch_node, &list); in rio_cm_shutdown()
2345 ret = -ENODEV; in riocm_init()