Lines Matching +full:cm +full:- +full:name

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>
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,
237 .name = DRV_NAME,
248 is_msg_capable(dev->src_ops, dev->dst_ops)
254 spin_lock_bh(&ch->lock); in riocm_cmp()
255 ret = (ch->state == cmp); in riocm_cmp()
256 spin_unlock_bh(&ch->lock); in riocm_cmp()
265 spin_lock_bh(&ch->lock); in riocm_cmp_exch()
266 ret = (ch->state == cmp); in riocm_cmp_exch()
268 ch->state = exch; in riocm_cmp_exch()
269 spin_unlock_bh(&ch->lock); in riocm_cmp_exch()
278 spin_lock_bh(&ch->lock); in riocm_exch()
279 old = ch->state; in riocm_exch()
280 ch->state = exch; in riocm_exch()
281 spin_unlock_bh(&ch->lock); in riocm_exch()
292 kref_get(&ch->ref); in riocm_get_channel()
299 kref_put(&ch->ref, riocm_ch_free); in riocm_put_channel()
302 static void *riocm_rx_get_msg(struct cm_dev *cm) in riocm_rx_get_msg() argument
307 msg = rio_get_inb_message(cm->mport, cmbox); in riocm_rx_get_msg()
310 if (cm->rx_buf[i] == msg) { in riocm_rx_get_msg()
311 cm->rx_buf[i] = NULL; in riocm_rx_get_msg()
312 cm->rx_slots++; in riocm_rx_get_msg()
325 * riocm_rx_fill - fills a ring of receive buffers for given cm device
326 * @cm: cm_dev object
331 static void riocm_rx_fill(struct cm_dev *cm, int nent) in riocm_rx_fill() argument
335 if (cm->rx_slots == 0) in riocm_rx_fill()
338 for (i = 0; i < RIOCM_RX_RING_SIZE && cm->rx_slots && nent; i++) { in riocm_rx_fill()
339 if (cm->rx_buf[i] == NULL) { in riocm_rx_fill()
340 cm->rx_buf[i] = kmalloc(RIO_MAX_MSG_SIZE, GFP_KERNEL); in riocm_rx_fill()
341 if (cm->rx_buf[i] == NULL) in riocm_rx_fill()
343 rio_add_inb_buffer(cm->mport, cmbox, cm->rx_buf[i]); in riocm_rx_fill()
344 cm->rx_slots--; in riocm_rx_fill()
345 nent--; in riocm_rx_fill()
351 * riocm_rx_free - frees all receive buffers associated with given cm device
352 * @cm: cm_dev object
356 static void riocm_rx_free(struct cm_dev *cm) in riocm_rx_free() argument
361 if (cm->rx_buf[i] != NULL) { in riocm_rx_free()
362 kfree(cm->rx_buf[i]); in riocm_rx_free()
363 cm->rx_buf[i] = NULL; in riocm_rx_free()
369 * riocm_req_handler - connection request handler
370 * @cm: cm_dev object
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) in riocm_req_handler() argument
385 chnum = ntohs(hh->dst_ch); in riocm_req_handler()
390 return -ENODEV; in riocm_req_handler()
392 if (ch->state != RIO_CM_LISTEN) { in riocm_req_handler()
395 return -EINVAL; in riocm_req_handler()
401 return -ENOMEM; in riocm_req_handler()
404 req->destid = ntohl(hh->bhdr.src_id); in riocm_req_handler()
405 req->chan = ntohs(hh->src_ch); in riocm_req_handler()
406 req->cmdev = cm; in riocm_req_handler()
408 spin_lock_bh(&ch->lock); in riocm_req_handler()
409 list_add_tail(&req->node, &ch->accept_queue); in riocm_req_handler()
410 spin_unlock_bh(&ch->lock); in riocm_req_handler()
411 complete(&ch->comp); in riocm_req_handler()
418 * riocm_resp_handler - response to connection request handler
422 * -EINVAL if channel is not in correct state,
423 * -ENODEV if cannot find a channel with specified ID,
431 chnum = ntohs(hh->dst_ch); in riocm_resp_handler()
434 return -ENODEV; in riocm_resp_handler()
436 if (ch->state != RIO_CM_CONNECT) { in riocm_resp_handler()
438 return -EINVAL; in riocm_resp_handler()
442 ch->rem_channel = ntohs(hh->src_ch); in riocm_resp_handler()
443 complete(&ch->comp); in riocm_resp_handler()
450 * riocm_close_handler - channel close request handler
454 * -ENODEV if cannot find a channel with specified ID,
463 riocm_debug(RX_CMD, "for ch=%d", ntohs(hh->dst_ch)); in riocm_close_handler()
466 ch = idr_find(&ch_idr, ntohs(hh->dst_ch)); in riocm_close_handler()
469 return -ENODEV; in riocm_close_handler()
471 idr_remove(&ch_idr, ch->id); in riocm_close_handler()
484 * rio_cm_handler - function that services request (non-data) packets
485 * @cm: cm_dev object
488 static void rio_cm_handler(struct cm_dev *cm, void *data) in rio_cm_handler() argument
492 if (!rio_mport_is_running(cm->mport)) in rio_cm_handler()
498 hdr->ch_op, ntohs(hdr->dst_ch), ntohs(hdr->src_ch)); in rio_cm_handler()
500 switch (hdr->ch_op) { in rio_cm_handler()
502 riocm_req_handler(cm, data); in rio_cm_handler()
519 * rio_rx_data_handler - received data packet handler
520 * @cm: cm_dev object
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) in rio_rx_data_handler() argument
535 riocm_debug(RX_DATA, "for ch=%d", ntohs(hdr->dst_ch)); in rio_rx_data_handler()
537 ch = riocm_get_channel(ntohs(hdr->dst_ch)); in rio_rx_data_handler()
539 /* Discard data message for non-existing channel */ in rio_rx_data_handler()
541 return -ENODEV; in rio_rx_data_handler()
545 spin_lock(&ch->lock); in rio_rx_data_handler()
547 if (ch->state != RIO_CM_CONNECTED) { in rio_rx_data_handler()
550 ch->id, ch->state); in rio_rx_data_handler()
551 spin_unlock(&ch->lock); in rio_rx_data_handler()
554 return -EIO; in rio_rx_data_handler()
557 if (ch->rx_ring.count == RIOCM_RX_RING_SIZE) { in rio_rx_data_handler()
559 riocm_debug(RX_DATA, "ch=%d is full", ch->id); in rio_rx_data_handler()
560 spin_unlock(&ch->lock); in rio_rx_data_handler()
563 return -ENOMEM; in rio_rx_data_handler()
566 ch->rx_ring.buf[ch->rx_ring.head] = buf; in rio_rx_data_handler()
567 ch->rx_ring.head++; in rio_rx_data_handler()
568 ch->rx_ring.count++; in rio_rx_data_handler()
569 ch->rx_ring.head %= RIOCM_RX_RING_SIZE; in rio_rx_data_handler()
571 complete(&ch->comp); in rio_rx_data_handler()
573 spin_unlock(&ch->lock); in rio_rx_data_handler()
580 * rio_ibmsg_handler - inbound message packet handler
584 struct cm_dev *cm = container_of(work, struct cm_dev, rx_work); in rio_ibmsg_handler() local
588 if (!rio_mport_is_running(cm->mport)) in rio_ibmsg_handler()
592 mutex_lock(&cm->rx_lock); in rio_ibmsg_handler()
593 data = riocm_rx_get_msg(cm); in rio_ibmsg_handler()
595 riocm_rx_fill(cm, 1); in rio_ibmsg_handler()
596 mutex_unlock(&cm->rx_lock); in rio_ibmsg_handler()
603 if (hdr->bhdr.type != RIO_CM_CHAN) { in rio_ibmsg_handler()
606 hdr->bhdr.type); in rio_ibmsg_handler()
612 if (hdr->ch_op == CM_DATA_MSG) in rio_ibmsg_handler()
613 rio_rx_data_handler(cm, data); in rio_ibmsg_handler()
615 rio_cm_handler(cm, data); in rio_ibmsg_handler()
622 struct cm_dev *cm = dev_id; in riocm_inb_msg_event() local
624 if (rio_mport_is_running(cm->mport) && !work_pending(&cm->rx_work)) in riocm_inb_msg_event()
625 queue_work(cm->rx_wq, &cm->rx_work); in riocm_inb_msg_event()
629 * rio_txcq_handler - TX completion handler
630 * @cm: cm_dev object
637 static void rio_txcq_handler(struct cm_dev *cm, int slot) in rio_txcq_handler() argument
646 cm->mport->id, slot, cm->tx_cnt); in rio_txcq_handler()
648 spin_lock(&cm->tx_lock); in rio_txcq_handler()
649 ack_slot = cm->tx_ack_slot; in rio_txcq_handler()
654 while (cm->tx_cnt && ((ack_slot != slot) || in rio_txcq_handler()
655 (cm->tx_cnt == RIOCM_TX_RING_SIZE))) { in rio_txcq_handler()
657 cm->tx_buf[ack_slot] = NULL; in rio_txcq_handler()
659 ack_slot &= (RIOCM_TX_RING_SIZE - 1); in rio_txcq_handler()
660 cm->tx_cnt--; in rio_txcq_handler()
663 if (cm->tx_cnt < 0 || cm->tx_cnt > RIOCM_TX_RING_SIZE) in rio_txcq_handler()
664 riocm_error("tx_cnt %d out of sync", cm->tx_cnt); in rio_txcq_handler()
666 WARN_ON((cm->tx_cnt < 0) || (cm->tx_cnt > RIOCM_TX_RING_SIZE)); in rio_txcq_handler()
668 cm->tx_ack_slot = ack_slot; in rio_txcq_handler()
673 if (!list_empty(&cm->tx_reqs) && (cm->tx_cnt < RIOCM_TX_RING_SIZE)) { in rio_txcq_handler()
677 list_for_each_entry_safe(req, _req, &cm->tx_reqs, node) { in rio_txcq_handler()
678 list_del(&req->node); in rio_txcq_handler()
679 cm->tx_buf[cm->tx_slot] = req->buffer; in rio_txcq_handler()
680 rc = rio_add_outb_message(cm->mport, req->rdev, cmbox, in rio_txcq_handler()
681 req->buffer, req->len); in rio_txcq_handler()
682 kfree(req->buffer); in rio_txcq_handler()
685 ++cm->tx_cnt; in rio_txcq_handler()
686 ++cm->tx_slot; in rio_txcq_handler()
687 cm->tx_slot &= (RIOCM_TX_RING_SIZE - 1); in rio_txcq_handler()
688 if (cm->tx_cnt == RIOCM_TX_RING_SIZE) in rio_txcq_handler()
693 spin_unlock(&cm->tx_lock); in rio_txcq_handler()
699 struct cm_dev *cm = dev_id; in riocm_outb_msg_event() local
701 if (cm && rio_mport_is_running(cm->mport)) in riocm_outb_msg_event()
702 rio_txcq_handler(cm, slot); in riocm_outb_msg_event()
705 static int riocm_queue_req(struct cm_dev *cm, struct rio_dev *rdev, in riocm_queue_req() argument
713 return -ENOMEM; in riocm_queue_req()
715 treq->rdev = rdev; in riocm_queue_req()
716 treq->buffer = buffer; in riocm_queue_req()
717 treq->len = len; in riocm_queue_req()
719 spin_lock_irqsave(&cm->tx_lock, flags); in riocm_queue_req()
720 list_add_tail(&treq->node, &cm->tx_reqs); in riocm_queue_req()
721 spin_unlock_irqrestore(&cm->tx_lock, flags); in riocm_queue_req()
726 * riocm_post_send - helper function that places packet into msg TX queue
727 * @cm: cm_dev object
735 static int riocm_post_send(struct cm_dev *cm, struct rio_dev *rdev, in riocm_post_send() argument
741 spin_lock_irqsave(&cm->tx_lock, flags); in riocm_post_send()
743 if (cm->mport == NULL) { in riocm_post_send()
744 rc = -ENODEV; in riocm_post_send()
748 if (cm->tx_cnt == RIOCM_TX_RING_SIZE) { in riocm_post_send()
750 rc = -EBUSY; in riocm_post_send()
754 cm->tx_buf[cm->tx_slot] = buffer; in riocm_post_send()
755 rc = rio_add_outb_message(cm->mport, rdev, cmbox, buffer, len); in riocm_post_send()
758 buffer, rdev->destid, cm->tx_slot, cm->tx_cnt); in riocm_post_send()
760 ++cm->tx_cnt; in riocm_post_send()
761 ++cm->tx_slot; in riocm_post_send()
762 cm->tx_slot &= (RIOCM_TX_RING_SIZE - 1); in riocm_post_send()
765 spin_unlock_irqrestore(&cm->tx_lock, flags); in riocm_post_send()
770 * riocm_ch_send - sends a data packet to a remote device
772 * @buf: pointer to a data buffer to send (including CM header)
773 * @len: length of data to transfer (including CM header)
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,
790 return -EINVAL; in riocm_ch_send()
794 riocm_error("%s(%d) ch_%d not found", current->comm, in riocm_ch_send()
796 return -ENODEV; in riocm_ch_send()
800 ret = -EAGAIN; in riocm_ch_send()
809 hdr->bhdr.src_id = htonl(ch->loc_destid); in riocm_ch_send()
810 hdr->bhdr.dst_id = htonl(ch->rem_destid); in riocm_ch_send()
811 hdr->bhdr.src_mbox = cmbox; in riocm_ch_send()
812 hdr->bhdr.dst_mbox = cmbox; in riocm_ch_send()
813 hdr->bhdr.type = RIO_CM_CHAN; in riocm_ch_send()
814 hdr->ch_op = CM_DATA_MSG; in riocm_ch_send()
815 hdr->dst_ch = htons(ch->rem_channel); in riocm_ch_send()
816 hdr->src_ch = htons(ch->id); in riocm_ch_send()
817 hdr->msg_len = htons((u16)len); in riocm_ch_send()
820 * HW-specific add_outb_message() routine copies TX data into its own in riocm_ch_send()
825 ret = riocm_post_send(ch->cmdev, ch->rdev, buf, len); in riocm_ch_send()
827 riocm_debug(TX, "ch %d send_err=%d", ch->id, ret); in riocm_ch_send()
835 int i, ret = -EINVAL; in riocm_ch_free_rxbuf()
837 spin_lock_bh(&ch->lock); in riocm_ch_free_rxbuf()
840 if (ch->rx_ring.inuse[i] == buf) { in riocm_ch_free_rxbuf()
841 ch->rx_ring.inuse[i] = NULL; in riocm_ch_free_rxbuf()
842 ch->rx_ring.inuse_cnt--; in riocm_ch_free_rxbuf()
848 spin_unlock_bh(&ch->lock); in riocm_ch_free_rxbuf()
857 * riocm_ch_receive - fetch a data packet received for the specified channel
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.
875 ret = -EAGAIN; in riocm_ch_receive()
879 if (ch->rx_ring.inuse_cnt == RIOCM_RX_RING_SIZE) { in riocm_ch_receive()
883 ret = -ENOMEM; in riocm_ch_receive()
887 wret = wait_for_completion_interruptible_timeout(&ch->comp, timeout); in riocm_ch_receive()
889 riocm_debug(WAIT, "wait on %d returned %ld", ch->id, wret); in riocm_ch_receive()
892 ret = -ETIME; in riocm_ch_receive()
893 else if (wret == -ERESTARTSYS) in riocm_ch_receive()
894 ret = -EINTR; in riocm_ch_receive()
896 ret = riocm_cmp(ch, RIO_CM_CONNECTED) ? 0 : -ECONNRESET; in riocm_ch_receive()
901 spin_lock_bh(&ch->lock); in riocm_ch_receive()
903 rxmsg = ch->rx_ring.buf[ch->rx_ring.tail]; in riocm_ch_receive()
904 ch->rx_ring.buf[ch->rx_ring.tail] = NULL; in riocm_ch_receive()
905 ch->rx_ring.count--; in riocm_ch_receive()
906 ch->rx_ring.tail++; in riocm_ch_receive()
907 ch->rx_ring.tail %= RIOCM_RX_RING_SIZE; in riocm_ch_receive()
908 ret = -ENOMEM; in riocm_ch_receive()
911 if (ch->rx_ring.inuse[i] == NULL) { in riocm_ch_receive()
912 ch->rx_ring.inuse[i] = rxmsg; in riocm_ch_receive()
913 ch->rx_ring.inuse_cnt++; in riocm_ch_receive()
925 spin_unlock_bh(&ch->lock); in riocm_ch_receive()
932 * riocm_ch_connect - sends a connect request to a remote device
934 * @cm: CM device to send connect request
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, in riocm_ch_connect() argument
954 return -ENODEV; in riocm_ch_connect()
957 ret = -EINVAL; in riocm_ch_connect()
961 ch->cmdev = cm; in riocm_ch_connect()
962 ch->rdev = peer->rdev; in riocm_ch_connect()
963 ch->context = NULL; in riocm_ch_connect()
964 ch->loc_destid = cm->mport->host_deviceid; in riocm_ch_connect()
965 ch->rem_channel = rem_ch; in riocm_ch_connect()
973 ret = -ENOMEM; in riocm_ch_connect()
977 hdr->bhdr.src_id = htonl(ch->loc_destid); in riocm_ch_connect()
978 hdr->bhdr.dst_id = htonl(peer->rdev->destid); in riocm_ch_connect()
979 hdr->bhdr.src_mbox = cmbox; in riocm_ch_connect()
980 hdr->bhdr.dst_mbox = cmbox; in riocm_ch_connect()
981 hdr->bhdr.type = RIO_CM_CHAN; in riocm_ch_connect()
982 hdr->ch_op = CM_CONN_REQ; in riocm_ch_connect()
983 hdr->dst_ch = htons(rem_ch); in riocm_ch_connect()
984 hdr->src_ch = htons(loc_ch); in riocm_ch_connect()
987 * HW-specific add_outb_message() routine copies TX data into its in riocm_ch_connect()
991 ret = riocm_post_send(cm, peer->rdev, hdr, sizeof(*hdr)); in riocm_ch_connect()
993 if (ret != -EBUSY) { in riocm_ch_connect()
996 ret = riocm_queue_req(cm, peer->rdev, hdr, sizeof(*hdr)); in riocm_ch_connect()
1007 wret = wait_for_completion_interruptible_timeout(&ch->comp, in riocm_ch_connect()
1009 riocm_debug(WAIT, "wait on %d returns %ld", ch->id, wret); in riocm_ch_connect()
1012 ret = -ETIME; in riocm_ch_connect()
1013 else if (wret == -ERESTARTSYS) in riocm_ch_connect()
1014 ret = -EINTR; in riocm_ch_connect()
1016 ret = riocm_cmp(ch, RIO_CM_CONNECTED) ? 0 : -1; in riocm_ch_connect()
1030 return -ENOMEM; in riocm_send_ack()
1032 hdr->bhdr.src_id = htonl(ch->loc_destid); in riocm_send_ack()
1033 hdr->bhdr.dst_id = htonl(ch->rem_destid); in riocm_send_ack()
1034 hdr->dst_ch = htons(ch->rem_channel); in riocm_send_ack()
1035 hdr->src_ch = htons(ch->id); in riocm_send_ack()
1036 hdr->bhdr.src_mbox = cmbox; in riocm_send_ack()
1037 hdr->bhdr.dst_mbox = cmbox; in riocm_send_ack()
1038 hdr->bhdr.type = RIO_CM_CHAN; in riocm_send_ack()
1039 hdr->ch_op = CM_CONN_ACK; in riocm_send_ack()
1045 ret = riocm_post_send(ch->cmdev, ch->rdev, hdr, sizeof(*hdr)); in riocm_send_ack()
1047 if (ret == -EBUSY && !riocm_queue_req(ch->cmdev, in riocm_send_ack()
1048 ch->rdev, hdr, sizeof(*hdr))) in riocm_send_ack()
1054 ch->id, rio_name(ch->rdev), ret); in riocm_send_ack()
1059 * riocm_ch_accept - accept incoming connection request
1062 * @timeout: wait timeout (if 0 non-blocking call, do not wait if connection
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.
1086 return ERR_PTR(-EINVAL); in riocm_ch_accept()
1089 err = -EINVAL; in riocm_ch_accept()
1095 if (!try_wait_for_completion(&ch->comp)) { in riocm_ch_accept()
1096 err = -EAGAIN; in riocm_ch_accept()
1100 riocm_debug(WAIT, "on %d", ch->id); in riocm_ch_accept()
1102 wret = wait_for_completion_interruptible_timeout(&ch->comp, in riocm_ch_accept()
1105 err = -ETIME; in riocm_ch_accept()
1107 } else if (wret == -ERESTARTSYS) { in riocm_ch_accept()
1108 err = -EINTR; in riocm_ch_accept()
1113 spin_lock_bh(&ch->lock); in riocm_ch_accept()
1115 if (ch->state != RIO_CM_LISTEN) { in riocm_ch_accept()
1116 err = -ECANCELED; in riocm_ch_accept()
1117 } else if (list_empty(&ch->accept_queue)) { in riocm_ch_accept()
1119 ch->id); in riocm_ch_accept()
1120 err = -EIO; in riocm_ch_accept()
1123 spin_unlock_bh(&ch->lock); in riocm_ch_accept()
1126 riocm_debug(WAIT, "on %d returns %d", ch->id, err); in riocm_ch_accept()
1136 err = -ENOMEM; in riocm_ch_accept()
1140 spin_lock_bh(&ch->lock); in riocm_ch_accept()
1142 req = list_first_entry(&ch->accept_queue, struct conn_req, node); in riocm_ch_accept()
1143 list_del(&req->node); in riocm_ch_accept()
1144 new_ch->cmdev = ch->cmdev; in riocm_ch_accept()
1145 new_ch->loc_destid = ch->loc_destid; in riocm_ch_accept()
1146 new_ch->rem_destid = req->destid; in riocm_ch_accept()
1147 new_ch->rem_channel = req->chan; in riocm_ch_accept()
1149 spin_unlock_bh(&ch->lock); in riocm_ch_accept()
1156 list_for_each_entry(peer, &new_ch->cmdev->peers, node) { in riocm_ch_accept()
1157 if (peer->rdev->destid == new_ch->rem_destid) { in riocm_ch_accept()
1159 rio_name(peer->rdev)); in riocm_ch_accept()
1168 err = -ENODEV; in riocm_ch_accept()
1172 new_ch->rdev = peer->rdev; in riocm_ch_accept()
1173 new_ch->state = RIO_CM_CONNECTED; in riocm_ch_accept()
1174 spin_lock_init(&new_ch->lock); in riocm_ch_accept()
1179 *new_ch_id = new_ch->id; in riocm_ch_accept()
1184 idr_remove(&ch_idr, new_ch->id); in riocm_ch_accept()
1196 * riocm_ch_listen - puts a channel into LISTEN state
1200 * -EINVAL if the specified channel does not exists or
1212 return -EINVAL; in riocm_ch_listen()
1214 ret = -EINVAL; in riocm_ch_listen()
1220 * riocm_ch_bind - associate a channel object and an mport device
1226 * -ENODEV if cannot find specified mport,
1227 * -EINVAL if the specified channel does not exist or
1233 struct cm_dev *cm; in riocm_ch_bind() local
1234 int rc = -ENODEV; in riocm_ch_bind()
1240 list_for_each_entry(cm, &cm_dev_list, list) { in riocm_ch_bind()
1241 if ((cm->mport->id == mport_id) && in riocm_ch_bind()
1242 rio_mport_is_running(cm->mport)) { in riocm_ch_bind()
1253 rc = -EINVAL; in riocm_ch_bind()
1257 spin_lock_bh(&ch->lock); in riocm_ch_bind()
1258 if (ch->state != RIO_CM_IDLE) { in riocm_ch_bind()
1259 spin_unlock_bh(&ch->lock); in riocm_ch_bind()
1260 rc = -EINVAL; in riocm_ch_bind()
1264 ch->cmdev = cm; in riocm_ch_bind()
1265 ch->loc_destid = cm->mport->host_deviceid; in riocm_ch_bind()
1266 ch->context = context; in riocm_ch_bind()
1267 ch->state = RIO_CM_CHAN_BOUND; in riocm_ch_bind()
1268 spin_unlock_bh(&ch->lock); in riocm_ch_bind()
1277 * riocm_ch_alloc - channel object allocation helper routine
1281 * or error-valued pointer
1291 return ERR_PTR(-ENOMEM); in riocm_ch_alloc()
1311 return ERR_PTR(id == -ENOSPC ? -EBUSY : id); in riocm_ch_alloc()
1314 ch->id = (u16)id; in riocm_ch_alloc()
1315 ch->state = RIO_CM_IDLE; in riocm_ch_alloc()
1316 spin_lock_init(&ch->lock); in riocm_ch_alloc()
1317 INIT_LIST_HEAD(&ch->accept_queue); in riocm_ch_alloc()
1318 INIT_LIST_HEAD(&ch->ch_node); in riocm_ch_alloc()
1319 init_completion(&ch->comp); in riocm_ch_alloc()
1320 init_completion(&ch->comp_close); in riocm_ch_alloc()
1321 kref_init(&ch->ref); in riocm_ch_alloc()
1322 ch->rx_ring.head = 0; in riocm_ch_alloc()
1323 ch->rx_ring.tail = 0; in riocm_ch_alloc()
1324 ch->rx_ring.count = 0; in riocm_ch_alloc()
1325 ch->rx_ring.inuse_cnt = 0; in riocm_ch_alloc()
1331 * riocm_ch_create - creates a new channel object and allocates ID for it
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
1344 * updated via pointer) or error-valued pointer if error.
1356 *ch_num = ch->id; in riocm_ch_create()
1362 * riocm_ch_free - channel object release routine
1370 riocm_debug(CHOP, "(ch_%d)", ch->id); in riocm_ch_free()
1372 if (ch->rx_ring.inuse_cnt) { in riocm_ch_free()
1374 i < RIOCM_RX_RING_SIZE && ch->rx_ring.inuse_cnt; i++) { in riocm_ch_free()
1375 if (ch->rx_ring.inuse[i] != NULL) { in riocm_ch_free()
1376 kfree(ch->rx_ring.inuse[i]); in riocm_ch_free()
1377 ch->rx_ring.inuse_cnt--; in riocm_ch_free()
1382 if (ch->rx_ring.count) in riocm_ch_free()
1383 for (i = 0; i < RIOCM_RX_RING_SIZE && ch->rx_ring.count; i++) { in riocm_ch_free()
1384 if (ch->rx_ring.buf[i] != NULL) { in riocm_ch_free()
1385 kfree(ch->rx_ring.buf[i]); in riocm_ch_free()
1386 ch->rx_ring.count--; in riocm_ch_free()
1390 complete(&ch->comp_close); in riocm_ch_free()
1404 return -ENOMEM; in riocm_send_close()
1406 hdr->bhdr.src_id = htonl(ch->loc_destid); in riocm_send_close()
1407 hdr->bhdr.dst_id = htonl(ch->rem_destid); in riocm_send_close()
1408 hdr->bhdr.src_mbox = cmbox; in riocm_send_close()
1409 hdr->bhdr.dst_mbox = cmbox; in riocm_send_close()
1410 hdr->bhdr.type = RIO_CM_CHAN; in riocm_send_close()
1411 hdr->ch_op = CM_CONN_CLOSE; in riocm_send_close()
1412 hdr->dst_ch = htons(ch->rem_channel); in riocm_send_close()
1413 hdr->src_ch = htons(ch->id); in riocm_send_close()
1419 ret = riocm_post_send(ch->cmdev, ch->rdev, hdr, sizeof(*hdr)); in riocm_send_close()
1421 if (ret == -EBUSY && !riocm_queue_req(ch->cmdev, ch->rdev, in riocm_send_close()
1427 riocm_error("ch(%d) send CLOSE failed (ret=%d)", ch->id, ret); in riocm_send_close()
1433 * riocm_ch_close - closes a channel object with specified ID (by local request)
1444 ch->id, current->comm, task_pid_nr(current)); in riocm_ch_close()
1450 complete_all(&ch->comp); in riocm_ch_close()
1453 wret = wait_for_completion_interruptible_timeout(&ch->comp_close, tmo); in riocm_ch_close()
1455 riocm_debug(WAIT, "wait on %d returns %ld", ch->id, wret); in riocm_ch_close()
1460 current->comm, task_pid_nr(current), ch->id); in riocm_ch_close()
1461 ret = -ETIMEDOUT; in riocm_ch_close()
1462 } else if (wret == -ERESTARTSYS) { in riocm_ch_close()
1465 current->comm, task_pid_nr(current), ch->id); in riocm_ch_close()
1466 ret = -EINTR; in riocm_ch_close()
1470 riocm_debug(CHOP, "ch_%d resources released", ch->id); in riocm_ch_close()
1473 riocm_debug(CHOP, "failed to release ch_%d resources", ch->id); in riocm_ch_close()
1480 * riocm_cdev_open() - Open character device
1485 current->comm, task_pid_nr(current), filp); in riocm_cdev_open()
1488 return -ENODEV; in riocm_cdev_open()
1494 * riocm_cdev_release() - Release character device
1503 current->comm, task_pid_nr(current), filp); in riocm_cdev_release()
1508 if (ch && ch->filp == filp) { in riocm_cdev_release()
1510 ch->id, current->comm, in riocm_cdev_release()
1512 idr_remove(&ch_idr, ch->id); in riocm_cdev_release()
1513 list_add(&ch->ch_node, &list); in riocm_cdev_release()
1520 list_del(&ch->ch_node); in riocm_cdev_release()
1529 * cm_ep_get_list_size() - Reports number of endpoints in the network
1536 struct cm_dev *cm; in cm_ep_get_list_size() local
1539 return -EFAULT; in cm_ep_get_list_size()
1541 return -EINVAL; in cm_ep_get_list_size()
1545 list_for_each_entry(cm, &cm_dev_list, list) { in cm_ep_get_list_size()
1546 if (cm->mport->id == mport_id) { in cm_ep_get_list_size()
1547 count = cm->npeers; in cm_ep_get_list_size()
1550 return -EFAULT; in cm_ep_get_list_size()
1556 return -ENODEV; in cm_ep_get_list_size()
1560 * cm_ep_get_list() - Returns list of attached endpoints
1564 struct cm_dev *cm; in cm_ep_get_list() local
1574 return -EFAULT; in cm_ep_get_list()
1577 return -EINVAL; in cm_ep_get_list()
1581 list_for_each_entry(cm, &cm_dev_list, list) in cm_ep_get_list()
1582 if (cm->mport->id == (u8)info[1]) in cm_ep_get_list()
1586 return -ENODEV; in cm_ep_get_list()
1589 nent = min(info[0], cm->npeers); in cm_ep_get_list()
1593 return -ENOMEM; in cm_ep_get_list()
1598 list_for_each_entry(peer, &cm->peers, node) { in cm_ep_get_list()
1599 *entry_ptr = (u32)peer->rdev->destid; in cm_ep_get_list()
1609 ret = -EFAULT; in cm_ep_get_list()
1616 * cm_mport_get_list() - Returns list of available local mport devices
1623 struct cm_dev *cm; in cm_mport_get_list() local
1628 return -EFAULT; in cm_mport_get_list()
1630 return -EINVAL; in cm_mport_get_list()
1633 return -ENOMEM; in cm_mport_get_list()
1638 list_for_each_entry(cm, &cm_dev_list, list) { in cm_mport_get_list()
1640 *entry_ptr = (cm->mport->id << 16) | in cm_mport_get_list()
1641 cm->mport->host_deviceid; in cm_mport_get_list()
1649 ret = -EFAULT; in cm_mport_get_list()
1656 * cm_chan_create() - Create a message exchange channel
1665 return -EFAULT; in cm_chan_create()
1668 ch_num, current->comm, task_pid_nr(current)); in cm_chan_create()
1673 ch->filp = filp; in cm_chan_create()
1675 ch_num, current->comm, task_pid_nr(current)); in cm_chan_create()
1680 * cm_chan_close() - Close channel
1691 return -EFAULT; in cm_chan_close()
1694 ch_num, current->comm, task_pid_nr(current)); in cm_chan_close()
1702 if (ch->filp != filp) { in cm_chan_close()
1704 return -EINVAL; in cm_chan_close()
1706 idr_remove(&ch_idr, ch->id); in cm_chan_close()
1713 * cm_chan_bind() - Bind channel
1721 return -EFAULT; in cm_chan_bind()
1723 return -EINVAL; in cm_chan_bind()
1729 * cm_chan_listen() - Listen on channel
1738 return -EFAULT; in cm_chan_listen()
1744 * cm_chan_accept() - Accept incoming connection
1755 return -EFAULT; in cm_chan_accept()
1758 param.ch_num, current->comm, task_pid_nr(current)); in cm_chan_accept()
1766 ch->filp = filp; in cm_chan_accept()
1769 ch->id, current->comm, task_pid_nr(current)); in cm_chan_accept()
1772 return -EFAULT; in cm_chan_accept()
1777 * cm_chan_connect() - Connect on channel
1783 struct cm_dev *cm; in cm_chan_connect() local
1785 int ret = -ENODEV; in cm_chan_connect()
1788 return -EFAULT; in cm_chan_connect()
1790 return -EINVAL; in cm_chan_connect()
1795 list_for_each_entry(cm, &cm_dev_list, list) { in cm_chan_connect()
1796 if (cm->mport->id == chan.mport_id) { in cm_chan_connect()
1805 if (chan.remote_destid >= RIO_ANY_DESTID(cm->mport->sys_size)) { in cm_chan_connect()
1806 ret = -EINVAL; in cm_chan_connect()
1811 ret = -ENODEV; in cm_chan_connect()
1813 list_for_each_entry(peer, &cm->peers, node) { in cm_chan_connect()
1814 if (peer->rdev->destid == chan.remote_destid) { in cm_chan_connect()
1825 return riocm_ch_connect(chan.id, cm, peer, chan.remote_channel); in cm_chan_connect()
1832 * cm_chan_msg_send() - Send a message through channel
1842 return -EFAULT; in cm_chan_msg_send()
1844 return -EINVAL; in cm_chan_msg_send()
1857 * cm_chan_msg_rcv() - Receive a message through channel
1869 return -EFAULT; in cm_chan_msg_rcv()
1872 return -EINVAL; in cm_chan_msg_rcv()
1876 return -ENODEV; in cm_chan_msg_rcv()
1887 ret = -EFAULT; in cm_chan_msg_rcv()
1896 * riocm_cdev_ioctl() - IOCTL requests handler
1928 return -EINVAL; in riocm_cdev_ioctl()
1939 * riocm_add_dev - add new remote RapidIO device into channel management core
1950 struct cm_dev *cm; in riocm_add_dev() local
1952 /* Check if the remote device has capabilities required to support CM */ in riocm_add_dev()
1960 return -ENOMEM; in riocm_add_dev()
1964 list_for_each_entry(cm, &cm_dev_list, list) { in riocm_add_dev()
1965 if (cm->mport == rdev->net->hport) in riocm_add_dev()
1971 return -ENODEV; in riocm_add_dev()
1974 peer->rdev = rdev; in riocm_add_dev()
1975 list_add_tail(&peer->node, &cm->peers); in riocm_add_dev()
1976 cm->npeers++; in riocm_add_dev()
1983 * riocm_remove_dev - remove remote RapidIO device from channel management core
1993 struct cm_dev *cm; in riocm_remove_dev() local
2000 /* Check if the remote device has capabilities required to support CM */ in riocm_remove_dev()
2008 list_for_each_entry(cm, &cm_dev_list, list) { in riocm_remove_dev()
2009 if (cm->mport == rdev->net->hport) { in riocm_remove_dev()
2022 list_for_each_entry(peer, &cm->peers, node) { in riocm_remove_dev()
2023 if (peer->rdev == rdev) { in riocm_remove_dev()
2026 list_del(&peer->node); in riocm_remove_dev()
2027 cm->npeers--; in riocm_remove_dev()
2044 if (ch && ch->rdev == rdev) { in riocm_remove_dev()
2045 if (atomic_read(&rdev->state) != RIO_DEVICE_SHUTDOWN) in riocm_remove_dev()
2047 idr_remove(&ch_idr, ch->id); in riocm_remove_dev()
2048 list_add(&ch->ch_node, &list); in riocm_remove_dev()
2055 list_del(&ch->ch_node); in riocm_remove_dev()
2062 * riocm_cdev_add() - Create rio_cm char device
2090 * riocm_add_mport - add new local mport device into channel management core
2093 * 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
2172 * registered with the CM core.
2177 struct cm_dev *cm; in riocm_remove_mport() local
2184 riocm_debug(MPORT, "%s", mport->name); in riocm_remove_mport()
2188 list_for_each_entry(cm, &cm_dev_list, list) { in riocm_remove_mport()
2189 if (cm->mport == mport) { in riocm_remove_mport()
2190 list_del(&cm->list); in riocm_remove_mport()
2199 flush_workqueue(cm->rx_wq); in riocm_remove_mport()
2200 destroy_workqueue(cm->rx_wq); in riocm_remove_mport()
2205 if (ch->cmdev == cm) { in riocm_remove_mport()
2207 mport->name, ch->id); in riocm_remove_mport()
2208 idr_remove(&ch_idr, ch->id); in riocm_remove_mport()
2209 list_add(&ch->ch_node, &list); in riocm_remove_mport()
2216 list_del(&ch->ch_node); in riocm_remove_mport()
2225 if (!list_empty(&cm->peers)) in riocm_remove_mport()
2227 list_for_each_entry_safe(peer, temp, &cm->peers, node) { in riocm_remove_mport()
2228 riocm_debug(RDEV, "removing peer %s", rio_name(peer->rdev)); in riocm_remove_mport()
2229 list_del(&peer->node); in riocm_remove_mport()
2233 riocm_rx_free(cm); in riocm_remove_mport()
2234 kfree(cm); in riocm_remove_mport()
2235 riocm_debug(MPORT, "%s done", mport->name); in riocm_remove_mport()
2256 if (ch->state == RIO_CM_CONNECTED) { in rio_cm_shutdown()
2257 riocm_debug(EXIT, "close ch %d", ch->id); in rio_cm_shutdown()
2258 idr_remove(&ch_idr, ch->id); in rio_cm_shutdown()
2259 list_add(&ch->ch_node, &list); in rio_cm_shutdown()
2274 .name = "rio_cm",
2343 ret = -ENODEV; in riocm_init()