xref: /linux/drivers/platform/mellanox/mlxbf-bootctl.c (revision 5b05bb3f6c5716fab6911e12d60dd1f43ad9806a)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Mellanox boot control driver
4  *
5  * This driver provides a sysfs interface for systems management
6  * software to manage reset-time actions.
7  *
8  * Copyright (C) 2019 Mellanox Technologies
9  */
10 
11 #include <linux/acpi.h>
12 #include <linux/arm-smccc.h>
13 #include <linux/bitfield.h>
14 #include <linux/delay.h>
15 #include <linux/if_ether.h>
16 #include <linux/iopoll.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 
20 #include "mlxbf-bootctl.h"
21 
22 #define MLXBF_BOOTCTL_SB_SECURE_MASK		0x03
23 #define MLXBF_BOOTCTL_SB_TEST_MASK		0x0c
24 #define MLXBF_BOOTCTL_SB_DEV_MASK		BIT(4)
25 
26 #define MLXBF_SB_KEY_NUM			4
27 
28 /* UUID used to probe ATF service. */
29 static const char *mlxbf_bootctl_svc_uuid_str =
30 	"89c036b4-e7d7-11e6-8797-001aca00bfc4";
31 
32 struct mlxbf_bootctl_name {
33 	u32 value;
34 	const char *name;
35 };
36 
37 static struct mlxbf_bootctl_name boot_names[] = {
38 	{ MLXBF_BOOTCTL_EXTERNAL, "external" },
39 	{ MLXBF_BOOTCTL_EMMC, "emmc" },
40 	{ MLNX_BOOTCTL_SWAP_EMMC, "swap_emmc" },
41 	{ MLXBF_BOOTCTL_EMMC_LEGACY, "emmc_legacy" },
42 	{ MLXBF_BOOTCTL_NONE, "none" },
43 };
44 
45 enum {
46 	MLXBF_BOOTCTL_SB_LIFECYCLE_PRODUCTION = 0,
47 	MLXBF_BOOTCTL_SB_LIFECYCLE_GA_SECURE = 1,
48 	MLXBF_BOOTCTL_SB_LIFECYCLE_GA_NON_SECURE = 2,
49 	MLXBF_BOOTCTL_SB_LIFECYCLE_RMA = 3
50 };
51 
52 static const char * const mlxbf_bootctl_lifecycle_states[] = {
53 	[MLXBF_BOOTCTL_SB_LIFECYCLE_PRODUCTION] = "Production",
54 	[MLXBF_BOOTCTL_SB_LIFECYCLE_GA_SECURE] = "GA Secured",
55 	[MLXBF_BOOTCTL_SB_LIFECYCLE_GA_NON_SECURE] = "GA Non-Secured",
56 	[MLXBF_BOOTCTL_SB_LIFECYCLE_RMA] = "RMA",
57 };
58 
59 /* Log header format. */
60 #define MLXBF_RSH_LOG_TYPE_MASK		GENMASK_ULL(59, 56)
61 #define MLXBF_RSH_LOG_LEN_MASK		GENMASK_ULL(54, 48)
62 #define MLXBF_RSH_LOG_LEVEL_MASK	GENMASK_ULL(7, 0)
63 
64 /* Log module ID and type (only MSG type in Linux driver for now). */
65 #define MLXBF_RSH_LOG_TYPE_MSG		0x04ULL
66 
67 /* Log ctl/data register offset. */
68 #define MLXBF_RSH_SCRATCH_BUF_CTL_OFF	0
69 #define MLXBF_RSH_SCRATCH_BUF_DATA_OFF	0x10
70 
71 /* Log message levels. */
72 enum {
73 	MLXBF_RSH_LOG_INFO,
74 	MLXBF_RSH_LOG_WARN,
75 	MLXBF_RSH_LOG_ERR,
76 	MLXBF_RSH_LOG_ASSERT
77 };
78 
79 /* Mapped pointer for RSH_BOOT_FIFO_DATA and RSH_BOOT_FIFO_COUNT register. */
80 static void __iomem *mlxbf_rsh_boot_data;
81 static void __iomem *mlxbf_rsh_boot_cnt;
82 
83 /* Mapped pointer for rsh log semaphore/ctrl/data register. */
84 static void __iomem *mlxbf_rsh_semaphore;
85 static void __iomem *mlxbf_rsh_scratch_buf_ctl;
86 static void __iomem *mlxbf_rsh_scratch_buf_data;
87 
88 /* Rsh log levels. */
89 static const char * const mlxbf_rsh_log_level[] = {
90 	"INFO", "WARN", "ERR", "ASSERT"};
91 
92 static DEFINE_MUTEX(icm_ops_lock);
93 static DEFINE_MUTEX(os_up_lock);
94 static DEFINE_MUTEX(mfg_ops_lock);
95 static DEFINE_MUTEX(rtc_ops_lock);
96 
97 /*
98  * Objects are stored within the MFG partition per type.
99  * Type 0 is not supported.
100  */
101 enum {
102 	MLNX_MFG_TYPE_OOB_MAC = 1,
103 	MLNX_MFG_TYPE_OPN_0,
104 	MLNX_MFG_TYPE_OPN_1,
105 	MLNX_MFG_TYPE_OPN_2,
106 	MLNX_MFG_TYPE_SKU_0,
107 	MLNX_MFG_TYPE_SKU_1,
108 	MLNX_MFG_TYPE_SKU_2,
109 	MLNX_MFG_TYPE_MODL_0,
110 	MLNX_MFG_TYPE_MODL_1,
111 	MLNX_MFG_TYPE_MODL_2,
112 	MLNX_MFG_TYPE_SN_0,
113 	MLNX_MFG_TYPE_SN_1,
114 	MLNX_MFG_TYPE_SN_2,
115 	MLNX_MFG_TYPE_UUID_0,
116 	MLNX_MFG_TYPE_UUID_1,
117 	MLNX_MFG_TYPE_UUID_2,
118 	MLNX_MFG_TYPE_UUID_3,
119 	MLNX_MFG_TYPE_UUID_4,
120 	MLNX_MFG_TYPE_REV,
121 };
122 
123 #define MLNX_MFG_OPN_VAL_LEN         24
124 #define MLNX_MFG_SKU_VAL_LEN         24
125 #define MLNX_MFG_MODL_VAL_LEN        24
126 #define MLNX_MFG_SN_VAL_LEN          24
127 #define MLNX_MFG_UUID_VAL_LEN        40
128 #define MLNX_MFG_REV_VAL_LEN         8
129 #define MLNX_MFG_VAL_QWORD_CNT(type) \
130 	(MLNX_MFG_##type##_VAL_LEN / sizeof(u64))
131 
132 /*
133  * The MAC address consists of 6 bytes (2 digits each) separated by ':'.
134  * The expected format is: "XX:XX:XX:XX:XX:XX"
135  */
136 #define MLNX_MFG_OOB_MAC_FORMAT_LEN \
137 	((ETH_ALEN * 2) + (ETH_ALEN - 1))
138 
139 /* ARM SMC call which is atomic and no need for lock. */
mlxbf_bootctl_smc(unsigned int smc_op,int smc_arg)140 static int mlxbf_bootctl_smc(unsigned int smc_op, int smc_arg)
141 {
142 	struct arm_smccc_res res;
143 
144 	arm_smccc_smc(smc_op, smc_arg, 0, 0, 0, 0, 0, 0, &res);
145 
146 	return res.a0;
147 }
148 
149 /* Return the action in integer or an error code. */
mlxbf_bootctl_reset_action_to_val(const char * action)150 static int mlxbf_bootctl_reset_action_to_val(const char *action)
151 {
152 	int i;
153 
154 	for (i = 0; i < ARRAY_SIZE(boot_names); i++)
155 		if (sysfs_streq(boot_names[i].name, action))
156 			return boot_names[i].value;
157 
158 	return -EINVAL;
159 }
160 
161 /* Return the action in string. */
mlxbf_bootctl_action_to_string(int action)162 static const char *mlxbf_bootctl_action_to_string(int action)
163 {
164 	int i;
165 
166 	for (i = 0; i < ARRAY_SIZE(boot_names); i++)
167 		if (boot_names[i].value == action)
168 			return boot_names[i].name;
169 
170 	return "invalid action";
171 }
172 
post_reset_wdog_show(struct device * dev,struct device_attribute * attr,char * buf)173 static ssize_t post_reset_wdog_show(struct device *dev,
174 				    struct device_attribute *attr, char *buf)
175 {
176 	int ret;
177 
178 	ret = mlxbf_bootctl_smc(MLXBF_BOOTCTL_GET_POST_RESET_WDOG, 0);
179 	if (ret < 0)
180 		return ret;
181 
182 	return sysfs_emit(buf, "%d\n", ret);
183 }
184 
post_reset_wdog_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)185 static ssize_t post_reset_wdog_store(struct device *dev,
186 				     struct device_attribute *attr,
187 				     const char *buf, size_t count)
188 {
189 	unsigned long value;
190 	int ret;
191 
192 	ret = kstrtoul(buf, 10, &value);
193 	if (ret)
194 		return ret;
195 
196 	ret = mlxbf_bootctl_smc(MLXBF_BOOTCTL_SET_POST_RESET_WDOG, value);
197 	if (ret < 0)
198 		return ret;
199 
200 	return count;
201 }
202 
mlxbf_bootctl_show(int smc_op,char * buf)203 static ssize_t mlxbf_bootctl_show(int smc_op, char *buf)
204 {
205 	int action;
206 
207 	action = mlxbf_bootctl_smc(smc_op, 0);
208 	if (action < 0)
209 		return action;
210 
211 	return sysfs_emit(buf, "%s\n", mlxbf_bootctl_action_to_string(action));
212 }
213 
mlxbf_bootctl_store(int smc_op,const char * buf,size_t count)214 static int mlxbf_bootctl_store(int smc_op, const char *buf, size_t count)
215 {
216 	int ret, action;
217 
218 	action = mlxbf_bootctl_reset_action_to_val(buf);
219 	if (action < 0)
220 		return action;
221 
222 	ret = mlxbf_bootctl_smc(smc_op, action);
223 	if (ret < 0)
224 		return ret;
225 
226 	return count;
227 }
228 
reset_action_show(struct device * dev,struct device_attribute * attr,char * buf)229 static ssize_t reset_action_show(struct device *dev,
230 				 struct device_attribute *attr, char *buf)
231 {
232 	return mlxbf_bootctl_show(MLXBF_BOOTCTL_GET_RESET_ACTION, buf);
233 }
234 
reset_action_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)235 static ssize_t reset_action_store(struct device *dev,
236 				  struct device_attribute *attr,
237 				  const char *buf, size_t count)
238 {
239 	return mlxbf_bootctl_store(MLXBF_BOOTCTL_SET_RESET_ACTION, buf, count);
240 }
241 
second_reset_action_show(struct device * dev,struct device_attribute * attr,char * buf)242 static ssize_t second_reset_action_show(struct device *dev,
243 					struct device_attribute *attr,
244 					char *buf)
245 {
246 	return mlxbf_bootctl_show(MLXBF_BOOTCTL_GET_SECOND_RESET_ACTION, buf);
247 }
248 
second_reset_action_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)249 static ssize_t second_reset_action_store(struct device *dev,
250 					 struct device_attribute *attr,
251 					 const char *buf, size_t count)
252 {
253 	return mlxbf_bootctl_store(MLXBF_BOOTCTL_SET_SECOND_RESET_ACTION, buf,
254 				   count);
255 }
256 
lifecycle_state_show(struct device * dev,struct device_attribute * attr,char * buf)257 static ssize_t lifecycle_state_show(struct device *dev,
258 				    struct device_attribute *attr, char *buf)
259 {
260 	int status_bits;
261 	int use_dev_key;
262 	int test_state;
263 	int lc_state;
264 
265 	status_bits = mlxbf_bootctl_smc(MLXBF_BOOTCTL_GET_TBB_FUSE_STATUS,
266 					MLXBF_BOOTCTL_FUSE_STATUS_LIFECYCLE);
267 	if (status_bits < 0)
268 		return status_bits;
269 
270 	use_dev_key = status_bits & MLXBF_BOOTCTL_SB_DEV_MASK;
271 	test_state = status_bits & MLXBF_BOOTCTL_SB_TEST_MASK;
272 	lc_state = status_bits & MLXBF_BOOTCTL_SB_SECURE_MASK;
273 
274 	/*
275 	 * If the test bits are set, we specify that the current state may be
276 	 * due to using the test bits.
277 	 */
278 	if (test_state) {
279 		return sysfs_emit(buf, "%s(test)\n",
280 			       mlxbf_bootctl_lifecycle_states[lc_state]);
281 	} else if (use_dev_key &&
282 		   (lc_state == MLXBF_BOOTCTL_SB_LIFECYCLE_GA_SECURE)) {
283 		return sysfs_emit(buf, "Secured (development)\n");
284 	}
285 
286 	return sysfs_emit(buf, "%s\n", mlxbf_bootctl_lifecycle_states[lc_state]);
287 }
288 
secure_boot_fuse_state_show(struct device * dev,struct device_attribute * attr,char * buf)289 static ssize_t secure_boot_fuse_state_show(struct device *dev,
290 					   struct device_attribute *attr,
291 					   char *buf)
292 {
293 	int burnt, valid, key, key_state, buf_len = 0, upper_key_used = 0;
294 	const char *status;
295 
296 	key_state = mlxbf_bootctl_smc(MLXBF_BOOTCTL_GET_TBB_FUSE_STATUS,
297 				      MLXBF_BOOTCTL_FUSE_STATUS_KEYS);
298 	if (key_state < 0)
299 		return key_state;
300 
301 	/*
302 	 * key_state contains the bits for 4 Key versions, loaded from eFuses
303 	 * after a hard reset. Lower 4 bits are a thermometer code indicating
304 	 * key programming has started for key n (0000 = none, 0001 = version 0,
305 	 * 0011 = version 1, 0111 = version 2, 1111 = version 3). Upper 4 bits
306 	 * are a thermometer code indicating key programming has completed for
307 	 * key n (same encodings as the start bits). This allows for detection
308 	 * of an interruption in the programming process which has left the key
309 	 * partially programmed (and thus invalid). The process is to burn the
310 	 * eFuse for the new key start bit, burn the key eFuses, then burn the
311 	 * eFuse for the new key complete bit.
312 	 *
313 	 * For example 0000_0000: no key valid, 0001_0001: key version 0 valid,
314 	 * 0011_0011: key 1 version valid, 0011_0111: key version 2 started
315 	 * programming but did not complete, etc. The most recent key for which
316 	 * both start and complete bit is set is loaded. On soft reset, this
317 	 * register is not modified.
318 	 */
319 	for (key = MLXBF_SB_KEY_NUM - 1; key >= 0; key--) {
320 		burnt = key_state & BIT(key);
321 		valid = key_state & BIT(key + MLXBF_SB_KEY_NUM);
322 
323 		if (burnt && valid)
324 			upper_key_used = 1;
325 
326 		if (upper_key_used) {
327 			if (burnt)
328 				status = valid ? "Used" : "Wasted";
329 			else
330 				status = valid ? "Invalid" : "Skipped";
331 		} else {
332 			if (burnt)
333 				status = valid ? "InUse" : "Incomplete";
334 			else
335 				status = valid ? "Invalid" : "Free";
336 		}
337 		buf_len += sysfs_emit_at(buf, buf_len, "%d:%s ", key, status);
338 	}
339 	buf_len += sysfs_emit_at(buf, buf_len, "\n");
340 
341 	return buf_len;
342 }
343 
fw_reset_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)344 static ssize_t fw_reset_store(struct device *dev,
345 			      struct device_attribute *attr,
346 			      const char *buf, size_t count)
347 {
348 	unsigned long key;
349 	int err;
350 
351 	err = kstrtoul(buf, 16, &key);
352 	if (err)
353 		return err;
354 
355 	if (mlxbf_bootctl_smc(MLXBF_BOOTCTL_FW_RESET, key) < 0)
356 		return -EINVAL;
357 
358 	return count;
359 }
360 
361 /* Size(8-byte words) of the log buffer. */
362 #define RSH_SCRATCH_BUF_CTL_IDX_MASK	0x7f
363 
364 /* 100ms timeout */
365 #define RSH_SCRATCH_BUF_POLL_TIMEOUT	100000
366 
mlxbf_rsh_log_sem_lock(void)367 static int mlxbf_rsh_log_sem_lock(void)
368 {
369 	unsigned long reg;
370 
371 	return readq_poll_timeout(mlxbf_rsh_semaphore, reg, !reg, 0,
372 				  RSH_SCRATCH_BUF_POLL_TIMEOUT);
373 }
374 
mlxbf_rsh_log_sem_unlock(void)375 static void mlxbf_rsh_log_sem_unlock(void)
376 {
377 	writeq(0, mlxbf_rsh_semaphore);
378 }
379 
rsh_log_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)380 static ssize_t rsh_log_store(struct device *dev,
381 			     struct device_attribute *attr,
382 			     const char *buf, size_t count)
383 {
384 	int rc, idx, num, len, level = MLXBF_RSH_LOG_INFO;
385 	size_t size = count;
386 	u64 data;
387 
388 	if (!size)
389 		return -EINVAL;
390 
391 	if (!mlxbf_rsh_semaphore || !mlxbf_rsh_scratch_buf_ctl)
392 		return -EOPNOTSUPP;
393 
394 	/* Ignore line break at the end. */
395 	if (buf[size - 1] == '\n')
396 		size--;
397 
398 	/* Check the message prefix. */
399 	for (idx = 0; idx < ARRAY_SIZE(mlxbf_rsh_log_level); idx++) {
400 		len = strlen(mlxbf_rsh_log_level[idx]);
401 		if (len + 1 < size &&
402 		    !strncmp(buf, mlxbf_rsh_log_level[idx], len)) {
403 			buf += len;
404 			size -= len;
405 			level = idx;
406 			break;
407 		}
408 	}
409 
410 	/* Ignore leading spaces. */
411 	while (size > 0 && buf[0] == ' ') {
412 		size--;
413 		buf++;
414 	}
415 
416 	/* Take the semaphore. */
417 	rc = mlxbf_rsh_log_sem_lock();
418 	if (rc)
419 		return rc;
420 
421 	/* Calculate how many words are available. */
422 	idx = readq(mlxbf_rsh_scratch_buf_ctl);
423 	num = min((int)DIV_ROUND_UP(size, sizeof(u64)),
424 		  RSH_SCRATCH_BUF_CTL_IDX_MASK - idx - 1);
425 	if (num <= 0)
426 		goto done;
427 
428 	/* Write Header. */
429 	data = FIELD_PREP(MLXBF_RSH_LOG_TYPE_MASK, MLXBF_RSH_LOG_TYPE_MSG);
430 	data |= FIELD_PREP(MLXBF_RSH_LOG_LEN_MASK, num);
431 	data |= FIELD_PREP(MLXBF_RSH_LOG_LEVEL_MASK, level);
432 	writeq(data, mlxbf_rsh_scratch_buf_data);
433 
434 	/* Write message. */
435 	for (idx = 0; idx < num && size > 0; idx++) {
436 		if (size < sizeof(u64)) {
437 			data = 0;
438 			memcpy(&data, buf, size);
439 			size = 0;
440 		} else {
441 			memcpy(&data, buf, sizeof(u64));
442 			size -= sizeof(u64);
443 			buf += sizeof(u64);
444 		}
445 		writeq(data, mlxbf_rsh_scratch_buf_data);
446 	}
447 
448 done:
449 	/* Release the semaphore. */
450 	mlxbf_rsh_log_sem_unlock();
451 
452 	/* Ignore the rest if no more space. */
453 	return count;
454 }
455 
large_icm_show(struct device * dev,struct device_attribute * attr,char * buf)456 static ssize_t large_icm_show(struct device *dev,
457 				struct device_attribute *attr, char *buf)
458 {
459 	struct arm_smccc_res res;
460 
461 	mutex_lock(&icm_ops_lock);
462 	arm_smccc_smc(MLNX_HANDLE_GET_ICM_INFO, 0, 0, 0, 0,
463 		      0, 0, 0, &res);
464 	mutex_unlock(&icm_ops_lock);
465 	if (res.a0)
466 		return -EPERM;
467 
468 	return sysfs_emit(buf, "0x%lx", res.a1);
469 }
470 
large_icm_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)471 static ssize_t large_icm_store(struct device *dev,
472 			       struct device_attribute *attr,
473 			       const char *buf, size_t count)
474 {
475 	struct arm_smccc_res res;
476 	unsigned long icm_data;
477 	int err;
478 
479 	err = kstrtoul(buf, MLXBF_LARGE_ICMC_MAX_STRING_SIZE, &icm_data);
480 	if (err)
481 		return err;
482 
483 	if ((icm_data != 0 && icm_data < MLXBF_LARGE_ICMC_SIZE_MIN) ||
484 	    icm_data > MLXBF_LARGE_ICMC_SIZE_MAX || icm_data % MLXBF_LARGE_ICMC_GRANULARITY)
485 		return -EPERM;
486 
487 	mutex_lock(&icm_ops_lock);
488 	arm_smccc_smc(MLNX_HANDLE_SET_ICM_INFO, icm_data, 0, 0, 0, 0, 0, 0, &res);
489 	mutex_unlock(&icm_ops_lock);
490 
491 	return res.a0 ? -EPERM : count;
492 }
493 
rtc_battery_show(struct device * dev,struct device_attribute * attr,char * buf)494 static ssize_t rtc_battery_show(struct device *dev,
495 				struct device_attribute *attr,
496 				char *buf)
497 {
498 	struct arm_smccc_res res;
499 
500 	mutex_lock(&rtc_ops_lock);
501 	arm_smccc_smc(MLNX_HANDLE_GET_RTC_LOW_BATT, 0, 0, 0, 0,
502 		      0, 0, 0, &res);
503 	mutex_unlock(&rtc_ops_lock);
504 
505 	if (res.a0)
506 		return -EPERM;
507 
508 	return sysfs_emit(buf, "0x%lx\n", res.a1);
509 }
510 
os_up_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)511 static ssize_t os_up_store(struct device *dev,
512 			   struct device_attribute *attr,
513 			   const char *buf, size_t count)
514 {
515 	struct arm_smccc_res res;
516 	unsigned long val;
517 	int err;
518 
519 	err = kstrtoul(buf, 10, &val);
520 	if (err)
521 		return err;
522 
523 	if (val != 1)
524 		return -EINVAL;
525 
526 	mutex_lock(&os_up_lock);
527 	arm_smccc_smc(MLNX_HANDLE_OS_UP, 0, 0, 0, 0, 0, 0, 0, &res);
528 	mutex_unlock(&os_up_lock);
529 
530 	return count;
531 }
532 
oob_mac_show(struct device * dev,struct device_attribute * attr,char * buf)533 static ssize_t oob_mac_show(struct device *dev,
534 			    struct device_attribute *attr, char *buf)
535 {
536 	struct arm_smccc_res res;
537 	u8 *mac_byte_ptr;
538 
539 	mutex_lock(&mfg_ops_lock);
540 	arm_smccc_smc(MLXBF_BOOTCTL_GET_MFG_INFO, MLNX_MFG_TYPE_OOB_MAC, 0, 0, 0,
541 		      0, 0, 0, &res);
542 	mutex_unlock(&mfg_ops_lock);
543 	if (res.a0)
544 		return -EPERM;
545 
546 	mac_byte_ptr = (u8 *)&res.a1;
547 
548 	return sysfs_format_mac(buf, mac_byte_ptr, ETH_ALEN);
549 }
550 
oob_mac_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)551 static ssize_t oob_mac_store(struct device *dev,
552 			     struct device_attribute *attr,
553 			     const char *buf, size_t count)
554 {
555 	unsigned int byte[MLNX_MFG_OOB_MAC_FORMAT_LEN] = { 0 };
556 	struct arm_smccc_res res;
557 	int byte_idx, len;
558 	u64 mac_addr = 0;
559 	u8 *mac_byte_ptr;
560 
561 	if ((count - 1) != MLNX_MFG_OOB_MAC_FORMAT_LEN)
562 		return -EINVAL;
563 
564 	len = sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x",
565 		     &byte[0], &byte[1], &byte[2],
566 		     &byte[3], &byte[4], &byte[5]);
567 	if (len != ETH_ALEN)
568 		return -EINVAL;
569 
570 	mac_byte_ptr = (u8 *)&mac_addr;
571 
572 	for (byte_idx = 0; byte_idx < ETH_ALEN; byte_idx++)
573 		mac_byte_ptr[byte_idx] = (u8)byte[byte_idx];
574 
575 	mutex_lock(&mfg_ops_lock);
576 	arm_smccc_smc(MLXBF_BOOTCTL_SET_MFG_INFO, MLNX_MFG_TYPE_OOB_MAC,
577 		      ETH_ALEN, mac_addr, 0, 0, 0, 0, &res);
578 	mutex_unlock(&mfg_ops_lock);
579 
580 	return res.a0 ? -EPERM : count;
581 }
582 
opn_show(struct device * dev,struct device_attribute * attr,char * buf)583 static ssize_t opn_show(struct device *dev,
584 			struct device_attribute *attr, char *buf)
585 {
586 	u64 opn_data[MLNX_MFG_VAL_QWORD_CNT(OPN) + 1] = { 0 };
587 	struct arm_smccc_res res;
588 	int word;
589 
590 	mutex_lock(&mfg_ops_lock);
591 	for (word = 0; word < MLNX_MFG_VAL_QWORD_CNT(OPN); word++) {
592 		arm_smccc_smc(MLXBF_BOOTCTL_GET_MFG_INFO,
593 			      MLNX_MFG_TYPE_OPN_0 + word,
594 			      0, 0, 0, 0, 0, 0, &res);
595 		if (res.a0) {
596 			mutex_unlock(&mfg_ops_lock);
597 			return -EPERM;
598 		}
599 		opn_data[word] = res.a1;
600 	}
601 	mutex_unlock(&mfg_ops_lock);
602 
603 	return sysfs_emit(buf, "%s", (char *)opn_data);
604 }
605 
opn_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)606 static ssize_t opn_store(struct device *dev,
607 			 struct device_attribute *attr,
608 			 const char *buf, size_t count)
609 {
610 	u64 opn[MLNX_MFG_VAL_QWORD_CNT(OPN)] = { 0 };
611 	struct arm_smccc_res res;
612 	int word;
613 
614 	if (count > MLNX_MFG_OPN_VAL_LEN)
615 		return -EINVAL;
616 
617 	memcpy(opn, buf, count);
618 
619 	mutex_lock(&mfg_ops_lock);
620 	for (word = 0; word < MLNX_MFG_VAL_QWORD_CNT(OPN); word++) {
621 		arm_smccc_smc(MLXBF_BOOTCTL_SET_MFG_INFO,
622 			      MLNX_MFG_TYPE_OPN_0 + word,
623 			      sizeof(u64), opn[word], 0, 0, 0, 0, &res);
624 		if (res.a0) {
625 			mutex_unlock(&mfg_ops_lock);
626 			return -EPERM;
627 		}
628 	}
629 	mutex_unlock(&mfg_ops_lock);
630 
631 	return count;
632 }
633 
sku_show(struct device * dev,struct device_attribute * attr,char * buf)634 static ssize_t sku_show(struct device *dev,
635 			struct device_attribute *attr, char *buf)
636 {
637 	u64 sku_data[MLNX_MFG_VAL_QWORD_CNT(SKU) + 1] = { 0 };
638 	struct arm_smccc_res res;
639 	int word;
640 
641 	mutex_lock(&mfg_ops_lock);
642 	for (word = 0; word < MLNX_MFG_VAL_QWORD_CNT(SKU); word++) {
643 		arm_smccc_smc(MLXBF_BOOTCTL_GET_MFG_INFO,
644 			      MLNX_MFG_TYPE_SKU_0 + word,
645 			      0, 0, 0, 0, 0, 0, &res);
646 		if (res.a0) {
647 			mutex_unlock(&mfg_ops_lock);
648 			return -EPERM;
649 		}
650 		sku_data[word] = res.a1;
651 	}
652 	mutex_unlock(&mfg_ops_lock);
653 
654 	return sysfs_emit(buf, "%s", (char *)sku_data);
655 }
656 
sku_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)657 static ssize_t sku_store(struct device *dev,
658 			 struct device_attribute *attr,
659 			 const char *buf, size_t count)
660 {
661 	u64 sku[MLNX_MFG_VAL_QWORD_CNT(SKU)] = { 0 };
662 	struct arm_smccc_res res;
663 	int word;
664 
665 	if (count > MLNX_MFG_SKU_VAL_LEN)
666 		return -EINVAL;
667 
668 	memcpy(sku, buf, count);
669 
670 	mutex_lock(&mfg_ops_lock);
671 	for (word = 0; word < MLNX_MFG_VAL_QWORD_CNT(SKU); word++) {
672 		arm_smccc_smc(MLXBF_BOOTCTL_SET_MFG_INFO,
673 			      MLNX_MFG_TYPE_SKU_0 + word,
674 			      sizeof(u64), sku[word], 0, 0, 0, 0, &res);
675 		if (res.a0) {
676 			mutex_unlock(&mfg_ops_lock);
677 			return -EPERM;
678 		}
679 	}
680 	mutex_unlock(&mfg_ops_lock);
681 
682 	return count;
683 }
684 
modl_show(struct device * dev,struct device_attribute * attr,char * buf)685 static ssize_t modl_show(struct device *dev,
686 			 struct device_attribute *attr, char *buf)
687 {
688 	u64 modl_data[MLNX_MFG_VAL_QWORD_CNT(MODL) + 1] = { 0 };
689 	struct arm_smccc_res res;
690 	int word;
691 
692 	mutex_lock(&mfg_ops_lock);
693 	for (word = 0; word < MLNX_MFG_VAL_QWORD_CNT(MODL); word++) {
694 		arm_smccc_smc(MLXBF_BOOTCTL_GET_MFG_INFO,
695 			      MLNX_MFG_TYPE_MODL_0 + word,
696 			      0, 0, 0, 0, 0, 0, &res);
697 		if (res.a0) {
698 			mutex_unlock(&mfg_ops_lock);
699 			return -EPERM;
700 		}
701 		modl_data[word] = res.a1;
702 	}
703 	mutex_unlock(&mfg_ops_lock);
704 
705 	return sysfs_emit(buf, "%s", (char *)modl_data);
706 }
707 
modl_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)708 static ssize_t modl_store(struct device *dev,
709 			  struct device_attribute *attr,
710 			  const char *buf, size_t count)
711 {
712 	u64 modl[MLNX_MFG_VAL_QWORD_CNT(MODL)] = { 0 };
713 	struct arm_smccc_res res;
714 	int word;
715 
716 	if (count > MLNX_MFG_MODL_VAL_LEN)
717 		return -EINVAL;
718 
719 	memcpy(modl, buf, count);
720 
721 	mutex_lock(&mfg_ops_lock);
722 	for (word = 0; word < MLNX_MFG_VAL_QWORD_CNT(MODL); word++) {
723 		arm_smccc_smc(MLXBF_BOOTCTL_SET_MFG_INFO,
724 			      MLNX_MFG_TYPE_MODL_0 + word,
725 			      sizeof(u64), modl[word], 0, 0, 0, 0, &res);
726 		if (res.a0) {
727 			mutex_unlock(&mfg_ops_lock);
728 			return -EPERM;
729 		}
730 	}
731 	mutex_unlock(&mfg_ops_lock);
732 
733 	return count;
734 }
735 
sn_show(struct device * dev,struct device_attribute * attr,char * buf)736 static ssize_t sn_show(struct device *dev,
737 		       struct device_attribute *attr, char *buf)
738 {
739 	u64 sn_data[MLNX_MFG_VAL_QWORD_CNT(SN) + 1] = { 0 };
740 	struct arm_smccc_res res;
741 	int word;
742 
743 	mutex_lock(&mfg_ops_lock);
744 	for (word = 0; word < MLNX_MFG_VAL_QWORD_CNT(SN); word++) {
745 		arm_smccc_smc(MLXBF_BOOTCTL_GET_MFG_INFO,
746 			      MLNX_MFG_TYPE_SN_0 + word,
747 			      0, 0, 0, 0, 0, 0, &res);
748 		if (res.a0) {
749 			mutex_unlock(&mfg_ops_lock);
750 			return -EPERM;
751 		}
752 		sn_data[word] = res.a1;
753 	}
754 	mutex_unlock(&mfg_ops_lock);
755 
756 	return sysfs_emit(buf, "%s", (char *)sn_data);
757 }
758 
sn_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)759 static ssize_t sn_store(struct device *dev,
760 			struct device_attribute *attr,
761 			const char *buf, size_t count)
762 {
763 	u64 sn[MLNX_MFG_VAL_QWORD_CNT(SN)] = { 0 };
764 	struct arm_smccc_res res;
765 	int word;
766 
767 	if (count > MLNX_MFG_SN_VAL_LEN)
768 		return -EINVAL;
769 
770 	memcpy(sn, buf, count);
771 
772 	mutex_lock(&mfg_ops_lock);
773 	for (word = 0; word < MLNX_MFG_VAL_QWORD_CNT(SN); word++) {
774 		arm_smccc_smc(MLXBF_BOOTCTL_SET_MFG_INFO,
775 			      MLNX_MFG_TYPE_SN_0 + word,
776 			      sizeof(u64), sn[word], 0, 0, 0, 0, &res);
777 		if (res.a0) {
778 			mutex_unlock(&mfg_ops_lock);
779 			return -EPERM;
780 		}
781 	}
782 	mutex_unlock(&mfg_ops_lock);
783 
784 	return count;
785 }
786 
uuid_show(struct device * dev,struct device_attribute * attr,char * buf)787 static ssize_t uuid_show(struct device *dev,
788 			 struct device_attribute *attr, char *buf)
789 {
790 	u64 uuid_data[MLNX_MFG_VAL_QWORD_CNT(UUID) + 1] = { 0 };
791 	struct arm_smccc_res res;
792 	int word;
793 
794 	mutex_lock(&mfg_ops_lock);
795 	for (word = 0; word < MLNX_MFG_VAL_QWORD_CNT(UUID); word++) {
796 		arm_smccc_smc(MLXBF_BOOTCTL_GET_MFG_INFO,
797 			      MLNX_MFG_TYPE_UUID_0 + word,
798 			      0, 0, 0, 0, 0, 0, &res);
799 		if (res.a0) {
800 			mutex_unlock(&mfg_ops_lock);
801 			return -EPERM;
802 		}
803 		uuid_data[word] = res.a1;
804 	}
805 	mutex_unlock(&mfg_ops_lock);
806 
807 	return sysfs_emit(buf, "%s", (char *)uuid_data);
808 }
809 
uuid_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)810 static ssize_t uuid_store(struct device *dev,
811 			  struct device_attribute *attr,
812 			  const char *buf, size_t count)
813 {
814 	u64 uuid[MLNX_MFG_VAL_QWORD_CNT(UUID)] = { 0 };
815 	struct arm_smccc_res res;
816 	int word;
817 
818 	if (count > MLNX_MFG_UUID_VAL_LEN)
819 		return -EINVAL;
820 
821 	memcpy(uuid, buf, count);
822 
823 	mutex_lock(&mfg_ops_lock);
824 	for (word = 0; word < MLNX_MFG_VAL_QWORD_CNT(UUID); word++) {
825 		arm_smccc_smc(MLXBF_BOOTCTL_SET_MFG_INFO,
826 			      MLNX_MFG_TYPE_UUID_0 + word,
827 			      sizeof(u64), uuid[word], 0, 0, 0, 0, &res);
828 		if (res.a0) {
829 			mutex_unlock(&mfg_ops_lock);
830 			return -EPERM;
831 		}
832 	}
833 	mutex_unlock(&mfg_ops_lock);
834 
835 	return count;
836 }
837 
rev_show(struct device * dev,struct device_attribute * attr,char * buf)838 static ssize_t rev_show(struct device *dev,
839 			struct device_attribute *attr, char *buf)
840 {
841 	u64 rev_data[MLNX_MFG_VAL_QWORD_CNT(REV) + 1] = { 0 };
842 	struct arm_smccc_res res;
843 	int word;
844 
845 	mutex_lock(&mfg_ops_lock);
846 	for (word = 0; word < MLNX_MFG_VAL_QWORD_CNT(REV); word++) {
847 		arm_smccc_smc(MLXBF_BOOTCTL_GET_MFG_INFO,
848 			      MLNX_MFG_TYPE_REV + word,
849 			      0, 0, 0, 0, 0, 0, &res);
850 		if (res.a0) {
851 			mutex_unlock(&mfg_ops_lock);
852 			return -EPERM;
853 		}
854 		rev_data[word] = res.a1;
855 	}
856 	mutex_unlock(&mfg_ops_lock);
857 
858 	return sysfs_emit(buf, "%s", (char *)rev_data);
859 }
860 
rev_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)861 static ssize_t rev_store(struct device *dev,
862 			 struct device_attribute *attr,
863 			 const char *buf, size_t count)
864 {
865 	u64 rev[MLNX_MFG_VAL_QWORD_CNT(REV)] = { 0 };
866 	struct arm_smccc_res res;
867 	int word;
868 
869 	if (count > MLNX_MFG_REV_VAL_LEN)
870 		return -EINVAL;
871 
872 	memcpy(rev, buf, count);
873 
874 	mutex_lock(&mfg_ops_lock);
875 	for (word = 0; word < MLNX_MFG_VAL_QWORD_CNT(REV); word++) {
876 		arm_smccc_smc(MLXBF_BOOTCTL_SET_MFG_INFO,
877 			      MLNX_MFG_TYPE_REV + word,
878 			      sizeof(u64), rev[word], 0, 0, 0, 0, &res);
879 		if (res.a0) {
880 			mutex_unlock(&mfg_ops_lock);
881 			return -EPERM;
882 		}
883 	}
884 	mutex_unlock(&mfg_ops_lock);
885 
886 	return count;
887 }
888 
mfg_lock_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)889 static ssize_t mfg_lock_store(struct device *dev,
890 			      struct device_attribute *attr,
891 			      const char *buf, size_t count)
892 {
893 	struct arm_smccc_res res;
894 	unsigned long val;
895 	int err;
896 
897 	err = kstrtoul(buf, 10, &val);
898 	if (err)
899 		return err;
900 
901 	if (val != 1)
902 		return -EINVAL;
903 
904 	mutex_lock(&mfg_ops_lock);
905 	arm_smccc_smc(MLXBF_BOOTCTL_LOCK_MFG_INFO, 0, 0, 0, 0, 0, 0, 0, &res);
906 	mutex_unlock(&mfg_ops_lock);
907 
908 	return count;
909 }
910 
911 static DEVICE_ATTR_RW(post_reset_wdog);
912 static DEVICE_ATTR_RW(reset_action);
913 static DEVICE_ATTR_RW(second_reset_action);
914 static DEVICE_ATTR_RO(lifecycle_state);
915 static DEVICE_ATTR_RO(secure_boot_fuse_state);
916 static DEVICE_ATTR_WO(fw_reset);
917 static DEVICE_ATTR_WO(rsh_log);
918 static DEVICE_ATTR_RW(large_icm);
919 static DEVICE_ATTR_WO(os_up);
920 static DEVICE_ATTR_RW(oob_mac);
921 static DEVICE_ATTR_RW(opn);
922 static DEVICE_ATTR_RW(sku);
923 static DEVICE_ATTR_RW(modl);
924 static DEVICE_ATTR_RW(sn);
925 static DEVICE_ATTR_RW(uuid);
926 static DEVICE_ATTR_RW(rev);
927 static DEVICE_ATTR_WO(mfg_lock);
928 static DEVICE_ATTR_RO(rtc_battery);
929 
930 static struct attribute *mlxbf_bootctl_attrs[] = {
931 	&dev_attr_post_reset_wdog.attr,
932 	&dev_attr_reset_action.attr,
933 	&dev_attr_second_reset_action.attr,
934 	&dev_attr_lifecycle_state.attr,
935 	&dev_attr_secure_boot_fuse_state.attr,
936 	&dev_attr_fw_reset.attr,
937 	&dev_attr_rsh_log.attr,
938 	&dev_attr_large_icm.attr,
939 	&dev_attr_os_up.attr,
940 	&dev_attr_oob_mac.attr,
941 	&dev_attr_opn.attr,
942 	&dev_attr_sku.attr,
943 	&dev_attr_modl.attr,
944 	&dev_attr_sn.attr,
945 	&dev_attr_uuid.attr,
946 	&dev_attr_rev.attr,
947 	&dev_attr_mfg_lock.attr,
948 	&dev_attr_rtc_battery.attr,
949 	NULL
950 };
951 
952 ATTRIBUTE_GROUPS(mlxbf_bootctl);
953 
954 static const struct acpi_device_id mlxbf_bootctl_acpi_ids[] = {
955 	{"MLNXBF04", 0},
956 	{}
957 };
958 
959 MODULE_DEVICE_TABLE(acpi, mlxbf_bootctl_acpi_ids);
960 
mlxbf_bootctl_bootfifo_read(struct file * filp,struct kobject * kobj,const struct bin_attribute * bin_attr,char * buf,loff_t pos,size_t count)961 static ssize_t mlxbf_bootctl_bootfifo_read(struct file *filp,
962 					   struct kobject *kobj,
963 					   const struct bin_attribute *bin_attr,
964 					   char *buf, loff_t pos,
965 					   size_t count)
966 {
967 	unsigned long timeout = msecs_to_jiffies(500);
968 	unsigned long expire = jiffies + timeout;
969 	u64 data, cnt = 0;
970 	char *p = buf;
971 
972 	while (count >= sizeof(data)) {
973 		/* Give up reading if no more data within 500ms. */
974 		if (!cnt) {
975 			cnt = readq(mlxbf_rsh_boot_cnt);
976 			if (!cnt) {
977 				if (time_after(jiffies, expire))
978 					break;
979 				usleep_range(10, 50);
980 				continue;
981 			}
982 		}
983 
984 		data = readq(mlxbf_rsh_boot_data);
985 		memcpy(p, &data, sizeof(data));
986 		count -= sizeof(data);
987 		p += sizeof(data);
988 		cnt--;
989 		expire = jiffies + timeout;
990 	}
991 
992 	return p - buf;
993 }
994 
995 static const struct bin_attribute mlxbf_bootctl_bootfifo_sysfs_attr = {
996 	.attr = { .name = "bootfifo", .mode = 0400 },
997 	.read = mlxbf_bootctl_bootfifo_read,
998 };
999 
mlxbf_bootctl_guid_match(const guid_t * guid,const struct arm_smccc_res * res)1000 static bool mlxbf_bootctl_guid_match(const guid_t *guid,
1001 				     const struct arm_smccc_res *res)
1002 {
1003 	guid_t id = GUID_INIT(res->a0, res->a1, res->a1 >> 16,
1004 			      res->a2, res->a2 >> 8, res->a2 >> 16,
1005 			      res->a2 >> 24, res->a3, res->a3 >> 8,
1006 			      res->a3 >> 16, res->a3 >> 24);
1007 
1008 	return guid_equal(guid, &id);
1009 }
1010 
mlxbf_bootctl_probe(struct platform_device * pdev)1011 static int mlxbf_bootctl_probe(struct platform_device *pdev)
1012 {
1013 	struct arm_smccc_res res = { 0 };
1014 	void __iomem *reg;
1015 	guid_t guid;
1016 	int ret;
1017 
1018 	/* Map the resource of the bootfifo data register. */
1019 	mlxbf_rsh_boot_data = devm_platform_ioremap_resource(pdev, 0);
1020 	if (IS_ERR(mlxbf_rsh_boot_data))
1021 		return PTR_ERR(mlxbf_rsh_boot_data);
1022 
1023 	/* Map the resource of the bootfifo counter register. */
1024 	mlxbf_rsh_boot_cnt = devm_platform_ioremap_resource(pdev, 1);
1025 	if (IS_ERR(mlxbf_rsh_boot_cnt))
1026 		return PTR_ERR(mlxbf_rsh_boot_cnt);
1027 
1028 	/* Map the resource of the rshim semaphore register. */
1029 	mlxbf_rsh_semaphore = devm_platform_ioremap_resource(pdev, 2);
1030 	if (IS_ERR(mlxbf_rsh_semaphore))
1031 		return PTR_ERR(mlxbf_rsh_semaphore);
1032 
1033 	/* Map the resource of the scratch buffer (log) registers. */
1034 	reg = devm_platform_ioremap_resource(pdev, 3);
1035 	if (IS_ERR(reg))
1036 		return PTR_ERR(reg);
1037 	mlxbf_rsh_scratch_buf_ctl = reg + MLXBF_RSH_SCRATCH_BUF_CTL_OFF;
1038 	mlxbf_rsh_scratch_buf_data = reg + MLXBF_RSH_SCRATCH_BUF_DATA_OFF;
1039 
1040 	/* Ensure we have the UUID we expect for this service. */
1041 	arm_smccc_smc(MLXBF_BOOTCTL_SIP_SVC_UID, 0, 0, 0, 0, 0, 0, 0, &res);
1042 	guid_parse(mlxbf_bootctl_svc_uuid_str, &guid);
1043 	if (!mlxbf_bootctl_guid_match(&guid, &res))
1044 		return -ENODEV;
1045 
1046 	/*
1047 	 * When watchdog is used, it sets boot mode to MLXBF_BOOTCTL_SWAP_EMMC
1048 	 * in case of boot failures. However it doesn't clear the state if there
1049 	 * is no failure. Restore the default boot mode here to avoid any
1050 	 * unnecessary boot partition swapping.
1051 	 */
1052 	ret = mlxbf_bootctl_smc(MLXBF_BOOTCTL_SET_RESET_ACTION,
1053 				MLXBF_BOOTCTL_EMMC);
1054 	if (ret < 0)
1055 		dev_warn(&pdev->dev, "Unable to reset the EMMC boot mode\n");
1056 
1057 	ret = sysfs_create_bin_file(&pdev->dev.kobj,
1058 				    &mlxbf_bootctl_bootfifo_sysfs_attr);
1059 	if (ret)
1060 		pr_err("Unable to create bootfifo sysfs file, error %d\n", ret);
1061 
1062 	return ret;
1063 }
1064 
mlxbf_bootctl_remove(struct platform_device * pdev)1065 static void mlxbf_bootctl_remove(struct platform_device *pdev)
1066 {
1067 	sysfs_remove_bin_file(&pdev->dev.kobj,
1068 			      &mlxbf_bootctl_bootfifo_sysfs_attr);
1069 }
1070 
1071 static struct platform_driver mlxbf_bootctl_driver = {
1072 	.probe = mlxbf_bootctl_probe,
1073 	.remove = mlxbf_bootctl_remove,
1074 	.driver = {
1075 		.name = "mlxbf-bootctl",
1076 		.dev_groups = mlxbf_bootctl_groups,
1077 		.acpi_match_table = mlxbf_bootctl_acpi_ids,
1078 	}
1079 };
1080 
1081 module_platform_driver(mlxbf_bootctl_driver);
1082 
1083 MODULE_DESCRIPTION("Mellanox boot control driver");
1084 MODULE_LICENSE("GPL v2");
1085 MODULE_AUTHOR("Mellanox Technologies");
1086