1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2013-2022, Intel Corporation. All rights reserved.
4 * Intel Management Engine Interface (Intel MEI) Linux driver
7 #include <linux/kernel.h>
8 #include <linux/sched.h>
9 #include <linux/module.h>
10 #include <linux/device.h>
11 #include <linux/slab.h>
12 #include <linux/uuid.h>
14 #include <linux/mei_cl_bus.h>
20 #define MEI_UUID_NFC_INFO UUID_LE(0xd2de1625, 0x382d, 0x417d, \
21 0x48, 0xa4, 0xef, 0xab, 0xba, 0x8a, 0x12, 0x06)
23 static const uuid_le mei_nfc_info_guid = MEI_UUID_NFC_INFO;
25 #define MEI_UUID_NFC_HCI UUID_LE(0x0bb17a78, 0x2a8e, 0x4c50, \
26 0x94, 0xd4, 0x50, 0x26, 0x67, 0x23, 0x77, 0x5c)
28 #define MEI_UUID_WD UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, \
29 0x89, 0x9D, 0xA9, 0x15, 0x14, 0xCB, 0x32, 0xAB)
31 #define MEI_UUID_MKHIF_FIX UUID_LE(0x55213584, 0x9a29, 0x4916, \
32 0xba, 0xdf, 0xf, 0xb7, 0xed, 0x68, 0x2a, 0xeb)
34 #define MEI_UUID_IGSC_MKHI UUID_LE(0xE2C2AFA2, 0x3817, 0x4D19, \
35 0x9D, 0x95, 0x06, 0xB1, 0x6B, 0x58, 0x8A, 0x5D)
37 #define MEI_UUID_IGSC_MKHI_FIX UUID_LE(0x46E0C1FB, 0xA546, 0x414F, \
38 0x91, 0x70, 0xB7, 0xF4, 0x6D, 0x57, 0xB4, 0xAD)
40 #define MEI_UUID_HDCP UUID_LE(0xB638AB7E, 0x94E2, 0x4EA2, \
41 0xA5, 0x52, 0xD1, 0xC5, 0x4B, 0x62, 0x7F, 0x04)
43 #define MEI_UUID_PAVP UUID_LE(0xfbf6fcf1, 0x96cf, 0x4e2e, 0xA6, \
44 0xa6, 0x1b, 0xab, 0x8c, 0xbe, 0x36, 0xb1)
46 #define MEI_UUID_ANY NULL_UUID_LE
49 * number_of_connections - determine whether an client be on the bus
50 * according number of connections
51 * We support only clients:
52 * 1. with single connection
53 * 2. and fixed clients (max_number_of_connections == 0)
55 * @cldev: me clients device
57 static void number_of_connections(struct mei_cl_device *cldev)
59 if (cldev->me_cl->props.max_number_of_connections > 1)
64 * blacklist - blacklist a client from the bus
66 * @cldev: me clients device
68 static void blacklist(struct mei_cl_device *cldev)
74 * whitelist - forcefully whitelist client
76 * @cldev: me clients device
78 static void whitelist(struct mei_cl_device *cldev)
83 #define OSTYPE_LINUX 2
93 struct mkhi_fw_ver_block {
102 struct mkhi_fw_ver_block ver[MEI_MAX_FW_VER_BLOCKS];
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)
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;
117 memset(buf, 0, size);
119 req = (struct mkhi_msg *)buf;
120 req->hdr.group_id = MKHI_FWCAPS_GROUP_ID;
121 req->hdr.command = MKHI_FWCAPS_SET_OS_VER_APP_RULE_CMD;
123 fwcaps = (struct mkhi_fwcaps *)req->data;
125 fwcaps->id.rule_type = 0x0;
126 fwcaps->id.feature_id = MKHI_FEATURE_PTT;
127 fwcaps->len = sizeof(*os_ver);
128 os_ver = (struct mei_os_ver *)fwcaps->data;
129 os_ver->os_type = OSTYPE_LINUX;
131 return __mei_cl_send(cldev->cl, buf, size, 0, mode);
134 #define MKHI_FWVER_BUF_LEN (sizeof(struct mkhi_msg_hdr) + \
135 sizeof(struct mkhi_fw_ver))
136 #define MKHI_FWVER_LEN(__num) (sizeof(struct mkhi_msg_hdr) + \
137 sizeof(struct mkhi_fw_ver_block) * (__num))
138 static int mei_fwver(struct mei_cl_device *cldev)
140 char buf[MKHI_FWVER_BUF_LEN];
142 struct mkhi_msg *rsp;
143 struct mkhi_fw_ver *fwver;
144 int bytes_recv, ret, i;
146 memset(buf, 0, sizeof(buf));
148 req.hdr.group_id = MKHI_GEN_GROUP_ID;
149 req.hdr.command = MKHI_GEN_GET_FW_VERSION_CMD;
151 ret = __mei_cl_send(cldev->cl, (u8 *)&req, sizeof(req), 0,
152 MEI_CL_IO_TX_BLOCKING);
154 dev_err(&cldev->dev, "Could not send ReqFWVersion cmd\n");
159 bytes_recv = __mei_cl_recv(cldev->cl, buf, sizeof(buf), NULL, 0,
160 cldev->bus->timeouts.mkhi_recv);
161 if (bytes_recv < 0 || (size_t)bytes_recv < MKHI_FWVER_LEN(1)) {
163 * Should be at least one version block,
164 * error out if nothing found
166 dev_err(&cldev->dev, "Could not read FW version\n");
170 rsp = (struct mkhi_msg *)buf;
171 fwver = (struct mkhi_fw_ver *)rsp->data;
172 memset(cldev->bus->fw_ver, 0, sizeof(cldev->bus->fw_ver));
173 for (i = 0; i < MEI_MAX_FW_VER_BLOCKS; i++) {
174 if ((size_t)bytes_recv < MKHI_FWVER_LEN(i + 1))
176 dev_dbg(&cldev->dev, "FW version%d %d:%d.%d.%d.%d\n",
177 i, fwver->ver[i].platform,
178 fwver->ver[i].major, fwver->ver[i].minor,
179 fwver->ver[i].hotfix, fwver->ver[i].buildno);
181 cldev->bus->fw_ver[i].platform = fwver->ver[i].platform;
182 cldev->bus->fw_ver[i].major = fwver->ver[i].major;
183 cldev->bus->fw_ver[i].minor = fwver->ver[i].minor;
184 cldev->bus->fw_ver[i].hotfix = fwver->ver[i].hotfix;
185 cldev->bus->fw_ver[i].buildno = fwver->ver[i].buildno;
191 static int mei_gfx_memory_ready(struct mei_cl_device *cldev)
193 struct mkhi_gfx_mem_ready req = {0};
194 unsigned int mode = MEI_CL_IO_TX_INTERNAL;
196 req.hdr.group_id = MKHI_GROUP_ID_GFX;
197 req.hdr.command = MKHI_GFX_MEMORY_READY_CMD_REQ;
198 req.flags = MKHI_GFX_MEM_READY_PXP_ALLOWED;
200 dev_dbg(&cldev->dev, "Sending memory ready command\n");
201 return __mei_cl_send(cldev->cl, (u8 *)&req, sizeof(req), 0, mode);
204 static void mei_mkhi_fix(struct mei_cl_device *cldev)
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->hbm_f_os_supported)
213 ret = mei_cldev_enable(cldev);
217 if (cldev->bus->fw_f_fw_ver_supported) {
218 ret = mei_fwver(cldev);
220 dev_err(&cldev->dev, "FW version command failed %d\n",
224 if (cldev->bus->hbm_f_os_supported) {
225 ret = mei_osver(cldev);
227 dev_err(&cldev->dev, "OS version command failed %d\n",
230 mei_cldev_disable(cldev);
233 static void mei_gsc_mkhi_ver(struct mei_cl_device *cldev)
237 /* No need to enable the client if nothing is needed from it */
238 if (!cldev->bus->fw_f_fw_ver_supported)
241 ret = mei_cldev_enable(cldev);
245 ret = mei_fwver(cldev);
247 dev_err(&cldev->dev, "FW version command failed %d\n", ret);
248 mei_cldev_disable(cldev);
251 static void mei_gsc_mkhi_fix_ver(struct mei_cl_device *cldev)
255 /* No need to enable the client if nothing is needed from it */
256 if (!cldev->bus->fw_f_fw_ver_supported &&
257 cldev->bus->pxp_mode != MEI_DEV_PXP_INIT)
260 ret = mei_cldev_enable(cldev);
264 if (cldev->bus->pxp_mode == MEI_DEV_PXP_INIT) {
265 ret = mei_gfx_memory_ready(cldev);
267 dev_err(&cldev->dev, "memory ready command failed %d\n", ret);
269 dev_dbg(&cldev->dev, "memory ready command sent\n");
270 /* we go to reset after that */
271 cldev->bus->pxp_mode = MEI_DEV_PXP_SETUP;
275 ret = mei_fwver(cldev);
277 dev_err(&cldev->dev, "FW version command failed %d\n",
280 mei_cldev_disable(cldev);
284 * mei_wd - wd client on the bus, change protocol version
285 * as the API has changed.
287 * @cldev: me clients device
289 #if IS_ENABLED(CONFIG_INTEL_MEI_ME)
290 #include <linux/pci.h>
291 #include "hw-me-regs.h"
292 static void mei_wd(struct mei_cl_device *cldev)
294 struct pci_dev *pdev = to_pci_dev(cldev->dev.parent);
296 if (pdev->device == MEI_DEV_ID_WPT_LP ||
297 pdev->device == MEI_DEV_ID_SPT ||
298 pdev->device == MEI_DEV_ID_SPT_H)
299 cldev->me_cl->props.protocol_version = 0x2;
304 static inline void mei_wd(struct mei_cl_device *cldev) {}
305 #endif /* CONFIG_INTEL_MEI_ME */
317 struct mei_nfc_reply {
328 struct mei_nfc_if_version {
329 u8 radio_version_sw[3];
331 u8 radio_version_hw[3];
339 #define MEI_NFC_CMD_MAINTENANCE 0x00
340 #define MEI_NFC_SUBCMD_IF_VERSION 0x01
343 #define MEI_NFC_VENDOR_INSIDE 0x00
344 #define MEI_NFC_VENDOR_NXP 0x01
347 #define MEI_NFC_VENDOR_INSIDE_UREAD 0x00
348 #define MEI_NFC_VENDOR_NXP_PN544 0x01
351 * mei_nfc_if_version - get NFC interface version
353 * @cl: host client (nfc info)
354 * @ver: NFC interface version to be filled in
356 * Return: 0 on success; < 0 otherwise
358 static int mei_nfc_if_version(struct mei_cl *cl,
359 struct mei_nfc_if_version *ver)
361 struct mei_device *bus;
362 struct mei_nfc_cmd cmd = {
363 .command = MEI_NFC_CMD_MAINTENANCE,
365 .sub_command = MEI_NFC_SUBCMD_IF_VERSION,
367 struct mei_nfc_reply *reply = NULL;
368 size_t if_version_length;
374 WARN_ON(mutex_is_locked(&bus->device_lock));
376 ret = __mei_cl_send(cl, (u8 *)&cmd, sizeof(cmd), 0,
377 MEI_CL_IO_TX_BLOCKING);
379 dev_err(bus->dev, "Could not send IF version cmd\n");
383 /* to be sure on the stack we alloc memory */
384 if_version_length = sizeof(*reply) + sizeof(*ver);
386 reply = kzalloc(if_version_length, GFP_KERNEL);
391 bytes_recv = __mei_cl_recv(cl, (u8 *)reply, if_version_length, &vtag,
393 if (bytes_recv < 0 || (size_t)bytes_recv < if_version_length) {
394 dev_err(bus->dev, "Could not read IF version\n");
399 memcpy(ver, reply->data, sizeof(*ver));
401 dev_info(bus->dev, "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n",
402 ver->fw_ivn, ver->vendor_id, ver->radio_type);
410 * mei_nfc_radio_name - derive nfc radio name from the interface version
412 * @ver: NFC radio version
414 * Return: radio name string
416 static const char *mei_nfc_radio_name(struct mei_nfc_if_version *ver)
419 if (ver->vendor_id == MEI_NFC_VENDOR_INSIDE) {
420 if (ver->radio_type == MEI_NFC_VENDOR_INSIDE_UREAD)
424 if (ver->vendor_id == MEI_NFC_VENDOR_NXP) {
425 if (ver->radio_type == MEI_NFC_VENDOR_NXP_PN544)
433 * mei_nfc - The nfc fixup function. The function retrieves nfc radio
434 * name and set is as device attribute so we can load
435 * the proper device driver for it
437 * @cldev: me client device (nfc)
439 static void mei_nfc(struct mei_cl_device *cldev)
441 struct mei_device *bus;
443 struct mei_me_client *me_cl = NULL;
444 struct mei_nfc_if_version ver;
445 const char *radio_name = NULL;
450 mutex_lock(&bus->device_lock);
451 /* we need to connect to INFO GUID */
452 cl = mei_cl_alloc_linked(bus);
456 dev_err(bus->dev, "nfc hook alloc failed %d\n", ret);
460 me_cl = mei_me_cl_by_uuid(bus, &mei_nfc_info_guid);
463 dev_err(bus->dev, "Cannot find nfc info %d\n", ret);
467 ret = mei_cl_connect(cl, me_cl, NULL);
469 dev_err(&cldev->dev, "Can't connect to the NFC INFO ME ret = %d\n",
474 mutex_unlock(&bus->device_lock);
476 ret = mei_nfc_if_version(cl, &ver);
480 radio_name = mei_nfc_radio_name(&ver);
484 dev_err(&cldev->dev, "Can't get the NFC interface version ret = %d\n",
489 dev_dbg(bus->dev, "nfc radio %s\n", radio_name);
490 strscpy(cldev->name, radio_name, sizeof(cldev->name));
493 mutex_lock(&bus->device_lock);
494 if (mei_cl_disconnect(cl) < 0)
495 dev_err(bus->dev, "Can't disconnect the NFC INFO ME\n");
497 mei_cl_flush_queues(cl, NULL);
501 mutex_unlock(&bus->device_lock);
502 mei_me_cl_put(me_cl);
508 dev_dbg(bus->dev, "end of fixup match = %d\n", cldev->do_match);
512 * vt_support - enable on bus clients with vtag support
514 * @cldev: me clients device
516 static void vt_support(struct mei_cl_device *cldev)
518 if (cldev->me_cl->props.vt_supported == 1)
523 * pxp_is_ready - enable bus client if pxp is ready
525 * @cldev: me clients device
527 static void pxp_is_ready(struct mei_cl_device *cldev)
529 struct mei_device *bus = cldev->bus;
531 switch (bus->pxp_mode) {
532 case MEI_DEV_PXP_READY:
533 case MEI_DEV_PXP_DEFAULT:
542 #define MEI_FIXUP(_uuid, _hook) { _uuid, _hook }
544 static struct mei_fixup {
547 void (*hook)(struct mei_cl_device *cldev);
549 MEI_FIXUP(MEI_UUID_ANY, number_of_connections),
550 MEI_FIXUP(MEI_UUID_NFC_INFO, blacklist),
551 MEI_FIXUP(MEI_UUID_NFC_HCI, mei_nfc),
552 MEI_FIXUP(MEI_UUID_WD, mei_wd),
553 MEI_FIXUP(MEI_UUID_MKHIF_FIX, mei_mkhi_fix),
554 MEI_FIXUP(MEI_UUID_IGSC_MKHI, mei_gsc_mkhi_ver),
555 MEI_FIXUP(MEI_UUID_IGSC_MKHI_FIX, mei_gsc_mkhi_fix_ver),
556 MEI_FIXUP(MEI_UUID_HDCP, whitelist),
557 MEI_FIXUP(MEI_UUID_ANY, vt_support),
558 MEI_FIXUP(MEI_UUID_PAVP, pxp_is_ready),
562 * mei_cl_bus_dev_fixup - run fixup handlers
564 * @cldev: me client device
566 void mei_cl_bus_dev_fixup(struct mei_cl_device *cldev)
569 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
572 for (i = 0; i < ARRAY_SIZE(mei_fixups); i++) {
575 if (uuid_le_cmp(f->uuid, MEI_UUID_ANY) == 0 ||
576 uuid_le_cmp(f->uuid, *uuid) == 0)