Lines Matching refs:channel

30  * Each channel consists of a control item (channel info) and a ring buffer
31 * pair. The channel info carry information related to channel state, flow
37 * Upon creating a new channel the remote processor allocates channel info and
39 * interrupt is sent to the other end of the channel and a scan for new
40 * channels should be done. A channel never goes away, it will only change
44 * channel by setting the state of its end of the channel to "opening" and
46 * consume the channel. Upon finding a consumer we finish the handshake and the
47 * channel is up.
49 * Upon closing a channel, the remote processor will update the state of its
50 * end of the channel and signal us, we will then unregister any attached
51 * device and close our end of the channel.
53 * Devices attached to a channel can use the qcom_smd_send function to push
54 * data to the channel, this is done by copying the data into the tx ring
55 * buffer, updating the pointers in the channel info and signaling the remote
59 * receiving the interrupt we check the channel info for new data and delivers
76 * smd channel entries.
107 * @mbox_chan: apcs ipc mailbox channel handle
111 * @smem_available: last available amount of smem triggering a channel scan
112 * @new_channel_event: wait queue for new channel events
148 * SMD channel states.
177 * struct qcom_smd_channel - smd channel struct
178 * @edge: qcom_smd_edge this channel is living on
180 * @registered: flag to indicate if the channel is registered
181 * @name: name of the channel
182 * @state: local state of the channel
183 * @remote_state: remote state of the channel
185 * @info: byte aligned outgoing/incoming channel info
186 * @info_word: word aligned outgoing/incoming channel info
187 * @tx_lock: lock to make writes to the channel mutually exclusive
193 * @cb: callback function registered for this channel
275 #define GET_RX_CHANNEL_FLAG(channel, param) \
277 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u8)); \
278 channel->info_word ? \
279 le32_to_cpu(channel->info_word->rx.param) : \
280 channel->info->rx.param; \
283 #define GET_RX_CHANNEL_INFO(channel, param) \
285 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u32)); \
286 le32_to_cpu(channel->info_word ? \
287 channel->info_word->rx.param : \
288 channel->info->rx.param); \
291 #define SET_RX_CHANNEL_FLAG(channel, param, value) \
293 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u8)); \
294 if (channel->info_word) \
295 channel->info_word->rx.param = cpu_to_le32(value); \
297 channel->info->rx.param = value; \
300 #define SET_RX_CHANNEL_INFO(channel, param, value) \
302 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u32)); \
303 if (channel->info_word) \
304 channel->info_word->rx.param = cpu_to_le32(value); \
306 channel->info->rx.param = cpu_to_le32(value); \
309 #define GET_TX_CHANNEL_FLAG(channel, param) \
311 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u8)); \
312 channel->info_word ? \
313 le32_to_cpu(channel->info_word->tx.param) : \
314 channel->info->tx.param; \
317 #define GET_TX_CHANNEL_INFO(channel, param) \
319 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u32)); \
320 le32_to_cpu(channel->info_word ? \
321 channel->info_word->tx.param : \
322 channel->info->tx.param); \
325 #define SET_TX_CHANNEL_FLAG(channel, param, value) \
327 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u8)); \
328 if (channel->info_word) \
329 channel->info_word->tx.param = cpu_to_le32(value); \
331 channel->info->tx.param = value; \
334 #define SET_TX_CHANNEL_INFO(channel, param, value) \
336 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u32)); \
337 if (channel->info_word) \
338 channel->info_word->tx.param = cpu_to_le32(value); \
340 channel->info->tx.param = cpu_to_le32(value); \
344 * struct qcom_smd_alloc_entry - channel allocation entry
345 * @name: channel name
346 * @cid: channel index
347 * @flags: channel flags and edge id
348 * @ref_count: reference count of the channel
368 * Signal the remote processor associated with 'channel'.
370 static void qcom_smd_signal_channel(struct qcom_smd_channel *channel)
372 struct qcom_smd_edge *edge = channel->edge;
388 * Initialize the tx channel info
390 static void qcom_smd_channel_reset(struct qcom_smd_channel *channel)
392 SET_TX_CHANNEL_INFO(channel, state, SMD_CHANNEL_CLOSED);
393 SET_TX_CHANNEL_FLAG(channel, fDSR, 0);
394 SET_TX_CHANNEL_FLAG(channel, fCTS, 0);
395 SET_TX_CHANNEL_FLAG(channel, fCD, 0);
396 SET_TX_CHANNEL_FLAG(channel, fRI, 0);
397 SET_TX_CHANNEL_FLAG(channel, fHEAD, 0);
398 SET_TX_CHANNEL_FLAG(channel, fTAIL, 0);
399 SET_TX_CHANNEL_FLAG(channel, fSTATE, 1);
400 SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 1);
401 SET_TX_CHANNEL_INFO(channel, head, 0);
402 SET_RX_CHANNEL_INFO(channel, tail, 0);
404 qcom_smd_signal_channel(channel);
406 channel->state = SMD_CHANNEL_CLOSED;
407 channel->pkt_size = 0;
411 * Set the callback for a channel, with appropriate locking
413 static void qcom_smd_channel_set_callback(struct qcom_smd_channel *channel,
416 struct rpmsg_endpoint *ept = &channel->qsept->ept;
419 spin_lock_irqsave(&channel->recv_lock, flags);
421 spin_unlock_irqrestore(&channel->recv_lock, flags);
427 static size_t qcom_smd_channel_get_rx_avail(struct qcom_smd_channel *channel)
432 head = GET_RX_CHANNEL_INFO(channel, head);
433 tail = GET_RX_CHANNEL_INFO(channel, tail);
435 return (head - tail) & (channel->fifo_size - 1);
439 * Set tx channel state and inform the remote processor
441 static void qcom_smd_channel_set_state(struct qcom_smd_channel *channel,
444 struct qcom_smd_edge *edge = channel->edge;
447 if (channel->state == state)
450 dev_dbg(&edge->dev, "set_state(%s, %d)\n", channel->name, state);
452 SET_TX_CHANNEL_FLAG(channel, fDSR, is_open);
453 SET_TX_CHANNEL_FLAG(channel, fCTS, is_open);
454 SET_TX_CHANNEL_FLAG(channel, fCD, is_open);
456 SET_TX_CHANNEL_INFO(channel, state, state);
457 SET_TX_CHANNEL_FLAG(channel, fSTATE, 1);
459 channel->state = state;
460 qcom_smd_signal_channel(channel);
497 static size_t qcom_smd_channel_peek(struct qcom_smd_channel *channel,
504 word_aligned = channel->info_word;
505 tail = GET_RX_CHANNEL_INFO(channel, tail);
507 len = min_t(size_t, count, channel->fifo_size - tail);
510 channel->rx_fifo + tail,
517 channel->rx_fifo,
528 static void qcom_smd_channel_advance(struct qcom_smd_channel *channel,
533 tail = GET_RX_CHANNEL_INFO(channel, tail);
535 tail &= (channel->fifo_size - 1);
536 SET_RX_CHANNEL_INFO(channel, tail, tail);
542 static int qcom_smd_channel_recv_single(struct qcom_smd_channel *channel)
544 struct rpmsg_endpoint *ept = &channel->qsept->ept;
550 tail = GET_RX_CHANNEL_INFO(channel, tail);
553 if (tail + channel->pkt_size >= channel->fifo_size) {
554 ptr = channel->bounce_buffer;
555 len = qcom_smd_channel_peek(channel, ptr, channel->pkt_size);
557 ptr = channel->rx_fifo + tail;
558 len = channel->pkt_size;
566 qcom_smd_channel_advance(channel, len);
568 channel->pkt_size = 0;
574 * Per channel interrupt handling
576 static bool qcom_smd_channel_intr(struct qcom_smd_channel *channel)
585 remote_state = GET_RX_CHANNEL_INFO(channel, state);
586 if (remote_state != channel->remote_state) {
587 channel->remote_state = remote_state;
590 wake_up_interruptible_all(&channel->state_change_event);
593 SET_RX_CHANNEL_FLAG(channel, fSTATE, 0);
596 if (!GET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR))
597 wake_up_interruptible_all(&channel->fblockread_event);
599 /* Don't consume any data until we've opened the channel */
600 if (channel->state != SMD_CHANNEL_OPENED)
604 SET_RX_CHANNEL_FLAG(channel, fHEAD, 0);
608 avail = qcom_smd_channel_get_rx_avail(channel);
610 if (!channel->pkt_size && avail >= SMD_PACKET_HEADER_LEN) {
611 qcom_smd_channel_peek(channel, &pktlen, sizeof(pktlen));
612 qcom_smd_channel_advance(channel, SMD_PACKET_HEADER_LEN);
613 channel->pkt_size = le32_to_cpu(pktlen);
614 } else if (channel->pkt_size && avail >= channel->pkt_size) {
615 ret = qcom_smd_channel_recv_single(channel);
624 SET_RX_CHANNEL_FLAG(channel, fTAIL, 1);
627 if (!GET_RX_CHANNEL_FLAG(channel, fBLOCKREADINTR)) {
628 /* Ensure ordering of channel info updates */
631 qcom_smd_signal_channel(channel);
640 * channel info updates or when new channels are created.
645 struct qcom_smd_channel *channel;
654 list_for_each_entry(channel, &edge->channels, list) {
655 spin_lock(&channel->recv_lock);
656 kick_state |= qcom_smd_channel_intr(channel);
657 spin_unlock(&channel->recv_lock);
662 * Creating a new channel requires allocating an smem entry, so we only
683 static size_t qcom_smd_get_tx_avail(struct qcom_smd_channel *channel)
687 unsigned mask = channel->fifo_size - 1;
689 head = GET_TX_CHANNEL_INFO(channel, head);
690 tail = GET_TX_CHANNEL_INFO(channel, tail);
696 * Write count bytes of data into channel, possibly wrapping in the ring buffer
698 static int qcom_smd_write_fifo(struct qcom_smd_channel *channel,
706 word_aligned = channel->info_word;
707 head = GET_TX_CHANNEL_INFO(channel, head);
709 len = min_t(size_t, count, channel->fifo_size - head);
711 smd_copy_to_fifo(channel->tx_fifo + head,
718 smd_copy_to_fifo(channel->tx_fifo,
725 head &= (channel->fifo_size - 1);
726 SET_TX_CHANNEL_INFO(channel, head, head);
732 * __qcom_smd_send - write data to smd channel
733 * @channel: channel handle
738 * This is a blocking write of len bytes into the channel's tx ring buffer and
743 static int __qcom_smd_send(struct qcom_smd_channel *channel, const void *data,
752 if (channel->info_word && len % 4)
756 if (tlen >= channel->fifo_size)
763 spin_lock_irqsave(&channel->tx_lock, flags);
765 while (qcom_smd_get_tx_avail(channel) < tlen &&
766 channel->state == SMD_CHANNEL_OPENED) {
772 SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 0);
775 spin_unlock_irqrestore(&channel->tx_lock, flags);
777 ret = wait_event_interruptible(channel->fblockread_event,
778 qcom_smd_get_tx_avail(channel) >= tlen ||
779 channel->state != SMD_CHANNEL_OPENED);
783 spin_lock_irqsave(&channel->tx_lock, flags);
785 SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 1);
788 /* Fail if the channel was closed */
789 if (channel->state != SMD_CHANNEL_OPENED) {
794 SET_TX_CHANNEL_FLAG(channel, fTAIL, 0);
796 qcom_smd_write_fifo(channel, hdr, sizeof(hdr));
797 qcom_smd_write_fifo(channel, data, len);
799 SET_TX_CHANNEL_FLAG(channel, fHEAD, 1);
801 /* Ensure ordering of channel info updates */
804 qcom_smd_signal_channel(channel);
807 spin_unlock_irqrestore(&channel->tx_lock, flags);
813 * Helper for opening a channel
815 static int qcom_smd_channel_open(struct qcom_smd_channel *channel,
818 struct qcom_smd_edge *edge = channel->edge;
825 bb_size = min(channel->fifo_size, SZ_4K);
826 channel->bounce_buffer = kmalloc(bb_size, GFP_KERNEL);
827 if (!channel->bounce_buffer)
830 qcom_smd_channel_set_callback(channel, cb);
831 qcom_smd_channel_set_state(channel, SMD_CHANNEL_OPENING);
834 ret = wait_event_interruptible_timeout(channel->state_change_event,
835 channel->remote_state == SMD_CHANNEL_OPENING ||
836 channel->remote_state == SMD_CHANNEL_OPENED,
843 qcom_smd_channel_set_state(channel, SMD_CHANNEL_OPENED);
846 ret = wait_event_interruptible_timeout(channel->state_change_event,
847 channel->remote_state == SMD_CHANNEL_OPENED,
857 qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSED);
862 * Helper for closing and resetting a channel
864 static void qcom_smd_channel_close(struct qcom_smd_channel *channel)
866 qcom_smd_channel_set_callback(channel, NULL);
868 kfree(channel->bounce_buffer);
869 channel->bounce_buffer = NULL;
871 qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSED);
872 qcom_smd_channel_reset(channel);
878 struct qcom_smd_channel *channel;
883 list_for_each_entry(channel, &edge->channels, list) {
884 if (!strcmp(channel->name, name)) {
885 ret = channel;
906 struct qcom_smd_channel *channel;
913 /* Wait up to HZ for the channel to appear */
915 (channel = qcom_smd_find_channel(edge, name)) != NULL,
920 if (channel->state != SMD_CHANNEL_CLOSED) {
921 dev_err(&rpdev->dev, "channel %s is busy\n", channel->name);
938 channel->qsept = qsept;
939 qsept->qsch = channel;
941 ret = qcom_smd_channel_open(channel, cb);
948 channel->qsept = NULL;
995 struct qcom_smd_channel *channel = qsept->qsch;
998 poll_wait(filp, &channel->fblockread_event, wait);
1000 if (qcom_smd_get_tx_avail(channel) > 20)
1007 * Finds the device_node for the smd child interested in this channel.
1010 const char *channel)
1023 if (strcmp(name, channel) == 0)
1033 struct qcom_smd_channel *channel = qept->qsch;
1037 spin_lock_irqsave(&channel->recv_lock, flags);
1038 kick_state = qcom_smd_channel_intr(channel);
1039 spin_unlock_irqrestore(&channel->recv_lock, flags);
1042 schedule_work(&channel->edge->state_work);
1070 * Create a smd client device for channel that is being opened.
1072 static int qcom_smd_create_device(struct qcom_smd_channel *channel)
1076 struct qcom_smd_edge *edge = channel->edge;
1078 dev_dbg(&edge->dev, "registering '%s'\n", channel->name);
1092 strscpy_pad(rpdev->id.name, channel->name, RPMSG_NAME_SIZE);
1096 rpdev->dev.of_node = qcom_smd_match_channel(edge->of_node, channel->name);
1120 * Allocate the qcom_smd_channel object for a newly found smd channel,
1128 struct qcom_smd_channel *channel;
1135 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
1136 if (!channel)
1139 channel->edge = edge;
1140 channel->name = kstrdup(name, GFP_KERNEL);
1141 if (!channel->name) {
1146 spin_lock_init(&channel->tx_lock);
1147 spin_lock_init(&channel->recv_lock);
1148 init_waitqueue_head(&channel->fblockread_event);
1149 init_waitqueue_head(&channel->state_change_event);
1158 * Use the size of the item to figure out which channel info struct to
1162 channel->info_word = info;
1164 channel->info = info;
1167 "channel info of size %zu not supported\n", info_size);
1178 /* The channel consist of a rx and tx fifo of equal size */
1181 dev_dbg(&edge->dev, "new channel '%s' info-size: %zu fifo-size: %zu\n",
1184 channel->tx_fifo = fifo_base;
1185 channel->rx_fifo = fifo_base + fifo_size;
1186 channel->fifo_size = fifo_size;
1188 qcom_smd_channel_reset(channel);
1190 return channel;
1193 kfree(channel->name);
1195 kfree(channel);
1210 struct qcom_smd_channel *channel;
1246 channel = qcom_smd_create_channel(edge, info_id, fifo_id, entry->name);
1247 if (IS_ERR(channel))
1251 list_add(&channel->list, &edge->channels);
1254 dev_dbg(&edge->dev, "new channel found: '%s'\n", channel->name);
1274 struct qcom_smd_channel *channel;
1283 * Register a device for any closed channel where the remote processor
1284 * is showing interest in opening the channel.
1287 list_for_each_entry(channel, &edge->channels, list) {
1288 if (channel->state != SMD_CHANNEL_CLOSED)
1295 remote_state = GET_RX_CHANNEL_INFO(channel, state);
1298 strcmp(channel->name, "rpm_requests"))
1301 if (channel->registered)
1305 qcom_smd_create_device(channel);
1307 channel->registered = true;
1311 * Unregister the device for any channel that is opened where the
1312 * remote processor is closing the channel.
1314 list_for_each_entry(channel, &edge->channels, list) {
1315 if (channel->state != SMD_CHANNEL_OPENING &&
1316 channel->state != SMD_CHANNEL_OPENED)
1319 remote_state = GET_RX_CHANNEL_INFO(channel, state);
1326 strscpy_pad(chinfo.name, channel->name, sizeof(chinfo.name));
1330 channel->registered = false;
1438 * Reset the state of each associated channel and free the edge context.
1442 struct qcom_smd_channel *channel, *tmp;
1445 list_for_each_entry_safe(channel, tmp, &edge->channels, list) {
1446 list_del(&channel->list);
1447 kfree(channel->name);
1448 kfree(channel);