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>
17 * ASM service processor event handling routines.
19 * Events are signalled to the device drivers through interrupts.
20 * They have the format of dot commands, with the type field set to
22 * The driver does not interpret the events, it simply stores them in a
26 static void wake_up_event_readers(struct service_processor
*sp
)
28 struct event_reader
*reader
;
30 list_for_each_entry(reader
, &sp
->event_buffer
->readers
, node
)
31 wake_up_interruptible(&reader
->wait
);
36 * Called by the interrupt handler when a dot command of type sp_event is
38 * Store the event in the circular event buffer, wake up any sleeping
40 * There is no reader marker in the buffer, therefore readers are
41 * responsible for keeping up with the writer, or they will lose events.
43 void ibmasm_receive_event(struct service_processor
*sp
, void *data
, unsigned int data_size
)
45 struct event_buffer
*buffer
= sp
->event_buffer
;
46 struct ibmasm_event
*event
;
49 data_size
= min(data_size
, IBMASM_EVENT_MAX_SIZE
);
51 spin_lock_irqsave(&sp
->lock
, flags
);
52 /* copy the event into the next slot in the circular buffer */
53 event
= &buffer
->events
[buffer
->next_index
];
54 memcpy_fromio(event
->data
, data
, data_size
);
55 event
->data_size
= data_size
;
56 event
->serial_number
= buffer
->next_serial_number
;
58 /* advance indices in the buffer */
59 buffer
->next_index
= (buffer
->next_index
+ 1) % IBMASM_NUM_EVENTS
;
60 buffer
->next_serial_number
++;
61 spin_unlock_irqrestore(&sp
->lock
, flags
);
63 wake_up_event_readers(sp
);
66 static inline int event_available(struct event_buffer
*b
, struct event_reader
*r
)
68 return (r
->next_serial_number
< b
->next_serial_number
);
73 * Called by event readers (initiated from user space through the file
75 * Sleeps until a new event is available.
77 int ibmasm_get_next_event(struct service_processor
*sp
, struct event_reader
*reader
)
79 struct event_buffer
*buffer
= sp
->event_buffer
;
80 struct ibmasm_event
*event
;
84 reader
->cancelled
= 0;
86 if (wait_event_interruptible(reader
->wait
,
87 event_available(buffer
, reader
) || reader
->cancelled
))
90 if (!event_available(buffer
, reader
))
93 spin_lock_irqsave(&sp
->lock
, flags
);
95 index
= buffer
->next_index
;
96 event
= &buffer
->events
[index
];
97 while (event
->serial_number
< reader
->next_serial_number
) {
98 index
= (index
+ 1) % IBMASM_NUM_EVENTS
;
99 event
= &buffer
->events
[index
];
101 memcpy(reader
->data
, event
->data
, event
->data_size
);
102 reader
->data_size
= event
->data_size
;
103 reader
->next_serial_number
= event
->serial_number
+ 1;
105 spin_unlock_irqrestore(&sp
->lock
, flags
);
107 return event
->data_size
;
110 void ibmasm_cancel_next_event(struct event_reader
*reader
)
112 reader
->cancelled
= 1;
113 wake_up_interruptible(&reader
->wait
);
116 void ibmasm_event_reader_register(struct service_processor
*sp
, struct event_reader
*reader
)
120 reader
->next_serial_number
= sp
->event_buffer
->next_serial_number
;
121 init_waitqueue_head(&reader
->wait
);
122 spin_lock_irqsave(&sp
->lock
, flags
);
123 list_add(&reader
->node
, &sp
->event_buffer
->readers
);
124 spin_unlock_irqrestore(&sp
->lock
, flags
);
127 void ibmasm_event_reader_unregister(struct service_processor
*sp
, struct event_reader
*reader
)
131 spin_lock_irqsave(&sp
->lock
, flags
);
132 list_del(&reader
->node
);
133 spin_unlock_irqrestore(&sp
->lock
, flags
);
136 int ibmasm_event_buffer_init(struct service_processor
*sp
)
138 struct event_buffer
*buffer
;
139 struct ibmasm_event
*event
;
142 buffer
= kmalloc(sizeof(struct event_buffer
), GFP_KERNEL
);
146 buffer
->next_index
= 0;
147 buffer
->next_serial_number
= 1;
149 event
= buffer
->events
;
150 for (i
=0; i
<IBMASM_NUM_EVENTS
; i
++, event
++)
151 event
->serial_number
= 0;
153 INIT_LIST_HEAD(&buffer
->readers
);
155 sp
->event_buffer
= buffer
;
160 void ibmasm_event_buffer_exit(struct service_processor
*sp
)
162 kfree(sp
->event_buffer
);