bus-fixup.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. *
  3. * Intel Management Engine Interface (Intel MEI) Linux driver
  4. * Copyright (c) 2003-2013, 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. #define MKHI_FWCAPS_GROUP_ID 0x3
  82. #define MKHI_FWCAPS_SET_OS_VER_APP_RULE_CMD 6
  83. struct mkhi_msg_hdr {
  84. u8 group_id;
  85. u8 command;
  86. u8 reserved;
  87. u8 result;
  88. } __packed;
  89. struct mkhi_msg {
  90. struct mkhi_msg_hdr hdr;
  91. u8 data[0];
  92. } __packed;
  93. static int mei_osver(struct mei_cl_device *cldev)
  94. {
  95. int ret;
  96. const size_t size = sizeof(struct mkhi_msg_hdr) +
  97. sizeof(struct mkhi_fwcaps) +
  98. sizeof(struct mei_os_ver);
  99. size_t length = 8;
  100. char buf[size];
  101. struct mkhi_msg *req;
  102. struct mkhi_fwcaps *fwcaps;
  103. struct mei_os_ver *os_ver;
  104. unsigned int mode = MEI_CL_IO_TX_BLOCKING | MEI_CL_IO_TX_INTERNAL;
  105. memset(buf, 0, size);
  106. req = (struct mkhi_msg *)buf;
  107. req->hdr.group_id = MKHI_FWCAPS_GROUP_ID;
  108. req->hdr.command = MKHI_FWCAPS_SET_OS_VER_APP_RULE_CMD;
  109. fwcaps = (struct mkhi_fwcaps *)req->data;
  110. fwcaps->id.rule_type = 0x0;
  111. fwcaps->id.feature_id = MKHI_FEATURE_PTT;
  112. fwcaps->len = sizeof(*os_ver);
  113. os_ver = (struct mei_os_ver *)fwcaps->data;
  114. os_ver->os_type = OSTYPE_LINUX;
  115. ret = __mei_cl_send(cldev->cl, buf, size, mode);
  116. if (ret < 0)
  117. return ret;
  118. ret = __mei_cl_recv(cldev->cl, buf, length, 0);
  119. if (ret < 0)
  120. return ret;
  121. return 0;
  122. }
  123. static void mei_mkhi_fix(struct mei_cl_device *cldev)
  124. {
  125. int ret;
  126. if (!cldev->bus->hbm_f_os_supported)
  127. return;
  128. ret = mei_cldev_enable(cldev);
  129. if (ret)
  130. return;
  131. ret = mei_osver(cldev);
  132. if (ret)
  133. dev_err(&cldev->dev, "OS version command failed %d\n", ret);
  134. mei_cldev_disable(cldev);
  135. }
  136. /**
  137. * mei_wd - wd client on the bus, change protocol version
  138. * as the API has changed.
  139. *
  140. * @cldev: me clients device
  141. */
  142. #if IS_ENABLED(CONFIG_INTEL_MEI_ME)
  143. #include <linux/pci.h>
  144. #include "hw-me-regs.h"
  145. static void mei_wd(struct mei_cl_device *cldev)
  146. {
  147. struct pci_dev *pdev = to_pci_dev(cldev->dev.parent);
  148. dev_dbg(&cldev->dev, "running hook %s\n", __func__);
  149. if (pdev->device == MEI_DEV_ID_WPT_LP ||
  150. pdev->device == MEI_DEV_ID_SPT ||
  151. pdev->device == MEI_DEV_ID_SPT_H)
  152. cldev->me_cl->props.protocol_version = 0x2;
  153. cldev->do_match = 1;
  154. }
  155. #else
  156. static inline void mei_wd(struct mei_cl_device *cldev) {}
  157. #endif /* CONFIG_INTEL_MEI_ME */
  158. struct mei_nfc_cmd {
  159. u8 command;
  160. u8 status;
  161. u16 req_id;
  162. u32 reserved;
  163. u16 data_size;
  164. u8 sub_command;
  165. u8 data[];
  166. } __packed;
  167. struct mei_nfc_reply {
  168. u8 command;
  169. u8 status;
  170. u16 req_id;
  171. u32 reserved;
  172. u16 data_size;
  173. u8 sub_command;
  174. u8 reply_status;
  175. u8 data[];
  176. } __packed;
  177. struct mei_nfc_if_version {
  178. u8 radio_version_sw[3];
  179. u8 reserved[3];
  180. u8 radio_version_hw[3];
  181. u8 i2c_addr;
  182. u8 fw_ivn;
  183. u8 vendor_id;
  184. u8 radio_type;
  185. } __packed;
  186. #define MEI_NFC_CMD_MAINTENANCE 0x00
  187. #define MEI_NFC_SUBCMD_IF_VERSION 0x01
  188. /* Vendors */
  189. #define MEI_NFC_VENDOR_INSIDE 0x00
  190. #define MEI_NFC_VENDOR_NXP 0x01
  191. /* Radio types */
  192. #define MEI_NFC_VENDOR_INSIDE_UREAD 0x00
  193. #define MEI_NFC_VENDOR_NXP_PN544 0x01
  194. /**
  195. * mei_nfc_if_version - get NFC interface version
  196. *
  197. * @cl: host client (nfc info)
  198. * @ver: NFC interface version to be filled in
  199. *
  200. * Return: 0 on success; < 0 otherwise
  201. */
  202. static int mei_nfc_if_version(struct mei_cl *cl,
  203. struct mei_nfc_if_version *ver)
  204. {
  205. struct mei_device *bus;
  206. struct mei_nfc_cmd cmd = {
  207. .command = MEI_NFC_CMD_MAINTENANCE,
  208. .data_size = 1,
  209. .sub_command = MEI_NFC_SUBCMD_IF_VERSION,
  210. };
  211. struct mei_nfc_reply *reply = NULL;
  212. size_t if_version_length;
  213. int bytes_recv, ret;
  214. bus = cl->dev;
  215. WARN_ON(mutex_is_locked(&bus->device_lock));
  216. ret = __mei_cl_send(cl, (u8 *)&cmd, sizeof(struct mei_nfc_cmd),
  217. MEI_CL_IO_TX_BLOCKING);
  218. if (ret < 0) {
  219. dev_err(bus->dev, "Could not send IF version cmd\n");
  220. return ret;
  221. }
  222. /* to be sure on the stack we alloc memory */
  223. if_version_length = sizeof(struct mei_nfc_reply) +
  224. sizeof(struct mei_nfc_if_version);
  225. reply = kzalloc(if_version_length, GFP_KERNEL);
  226. if (!reply)
  227. return -ENOMEM;
  228. ret = 0;
  229. bytes_recv = __mei_cl_recv(cl, (u8 *)reply, if_version_length, 0);
  230. if (bytes_recv < if_version_length) {
  231. dev_err(bus->dev, "Could not read IF version\n");
  232. ret = -EIO;
  233. goto err;
  234. }
  235. memcpy(ver, reply->data, sizeof(struct mei_nfc_if_version));
  236. dev_info(bus->dev, "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n",
  237. ver->fw_ivn, ver->vendor_id, ver->radio_type);
  238. err:
  239. kfree(reply);
  240. return ret;
  241. }
  242. /**
  243. * mei_nfc_radio_name - derive nfc radio name from the interface version
  244. *
  245. * @ver: NFC radio version
  246. *
  247. * Return: radio name string
  248. */
  249. static const char *mei_nfc_radio_name(struct mei_nfc_if_version *ver)
  250. {
  251. if (ver->vendor_id == MEI_NFC_VENDOR_INSIDE) {
  252. if (ver->radio_type == MEI_NFC_VENDOR_INSIDE_UREAD)
  253. return "microread";
  254. }
  255. if (ver->vendor_id == MEI_NFC_VENDOR_NXP) {
  256. if (ver->radio_type == MEI_NFC_VENDOR_NXP_PN544)
  257. return "pn544";
  258. }
  259. return NULL;
  260. }
  261. /**
  262. * mei_nfc - The nfc fixup function. The function retrieves nfc radio
  263. * name and set is as device attribute so we can load
  264. * the proper device driver for it
  265. *
  266. * @cldev: me client device (nfc)
  267. */
  268. static void mei_nfc(struct mei_cl_device *cldev)
  269. {
  270. struct mei_device *bus;
  271. struct mei_cl *cl;
  272. struct mei_me_client *me_cl = NULL;
  273. struct mei_nfc_if_version ver;
  274. const char *radio_name = NULL;
  275. int ret;
  276. bus = cldev->bus;
  277. dev_dbg(&cldev->dev, "running hook %s\n", __func__);
  278. mutex_lock(&bus->device_lock);
  279. /* we need to connect to INFO GUID */
  280. cl = mei_cl_alloc_linked(bus);
  281. if (IS_ERR(cl)) {
  282. ret = PTR_ERR(cl);
  283. cl = NULL;
  284. dev_err(bus->dev, "nfc hook alloc failed %d\n", ret);
  285. goto out;
  286. }
  287. me_cl = mei_me_cl_by_uuid(bus, &mei_nfc_info_guid);
  288. if (!me_cl) {
  289. ret = -ENOTTY;
  290. dev_err(bus->dev, "Cannot find nfc info %d\n", ret);
  291. goto out;
  292. }
  293. ret = mei_cl_connect(cl, me_cl, NULL);
  294. if (ret < 0) {
  295. dev_err(&cldev->dev, "Can't connect to the NFC INFO ME ret = %d\n",
  296. ret);
  297. goto out;
  298. }
  299. mutex_unlock(&bus->device_lock);
  300. ret = mei_nfc_if_version(cl, &ver);
  301. if (ret)
  302. goto disconnect;
  303. radio_name = mei_nfc_radio_name(&ver);
  304. if (!radio_name) {
  305. ret = -ENOENT;
  306. dev_err(&cldev->dev, "Can't get the NFC interface version ret = %d\n",
  307. ret);
  308. goto disconnect;
  309. }
  310. dev_dbg(bus->dev, "nfc radio %s\n", radio_name);
  311. strlcpy(cldev->name, radio_name, sizeof(cldev->name));
  312. disconnect:
  313. mutex_lock(&bus->device_lock);
  314. if (mei_cl_disconnect(cl) < 0)
  315. dev_err(bus->dev, "Can't disconnect the NFC INFO ME\n");
  316. mei_cl_flush_queues(cl, NULL);
  317. out:
  318. mei_cl_unlink(cl);
  319. mutex_unlock(&bus->device_lock);
  320. mei_me_cl_put(me_cl);
  321. kfree(cl);
  322. if (ret)
  323. cldev->do_match = 0;
  324. dev_dbg(bus->dev, "end of fixup match = %d\n", cldev->do_match);
  325. }
  326. #define MEI_FIXUP(_uuid, _hook) { _uuid, _hook }
  327. static struct mei_fixup {
  328. const uuid_le uuid;
  329. void (*hook)(struct mei_cl_device *cldev);
  330. } mei_fixups[] = {
  331. MEI_FIXUP(MEI_UUID_ANY, number_of_connections),
  332. MEI_FIXUP(MEI_UUID_NFC_INFO, blacklist),
  333. MEI_FIXUP(MEI_UUID_NFC_HCI, mei_nfc),
  334. MEI_FIXUP(MEI_UUID_WD, mei_wd),
  335. MEI_FIXUP(MEI_UUID_MKHIF_FIX, mei_mkhi_fix),
  336. };
  337. /**
  338. * mei_cldev_fixup - run fixup handlers
  339. *
  340. * @cldev: me client device
  341. */
  342. void mei_cl_bus_dev_fixup(struct mei_cl_device *cldev)
  343. {
  344. struct mei_fixup *f;
  345. const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
  346. int i;
  347. for (i = 0; i < ARRAY_SIZE(mei_fixups); i++) {
  348. f = &mei_fixups[i];
  349. if (uuid_le_cmp(f->uuid, MEI_UUID_ANY) == 0 ||
  350. uuid_le_cmp(f->uuid, *uuid) == 0)
  351. f->hook(cldev);
  352. }
  353. }