| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Copyright (c) 2021, MediaTek Inc. |
| 4 |
* Copyright (c) 2021-2022, Intel Corporation. |
| 5 |
* |
| 6 |
* Authors: |
| 7 |
* Haijun Liu <[email protected]> |
| 8 |
* Eliot Lee <[email protected]> |
| 9 |
* Moises Veleta <[email protected]> |
| 10 |
* Ricardo Martinez <[email protected]> |
| 11 |
* |
| 12 |
* Contributors: |
| 13 |
* Amir Hanania <[email protected]> |
| 14 |
* Sreehari Kancharla <[email protected]> |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <linux/bits.h> |
| 18 |
#include <linux/bitfield.h> |
| 19 |
#include <linux/completion.h> |
| 20 |
#include <linux/device.h> |
| 21 |
#include <linux/delay.h> |
| 22 |
#include <linux/err.h> |
| 23 |
#include <linux/gfp.h> |
| 24 |
#include <linux/iopoll.h> |
| 25 |
#include <linux/jiffies.h> |
| 26 |
#include <linux/kernel.h> |
| 27 |
#include <linux/kthread.h> |
| 28 |
#include <linux/list.h> |
| 29 |
#include <linux/slab.h> |
| 30 |
#include <linux/spinlock.h> |
| 31 |
#include <linux/string.h> |
| 32 |
#include <linux/types.h> |
| 33 |
#include <linux/wait.h> |
| 34 |
|
| 35 |
#include "t7xx_hif_cldma.h" |
| 36 |
#include "t7xx_mhccif.h" |
| 37 |
#include "t7xx_modem_ops.h" |
| 38 |
#include "t7xx_pci.h" |
| 39 |
#include "t7xx_pcie_mac.h" |
| 40 |
#include "t7xx_port_proxy.h" |
| 41 |
#include "t7xx_reg.h" |
| 42 |
#include "t7xx_state_monitor.h" |
| 43 |
|
| 44 |
#define FSM_DRM_DISABLE_DELAY_MS 200 |
| 45 |
#define FSM_EVENT_POLL_INTERVAL_MS 20 |
| 46 |
#define FSM_MD_EX_REC_OK_TIMEOUT_MS 10000 |
| 47 |
#define FSM_MD_EX_PASS_TIMEOUT_MS 45000 |
| 48 |
#define FSM_CMD_TIMEOUT_MS 2000 |
| 49 |
|
| 50 |
#define wait_for_expected_dev_stage(status) \ |
| 51 |
read_poll_timeout(ioread32, status, \ |
| 52 |
((status & MISC_STAGE_MASK) == T7XX_DEV_STAGE_LINUX) || \ |
| 53 |
((status & MISC_STAGE_MASK) == T7XX_DEV_STAGE_LK), 100000, \ |
| 54 |
20000000, false, IREG_BASE(md->t7xx_dev) + \ |
| 55 |
T7XX_PCIE_MISC_DEV_STATUS) |
| 56 |
|
| 57 |
void t7xx_fsm_notifier_register(struct t7xx_modem *md, struct t7xx_fsm_notifier *notifier) |
| 58 |
{ |
| 59 |
struct t7xx_fsm_ctl *ctl = md->fsm_ctl; |
| 60 |
unsigned long flags; |
| 61 |
|
| 62 |
spin_lock_irqsave(&ctl->notifier_lock, flags); |
| 63 |
list_add_tail(¬ifier->entry, &ctl->notifier_list); |
| 64 |
spin_unlock_irqrestore(&ctl->notifier_lock, flags); |
| 65 |
} |
| 66 |
|
| 67 |
void t7xx_fsm_notifier_unregister(struct t7xx_modem *md, struct t7xx_fsm_notifier *notifier) |
| 68 |
{ |
| 69 |
struct t7xx_fsm_notifier *notifier_cur, *notifier_next; |
| 70 |
struct t7xx_fsm_ctl *ctl = md->fsm_ctl; |
| 71 |
unsigned long flags; |
| 72 |
|
| 73 |
spin_lock_irqsave(&ctl->notifier_lock, flags); |
| 74 |
list_for_each_entry_safe(notifier_cur, notifier_next, &ctl->notifier_list, entry) { |
| 75 |
if (notifier_cur == notifier) |
| 76 |
list_del(¬ifier->entry); |
| 77 |
} |
| 78 |
spin_unlock_irqrestore(&ctl->notifier_lock, flags); |
| 79 |
} |
| 80 |
|
| 81 |
static void fsm_state_notify(struct t7xx_modem *md, enum md_state state) |
| 82 |
{ |
| 83 |
struct t7xx_fsm_ctl *ctl = md->fsm_ctl; |
| 84 |
struct t7xx_fsm_notifier *notifier; |
| 85 |
unsigned long flags; |
| 86 |
|
| 87 |
spin_lock_irqsave(&ctl->notifier_lock, flags); |
| 88 |
list_for_each_entry(notifier, &ctl->notifier_list, entry) { |
| 89 |
spin_unlock_irqrestore(&ctl->notifier_lock, flags); |
| 90 |
if (notifier->notifier_fn) |
| 91 |
notifier->notifier_fn(state, notifier->data); |
| 92 |
|
| 93 |
spin_lock_irqsave(&ctl->notifier_lock, flags); |
| 94 |
} |
| 95 |
spin_unlock_irqrestore(&ctl->notifier_lock, flags); |
| 96 |
} |
| 97 |
|
| 98 |
void t7xx_fsm_broadcast_state(struct t7xx_fsm_ctl *ctl, enum md_state state) |
| 99 |
{ |
| 100 |
ctl->md_state = state; |
| 101 |
|
| 102 |
/* Update to port first, otherwise sending message on HS2 may fail */ |
| 103 |
t7xx_port_proxy_md_status_notify(ctl->md->port_prox, state); |
| 104 |
fsm_state_notify(ctl->md, state); |
| 105 |
} |
| 106 |
|
| 107 |
static void fsm_release_command(struct kref *ref) |
| 108 |
{ |
| 109 |
struct t7xx_fsm_command *cmd = container_of(ref, typeof(*cmd), refcnt); |
| 110 |
|
| 111 |
kfree(cmd); |
| 112 |
} |
| 113 |
|
| 114 |
static void fsm_finish_command(struct t7xx_fsm_ctl *ctl, struct t7xx_fsm_command *cmd, int result) |
| 115 |
{ |
| 116 |
if (cmd->flag & FSM_CMD_FLAG_WAIT_FOR_COMPLETION) { |
| 117 |
cmd->result = result; |
| 118 |
complete_all(&cmd->done); |
| 119 |
} |
| 120 |
|
| 121 |
kref_put(&cmd->refcnt, fsm_release_command); |
| 122 |
} |
| 123 |
|
| 124 |
static void fsm_del_kf_event(struct t7xx_fsm_event *event) |
| 125 |
{ |
| 126 |
list_del(&event->entry); |
| 127 |
kfree(event); |
| 128 |
} |
| 129 |
|
| 130 |
static void fsm_flush_event_cmd_qs(struct t7xx_fsm_ctl *ctl) |
| 131 |
{ |
| 132 |
struct device *dev = &ctl->md->t7xx_dev->pdev->dev; |
| 133 |
struct t7xx_fsm_event *event, *evt_next; |
| 134 |
struct t7xx_fsm_command *cmd, *cmd_next; |
| 135 |
unsigned long flags; |
| 136 |
|
| 137 |
spin_lock_irqsave(&ctl->command_lock, flags); |
| 138 |
list_for_each_entry_safe(cmd, cmd_next, &ctl->command_queue, entry) { |
| 139 |
dev_warn(dev, "Unhandled command %d\n", cmd->cmd_id); |
| 140 |
list_del(&cmd->entry); |
| 141 |
fsm_finish_command(ctl, cmd, -EINVAL); |
| 142 |
} |
| 143 |
spin_unlock_irqrestore(&ctl->command_lock, flags); |
| 144 |
|
| 145 |
spin_lock_irqsave(&ctl->event_lock, flags); |
| 146 |
list_for_each_entry_safe(event, evt_next, &ctl->event_queue, entry) { |
| 147 |
dev_warn(dev, "Unhandled event %d\n", event->event_id); |
| 148 |
fsm_del_kf_event(event); |
| 149 |
} |
| 150 |
spin_unlock_irqrestore(&ctl->event_lock, flags); |
| 151 |
} |
| 152 |
|
| 153 |
static void fsm_wait_for_event(struct t7xx_fsm_ctl *ctl, enum t7xx_fsm_event_state event_expected, |
| 154 |
enum t7xx_fsm_event_state event_ignore, int retries) |
| 155 |
{ |
| 156 |
struct t7xx_fsm_event *event; |
| 157 |
bool event_received = false; |
| 158 |
unsigned long flags; |
| 159 |
int cnt = 0; |
| 160 |
|
| 161 |
while (cnt++ < retries && !event_received) { |
| 162 |
bool sleep_required = true; |
| 163 |
|
| 164 |
if (kthread_should_stop()) |
| 165 |
return; |
| 166 |
|
| 167 |
spin_lock_irqsave(&ctl->event_lock, flags); |
| 168 |
event = list_first_entry_or_null(&ctl->event_queue, struct t7xx_fsm_event, entry); |
| 169 |
if (event) { |
| 170 |
event_received = event->event_id == event_expected; |
| 171 |
if (event_received || event->event_id == event_ignore) { |
| 172 |
fsm_del_kf_event(event); |
| 173 |
sleep_required = false; |
| 174 |
} |
| 175 |
} |
| 176 |
spin_unlock_irqrestore(&ctl->event_lock, flags); |
| 177 |
|
| 178 |
if (sleep_required) |
| 179 |
msleep(FSM_EVENT_POLL_INTERVAL_MS); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
static void fsm_routine_exception(struct t7xx_fsm_ctl *ctl, struct t7xx_fsm_command *cmd, |
| 184 |
enum t7xx_ex_reason reason) |
| 185 |
{ |
| 186 |
struct device *dev = &ctl->md->t7xx_dev->pdev->dev; |
| 187 |
|
| 188 |
if (ctl->curr_state != FSM_STATE_READY && ctl->curr_state != FSM_STATE_STARTING) { |
| 189 |
if (cmd) |
| 190 |
fsm_finish_command(ctl, cmd, -EINVAL); |
| 191 |
|
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
ctl->curr_state = FSM_STATE_EXCEPTION; |
| 196 |
|
| 197 |
switch (reason) { |
| 198 |
case EXCEPTION_HS_TIMEOUT: |
| 199 |
dev_err(dev, "Boot Handshake failure\n"); |
| 200 |
break; |
| 201 |
|
| 202 |
case EXCEPTION_EVENT: |
| 203 |
dev_err(dev, "Exception event\n"); |
| 204 |
t7xx_fsm_broadcast_state(ctl, MD_STATE_EXCEPTION); |
| 205 |
t7xx_pci_pm_exp_detected(ctl->md->t7xx_dev); |
| 206 |
t7xx_md_exception_handshake(ctl->md); |
| 207 |
|
| 208 |
fsm_wait_for_event(ctl, FSM_EVENT_MD_EX_REC_OK, FSM_EVENT_MD_EX, |
| 209 |
FSM_MD_EX_REC_OK_TIMEOUT_MS / FSM_EVENT_POLL_INTERVAL_MS); |
| 210 |
fsm_wait_for_event(ctl, FSM_EVENT_MD_EX_PASS, FSM_EVENT_INVALID, |
| 211 |
FSM_MD_EX_PASS_TIMEOUT_MS / FSM_EVENT_POLL_INTERVAL_MS); |
| 212 |
break; |
| 213 |
|
| 214 |
default: |
| 215 |
dev_err(dev, "Exception %d\n", reason); |
| 216 |
break; |
| 217 |
} |
| 218 |
|
| 219 |
if (cmd) |
| 220 |
fsm_finish_command(ctl, cmd, 0); |
| 221 |
} |
| 222 |
|
| 223 |
static void t7xx_lk_stage_event_handling(struct t7xx_fsm_ctl *ctl, unsigned int status) |
| 224 |
{ |
| 225 |
struct t7xx_modem *md = ctl->md; |
| 226 |
struct cldma_ctrl *md_ctrl; |
| 227 |
enum lk_event_id lk_event; |
| 228 |
struct device *dev; |
| 229 |
struct t7xx_port *port; |
| 230 |
|
| 231 |
dev = &md->t7xx_dev->pdev->dev; |
| 232 |
lk_event = FIELD_GET(MISC_LK_EVENT_MASK, status); |
| 233 |
switch (lk_event) { |
| 234 |
case LK_EVENT_NORMAL: |
| 235 |
case LK_EVENT_RESET: |
| 236 |
break; |
| 237 |
|
| 238 |
case LK_EVENT_CREATE_PD_PORT: |
| 239 |
case LK_EVENT_CREATE_POST_DL_PORT: |
| 240 |
md_ctrl = md->md_ctrl[CLDMA_ID_AP]; |
| 241 |
t7xx_cldma_hif_hw_init(md_ctrl); |
| 242 |
t7xx_cldma_stop(md_ctrl); |
| 243 |
t7xx_cldma_switch_cfg(md_ctrl, CLDMA_DEDICATED_Q_CFG); |
| 244 |
|
| 245 |
port = &ctl->md->port_prox->ports[0]; |
| 246 |
port->port_conf->ops->enable_chl(port); |
| 247 |
|
| 248 |
t7xx_cldma_start(md_ctrl); |
| 249 |
|
| 250 |
if (lk_event == LK_EVENT_CREATE_POST_DL_PORT) |
| 251 |
t7xx_mode_update(md->t7xx_dev, T7XX_FASTBOOT_DOWNLOAD); |
| 252 |
else |
| 253 |
t7xx_mode_update(md->t7xx_dev, T7XX_FASTBOOT_DUMP); |
| 254 |
break; |
| 255 |
|
| 256 |
default: |
| 257 |
dev_err(dev, "Invalid LK event %d\n", lk_event); |
| 258 |
break; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
static int fsm_stopped_handler(struct t7xx_fsm_ctl *ctl) |
| 263 |
{ |
| 264 |
enum t7xx_mode mode; |
| 265 |
|
| 266 |
ctl->curr_state = FSM_STATE_STOPPED; |
| 267 |
|
| 268 |
mode = READ_ONCE(ctl->md->t7xx_dev->mode); |
| 269 |
if (mode == T7XX_FASTBOOT_DOWNLOAD || mode == T7XX_FASTBOOT_DUMP) |
| 270 |
return 0; |
| 271 |
|
| 272 |
t7xx_fsm_broadcast_state(ctl, MD_STATE_STOPPED); |
| 273 |
return t7xx_md_reset(ctl->md->t7xx_dev); |
| 274 |
} |
| 275 |
|
| 276 |
static void fsm_routine_stopped(struct t7xx_fsm_ctl *ctl, struct t7xx_fsm_command *cmd) |
| 277 |
{ |
| 278 |
if (ctl->curr_state == FSM_STATE_STOPPED) { |
| 279 |
fsm_finish_command(ctl, cmd, -EINVAL); |
| 280 |
return; |
| 281 |
} |
| 282 |
|
| 283 |
fsm_finish_command(ctl, cmd, fsm_stopped_handler(ctl)); |
| 284 |
} |
| 285 |
|
| 286 |
static void fsm_routine_stopping(struct t7xx_fsm_ctl *ctl, struct t7xx_fsm_command *cmd) |
| 287 |
{ |
| 288 |
struct cldma_ctrl *md_ctrl = ctl->md->md_ctrl[CLDMA_ID_MD]; |
| 289 |
struct t7xx_pci_dev *t7xx_dev = ctl->md->t7xx_dev; |
| 290 |
|
| 291 |
if (ctl->curr_state == FSM_STATE_STOPPED || ctl->curr_state == FSM_STATE_STOPPING) { |
| 292 |
fsm_finish_command(ctl, cmd, -EINVAL); |
| 293 |
return; |
| 294 |
} |
| 295 |
|
| 296 |
ctl->curr_state = FSM_STATE_STOPPING; |
| 297 |
t7xx_fsm_broadcast_state(ctl, MD_STATE_WAITING_TO_STOP); |
| 298 |
t7xx_cldma_stop(md_ctrl); |
| 299 |
|
| 300 |
t7xx_mhccif_h2d_swint_trigger(t7xx_dev, H2D_CH_DRM_DISABLE_AP); |
| 301 |
/* Wait for the DRM disable to take effect */ |
| 302 |
msleep(FSM_DRM_DISABLE_DELAY_MS); |
| 303 |
|
| 304 |
fsm_finish_command(ctl, cmd, fsm_stopped_handler(ctl)); |
| 305 |
} |
| 306 |
|
| 307 |
static void t7xx_fsm_broadcast_ready_state(struct t7xx_fsm_ctl *ctl) |
| 308 |
{ |
| 309 |
if (ctl->md_state != MD_STATE_WAITING_FOR_HS2) |
| 310 |
return; |
| 311 |
|
| 312 |
ctl->md_state = MD_STATE_READY; |
| 313 |
|
| 314 |
fsm_state_notify(ctl->md, MD_STATE_READY); |
| 315 |
t7xx_port_proxy_md_status_notify(ctl->md->port_prox, MD_STATE_READY); |
| 316 |
} |
| 317 |
|
| 318 |
static void fsm_routine_ready(struct t7xx_fsm_ctl *ctl) |
| 319 |
{ |
| 320 |
struct t7xx_modem *md = ctl->md; |
| 321 |
|
| 322 |
ctl->curr_state = FSM_STATE_READY; |
| 323 |
t7xx_fsm_broadcast_ready_state(ctl); |
| 324 |
t7xx_mode_update(md->t7xx_dev, T7XX_READY); |
| 325 |
t7xx_md_event_notify(md, FSM_READY); |
| 326 |
} |
| 327 |
|
| 328 |
static int fsm_routine_starting(struct t7xx_fsm_ctl *ctl) |
| 329 |
{ |
| 330 |
struct t7xx_modem *md = ctl->md; |
| 331 |
struct device *dev; |
| 332 |
|
| 333 |
ctl->curr_state = FSM_STATE_STARTING; |
| 334 |
|
| 335 |
t7xx_fsm_broadcast_state(ctl, MD_STATE_WAITING_FOR_HS1); |
| 336 |
t7xx_md_event_notify(md, FSM_START); |
| 337 |
|
| 338 |
wait_event_interruptible_timeout(ctl->async_hk_wq, |
| 339 |
(md->core_md.ready && md->core_ap.ready) || |
| 340 |
ctl->exp_flg, HZ * 60); |
| 341 |
dev = &md->t7xx_dev->pdev->dev; |
| 342 |
|
| 343 |
if (ctl->exp_flg) |
| 344 |
dev_err(dev, "MD exception is captured during handshake\n"); |
| 345 |
|
| 346 |
if (!md->core_md.ready) { |
| 347 |
dev_err(dev, "MD handshake timeout\n"); |
| 348 |
if (md->core_md.handshake_ongoing) |
| 349 |
t7xx_fsm_append_event(ctl, FSM_EVENT_MD_HS2_EXIT, NULL, 0); |
| 350 |
|
| 351 |
fsm_routine_exception(ctl, NULL, EXCEPTION_HS_TIMEOUT); |
| 352 |
return -ETIMEDOUT; |
| 353 |
} else if (!md->core_ap.ready) { |
| 354 |
dev_err(dev, "AP handshake timeout\n"); |
| 355 |
if (md->core_ap.handshake_ongoing) |
| 356 |
t7xx_fsm_append_event(ctl, FSM_EVENT_AP_HS2_EXIT, NULL, 0); |
| 357 |
|
| 358 |
fsm_routine_exception(ctl, NULL, EXCEPTION_HS_TIMEOUT); |
| 359 |
return -ETIMEDOUT; |
| 360 |
} |
| 361 |
|
| 362 |
t7xx_pci_pm_init_late(md->t7xx_dev); |
| 363 |
fsm_routine_ready(ctl); |
| 364 |
return 0; |
| 365 |
} |
| 366 |
|
| 367 |
static void fsm_routine_start(struct t7xx_fsm_ctl *ctl, struct t7xx_fsm_command *cmd) |
| 368 |
{ |
| 369 |
struct t7xx_modem *md = ctl->md; |
| 370 |
struct device *dev; |
| 371 |
u32 status; |
| 372 |
int ret; |
| 373 |
|
| 374 |
if (!md) |
| 375 |
return; |
| 376 |
|
| 377 |
if (ctl->curr_state != FSM_STATE_INIT && ctl->curr_state != FSM_STATE_PRE_START && |
| 378 |
ctl->curr_state != FSM_STATE_STOPPED) { |
| 379 |
fsm_finish_command(ctl, cmd, -EINVAL); |
| 380 |
return; |
| 381 |
} |
| 382 |
|
| 383 |
dev = &md->t7xx_dev->pdev->dev; |
| 384 |
ctl->curr_state = FSM_STATE_PRE_START; |
| 385 |
t7xx_md_event_notify(md, FSM_PRE_START); |
| 386 |
|
| 387 |
ret = wait_for_expected_dev_stage(status); |
| 388 |
|
| 389 |
if (ret) { |
| 390 |
dev_err(dev, "read poll timeout %d\n", ret); |
| 391 |
goto finish_command; |
| 392 |
} |
| 393 |
|
| 394 |
if (status != ctl->status || cmd->flag != 0) { |
| 395 |
u32 stage = FIELD_GET(MISC_STAGE_MASK, status); |
| 396 |
|
| 397 |
switch (stage) { |
| 398 |
case T7XX_DEV_STAGE_INIT: |
| 399 |
case T7XX_DEV_STAGE_BROM_PRE: |
| 400 |
case T7XX_DEV_STAGE_BROM_POST: |
| 401 |
dev_dbg(dev, "BROM_STAGE Entered\n"); |
| 402 |
ret = t7xx_fsm_append_cmd(ctl, FSM_CMD_START, 0); |
| 403 |
break; |
| 404 |
|
| 405 |
case T7XX_DEV_STAGE_LK: |
| 406 |
dev_dbg(dev, "LK_STAGE Entered\n"); |
| 407 |
t7xx_port_proxy_set_cfg(md, PORT_CFG_ID_EARLY); |
| 408 |
t7xx_lk_stage_event_handling(ctl, status); |
| 409 |
|
| 410 |
break; |
| 411 |
|
| 412 |
case T7XX_DEV_STAGE_LINUX: |
| 413 |
dev_dbg(dev, "LINUX_STAGE Entered\n"); |
| 414 |
t7xx_mhccif_mask_clr(md->t7xx_dev, D2H_INT_PORT_ENUM | |
| 415 |
D2H_INT_ASYNC_MD_HK | D2H_INT_ASYNC_AP_HK); |
| 416 |
if (cmd->flag == 0) |
| 417 |
break; |
| 418 |
t7xx_cldma_hif_hw_init(md->md_ctrl[CLDMA_ID_AP]); |
| 419 |
t7xx_cldma_hif_hw_init(md->md_ctrl[CLDMA_ID_MD]); |
| 420 |
t7xx_port_proxy_set_cfg(md, PORT_CFG_ID_NORMAL); |
| 421 |
ret = fsm_routine_starting(ctl); |
| 422 |
break; |
| 423 |
|
| 424 |
default: |
| 425 |
break; |
| 426 |
} |
| 427 |
ctl->status = status; |
| 428 |
} |
| 429 |
|
| 430 |
finish_command: |
| 431 |
if (ret) |
| 432 |
t7xx_mode_update(md->t7xx_dev, T7XX_UNKNOWN); |
| 433 |
|
| 434 |
fsm_finish_command(ctl, cmd, ret); |
| 435 |
} |
| 436 |
|
| 437 |
static int fsm_main_thread(void *data) |
| 438 |
{ |
| 439 |
struct t7xx_fsm_ctl *ctl = data; |
| 440 |
struct t7xx_fsm_command *cmd; |
| 441 |
unsigned long flags; |
| 442 |
|
| 443 |
while (!kthread_should_stop()) { |
| 444 |
if (wait_event_interruptible(ctl->command_wq, !list_empty(&ctl->command_queue) || |
| 445 |
kthread_should_stop())) |
| 446 |
continue; |
| 447 |
|
| 448 |
if (kthread_should_stop()) |
| 449 |
break; |
| 450 |
|
| 451 |
spin_lock_irqsave(&ctl->command_lock, flags); |
| 452 |
cmd = list_first_entry(&ctl->command_queue, struct t7xx_fsm_command, entry); |
| 453 |
list_del(&cmd->entry); |
| 454 |
spin_unlock_irqrestore(&ctl->command_lock, flags); |
| 455 |
|
| 456 |
switch (cmd->cmd_id) { |
| 457 |
case FSM_CMD_START: |
| 458 |
fsm_routine_start(ctl, cmd); |
| 459 |
break; |
| 460 |
|
| 461 |
case FSM_CMD_EXCEPTION: |
| 462 |
fsm_routine_exception(ctl, cmd, FIELD_GET(FSM_CMD_EX_REASON, cmd->flag)); |
| 463 |
break; |
| 464 |
|
| 465 |
case FSM_CMD_PRE_STOP: |
| 466 |
fsm_routine_stopping(ctl, cmd); |
| 467 |
break; |
| 468 |
|
| 469 |
case FSM_CMD_STOP: |
| 470 |
fsm_routine_stopped(ctl, cmd); |
| 471 |
break; |
| 472 |
|
| 473 |
default: |
| 474 |
fsm_finish_command(ctl, cmd, -EINVAL); |
| 475 |
fsm_flush_event_cmd_qs(ctl); |
| 476 |
break; |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
return 0; |
| 481 |
} |
| 482 |
|
| 483 |
int t7xx_fsm_append_cmd(struct t7xx_fsm_ctl *ctl, enum t7xx_fsm_cmd_state cmd_id, unsigned int flag) |
| 484 |
{ |
| 485 |
struct t7xx_fsm_command *cmd; |
| 486 |
unsigned long flags; |
| 487 |
int ret; |
| 488 |
|
| 489 |
cmd = kzalloc_obj(*cmd, |
| 490 |
flag & FSM_CMD_FLAG_IN_INTERRUPT ? GFP_ATOMIC : GFP_KERNEL); |
| 491 |
if (!cmd) |
| 492 |
return -ENOMEM; |
| 493 |
|
| 494 |
INIT_LIST_HEAD(&cmd->entry); |
| 495 |
cmd->cmd_id = cmd_id; |
| 496 |
cmd->flag = flag; |
| 497 |
kref_init(&cmd->refcnt); |
| 498 |
if (flag & FSM_CMD_FLAG_WAIT_FOR_COMPLETION) { |
| 499 |
init_completion(&cmd->done); |
| 500 |
kref_get(&cmd->refcnt); |
| 501 |
} |
| 502 |
|
| 503 |
kref_get(&cmd->refcnt); |
| 504 |
spin_lock_irqsave(&ctl->command_lock, flags); |
| 505 |
list_add_tail(&cmd->entry, &ctl->command_queue); |
| 506 |
spin_unlock_irqrestore(&ctl->command_lock, flags); |
| 507 |
|
| 508 |
wake_up(&ctl->command_wq); |
| 509 |
|
| 510 |
if (flag & FSM_CMD_FLAG_WAIT_FOR_COMPLETION) { |
| 511 |
unsigned long wait_ret; |
| 512 |
|
| 513 |
wait_ret = wait_for_completion_timeout(&cmd->done, |
| 514 |
msecs_to_jiffies(FSM_CMD_TIMEOUT_MS)); |
| 515 |
|
| 516 |
ret = wait_ret ? cmd->result : -ETIMEDOUT; |
| 517 |
kref_put(&cmd->refcnt, fsm_release_command); |
| 518 |
return ret; |
| 519 |
} |
| 520 |
|
| 521 |
return 0; |
| 522 |
} |
| 523 |
|
| 524 |
int t7xx_fsm_append_event(struct t7xx_fsm_ctl *ctl, enum t7xx_fsm_event_state event_id, |
| 525 |
unsigned char *data, unsigned int length) |
| 526 |
{ |
| 527 |
struct device *dev = &ctl->md->t7xx_dev->pdev->dev; |
| 528 |
struct t7xx_fsm_event *event; |
| 529 |
unsigned long flags; |
| 530 |
|
| 531 |
if (event_id <= FSM_EVENT_INVALID || event_id >= FSM_EVENT_MAX) { |
| 532 |
dev_err(dev, "Invalid event %d\n", event_id); |
| 533 |
return -EINVAL; |
| 534 |
} |
| 535 |
|
| 536 |
event = kmalloc_flex(*event, data, length, |
| 537 |
in_interrupt() ? GFP_ATOMIC : GFP_KERNEL); |
| 538 |
if (!event) |
| 539 |
return -ENOMEM; |
| 540 |
|
| 541 |
INIT_LIST_HEAD(&event->entry); |
| 542 |
event->event_id = event_id; |
| 543 |
event->length = length; |
| 544 |
|
| 545 |
if (data && length) |
| 546 |
memcpy(event->data, data, length); |
| 547 |
|
| 548 |
spin_lock_irqsave(&ctl->event_lock, flags); |
| 549 |
list_add_tail(&event->entry, &ctl->event_queue); |
| 550 |
spin_unlock_irqrestore(&ctl->event_lock, flags); |
| 551 |
|
| 552 |
wake_up_all(&ctl->event_wq); |
| 553 |
return 0; |
| 554 |
} |
| 555 |
|
| 556 |
void t7xx_fsm_clr_event(struct t7xx_fsm_ctl *ctl, enum t7xx_fsm_event_state event_id) |
| 557 |
{ |
| 558 |
struct t7xx_fsm_event *event, *evt_next; |
| 559 |
unsigned long flags; |
| 560 |
|
| 561 |
spin_lock_irqsave(&ctl->event_lock, flags); |
| 562 |
list_for_each_entry_safe(event, evt_next, &ctl->event_queue, entry) { |
| 563 |
if (event->event_id == event_id) |
| 564 |
fsm_del_kf_event(event); |
| 565 |
} |
| 566 |
spin_unlock_irqrestore(&ctl->event_lock, flags); |
| 567 |
} |
| 568 |
|
| 569 |
enum md_state t7xx_fsm_get_md_state(struct t7xx_fsm_ctl *ctl) |
| 570 |
{ |
| 571 |
if (ctl) |
| 572 |
return ctl->md_state; |
| 573 |
|
| 574 |
return MD_STATE_INVALID; |
| 575 |
} |
| 576 |
|
| 577 |
unsigned int t7xx_fsm_get_ctl_state(struct t7xx_fsm_ctl *ctl) |
| 578 |
{ |
| 579 |
if (ctl) |
| 580 |
return ctl->curr_state; |
| 581 |
|
| 582 |
return FSM_STATE_STOPPED; |
| 583 |
} |
| 584 |
|
| 585 |
int t7xx_fsm_recv_md_intr(struct t7xx_fsm_ctl *ctl, enum t7xx_md_irq_type type) |
| 586 |
{ |
| 587 |
unsigned int cmd_flags = FSM_CMD_FLAG_IN_INTERRUPT; |
| 588 |
|
| 589 |
if (type == MD_IRQ_PORT_ENUM) { |
| 590 |
return t7xx_fsm_append_cmd(ctl, FSM_CMD_START, cmd_flags); |
| 591 |
} else if (type == MD_IRQ_CCIF_EX) { |
| 592 |
ctl->exp_flg = true; |
| 593 |
wake_up(&ctl->async_hk_wq); |
| 594 |
cmd_flags |= FIELD_PREP(FSM_CMD_EX_REASON, EXCEPTION_EVENT); |
| 595 |
return t7xx_fsm_append_cmd(ctl, FSM_CMD_EXCEPTION, cmd_flags); |
| 596 |
} |
| 597 |
|
| 598 |
return -EINVAL; |
| 599 |
} |
| 600 |
|
| 601 |
void t7xx_fsm_reset(struct t7xx_modem *md) |
| 602 |
{ |
| 603 |
struct t7xx_fsm_ctl *ctl = md->fsm_ctl; |
| 604 |
|
| 605 |
fsm_flush_event_cmd_qs(ctl); |
| 606 |
ctl->curr_state = FSM_STATE_STOPPED; |
| 607 |
ctl->exp_flg = false; |
| 608 |
ctl->status = T7XX_DEV_STAGE_INIT; |
| 609 |
} |
| 610 |
|
| 611 |
int t7xx_fsm_init(struct t7xx_modem *md) |
| 612 |
{ |
| 613 |
struct device *dev = &md->t7xx_dev->pdev->dev; |
| 614 |
struct t7xx_fsm_ctl *ctl; |
| 615 |
|
| 616 |
ctl = devm_kzalloc(dev, sizeof(*ctl), GFP_KERNEL); |
| 617 |
if (!ctl) |
| 618 |
return -ENOMEM; |
| 619 |
|
| 620 |
md->fsm_ctl = ctl; |
| 621 |
ctl->md = md; |
| 622 |
ctl->curr_state = FSM_STATE_INIT; |
| 623 |
INIT_LIST_HEAD(&ctl->command_queue); |
| 624 |
INIT_LIST_HEAD(&ctl->event_queue); |
| 625 |
init_waitqueue_head(&ctl->async_hk_wq); |
| 626 |
init_waitqueue_head(&ctl->event_wq); |
| 627 |
INIT_LIST_HEAD(&ctl->notifier_list); |
| 628 |
init_waitqueue_head(&ctl->command_wq); |
| 629 |
spin_lock_init(&ctl->event_lock); |
| 630 |
spin_lock_init(&ctl->command_lock); |
| 631 |
ctl->exp_flg = false; |
| 632 |
spin_lock_init(&ctl->notifier_lock); |
| 633 |
|
| 634 |
ctl->fsm_thread = kthread_run(fsm_main_thread, ctl, "t7xx_fsm"); |
| 635 |
return PTR_ERR_OR_ZERO(ctl->fsm_thread); |
| 636 |
} |
| 637 |
|
| 638 |
void t7xx_fsm_uninit(struct t7xx_modem *md) |
| 639 |
{ |
| 640 |
struct t7xx_fsm_ctl *ctl = md->fsm_ctl; |
| 641 |
|
| 642 |
if (!ctl) |
| 643 |
return; |
| 644 |
|
| 645 |
if (ctl->fsm_thread) |
| 646 |
kthread_stop(ctl->fsm_thread); |
| 647 |
|
| 648 |
fsm_flush_event_cmd_qs(ctl); |
| 649 |
} |