1 // SPDX-License-Identifier: GPL-2.0+
2 
3 #define pr_fmt(fmt) "ipmi_hardcode: " fmt
4 
5 #include <linux/moduleparam.h>
6 #include <linux/platform_device.h>
7 #include "ipmi_si.h"
8 #include "ipmi_plat_data.h"
9 
10 /*
11  * There can be 4 IO ports passed in (with or without IRQs), 4 addresses,
12  * a default IO port, and 1 ACPI/SPMI address.  That sets SI_MAX_DRIVERS.
13  */
14 
15 #define SI_MAX_PARMS 4
16 
17 #define MAX_SI_TYPE_STR 30
18 static char          si_type_str[MAX_SI_TYPE_STR] __initdata;
19 static unsigned long addrs[SI_MAX_PARMS];
20 static unsigned int num_addrs;
21 static unsigned int  ports[SI_MAX_PARMS];
22 static unsigned int num_ports;
23 static int           irqs[SI_MAX_PARMS] __initdata;
24 static unsigned int num_irqs __initdata;
25 static int           regspacings[SI_MAX_PARMS] __initdata;
26 static unsigned int num_regspacings __initdata;
27 static int           regsizes[SI_MAX_PARMS] __initdata;
28 static unsigned int num_regsizes __initdata;
29 static int           regshifts[SI_MAX_PARMS] __initdata;
30 static unsigned int num_regshifts __initdata;
31 static int slave_addrs[SI_MAX_PARMS] __initdata;
32 static unsigned int num_slave_addrs __initdata;
33 
34 module_param_string(type, si_type_str, MAX_SI_TYPE_STR, 0);
35 MODULE_PARM_DESC(type, "Defines the type of each interface, each"
36 		 " interface separated by commas.  The types are 'kcs',"
37 		 " 'smic', and 'bt'.  For example si_type=kcs,bt will set"
38 		 " the first interface to kcs and the second to bt");
39 module_param_hw_array(addrs, ulong, iomem, &num_addrs, 0);
40 MODULE_PARM_DESC(addrs, "Sets the memory address of each interface, the"
41 		 " addresses separated by commas.  Only use if an interface"
42 		 " is in memory.  Otherwise, set it to zero or leave"
43 		 " it blank.");
44 module_param_hw_array(ports, uint, ioport, &num_ports, 0);
45 MODULE_PARM_DESC(ports, "Sets the port address of each interface, the"
46 		 " addresses separated by commas.  Only use if an interface"
47 		 " is a port.  Otherwise, set it to zero or leave"
48 		 " it blank.");
49 module_param_hw_array(irqs, int, irq, &num_irqs, 0);
50 MODULE_PARM_DESC(irqs, "Sets the interrupt of each interface, the"
51 		 " addresses separated by commas.  Only use if an interface"
52 		 " has an interrupt.  Otherwise, set it to zero or leave"
53 		 " it blank.");
54 module_param_hw_array(regspacings, int, other, &num_regspacings, 0);
55 MODULE_PARM_DESC(regspacings, "The number of bytes between the start address"
56 		 " and each successive register used by the interface.  For"
57 		 " instance, if the start address is 0xca2 and the spacing"
58 		 " is 2, then the second address is at 0xca4.  Defaults"
59 		 " to 1.");
60 module_param_hw_array(regsizes, int, other, &num_regsizes, 0);
61 MODULE_PARM_DESC(regsizes, "The size of the specific IPMI register in bytes."
62 		 " This should generally be 1, 2, 4, or 8 for an 8-bit,"
63 		 " 16-bit, 32-bit, or 64-bit register.  Use this if you"
64 		 " the 8-bit IPMI register has to be read from a larger"
65 		 " register.");
66 module_param_hw_array(regshifts, int, other, &num_regshifts, 0);
67 MODULE_PARM_DESC(regshifts, "The amount to shift the data read from the."
68 		 " IPMI register, in bits.  For instance, if the data"
69 		 " is read from a 32-bit word and the IPMI data is in"
70 		 " bit 8-15, then the shift would be 8");
71 module_param_hw_array(slave_addrs, int, other, &num_slave_addrs, 0);
72 MODULE_PARM_DESC(slave_addrs, "Set the default IPMB slave address for"
73 		 " the controller.  Normally this is 0x20, but can be"
74 		 " overridden by this parm.  This is an array indexed"
75 		 " by interface number.");
76 
77 static void __init ipmi_hardcode_init_one(const char *si_type_str,
78 					  unsigned int i,
79 					  unsigned long addr,
80 					  enum ipmi_addr_space addr_space)
81 {
82 	struct ipmi_plat_data p;
83 
84 	memset(&p, 0, sizeof(p));
85 
86 	p.iftype = IPMI_PLAT_IF_SI;
87 	if (!si_type_str || !*si_type_str || strcmp(si_type_str, "kcs") == 0) {
88 		p.type = SI_KCS;
89 	} else if (strcmp(si_type_str, "smic") == 0) {
90 		p.type = SI_SMIC;
91 	} else if (strcmp(si_type_str, "bt") == 0) {
92 		p.type = SI_BT;
93 	} else if (strcmp(si_type_str, "invalid") == 0) {
94 		/*
95 		 * Allow a firmware-specified interface to be
96 		 * disabled.
97 		 */
98 		p.type = SI_TYPE_INVALID;
99 	} else {
100 		pr_warn("Interface type specified for interface %d, was invalid: %s\n",
101 			i, si_type_str);
102 		return;
103 	}
104 
105 	p.regsize = regsizes[i];
106 	p.slave_addr = slave_addrs[i];
107 	p.addr_source = SI_HARDCODED;
108 	p.regshift = regshifts[i];
109 	p.regsize = regsizes[i];
110 	p.addr = addr;
111 	p.space = addr_space;
112 
113 	ipmi_platform_add("hardcode-ipmi-si", i, &p);
114 }
115 
116 void __init ipmi_hardcode_init(void)
117 {
118 	unsigned int i;
119 	char *str;
120 	char *si_type[SI_MAX_PARMS];
121 
122 	memset(si_type, 0, sizeof(si_type));
123 
124 	/* Parse out the si_type string into its components. */
125 	str = si_type_str;
126 	if (*str != '\0') {
127 		for (i = 0; (i < SI_MAX_PARMS) && (*str != '\0'); i++) {
128 			si_type[i] = str;
129 			str = strchr(str, ',');
130 			if (str) {
131 				*str = '\0';
132 				str++;
133 			} else {
134 				break;
135 			}
136 		}
137 	}
138 
139 	for (i = 0; i < SI_MAX_PARMS; i++) {
140 		if (i < num_ports && ports[i])
141 			ipmi_hardcode_init_one(si_type[i], i, ports[i],
142 					       IPMI_IO_ADDR_SPACE);
143 		if (i < num_addrs && addrs[i])
144 			ipmi_hardcode_init_one(si_type[i], i, addrs[i],
145 					       IPMI_MEM_ADDR_SPACE);
146 	}
147 }
148 
149 
150 void ipmi_si_hardcode_exit(void)
151 {
152 	ipmi_remove_platform_device_by_name("hardcode-ipmi-si");
153 }
154 
155 /*
156  * Returns true of the given address exists as a hardcoded address,
157  * false if not.
158  */
159 int ipmi_si_hardcode_match(int addr_space, unsigned long addr)
160 {
161 	unsigned int i;
162 
163 	if (addr_space == IPMI_IO_ADDR_SPACE) {
164 		for (i = 0; i < num_ports; i++) {
165 			if (ports[i] == addr)
166 				return 1;
167 		}
168 	} else {
169 		for (i = 0; i < num_addrs; i++) {
170 			if (addrs[i] == addr)
171 				return 1;
172 		}
173 	}
174 
175 	return 0;
176 }
177