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