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