bus-fixup.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2013-2022, Intel Corporation. All rights reserved.
  4. * Intel Management Engine Interface (Intel MEI) Linux driver
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/sched.h>
  8. #include <linux/module.h>
  9. #include <linux/device.h>
  10. #include <linux/slab.h>
  11. #include <linux/uuid.h>
  12. #include <linux/mei_cl_bus.h>
  13. #include "mei_dev.h"
  14. #include "client.h"
  15. #include "mkhi.h"
  16. #define MEI_UUID_NFC_INFO UUID_LE(0xd2de1625, 0x382d, 0x417d, \
  17. 0x48, 0xa4, 0xef, 0xab, 0xba, 0x8a, 0x12, 0x06)
  18. static const uuid_le mei_nfc_info_guid = MEI_UUID_NFC_INFO;
  19. #define MEI_UUID_NFC_HCI UUID_LE(0x0bb17a78, 0x2a8e, 0x4c50, \
  20. 0x94, 0xd4, 0x50, 0x26, 0x67, 0x23, 0x77, 0x5c)
  21. #define MEI_UUID_WD UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, \
  22. 0x89, 0x9D, 0xA9, 0x15, 0x14, 0xCB, 0x32, 0xAB)
  23. #define MEI_UUID_MKHIF_FIX UUID_LE(0x55213584, 0x9a29, 0x4916, \
  24. 0xba, 0xdf, 0xf, 0xb7, 0xed, 0x68, 0x2a, 0xeb)
  25. #define MEI_UUID_IGSC_MKHI UUID_LE(0xE2C2AFA2, 0x3817, 0x4D19, \
  26. 0x9D, 0x95, 0x06, 0xB1, 0x6B, 0x58, 0x8A, 0x5D)
  27. #define MEI_UUID_IGSC_MKHI_FIX UUID_LE(0x46E0C1FB, 0xA546, 0x414F, \
  28. 0x91, 0x70, 0xB7, 0xF4, 0x6D, 0x57, 0xB4, 0xAD)
  29. #define MEI_UUID_HDCP UUID_LE(0xB638AB7E, 0x94E2, 0x4EA2, \
  30. 0xA5, 0x52, 0xD1, 0xC5, 0x4B, 0x62, 0x7F, 0x04)
  31. #define MEI_UUID_PAVP UUID_LE(0xfbf6fcf1, 0x96cf, 0x4e2e, 0xA6, \
  32. 0xa6, 0x1b, 0xab, 0x8c, 0xbe, 0x36, 0xb1)
  33. #define MEI_UUID_ANY NULL_UUID_LE
  34. /**
  35. * number_of_connections - determine whether an client be on the bus
  36. * according number of connections
  37. * We support only clients:
  38. * 1. with single connection
  39. * 2. and fixed clients (max_number_of_connections == 0)
  40. *
  41. * @cldev: me clients device
  42. */
  43. static void number_of_connections(struct mei_cl_device *cldev)
  44. {
  45. if (cldev->me_cl->props.max_number_of_connections > 1)
  46. cldev->do_match = 0;
  47. }
  48. /**
  49. * blacklist - blacklist a client from the bus
  50. *
  51. * @cldev: me clients device
  52. */
  53. static void blacklist(struct mei_cl_device *cldev)
  54. {
  55. cldev->do_match = 0;
  56. }
  57. /**
  58. * whitelist - forcefully whitelist client
  59. *
  60. * @cldev: me clients device
  61. */
  62. static void whitelist(struct mei_cl_device *cldev)
  63. {
  64. cldev->do_match = 1;
  65. }
  66. #define OSTYPE_LINUX 2
  67. struct mei_os_ver {
  68. __le16 build;
  69. __le16 reserved1;
  70. u8 os_type;
  71. u8 major;
  72. u8 minor;
  73. u8 reserved2;
  74. } __packed;
  75. struct mkhi_fw_ver_block {
  76. u16 minor;
  77. u8 major;
  78. u8 platform;
  79. u16 buildno;
  80. u16 hotfix;
  81. } __packed;
  82. struct mkhi_fw_ver {
  83. struct mkhi_fw_ver_block ver[MEI_MAX_FW_VER_BLOCKS];
  84. } __packed;
  85. #define MKHI_OSVER_BUF_LEN (sizeof(struct mkhi_msg_hdr) + \
  86. sizeof(struct mkhi_fwcaps) + \
  87. sizeof(struct mei_os_ver))
  88. static int mei_osver(struct mei_cl_device *cldev)
  89. {
  90. const size_t size = MKHI_OSVER_BUF_LEN;
  91. char buf[MKHI_OSVER_BUF_LEN];
  92. struct mkhi_msg *req;
  93. struct mkhi_fwcaps *fwcaps;
  94. struct mei_os_ver *os_ver;
  95. unsigned int mode = MEI_CL_IO_TX_BLOCKING | MEI_CL_IO_TX_INTERNAL;
  96. memset(buf, 0, size);
  97. req = (struct mkhi_msg *)buf;
  98. req->hdr.group_id = MKHI_FWCAPS_GROUP_ID;
  99. req->hdr.command = MKHI_FWCAPS_SET_OS_VER_APP_RULE_CMD;
  100. fwcaps = (struct mkhi_fwcaps *)req->data;
  101. fwcaps->id.rule_type = 0x0;
  102. fwcaps->id.feature_id = MKHI_FEATURE_PTT;
  103. fwcaps->len = sizeof(*os_ver);
  104. os_ver = (struct mei_os_ver *)fwcaps->data;
  105. os_ver->os_type = OSTYPE_LINUX;
  106. return __mei_cl_send(cldev->cl, buf, size, 0, mode);
  107. }
  108. #define MKHI_FWVER_BUF_LEN (sizeof(struct mkhi_msg_hdr) + \
  109. sizeof(struct mkhi_fw_ver))
  110. #define MKHI_FWVER_LEN(__num) (sizeof(struct mkhi_msg_hdr) + \
  111. sizeof(struct mkhi_fw_ver_block) * (__num))
  112. static int mei_fwver(struct mei_cl_device *cldev)
  113. {
  114. char buf[MKHI_FWVER_BUF_LEN];
  115. struct mkhi_msg req;
  116. struct mkhi_msg *rsp;
  117. struct mkhi_fw_ver *fwver;
  118. int bytes_recv, ret, i;
  119. memset(buf, 0, sizeof(buf));
  120. req.hdr.group_id = MKHI_GEN_GROUP_ID;
  121. req.hdr.command = MKHI_GEN_GET_FW_VERSION_CMD;
  122. ret = __mei_cl_send(cldev->cl, (u8 *)&req, sizeof(req), 0,
  123. MEI_CL_IO_TX_BLOCKING);
  124. if (ret < 0) {
  125. dev_err(&cldev->dev, "Could not send ReqFWVersion cmd ret = %d\n", ret);
  126. return ret;
  127. }
  128. ret = 0;
  129. bytes_recv = __mei_cl_recv(cldev->cl, buf, sizeof(buf), NULL, 0,
  130. cldev->bus->timeouts.mkhi_recv);
  131. if (bytes_recv < 0 || (size_t)bytes_recv < MKHI_FWVER_LEN(1)) {
  132. /*
  133. * Should be at least one version block,
  134. * error out if nothing found
  135. */
  136. dev_err(&cldev->dev, "Could not read FW version ret = %d\n", bytes_recv);
  137. return -EIO;
  138. }
  139. rsp = (struct mkhi_msg *)buf;
  140. fwver = (struct mkhi_fw_ver *)rsp->data;
  141. memset(cldev->bus->fw_ver, 0, sizeof(cldev->bus->fw_ver));
  142. for (i = 0; i < MEI_MAX_FW_VER_BLOCKS; i++) {
  143. if ((size_t)bytes_recv < MKHI_FWVER_LEN(i + 1))
  144. break;
  145. dev_dbg(&cldev->dev, "FW version%d %d:%d.%d.%d.%d\n",
  146. i, fwver->ver[i].platform,
  147. fwver->ver[i].major, fwver->ver[i].minor,
  148. fwver->ver[i].hotfix, fwver->ver[i].buildno);
  149. cldev->bus->fw_ver[i].platform = fwver->ver[i].platform;
  150. cldev->bus->fw_ver[i].major = fwver->ver[i].major;
  151. cldev->bus->fw_ver[i].minor = fwver->ver[i].minor;
  152. cldev->bus->fw_ver[i].hotfix = fwver->ver[i].hotfix;
  153. cldev->bus->fw_ver[i].buildno = fwver->ver[i].buildno;
  154. }
  155. return ret;
  156. }
  157. static int mei_gfx_memory_ready(struct mei_cl_device *cldev)
  158. {
  159. struct mkhi_gfx_mem_ready req = {0};
  160. unsigned int mode = MEI_CL_IO_TX_INTERNAL;
  161. req.hdr.group_id = MKHI_GROUP_ID_GFX;
  162. req.hdr.command = MKHI_GFX_MEMORY_READY_CMD_REQ;
  163. req.flags = MKHI_GFX_MEM_READY_PXP_ALLOWED;
  164. dev_dbg(&cldev->dev, "Sending memory ready command\n");
  165. return __mei_cl_send(cldev->cl, (u8 *)&req, sizeof(req), 0, mode);
  166. }
  167. static void mei_mkhi_fix(struct mei_cl_device *cldev)
  168. {
  169. int ret;
  170. /* No need to enable the client if nothing is needed from it */
  171. if (!cldev->bus->fw_f_fw_ver_supported &&
  172. !cldev->bus->hbm_f_os_supported)
  173. return;
  174. ret = mei_cldev_enable(cldev);
  175. if (ret)
  176. return;
  177. if (cldev->bus->fw_f_fw_ver_supported) {
  178. ret = mei_fwver(cldev);
  179. if (ret < 0)
  180. dev_err(&cldev->dev, "FW version command failed %d\n",
  181. ret);
  182. }
  183. if (cldev->bus->hbm_f_os_supported) {
  184. ret = mei_osver(cldev);
  185. if (ret < 0)
  186. dev_err(&cldev->dev, "OS version command failed %d\n",
  187. ret);
  188. }
  189. mei_cldev_disable(cldev);
  190. }
  191. static void mei_gsc_mkhi_ver(struct mei_cl_device *cldev)
  192. {
  193. int ret;
  194. /* No need to enable the client if nothing is needed from it */
  195. if (!cldev->bus->fw_f_fw_ver_supported)
  196. return;
  197. ret = mei_cldev_enable(cldev);
  198. if (ret)
  199. return;
  200. ret = mei_fwver(cldev);
  201. if (ret < 0)
  202. dev_err(&cldev->dev, "FW version command failed %d\n", ret);
  203. mei_cldev_disable(cldev);
  204. }
  205. static void mei_gsc_mkhi_fix_ver(struct mei_cl_device *cldev)
  206. {
  207. int ret;
  208. /* No need to enable the client if nothing is needed from it */
  209. if (!cldev->bus->fw_f_fw_ver_supported &&
  210. cldev->bus->pxp_mode != MEI_DEV_PXP_INIT)
  211. return;
  212. ret = mei_cldev_enable(cldev);
  213. if (ret)
  214. return;
  215. if (cldev->bus->pxp_mode == MEI_DEV_PXP_INIT) {
  216. ret = mei_gfx_memory_ready(cldev);
  217. if (ret < 0)
  218. dev_err(&cldev->dev, "memory ready command failed %d\n", ret);
  219. else
  220. dev_dbg(&cldev->dev, "memory ready command sent\n");
  221. /* we go to reset after that */
  222. cldev->bus->pxp_mode = MEI_DEV_PXP_SETUP;
  223. goto out;
  224. }
  225. ret = mei_fwver(cldev);
  226. if (ret < 0)
  227. dev_err(&cldev->dev, "FW version command failed %d\n",
  228. ret);
  229. out:
  230. mei_cldev_disable(cldev);
  231. }
  232. /**
  233. * mei_wd - wd client on the bus, change protocol version
  234. * as the API has changed.
  235. *
  236. * @cldev: me clients device
  237. */
  238. #if IS_ENABLED(CONFIG_INTEL_MEI_ME)
  239. #include <linux/pci.h>
  240. #include "hw-me-regs.h"
  241. static void mei_wd(struct mei_cl_device *cldev)
  242. {
  243. struct pci_dev *pdev = to_pci_dev(cldev->dev.parent);
  244. if (pdev->device == MEI_DEV_ID_WPT_LP ||
  245. pdev->device == MEI_DEV_ID_SPT ||
  246. pdev->device == MEI_DEV_ID_SPT_H)
  247. cldev->me_cl->props.protocol_version = 0x2;
  248. cldev->do_match = 1;
  249. }
  250. #else
  251. static inline void mei_wd(struct mei_cl_device *cldev) {}
  252. #endif /* CONFIG_INTEL_MEI_ME */
  253. struct mei_nfc_cmd {
  254. u8 command;
  255. u8 status;
  256. u16 req_id;
  257. u32 reserved;
  258. u16 data_size;
  259. u8 sub_command;
  260. u8 data[];
  261. } __packed;
  262. struct mei_nfc_reply {
  263. u8 command;
  264. u8 status;
  265. u16 req_id;
  266. u32 reserved;
  267. u16 data_size;
  268. u8 sub_command;
  269. u8 reply_status;
  270. u8 data[];
  271. } __packed;
  272. struct mei_nfc_if_version {
  273. u8 radio_version_sw[3];
  274. u8 reserved[3];
  275. u8 radio_version_hw[3];
  276. u8 i2c_addr;
  277. u8 fw_ivn;
  278. u8 vendor_id;
  279. u8 radio_type;
  280. } __packed;
  281. #define MEI_NFC_CMD_MAINTENANCE 0x00
  282. #define MEI_NFC_SUBCMD_IF_VERSION 0x01
  283. /* Vendors */
  284. #define MEI_NFC_VENDOR_INSIDE 0x00
  285. #define MEI_NFC_VENDOR_NXP 0x01
  286. /* Radio types */
  287. #define MEI_NFC_VENDOR_INSIDE_UREAD 0x00
  288. #define MEI_NFC_VENDOR_NXP_PN544 0x01
  289. /**
  290. * mei_nfc_if_version - get NFC interface version
  291. *
  292. * @cl: host client (nfc info)
  293. * @ver: NFC interface version to be filled in
  294. *
  295. * Return: 0 on success; < 0 otherwise
  296. */
  297. static int mei_nfc_if_version(struct mei_cl *cl,
  298. struct mei_nfc_if_version *ver)
  299. {
  300. struct mei_device *bus;
  301. struct mei_nfc_cmd cmd = {
  302. .command = MEI_NFC_CMD_MAINTENANCE,
  303. .data_size = 1,
  304. .sub_command = MEI_NFC_SUBCMD_IF_VERSION,
  305. };
  306. struct mei_nfc_reply *reply = NULL;
  307. size_t if_version_length;
  308. u8 vtag;
  309. int bytes_recv, ret;
  310. bus = cl->dev;
  311. WARN_ON(mutex_is_locked(&bus->device_lock));
  312. ret = __mei_cl_send(cl, (u8 *)&cmd, sizeof(cmd), 0,
  313. MEI_CL_IO_TX_BLOCKING);
  314. if (ret < 0) {
  315. dev_err(bus->dev, "Could not send IF version cmd ret = %d\n", ret);
  316. return ret;
  317. }
  318. /* to be sure on the stack we alloc memory */
  319. if_version_length = sizeof(*reply) + sizeof(*ver);
  320. reply = kzalloc(if_version_length, GFP_KERNEL);
  321. if (!reply)
  322. return -ENOMEM;
  323. ret = 0;
  324. bytes_recv = __mei_cl_recv(cl, (u8 *)reply, if_version_length, &vtag,
  325. 0, 0);
  326. if (bytes_recv < 0 || (size_t)bytes_recv < if_version_length) {
  327. dev_err(bus->dev, "Could not read IF version ret = %d\n", bytes_recv);
  328. ret = -EIO;
  329. goto err;
  330. }
  331. memcpy(ver, reply->data, sizeof(*ver));
  332. dev_info(bus->dev, "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n",
  333. ver->fw_ivn, ver->vendor_id, ver->radio_type);
  334. err:
  335. kfree(reply);
  336. return ret;
  337. }
  338. /**
  339. * mei_nfc_radio_name - derive nfc radio name from the interface version
  340. *
  341. * @ver: NFC radio version
  342. *
  343. * Return: radio name string
  344. */
  345. static const char *mei_nfc_radio_name(struct mei_nfc_if_version *ver)
  346. {
  347. if (ver->vendor_id == MEI_NFC_VENDOR_INSIDE) {
  348. if (ver->radio_type == MEI_NFC_VENDOR_INSIDE_UREAD)
  349. return "microread";
  350. }
  351. if (ver->vendor_id == MEI_NFC_VENDOR_NXP) {
  352. if (ver->radio_type == MEI_NFC_VENDOR_NXP_PN544)
  353. return "pn544";
  354. }
  355. return NULL;
  356. }
  357. /**
  358. * mei_nfc - The nfc fixup function. The function retrieves nfc radio
  359. * name and set is as device attribute so we can load
  360. * the proper device driver for it
  361. *
  362. * @cldev: me client device (nfc)
  363. */
  364. static void mei_nfc(struct mei_cl_device *cldev)
  365. {
  366. struct mei_device *bus;
  367. struct mei_cl *cl;
  368. struct mei_me_client *me_cl = NULL;
  369. struct mei_nfc_if_version ver;
  370. const char *radio_name = NULL;
  371. int ret;
  372. bus = cldev->bus;
  373. mutex_lock(&bus->device_lock);
  374. /* we need to connect to INFO GUID */
  375. cl = mei_cl_alloc_linked(bus);
  376. if (IS_ERR(cl)) {
  377. ret = PTR_ERR(cl);
  378. cl = NULL;
  379. dev_err(bus->dev, "nfc hook alloc failed %d\n", ret);
  380. goto out;
  381. }
  382. me_cl = mei_me_cl_by_uuid(bus, &mei_nfc_info_guid);
  383. if (!me_cl) {
  384. ret = -ENOTTY;
  385. dev_err(bus->dev, "Cannot find nfc info %d\n", ret);
  386. goto out;
  387. }
  388. ret = mei_cl_connect(cl, me_cl, NULL);
  389. if (ret < 0) {
  390. dev_err(&cldev->dev, "Can't connect to the NFC INFO ME ret = %d\n",
  391. ret);
  392. goto out;
  393. }
  394. mutex_unlock(&bus->device_lock);
  395. ret = mei_nfc_if_version(cl, &ver);
  396. if (ret)
  397. goto disconnect;
  398. radio_name = mei_nfc_radio_name(&ver);
  399. if (!radio_name) {
  400. ret = -ENOENT;
  401. dev_err(&cldev->dev, "Can't get the NFC interface version ret = %d\n",
  402. ret);
  403. goto disconnect;
  404. }
  405. dev_dbg(bus->dev, "nfc radio %s\n", radio_name);
  406. strscpy(cldev->name, radio_name, sizeof(cldev->name));
  407. disconnect:
  408. mutex_lock(&bus->device_lock);
  409. if (mei_cl_disconnect(cl) < 0)
  410. dev_err(bus->dev, "Can't disconnect the NFC INFO ME\n");
  411. mei_cl_flush_queues(cl, NULL);
  412. out:
  413. mei_cl_unlink(cl);
  414. mutex_unlock(&bus->device_lock);
  415. mei_me_cl_put(me_cl);
  416. kfree(cl);
  417. if (ret)
  418. cldev->do_match = 0;
  419. dev_dbg(bus->dev, "end of fixup match = %d\n", cldev->do_match);
  420. }
  421. /**
  422. * vt_support - enable on bus clients with vtag support
  423. *
  424. * @cldev: me clients device
  425. */
  426. static void vt_support(struct mei_cl_device *cldev)
  427. {
  428. if (cldev->me_cl->props.vt_supported == 1)
  429. cldev->do_match = 1;
  430. }
  431. /**
  432. * pxp_is_ready - enable bus client if pxp is ready
  433. *
  434. * @cldev: me clients device
  435. */
  436. static void pxp_is_ready(struct mei_cl_device *cldev)
  437. {
  438. struct mei_device *bus = cldev->bus;
  439. switch (bus->pxp_mode) {
  440. case MEI_DEV_PXP_READY:
  441. case MEI_DEV_PXP_DEFAULT:
  442. cldev->do_match = 1;
  443. break;
  444. default:
  445. cldev->do_match = 0;
  446. break;
  447. }
  448. }
  449. #define MEI_FIXUP(_uuid, _hook) { _uuid, _hook }
  450. static struct mei_fixup {
  451. const uuid_le uuid;
  452. void (*hook)(struct mei_cl_device *cldev);
  453. } mei_fixups[] = {
  454. MEI_FIXUP(MEI_UUID_ANY, number_of_connections),
  455. MEI_FIXUP(MEI_UUID_NFC_INFO, blacklist),
  456. MEI_FIXUP(MEI_UUID_NFC_HCI, mei_nfc),
  457. MEI_FIXUP(MEI_UUID_WD, mei_wd),
  458. MEI_FIXUP(MEI_UUID_MKHIF_FIX, mei_mkhi_fix),
  459. MEI_FIXUP(MEI_UUID_IGSC_MKHI, mei_gsc_mkhi_ver),
  460. MEI_FIXUP(MEI_UUID_IGSC_MKHI_FIX, mei_gsc_mkhi_fix_ver),
  461. MEI_FIXUP(MEI_UUID_HDCP, whitelist),
  462. MEI_FIXUP(MEI_UUID_ANY, vt_support),
  463. MEI_FIXUP(MEI_UUID_PAVP, pxp_is_ready),
  464. };
  465. /**
  466. * mei_cl_bus_dev_fixup - run fixup handlers
  467. *
  468. * @cldev: me client device
  469. */
  470. void mei_cl_bus_dev_fixup(struct mei_cl_device *cldev)
  471. {
  472. struct mei_fixup *f;
  473. const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
  474. size_t i;
  475. for (i = 0; i < ARRAY_SIZE(mei_fixups); i++) {
  476. f = &mei_fixups[i];
  477. if (uuid_le_cmp(f->uuid, MEI_UUID_ANY) == 0 ||
  478. uuid_le_cmp(f->uuid, *uuid) == 0)
  479. f->hook(cldev);
  480. }
  481. }