Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (c) 2013-2020, Intel Corporation. All rights reserved.
0004  * Intel Management Engine Interface (Intel MEI) Linux driver
0005  */
0006 
0007 #include <linux/kernel.h>
0008 #include <linux/sched.h>
0009 #include <linux/module.h>
0010 #include <linux/device.h>
0011 #include <linux/slab.h>
0012 #include <linux/uuid.h>
0013 
0014 #include <linux/mei_cl_bus.h>
0015 
0016 #include "mei_dev.h"
0017 #include "client.h"
0018 
0019 #define MEI_UUID_NFC_INFO UUID_LE(0xd2de1625, 0x382d, 0x417d, \
0020             0x48, 0xa4, 0xef, 0xab, 0xba, 0x8a, 0x12, 0x06)
0021 
0022 static const uuid_le mei_nfc_info_guid = MEI_UUID_NFC_INFO;
0023 
0024 #define MEI_UUID_NFC_HCI UUID_LE(0x0bb17a78, 0x2a8e, 0x4c50, \
0025             0x94, 0xd4, 0x50, 0x26, 0x67, 0x23, 0x77, 0x5c)
0026 
0027 #define MEI_UUID_WD UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, \
0028                 0x89, 0x9D, 0xA9, 0x15, 0x14, 0xCB, 0x32, 0xAB)
0029 
0030 #define MEI_UUID_MKHIF_FIX UUID_LE(0x55213584, 0x9a29, 0x4916, \
0031             0xba, 0xdf, 0xf, 0xb7, 0xed, 0x68, 0x2a, 0xeb)
0032 
0033 #define MEI_UUID_IGSC_MKHI UUID_LE(0xE2C2AFA2, 0x3817, 0x4D19, \
0034             0x9D, 0x95, 0x06, 0xB1, 0x6B, 0x58, 0x8A, 0x5D)
0035 
0036 #define MEI_UUID_IGSC_MKHI_FIX UUID_LE(0x46E0C1FB, 0xA546, 0x414F, \
0037             0x91, 0x70, 0xB7, 0xF4, 0x6D, 0x57, 0xB4, 0xAD)
0038 
0039 #define MEI_UUID_HDCP UUID_LE(0xB638AB7E, 0x94E2, 0x4EA2, \
0040                   0xA5, 0x52, 0xD1, 0xC5, 0x4B, 0x62, 0x7F, 0x04)
0041 
0042 #define MEI_UUID_PAVP UUID_LE(0xfbf6fcf1, 0x96cf, 0x4e2e, 0xA6, \
0043                   0xa6, 0x1b, 0xab, 0x8c, 0xbe, 0x36, 0xb1)
0044 
0045 #define MEI_UUID_ANY NULL_UUID_LE
0046 
0047 /**
0048  * number_of_connections - determine whether an client be on the bus
0049  *    according number of connections
0050  *    We support only clients:
0051  *       1. with single connection
0052  *       2. and fixed clients (max_number_of_connections == 0)
0053  *
0054  * @cldev: me clients device
0055  */
0056 static void number_of_connections(struct mei_cl_device *cldev)
0057 {
0058     if (cldev->me_cl->props.max_number_of_connections > 1)
0059         cldev->do_match = 0;
0060 }
0061 
0062 /**
0063  * blacklist - blacklist a client from the bus
0064  *
0065  * @cldev: me clients device
0066  */
0067 static void blacklist(struct mei_cl_device *cldev)
0068 {
0069     cldev->do_match = 0;
0070 }
0071 
0072 /**
0073  * whitelist - forcefully whitelist client
0074  *
0075  * @cldev: me clients device
0076  */
0077 static void whitelist(struct mei_cl_device *cldev)
0078 {
0079     cldev->do_match = 1;
0080 }
0081 
0082 #define OSTYPE_LINUX    2
0083 struct mei_os_ver {
0084     __le16 build;
0085     __le16 reserved1;
0086     u8  os_type;
0087     u8  major;
0088     u8  minor;
0089     u8  reserved2;
0090 } __packed;
0091 
0092 #define MKHI_FEATURE_PTT 0x10
0093 
0094 struct mkhi_rule_id {
0095     __le16 rule_type;
0096     u8 feature_id;
0097     u8 reserved;
0098 } __packed;
0099 
0100 struct mkhi_fwcaps {
0101     struct mkhi_rule_id id;
0102     u8 len;
0103     u8 data[];
0104 } __packed;
0105 
0106 struct mkhi_fw_ver_block {
0107     u16 minor;
0108     u8 major;
0109     u8 platform;
0110     u16 buildno;
0111     u16 hotfix;
0112 } __packed;
0113 
0114 struct mkhi_fw_ver {
0115     struct mkhi_fw_ver_block ver[MEI_MAX_FW_VER_BLOCKS];
0116 } __packed;
0117 
0118 #define MKHI_FWCAPS_GROUP_ID 0x3
0119 #define MKHI_FWCAPS_SET_OS_VER_APP_RULE_CMD 6
0120 #define MKHI_GEN_GROUP_ID 0xFF
0121 #define MKHI_GEN_GET_FW_VERSION_CMD 0x2
0122 struct mkhi_msg_hdr {
0123     u8  group_id;
0124     u8  command;
0125     u8  reserved;
0126     u8  result;
0127 } __packed;
0128 
0129 struct mkhi_msg {
0130     struct mkhi_msg_hdr hdr;
0131     u8 data[];
0132 } __packed;
0133 
0134 #define MKHI_OSVER_BUF_LEN (sizeof(struct mkhi_msg_hdr) + \
0135                 sizeof(struct mkhi_fwcaps) + \
0136                 sizeof(struct mei_os_ver))
0137 static int mei_osver(struct mei_cl_device *cldev)
0138 {
0139     const size_t size = MKHI_OSVER_BUF_LEN;
0140     char buf[MKHI_OSVER_BUF_LEN];
0141     struct mkhi_msg *req;
0142     struct mkhi_fwcaps *fwcaps;
0143     struct mei_os_ver *os_ver;
0144     unsigned int mode = MEI_CL_IO_TX_BLOCKING | MEI_CL_IO_TX_INTERNAL;
0145 
0146     memset(buf, 0, size);
0147 
0148     req = (struct mkhi_msg *)buf;
0149     req->hdr.group_id = MKHI_FWCAPS_GROUP_ID;
0150     req->hdr.command = MKHI_FWCAPS_SET_OS_VER_APP_RULE_CMD;
0151 
0152     fwcaps = (struct mkhi_fwcaps *)req->data;
0153 
0154     fwcaps->id.rule_type = 0x0;
0155     fwcaps->id.feature_id = MKHI_FEATURE_PTT;
0156     fwcaps->len = sizeof(*os_ver);
0157     os_ver = (struct mei_os_ver *)fwcaps->data;
0158     os_ver->os_type = OSTYPE_LINUX;
0159 
0160     return __mei_cl_send(cldev->cl, buf, size, 0, mode);
0161 }
0162 
0163 #define MKHI_FWVER_BUF_LEN (sizeof(struct mkhi_msg_hdr) + \
0164                 sizeof(struct mkhi_fw_ver))
0165 #define MKHI_FWVER_LEN(__num) (sizeof(struct mkhi_msg_hdr) + \
0166                    sizeof(struct mkhi_fw_ver_block) * (__num))
0167 #define MKHI_RCV_TIMEOUT 500 /* receive timeout in msec */
0168 static int mei_fwver(struct mei_cl_device *cldev)
0169 {
0170     char buf[MKHI_FWVER_BUF_LEN];
0171     struct mkhi_msg req;
0172     struct mkhi_msg *rsp;
0173     struct mkhi_fw_ver *fwver;
0174     int bytes_recv, ret, i;
0175 
0176     memset(buf, 0, sizeof(buf));
0177 
0178     req.hdr.group_id = MKHI_GEN_GROUP_ID;
0179     req.hdr.command = MKHI_GEN_GET_FW_VERSION_CMD;
0180 
0181     ret = __mei_cl_send(cldev->cl, (u8 *)&req, sizeof(req), 0,
0182                 MEI_CL_IO_TX_BLOCKING);
0183     if (ret < 0) {
0184         dev_err(&cldev->dev, "Could not send ReqFWVersion cmd\n");
0185         return ret;
0186     }
0187 
0188     ret = 0;
0189     bytes_recv = __mei_cl_recv(cldev->cl, buf, sizeof(buf), NULL, 0,
0190                    MKHI_RCV_TIMEOUT);
0191     if (bytes_recv < 0 || (size_t)bytes_recv < MKHI_FWVER_LEN(1)) {
0192         /*
0193          * Should be at least one version block,
0194          * error out if nothing found
0195          */
0196         dev_err(&cldev->dev, "Could not read FW version\n");
0197         return -EIO;
0198     }
0199 
0200     rsp = (struct mkhi_msg *)buf;
0201     fwver = (struct mkhi_fw_ver *)rsp->data;
0202     memset(cldev->bus->fw_ver, 0, sizeof(cldev->bus->fw_ver));
0203     for (i = 0; i < MEI_MAX_FW_VER_BLOCKS; i++) {
0204         if ((size_t)bytes_recv < MKHI_FWVER_LEN(i + 1))
0205             break;
0206         dev_dbg(&cldev->dev, "FW version%d %d:%d.%d.%d.%d\n",
0207             i, fwver->ver[i].platform,
0208             fwver->ver[i].major, fwver->ver[i].minor,
0209             fwver->ver[i].hotfix, fwver->ver[i].buildno);
0210 
0211         cldev->bus->fw_ver[i].platform = fwver->ver[i].platform;
0212         cldev->bus->fw_ver[i].major = fwver->ver[i].major;
0213         cldev->bus->fw_ver[i].minor = fwver->ver[i].minor;
0214         cldev->bus->fw_ver[i].hotfix = fwver->ver[i].hotfix;
0215         cldev->bus->fw_ver[i].buildno = fwver->ver[i].buildno;
0216     }
0217 
0218     return ret;
0219 }
0220 
0221 static void mei_mkhi_fix(struct mei_cl_device *cldev)
0222 {
0223     int ret;
0224 
0225     /* No need to enable the client if nothing is needed from it */
0226     if (!cldev->bus->fw_f_fw_ver_supported &&
0227         !cldev->bus->hbm_f_os_supported)
0228         return;
0229 
0230     ret = mei_cldev_enable(cldev);
0231     if (ret)
0232         return;
0233 
0234     if (cldev->bus->fw_f_fw_ver_supported) {
0235         ret = mei_fwver(cldev);
0236         if (ret < 0)
0237             dev_err(&cldev->dev, "FW version command failed %d\n",
0238                 ret);
0239     }
0240 
0241     if (cldev->bus->hbm_f_os_supported) {
0242         ret = mei_osver(cldev);
0243         if (ret < 0)
0244             dev_err(&cldev->dev, "OS version command failed %d\n",
0245                 ret);
0246     }
0247     mei_cldev_disable(cldev);
0248 }
0249 
0250 static void mei_gsc_mkhi_ver(struct mei_cl_device *cldev)
0251 {
0252     int ret;
0253 
0254     /* No need to enable the client if nothing is needed from it */
0255     if (!cldev->bus->fw_f_fw_ver_supported)
0256         return;
0257 
0258     ret = mei_cldev_enable(cldev);
0259     if (ret)
0260         return;
0261 
0262     ret = mei_fwver(cldev);
0263     if (ret < 0)
0264         dev_err(&cldev->dev, "FW version command failed %d\n", ret);
0265     mei_cldev_disable(cldev);
0266 }
0267 /**
0268  * mei_wd - wd client on the bus, change protocol version
0269  *   as the API has changed.
0270  *
0271  * @cldev: me clients device
0272  */
0273 #if IS_ENABLED(CONFIG_INTEL_MEI_ME)
0274 #include <linux/pci.h>
0275 #include "hw-me-regs.h"
0276 static void mei_wd(struct mei_cl_device *cldev)
0277 {
0278     struct pci_dev *pdev = to_pci_dev(cldev->dev.parent);
0279 
0280     if (pdev->device == MEI_DEV_ID_WPT_LP ||
0281         pdev->device == MEI_DEV_ID_SPT ||
0282         pdev->device == MEI_DEV_ID_SPT_H)
0283         cldev->me_cl->props.protocol_version = 0x2;
0284 
0285     cldev->do_match = 1;
0286 }
0287 #else
0288 static inline void mei_wd(struct mei_cl_device *cldev) {}
0289 #endif /* CONFIG_INTEL_MEI_ME */
0290 
0291 struct mei_nfc_cmd {
0292     u8 command;
0293     u8 status;
0294     u16 req_id;
0295     u32 reserved;
0296     u16 data_size;
0297     u8 sub_command;
0298     u8 data[];
0299 } __packed;
0300 
0301 struct mei_nfc_reply {
0302     u8 command;
0303     u8 status;
0304     u16 req_id;
0305     u32 reserved;
0306     u16 data_size;
0307     u8 sub_command;
0308     u8 reply_status;
0309     u8 data[];
0310 } __packed;
0311 
0312 struct mei_nfc_if_version {
0313     u8 radio_version_sw[3];
0314     u8 reserved[3];
0315     u8 radio_version_hw[3];
0316     u8 i2c_addr;
0317     u8 fw_ivn;
0318     u8 vendor_id;
0319     u8 radio_type;
0320 } __packed;
0321 
0322 
0323 #define MEI_NFC_CMD_MAINTENANCE 0x00
0324 #define MEI_NFC_SUBCMD_IF_VERSION 0x01
0325 
0326 /* Vendors */
0327 #define MEI_NFC_VENDOR_INSIDE 0x00
0328 #define MEI_NFC_VENDOR_NXP    0x01
0329 
0330 /* Radio types */
0331 #define MEI_NFC_VENDOR_INSIDE_UREAD 0x00
0332 #define MEI_NFC_VENDOR_NXP_PN544    0x01
0333 
0334 /**
0335  * mei_nfc_if_version - get NFC interface version
0336  *
0337  * @cl: host client (nfc info)
0338  * @ver: NFC interface version to be filled in
0339  *
0340  * Return: 0 on success; < 0 otherwise
0341  */
0342 static int mei_nfc_if_version(struct mei_cl *cl,
0343                   struct mei_nfc_if_version *ver)
0344 {
0345     struct mei_device *bus;
0346     struct mei_nfc_cmd cmd = {
0347         .command = MEI_NFC_CMD_MAINTENANCE,
0348         .data_size = 1,
0349         .sub_command = MEI_NFC_SUBCMD_IF_VERSION,
0350     };
0351     struct mei_nfc_reply *reply = NULL;
0352     size_t if_version_length;
0353     u8 vtag;
0354     int bytes_recv, ret;
0355 
0356     bus = cl->dev;
0357 
0358     WARN_ON(mutex_is_locked(&bus->device_lock));
0359 
0360     ret = __mei_cl_send(cl, (u8 *)&cmd, sizeof(cmd), 0,
0361                 MEI_CL_IO_TX_BLOCKING);
0362     if (ret < 0) {
0363         dev_err(bus->dev, "Could not send IF version cmd\n");
0364         return ret;
0365     }
0366 
0367     /* to be sure on the stack we alloc memory */
0368     if_version_length = sizeof(*reply) + sizeof(*ver);
0369 
0370     reply = kzalloc(if_version_length, GFP_KERNEL);
0371     if (!reply)
0372         return -ENOMEM;
0373 
0374     ret = 0;
0375     bytes_recv = __mei_cl_recv(cl, (u8 *)reply, if_version_length, &vtag,
0376                    0, 0);
0377     if (bytes_recv < 0 || (size_t)bytes_recv < if_version_length) {
0378         dev_err(bus->dev, "Could not read IF version\n");
0379         ret = -EIO;
0380         goto err;
0381     }
0382 
0383     memcpy(ver, reply->data, sizeof(*ver));
0384 
0385     dev_info(bus->dev, "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n",
0386         ver->fw_ivn, ver->vendor_id, ver->radio_type);
0387 
0388 err:
0389     kfree(reply);
0390     return ret;
0391 }
0392 
0393 /**
0394  * mei_nfc_radio_name - derive nfc radio name from the interface version
0395  *
0396  * @ver: NFC radio version
0397  *
0398  * Return: radio name string
0399  */
0400 static const char *mei_nfc_radio_name(struct mei_nfc_if_version *ver)
0401 {
0402 
0403     if (ver->vendor_id == MEI_NFC_VENDOR_INSIDE) {
0404         if (ver->radio_type == MEI_NFC_VENDOR_INSIDE_UREAD)
0405             return "microread";
0406     }
0407 
0408     if (ver->vendor_id == MEI_NFC_VENDOR_NXP) {
0409         if (ver->radio_type == MEI_NFC_VENDOR_NXP_PN544)
0410             return "pn544";
0411     }
0412 
0413     return NULL;
0414 }
0415 
0416 /**
0417  * mei_nfc - The nfc fixup function. The function retrieves nfc radio
0418  *    name and set is as device attribute so we can load
0419  *    the proper device driver for it
0420  *
0421  * @cldev: me client device (nfc)
0422  */
0423 static void mei_nfc(struct mei_cl_device *cldev)
0424 {
0425     struct mei_device *bus;
0426     struct mei_cl *cl;
0427     struct mei_me_client *me_cl = NULL;
0428     struct mei_nfc_if_version ver;
0429     const char *radio_name = NULL;
0430     int ret;
0431 
0432     bus = cldev->bus;
0433 
0434     mutex_lock(&bus->device_lock);
0435     /* we need to connect to INFO GUID */
0436     cl = mei_cl_alloc_linked(bus);
0437     if (IS_ERR(cl)) {
0438         ret = PTR_ERR(cl);
0439         cl = NULL;
0440         dev_err(bus->dev, "nfc hook alloc failed %d\n", ret);
0441         goto out;
0442     }
0443 
0444     me_cl = mei_me_cl_by_uuid(bus, &mei_nfc_info_guid);
0445     if (!me_cl) {
0446         ret = -ENOTTY;
0447         dev_err(bus->dev, "Cannot find nfc info %d\n", ret);
0448         goto out;
0449     }
0450 
0451     ret = mei_cl_connect(cl, me_cl, NULL);
0452     if (ret < 0) {
0453         dev_err(&cldev->dev, "Can't connect to the NFC INFO ME ret = %d\n",
0454             ret);
0455         goto out;
0456     }
0457 
0458     mutex_unlock(&bus->device_lock);
0459 
0460     ret = mei_nfc_if_version(cl, &ver);
0461     if (ret)
0462         goto disconnect;
0463 
0464     radio_name = mei_nfc_radio_name(&ver);
0465 
0466     if (!radio_name) {
0467         ret = -ENOENT;
0468         dev_err(&cldev->dev, "Can't get the NFC interface version ret = %d\n",
0469             ret);
0470         goto disconnect;
0471     }
0472 
0473     dev_dbg(bus->dev, "nfc radio %s\n", radio_name);
0474     strlcpy(cldev->name, radio_name, sizeof(cldev->name));
0475 
0476 disconnect:
0477     mutex_lock(&bus->device_lock);
0478     if (mei_cl_disconnect(cl) < 0)
0479         dev_err(bus->dev, "Can't disconnect the NFC INFO ME\n");
0480 
0481     mei_cl_flush_queues(cl, NULL);
0482 
0483 out:
0484     mei_cl_unlink(cl);
0485     mutex_unlock(&bus->device_lock);
0486     mei_me_cl_put(me_cl);
0487     kfree(cl);
0488 
0489     if (ret)
0490         cldev->do_match = 0;
0491 
0492     dev_dbg(bus->dev, "end of fixup match = %d\n", cldev->do_match);
0493 }
0494 
0495 /**
0496  * vt_support - enable on bus clients with vtag support
0497  *
0498  * @cldev: me clients device
0499  */
0500 static void vt_support(struct mei_cl_device *cldev)
0501 {
0502     if (cldev->me_cl->props.vt_supported == 1)
0503         cldev->do_match = 1;
0504 }
0505 
0506 #define MEI_FIXUP(_uuid, _hook) { _uuid, _hook }
0507 
0508 static struct mei_fixup {
0509 
0510     const uuid_le uuid;
0511     void (*hook)(struct mei_cl_device *cldev);
0512 } mei_fixups[] = {
0513     MEI_FIXUP(MEI_UUID_ANY, number_of_connections),
0514     MEI_FIXUP(MEI_UUID_NFC_INFO, blacklist),
0515     MEI_FIXUP(MEI_UUID_NFC_HCI, mei_nfc),
0516     MEI_FIXUP(MEI_UUID_WD, mei_wd),
0517     MEI_FIXUP(MEI_UUID_MKHIF_FIX, mei_mkhi_fix),
0518     MEI_FIXUP(MEI_UUID_IGSC_MKHI, mei_gsc_mkhi_ver),
0519     MEI_FIXUP(MEI_UUID_IGSC_MKHI_FIX, mei_gsc_mkhi_ver),
0520     MEI_FIXUP(MEI_UUID_HDCP, whitelist),
0521     MEI_FIXUP(MEI_UUID_ANY, vt_support),
0522     MEI_FIXUP(MEI_UUID_PAVP, whitelist),
0523 };
0524 
0525 /**
0526  * mei_cl_bus_dev_fixup - run fixup handlers
0527  *
0528  * @cldev: me client device
0529  */
0530 void mei_cl_bus_dev_fixup(struct mei_cl_device *cldev)
0531 {
0532     struct mei_fixup *f;
0533     const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
0534     size_t i;
0535 
0536     for (i = 0; i < ARRAY_SIZE(mei_fixups); i++) {
0537 
0538         f = &mei_fixups[i];
0539         if (uuid_le_cmp(f->uuid, MEI_UUID_ANY) == 0 ||
0540             uuid_le_cmp(f->uuid, *uuid) == 0)
0541             f->hook(cldev);
0542     }
0543 }
0544