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