hbm.c 23 KB

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