hbm.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. /*
  2. * ISHTP bus layer messages handling
  3. *
  4. * Copyright (c) 2003-2016, Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. */
  16. #include <linux/export.h>
  17. #include <linux/slab.h>
  18. #include <linux/sched.h>
  19. #include <linux/wait.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/miscdevice.h>
  22. #include "ishtp-dev.h"
  23. #include "hbm.h"
  24. #include "client.h"
  25. /**
  26. * ishtp_hbm_fw_cl_allocate() - Allocate FW clients
  27. * @dev: ISHTP device instance
  28. *
  29. * Allocates storage for fw clients
  30. */
  31. static void ishtp_hbm_fw_cl_allocate(struct ishtp_device *dev)
  32. {
  33. struct ishtp_fw_client *clients;
  34. int b;
  35. /* count how many ISH clients we have */
  36. for_each_set_bit(b, dev->fw_clients_map, ISHTP_CLIENTS_MAX)
  37. dev->fw_clients_num++;
  38. if (dev->fw_clients_num <= 0)
  39. return;
  40. /* allocate storage for fw clients representation */
  41. clients = kcalloc(dev->fw_clients_num, sizeof(struct ishtp_fw_client),
  42. GFP_KERNEL);
  43. if (!clients) {
  44. dev->dev_state = ISHTP_DEV_RESETTING;
  45. ish_hw_reset(dev);
  46. return;
  47. }
  48. dev->fw_clients = clients;
  49. }
  50. /**
  51. * ishtp_hbm_cl_hdr() - construct client hbm header
  52. * @cl: client
  53. * @hbm_cmd: host bus message command
  54. * @buf: buffer for cl header
  55. * @len: buffer length
  56. *
  57. * Initialize HBM buffer
  58. */
  59. static inline void ishtp_hbm_cl_hdr(struct ishtp_cl *cl, uint8_t hbm_cmd,
  60. void *buf, size_t len)
  61. {
  62. struct ishtp_hbm_cl_cmd *cmd = buf;
  63. memset(cmd, 0, len);
  64. cmd->hbm_cmd = hbm_cmd;
  65. cmd->host_addr = cl->host_client_id;
  66. cmd->fw_addr = cl->fw_client_id;
  67. }
  68. /**
  69. * ishtp_hbm_cl_addr_equal() - Compare client address
  70. * @cl: client
  71. * @buf: Client command buffer
  72. *
  73. * Compare client address with the address in command buffer
  74. *
  75. * Return: True if they have the same address
  76. */
  77. static inline bool ishtp_hbm_cl_addr_equal(struct ishtp_cl *cl, void *buf)
  78. {
  79. struct ishtp_hbm_cl_cmd *cmd = buf;
  80. return cl->host_client_id == cmd->host_addr &&
  81. cl->fw_client_id == cmd->fw_addr;
  82. }
  83. /**
  84. * ishtp_hbm_start_wait() - Wait for HBM start message
  85. * @dev: ISHTP device instance
  86. *
  87. * Wait for HBM start message from firmware
  88. *
  89. * Return: 0 if HBM start is/was received else timeout error
  90. */
  91. int ishtp_hbm_start_wait(struct ishtp_device *dev)
  92. {
  93. int ret;
  94. if (dev->hbm_state > ISHTP_HBM_START)
  95. return 0;
  96. dev_dbg(dev->devc, "Going to wait for ishtp start. hbm_state=%08X\n",
  97. dev->hbm_state);
  98. ret = wait_event_interruptible_timeout(dev->wait_hbm_recvd_msg,
  99. dev->hbm_state >= ISHTP_HBM_STARTED,
  100. (ISHTP_INTEROP_TIMEOUT * HZ));
  101. dev_dbg(dev->devc,
  102. "Woke up from waiting for ishtp start. hbm_state=%08X\n",
  103. dev->hbm_state);
  104. if (ret <= 0 && (dev->hbm_state <= ISHTP_HBM_START)) {
  105. dev->hbm_state = ISHTP_HBM_IDLE;
  106. dev_err(dev->devc,
  107. "waiting for ishtp start failed. ret=%d hbm_state=%08X\n",
  108. ret, dev->hbm_state);
  109. return -ETIMEDOUT;
  110. }
  111. return 0;
  112. }
  113. /**
  114. * ishtp_hbm_start_req() - Send HBM start message
  115. * @dev: ISHTP device instance
  116. *
  117. * Send HBM start message to firmware
  118. *
  119. * Return: 0 if success else error code
  120. */
  121. int ishtp_hbm_start_req(struct ishtp_device *dev)
  122. {
  123. struct ishtp_msg_hdr hdr;
  124. unsigned char data[128];
  125. struct ishtp_msg_hdr *ishtp_hdr = &hdr;
  126. struct hbm_host_version_request *start_req;
  127. const size_t len = sizeof(struct hbm_host_version_request);
  128. ishtp_hbm_hdr(ishtp_hdr, len);
  129. /* host start message */
  130. start_req = (struct hbm_host_version_request *)data;
  131. memset(start_req, 0, len);
  132. start_req->hbm_cmd = HOST_START_REQ_CMD;
  133. start_req->host_version.major_version = HBM_MAJOR_VERSION;
  134. start_req->host_version.minor_version = HBM_MINOR_VERSION;
  135. /*
  136. * (!) Response to HBM start may be so quick that this thread would get
  137. * preempted BEFORE managing to set hbm_state = ISHTP_HBM_START.
  138. * So set it at first, change back to ISHTP_HBM_IDLE upon failure
  139. */
  140. dev->hbm_state = ISHTP_HBM_START;
  141. if (ishtp_write_message(dev, ishtp_hdr, data)) {
  142. dev_err(dev->devc, "version message send failed\n");
  143. dev->dev_state = ISHTP_DEV_RESETTING;
  144. dev->hbm_state = ISHTP_HBM_IDLE;
  145. ish_hw_reset(dev);
  146. return -ENODEV;
  147. }
  148. return 0;
  149. }
  150. /**
  151. * ishtp_hbm_enum_clients_req() - Send client enum req
  152. * @dev: ISHTP device instance
  153. *
  154. * Send enumeration client request message
  155. *
  156. * Return: 0 if success else error code
  157. */
  158. void ishtp_hbm_enum_clients_req(struct ishtp_device *dev)
  159. {
  160. struct ishtp_msg_hdr hdr;
  161. unsigned char data[128];
  162. struct ishtp_msg_hdr *ishtp_hdr = &hdr;
  163. struct hbm_host_enum_request *enum_req;
  164. const size_t len = sizeof(struct hbm_host_enum_request);
  165. /* enumerate clients */
  166. ishtp_hbm_hdr(ishtp_hdr, len);
  167. enum_req = (struct hbm_host_enum_request *)data;
  168. memset(enum_req, 0, len);
  169. enum_req->hbm_cmd = HOST_ENUM_REQ_CMD;
  170. if (ishtp_write_message(dev, ishtp_hdr, data)) {
  171. dev->dev_state = ISHTP_DEV_RESETTING;
  172. dev_err(dev->devc, "enumeration request send failed\n");
  173. ish_hw_reset(dev);
  174. }
  175. dev->hbm_state = ISHTP_HBM_ENUM_CLIENTS;
  176. }
  177. /**
  178. * ishtp_hbm_prop_req() - Request property
  179. * @dev: ISHTP device instance
  180. *
  181. * Request property for a single client
  182. *
  183. * Return: 0 if success else error code
  184. */
  185. static int ishtp_hbm_prop_req(struct ishtp_device *dev)
  186. {
  187. struct ishtp_msg_hdr hdr;
  188. unsigned char data[128];
  189. struct ishtp_msg_hdr *ishtp_hdr = &hdr;
  190. struct hbm_props_request *prop_req;
  191. const size_t len = sizeof(struct hbm_props_request);
  192. unsigned long next_client_index;
  193. uint8_t client_num;
  194. client_num = dev->fw_client_presentation_num;
  195. next_client_index = find_next_bit(dev->fw_clients_map,
  196. ISHTP_CLIENTS_MAX, dev->fw_client_index);
  197. /* We got all client properties */
  198. if (next_client_index == ISHTP_CLIENTS_MAX) {
  199. dev->hbm_state = ISHTP_HBM_WORKING;
  200. dev->dev_state = ISHTP_DEV_ENABLED;
  201. for (dev->fw_client_presentation_num = 1;
  202. dev->fw_client_presentation_num < client_num + 1;
  203. ++dev->fw_client_presentation_num)
  204. /* Add new client device */
  205. ishtp_bus_new_client(dev);
  206. return 0;
  207. }
  208. dev->fw_clients[client_num].client_id = next_client_index;
  209. ishtp_hbm_hdr(ishtp_hdr, len);
  210. prop_req = (struct hbm_props_request *)data;
  211. memset(prop_req, 0, sizeof(struct hbm_props_request));
  212. prop_req->hbm_cmd = HOST_CLIENT_PROPERTIES_REQ_CMD;
  213. prop_req->address = next_client_index;
  214. if (ishtp_write_message(dev, ishtp_hdr, data)) {
  215. dev->dev_state = ISHTP_DEV_RESETTING;
  216. dev_err(dev->devc, "properties request send failed\n");
  217. ish_hw_reset(dev);
  218. return -EIO;
  219. }
  220. dev->fw_client_index = next_client_index;
  221. return 0;
  222. }
  223. /**
  224. * ishtp_hbm_stop_req() - Send HBM stop
  225. * @dev: ISHTP device instance
  226. *
  227. * Send stop request message
  228. */
  229. static void ishtp_hbm_stop_req(struct ishtp_device *dev)
  230. {
  231. struct ishtp_msg_hdr hdr;
  232. unsigned char data[128];
  233. struct ishtp_msg_hdr *ishtp_hdr = &hdr;
  234. struct hbm_host_stop_request *req;
  235. const size_t len = sizeof(struct hbm_host_stop_request);
  236. ishtp_hbm_hdr(ishtp_hdr, len);
  237. req = (struct hbm_host_stop_request *)data;
  238. memset(req, 0, sizeof(struct hbm_host_stop_request));
  239. req->hbm_cmd = HOST_STOP_REQ_CMD;
  240. req->reason = DRIVER_STOP_REQUEST;
  241. ishtp_write_message(dev, ishtp_hdr, data);
  242. }
  243. /**
  244. * ishtp_hbm_cl_flow_control_req() - Send flow control request
  245. * @dev: ISHTP device instance
  246. * @cl: ISHTP client instance
  247. *
  248. * Send flow control request
  249. *
  250. * Return: 0 if success else error code
  251. */
  252. int ishtp_hbm_cl_flow_control_req(struct ishtp_device *dev,
  253. struct ishtp_cl *cl)
  254. {
  255. struct ishtp_msg_hdr hdr;
  256. unsigned char data[128];
  257. struct ishtp_msg_hdr *ishtp_hdr = &hdr;
  258. const size_t len = sizeof(struct hbm_flow_control);
  259. int rv;
  260. unsigned int num_frags;
  261. unsigned long flags;
  262. spin_lock_irqsave(&cl->fc_spinlock, flags);
  263. ishtp_hbm_hdr(ishtp_hdr, len);
  264. ishtp_hbm_cl_hdr(cl, ISHTP_FLOW_CONTROL_CMD, data, len);
  265. /*
  266. * Sync possible race when RB recycle and packet receive paths
  267. * both try to send an out FC
  268. */
  269. if (cl->out_flow_ctrl_creds) {
  270. spin_unlock_irqrestore(&cl->fc_spinlock, flags);
  271. return 0;
  272. }
  273. num_frags = cl->recv_msg_num_frags;
  274. cl->recv_msg_num_frags = 0;
  275. rv = ishtp_write_message(dev, ishtp_hdr, data);
  276. if (!rv) {
  277. ++cl->out_flow_ctrl_creds;
  278. ++cl->out_flow_ctrl_cnt;
  279. getnstimeofday(&cl->ts_out_fc);
  280. if (cl->ts_rx.tv_sec && cl->ts_rx.tv_nsec) {
  281. struct timespec ts_diff;
  282. ts_diff = timespec_sub(cl->ts_out_fc, cl->ts_rx);
  283. if (timespec_compare(&ts_diff, &cl->ts_max_fc_delay)
  284. > 0)
  285. cl->ts_max_fc_delay = ts_diff;
  286. }
  287. } else {
  288. ++cl->err_send_fc;
  289. }
  290. spin_unlock_irqrestore(&cl->fc_spinlock, flags);
  291. return rv;
  292. }
  293. /**
  294. * ishtp_hbm_cl_disconnect_req() - Send disconnect request
  295. * @dev: ISHTP device instance
  296. * @cl: ISHTP client instance
  297. *
  298. * Send disconnect message to fw
  299. *
  300. * Return: 0 if success else error code
  301. */
  302. int ishtp_hbm_cl_disconnect_req(struct ishtp_device *dev, struct ishtp_cl *cl)
  303. {
  304. struct ishtp_msg_hdr hdr;
  305. unsigned char data[128];
  306. struct ishtp_msg_hdr *ishtp_hdr = &hdr;
  307. const size_t len = sizeof(struct hbm_client_connect_request);
  308. ishtp_hbm_hdr(ishtp_hdr, len);
  309. ishtp_hbm_cl_hdr(cl, CLIENT_DISCONNECT_REQ_CMD, data, len);
  310. return ishtp_write_message(dev, ishtp_hdr, data);
  311. }
  312. /**
  313. * ishtp_hbm_cl_disconnect_res() - Get disconnect response
  314. * @dev: ISHTP device instance
  315. * @rs: Response message
  316. *
  317. * Received disconnect response from fw
  318. */
  319. static void ishtp_hbm_cl_disconnect_res(struct ishtp_device *dev,
  320. struct hbm_client_connect_response *rs)
  321. {
  322. struct ishtp_cl *cl = NULL;
  323. unsigned long flags;
  324. spin_lock_irqsave(&dev->cl_list_lock, flags);
  325. list_for_each_entry(cl, &dev->cl_list, link) {
  326. if (!rs->status && ishtp_hbm_cl_addr_equal(cl, rs)) {
  327. cl->state = ISHTP_CL_DISCONNECTED;
  328. wake_up_interruptible(&cl->wait_ctrl_res);
  329. break;
  330. }
  331. }
  332. spin_unlock_irqrestore(&dev->cl_list_lock, flags);
  333. }
  334. /**
  335. * ishtp_hbm_cl_connect_req() - Send connect request
  336. * @dev: ISHTP device instance
  337. * @cl: client device instance
  338. *
  339. * Send connection request to specific fw client
  340. *
  341. * Return: 0 if success else error code
  342. */
  343. int ishtp_hbm_cl_connect_req(struct ishtp_device *dev, struct ishtp_cl *cl)
  344. {
  345. struct ishtp_msg_hdr hdr;
  346. unsigned char data[128];
  347. struct ishtp_msg_hdr *ishtp_hdr = &hdr;
  348. const size_t len = sizeof(struct hbm_client_connect_request);
  349. ishtp_hbm_hdr(ishtp_hdr, len);
  350. ishtp_hbm_cl_hdr(cl, CLIENT_CONNECT_REQ_CMD, data, len);
  351. return ishtp_write_message(dev, ishtp_hdr, data);
  352. }
  353. /**
  354. * ishtp_hbm_cl_connect_res() - Get connect response
  355. * @dev: ISHTP device instance
  356. * @rs: Response message
  357. *
  358. * Received connect response from fw
  359. */
  360. static void ishtp_hbm_cl_connect_res(struct ishtp_device *dev,
  361. struct hbm_client_connect_response *rs)
  362. {
  363. struct ishtp_cl *cl = NULL;
  364. unsigned long flags;
  365. spin_lock_irqsave(&dev->cl_list_lock, flags);
  366. list_for_each_entry(cl, &dev->cl_list, link) {
  367. if (ishtp_hbm_cl_addr_equal(cl, rs)) {
  368. if (!rs->status) {
  369. cl->state = ISHTP_CL_CONNECTED;
  370. cl->status = 0;
  371. } else {
  372. cl->state = ISHTP_CL_DISCONNECTED;
  373. cl->status = -ENODEV;
  374. }
  375. wake_up_interruptible(&cl->wait_ctrl_res);
  376. break;
  377. }
  378. }
  379. spin_unlock_irqrestore(&dev->cl_list_lock, flags);
  380. }
  381. /**
  382. * ishtp_client_disconnect_request() - Receive disconnect request
  383. * @dev: ISHTP device instance
  384. * @disconnect_req: disconnect request structure
  385. *
  386. * Disconnect request bus message from the fw. Send diconnect response.
  387. */
  388. static void ishtp_hbm_fw_disconnect_req(struct ishtp_device *dev,
  389. struct hbm_client_connect_request *disconnect_req)
  390. {
  391. struct ishtp_cl *cl;
  392. const size_t len = sizeof(struct hbm_client_connect_response);
  393. unsigned long flags;
  394. struct ishtp_msg_hdr hdr;
  395. unsigned char data[4]; /* All HBM messages are 4 bytes */
  396. spin_lock_irqsave(&dev->cl_list_lock, flags);
  397. list_for_each_entry(cl, &dev->cl_list, link) {
  398. if (ishtp_hbm_cl_addr_equal(cl, disconnect_req)) {
  399. cl->state = ISHTP_CL_DISCONNECTED;
  400. /* send disconnect response */
  401. ishtp_hbm_hdr(&hdr, len);
  402. ishtp_hbm_cl_hdr(cl, CLIENT_DISCONNECT_RES_CMD, data,
  403. len);
  404. ishtp_write_message(dev, &hdr, data);
  405. break;
  406. }
  407. }
  408. spin_unlock_irqrestore(&dev->cl_list_lock, flags);
  409. }
  410. /**
  411. * ishtp_hbm_dma_xfer_ack(() - Receive transfer ACK
  412. * @dev: ISHTP device instance
  413. * @dma_xfer: HBM transfer message
  414. *
  415. * Receive ack for ISHTP-over-DMA client message
  416. */
  417. static void ishtp_hbm_dma_xfer_ack(struct ishtp_device *dev,
  418. struct dma_xfer_hbm *dma_xfer)
  419. {
  420. void *msg;
  421. uint64_t offs;
  422. struct ishtp_msg_hdr *ishtp_hdr =
  423. (struct ishtp_msg_hdr *)&dev->ishtp_msg_hdr;
  424. unsigned int msg_offs;
  425. struct ishtp_cl *cl;
  426. for (msg_offs = 0; msg_offs < ishtp_hdr->length;
  427. msg_offs += sizeof(struct dma_xfer_hbm)) {
  428. offs = dma_xfer->msg_addr - dev->ishtp_host_dma_tx_buf_phys;
  429. if (offs > dev->ishtp_host_dma_tx_buf_size) {
  430. dev_err(dev->devc, "Bad DMA Tx ack message address\n");
  431. return;
  432. }
  433. if (dma_xfer->msg_length >
  434. dev->ishtp_host_dma_tx_buf_size - offs) {
  435. dev_err(dev->devc, "Bad DMA Tx ack message size\n");
  436. return;
  437. }
  438. /* logical address of the acked mem */
  439. msg = (unsigned char *)dev->ishtp_host_dma_tx_buf + offs;
  440. ishtp_cl_release_dma_acked_mem(dev, msg, dma_xfer->msg_length);
  441. list_for_each_entry(cl, &dev->cl_list, link) {
  442. if (cl->fw_client_id == dma_xfer->fw_client_id &&
  443. cl->host_client_id == dma_xfer->host_client_id)
  444. /*
  445. * in case that a single ack may be sent
  446. * over several dma transfers, and the last msg
  447. * addr was inside the acked memory, but not in
  448. * its start
  449. */
  450. if (cl->last_dma_addr >=
  451. (unsigned char *)msg &&
  452. cl->last_dma_addr <
  453. (unsigned char *)msg +
  454. dma_xfer->msg_length) {
  455. cl->last_dma_acked = 1;
  456. if (!list_empty(&cl->tx_list.list) &&
  457. cl->ishtp_flow_ctrl_creds) {
  458. /*
  459. * start sending the first msg
  460. */
  461. ishtp_cl_send_msg(dev, cl);
  462. }
  463. }
  464. }
  465. ++dma_xfer;
  466. }
  467. }
  468. /**
  469. * ishtp_hbm_dma_xfer() - Receive DMA transfer message
  470. * @dev: ISHTP device instance
  471. * @dma_xfer: HBM transfer message
  472. *
  473. * Receive ISHTP-over-DMA client message
  474. */
  475. static void ishtp_hbm_dma_xfer(struct ishtp_device *dev,
  476. struct dma_xfer_hbm *dma_xfer)
  477. {
  478. void *msg;
  479. uint64_t offs;
  480. struct ishtp_msg_hdr hdr;
  481. struct ishtp_msg_hdr *ishtp_hdr =
  482. (struct ishtp_msg_hdr *) &dev->ishtp_msg_hdr;
  483. struct dma_xfer_hbm *prm = dma_xfer;
  484. unsigned int msg_offs;
  485. for (msg_offs = 0; msg_offs < ishtp_hdr->length;
  486. msg_offs += sizeof(struct dma_xfer_hbm)) {
  487. offs = dma_xfer->msg_addr - dev->ishtp_host_dma_rx_buf_phys;
  488. if (offs > dev->ishtp_host_dma_rx_buf_size) {
  489. dev_err(dev->devc, "Bad DMA Rx message address\n");
  490. return;
  491. }
  492. if (dma_xfer->msg_length >
  493. dev->ishtp_host_dma_rx_buf_size - offs) {
  494. dev_err(dev->devc, "Bad DMA Rx message size\n");
  495. return;
  496. }
  497. msg = dev->ishtp_host_dma_rx_buf + offs;
  498. recv_ishtp_cl_msg_dma(dev, msg, dma_xfer);
  499. dma_xfer->hbm = DMA_XFER_ACK; /* Prepare for response */
  500. ++dma_xfer;
  501. }
  502. /* Send DMA_XFER_ACK [...] */
  503. ishtp_hbm_hdr(&hdr, ishtp_hdr->length);
  504. ishtp_write_message(dev, &hdr, (unsigned char *)prm);
  505. }
  506. /**
  507. * ishtp_hbm_dispatch() - HBM dispatch function
  508. * @dev: ISHTP device instance
  509. * @hdr: bus message
  510. *
  511. * Bottom half read routine after ISR to handle the read bus message cmd
  512. * processing
  513. */
  514. void ishtp_hbm_dispatch(struct ishtp_device *dev,
  515. struct ishtp_bus_message *hdr)
  516. {
  517. struct ishtp_bus_message *ishtp_msg;
  518. struct ishtp_fw_client *fw_client;
  519. struct hbm_host_version_response *version_res;
  520. struct hbm_client_connect_response *connect_res;
  521. struct hbm_client_connect_response *disconnect_res;
  522. struct hbm_client_connect_request *disconnect_req;
  523. struct hbm_props_response *props_res;
  524. struct hbm_host_enum_response *enum_res;
  525. struct ishtp_msg_hdr ishtp_hdr;
  526. struct dma_alloc_notify dma_alloc_notify;
  527. struct dma_xfer_hbm *dma_xfer;
  528. ishtp_msg = hdr;
  529. switch (ishtp_msg->hbm_cmd) {
  530. case HOST_START_RES_CMD:
  531. version_res = (struct hbm_host_version_response *)ishtp_msg;
  532. if (!version_res->host_version_supported) {
  533. dev->version = version_res->fw_max_version;
  534. dev->hbm_state = ISHTP_HBM_STOPPED;
  535. ishtp_hbm_stop_req(dev);
  536. return;
  537. }
  538. dev->version.major_version = HBM_MAJOR_VERSION;
  539. dev->version.minor_version = HBM_MINOR_VERSION;
  540. if (dev->dev_state == ISHTP_DEV_INIT_CLIENTS &&
  541. dev->hbm_state == ISHTP_HBM_START) {
  542. dev->hbm_state = ISHTP_HBM_STARTED;
  543. ishtp_hbm_enum_clients_req(dev);
  544. } else {
  545. dev_err(dev->devc,
  546. "reset: wrong host start response\n");
  547. /* BUG: why do we arrive here? */
  548. ish_hw_reset(dev);
  549. return;
  550. }
  551. wake_up_interruptible(&dev->wait_hbm_recvd_msg);
  552. break;
  553. case CLIENT_CONNECT_RES_CMD:
  554. connect_res = (struct hbm_client_connect_response *)ishtp_msg;
  555. ishtp_hbm_cl_connect_res(dev, connect_res);
  556. break;
  557. case CLIENT_DISCONNECT_RES_CMD:
  558. disconnect_res =
  559. (struct hbm_client_connect_response *)ishtp_msg;
  560. ishtp_hbm_cl_disconnect_res(dev, disconnect_res);
  561. break;
  562. case HOST_CLIENT_PROPERTIES_RES_CMD:
  563. props_res = (struct hbm_props_response *)ishtp_msg;
  564. fw_client = &dev->fw_clients[dev->fw_client_presentation_num];
  565. if (props_res->status || !dev->fw_clients) {
  566. dev_err(dev->devc,
  567. "reset: properties response hbm wrong status\n");
  568. ish_hw_reset(dev);
  569. return;
  570. }
  571. if (fw_client->client_id != props_res->address) {
  572. dev_err(dev->devc,
  573. "reset: host properties response address mismatch [%02X %02X]\n",
  574. fw_client->client_id, props_res->address);
  575. ish_hw_reset(dev);
  576. return;
  577. }
  578. if (dev->dev_state != ISHTP_DEV_INIT_CLIENTS ||
  579. dev->hbm_state != ISHTP_HBM_CLIENT_PROPERTIES) {
  580. dev_err(dev->devc,
  581. "reset: unexpected properties response\n");
  582. ish_hw_reset(dev);
  583. return;
  584. }
  585. fw_client->props = props_res->client_properties;
  586. dev->fw_client_index++;
  587. dev->fw_client_presentation_num++;
  588. /* request property for the next client */
  589. ishtp_hbm_prop_req(dev);
  590. if (dev->dev_state != ISHTP_DEV_ENABLED)
  591. break;
  592. if (!ishtp_use_dma_transfer())
  593. break;
  594. dev_dbg(dev->devc, "Requesting to use DMA\n");
  595. ishtp_cl_alloc_dma_buf(dev);
  596. if (dev->ishtp_host_dma_rx_buf) {
  597. const size_t len = sizeof(dma_alloc_notify);
  598. memset(&dma_alloc_notify, 0, sizeof(dma_alloc_notify));
  599. dma_alloc_notify.hbm = DMA_BUFFER_ALLOC_NOTIFY;
  600. dma_alloc_notify.buf_size =
  601. dev->ishtp_host_dma_rx_buf_size;
  602. dma_alloc_notify.buf_address =
  603. dev->ishtp_host_dma_rx_buf_phys;
  604. ishtp_hbm_hdr(&ishtp_hdr, len);
  605. ishtp_write_message(dev, &ishtp_hdr,
  606. (unsigned char *)&dma_alloc_notify);
  607. }
  608. break;
  609. case HOST_ENUM_RES_CMD:
  610. enum_res = (struct hbm_host_enum_response *) ishtp_msg;
  611. memcpy(dev->fw_clients_map, enum_res->valid_addresses, 32);
  612. if (dev->dev_state == ISHTP_DEV_INIT_CLIENTS &&
  613. dev->hbm_state == ISHTP_HBM_ENUM_CLIENTS) {
  614. dev->fw_client_presentation_num = 0;
  615. dev->fw_client_index = 0;
  616. ishtp_hbm_fw_cl_allocate(dev);
  617. dev->hbm_state = ISHTP_HBM_CLIENT_PROPERTIES;
  618. /* first property request */
  619. ishtp_hbm_prop_req(dev);
  620. } else {
  621. dev_err(dev->devc,
  622. "reset: unexpected enumeration response hbm\n");
  623. ish_hw_reset(dev);
  624. return;
  625. }
  626. break;
  627. case HOST_STOP_RES_CMD:
  628. if (dev->hbm_state != ISHTP_HBM_STOPPED)
  629. dev_err(dev->devc, "unexpected stop response\n");
  630. dev->dev_state = ISHTP_DEV_DISABLED;
  631. dev_info(dev->devc, "reset: FW stop response\n");
  632. ish_hw_reset(dev);
  633. break;
  634. case CLIENT_DISCONNECT_REQ_CMD:
  635. /* search for client */
  636. disconnect_req =
  637. (struct hbm_client_connect_request *)ishtp_msg;
  638. ishtp_hbm_fw_disconnect_req(dev, disconnect_req);
  639. break;
  640. case FW_STOP_REQ_CMD:
  641. dev->hbm_state = ISHTP_HBM_STOPPED;
  642. break;
  643. case DMA_BUFFER_ALLOC_RESPONSE:
  644. dev->ishtp_host_dma_enabled = 1;
  645. break;
  646. case DMA_XFER:
  647. dma_xfer = (struct dma_xfer_hbm *)ishtp_msg;
  648. if (!dev->ishtp_host_dma_enabled) {
  649. dev_err(dev->devc,
  650. "DMA XFER requested but DMA is not enabled\n");
  651. break;
  652. }
  653. ishtp_hbm_dma_xfer(dev, dma_xfer);
  654. break;
  655. case DMA_XFER_ACK:
  656. dma_xfer = (struct dma_xfer_hbm *)ishtp_msg;
  657. if (!dev->ishtp_host_dma_enabled ||
  658. !dev->ishtp_host_dma_tx_buf) {
  659. dev_err(dev->devc,
  660. "DMA XFER acked but DMA Tx is not enabled\n");
  661. break;
  662. }
  663. ishtp_hbm_dma_xfer_ack(dev, dma_xfer);
  664. break;
  665. default:
  666. dev_err(dev->devc, "unknown HBM: %u\n",
  667. (unsigned int)ishtp_msg->hbm_cmd);
  668. break;
  669. }
  670. }
  671. /**
  672. * bh_hbm_work_fn() - HBM work function
  673. * @work: work struct
  674. *
  675. * Bottom half processing work function (instead of thread handler)
  676. * for processing hbm messages
  677. */
  678. void bh_hbm_work_fn(struct work_struct *work)
  679. {
  680. unsigned long flags;
  681. struct ishtp_device *dev;
  682. unsigned char hbm[IPC_PAYLOAD_SIZE];
  683. dev = container_of(work, struct ishtp_device, bh_hbm_work);
  684. spin_lock_irqsave(&dev->rd_msg_spinlock, flags);
  685. if (dev->rd_msg_fifo_head != dev->rd_msg_fifo_tail) {
  686. memcpy(hbm, dev->rd_msg_fifo + dev->rd_msg_fifo_head,
  687. IPC_PAYLOAD_SIZE);
  688. dev->rd_msg_fifo_head =
  689. (dev->rd_msg_fifo_head + IPC_PAYLOAD_SIZE) %
  690. (RD_INT_FIFO_SIZE * IPC_PAYLOAD_SIZE);
  691. spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
  692. ishtp_hbm_dispatch(dev, (struct ishtp_bus_message *)hbm);
  693. } else {
  694. spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
  695. }
  696. }
  697. /**
  698. * recv_hbm() - Receive HBM message
  699. * @dev: ISHTP device instance
  700. * @ishtp_hdr: received bus message
  701. *
  702. * Receive and process ISHTP bus messages in ISR context. This will schedule
  703. * work function to process message
  704. */
  705. void recv_hbm(struct ishtp_device *dev, struct ishtp_msg_hdr *ishtp_hdr)
  706. {
  707. uint8_t rd_msg_buf[ISHTP_RD_MSG_BUF_SIZE];
  708. struct ishtp_bus_message *ishtp_msg =
  709. (struct ishtp_bus_message *)rd_msg_buf;
  710. unsigned long flags;
  711. dev->ops->ishtp_read(dev, rd_msg_buf, ishtp_hdr->length);
  712. /* Flow control - handle in place */
  713. if (ishtp_msg->hbm_cmd == ISHTP_FLOW_CONTROL_CMD) {
  714. struct hbm_flow_control *flow_control =
  715. (struct hbm_flow_control *)ishtp_msg;
  716. struct ishtp_cl *cl = NULL;
  717. unsigned long flags, tx_flags;
  718. spin_lock_irqsave(&dev->cl_list_lock, flags);
  719. list_for_each_entry(cl, &dev->cl_list, link) {
  720. if (cl->host_client_id == flow_control->host_addr &&
  721. cl->fw_client_id ==
  722. flow_control->fw_addr) {
  723. /*
  724. * NOTE: It's valid only for counting
  725. * flow-control implementation to receive a
  726. * FC in the middle of sending. Meanwhile not
  727. * supported
  728. */
  729. if (cl->ishtp_flow_ctrl_creds)
  730. dev_err(dev->devc,
  731. "recv extra FC from FW client %u (host client %u) (FC count was %d)\n",
  732. (unsigned int)cl->fw_client_id,
  733. (unsigned int)cl->host_client_id,
  734. cl->ishtp_flow_ctrl_creds);
  735. else {
  736. ++cl->ishtp_flow_ctrl_creds;
  737. ++cl->ishtp_flow_ctrl_cnt;
  738. cl->last_ipc_acked = 1;
  739. spin_lock_irqsave(
  740. &cl->tx_list_spinlock,
  741. tx_flags);
  742. if (!list_empty(&cl->tx_list.list)) {
  743. /*
  744. * start sending the first msg
  745. * = the callback function
  746. */
  747. spin_unlock_irqrestore(
  748. &cl->tx_list_spinlock,
  749. tx_flags);
  750. ishtp_cl_send_msg(dev, cl);
  751. } else {
  752. spin_unlock_irqrestore(
  753. &cl->tx_list_spinlock,
  754. tx_flags);
  755. }
  756. }
  757. break;
  758. }
  759. }
  760. spin_unlock_irqrestore(&dev->cl_list_lock, flags);
  761. goto eoi;
  762. }
  763. /*
  764. * Some messages that are safe for ISR processing and important
  765. * to be done "quickly" and in-order, go here
  766. */
  767. if (ishtp_msg->hbm_cmd == CLIENT_CONNECT_RES_CMD ||
  768. ishtp_msg->hbm_cmd == CLIENT_DISCONNECT_RES_CMD ||
  769. ishtp_msg->hbm_cmd == CLIENT_DISCONNECT_REQ_CMD ||
  770. ishtp_msg->hbm_cmd == DMA_XFER) {
  771. ishtp_hbm_dispatch(dev, ishtp_msg);
  772. goto eoi;
  773. }
  774. /*
  775. * All other HBMs go here.
  776. * We schedule HBMs for processing serially by using system wq,
  777. * possibly there will be multiple HBMs scheduled at the same time.
  778. */
  779. spin_lock_irqsave(&dev->rd_msg_spinlock, flags);
  780. if ((dev->rd_msg_fifo_tail + IPC_PAYLOAD_SIZE) %
  781. (RD_INT_FIFO_SIZE * IPC_PAYLOAD_SIZE) ==
  782. dev->rd_msg_fifo_head) {
  783. spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
  784. dev_err(dev->devc, "BH buffer overflow, dropping HBM %u\n",
  785. (unsigned int)ishtp_msg->hbm_cmd);
  786. goto eoi;
  787. }
  788. memcpy(dev->rd_msg_fifo + dev->rd_msg_fifo_tail, ishtp_msg,
  789. ishtp_hdr->length);
  790. dev->rd_msg_fifo_tail = (dev->rd_msg_fifo_tail + IPC_PAYLOAD_SIZE) %
  791. (RD_INT_FIFO_SIZE * IPC_PAYLOAD_SIZE);
  792. spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
  793. schedule_work(&dev->bh_hbm_work);
  794. eoi:
  795. return;
  796. }
  797. /**
  798. * recv_fixed_cl_msg() - Receive fixed client message
  799. * @dev: ISHTP device instance
  800. * @ishtp_hdr: received bus message
  801. *
  802. * Receive and process ISHTP fixed client messages (address == 0)
  803. * in ISR context
  804. */
  805. void recv_fixed_cl_msg(struct ishtp_device *dev,
  806. struct ishtp_msg_hdr *ishtp_hdr)
  807. {
  808. uint8_t rd_msg_buf[ISHTP_RD_MSG_BUF_SIZE];
  809. dev->print_log(dev,
  810. "%s() got fixed client msg from client #%d\n",
  811. __func__, ishtp_hdr->fw_addr);
  812. dev->ops->ishtp_read(dev, rd_msg_buf, ishtp_hdr->length);
  813. if (ishtp_hdr->fw_addr == ISHTP_SYSTEM_STATE_CLIENT_ADDR) {
  814. struct ish_system_states_header *msg_hdr =
  815. (struct ish_system_states_header *)rd_msg_buf;
  816. if (msg_hdr->cmd == SYSTEM_STATE_SUBSCRIBE)
  817. ishtp_send_resume(dev);
  818. /* if FW request arrived here, the system is not suspended */
  819. else
  820. dev_err(dev->devc, "unknown fixed client msg [%02X]\n",
  821. msg_hdr->cmd);
  822. }
  823. }
  824. /**
  825. * fix_cl_hdr() - Initialize fixed client header
  826. * @hdr: message header
  827. * @length: length of message
  828. * @cl_addr: Client address
  829. *
  830. * Initialize message header for fixed client
  831. */
  832. static inline void fix_cl_hdr(struct ishtp_msg_hdr *hdr, size_t length,
  833. uint8_t cl_addr)
  834. {
  835. hdr->host_addr = 0;
  836. hdr->fw_addr = cl_addr;
  837. hdr->length = length;
  838. hdr->msg_complete = 1;
  839. hdr->reserved = 0;
  840. }
  841. /*** Suspend and resume notification ***/
  842. static uint32_t current_state;
  843. static uint32_t supported_states = 0 | SUSPEND_STATE_BIT;
  844. /**
  845. * ishtp_send_suspend() - Send suspend message to FW
  846. * @dev: ISHTP device instance
  847. *
  848. * Send suspend message to FW. This is useful for system freeze (non S3) case
  849. */
  850. void ishtp_send_suspend(struct ishtp_device *dev)
  851. {
  852. struct ishtp_msg_hdr ishtp_hdr;
  853. struct ish_system_states_status state_status_msg;
  854. const size_t len = sizeof(struct ish_system_states_status);
  855. fix_cl_hdr(&ishtp_hdr, len, ISHTP_SYSTEM_STATE_CLIENT_ADDR);
  856. memset(&state_status_msg, 0, len);
  857. state_status_msg.hdr.cmd = SYSTEM_STATE_STATUS;
  858. state_status_msg.supported_states = supported_states;
  859. current_state |= SUSPEND_STATE_BIT;
  860. dev->print_log(dev, "%s() sends SUSPEND notification\n", __func__);
  861. state_status_msg.states_status = current_state;
  862. ishtp_write_message(dev, &ishtp_hdr,
  863. (unsigned char *)&state_status_msg);
  864. }
  865. EXPORT_SYMBOL(ishtp_send_suspend);
  866. /**
  867. * ishtp_send_resume() - Send resume message to FW
  868. * @dev: ISHTP device instance
  869. *
  870. * Send resume message to FW. This is useful for system freeze (non S3) case
  871. */
  872. void ishtp_send_resume(struct ishtp_device *dev)
  873. {
  874. struct ishtp_msg_hdr ishtp_hdr;
  875. struct ish_system_states_status state_status_msg;
  876. const size_t len = sizeof(struct ish_system_states_status);
  877. fix_cl_hdr(&ishtp_hdr, len, ISHTP_SYSTEM_STATE_CLIENT_ADDR);
  878. memset(&state_status_msg, 0, len);
  879. state_status_msg.hdr.cmd = SYSTEM_STATE_STATUS;
  880. state_status_msg.supported_states = supported_states;
  881. current_state &= ~SUSPEND_STATE_BIT;
  882. dev->print_log(dev, "%s() sends RESUME notification\n", __func__);
  883. state_status_msg.states_status = current_state;
  884. ishtp_write_message(dev, &ishtp_hdr,
  885. (unsigned char *)&state_status_msg);
  886. }
  887. EXPORT_SYMBOL(ishtp_send_resume);
  888. /**
  889. * ishtp_query_subscribers() - Send query subscribers message
  890. * @dev: ISHTP device instance
  891. *
  892. * Send message to query subscribers
  893. */
  894. void ishtp_query_subscribers(struct ishtp_device *dev)
  895. {
  896. struct ishtp_msg_hdr ishtp_hdr;
  897. struct ish_system_states_query_subscribers query_subscribers_msg;
  898. const size_t len = sizeof(struct ish_system_states_query_subscribers);
  899. fix_cl_hdr(&ishtp_hdr, len, ISHTP_SYSTEM_STATE_CLIENT_ADDR);
  900. memset(&query_subscribers_msg, 0, len);
  901. query_subscribers_msg.hdr.cmd = SYSTEM_STATE_QUERY_SUBSCRIBERS;
  902. ishtp_write_message(dev, &ishtp_hdr,
  903. (unsigned char *)&query_subscribers_msg);
  904. }