1 /*
2  * Copyright (c) 2016 Wind River Systems, Inc.
3  * Copyright (c) 2016,2022 Intel Corporation
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <zephyr/kernel.h>
9 #include <zephyr/init.h>
10 #include <zephyr/drivers/i3c.h>
11 
12 #include <zephyr/logging/log.h>
13 LOG_MODULE_DECLARE(i3c, CONFIG_I3C_LOG_LEVEL);
14 
15 /* Statically allocated array of IBI work item nodes */
16 static struct i3c_ibi_work i3c_ibi_work_nodes[CONFIG_I3C_IBI_WORKQUEUE_LENGTH];
17 
18 static K_KERNEL_STACK_DEFINE(i3c_ibi_work_q_stack,
19 			     CONFIG_I3C_IBI_WORKQUEUE_STACK_SIZE);
20 
21 /* IBI workqueue */
22 static struct k_work_q i3c_ibi_work_q;
23 
24 static sys_slist_t i3c_ibi_work_nodes_free;
25 
ibi_work_submit(struct i3c_ibi_work * ibi_node)26 static inline int ibi_work_submit(struct i3c_ibi_work *ibi_node)
27 {
28 	return k_work_submit_to_queue(&i3c_ibi_work_q, &ibi_node->work);
29 }
30 
i3c_ibi_work_enqueue(struct i3c_ibi_work * ibi_work)31 int i3c_ibi_work_enqueue(struct i3c_ibi_work *ibi_work)
32 {
33 	sys_snode_t *node;
34 	struct i3c_ibi_work *ibi_node;
35 	int ret;
36 
37 	node = sys_slist_get(&i3c_ibi_work_nodes_free);
38 	if (node == NULL) {
39 		ret = -ENOMEM;
40 		goto out;
41 	}
42 
43 	ibi_node = (struct i3c_ibi_work *)node;
44 
45 	(void)memcpy(ibi_node, ibi_work, sizeof(*ibi_node));
46 
47 	ret = ibi_work_submit(ibi_node);
48 	if (ret >= 0) {
49 		ret = 0;
50 	}
51 
52 out:
53 	return ret;
54 }
55 
i3c_ibi_work_enqueue_target_irq(struct i3c_device_desc * target,uint8_t * payload,size_t payload_len)56 int i3c_ibi_work_enqueue_target_irq(struct i3c_device_desc *target,
57 				    uint8_t *payload, size_t payload_len)
58 {
59 	sys_snode_t *node;
60 	struct i3c_ibi_work *ibi_node;
61 	int ret;
62 
63 	node = sys_slist_get(&i3c_ibi_work_nodes_free);
64 	if (node == NULL) {
65 		ret = -ENOMEM;
66 		goto out;
67 	}
68 
69 	ibi_node = (struct i3c_ibi_work *)node;
70 
71 	ibi_node->type = I3C_IBI_TARGET_INTR;
72 	ibi_node->target = target;
73 	ibi_node->payload.payload_len = payload_len;
74 
75 	if ((payload != NULL) && (payload_len > 0U)) {
76 		(void)memcpy(&ibi_node->payload.payload[0],
77 			     payload, payload_len);
78 	}
79 
80 	ret = ibi_work_submit(ibi_node);
81 	if (ret >= 0) {
82 		ret = 0;
83 	}
84 
85 out:
86 	return ret;
87 }
88 
i3c_ibi_work_enqueue_hotjoin(const struct device * dev)89 int i3c_ibi_work_enqueue_hotjoin(const struct device *dev)
90 {
91 	sys_snode_t *node;
92 	struct i3c_ibi_work *ibi_node;
93 	int ret;
94 
95 	node = sys_slist_get(&i3c_ibi_work_nodes_free);
96 	if (node == NULL) {
97 		ret = -ENOMEM;
98 		goto out;
99 	}
100 
101 	ibi_node = (struct i3c_ibi_work *)node;
102 
103 	ibi_node->type = I3C_IBI_HOTJOIN;
104 	ibi_node->controller = dev;
105 	ibi_node->payload.payload_len = 0;
106 
107 	ret = ibi_work_submit(ibi_node);
108 	if (ret >= 0) {
109 		ret = 0;
110 	}
111 
112 out:
113 	return ret;
114 }
115 
i3c_ibi_work_enqueue_cb(const struct device * dev,k_work_handler_t work_cb)116 int i3c_ibi_work_enqueue_cb(const struct device *dev,
117 			    k_work_handler_t work_cb)
118 {
119 	sys_snode_t *node;
120 	struct i3c_ibi_work *ibi_node;
121 	int ret;
122 
123 	node = sys_slist_get(&i3c_ibi_work_nodes_free);
124 	if (node == NULL) {
125 		ret = -ENOMEM;
126 		goto out;
127 	}
128 
129 	ibi_node = (struct i3c_ibi_work *)node;
130 
131 	ibi_node->type = I3C_IBI_WORKQUEUE_CB;
132 	ibi_node->controller = dev;
133 	ibi_node->work_cb = work_cb;
134 
135 	ret = ibi_work_submit(ibi_node);
136 	if (ret >= 0) {
137 		ret = 0;
138 	}
139 
140 out:
141 	return ret;
142 }
143 
i3c_ibi_work_handler(struct k_work * work)144 static void i3c_ibi_work_handler(struct k_work *work)
145 {
146 	struct i3c_ibi_work *ibi_node = CONTAINER_OF(work, struct i3c_ibi_work, work);
147 	struct i3c_ibi_payload *payload;
148 	int ret = 0;
149 
150 	if (IS_ENABLED(CONFIG_I3C_IBI_WORKQUEUE_VERBOSE_DEBUG) &&
151 	    ((uint32_t)ibi_node->type <= I3C_IBI_TYPE_MAX)) {
152 		LOG_DBG("Processing IBI work %p (type %d, len %u)",
153 			ibi_node, (int)ibi_node->type,
154 			ibi_node->payload.payload_len);
155 
156 		if (ibi_node->payload.payload_len > 0U) {
157 			LOG_HEXDUMP_DBG(&ibi_node->payload.payload[0],
158 					ibi_node->payload.payload_len, "IBI Payload");
159 		}
160 	}
161 
162 	switch (ibi_node->type) {
163 	case I3C_IBI_TARGET_INTR:
164 		if (ibi_node->payload.payload_len != 0U) {
165 			payload = &ibi_node->payload;
166 		} else {
167 			payload = NULL;
168 		}
169 
170 		ret = ibi_node->target->ibi_cb(ibi_node->target, payload);
171 		if ((ret != 0) && (ret != -EBUSY)) {
172 			LOG_ERR("IBI work %p cb returns %d", ibi_node, ret);
173 		}
174 		break;
175 
176 	case I3C_IBI_HOTJOIN:
177 		ret = i3c_do_daa(ibi_node->controller);
178 		if ((ret != 0) && (ret != -EBUSY)) {
179 			LOG_ERR("i3c_do_daa returns %d", ret);
180 		}
181 		break;
182 
183 	case I3C_IBI_WORKQUEUE_CB:
184 		if (ibi_node->work_cb != NULL) {
185 			ibi_node->work_cb(work);
186 		}
187 		break;
188 
189 	case I3C_IBI_CONTROLLER_ROLE_REQUEST:
190 		/* TODO: Add support for controller role request */
191 		__fallthrough;
192 
193 	default:
194 		/* Unknown IBI type: do nothing */
195 		LOG_DBG("Cannot process IBI type %d", (int)ibi_node->type);
196 		break;
197 	}
198 
199 	if (ret == -EBUSY) {
200 		/* Retry if bus is busy. */
201 		if (ibi_work_submit(ibi_node) < 0) {
202 			LOG_ERR("Error re-adding IBI work %p", ibi_node);
203 		}
204 	} else {
205 		/* Add the now processed node back to the free list */
206 		sys_slist_append(&i3c_ibi_work_nodes_free, (sys_snode_t *)ibi_node);
207 	}
208 }
209 
i3c_ibi_work_q_init(void)210 static int i3c_ibi_work_q_init(void)
211 {
212 	struct k_work_queue_config cfg = {
213 		.name = "i3c_ibi_workq",
214 		.no_yield = true,
215 	};
216 
217 	/* Init the linked list of work item nodes */
218 	sys_slist_init(&i3c_ibi_work_nodes_free);
219 
220 	for (int i = 0; i < ARRAY_SIZE(i3c_ibi_work_nodes); i++) {
221 		i3c_ibi_work_nodes[i].work.handler = i3c_ibi_work_handler;
222 
223 		sys_slist_append(&i3c_ibi_work_nodes_free,
224 				 (sys_snode_t *)&i3c_ibi_work_nodes[i]);
225 	}
226 
227 	/* Start the workqueue */
228 	k_work_queue_start(&i3c_ibi_work_q, i3c_ibi_work_q_stack,
229 			   K_KERNEL_STACK_SIZEOF(i3c_ibi_work_q_stack),
230 			   CONFIG_I3C_IBI_WORKQUEUE_PRIORITY, &cfg);
231 
232 	return 0;
233 }
234 
235 SYS_INIT(i3c_ibi_work_q_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
236