hbm.c 30 KB

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