command.c 4.7 KB

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