hbm.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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. break;
  329. }
  330. }
  331. if (cl)
  332. wake_up_interruptible(&cl->wait_ctrl_res);
  333. spin_unlock_irqrestore(&dev->cl_list_lock, flags);
  334. }
  335. /**
  336. * ishtp_hbm_cl_connect_req() - Send connect request
  337. * @dev: ISHTP device instance
  338. * @cl: client device instance
  339. *
  340. * Send connection request to specific fw client
  341. *
  342. * Return: 0 if success else error code
  343. */
  344. int ishtp_hbm_cl_connect_req(struct ishtp_device *dev, struct ishtp_cl *cl)
  345. {
  346. struct ishtp_msg_hdr hdr;
  347. unsigned char data[128];
  348. struct ishtp_msg_hdr *ishtp_hdr = &hdr;
  349. const size_t len = sizeof(struct hbm_client_connect_request);
  350. ishtp_hbm_hdr(ishtp_hdr, len);
  351. ishtp_hbm_cl_hdr(cl, CLIENT_CONNECT_REQ_CMD, data, len);
  352. return ishtp_write_message(dev, ishtp_hdr, data);
  353. }
  354. /**
  355. * ishtp_hbm_cl_connect_res() - Get connect response
  356. * @dev: ISHTP device instance
  357. * @rs: Response message
  358. *
  359. * Received connect response from fw
  360. */
  361. static void ishtp_hbm_cl_connect_res(struct ishtp_device *dev,
  362. struct hbm_client_connect_response *rs)
  363. {
  364. struct ishtp_cl *cl = NULL;
  365. unsigned long flags;
  366. spin_lock_irqsave(&dev->cl_list_lock, flags);
  367. list_for_each_entry(cl, &dev->cl_list, link) {
  368. if (ishtp_hbm_cl_addr_equal(cl, rs)) {
  369. if (!rs->status) {
  370. cl->state = ISHTP_CL_CONNECTED;
  371. cl->status = 0;
  372. } else {
  373. cl->state = ISHTP_CL_DISCONNECTED;
  374. cl->status = -ENODEV;
  375. }
  376. break;
  377. }
  378. }
  379. if (cl)
  380. wake_up_interruptible(&cl->wait_ctrl_res);
  381. spin_unlock_irqrestore(&dev->cl_list_lock, flags);
  382. }
  383. /**
  384. * ishtp_client_disconnect_request() - Receive disconnect request
  385. * @dev: ISHTP device instance
  386. * @disconnect_req: disconnect request structure
  387. *
  388. * Disconnect request bus message from the fw. Send diconnect response.
  389. */
  390. static void ishtp_hbm_fw_disconnect_req(struct ishtp_device *dev,
  391. struct hbm_client_connect_request *disconnect_req)
  392. {
  393. struct ishtp_cl *cl;
  394. const size_t len = sizeof(struct hbm_client_connect_response);
  395. unsigned long flags;
  396. struct ishtp_msg_hdr hdr;
  397. unsigned char data[4]; /* All HBM messages are 4 bytes */
  398. spin_lock_irqsave(&dev->cl_list_lock, flags);
  399. list_for_each_entry(cl, &dev->cl_list, link) {
  400. if (ishtp_hbm_cl_addr_equal(cl, disconnect_req)) {
  401. cl->state = ISHTP_CL_DISCONNECTED;
  402. /* send disconnect response */
  403. ishtp_hbm_hdr(&hdr, len);
  404. ishtp_hbm_cl_hdr(cl, CLIENT_DISCONNECT_RES_CMD, data,
  405. len);
  406. ishtp_write_message(dev, &hdr, data);
  407. break;
  408. }
  409. }
  410. spin_unlock_irqrestore(&dev->cl_list_lock, flags);
  411. }
  412. /**
  413. * ishtp_hbm_dma_xfer_ack(() - Receive transfer ACK
  414. * @dev: ISHTP device instance
  415. * @dma_xfer: HBM transfer message
  416. *
  417. * Receive ack for ISHTP-over-DMA client message
  418. */
  419. static void ishtp_hbm_dma_xfer_ack(struct ishtp_device *dev,
  420. struct dma_xfer_hbm *dma_xfer)
  421. {
  422. void *msg;
  423. uint64_t offs;
  424. struct ishtp_msg_hdr *ishtp_hdr =
  425. (struct ishtp_msg_hdr *)&dev->ishtp_msg_hdr;
  426. unsigned int msg_offs;
  427. struct ishtp_cl *cl;
  428. for (msg_offs = 0; msg_offs < ishtp_hdr->length;
  429. msg_offs += sizeof(struct dma_xfer_hbm)) {
  430. offs = dma_xfer->msg_addr - dev->ishtp_host_dma_tx_buf_phys;
  431. if (offs > dev->ishtp_host_dma_tx_buf_size) {
  432. dev_err(dev->devc, "Bad DMA Tx ack message address\n");
  433. return;
  434. }
  435. if (dma_xfer->msg_length >
  436. dev->ishtp_host_dma_tx_buf_size - offs) {
  437. dev_err(dev->devc, "Bad DMA Tx ack message size\n");
  438. return;
  439. }
  440. /* logical address of the acked mem */
  441. msg = (unsigned char *)dev->ishtp_host_dma_tx_buf + offs;
  442. ishtp_cl_release_dma_acked_mem(dev, msg, dma_xfer->msg_length);
  443. list_for_each_entry(cl, &dev->cl_list, link) {
  444. if (cl->fw_client_id == dma_xfer->fw_client_id &&
  445. cl->host_client_id == dma_xfer->host_client_id)
  446. /*
  447. * in case that a single ack may be sent
  448. * over several dma transfers, and the last msg
  449. * addr was inside the acked memory, but not in
  450. * its start
  451. */
  452. if (cl->last_dma_addr >=
  453. (unsigned char *)msg &&
  454. cl->last_dma_addr <
  455. (unsigned char *)msg +
  456. dma_xfer->msg_length) {
  457. cl->last_dma_acked = 1;
  458. if (!list_empty(&cl->tx_list.list) &&
  459. cl->ishtp_flow_ctrl_creds) {
  460. /*
  461. * start sending the first msg
  462. */
  463. ishtp_cl_send_msg(dev, cl);
  464. }
  465. }
  466. }
  467. ++dma_xfer;
  468. }
  469. }
  470. /**
  471. * ishtp_hbm_dma_xfer() - Receive DMA transfer message
  472. * @dev: ISHTP device instance
  473. * @dma_xfer: HBM transfer message
  474. *
  475. * Receive ISHTP-over-DMA client message
  476. */
  477. static void ishtp_hbm_dma_xfer(struct ishtp_device *dev,
  478. struct dma_xfer_hbm *dma_xfer)
  479. {
  480. void *msg;
  481. uint64_t offs;
  482. struct ishtp_msg_hdr hdr;
  483. struct ishtp_msg_hdr *ishtp_hdr =
  484. (struct ishtp_msg_hdr *) &dev->ishtp_msg_hdr;
  485. struct dma_xfer_hbm *prm = dma_xfer;
  486. unsigned int msg_offs;
  487. for (msg_offs = 0; msg_offs < ishtp_hdr->length;
  488. msg_offs += sizeof(struct dma_xfer_hbm)) {
  489. offs = dma_xfer->msg_addr - dev->ishtp_host_dma_rx_buf_phys;
  490. if (offs > dev->ishtp_host_dma_rx_buf_size) {
  491. dev_err(dev->devc, "Bad DMA Rx message address\n");
  492. return;
  493. }
  494. if (dma_xfer->msg_length >
  495. dev->ishtp_host_dma_rx_buf_size - offs) {
  496. dev_err(dev->devc, "Bad DMA Rx message size\n");
  497. return;
  498. }
  499. msg = dev->ishtp_host_dma_rx_buf + offs;
  500. recv_ishtp_cl_msg_dma(dev, msg, dma_xfer);
  501. dma_xfer->hbm = DMA_XFER_ACK; /* Prepare for response */
  502. ++dma_xfer;
  503. }
  504. /* Send DMA_XFER_ACK [...] */
  505. ishtp_hbm_hdr(&hdr, ishtp_hdr->length);
  506. ishtp_write_message(dev, &hdr, (unsigned char *)prm);
  507. }
  508. /**
  509. * ishtp_hbm_dispatch() - HBM dispatch function
  510. * @dev: ISHTP device instance
  511. * @hdr: bus message
  512. *
  513. * Bottom half read routine after ISR to handle the read bus message cmd
  514. * processing
  515. */
  516. void ishtp_hbm_dispatch(struct ishtp_device *dev,
  517. struct ishtp_bus_message *hdr)
  518. {
  519. struct ishtp_bus_message *ishtp_msg;
  520. struct ishtp_fw_client *fw_client;
  521. struct hbm_host_version_response *version_res;
  522. struct hbm_client_connect_response *connect_res;
  523. struct hbm_client_connect_response *disconnect_res;
  524. struct hbm_client_connect_request *disconnect_req;
  525. struct hbm_props_response *props_res;
  526. struct hbm_host_enum_response *enum_res;
  527. struct ishtp_msg_hdr ishtp_hdr;
  528. struct dma_alloc_notify dma_alloc_notify;
  529. struct dma_xfer_hbm *dma_xfer;
  530. ishtp_msg = hdr;
  531. switch (ishtp_msg->hbm_cmd) {
  532. case HOST_START_RES_CMD:
  533. version_res = (struct hbm_host_version_response *)ishtp_msg;
  534. if (!version_res->host_version_supported) {
  535. dev->version = version_res->fw_max_version;
  536. dev->hbm_state = ISHTP_HBM_STOPPED;
  537. ishtp_hbm_stop_req(dev);
  538. return;
  539. }
  540. dev->version.major_version = HBM_MAJOR_VERSION;
  541. dev->version.minor_version = HBM_MINOR_VERSION;
  542. if (dev->dev_state == ISHTP_DEV_INIT_CLIENTS &&
  543. dev->hbm_state == ISHTP_HBM_START) {
  544. dev->hbm_state = ISHTP_HBM_STARTED;
  545. ishtp_hbm_enum_clients_req(dev);
  546. } else {
  547. dev_err(dev->devc,
  548. "reset: wrong host start response\n");
  549. /* BUG: why do we arrive here? */
  550. ish_hw_reset(dev);
  551. return;
  552. }
  553. wake_up_interruptible(&dev->wait_hbm_recvd_msg);
  554. break;
  555. case CLIENT_CONNECT_RES_CMD:
  556. connect_res = (struct hbm_client_connect_response *)ishtp_msg;
  557. ishtp_hbm_cl_connect_res(dev, connect_res);
  558. break;
  559. case CLIENT_DISCONNECT_RES_CMD:
  560. disconnect_res =
  561. (struct hbm_client_connect_response *)ishtp_msg;
  562. ishtp_hbm_cl_disconnect_res(dev, disconnect_res);
  563. break;
  564. case HOST_CLIENT_PROPERTIES_RES_CMD:
  565. props_res = (struct hbm_props_response *)ishtp_msg;
  566. fw_client = &dev->fw_clients[dev->fw_client_presentation_num];
  567. if (props_res->status || !dev->fw_clients) {
  568. dev_err(dev->devc,
  569. "reset: properties response hbm wrong status\n");
  570. ish_hw_reset(dev);
  571. return;
  572. }
  573. if (fw_client->client_id != props_res->address) {
  574. dev_err(dev->devc,
  575. "reset: host properties response address mismatch [%02X %02X]\n",
  576. fw_client->client_id, props_res->address);
  577. ish_hw_reset(dev);
  578. return;
  579. }
  580. if (dev->dev_state != ISHTP_DEV_INIT_CLIENTS ||
  581. dev->hbm_state != ISHTP_HBM_CLIENT_PROPERTIES) {
  582. dev_err(dev->devc,
  583. "reset: unexpected properties response\n");
  584. ish_hw_reset(dev);
  585. return;
  586. }
  587. fw_client->props = props_res->client_properties;
  588. dev->fw_client_index++;
  589. dev->fw_client_presentation_num++;
  590. /* request property for the next client */
  591. ishtp_hbm_prop_req(dev);
  592. if (dev->dev_state != ISHTP_DEV_ENABLED)
  593. break;
  594. if (!ishtp_use_dma_transfer())
  595. break;
  596. dev_dbg(dev->devc, "Requesting to use DMA\n");
  597. ishtp_cl_alloc_dma_buf(dev);
  598. if (dev->ishtp_host_dma_rx_buf) {
  599. const size_t len = sizeof(dma_alloc_notify);
  600. memset(&dma_alloc_notify, 0, sizeof(dma_alloc_notify));
  601. dma_alloc_notify.hbm = DMA_BUFFER_ALLOC_NOTIFY;
  602. dma_alloc_notify.buf_size =
  603. dev->ishtp_host_dma_rx_buf_size;
  604. dma_alloc_notify.buf_address =
  605. dev->ishtp_host_dma_rx_buf_phys;
  606. ishtp_hbm_hdr(&ishtp_hdr, len);
  607. ishtp_write_message(dev, &ishtp_hdr,
  608. (unsigned char *)&dma_alloc_notify);
  609. }
  610. break;
  611. case HOST_ENUM_RES_CMD:
  612. enum_res = (struct hbm_host_enum_response *) ishtp_msg;
  613. memcpy(dev->fw_clients_map, enum_res->valid_addresses, 32);
  614. if (dev->dev_state == ISHTP_DEV_INIT_CLIENTS &&
  615. dev->hbm_state == ISHTP_HBM_ENUM_CLIENTS) {
  616. dev->fw_client_presentation_num = 0;
  617. dev->fw_client_index = 0;
  618. ishtp_hbm_fw_cl_allocate(dev);
  619. dev->hbm_state = ISHTP_HBM_CLIENT_PROPERTIES;
  620. /* first property request */
  621. ishtp_hbm_prop_req(dev);
  622. } else {
  623. dev_err(dev->devc,
  624. "reset: unexpected enumeration response hbm\n");
  625. ish_hw_reset(dev);
  626. return;
  627. }
  628. break;
  629. case HOST_STOP_RES_CMD:
  630. if (dev->hbm_state != ISHTP_HBM_STOPPED)
  631. dev_err(dev->devc, "unexpected stop response\n");
  632. dev->dev_state = ISHTP_DEV_DISABLED;
  633. dev_info(dev->devc, "reset: FW stop response\n");
  634. ish_hw_reset(dev);
  635. break;
  636. case CLIENT_DISCONNECT_REQ_CMD:
  637. /* search for client */
  638. disconnect_req =
  639. (struct hbm_client_connect_request *)ishtp_msg;
  640. ishtp_hbm_fw_disconnect_req(dev, disconnect_req);
  641. break;
  642. case FW_STOP_REQ_CMD:
  643. dev->hbm_state = ISHTP_HBM_STOPPED;
  644. break;
  645. case DMA_BUFFER_ALLOC_RESPONSE:
  646. dev->ishtp_host_dma_enabled = 1;
  647. break;
  648. case DMA_XFER:
  649. dma_xfer = (struct dma_xfer_hbm *)ishtp_msg;
  650. if (!dev->ishtp_host_dma_enabled) {
  651. dev_err(dev->devc,
  652. "DMA XFER requested but DMA is not enabled\n");
  653. break;
  654. }
  655. ishtp_hbm_dma_xfer(dev, dma_xfer);
  656. break;
  657. case DMA_XFER_ACK:
  658. dma_xfer = (struct dma_xfer_hbm *)ishtp_msg;
  659. if (!dev->ishtp_host_dma_enabled ||
  660. !dev->ishtp_host_dma_tx_buf) {
  661. dev_err(dev->devc,
  662. "DMA XFER acked but DMA Tx is not enabled\n");
  663. break;
  664. }
  665. ishtp_hbm_dma_xfer_ack(dev, dma_xfer);
  666. break;
  667. default:
  668. dev_err(dev->devc, "unknown HBM: %u\n",
  669. (unsigned int)ishtp_msg->hbm_cmd);
  670. break;
  671. }
  672. }
  673. /**
  674. * bh_hbm_work_fn() - HBM work function
  675. * @work: work struct
  676. *
  677. * Bottom half processing work function (instead of thread handler)
  678. * for processing hbm messages
  679. */
  680. void bh_hbm_work_fn(struct work_struct *work)
  681. {
  682. unsigned long flags;
  683. struct ishtp_device *dev;
  684. unsigned char hbm[IPC_PAYLOAD_SIZE];
  685. dev = container_of(work, struct ishtp_device, bh_hbm_work);
  686. spin_lock_irqsave(&dev->rd_msg_spinlock, flags);
  687. if (dev->rd_msg_fifo_head != dev->rd_msg_fifo_tail) {
  688. memcpy(hbm, dev->rd_msg_fifo + dev->rd_msg_fifo_head,
  689. IPC_PAYLOAD_SIZE);
  690. dev->rd_msg_fifo_head =
  691. (dev->rd_msg_fifo_head + IPC_PAYLOAD_SIZE) %
  692. (RD_INT_FIFO_SIZE * IPC_PAYLOAD_SIZE);
  693. spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
  694. ishtp_hbm_dispatch(dev, (struct ishtp_bus_message *)hbm);
  695. } else {
  696. spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
  697. }
  698. }
  699. /**
  700. * recv_hbm() - Receive HBM message
  701. * @dev: ISHTP device instance
  702. * @ishtp_hdr: received bus message
  703. *
  704. * Receive and process ISHTP bus messages in ISR context. This will schedule
  705. * work function to process message
  706. */
  707. void recv_hbm(struct ishtp_device *dev, struct ishtp_msg_hdr *ishtp_hdr)
  708. {
  709. uint8_t rd_msg_buf[ISHTP_RD_MSG_BUF_SIZE];
  710. struct ishtp_bus_message *ishtp_msg =
  711. (struct ishtp_bus_message *)rd_msg_buf;
  712. unsigned long flags;
  713. dev->ops->ishtp_read(dev, rd_msg_buf, ishtp_hdr->length);
  714. /* Flow control - handle in place */
  715. if (ishtp_msg->hbm_cmd == ISHTP_FLOW_CONTROL_CMD) {
  716. struct hbm_flow_control *flow_control =
  717. (struct hbm_flow_control *)ishtp_msg;
  718. struct ishtp_cl *cl = NULL;
  719. unsigned long flags, tx_flags;
  720. spin_lock_irqsave(&dev->cl_list_lock, flags);
  721. list_for_each_entry(cl, &dev->cl_list, link) {
  722. if (cl->host_client_id == flow_control->host_addr &&
  723. cl->fw_client_id ==
  724. flow_control->fw_addr) {
  725. /*
  726. * NOTE: It's valid only for counting
  727. * flow-control implementation to receive a
  728. * FC in the middle of sending. Meanwhile not
  729. * supported
  730. */
  731. if (cl->ishtp_flow_ctrl_creds)
  732. dev_err(dev->devc,
  733. "recv extra FC from FW client %u (host client %u) (FC count was %d)\n",
  734. (unsigned int)cl->fw_client_id,
  735. (unsigned int)cl->host_client_id,
  736. cl->ishtp_flow_ctrl_creds);
  737. else {
  738. ++cl->ishtp_flow_ctrl_creds;
  739. ++cl->ishtp_flow_ctrl_cnt;
  740. cl->last_ipc_acked = 1;
  741. spin_lock_irqsave(
  742. &cl->tx_list_spinlock,
  743. tx_flags);
  744. if (!list_empty(&cl->tx_list.list)) {
  745. /*
  746. * start sending the first msg
  747. * = the callback function
  748. */
  749. spin_unlock_irqrestore(
  750. &cl->tx_list_spinlock,
  751. tx_flags);
  752. ishtp_cl_send_msg(dev, cl);
  753. } else {
  754. spin_unlock_irqrestore(
  755. &cl->tx_list_spinlock,
  756. tx_flags);
  757. }
  758. }
  759. break;
  760. }
  761. }
  762. spin_unlock_irqrestore(&dev->cl_list_lock, flags);
  763. goto eoi;
  764. }
  765. /*
  766. * Some messages that are safe for ISR processing and important
  767. * to be done "quickly" and in-order, go here
  768. */
  769. if (ishtp_msg->hbm_cmd == CLIENT_CONNECT_RES_CMD ||
  770. ishtp_msg->hbm_cmd == CLIENT_DISCONNECT_RES_CMD ||
  771. ishtp_msg->hbm_cmd == CLIENT_DISCONNECT_REQ_CMD ||
  772. ishtp_msg->hbm_cmd == DMA_XFER) {
  773. ishtp_hbm_dispatch(dev, ishtp_msg);
  774. goto eoi;
  775. }
  776. /*
  777. * All other HBMs go here.
  778. * We schedule HBMs for processing serially by using system wq,
  779. * possibly there will be multiple HBMs scheduled at the same time.
  780. */
  781. spin_lock_irqsave(&dev->rd_msg_spinlock, flags);
  782. if ((dev->rd_msg_fifo_tail + IPC_PAYLOAD_SIZE) %
  783. (RD_INT_FIFO_SIZE * IPC_PAYLOAD_SIZE) ==
  784. dev->rd_msg_fifo_head) {
  785. spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
  786. dev_err(dev->devc, "BH buffer overflow, dropping HBM %u\n",
  787. (unsigned int)ishtp_msg->hbm_cmd);
  788. goto eoi;
  789. }
  790. memcpy(dev->rd_msg_fifo + dev->rd_msg_fifo_tail, ishtp_msg,
  791. ishtp_hdr->length);
  792. dev->rd_msg_fifo_tail = (dev->rd_msg_fifo_tail + IPC_PAYLOAD_SIZE) %
  793. (RD_INT_FIFO_SIZE * IPC_PAYLOAD_SIZE);
  794. spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
  795. schedule_work(&dev->bh_hbm_work);
  796. eoi:
  797. return;
  798. }
  799. /**
  800. * recv_fixed_cl_msg() - Receive fixed client message
  801. * @dev: ISHTP device instance
  802. * @ishtp_hdr: received bus message
  803. *
  804. * Receive and process ISHTP fixed client messages (address == 0)
  805. * in ISR context
  806. */
  807. void recv_fixed_cl_msg(struct ishtp_device *dev,
  808. struct ishtp_msg_hdr *ishtp_hdr)
  809. {
  810. uint8_t rd_msg_buf[ISHTP_RD_MSG_BUF_SIZE];
  811. dev->print_log(dev,
  812. "%s() got fixed client msg from client #%d\n",
  813. __func__, ishtp_hdr->fw_addr);
  814. dev->ops->ishtp_read(dev, rd_msg_buf, ishtp_hdr->length);
  815. if (ishtp_hdr->fw_addr == ISHTP_SYSTEM_STATE_CLIENT_ADDR) {
  816. struct ish_system_states_header *msg_hdr =
  817. (struct ish_system_states_header *)rd_msg_buf;
  818. if (msg_hdr->cmd == SYSTEM_STATE_SUBSCRIBE)
  819. ishtp_send_resume(dev);
  820. /* if FW request arrived here, the system is not suspended */
  821. else
  822. dev_err(dev->devc, "unknown fixed client msg [%02X]\n",
  823. msg_hdr->cmd);
  824. }
  825. }
  826. /**
  827. * fix_cl_hdr() - Initialize fixed client header
  828. * @hdr: message header
  829. * @length: length of message
  830. * @cl_addr: Client address
  831. *
  832. * Initialize message header for fixed client
  833. */
  834. static inline void fix_cl_hdr(struct ishtp_msg_hdr *hdr, size_t length,
  835. uint8_t cl_addr)
  836. {
  837. hdr->host_addr = 0;
  838. hdr->fw_addr = cl_addr;
  839. hdr->length = length;
  840. hdr->msg_complete = 1;
  841. hdr->reserved = 0;
  842. }
  843. /*** Suspend and resume notification ***/
  844. static uint32_t current_state;
  845. static uint32_t supported_states = 0 | SUSPEND_STATE_BIT;
  846. /**
  847. * ishtp_send_suspend() - Send suspend message to FW
  848. * @dev: ISHTP device instance
  849. *
  850. * Send suspend message to FW. This is useful for system freeze (non S3) case
  851. */
  852. void ishtp_send_suspend(struct ishtp_device *dev)
  853. {
  854. struct ishtp_msg_hdr ishtp_hdr;
  855. struct ish_system_states_status state_status_msg;
  856. const size_t len = sizeof(struct ish_system_states_status);
  857. fix_cl_hdr(&ishtp_hdr, len, ISHTP_SYSTEM_STATE_CLIENT_ADDR);
  858. memset(&state_status_msg, 0, len);
  859. state_status_msg.hdr.cmd = SYSTEM_STATE_STATUS;
  860. state_status_msg.supported_states = supported_states;
  861. current_state |= SUSPEND_STATE_BIT;
  862. dev->print_log(dev, "%s() sends SUSPEND notification\n", __func__);
  863. state_status_msg.states_status = current_state;
  864. ishtp_write_message(dev, &ishtp_hdr,
  865. (unsigned char *)&state_status_msg);
  866. }
  867. EXPORT_SYMBOL(ishtp_send_suspend);
  868. /**
  869. * ishtp_send_resume() - Send resume message to FW
  870. * @dev: ISHTP device instance
  871. *
  872. * Send resume message to FW. This is useful for system freeze (non S3) case
  873. */
  874. void ishtp_send_resume(struct ishtp_device *dev)
  875. {
  876. struct ishtp_msg_hdr ishtp_hdr;
  877. struct ish_system_states_status state_status_msg;
  878. const size_t len = sizeof(struct ish_system_states_status);
  879. fix_cl_hdr(&ishtp_hdr, len, ISHTP_SYSTEM_STATE_CLIENT_ADDR);
  880. memset(&state_status_msg, 0, len);
  881. state_status_msg.hdr.cmd = SYSTEM_STATE_STATUS;
  882. state_status_msg.supported_states = supported_states;
  883. current_state &= ~SUSPEND_STATE_BIT;
  884. dev->print_log(dev, "%s() sends RESUME notification\n", __func__);
  885. state_status_msg.states_status = current_state;
  886. ishtp_write_message(dev, &ishtp_hdr,
  887. (unsigned char *)&state_status_msg);
  888. }
  889. EXPORT_SYMBOL(ishtp_send_resume);
  890. /**
  891. * ishtp_query_subscribers() - Send query subscribers message
  892. * @dev: ISHTP device instance
  893. *
  894. * Send message to query subscribers
  895. */
  896. void ishtp_query_subscribers(struct ishtp_device *dev)
  897. {
  898. struct ishtp_msg_hdr ishtp_hdr;
  899. struct ish_system_states_query_subscribers query_subscribers_msg;
  900. const size_t len = sizeof(struct ish_system_states_query_subscribers);
  901. fix_cl_hdr(&ishtp_hdr, len, ISHTP_SYSTEM_STATE_CLIENT_ADDR);
  902. memset(&query_subscribers_msg, 0, len);
  903. query_subscribers_msg.hdr.cmd = SYSTEM_STATE_QUERY_SUBSCRIBERS;
  904. ishtp_write_message(dev, &ishtp_hdr,
  905. (unsigned char *)&query_subscribers_msg);
  906. }