mcdi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Management-Controller-to-Driver Interface
  4. *
  5. * Copyright 2008-2013 Solarflare Communications Inc.
  6. * Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/slab.h>
  10. #include <linux/io.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/ethtool.h>
  15. #include <linux/if_vlan.h>
  16. #include <linux/timer.h>
  17. #include <linux/list.h>
  18. #include <linux/pci.h>
  19. #include <linux/device.h>
  20. #include <linux/rwsem.h>
  21. #include <linux/vmalloc.h>
  22. #include <net/netevent.h>
  23. #include <linux/log2.h>
  24. #include <linux/net_tstamp.h>
  25. #include <linux/wait.h>
  26. #include <linux/cdx/bitfield.h>
  27. #include <linux/cdx/mcdi.h>
  28. #include "mcdid.h"
  29. static void cdx_mcdi_cancel_cmd(struct cdx_mcdi *cdx, struct cdx_mcdi_cmd *cmd);
  30. static void cdx_mcdi_wait_for_cleanup(struct cdx_mcdi *cdx);
  31. static int cdx_mcdi_rpc_async_internal(struct cdx_mcdi *cdx,
  32. struct cdx_mcdi_cmd *cmd,
  33. unsigned int *handle);
  34. static void cdx_mcdi_start_or_queue(struct cdx_mcdi_iface *mcdi,
  35. bool allow_retry);
  36. static void cdx_mcdi_cmd_start_or_queue(struct cdx_mcdi_iface *mcdi,
  37. struct cdx_mcdi_cmd *cmd);
  38. static bool cdx_mcdi_complete_cmd(struct cdx_mcdi_iface *mcdi,
  39. struct cdx_mcdi_cmd *cmd,
  40. struct cdx_dword *outbuf,
  41. int len,
  42. struct list_head *cleanup_list);
  43. static void cdx_mcdi_timeout_cmd(struct cdx_mcdi_iface *mcdi,
  44. struct cdx_mcdi_cmd *cmd,
  45. struct list_head *cleanup_list);
  46. static void cdx_mcdi_cmd_work(struct work_struct *context);
  47. static void cdx_mcdi_mode_fail(struct cdx_mcdi *cdx, struct list_head *cleanup_list);
  48. static void _cdx_mcdi_display_error(struct cdx_mcdi *cdx, unsigned int cmd,
  49. size_t inlen, int raw, int arg, int err_no);
  50. static bool cdx_cmd_cancelled(struct cdx_mcdi_cmd *cmd)
  51. {
  52. return cmd->state == MCDI_STATE_RUNNING_CANCELLED;
  53. }
  54. static void cdx_mcdi_cmd_release(struct kref *ref)
  55. {
  56. kfree(container_of(ref, struct cdx_mcdi_cmd, ref));
  57. }
  58. static unsigned int cdx_mcdi_cmd_handle(struct cdx_mcdi_cmd *cmd)
  59. {
  60. return cmd->handle;
  61. }
  62. static void _cdx_mcdi_remove_cmd(struct cdx_mcdi_iface *mcdi,
  63. struct cdx_mcdi_cmd *cmd,
  64. struct list_head *cleanup_list)
  65. {
  66. /* if cancelled, the completers have already been called */
  67. if (cdx_cmd_cancelled(cmd))
  68. return;
  69. if (cmd->completer) {
  70. list_add_tail(&cmd->cleanup_list, cleanup_list);
  71. ++mcdi->outstanding_cleanups;
  72. kref_get(&cmd->ref);
  73. }
  74. }
  75. static void cdx_mcdi_remove_cmd(struct cdx_mcdi_iface *mcdi,
  76. struct cdx_mcdi_cmd *cmd,
  77. struct list_head *cleanup_list)
  78. {
  79. list_del(&cmd->list);
  80. _cdx_mcdi_remove_cmd(mcdi, cmd, cleanup_list);
  81. cmd->state = MCDI_STATE_FINISHED;
  82. kref_put(&cmd->ref, cdx_mcdi_cmd_release);
  83. if (list_empty(&mcdi->cmd_list))
  84. wake_up(&mcdi->cmd_complete_wq);
  85. }
  86. static unsigned long cdx_mcdi_rpc_timeout(struct cdx_mcdi *cdx, unsigned int cmd)
  87. {
  88. if (!cdx->mcdi_ops->mcdi_rpc_timeout)
  89. return MCDI_RPC_TIMEOUT;
  90. else
  91. return cdx->mcdi_ops->mcdi_rpc_timeout(cdx, cmd);
  92. }
  93. /**
  94. * cdx_mcdi_init - Initialize MCDI (Management Controller Driver Interface) state
  95. * @cdx: Handle to the CDX MCDI structure
  96. *
  97. * This function allocates and initializes internal MCDI structures and resources
  98. * for the CDX device, including the workqueue, locking primitives, and command
  99. * tracking mechanisms. It sets the initial operating mode and prepares the device
  100. * for MCDI operations.
  101. *
  102. * Return:
  103. * * 0 - on success
  104. * * -ENOMEM - if memory allocation or workqueue creation fails
  105. */
  106. int cdx_mcdi_init(struct cdx_mcdi *cdx)
  107. {
  108. struct cdx_mcdi_iface *mcdi;
  109. int rc = -ENOMEM;
  110. cdx->mcdi = kzalloc_obj(*cdx->mcdi);
  111. if (!cdx->mcdi)
  112. goto fail;
  113. mcdi = cdx_mcdi_if(cdx);
  114. mcdi->cdx = cdx;
  115. mcdi->workqueue = alloc_ordered_workqueue("mcdi_wq", 0);
  116. if (!mcdi->workqueue)
  117. goto fail2;
  118. mutex_init(&mcdi->iface_lock);
  119. mcdi->mode = MCDI_MODE_EVENTS;
  120. INIT_LIST_HEAD(&mcdi->cmd_list);
  121. init_waitqueue_head(&mcdi->cmd_complete_wq);
  122. mcdi->new_epoch = true;
  123. return 0;
  124. fail2:
  125. kfree(cdx->mcdi);
  126. cdx->mcdi = NULL;
  127. fail:
  128. return rc;
  129. }
  130. EXPORT_SYMBOL_GPL(cdx_mcdi_init);
  131. /**
  132. * cdx_mcdi_finish - Cleanup MCDI (Management Controller Driver Interface) state
  133. * @cdx: Handle to the CDX MCDI structure
  134. *
  135. * This function is responsible for cleaning up the MCDI (Management Controller Driver Interface)
  136. * resources associated with a cdx_mcdi structure. Also destroys the mcdi workqueue.
  137. *
  138. */
  139. void cdx_mcdi_finish(struct cdx_mcdi *cdx)
  140. {
  141. struct cdx_mcdi_iface *mcdi;
  142. mcdi = cdx_mcdi_if(cdx);
  143. if (!mcdi)
  144. return;
  145. cdx_mcdi_wait_for_cleanup(cdx);
  146. destroy_workqueue(mcdi->workqueue);
  147. kfree(cdx->mcdi);
  148. cdx->mcdi = NULL;
  149. }
  150. EXPORT_SYMBOL_GPL(cdx_mcdi_finish);
  151. static bool cdx_mcdi_flushed(struct cdx_mcdi_iface *mcdi, bool ignore_cleanups)
  152. {
  153. bool flushed;
  154. mutex_lock(&mcdi->iface_lock);
  155. flushed = list_empty(&mcdi->cmd_list) &&
  156. (ignore_cleanups || !mcdi->outstanding_cleanups);
  157. mutex_unlock(&mcdi->iface_lock);
  158. return flushed;
  159. }
  160. /* Wait for outstanding MCDI commands to complete. */
  161. static void cdx_mcdi_wait_for_cleanup(struct cdx_mcdi *cdx)
  162. {
  163. struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
  164. if (!mcdi)
  165. return;
  166. wait_event(mcdi->cmd_complete_wq,
  167. cdx_mcdi_flushed(mcdi, false));
  168. }
  169. int cdx_mcdi_wait_for_quiescence(struct cdx_mcdi *cdx,
  170. unsigned int timeout_jiffies)
  171. {
  172. struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
  173. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  174. int rc = 0;
  175. if (!mcdi)
  176. return -EINVAL;
  177. flush_workqueue(mcdi->workqueue);
  178. add_wait_queue(&mcdi->cmd_complete_wq, &wait);
  179. while (!cdx_mcdi_flushed(mcdi, true)) {
  180. rc = wait_woken(&wait, TASK_IDLE, timeout_jiffies);
  181. if (rc)
  182. continue;
  183. break;
  184. }
  185. remove_wait_queue(&mcdi->cmd_complete_wq, &wait);
  186. if (rc > 0)
  187. rc = 0;
  188. else if (rc == 0)
  189. rc = -ETIMEDOUT;
  190. return rc;
  191. }
  192. static u8 cdx_mcdi_payload_csum(const struct cdx_dword *hdr, size_t hdr_len,
  193. const struct cdx_dword *sdu, size_t sdu_len)
  194. {
  195. u8 *p = (u8 *)hdr;
  196. u8 csum = 0;
  197. int i;
  198. for (i = 0; i < hdr_len; i++)
  199. csum += p[i];
  200. p = (u8 *)sdu;
  201. for (i = 0; i < sdu_len; i++)
  202. csum += p[i];
  203. return ~csum & 0xff;
  204. }
  205. static void cdx_mcdi_send_request(struct cdx_mcdi *cdx,
  206. struct cdx_mcdi_cmd *cmd)
  207. {
  208. struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
  209. const struct cdx_dword *inbuf = cmd->inbuf;
  210. size_t inlen = cmd->inlen;
  211. struct cdx_dword hdr[2];
  212. size_t hdr_len;
  213. bool not_epoch;
  214. u32 xflags;
  215. if (!mcdi)
  216. return;
  217. mcdi->prev_seq = cmd->seq;
  218. mcdi->seq_held_by[cmd->seq] = cmd;
  219. mcdi->db_held_by = cmd;
  220. cmd->started = jiffies;
  221. not_epoch = !mcdi->new_epoch;
  222. xflags = 0;
  223. /* MCDI v2 */
  224. WARN_ON(inlen > MCDI_CTL_SDU_LEN_MAX_V2);
  225. CDX_POPULATE_DWORD_7(hdr[0],
  226. MCDI_HEADER_RESPONSE, 0,
  227. MCDI_HEADER_RESYNC, 1,
  228. MCDI_HEADER_CODE, MC_CMD_V2_EXTN,
  229. MCDI_HEADER_DATALEN, 0,
  230. MCDI_HEADER_SEQ, cmd->seq,
  231. MCDI_HEADER_XFLAGS, xflags,
  232. MCDI_HEADER_NOT_EPOCH, not_epoch);
  233. CDX_POPULATE_DWORD_3(hdr[1],
  234. MC_CMD_V2_EXTN_IN_EXTENDED_CMD, cmd->cmd,
  235. MC_CMD_V2_EXTN_IN_ACTUAL_LEN, inlen,
  236. MC_CMD_V2_EXTN_IN_MESSAGE_TYPE,
  237. MC_CMD_V2_EXTN_IN_MCDI_MESSAGE_TYPE_PLATFORM);
  238. hdr_len = 8;
  239. hdr[0].cdx_u32 |= (__force __le32)(cdx_mcdi_payload_csum(hdr, hdr_len, inbuf, inlen) <<
  240. MCDI_HEADER_XFLAGS_LBN);
  241. print_hex_dump_debug("MCDI REQ HEADER: ", DUMP_PREFIX_NONE, 32, 4, hdr, hdr_len, false);
  242. print_hex_dump_debug("MCDI REQ PAYLOAD: ", DUMP_PREFIX_NONE, 32, 4, inbuf, inlen, false);
  243. cdx->mcdi_ops->mcdi_request(cdx, hdr, hdr_len, inbuf, inlen);
  244. mcdi->new_epoch = false;
  245. }
  246. static int cdx_mcdi_errno(struct cdx_mcdi *cdx, unsigned int mcdi_err)
  247. {
  248. switch (mcdi_err) {
  249. case 0:
  250. case MC_CMD_ERR_QUEUE_FULL:
  251. return mcdi_err;
  252. case MC_CMD_ERR_EPERM:
  253. return -EPERM;
  254. case MC_CMD_ERR_ENOENT:
  255. return -ENOENT;
  256. case MC_CMD_ERR_EINTR:
  257. return -EINTR;
  258. case MC_CMD_ERR_EAGAIN:
  259. return -EAGAIN;
  260. case MC_CMD_ERR_EACCES:
  261. return -EACCES;
  262. case MC_CMD_ERR_EBUSY:
  263. return -EBUSY;
  264. case MC_CMD_ERR_EINVAL:
  265. return -EINVAL;
  266. case MC_CMD_ERR_ERANGE:
  267. return -ERANGE;
  268. case MC_CMD_ERR_EDEADLK:
  269. return -EDEADLK;
  270. case MC_CMD_ERR_ENOSYS:
  271. return -EOPNOTSUPP;
  272. case MC_CMD_ERR_ETIME:
  273. return -ETIME;
  274. case MC_CMD_ERR_EALREADY:
  275. return -EALREADY;
  276. case MC_CMD_ERR_ENOSPC:
  277. return -ENOSPC;
  278. case MC_CMD_ERR_ENOMEM:
  279. return -ENOMEM;
  280. case MC_CMD_ERR_ENOTSUP:
  281. return -EOPNOTSUPP;
  282. case MC_CMD_ERR_ALLOC_FAIL:
  283. return -ENOBUFS;
  284. case MC_CMD_ERR_MAC_EXIST:
  285. return -EADDRINUSE;
  286. case MC_CMD_ERR_NO_EVB_PORT:
  287. return -EAGAIN;
  288. default:
  289. return -EPROTO;
  290. }
  291. }
  292. static void cdx_mcdi_process_cleanup_list(struct cdx_mcdi *cdx,
  293. struct list_head *cleanup_list)
  294. {
  295. struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
  296. unsigned int cleanups = 0;
  297. if (!mcdi)
  298. return;
  299. while (!list_empty(cleanup_list)) {
  300. struct cdx_mcdi_cmd *cmd =
  301. list_first_entry(cleanup_list,
  302. struct cdx_mcdi_cmd, cleanup_list);
  303. cmd->completer(cdx, cmd->cookie, cmd->rc,
  304. cmd->outbuf, cmd->outlen);
  305. list_del(&cmd->cleanup_list);
  306. kref_put(&cmd->ref, cdx_mcdi_cmd_release);
  307. ++cleanups;
  308. }
  309. if (cleanups) {
  310. bool all_done;
  311. mutex_lock(&mcdi->iface_lock);
  312. CDX_WARN_ON_PARANOID(cleanups > mcdi->outstanding_cleanups);
  313. all_done = (mcdi->outstanding_cleanups -= cleanups) == 0;
  314. mutex_unlock(&mcdi->iface_lock);
  315. if (all_done)
  316. wake_up(&mcdi->cmd_complete_wq);
  317. }
  318. }
  319. static void _cdx_mcdi_cancel_cmd(struct cdx_mcdi_iface *mcdi,
  320. unsigned int handle,
  321. struct list_head *cleanup_list)
  322. {
  323. struct cdx_mcdi_cmd *cmd;
  324. list_for_each_entry(cmd, &mcdi->cmd_list, list)
  325. if (cdx_mcdi_cmd_handle(cmd) == handle) {
  326. switch (cmd->state) {
  327. case MCDI_STATE_QUEUED:
  328. case MCDI_STATE_RETRY:
  329. pr_debug("command %#x inlen %zu cancelled in queue\n",
  330. cmd->cmd, cmd->inlen);
  331. /* if not yet running, properly cancel it */
  332. cmd->rc = -EPIPE;
  333. cdx_mcdi_remove_cmd(mcdi, cmd, cleanup_list);
  334. break;
  335. case MCDI_STATE_RUNNING:
  336. case MCDI_STATE_RUNNING_CANCELLED:
  337. case MCDI_STATE_FINISHED:
  338. default:
  339. /* invalid state? */
  340. WARN_ON(1);
  341. }
  342. break;
  343. }
  344. }
  345. static void cdx_mcdi_cancel_cmd(struct cdx_mcdi *cdx, struct cdx_mcdi_cmd *cmd)
  346. {
  347. struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
  348. LIST_HEAD(cleanup_list);
  349. if (!mcdi)
  350. return;
  351. mutex_lock(&mcdi->iface_lock);
  352. cdx_mcdi_timeout_cmd(mcdi, cmd, &cleanup_list);
  353. mutex_unlock(&mcdi->iface_lock);
  354. cdx_mcdi_process_cleanup_list(cdx, &cleanup_list);
  355. }
  356. struct cdx_mcdi_blocking_data {
  357. struct kref ref;
  358. bool done;
  359. wait_queue_head_t wq;
  360. int rc;
  361. struct cdx_dword *outbuf;
  362. size_t outlen;
  363. size_t outlen_actual;
  364. };
  365. static void cdx_mcdi_blocking_data_release(struct kref *ref)
  366. {
  367. kfree(container_of(ref, struct cdx_mcdi_blocking_data, ref));
  368. }
  369. static void cdx_mcdi_rpc_completer(struct cdx_mcdi *cdx, unsigned long cookie,
  370. int rc, struct cdx_dword *outbuf,
  371. size_t outlen_actual)
  372. {
  373. struct cdx_mcdi_blocking_data *wait_data =
  374. (struct cdx_mcdi_blocking_data *)cookie;
  375. wait_data->rc = rc;
  376. memcpy(wait_data->outbuf, outbuf,
  377. min(outlen_actual, wait_data->outlen));
  378. wait_data->outlen_actual = outlen_actual;
  379. /* memory barrier */
  380. smp_wmb();
  381. wait_data->done = true;
  382. wake_up(&wait_data->wq);
  383. kref_put(&wait_data->ref, cdx_mcdi_blocking_data_release);
  384. }
  385. static int cdx_mcdi_rpc_sync(struct cdx_mcdi *cdx, unsigned int cmd,
  386. const struct cdx_dword *inbuf, size_t inlen,
  387. struct cdx_dword *outbuf, size_t outlen,
  388. size_t *outlen_actual, bool quiet)
  389. {
  390. struct cdx_mcdi_blocking_data *wait_data;
  391. struct cdx_mcdi_cmd *cmd_item;
  392. unsigned int handle;
  393. int rc;
  394. if (outlen_actual)
  395. *outlen_actual = 0;
  396. wait_data = kmalloc_obj(*wait_data);
  397. if (!wait_data)
  398. return -ENOMEM;
  399. cmd_item = kmalloc_obj(*cmd_item);
  400. if (!cmd_item) {
  401. kfree(wait_data);
  402. return -ENOMEM;
  403. }
  404. kref_init(&wait_data->ref);
  405. wait_data->done = false;
  406. init_waitqueue_head(&wait_data->wq);
  407. wait_data->outbuf = outbuf;
  408. wait_data->outlen = outlen;
  409. kref_init(&cmd_item->ref);
  410. cmd_item->quiet = quiet;
  411. cmd_item->cookie = (unsigned long)wait_data;
  412. cmd_item->completer = &cdx_mcdi_rpc_completer;
  413. cmd_item->cmd = cmd;
  414. cmd_item->inlen = inlen;
  415. cmd_item->inbuf = inbuf;
  416. /* Claim an extra reference for the completer to put. */
  417. kref_get(&wait_data->ref);
  418. rc = cdx_mcdi_rpc_async_internal(cdx, cmd_item, &handle);
  419. if (rc) {
  420. kref_put(&wait_data->ref, cdx_mcdi_blocking_data_release);
  421. goto out;
  422. }
  423. if (!wait_event_timeout(wait_data->wq, wait_data->done,
  424. cdx_mcdi_rpc_timeout(cdx, cmd)) &&
  425. !wait_data->done) {
  426. pr_err("MC command 0x%x inlen %zu timed out (sync)\n",
  427. cmd, inlen);
  428. cdx_mcdi_cancel_cmd(cdx, cmd_item);
  429. wait_data->rc = -ETIMEDOUT;
  430. wait_data->outlen_actual = 0;
  431. }
  432. if (outlen_actual)
  433. *outlen_actual = wait_data->outlen_actual;
  434. rc = wait_data->rc;
  435. out:
  436. kref_put(&wait_data->ref, cdx_mcdi_blocking_data_release);
  437. return rc;
  438. }
  439. static bool cdx_mcdi_get_seq(struct cdx_mcdi_iface *mcdi, unsigned char *seq)
  440. {
  441. *seq = mcdi->prev_seq;
  442. do {
  443. *seq = (*seq + 1) % ARRAY_SIZE(mcdi->seq_held_by);
  444. } while (mcdi->seq_held_by[*seq] && *seq != mcdi->prev_seq);
  445. return !mcdi->seq_held_by[*seq];
  446. }
  447. static int cdx_mcdi_rpc_async_internal(struct cdx_mcdi *cdx,
  448. struct cdx_mcdi_cmd *cmd,
  449. unsigned int *handle)
  450. {
  451. struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
  452. LIST_HEAD(cleanup_list);
  453. if (!mcdi) {
  454. kref_put(&cmd->ref, cdx_mcdi_cmd_release);
  455. return -ENETDOWN;
  456. }
  457. if (mcdi->mode == MCDI_MODE_FAIL) {
  458. kref_put(&cmd->ref, cdx_mcdi_cmd_release);
  459. return -ENETDOWN;
  460. }
  461. cmd->mcdi = mcdi;
  462. INIT_WORK(&cmd->work, cdx_mcdi_cmd_work);
  463. INIT_LIST_HEAD(&cmd->list);
  464. INIT_LIST_HEAD(&cmd->cleanup_list);
  465. cmd->rc = 0;
  466. cmd->outbuf = NULL;
  467. cmd->outlen = 0;
  468. queue_work(mcdi->workqueue, &cmd->work);
  469. return 0;
  470. }
  471. static void cdx_mcdi_cmd_start_or_queue(struct cdx_mcdi_iface *mcdi,
  472. struct cdx_mcdi_cmd *cmd)
  473. {
  474. struct cdx_mcdi *cdx = mcdi->cdx;
  475. u8 seq;
  476. if (!mcdi->db_held_by &&
  477. cdx_mcdi_get_seq(mcdi, &seq)) {
  478. cmd->seq = seq;
  479. cmd->reboot_seen = false;
  480. cdx_mcdi_send_request(cdx, cmd);
  481. cmd->state = MCDI_STATE_RUNNING;
  482. } else {
  483. cmd->state = MCDI_STATE_QUEUED;
  484. }
  485. }
  486. /* try to advance other commands */
  487. static void cdx_mcdi_start_or_queue(struct cdx_mcdi_iface *mcdi,
  488. bool allow_retry)
  489. {
  490. struct cdx_mcdi_cmd *cmd, *tmp;
  491. list_for_each_entry_safe(cmd, tmp, &mcdi->cmd_list, list)
  492. if (cmd->state == MCDI_STATE_QUEUED ||
  493. (cmd->state == MCDI_STATE_RETRY && allow_retry))
  494. cdx_mcdi_cmd_start_or_queue(mcdi, cmd);
  495. }
  496. /**
  497. * cdx_mcdi_process_cmd - Process an incoming MCDI response
  498. * @cdx: Handle to the CDX MCDI structure
  499. * @outbuf: Pointer to the response buffer received from the management controller
  500. * @len: Length of the response buffer in bytes
  501. *
  502. * This function handles a response from the management controller. It locates the
  503. * corresponding command using the sequence number embedded in the header,
  504. * completes the command if it is still pending, and initiates any necessary cleanup.
  505. *
  506. * The function assumes that the response buffer is well-formed and at least one
  507. * dword in size.
  508. */
  509. void cdx_mcdi_process_cmd(struct cdx_mcdi *cdx, struct cdx_dword *outbuf, int len)
  510. {
  511. struct cdx_mcdi_iface *mcdi;
  512. struct cdx_mcdi_cmd *cmd;
  513. LIST_HEAD(cleanup_list);
  514. unsigned int respseq;
  515. if (!len || !outbuf) {
  516. pr_err("Got empty MC response\n");
  517. return;
  518. }
  519. mcdi = cdx_mcdi_if(cdx);
  520. if (!mcdi)
  521. return;
  522. respseq = CDX_DWORD_FIELD(outbuf[0], MCDI_HEADER_SEQ);
  523. mutex_lock(&mcdi->iface_lock);
  524. cmd = mcdi->seq_held_by[respseq];
  525. if (cmd) {
  526. if (cmd->state == MCDI_STATE_FINISHED) {
  527. mutex_unlock(&mcdi->iface_lock);
  528. kref_put(&cmd->ref, cdx_mcdi_cmd_release);
  529. return;
  530. }
  531. cdx_mcdi_complete_cmd(mcdi, cmd, outbuf, len, &cleanup_list);
  532. } else {
  533. pr_err("MC response unexpected for seq : %0X\n", respseq);
  534. }
  535. mutex_unlock(&mcdi->iface_lock);
  536. cdx_mcdi_process_cleanup_list(mcdi->cdx, &cleanup_list);
  537. }
  538. EXPORT_SYMBOL_GPL(cdx_mcdi_process_cmd);
  539. static void cdx_mcdi_cmd_work(struct work_struct *context)
  540. {
  541. struct cdx_mcdi_cmd *cmd =
  542. container_of(context, struct cdx_mcdi_cmd, work);
  543. struct cdx_mcdi_iface *mcdi = cmd->mcdi;
  544. mutex_lock(&mcdi->iface_lock);
  545. cmd->handle = mcdi->prev_handle++;
  546. list_add_tail(&cmd->list, &mcdi->cmd_list);
  547. cdx_mcdi_cmd_start_or_queue(mcdi, cmd);
  548. mutex_unlock(&mcdi->iface_lock);
  549. }
  550. /*
  551. * Returns true if the MCDI module is finished with the command.
  552. * (examples of false would be if the command was proxied, or it was
  553. * rejected by the MC due to lack of resources and requeued).
  554. */
  555. static bool cdx_mcdi_complete_cmd(struct cdx_mcdi_iface *mcdi,
  556. struct cdx_mcdi_cmd *cmd,
  557. struct cdx_dword *outbuf,
  558. int len,
  559. struct list_head *cleanup_list)
  560. {
  561. size_t resp_hdr_len, resp_data_len;
  562. struct cdx_mcdi *cdx = mcdi->cdx;
  563. unsigned int respcmd, error;
  564. bool completed = false;
  565. int rc;
  566. /* ensure the command can't go away before this function returns */
  567. kref_get(&cmd->ref);
  568. respcmd = CDX_DWORD_FIELD(outbuf[0], MCDI_HEADER_CODE);
  569. error = CDX_DWORD_FIELD(outbuf[0], MCDI_HEADER_ERROR);
  570. if (respcmd != MC_CMD_V2_EXTN) {
  571. resp_hdr_len = 4;
  572. resp_data_len = CDX_DWORD_FIELD(outbuf[0], MCDI_HEADER_DATALEN);
  573. } else {
  574. resp_data_len = 0;
  575. resp_hdr_len = 8;
  576. if (len >= 8)
  577. resp_data_len =
  578. CDX_DWORD_FIELD(outbuf[1], MC_CMD_V2_EXTN_IN_ACTUAL_LEN);
  579. }
  580. if ((resp_hdr_len + resp_data_len) > len) {
  581. pr_warn("Incomplete MCDI response received %d. Expected %zu\n",
  582. len, (resp_hdr_len + resp_data_len));
  583. resp_data_len = 0;
  584. }
  585. print_hex_dump_debug("MCDI RESP HEADER: ", DUMP_PREFIX_NONE, 32, 4,
  586. outbuf, resp_hdr_len, false);
  587. print_hex_dump_debug("MCDI RESP PAYLOAD: ", DUMP_PREFIX_NONE, 32, 4,
  588. outbuf + (resp_hdr_len / 4), resp_data_len, false);
  589. if (error && resp_data_len == 0) {
  590. /* MC rebooted during command */
  591. rc = -EIO;
  592. } else {
  593. if (WARN_ON_ONCE(error && resp_data_len < 4))
  594. resp_data_len = 4;
  595. if (error) {
  596. rc = CDX_DWORD_FIELD(outbuf[resp_hdr_len / 4], CDX_DWORD);
  597. if (!cmd->quiet) {
  598. int err_arg = 0;
  599. if (resp_data_len >= MC_CMD_ERR_ARG_OFST + 4) {
  600. int offset = (resp_hdr_len + MC_CMD_ERR_ARG_OFST) / 4;
  601. err_arg = CDX_DWORD_VAL(outbuf[offset]);
  602. }
  603. _cdx_mcdi_display_error(cdx, cmd->cmd,
  604. cmd->inlen, rc, err_arg,
  605. cdx_mcdi_errno(cdx, rc));
  606. }
  607. rc = cdx_mcdi_errno(cdx, rc);
  608. } else {
  609. rc = 0;
  610. }
  611. }
  612. /* free doorbell */
  613. if (mcdi->db_held_by == cmd)
  614. mcdi->db_held_by = NULL;
  615. if (cdx_cmd_cancelled(cmd)) {
  616. list_del(&cmd->list);
  617. kref_put(&cmd->ref, cdx_mcdi_cmd_release);
  618. completed = true;
  619. } else if (rc == MC_CMD_ERR_QUEUE_FULL) {
  620. cmd->state = MCDI_STATE_RETRY;
  621. } else {
  622. cmd->rc = rc;
  623. cmd->outbuf = outbuf + DIV_ROUND_UP(resp_hdr_len, 4);
  624. cmd->outlen = resp_data_len;
  625. cdx_mcdi_remove_cmd(mcdi, cmd, cleanup_list);
  626. completed = true;
  627. }
  628. /* free sequence number and buffer */
  629. mcdi->seq_held_by[cmd->seq] = NULL;
  630. cdx_mcdi_start_or_queue(mcdi, rc != MC_CMD_ERR_QUEUE_FULL);
  631. /* wake up anyone waiting for flush */
  632. wake_up(&mcdi->cmd_complete_wq);
  633. kref_put(&cmd->ref, cdx_mcdi_cmd_release);
  634. return completed;
  635. }
  636. static void cdx_mcdi_timeout_cmd(struct cdx_mcdi_iface *mcdi,
  637. struct cdx_mcdi_cmd *cmd,
  638. struct list_head *cleanup_list)
  639. {
  640. struct cdx_mcdi *cdx = mcdi->cdx;
  641. pr_err("MC command 0x%x inlen %zu state %d timed out after %u ms\n",
  642. cmd->cmd, cmd->inlen, cmd->state,
  643. jiffies_to_msecs(jiffies - cmd->started));
  644. cmd->rc = -ETIMEDOUT;
  645. cdx_mcdi_remove_cmd(mcdi, cmd, cleanup_list);
  646. cdx_mcdi_mode_fail(cdx, cleanup_list);
  647. }
  648. /**
  649. * cdx_mcdi_rpc - Issue an MCDI command and wait for completion
  650. * @cdx: NIC through which to issue the command
  651. * @cmd: Command type number
  652. * @inbuf: Command parameters
  653. * @inlen: Length of command parameters, in bytes. Must be a multiple
  654. * of 4 and no greater than %MCDI_CTL_SDU_LEN_MAX_V1.
  655. * @outbuf: Response buffer. May be %NULL if @outlen is 0.
  656. * @outlen: Length of response buffer, in bytes. If the actual
  657. * response is longer than @outlen & ~3, it will be truncated
  658. * to that length.
  659. * @outlen_actual: Pointer through which to return the actual response
  660. * length. May be %NULL if this is not needed.
  661. *
  662. * This function may sleep and therefore must be called in process
  663. * context.
  664. *
  665. * Return: A negative error code, or zero if successful. The error
  666. * code may come from the MCDI response or may indicate a failure
  667. * to communicate with the MC. In the former case, the response
  668. * will still be copied to @outbuf and *@outlen_actual will be
  669. * set accordingly. In the latter case, *@outlen_actual will be
  670. * set to zero.
  671. */
  672. int cdx_mcdi_rpc(struct cdx_mcdi *cdx, unsigned int cmd,
  673. const struct cdx_dword *inbuf, size_t inlen,
  674. struct cdx_dword *outbuf, size_t outlen,
  675. size_t *outlen_actual)
  676. {
  677. return cdx_mcdi_rpc_sync(cdx, cmd, inbuf, inlen, outbuf, outlen,
  678. outlen_actual, false);
  679. }
  680. EXPORT_SYMBOL_GPL(cdx_mcdi_rpc);
  681. /**
  682. * cdx_mcdi_rpc_async - Schedule an MCDI command to run asynchronously
  683. * @cdx: NIC through which to issue the command
  684. * @cmd: Command type number
  685. * @inbuf: Command parameters
  686. * @inlen: Length of command parameters, in bytes
  687. * @complete: Function to be called on completion or cancellation.
  688. * @cookie: Arbitrary value to be passed to @complete.
  689. *
  690. * This function does not sleep and therefore may be called in atomic
  691. * context. It will fail if event queues are disabled or if MCDI
  692. * event completions have been disabled due to an error.
  693. *
  694. * If it succeeds, the @complete function will be called exactly once
  695. * in process context, when one of the following occurs:
  696. * (a) the completion event is received (in process context)
  697. * (b) event queues are disabled (in the process that disables them)
  698. */
  699. int
  700. cdx_mcdi_rpc_async(struct cdx_mcdi *cdx, unsigned int cmd,
  701. const struct cdx_dword *inbuf, size_t inlen,
  702. cdx_mcdi_async_completer *complete, unsigned long cookie)
  703. {
  704. struct cdx_mcdi_cmd *cmd_item =
  705. kmalloc(sizeof(struct cdx_mcdi_cmd) + inlen, GFP_ATOMIC);
  706. if (!cmd_item)
  707. return -ENOMEM;
  708. kref_init(&cmd_item->ref);
  709. cmd_item->quiet = true;
  710. cmd_item->cookie = cookie;
  711. cmd_item->completer = complete;
  712. cmd_item->cmd = cmd;
  713. cmd_item->inlen = inlen;
  714. /* inbuf is probably not valid after return, so take a copy */
  715. cmd_item->inbuf = (struct cdx_dword *)(cmd_item + 1);
  716. memcpy(cmd_item + 1, inbuf, inlen);
  717. return cdx_mcdi_rpc_async_internal(cdx, cmd_item, NULL);
  718. }
  719. static void _cdx_mcdi_display_error(struct cdx_mcdi *cdx, unsigned int cmd,
  720. size_t inlen, int raw, int arg, int err_no)
  721. {
  722. pr_err("MC command 0x%x inlen %d failed err_no=%d (raw=%d) arg=%d\n",
  723. cmd, (int)inlen, err_no, raw, arg);
  724. }
  725. /*
  726. * Set MCDI mode to fail to prevent any new commands, then cancel any
  727. * outstanding commands.
  728. * Caller must hold the mcdi iface_lock.
  729. */
  730. static void cdx_mcdi_mode_fail(struct cdx_mcdi *cdx, struct list_head *cleanup_list)
  731. {
  732. struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
  733. if (!mcdi)
  734. return;
  735. mcdi->mode = MCDI_MODE_FAIL;
  736. while (!list_empty(&mcdi->cmd_list)) {
  737. struct cdx_mcdi_cmd *cmd;
  738. cmd = list_first_entry(&mcdi->cmd_list, struct cdx_mcdi_cmd,
  739. list);
  740. _cdx_mcdi_cancel_cmd(mcdi, cdx_mcdi_cmd_handle(cmd), cleanup_list);
  741. }
  742. }