summaryrefslogtreecommitdiff
path: root/src/drivers/ipmi/ipmi_kcs_ops.c
blob: 7fa28e3df5476a7b9d17fff7717d5112c7ad01f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* SPDX-License-Identifier: GPL-2.0-only */

/*
 * Place in devicetree.cb:
 *
 * chip drivers/ipmi
 *   device pnp ca2.0 on end         # IPMI KCS
 * end
 */

#include <console/console.h>
#include <device/device.h>
#include <device/pnp.h>
#if CONFIG(HAVE_ACPI_TABLES)
#include <acpi/acpi.h>
#include <acpi/acpigen.h>
#endif
#if CONFIG(GENERATE_SMBIOS_TABLES)
#include <smbios.h>
#endif
#include <version.h>
#include <delay.h>
#include <timer.h>
#include "ipmi_kcs.h"
#include "chip.h"

/* 4 bit encoding */
static u8 ipmi_revision_major = 0x1;
static u8 ipmi_revision_minor = 0x0;

static int ipmi_get_device_id(struct device *dev, struct ipmi_devid_rsp *rsp)
{
	int ret;

	ret = ipmi_kcs_message(dev->path.pnp.port, IPMI_NETFN_APPLICATION, 0,
			     IPMI_BMC_GET_DEVICE_ID, NULL, 0, (u8 *)rsp,
			     sizeof(*rsp));
	if (ret < sizeof(struct ipmi_rsp) || rsp->resp.completion_code) {
		printk(BIOS_ERR, "IPMI: %s command failed (ret=%d resp=0x%x)\n",
		       __func__, ret, rsp->resp.completion_code);
		return 1;
	}
	if (ret != sizeof(*rsp)) {
		printk(BIOS_ERR, "IPMI: %s response truncated\n", __func__);
		return 1;
	}
	return 0;
}

static int ipmi_get_bmc_self_test_result(struct device *dev, struct ipmi_selftest_rsp *rsp)
{
	int ret;

	ret = ipmi_kcs_message(dev->path.pnp.port, IPMI_NETFN_APPLICATION, 0,
				 IPMI_BMC_GET_SELFTEST_RESULTS, NULL, 0, (u8 *)rsp,
				 sizeof(*rsp));

	if (ret < sizeof(struct ipmi_rsp) || rsp->resp.completion_code) {
		printk(BIOS_ERR, "IPMI: %s command failed (ret=%d resp=0x%x)\n",
		       __func__, ret, rsp->resp.completion_code);
		return 1;
	}
	if (ret != sizeof(*rsp)) {
		printk(BIOS_ERR, "IPMI: %s response truncated\n", __func__);
		return 1;
	}

	return 0;
}

