command.c 5.0 KB

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