hbm.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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. MEI_CL_CS(NOT_ALLOWED);
  51. default: return "unknown";
  52. }
  53. #undef MEI_CL_CCS
  54. }
  55. const char *mei_hbm_state_str(enum mei_hbm_state state)
  56. {
  57. #define MEI_HBM_STATE(state) case MEI_HBM_##state: return #state
  58. switch (state) {
  59. MEI_HBM_STATE(IDLE);
  60. MEI_HBM_STATE(STARTING);
  61. MEI_HBM_STATE(STARTED);
  62. MEI_HBM_STATE(ENUM_CLIENTS);
  63. MEI_HBM_STATE(CLIENT_PROPERTIES);
  64. MEI_HBM_STATE(STOPPED);
  65. default:
  66. return "unknown";
  67. }
  68. #undef MEI_HBM_STATE
  69. }
  70. /**
  71. * mei_cl_conn_status_to_errno - convert client connect response
  72. * status to error code
  73. *
  74. * @status: client connect response status
  75. *
  76. * Return: corresponding error code
  77. */
  78. static int mei_cl_conn_status_to_errno(enum mei_cl_connect_status status)
  79. {
  80. switch (status) {
  81. case MEI_CL_CONN_SUCCESS: return 0;
  82. case MEI_CL_CONN_NOT_FOUND: return -ENOTTY;
  83. case MEI_CL_CONN_ALREADY_STARTED: return -EBUSY;
  84. case MEI_CL_CONN_OUT_OF_RESOURCES: return -EBUSY;
  85. case MEI_CL_CONN_MESSAGE_SMALL: return -EINVAL;
  86. case MEI_CL_CONN_NOT_ALLOWED: return -EBUSY;
  87. default: return -EINVAL;
  88. }
  89. }
  90. /**
  91. * mei_hbm_idle - set hbm to idle state
  92. *
  93. * @dev: the device structure
  94. */
  95. void mei_hbm_idle(struct mei_device *dev)
  96. {
  97. dev->init_clients_timer = 0;
  98. dev->hbm_state = MEI_HBM_IDLE;
  99. }
  100. /**
  101. * mei_hbm_reset - reset hbm counters and book keeping data structurs
  102. *
  103. * @dev: the device structure
  104. */
  105. void mei_hbm_reset(struct mei_device *dev)
  106. {
  107. dev->me_client_index = 0;
  108. mei_me_cl_rm_all(dev);
  109. mei_hbm_idle(dev);
  110. }
  111. /**
  112. * mei_hbm_hdr - construct hbm header
  113. *
  114. * @hdr: hbm header
  115. * @length: payload length
  116. */
  117. static inline void mei_hbm_hdr(struct mei_msg_hdr *hdr, size_t length)
  118. {
  119. hdr->host_addr = 0;
  120. hdr->me_addr = 0;
  121. hdr->length = length;
  122. hdr->msg_complete = 1;
  123. hdr->reserved = 0;
  124. }
  125. /**
  126. * mei_hbm_cl_hdr - construct client hbm header
  127. *
  128. * @cl: client
  129. * @hbm_cmd: host bus message command
  130. * @buf: buffer for cl header
  131. * @len: buffer length
  132. */
  133. static inline
  134. void mei_hbm_cl_hdr(struct mei_cl *cl, u8 hbm_cmd, void *buf, size_t len)
  135. {
  136. struct mei_hbm_cl_cmd *cmd = buf;
  137. memset(cmd, 0, len);
  138. cmd->hbm_cmd = hbm_cmd;
  139. cmd->host_addr = mei_cl_host_addr(cl);
  140. cmd->me_addr = mei_cl_me_id(cl);
  141. }
  142. /**
  143. * mei_hbm_cl_write - write simple hbm client message
  144. *
  145. * @dev: the device structure
  146. * @cl: client
  147. * @hbm_cmd: host bus message command
  148. * @len: buffer length
  149. *
  150. * Return: 0 on success, <0 on failure.
  151. */
  152. static inline
  153. int mei_hbm_cl_write(struct mei_device *dev,
  154. struct mei_cl *cl, u8 hbm_cmd, size_t len)
  155. {
  156. struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
  157. mei_hbm_hdr(mei_hdr, len);
  158. mei_hbm_cl_hdr(cl, hbm_cmd, dev->wr_msg.data, len);
  159. return mei_write_message(dev, mei_hdr, dev->wr_msg.data);
  160. }
  161. /**
  162. * mei_hbm_cl_addr_equal - check if the client's and
  163. * the message address match
  164. *
  165. * @cl: client
  166. * @cmd: hbm client message
  167. *
  168. * Return: true if addresses are the same
  169. */
  170. static inline
  171. bool mei_hbm_cl_addr_equal(struct mei_cl *cl, struct mei_hbm_cl_cmd *cmd)
  172. {
  173. return mei_cl_host_addr(cl) == cmd->host_addr &&
  174. mei_cl_me_id(cl) == cmd->me_addr;
  175. }
  176. /**
  177. * mei_hbm_cl_find_by_cmd - find recipient client
  178. *
  179. * @dev: the device structure
  180. * @buf: a buffer with hbm cl command
  181. *
  182. * Return: the recipient client or NULL if not found
  183. */
  184. static inline
  185. struct mei_cl *mei_hbm_cl_find_by_cmd(struct mei_device *dev, void *buf)
  186. {
  187. struct mei_hbm_cl_cmd *cmd = (struct mei_hbm_cl_cmd *)buf;
  188. struct mei_cl *cl;
  189. list_for_each_entry(cl, &dev->file_list, link)
  190. if (mei_hbm_cl_addr_equal(cl, cmd))
  191. return cl;
  192. return NULL;
  193. }
  194. /**
  195. * mei_hbm_start_wait - wait for start response message.
  196. *
  197. * @dev: the device structure
  198. *
  199. * Return: 0 on success and < 0 on failure
  200. */
  201. int mei_hbm_start_wait(struct mei_device *dev)
  202. {
  203. int ret;
  204. if (dev->hbm_state > MEI_HBM_STARTING)
  205. return 0;
  206. mutex_unlock(&dev->device_lock);
  207. ret = wait_event_timeout(dev->wait_hbm_start,
  208. dev->hbm_state != MEI_HBM_STARTING,
  209. mei_secs_to_jiffies(MEI_HBM_TIMEOUT));
  210. mutex_lock(&dev->device_lock);
  211. if (ret == 0 && (dev->hbm_state <= MEI_HBM_STARTING)) {
  212. dev->hbm_state = MEI_HBM_IDLE;
  213. dev_err(dev->dev, "waiting for mei start failed\n");
  214. return -ETIME;
  215. }
  216. return 0;
  217. }
  218. /**
  219. * mei_hbm_start_req - sends start request message.
  220. *
  221. * @dev: the device structure
  222. *
  223. * Return: 0 on success and < 0 on failure
  224. */
  225. int mei_hbm_start_req(struct mei_device *dev)
  226. {
  227. struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
  228. struct hbm_host_version_request *start_req;
  229. const size_t len = sizeof(struct hbm_host_version_request);
  230. int ret;
  231. mei_hbm_reset(dev);
  232. mei_hbm_hdr(mei_hdr, len);
  233. /* host start message */
  234. start_req = (struct hbm_host_version_request *)dev->wr_msg.data;
  235. memset(start_req, 0, len);
  236. start_req->hbm_cmd = HOST_START_REQ_CMD;
  237. start_req->host_version.major_version = HBM_MAJOR_VERSION;
  238. start_req->host_version.minor_version = HBM_MINOR_VERSION;
  239. dev->hbm_state = MEI_HBM_IDLE;
  240. ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data);
  241. if (ret) {
  242. dev_err(dev->dev, "version message write failed: ret = %d\n",
  243. ret);
  244. return ret;
  245. }
  246. dev->hbm_state = MEI_HBM_STARTING;
  247. dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
  248. return 0;
  249. }
  250. /*
  251. * mei_hbm_enum_clients_req - sends enumeration client request message.
  252. *
  253. * @dev: the device structure
  254. *
  255. * Return: 0 on success and < 0 on failure
  256. */
  257. static int mei_hbm_enum_clients_req(struct mei_device *dev)
  258. {
  259. struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
  260. struct hbm_host_enum_request *enum_req;
  261. const size_t len = sizeof(struct hbm_host_enum_request);
  262. int ret;
  263. /* enumerate clients */
  264. mei_hbm_hdr(mei_hdr, len);
  265. enum_req = (struct hbm_host_enum_request *)dev->wr_msg.data;
  266. memset(enum_req, 0, len);
  267. enum_req->hbm_cmd = HOST_ENUM_REQ_CMD;
  268. enum_req->allow_add = dev->hbm_f_dc_supported;
  269. ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data);
  270. if (ret) {
  271. dev_err(dev->dev, "enumeration request write failed: ret = %d.\n",
  272. ret);
  273. return ret;
  274. }
  275. dev->hbm_state = MEI_HBM_ENUM_CLIENTS;
  276. dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
  277. return 0;
  278. }
  279. /*
  280. * mei_hbm_me_cl_add - add new me client to the list
  281. *
  282. * @dev: the device structure
  283. * @res: hbm property response
  284. *
  285. * Return: 0 on success and -ENOMEM on allocation failure
  286. */
  287. static int mei_hbm_me_cl_add(struct mei_device *dev,
  288. struct hbm_props_response *res)
  289. {
  290. struct mei_me_client *me_cl;
  291. const uuid_le *uuid = &res->client_properties.protocol_name;
  292. mei_me_cl_rm_by_uuid(dev, uuid);
  293. me_cl = kzalloc(sizeof(struct mei_me_client), GFP_KERNEL);
  294. if (!me_cl)
  295. return -ENOMEM;
  296. mei_me_cl_init(me_cl);
  297. me_cl->props = res->client_properties;
  298. me_cl->client_id = res->me_addr;
  299. me_cl->mei_flow_ctrl_creds = 0;
  300. mei_me_cl_add(dev, me_cl);
  301. return 0;
  302. }
  303. /**
  304. * mei_hbm_add_cl_resp - send response to fw on client add request
  305. *
  306. * @dev: the device structure
  307. * @addr: me address
  308. * @status: response status
  309. *
  310. * Return: 0 on success and < 0 on failure
  311. */
  312. static int mei_hbm_add_cl_resp(struct mei_device *dev, u8 addr, u8 status)
  313. {
  314. struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
  315. struct hbm_add_client_response *resp;
  316. const size_t len = sizeof(struct hbm_add_client_response);
  317. int ret;
  318. dev_dbg(dev->dev, "adding client response\n");
  319. resp = (struct hbm_add_client_response *)dev->wr_msg.data;
  320. mei_hbm_hdr(mei_hdr, len);
  321. memset(resp, 0, sizeof(struct hbm_add_client_response));
  322. resp->hbm_cmd = MEI_HBM_ADD_CLIENT_RES_CMD;
  323. resp->me_addr = addr;
  324. resp->status = status;
  325. ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data);
  326. if (ret)
  327. dev_err(dev->dev, "add client response write failed: ret = %d\n",
  328. ret);
  329. return ret;
  330. }
  331. /**
  332. * mei_hbm_fw_add_cl_req - request from the fw to add a client
  333. *
  334. * @dev: the device structure
  335. * @req: add client request
  336. *
  337. * Return: 0 on success and < 0 on failure
  338. */
  339. static int mei_hbm_fw_add_cl_req(struct mei_device *dev,
  340. struct hbm_add_client_request *req)
  341. {
  342. int ret;
  343. u8 status = MEI_HBMS_SUCCESS;
  344. BUILD_BUG_ON(sizeof(struct hbm_add_client_request) !=
  345. sizeof(struct hbm_props_response));
  346. ret = mei_hbm_me_cl_add(dev, (struct hbm_props_response *)req);
  347. if (ret)
  348. status = !MEI_HBMS_SUCCESS;
  349. return mei_hbm_add_cl_resp(dev, req->me_addr, status);
  350. }
  351. /**
  352. * mei_hbm_cl_notify_req - send notification request
  353. *
  354. * @dev: the device structure
  355. * @cl: a client to disconnect from
  356. * @start: true for start false for stop
  357. *
  358. * Return: 0 on success and -EIO on write failure
  359. */
  360. int mei_hbm_cl_notify_req(struct mei_device *dev,
  361. struct mei_cl *cl, u8 start)
  362. {
  363. struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
  364. struct hbm_notification_request *req;
  365. const size_t len = sizeof(struct hbm_notification_request);
  366. int ret;
  367. mei_hbm_hdr(mei_hdr, len);
  368. mei_hbm_cl_hdr(cl, MEI_HBM_NOTIFY_REQ_CMD, dev->wr_msg.data, len);
  369. req = (struct hbm_notification_request *)dev->wr_msg.data;
  370. req->start = start;
  371. ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data);
  372. if (ret)
  373. dev_err(dev->dev, "notify request failed: ret = %d\n", ret);
  374. return ret;
  375. }
  376. /**
  377. * notify_res_to_fop - convert notification response to the proper
  378. * notification FOP
  379. *
  380. * @cmd: client notification start response command
  381. *
  382. * Return: MEI_FOP_NOTIFY_START or MEI_FOP_NOTIFY_STOP;
  383. */
  384. static inline enum mei_cb_file_ops notify_res_to_fop(struct mei_hbm_cl_cmd *cmd)
  385. {
  386. struct hbm_notification_response *rs =
  387. (struct hbm_notification_response *)cmd;
  388. return mei_cl_notify_req2fop(rs->start);
  389. }
  390. /**
  391. * mei_hbm_cl_notify_start_res - update the client state according
  392. * notify start response
  393. *
  394. * @dev: the device structure
  395. * @cl: mei host client
  396. * @cmd: client notification start response command
  397. */
  398. static void mei_hbm_cl_notify_start_res(struct mei_device *dev,
  399. struct mei_cl *cl,
  400. struct mei_hbm_cl_cmd *cmd)
  401. {
  402. struct hbm_notification_response *rs =
  403. (struct hbm_notification_response *)cmd;
  404. cl_dbg(dev, cl, "hbm: notify start response status=%d\n", rs->status);
  405. if (rs->status == MEI_HBMS_SUCCESS ||
  406. rs->status == MEI_HBMS_ALREADY_STARTED) {
  407. cl->notify_en = true;
  408. cl->status = 0;
  409. } else {
  410. cl->status = -EINVAL;
  411. }
  412. }
  413. /**
  414. * mei_hbm_cl_notify_stop_res - update the client state according
  415. * notify stop response
  416. *
  417. * @dev: the device structure
  418. * @cl: mei host client
  419. * @cmd: client notification stop response command
  420. */
  421. static void mei_hbm_cl_notify_stop_res(struct mei_device *dev,
  422. struct mei_cl *cl,
  423. struct mei_hbm_cl_cmd *cmd)
  424. {
  425. struct hbm_notification_response *rs =
  426. (struct hbm_notification_response *)cmd;
  427. cl_dbg(dev, cl, "hbm: notify stop response status=%d\n", rs->status);
  428. if (rs->status == MEI_HBMS_SUCCESS ||
  429. rs->status == MEI_HBMS_NOT_STARTED) {
  430. cl->notify_en = false;
  431. cl->status = 0;
  432. } else {
  433. /* TODO: spec is not clear yet about other possible issues */
  434. cl->status = -EINVAL;
  435. }
  436. }
  437. /**
  438. * mei_hbm_cl_notify - signal notification event
  439. *
  440. * @dev: the device structure
  441. * @cmd: notification client message
  442. */
  443. static void mei_hbm_cl_notify(struct mei_device *dev,
  444. struct mei_hbm_cl_cmd *cmd)
  445. {
  446. struct mei_cl *cl;
  447. cl = mei_hbm_cl_find_by_cmd(dev, cmd);
  448. if (cl)
  449. mei_cl_notify(cl);
  450. }
  451. /**
  452. * mei_hbm_prop_req - request property for a single client
  453. *
  454. * @dev: the device structure
  455. *
  456. * Return: 0 on success and < 0 on failure
  457. */
  458. static int mei_hbm_prop_req(struct mei_device *dev)
  459. {
  460. struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
  461. struct hbm_props_request *prop_req;
  462. const size_t len = sizeof(struct hbm_props_request);
  463. unsigned long next_client_index;
  464. int ret;
  465. next_client_index = find_next_bit(dev->me_clients_map, MEI_CLIENTS_MAX,
  466. dev->me_client_index);
  467. /* We got all client properties */
  468. if (next_client_index == MEI_CLIENTS_MAX) {
  469. dev->hbm_state = MEI_HBM_STARTED;
  470. schedule_work(&dev->init_work);
  471. return 0;
  472. }
  473. mei_hbm_hdr(mei_hdr, len);
  474. prop_req = (struct hbm_props_request *)dev->wr_msg.data;
  475. memset(prop_req, 0, sizeof(struct hbm_props_request));
  476. prop_req->hbm_cmd = HOST_CLIENT_PROPERTIES_REQ_CMD;
  477. prop_req->me_addr = next_client_index;
  478. ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data);
  479. if (ret) {
  480. dev_err(dev->dev, "properties request write failed: ret = %d\n",
  481. ret);
  482. return ret;
  483. }
  484. dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
  485. dev->me_client_index = next_client_index;
  486. return 0;
  487. }
  488. /*
  489. * mei_hbm_pg - sends pg command
  490. *
  491. * @dev: the device structure
  492. * @pg_cmd: the pg command code
  493. *
  494. * Return: -EIO on write failure
  495. * -EOPNOTSUPP if the operation is not supported by the protocol
  496. */
  497. int mei_hbm_pg(struct mei_device *dev, u8 pg_cmd)
  498. {
  499. struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
  500. struct hbm_power_gate *req;
  501. const size_t len = sizeof(struct hbm_power_gate);
  502. int ret;
  503. if (!dev->hbm_f_pg_supported)
  504. return -EOPNOTSUPP;
  505. mei_hbm_hdr(mei_hdr, len);
  506. req = (struct hbm_power_gate *)dev->wr_msg.data;
  507. memset(req, 0, len);
  508. req->hbm_cmd = pg_cmd;
  509. ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data);
  510. if (ret)
  511. dev_err(dev->dev, "power gate command write failed.\n");
  512. return ret;
  513. }
  514. EXPORT_SYMBOL_GPL(mei_hbm_pg);
  515. /**
  516. * mei_hbm_stop_req - send stop request message
  517. *
  518. * @dev: mei device
  519. *
  520. * Return: -EIO on write failure
  521. */
  522. static int mei_hbm_stop_req(struct mei_device *dev)
  523. {
  524. struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
  525. struct hbm_host_stop_request *req =
  526. (struct hbm_host_stop_request *)dev->wr_msg.data;
  527. const size_t len = sizeof(struct hbm_host_stop_request);
  528. mei_hbm_hdr(mei_hdr, len);
  529. memset(req, 0, len);
  530. req->hbm_cmd = HOST_STOP_REQ_CMD;
  531. req->reason = DRIVER_STOP_REQUEST;
  532. return mei_write_message(dev, mei_hdr, dev->wr_msg.data);
  533. }
  534. /**
  535. * mei_hbm_cl_flow_control_req - sends flow control request.
  536. *
  537. * @dev: the device structure
  538. * @cl: client info
  539. *
  540. * Return: -EIO on write failure
  541. */
  542. int mei_hbm_cl_flow_control_req(struct mei_device *dev, struct mei_cl *cl)
  543. {
  544. const size_t len = sizeof(struct hbm_flow_control);
  545. cl_dbg(dev, cl, "sending flow control\n");
  546. return mei_hbm_cl_write(dev, cl, MEI_FLOW_CONTROL_CMD, len);
  547. }
  548. /**
  549. * mei_hbm_add_single_flow_creds - adds single buffer credentials.
  550. *
  551. * @dev: the device structure
  552. * @flow: flow control.
  553. *
  554. * Return: 0 on success, < 0 otherwise
  555. */
  556. static int mei_hbm_add_single_flow_creds(struct mei_device *dev,
  557. struct hbm_flow_control *flow)
  558. {
  559. struct mei_me_client *me_cl;
  560. int rets;
  561. me_cl = mei_me_cl_by_id(dev, flow->me_addr);
  562. if (!me_cl) {
  563. dev_err(dev->dev, "no such me client %d\n",
  564. flow->me_addr);
  565. return -ENOENT;
  566. }
  567. if (WARN_ON(me_cl->props.single_recv_buf == 0)) {
  568. rets = -EINVAL;
  569. goto out;
  570. }
  571. me_cl->mei_flow_ctrl_creds++;
  572. dev_dbg(dev->dev, "recv flow ctrl msg ME %d (single) creds = %d.\n",
  573. flow->me_addr, me_cl->mei_flow_ctrl_creds);
  574. rets = 0;
  575. out:
  576. mei_me_cl_put(me_cl);
  577. return rets;
  578. }
  579. /**
  580. * mei_hbm_cl_flow_control_res - flow control response from me
  581. *
  582. * @dev: the device structure
  583. * @flow_control: flow control response bus message
  584. */
  585. static void mei_hbm_cl_flow_control_res(struct mei_device *dev,
  586. struct hbm_flow_control *flow_control)
  587. {
  588. struct mei_cl *cl;
  589. if (!flow_control->host_addr) {
  590. /* single receive buffer */
  591. mei_hbm_add_single_flow_creds(dev, flow_control);
  592. return;
  593. }
  594. cl = mei_hbm_cl_find_by_cmd(dev, flow_control);
  595. if (cl) {
  596. cl->mei_flow_ctrl_creds++;
  597. cl_dbg(dev, cl, "flow control creds = %d.\n",
  598. cl->mei_flow_ctrl_creds);
  599. }
  600. }
  601. /**
  602. * mei_hbm_cl_disconnect_req - sends disconnect message to fw.
  603. *
  604. * @dev: the device structure
  605. * @cl: a client to disconnect from
  606. *
  607. * Return: -EIO on write failure
  608. */
  609. int mei_hbm_cl_disconnect_req(struct mei_device *dev, struct mei_cl *cl)
  610. {
  611. const size_t len = sizeof(struct hbm_client_connect_request);
  612. return mei_hbm_cl_write(dev, cl, CLIENT_DISCONNECT_REQ_CMD, len);
  613. }
  614. /**
  615. * mei_hbm_cl_disconnect_rsp - sends disconnect respose to the FW
  616. *
  617. * @dev: the device structure
  618. * @cl: a client to disconnect from
  619. *
  620. * Return: -EIO on write failure
  621. */
  622. int mei_hbm_cl_disconnect_rsp(struct mei_device *dev, struct mei_cl *cl)
  623. {
  624. const size_t len = sizeof(struct hbm_client_connect_response);
  625. return mei_hbm_cl_write(dev, cl, CLIENT_DISCONNECT_RES_CMD, len);
  626. }
  627. /**
  628. * mei_hbm_cl_disconnect_res - update the client state according
  629. * disconnect response
  630. *
  631. * @dev: the device structure
  632. * @cl: mei host client
  633. * @cmd: disconnect client response host bus message
  634. */
  635. static void mei_hbm_cl_disconnect_res(struct mei_device *dev, struct mei_cl *cl,
  636. struct mei_hbm_cl_cmd *cmd)
  637. {
  638. struct hbm_client_connect_response *rs =
  639. (struct hbm_client_connect_response *)cmd;
  640. cl_dbg(dev, cl, "hbm: disconnect response status=%d\n", rs->status);
  641. if (rs->status == MEI_CL_DISCONN_SUCCESS)
  642. cl->state = MEI_FILE_DISCONNECT_REPLY;
  643. cl->status = 0;
  644. }
  645. /**
  646. * mei_hbm_cl_connect_req - send connection request to specific me client
  647. *
  648. * @dev: the device structure
  649. * @cl: a client to connect to
  650. *
  651. * Return: -EIO on write failure
  652. */
  653. int mei_hbm_cl_connect_req(struct mei_device *dev, struct mei_cl *cl)
  654. {
  655. const size_t len = sizeof(struct hbm_client_connect_request);
  656. return mei_hbm_cl_write(dev, cl, CLIENT_CONNECT_REQ_CMD, len);
  657. }
  658. /**
  659. * mei_hbm_cl_connect_res - update the client state according
  660. * connection response
  661. *
  662. * @dev: the device structure
  663. * @cl: mei host client
  664. * @cmd: connect client response host bus message
  665. */
  666. static void mei_hbm_cl_connect_res(struct mei_device *dev, struct mei_cl *cl,
  667. struct mei_hbm_cl_cmd *cmd)
  668. {
  669. struct hbm_client_connect_response *rs =
  670. (struct hbm_client_connect_response *)cmd;
  671. cl_dbg(dev, cl, "hbm: connect response status=%s\n",
  672. mei_cl_conn_status_str(rs->status));
  673. if (rs->status == MEI_CL_CONN_SUCCESS)
  674. cl->state = MEI_FILE_CONNECTED;
  675. else {
  676. cl->state = MEI_FILE_DISCONNECT_REPLY;
  677. if (rs->status == MEI_CL_CONN_NOT_FOUND)
  678. mei_me_cl_del(dev, cl->me_cl);
  679. }
  680. cl->status = mei_cl_conn_status_to_errno(rs->status);
  681. }
  682. /**
  683. * mei_hbm_cl_res - process hbm response received on behalf
  684. * an client
  685. *
  686. * @dev: the device structure
  687. * @rs: hbm client message
  688. * @fop_type: file operation type
  689. */
  690. static void mei_hbm_cl_res(struct mei_device *dev,
  691. struct mei_hbm_cl_cmd *rs,
  692. enum mei_cb_file_ops fop_type)
  693. {
  694. struct mei_cl *cl;
  695. struct mei_cl_cb *cb, *next;
  696. cl = NULL;
  697. list_for_each_entry_safe(cb, next, &dev->ctrl_rd_list.list, list) {
  698. cl = cb->cl;
  699. if (cb->fop_type != fop_type)
  700. continue;
  701. if (mei_hbm_cl_addr_equal(cl, rs)) {
  702. list_del_init(&cb->list);
  703. break;
  704. }
  705. }
  706. if (!cl)
  707. return;
  708. switch (fop_type) {
  709. case MEI_FOP_CONNECT:
  710. mei_hbm_cl_connect_res(dev, cl, rs);
  711. break;
  712. case MEI_FOP_DISCONNECT:
  713. mei_hbm_cl_disconnect_res(dev, cl, rs);
  714. break;
  715. case MEI_FOP_NOTIFY_START:
  716. mei_hbm_cl_notify_start_res(dev, cl, rs);
  717. break;
  718. case MEI_FOP_NOTIFY_STOP:
  719. mei_hbm_cl_notify_stop_res(dev, cl, rs);
  720. break;
  721. default:
  722. return;
  723. }
  724. cl->timer_count = 0;
  725. wake_up(&cl->wait);
  726. }
  727. /**
  728. * mei_hbm_fw_disconnect_req - disconnect request initiated by ME firmware
  729. * host sends disconnect response
  730. *
  731. * @dev: the device structure.
  732. * @disconnect_req: disconnect request bus message from the me
  733. *
  734. * Return: -ENOMEM on allocation failure
  735. */
  736. static int mei_hbm_fw_disconnect_req(struct mei_device *dev,
  737. struct hbm_client_connect_request *disconnect_req)
  738. {
  739. struct mei_cl *cl;
  740. struct mei_cl_cb *cb;
  741. cl = mei_hbm_cl_find_by_cmd(dev, disconnect_req);
  742. if (cl) {
  743. cl_dbg(dev, cl, "fw disconnect request received\n");
  744. cl->state = MEI_FILE_DISCONNECTING;
  745. cl->timer_count = 0;
  746. cb = mei_io_cb_init(cl, MEI_FOP_DISCONNECT_RSP, NULL);
  747. if (!cb)
  748. return -ENOMEM;
  749. cl_dbg(dev, cl, "add disconnect response as first\n");
  750. list_add(&cb->list, &dev->ctrl_wr_list.list);
  751. }
  752. return 0;
  753. }
  754. /**
  755. * mei_hbm_pg_enter_res - PG enter response received
  756. *
  757. * @dev: the device structure.
  758. *
  759. * Return: 0 on success, -EPROTO on state mismatch
  760. */
  761. static int mei_hbm_pg_enter_res(struct mei_device *dev)
  762. {
  763. if (mei_pg_state(dev) != MEI_PG_OFF ||
  764. dev->pg_event != MEI_PG_EVENT_WAIT) {
  765. dev_err(dev->dev, "hbm: pg entry response: state mismatch [%s, %d]\n",
  766. mei_pg_state_str(mei_pg_state(dev)), dev->pg_event);
  767. return -EPROTO;
  768. }
  769. dev->pg_event = MEI_PG_EVENT_RECEIVED;
  770. wake_up(&dev->wait_pg);
  771. return 0;
  772. }
  773. /**
  774. * mei_hbm_pg_resume - process with PG resume
  775. *
  776. * @dev: the device structure.
  777. */
  778. void mei_hbm_pg_resume(struct mei_device *dev)
  779. {
  780. pm_request_resume(dev->dev);
  781. }
  782. EXPORT_SYMBOL_GPL(mei_hbm_pg_resume);
  783. /**
  784. * mei_hbm_pg_exit_res - PG exit response received
  785. *
  786. * @dev: the device structure.
  787. *
  788. * Return: 0 on success, -EPROTO on state mismatch
  789. */
  790. static int mei_hbm_pg_exit_res(struct mei_device *dev)
  791. {
  792. if (mei_pg_state(dev) != MEI_PG_ON ||
  793. (dev->pg_event != MEI_PG_EVENT_WAIT &&
  794. dev->pg_event != MEI_PG_EVENT_IDLE)) {
  795. dev_err(dev->dev, "hbm: pg exit response: state mismatch [%s, %d]\n",
  796. mei_pg_state_str(mei_pg_state(dev)), dev->pg_event);
  797. return -EPROTO;
  798. }
  799. switch (dev->pg_event) {
  800. case MEI_PG_EVENT_WAIT:
  801. dev->pg_event = MEI_PG_EVENT_RECEIVED;
  802. wake_up(&dev->wait_pg);
  803. break;
  804. case MEI_PG_EVENT_IDLE:
  805. /*
  806. * If the driver is not waiting on this then
  807. * this is HW initiated exit from PG.
  808. * Start runtime pm resume sequence to exit from PG.
  809. */
  810. dev->pg_event = MEI_PG_EVENT_RECEIVED;
  811. mei_hbm_pg_resume(dev);
  812. break;
  813. default:
  814. WARN(1, "hbm: pg exit response: unexpected pg event = %d\n",
  815. dev->pg_event);
  816. return -EPROTO;
  817. }
  818. return 0;
  819. }
  820. /**
  821. * mei_hbm_config_features - check what hbm features and commands
  822. * are supported by the fw
  823. *
  824. * @dev: the device structure
  825. */
  826. static void mei_hbm_config_features(struct mei_device *dev)
  827. {
  828. /* Power Gating Isolation Support */
  829. dev->hbm_f_pg_supported = 0;
  830. if (dev->version.major_version > HBM_MAJOR_VERSION_PGI)
  831. dev->hbm_f_pg_supported = 1;
  832. if (dev->version.major_version == HBM_MAJOR_VERSION_PGI &&
  833. dev->version.minor_version >= HBM_MINOR_VERSION_PGI)
  834. dev->hbm_f_pg_supported = 1;
  835. if (dev->version.major_version >= HBM_MAJOR_VERSION_DC)
  836. dev->hbm_f_dc_supported = 1;
  837. /* disconnect on connect timeout instead of link reset */
  838. if (dev->version.major_version >= HBM_MAJOR_VERSION_DOT)
  839. dev->hbm_f_dot_supported = 1;
  840. /* Notification Event Support */
  841. if (dev->version.major_version >= HBM_MAJOR_VERSION_EV)
  842. dev->hbm_f_ev_supported = 1;
  843. }
  844. /**
  845. * mei_hbm_version_is_supported - checks whether the driver can
  846. * support the hbm version of the device
  847. *
  848. * @dev: the device structure
  849. * Return: true if driver can support hbm version of the device
  850. */
  851. bool mei_hbm_version_is_supported(struct mei_device *dev)
  852. {
  853. return (dev->version.major_version < HBM_MAJOR_VERSION) ||
  854. (dev->version.major_version == HBM_MAJOR_VERSION &&
  855. dev->version.minor_version <= HBM_MINOR_VERSION);
  856. }
  857. /**
  858. * mei_hbm_dispatch - bottom half read routine after ISR to
  859. * handle the read bus message cmd processing.
  860. *
  861. * @dev: the device structure
  862. * @hdr: header of bus message
  863. *
  864. * Return: 0 on success and < 0 on failure
  865. */
  866. int mei_hbm_dispatch(struct mei_device *dev, struct mei_msg_hdr *hdr)
  867. {
  868. struct mei_bus_message *mei_msg;
  869. struct hbm_host_version_response *version_res;
  870. struct hbm_props_response *props_res;
  871. struct hbm_host_enum_response *enum_res;
  872. struct hbm_add_client_request *add_cl_req;
  873. int ret;
  874. struct mei_hbm_cl_cmd *cl_cmd;
  875. struct hbm_client_connect_request *disconnect_req;
  876. struct hbm_flow_control *flow_control;
  877. /* read the message to our buffer */
  878. BUG_ON(hdr->length >= sizeof(dev->rd_msg_buf));
  879. mei_read_slots(dev, dev->rd_msg_buf, hdr->length);
  880. mei_msg = (struct mei_bus_message *)dev->rd_msg_buf;
  881. cl_cmd = (struct mei_hbm_cl_cmd *)mei_msg;
  882. /* ignore spurious message and prevent reset nesting
  883. * hbm is put to idle during system reset
  884. */
  885. if (dev->hbm_state == MEI_HBM_IDLE) {
  886. dev_dbg(dev->dev, "hbm: state is idle ignore spurious messages\n");
  887. return 0;
  888. }
  889. switch (mei_msg->hbm_cmd) {
  890. case HOST_START_RES_CMD:
  891. dev_dbg(dev->dev, "hbm: start: response message received.\n");
  892. dev->init_clients_timer = 0;
  893. version_res = (struct hbm_host_version_response *)mei_msg;
  894. dev_dbg(dev->dev, "HBM VERSION: DRIVER=%02d:%02d DEVICE=%02d:%02d\n",
  895. HBM_MAJOR_VERSION, HBM_MINOR_VERSION,
  896. version_res->me_max_version.major_version,
  897. version_res->me_max_version.minor_version);
  898. if (version_res->host_version_supported) {
  899. dev->version.major_version = HBM_MAJOR_VERSION;
  900. dev->version.minor_version = HBM_MINOR_VERSION;
  901. } else {
  902. dev->version.major_version =
  903. version_res->me_max_version.major_version;
  904. dev->version.minor_version =
  905. version_res->me_max_version.minor_version;
  906. }
  907. if (!mei_hbm_version_is_supported(dev)) {
  908. dev_warn(dev->dev, "hbm: start: version mismatch - stopping the driver.\n");
  909. dev->hbm_state = MEI_HBM_STOPPED;
  910. if (mei_hbm_stop_req(dev)) {
  911. dev_err(dev->dev, "hbm: start: failed to send stop request\n");
  912. return -EIO;
  913. }
  914. break;
  915. }
  916. mei_hbm_config_features(dev);
  917. if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
  918. dev->hbm_state != MEI_HBM_STARTING) {
  919. dev_err(dev->dev, "hbm: start: state mismatch, [%d, %d]\n",
  920. dev->dev_state, dev->hbm_state);
  921. return -EPROTO;
  922. }
  923. if (mei_hbm_enum_clients_req(dev)) {
  924. dev_err(dev->dev, "hbm: start: failed to send enumeration request\n");
  925. return -EIO;
  926. }
  927. wake_up(&dev->wait_hbm_start);
  928. break;
  929. case CLIENT_CONNECT_RES_CMD:
  930. dev_dbg(dev->dev, "hbm: client connect response: message received.\n");
  931. mei_hbm_cl_res(dev, cl_cmd, MEI_FOP_CONNECT);
  932. break;
  933. case CLIENT_DISCONNECT_RES_CMD:
  934. dev_dbg(dev->dev, "hbm: client disconnect response: message received.\n");
  935. mei_hbm_cl_res(dev, cl_cmd, MEI_FOP_DISCONNECT);
  936. break;
  937. case MEI_FLOW_CONTROL_CMD:
  938. dev_dbg(dev->dev, "hbm: client flow control response: message received.\n");
  939. flow_control = (struct hbm_flow_control *) mei_msg;
  940. mei_hbm_cl_flow_control_res(dev, flow_control);
  941. break;
  942. case MEI_PG_ISOLATION_ENTRY_RES_CMD:
  943. dev_dbg(dev->dev, "hbm: power gate isolation entry response received\n");
  944. ret = mei_hbm_pg_enter_res(dev);
  945. if (ret)
  946. return ret;
  947. break;
  948. case MEI_PG_ISOLATION_EXIT_REQ_CMD:
  949. dev_dbg(dev->dev, "hbm: power gate isolation exit request received\n");
  950. ret = mei_hbm_pg_exit_res(dev);
  951. if (ret)
  952. return ret;
  953. break;
  954. case HOST_CLIENT_PROPERTIES_RES_CMD:
  955. dev_dbg(dev->dev, "hbm: properties response: message received.\n");
  956. dev->init_clients_timer = 0;
  957. if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
  958. dev->hbm_state != MEI_HBM_CLIENT_PROPERTIES) {
  959. dev_err(dev->dev, "hbm: properties response: state mismatch, [%d, %d]\n",
  960. dev->dev_state, dev->hbm_state);
  961. return -EPROTO;
  962. }
  963. props_res = (struct hbm_props_response *)mei_msg;
  964. if (props_res->status) {
  965. dev_err(dev->dev, "hbm: properties response: wrong status = %d %s\n",
  966. props_res->status,
  967. mei_hbm_status_str(props_res->status));
  968. return -EPROTO;
  969. }
  970. mei_hbm_me_cl_add(dev, props_res);
  971. dev->me_client_index++;
  972. /* request property for the next client */
  973. if (mei_hbm_prop_req(dev))
  974. return -EIO;
  975. break;
  976. case HOST_ENUM_RES_CMD:
  977. dev_dbg(dev->dev, "hbm: enumeration response: message received\n");
  978. dev->init_clients_timer = 0;
  979. enum_res = (struct hbm_host_enum_response *) mei_msg;
  980. BUILD_BUG_ON(sizeof(dev->me_clients_map)
  981. < sizeof(enum_res->valid_addresses));
  982. memcpy(dev->me_clients_map, enum_res->valid_addresses,
  983. sizeof(enum_res->valid_addresses));
  984. if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
  985. dev->hbm_state != MEI_HBM_ENUM_CLIENTS) {
  986. dev_err(dev->dev, "hbm: enumeration response: state mismatch, [%d, %d]\n",
  987. dev->dev_state, dev->hbm_state);
  988. return -EPROTO;
  989. }
  990. dev->hbm_state = MEI_HBM_CLIENT_PROPERTIES;
  991. /* first property request */
  992. if (mei_hbm_prop_req(dev))
  993. return -EIO;
  994. break;
  995. case HOST_STOP_RES_CMD:
  996. dev_dbg(dev->dev, "hbm: stop response: message received\n");
  997. dev->init_clients_timer = 0;
  998. if (dev->hbm_state != MEI_HBM_STOPPED) {
  999. dev_err(dev->dev, "hbm: stop response: state mismatch, [%d, %d]\n",
  1000. dev->dev_state, dev->hbm_state);
  1001. return -EPROTO;
  1002. }
  1003. dev->dev_state = MEI_DEV_POWER_DOWN;
  1004. dev_info(dev->dev, "hbm: stop response: resetting.\n");
  1005. /* force the reset */
  1006. return -EPROTO;
  1007. break;
  1008. case CLIENT_DISCONNECT_REQ_CMD:
  1009. dev_dbg(dev->dev, "hbm: disconnect request: message received\n");
  1010. disconnect_req = (struct hbm_client_connect_request *)mei_msg;
  1011. mei_hbm_fw_disconnect_req(dev, disconnect_req);
  1012. break;
  1013. case ME_STOP_REQ_CMD:
  1014. dev_dbg(dev->dev, "hbm: stop request: message received\n");
  1015. dev->hbm_state = MEI_HBM_STOPPED;
  1016. if (mei_hbm_stop_req(dev)) {
  1017. dev_err(dev->dev, "hbm: stop request: failed to send stop request\n");
  1018. return -EIO;
  1019. }
  1020. break;
  1021. case MEI_HBM_ADD_CLIENT_REQ_CMD:
  1022. dev_dbg(dev->dev, "hbm: add client request received\n");
  1023. /*
  1024. * after the host receives the enum_resp
  1025. * message clients may be added or removed
  1026. */
  1027. if (dev->hbm_state <= MEI_HBM_ENUM_CLIENTS ||
  1028. dev->hbm_state >= MEI_HBM_STOPPED) {
  1029. dev_err(dev->dev, "hbm: add client: state mismatch, [%d, %d]\n",
  1030. dev->dev_state, dev->hbm_state);
  1031. return -EPROTO;
  1032. }
  1033. add_cl_req = (struct hbm_add_client_request *)mei_msg;
  1034. ret = mei_hbm_fw_add_cl_req(dev, add_cl_req);
  1035. if (ret) {
  1036. dev_err(dev->dev, "hbm: add client: failed to send response %d\n",
  1037. ret);
  1038. return -EIO;
  1039. }
  1040. dev_dbg(dev->dev, "hbm: add client request processed\n");
  1041. break;
  1042. case MEI_HBM_NOTIFY_RES_CMD:
  1043. dev_dbg(dev->dev, "hbm: notify response received\n");
  1044. mei_hbm_cl_res(dev, cl_cmd, notify_res_to_fop(cl_cmd));
  1045. break;
  1046. case MEI_HBM_NOTIFICATION_CMD:
  1047. dev_dbg(dev->dev, "hbm: notification\n");
  1048. mei_hbm_cl_notify(dev, cl_cmd);
  1049. break;
  1050. default:
  1051. BUG();
  1052. break;
  1053. }
  1054. return 0;
  1055. }