1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * IBM ASM Service Processor Device Driver
6 * Copyright (C) IBM Corporation, 2004
8 * Author: Max Asböck <amax@us.ibm.com>
11 #include <linux/sched.h>
12 #include <linux/slab.h>
16 static void exec_next_command(struct service_processor
*sp
);
18 static atomic_t command_count
= ATOMIC_INIT(0);
20 struct command
*ibmasm_new_command(struct service_processor
*sp
, size_t buffer_size
)
24 if (buffer_size
> IBMASM_CMD_MAX_BUFFER_SIZE
)
27 cmd
= kzalloc(sizeof(struct command
), GFP_KERNEL
);
32 cmd
->buffer
= kzalloc(buffer_size
, GFP_KERNEL
);
33 if (cmd
->buffer
== NULL
) {
37 cmd
->buffer_size
= buffer_size
;
39 kref_init(&cmd
->kref
);
40 cmd
->lock
= &sp
->lock
;
42 cmd
->status
= IBMASM_CMD_PENDING
;
43 init_waitqueue_head(&cmd
->wait
);
44 INIT_LIST_HEAD(&cmd
->queue_node
);
46 atomic_inc(&command_count
);
47 dbg("command count: %d\n", atomic_read(&command_count
));
52 void ibmasm_free_command(struct kref
*kref
)
54 struct command
*cmd
= to_command(kref
);
56 list_del(&cmd
->queue_node
);
57 atomic_dec(&command_count
);
58 dbg("command count: %d\n", atomic_read(&command_count
));
63 static void enqueue_command(struct service_processor
*sp
, struct command
*cmd
)
65 list_add_tail(&cmd
->queue_node
, &sp
->command_queue
);
68 static struct command
*dequeue_command(struct service_processor
*sp
)
71 struct list_head
*next
;
73 if (list_empty(&sp
->command_queue
))
76 next
= sp
->command_queue
.next
;
78 cmd
= list_entry(next
, struct command
, queue_node
);
83 static inline void do_exec_command(struct service_processor
*sp
)
87 dbg("%s:%d at %s\n", __func__
, __LINE__
, get_timestamp(tsbuf
));
89 if (ibmasm_send_i2o_message(sp
)) {
90 sp
->current_command
->status
= IBMASM_CMD_FAILED
;
91 wake_up(&sp
->current_command
->wait
);
92 command_put(sp
->current_command
);
93 exec_next_command(sp
);
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
103 * the current commands buffer,
105 void ibmasm_exec_command(struct service_processor
*sp
, struct command
*cmd
)
110 dbg("%s:%d at %s\n", __func__
, __LINE__
, get_timestamp(tsbuf
));
112 spin_lock_irqsave(&sp
->lock
, flags
);
114 if (!sp
->current_command
) {
115 sp
->current_command
= cmd
;
116 command_get(sp
->current_command
);
117 spin_unlock_irqrestore(&sp
->lock
, flags
);
120 enqueue_command(sp
, cmd
);
121 spin_unlock_irqrestore(&sp
->lock
, flags
);
125 static void exec_next_command(struct service_processor
*sp
)
130 dbg("%s:%d at %s\n", __func__
, __LINE__
, get_timestamp(tsbuf
));
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
);
139 spin_unlock_irqrestore(&sp
->lock
, flags
);
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).
148 void ibmasm_wait_for_response(struct command
*cmd
, int timeout
)
150 wait_event_interruptible_timeout(cmd
->wait
,
151 cmd
->status
== IBMASM_CMD_COMPLETE
||
152 cmd
->status
== IBMASM_CMD_FAILED
,
157 * receive_command_response
158 * called by the interrupt handler when a dot command of type command_response
161 void ibmasm_receive_command_response(struct service_processor
*sp
, void *response
, size_t size
)
163 struct command
*cmd
= sp
->current_command
;
165 if (!sp
->current_command
)
168 memcpy_fromio(cmd
->buffer
, response
, min(size
, cmd
->buffer_size
));
169 cmd
->status
= IBMASM_CMD_COMPLETE
;
170 wake_up(&sp
->current_command
->wait
);
171 command_put(sp
->current_command
);
172 exec_next_command(sp
);