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 	memset(si_type, 0, sizeof(si_type));
122 
123 	/* Parse out the si_type string into its components. */
124 	str = si_type_str;
125 	if (*str != '\0') {
126 		for (i = 0; (i < SI_MAX_PARMS) && (*str != '\0'); i++) {
127 			si_type[i] = str;
128 			str = strchr(str, ',');
129 			if (str) {
130 				*str = '\0';
131 				str++;
132 			} else {
133 				break;
134 			}
135 		}
136 	}
137 
138 	for (i = 0; i < SI_MAX_PARMS; i++) {
139 		if (i < num_ports && ports[i])
140 			ipmi_hardcode_init_one(si_type[i], i, ports[i],
141 					       IPMI_IO_ADDR_SPACE);
142 		if (i < num_addrs && addrs[i])
143 			ipmi_hardcode_init_one(si_type[i], i, addrs[i],
144 					       IPMI_MEM_ADDR_SPACE);
145 	}
146 }
147 
148 
149 void ipmi_si_hardcode_exit(void)
150 {
151 	ipmi_remove_platform_device_by_name("hardcode-ipmi-si");
152 }
153 
154 /*
155  * Returns true of the given address exists as a hardcoded address,
156  * false if not.
157  */
158 int ipmi_si_hardcode_match(int addr_space, unsigned long addr)
159 {
160 	unsigned int i;
161 
162 	if (addr_space == IPMI_IO_ADDR_SPACE) {
163 		for (i = 0; i < num_ports; i++) {
164 			if (ports[i] == addr)
165 				return 1;
166 		}
167 	} else {
168 		for (i = 0; i < num_addrs; i++) {
169 			if (addrs[i] == addr)
170 				return 1;
171 		}
172 	}
173 
174 	return 0;
175 }
176