hbm.c 40 KB

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