command.c 4.9 KB

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