hbm.c 40 KB

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