hbm.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2003-2022, Intel Corporation. All rights reserved.
  4. * Intel Management Engine Interface (Intel MEI) Linux driver
  5. */
  6. #include <linux/export.h>
  7. #include <linux/sched.h>
  8. #include <linux/wait.h>
  9. #include <linux/pm_runtime.h>
  10. #include <linux/slab.h>
  11. #include <linux/mei.h>
  12. #include "mei_dev.h"
  13. #include "hbm.h"
  14. #include "client.h"
  15. static const char *mei_hbm_status_str(enum mei_hbm_status status)
  16. {
  17. #define MEI_HBM_STATUS(status) case MEI_HBMS_##status: return #status
  18. switch (status) {
  19. MEI_HBM_STATUS(SUCCESS);
  20. MEI_HBM_STATUS(CLIENT_NOT_FOUND);
  21. MEI_HBM_STATUS(ALREADY_EXISTS);
  22. MEI_HBM_STATUS(REJECTED);
  23. MEI_HBM_STATUS(INVALID_PARAMETER);
  24. MEI_HBM_STATUS(NOT_ALLOWED);
  25. MEI_HBM_STATUS(ALREADY_STARTED);
  26. MEI_HBM_STATUS(NOT_STARTED);
  27. default: return "unknown";
  28. }
  29. #undef MEI_HBM_STATUS
  30. };
  31. static const char *mei_cl_conn_status_str(enum mei_cl_connect_status status)
  32. {
  33. #define MEI_CL_CS(status) case MEI_CL_CONN_##status: return #status
  34. switch (status) {
  35. MEI_CL_CS(SUCCESS);
  36. MEI_CL_CS(NOT_FOUND);
  37. MEI_CL_CS(ALREADY_STARTED);
  38. MEI_CL_CS(OUT_OF_RESOURCES);
  39. MEI_CL_CS(MESSAGE_SMALL);
  40. MEI_CL_CS(NOT_ALLOWED);
  41. default: return "unknown";
  42. }
  43. #undef MEI_CL_CCS
  44. }
  45. const char *mei_hbm_state_str(enum mei_hbm_state state)
  46. {
  47. #define MEI_HBM_STATE(state) case MEI_HBM_##state: return #state
  48. switch (state) {
  49. MEI_HBM_STATE(IDLE);
  50. MEI_HBM_STATE(STARTING);
  51. MEI_HBM_STATE(STARTED);
  52. MEI_HBM_STATE(DR_SETUP);
  53. MEI_HBM_STATE(ENUM_CLIENTS);
  54. MEI_HBM_STATE(CLIENT_PROPERTIES);
  55. MEI_HBM_STATE(STOPPED);
  56. default:
  57. return "unknown";
  58. }
  59. #undef MEI_HBM_STATE
  60. }
  61. /**
  62. * mei_cl_conn_status_to_errno - convert client connect response
  63. * status to error code
  64. *
  65. * @status: client connect response status
  66. *
  67. * Return: corresponding error code
  68. */
  69. static int mei_cl_conn_status_to_errno(enum mei_cl_connect_status status)
  70. {
  71. switch (status) {
  72. case MEI_CL_CONN_SUCCESS: return 0;
  73. case MEI_CL_CONN_NOT_FOUND: return -ENOTTY;
  74. case MEI_CL_CONN_ALREADY_STARTED: return -EBUSY;
  75. case MEI_CL_CONN_OUT_OF_RESOURCES: return -EBUSY;
  76. case MEI_CL_CONN_MESSAGE_SMALL: return -EINVAL;
  77. case MEI_CL_CONN_NOT_ALLOWED: return -EBUSY;
  78. default: return -EINVAL;
  79. }
  80. }
  81. /**
  82. * mei_hbm_write_message - wrapper for sending hbm messages.
  83. *
  84. * @dev: mei device
  85. * @hdr: mei header
  86. * @data: payload
  87. */
  88. static inline int mei_hbm_write_message(struct mei_device *dev,
  89. struct mei_msg_hdr *hdr,
  90. const void *data)
  91. {
  92. return mei_write_message(dev, hdr, sizeof(*hdr), data, hdr->length);
  93. }
  94. /**
  95. * mei_hbm_idle - set hbm to idle state
  96. *
  97. * @dev: the device structure
  98. */
  99. void mei_hbm_idle(struct mei_device *dev)
  100. {
  101. dev->init_clients_timer = 0;
  102. dev->hbm_state = MEI_HBM_IDLE;
  103. }
  104. /**
  105. * mei_hbm_reset - reset hbm counters and book keeping data structurs
  106. *
  107. * @dev: the device structure
  108. */
  109. void mei_hbm_reset(struct mei_device *dev)
  110. {
  111. mei_me_cl_rm_all(dev);
  112. mei_hbm_idle(dev);
  113. }
  114. /**
  115. * mei_hbm_hdr - construct hbm header
  116. *
  117. * @mei_hdr: hbm header
  118. * @length: payload length
  119. */
  120. static inline void mei_hbm_hdr(struct mei_msg_hdr *mei_hdr, size_t length)
  121. {
  122. memset(mei_hdr, 0, sizeof(*mei_hdr));
  123. mei_hdr->length = length;
  124. mei_hdr->msg_complete = 1;
  125. }
  126. /**
  127. * mei_hbm_cl_hdr - construct client hbm header
  128. *
  129. * @cl: client
  130. * @hbm_cmd: host bus message command
  131. * @buf: buffer for cl header
  132. * @len: buffer length
  133. */
  134. static inline
  135. void mei_hbm_cl_hdr(struct mei_cl *cl, u8 hbm_cmd, void *buf, size_t len)
  136. {
  137. struct mei_hbm_cl_cmd *cmd = buf;
  138. memset(cmd, 0, len);
  139. cmd->hbm_cmd = hbm_cmd;
  140. cmd->host_addr = mei_cl_host_addr(cl);
  141. cmd->me_addr = mei_cl_me_id(cl);
  142. }
  143. /**
  144. * mei_hbm_cl_write - write simple hbm client message
  145. *
  146. * @dev: the device structure
  147. * @cl: client
  148. * @hbm_cmd: host bus message command
  149. * @buf: message buffer
  150. * @len: buffer length
  151. *
  152. * Return: 0 on success, <0 on failure.
  153. */
  154. static inline int mei_hbm_cl_write(struct mei_device *dev, struct mei_cl *cl,
  155. u8 hbm_cmd, void *buf, size_t len)
  156. {
  157. struct mei_msg_hdr mei_hdr;
  158. mei_hbm_hdr(&mei_hdr, len);
  159. mei_hbm_cl_hdr(cl, hbm_cmd, buf, len);
  160. return mei_hbm_write_message(dev, &mei_hdr, buf);
  161. }
  162. /**
  163. * mei_hbm_cl_addr_equal - check if the client's and
  164. * the message address match
  165. *
  166. * @cl: client
  167. * @cmd: hbm client message
  168. *
  169. * Return: true if addresses are the same
  170. */
  171. static inline
  172. bool mei_hbm_cl_addr_equal(struct mei_cl *cl, struct mei_hbm_cl_cmd *cmd)
  173. {
  174. return mei_cl_host_addr(cl) == cmd->host_addr &&
  175. mei_cl_me_id(cl) == cmd->me_addr;
  176. }
  177. /**
  178. * mei_hbm_cl_find_by_cmd - find recipient client
  179. *
  180. * @dev: the device structure
  181. * @buf: a buffer with hbm cl command
  182. *
  183. * Return: the recipient client or NULL if not found
  184. */
  185. static inline
  186. struct mei_cl *mei_hbm_cl_find_by_cmd(struct mei_device *dev, void *buf)
  187. {
  188. struct mei_hbm_cl_cmd *cmd = (struct mei_hbm_cl_cmd *)buf;
  189. struct mei_cl *cl;
  190. list_for_each_entry(cl, &dev->file_list, link)
  191. if (mei_hbm_cl_addr_equal(cl, cmd))
  192. return cl;
  193. return NULL;
  194. }
  195. /**
  196. * mei_hbm_start_wait - wait for start response message.
  197. *
  198. * @dev: the device structure
  199. *
  200. * Return: 0 on success and < 0 on failure
  201. */
  202. int mei_hbm_start_wait(struct mei_device *dev)
  203. {
  204. int ret;
  205. if (dev->hbm_state > MEI_HBM_STARTING)
  206. return 0;
  207. mutex_unlock(&dev->device_lock);
  208. ret = wait_event_timeout(dev->wait_hbm_start,
  209. dev->hbm_state != MEI_HBM_STARTING,
  210. dev->timeouts.hbm);
  211. mutex_lock(&dev->device_lock);
  212. if (ret == 0 && (dev->hbm_state <= MEI_HBM_STARTING)) {
  213. dev->hbm_state = MEI_HBM_IDLE;
  214. dev_err(dev->dev, "waiting for mei start failed\n");
  215. return -ETIME;
  216. }
  217. return 0;
  218. }
  219. /**
  220. * mei_hbm_start_req - sends start request message.
  221. *
  222. * @dev: the device structure
  223. *
  224. * Return: 0 on success and < 0 on failure
  225. */
  226. int mei_hbm_start_req(struct mei_device *dev)
  227. {
  228. struct mei_msg_hdr mei_hdr;
  229. struct hbm_host_version_request req;
  230. int ret;
  231. mei_hbm_reset(dev);
  232. mei_hbm_hdr(&mei_hdr, sizeof(req));
  233. /* host start message */
  234. memset(&req, 0, sizeof(req));
  235. req.hbm_cmd = HOST_START_REQ_CMD;
  236. req.host_version.major_version = HBM_MAJOR_VERSION;
  237. req.host_version.minor_version = HBM_MINOR_VERSION;
  238. dev->hbm_state = MEI_HBM_IDLE;
  239. ret = mei_hbm_write_message(dev, &mei_hdr, &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 = dev->timeouts.client_init;
  247. mei_schedule_stall_timer(dev);
  248. return 0;
  249. }
  250. /**
  251. * mei_hbm_dma_setup_req() - setup DMA request
  252. * @dev: the device structure
  253. *
  254. * Return: 0 on success and < 0 on failure
  255. */
  256. static int mei_hbm_dma_setup_req(struct mei_device *dev)
  257. {
  258. struct mei_msg_hdr mei_hdr;
  259. struct hbm_dma_setup_request req;
  260. unsigned int i;
  261. int ret;
  262. mei_hbm_hdr(&mei_hdr, sizeof(req));
  263. memset(&req, 0, sizeof(req));
  264. req.hbm_cmd = MEI_HBM_DMA_SETUP_REQ_CMD;
  265. for (i = 0; i < DMA_DSCR_NUM; i++) {
  266. phys_addr_t paddr;
  267. paddr = dev->dr_dscr[i].daddr;
  268. req.dma_dscr[i].addr_hi = upper_32_bits(paddr);
  269. req.dma_dscr[i].addr_lo = lower_32_bits(paddr);
  270. req.dma_dscr[i].size = dev->dr_dscr[i].size;
  271. }
  272. mei_dma_ring_reset(dev);
  273. ret = mei_hbm_write_message(dev, &mei_hdr, &req);
  274. if (ret) {
  275. dev_err(dev->dev, "dma setup request write failed: ret = %d.\n",
  276. ret);
  277. return ret;
  278. }
  279. dev->hbm_state = MEI_HBM_DR_SETUP;
  280. dev->init_clients_timer = dev->timeouts.client_init;
  281. mei_schedule_stall_timer(dev);
  282. return 0;
  283. }
  284. /**
  285. * mei_hbm_capabilities_req - request capabilities
  286. *
  287. * @dev: the device structure
  288. *
  289. * Return: 0 on success and < 0 on failure
  290. */
  291. static int mei_hbm_capabilities_req(struct mei_device *dev)
  292. {
  293. struct mei_msg_hdr mei_hdr;
  294. struct hbm_capability_request req;
  295. int ret;
  296. mei_hbm_hdr(&mei_hdr, sizeof(req));
  297. memset(&req, 0, sizeof(req));
  298. req.hbm_cmd = MEI_HBM_CAPABILITIES_REQ_CMD;
  299. if (dev->hbm_f_vt_supported)
  300. req.capability_requested[0] |= HBM_CAP_VT;
  301. if (dev->hbm_f_cd_supported)
  302. req.capability_requested[0] |= HBM_CAP_CD;
  303. ret = mei_hbm_write_message(dev, &mei_hdr, &req);
  304. if (ret) {
  305. dev_err(dev->dev,
  306. "capabilities request write failed: ret = %d.\n", ret);
  307. return ret;
  308. }
  309. dev->hbm_state = MEI_HBM_CAP_SETUP;
  310. dev->init_clients_timer = dev->timeouts.client_init;
  311. mei_schedule_stall_timer(dev);
  312. return 0;
  313. }
  314. /**
  315. * mei_hbm_enum_clients_req - sends enumeration client request message.
  316. *
  317. * @dev: the device structure
  318. *
  319. * Return: 0 on success and < 0 on failure
  320. */
  321. static int mei_hbm_enum_clients_req(struct mei_device *dev)
  322. {
  323. struct mei_msg_hdr mei_hdr;
  324. struct hbm_host_enum_request req;
  325. int ret;
  326. /* enumerate clients */
  327. mei_hbm_hdr(&mei_hdr, sizeof(req));
  328. memset(&req, 0, sizeof(req));
  329. req.hbm_cmd = HOST_ENUM_REQ_CMD;
  330. req.flags |= dev->hbm_f_dc_supported ? MEI_HBM_ENUM_F_ALLOW_ADD : 0;
  331. req.flags |= dev->hbm_f_ie_supported ?
  332. MEI_HBM_ENUM_F_IMMEDIATE_ENUM : 0;
  333. ret = mei_hbm_write_message(dev, &mei_hdr, &req);
  334. if (ret) {
  335. dev_err(dev->dev, "enumeration request write failed: ret = %d.\n",
  336. ret);
  337. return ret;
  338. }
  339. dev->hbm_state = MEI_HBM_ENUM_CLIENTS;
  340. dev->init_clients_timer = dev->timeouts.client_init;
  341. mei_schedule_stall_timer(dev);
  342. return 0;
  343. }
  344. /**
  345. * mei_hbm_me_cl_add - add new me client to the list
  346. *
  347. * @dev: the device structure
  348. * @res: hbm property response
  349. *
  350. * Return: 0 on success and -ENOMEM on allocation failure
  351. */
  352. static int mei_hbm_me_cl_add(struct mei_device *dev,
  353. struct hbm_props_response *res)
  354. {
  355. struct mei_me_client *me_cl;
  356. const uuid_le *uuid = &res->client_properties.protocol_name;
  357. mei_me_cl_rm_by_uuid(dev, uuid);
  358. me_cl = kzalloc(sizeof(*me_cl), GFP_KERNEL);
  359. if (!me_cl)
  360. return -ENOMEM;
  361. mei_me_cl_init(me_cl);
  362. me_cl->props = res->client_properties;
  363. me_cl->client_id = res->me_addr;
  364. me_cl->tx_flow_ctrl_creds = 0;
  365. mei_me_cl_add(dev, me_cl);
  366. return 0;
  367. }
  368. /**
  369. * mei_hbm_add_cl_resp - send response to fw on client add request
  370. *
  371. * @dev: the device structure
  372. * @addr: me address
  373. * @status: response status
  374. *
  375. * Return: 0 on success and < 0 on failure
  376. */
  377. static int mei_hbm_add_cl_resp(struct mei_device *dev, u8 addr, u8 status)
  378. {
  379. struct mei_msg_hdr mei_hdr;
  380. struct hbm_add_client_response resp;
  381. int ret;
  382. dev_dbg(dev->dev, "adding client response\n");
  383. mei_hbm_hdr(&mei_hdr, sizeof(resp));
  384. memset(&resp, 0, sizeof(resp));
  385. resp.hbm_cmd = MEI_HBM_ADD_CLIENT_RES_CMD;
  386. resp.me_addr = addr;
  387. resp.status = status;
  388. ret = mei_hbm_write_message(dev, &mei_hdr, &resp);
  389. if (ret)
  390. dev_err(dev->dev, "add client response write failed: ret = %d\n",
  391. ret);
  392. return ret;
  393. }
  394. /**
  395. * mei_hbm_fw_add_cl_req - request from the fw to add a client
  396. *
  397. * @dev: the device structure
  398. * @req: add client request
  399. *
  400. * Return: 0 on success and < 0 on failure
  401. */
  402. static int mei_hbm_fw_add_cl_req(struct mei_device *dev,
  403. struct hbm_add_client_request *req)
  404. {
  405. int ret;
  406. u8 status = MEI_HBMS_SUCCESS;
  407. BUILD_BUG_ON(sizeof(struct hbm_add_client_request) !=
  408. sizeof(struct hbm_props_response));
  409. ret = mei_hbm_me_cl_add(dev, (struct hbm_props_response *)req);
  410. if (ret)
  411. status = !MEI_HBMS_SUCCESS;
  412. if (dev->dev_state == MEI_DEV_ENABLED)
  413. schedule_work(&dev->bus_rescan_work);
  414. return mei_hbm_add_cl_resp(dev, req->me_addr, status);
  415. }
  416. /**
  417. * mei_hbm_cl_notify_req - send notification request
  418. *
  419. * @dev: the device structure
  420. * @cl: a client to disconnect from
  421. * @start: true for start false for stop
  422. *
  423. * Return: 0 on success and -EIO on write failure
  424. */
  425. int mei_hbm_cl_notify_req(struct mei_device *dev,
  426. struct mei_cl *cl, u8 start)
  427. {
  428. struct mei_msg_hdr mei_hdr;
  429. struct hbm_notification_request req;
  430. int ret;
  431. mei_hbm_hdr(&mei_hdr, sizeof(req));
  432. mei_hbm_cl_hdr(cl, MEI_HBM_NOTIFY_REQ_CMD, &req, sizeof(req));
  433. req.start = start;
  434. ret = mei_hbm_write_message(dev, &mei_hdr, &req);
  435. if (ret)
  436. dev_err(dev->dev, "notify request failed: ret = %d\n", ret);
  437. return ret;
  438. }
  439. /**
  440. * notify_res_to_fop - convert notification response to the proper
  441. * notification FOP
  442. *
  443. * @cmd: client notification start response command
  444. *
  445. * Return: MEI_FOP_NOTIFY_START or MEI_FOP_NOTIFY_STOP;
  446. */
  447. static inline enum mei_cb_file_ops notify_res_to_fop(struct mei_hbm_cl_cmd *cmd)
  448. {
  449. struct hbm_notification_response *rs =
  450. (struct hbm_notification_response *)cmd;
  451. return mei_cl_notify_req2fop(rs->start);
  452. }
  453. /**
  454. * mei_hbm_cl_notify_start_res - update the client state according
  455. * notify start response
  456. *
  457. * @dev: the device structure
  458. * @cl: mei host client
  459. * @cmd: client notification start response command
  460. */
  461. static void mei_hbm_cl_notify_start_res(struct mei_device *dev,
  462. struct mei_cl *cl,
  463. struct mei_hbm_cl_cmd *cmd)
  464. {
  465. struct hbm_notification_response *rs =
  466. (struct hbm_notification_response *)cmd;
  467. cl_dbg(dev, cl, "hbm: notify start response status=%d\n", rs->status);
  468. if (rs->status == MEI_HBMS_SUCCESS ||
  469. rs->status == MEI_HBMS_ALREADY_STARTED) {
  470. cl->notify_en = true;
  471. cl->status = 0;
  472. } else {
  473. cl->status = -EINVAL;
  474. }
  475. }
  476. /**
  477. * mei_hbm_cl_notify_stop_res - update the client state according
  478. * notify stop response
  479. *
  480. * @dev: the device structure
  481. * @cl: mei host client
  482. * @cmd: client notification stop response command
  483. */
  484. static void mei_hbm_cl_notify_stop_res(struct mei_device *dev,
  485. struct mei_cl *cl,
  486. struct mei_hbm_cl_cmd *cmd)
  487. {
  488. struct hbm_notification_response *rs =
  489. (struct hbm_notification_response *)cmd;
  490. cl_dbg(dev, cl, "hbm: notify stop response status=%d\n", rs->status);
  491. if (rs->status == MEI_HBMS_SUCCESS ||
  492. rs->status == MEI_HBMS_NOT_STARTED) {
  493. cl->notify_en = false;
  494. cl->status = 0;
  495. } else {
  496. /* TODO: spec is not clear yet about other possible issues */
  497. cl->status = -EINVAL;
  498. }
  499. }
  500. /**
  501. * mei_hbm_cl_notify - signal notification event
  502. *
  503. * @dev: the device structure
  504. * @cmd: notification client message
  505. */
  506. static void mei_hbm_cl_notify(struct mei_device *dev,
  507. struct mei_hbm_cl_cmd *cmd)
  508. {
  509. struct mei_cl *cl;
  510. cl = mei_hbm_cl_find_by_cmd(dev, cmd);
  511. if (cl)
  512. mei_cl_notify(cl);
  513. }
  514. /**
  515. * mei_hbm_cl_dma_map_req - send client dma map request
  516. *
  517. * @dev: the device structure
  518. * @cl: mei host client
  519. *
  520. * Return: 0 on success and -EIO on write failure
  521. */
  522. int mei_hbm_cl_dma_map_req(struct mei_device *dev, struct mei_cl *cl)
  523. {
  524. struct mei_msg_hdr mei_hdr;
  525. struct hbm_client_dma_map_request req;
  526. int ret;
  527. mei_hbm_hdr(&mei_hdr, sizeof(req));
  528. memset(&req, 0, sizeof(req));
  529. req.hbm_cmd = MEI_HBM_CLIENT_DMA_MAP_REQ_CMD;
  530. req.client_buffer_id = cl->dma.buffer_id;
  531. req.address_lsb = lower_32_bits(cl->dma.daddr);
  532. req.address_msb = upper_32_bits(cl->dma.daddr);
  533. req.size = cl->dma.size;
  534. ret = mei_hbm_write_message(dev, &mei_hdr, &req);
  535. if (ret)
  536. dev_err(dev->dev, "dma map request failed: ret = %d\n", ret);
  537. return ret;
  538. }
  539. /**
  540. * mei_hbm_cl_dma_unmap_req - send client dma unmap request
  541. *
  542. * @dev: the device structure
  543. * @cl: mei host client
  544. *
  545. * Return: 0 on success and -EIO on write failure
  546. */
  547. int mei_hbm_cl_dma_unmap_req(struct mei_device *dev, struct mei_cl *cl)
  548. {
  549. struct mei_msg_hdr mei_hdr;
  550. struct hbm_client_dma_unmap_request req;
  551. int ret;
  552. mei_hbm_hdr(&mei_hdr, sizeof(req));
  553. memset(&req, 0, sizeof(req));
  554. req.hbm_cmd = MEI_HBM_CLIENT_DMA_UNMAP_REQ_CMD;
  555. req.client_buffer_id = cl->dma.buffer_id;
  556. ret = mei_hbm_write_message(dev, &mei_hdr, &req);
  557. if (ret)
  558. dev_err(dev->dev, "dma unmap request failed: ret = %d\n", ret);
  559. return ret;
  560. }
  561. static void mei_hbm_cl_dma_map_res(struct mei_device *dev,
  562. struct hbm_client_dma_response *res)
  563. {
  564. struct mei_cl *cl;
  565. struct mei_cl_cb *cb, *next;
  566. cl = NULL;
  567. list_for_each_entry_safe(cb, next, &dev->ctrl_rd_list, list) {
  568. if (cb->fop_type != MEI_FOP_DMA_MAP)
  569. continue;
  570. if (!cb->cl->dma.buffer_id || cb->cl->dma_mapped)
  571. continue;
  572. cl = cb->cl;
  573. break;
  574. }
  575. if (!cl)
  576. return;
  577. if (res->status) {
  578. dev_err(dev->dev, "cl dma map failed %d\n", res->status);
  579. cl->status = -EFAULT;
  580. } else {
  581. dev_dbg(dev->dev, "cl dma map succeeded\n");
  582. cl->dma_mapped = 1;
  583. cl->status = 0;
  584. }
  585. wake_up(&cl->wait);
  586. }
  587. static void mei_hbm_cl_dma_unmap_res(struct mei_device *dev,
  588. struct hbm_client_dma_response *res)
  589. {
  590. struct mei_cl *cl;
  591. struct mei_cl_cb *cb, *next;
  592. cl = NULL;
  593. list_for_each_entry_safe(cb, next, &dev->ctrl_rd_list, list) {
  594. if (cb->fop_type != MEI_FOP_DMA_UNMAP)
  595. continue;
  596. if (!cb->cl->dma.buffer_id || !cb->cl->dma_mapped)
  597. continue;
  598. cl = cb->cl;
  599. break;
  600. }
  601. if (!cl)
  602. return;
  603. if (res->status) {
  604. dev_err(dev->dev, "cl dma unmap failed %d\n", res->status);
  605. cl->status = -EFAULT;
  606. } else {
  607. dev_dbg(dev->dev, "cl dma unmap succeeded\n");
  608. cl->dma_mapped = 0;
  609. cl->status = 0;
  610. }
  611. wake_up(&cl->wait);
  612. }
  613. /**
  614. * mei_hbm_prop_req - request property for a single client
  615. *
  616. * @dev: the device structure
  617. * @start_idx: client index to start search
  618. *
  619. * Return: 0 on success and < 0 on failure
  620. */
  621. static int mei_hbm_prop_req(struct mei_device *dev, unsigned long start_idx)
  622. {
  623. struct mei_msg_hdr mei_hdr;
  624. struct hbm_props_request req;
  625. unsigned long addr;
  626. int ret;
  627. addr = find_next_bit(dev->me_clients_map, MEI_CLIENTS_MAX, start_idx);
  628. /* We got all client properties */
  629. if (addr == MEI_CLIENTS_MAX) {
  630. dev->hbm_state = MEI_HBM_STARTED;
  631. mei_host_client_init(dev);
  632. return 0;
  633. }
  634. mei_hbm_hdr(&mei_hdr, sizeof(req));
  635. memset(&req, 0, sizeof(req));
  636. req.hbm_cmd = HOST_CLIENT_PROPERTIES_REQ_CMD;
  637. req.me_addr = addr;
  638. ret = mei_hbm_write_message(dev, &mei_hdr, &req);
  639. if (ret) {
  640. dev_err(dev->dev, "properties request write failed: ret = %d\n",
  641. ret);
  642. return ret;
  643. }
  644. dev->init_clients_timer = dev->timeouts.client_init;
  645. mei_schedule_stall_timer(dev);
  646. return 0;
  647. }
  648. /**
  649. * mei_hbm_pg - sends pg command
  650. *
  651. * @dev: the device structure
  652. * @pg_cmd: the pg command code
  653. *
  654. * Return: -EIO on write failure
  655. * -EOPNOTSUPP if the operation is not supported by the protocol
  656. */
  657. int mei_hbm_pg(struct mei_device *dev, u8 pg_cmd)
  658. {
  659. struct mei_msg_hdr mei_hdr;
  660. struct hbm_power_gate req;
  661. int ret;
  662. if (!dev->hbm_f_pg_supported)
  663. return -EOPNOTSUPP;
  664. mei_hbm_hdr(&mei_hdr, sizeof(req));
  665. memset(&req, 0, sizeof(req));
  666. req.hbm_cmd = pg_cmd;
  667. ret = mei_hbm_write_message(dev, &mei_hdr, &req);
  668. if (ret)
  669. dev_err(dev->dev, "power gate command write failed.\n");
  670. return ret;
  671. }
  672. EXPORT_SYMBOL_GPL(mei_hbm_pg);
  673. /**
  674. * mei_hbm_stop_req - send stop request message
  675. *
  676. * @dev: mei device
  677. *
  678. * Return: -EIO on write failure
  679. */
  680. static int mei_hbm_stop_req(struct mei_device *dev)
  681. {
  682. struct mei_msg_hdr mei_hdr;
  683. struct hbm_host_stop_request req;
  684. mei_hbm_hdr(&mei_hdr, sizeof(req));
  685. memset(&req, 0, sizeof(req));
  686. req.hbm_cmd = HOST_STOP_REQ_CMD;
  687. req.reason = DRIVER_STOP_REQUEST;
  688. return mei_hbm_write_message(dev, &mei_hdr, &req);
  689. }
  690. /**
  691. * mei_hbm_cl_flow_control_req - sends flow control request.
  692. *
  693. * @dev: the device structure
  694. * @cl: client info
  695. *
  696. * Return: -EIO on write failure
  697. */
  698. int mei_hbm_cl_flow_control_req(struct mei_device *dev, struct mei_cl *cl)
  699. {
  700. struct hbm_flow_control req;
  701. cl_dbg(dev, cl, "sending flow control\n");
  702. return mei_hbm_cl_write(dev, cl, MEI_FLOW_CONTROL_CMD,
  703. &req, sizeof(req));
  704. }
  705. /**
  706. * mei_hbm_add_single_tx_flow_ctrl_creds - adds single buffer credentials.
  707. *
  708. * @dev: the device structure
  709. * @fctrl: flow control response bus message
  710. *
  711. * Return: 0 on success, < 0 otherwise
  712. */
  713. static int mei_hbm_add_single_tx_flow_ctrl_creds(struct mei_device *dev,
  714. struct hbm_flow_control *fctrl)
  715. {
  716. struct mei_me_client *me_cl;
  717. int rets;
  718. me_cl = mei_me_cl_by_id(dev, fctrl->me_addr);
  719. if (!me_cl) {
  720. dev_err(dev->dev, "no such me client %d\n", fctrl->me_addr);
  721. return -ENOENT;
  722. }
  723. if (WARN_ON(me_cl->props.single_recv_buf == 0)) {
  724. rets = -EINVAL;
  725. goto out;
  726. }
  727. me_cl->tx_flow_ctrl_creds++;
  728. dev_dbg(dev->dev, "recv flow ctrl msg ME %d (single) creds = %d.\n",
  729. fctrl->me_addr, me_cl->tx_flow_ctrl_creds);
  730. rets = 0;
  731. out:
  732. mei_me_cl_put(me_cl);
  733. return rets;
  734. }
  735. /**
  736. * mei_hbm_cl_tx_flow_ctrl_creds_res - flow control response from me
  737. *
  738. * @dev: the device structure
  739. * @fctrl: flow control response bus message
  740. */
  741. static void mei_hbm_cl_tx_flow_ctrl_creds_res(struct mei_device *dev,
  742. struct hbm_flow_control *fctrl)
  743. {
  744. struct mei_cl *cl;
  745. if (!fctrl->host_addr) {
  746. /* single receive buffer */
  747. mei_hbm_add_single_tx_flow_ctrl_creds(dev, fctrl);
  748. return;
  749. }
  750. cl = mei_hbm_cl_find_by_cmd(dev, fctrl);
  751. if (cl) {
  752. cl->tx_flow_ctrl_creds++;
  753. cl_dbg(dev, cl, "flow control creds = %d.\n",
  754. cl->tx_flow_ctrl_creds);
  755. }
  756. }
  757. /**
  758. * mei_hbm_cl_disconnect_req - sends disconnect message to fw.
  759. *
  760. * @dev: the device structure
  761. * @cl: a client to disconnect from
  762. *
  763. * Return: -EIO on write failure
  764. */
  765. int mei_hbm_cl_disconnect_req(struct mei_device *dev, struct mei_cl *cl)
  766. {
  767. struct hbm_client_connect_request req;
  768. return mei_hbm_cl_write(dev, cl, CLIENT_DISCONNECT_REQ_CMD,
  769. &req, sizeof(req));
  770. }
  771. /**
  772. * mei_hbm_cl_disconnect_rsp - sends disconnect respose to the FW
  773. *
  774. * @dev: the device structure
  775. * @cl: a client to disconnect from
  776. *
  777. * Return: -EIO on write failure
  778. */
  779. int mei_hbm_cl_disconnect_rsp(struct mei_device *dev, struct mei_cl *cl)
  780. {
  781. struct hbm_client_connect_response resp;
  782. return mei_hbm_cl_write(dev, cl, CLIENT_DISCONNECT_RES_CMD,
  783. &resp, sizeof(resp));
  784. }
  785. /**
  786. * mei_hbm_cl_disconnect_res - update the client state according
  787. * disconnect response
  788. *
  789. * @dev: the device structure
  790. * @cl: mei host client
  791. * @cmd: disconnect client response host bus message
  792. */
  793. static void mei_hbm_cl_disconnect_res(struct mei_device *dev, struct mei_cl *cl,
  794. struct mei_hbm_cl_cmd *cmd)
  795. {
  796. struct hbm_client_connect_response *rs =
  797. (struct hbm_client_connect_response *)cmd;
  798. cl_dbg(dev, cl, "hbm: disconnect response status=%d\n", rs->status);
  799. if (rs->status == MEI_CL_DISCONN_SUCCESS)
  800. cl->state = MEI_FILE_DISCONNECT_REPLY;
  801. cl->status = 0;
  802. }
  803. /**
  804. * mei_hbm_cl_connect_req - send connection request to specific me client
  805. *
  806. * @dev: the device structure
  807. * @cl: a client to connect to
  808. *
  809. * Return: -EIO on write failure
  810. */
  811. int mei_hbm_cl_connect_req(struct mei_device *dev, struct mei_cl *cl)
  812. {
  813. struct hbm_client_connect_request req;
  814. return mei_hbm_cl_write(dev, cl, CLIENT_CONNECT_REQ_CMD,
  815. &req, sizeof(req));
  816. }
  817. /**
  818. * mei_hbm_cl_connect_res - update the client state according
  819. * connection response
  820. *
  821. * @dev: the device structure
  822. * @cl: mei host client
  823. * @cmd: connect client response host bus message
  824. */
  825. static void mei_hbm_cl_connect_res(struct mei_device *dev, struct mei_cl *cl,
  826. struct mei_hbm_cl_cmd *cmd)
  827. {
  828. struct hbm_client_connect_response *rs =
  829. (struct hbm_client_connect_response *)cmd;
  830. cl_dbg(dev, cl, "hbm: connect response status=%s\n",
  831. mei_cl_conn_status_str(rs->status));
  832. if (rs->status == MEI_CL_CONN_SUCCESS)
  833. cl->state = MEI_FILE_CONNECTED;
  834. else {
  835. cl->state = MEI_FILE_DISCONNECT_REPLY;
  836. if (rs->status == MEI_CL_CONN_NOT_FOUND) {
  837. mei_me_cl_del(dev, cl->me_cl);
  838. if (dev->dev_state == MEI_DEV_ENABLED)
  839. schedule_work(&dev->bus_rescan_work);
  840. }
  841. }
  842. cl->status = mei_cl_conn_status_to_errno(rs->status);
  843. }
  844. /**
  845. * mei_hbm_cl_res - process hbm response received on behalf
  846. * an client
  847. *
  848. * @dev: the device structure
  849. * @rs: hbm client message
  850. * @fop_type: file operation type
  851. */
  852. static void mei_hbm_cl_res(struct mei_device *dev,
  853. struct mei_hbm_cl_cmd *rs,
  854. enum mei_cb_file_ops fop_type)
  855. {
  856. struct mei_cl *cl;
  857. struct mei_cl_cb *cb, *next;
  858. cl = NULL;
  859. list_for_each_entry_safe(cb, next, &dev->ctrl_rd_list, list) {
  860. cl = cb->cl;
  861. if (cb->fop_type != fop_type)
  862. continue;
  863. if (mei_hbm_cl_addr_equal(cl, rs)) {
  864. list_del_init(&cb->list);
  865. break;
  866. }
  867. }
  868. if (!cl)
  869. return;
  870. switch (fop_type) {
  871. case MEI_FOP_CONNECT:
  872. mei_hbm_cl_connect_res(dev, cl, rs);
  873. break;
  874. case MEI_FOP_DISCONNECT:
  875. mei_hbm_cl_disconnect_res(dev, cl, rs);
  876. break;
  877. case MEI_FOP_NOTIFY_START:
  878. mei_hbm_cl_notify_start_res(dev, cl, rs);
  879. break;
  880. case MEI_FOP_NOTIFY_STOP:
  881. mei_hbm_cl_notify_stop_res(dev, cl, rs);
  882. break;
  883. default:
  884. return;
  885. }
  886. cl->timer_count = 0;
  887. wake_up(&cl->wait);
  888. }
  889. /**
  890. * mei_hbm_fw_disconnect_req - disconnect request initiated by ME firmware
  891. * host sends disconnect response
  892. *
  893. * @dev: the device structure.
  894. * @disconnect_req: disconnect request bus message from the me
  895. *
  896. * Return: -ENOMEM on allocation failure
  897. */
  898. static int mei_hbm_fw_disconnect_req(struct mei_device *dev,
  899. struct hbm_client_connect_request *disconnect_req)
  900. {
  901. struct mei_cl *cl;
  902. struct mei_cl_cb *cb;
  903. cl = mei_hbm_cl_find_by_cmd(dev, disconnect_req);
  904. if (cl) {
  905. cl_warn(dev, cl, "fw disconnect request received\n");
  906. cl->state = MEI_FILE_DISCONNECTING;
  907. cl->timer_count = 0;
  908. cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DISCONNECT_RSP,
  909. NULL);
  910. if (!cb)
  911. return -ENOMEM;
  912. }
  913. return 0;
  914. }
  915. /**
  916. * mei_hbm_pg_enter_res - PG enter response received
  917. *
  918. * @dev: the device structure.
  919. *
  920. * Return: 0 on success, -EPROTO on state mismatch
  921. */
  922. static int mei_hbm_pg_enter_res(struct mei_device *dev)
  923. {
  924. if (mei_pg_state(dev) != MEI_PG_OFF ||
  925. dev->pg_event != MEI_PG_EVENT_WAIT) {
  926. dev_err(dev->dev, "hbm: pg entry response: state mismatch [%s, %d]\n",
  927. mei_pg_state_str(mei_pg_state(dev)), dev->pg_event);
  928. return -EPROTO;
  929. }
  930. dev->pg_event = MEI_PG_EVENT_RECEIVED;
  931. wake_up(&dev->wait_pg);
  932. return 0;
  933. }
  934. /**
  935. * mei_hbm_pg_resume - process with PG resume
  936. *
  937. * @dev: the device structure.
  938. */
  939. void mei_hbm_pg_resume(struct mei_device *dev)
  940. {
  941. pm_request_resume(dev->dev);
  942. }
  943. EXPORT_SYMBOL_GPL(mei_hbm_pg_resume);
  944. /**
  945. * mei_hbm_pg_exit_res - PG exit response received
  946. *
  947. * @dev: the device structure.
  948. *
  949. * Return: 0 on success, -EPROTO on state mismatch
  950. */
  951. static int mei_hbm_pg_exit_res(struct mei_device *dev)
  952. {
  953. if (mei_pg_state(dev) != MEI_PG_ON ||
  954. (dev->pg_event != MEI_PG_EVENT_WAIT &&
  955. dev->pg_event != MEI_PG_EVENT_IDLE)) {
  956. dev_err(dev->dev, "hbm: pg exit response: state mismatch [%s, %d]\n",
  957. mei_pg_state_str(mei_pg_state(dev)), dev->pg_event);
  958. return -EPROTO;
  959. }
  960. switch (dev->pg_event) {
  961. case MEI_PG_EVENT_WAIT:
  962. dev->pg_event = MEI_PG_EVENT_RECEIVED;
  963. wake_up(&dev->wait_pg);
  964. break;
  965. case MEI_PG_EVENT_IDLE:
  966. /*
  967. * If the driver is not waiting on this then
  968. * this is HW initiated exit from PG.
  969. * Start runtime pm resume sequence to exit from PG.
  970. */
  971. dev->pg_event = MEI_PG_EVENT_RECEIVED;
  972. mei_hbm_pg_resume(dev);
  973. break;
  974. default:
  975. WARN(1, "hbm: pg exit response: unexpected pg event = %d\n",
  976. dev->pg_event);
  977. return -EPROTO;
  978. }
  979. return 0;
  980. }
  981. /**
  982. * mei_hbm_config_features - check what hbm features and commands
  983. * are supported by the fw
  984. *
  985. * @dev: the device structure
  986. */
  987. static void mei_hbm_config_features(struct mei_device *dev)
  988. {
  989. /* Power Gating Isolation Support */
  990. dev->hbm_f_pg_supported = 0;
  991. if (dev->version.major_version > HBM_MAJOR_VERSION_PGI)
  992. dev->hbm_f_pg_supported = 1;
  993. if (dev->version.major_version == HBM_MAJOR_VERSION_PGI &&
  994. dev->version.minor_version >= HBM_MINOR_VERSION_PGI)
  995. dev->hbm_f_pg_supported = 1;
  996. dev->hbm_f_dc_supported = 0;
  997. if (dev->version.major_version >= HBM_MAJOR_VERSION_DC)
  998. dev->hbm_f_dc_supported = 1;
  999. dev->hbm_f_ie_supported = 0;
  1000. if (dev->version.major_version >= HBM_MAJOR_VERSION_IE)
  1001. dev->hbm_f_ie_supported = 1;
  1002. /* disconnect on connect timeout instead of link reset */
  1003. dev->hbm_f_dot_supported = 0;
  1004. if (dev->version.major_version >= HBM_MAJOR_VERSION_DOT)
  1005. dev->hbm_f_dot_supported = 1;
  1006. /* Notification Event Support */
  1007. dev->hbm_f_ev_supported = 0;
  1008. if (dev->version.major_version >= HBM_MAJOR_VERSION_EV)
  1009. dev->hbm_f_ev_supported = 1;
  1010. /* Fixed Address Client Support */
  1011. dev->hbm_f_fa_supported = 0;
  1012. if (dev->version.major_version >= HBM_MAJOR_VERSION_FA)
  1013. dev->hbm_f_fa_supported = 1;
  1014. /* OS ver message Support */
  1015. dev->hbm_f_os_supported = 0;
  1016. if (dev->version.major_version >= HBM_MAJOR_VERSION_OS)
  1017. dev->hbm_f_os_supported = 1;
  1018. /* DMA Ring Support */
  1019. dev->hbm_f_dr_supported = 0;
  1020. if (dev->version.major_version > HBM_MAJOR_VERSION_DR ||
  1021. (dev->version.major_version == HBM_MAJOR_VERSION_DR &&
  1022. dev->version.minor_version >= HBM_MINOR_VERSION_DR))
  1023. dev->hbm_f_dr_supported = 1;
  1024. /* VTag Support */
  1025. dev->hbm_f_vt_supported = 0;
  1026. if (dev->version.major_version > HBM_MAJOR_VERSION_VT ||
  1027. (dev->version.major_version == HBM_MAJOR_VERSION_VT &&
  1028. dev->version.minor_version >= HBM_MINOR_VERSION_VT))
  1029. dev->hbm_f_vt_supported = 1;
  1030. /* Capability message Support */
  1031. dev->hbm_f_cap_supported = 0;
  1032. if (dev->version.major_version > HBM_MAJOR_VERSION_CAP ||
  1033. (dev->version.major_version == HBM_MAJOR_VERSION_CAP &&
  1034. dev->version.minor_version >= HBM_MINOR_VERSION_CAP))
  1035. dev->hbm_f_cap_supported = 1;
  1036. /* Client DMA Support */
  1037. dev->hbm_f_cd_supported = 0;
  1038. if (dev->version.major_version > HBM_MAJOR_VERSION_CD ||
  1039. (dev->version.major_version == HBM_MAJOR_VERSION_CD &&
  1040. dev->version.minor_version >= HBM_MINOR_VERSION_CD))
  1041. dev->hbm_f_cd_supported = 1;
  1042. }
  1043. /**
  1044. * mei_hbm_version_is_supported - checks whether the driver can
  1045. * support the hbm version of the device
  1046. *
  1047. * @dev: the device structure
  1048. * Return: true if driver can support hbm version of the device
  1049. */
  1050. bool mei_hbm_version_is_supported(struct mei_device *dev)
  1051. {
  1052. return (dev->version.major_version < HBM_MAJOR_VERSION) ||
  1053. (dev->version.major_version == HBM_MAJOR_VERSION &&
  1054. dev->version.minor_version <= HBM_MINOR_VERSION);
  1055. }
  1056. /**
  1057. * mei_hbm_dispatch - bottom half read routine after ISR to
  1058. * handle the read bus message cmd processing.
  1059. *
  1060. * @dev: the device structure
  1061. * @hdr: header of bus message
  1062. *
  1063. * Return: 0 on success and < 0 on failure
  1064. */
  1065. int mei_hbm_dispatch(struct mei_device *dev, struct mei_msg_hdr *hdr)
  1066. {
  1067. struct mei_bus_message *mei_msg;
  1068. struct hbm_host_version_response *version_res;
  1069. struct hbm_props_response *props_res;
  1070. struct hbm_host_enum_response *enum_res;
  1071. struct hbm_dma_setup_response *dma_setup_res;
  1072. struct hbm_add_client_request *add_cl_req;
  1073. struct hbm_capability_response *capability_res;
  1074. int ret;
  1075. struct mei_hbm_cl_cmd *cl_cmd;
  1076. struct hbm_client_connect_request *disconnect_req;
  1077. struct hbm_flow_control *fctrl;
  1078. struct hbm_client_dma_response *client_dma_res;
  1079. /* read the message to our buffer */
  1080. BUG_ON(hdr->length >= sizeof(dev->rd_msg_buf));
  1081. mei_read_slots(dev, dev->rd_msg_buf, hdr->length);
  1082. mei_msg = (struct mei_bus_message *)dev->rd_msg_buf;
  1083. cl_cmd = (struct mei_hbm_cl_cmd *)mei_msg;
  1084. /* ignore spurious message and prevent reset nesting
  1085. * hbm is put to idle during system reset
  1086. */
  1087. if (dev->hbm_state == MEI_HBM_IDLE) {
  1088. dev_dbg(dev->dev, "hbm: state is idle ignore spurious messages\n");
  1089. return 0;
  1090. }
  1091. switch (mei_msg->hbm_cmd) {
  1092. case HOST_START_RES_CMD:
  1093. dev_dbg(dev->dev, "hbm: start: response message received.\n");
  1094. dev->init_clients_timer = 0;
  1095. version_res = (struct hbm_host_version_response *)mei_msg;
  1096. dev_dbg(dev->dev, "HBM VERSION: DRIVER=%02d:%02d DEVICE=%02d:%02d\n",
  1097. HBM_MAJOR_VERSION, HBM_MINOR_VERSION,
  1098. version_res->me_max_version.major_version,
  1099. version_res->me_max_version.minor_version);
  1100. if (version_res->host_version_supported) {
  1101. dev->version.major_version = HBM_MAJOR_VERSION;
  1102. dev->version.minor_version = HBM_MINOR_VERSION;
  1103. } else {
  1104. dev->version.major_version =
  1105. version_res->me_max_version.major_version;
  1106. dev->version.minor_version =
  1107. version_res->me_max_version.minor_version;
  1108. }
  1109. if (!mei_hbm_version_is_supported(dev)) {
  1110. dev_warn(dev->dev, "hbm: start: version mismatch - stopping the driver.\n");
  1111. dev->hbm_state = MEI_HBM_STOPPED;
  1112. if (mei_hbm_stop_req(dev)) {
  1113. dev_err(dev->dev, "hbm: start: failed to send stop request\n");
  1114. return -EIO;
  1115. }
  1116. break;
  1117. }
  1118. mei_hbm_config_features(dev);
  1119. if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
  1120. dev->hbm_state != MEI_HBM_STARTING) {
  1121. if (dev->dev_state == MEI_DEV_POWER_DOWN ||
  1122. dev->dev_state == MEI_DEV_POWERING_DOWN) {
  1123. dev_dbg(dev->dev, "hbm: start: on shutdown, ignoring\n");
  1124. return 0;
  1125. }
  1126. dev_err(dev->dev, "hbm: start: state mismatch, [%d, %d]\n",
  1127. dev->dev_state, dev->hbm_state);
  1128. return -EPROTO;
  1129. }
  1130. if (dev->hbm_f_cap_supported) {
  1131. if (mei_hbm_capabilities_req(dev))
  1132. return -EIO;
  1133. wake_up(&dev->wait_hbm_start);
  1134. break;
  1135. }
  1136. if (dev->hbm_f_dr_supported) {
  1137. if (mei_dmam_ring_alloc(dev))
  1138. dev_info(dev->dev, "running w/o dma ring\n");
  1139. if (mei_dma_ring_is_allocated(dev)) {
  1140. if (mei_hbm_dma_setup_req(dev))
  1141. return -EIO;
  1142. wake_up(&dev->wait_hbm_start);
  1143. break;
  1144. }
  1145. }
  1146. dev->hbm_f_dr_supported = 0;
  1147. mei_dmam_ring_free(dev);
  1148. if (mei_hbm_enum_clients_req(dev))
  1149. return -EIO;
  1150. wake_up(&dev->wait_hbm_start);
  1151. break;
  1152. case MEI_HBM_CAPABILITIES_RES_CMD:
  1153. dev_dbg(dev->dev, "hbm: capabilities response: message received.\n");
  1154. dev->init_clients_timer = 0;
  1155. if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
  1156. dev->hbm_state != MEI_HBM_CAP_SETUP) {
  1157. if (dev->dev_state == MEI_DEV_POWER_DOWN ||
  1158. dev->dev_state == MEI_DEV_POWERING_DOWN) {
  1159. dev_dbg(dev->dev, "hbm: capabilities response: on shutdown, ignoring\n");
  1160. return 0;
  1161. }
  1162. dev_err(dev->dev, "hbm: capabilities response: state mismatch, [%d, %d]\n",
  1163. dev->dev_state, dev->hbm_state);
  1164. return -EPROTO;
  1165. }
  1166. capability_res = (struct hbm_capability_response *)mei_msg;
  1167. if (!(capability_res->capability_granted[0] & HBM_CAP_VT))
  1168. dev->hbm_f_vt_supported = 0;
  1169. if (!(capability_res->capability_granted[0] & HBM_CAP_CD))
  1170. dev->hbm_f_cd_supported = 0;
  1171. if (dev->hbm_f_dr_supported) {
  1172. if (mei_dmam_ring_alloc(dev))
  1173. dev_info(dev->dev, "running w/o dma ring\n");
  1174. if (mei_dma_ring_is_allocated(dev)) {
  1175. if (mei_hbm_dma_setup_req(dev))
  1176. return -EIO;
  1177. break;
  1178. }
  1179. }
  1180. dev->hbm_f_dr_supported = 0;
  1181. mei_dmam_ring_free(dev);
  1182. if (mei_hbm_enum_clients_req(dev))
  1183. return -EIO;
  1184. break;
  1185. case MEI_HBM_DMA_SETUP_RES_CMD:
  1186. dev_dbg(dev->dev, "hbm: dma setup response: message received.\n");
  1187. dev->init_clients_timer = 0;
  1188. if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
  1189. dev->hbm_state != MEI_HBM_DR_SETUP) {
  1190. if (dev->dev_state == MEI_DEV_POWER_DOWN ||
  1191. dev->dev_state == MEI_DEV_POWERING_DOWN) {
  1192. dev_dbg(dev->dev, "hbm: dma setup response: on shutdown, ignoring\n");
  1193. return 0;
  1194. }
  1195. dev_err(dev->dev, "hbm: dma setup response: state mismatch, [%d, %d]\n",
  1196. dev->dev_state, dev->hbm_state);
  1197. return -EPROTO;
  1198. }
  1199. dma_setup_res = (struct hbm_dma_setup_response *)mei_msg;
  1200. if (dma_setup_res->status) {
  1201. u8 status = dma_setup_res->status;
  1202. if (status == MEI_HBMS_NOT_ALLOWED) {
  1203. dev_dbg(dev->dev, "hbm: dma setup not allowed\n");
  1204. } else {
  1205. dev_info(dev->dev, "hbm: dma setup response: failure = %d %s\n",
  1206. status,
  1207. mei_hbm_status_str(status));
  1208. }
  1209. dev->hbm_f_dr_supported = 0;
  1210. mei_dmam_ring_free(dev);
  1211. }
  1212. if (mei_hbm_enum_clients_req(dev))
  1213. return -EIO;
  1214. break;
  1215. case CLIENT_CONNECT_RES_CMD:
  1216. dev_dbg(dev->dev, "hbm: client connect response: message received.\n");
  1217. mei_hbm_cl_res(dev, cl_cmd, MEI_FOP_CONNECT);
  1218. break;
  1219. case CLIENT_DISCONNECT_RES_CMD:
  1220. dev_dbg(dev->dev, "hbm: client disconnect response: message received.\n");
  1221. mei_hbm_cl_res(dev, cl_cmd, MEI_FOP_DISCONNECT);
  1222. break;
  1223. case MEI_FLOW_CONTROL_CMD:
  1224. dev_dbg(dev->dev, "hbm: client flow control response: message received.\n");
  1225. fctrl = (struct hbm_flow_control *)mei_msg;
  1226. mei_hbm_cl_tx_flow_ctrl_creds_res(dev, fctrl);
  1227. break;
  1228. case MEI_PG_ISOLATION_ENTRY_RES_CMD:
  1229. dev_dbg(dev->dev, "hbm: power gate isolation entry response received\n");
  1230. ret = mei_hbm_pg_enter_res(dev);
  1231. if (ret)
  1232. return ret;
  1233. break;
  1234. case MEI_PG_ISOLATION_EXIT_REQ_CMD:
  1235. dev_dbg(dev->dev, "hbm: power gate isolation exit request received\n");
  1236. ret = mei_hbm_pg_exit_res(dev);
  1237. if (ret)
  1238. return ret;
  1239. break;
  1240. case HOST_CLIENT_PROPERTIES_RES_CMD:
  1241. dev_dbg(dev->dev, "hbm: properties response: message received.\n");
  1242. dev->init_clients_timer = 0;
  1243. if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
  1244. dev->hbm_state != MEI_HBM_CLIENT_PROPERTIES) {
  1245. if (dev->dev_state == MEI_DEV_POWER_DOWN ||
  1246. dev->dev_state == MEI_DEV_POWERING_DOWN) {
  1247. dev_dbg(dev->dev, "hbm: properties response: on shutdown, ignoring\n");
  1248. return 0;
  1249. }
  1250. dev_err(dev->dev, "hbm: properties response: state mismatch, [%d, %d]\n",
  1251. dev->dev_state, dev->hbm_state);
  1252. return -EPROTO;
  1253. }
  1254. props_res = (struct hbm_props_response *)mei_msg;
  1255. if (props_res->status == MEI_HBMS_CLIENT_NOT_FOUND) {
  1256. dev_dbg(dev->dev, "hbm: properties response: %d CLIENT_NOT_FOUND\n",
  1257. props_res->me_addr);
  1258. } else if (props_res->status) {
  1259. dev_err(dev->dev, "hbm: properties response: wrong status = %d %s\n",
  1260. props_res->status,
  1261. mei_hbm_status_str(props_res->status));
  1262. return -EPROTO;
  1263. } else {
  1264. mei_hbm_me_cl_add(dev, props_res);
  1265. }
  1266. /* request property for the next client */
  1267. if (mei_hbm_prop_req(dev, props_res->me_addr + 1))
  1268. return -EIO;
  1269. break;
  1270. case HOST_ENUM_RES_CMD:
  1271. dev_dbg(dev->dev, "hbm: enumeration response: message received\n");
  1272. dev->init_clients_timer = 0;
  1273. enum_res = (struct hbm_host_enum_response *) mei_msg;
  1274. BUILD_BUG_ON(sizeof(dev->me_clients_map)
  1275. < sizeof(enum_res->valid_addresses));
  1276. memcpy(dev->me_clients_map, enum_res->valid_addresses,
  1277. sizeof(enum_res->valid_addresses));
  1278. if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
  1279. dev->hbm_state != MEI_HBM_ENUM_CLIENTS) {
  1280. if (dev->dev_state == MEI_DEV_POWER_DOWN ||
  1281. dev->dev_state == MEI_DEV_POWERING_DOWN) {
  1282. dev_dbg(dev->dev, "hbm: enumeration response: on shutdown, ignoring\n");
  1283. return 0;
  1284. }
  1285. dev_err(dev->dev, "hbm: enumeration response: state mismatch, [%d, %d]\n",
  1286. dev->dev_state, dev->hbm_state);
  1287. return -EPROTO;
  1288. }
  1289. dev->hbm_state = MEI_HBM_CLIENT_PROPERTIES;
  1290. /* first property request */
  1291. if (mei_hbm_prop_req(dev, 0))
  1292. return -EIO;
  1293. break;
  1294. case HOST_STOP_RES_CMD:
  1295. dev_dbg(dev->dev, "hbm: stop response: message received\n");
  1296. dev->init_clients_timer = 0;
  1297. if (dev->hbm_state != MEI_HBM_STOPPED) {
  1298. dev_err(dev->dev, "hbm: stop response: state mismatch, [%d, %d]\n",
  1299. dev->dev_state, dev->hbm_state);
  1300. return -EPROTO;
  1301. }
  1302. mei_set_devstate(dev, MEI_DEV_POWER_DOWN);
  1303. dev_info(dev->dev, "hbm: stop response: resetting.\n");
  1304. /* force the reset */
  1305. return -EPROTO;
  1306. case CLIENT_DISCONNECT_REQ_CMD:
  1307. dev_dbg(dev->dev, "hbm: disconnect request: message received\n");
  1308. disconnect_req = (struct hbm_client_connect_request *)mei_msg;
  1309. mei_hbm_fw_disconnect_req(dev, disconnect_req);
  1310. break;
  1311. case ME_STOP_REQ_CMD:
  1312. dev_dbg(dev->dev, "hbm: stop request: message received\n");
  1313. dev->hbm_state = MEI_HBM_STOPPED;
  1314. if (mei_hbm_stop_req(dev)) {
  1315. dev_err(dev->dev, "hbm: stop request: failed to send stop request\n");
  1316. return -EIO;
  1317. }
  1318. break;
  1319. case MEI_HBM_ADD_CLIENT_REQ_CMD:
  1320. dev_dbg(dev->dev, "hbm: add client request received\n");
  1321. /*
  1322. * after the host receives the enum_resp
  1323. * message clients may be added or removed
  1324. */
  1325. if (dev->hbm_state <= MEI_HBM_ENUM_CLIENTS ||
  1326. dev->hbm_state >= MEI_HBM_STOPPED) {
  1327. dev_err(dev->dev, "hbm: add client: state mismatch, [%d, %d]\n",
  1328. dev->dev_state, dev->hbm_state);
  1329. return -EPROTO;
  1330. }
  1331. add_cl_req = (struct hbm_add_client_request *)mei_msg;
  1332. ret = mei_hbm_fw_add_cl_req(dev, add_cl_req);
  1333. if (ret) {
  1334. dev_err(dev->dev, "hbm: add client: failed to send response %d\n",
  1335. ret);
  1336. return -EIO;
  1337. }
  1338. dev_dbg(dev->dev, "hbm: add client request processed\n");
  1339. break;
  1340. case MEI_HBM_NOTIFY_RES_CMD:
  1341. dev_dbg(dev->dev, "hbm: notify response received\n");
  1342. mei_hbm_cl_res(dev, cl_cmd, notify_res_to_fop(cl_cmd));
  1343. break;
  1344. case MEI_HBM_NOTIFICATION_CMD:
  1345. dev_dbg(dev->dev, "hbm: notification\n");
  1346. mei_hbm_cl_notify(dev, cl_cmd);
  1347. break;
  1348. case MEI_HBM_CLIENT_DMA_MAP_RES_CMD:
  1349. dev_dbg(dev->dev, "hbm: client dma map response: message received.\n");
  1350. client_dma_res = (struct hbm_client_dma_response *)mei_msg;
  1351. mei_hbm_cl_dma_map_res(dev, client_dma_res);
  1352. break;
  1353. case MEI_HBM_CLIENT_DMA_UNMAP_RES_CMD:
  1354. dev_dbg(dev->dev, "hbm: client dma unmap response: message received.\n");
  1355. client_dma_res = (struct hbm_client_dma_response *)mei_msg;
  1356. mei_hbm_cl_dma_unmap_res(dev, client_dma_res);
  1357. break;
  1358. default:
  1359. WARN(1, "hbm: wrong command %d\n", mei_msg->hbm_cmd);
  1360. return -EPROTO;
  1361. }
  1362. return 0;
  1363. }