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