hbm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. /*
  2. *
  3. * Intel Management Engine Interface (Intel MEI) Linux driver
  4. * Copyright (c) 2003-2012, 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/sched.h>
  18. #include <linux/wait.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/slab.h>
  21. #include <linux/mei.h>
  22. #include "mei_dev.h"
  23. #include "hbm.h"
  24. #include "client.h"
  25. static const char *mei_hbm_status_str(enum mei_hbm_status status)
  26. {
  27. #define MEI_HBM_STATUS(status) case MEI_HBMS_##status: return #status
  28. switch (status) {
  29. MEI_HBM_STATUS(SUCCESS);
  30. MEI_HBM_STATUS(CLIENT_NOT_FOUND);
  31. MEI_HBM_STATUS(ALREADY_EXISTS);
  32. MEI_HBM_STATUS(REJECTED);
  33. MEI_HBM_STATUS(INVALID_PARAMETER);
  34. MEI_HBM_STATUS(NOT_ALLOWED);
  35. MEI_HBM_STATUS(ALREADY_STARTED);
  36. MEI_HBM_STATUS(NOT_STARTED);
  37. default: return "unknown";
  38. }
  39. #undef MEI_HBM_STATUS
  40. };
  41. static const char *mei_cl_conn_status_str(enum mei_cl_connect_status status)
  42. {
  43. #define MEI_CL_CS(status) case MEI_CL_CONN_##status: return #status
  44. switch (status) {
  45. MEI_CL_CS(SUCCESS);
  46. MEI_CL_CS(NOT_FOUND);
  47. MEI_CL_CS(ALREADY_STARTED);
  48. MEI_CL_CS(OUT_OF_RESOURCES);
  49. MEI_CL_CS(MESSAGE_SMALL);
  50. default: return "unknown";
  51. }
  52. #undef MEI_CL_CCS
  53. }
  54. const char *mei_hbm_state_str(enum mei_hbm_state state)
  55. {
  56. #define MEI_HBM_STATE(state) case MEI_HBM_##state: return #state
  57. switch (state) {
  58. MEI_HBM_STATE(IDLE);
  59. MEI_HBM_STATE(STARTING);
  60. MEI_HBM_STATE(STARTED);
  61. MEI_HBM_STATE(ENUM_CLIENTS);
  62. MEI_HBM_STATE(CLIENT_PROPERTIES);
  63. MEI_HBM_STATE(STOPPED);
  64. default:
  65. return "unknown";
  66. }
  67. #undef MEI_HBM_STATE
  68. }
  69. /**
  70. * mei_cl_conn_status_to_errno - convert client connect response
  71. * status to error code
  72. *
  73. * @status: client connect response status
  74. *
  75. * Return: corresponding error code
  76. */
  77. static int mei_cl_conn_status_to_errno(enum mei_cl_connect_status status)
  78. {
  79. switch (status) {
  80. case MEI_CL_CONN_SUCCESS: return 0;
  81. case MEI_CL_CONN_NOT_FOUND: return -ENOTTY;
  82. case MEI_CL_CONN_ALREADY_STARTED: return -EBUSY;
  83. case MEI_CL_CONN_OUT_OF_RESOURCES: return -EBUSY;
  84. case MEI_CL_CONN_MESSAGE_SMALL: return -EINVAL;
  85. default: return -EINVAL;
  86. }
  87. }
  88. /**
  89. * mei_hbm_idle - set hbm to idle state
  90. *
  91. * @dev: the device structure
  92. */
  93. void mei_hbm_idle(struct mei_device *dev)
  94. {
  95. dev->init_clients_timer = 0;
  96. dev->hbm_state = MEI_HBM_IDLE;
  97. }
  98. /**
  99. * mei_me_cl_remove_all - remove all me clients
  100. *
  101. * @dev: the device structure
  102. */
  103. static void mei_me_cl_remove_all(struct mei_device *dev)
  104. {
  105. struct mei_me_client *me_cl, *next;
  106. list_for_each_entry_safe(me_cl, next, &dev->me_clients, list) {
  107. list_del(&me_cl->list);
  108. kfree(me_cl);
  109. }
  110. }
  111. /**
  112. * mei_hbm_reset - reset hbm counters and book keeping data structurs
  113. *
  114. * @dev: the device structure
  115. */
  116. void mei_hbm_reset(struct mei_device *dev)
  117. {
  118. dev->me_client_index = 0;
  119. mei_me_cl_remove_all(dev);
  120. mei_hbm_idle(dev);
  121. }
  122. /**
  123. * mei_hbm_hdr - construct hbm header
  124. *
  125. * @hdr: hbm header
  126. * @length: payload length
  127. */
  128. static inline void mei_hbm_hdr(struct mei_msg_hdr *hdr, size_t length)
  129. {
  130. hdr->host_addr = 0;
  131. hdr->me_addr = 0;
  132. hdr->length = length;
  133. hdr->msg_complete = 1;
  134. hdr->reserved = 0;
  135. }
  136. /**
  137. * mei_hbm_cl_hdr - construct client hbm header
  138. *
  139. * @cl: client
  140. * @hbm_cmd: host bus message command
  141. * @buf: buffer for cl header
  142. * @len: buffer length
  143. */
  144. static inline
  145. void mei_hbm_cl_hdr(struct mei_cl *cl, u8 hbm_cmd, void *buf, size_t len)
  146. {
  147. struct mei_hbm_cl_cmd *cmd = buf;
  148. memset(cmd, 0, len);
  149. cmd->hbm_cmd = hbm_cmd;
  150. cmd->host_addr = cl->host_client_id;
  151. cmd->me_addr = cl->me_client_id;
  152. }
  153. /**
  154. * mei_hbm_cl_write - write simple hbm client message
  155. *
  156. * @dev: the device structure
  157. * @cl: client
  158. * @hbm_cmd: host bus message command
  159. * @len: buffer length
  160. *
  161. * Return: 0 on success, <0 on failure.
  162. */
  163. static inline
  164. int mei_hbm_cl_write(struct mei_device *dev,
  165. struct mei_cl *cl, u8 hbm_cmd, size_t len)
  166. {
  167. struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
  168. mei_hbm_hdr(mei_hdr, len);
  169. mei_hbm_cl_hdr(cl, hbm_cmd, dev->wr_msg.data, len);
  170. return mei_write_message(dev, mei_hdr, dev->wr_msg.data);
  171. }
  172. /**
  173. * mei_hbm_cl_addr_equal - check if the client's and
  174. * the message address match
  175. *
  176. * @cl: client
  177. * @cmd: hbm client message
  178. *
  179. * Return: true if addresses are the same
  180. */
  181. static inline
  182. bool mei_hbm_cl_addr_equal(struct mei_cl *cl, struct mei_hbm_cl_cmd *cmd)
  183. {
  184. return cl->host_client_id == cmd->host_addr &&
  185. cl->me_client_id == cmd->me_addr;
  186. }
  187. /**
  188. * mei_hbm_cl_find_by_cmd - find recipient client
  189. *
  190. * @dev: the device structure
  191. * @buf: a buffer with hbm cl command
  192. *
  193. * Return: the recipient client or NULL if not found
  194. */
  195. static inline
  196. struct mei_cl *mei_hbm_cl_find_by_cmd(struct mei_device *dev, void *buf)
  197. {
  198. struct mei_hbm_cl_cmd *cmd = (struct mei_hbm_cl_cmd *)buf;
  199. struct mei_cl *cl;
  200. list_for_each_entry(cl, &dev->file_list, link)
  201. if (mei_hbm_cl_addr_equal(cl, cmd))
  202. return cl;
  203. return NULL;
  204. }
  205. /**
  206. * mei_hbm_start_wait - wait for start response message.
  207. *
  208. * @dev: the device structure
  209. *
  210. * Return: 0 on success and < 0 on failure
  211. */
  212. int mei_hbm_start_wait(struct mei_device *dev)
  213. {
  214. int ret;
  215. if (dev->hbm_state > MEI_HBM_STARTING)
  216. return 0;
  217. mutex_unlock(&dev->device_lock);
  218. ret = wait_event_timeout(dev->wait_hbm_start,
  219. dev->hbm_state != MEI_HBM_STARTING,
  220. mei_secs_to_jiffies(MEI_HBM_TIMEOUT));
  221. mutex_lock(&dev->device_lock);
  222. if (ret == 0 && (dev->hbm_state <= MEI_HBM_STARTING)) {
  223. dev->hbm_state = MEI_HBM_IDLE;
  224. dev_err(dev->dev, "waiting for mei start failed\n");
  225. return -ETIME;
  226. }
  227. return 0;
  228. }
  229. /**
  230. * mei_hbm_start_req - sends start request message.
  231. *
  232. * @dev: the device structure
  233. *
  234. * Return: 0 on success and < 0 on failure
  235. */
  236. int mei_hbm_start_req(struct mei_device *dev)
  237. {
  238. struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
  239. struct hbm_host_version_request *start_req;
  240. const size_t len = sizeof(struct hbm_host_version_request);
  241. int ret;
  242. mei_hbm_reset(dev);
  243. mei_hbm_hdr(mei_hdr, len);
  244. /* host start message */
  245. start_req = (struct hbm_host_version_request *)dev->wr_msg.data;
  246. memset(start_req, 0, len);
  247. start_req->hbm_cmd = HOST_START_REQ_CMD;
  248. start_req->host_version.major_version = HBM_MAJOR_VERSION;
  249. start_req->host_version.minor_version = HBM_MINOR_VERSION;
  250. dev->hbm_state = MEI_HBM_IDLE;
  251. ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data);
  252. if (ret) {
  253. dev_err(dev->dev, "version message write failed: ret = %d\n",
  254. ret);
  255. return ret;
  256. }
  257. dev->hbm_state = MEI_HBM_STARTING;
  258. dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
  259. return 0;
  260. }
  261. /*
  262. * mei_hbm_enum_clients_req - sends enumeration client request message.
  263. *
  264. * @dev: the device structure
  265. *
  266. * Return: 0 on success and < 0 on failure
  267. */
  268. static int mei_hbm_enum_clients_req(struct mei_device *dev)
  269. {
  270. struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
  271. struct hbm_host_enum_request *enum_req;
  272. const size_t len = sizeof(struct hbm_host_enum_request);
  273. int ret;
  274. /* enumerate clients */
  275. mei_hbm_hdr(mei_hdr, len);
  276. enum_req = (struct hbm_host_enum_request *)dev->wr_msg.data;
  277. memset(enum_req, 0, len);
  278. enum_req->hbm_cmd = HOST_ENUM_REQ_CMD;
  279. ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data);
  280. if (ret) {
  281. dev_err(dev->dev, "enumeration request write failed: ret = %d.\n",
  282. ret);
  283. return ret;
  284. }
  285. dev->hbm_state = MEI_HBM_ENUM_CLIENTS;
  286. dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
  287. return 0;
  288. }
  289. /*
  290. * mei_hbm_me_cl_add - add new me client to the list
  291. *
  292. * @dev: the device structure
  293. * @res: hbm property response
  294. *
  295. * Return: 0 on success and -ENOMEM on allocation failure
  296. */
  297. static int mei_hbm_me_cl_add(struct mei_device *dev,
  298. struct hbm_props_response *res)
  299. {
  300. struct mei_me_client *me_cl;
  301. me_cl = kzalloc(sizeof(struct mei_me_client), GFP_KERNEL);
  302. if (!me_cl)
  303. return -ENOMEM;
  304. me_cl->props = res->client_properties;
  305. me_cl->client_id = res->me_addr;
  306. me_cl->mei_flow_ctrl_creds = 0;
  307. list_add(&me_cl->list, &dev->me_clients);
  308. return 0;
  309. }
  310. /**
  311. * mei_hbm_prop_req - request property for a single client
  312. *
  313. * @dev: the device structure
  314. *
  315. * Return: 0 on success and < 0 on failure
  316. */
  317. static int mei_hbm_prop_req(struct mei_device *dev)
  318. {
  319. struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
  320. struct hbm_props_request *prop_req;
  321. const size_t len = sizeof(struct hbm_props_request);
  322. unsigned long next_client_index;
  323. int ret;
  324. next_client_index = find_next_bit(dev->me_clients_map, MEI_CLIENTS_MAX,
  325. dev->me_client_index);
  326. /* We got all client properties */
  327. if (next_client_index == MEI_CLIENTS_MAX) {
  328. dev->hbm_state = MEI_HBM_STARTED;
  329. schedule_work(&dev->init_work);
  330. return 0;
  331. }
  332. mei_hbm_hdr(mei_hdr, len);
  333. prop_req = (struct hbm_props_request *)dev->wr_msg.data;
  334. memset(prop_req, 0, sizeof(struct hbm_props_request));
  335. prop_req->hbm_cmd = HOST_CLIENT_PROPERTIES_REQ_CMD;
  336. prop_req->me_addr = next_client_index;
  337. ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data);
  338. if (ret) {
  339. dev_err(dev->dev, "properties request write failed: ret = %d\n",
  340. ret);
  341. return ret;
  342. }
  343. dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
  344. dev->me_client_index = next_client_index;
  345. return 0;
  346. }
  347. /*
  348. * mei_hbm_pg - sends pg command
  349. *
  350. * @dev: the device structure
  351. * @pg_cmd: the pg command code
  352. *
  353. * Return: -EIO on write failure
  354. * -EOPNOTSUPP if the operation is not supported by the protocol
  355. */
  356. int mei_hbm_pg(struct mei_device *dev, u8 pg_cmd)
  357. {
  358. struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
  359. struct hbm_power_gate *req;
  360. const size_t len = sizeof(struct hbm_power_gate);
  361. int ret;
  362. if (!dev->hbm_f_pg_supported)
  363. return -EOPNOTSUPP;
  364. mei_hbm_hdr(mei_hdr, len);
  365. req = (struct hbm_power_gate *)dev->wr_msg.data;
  366. memset(req, 0, len);
  367. req->hbm_cmd = pg_cmd;
  368. ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data);
  369. if (ret)
  370. dev_err(dev->dev, "power gate command write failed.\n");
  371. return ret;
  372. }
  373. EXPORT_SYMBOL_GPL(mei_hbm_pg);
  374. /**
  375. * mei_hbm_stop_req - send stop request message
  376. *
  377. * @dev: mei device
  378. *
  379. * Return: -EIO on write failure
  380. */
  381. static int mei_hbm_stop_req(struct mei_device *dev)
  382. {
  383. struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
  384. struct hbm_host_stop_request *req =
  385. (struct hbm_host_stop_request *)dev->wr_msg.data;
  386. const size_t len = sizeof(struct hbm_host_stop_request);
  387. mei_hbm_hdr(mei_hdr, len);
  388. memset(req, 0, len);
  389. req->hbm_cmd = HOST_STOP_REQ_CMD;
  390. req->reason = DRIVER_STOP_REQUEST;
  391. return mei_write_message(dev, mei_hdr, dev->wr_msg.data);
  392. }
  393. /**
  394. * mei_hbm_cl_flow_control_req - sends flow control request.
  395. *
  396. * @dev: the device structure
  397. * @cl: client info
  398. *
  399. * Return: -EIO on write failure
  400. */
  401. int mei_hbm_cl_flow_control_req(struct mei_device *dev, struct mei_cl *cl)
  402. {
  403. const size_t len = sizeof(struct hbm_flow_control);
  404. cl_dbg(dev, cl, "sending flow control\n");
  405. return mei_hbm_cl_write(dev, cl, MEI_FLOW_CONTROL_CMD, len);
  406. }
  407. /**
  408. * mei_hbm_add_single_flow_creds - adds single buffer credentials.
  409. *
  410. * @dev: the device structure
  411. * @flow: flow control.
  412. *
  413. * Return: 0 on success, < 0 otherwise
  414. */
  415. static int mei_hbm_add_single_flow_creds(struct mei_device *dev,
  416. struct hbm_flow_control *flow)
  417. {
  418. struct mei_me_client *me_cl;
  419. me_cl = mei_me_cl_by_id(dev, flow->me_addr);
  420. if (!me_cl) {
  421. dev_err(dev->dev, "no such me client %d\n",
  422. flow->me_addr);
  423. return -ENOENT;
  424. }
  425. if (WARN_ON(me_cl->props.single_recv_buf == 0))
  426. return -EINVAL;
  427. me_cl->mei_flow_ctrl_creds++;
  428. dev_dbg(dev->dev, "recv flow ctrl msg ME %d (single) creds = %d.\n",
  429. flow->me_addr, me_cl->mei_flow_ctrl_creds);
  430. return 0;
  431. }
  432. /**
  433. * mei_hbm_cl_flow_control_res - flow control response from me
  434. *
  435. * @dev: the device structure
  436. * @flow_control: flow control response bus message
  437. */
  438. static void mei_hbm_cl_flow_control_res(struct mei_device *dev,
  439. struct hbm_flow_control *flow_control)
  440. {
  441. struct mei_cl *cl;
  442. if (!flow_control->host_addr) {
  443. /* single receive buffer */
  444. mei_hbm_add_single_flow_creds(dev, flow_control);
  445. return;
  446. }
  447. cl = mei_hbm_cl_find_by_cmd(dev, flow_control);
  448. if (cl) {
  449. cl->mei_flow_ctrl_creds++;
  450. cl_dbg(dev, cl, "flow control creds = %d.\n",
  451. cl->mei_flow_ctrl_creds);
  452. }
  453. }
  454. /**
  455. * mei_hbm_cl_disconnect_req - sends disconnect message to fw.
  456. *
  457. * @dev: the device structure
  458. * @cl: a client to disconnect from
  459. *
  460. * Return: -EIO on write failure
  461. */
  462. int mei_hbm_cl_disconnect_req(struct mei_device *dev, struct mei_cl *cl)
  463. {
  464. const size_t len = sizeof(struct hbm_client_connect_request);
  465. return mei_hbm_cl_write(dev, cl, CLIENT_DISCONNECT_REQ_CMD, len);
  466. }
  467. /**
  468. * mei_hbm_cl_disconnect_rsp - sends disconnect respose to the FW
  469. *
  470. * @dev: the device structure
  471. * @cl: a client to disconnect from
  472. *
  473. * Return: -EIO on write failure
  474. */
  475. int mei_hbm_cl_disconnect_rsp(struct mei_device *dev, struct mei_cl *cl)
  476. {
  477. const size_t len = sizeof(struct hbm_client_connect_response);
  478. return mei_hbm_cl_write(dev, cl, CLIENT_DISCONNECT_RES_CMD, len);
  479. }
  480. /**
  481. * mei_hbm_cl_disconnect_res - update the client state according
  482. * disconnect response
  483. *
  484. * @dev: the device structure
  485. * @cl: mei host client
  486. * @cmd: disconnect client response host bus message
  487. */
  488. static void mei_hbm_cl_disconnect_res(struct mei_device *dev, struct mei_cl *cl,
  489. struct mei_hbm_cl_cmd *cmd)
  490. {
  491. struct hbm_client_connect_response *rs =
  492. (struct hbm_client_connect_response *)cmd;
  493. cl_dbg(dev, cl, "hbm: disconnect response status=%d\n", rs->status);
  494. if (rs->status == MEI_CL_DISCONN_SUCCESS)
  495. cl->state = MEI_FILE_DISCONNECTED;
  496. cl->status = 0;
  497. }
  498. /**
  499. * mei_hbm_cl_connect_req - send connection request to specific me client
  500. *
  501. * @dev: the device structure
  502. * @cl: a client to connect to
  503. *
  504. * Return: -EIO on write failure
  505. */
  506. int mei_hbm_cl_connect_req(struct mei_device *dev, struct mei_cl *cl)
  507. {
  508. const size_t len = sizeof(struct hbm_client_connect_request);
  509. return mei_hbm_cl_write(dev, cl, CLIENT_CONNECT_REQ_CMD, len);
  510. }
  511. /**
  512. * mei_hbm_cl_connect_res - update the client state according
  513. * connection response
  514. *
  515. * @dev: the device structure
  516. * @cl: mei host client
  517. * @cmd: connect client response host bus message
  518. */
  519. static void mei_hbm_cl_connect_res(struct mei_device *dev, struct mei_cl *cl,
  520. struct mei_hbm_cl_cmd *cmd)
  521. {
  522. struct hbm_client_connect_response *rs =
  523. (struct hbm_client_connect_response *)cmd;
  524. cl_dbg(dev, cl, "hbm: connect response status=%s\n",
  525. mei_cl_conn_status_str(rs->status));
  526. if (rs->status == MEI_CL_CONN_SUCCESS)
  527. cl->state = MEI_FILE_CONNECTED;
  528. else
  529. cl->state = MEI_FILE_DISCONNECTED;
  530. cl->status = mei_cl_conn_status_to_errno(rs->status);
  531. }
  532. /**
  533. * mei_hbm_cl_res - process hbm response received on behalf
  534. * an client
  535. *
  536. * @dev: the device structure
  537. * @rs: hbm client message
  538. * @fop_type: file operation type
  539. */
  540. static void mei_hbm_cl_res(struct mei_device *dev,
  541. struct mei_hbm_cl_cmd *rs,
  542. enum mei_cb_file_ops fop_type)
  543. {
  544. struct mei_cl *cl;
  545. struct mei_cl_cb *cb, *next;
  546. cl = NULL;
  547. list_for_each_entry_safe(cb, next, &dev->ctrl_rd_list.list, list) {
  548. cl = cb->cl;
  549. if (cb->fop_type != fop_type)
  550. continue;
  551. if (mei_hbm_cl_addr_equal(cl, rs)) {
  552. list_del(&cb->list);
  553. break;
  554. }
  555. }
  556. if (!cl)
  557. return;
  558. switch (fop_type) {
  559. case MEI_FOP_CONNECT:
  560. mei_hbm_cl_connect_res(dev, cl, rs);
  561. break;
  562. case MEI_FOP_DISCONNECT:
  563. mei_hbm_cl_disconnect_res(dev, cl, rs);
  564. break;
  565. default:
  566. return;
  567. }
  568. cl->timer_count = 0;
  569. wake_up(&cl->wait);
  570. }
  571. /**
  572. * mei_hbm_fw_disconnect_req - disconnect request initiated by ME firmware
  573. * host sends disconnect response
  574. *
  575. * @dev: the device structure.
  576. * @disconnect_req: disconnect request bus message from the me
  577. *
  578. * Return: -ENOMEM on allocation failure
  579. */
  580. static int mei_hbm_fw_disconnect_req(struct mei_device *dev,
  581. struct hbm_client_connect_request *disconnect_req)
  582. {
  583. struct mei_cl *cl;
  584. struct mei_cl_cb *cb;
  585. cl = mei_hbm_cl_find_by_cmd(dev, disconnect_req);
  586. if (cl) {
  587. cl_dbg(dev, cl, "disconnect request received\n");
  588. cl->state = MEI_FILE_DISCONNECTED;
  589. cl->timer_count = 0;
  590. cb = mei_io_cb_init(cl, NULL);
  591. if (!cb)
  592. return -ENOMEM;
  593. cb->fop_type = MEI_FOP_DISCONNECT_RSP;
  594. cl_dbg(dev, cl, "add disconnect response as first\n");
  595. list_add(&cb->list, &dev->ctrl_wr_list.list);
  596. }
  597. return 0;
  598. }
  599. /**
  600. * mei_hbm_config_features - check what hbm features and commands
  601. * are supported by the fw
  602. *
  603. * @dev: the device structure
  604. */
  605. static void mei_hbm_config_features(struct mei_device *dev)
  606. {
  607. /* Power Gating Isolation Support */
  608. dev->hbm_f_pg_supported = 0;
  609. if (dev->version.major_version > HBM_MAJOR_VERSION_PGI)
  610. dev->hbm_f_pg_supported = 1;
  611. if (dev->version.major_version == HBM_MAJOR_VERSION_PGI &&
  612. dev->version.minor_version >= HBM_MINOR_VERSION_PGI)
  613. dev->hbm_f_pg_supported = 1;
  614. }
  615. /**
  616. * mei_hbm_version_is_supported - checks whether the driver can
  617. * support the hbm version of the device
  618. *
  619. * @dev: the device structure
  620. * Return: true if driver can support hbm version of the device
  621. */
  622. bool mei_hbm_version_is_supported(struct mei_device *dev)
  623. {
  624. return (dev->version.major_version < HBM_MAJOR_VERSION) ||
  625. (dev->version.major_version == HBM_MAJOR_VERSION &&
  626. dev->version.minor_version <= HBM_MINOR_VERSION);
  627. }
  628. /**
  629. * mei_hbm_dispatch - bottom half read routine after ISR to
  630. * handle the read bus message cmd processing.
  631. *
  632. * @dev: the device structure
  633. * @hdr: header of bus message
  634. *
  635. * Return: 0 on success and < 0 on failure
  636. */
  637. int mei_hbm_dispatch(struct mei_device *dev, struct mei_msg_hdr *hdr)
  638. {
  639. struct mei_bus_message *mei_msg;
  640. struct hbm_host_version_response *version_res;
  641. struct hbm_props_response *props_res;
  642. struct hbm_host_enum_response *enum_res;
  643. struct mei_hbm_cl_cmd *cl_cmd;
  644. struct hbm_client_connect_request *disconnect_req;
  645. struct hbm_flow_control *flow_control;
  646. /* read the message to our buffer */
  647. BUG_ON(hdr->length >= sizeof(dev->rd_msg_buf));
  648. mei_read_slots(dev, dev->rd_msg_buf, hdr->length);
  649. mei_msg = (struct mei_bus_message *)dev->rd_msg_buf;
  650. cl_cmd = (struct mei_hbm_cl_cmd *)mei_msg;
  651. /* ignore spurious message and prevent reset nesting
  652. * hbm is put to idle during system reset
  653. */
  654. if (dev->hbm_state == MEI_HBM_IDLE) {
  655. dev_dbg(dev->dev, "hbm: state is idle ignore spurious messages\n");
  656. return 0;
  657. }
  658. switch (mei_msg->hbm_cmd) {
  659. case HOST_START_RES_CMD:
  660. dev_dbg(dev->dev, "hbm: start: response message received.\n");
  661. dev->init_clients_timer = 0;
  662. version_res = (struct hbm_host_version_response *)mei_msg;
  663. dev_dbg(dev->dev, "HBM VERSION: DRIVER=%02d:%02d DEVICE=%02d:%02d\n",
  664. HBM_MAJOR_VERSION, HBM_MINOR_VERSION,
  665. version_res->me_max_version.major_version,
  666. version_res->me_max_version.minor_version);
  667. if (version_res->host_version_supported) {
  668. dev->version.major_version = HBM_MAJOR_VERSION;
  669. dev->version.minor_version = HBM_MINOR_VERSION;
  670. } else {
  671. dev->version.major_version =
  672. version_res->me_max_version.major_version;
  673. dev->version.minor_version =
  674. version_res->me_max_version.minor_version;
  675. }
  676. if (!mei_hbm_version_is_supported(dev)) {
  677. dev_warn(dev->dev, "hbm: start: version mismatch - stopping the driver.\n");
  678. dev->hbm_state = MEI_HBM_STOPPED;
  679. if (mei_hbm_stop_req(dev)) {
  680. dev_err(dev->dev, "hbm: start: failed to send stop request\n");
  681. return -EIO;
  682. }
  683. break;
  684. }
  685. mei_hbm_config_features(dev);
  686. if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
  687. dev->hbm_state != MEI_HBM_STARTING) {
  688. dev_err(dev->dev, "hbm: start: state mismatch, [%d, %d]\n",
  689. dev->dev_state, dev->hbm_state);
  690. return -EPROTO;
  691. }
  692. if (mei_hbm_enum_clients_req(dev)) {
  693. dev_err(dev->dev, "hbm: start: failed to send enumeration request\n");
  694. return -EIO;
  695. }
  696. wake_up(&dev->wait_hbm_start);
  697. break;
  698. case CLIENT_CONNECT_RES_CMD:
  699. dev_dbg(dev->dev, "hbm: client connect response: message received.\n");
  700. mei_hbm_cl_res(dev, cl_cmd, MEI_FOP_CONNECT);
  701. break;
  702. case CLIENT_DISCONNECT_RES_CMD:
  703. dev_dbg(dev->dev, "hbm: client disconnect response: message received.\n");
  704. mei_hbm_cl_res(dev, cl_cmd, MEI_FOP_DISCONNECT);
  705. break;
  706. case MEI_FLOW_CONTROL_CMD:
  707. dev_dbg(dev->dev, "hbm: client flow control response: message received.\n");
  708. flow_control = (struct hbm_flow_control *) mei_msg;
  709. mei_hbm_cl_flow_control_res(dev, flow_control);
  710. break;
  711. case MEI_PG_ISOLATION_ENTRY_RES_CMD:
  712. dev_dbg(dev->dev, "power gate isolation entry response received\n");
  713. dev->pg_event = MEI_PG_EVENT_RECEIVED;
  714. if (waitqueue_active(&dev->wait_pg))
  715. wake_up(&dev->wait_pg);
  716. break;
  717. case MEI_PG_ISOLATION_EXIT_REQ_CMD:
  718. dev_dbg(dev->dev, "power gate isolation exit request received\n");
  719. dev->pg_event = MEI_PG_EVENT_RECEIVED;
  720. if (waitqueue_active(&dev->wait_pg))
  721. wake_up(&dev->wait_pg);
  722. else
  723. /*
  724. * If the driver is not waiting on this then
  725. * this is HW initiated exit from PG.
  726. * Start runtime pm resume sequence to exit from PG.
  727. */
  728. pm_request_resume(dev->dev);
  729. break;
  730. case HOST_CLIENT_PROPERTIES_RES_CMD:
  731. dev_dbg(dev->dev, "hbm: properties response: message received.\n");
  732. dev->init_clients_timer = 0;
  733. if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
  734. dev->hbm_state != MEI_HBM_CLIENT_PROPERTIES) {
  735. dev_err(dev->dev, "hbm: properties response: state mismatch, [%d, %d]\n",
  736. dev->dev_state, dev->hbm_state);
  737. return -EPROTO;
  738. }
  739. props_res = (struct hbm_props_response *)mei_msg;
  740. if (props_res->status) {
  741. dev_err(dev->dev, "hbm: properties response: wrong status = %d %s\n",
  742. props_res->status,
  743. mei_hbm_status_str(props_res->status));
  744. return -EPROTO;
  745. }
  746. mei_hbm_me_cl_add(dev, props_res);
  747. dev->me_client_index++;
  748. /* request property for the next client */
  749. if (mei_hbm_prop_req(dev))
  750. return -EIO;
  751. break;
  752. case HOST_ENUM_RES_CMD:
  753. dev_dbg(dev->dev, "hbm: enumeration response: message received\n");
  754. dev->init_clients_timer = 0;
  755. enum_res = (struct hbm_host_enum_response *) mei_msg;
  756. BUILD_BUG_ON(sizeof(dev->me_clients_map)
  757. < sizeof(enum_res->valid_addresses));
  758. memcpy(dev->me_clients_map, enum_res->valid_addresses,
  759. sizeof(enum_res->valid_addresses));
  760. if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
  761. dev->hbm_state != MEI_HBM_ENUM_CLIENTS) {
  762. dev_err(dev->dev, "hbm: enumeration response: state mismatch, [%d, %d]\n",
  763. dev->dev_state, dev->hbm_state);
  764. return -EPROTO;
  765. }
  766. dev->hbm_state = MEI_HBM_CLIENT_PROPERTIES;
  767. /* first property request */
  768. if (mei_hbm_prop_req(dev))
  769. return -EIO;
  770. break;
  771. case HOST_STOP_RES_CMD:
  772. dev_dbg(dev->dev, "hbm: stop response: message received\n");
  773. dev->init_clients_timer = 0;
  774. if (dev->hbm_state != MEI_HBM_STOPPED) {
  775. dev_err(dev->dev, "hbm: stop response: state mismatch, [%d, %d]\n",
  776. dev->dev_state, dev->hbm_state);
  777. return -EPROTO;
  778. }
  779. dev->dev_state = MEI_DEV_POWER_DOWN;
  780. dev_info(dev->dev, "hbm: stop response: resetting.\n");
  781. /* force the reset */
  782. return -EPROTO;
  783. break;
  784. case CLIENT_DISCONNECT_REQ_CMD:
  785. dev_dbg(dev->dev, "hbm: disconnect request: message received\n");
  786. disconnect_req = (struct hbm_client_connect_request *)mei_msg;
  787. mei_hbm_fw_disconnect_req(dev, disconnect_req);
  788. break;
  789. case ME_STOP_REQ_CMD:
  790. dev_dbg(dev->dev, "hbm: stop request: message received\n");
  791. dev->hbm_state = MEI_HBM_STOPPED;
  792. if (mei_hbm_stop_req(dev)) {
  793. dev_err(dev->dev, "hbm: stop request: failed to send stop request\n");
  794. return -EIO;
  795. }
  796. break;
  797. default:
  798. BUG();
  799. break;
  800. }
  801. return 0;
  802. }