11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Compaq Hot Plug Controller Driver 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1995,2001 Compaq Computer Corporation 51da177e4SLinus Torvalds * Copyright (C) 2001,2003 Greg Kroah-Hartman (greg@kroah.com) 61da177e4SLinus Torvalds * Copyright (C) 2001 IBM Corp. 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * All rights reserved. 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 111da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 121da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or (at 131da177e4SLinus Torvalds * your option) any later version. 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, but 161da177e4SLinus Torvalds * WITHOUT ANY WARRANTY; without even the implied warranty of 171da177e4SLinus Torvalds * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or 181da177e4SLinus Torvalds * NON INFRINGEMENT. See the GNU General Public License for more 191da177e4SLinus Torvalds * details. 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License 221da177e4SLinus Torvalds * along with this program; if not, write to the Free Software 231da177e4SLinus Torvalds * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 241da177e4SLinus Torvalds * 251da177e4SLinus Torvalds * Send feedback to <greg@kroah.com> 261da177e4SLinus Torvalds * 271da177e4SLinus Torvalds */ 281da177e4SLinus Torvalds 291da177e4SLinus Torvalds #include <linux/module.h> 301da177e4SLinus Torvalds #include <linux/kernel.h> 31*5a0e3ad6STejun Heo #include <linux/slab.h> 321da177e4SLinus Torvalds #include <linux/types.h> 331da177e4SLinus Torvalds #include <linux/proc_fs.h> 341da177e4SLinus Torvalds #include <linux/workqueue.h> 351da177e4SLinus Torvalds #include <linux/pci.h> 367a54f25cSGreg Kroah-Hartman #include <linux/pci_hotplug.h> 37405f5571SAlexey Dobriyan #include <linux/smp_lock.h> 389f3f4681SGreg Kroah-Hartman #include <linux/debugfs.h> 391da177e4SLinus Torvalds #include "cpqphp.h" 401da177e4SLinus Torvalds 419f3f4681SGreg Kroah-Hartman static int show_ctrl (struct controller *ctrl, char *buf) 421da177e4SLinus Torvalds { 431da177e4SLinus Torvalds char *out = buf; 441da177e4SLinus Torvalds int index; 451da177e4SLinus Torvalds struct pci_resource *res; 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds out += sprintf(buf, "Free resources: memory\n"); 481da177e4SLinus Torvalds index = 11; 491da177e4SLinus Torvalds res = ctrl->mem_head; 501da177e4SLinus Torvalds while (res && index--) { 511da177e4SLinus Torvalds out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length); 521da177e4SLinus Torvalds res = res->next; 531da177e4SLinus Torvalds } 541da177e4SLinus Torvalds out += sprintf(out, "Free resources: prefetchable memory\n"); 551da177e4SLinus Torvalds index = 11; 561da177e4SLinus Torvalds res = ctrl->p_mem_head; 571da177e4SLinus Torvalds while (res && index--) { 581da177e4SLinus Torvalds out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length); 591da177e4SLinus Torvalds res = res->next; 601da177e4SLinus Torvalds } 611da177e4SLinus Torvalds out += sprintf(out, "Free resources: IO\n"); 621da177e4SLinus Torvalds index = 11; 631da177e4SLinus Torvalds res = ctrl->io_head; 641da177e4SLinus Torvalds while (res && index--) { 651da177e4SLinus Torvalds out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length); 661da177e4SLinus Torvalds res = res->next; 671da177e4SLinus Torvalds } 681da177e4SLinus Torvalds out += sprintf(out, "Free resources: bus numbers\n"); 691da177e4SLinus Torvalds index = 11; 701da177e4SLinus Torvalds res = ctrl->bus_head; 711da177e4SLinus Torvalds while (res && index--) { 721da177e4SLinus Torvalds out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length); 731da177e4SLinus Torvalds res = res->next; 741da177e4SLinus Torvalds } 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds return out - buf; 771da177e4SLinus Torvalds } 781da177e4SLinus Torvalds 799f3f4681SGreg Kroah-Hartman static int show_dev (struct controller *ctrl, char *buf) 801da177e4SLinus Torvalds { 811da177e4SLinus Torvalds char * out = buf; 821da177e4SLinus Torvalds int index; 831da177e4SLinus Torvalds struct pci_resource *res; 841da177e4SLinus Torvalds struct pci_func *new_slot; 851da177e4SLinus Torvalds struct slot *slot; 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds slot = ctrl->slot; 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds while (slot) { 901da177e4SLinus Torvalds new_slot = cpqhp_slot_find(slot->bus, slot->device, 0); 911da177e4SLinus Torvalds if (!new_slot) 921da177e4SLinus Torvalds break; 931da177e4SLinus Torvalds out += sprintf(out, "assigned resources: memory\n"); 941da177e4SLinus Torvalds index = 11; 951da177e4SLinus Torvalds res = new_slot->mem_head; 961da177e4SLinus Torvalds while (res && index--) { 971da177e4SLinus Torvalds out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length); 981da177e4SLinus Torvalds res = res->next; 991da177e4SLinus Torvalds } 1001da177e4SLinus Torvalds out += sprintf(out, "assigned resources: prefetchable memory\n"); 1011da177e4SLinus Torvalds index = 11; 1021da177e4SLinus Torvalds res = new_slot->p_mem_head; 1031da177e4SLinus Torvalds while (res && index--) { 1041da177e4SLinus Torvalds out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length); 1051da177e4SLinus Torvalds res = res->next; 1061da177e4SLinus Torvalds } 1071da177e4SLinus Torvalds out += sprintf(out, "assigned resources: IO\n"); 1081da177e4SLinus Torvalds index = 11; 1091da177e4SLinus Torvalds res = new_slot->io_head; 1101da177e4SLinus Torvalds while (res && index--) { 1111da177e4SLinus Torvalds out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length); 1121da177e4SLinus Torvalds res = res->next; 1131da177e4SLinus Torvalds } 1141da177e4SLinus Torvalds out += sprintf(out, "assigned resources: bus numbers\n"); 1151da177e4SLinus Torvalds index = 11; 1161da177e4SLinus Torvalds res = new_slot->bus_head; 1171da177e4SLinus Torvalds while (res && index--) { 1181da177e4SLinus Torvalds out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length); 1191da177e4SLinus Torvalds res = res->next; 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds slot=slot->next; 1221da177e4SLinus Torvalds } 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds return out - buf; 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds 1279f3f4681SGreg Kroah-Hartman static int spew_debug_info(struct controller *ctrl, char *data, int size) 1281da177e4SLinus Torvalds { 1299f3f4681SGreg Kroah-Hartman int used; 1309f3f4681SGreg Kroah-Hartman 1319f3f4681SGreg Kroah-Hartman used = size - show_ctrl(ctrl, data); 1329f3f4681SGreg Kroah-Hartman used = (size - used) - show_dev(ctrl, &data[used]); 1339f3f4681SGreg Kroah-Hartman return used; 1341da177e4SLinus Torvalds } 1359f3f4681SGreg Kroah-Hartman 1369f3f4681SGreg Kroah-Hartman struct ctrl_dbg { 1379f3f4681SGreg Kroah-Hartman int size; 1389f3f4681SGreg Kroah-Hartman char *data; 1399f3f4681SGreg Kroah-Hartman struct controller *ctrl; 1409f3f4681SGreg Kroah-Hartman }; 1419f3f4681SGreg Kroah-Hartman 1429f3f4681SGreg Kroah-Hartman #define MAX_OUTPUT (4*PAGE_SIZE) 1439f3f4681SGreg Kroah-Hartman 1449f3f4681SGreg Kroah-Hartman static int open(struct inode *inode, struct file *file) 1459f3f4681SGreg Kroah-Hartman { 1468e18e294STheodore Ts'o struct controller *ctrl = inode->i_private; 1479f3f4681SGreg Kroah-Hartman struct ctrl_dbg *dbg; 1489f3f4681SGreg Kroah-Hartman int retval = -ENOMEM; 1499f3f4681SGreg Kroah-Hartman 1509f3f4681SGreg Kroah-Hartman lock_kernel(); 1519f3f4681SGreg Kroah-Hartman dbg = kmalloc(sizeof(*dbg), GFP_KERNEL); 1529f3f4681SGreg Kroah-Hartman if (!dbg) 1539f3f4681SGreg Kroah-Hartman goto exit; 1549f3f4681SGreg Kroah-Hartman dbg->data = kmalloc(MAX_OUTPUT, GFP_KERNEL); 1559f3f4681SGreg Kroah-Hartman if (!dbg->data) { 1569f3f4681SGreg Kroah-Hartman kfree(dbg); 1579f3f4681SGreg Kroah-Hartman goto exit; 1589f3f4681SGreg Kroah-Hartman } 1599f3f4681SGreg Kroah-Hartman dbg->size = spew_debug_info(ctrl, dbg->data, MAX_OUTPUT); 1609f3f4681SGreg Kroah-Hartman file->private_data = dbg; 1619f3f4681SGreg Kroah-Hartman retval = 0; 1629f3f4681SGreg Kroah-Hartman exit: 1639f3f4681SGreg Kroah-Hartman unlock_kernel(); 1649f3f4681SGreg Kroah-Hartman return retval; 1659f3f4681SGreg Kroah-Hartman } 1669f3f4681SGreg Kroah-Hartman 1679f3f4681SGreg Kroah-Hartman static loff_t lseek(struct file *file, loff_t off, int whence) 1689f3f4681SGreg Kroah-Hartman { 1699f3f4681SGreg Kroah-Hartman struct ctrl_dbg *dbg; 1709f3f4681SGreg Kroah-Hartman loff_t new = -1; 1719f3f4681SGreg Kroah-Hartman 1729f3f4681SGreg Kroah-Hartman lock_kernel(); 1739f3f4681SGreg Kroah-Hartman dbg = file->private_data; 1749f3f4681SGreg Kroah-Hartman 1759f3f4681SGreg Kroah-Hartman switch (whence) { 1769f3f4681SGreg Kroah-Hartman case 0: 1779f3f4681SGreg Kroah-Hartman new = off; 1789f3f4681SGreg Kroah-Hartman break; 1799f3f4681SGreg Kroah-Hartman case 1: 1809f3f4681SGreg Kroah-Hartman new = file->f_pos + off; 1819f3f4681SGreg Kroah-Hartman break; 1829f3f4681SGreg Kroah-Hartman } 1839f3f4681SGreg Kroah-Hartman if (new < 0 || new > dbg->size) { 1849f3f4681SGreg Kroah-Hartman unlock_kernel(); 1859f3f4681SGreg Kroah-Hartman return -EINVAL; 1869f3f4681SGreg Kroah-Hartman } 1879f3f4681SGreg Kroah-Hartman unlock_kernel(); 1889f3f4681SGreg Kroah-Hartman return (file->f_pos = new); 1899f3f4681SGreg Kroah-Hartman } 1909f3f4681SGreg Kroah-Hartman 1919f3f4681SGreg Kroah-Hartman static ssize_t read(struct file *file, char __user *buf, 1929f3f4681SGreg Kroah-Hartman size_t nbytes, loff_t *ppos) 1939f3f4681SGreg Kroah-Hartman { 1949f3f4681SGreg Kroah-Hartman struct ctrl_dbg *dbg = file->private_data; 1959f3f4681SGreg Kroah-Hartman return simple_read_from_buffer(buf, nbytes, ppos, dbg->data, dbg->size); 1969f3f4681SGreg Kroah-Hartman } 1979f3f4681SGreg Kroah-Hartman 1989f3f4681SGreg Kroah-Hartman static int release(struct inode *inode, struct file *file) 1999f3f4681SGreg Kroah-Hartman { 2009f3f4681SGreg Kroah-Hartman struct ctrl_dbg *dbg = file->private_data; 2019f3f4681SGreg Kroah-Hartman 2029f3f4681SGreg Kroah-Hartman kfree(dbg->data); 2039f3f4681SGreg Kroah-Hartman kfree(dbg); 2049f3f4681SGreg Kroah-Hartman return 0; 2059f3f4681SGreg Kroah-Hartman } 2069f3f4681SGreg Kroah-Hartman 207d54b1fdbSArjan van de Ven static const struct file_operations debug_ops = { 2089f3f4681SGreg Kroah-Hartman .owner = THIS_MODULE, 2099f3f4681SGreg Kroah-Hartman .open = open, 2109f3f4681SGreg Kroah-Hartman .llseek = lseek, 2119f3f4681SGreg Kroah-Hartman .read = read, 2129f3f4681SGreg Kroah-Hartman .release = release, 2139f3f4681SGreg Kroah-Hartman }; 2149f3f4681SGreg Kroah-Hartman 2159f3f4681SGreg Kroah-Hartman static struct dentry *root; 2169f3f4681SGreg Kroah-Hartman 2179f3f4681SGreg Kroah-Hartman void cpqhp_initialize_debugfs(void) 2189f3f4681SGreg Kroah-Hartman { 2199f3f4681SGreg Kroah-Hartman if (!root) 2209f3f4681SGreg Kroah-Hartman root = debugfs_create_dir("cpqhp", NULL); 2219f3f4681SGreg Kroah-Hartman } 2229f3f4681SGreg Kroah-Hartman 2239f3f4681SGreg Kroah-Hartman void cpqhp_shutdown_debugfs(void) 2249f3f4681SGreg Kroah-Hartman { 2259f3f4681SGreg Kroah-Hartman debugfs_remove(root); 2269f3f4681SGreg Kroah-Hartman } 2279f3f4681SGreg Kroah-Hartman 2289f3f4681SGreg Kroah-Hartman void cpqhp_create_debugfs_files(struct controller *ctrl) 2299f3f4681SGreg Kroah-Hartman { 23054cc6954SKay Sievers ctrl->dentry = debugfs_create_file(dev_name(&ctrl->pci_dev->dev), 23154cc6954SKay Sievers S_IRUGO, root, ctrl, &debug_ops); 2329f3f4681SGreg Kroah-Hartman } 2339f3f4681SGreg Kroah-Hartman 2349f3f4681SGreg Kroah-Hartman void cpqhp_remove_debugfs_files(struct controller *ctrl) 2359f3f4681SGreg Kroah-Hartman { 2369f3f4681SGreg Kroah-Hartman if (ctrl->dentry) 2379f3f4681SGreg Kroah-Hartman debugfs_remove(ctrl->dentry); 2389f3f4681SGreg Kroah-Hartman ctrl->dentry = NULL; 2399f3f4681SGreg Kroah-Hartman } 2409f3f4681SGreg Kroah-Hartman 241