command.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. static void exec_next_command(struct service_processor *sp);
  28. static atomic_t command_count = ATOMIC_INIT(0);
  29. struct command *ibmasm_new_command(struct service_processor *sp, size_t buffer_size)
  30. {
  31. struct command *cmd;
  32. if (buffer_size > IBMASM_CMD_MAX_BUFFER_SIZE)
  33. return NULL;
  34. cmd = kzalloc(sizeof(struct command), GFP_KERNEL);
  35. if (cmd == NULL)
  36. return NULL;
  37. cmd->buffer = kzalloc(buffer_size, GFP_KERNEL);
  38. if (cmd->buffer == NULL) {
  39. kfree(cmd);
  40. return NULL;
  41. }
  42. cmd->buffer_size = buffer_size;
  43. kref_init(&cmd->kref);
  44. cmd->lock = &sp->lock;
  45. cmd->status = IBMASM_CMD_PENDING;
  46. init_waitqueue_head(&cmd->wait);
  47. INIT_LIST_HEAD(&cmd->queue_node);
  48. atomic_inc(&command_count);
  49. dbg("command count: %d\n", atomic_read(&command_count));
  50. return cmd;
  51. }
  52. void ibmasm_free_command(struct kref *kref)
  53. {
  54. struct command *cmd = to_command(kref);
  55. list_del(&cmd->queue_node);
  56. atomic_dec(&command_count);
  57. dbg("command count: %d\n", atomic_read(&command_count));
  58. kfree(cmd->buffer);
  59. kfree(cmd);
  60. }
  61. static void enqueue_command(struct service_processor *sp, struct command *cmd)
  62. {
  63. list_add_tail(&cmd->queue_node, &sp->command_queue);
  64. }
  65. static struct command *dequeue_command(struct service_processor *sp)
  66. {
  67. struct command *cmd;
  68. struct list_head *next;
  69. if (list_empty(&sp->command_queue))
  70. return NULL;
  71. next = sp->command_queue.next;
  72. list_del_init(next);
  73. cmd = list_entry(next, struct command, queue_node);
  74. return cmd;
  75. }
  76. static inline void do_exec_command(struct service_processor *sp)
  77. {
  78. char tsbuf[32];
  79. dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf));
  80. if (ibmasm_send_i2o_message(sp)) {
  81. sp->current_command->status = IBMASM_CMD_FAILED;
  82. wake_up(&sp->current_command->wait);
  83. command_put(sp->current_command);
  84. exec_next_command(sp);
  85. }
  86. }
  87. /**
  88. * exec_command
  89. * send a command to a service processor
  90. * Commands are executed sequentially. One command (sp->current_command)
  91. * is sent to the service processor. Once the interrupt handler gets a
  92. * message of type command_response, the message is copied into
  93. * the current commands buffer,
  94. */
  95. void ibmasm_exec_command(struct service_processor *sp, struct command *cmd)
  96. {
  97. unsigned long flags;
  98. char tsbuf[32];
  99. dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf));
  100. spin_lock_irqsave(&sp->lock, flags);
  101. if (!sp->current_command) {
  102. sp->current_command = cmd;
  103. command_get(sp->current_command);
  104. spin_unlock_irqrestore(&sp->lock, flags);
  105. do_exec_command(sp);
  106. } else {
  107. enqueue_command(sp, cmd);
  108. spin_unlock_irqrestore(&sp->lock, flags);
  109. }
  110. }
  111. static void exec_next_command(struct service_processor *sp)
  112. {
  113. unsigned long flags;
  114. char tsbuf[32];
  115. dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf));
  116. spin_lock_irqsave(&sp->lock, flags);
  117. sp->current_command = dequeue_command(sp);
  118. if (sp->current_command) {
  119. command_get(sp->current_command);
  120. spin_unlock_irqrestore(&sp->lock, flags);
  121. do_exec_command(sp);
  122. } else {
  123. spin_unlock_irqrestore(&sp->lock, flags);
  124. }
  125. }
  126. /**
  127. * Sleep until a command has failed or a response has been received
  128. * and the command status been updated by the interrupt handler.
  129. * (see receive_response).
  130. */
  131. void ibmasm_wait_for_response(struct command *cmd, int timeout)
  132. {
  133. wait_event_interruptible_timeout(cmd->wait,
  134. cmd->status == IBMASM_CMD_COMPLETE ||
  135. cmd->status == IBMASM_CMD_FAILED,
  136. timeout * HZ);
  137. }
  138. /**
  139. * receive_command_response
  140. * called by the interrupt handler when a dot command of type command_response
  141. * was received.
  142. */
  143. void ibmasm_receive_command_response(struct service_processor *sp, void *response, size_t size)
  144. {
  145. struct command *cmd = sp->current_command;
  146. if (!sp->current_command)
  147. return;
  148. memcpy_fromio(cmd->buffer, response, min(size, cmd->buffer_size));
  149. cmd->status = IBMASM_CMD_COMPLETE;
  150. wake_up(&sp->current_command->wait);
  151. command_put(sp->current_command);
  152. exec_next_command(sp);
  153. }