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