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