hbm.c 30 KB

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