Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * IBM ASM Service Processor Device Driver |
| 5 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Copyright (C) IBM Corporation, 2004 |
| 7 | * |
Al Viro | d36b691 | 2011-12-29 17:09:01 -0500 | [diff] [blame] | 8 | * Author: Max Asböck <amax@us.ibm.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * |
Dmitry Torokhov | 3110dc7 | 2007-07-17 04:03:58 -0700 | [diff] [blame] | 10 | * This driver is based on code originally written by Pete Reynolds |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * and others. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | /* |
| 15 | * The ASM device driver does the following things: |
| 16 | * |
| 17 | * 1) When loaded it sends a message to the service processor, |
| 18 | * indicating that an OS is * running. This causes the service processor |
Dmitry Torokhov | 3110dc7 | 2007-07-17 04:03:58 -0700 | [diff] [blame] | 19 | * to send periodic heartbeats to the OS. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | * |
| 21 | * 2) Answers the periodic heartbeats sent by the service processor. |
| 22 | * Failure to do so would result in system reboot. |
| 23 | * |
| 24 | * 3) Acts as a pass through for dot commands sent from user applications. |
Dmitry Torokhov | 3110dc7 | 2007-07-17 04:03:58 -0700 | [diff] [blame] | 25 | * The interface for this is the ibmasmfs file system. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | * |
| 27 | * 4) Allows user applications to register for event notification. Events |
| 28 | * are sent to the driver through interrupts. They can be read from user |
| 29 | * space through the ibmasmfs file system. |
| 30 | * |
| 31 | * 5) Allows user space applications to send heartbeats to the service |
| 32 | * processor (aka reverse heartbeats). Again this happens through ibmasmfs. |
| 33 | * |
| 34 | * 6) Handles remote mouse and keyboard event interrupts and makes them |
| 35 | * available to user applications through ibmasmfs. |
| 36 | * |
| 37 | */ |
| 38 | |
| 39 | #include <linux/pci.h> |
| 40 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 41 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include "ibmasm.h" |
| 43 | #include "lowlevel.h" |
| 44 | #include "remote.h" |
| 45 | |
Max Asbock | 278d72a | 2005-06-21 17:16:34 -0700 | [diff] [blame] | 46 | int ibmasm_debug = 0; |
| 47 | module_param(ibmasm_debug, int , S_IRUGO | S_IWUSR); |
| 48 | MODULE_PARM_DESC(ibmasm_debug, " Set debug mode on or off"); |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Bill Pemberton | 80c8ae2 | 2012-11-19 13:23:05 -0500 | [diff] [blame] | 51 | static int ibmasm_init_one(struct pci_dev *pdev, const struct pci_device_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
Max Asbock | 278d72a | 2005-06-21 17:16:34 -0700 | [diff] [blame] | 53 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | struct service_processor *sp; |
| 55 | |
Max Asbock | 278d72a | 2005-06-21 17:16:34 -0700 | [diff] [blame] | 56 | if ((result = pci_enable_device(pdev))) { |
| 57 | dev_err(&pdev->dev, "Failed to enable PCI device\n"); |
| 58 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } |
Max Asbock | 278d72a | 2005-06-21 17:16:34 -0700 | [diff] [blame] | 60 | if ((result = pci_request_regions(pdev, DRIVER_NAME))) { |
| 61 | dev_err(&pdev->dev, "Failed to allocate PCI resources\n"); |
| 62 | goto error_resources; |
| 63 | } |
| 64 | /* vnc client won't work without bus-mastering */ |
| 65 | pci_set_master(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 67 | sp = kzalloc(sizeof(struct service_processor), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | if (sp == NULL) { |
| 69 | dev_err(&pdev->dev, "Failed to allocate memory\n"); |
| 70 | result = -ENOMEM; |
| 71 | goto error_kmalloc; |
| 72 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Ingo Molnar | 34af946 | 2006-06-27 02:53:55 -0700 | [diff] [blame] | 74 | spin_lock_init(&sp->lock); |
Max Asbock | 278d72a | 2005-06-21 17:16:34 -0700 | [diff] [blame] | 75 | INIT_LIST_HEAD(&sp->command_queue); |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | pci_set_drvdata(pdev, (void *)sp); |
| 78 | sp->dev = &pdev->dev; |
| 79 | sp->number = pdev->bus->number; |
| 80 | snprintf(sp->dirname, IBMASM_NAME_SIZE, "%d", sp->number); |
| 81 | snprintf(sp->devname, IBMASM_NAME_SIZE, "%s%d", DRIVER_NAME, sp->number); |
| 82 | |
Anton Vasilyev | f403f85f | 2017-08-01 16:25:59 +0300 | [diff] [blame] | 83 | result = ibmasm_event_buffer_init(sp); |
| 84 | if (result) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | dev_err(sp->dev, "Failed to allocate event buffer\n"); |
| 86 | goto error_eventbuffer; |
| 87 | } |
| 88 | |
Anton Vasilyev | f403f85f | 2017-08-01 16:25:59 +0300 | [diff] [blame] | 89 | result = ibmasm_heartbeat_init(sp); |
| 90 | if (result) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | dev_err(sp->dev, "Failed to allocate heartbeat command\n"); |
| 92 | goto error_heartbeat; |
| 93 | } |
| 94 | |
| 95 | sp->irq = pdev->irq; |
Arjan van de Ven | bdbeed7 | 2009-01-06 14:40:39 -0800 | [diff] [blame] | 96 | sp->base_address = pci_ioremap_bar(pdev, 0); |
Al Viro | 5cf83b9 | 2008-03-29 03:07:48 +0000 | [diff] [blame] | 97 | if (!sp->base_address) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | dev_err(sp->dev, "Failed to ioremap pci memory\n"); |
| 99 | result = -ENODEV; |
| 100 | goto error_ioremap; |
| 101 | } |
| 102 | |
Thomas Gleixner | dace145 | 2006-07-01 19:29:38 -0700 | [diff] [blame] | 103 | result = request_irq(sp->irq, ibmasm_interrupt_handler, IRQF_SHARED, sp->devname, (void*)sp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | if (result) { |
| 105 | dev_err(sp->dev, "Failed to register interrupt handler\n"); |
| 106 | goto error_request_irq; |
| 107 | } |
| 108 | |
| 109 | enable_sp_interrupts(sp->base_address); |
Max Asbock | 278d72a | 2005-06-21 17:16:34 -0700 | [diff] [blame] | 110 | |
| 111 | result = ibmasm_init_remote_input_dev(sp); |
| 112 | if (result) { |
| 113 | dev_err(sp->dev, "Failed to initialize remote queue\n"); |
| 114 | goto error_send_message; |
| 115 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
| 117 | result = ibmasm_send_driver_vpd(sp); |
| 118 | if (result) { |
| 119 | dev_err(sp->dev, "Failed to send driver VPD to service processor\n"); |
| 120 | goto error_send_message; |
| 121 | } |
| 122 | result = ibmasm_send_os_state(sp, SYSTEM_STATE_OS_UP); |
| 123 | if (result) { |
| 124 | dev_err(sp->dev, "Failed to send OS state to service processor\n"); |
| 125 | goto error_send_message; |
| 126 | } |
| 127 | ibmasmfs_add_sp(sp); |
| 128 | |
| 129 | ibmasm_register_uart(sp); |
| 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | return 0; |
| 132 | |
| 133 | error_send_message: |
| 134 | disable_sp_interrupts(sp->base_address); |
Max Asbock | 278d72a | 2005-06-21 17:16:34 -0700 | [diff] [blame] | 135 | ibmasm_free_remote_input_dev(sp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | free_irq(sp->irq, (void *)sp); |
| 137 | error_request_irq: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | iounmap(sp->base_address); |
| 139 | error_ioremap: |
| 140 | ibmasm_heartbeat_exit(sp); |
| 141 | error_heartbeat: |
| 142 | ibmasm_event_buffer_exit(sp); |
| 143 | error_eventbuffer: |
| 144 | kfree(sp); |
| 145 | error_kmalloc: |
Max Asbock | 278d72a | 2005-06-21 17:16:34 -0700 | [diff] [blame] | 146 | pci_release_regions(pdev); |
| 147 | error_resources: |
| 148 | pci_disable_device(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
| 150 | return result; |
| 151 | } |
| 152 | |
Bill Pemberton | 486a5c2 | 2012-11-19 13:26:02 -0500 | [diff] [blame] | 153 | static void ibmasm_remove_one(struct pci_dev *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
Jingoo Han | bbf831d | 2013-09-09 14:15:52 +0900 | [diff] [blame] | 155 | struct service_processor *sp = pci_get_drvdata(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
Max Asbock | 278d72a | 2005-06-21 17:16:34 -0700 | [diff] [blame] | 157 | dbg("Unregistering UART\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | ibmasm_unregister_uart(sp); |
Max Asbock | 278d72a | 2005-06-21 17:16:34 -0700 | [diff] [blame] | 159 | dbg("Sending OS down message\n"); |
| 160 | if (ibmasm_send_os_state(sp, SYSTEM_STATE_OS_DOWN)) |
Colin Ian King | ecbf128 | 2016-11-12 17:54:43 +0000 | [diff] [blame] | 161 | err("failed to get response to 'Send OS State' command\n"); |
Max Asbock | 278d72a | 2005-06-21 17:16:34 -0700 | [diff] [blame] | 162 | dbg("Disabling heartbeats\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | ibmasm_heartbeat_exit(sp); |
Max Asbock | 278d72a | 2005-06-21 17:16:34 -0700 | [diff] [blame] | 164 | dbg("Disabling interrupts\n"); |
| 165 | disable_sp_interrupts(sp->base_address); |
| 166 | dbg("Freeing SP irq\n"); |
| 167 | free_irq(sp->irq, (void *)sp); |
| 168 | dbg("Cleaning up\n"); |
| 169 | ibmasm_free_remote_input_dev(sp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | iounmap(sp->base_address); |
| 171 | ibmasm_event_buffer_exit(sp); |
| 172 | kfree(sp); |
Max Asbock | 278d72a | 2005-06-21 17:16:34 -0700 | [diff] [blame] | 173 | pci_release_regions(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | pci_disable_device(pdev); |
| 175 | } |
| 176 | |
| 177 | static struct pci_device_id ibmasm_pci_table[] = |
| 178 | { |
| 179 | { PCI_DEVICE(VENDORID_IBM, DEVICEID_RSA) }, |
| 180 | {}, |
| 181 | }; |
| 182 | |
| 183 | static struct pci_driver ibmasm_driver = { |
| 184 | .name = DRIVER_NAME, |
| 185 | .id_table = ibmasm_pci_table, |
| 186 | .probe = ibmasm_init_one, |
Bill Pemberton | 2d6bed9 | 2012-11-19 13:21:23 -0500 | [diff] [blame] | 187 | .remove = ibmasm_remove_one, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | static void __exit ibmasm_exit (void) |
| 191 | { |
| 192 | ibmasm_unregister_panic_notifier(); |
| 193 | ibmasmfs_unregister(); |
| 194 | pci_unregister_driver(&ibmasm_driver); |
| 195 | info(DRIVER_DESC " version " DRIVER_VERSION " unloaded"); |
| 196 | } |
| 197 | |
| 198 | static int __init ibmasm_init(void) |
| 199 | { |
Al Viro | ea23b453 | 2012-03-17 01:50:32 -0400 | [diff] [blame] | 200 | int result = pci_register_driver(&ibmasm_driver); |
| 201 | if (result) |
| 202 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
| 204 | result = ibmasmfs_register(); |
| 205 | if (result) { |
Al Viro | ea23b453 | 2012-03-17 01:50:32 -0400 | [diff] [blame] | 206 | pci_unregister_driver(&ibmasm_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | err("Failed to register ibmasmfs file system"); |
| 208 | return result; |
| 209 | } |
Al Viro | ea23b453 | 2012-03-17 01:50:32 -0400 | [diff] [blame] | 210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | ibmasm_register_panic_notifier(); |
| 212 | info(DRIVER_DESC " version " DRIVER_VERSION " loaded"); |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | module_init(ibmasm_init); |
| 217 | module_exit(ibmasm_exit); |
| 218 | |
| 219 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 220 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 221 | MODULE_LICENSE("GPL"); |
| 222 | MODULE_DEVICE_TABLE(pci, ibmasm_pci_table); |
| 223 | |