11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds
31da177e4SLinus Torvalds /*
41da177e4SLinus Torvalds * IBM ASM Service Processor Device Driver
51da177e4SLinus Torvalds *
61da177e4SLinus Torvalds * Copyright (C) IBM Corporation, 2004
71da177e4SLinus Torvalds *
8d36b6910SAl Viro * Author: Max Asböck <amax@us.ibm.com>
91da177e4SLinus Torvalds *
101da177e4SLinus Torvalds * This driver is based on code originally written by Pete Reynolds
111da177e4SLinus Torvalds * and others.
121da177e4SLinus Torvalds */
131da177e4SLinus Torvalds
141da177e4SLinus Torvalds /*
151da177e4SLinus Torvalds * The ASM device driver does the following things:
161da177e4SLinus Torvalds *
171da177e4SLinus Torvalds * 1) When loaded it sends a message to the service processor,
181da177e4SLinus Torvalds * indicating that an OS is * running. This causes the service processor
191da177e4SLinus Torvalds * to send periodic heartbeats to the OS.
201da177e4SLinus Torvalds *
211da177e4SLinus Torvalds * 2) Answers the periodic heartbeats sent by the service processor.
221da177e4SLinus Torvalds * Failure to do so would result in system reboot.
231da177e4SLinus Torvalds *
241da177e4SLinus Torvalds * 3) Acts as a pass through for dot commands sent from user applications.
251da177e4SLinus Torvalds * The interface for this is the ibmasmfs file system.
261da177e4SLinus Torvalds *
271da177e4SLinus Torvalds * 4) Allows user applications to register for event notification. Events
281da177e4SLinus Torvalds * are sent to the driver through interrupts. They can be read from user
291da177e4SLinus Torvalds * space through the ibmasmfs file system.
301da177e4SLinus Torvalds *
311da177e4SLinus Torvalds * 5) Allows user space applications to send heartbeats to the service
321da177e4SLinus Torvalds * processor (aka reverse heartbeats). Again this happens through ibmasmfs.
331da177e4SLinus Torvalds *
341da177e4SLinus Torvalds * 6) Handles remote mouse and keyboard event interrupts and makes them
351da177e4SLinus Torvalds * available to user applications through ibmasmfs.
361da177e4SLinus Torvalds *
371da177e4SLinus Torvalds */
381da177e4SLinus Torvalds
391da177e4SLinus Torvalds #include <linux/pci.h>
401da177e4SLinus Torvalds #include <linux/init.h>
415a0e3ad6STejun Heo #include <linux/slab.h>
421da177e4SLinus Torvalds #include "ibmasm.h"
431da177e4SLinus Torvalds #include "lowlevel.h"
441da177e4SLinus Torvalds #include "remote.h"
451da177e4SLinus Torvalds
46278d72aeSMax Asbock int ibmasm_debug = 0;
47278d72aeSMax Asbock module_param(ibmasm_debug, int , S_IRUGO | S_IWUSR);
48278d72aeSMax Asbock MODULE_PARM_DESC(ibmasm_debug, " Set debug mode on or off");
49278d72aeSMax Asbock
501da177e4SLinus Torvalds
ibmasm_init_one(struct pci_dev * pdev,const struct pci_device_id * id)5180c8ae28SBill Pemberton static int ibmasm_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
521da177e4SLinus Torvalds {
53278d72aeSMax Asbock int result;
541da177e4SLinus Torvalds struct service_processor *sp;
551da177e4SLinus Torvalds
56278d72aeSMax Asbock if ((result = pci_enable_device(pdev))) {
57278d72aeSMax Asbock dev_err(&pdev->dev, "Failed to enable PCI device\n");
58278d72aeSMax Asbock return result;
591da177e4SLinus Torvalds }
60278d72aeSMax Asbock if ((result = pci_request_regions(pdev, DRIVER_NAME))) {
61278d72aeSMax Asbock dev_err(&pdev->dev, "Failed to allocate PCI resources\n");
62278d72aeSMax Asbock goto error_resources;
63278d72aeSMax Asbock }
64278d72aeSMax Asbock /* vnc client won't work without bus-mastering */
65278d72aeSMax Asbock pci_set_master(pdev);
661da177e4SLinus Torvalds
67dd00cc48SYoann Padioleau sp = kzalloc(sizeof(struct service_processor), GFP_KERNEL);
681da177e4SLinus Torvalds if (sp == NULL) {
691da177e4SLinus Torvalds dev_err(&pdev->dev, "Failed to allocate memory\n");
701da177e4SLinus Torvalds result = -ENOMEM;
711da177e4SLinus Torvalds goto error_kmalloc;
721da177e4SLinus Torvalds }
731da177e4SLinus Torvalds
7434af946aSIngo Molnar spin_lock_init(&sp->lock);
75278d72aeSMax Asbock INIT_LIST_HEAD(&sp->command_queue);
76278d72aeSMax Asbock
771da177e4SLinus Torvalds pci_set_drvdata(pdev, (void *)sp);
781da177e4SLinus Torvalds sp->dev = &pdev->dev;
791da177e4SLinus Torvalds sp->number = pdev->bus->number;
801da177e4SLinus Torvalds snprintf(sp->dirname, IBMASM_NAME_SIZE, "%d", sp->number);
811da177e4SLinus Torvalds snprintf(sp->devname, IBMASM_NAME_SIZE, "%s%d", DRIVER_NAME, sp->number);
821da177e4SLinus Torvalds
83f403f85fSAnton Vasilyev result = ibmasm_event_buffer_init(sp);
84f403f85fSAnton Vasilyev if (result) {
851da177e4SLinus Torvalds dev_err(sp->dev, "Failed to allocate event buffer\n");
861da177e4SLinus Torvalds goto error_eventbuffer;
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds
89f403f85fSAnton Vasilyev result = ibmasm_heartbeat_init(sp);
90f403f85fSAnton Vasilyev if (result) {
911da177e4SLinus Torvalds dev_err(sp->dev, "Failed to allocate heartbeat command\n");
921da177e4SLinus Torvalds goto error_heartbeat;
931da177e4SLinus Torvalds }
941da177e4SLinus Torvalds
951da177e4SLinus Torvalds sp->irq = pdev->irq;
96bdbeed75SArjan van de Ven sp->base_address = pci_ioremap_bar(pdev, 0);
975cf83b9bSAl Viro if (!sp->base_address) {
981da177e4SLinus Torvalds dev_err(sp->dev, "Failed to ioremap pci memory\n");
991da177e4SLinus Torvalds result = -ENODEV;
1001da177e4SLinus Torvalds goto error_ioremap;
1011da177e4SLinus Torvalds }
1021da177e4SLinus Torvalds
103dace1453SThomas Gleixner result = request_irq(sp->irq, ibmasm_interrupt_handler, IRQF_SHARED, sp->devname, (void*)sp);
1041da177e4SLinus Torvalds if (result) {
1051da177e4SLinus Torvalds dev_err(sp->dev, "Failed to register interrupt handler\n");
1061da177e4SLinus Torvalds goto error_request_irq;
1071da177e4SLinus Torvalds }
1081da177e4SLinus Torvalds
1091da177e4SLinus Torvalds enable_sp_interrupts(sp->base_address);
110278d72aeSMax Asbock
111278d72aeSMax Asbock result = ibmasm_init_remote_input_dev(sp);
112278d72aeSMax Asbock if (result) {
113278d72aeSMax Asbock dev_err(sp->dev, "Failed to initialize remote queue\n");
114*7272b591SLv Yunlong goto error_init_remote;
115278d72aeSMax Asbock }
1161da177e4SLinus Torvalds
1171da177e4SLinus Torvalds result = ibmasm_send_driver_vpd(sp);
1181da177e4SLinus Torvalds if (result) {
1191da177e4SLinus Torvalds dev_err(sp->dev, "Failed to send driver VPD to service processor\n");
1201da177e4SLinus Torvalds goto error_send_message;
1211da177e4SLinus Torvalds }
1221da177e4SLinus Torvalds result = ibmasm_send_os_state(sp, SYSTEM_STATE_OS_UP);
1231da177e4SLinus Torvalds if (result) {
1241da177e4SLinus Torvalds dev_err(sp->dev, "Failed to send OS state to service processor\n");
1251da177e4SLinus Torvalds goto error_send_message;
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds ibmasmfs_add_sp(sp);
1281da177e4SLinus Torvalds
1291da177e4SLinus Torvalds ibmasm_register_uart(sp);
1301da177e4SLinus Torvalds
1311da177e4SLinus Torvalds return 0;
1321da177e4SLinus Torvalds
1331da177e4SLinus Torvalds error_send_message:
134278d72aeSMax Asbock ibmasm_free_remote_input_dev(sp);
135*7272b591SLv Yunlong error_init_remote:
136*7272b591SLv Yunlong disable_sp_interrupts(sp->base_address);
1371da177e4SLinus Torvalds free_irq(sp->irq, (void *)sp);
1381da177e4SLinus Torvalds error_request_irq:
1391da177e4SLinus Torvalds iounmap(sp->base_address);
1401da177e4SLinus Torvalds error_ioremap:
1411da177e4SLinus Torvalds ibmasm_heartbeat_exit(sp);
1421da177e4SLinus Torvalds error_heartbeat:
1431da177e4SLinus Torvalds ibmasm_event_buffer_exit(sp);
1441da177e4SLinus Torvalds error_eventbuffer:
1451da177e4SLinus Torvalds kfree(sp);
1461da177e4SLinus Torvalds error_kmalloc:
147278d72aeSMax Asbock pci_release_regions(pdev);
148278d72aeSMax Asbock error_resources:
1491da177e4SLinus Torvalds pci_disable_device(pdev);
1501da177e4SLinus Torvalds
1511da177e4SLinus Torvalds return result;
1521da177e4SLinus Torvalds }
1531da177e4SLinus Torvalds
ibmasm_remove_one(struct pci_dev * pdev)154486a5c28SBill Pemberton static void ibmasm_remove_one(struct pci_dev *pdev)
1551da177e4SLinus Torvalds {
156bbf831dfSJingoo Han struct service_processor *sp = pci_get_drvdata(pdev);
1571da177e4SLinus Torvalds
158278d72aeSMax Asbock dbg("Unregistering UART\n");
1591da177e4SLinus Torvalds ibmasm_unregister_uart(sp);
160278d72aeSMax Asbock dbg("Sending OS down message\n");
161278d72aeSMax Asbock if (ibmasm_send_os_state(sp, SYSTEM_STATE_OS_DOWN))
162ecbf1288SColin Ian King err("failed to get response to 'Send OS State' command\n");
163278d72aeSMax Asbock dbg("Disabling heartbeats\n");
1641da177e4SLinus Torvalds ibmasm_heartbeat_exit(sp);
165278d72aeSMax Asbock dbg("Disabling interrupts\n");
166278d72aeSMax Asbock disable_sp_interrupts(sp->base_address);
167278d72aeSMax Asbock dbg("Freeing SP irq\n");
168278d72aeSMax Asbock free_irq(sp->irq, (void *)sp);
169278d72aeSMax Asbock dbg("Cleaning up\n");
170278d72aeSMax Asbock ibmasm_free_remote_input_dev(sp);
1711da177e4SLinus Torvalds iounmap(sp->base_address);
1721da177e4SLinus Torvalds ibmasm_event_buffer_exit(sp);
1731da177e4SLinus Torvalds kfree(sp);
174278d72aeSMax Asbock pci_release_regions(pdev);
1751da177e4SLinus Torvalds pci_disable_device(pdev);
1761da177e4SLinus Torvalds }
1771da177e4SLinus Torvalds
1781da177e4SLinus Torvalds static struct pci_device_id ibmasm_pci_table[] =
1791da177e4SLinus Torvalds {
1801da177e4SLinus Torvalds { PCI_DEVICE(VENDORID_IBM, DEVICEID_RSA) },
1811da177e4SLinus Torvalds {},
1821da177e4SLinus Torvalds };
1831da177e4SLinus Torvalds
1841da177e4SLinus Torvalds static struct pci_driver ibmasm_driver = {
1851da177e4SLinus Torvalds .name = DRIVER_NAME,
1861da177e4SLinus Torvalds .id_table = ibmasm_pci_table,
1871da177e4SLinus Torvalds .probe = ibmasm_init_one,
1882d6bed9cSBill Pemberton .remove = ibmasm_remove_one,
1891da177e4SLinus Torvalds };
1901da177e4SLinus Torvalds
ibmasm_exit(void)1911da177e4SLinus Torvalds static void __exit ibmasm_exit (void)
1921da177e4SLinus Torvalds {
1931da177e4SLinus Torvalds ibmasm_unregister_panic_notifier();
1941da177e4SLinus Torvalds ibmasmfs_unregister();
1951da177e4SLinus Torvalds pci_unregister_driver(&ibmasm_driver);
1961da177e4SLinus Torvalds info(DRIVER_DESC " version " DRIVER_VERSION " unloaded");
1971da177e4SLinus Torvalds }
1981da177e4SLinus Torvalds
ibmasm_init(void)1991da177e4SLinus Torvalds static int __init ibmasm_init(void)
2001da177e4SLinus Torvalds {
201ea23b453SAl Viro int result = pci_register_driver(&ibmasm_driver);
202ea23b453SAl Viro if (result)
203ea23b453SAl Viro return result;
2041da177e4SLinus Torvalds
2051da177e4SLinus Torvalds result = ibmasmfs_register();
2061da177e4SLinus Torvalds if (result) {
207ea23b453SAl Viro pci_unregister_driver(&ibmasm_driver);
2081da177e4SLinus Torvalds err("Failed to register ibmasmfs file system");
2091da177e4SLinus Torvalds return result;
2101da177e4SLinus Torvalds }
211ea23b453SAl Viro
2121da177e4SLinus Torvalds ibmasm_register_panic_notifier();
2131da177e4SLinus Torvalds info(DRIVER_DESC " version " DRIVER_VERSION " loaded");
2141da177e4SLinus Torvalds return 0;
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds
2171da177e4SLinus Torvalds module_init(ibmasm_init);
2181da177e4SLinus Torvalds module_exit(ibmasm_exit);
2191da177e4SLinus Torvalds
2201da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
2211da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
2221da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2231da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, ibmasm_pci_table);
2241da177e4SLinus Torvalds
225