static void ipmi_kcs_init(struct device *dev)
{
	struct ipmi_devid_rsp rsp;
	uint32_t man_id = 0, prod_id = 0;
	struct drivers_ipmi_config *conf = NULL;
	struct ipmi_selftest_rsp selftestrsp;
	uint8_t retry_count;

	if (!dev->enabled)
		return;

	printk(BIOS_DEBUG, "IPMI: PNP KCS 0x%x\n", dev->path.pnp.port);

	if (dev->chip_info)
		conf = dev->chip_info;

	/* Get IPMI version for ACPI and SMBIOS */
	if (conf && conf->wait_for_bmc && conf->bmc_boot_timeout) {
		struct stopwatch sw;
		stopwatch_init_msecs_expire(&sw, conf->bmc_boot_timeout * 1000);
		printk(BIOS_INFO, "IPMI: Waiting for BMC...\n");

		while (!stopwatch_expired(&sw)) {
			if (inb(dev->path.pnp.port) != 0xff)
				break;
			mdelay(100);
		}
		if (stopwatch_expired(&sw)) {
			printk(BIOS_INFO, "IPMI: Waiting for BMC timed out\n");
			/* Don't write tables if communication failed */
			dev->enabled = 0;
			return;
		}
	}

	printk(BIOS_INFO, "Get BMC self test result...");
	for (retry_count = 0; retry_count < conf->bmc_boot_timeout; retry_count++) {
		if (!ipmi_get_bmc_self_test_result(dev, &selftestrsp))
			break;

		mdelay(1000);
	}

	switch (selftestrsp.result) {
	case IPMI_APP_SELFTEST_NO_ERROR: /* 0x55 */
		printk(BIOS_DEBUG, "No Error\n");
		break;
	case IPMI_APP_SELFTEST_NOT_IMPLEMENTED: /* 0x56 */
		printk(BIOS_DEBUG, "Function Not Implemented\n");
		break;
	case IPMI_APP_SELFTEST_ERROR: /* 0x57 */
		printk(BIOS_ERR, "BMC: Corrupted or inaccessible data or device\n");
		/* Don't write tables if communication failed */
		dev->enabled = 0;
		break;
	case IPMI_APP_SELFTEST_FATAL_HW_ERROR: /* 0x58 */
		printk(BIOS_ERR, "BMC: Fatal Hardware Error\n");
		/* Don't write tables if communication failed */
		dev->enabled = 0;
		break;
	case IPMI_APP_SELFTEST_RESERVED: /* 0xFF */
		printk(BIOS_DEBUG, "Reserved\n");
		break;

	default: /* Other Device Specific Hardware Error */
		printk(BIOS_ERR, "BMC: Device Specific Error\n");
		/* Don't write tables if communication failed */
		dev->enabled = 0;
		break;
	}

	if (!ipmi_get_device_id(dev, &rsp)) {
		/* Queried the IPMI revision from BMC */
		ipmi_revision_minor = IPMI_IPMI_VERSION_MINOR(rsp.ipmi_version);
		ipmi_revision_major = IPMI_IPMI_VERSION_MAJOR(rsp.ipmi_version);

		memcpy(&man_id, rsp.manufacturer_id,
		       sizeof(rsp.manufacturer_id));

		memcpy(&prod_id, rsp.product_id, sizeof(rsp.product_id));

		printk(BIOS_INFO, "IPMI: Found man_id 0x%06x, prod_id 0x%04x\n",
		       man_id, prod_id);

		printk(BIOS_INFO, "IPMI: Version %01x.%01x\n",
		       ipmi_revision_major, ipmi_revision_minor);
	} else {
		/* Don't write tables if communication failed */
		dev->enabled = 0;
	}
}

#if CONFIG(HAVE_ACPI_TABLES)
static uint32_t uid_cnt = 0;

static unsigned long
ipmi_write_acpi_tables(const struct device *dev, unsigned long current,
		       struct acpi_rsdp *rsdp)
{
	struct drivers_ipmi_config *conf = NULL;
	struct acpi_spmi *spmi;
	s8 gpe_interrupt = -1;
	u32 apic_interrupt = 0;
	acpi_addr_t addr = {
		.space_id = ACPI_ADDRESS_SPACE_IO,
		.access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
		.addrl = dev->path.pnp.port,
		.bit_width = 8,
	};

	switch (CONFIG_IPMI_KCS_REGISTER_SPACING) {
	case 4:
		addr.bit_offset = 32;
		break;
	case 16:
		addr.bit_offset = 128;
		break;
	default:
		printk(BIOS_ERR, "IPMI: Unsupported register spacing for SPMI\n");
		/* fall through */
	case 1:
		addr.bit_offset = 8;
		break;
	}

	current = ALIGN_UP(current, 8);
	printk(BIOS_DEBUG, "ACPI:    * SPMI at %lx\n", current);
	spmi = (struct acpi_spmi *)current;

	if (dev->chip_info)
		conf = dev->chip_info;

	if (conf) {
		if (conf->have_gpe)
			gpe_interrupt = conf->gpe_interrupt;
		if (conf->have_apic)
			apic_interrupt = conf->apic_interrupt;
	}

	/* Use command to get UID from ipmi_ssdt */
	acpi_create_ipmi(dev, spmi, (ipmi_revision_major << 8) |
			 (ipmi_revision_minor << 4), &addr,
			 IPMI_INTERFACE_KCS, gpe_interrupt, apic_interrupt,
			 conf->uid);

	acpi_add_table(rsdp, spmi);

	current += spmi->header.length;

	return current;
}

