xref: /openbmc/linux/drivers/misc/ibmasm/module.c (revision b6dcefde)
1 
2 /*
3  * IBM ASM Service Processor Device Driver
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Copyright (C) IBM Corporation, 2004
20  *
21  * Author: Max Asb�ck <amax@us.ibm.com>
22  *
23  * This driver is based on code originally written by Pete Reynolds
24  * and others.
25  *
26  */
27 
28 /*
29  * The ASM device driver does the following things:
30  *
31  * 1) When loaded it sends a message to the service processor,
32  * indicating that an OS is * running. This causes the service processor
33  * to send periodic heartbeats to the OS.
34  *
35  * 2) Answers the periodic heartbeats sent by the service processor.
36  * Failure to do so would result in system reboot.
37  *
38  * 3) Acts as a pass through for dot commands sent from user applications.
39  * The interface for this is the ibmasmfs file system.
40  *
41  * 4) Allows user applications to register for event notification. Events
42  * are sent to the driver through interrupts. They can be read from user
43  * space through the ibmasmfs file system.
44  *
45  * 5) Allows user space applications to send heartbeats to the service
46  * processor (aka reverse heartbeats). Again this happens through ibmasmfs.
47  *
48  * 6) Handles remote mouse and keyboard event interrupts and makes them
49  * available to user applications through ibmasmfs.
50  *
51  */
52 
53 #include <linux/pci.h>
54 #include <linux/init.h>
55 #include "ibmasm.h"
56 #include "lowlevel.h"
57 #include "remote.h"
58 
59 int ibmasm_debug = 0;
60 module_param(ibmasm_debug, int , S_IRUGO | S_IWUSR);
61 MODULE_PARM_DESC(ibmasm_debug, " Set debug mode on or off");
62 
63 
64 static int __devinit ibmasm_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
65 {
66 	int result;
67 	struct service_processor *sp;
68 
69 	if ((result = pci_enable_device(pdev))) {
70 		dev_err(&pdev->dev, "Failed to enable PCI device\n");
71 		return result;
72 	}
73 	if ((result = pci_request_regions(pdev, DRIVER_NAME))) {
74 		dev_err(&pdev->dev, "Failed to allocate PCI resources\n");
75 		goto error_resources;
76 	}
77 	/* vnc client won't work without bus-mastering */
78 	pci_set_master(pdev);
79 
80 	sp = kzalloc(sizeof(struct service_processor), GFP_KERNEL);
81 	if (sp == NULL) {
82 		dev_err(&pdev->dev, "Failed to allocate memory\n");
83 		result = -ENOMEM;
84 		goto error_kmalloc;
85 	}
86 
87 	spin_lock_init(&sp->lock);
88 	INIT_LIST_HEAD(&sp->command_queue);
89 
90 	pci_set_drvdata(pdev, (void *)sp);
91 	sp->dev = &pdev->dev;
92 	sp->number = pdev->bus->number;
93 	snprintf(sp->dirname, IBMASM_NAME_SIZE, "%d", sp->number);
94 	snprintf(sp->devname, IBMASM_NAME_SIZE, "%s%d", DRIVER_NAME, sp->number);
95 
96 	if (ibmasm_event_buffer_init(sp)) {
97 		dev_err(sp->dev, "Failed to allocate event buffer\n");
98 		goto error_eventbuffer;
99 	}
100 
101 	if (ibmasm_heartbeat_init(sp)) {
102 		dev_err(sp->dev, "Failed to allocate heartbeat command\n");
103 		goto error_heartbeat;
104 	}
105 
106 	sp->irq = pdev->irq;
107 	sp->base_address = pci_ioremap_bar(pdev, 0);
108 	if (!sp->base_address) {
109 		dev_err(sp->dev, "Failed to ioremap pci memory\n");
110 		result =  -ENODEV;
111 		goto error_ioremap;
112 	}
113 
114 	result = request_irq(sp->irq, ibmasm_interrupt_handler, IRQF_SHARED, sp->devname, (void*)sp);
115 	if (result) {
116 		dev_err(sp->dev, "Failed to register interrupt handler\n");
117 		goto error_request_irq;
118 	}
119 
120 	enable_sp_interrupts(sp->base_address);
121 
122 	result = ibmasm_init_remote_input_dev(sp);
123 	if (result) {
124 		dev_err(sp->dev, "Failed to initialize remote queue\n");
125 		goto error_send_message;
126 	}
127 
128 	result = ibmasm_send_driver_vpd(sp);
129 	if (result) {
130 		dev_err(sp->dev, "Failed to send driver VPD to service processor\n");
131 		goto error_send_message;
132 	}
133 	result = ibmasm_send_os_state(sp, SYSTEM_STATE_OS_UP);
134 	if (result) {
135 		dev_err(sp->dev, "Failed to send OS state to service processor\n");
136 		goto error_send_message;
137 	}
138 	ibmasmfs_add_sp(sp);
139 
140 	ibmasm_register_uart(sp);
141 
142 	return 0;
143 
144 error_send_message:
145 	disable_sp_interrupts(sp->base_address);
146 	ibmasm_free_remote_input_dev(sp);
147 	free_irq(sp->irq, (void *)sp);
148 error_request_irq:
149 	iounmap(sp->base_address);
150 error_ioremap:
151 	ibmasm_heartbeat_exit(sp);
152 error_heartbeat:
153 	ibmasm_event_buffer_exit(sp);
154 error_eventbuffer:
155 	pci_set_drvdata(pdev, NULL);
156 	kfree(sp);
157 error_kmalloc:
158         pci_release_regions(pdev);
159 error_resources:
160         pci_disable_device(pdev);
161 
162 	return result;
163 }
164 
165 static void __devexit ibmasm_remove_one(struct pci_dev *pdev)
166 {
167 	struct service_processor *sp = (struct service_processor *)pci_get_drvdata(pdev);
168 
169 	dbg("Unregistering UART\n");
170 	ibmasm_unregister_uart(sp);
171 	dbg("Sending OS down message\n");
172 	if (ibmasm_send_os_state(sp, SYSTEM_STATE_OS_DOWN))
173 		err("failed to get repsonse to 'Send OS State' command\n");
174 	dbg("Disabling heartbeats\n");
175 	ibmasm_heartbeat_exit(sp);
176 	dbg("Disabling interrupts\n");
177 	disable_sp_interrupts(sp->base_address);
178 	dbg("Freeing SP irq\n");
179 	free_irq(sp->irq, (void *)sp);
180 	dbg("Cleaning up\n");
181 	ibmasm_free_remote_input_dev(sp);
182 	iounmap(sp->base_address);
183 	ibmasm_event_buffer_exit(sp);
184 	pci_set_drvdata(pdev, NULL);
185 	kfree(sp);
186 	pci_release_regions(pdev);
187 	pci_disable_device(pdev);
188 }
189 
190 static struct pci_device_id ibmasm_pci_table[] =
191 {
192 	{ PCI_DEVICE(VENDORID_IBM, DEVICEID_RSA) },
193 	{},
194 };
195 
196 static struct pci_driver ibmasm_driver = {
197 	.name		= DRIVER_NAME,
198 	.id_table	= ibmasm_pci_table,
199 	.probe		= ibmasm_init_one,
200 	.remove		= __devexit_p(ibmasm_remove_one),
201 };
202 
203 static void __exit ibmasm_exit (void)
204 {
205 	ibmasm_unregister_panic_notifier();
206 	ibmasmfs_unregister();
207 	pci_unregister_driver(&ibmasm_driver);
208 	info(DRIVER_DESC " version " DRIVER_VERSION " unloaded");
209 }
210 
211 static int __init ibmasm_init(void)
212 {
213 	int result;
214 
215 	result = ibmasmfs_register();
216 	if (result) {
217 		err("Failed to register ibmasmfs file system");
218 		return result;
219 	}
220 	result = pci_register_driver(&ibmasm_driver);
221 	if (result) {
222 		ibmasmfs_unregister();
223 		return result;
224 	}
225 	ibmasm_register_panic_notifier();
226 	info(DRIVER_DESC " version " DRIVER_VERSION " loaded");
227 	return 0;
228 }
229 
230 module_init(ibmasm_init);
231 module_exit(ibmasm_exit);
232 
233 MODULE_AUTHOR(DRIVER_AUTHOR);
234 MODULE_DESCRIPTION(DRIVER_DESC);
235 MODULE_LICENSE("GPL");
236 MODULE_DEVICE_TABLE(pci, ibmasm_pci_table);
237 
238