bus-fixup.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. *
  3. * Intel Management Engine Interface (Intel MEI) Linux driver
  4. * Copyright (c) 2003-2018, Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/sched.h>
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/device.h>
  21. #include <linux/slab.h>
  22. #include <linux/uuid.h>
  23. #include <linux/mei_cl_bus.h>
  24. #include "mei_dev.h"
  25. #include "client.h"
  26. #define MEI_UUID_NFC_INFO UUID_LE(0xd2de1625, 0x382d, 0x417d, \
  27. 0x48, 0xa4, 0xef, 0xab, 0xba, 0x8a, 0x12, 0x06)
  28. static const uuid_le mei_nfc_info_guid = MEI_UUID_NFC_INFO;
  29. #define MEI_UUID_NFC_HCI UUID_LE(0x0bb17a78, 0x2a8e, 0x4c50, \
  30. 0x94, 0xd4, 0x50, 0x26, 0x67, 0x23, 0x77, 0x5c)
  31. #define MEI_UUID_WD UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, \
  32. 0x89, 0x9D, 0xA9, 0x15, 0x14, 0xCB, 0x32, 0xAB)
  33. #define MEI_UUID_MKHIF_FIX UUID_LE(0x55213584, 0x9a29, 0x4916, \
  34. 0xba, 0xdf, 0xf, 0xb7, 0xed, 0x68, 0x2a, 0xeb)
  35. #define MEI_UUID_ANY NULL_UUID_LE
  36. /**
  37. * number_of_connections - determine whether an client be on the bus
  38. * according number of connections
  39. * We support only clients:
  40. * 1. with single connection
  41. * 2. and fixed clients (max_number_of_connections == 0)
  42. *
  43. * @cldev: me clients device
  44. */
  45. static void number_of_connections(struct mei_cl_device *cldev)
  46. {
  47. dev_dbg(&cldev->dev, "running hook %s\n", __func__);
  48. if (cldev->me_cl->props.max_number_of_connections > 1)
  49. cldev->do_match = 0;
  50. }
  51. /**
  52. * blacklist - blacklist a client from the bus
  53. *
  54. * @cldev: me clients device
  55. */
  56. static void blacklist(struct mei_cl_device *cldev)
  57. {
  58. dev_dbg(&cldev->dev, "running hook %s\n", __func__);
  59. cldev->do_match = 0;
  60. }
  61. #define OSTYPE_LINUX 2
  62. struct mei_os_ver {
  63. __le16 build;
  64. __le16 reserved1;
  65. u8 os_type;
  66. u8 major;
  67. u8 minor;
  68. u8 reserved2;
  69. } __packed;
  70. #define MKHI_FEATURE_PTT 0x10
  71. struct mkhi_rule_id {
  72. __le16 rule_type;
  73. u8 feature_id;
  74. u8 reserved;
  75. } __packed;
  76. struct mkhi_fwcaps {
  77. struct mkhi_rule_id id;
  78. u8 len;
  79. u8 data[0];
  80. } __packed;
  81. struct mkhi_fw_ver_block {
  82. u16 minor;
  83. u8 major;
  84. u8 platform;
  85. u16 buildno;
  86. u16 hotfix;
  87. } __packed;
  88. struct mkhi_fw_ver {
  89. struct mkhi_fw_ver_block ver[MEI_MAX_FW_VER_BLOCKS];
  90. } __packed;
  91. #define MKHI_FWCAPS_GROUP_ID 0x3
  92. #define MKHI_FWCAPS_SET_OS_VER_APP_RULE_CMD 6
  93. #define MKHI_GEN_GROUP_ID 0xFF
  94. #define MKHI_GEN_GET_FW_VERSION_CMD 0x2
  95. struct mkhi_msg_hdr {
  96. u8 group_id;
  97. u8 command;
  98. u8 reserved;
  99. u8 result;
  100. } __packed;
  101. struct mkhi_msg {
  102. struct mkhi_msg_hdr hdr;
  103. u8 data[0];
  104. } __packed;
  105. #define MKHI_OSVER_BUF_LEN (sizeof(struct mkhi_msg_hdr) + \
  106. sizeof(struct mkhi_fwcaps) + \
  107. sizeof(struct mei_os_ver))
  108. static int mei_osver(struct mei_cl_device *cldev)
  109. {
  110. const size_t size = MKHI_OSVER_BUF_LEN;
  111. char buf[MKHI_OSVER_BUF_LEN];
  112. struct mkhi_msg *req;
  113. struct mkhi_fwcaps *fwcaps;
  114. struct mei_os_ver *os_ver;
  115. unsigned int mode = MEI_CL_IO_TX_BLOCKING | MEI_CL_IO_TX_INTERNAL;
  116. memset(buf, 0, size);
  117. req = (struct mkhi_msg *)buf;
  118. req->hdr.group_id = MKHI_FWCAPS_GROUP_ID;
  119. req->hdr.command = MKHI_FWCAPS_SET_OS_VER_APP_RULE_CMD;
  120. fwcaps = (struct mkhi_fwcaps *)req->data;
  121. fwcaps->id.rule_type = 0x0;
  122. fwcaps->id.feature_id = MKHI_FEATURE_PTT;
  123. fwcaps->len = sizeof(*os_ver);
  124. os_ver = (struct mei_os_ver *)fwcaps->data;
  125. os_ver->os_type = OSTYPE_LINUX;
  126. return __mei_cl_send(cldev->cl, buf, size, mode);
  127. }
  128. #define MKHI_FWVER_BUF_LEN (sizeof(struct mkhi_msg_hdr) + \
  129. sizeof(struct mkhi_fw_ver))
  130. #define MKHI_FWVER_LEN(__num) (sizeof(struct mkhi_msg_hdr) + \
  131. sizeof(struct mkhi_fw_ver_block) * (__num))
  132. #define MKHI_RCV_TIMEOUT 500 /* receive timeout in msec */
  133. static int mei_fwver(struct mei_cl_device *cldev)
  134. {
  135. char buf[MKHI_FWVER_BUF_LEN];
  136. struct mkhi_msg *req;
  137. struct mkhi_fw_ver *fwver;
  138. int bytes_recv, ret, i;
  139. memset(buf, 0, sizeof(buf));
  140. req = (struct mkhi_msg *)buf;
  141. req->hdr.group_id = MKHI_GEN_GROUP_ID;
  142. req->hdr.command = MKHI_GEN_GET_FW_VERSION_CMD;
  143. ret = __mei_cl_send(cldev->cl, buf, sizeof(struct mkhi_msg_hdr),
  144. MEI_CL_IO_TX_BLOCKING);
  145. if (ret < 0) {
  146. dev_err(&cldev->dev, "Could not send ReqFWVersion cmd\n");
  147. return ret;
  148. }
  149. ret = 0;
  150. bytes_recv = __mei_cl_recv(cldev->cl, buf, sizeof(buf), 0,
  151. MKHI_RCV_TIMEOUT);
  152. if (bytes_recv < 0 || (size_t)bytes_recv < MKHI_FWVER_LEN(1)) {
  153. /*
  154. * Should be at least one version block,
  155. * error out if nothing found
  156. */
  157. dev_err(&cldev->dev, "Could not read FW version\n");
  158. return -EIO;
  159. }
  160. fwver = (struct mkhi_fw_ver *)req->data;
  161. memset(cldev->bus->fw_ver, 0, sizeof(cldev->bus->fw_ver));
  162. for (i = 0; i < MEI_MAX_FW_VER_BLOCKS; i++) {
  163. if ((size_t)bytes_recv < MKHI_FWVER_LEN(i + 1))
  164. break;
  165. dev_dbg(&cldev->dev, "FW version%d %d:%d.%d.%d.%d\n",
  166. i, fwver->ver[i].platform,
  167. fwver->ver[i].major, fwver->ver[i].minor,
  168. fwver->ver[i].hotfix, fwver->ver[i].buildno);
  169. cldev->bus->fw_ver[i].platform = fwver->ver[i].platform;
  170. cldev->bus->fw_ver[i].major = fwver->ver[i].major;
  171. cldev->bus->fw_ver[i].minor = fwver->ver[i].minor;
  172. cldev->bus->fw_ver[i].hotfix = fwver->ver[i].hotfix;
  173. cldev->bus->fw_ver[i].buildno = fwver->ver[i].buildno;
  174. }
  175. return ret;
  176. }
  177. static void mei_mkhi_fix(struct mei_cl_device *cldev)
  178. {
  179. int ret;
  180. /* No need to enable the client if nothing is needed from it */
  181. if (!cldev->bus->fw_f_fw_ver_supported &&
  182. !cldev->bus->hbm_f_os_supported)
  183. return;
  184. ret = mei_cldev_enable(cldev);
  185. if (ret)
  186. return;
  187. if (cldev->bus->fw_f_fw_ver_supported) {
  188. ret = mei_fwver(cldev);
  189. if (ret < 0)
  190. dev_err(&cldev->dev, "FW version command failed %d\n",
  191. ret);
  192. }
  193. if (cldev->bus->hbm_f_os_supported) {
  194. ret = mei_osver(cldev);
  195. if (ret < 0)
  196. dev_err(&cldev->dev, "OS version command failed %d\n",
  197. ret);
  198. }
  199. mei_cldev_disable(cldev);
  200. }
  201. /**
  202. * mei_wd - wd client on the bus, change protocol version
  203. * as the API has changed.
  204. *
  205. * @cldev: me clients device
  206. */
  207. #if IS_ENABLED(CONFIG_INTEL_MEI_ME)
  208. #include <linux/pci.h>
  209. #include "hw-me-regs.h"
  210. static void mei_wd(struct mei_cl_device *cldev)
  211. {
  212. struct pci_dev *pdev = to_pci_dev(cldev->dev.parent);
  213. dev_dbg(&cldev->dev, "running hook %s\n", __func__);
  214. if (pdev->device == MEI_DEV_ID_WPT_LP ||
  215. pdev->device == MEI_DEV_ID_SPT ||
  216. pdev->device == MEI_DEV_ID_SPT_H)
  217. cldev->me_cl->props.protocol_version = 0x2;
  218. cldev->do_match = 1;
  219. }
  220. #else
  221. static inline void mei_wd(struct mei_cl_device *cldev) {}
  222. #endif /* CONFIG_INTEL_MEI_ME */
  223. struct mei_nfc_cmd {
  224. u8 command;
  225. u8 status;
  226. u16 req_id;
  227. u32 reserved;
  228. u16 data_size;
  229. u8 sub_command;
  230. u8 data[];
  231. } __packed;
  232. struct mei_nfc_reply {
  233. u8 command;
  234. u8 status;
  235. u16 req_id;
  236. u32 reserved;
  237. u16 data_size;
  238. u8 sub_command;
  239. u8 reply_status;
  240. u8 data[];
  241. } __packed;
  242. struct mei_nfc_if_version {
  243. u8 radio_version_sw[3];
  244. u8 reserved[3];
  245. u8 radio_version_hw[3];
  246. u8 i2c_addr;
  247. u8 fw_ivn;
  248. u8 vendor_id;
  249. u8 radio_type;
  250. } __packed;
  251. #define MEI_NFC_CMD_MAINTENANCE 0x00
  252. #define MEI_NFC_SUBCMD_IF_VERSION 0x01
  253. /* Vendors */
  254. #define MEI_NFC_VENDOR_INSIDE 0x00
  255. #define MEI_NFC_VENDOR_NXP 0x01
  256. /* Radio types */
  257. #define MEI_NFC_VENDOR_INSIDE_UREAD 0x00
  258. #define MEI_NFC_VENDOR_NXP_PN544 0x01
  259. /**
  260. * mei_nfc_if_version - get NFC interface version
  261. *
  262. * @cl: host client (nfc info)
  263. * @ver: NFC interface version to be filled in
  264. *
  265. * Return: 0 on success; < 0 otherwise
  266. */
  267. static int mei_nfc_if_version(struct mei_cl *cl,
  268. struct mei_nfc_if_version *ver)
  269. {
  270. struct mei_device *bus;
  271. struct mei_nfc_cmd cmd = {
  272. .command = MEI_NFC_CMD_MAINTENANCE,
  273. .data_size = 1,
  274. .sub_command = MEI_NFC_SUBCMD_IF_VERSION,
  275. };
  276. struct mei_nfc_reply *reply = NULL;
  277. size_t if_version_length;
  278. int bytes_recv, ret;
  279. bus = cl->dev;
  280. WARN_ON(mutex_is_locked(&bus->device_lock));
  281. ret = __mei_cl_send(cl, (u8 *)&cmd, sizeof(struct mei_nfc_cmd),
  282. MEI_CL_IO_TX_BLOCKING);
  283. if (ret < 0) {
  284. dev_err(bus->dev, "Could not send IF version cmd\n");
  285. return ret;
  286. }
  287. /* to be sure on the stack we alloc memory */
  288. if_version_length = sizeof(struct mei_nfc_reply) +
  289. sizeof(struct mei_nfc_if_version);
  290. reply = kzalloc(if_version_length, GFP_KERNEL);
  291. if (!reply)
  292. return -ENOMEM;
  293. ret = 0;
  294. bytes_recv = __mei_cl_recv(cl, (u8 *)reply, if_version_length, 0, 0);
  295. if (bytes_recv < 0 || (size_t)bytes_recv < if_version_length) {
  296. dev_err(bus->dev, "Could not read IF version\n");
  297. ret = -EIO;
  298. goto err;
  299. }
  300. memcpy(ver, reply->data, sizeof(struct mei_nfc_if_version));
  301. dev_info(bus->dev, "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n",
  302. ver->fw_ivn, ver->vendor_id, ver->radio_type);
  303. err:
  304. kfree(reply);
  305. return ret;
  306. }
  307. /**
  308. * mei_nfc_radio_name - derive nfc radio name from the interface version
  309. *
  310. * @ver: NFC radio version
  311. *
  312. * Return: radio name string
  313. */
  314. static const char *mei_nfc_radio_name(struct mei_nfc_if_version *ver)
  315. {
  316. if (ver->vendor_id == MEI_NFC_VENDOR_INSIDE) {
  317. if (ver->radio_type == MEI_NFC_VENDOR_INSIDE_UREAD)
  318. return "microread";
  319. }
  320. if (ver->vendor_id == MEI_NFC_VENDOR_NXP) {
  321. if (ver->radio_type == MEI_NFC_VENDOR_NXP_PN544)
  322. return "pn544";
  323. }
  324. return NULL;
  325. }
  326. /**
  327. * mei_nfc - The nfc fixup function. The function retrieves nfc radio
  328. * name and set is as device attribute so we can load
  329. * the proper device driver for it
  330. *
  331. * @cldev: me client device (nfc)
  332. */
  333. static void mei_nfc(struct mei_cl_device *cldev)
  334. {
  335. struct mei_device *bus;
  336. struct mei_cl *cl;
  337. struct mei_me_client *me_cl = NULL;
  338. struct mei_nfc_if_version ver;
  339. const char *radio_name = NULL;
  340. int ret;
  341. bus = cldev->bus;
  342. dev_dbg(&cldev->dev, "running hook %s\n", __func__);
  343. mutex_lock(&bus->device_lock);
  344. /* we need to connect to INFO GUID */
  345. cl = mei_cl_alloc_linked(bus);
  346. if (IS_ERR(cl)) {
  347. ret = PTR_ERR(cl);
  348. cl = NULL;
  349. dev_err(bus->dev, "nfc hook alloc failed %d\n", ret);
  350. goto out;
  351. }
  352. me_cl = mei_me_cl_by_uuid(bus, &mei_nfc_info_guid);
  353. if (!me_cl) {
  354. ret = -ENOTTY;
  355. dev_err(bus->dev, "Cannot find nfc info %d\n", ret);
  356. goto out;
  357. }
  358. ret = mei_cl_connect(cl, me_cl, NULL);
  359. if (ret < 0) {
  360. dev_err(&cldev->dev, "Can't connect to the NFC INFO ME ret = %d\n",
  361. ret);
  362. goto out;
  363. }
  364. mutex_unlock(&bus->device_lock);
  365. ret = mei_nfc_if_version(cl, &ver);
  366. if (ret)
  367. goto disconnect;
  368. radio_name = mei_nfc_radio_name(&ver);
  369. if (!radio_name) {
  370. ret = -ENOENT;
  371. dev_err(&cldev->dev, "Can't get the NFC interface version ret = %d\n",
  372. ret);
  373. goto disconnect;
  374. }
  375. dev_dbg(bus->dev, "nfc radio %s\n", radio_name);
  376. strlcpy(cldev->name, radio_name, sizeof(cldev->name));
  377. disconnect:
  378. mutex_lock(&bus->device_lock);
  379. if (mei_cl_disconnect(cl) < 0)
  380. dev_err(bus->dev, "Can't disconnect the NFC INFO ME\n");
  381. mei_cl_flush_queues(cl, NULL);
  382. out:
  383. mei_cl_unlink(cl);
  384. mutex_unlock(&bus->device_lock);
  385. mei_me_cl_put(me_cl);
  386. kfree(cl);
  387. if (ret)
  388. cldev->do_match = 0;
  389. dev_dbg(bus->dev, "end of fixup match = %d\n", cldev->do_match);
  390. }
  391. #define MEI_FIXUP(_uuid, _hook) { _uuid, _hook }
  392. static struct mei_fixup {
  393. const uuid_le uuid;
  394. void (*hook)(struct mei_cl_device *cldev);
  395. } mei_fixups[] = {
  396. MEI_FIXUP(MEI_UUID_ANY, number_of_connections),
  397. MEI_FIXUP(MEI_UUID_NFC_INFO, blacklist),
  398. MEI_FIXUP(MEI_UUID_NFC_HCI, mei_nfc),
  399. MEI_FIXUP(MEI_UUID_WD, mei_wd),
  400. MEI_FIXUP(MEI_UUID_MKHIF_FIX, mei_mkhi_fix),
  401. };
  402. /**
  403. * mei_cldev_fixup - run fixup handlers
  404. *
  405. * @cldev: me client device
  406. */
  407. void mei_cl_bus_dev_fixup(struct mei_cl_device *cldev)
  408. {
  409. struct mei_fixup *f;
  410. const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
  411. size_t i;
  412. for (i = 0; i < ARRAY_SIZE(mei_fixups); i++) {
  413. f = &mei_fixups[i];
  414. if (uuid_le_cmp(f->uuid, MEI_UUID_ANY) == 0 ||
  415. uuid_le_cmp(f->uuid, *uuid) == 0)
  416. f->hook(cldev);
  417. }
  418. }