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 	if (!si_type_str || !*si_type_str || strcmp(si_type_str, "kcs") == 0) {
87 		p.type = SI_KCS;
88 	} else if (strcmp(si_type_str, "smic") == 0) {
89 		p.type = SI_SMIC;
90 	} else if (strcmp(si_type_str, "bt") == 0) {
91 		p.type = SI_BT;
92 	} else if (strcmp(si_type_str, "invalid") == 0) {
93 		/*
94 		 * Allow a firmware-specified interface to be
95 		 * disabled.
96 		 */
97 		p.type = SI_TYPE_INVALID;
98 	} else {
99 		pr_warn("Interface type specified for interface %d, was invalid: %s\n",
100 			i, si_type_str);
101 		return;
102 	}
103 
104 	p.regsize = regsizes[i];
105 	p.slave_addr = slave_addrs[i];
106 	p.addr_source = SI_HARDCODED;
107 	p.regshift = regshifts[i];
108 	p.regsize = regsizes[i];
109 	p.addr = addr;
110 	p.space = addr_space;
111 
112 	ipmi_platform_add("hardcode-ipmi-si", i, &p);
113 }
114 
115 void __init ipmi_hardcode_init(void)
116 {
117 	unsigned int i;
118 	char *str;
119 	char *si_type[SI_MAX_PARMS];
120 
121 	/* Parse out the si_type string into its components. */
122 	str = si_type_str;
123 	if (*str != '\0') {
124 		for (i = 0; (i < SI_MAX_PARMS) && (*str != '\0'); i++) {
125 			si_type[i] = str;
126 			str = strchr(str, ',');
127 			if (str) {
128 				*str = '\0';
129 				str++;
130 			} else {
131 				break;
132 			}
133 		}
134 	}
135 
136 	for (i = 0; i < SI_MAX_PARMS; i++) {
137 		if (i < num_ports && ports[i])
138 			ipmi_hardcode_init_one(si_type[i], i, ports[i],
139 					       IPMI_IO_ADDR_SPACE);
140 		if (i < num_addrs && addrs[i])
141 			ipmi_hardcode_init_one(si_type[i], i, addrs[i],
142 					       IPMI_MEM_ADDR_SPACE);
143 	}
144 }
145 
146 
147 void ipmi_si_hardcode_exit(void)
148 {
149 	ipmi_remove_platform_device_by_name("hardcode-ipmi-si");
150 }
151 
152 /*
153  * Returns true of the given address exists as a hardcoded address,
154  * false if not.
155  */
156 int ipmi_si_hardcode_match(int addr_space, unsigned long addr)
157 {
158 	unsigned int i;
159 
160 	if (addr_space == IPMI_IO_ADDR_SPACE) {
161 		for (i = 0; i < num_ports; i++) {
162 			if (ports[i] == addr)
163 				return 1;
164 		}
165 	} else {
166 		for (i = 0; i < num_addrs; i++) {
167 			if (addrs[i] == addr)
168 				return 1;
169 		}
170 	}
171 
172 	return 0;
173 }
174