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