static void ipmi_ssdt(const struct device *dev)
{
	const char *scope = acpi_device_scope(dev);
	struct drivers_ipmi_config *conf = NULL;

	if (!scope) {
		printk(BIOS_ERR, "IPMI: Missing ACPI scope for %s\n",
		       dev_path(dev));
		return;
	}

	if (dev->chip_info)
		conf = dev->chip_info;

	/* Use command to pass UID to ipmi_write_acpi_tables */
	conf->uid = uid_cnt++;

	/* write SPMI device */
	acpigen_write_scope(scope);
	acpigen_write_device("SPMI");
	acpigen_write_name_string("_HID", "IPI0001");
	acpigen_write_name_unicode("_STR", "IPMI_KCS");
	acpigen_write_name_byte("_UID", conf->uid);
	acpigen_write_STA(0xf);
	acpigen_write_name("_CRS");
	acpigen_write_resourcetemplate_header();
	acpigen_write_io16(dev->path.pnp.port, dev->path.pnp.port, 1, 1, 1);
	acpigen_write_io16(dev->path.pnp.port + CONFIG_IPMI_KCS_REGISTER_SPACING,
			   dev->path.pnp.port + CONFIG_IPMI_KCS_REGISTER_SPACING, 1, 1, 1);

	if (conf) {
		// FIXME: is that correct?
		if (conf->have_apic)
			acpigen_write_irq(1 << conf->apic_interrupt);
	}

	acpigen_write_resourcetemplate_footer();

	acpigen_write_method("_IFT", 0);
	acpigen_write_return_byte(1);	// KCS
	acpigen_pop_len();

	acpigen_write_method("_SRV", 0);
	acpigen_write_return_integer((ipmi_revision_major << 8) |
				     (ipmi_revision_minor << 4));
	acpigen_pop_len();

	acpigen_pop_len(); /* pop device */
	acpigen_pop_len(); /* pop scope */
}
#endif

#if CONFIG(GENERATE_SMBIOS_TABLES)
static int ipmi_smbios_data(struct device *dev, int *handle,
			    unsigned long *current)
{
	struct drivers_ipmi_config *conf = NULL;
	u8 nv_storage = 0xff;
	u8 i2c_address = 0;
	u8 register_spacing;

	int len = 0;

	if (dev->chip_info)
		conf = dev->chip_info;

	if (conf) {
		if (conf->have_nv_storage)
			nv_storage = conf->nv_storage_device_address;
		i2c_address = conf->bmc_i2c_address;
	}

	switch (CONFIG_IPMI_KCS_REGISTER_SPACING) {
	case 4:
		register_spacing = 1 << 6;
		break;
	case 16:
		register_spacing = 2 << 6;
		break;
	default:
		printk(BIOS_ERR, "IPMI: Unsupported register spacing for SMBIOS\n");
		/* fall through */
	case 1:
		register_spacing = 0 << 6;
		break;
	}

	// add IPMI Device Information
	len += smbios_write_type38(
		current, handle,
		SMBIOS_BMC_INTERFACE_KCS,
		ipmi_revision_minor | (ipmi_revision_major << 4),
		i2c_address, // I2C address
		nv_storage, // NV storage
		dev->path.pnp.port | 1, // IO interface
		register_spacing,
		0); // no IRQ

	return len;
}
#endif

static void ipmi_set_resources(struct device *dev)
{
	struct resource *res;

	for (res = dev->resource_list; res; res = res->next) {
		if (!(res->flags & IORESOURCE_ASSIGNED))
			continue;

		res->flags |= IORESOURCE_STORED;
		report_resource_stored(dev, res, "");
	}
}

static void ipmi_read_resources(struct device *dev)
{
	struct resource *res = new_resource(dev, 0);
	res->base = dev->path.pnp.port;
	res->size = 2;
	res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
}

static struct device_operations ops = {
	.read_resources   = ipmi_read_resources,
	.set_resources    = ipmi_set_resources,
	.init             = ipmi_kcs_init,
#if CONFIG(HAVE_ACPI_TABLES)
	.write_acpi_tables = ipmi_write_acpi_tables,
	.acpi_fill_ssdt    = ipmi_ssdt,
#endif
#if CONFIG(GENERATE_SMBIOS_TABLES)
	.get_smbios_data = ipmi_smbios_data,
#endif
};

static void enable_dev(struct device *dev)
{
	if (dev->path.type != DEVICE_PATH_PNP)
		printk(BIOS_ERR, "%s: Unsupported device type\n",
		       dev_path(dev));
	else if (dev->path.pnp.port & 1)
		printk(BIOS_ERR, "%s: Base address needs to be aligned to 2\n",
		       dev_path(dev));
	else
		dev->ops = &ops;
}

struct chip_operations drivers_ipmi_ops = {
	CHIP_NAME("IPMI KCS")
	.enable_dev = enable_dev,
};