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 | */ |
| 10 | |
Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame] | 11 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "ibmasm.h" |
Max Asbock | 8818760 | 2005-06-21 17:16:36 -0700 | [diff] [blame] | 14 | #include "lowlevel.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | static void exec_next_command(struct service_processor *sp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Max Asbock | 8818760 | 2005-06-21 17:16:36 -0700 | [diff] [blame] | 18 | static atomic_t command_count = ATOMIC_INIT(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Max Asbock | 8818760 | 2005-06-21 17:16:36 -0700 | [diff] [blame] | 20 | struct command *ibmasm_new_command(struct service_processor *sp, size_t buffer_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | { |
| 22 | struct command *cmd; |
| 23 | |
| 24 | if (buffer_size > IBMASM_CMD_MAX_BUFFER_SIZE) |
| 25 | return NULL; |
| 26 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 27 | cmd = kzalloc(sizeof(struct command), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | if (cmd == NULL) |
| 29 | return NULL; |
| 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 32 | cmd->buffer = kzalloc(buffer_size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | if (cmd->buffer == NULL) { |
| 34 | kfree(cmd); |
| 35 | return NULL; |
| 36 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | cmd->buffer_size = buffer_size; |
| 38 | |
Greg Kroah-Hartman | a045171 | 2007-12-03 21:16:20 -0700 | [diff] [blame] | 39 | kref_init(&cmd->kref); |
Max Asbock | 8818760 | 2005-06-21 17:16:36 -0700 | [diff] [blame] | 40 | cmd->lock = &sp->lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | cmd->status = IBMASM_CMD_PENDING; |
| 43 | init_waitqueue_head(&cmd->wait); |
| 44 | INIT_LIST_HEAD(&cmd->queue_node); |
| 45 | |
Max Asbock | 8818760 | 2005-06-21 17:16:36 -0700 | [diff] [blame] | 46 | atomic_inc(&command_count); |
| 47 | dbg("command count: %d\n", atomic_read(&command_count)); |
| 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | return cmd; |
| 50 | } |
| 51 | |
Greg Kroah-Hartman | a045171 | 2007-12-03 21:16:20 -0700 | [diff] [blame] | 52 | void ibmasm_free_command(struct kref *kref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | { |
Greg Kroah-Hartman | a045171 | 2007-12-03 21:16:20 -0700 | [diff] [blame] | 54 | struct command *cmd = to_command(kref); |
Dmitry Torokhov | 3110dc7 | 2007-07-17 04:03:58 -0700 | [diff] [blame] | 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | list_del(&cmd->queue_node); |
Max Asbock | 8818760 | 2005-06-21 17:16:36 -0700 | [diff] [blame] | 57 | atomic_dec(&command_count); |
| 58 | dbg("command count: %d\n", atomic_read(&command_count)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | kfree(cmd->buffer); |
| 60 | kfree(cmd); |
| 61 | } |
| 62 | |
| 63 | static void enqueue_command(struct service_processor *sp, struct command *cmd) |
| 64 | { |
| 65 | list_add_tail(&cmd->queue_node, &sp->command_queue); |
| 66 | } |
| 67 | |
| 68 | static struct command *dequeue_command(struct service_processor *sp) |
| 69 | { |
| 70 | struct command *cmd; |
| 71 | struct list_head *next; |
| 72 | |
| 73 | if (list_empty(&sp->command_queue)) |
| 74 | return NULL; |
| 75 | |
| 76 | next = sp->command_queue.next; |
| 77 | list_del_init(next); |
| 78 | cmd = list_entry(next, struct command, queue_node); |
| 79 | |
| 80 | return cmd; |
| 81 | } |
| 82 | |
| 83 | static inline void do_exec_command(struct service_processor *sp) |
| 84 | { |
Max Asbock | 8818760 | 2005-06-21 17:16:36 -0700 | [diff] [blame] | 85 | char tsbuf[32]; |
| 86 | |
Harvey Harrison | 6e57419 | 2008-04-29 00:59:20 -0700 | [diff] [blame] | 87 | dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf)); |
Max Asbock | 8818760 | 2005-06-21 17:16:36 -0700 | [diff] [blame] | 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | if (ibmasm_send_i2o_message(sp)) { |
| 90 | sp->current_command->status = IBMASM_CMD_FAILED; |
Max Asbock | 8818760 | 2005-06-21 17:16:36 -0700 | [diff] [blame] | 91 | wake_up(&sp->current_command->wait); |
| 92 | command_put(sp->current_command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | exec_next_command(sp); |
| 94 | } |
| 95 | } |
Dmitry Torokhov | 3110dc7 | 2007-07-17 04:03:58 -0700 | [diff] [blame] | 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | /** |
| 98 | * exec_command |
| 99 | * send a command to a service processor |
| 100 | * Commands are executed sequentially. One command (sp->current_command) |
| 101 | * is sent to the service processor. Once the interrupt handler gets a |
| 102 | * message of type command_response, the message is copied into |
Dmitry Torokhov | 3110dc7 | 2007-07-17 04:03:58 -0700 | [diff] [blame] | 103 | * the current commands buffer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | */ |
| 105 | void ibmasm_exec_command(struct service_processor *sp, struct command *cmd) |
| 106 | { |
| 107 | unsigned long flags; |
Max Asbock | 8818760 | 2005-06-21 17:16:36 -0700 | [diff] [blame] | 108 | char tsbuf[32]; |
| 109 | |
Harvey Harrison | 6e57419 | 2008-04-29 00:59:20 -0700 | [diff] [blame] | 110 | dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | spin_lock_irqsave(&sp->lock, flags); |
| 113 | |
| 114 | if (!sp->current_command) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | sp->current_command = cmd; |
Max Asbock | 8818760 | 2005-06-21 17:16:36 -0700 | [diff] [blame] | 116 | command_get(sp->current_command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | spin_unlock_irqrestore(&sp->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | do_exec_command(sp); |
| 119 | } else { |
| 120 | enqueue_command(sp, cmd); |
| 121 | spin_unlock_irqrestore(&sp->lock, flags); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | static void exec_next_command(struct service_processor *sp) |
| 126 | { |
| 127 | unsigned long flags; |
Max Asbock | 8818760 | 2005-06-21 17:16:36 -0700 | [diff] [blame] | 128 | char tsbuf[32]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Harvey Harrison | 6e57419 | 2008-04-29 00:59:20 -0700 | [diff] [blame] | 130 | dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
| 132 | spin_lock_irqsave(&sp->lock, flags); |
| 133 | sp->current_command = dequeue_command(sp); |
| 134 | if (sp->current_command) { |
| 135 | command_get(sp->current_command); |
| 136 | spin_unlock_irqrestore(&sp->lock, flags); |
| 137 | do_exec_command(sp); |
| 138 | } else { |
| 139 | spin_unlock_irqrestore(&sp->lock, flags); |
| 140 | } |
| 141 | } |
| 142 | |
Dmitry Torokhov | 3110dc7 | 2007-07-17 04:03:58 -0700 | [diff] [blame] | 143 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | * Sleep until a command has failed or a response has been received |
| 145 | * and the command status been updated by the interrupt handler. |
| 146 | * (see receive_response). |
| 147 | */ |
| 148 | void ibmasm_wait_for_response(struct command *cmd, int timeout) |
| 149 | { |
| 150 | wait_event_interruptible_timeout(cmd->wait, |
| 151 | cmd->status == IBMASM_CMD_COMPLETE || |
| 152 | cmd->status == IBMASM_CMD_FAILED, |
| 153 | timeout * HZ); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * receive_command_response |
| 158 | * called by the interrupt handler when a dot command of type command_response |
| 159 | * was received. |
| 160 | */ |
| 161 | void ibmasm_receive_command_response(struct service_processor *sp, void *response, size_t size) |
| 162 | { |
| 163 | struct command *cmd = sp->current_command; |
| 164 | |
Dmitry Torokhov | 3110dc7 | 2007-07-17 04:03:58 -0700 | [diff] [blame] | 165 | if (!sp->current_command) |
| 166 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
Max Asbock | 8818760 | 2005-06-21 17:16:36 -0700 | [diff] [blame] | 168 | memcpy_fromio(cmd->buffer, response, min(size, cmd->buffer_size)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | cmd->status = IBMASM_CMD_COMPLETE; |
Max Asbock | 8818760 | 2005-06-21 17:16:36 -0700 | [diff] [blame] | 170 | wake_up(&sp->current_command->wait); |
| 171 | command_put(sp->current_command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | exec_next_command(sp); |
| 173 | } |