blob: 2863657fa268343dcef68c9926d226aa1d2a09dd [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002
3/*
4 * IBM ASM Service Processor Device Driver
5 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (C) IBM Corporation, 2004
7 *
Al Virod36b6912011-12-29 17:09:01 -05008 * Author: Max Asböck <amax@us.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040011#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "ibmasm.h"
Max Asbock88187602005-06-21 17:16:36 -070014#include "lowlevel.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16static void exec_next_command(struct service_processor *sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Max Asbock88187602005-06-21 17:16:36 -070018static atomic_t command_count = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Max Asbock88187602005-06-21 17:16:36 -070020struct command *ibmasm_new_command(struct service_processor *sp, size_t buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
22 struct command *cmd;
23
24 if (buffer_size > IBMASM_CMD_MAX_BUFFER_SIZE)
25 return NULL;
26
Yoann Padioleaudd00cc42007-07-19 01:49:03 -070027 cmd = kzalloc(sizeof(struct command), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 if (cmd == NULL)
29 return NULL;
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Yoann Padioleaudd00cc42007-07-19 01:49:03 -070032 cmd->buffer = kzalloc(buffer_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 if (cmd->buffer == NULL) {
34 kfree(cmd);
35 return NULL;
36 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 cmd->buffer_size = buffer_size;
38
Greg Kroah-Hartmana0451712007-12-03 21:16:20 -070039 kref_init(&cmd->kref);
Max Asbock88187602005-06-21 17:16:36 -070040 cmd->lock = &sp->lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42 cmd->status = IBMASM_CMD_PENDING;
43 init_waitqueue_head(&cmd->wait);
44 INIT_LIST_HEAD(&cmd->queue_node);
45
Max Asbock88187602005-06-21 17:16:36 -070046 atomic_inc(&command_count);
47 dbg("command count: %d\n", atomic_read(&command_count));
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 return cmd;
50}
51
Greg Kroah-Hartmana0451712007-12-03 21:16:20 -070052void ibmasm_free_command(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Greg Kroah-Hartmana0451712007-12-03 21:16:20 -070054 struct command *cmd = to_command(kref);
Dmitry Torokhov3110dc72007-07-17 04:03:58 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 list_del(&cmd->queue_node);
Max Asbock88187602005-06-21 17:16:36 -070057 atomic_dec(&command_count);
58 dbg("command count: %d\n", atomic_read(&command_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 kfree(cmd->buffer);
60 kfree(cmd);
61}
62
63static void enqueue_command(struct service_processor *sp, struct command *cmd)
64{
65 list_add_tail(&cmd->queue_node, &sp->command_queue);
66}
67
68static 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
83static inline void do_exec_command(struct service_processor *sp)
84{
Max Asbock88187602005-06-21 17:16:36 -070085 char tsbuf[32];
86
Harvey Harrison6e574192008-04-29 00:59:20 -070087 dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf));
Max Asbock88187602005-06-21 17:16:36 -070088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 if (ibmasm_send_i2o_message(sp)) {
90 sp->current_command->status = IBMASM_CMD_FAILED;
Max Asbock88187602005-06-21 17:16:36 -070091 wake_up(&sp->current_command->wait);
92 command_put(sp->current_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 exec_next_command(sp);
94 }
95}
Dmitry Torokhov3110dc72007-07-17 04:03:58 -070096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/**
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 Torokhov3110dc72007-07-17 04:03:58 -0700103 * the current commands buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 */
105void ibmasm_exec_command(struct service_processor *sp, struct command *cmd)
106{
107 unsigned long flags;
Max Asbock88187602005-06-21 17:16:36 -0700108 char tsbuf[32];
109
Harvey Harrison6e574192008-04-29 00:59:20 -0700110 dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 spin_lock_irqsave(&sp->lock, flags);
113
114 if (!sp->current_command) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 sp->current_command = cmd;
Max Asbock88187602005-06-21 17:16:36 -0700116 command_get(sp->current_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 spin_unlock_irqrestore(&sp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 do_exec_command(sp);
119 } else {
120 enqueue_command(sp, cmd);
121 spin_unlock_irqrestore(&sp->lock, flags);
122 }
123}
124
125static void exec_next_command(struct service_processor *sp)
126{
127 unsigned long flags;
Max Asbock88187602005-06-21 17:16:36 -0700128 char tsbuf[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Harvey Harrison6e574192008-04-29 00:59:20 -0700130 dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
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 Torokhov3110dc72007-07-17 04:03:58 -0700143/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 * 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 */
148void 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 */
161void ibmasm_receive_command_response(struct service_processor *sp, void *response, size_t size)
162{
163 struct command *cmd = sp->current_command;
164
Dmitry Torokhov3110dc72007-07-17 04:03:58 -0700165 if (!sp->current_command)
166 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Max Asbock88187602005-06-21 17:16:36 -0700168 memcpy_fromio(cmd->buffer, response, min(size, cmd->buffer_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 cmd->status = IBMASM_CMD_COMPLETE;
Max Asbock88187602005-06-21 17:16:36 -0700170 wake_up(&sp->current_command->wait);
171 command_put(sp->current_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 exec_next_command(sp);
173}