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,
36 		 "Defines the type of each interface, each interface separated by commas.  The types are 'kcs', 'smic', and 'bt'.  For example si_type=kcs,bt will set the first interface to kcs and the second to bt");
37 module_param_hw_array(addrs, ulong, iomem, &num_addrs, 0);
38 MODULE_PARM_DESC(addrs,
39 		 "Sets the memory address of each interface, the addresses separated by commas.  Only use if an interface is in memory.  Otherwise, set it to zero or leave it blank.");
40 module_param_hw_array(ports, uint, ioport, &num_ports, 0);
41 MODULE_PARM_DESC(ports,
42 		 "Sets the port address of each interface, the addresses separated by commas.  Only use if an interface is a port.  Otherwise, set it to zero or leave it blank.");
43 module_param_hw_array(irqs, int, irq, &num_irqs, 0);
44 MODULE_PARM_DESC(irqs,
45 		 "Sets the interrupt of each interface, the addresses separated by commas.  Only use if an interface has an interrupt.  Otherwise, set it to zero or leave it blank.");
46 module_param_hw_array(regspacings, int, other, &num_regspacings, 0);
47 MODULE_PARM_DESC(regspacings,
48 		 "The number of bytes between the start address and each successive register used by the interface.  For instance, if the start address is 0xca2 and the spacing is 2, then the second address is at 0xca4.  Defaults to 1.");
49 module_param_hw_array(regsizes, int, other, &num_regsizes, 0);
50 MODULE_PARM_DESC(regsizes,
51 		 "The size of the specific IPMI register in bytes. This should generally be 1, 2, 4, or 8 for an 8-bit, 16-bit, 32-bit, or 64-bit register.  Use this if you the 8-bit IPMI register has to be read from a larger register.");
52 module_param_hw_array(regshifts, int, other, &num_regshifts, 0);
53 MODULE_PARM_DESC(regshifts,
54 		 "The amount to shift the data read from the. IPMI register, in bits.  For instance, if the data is read from a 32-bit word and the IPMI data is in bit 8-15, then the shift would be 8");
55 module_param_hw_array(slave_addrs, int, other, &num_slave_addrs, 0);
56 MODULE_PARM_DESC(slave_addrs,
57 		 "Set the default IPMB slave address for the controller.  Normally this is 0x20, but can be overridden by this parm.  This is an array indexed by interface number.");
58 
59 static void __init ipmi_hardcode_init_one(const char *si_type_str,
60 					  unsigned int i,
61 					  unsigned long addr,
62 					  enum ipmi_addr_space addr_space)
63 {
64 	struct ipmi_plat_data p;
65 	int t;
66 
67 	memset(&p, 0, sizeof(p));
68 
69 	p.iftype = IPMI_PLAT_IF_SI;
70 	if (!si_type_str || !*si_type_str) {
71 		p.type = SI_KCS;
72 	} else {
73 		t = match_string(si_to_str, -1, si_type_str);
74 		if (t < 0) {
75 			pr_warn("Interface type specified for interface %d, was invalid: %s\n",
76 				i, si_type_str);
77 			return;
78 		}
79 		p.type = t;
80 	}
81 
82 	p.regsize = regsizes[i];
83 	p.slave_addr = slave_addrs[i];
84 	p.addr_source = SI_HARDCODED;
85 	p.regshift = regshifts[i];
86 	p.regsize = regsizes[i];
87 	p.addr = addr;
88 	p.space = addr_space;
89 
90 	ipmi_platform_add("hardcode-ipmi-si", i, &p);
91 }
92 
93 void __init ipmi_hardcode_init(void)
94 {
95 	unsigned int i;
96 	char *str;
97 	char *si_type[SI_MAX_PARMS];
98 
99 	memset(si_type, 0, sizeof(si_type));
100 
101 	/* Parse out the si_type string into its components. */
102 	str = si_type_str;
103 	if (*str != '\0') {
104 		for (i = 0; (i < SI_MAX_PARMS) && (*str != '\0'); i++) {
105 			si_type[i] = str;
106 			str = strchr(str, ',');
107 			if (str) {
108 				*str = '\0';
109 				str++;
110 			} else {
111 				break;
112 			}
113 		}
114 	}
115 
116 	for (i = 0; i < SI_MAX_PARMS; i++) {
117 		if (i < num_ports && ports[i])
118 			ipmi_hardcode_init_one(si_type[i], i, ports[i],
119 					       IPMI_IO_ADDR_SPACE);
120 		if (i < num_addrs && addrs[i])
121 			ipmi_hardcode_init_one(si_type[i], i, addrs[i],
122 					       IPMI_MEM_ADDR_SPACE);
123 	}
124 }
125 
126 
127 void ipmi_si_hardcode_exit(void)
128 {
129 	ipmi_remove_platform_device_by_name("hardcode-ipmi-si");
130 }
131 
132 /*
133  * Returns true of the given address exists as a hardcoded address,
134  * false if not.
135  */
136 int ipmi_si_hardcode_match(int addr_space, unsigned long addr)
137 {
138 	unsigned int i;
139 
140 	if (addr_space == IPMI_IO_ADDR_SPACE) {
141 		for (i = 0; i < num_ports; i++) {
142 			if (ports[i] == addr)
143 				return 1;
144 		}
145 	} else {
146 		for (i = 0; i < num_addrs; i++) {
147 			if (addrs[i] == addr)
148 				return 1;
149 		}
150 	}
151 
152 	return 0;
153 }
154