xref: /openbmc/linux/drivers/misc/ibmasm/event.c (revision d43c36dc6b357fa1806800f18aa30123c747a6d1)
11da177e4SLinus Torvalds 
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * IBM ASM Service Processor Device Driver
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
61da177e4SLinus Torvalds  * it under the terms of the GNU General Public License as published by
71da177e4SLinus Torvalds  * the Free Software Foundation; either version 2 of the License, or
81da177e4SLinus Torvalds  * (at your option) any later version.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * This program is distributed in the hope that it will be useful,
111da177e4SLinus Torvalds  * but WITHOUT ANY WARRANTY; without even the implied warranty of
121da177e4SLinus Torvalds  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
131da177e4SLinus Torvalds  * GNU General Public License for more details.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
161da177e4SLinus Torvalds  * along with this program; if not, write to the Free Software
171da177e4SLinus Torvalds  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  * Copyright (C) IBM Corporation, 2004
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  * Author: Max Asb�ck <amax@us.ibm.com>
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  */
241da177e4SLinus Torvalds 
25*d43c36dcSAlexey Dobriyan #include <linux/sched.h>
261da177e4SLinus Torvalds #include "ibmasm.h"
27b8acb808SMax Asbock #include "lowlevel.h"
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds /*
301da177e4SLinus Torvalds  * ASM service processor event handling routines.
311da177e4SLinus Torvalds  *
321da177e4SLinus Torvalds  * Events are signalled to the device drivers through interrupts.
331da177e4SLinus Torvalds  * They have the format of dot commands, with the type field set to
341da177e4SLinus Torvalds  * sp_event.
351da177e4SLinus Torvalds  * The driver does not interpret the events, it simply stores them in a
361da177e4SLinus Torvalds  * circular buffer.
371da177e4SLinus Torvalds  */
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds static void wake_up_event_readers(struct service_processor *sp)
401da177e4SLinus Torvalds {
411da177e4SLinus Torvalds 	struct event_reader *reader;
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds 	list_for_each_entry(reader, &sp->event_buffer->readers, node)
441da177e4SLinus Torvalds                 wake_up_interruptible(&reader->wait);
451da177e4SLinus Torvalds }
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds /**
481da177e4SLinus Torvalds  * receive_event
491da177e4SLinus Torvalds  * Called by the interrupt handler when a dot command of type sp_event is
501da177e4SLinus Torvalds  * received.
511da177e4SLinus Torvalds  * Store the event in the circular event buffer, wake up any sleeping
521da177e4SLinus Torvalds  * event readers.
531da177e4SLinus Torvalds  * There is no reader marker in the buffer, therefore readers are
543a4e3678SNick Andrew  * responsible for keeping up with the writer, or they will lose events.
551da177e4SLinus Torvalds  */
561da177e4SLinus Torvalds void ibmasm_receive_event(struct service_processor *sp, void *data, unsigned int data_size)
571da177e4SLinus Torvalds {
581da177e4SLinus Torvalds 	struct event_buffer *buffer = sp->event_buffer;
591da177e4SLinus Torvalds 	struct ibmasm_event *event;
601da177e4SLinus Torvalds 	unsigned long flags;
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds 	data_size = min(data_size, IBMASM_EVENT_MAX_SIZE);
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds 	spin_lock_irqsave(&sp->lock, flags);
651da177e4SLinus Torvalds 	/* copy the event into the next slot in the circular buffer */
661da177e4SLinus Torvalds 	event = &buffer->events[buffer->next_index];
67b8acb808SMax Asbock 	memcpy_fromio(event->data, data, data_size);
681da177e4SLinus Torvalds 	event->data_size = data_size;
691da177e4SLinus Torvalds 	event->serial_number = buffer->next_serial_number;
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 	/* advance indices in the buffer */
721da177e4SLinus Torvalds 	buffer->next_index = (buffer->next_index + 1) % IBMASM_NUM_EVENTS;
731da177e4SLinus Torvalds 	buffer->next_serial_number++;
741da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sp->lock, flags);
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds 	wake_up_event_readers(sp);
771da177e4SLinus Torvalds }
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds static inline int event_available(struct event_buffer *b, struct event_reader *r)
801da177e4SLinus Torvalds {
811da177e4SLinus Torvalds 	return (r->next_serial_number < b->next_serial_number);
821da177e4SLinus Torvalds }
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds /**
851da177e4SLinus Torvalds  * get_next_event
861da177e4SLinus Torvalds  * Called by event readers (initiated from user space through the file
871da177e4SLinus Torvalds  * system).
881da177e4SLinus Torvalds  * Sleeps until a new event is available.
891da177e4SLinus Torvalds  */
901da177e4SLinus Torvalds int ibmasm_get_next_event(struct service_processor *sp, struct event_reader *reader)
911da177e4SLinus Torvalds {
921da177e4SLinus Torvalds 	struct event_buffer *buffer = sp->event_buffer;
931da177e4SLinus Torvalds 	struct ibmasm_event *event;
941da177e4SLinus Torvalds 	unsigned int index;
951da177e4SLinus Torvalds 	unsigned long flags;
961da177e4SLinus Torvalds 
97b8acb808SMax Asbock 	reader->cancelled = 0;
98b8acb808SMax Asbock 
99b8acb808SMax Asbock 	if (wait_event_interruptible(reader->wait,
100b8acb808SMax Asbock 			event_available(buffer, reader) || reader->cancelled))
1011da177e4SLinus Torvalds 		return -ERESTARTSYS;
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 	if (!event_available(buffer, reader))
1041da177e4SLinus Torvalds 		return 0;
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	spin_lock_irqsave(&sp->lock, flags);
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 	index = buffer->next_index;
1091da177e4SLinus Torvalds 	event = &buffer->events[index];
1101da177e4SLinus Torvalds 	while (event->serial_number < reader->next_serial_number) {
1111da177e4SLinus Torvalds 		index = (index + 1) % IBMASM_NUM_EVENTS;
1121da177e4SLinus Torvalds 		event = &buffer->events[index];
1131da177e4SLinus Torvalds 	}
1141da177e4SLinus Torvalds 	memcpy(reader->data, event->data, event->data_size);
1151da177e4SLinus Torvalds 	reader->data_size = event->data_size;
1161da177e4SLinus Torvalds 	reader->next_serial_number = event->serial_number + 1;
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sp->lock, flags);
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	return event->data_size;
1211da177e4SLinus Torvalds }
1221da177e4SLinus Torvalds 
123b8acb808SMax Asbock void ibmasm_cancel_next_event(struct event_reader *reader)
124b8acb808SMax Asbock {
125b8acb808SMax Asbock         reader->cancelled = 1;
126b8acb808SMax Asbock         wake_up_interruptible(&reader->wait);
127b8acb808SMax Asbock }
128b8acb808SMax Asbock 
1291da177e4SLinus Torvalds void ibmasm_event_reader_register(struct service_processor *sp, struct event_reader *reader)
1301da177e4SLinus Torvalds {
1311da177e4SLinus Torvalds 	unsigned long flags;
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	reader->next_serial_number = sp->event_buffer->next_serial_number;
1341da177e4SLinus Torvalds 	init_waitqueue_head(&reader->wait);
1351da177e4SLinus Torvalds 	spin_lock_irqsave(&sp->lock, flags);
1361da177e4SLinus Torvalds 	list_add(&reader->node, &sp->event_buffer->readers);
1371da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sp->lock, flags);
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds void ibmasm_event_reader_unregister(struct service_processor *sp, struct event_reader *reader)
1411da177e4SLinus Torvalds {
1421da177e4SLinus Torvalds 	unsigned long flags;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	spin_lock_irqsave(&sp->lock, flags);
1451da177e4SLinus Torvalds 	list_del(&reader->node);
1461da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sp->lock, flags);
1471da177e4SLinus Torvalds }
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds int ibmasm_event_buffer_init(struct service_processor *sp)
1501da177e4SLinus Torvalds {
1511da177e4SLinus Torvalds 	struct event_buffer *buffer;
1521da177e4SLinus Torvalds 	struct ibmasm_event *event;
1531da177e4SLinus Torvalds 	int i;
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	buffer = kmalloc(sizeof(struct event_buffer), GFP_KERNEL);
1561da177e4SLinus Torvalds 	if (!buffer)
1571da177e4SLinus Torvalds 		return 1;
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	buffer->next_index = 0;
1601da177e4SLinus Torvalds 	buffer->next_serial_number = 1;
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	event = buffer->events;
1631da177e4SLinus Torvalds 	for (i=0; i<IBMASM_NUM_EVENTS; i++, event++)
1641da177e4SLinus Torvalds 		event->serial_number = 0;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	INIT_LIST_HEAD(&buffer->readers);
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	sp->event_buffer = buffer;
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 	return 0;
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds void ibmasm_event_buffer_exit(struct service_processor *sp)
1741da177e4SLinus Torvalds {
1751da177e4SLinus Torvalds 	kfree(sp->event_buffer);
1761da177e4SLinus Torvalds }
177