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