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 * 21*d36b6910SAl Viro * Author: Max Asböck <amax@us.ibm.com> 221da177e4SLinus Torvalds * 231da177e4SLinus Torvalds */ 241da177e4SLinus Torvalds 25d43c36dcSAlexey Dobriyan #include <linux/sched.h> 265a0e3ad6STejun Heo #include <linux/slab.h> 271da177e4SLinus Torvalds #include "ibmasm.h" 28b8acb808SMax Asbock #include "lowlevel.h" 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds /* 311da177e4SLinus Torvalds * ASM service processor event handling routines. 321da177e4SLinus Torvalds * 331da177e4SLinus Torvalds * Events are signalled to the device drivers through interrupts. 341da177e4SLinus Torvalds * They have the format of dot commands, with the type field set to 351da177e4SLinus Torvalds * sp_event. 361da177e4SLinus Torvalds * The driver does not interpret the events, it simply stores them in a 371da177e4SLinus Torvalds * circular buffer. 381da177e4SLinus Torvalds */ 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds static void wake_up_event_readers(struct service_processor *sp) 411da177e4SLinus Torvalds { 421da177e4SLinus Torvalds struct event_reader *reader; 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds list_for_each_entry(reader, &sp->event_buffer->readers, node) 451da177e4SLinus Torvalds wake_up_interruptible(&reader->wait); 461da177e4SLinus Torvalds } 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds /** 491da177e4SLinus Torvalds * receive_event 501da177e4SLinus Torvalds * Called by the interrupt handler when a dot command of type sp_event is 511da177e4SLinus Torvalds * received. 521da177e4SLinus Torvalds * Store the event in the circular event buffer, wake up any sleeping 531da177e4SLinus Torvalds * event readers. 541da177e4SLinus Torvalds * There is no reader marker in the buffer, therefore readers are 553a4e3678SNick Andrew * responsible for keeping up with the writer, or they will lose events. 561da177e4SLinus Torvalds */ 571da177e4SLinus Torvalds void ibmasm_receive_event(struct service_processor *sp, void *data, unsigned int data_size) 581da177e4SLinus Torvalds { 591da177e4SLinus Torvalds struct event_buffer *buffer = sp->event_buffer; 601da177e4SLinus Torvalds struct ibmasm_event *event; 611da177e4SLinus Torvalds unsigned long flags; 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds data_size = min(data_size, IBMASM_EVENT_MAX_SIZE); 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds spin_lock_irqsave(&sp->lock, flags); 661da177e4SLinus Torvalds /* copy the event into the next slot in the circular buffer */ 671da177e4SLinus Torvalds event = &buffer->events[buffer->next_index]; 68b8acb808SMax Asbock memcpy_fromio(event->data, data, data_size); 691da177e4SLinus Torvalds event->data_size = data_size; 701da177e4SLinus Torvalds event->serial_number = buffer->next_serial_number; 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds /* advance indices in the buffer */ 731da177e4SLinus Torvalds buffer->next_index = (buffer->next_index + 1) % IBMASM_NUM_EVENTS; 741da177e4SLinus Torvalds buffer->next_serial_number++; 751da177e4SLinus Torvalds spin_unlock_irqrestore(&sp->lock, flags); 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds wake_up_event_readers(sp); 781da177e4SLinus Torvalds } 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds static inline int event_available(struct event_buffer *b, struct event_reader *r) 811da177e4SLinus Torvalds { 821da177e4SLinus Torvalds return (r->next_serial_number < b->next_serial_number); 831da177e4SLinus Torvalds } 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds /** 861da177e4SLinus Torvalds * get_next_event 871da177e4SLinus Torvalds * Called by event readers (initiated from user space through the file 881da177e4SLinus Torvalds * system). 891da177e4SLinus Torvalds * Sleeps until a new event is available. 901da177e4SLinus Torvalds */ 911da177e4SLinus Torvalds int ibmasm_get_next_event(struct service_processor *sp, struct event_reader *reader) 921da177e4SLinus Torvalds { 931da177e4SLinus Torvalds struct event_buffer *buffer = sp->event_buffer; 941da177e4SLinus Torvalds struct ibmasm_event *event; 951da177e4SLinus Torvalds unsigned int index; 961da177e4SLinus Torvalds unsigned long flags; 971da177e4SLinus Torvalds 98b8acb808SMax Asbock reader->cancelled = 0; 99b8acb808SMax Asbock 100b8acb808SMax Asbock if (wait_event_interruptible(reader->wait, 101b8acb808SMax Asbock event_available(buffer, reader) || reader->cancelled)) 1021da177e4SLinus Torvalds return -ERESTARTSYS; 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds if (!event_available(buffer, reader)) 1051da177e4SLinus Torvalds return 0; 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds spin_lock_irqsave(&sp->lock, flags); 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds index = buffer->next_index; 1101da177e4SLinus Torvalds event = &buffer->events[index]; 1111da177e4SLinus Torvalds while (event->serial_number < reader->next_serial_number) { 1121da177e4SLinus Torvalds index = (index + 1) % IBMASM_NUM_EVENTS; 1131da177e4SLinus Torvalds event = &buffer->events[index]; 1141da177e4SLinus Torvalds } 1151da177e4SLinus Torvalds memcpy(reader->data, event->data, event->data_size); 1161da177e4SLinus Torvalds reader->data_size = event->data_size; 1171da177e4SLinus Torvalds reader->next_serial_number = event->serial_number + 1; 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds spin_unlock_irqrestore(&sp->lock, flags); 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds return event->data_size; 1221da177e4SLinus Torvalds } 1231da177e4SLinus Torvalds 124b8acb808SMax Asbock void ibmasm_cancel_next_event(struct event_reader *reader) 125b8acb808SMax Asbock { 126b8acb808SMax Asbock reader->cancelled = 1; 127b8acb808SMax Asbock wake_up_interruptible(&reader->wait); 128b8acb808SMax Asbock } 129b8acb808SMax Asbock 1301da177e4SLinus Torvalds void ibmasm_event_reader_register(struct service_processor *sp, struct event_reader *reader) 1311da177e4SLinus Torvalds { 1321da177e4SLinus Torvalds unsigned long flags; 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds reader->next_serial_number = sp->event_buffer->next_serial_number; 1351da177e4SLinus Torvalds init_waitqueue_head(&reader->wait); 1361da177e4SLinus Torvalds spin_lock_irqsave(&sp->lock, flags); 1371da177e4SLinus Torvalds list_add(&reader->node, &sp->event_buffer->readers); 1381da177e4SLinus Torvalds spin_unlock_irqrestore(&sp->lock, flags); 1391da177e4SLinus Torvalds } 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds void ibmasm_event_reader_unregister(struct service_processor *sp, struct event_reader *reader) 1421da177e4SLinus Torvalds { 1431da177e4SLinus Torvalds unsigned long flags; 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds spin_lock_irqsave(&sp->lock, flags); 1461da177e4SLinus Torvalds list_del(&reader->node); 1471da177e4SLinus Torvalds spin_unlock_irqrestore(&sp->lock, flags); 1481da177e4SLinus Torvalds } 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds int ibmasm_event_buffer_init(struct service_processor *sp) 1511da177e4SLinus Torvalds { 1521da177e4SLinus Torvalds struct event_buffer *buffer; 1531da177e4SLinus Torvalds struct ibmasm_event *event; 1541da177e4SLinus Torvalds int i; 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds buffer = kmalloc(sizeof(struct event_buffer), GFP_KERNEL); 1571da177e4SLinus Torvalds if (!buffer) 1581da177e4SLinus Torvalds return 1; 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds buffer->next_index = 0; 1611da177e4SLinus Torvalds buffer->next_serial_number = 1; 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds event = buffer->events; 1641da177e4SLinus Torvalds for (i=0; i<IBMASM_NUM_EVENTS; i++, event++) 1651da177e4SLinus Torvalds event->serial_number = 0; 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds INIT_LIST_HEAD(&buffer->readers); 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds sp->event_buffer = buffer; 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds return 0; 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds void ibmasm_event_buffer_exit(struct service_processor *sp) 1751da177e4SLinus Torvalds { 1761da177e4SLinus Torvalds kfree(sp->event_buffer); 1771da177e4SLinus Torvalds } 178