treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / misc / ibmasm / event.c
blob974d63f5a4ddae49dd19f922807c21c528c1c6f1
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /*
4 * IBM ASM Service Processor Device Driver
6 * Copyright (C) IBM Corporation, 2004
8 * Author: Max Asböck <amax@us.ibm.com>
9 */
11 #include <linux/sched.h>
12 #include <linux/slab.h>
13 #include "ibmasm.h"
14 #include "lowlevel.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
21 * sp_event.
22 * The driver does not interpret the events, it simply stores them in a
23 * circular buffer.
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);
34 /**
35 * receive_event
36 * Called by the interrupt handler when a dot command of type sp_event is
37 * received.
38 * Store the event in the circular event buffer, wake up any sleeping
39 * event readers.
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;
47 unsigned long flags;
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);
71 /**
72 * get_next_event
73 * Called by event readers (initiated from user space through the file
74 * system).
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;
81 unsigned int index;
82 unsigned long flags;
84 reader->cancelled = 0;
86 if (wait_event_interruptible(reader->wait,
87 event_available(buffer, reader) || reader->cancelled))
88 return -ERESTARTSYS;
90 if (!event_available(buffer, reader))
91 return 0;
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)
118 unsigned long flags;
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)
129 unsigned long flags;
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;
140 int i;
142 buffer = kmalloc(sizeof(struct event_buffer), GFP_KERNEL);
143 if (!buffer)
144 return -ENOMEM;
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;
157 return 0;
160 void ibmasm_event_buffer_exit(struct service_processor *sp)
162 kfree(sp->event_buffer);