command.c 4.7 KB

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