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