hbm.c 31 KB

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