hbm.c 30 KB

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