blob: e914b8c80943316a270ac3ba9e64bcc1addbc979 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
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 *
Al Virod36b6912011-12-29 17:09:01 -050021 * Author: Max Asböck <amax@us.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 *
Dmitry Torokhov3110dc72007-07-17 04:03:58 -070023 * This driver is based on code originally written by Pete Reynolds
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * 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
Dmitry Torokhov3110dc72007-07-17 04:03:58 -070033 * to send periodic heartbeats to the OS.
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 *
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.
Dmitry Torokhov3110dc72007-07-17 04:03:58 -070039 * The interface for this is the ibmasmfs file system.
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090055#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include "ibmasm.h"
57#include "lowlevel.h"
58#include "remote.h"
59
Max Asbock278d72a2005-06-21 17:16:34 -070060int ibmasm_debug = 0;
61module_param(ibmasm_debug, int , S_IRUGO | S_IWUSR);
62MODULE_PARM_DESC(ibmasm_debug, " Set debug mode on or off");
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Bill Pemberton80c8ae22012-11-19 13:23:05 -050065static int ibmasm_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Max Asbock278d72a2005-06-21 17:16:34 -070067 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 struct service_processor *sp;
69
Max Asbock278d72a2005-06-21 17:16:34 -070070 if ((result = pci_enable_device(pdev))) {
71 dev_err(&pdev->dev, "Failed to enable PCI device\n");
72 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
Max Asbock278d72a2005-06-21 17:16:34 -070074 if ((result = pci_request_regions(pdev, DRIVER_NAME))) {
75 dev_err(&pdev->dev, "Failed to allocate PCI resources\n");
76 goto error_resources;
77 }
78 /* vnc client won't work without bus-mastering */
79 pci_set_master(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Yoann Padioleaudd00cc42007-07-19 01:49:03 -070081 sp = kzalloc(sizeof(struct service_processor), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (sp == NULL) {
83 dev_err(&pdev->dev, "Failed to allocate memory\n");
84 result = -ENOMEM;
85 goto error_kmalloc;
86 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Ingo Molnar34af9462006-06-27 02:53:55 -070088 spin_lock_init(&sp->lock);
Max Asbock278d72a2005-06-21 17:16:34 -070089 INIT_LIST_HEAD(&sp->command_queue);
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 pci_set_drvdata(pdev, (void *)sp);
92 sp->dev = &pdev->dev;
93 sp->number = pdev->bus->number;
94 snprintf(sp->dirname, IBMASM_NAME_SIZE, "%d", sp->number);
95 snprintf(sp->devname, IBMASM_NAME_SIZE, "%s%d", DRIVER_NAME, sp->number);
96
Anton Vasilyevf403f85f2017-08-01 16:25:59 +030097 result = ibmasm_event_buffer_init(sp);
98 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 dev_err(sp->dev, "Failed to allocate event buffer\n");
100 goto error_eventbuffer;
101 }
102
Anton Vasilyevf403f85f2017-08-01 16:25:59 +0300103 result = ibmasm_heartbeat_init(sp);
104 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 dev_err(sp->dev, "Failed to allocate heartbeat command\n");
106 goto error_heartbeat;
107 }
108
109 sp->irq = pdev->irq;
Arjan van de Venbdbeed72009-01-06 14:40:39 -0800110 sp->base_address = pci_ioremap_bar(pdev, 0);
Al Viro5cf83b92008-03-29 03:07:48 +0000111 if (!sp->base_address) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 dev_err(sp->dev, "Failed to ioremap pci memory\n");
113 result = -ENODEV;
114 goto error_ioremap;
115 }
116
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700117 result = request_irq(sp->irq, ibmasm_interrupt_handler, IRQF_SHARED, sp->devname, (void*)sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (result) {
119 dev_err(sp->dev, "Failed to register interrupt handler\n");
120 goto error_request_irq;
121 }
122
123 enable_sp_interrupts(sp->base_address);
Max Asbock278d72a2005-06-21 17:16:34 -0700124
125 result = ibmasm_init_remote_input_dev(sp);
126 if (result) {
127 dev_err(sp->dev, "Failed to initialize remote queue\n");
128 goto error_send_message;
129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 result = ibmasm_send_driver_vpd(sp);
132 if (result) {
133 dev_err(sp->dev, "Failed to send driver VPD to service processor\n");
134 goto error_send_message;
135 }
136 result = ibmasm_send_os_state(sp, SYSTEM_STATE_OS_UP);
137 if (result) {
138 dev_err(sp->dev, "Failed to send OS state to service processor\n");
139 goto error_send_message;
140 }
141 ibmasmfs_add_sp(sp);
142
143 ibmasm_register_uart(sp);
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return 0;
146
147error_send_message:
148 disable_sp_interrupts(sp->base_address);
Max Asbock278d72a2005-06-21 17:16:34 -0700149 ibmasm_free_remote_input_dev(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 free_irq(sp->irq, (void *)sp);
151error_request_irq:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 iounmap(sp->base_address);
153error_ioremap:
154 ibmasm_heartbeat_exit(sp);
155error_heartbeat:
156 ibmasm_event_buffer_exit(sp);
157error_eventbuffer:
158 kfree(sp);
159error_kmalloc:
Max Asbock278d72a2005-06-21 17:16:34 -0700160 pci_release_regions(pdev);
161error_resources:
162 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 return result;
165}
166
Bill Pemberton486a5c22012-11-19 13:26:02 -0500167static void ibmasm_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Jingoo Hanbbf831d2013-09-09 14:15:52 +0900169 struct service_processor *sp = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Max Asbock278d72a2005-06-21 17:16:34 -0700171 dbg("Unregistering UART\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 ibmasm_unregister_uart(sp);
Max Asbock278d72a2005-06-21 17:16:34 -0700173 dbg("Sending OS down message\n");
174 if (ibmasm_send_os_state(sp, SYSTEM_STATE_OS_DOWN))
Colin Ian Kingecbf1282016-11-12 17:54:43 +0000175 err("failed to get response to 'Send OS State' command\n");
Max Asbock278d72a2005-06-21 17:16:34 -0700176 dbg("Disabling heartbeats\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 ibmasm_heartbeat_exit(sp);
Max Asbock278d72a2005-06-21 17:16:34 -0700178 dbg("Disabling interrupts\n");
179 disable_sp_interrupts(sp->base_address);
180 dbg("Freeing SP irq\n");
181 free_irq(sp->irq, (void *)sp);
182 dbg("Cleaning up\n");
183 ibmasm_free_remote_input_dev(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 iounmap(sp->base_address);
185 ibmasm_event_buffer_exit(sp);
186 kfree(sp);
Max Asbock278d72a2005-06-21 17:16:34 -0700187 pci_release_regions(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 pci_disable_device(pdev);
189}
190
191static struct pci_device_id ibmasm_pci_table[] =
192{
193 { PCI_DEVICE(VENDORID_IBM, DEVICEID_RSA) },
194 {},
195};
196
197static struct pci_driver ibmasm_driver = {
198 .name = DRIVER_NAME,
199 .id_table = ibmasm_pci_table,
200 .probe = ibmasm_init_one,
Bill Pemberton2d6bed92012-11-19 13:21:23 -0500201 .remove = ibmasm_remove_one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202};
203
204static void __exit ibmasm_exit (void)
205{
206 ibmasm_unregister_panic_notifier();
207 ibmasmfs_unregister();
208 pci_unregister_driver(&ibmasm_driver);
209 info(DRIVER_DESC " version " DRIVER_VERSION " unloaded");
210}
211
212static int __init ibmasm_init(void)
213{
Al Viroea23b4532012-03-17 01:50:32 -0400214 int result = pci_register_driver(&ibmasm_driver);
215 if (result)
216 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 result = ibmasmfs_register();
219 if (result) {
Al Viroea23b4532012-03-17 01:50:32 -0400220 pci_unregister_driver(&ibmasm_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 err("Failed to register ibmasmfs file system");
222 return result;
223 }
Al Viroea23b4532012-03-17 01:50:32 -0400224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 ibmasm_register_panic_notifier();
226 info(DRIVER_DESC " version " DRIVER_VERSION " loaded");
227 return 0;
228}
229
230module_init(ibmasm_init);
231module_exit(ibmasm_exit);
232
233MODULE_AUTHOR(DRIVER_AUTHOR);
234MODULE_DESCRIPTION(DRIVER_DESC);
235MODULE_LICENSE("GPL");
236MODULE_DEVICE_TABLE(pci, ibmasm_pci_table);
237