1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2021, MediaTek Inc.
4 * Copyright (c) 2021-2022, Intel Corporation.
7 * Haijun Liu <haijun.liu@mediatek.com>
8 * Ricardo Martinez <ricardo.martinez@linux.intel.com>
9 * Moises Veleta <moises.veleta@intel.com>
12 * Amir Hanania <amir.hanania@intel.com>
13 * Chiranjeevi Rapolu <chiranjeevi.rapolu@intel.com>
14 * Eliot Lee <eliot.lee@intel.com>
15 * Sreehari Kancharla <sreehari.kancharla@intel.com>
18 #include <linux/bitfield.h>
19 #include <linux/device.h>
20 #include <linux/err.h>
21 #include <linux/kthread.h>
22 #include <linux/netdevice.h>
23 #include <linux/skbuff.h>
24 #include <linux/spinlock.h>
26 #include "t7xx_port.h"
27 #include "t7xx_port_proxy.h"
28 #include "t7xx_state_monitor.h"
30 #define PORT_MSG_VERSION GENMASK(31, 16)
31 #define PORT_MSG_PRT_CNT GENMASK(15, 0)
40 static int port_ctl_send_msg_to_md(struct t7xx_port *port, unsigned int msg, unsigned int ex_msg)
45 skb = t7xx_ctrl_alloc_skb(0);
49 ret = t7xx_port_send_ctl_skb(port, skb, msg, ex_msg);
51 dev_kfree_skb_any(skb);
56 static int fsm_ee_message_handler(struct t7xx_port *port, struct t7xx_fsm_ctl *ctl,
59 struct ctrl_msg_header *ctrl_msg_h = (struct ctrl_msg_header *)skb->data;
60 struct device *dev = &ctl->md->t7xx_dev->pdev->dev;
61 enum md_state md_state;
64 md_state = t7xx_fsm_get_md_state(ctl);
65 if (md_state != MD_STATE_EXCEPTION) {
66 dev_err(dev, "Receive invalid MD_EX %x when MD state is %d\n",
67 ctrl_msg_h->ex_msg, md_state);
71 switch (le32_to_cpu(ctrl_msg_h->ctrl_msg_id)) {
73 if (le32_to_cpu(ctrl_msg_h->ex_msg) != MD_EX_CHK_ID) {
74 dev_err(dev, "Receive invalid MD_EX %x\n", ctrl_msg_h->ex_msg);
78 ret = port_ctl_send_msg_to_md(port, CTL_ID_MD_EX, MD_EX_CHK_ID);
80 dev_err(dev, "Failed to send exception message to modem\n");
84 ret = t7xx_fsm_append_event(ctl, FSM_EVENT_MD_EX, NULL, 0);
86 dev_err(dev, "Failed to append Modem Exception event");
90 case CTL_ID_MD_EX_ACK:
91 if (le32_to_cpu(ctrl_msg_h->ex_msg) != MD_EX_CHK_ACK_ID) {
92 dev_err(dev, "Receive invalid MD_EX_ACK %x\n", ctrl_msg_h->ex_msg);
96 ret = t7xx_fsm_append_event(ctl, FSM_EVENT_MD_EX_REC_OK, NULL, 0);
98 dev_err(dev, "Failed to append Modem Exception Received event");
102 case CTL_ID_MD_EX_PASS:
103 ret = t7xx_fsm_append_event(ctl, FSM_EVENT_MD_EX_PASS, NULL, 0);
105 dev_err(dev, "Failed to append Modem Exception Passed event");
109 case CTL_ID_DRV_VER_ERROR:
110 dev_err(dev, "AP/MD driver version mismatch\n");
117 * t7xx_port_enum_msg_handler() - Parse the port enumeration message to create/remove nodes.
118 * @md: Modem context.
121 * Used to control create/remove device node.
125 * * -EFAULT - Message check failure.
127 int t7xx_port_enum_msg_handler(struct t7xx_modem *md, void *msg)
129 struct device *dev = &md->t7xx_dev->pdev->dev;
130 unsigned int version, port_count, i;
131 struct port_msg *port_msg = msg;
133 version = FIELD_GET(PORT_MSG_VERSION, le32_to_cpu(port_msg->info));
134 if (version != PORT_ENUM_VER ||
135 le32_to_cpu(port_msg->head_pattern) != PORT_ENUM_HEAD_PATTERN ||
136 le32_to_cpu(port_msg->tail_pattern) != PORT_ENUM_TAIL_PATTERN) {
137 dev_err(dev, "Invalid port control message %x:%x:%x\n",
138 version, le32_to_cpu(port_msg->head_pattern),
139 le32_to_cpu(port_msg->tail_pattern));
143 port_count = FIELD_GET(PORT_MSG_PRT_CNT, le32_to_cpu(port_msg->info));
144 for (i = 0; i < port_count; i++) {
145 u32 port_info = le32_to_cpu(port_msg->data[i]);
149 ch_id = FIELD_GET(PORT_INFO_CH_ID, port_info);
150 en_flag = port_info & PORT_INFO_ENFLG;
151 if (t7xx_port_proxy_chl_enable_disable(md->port_prox, ch_id, en_flag))
152 dev_dbg(dev, "Port:%x not found\n", ch_id);
158 static int control_msg_handler(struct t7xx_port *port, struct sk_buff *skb)
160 const struct t7xx_port_conf *port_conf = port->port_conf;
161 struct t7xx_fsm_ctl *ctl = port->t7xx_dev->md->fsm_ctl;
162 struct ctrl_msg_header *ctrl_msg_h;
165 ctrl_msg_h = (struct ctrl_msg_header *)skb->data;
166 switch (le32_to_cpu(ctrl_msg_h->ctrl_msg_id)) {
168 skb_pull(skb, sizeof(*ctrl_msg_h));
170 if (port_conf->rx_ch == PORT_CH_CONTROL_RX ||
171 port_conf->rx_ch == PORT_CH_AP_CONTROL_RX) {
172 int event = port_conf->rx_ch == PORT_CH_CONTROL_RX ?
173 FSM_EVENT_MD_HS2 : FSM_EVENT_AP_HS2;
175 ret = t7xx_fsm_append_event(ctl, event, skb->data,
176 le32_to_cpu(ctrl_msg_h->data_length));
178 dev_err(port->dev, "Failed to append Handshake 2 event");
181 dev_kfree_skb_any(skb);
185 case CTL_ID_MD_EX_ACK:
186 case CTL_ID_MD_EX_PASS:
187 case CTL_ID_DRV_VER_ERROR:
188 ret = fsm_ee_message_handler(port, ctl, skb);
189 dev_kfree_skb_any(skb);
192 case CTL_ID_PORT_ENUM:
193 skb_pull(skb, sizeof(*ctrl_msg_h));
194 ret = t7xx_port_enum_msg_handler(ctl->md, (struct port_msg *)skb->data);
196 ret = port_ctl_send_msg_to_md(port, CTL_ID_PORT_ENUM, 0);
198 ret = port_ctl_send_msg_to_md(port, CTL_ID_PORT_ENUM,
199 PORT_ENUM_VER_MISMATCH);
205 dev_err(port->dev, "Unknown control message ID to FSM %x\n",
206 le32_to_cpu(ctrl_msg_h->ctrl_msg_id));
211 dev_err(port->dev, "%s control message handle error: %d\n", port_conf->name, ret);
216 static int port_ctl_rx_thread(void *arg)
218 while (!kthread_should_stop()) {
219 struct t7xx_port *port = arg;
223 spin_lock_irqsave(&port->rx_wq.lock, flags);
224 if (skb_queue_empty(&port->rx_skb_list) &&
225 wait_event_interruptible_locked_irq(port->rx_wq,
226 !skb_queue_empty(&port->rx_skb_list) ||
227 kthread_should_stop())) {
228 spin_unlock_irqrestore(&port->rx_wq.lock, flags);
231 if (kthread_should_stop()) {
232 spin_unlock_irqrestore(&port->rx_wq.lock, flags);
235 skb = __skb_dequeue(&port->rx_skb_list);
236 spin_unlock_irqrestore(&port->rx_wq.lock, flags);
238 control_msg_handler(port, skb);
244 static int port_ctl_init(struct t7xx_port *port)
246 const struct t7xx_port_conf *port_conf = port->port_conf;
248 port->thread = kthread_run(port_ctl_rx_thread, port, "%s", port_conf->name);
249 if (IS_ERR(port->thread)) {
250 dev_err(port->dev, "Failed to start port control thread\n");
251 return PTR_ERR(port->thread);
254 port->rx_length_th = CTRL_QUEUE_MAXLEN;
258 static void port_ctl_uninit(struct t7xx_port *port)
264 kthread_stop(port->thread);
266 spin_lock_irqsave(&port->rx_wq.lock, flags);
267 port->rx_length_th = 0;
268 while ((skb = __skb_dequeue(&port->rx_skb_list)) != NULL)
269 dev_kfree_skb_any(skb);
270 spin_unlock_irqrestore(&port->rx_wq.lock, flags);
273 struct port_ops ctl_port_ops = {
274 .init = port_ctl_init,
275 .recv_skb = t7xx_port_enqueue_skb,
276 .uninit = port_ctl_uninit,