bus-fixup.c 12 KB

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