Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Turris Mox rWTM firmware driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2019 Marek Behun <marek.behun@nic.cz>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/armada-37xx-rwtm-mailbox.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/completion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/dma-mapping.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/hw_random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/mailbox_client.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define DRIVER_NAME		"turris-mox-rwtm"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * The macros and constants below come from Turris Mox's rWTM firmware code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * This firmware is open source and it's sources can be found at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * https://gitlab.labs.nic.cz/turris/mox-boot-builder/tree/master/wtmi.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define MBOX_STS_SUCCESS	(0 << 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define MBOX_STS_FAIL		(1 << 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define MBOX_STS_BADCMD		(2 << 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define MBOX_STS_ERROR(s)	((s) & (3 << 30))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define MBOX_STS_VALUE(s)	(((s) >> 10) & 0xfffff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define MBOX_STS_CMD(s)		((s) & 0x3ff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) enum mbox_cmd {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	MBOX_CMD_GET_RANDOM	= 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	MBOX_CMD_BOARD_INFO	= 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	MBOX_CMD_ECDSA_PUB_KEY	= 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	MBOX_CMD_HASH		= 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	MBOX_CMD_SIGN		= 5,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	MBOX_CMD_VERIFY		= 6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	MBOX_CMD_OTP_READ	= 7,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	MBOX_CMD_OTP_WRITE	= 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) struct mox_kobject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) struct mox_rwtm {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct mbox_client mbox_client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	struct mbox_chan *mbox;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	struct mox_kobject *kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct hwrng hwrng;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	struct armada_37xx_rwtm_rx_msg reply;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	dma_addr_t buf_phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	struct mutex busy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	struct completion cmd_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	/* board information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	int has_board_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	u64 serial_number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	int board_version, ram_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	u8 mac_address1[6], mac_address2[6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	/* public key burned in eFuse */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	int has_pubkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	u8 pubkey[135];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) #ifdef CONFIG_DEBUG_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	 * Signature process. This is currently done via debugfs, because it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	 * does not conform to the sysfs standard "one file per attribute".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	 * It should be rewritten via crypto API once akcipher API is available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	 * from userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	struct dentry *debugfs_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	u32 last_sig[34];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	int last_sig_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) struct mox_kobject {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	struct kobject kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	struct mox_rwtm *rwtm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static inline struct kobject *rwtm_to_kobj(struct mox_rwtm *rwtm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	return &rwtm->kobj->kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) static inline struct mox_rwtm *to_rwtm(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	return container_of(kobj, struct mox_kobject, kobj)->rwtm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static void mox_kobj_release(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	kfree(to_rwtm(kobj)->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static struct kobj_type mox_kobj_ktype = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	.release	= mox_kobj_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	.sysfs_ops	= &kobj_sysfs_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static int mox_kobj_create(struct mox_rwtm *rwtm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	rwtm->kobj = kzalloc(sizeof(*rwtm->kobj), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (!rwtm->kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	kobject_init(rwtm_to_kobj(rwtm), &mox_kobj_ktype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	if (kobject_add(rwtm_to_kobj(rwtm), firmware_kobj, "turris-mox-rwtm")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		kobject_put(rwtm_to_kobj(rwtm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	rwtm->kobj->rwtm = rwtm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) #define MOX_ATTR_RO(name, format, cat)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static ssize_t							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) name##_show(struct kobject *kobj, struct kobj_attribute *a,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	    char *buf)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	struct mox_rwtm *rwtm = to_rwtm(kobj);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (!rwtm->has_##cat)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		return -ENODATA;				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	return sprintf(buf, format, rwtm->name);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static struct kobj_attribute mox_attr_##name = __ATTR_RO(name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) MOX_ATTR_RO(serial_number, "%016llX\n", board_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) MOX_ATTR_RO(board_version, "%i\n", board_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) MOX_ATTR_RO(ram_size, "%i\n", board_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) MOX_ATTR_RO(mac_address1, "%pM\n", board_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) MOX_ATTR_RO(mac_address2, "%pM\n", board_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) MOX_ATTR_RO(pubkey, "%s\n", pubkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static int mox_get_status(enum mbox_cmd cmd, u32 retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	if (MBOX_STS_CMD(retval) != cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	else if (MBOX_STS_ERROR(retval) == MBOX_STS_FAIL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		return -(int)MBOX_STS_VALUE(retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	else if (MBOX_STS_ERROR(retval) == MBOX_STS_BADCMD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	else if (MBOX_STS_ERROR(retval) != MBOX_STS_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		return MBOX_STS_VALUE(retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static const struct attribute *mox_rwtm_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	&mox_attr_serial_number.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	&mox_attr_board_version.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	&mox_attr_ram_size.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	&mox_attr_mac_address1.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	&mox_attr_mac_address2.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	&mox_attr_pubkey.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static void mox_rwtm_rx_callback(struct mbox_client *cl, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	struct mox_rwtm *rwtm = dev_get_drvdata(cl->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	struct armada_37xx_rwtm_rx_msg *msg = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	rwtm->reply = *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	complete(&rwtm->cmd_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static void reply_to_mac_addr(u8 *mac, u32 t1, u32 t2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	mac[0] = t1 >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	mac[1] = t1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	mac[2] = t2 >> 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	mac[3] = t2 >> 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	mac[4] = t2 >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	mac[5] = t2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static int mox_get_board_info(struct mox_rwtm *rwtm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	struct armada_37xx_rwtm_tx_msg msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	struct armada_37xx_rwtm_rx_msg *reply = &rwtm->reply;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	msg.command = MBOX_CMD_BOARD_INFO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	ret = mbox_send_message(rwtm->mbox, &msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	ret = wait_for_completion_timeout(&rwtm->cmd_done, HZ / 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	ret = mox_get_status(MBOX_CMD_BOARD_INFO, reply->retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (ret == -ENODATA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		dev_warn(rwtm->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 			 "Board does not have manufacturing information burned!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	} else if (ret == -ENOSYS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		dev_notice(rwtm->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			   "Firmware does not support the BOARD_INFO command\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	} else if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		rwtm->serial_number = reply->status[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		rwtm->serial_number <<= 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		rwtm->serial_number |= reply->status[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		rwtm->board_version = reply->status[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		rwtm->ram_size = reply->status[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		reply_to_mac_addr(rwtm->mac_address1, reply->status[4],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 				  reply->status[5]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		reply_to_mac_addr(rwtm->mac_address2, reply->status[6],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 				  reply->status[7]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		rwtm->has_board_info = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		pr_info("Turris Mox serial number %016llX\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			rwtm->serial_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		pr_info("           board version %i\n", rwtm->board_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		pr_info("           burned RAM size %i MiB\n", rwtm->ram_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	msg.command = MBOX_CMD_ECDSA_PUB_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	ret = mbox_send_message(rwtm->mbox, &msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	ret = wait_for_completion_timeout(&rwtm->cmd_done, HZ / 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	ret = mox_get_status(MBOX_CMD_ECDSA_PUB_KEY, reply->retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (ret == -ENODATA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		dev_warn(rwtm->dev, "Board has no public key burned!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	} else if (ret == -ENOSYS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		dev_notice(rwtm->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			   "Firmware does not support the ECDSA_PUB_KEY command\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	} else if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		u32 *s = reply->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		rwtm->has_pubkey = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		sprintf(rwtm->pubkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			"%06x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			ret, s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			s[8], s[9], s[10], s[11], s[12], s[13], s[14], s[15]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) static int check_get_random_support(struct mox_rwtm *rwtm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	struct armada_37xx_rwtm_tx_msg msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	msg.command = MBOX_CMD_GET_RANDOM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	msg.args[0] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	msg.args[1] = rwtm->buf_phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	msg.args[2] = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	ret = mbox_send_message(rwtm->mbox, &msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	ret = wait_for_completion_timeout(&rwtm->cmd_done, HZ / 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	return mox_get_status(MBOX_CMD_GET_RANDOM, rwtm->reply.retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) static int mox_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	struct mox_rwtm *rwtm = (struct mox_rwtm *) rng->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	struct armada_37xx_rwtm_tx_msg msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	if (max > 4096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		max = 4096;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	msg.command = MBOX_CMD_GET_RANDOM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	msg.args[0] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	msg.args[1] = rwtm->buf_phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	msg.args[2] = (max + 3) & ~3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	if (!wait) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		if (!mutex_trylock(&rwtm->busy))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 			return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		mutex_lock(&rwtm->busy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	ret = mbox_send_message(rwtm->mbox, &msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		goto unlock_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	ret = wait_for_completion_interruptible(&rwtm->cmd_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		goto unlock_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	ret = mox_get_status(MBOX_CMD_GET_RANDOM, rwtm->reply.retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		goto unlock_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	memcpy(data, rwtm->buf, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	ret = max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) unlock_mutex:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	mutex_unlock(&rwtm->busy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) #ifdef CONFIG_DEBUG_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static int rwtm_debug_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	file->private_data = inode->i_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	return nonseekable_open(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) static ssize_t do_sign_read(struct file *file, char __user *buf, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			    loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	struct mox_rwtm *rwtm = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	/* only allow one read, of 136 bytes, from position 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	if (*ppos != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	if (len < 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	if (!rwtm->last_sig_done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	/* 2 arrays of 17 32-bit words are 136 bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	ret = simple_read_from_buffer(buf, len, ppos, rwtm->last_sig, 136);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	rwtm->last_sig_done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) static ssize_t do_sign_write(struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 			     size_t len, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	struct mox_rwtm *rwtm = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	struct armada_37xx_rwtm_rx_msg *reply = &rwtm->reply;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	struct armada_37xx_rwtm_tx_msg msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	loff_t dummy = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	/* the input is a SHA-512 hash, so exactly 64 bytes have to be read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	if (len != 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	/* if last result is not zero user has not read that information yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	if (rwtm->last_sig_done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	if (!mutex_trylock(&rwtm->busy))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	 * Here we have to send:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	 *   1. Address of the input to sign.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	 *      The input is an array of 17 32-bit words, the first (most
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	 *      significat) is 0, the rest 16 words are copied from the SHA-512
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	 *      hash given by the user and converted from BE to LE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	 *   2. Address of the buffer where ECDSA signature value R shall be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	 *      stored by the rWTM firmware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	 *   3. Address of the buffer where ECDSA signature value S shall be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	 *      stored by the rWTM firmware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	memset(rwtm->buf, 0, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	ret = simple_write_to_buffer(rwtm->buf + 4, 64, &dummy, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		goto unlock_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	be32_to_cpu_array(rwtm->buf, rwtm->buf, 17);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	msg.command = MBOX_CMD_SIGN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	msg.args[0] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	msg.args[1] = rwtm->buf_phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	msg.args[2] = rwtm->buf_phys + 68;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	msg.args[3] = rwtm->buf_phys + 2 * 68;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	ret = mbox_send_message(rwtm->mbox, &msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		goto unlock_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	ret = wait_for_completion_interruptible(&rwtm->cmd_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		goto unlock_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	ret = MBOX_STS_VALUE(reply->retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	if (MBOX_STS_ERROR(reply->retval) != MBOX_STS_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		goto unlock_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	 * Here we read the R and S values of the ECDSA signature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	 * computed by the rWTM firmware and convert their words from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	 * LE to BE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	memcpy(rwtm->last_sig, rwtm->buf + 68, 136);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	cpu_to_be32_array(rwtm->last_sig, rwtm->last_sig, 34);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	rwtm->last_sig_done = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	mutex_unlock(&rwtm->busy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) unlock_mutex:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	mutex_unlock(&rwtm->busy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) static const struct file_operations do_sign_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	.owner	= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	.open	= rwtm_debug_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	.read	= do_sign_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	.write	= do_sign_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	.llseek	= no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) static int rwtm_register_debugfs(struct mox_rwtm *rwtm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	struct dentry *root, *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	root = debugfs_create_dir("turris-mox-rwtm", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	if (IS_ERR(root))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		return PTR_ERR(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	entry = debugfs_create_file_unsafe("do_sign", 0600, root, rwtm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 					   &do_sign_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	if (IS_ERR(entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		goto err_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	rwtm->debugfs_root = root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) err_remove:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	debugfs_remove_recursive(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	return PTR_ERR(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) static void rwtm_unregister_debugfs(struct mox_rwtm *rwtm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	debugfs_remove_recursive(rwtm->debugfs_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) static inline int rwtm_register_debugfs(struct mox_rwtm *rwtm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) static inline void rwtm_unregister_debugfs(struct mox_rwtm *rwtm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) static int turris_mox_rwtm_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	struct mox_rwtm *rwtm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	rwtm = devm_kzalloc(dev, sizeof(*rwtm), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	if (!rwtm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	rwtm->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	rwtm->buf = dmam_alloc_coherent(dev, PAGE_SIZE, &rwtm->buf_phys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 					GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	if (!rwtm->buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	ret = mox_kobj_create(rwtm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		dev_err(dev, "Cannot create turris-mox-rwtm kobject!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	ret = sysfs_create_files(rwtm_to_kobj(rwtm), mox_rwtm_attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		dev_err(dev, "Cannot create sysfs files!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		goto put_kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	platform_set_drvdata(pdev, rwtm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	mutex_init(&rwtm->busy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	rwtm->mbox_client.dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	rwtm->mbox_client.rx_callback = mox_rwtm_rx_callback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	rwtm->mbox = mbox_request_channel(&rwtm->mbox_client, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	if (IS_ERR(rwtm->mbox)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		ret = PTR_ERR(rwtm->mbox);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		if (ret != -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 			dev_err(dev, "Cannot request mailbox channel: %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 				ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		goto remove_files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	init_completion(&rwtm->cmd_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	ret = mox_get_board_info(rwtm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		dev_warn(dev, "Cannot read board information: %i\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	ret = check_get_random_support(rwtm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		dev_notice(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 			   "Firmware does not support the GET_RANDOM command\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		goto free_channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	rwtm->hwrng.name = DRIVER_NAME "_hwrng";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	rwtm->hwrng.read = mox_hwrng_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	rwtm->hwrng.priv = (unsigned long) rwtm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	rwtm->hwrng.quality = 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	ret = devm_hwrng_register(dev, &rwtm->hwrng);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 		dev_err(dev, "Cannot register HWRNG: %i\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		goto free_channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	ret = rwtm_register_debugfs(rwtm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 		dev_err(dev, "Failed creating debugfs entries: %i\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		goto free_channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	dev_info(dev, "HWRNG successfully registered\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) free_channel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	mbox_free_channel(rwtm->mbox);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) remove_files:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	sysfs_remove_files(rwtm_to_kobj(rwtm), mox_rwtm_attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) put_kobj:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	kobject_put(rwtm_to_kobj(rwtm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) static int turris_mox_rwtm_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	struct mox_rwtm *rwtm = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	rwtm_unregister_debugfs(rwtm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	sysfs_remove_files(rwtm_to_kobj(rwtm), mox_rwtm_attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	kobject_put(rwtm_to_kobj(rwtm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	mbox_free_channel(rwtm->mbox);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) static const struct of_device_id turris_mox_rwtm_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	{ .compatible = "cznic,turris-mox-rwtm", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	{ .compatible = "marvell,armada-3700-rwtm-firmware", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	{ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) MODULE_DEVICE_TABLE(of, turris_mox_rwtm_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) static struct platform_driver turris_mox_rwtm_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	.probe	= turris_mox_rwtm_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	.remove	= turris_mox_rwtm_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	.driver	= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 		.name		= DRIVER_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 		.of_match_table	= turris_mox_rwtm_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) module_platform_driver(turris_mox_rwtm_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) MODULE_DESCRIPTION("Turris Mox rWTM firmware driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) MODULE_AUTHOR("Marek Behun <marek.behun@nic.cz>");