iosm_ipc_imem_ops.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2020-21 Intel Corporation.
  4. */
  5. #include <linux/delay.h>
  6. #include "iosm_ipc_chnl_cfg.h"
  7. #include "iosm_ipc_devlink.h"
  8. #include "iosm_ipc_imem.h"
  9. #include "iosm_ipc_imem_ops.h"
  10. #include "iosm_ipc_port.h"
  11. #include "iosm_ipc_task_queue.h"
  12. /* Open a packet data online channel between the network layer and CP. */
  13. int ipc_imem_sys_wwan_open(struct iosm_imem *ipc_imem, int if_id)
  14. {
  15. dev_dbg(ipc_imem->dev, "%s if id: %d",
  16. ipc_imem_phase_get_string(ipc_imem->phase), if_id);
  17. /* The network interface is only supported in the runtime phase. */
  18. if (ipc_imem_phase_update(ipc_imem) != IPC_P_RUN) {
  19. dev_err(ipc_imem->dev, "net:%d : refused phase %s", if_id,
  20. ipc_imem_phase_get_string(ipc_imem->phase));
  21. return -EIO;
  22. }
  23. return ipc_mux_open_session(ipc_imem->mux, if_id);
  24. }
  25. /* Release a net link to CP. */
  26. void ipc_imem_sys_wwan_close(struct iosm_imem *ipc_imem, int if_id,
  27. int channel_id)
  28. {
  29. if (ipc_imem->mux && if_id >= IP_MUX_SESSION_START &&
  30. if_id <= IP_MUX_SESSION_END)
  31. ipc_mux_close_session(ipc_imem->mux, if_id);
  32. }
  33. /* Tasklet call to do uplink transfer. */
  34. static int ipc_imem_tq_cdev_write(struct iosm_imem *ipc_imem, int arg,
  35. void *msg, size_t size)
  36. {
  37. ipc_imem_ul_send(ipc_imem);
  38. return 0;
  39. }
  40. /* Through tasklet to do sio write. */
  41. static int ipc_imem_call_cdev_write(struct iosm_imem *ipc_imem)
  42. {
  43. return ipc_task_queue_send_task(ipc_imem, ipc_imem_tq_cdev_write, 0,
  44. NULL, 0, false);
  45. }
  46. /* Function for transfer UL data */
  47. int ipc_imem_sys_wwan_transmit(struct iosm_imem *ipc_imem,
  48. int if_id, int channel_id, struct sk_buff *skb)
  49. {
  50. int ret = -EINVAL;
  51. if (!ipc_imem || channel_id < 0)
  52. goto out;
  53. /* Is CP Running? */
  54. if (ipc_imem->phase != IPC_P_RUN) {
  55. dev_dbg(ipc_imem->dev, "phase %s transmit",
  56. ipc_imem_phase_get_string(ipc_imem->phase));
  57. ret = -EIO;
  58. goto out;
  59. }
  60. /* Route the UL packet through IP MUX Layer */
  61. ret = ipc_mux_ul_trigger_encode(ipc_imem->mux, if_id, skb);
  62. out:
  63. return ret;
  64. }
  65. /* Initialize wwan channel */
  66. int ipc_imem_wwan_channel_init(struct iosm_imem *ipc_imem,
  67. enum ipc_mux_protocol mux_type)
  68. {
  69. struct ipc_chnl_cfg chnl_cfg = { 0 };
  70. ipc_imem->cp_version = ipc_mmio_get_cp_version(ipc_imem->mmio);
  71. /* If modem version is invalid (0xffffffff), do not initialize WWAN. */
  72. if (ipc_imem->cp_version == -1) {
  73. dev_err(ipc_imem->dev, "invalid CP version");
  74. return -EIO;
  75. }
  76. ipc_chnl_cfg_get(&chnl_cfg, ipc_imem->nr_of_channels);
  77. if (ipc_imem->mmio->mux_protocol == MUX_AGGREGATION &&
  78. ipc_imem->nr_of_channels == IPC_MEM_IP_CHL_ID_0) {
  79. chnl_cfg.ul_nr_of_entries = IPC_MEM_MAX_TDS_MUX_AGGR_UL;
  80. chnl_cfg.dl_nr_of_entries = IPC_MEM_MAX_TDS_MUX_AGGR_DL;
  81. chnl_cfg.dl_buf_size = IPC_MEM_MAX_ADB_BUF_SIZE;
  82. }
  83. ipc_imem_channel_init(ipc_imem, IPC_CTYPE_WWAN, chnl_cfg,
  84. IRQ_MOD_OFF);
  85. /* WWAN registration. */
  86. ipc_imem->wwan = ipc_wwan_init(ipc_imem, ipc_imem->dev);
  87. if (!ipc_imem->wwan) {
  88. dev_err(ipc_imem->dev,
  89. "failed to register the ipc_wwan interfaces");
  90. return -ENOMEM;
  91. }
  92. return 0;
  93. }
  94. /* Map SKB to DMA for transfer */
  95. static int ipc_imem_map_skb_to_dma(struct iosm_imem *ipc_imem,
  96. struct sk_buff *skb)
  97. {
  98. struct iosm_pcie *ipc_pcie = ipc_imem->pcie;
  99. char *buf = skb->data;
  100. int len = skb->len;
  101. dma_addr_t mapping;
  102. int ret;
  103. ret = ipc_pcie_addr_map(ipc_pcie, buf, len, &mapping, DMA_TO_DEVICE);
  104. if (ret)
  105. goto err;
  106. BUILD_BUG_ON(sizeof(*IPC_CB(skb)) > sizeof(skb->cb));
  107. IPC_CB(skb)->mapping = mapping;
  108. IPC_CB(skb)->direction = DMA_TO_DEVICE;
  109. IPC_CB(skb)->len = len;
  110. IPC_CB(skb)->op_type = (u8)UL_DEFAULT;
  111. err:
  112. return ret;
  113. }
  114. /* return true if channel is ready for use */
  115. static bool ipc_imem_is_channel_active(struct iosm_imem *ipc_imem,
  116. struct ipc_mem_channel *channel)
  117. {
  118. enum ipc_phase phase;
  119. /* Update the current operation phase. */
  120. phase = ipc_imem->phase;
  121. /* Select the operation depending on the execution stage. */
  122. switch (phase) {
  123. case IPC_P_RUN:
  124. case IPC_P_PSI:
  125. case IPC_P_EBL:
  126. break;
  127. case IPC_P_ROM:
  128. /* Prepare the PSI image for the CP ROM driver and
  129. * suspend the flash app.
  130. */
  131. if (channel->state != IMEM_CHANNEL_RESERVED) {
  132. dev_err(ipc_imem->dev,
  133. "ch[%d]:invalid channel state %d,expected %d",
  134. channel->channel_id, channel->state,
  135. IMEM_CHANNEL_RESERVED);
  136. goto channel_unavailable;
  137. }
  138. goto channel_available;
  139. default:
  140. /* Ignore uplink actions in all other phases. */
  141. dev_err(ipc_imem->dev, "ch[%d]: confused phase %d",
  142. channel->channel_id, phase);
  143. goto channel_unavailable;
  144. }
  145. /* Check the full availability of the channel. */
  146. if (channel->state != IMEM_CHANNEL_ACTIVE) {
  147. dev_err(ipc_imem->dev, "ch[%d]: confused channel state %d",
  148. channel->channel_id, channel->state);
  149. goto channel_unavailable;
  150. }
  151. channel_available:
  152. return true;
  153. channel_unavailable:
  154. return false;
  155. }
  156. /**
  157. * ipc_imem_sys_port_close - Release a sio link to CP.
  158. * @ipc_imem: Imem instance.
  159. * @channel: Channel instance.
  160. */
  161. void ipc_imem_sys_port_close(struct iosm_imem *ipc_imem,
  162. struct ipc_mem_channel *channel)
  163. {
  164. enum ipc_phase curr_phase;
  165. int status = 0;
  166. u32 tail = 0;
  167. curr_phase = ipc_imem->phase;
  168. /* If current phase is IPC_P_OFF or SIO ID is -ve then
  169. * channel is already freed. Nothing to do.
  170. */
  171. if (curr_phase == IPC_P_OFF) {
  172. dev_err(ipc_imem->dev,
  173. "nothing to do. Current Phase: %s",
  174. ipc_imem_phase_get_string(curr_phase));
  175. return;
  176. }
  177. if (channel->state == IMEM_CHANNEL_FREE) {
  178. dev_err(ipc_imem->dev, "ch[%d]: invalid channel state %d",
  179. channel->channel_id, channel->state);
  180. return;
  181. }
  182. /* If there are any pending TDs then wait for Timeout/Completion before
  183. * closing pipe.
  184. */
  185. if (channel->ul_pipe.old_tail != channel->ul_pipe.old_head) {
  186. ipc_imem->app_notify_ul_pend = 1;
  187. /* Suspend the user app and wait a certain time for processing
  188. * UL Data.
  189. */
  190. status = wait_for_completion_interruptible_timeout
  191. (&ipc_imem->ul_pend_sem,
  192. msecs_to_jiffies(IPC_PEND_DATA_TIMEOUT));
  193. if (status == 0) {
  194. dev_dbg(ipc_imem->dev,
  195. "Pend data Timeout UL-Pipe:%d Head:%d Tail:%d",
  196. channel->ul_pipe.pipe_nr,
  197. channel->ul_pipe.old_head,
  198. channel->ul_pipe.old_tail);
  199. }
  200. ipc_imem->app_notify_ul_pend = 0;
  201. }
  202. /* If there are any pending TDs then wait for Timeout/Completion before
  203. * closing pipe.
  204. */
  205. ipc_protocol_get_head_tail_index(ipc_imem->ipc_protocol,
  206. &channel->dl_pipe, NULL, &tail);
  207. if (tail != channel->dl_pipe.old_tail) {
  208. ipc_imem->app_notify_dl_pend = 1;
  209. /* Suspend the user app and wait a certain time for processing
  210. * DL Data.
  211. */
  212. status = wait_for_completion_interruptible_timeout
  213. (&ipc_imem->dl_pend_sem,
  214. msecs_to_jiffies(IPC_PEND_DATA_TIMEOUT));
  215. if (status == 0) {
  216. dev_dbg(ipc_imem->dev,
  217. "Pend data Timeout DL-Pipe:%d Head:%d Tail:%d",
  218. channel->dl_pipe.pipe_nr,
  219. channel->dl_pipe.old_head,
  220. channel->dl_pipe.old_tail);
  221. }
  222. ipc_imem->app_notify_dl_pend = 0;
  223. }
  224. /* Due to wait for completion in messages, there is a small window
  225. * between closing the pipe and updating the channel is closed. In this
  226. * small window there could be HP update from Host Driver. Hence update
  227. * the channel state as CLOSING to aviod unnecessary interrupt
  228. * towards CP.
  229. */
  230. channel->state = IMEM_CHANNEL_CLOSING;
  231. ipc_imem_pipe_close(ipc_imem, &channel->ul_pipe);
  232. ipc_imem_pipe_close(ipc_imem, &channel->dl_pipe);
  233. ipc_imem_channel_free(channel);
  234. }
  235. /* Open a PORT link to CP and return the channel */
  236. struct ipc_mem_channel *ipc_imem_sys_port_open(struct iosm_imem *ipc_imem,
  237. int chl_id, int hp_id)
  238. {
  239. struct ipc_mem_channel *channel;
  240. int ch_id;
  241. /* The PORT interface is only supported in the runtime phase. */
  242. if (ipc_imem_phase_update(ipc_imem) != IPC_P_RUN) {
  243. dev_err(ipc_imem->dev, "PORT open refused, phase %s",
  244. ipc_imem_phase_get_string(ipc_imem->phase));
  245. return NULL;
  246. }
  247. ch_id = ipc_imem_channel_alloc(ipc_imem, chl_id, IPC_CTYPE_CTRL);
  248. if (ch_id < 0) {
  249. dev_err(ipc_imem->dev, "reservation of an PORT chnl id failed");
  250. return NULL;
  251. }
  252. channel = ipc_imem_channel_open(ipc_imem, ch_id, hp_id);
  253. if (!channel) {
  254. dev_err(ipc_imem->dev, "PORT channel id open failed");
  255. return NULL;
  256. }
  257. return channel;
  258. }
  259. /* transfer skb to modem */
  260. int ipc_imem_sys_cdev_write(struct iosm_cdev *ipc_cdev, struct sk_buff *skb)
  261. {
  262. struct ipc_mem_channel *channel = ipc_cdev->channel;
  263. struct iosm_imem *ipc_imem = ipc_cdev->ipc_imem;
  264. int ret = -EIO;
  265. if (!ipc_imem_is_channel_active(ipc_imem, channel) ||
  266. ipc_imem->phase == IPC_P_OFF_REQ)
  267. goto out;
  268. ret = ipc_imem_map_skb_to_dma(ipc_imem, skb);
  269. if (ret)
  270. goto out;
  271. /* Add skb to the uplink skbuf accumulator. */
  272. skb_queue_tail(&channel->ul_list, skb);
  273. ret = ipc_imem_call_cdev_write(ipc_imem);
  274. if (ret) {
  275. skb_dequeue_tail(&channel->ul_list);
  276. dev_err(ipc_cdev->dev, "channel id[%d] write failed\n",
  277. ipc_cdev->channel->channel_id);
  278. }
  279. out:
  280. return ret;
  281. }
  282. /* Open a SIO link to CP and return the channel instance */
  283. struct ipc_mem_channel *ipc_imem_sys_devlink_open(struct iosm_imem *ipc_imem)
  284. {
  285. struct ipc_mem_channel *channel;
  286. enum ipc_phase phase;
  287. int channel_id;
  288. phase = ipc_imem_phase_update(ipc_imem);
  289. switch (phase) {
  290. case IPC_P_OFF:
  291. case IPC_P_ROM:
  292. /* Get a channel id as flash id and reserve it. */
  293. channel_id = ipc_imem_channel_alloc(ipc_imem,
  294. IPC_MEM_CTRL_CHL_ID_7,
  295. IPC_CTYPE_CTRL);
  296. if (channel_id < 0) {
  297. dev_err(ipc_imem->dev,
  298. "reservation of a flash channel id failed");
  299. goto error;
  300. }
  301. ipc_imem->ipc_devlink->devlink_sio.channel_id = channel_id;
  302. channel = &ipc_imem->channels[channel_id];
  303. /* Enqueue chip info data to be read */
  304. if (ipc_imem_devlink_trigger_chip_info(ipc_imem)) {
  305. dev_err(ipc_imem->dev, "Enqueue of chip info failed");
  306. channel->state = IMEM_CHANNEL_FREE;
  307. goto error;
  308. }
  309. return channel;
  310. case IPC_P_PSI:
  311. case IPC_P_EBL:
  312. ipc_imem->cp_version = ipc_mmio_get_cp_version(ipc_imem->mmio);
  313. if (ipc_imem->cp_version == -1) {
  314. dev_err(ipc_imem->dev, "invalid CP version");
  315. goto error;
  316. }
  317. channel_id = ipc_imem->ipc_devlink->devlink_sio.channel_id;
  318. return ipc_imem_channel_open(ipc_imem, channel_id,
  319. IPC_HP_CDEV_OPEN);
  320. default:
  321. /* CP is in the wrong state (e.g. CRASH or CD_READY) */
  322. dev_err(ipc_imem->dev, "SIO open refused, phase %d", phase);
  323. }
  324. error:
  325. return NULL;
  326. }
  327. /* Release a SIO channel link to CP. */
  328. void ipc_imem_sys_devlink_close(struct iosm_devlink *ipc_devlink)
  329. {
  330. struct iosm_imem *ipc_imem = ipc_devlink->pcie->imem;
  331. int boot_check_timeout = BOOT_CHECK_DEFAULT_TIMEOUT;
  332. enum ipc_mem_exec_stage exec_stage;
  333. struct ipc_mem_channel *channel;
  334. int status = 0;
  335. u32 tail = 0;
  336. channel = ipc_imem->ipc_devlink->devlink_sio.channel;
  337. /* Increase the total wait time to boot_check_timeout */
  338. do {
  339. exec_stage = ipc_mmio_get_exec_stage(ipc_imem->mmio);
  340. if (exec_stage == IPC_MEM_EXEC_STAGE_RUN ||
  341. exec_stage == IPC_MEM_EXEC_STAGE_PSI)
  342. break;
  343. msleep(20);
  344. boot_check_timeout -= 20;
  345. } while (boot_check_timeout > 0);
  346. /* If there are any pending TDs then wait for Timeout/Completion before
  347. * closing pipe.
  348. */
  349. if (channel->ul_pipe.old_tail != channel->ul_pipe.old_head) {
  350. status = wait_for_completion_interruptible_timeout
  351. (&ipc_imem->ul_pend_sem,
  352. msecs_to_jiffies(IPC_PEND_DATA_TIMEOUT));
  353. if (status == 0) {
  354. dev_dbg(ipc_imem->dev,
  355. "Data Timeout on UL-Pipe:%d Head:%d Tail:%d",
  356. channel->ul_pipe.pipe_nr,
  357. channel->ul_pipe.old_head,
  358. channel->ul_pipe.old_tail);
  359. }
  360. }
  361. ipc_protocol_get_head_tail_index(ipc_imem->ipc_protocol,
  362. &channel->dl_pipe, NULL, &tail);
  363. if (tail != channel->dl_pipe.old_tail) {
  364. status = wait_for_completion_interruptible_timeout
  365. (&ipc_imem->dl_pend_sem,
  366. msecs_to_jiffies(IPC_PEND_DATA_TIMEOUT));
  367. if (status == 0) {
  368. dev_dbg(ipc_imem->dev,
  369. "Data Timeout on DL-Pipe:%d Head:%d Tail:%d",
  370. channel->dl_pipe.pipe_nr,
  371. channel->dl_pipe.old_head,
  372. channel->dl_pipe.old_tail);
  373. }
  374. }
  375. /* Due to wait for completion in messages, there is a small window
  376. * between closing the pipe and updating the channel is closed. In this
  377. * small window there could be HP update from Host Driver. Hence update
  378. * the channel state as CLOSING to aviod unnecessary interrupt
  379. * towards CP.
  380. */
  381. channel->state = IMEM_CHANNEL_CLOSING;
  382. /* Release the pipe resources */
  383. ipc_imem_pipe_cleanup(ipc_imem, &channel->ul_pipe);
  384. ipc_imem_pipe_cleanup(ipc_imem, &channel->dl_pipe);
  385. ipc_imem->nr_of_channels--;
  386. }
  387. void ipc_imem_sys_devlink_notify_rx(struct iosm_devlink *ipc_devlink,
  388. struct sk_buff *skb)
  389. {
  390. skb_queue_tail(&ipc_devlink->devlink_sio.rx_list, skb);
  391. complete(&ipc_devlink->devlink_sio.read_sem);
  392. }
  393. /* PSI transfer */
  394. static int ipc_imem_sys_psi_transfer(struct iosm_imem *ipc_imem,
  395. struct ipc_mem_channel *channel,
  396. unsigned char *buf, int count)
  397. {
  398. int psi_start_timeout = PSI_START_DEFAULT_TIMEOUT;
  399. enum ipc_mem_exec_stage exec_stage;
  400. dma_addr_t mapping = 0;
  401. int ret;
  402. ret = ipc_pcie_addr_map(ipc_imem->pcie, buf, count, &mapping,
  403. DMA_TO_DEVICE);
  404. if (ret)
  405. goto pcie_addr_map_fail;
  406. /* Save the PSI information for the CP ROM driver on the doorbell
  407. * scratchpad.
  408. */
  409. ipc_mmio_set_psi_addr_and_size(ipc_imem->mmio, mapping, count);
  410. ipc_doorbell_fire(ipc_imem->pcie, 0, IPC_MEM_EXEC_STAGE_BOOT);
  411. ret = wait_for_completion_interruptible_timeout
  412. (&channel->ul_sem,
  413. msecs_to_jiffies(IPC_PSI_TRANSFER_TIMEOUT));
  414. if (ret <= 0) {
  415. dev_err(ipc_imem->dev, "Failed PSI transfer to CP, Error-%d",
  416. ret);
  417. goto psi_transfer_fail;
  418. }
  419. /* If the PSI download fails, return the CP boot ROM exit code */
  420. if (ipc_imem->rom_exit_code != IMEM_ROM_EXIT_OPEN_EXT &&
  421. ipc_imem->rom_exit_code != IMEM_ROM_EXIT_CERT_EXT) {
  422. ret = (-1) * ((int)ipc_imem->rom_exit_code);
  423. goto psi_transfer_fail;
  424. }
  425. dev_dbg(ipc_imem->dev, "PSI image successfully downloaded");
  426. /* Wait psi_start_timeout milliseconds until the CP PSI image is
  427. * running and updates the execution_stage field with
  428. * IPC_MEM_EXEC_STAGE_PSI. Verify the execution stage.
  429. */
  430. do {
  431. exec_stage = ipc_mmio_get_exec_stage(ipc_imem->mmio);
  432. if (exec_stage == IPC_MEM_EXEC_STAGE_PSI)
  433. break;
  434. msleep(20);
  435. psi_start_timeout -= 20;
  436. } while (psi_start_timeout > 0);
  437. if (exec_stage != IPC_MEM_EXEC_STAGE_PSI)
  438. goto psi_transfer_fail; /* Unknown status of CP PSI process. */
  439. ipc_imem->phase = IPC_P_PSI;
  440. /* Enter the PSI phase. */
  441. dev_dbg(ipc_imem->dev, "execution_stage[%X] eq. PSI", exec_stage);
  442. /* Request the RUNNING state from CP and wait until it was reached
  443. * or timeout.
  444. */
  445. ipc_imem_ipc_init_check(ipc_imem);
  446. ret = wait_for_completion_interruptible_timeout
  447. (&channel->ul_sem, msecs_to_jiffies(IPC_PSI_TRANSFER_TIMEOUT));
  448. if (ret <= 0) {
  449. dev_err(ipc_imem->dev,
  450. "Failed PSI RUNNING state on CP, Error-%d", ret);
  451. goto psi_transfer_fail;
  452. }
  453. if (ipc_mmio_get_ipc_state(ipc_imem->mmio) !=
  454. IPC_MEM_DEVICE_IPC_RUNNING) {
  455. dev_err(ipc_imem->dev,
  456. "ch[%d] %s: unexpected CP IPC state %d, not RUNNING",
  457. channel->channel_id,
  458. ipc_imem_phase_get_string(ipc_imem->phase),
  459. ipc_mmio_get_ipc_state(ipc_imem->mmio));
  460. goto psi_transfer_fail;
  461. }
  462. /* Create the flash channel for the transfer of the images. */
  463. if (!ipc_imem_sys_devlink_open(ipc_imem)) {
  464. dev_err(ipc_imem->dev, "can't open flash_channel");
  465. goto psi_transfer_fail;
  466. }
  467. ret = 0;
  468. psi_transfer_fail:
  469. ipc_pcie_addr_unmap(ipc_imem->pcie, count, mapping, DMA_TO_DEVICE);
  470. pcie_addr_map_fail:
  471. return ret;
  472. }
  473. int ipc_imem_sys_devlink_write(struct iosm_devlink *ipc_devlink,
  474. unsigned char *buf, int count)
  475. {
  476. struct iosm_imem *ipc_imem = ipc_devlink->pcie->imem;
  477. struct ipc_mem_channel *channel;
  478. struct sk_buff *skb;
  479. dma_addr_t mapping;
  480. int ret;
  481. channel = ipc_imem->ipc_devlink->devlink_sio.channel;
  482. /* In the ROM phase the PSI image is passed to CP about a specific
  483. * shared memory area and doorbell scratchpad directly.
  484. */
  485. if (ipc_imem->phase == IPC_P_ROM) {
  486. ret = ipc_imem_sys_psi_transfer(ipc_imem, channel, buf, count);
  487. /* If the PSI transfer fails then send crash
  488. * Signature.
  489. */
  490. if (ret > 0)
  491. ipc_imem_msg_send_feature_set(ipc_imem,
  492. IPC_MEM_INBAND_CRASH_SIG,
  493. false);
  494. goto out;
  495. }
  496. /* Allocate skb memory for the uplink buffer. */
  497. skb = ipc_pcie_alloc_skb(ipc_devlink->pcie, count, GFP_KERNEL, &mapping,
  498. DMA_TO_DEVICE, 0);
  499. if (!skb) {
  500. ret = -ENOMEM;
  501. goto out;
  502. }
  503. skb_put_data(skb, buf, count);
  504. IPC_CB(skb)->op_type = UL_USR_OP_BLOCKED;
  505. /* Add skb to the uplink skbuf accumulator. */
  506. skb_queue_tail(&channel->ul_list, skb);
  507. /* Inform the IPC tasklet to pass uplink IP packets to CP. */
  508. if (!ipc_imem_call_cdev_write(ipc_imem)) {
  509. ret = wait_for_completion_interruptible(&channel->ul_sem);
  510. if (ret < 0) {
  511. dev_err(ipc_imem->dev,
  512. "ch[%d] no CP confirmation, status = %d",
  513. channel->channel_id, ret);
  514. ipc_pcie_kfree_skb(ipc_devlink->pcie, skb);
  515. goto out;
  516. }
  517. }
  518. ret = 0;
  519. out:
  520. return ret;
  521. }
  522. int ipc_imem_sys_devlink_read(struct iosm_devlink *devlink, u8 *data,
  523. u32 bytes_to_read, u32 *bytes_read)
  524. {
  525. struct sk_buff *skb = NULL;
  526. int rc = 0;
  527. /* check skb is available in rx_list or wait for skb */
  528. devlink->devlink_sio.devlink_read_pend = 1;
  529. while (!skb && !(skb = skb_dequeue(&devlink->devlink_sio.rx_list))) {
  530. if (!wait_for_completion_interruptible_timeout
  531. (&devlink->devlink_sio.read_sem,
  532. msecs_to_jiffies(IPC_READ_TIMEOUT))) {
  533. dev_err(devlink->dev, "Read timedout");
  534. rc = -ETIMEDOUT;
  535. goto devlink_read_fail;
  536. }
  537. }
  538. devlink->devlink_sio.devlink_read_pend = 0;
  539. if (bytes_to_read < skb->len) {
  540. dev_err(devlink->dev, "Invalid size,expected len %d", skb->len);
  541. rc = -EINVAL;
  542. goto devlink_read_fail;
  543. }
  544. *bytes_read = skb->len;
  545. memcpy(data, skb->data, skb->len);
  546. devlink_read_fail:
  547. dev_kfree_skb(skb);
  548. return rc;
  549. }