event.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * IBM ASM Service Processor Device Driver
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright (C) IBM Corporation, 2004
  19. *
  20. * Author: Max Asböck <amax@us.ibm.com>
  21. *
  22. */
  23. #include <linux/sched.h>
  24. #include <linux/slab.h>
  25. #include "ibmasm.h"
  26. #include "lowlevel.h"
  27. /*
  28. * ASM service processor event handling routines.
  29. *
  30. * Events are signalled to the device drivers through interrupts.
  31. * They have the format of dot commands, with the type field set to
  32. * sp_event.
  33. * The driver does not interpret the events, it simply stores them in a
  34. * circular buffer.
  35. */
  36. static void wake_up_event_readers(struct service_processor *sp)
  37. {
  38. struct event_reader *reader;
  39. list_for_each_entry(reader, &sp->event_buffer->readers, node)
  40. wake_up_interruptible(&reader->wait);
  41. }
  42. /**
  43. * receive_event
  44. * Called by the interrupt handler when a dot command of type sp_event is
  45. * received.
  46. * Store the event in the circular event buffer, wake up any sleeping
  47. * event readers.
  48. * There is no reader marker in the buffer, therefore readers are
  49. * responsible for keeping up with the writer, or they will lose events.
  50. */
  51. void ibmasm_receive_event(struct service_processor *sp, void *data, unsigned int data_size)
  52. {
  53. struct event_buffer *buffer = sp->event_buffer;
  54. struct ibmasm_event *event;
  55. unsigned long flags;
  56. data_size = min(data_size, IBMASM_EVENT_MAX_SIZE);
  57. spin_lock_irqsave(&sp->lock, flags);
  58. /* copy the event into the next slot in the circular buffer */
  59. event = &buffer->events[buffer->next_index];
  60. memcpy_fromio(event->data, data, data_size);
  61. event->data_size = data_size;
  62. event->serial_number = buffer->next_serial_number;
  63. /* advance indices in the buffer */
  64. buffer->next_index = (buffer->next_index + 1) % IBMASM_NUM_EVENTS;
  65. buffer->next_serial_number++;
  66. spin_unlock_irqrestore(&sp->lock, flags);
  67. wake_up_event_readers(sp);
  68. }
  69. static inline int event_available(struct event_buffer *b, struct event_reader *r)
  70. {
  71. return (r->next_serial_number < b->next_serial_number);
  72. }
  73. /**
  74. * get_next_event
  75. * Called by event readers (initiated from user space through the file
  76. * system).
  77. * Sleeps until a new event is available.
  78. */
  79. int ibmasm_get_next_event(struct service_processor *sp, struct event_reader *reader)
  80. {
  81. struct event_buffer *buffer = sp->event_buffer;
  82. struct ibmasm_event *event;
  83. unsigned int index;
  84. unsigned long flags;
  85. reader->cancelled = 0;
  86. if (wait_event_interruptible(reader->wait,
  87. event_available(buffer, reader) || reader->cancelled))
  88. return -ERESTARTSYS;
  89. if (!event_available(buffer, reader))
  90. return 0;
  91. spin_lock_irqsave(&sp->lock, flags);
  92. index = buffer->next_index;
  93. event = &buffer->events[index];
  94. while (event->serial_number < reader->next_serial_number) {
  95. index = (index + 1) % IBMASM_NUM_EVENTS;
  96. event = &buffer->events[index];
  97. }
  98. memcpy(reader->data, event->data, event->data_size);
  99. reader->data_size = event->data_size;
  100. reader->next_serial_number = event->serial_number + 1;
  101. spin_unlock_irqrestore(&sp->lock, flags);
  102. return event->data_size;
  103. }
  104. void ibmasm_cancel_next_event(struct event_reader *reader)
  105. {
  106. reader->cancelled = 1;
  107. wake_up_interruptible(&reader->wait);
  108. }
  109. void ibmasm_event_reader_register(struct service_processor *sp, struct event_reader *reader)
  110. {
  111. unsigned long flags;
  112. reader->next_serial_number = sp->event_buffer->next_serial_number;
  113. init_waitqueue_head(&reader->wait);
  114. spin_lock_irqsave(&sp->lock, flags);
  115. list_add(&reader->node, &sp->event_buffer->readers);
  116. spin_unlock_irqrestore(&sp->lock, flags);
  117. }
  118. void ibmasm_event_reader_unregister(struct service_processor *sp, struct event_reader *reader)
  119. {
  120. unsigned long flags;
  121. spin_lock_irqsave(&sp->lock, flags);
  122. list_del(&reader->node);
  123. spin_unlock_irqrestore(&sp->lock, flags);
  124. }
  125. int ibmasm_event_buffer_init(struct service_processor *sp)
  126. {
  127. struct event_buffer *buffer;
  128. struct ibmasm_event *event;
  129. int i;
  130. buffer = kmalloc(sizeof(struct event_buffer), GFP_KERNEL);
  131. if (!buffer)
  132. return -ENOMEM;
  133. buffer->next_index = 0;
  134. buffer->next_serial_number = 1;
  135. event = buffer->events;
  136. for (i=0; i<IBMASM_NUM_EVENTS; i++, event++)
  137. event->serial_number = 0;
  138. INIT_LIST_HEAD(&buffer->readers);
  139. sp->event_buffer = buffer;
  140. return 0;
  141. }
  142. void ibmasm_event_buffer_exit(struct service_processor *sp)
  143. {
  144. kfree(sp->event_buffer);
  145. }