ccci_core.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /*
  2. * CCCI common service and routine. Consider it as a "logical" layer.
  3. *
  4. * V0.1: Xiao Wang <xiao.wang@mediatek.com>
  5. */
  6. #include <linux/list.h>
  7. #include <linux/device.h>
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/err.h>
  11. #include <linux/kdev_t.h>
  12. #include <linux/slab.h>
  13. #include <linux/kobject.h>
  14. #include <mt-plat/mt_ccci_common.h>
  15. #include "ccci_config.h"
  16. #include "ccci_platform.h"
  17. #include "ccci_core.h"
  18. #include "ccci_bm.h"
  19. #include "ccci_support.h"
  20. #include "port_cfg.h"
  21. static LIST_HEAD(modem_list); /* don't use array, due to MD index may not be continuous */
  22. static void *dev_class;
  23. void __iomem *md1_excp_smem_vir;
  24. unsigned int md1_excp_smem__size;
  25. /* used for throttling feature - start */
  26. unsigned long ccci_modem_boot_count[5];
  27. unsigned long ccci_get_md_boot_count(int md_id)
  28. {
  29. return ccci_modem_boot_count[md_id];
  30. }
  31. /* used for throttling feature - end */
  32. int boot_md_show(int md_id, char *buf, int size)
  33. {
  34. int curr = 0;
  35. struct ccci_modem *md;
  36. list_for_each_entry(md, &modem_list, entry) {
  37. if (md->index == md_id)
  38. curr += snprintf(&buf[curr], size, "md%d:%d/%d/%d", md->index + 1,
  39. md->md_state, md->boot_stage, md->ex_stage);
  40. }
  41. return curr;
  42. }
  43. int boot_md_store(int md_id)
  44. {
  45. struct ccci_modem *md;
  46. list_for_each_entry(md, &modem_list, entry) {
  47. CCCI_INF_MSG(md_id, CORE, "ccci core boot md%d, md_state=%d\n", md_id + 1, md->md_state);
  48. if (md->index == md_id && md->md_state == GATED) {
  49. md->ops->start(md);
  50. return 0;
  51. }
  52. }
  53. return -1;
  54. }
  55. char *ccci_get_ap_platform(void)
  56. {
  57. return AP_PLATFORM_INFO;
  58. }
  59. /*
  60. * if recv_request returns 0 or -CCCI_ERR_DROP_PACKET, then it's port's duty to free the request, and caller should
  61. * NOT reference the request any more. but if it returns other error, caller should be responsible to free the request.
  62. */
  63. int ccci_port_recv_request(struct ccci_modem *md, struct ccci_request *req, struct sk_buff *skb)
  64. {
  65. struct ccci_header *ccci_h;
  66. struct ccci_port *port = NULL;
  67. struct list_head *port_list = NULL;
  68. int ret = -CCCI_ERR_CHANNEL_NUM_MIS_MATCH;
  69. char matched = 0;
  70. if (likely(skb)) {
  71. ccci_h = (struct ccci_header *)skb->data;
  72. } else if (req) {
  73. ccci_h = (struct ccci_header *)req->skb->data;
  74. } else {
  75. ret = -CCCI_ERR_INVALID_PARAM;
  76. goto err_exit;
  77. }
  78. if (unlikely(ccci_h->channel >= CCCI_MAX_CH_NUM)) {
  79. ret = -CCCI_ERR_CHANNEL_NUM_MIS_MATCH;
  80. goto err_exit;
  81. }
  82. if (unlikely((md->md_state == GATED || md->md_state == INVALID) &&
  83. ccci_h->channel != CCCI_MONITOR_CH)) {
  84. ret = -CCCI_ERR_HIF_NOT_POWER_ON;
  85. goto err_exit;
  86. }
  87. port_list = &md->rx_ch_ports[ccci_h->channel];
  88. list_for_each_entry(port, port_list, entry) {
  89. /*
  90. * multi-cast is not supported, because one port may freed or modified this request
  91. * before another port can process it. but we still can use req->state to achive some
  92. * kind of multi-cast if needed.
  93. */
  94. matched =
  95. (port->ops->req_match == NULL || req == NULL) ?
  96. (ccci_h->channel == port->rx_ch) : port->ops->req_match(port, req);
  97. if (matched) {
  98. if (likely(skb && port->ops->recv_skb)) {
  99. ret = port->ops->recv_skb(port, skb);
  100. } else if (req && port->ops->recv_request) {
  101. ret = port->ops->recv_request(port, req);
  102. } else {
  103. CCCI_ERR_MSG(md->index, CORE, "port->ops->recv_request is null\n");
  104. ret = -CCCI_ERR_CHANNEL_NUM_MIS_MATCH;
  105. goto err_exit;
  106. }
  107. if (ret == -CCCI_ERR_PORT_RX_FULL)
  108. port->rx_busy_count++;
  109. break;
  110. }
  111. }
  112. err_exit:
  113. if (ret == -CCCI_ERR_CHANNEL_NUM_MIS_MATCH || ret == -CCCI_ERR_HIF_NOT_POWER_ON) {
  114. /* CCCI_ERR_MSG(md->index, CORE, "drop on channel %d\n", ccci_h->channel); */ /* Fix me, mask temp */
  115. if (req) {
  116. list_del(&req->entry);
  117. req->policy = RECYCLE;
  118. ccci_free_req(req);
  119. } else if (skb) {
  120. dev_kfree_skb_any(skb);
  121. }
  122. ret = -CCCI_ERR_DROP_PACKET;
  123. }
  124. return ret;
  125. }
  126. static void ccci_dump_log_rec(struct ccci_modem *md, struct ccci_log *log)
  127. {
  128. u64 ts_nsec = log->tv;
  129. unsigned long rem_nsec;
  130. if (ts_nsec == 0)
  131. return;
  132. rem_nsec = do_div(ts_nsec, 1000000000);
  133. if (!log->droped) {
  134. CCCI_INF_MSG(md->index, CORE, "%08X %08X %08X %08X %5lu.%06lu\n",
  135. log->msg.data[0], log->msg.data[1], *(((u32 *)&log->msg) + 2),
  136. log->msg.reserved, (unsigned long)ts_nsec, rem_nsec / 1000);
  137. } else {
  138. CCCI_INF_MSG(md->index, CORE, "%08X %08X %08X %08X %5lu.%06lu -\n",
  139. log->msg.data[0], log->msg.data[1], *(((u32 *)&log->msg) + 2),
  140. log->msg.reserved, (unsigned long)ts_nsec, rem_nsec / 1000);
  141. }
  142. }
  143. void ccci_dump_log_add(struct ccci_modem *md, DIRECTION dir, int queue_index, struct ccci_header *msg, int is_droped)
  144. {
  145. #ifdef PACKET_HISTORY_DEPTH
  146. if (dir == OUT) {
  147. memcpy(&md->tx_history[queue_index][md->tx_history_ptr[queue_index]].msg, msg,
  148. sizeof(struct ccci_header));
  149. md->tx_history[queue_index][md->tx_history_ptr[queue_index]].tv = local_clock();
  150. md->tx_history[queue_index][md->tx_history_ptr[queue_index]].droped = is_droped;
  151. md->tx_history_ptr[queue_index]++;
  152. md->tx_history_ptr[queue_index] &= (PACKET_HISTORY_DEPTH - 1);
  153. }
  154. if (dir == IN) {
  155. memcpy(&md->rx_history[queue_index][md->rx_history_ptr[queue_index]].msg, msg,
  156. sizeof(struct ccci_header));
  157. md->rx_history[queue_index][md->rx_history_ptr[queue_index]].tv = local_clock();
  158. md->rx_history[queue_index][md->rx_history_ptr[queue_index]].droped = is_droped;
  159. md->rx_history_ptr[queue_index]++;
  160. md->rx_history_ptr[queue_index] &= (PACKET_HISTORY_DEPTH - 1);
  161. }
  162. #endif
  163. }
  164. void ccci_dump_log_history(struct ccci_modem *md, int dump_multi_rec, int tx_queue_num, int rx_queue_num)
  165. {
  166. #ifdef PACKET_HISTORY_DEPTH
  167. int i, j;
  168. if (dump_multi_rec) {
  169. for (i = 0; i < ((tx_queue_num <= MAX_TXQ_NUM) ? tx_queue_num : MAX_TXQ_NUM); i++) {
  170. CCCI_INF_MSG(md->index, CORE, "dump txq%d packet history, ptr=%d\n", i,
  171. md->tx_history_ptr[i]);
  172. for (j = 0; j < PACKET_HISTORY_DEPTH; j++)
  173. ccci_dump_log_rec(md, &md->tx_history[i][j]);
  174. }
  175. for (i = 0; i < ((rx_queue_num <= MAX_RXQ_NUM) ? rx_queue_num : MAX_RXQ_NUM); i++) {
  176. CCCI_INF_MSG(md->index, CORE, "dump rxq%d packet history, ptr=%d\n", i,
  177. md->rx_history_ptr[i]);
  178. for (j = 0; j < PACKET_HISTORY_DEPTH; j++)
  179. ccci_dump_log_rec(md, &md->rx_history[i][j]);
  180. }
  181. } else {
  182. CCCI_INF_MSG(md->index, CORE, "dump txq%d packet history, ptr=%d\n", tx_queue_num,
  183. md->tx_history_ptr[tx_queue_num]);
  184. for (j = 0; j < PACKET_HISTORY_DEPTH; j++)
  185. ccci_dump_log_rec(md, &md->tx_history[tx_queue_num][j]);
  186. CCCI_INF_MSG(md->index, CORE, "dump rxq%d packet history, ptr=%d\n", rx_queue_num,
  187. md->rx_history_ptr[rx_queue_num]);
  188. for (j = 0; j < PACKET_HISTORY_DEPTH; j++)
  189. ccci_dump_log_rec(md, &md->rx_history[rx_queue_num][j]);
  190. }
  191. #endif
  192. }
  193. /*
  194. * most of this file is copied from mtk_ccci_helper.c, we use this function to
  195. * translate legacy data structure into current CCCI core.
  196. */
  197. void ccci_config_modem(struct ccci_modem *md)
  198. {
  199. phys_addr_t md_resv_mem_addr = 0, md_resv_smem_addr = 0;
  200. /* void __iomem *smem_base_vir; */
  201. unsigned int md_resv_mem_size = 0, md_resv_smem_size = 0;
  202. /* setup config */
  203. md->config.load_type = get_modem_support_cap(md->index);
  204. if (get_modem_is_enabled(md->index))
  205. md->config.setting |= MD_SETTING_ENABLE;
  206. else
  207. md->config.setting &= ~MD_SETTING_ENABLE;
  208. /* Get memory info */
  209. get_md_resv_mem_info(md->index, &md_resv_mem_addr, &md_resv_mem_size, &md_resv_smem_addr, &md_resv_smem_size);
  210. /* setup memory layout */
  211. /* MD image */
  212. md->mem_layout.md_region_phy = md_resv_mem_addr;
  213. md->mem_layout.md_region_size = md_resv_mem_size;
  214. md->mem_layout.md_region_vir = ioremap_nocache(md->mem_layout.md_region_phy, MD_IMG_DUMP_SIZE);
  215. /* do not remap whole region, consume too much vmalloc space */
  216. /* DSP image */
  217. md->mem_layout.dsp_region_phy = 0;
  218. md->mem_layout.dsp_region_size = 0;
  219. md->mem_layout.dsp_region_vir = 0;
  220. /* Share memory */
  221. md->mem_layout.smem_region_phy = md_resv_smem_addr;
  222. md->mem_layout.smem_region_size = md_resv_smem_size;
  223. md->mem_layout.smem_region_vir =
  224. ioremap_nocache(md->mem_layout.smem_region_phy, md->mem_layout.smem_region_size);
  225. memset_io(md->mem_layout.smem_region_vir, 0, md->mem_layout.smem_region_size);
  226. /* exception region */
  227. md->smem_layout.ccci_exp_smem_base_phy = md->mem_layout.smem_region_phy;
  228. md->smem_layout.ccci_exp_smem_base_vir = md->mem_layout.smem_region_vir;
  229. md->smem_layout.ccci_exp_smem_size = CCCI_SMEM_SIZE_EXCEPTION;
  230. md->smem_layout.ccci_exp_dump_size = CCCI_SMEM_DUMP_SIZE;
  231. if (md->index == MD_SYS1) {
  232. md1_excp_smem_vir = md->smem_layout.ccci_exp_smem_base_vir;
  233. md1_excp_smem__size = md->smem_layout.ccci_exp_dump_size;
  234. }
  235. /* dump region */
  236. md->smem_layout.ccci_exp_smem_ccci_debug_vir =
  237. md->smem_layout.ccci_exp_smem_base_vir + CCCI_SMEM_OFFSET_CCCI_DEBUG;
  238. md->smem_layout.ccci_exp_smem_ccci_debug_size = CCCI_SMEM_CCCI_DEBUG_SIZE;
  239. md->smem_layout.ccci_exp_smem_mdss_debug_vir =
  240. md->smem_layout.ccci_exp_smem_base_vir + CCCI_SMEM_OFFSET_MDSS_DEBUG;
  241. #ifdef MD_UMOLY_EE_SUPPORT
  242. if (md->index == MD_SYS1)
  243. md->smem_layout.ccci_exp_smem_mdss_debug_size = CCCI_SMEM_MDSS_DEBUG_SIZE_UMOLY;
  244. else
  245. #endif
  246. md->smem_layout.ccci_exp_smem_mdss_debug_size = CCCI_SMEM_MDSS_DEBUG_SIZE;
  247. md->smem_layout.ccci_exp_smem_sleep_debug_vir = md->smem_layout.ccci_exp_smem_base_vir +
  248. md->smem_layout.ccci_exp_smem_size - CCCI_SMEM_SLEEP_MODE_DBG_SIZE;
  249. md->smem_layout.ccci_exp_smem_sleep_debug_size = CCCI_SMEM_SLEEP_MODE_DBG_DUMP;
  250. /* exception record start address */
  251. md->smem_layout.ccci_exp_rec_base_vir = md->smem_layout.ccci_exp_smem_base_vir + CCCI_SMEM_OFFSET_EXREC;
  252. /*runtime region */
  253. md->smem_layout.ccci_rt_smem_base_phy = md->smem_layout.ccci_exp_smem_base_phy +
  254. md->smem_layout.ccci_exp_smem_size;
  255. md->smem_layout.ccci_rt_smem_base_vir = md->smem_layout.ccci_exp_smem_base_vir +
  256. md->smem_layout.ccci_exp_smem_size;
  257. md->smem_layout.ccci_rt_smem_size = CCCI_SMEM_SIZE_RUNTIME;
  258. /*md1 md3 shared memory region and remap*/
  259. get_md1_md3_resv_smem_info(md->index, &md->mem_layout.md1_md3_smem_phy,
  260. &md->mem_layout.md1_md3_smem_size);
  261. md->mem_layout.md1_md3_smem_vir =
  262. ioremap_nocache(md->mem_layout.md1_md3_smem_phy, md->mem_layout.md1_md3_smem_size);
  263. memset_io(md->mem_layout.md1_md3_smem_vir, 0, md->mem_layout.md1_md3_smem_size);
  264. /* updae image info */
  265. md->img_info[IMG_MD].type = IMG_MD;
  266. md->img_info[IMG_MD].address = md->mem_layout.md_region_phy;
  267. md->img_info[IMG_DSP].type = IMG_DSP;
  268. md->img_info[IMG_DSP].address = md->mem_layout.dsp_region_phy;
  269. md->img_info[IMG_ARMV7].type = IMG_ARMV7;
  270. if (md->config.setting & MD_SETTING_ENABLE)
  271. ccci_set_mem_remap(md, md_resv_smem_addr - md_resv_mem_addr,
  272. ALIGN(md_resv_mem_addr + md_resv_mem_size + md_resv_smem_size, 0x2000000));
  273. }
  274. /*
  275. * for debug log: 0 to disable; 1 for print to ram; 2 for print to uart
  276. * other value to desiable all log
  277. */
  278. #ifndef CCCI_LOG_LEVEL
  279. #define CCCI_LOG_LEVEL 0
  280. #endif
  281. unsigned int ccci_debug_enable = CCCI_LOG_LEVEL;
  282. /* ================================================================== */
  283. /* MD relate sys */
  284. /* ================================================================== */
  285. static void ccci_md_obj_release(struct kobject *kobj)
  286. {
  287. struct ccci_modem *md = container_of(kobj, struct ccci_modem, kobj);
  288. CCCI_DBG_MSG(md->index, SYSFS, "md kobject release\n");
  289. }
  290. static ssize_t ccci_md_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
  291. {
  292. ssize_t len = 0;
  293. struct ccci_md_attribute *a = container_of(attr, struct ccci_md_attribute, attr);
  294. if (a->show)
  295. len = a->show(a->modem, buf);
  296. return len;
  297. }
  298. static ssize_t ccci_md_attr_store(struct kobject *kobj, struct attribute *attr, const char *buf, size_t count)
  299. {
  300. ssize_t len = 0;
  301. struct ccci_md_attribute *a = container_of(attr, struct ccci_md_attribute, attr);
  302. if (a->store)
  303. len = a->store(a->modem, buf, count);
  304. return len;
  305. }
  306. static const struct sysfs_ops ccci_md_sysfs_ops = {
  307. .show = ccci_md_attr_show,
  308. .store = ccci_md_attr_store
  309. };
  310. static struct attribute *ccci_md_default_attrs[] = {
  311. NULL
  312. };
  313. static struct kobj_type ccci_md_ktype = {
  314. .release = ccci_md_obj_release,
  315. .sysfs_ops = &ccci_md_sysfs_ops,
  316. .default_attrs = ccci_md_default_attrs
  317. };
  318. /* ------------------------------------------------------------------------- */
  319. static int __init ccci_init(void)
  320. {
  321. CCCI_INF_MSG(-1, CORE, "ccci core init\n");
  322. dev_class = class_create(THIS_MODULE, "ccci_node");
  323. /* init common sub-system */
  324. /* ccci_subsys_sysfs_init(); */
  325. ccci_subsys_bm_init();
  326. ccci_plat_common_init();
  327. ccci_subsys_kernel_init();
  328. return 0;
  329. }
  330. /* setup function is only for data structure initialization */
  331. struct ccci_modem *ccci_allocate_modem(int private_size)
  332. {
  333. struct ccci_modem *md = kzalloc(sizeof(struct ccci_modem), GFP_KERNEL);
  334. int i;
  335. if (!md) {
  336. CCCI_ERR_MSG(-1, CORE, "fail to allocate memory for modem structure\n");
  337. goto out;
  338. }
  339. md->private_data = kzalloc(private_size, GFP_KERNEL);
  340. md->sim_type = 0xEEEEEEEE; /* sim_type(MCC/MNC) sent by MD wouldn't be 0xEEEEEEEE */
  341. md->config.setting |= MD_SETTING_FIRST_BOOT;
  342. md->md_state = INVALID;
  343. md->boot_stage = MD_BOOT_STAGE_0;
  344. #ifdef MD_UMOLY_EE_SUPPORT
  345. md->flight_mode = MD_FIGHT_MODE_NONE; /* leave flight mode */
  346. #endif
  347. md->ex_stage = EX_NONE;
  348. atomic_set(&md->wakeup_src, 0);
  349. INIT_LIST_HEAD(&md->entry);
  350. ccci_reset_seq_num(md);
  351. init_timer(&md->bootup_timer);
  352. md->bootup_timer.function = md_bootup_timeout_func;
  353. md->bootup_timer.data = (unsigned long)md;
  354. init_timer(&md->ex_monitor);
  355. md->ex_monitor.function = md_ex_monitor_func;
  356. md->ex_monitor.data = (unsigned long)md;
  357. init_timer(&md->ex_monitor2);
  358. md->ex_monitor2.function = md_ex_monitor2_func;
  359. md->ex_monitor2.data = (unsigned long)md;
  360. init_timer(&md->md_status_poller);
  361. md->md_status_poller.function = md_status_poller_func;
  362. md->md_status_poller.data = (unsigned long)md;
  363. init_timer(&md->md_status_timeout);
  364. md->md_status_timeout.function = md_status_timeout_func;
  365. md->md_status_timeout.data = (unsigned long)md;
  366. spin_lock_init(&md->ctrl_lock);
  367. for (i = 0; i < ARRAY_SIZE(md->rx_ch_ports); i++)
  368. INIT_LIST_HEAD(&md->rx_ch_ports[i]);
  369. out:
  370. return md;
  371. }
  372. EXPORT_SYMBOL(ccci_allocate_modem);
  373. int ccci_register_modem(struct ccci_modem *modem)
  374. {
  375. int ret;
  376. /* init per-modem sub-system */
  377. ccci_subsys_char_init(modem);
  378. CCCI_INF_MSG(modem->index, CORE, "register modem %d\n", modem->major);
  379. md_port_cfg(modem);
  380. /* init modem */
  381. /* TODO: check modem->ops for all must-have functions */
  382. ret = modem->ops->init(modem);
  383. if (ret < 0)
  384. return ret;
  385. ccci_config_modem(modem);
  386. list_add_tail(&modem->entry, &modem_list);
  387. ccci_sysfs_add_modem(modem->index, (void *)&modem->kobj, (void *)&ccci_md_ktype, boot_md_show, boot_md_store);
  388. ccci_platform_init(modem);
  389. return 0;
  390. }
  391. EXPORT_SYMBOL(ccci_register_modem);
  392. struct ccci_modem *ccci_get_modem_by_id(int md_id)
  393. {
  394. struct ccci_modem *md = NULL;
  395. list_for_each_entry(md, &modem_list, entry) {
  396. if (md->index == md_id)
  397. return md;
  398. }
  399. return NULL;
  400. }
  401. int ccci_get_modem_state(int md_id)
  402. {
  403. struct ccci_modem *md = NULL;
  404. list_for_each_entry(md, &modem_list, entry) {
  405. if (md->index == md_id)
  406. return md->md_state;
  407. }
  408. return -CCCI_ERR_MD_INDEX_NOT_FOUND;
  409. }
  410. int exec_ccci_kern_func_by_md_id(int md_id, unsigned int id, char *buf, unsigned int len)
  411. {
  412. struct ccci_modem *md = NULL;
  413. int ret = 0;
  414. list_for_each_entry(md, &modem_list, entry) {
  415. if (md->index == md_id) {
  416. ret = 1;
  417. break;
  418. }
  419. }
  420. if (!ret)
  421. return -CCCI_ERR_MD_INDEX_NOT_FOUND;
  422. CCCI_DBG_MSG(md->index, CORE, "%ps execuste function %d\n", __builtin_return_address(0), id);
  423. switch (id) {
  424. case ID_GET_MD_WAKEUP_SRC:
  425. atomic_set(&md->wakeup_src, 1);
  426. break;
  427. case ID_GET_TXPOWER:
  428. if (buf[0] == 0)
  429. ret = ccci_send_msg_to_md(md, CCCI_SYSTEM_TX, MD_TX_POWER, 0, 0);
  430. else if (buf[0] == 1)
  431. ret = ccci_send_msg_to_md(md, CCCI_SYSTEM_TX, MD_RF_TEMPERATURE, 0, 0);
  432. else if (buf[0] == 2)
  433. ret = ccci_send_msg_to_md(md, CCCI_SYSTEM_TX, MD_RF_TEMPERATURE_3G, 0, 0);
  434. break;
  435. case ID_PAUSE_LTE:
  436. /*
  437. * MD booting/flight mode/exception mode: return >0 to DVFS.
  438. * MD ready: return 0 if message delivered, return <0 if get error.
  439. * DVFS will call this API with IRQ disabled.
  440. */
  441. if (md->md_state != READY)
  442. ret = 1;
  443. else {
  444. ret = ccci_send_msg_to_md(md, CCCI_SYSTEM_TX, MD_PAUSE_LTE, *((int *)buf), 1);
  445. if (ret == -CCCI_ERR_MD_NOT_READY || ret == -CCCI_ERR_HIF_NOT_POWER_ON)
  446. ret = 1;
  447. }
  448. break;
  449. case ID_STORE_SIM_SWITCH_MODE:
  450. {
  451. int simmode = *((int *)buf);
  452. ccci_store_sim_switch_mode(md, simmode);
  453. }
  454. break;
  455. case ID_GET_SIM_SWITCH_MODE:
  456. {
  457. int simmode = ccci_get_sim_switch_mode();
  458. memcpy(buf, &simmode, sizeof(int));
  459. }
  460. break;
  461. case ID_GET_MD_STATE:
  462. ret = md->boot_stage;
  463. break;
  464. /* used for throttling feature - start */
  465. case ID_THROTTLING_CFG:
  466. ret = ccci_send_msg_to_md(md, CCCI_SYSTEM_TX, MD_THROTTLING, *((int *)buf), 1);
  467. break;
  468. /* used for throttling feature - end */
  469. #if defined(CONFIG_MTK_SWITCH_TX_POWER)
  470. case ID_UPDATE_TX_POWER:
  471. {
  472. unsigned int msg_id = (md_id == 0) ? MD_SW_MD1_TX_POWER : MD_SW_MD2_TX_POWER;
  473. unsigned int mode = *((unsigned int *)buf);
  474. ret = ccci_send_msg_to_md(md, CCCI_SYSTEM_TX, msg_id, mode, 0);
  475. }
  476. break;
  477. #endif
  478. case ID_RESET_MD:
  479. CCCI_INF_MSG(md->index, CHAR, "MD reset API called by %ps\n", __builtin_return_address(0));
  480. ret = md->ops->reset(md);
  481. if (ret == 0)
  482. ret = ccci_send_virtual_md_msg(md, CCCI_MONITOR_CH, CCCI_MD_MSG_RESET, 0);
  483. break;
  484. case ID_DUMP_MD_SLEEP_MODE:
  485. md->ops->dump_info(md, DUMP_FLAG_SMEM_MDSLP, NULL, 0);
  486. break;
  487. case ID_MD_RF_DESENSE:
  488. if (buf == NULL)
  489. CCCI_ERR_MSG(md->index, CHAR, "%ps,ID_MD_RF_DESENSE buf is null\n",
  490. __builtin_return_address(0));
  491. else
  492. ret = ccci_update_rf_desense(md, buf[0]);
  493. break;
  494. default:
  495. ret = -CCCI_ERR_FUNC_ID_ERROR;
  496. break;
  497. };
  498. return ret;
  499. }
  500. int aee_dump_ccci_debug_info(int md_id, void **addr, int *size)
  501. {
  502. struct ccci_modem *md = NULL;
  503. int ret = 0;
  504. md_id--; /* EE string use 1 and 2, not 0 and 1 */
  505. list_for_each_entry(md, &modem_list, entry) {
  506. if (md->index == md_id) {
  507. ret = 1;
  508. break;
  509. }
  510. }
  511. if (!ret)
  512. return -CCCI_ERR_MD_INDEX_NOT_FOUND;
  513. *addr = md->smem_layout.ccci_exp_smem_ccci_debug_vir;
  514. *size = md->smem_layout.ccci_exp_smem_ccci_debug_size;
  515. return 0;
  516. }
  517. struct ccci_port *ccci_get_port_for_node(int major, int minor)
  518. {
  519. struct ccci_modem *md = NULL;
  520. struct ccci_port *port = NULL;
  521. list_for_each_entry(md, &modem_list, entry) {
  522. if (md->major == major) {
  523. port = md->ops->get_port_by_minor(md, minor);
  524. break;
  525. }
  526. }
  527. return port;
  528. }
  529. int ccci_register_dev_node(const char *name, int major_id, int minor)
  530. {
  531. int ret = 0;
  532. dev_t dev_n;
  533. struct device *dev;
  534. dev_n = MKDEV(major_id, minor);
  535. dev = device_create(dev_class, NULL, dev_n, NULL, "%s", name);
  536. if (IS_ERR(dev))
  537. ret = PTR_ERR(dev);
  538. return ret;
  539. }
  540. /*
  541. * kernel inject CCCI message to modem.
  542. */
  543. int ccci_send_msg_to_md(struct ccci_modem *md, CCCI_CH ch, CCCI_MD_MSG msg, u32 resv, int blocking)
  544. {
  545. struct ccci_port *port = NULL;
  546. struct ccci_request *req = NULL;
  547. struct ccci_header *ccci_h;
  548. int ret;
  549. if (md->md_state != BOOTING && md->md_state != READY && md->md_state != EXCEPTION)
  550. return -CCCI_ERR_MD_NOT_READY;
  551. if (ch == CCCI_SYSTEM_TX && md->md_state != READY)
  552. return -CCCI_ERR_MD_NOT_READY;
  553. port = md->ops->get_port_by_channel(md, ch);
  554. if (port) {
  555. if (!blocking)
  556. req = ccci_alloc_req(OUT, sizeof(struct ccci_header), 0, 0);
  557. else
  558. req = ccci_alloc_req(OUT, sizeof(struct ccci_header), 1, 1);
  559. if (req) {
  560. req->policy = RECYCLE;
  561. ccci_h = (struct ccci_header *)skb_put(req->skb, sizeof(struct ccci_header));
  562. ccci_h->data[0] = CCCI_MAGIC_NUM;
  563. ccci_h->data[1] = msg;
  564. ccci_h->channel = ch;
  565. ccci_h->reserved = resv;
  566. ret = ccci_port_send_request(port, req);
  567. if (ret)
  568. ccci_free_req(req);
  569. return ret;
  570. } else {
  571. return -CCCI_ERR_ALLOCATE_MEMORY_FAIL;
  572. }
  573. }
  574. return -CCCI_ERR_INVALID_LOGIC_CHANNEL_ID;
  575. }
  576. /*
  577. * kernel inject message to user space daemon, this function may sleep
  578. */
  579. int ccci_send_virtual_md_msg(struct ccci_modem *md, CCCI_CH ch, CCCI_MD_MSG msg, u32 resv)
  580. {
  581. struct ccci_request *req = NULL;
  582. struct ccci_header *ccci_h;
  583. int ret = 0, count = 0;
  584. if (unlikely(ch != CCCI_MONITOR_CH)) {
  585. CCCI_ERR_MSG(md->index, CORE, "invalid channel %x for sending virtual msg\n", ch);
  586. return -CCCI_ERR_INVALID_LOGIC_CHANNEL_ID;
  587. }
  588. if (unlikely(in_interrupt())) {
  589. CCCI_ERR_MSG(md->index, CORE, "sending virtual msg from IRQ context %ps\n",
  590. __builtin_return_address(0));
  591. return -CCCI_ERR_ASSERT_ERR;
  592. }
  593. req = ccci_alloc_req(IN, sizeof(struct ccci_header), 1, 0);
  594. /* request will be recycled in char device's read function */
  595. ccci_h = (struct ccci_header *)skb_put(req->skb, sizeof(struct ccci_header));
  596. ccci_h->data[0] = CCCI_MAGIC_NUM;
  597. ccci_h->data[1] = msg;
  598. ccci_h->channel = ch;
  599. #ifdef FEATURE_SEQ_CHECK_EN
  600. ccci_h->assert_bit = 0;
  601. #endif
  602. ccci_h->reserved = resv;
  603. INIT_LIST_HEAD(&req->entry); /* as port will run list_del */
  604. retry:
  605. ret = ccci_port_recv_request(md, req, NULL);
  606. if (ret >= 0 || ret == -CCCI_ERR_DROP_PACKET)
  607. return ret;
  608. msleep(100);
  609. if (count++ < 20) {
  610. goto retry;
  611. } else {
  612. CCCI_ERR_MSG(md->index, CORE, "fail to send virtual msg %x for %ps\n", msg,
  613. __builtin_return_address(0));
  614. list_del(&req->entry);
  615. req->policy = RECYCLE;
  616. ccci_free_req(req);
  617. }
  618. return ret;
  619. }
  620. #if defined(CONFIG_MTK_SWITCH_TX_POWER)
  621. static int switch_Tx_Power(int md_id, unsigned int mode)
  622. {
  623. int ret = 0;
  624. unsigned int resv = mode;
  625. unsigned int msg_id = (md_id == 0) ? MD_SW_MD1_TX_POWER : MD_SW_MD2_TX_POWER;
  626. #if 1
  627. ret = exec_ccci_kern_func_by_md_id(md_id, ID_UPDATE_TX_POWER, (char *)&resv, sizeof(resv));
  628. #else
  629. ret = ccci_send_msg_to_md(md_id, CCCI_SYSTEM_TX, msg_id, resv, 0);
  630. #endif
  631. pr_debug("[swtp] switch_MD%d_Tx_Power(%d): ret[%d]\n", md_id + 1, resv, ret);
  632. CCCI_DBG_MSG(md_id, "ctl", "switch_MD%d_Tx_Power(%d): %d\n", md_id + 1, resv, ret);
  633. return ret;
  634. }
  635. int switch_MD1_Tx_Power(unsigned int mode)
  636. {
  637. return switch_Tx_Power(0, mode);
  638. }
  639. EXPORT_SYMBOL(switch_MD1_Tx_Power);
  640. int switch_MD2_Tx_Power(unsigned int mode)
  641. {
  642. return switch_Tx_Power(1, mode);
  643. }
  644. EXPORT_SYMBOL(switch_MD2_Tx_Power);
  645. #endif
  646. int register_smem_sub_region_mem_func(int md_id, smem_sub_region_cb_t pfunc, int region_id)
  647. {
  648. struct ccci_modem *md = NULL;
  649. int ret = 0;
  650. list_for_each_entry(md, &modem_list, entry) {
  651. if (md->index == md_id) {
  652. ret = 1;
  653. break;
  654. }
  655. }
  656. if (ret) {
  657. if (region_id < SMEM_SUB_REGION_MAX) {
  658. md->sub_region_cb_tbl[region_id] = pfunc;
  659. CCCI_INF_MSG(md_id, CORE, "region%d call back %p register success\n", region_id, pfunc);
  660. return 0;
  661. }
  662. CCCI_INF_MSG(md_id, CORE, "sub_region invalid %d\n", region_id);
  663. return -2;
  664. }
  665. CCCI_INF_MSG(md_id, CORE, "md id invalid %d\n", md_id);
  666. return -1;
  667. }
  668. void __iomem *get_smem_start_addr(int md_id, int region_id, int *size_o)
  669. {
  670. struct ccci_modem *md = NULL;
  671. int ret = 0;
  672. list_for_each_entry(md, &modem_list, entry) {
  673. if (md->index == md_id) {
  674. ret = 1;
  675. break;
  676. }
  677. }
  678. if (!ret) {
  679. CCCI_INF_MSG(md_id, CORE, "md%d not support\n", md_id+1);
  680. return NULL;
  681. }
  682. if (region_id >= SMEM_SUB_REGION_MAX) {
  683. CCCI_INF_MSG(md_id, CORE, "region id%d not support\n", region_id);
  684. return NULL;
  685. }
  686. if (md->sub_region_cb_tbl[region_id] == NULL) {
  687. CCCI_INF_MSG(md_id, CORE, "region%d call back not register\n", region_id);
  688. return NULL;
  689. }
  690. return md->sub_region_cb_tbl[region_id](md, size_o);
  691. }
  692. subsys_initcall(ccci_init);
  693. MODULE_AUTHOR("Xiao Wang <xiao.wang@mediatek.com>");
  694. MODULE_DESCRIPTION("Unified CCCI driver v0.1");
  695. MODULE_LICENSE("GPL");