xref: /linux/drivers/misc/ibmasm/module.c (revision 278d72ae8803ffcd16070c95fe1d53f4466dc741)
11da177e4SLinus Torvalds 
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * IBM ASM Service Processor Device Driver
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
61da177e4SLinus Torvalds  * it under the terms of the GNU General Public License as published by
71da177e4SLinus Torvalds  * the Free Software Foundation; either version 2 of the License, or
81da177e4SLinus Torvalds  * (at your option) any later version.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * This program is distributed in the hope that it will be useful,
111da177e4SLinus Torvalds  * but WITHOUT ANY WARRANTY; without even the implied warranty of
121da177e4SLinus Torvalds  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
131da177e4SLinus Torvalds  * GNU General Public License for more details.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
161da177e4SLinus Torvalds  * along with this program; if not, write to the Free Software
171da177e4SLinus Torvalds  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  * Copyright (C) IBM Corporation, 2004
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  * Author: Max Asb�ck <amax@us.ibm.com>
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  * This driver is based on code originally written by Pete Reynolds
241da177e4SLinus Torvalds  * and others.
251da177e4SLinus Torvalds  *
261da177e4SLinus Torvalds  */
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds /*
291da177e4SLinus Torvalds  * The ASM device driver does the following things:
301da177e4SLinus Torvalds  *
311da177e4SLinus Torvalds  * 1) When loaded it sends a message to the service processor,
321da177e4SLinus Torvalds  * indicating that an OS is * running. This causes the service processor
331da177e4SLinus Torvalds  * to send periodic heartbeats to the OS.
341da177e4SLinus Torvalds  *
351da177e4SLinus Torvalds  * 2) Answers the periodic heartbeats sent by the service processor.
361da177e4SLinus Torvalds  * Failure to do so would result in system reboot.
371da177e4SLinus Torvalds  *
381da177e4SLinus Torvalds  * 3) Acts as a pass through for dot commands sent from user applications.
391da177e4SLinus Torvalds  * The interface for this is the ibmasmfs file system.
401da177e4SLinus Torvalds  *
411da177e4SLinus Torvalds  * 4) Allows user applications to register for event notification. Events
421da177e4SLinus Torvalds  * are sent to the driver through interrupts. They can be read from user
431da177e4SLinus Torvalds  * space through the ibmasmfs file system.
441da177e4SLinus Torvalds  *
451da177e4SLinus Torvalds  * 5) Allows user space applications to send heartbeats to the service
461da177e4SLinus Torvalds  * processor (aka reverse heartbeats). Again this happens through ibmasmfs.
471da177e4SLinus Torvalds  *
481da177e4SLinus Torvalds  * 6) Handles remote mouse and keyboard event interrupts and makes them
491da177e4SLinus Torvalds  * available to user applications through ibmasmfs.
501da177e4SLinus Torvalds  *
511da177e4SLinus Torvalds  */
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds #include <linux/pci.h>
541da177e4SLinus Torvalds #include <linux/init.h>
551da177e4SLinus Torvalds #include "ibmasm.h"
561da177e4SLinus Torvalds #include "lowlevel.h"
571da177e4SLinus Torvalds #include "remote.h"
581da177e4SLinus Torvalds 
59*278d72aeSMax Asbock int ibmasm_debug = 0;
60*278d72aeSMax Asbock module_param(ibmasm_debug, int , S_IRUGO | S_IWUSR);
61*278d72aeSMax Asbock MODULE_PARM_DESC(ibmasm_debug, " Set debug mode on or off");
62*278d72aeSMax Asbock 
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds static int __devinit ibmasm_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
651da177e4SLinus Torvalds {
66*278d72aeSMax Asbock 	int result;
671da177e4SLinus Torvalds 	struct service_processor *sp;
681da177e4SLinus Torvalds 
69*278d72aeSMax Asbock 	if ((result = pci_enable_device(pdev))) {
70*278d72aeSMax Asbock 		dev_err(&pdev->dev, "Failed to enable PCI device\n");
71*278d72aeSMax Asbock 		return result;
721da177e4SLinus Torvalds 	}
73*278d72aeSMax Asbock 	if ((result = pci_request_regions(pdev, DRIVER_NAME))) {
74*278d72aeSMax Asbock 		dev_err(&pdev->dev, "Failed to allocate PCI resources\n");
75*278d72aeSMax Asbock 		goto error_resources;
76*278d72aeSMax Asbock 	}
77*278d72aeSMax Asbock 	/* vnc client won't work without bus-mastering */
78*278d72aeSMax Asbock 	pci_set_master(pdev);
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	sp = kmalloc(sizeof(struct service_processor), GFP_KERNEL);
811da177e4SLinus Torvalds 	if (sp == NULL) {
821da177e4SLinus Torvalds 		dev_err(&pdev->dev, "Failed to allocate memory\n");
831da177e4SLinus Torvalds 		result = -ENOMEM;
841da177e4SLinus Torvalds 		goto error_kmalloc;
851da177e4SLinus Torvalds 	}
861da177e4SLinus Torvalds 	memset(sp, 0, sizeof(struct service_processor));
871da177e4SLinus Torvalds 
88*278d72aeSMax Asbock 	sp->lock = SPIN_LOCK_UNLOCKED;
89*278d72aeSMax Asbock 	INIT_LIST_HEAD(&sp->command_queue);
90*278d72aeSMax Asbock 
911da177e4SLinus Torvalds 	pci_set_drvdata(pdev, (void *)sp);
921da177e4SLinus Torvalds 	sp->dev = &pdev->dev;
931da177e4SLinus Torvalds 	sp->number = pdev->bus->number;
941da177e4SLinus Torvalds 	snprintf(sp->dirname, IBMASM_NAME_SIZE, "%d", sp->number);
951da177e4SLinus Torvalds 	snprintf(sp->devname, IBMASM_NAME_SIZE, "%s%d", DRIVER_NAME, sp->number);
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	if (ibmasm_event_buffer_init(sp)) {
981da177e4SLinus Torvalds 		dev_err(sp->dev, "Failed to allocate event buffer\n");
991da177e4SLinus Torvalds 		goto error_eventbuffer;
1001da177e4SLinus Torvalds 	}
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 	if (ibmasm_heartbeat_init(sp)) {
1031da177e4SLinus Torvalds 		dev_err(sp->dev, "Failed to allocate heartbeat command\n");
1041da177e4SLinus Torvalds 		goto error_heartbeat;
1051da177e4SLinus Torvalds 	}
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 	sp->irq = pdev->irq;
1081da177e4SLinus Torvalds 	sp->base_address = ioremap(pci_resource_start(pdev, 0),
1091da177e4SLinus Torvalds 					pci_resource_len(pdev, 0));
1101da177e4SLinus Torvalds 	if (sp->base_address == 0) {
1111da177e4SLinus Torvalds 		dev_err(sp->dev, "Failed to ioremap pci memory\n");
1121da177e4SLinus Torvalds 		result =  -ENODEV;
1131da177e4SLinus Torvalds 		goto error_ioremap;
1141da177e4SLinus Torvalds 	}
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 	result = request_irq(sp->irq, ibmasm_interrupt_handler, SA_SHIRQ, sp->devname, (void*)sp);
1171da177e4SLinus Torvalds 	if (result) {
1181da177e4SLinus Torvalds 		dev_err(sp->dev, "Failed to register interrupt handler\n");
1191da177e4SLinus Torvalds 		goto error_request_irq;
1201da177e4SLinus Torvalds 	}
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	enable_sp_interrupts(sp->base_address);
123*278d72aeSMax Asbock 
124*278d72aeSMax Asbock 	result = ibmasm_init_remote_input_dev(sp);
125*278d72aeSMax Asbock 	if (result) {
126*278d72aeSMax Asbock 		dev_err(sp->dev, "Failed to initialize remote queue\n");
127*278d72aeSMax Asbock 		goto error_send_message;
128*278d72aeSMax Asbock 	}
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	result = ibmasm_send_driver_vpd(sp);
1311da177e4SLinus Torvalds 	if (result) {
1321da177e4SLinus Torvalds 		dev_err(sp->dev, "Failed to send driver VPD to service processor\n");
1331da177e4SLinus Torvalds 		goto error_send_message;
1341da177e4SLinus Torvalds 	}
1351da177e4SLinus Torvalds 	result = ibmasm_send_os_state(sp, SYSTEM_STATE_OS_UP);
1361da177e4SLinus Torvalds 	if (result) {
1371da177e4SLinus Torvalds 		dev_err(sp->dev, "Failed to send OS state to service processor\n");
1381da177e4SLinus Torvalds 		goto error_send_message;
1391da177e4SLinus Torvalds 	}
1401da177e4SLinus Torvalds 	ibmasmfs_add_sp(sp);
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	ibmasm_register_uart(sp);
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	return 0;
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds error_send_message:
1471da177e4SLinus Torvalds 	disable_sp_interrupts(sp->base_address);
148*278d72aeSMax Asbock 	ibmasm_free_remote_input_dev(sp);
1491da177e4SLinus Torvalds 	free_irq(sp->irq, (void *)sp);
1501da177e4SLinus Torvalds error_request_irq:
1511da177e4SLinus Torvalds 	iounmap(sp->base_address);
1521da177e4SLinus Torvalds error_ioremap:
1531da177e4SLinus Torvalds 	ibmasm_heartbeat_exit(sp);
1541da177e4SLinus Torvalds error_heartbeat:
1551da177e4SLinus Torvalds 	ibmasm_event_buffer_exit(sp);
1561da177e4SLinus Torvalds error_eventbuffer:
157*278d72aeSMax Asbock 	pci_set_drvdata(pdev, NULL);
1581da177e4SLinus Torvalds 	kfree(sp);
1591da177e4SLinus Torvalds error_kmalloc:
160*278d72aeSMax Asbock         pci_release_regions(pdev);
161*278d72aeSMax Asbock error_resources:
1621da177e4SLinus Torvalds         pci_disable_device(pdev);
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	return result;
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds static void __devexit ibmasm_remove_one(struct pci_dev *pdev)
1681da177e4SLinus Torvalds {
1691da177e4SLinus Torvalds 	struct service_processor *sp = (struct service_processor *)pci_get_drvdata(pdev);
1701da177e4SLinus Torvalds 
171*278d72aeSMax Asbock 	dbg("Unregistering UART\n");
1721da177e4SLinus Torvalds 	ibmasm_unregister_uart(sp);
173*278d72aeSMax Asbock 	dbg("Sending OS down message\n");
174*278d72aeSMax Asbock 	if (ibmasm_send_os_state(sp, SYSTEM_STATE_OS_DOWN))
175*278d72aeSMax Asbock 		err("failed to get repsonse to 'Send OS State' command\n");
176*278d72aeSMax Asbock 	dbg("Disabling heartbeats\n");
1771da177e4SLinus Torvalds 	ibmasm_heartbeat_exit(sp);
178*278d72aeSMax Asbock 	dbg("Disabling interrupts\n");
179*278d72aeSMax Asbock 	disable_sp_interrupts(sp->base_address);
180*278d72aeSMax Asbock 	dbg("Freeing SP irq\n");
181*278d72aeSMax Asbock 	free_irq(sp->irq, (void *)sp);
182*278d72aeSMax Asbock 	dbg("Cleaning up\n");
183*278d72aeSMax Asbock 	ibmasm_free_remote_input_dev(sp);
1841da177e4SLinus Torvalds 	iounmap(sp->base_address);
1851da177e4SLinus Torvalds 	ibmasm_event_buffer_exit(sp);
186*278d72aeSMax Asbock 	pci_set_drvdata(pdev, NULL);
1871da177e4SLinus Torvalds 	kfree(sp);
188*278d72aeSMax Asbock 	pci_release_regions(pdev);
1891da177e4SLinus Torvalds 	pci_disable_device(pdev);
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds static struct pci_device_id ibmasm_pci_table[] =
1931da177e4SLinus Torvalds {
1941da177e4SLinus Torvalds 	{ PCI_DEVICE(VENDORID_IBM, DEVICEID_RSA) },
1951da177e4SLinus Torvalds 	{},
1961da177e4SLinus Torvalds };
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds static struct pci_driver ibmasm_driver = {
1991da177e4SLinus Torvalds 	.name		= DRIVER_NAME,
2001da177e4SLinus Torvalds 	.id_table	= ibmasm_pci_table,
2011da177e4SLinus Torvalds 	.probe		= ibmasm_init_one,
2021da177e4SLinus Torvalds 	.remove		= __devexit_p(ibmasm_remove_one),
2031da177e4SLinus Torvalds };
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds static void __exit ibmasm_exit (void)
2061da177e4SLinus Torvalds {
2071da177e4SLinus Torvalds 	ibmasm_unregister_panic_notifier();
2081da177e4SLinus Torvalds 	ibmasmfs_unregister();
2091da177e4SLinus Torvalds 	pci_unregister_driver(&ibmasm_driver);
2101da177e4SLinus Torvalds 	info(DRIVER_DESC " version " DRIVER_VERSION " unloaded");
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds static int __init ibmasm_init(void)
2141da177e4SLinus Torvalds {
2151da177e4SLinus Torvalds 	int result;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	result = ibmasmfs_register();
2181da177e4SLinus Torvalds 	if (result) {
2191da177e4SLinus Torvalds 		err("Failed to register ibmasmfs file system");
2201da177e4SLinus Torvalds 		return result;
2211da177e4SLinus Torvalds 	}
2221da177e4SLinus Torvalds 	result = pci_register_driver(&ibmasm_driver);
2231da177e4SLinus Torvalds 	if (result) {
2241da177e4SLinus Torvalds 		ibmasmfs_unregister();
2251da177e4SLinus Torvalds 		return result;
2261da177e4SLinus Torvalds 	}
2271da177e4SLinus Torvalds 	ibmasm_register_panic_notifier();
2281da177e4SLinus Torvalds 	info(DRIVER_DESC " version " DRIVER_VERSION " loaded");
2291da177e4SLinus Torvalds 	return 0;
2301da177e4SLinus Torvalds }
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds module_init(ibmasm_init);
2331da177e4SLinus Torvalds module_exit(ibmasm_exit);
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
2361da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
2371da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2381da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, ibmasm_pci_table);
2391da177e4SLinus Torvalds 
240