1 /*
2  * ACPI PCI Hot Plug IBM Extension
3  *
4  * Copyright (C) 2004 Vernon Mauery <vernux@us.ibm.com>
5  * Copyright (C) 2004 IBM Corp.
6  *
7  * All rights reserved.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or (at
12  * your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
17  * NON INFRINGEMENT.  See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  * Send feedback to <vernux@us.ibm.com>
25  *
26  */
27 
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <acpi/acpi_bus.h>
32 #include <linux/sysfs.h>
33 #include <linux/kobject.h>
34 #include <asm/uaccess.h>
35 #include <linux/moduleparam.h>
36 
37 #include "acpiphp.h"
38 
39 #define DRIVER_VERSION	"1.0.1"
40 #define DRIVER_AUTHOR	"Irene Zubarev <zubarev@us.ibm.com>, Vernon Mauery <vernux@us.ibm.com>"
41 #define DRIVER_DESC	"ACPI Hot Plug PCI Controller Driver IBM extension"
42 
43 static int debug;
44 
45 MODULE_AUTHOR(DRIVER_AUTHOR);
46 MODULE_DESCRIPTION(DRIVER_DESC);
47 MODULE_LICENSE("GPL");
48 MODULE_VERSION(DRIVER_VERSION);
49 module_param(debug, bool, 0644);
50 MODULE_PARM_DESC(debug, " Debugging mode enabled or not");
51 #define MY_NAME "acpiphp_ibm"
52 
53 #undef dbg
54 #define dbg(format, arg...)				\
55 do {							\
56 	if (debug)					\
57 		printk(KERN_DEBUG "%s: " format,	\
58 				MY_NAME , ## arg);	\
59 } while (0)
60 
61 #define FOUND_APCI 0x61504349
62 /* these are the names for the IBM ACPI pseudo-device */
63 #define IBM_HARDWARE_ID1 "IBM37D0"
64 #define IBM_HARDWARE_ID2 "IBM37D4"
65 
66 #define hpslot_to_sun(A) (((struct slot *)((A)->private))->acpi_slot->sun)
67 
68 /* union apci_descriptor - allows access to the
69  * various device descriptors that are embedded in the
70  * aPCI table
71  */
72 union apci_descriptor {
73 	struct {
74 		char sig[4];
75 		u8   len;
76 	} header;
77 	struct {
78 		u8  type;
79 		u8  len;
80 		u16 slot_id;
81 		u8  bus_id;
82 		u8  dev_num;
83 		u8  slot_num;
84 		u8  slot_attr[2];
85 		u8  attn;
86 		u8  status[2];
87 		u8  sun;
88 		u8  res[3];
89 	} slot;
90 	struct {
91 		u8 type;
92 		u8 len;
93 	} generic;
94 };
95 
96 /* struct notification - keeps info about the device
97  * that cause the ACPI notification event
98  */
99 struct notification {
100 	struct acpi_device *device;
101 	u8                  event;
102 };
103 
104 static int ibm_set_attention_status(struct hotplug_slot *slot, u8 status);
105 static int ibm_get_attention_status(struct hotplug_slot *slot, u8 *status);
106 static void ibm_handle_events(acpi_handle handle, u32 event, void *context);
107 static int ibm_get_table_from_acpi(char **bufp);
108 static ssize_t ibm_read_apci_table(struct kobject *kobj,
109 				   struct bin_attribute *bin_attr,
110 				   char *buffer, loff_t pos, size_t size);
111 static acpi_status __init ibm_find_acpi_device(acpi_handle handle,
112 		u32 lvl, void *context, void **rv);
113 static int __init ibm_acpiphp_init(void);
114 static void __exit ibm_acpiphp_exit(void);
115 
116 static acpi_handle ibm_acpi_handle;
117 static struct notification ibm_note;
118 static struct bin_attribute ibm_apci_table_attr = {
119 	    .attr = {
120 		    .name = "apci_table",
121 		    .mode = S_IRUGO,
122 	    },
123 	    .read = ibm_read_apci_table,
124 	    .write = NULL,
125 };
126 static struct acpiphp_attention_info ibm_attention_info =
127 {
128 	.set_attn = ibm_set_attention_status,
129 	.get_attn = ibm_get_attention_status,
130 	.owner = THIS_MODULE,
131 };
132 
133 /**
134  * ibm_slot_from_id - workaround for bad ibm hardware
135  * @id: the slot number that linux refers to the slot by
136  *
137  * Description: This method returns the aCPI slot descriptor
138  * corresponding to the Linux slot number.  This descriptor
139  * has info about the aPCI slot id and attention status.
140  * This descriptor must be freed using kfree when done.
141  */
142 static union apci_descriptor *ibm_slot_from_id(int id)
143 {
144 	int ind = 0, size;
145 	union apci_descriptor *ret = NULL, *des;
146 	char *table;
147 
148 	size = ibm_get_table_from_acpi(&table);
149 	des = (union apci_descriptor *)table;
150 	if (memcmp(des->header.sig, "aPCI", 4) != 0)
151 		goto ibm_slot_done;
152 
153 	des = (union apci_descriptor *)&table[ind += des->header.len];
154 	while (ind < size && (des->generic.type != 0x82 ||
155 			des->slot.slot_num != id)) {
156 		des = (union apci_descriptor *)&table[ind += des->generic.len];
157 	}
158 
159 	if (ind < size && des->slot.slot_num == id)
160 		ret = des;
161 
162 ibm_slot_done:
163 	if (ret) {
164 		ret = kmalloc(sizeof(union apci_descriptor), GFP_KERNEL);
165 		memcpy(ret, des, sizeof(union apci_descriptor));
166 	}
167 	kfree(table);
168 	return ret;
169 }
170 
171 /**
172  * ibm_set_attention_status - callback method to set the attention LED
173  * @slot: the hotplug_slot to work with
174  * @status: what to set the LED to (0 or 1)
175  *
176  * Description: This method is registered with the acpiphp module as a
177  * callback to do the device specific task of setting the LED status.
178  */
179 static int ibm_set_attention_status(struct hotplug_slot *slot, u8 status)
180 {
181 	union acpi_object args[2];
182 	struct acpi_object_list params = { .pointer = args, .count = 2 };
183 	acpi_status stat;
184 	unsigned long rc;
185 	union apci_descriptor *ibm_slot;
186 
187 	ibm_slot = ibm_slot_from_id(hpslot_to_sun(slot));
188 
189 	dbg("%s: set slot %d (%d) attention status to %d\n", __func__,
190 			ibm_slot->slot.slot_num, ibm_slot->slot.slot_id,
191 			(status ? 1 : 0));
192 
193 	args[0].type = ACPI_TYPE_INTEGER;
194 	args[0].integer.value = ibm_slot->slot.slot_id;
195 	args[1].type = ACPI_TYPE_INTEGER;
196 	args[1].integer.value = (status) ? 1 : 0;
197 
198 	kfree(ibm_slot);
199 
200 	stat = acpi_evaluate_integer(ibm_acpi_handle, "APLS", &params, &rc);
201 	if (ACPI_FAILURE(stat)) {
202 		err("APLS evaluation failed:  0x%08x\n", stat);
203 		return -ENODEV;
204 	} else if (!rc) {
205 		err("APLS method failed:  0x%08lx\n", rc);
206 		return -ERANGE;
207 	}
208 	return 0;
209 }
210 
211 /**
212  * ibm_get_attention_status - callback method to get attention LED status
213  * @slot: the hotplug_slot to work with
214  * @status: returns what the LED is set to (0 or 1)
215  *
216  * Description: This method is registered with the acpiphp module as a
217  * callback to do the device specific task of getting the LED status.
218  *
219  * Because there is no direct method of getting the LED status directly
220  * from an ACPI call, we read the aPCI table and parse out our
221  * slot descriptor to read the status from that.
222  */
223 static int ibm_get_attention_status(struct hotplug_slot *slot, u8 *status)
224 {
225 	union apci_descriptor *ibm_slot;
226 
227 	ibm_slot = ibm_slot_from_id(hpslot_to_sun(slot));
228 
229 	if (ibm_slot->slot.attn & 0xa0 || ibm_slot->slot.status[1] & 0x08)
230 		*status = 1;
231 	else
232 		*status = 0;
233 
234 	dbg("%s: get slot %d (%d) attention status is %d\n", __func__,
235 			ibm_slot->slot.slot_num, ibm_slot->slot.slot_id,
236 			*status);
237 
238 	kfree(ibm_slot);
239 	return 0;
240 }
241 
242 /**
243  * ibm_handle_events - listens for ACPI events for the IBM37D0 device
244  * @handle: an ACPI handle to the device that caused the event
245  * @event: the event info (device specific)
246  * @context: passed context (our notification struct)
247  *
248  * Description: This method is registered as a callback with the ACPI
249  * subsystem it is called when this device has an event to notify the OS of.
250  *
251  * The events actually come from the device as two events that get
252  * synthesized into one event with data by this function.  The event
253  * ID comes first and then the slot number that caused it.  We report
254  * this as one event to the OS.
255  *
256  * From section 5.6.2.2 of the ACPI 2.0 spec, I understand that the OSPM will
257  * only re-enable the interrupt that causes this event AFTER this method
258  * has returned, thereby enforcing serial access for the notification struct.
259  */
260 static void ibm_handle_events(acpi_handle handle, u32 event, void *context)
261 {
262 	u8 detail = event & 0x0f;
263 	u8 subevent = event & 0xf0;
264 	struct notification *note = context;
265 
266 	dbg("%s: Received notification %02x\n", __func__, event);
267 
268 	if (subevent == 0x80) {
269 		dbg("%s: generationg bus event\n", __func__);
270 		acpi_bus_generate_proc_event(note->device, note->event, detail);
271 		acpi_bus_generate_netlink_event(note->device->pnp.device_class,
272 						  note->device->dev.bus_id,
273 						  note->event, detail);
274 	} else
275 		note->event = event;
276 }
277 
278 /**
279  * ibm_get_table_from_acpi - reads the APLS buffer from ACPI
280  * @bufp: address to pointer to allocate for the table
281  *
282  * Description: This method reads the APLS buffer in from ACPI and
283  * stores the "stripped" table into a single buffer
284  * it allocates and passes the address back in bufp.
285  *
286  * If NULL is passed in as buffer, this method only calculates
287  * the size of the table and returns that without filling
288  * in the buffer.
289  *
290  * Returns < 0 on error or the size of the table on success.
291  */
292 static int ibm_get_table_from_acpi(char **bufp)
293 {
294 	union acpi_object *package;
295 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
296 	acpi_status status;
297 	char *lbuf = NULL;
298 	int i, size = -EIO;
299 
300 	status = acpi_evaluate_object(ibm_acpi_handle, "APCI", NULL, &buffer);
301 	if (ACPI_FAILURE(status)) {
302 		err("%s:  APCI evaluation failed\n", __func__);
303 		return -ENODEV;
304 	}
305 
306 	package = (union acpi_object *) buffer.pointer;
307 	if (!(package) ||
308 			(package->type != ACPI_TYPE_PACKAGE) ||
309 			!(package->package.elements)) {
310 		err("%s:  Invalid APCI object\n", __func__);
311 		goto read_table_done;
312 	}
313 
314 	for(size = 0, i = 0; i < package->package.count; i++) {
315 		if (package->package.elements[i].type != ACPI_TYPE_BUFFER) {
316 			err("%s:  Invalid APCI element %d\n", __func__, i);
317 			goto read_table_done;
318 		}
319 		size += package->package.elements[i].buffer.length;
320 	}
321 
322 	if (bufp == NULL)
323 		goto read_table_done;
324 
325 	lbuf = kzalloc(size, GFP_KERNEL);
326 	dbg("%s: element count: %i, ASL table size: %i, &table = 0x%p\n",
327 			__func__, package->package.count, size, lbuf);
328 
329 	if (lbuf) {
330 		*bufp = lbuf;
331 	} else {
332 		size = -ENOMEM;
333 		goto read_table_done;
334 	}
335 
336 	size = 0;
337 	for (i=0; i<package->package.count; i++) {
338 		memcpy(&lbuf[size],
339 				package->package.elements[i].buffer.pointer,
340 				package->package.elements[i].buffer.length);
341 		size += package->package.elements[i].buffer.length;
342 	}
343 
344 read_table_done:
345 	kfree(buffer.pointer);
346 	return size;
347 }
348 
349 /**
350  * ibm_read_apci_table - callback for the sysfs apci_table file
351  * @kobj: the kobject this binary attribute is a part of
352  * @bin_attr: struct bin_attribute for this file
353  * @buffer: the kernel space buffer to fill
354  * @pos: the offset into the file
355  * @size: the number of bytes requested
356  *
357  * Description: Gets registered with sysfs as the reader callback
358  * to be executed when /sys/bus/pci/slots/apci_table gets read.
359  *
360  * Since we don't get notified on open and close for this file,
361  * things get really tricky here...
362  * our solution is to only allow reading the table in all at once.
363  */
364 static ssize_t ibm_read_apci_table(struct kobject *kobj,
365 				   struct bin_attribute *bin_attr,
366 				   char *buffer, loff_t pos, size_t size)
367 {
368 	int bytes_read = -EINVAL;
369 	char *table = NULL;
370 
371 	dbg("%s: pos = %d, size = %zd\n", __func__, (int)pos, size);
372 
373 	if (pos == 0) {
374 		bytes_read = ibm_get_table_from_acpi(&table);
375 		if (bytes_read > 0 && bytes_read <= size)
376 			memcpy(buffer, table, bytes_read);
377 		kfree(table);
378 	}
379 	return bytes_read;
380 }
381 
382 /**
383  * ibm_find_acpi_device - callback to find our ACPI device
384  * @handle: the ACPI handle of the device we are inspecting
385  * @lvl: depth into the namespace tree
386  * @context: a pointer to our handle to fill when we find the device
387  * @rv: a return value to fill if desired
388  *
389  * Description: Used as a callback when calling acpi_walk_namespace
390  * to find our device.  When this method returns non-zero
391  * acpi_walk_namespace quits its search and returns our value.
392  */
393 static acpi_status __init ibm_find_acpi_device(acpi_handle handle,
394 		u32 lvl, void *context, void **rv)
395 {
396 	acpi_handle *phandle = (acpi_handle *)context;
397 	acpi_status status;
398 	struct acpi_device_info *info;
399 	struct acpi_buffer info_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
400 	int retval = 0;
401 
402 	status = acpi_get_object_info(handle, &info_buffer);
403 	if (ACPI_FAILURE(status)) {
404 		err("%s:  Failed to get device information status=0x%x\n",
405 			__func__, status);
406 		return retval;
407 	}
408 	info = info_buffer.pointer;
409 	info->hardware_id.value[sizeof(info->hardware_id.value) - 1] = '\0';
410 
411 	if (info->current_status && (info->valid & ACPI_VALID_HID) &&
412 			(!strcmp(info->hardware_id.value, IBM_HARDWARE_ID1) ||
413 			 !strcmp(info->hardware_id.value, IBM_HARDWARE_ID2))) {
414 		dbg("found hardware: %s, handle: %p\n",
415 			info->hardware_id.value, handle);
416 		*phandle = handle;
417 		/* returning non-zero causes the search to stop
418 		 * and returns this value to the caller of
419 		 * acpi_walk_namespace, but it also causes some warnings
420 		 * in the acpi debug code to print...
421 		 */
422 		retval = FOUND_APCI;
423 	}
424 	kfree(info);
425 	return retval;
426 }
427 
428 static int __init ibm_acpiphp_init(void)
429 {
430 	int retval = 0;
431 	acpi_status status;
432 	struct acpi_device *device;
433 	struct kobject *sysdir = &pci_hotplug_slots_kset->kobj;
434 
435 	dbg("%s\n", __func__);
436 
437 	if (acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
438 			ACPI_UINT32_MAX, ibm_find_acpi_device,
439 			&ibm_acpi_handle, NULL) != FOUND_APCI) {
440 		err("%s: acpi_walk_namespace failed\n", __func__);
441 		retval = -ENODEV;
442 		goto init_return;
443 	}
444 	dbg("%s: found IBM aPCI device\n", __func__);
445 	if (acpi_bus_get_device(ibm_acpi_handle, &device)) {
446 		err("%s: acpi_bus_get_device failed\n", __func__);
447 		retval = -ENODEV;
448 		goto init_return;
449 	}
450 	if (acpiphp_register_attention(&ibm_attention_info)) {
451 		retval = -ENODEV;
452 		goto init_return;
453 	}
454 
455 	ibm_note.device = device;
456 	status = acpi_install_notify_handler(ibm_acpi_handle,
457 			ACPI_DEVICE_NOTIFY, ibm_handle_events,
458 			&ibm_note);
459 	if (ACPI_FAILURE(status)) {
460 		err("%s: Failed to register notification handler\n",
461 				__func__);
462 		retval = -EBUSY;
463 		goto init_cleanup;
464 	}
465 
466 	ibm_apci_table_attr.size = ibm_get_table_from_acpi(NULL);
467 	retval = sysfs_create_bin_file(sysdir, &ibm_apci_table_attr);
468 
469 	return retval;
470 
471 init_cleanup:
472 	acpiphp_unregister_attention(&ibm_attention_info);
473 init_return:
474 	return retval;
475 }
476 
477 static void __exit ibm_acpiphp_exit(void)
478 {
479 	acpi_status status;
480 	struct kobject *sysdir = &pci_hotplug_slots_kset->kobj;
481 
482 	dbg("%s\n", __func__);
483 
484 	if (acpiphp_unregister_attention(&ibm_attention_info))
485 		err("%s: attention info deregistration failed", __func__);
486 
487 	status = acpi_remove_notify_handler(
488 			   ibm_acpi_handle,
489 			   ACPI_DEVICE_NOTIFY,
490 			   ibm_handle_events);
491 	if (ACPI_FAILURE(status))
492 		err("%s: Notification handler removal failed\n", __func__);
493 	/* remove the /sys entries */
494 	sysfs_remove_bin_file(sysdir, &ibm_apci_table_attr);
495 }
496 
497 module_init(ibm_acpiphp_init);
498 module_exit(ibm_acpiphp_exit);
499