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