command.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. static void exec_next_command(struct service_processor *sp);
  25. static void free_command(struct kobject *kobj);
  26. static struct kobj_type ibmasm_cmd_kobj_type = {
  27. .release = free_command,
  28. };
  29. struct command *ibmasm_new_command(size_t buffer_size)
  30. {
  31. struct command *cmd;
  32. if (buffer_size > IBMASM_CMD_MAX_BUFFER_SIZE)
  33. return NULL;
  34. cmd = kmalloc(sizeof(struct command), GFP_KERNEL);
  35. if (cmd == NULL)
  36. return NULL;
  37. memset(cmd, 0, sizeof(*cmd));
  38. cmd->buffer = kmalloc(buffer_size, GFP_KERNEL);
  39. if (cmd->buffer == NULL) {
  40. kfree(cmd);
  41. return NULL;
  42. }
  43. memset(cmd->buffer, 0, buffer_size);
  44. cmd->buffer_size = buffer_size;
  45. kobject_init(&cmd->kobj);
  46. cmd->kobj.ktype = &ibmasm_cmd_kobj_type;
  47. cmd->status = IBMASM_CMD_PENDING;
  48. init_waitqueue_head(&cmd->wait);
  49. INIT_LIST_HEAD(&cmd->queue_node);
  50. return cmd;
  51. }
  52. static void free_command(struct kobject *kobj)
  53. {
  54. struct command *cmd = to_command(kobj);
  55. list_del(&cmd->queue_node);
  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. if (ibmasm_send_i2o_message(sp)) {
  77. sp->current_command->status = IBMASM_CMD_FAILED;
  78. exec_next_command(sp);
  79. }
  80. }
  81. /**
  82. * exec_command
  83. * send a command to a service processor
  84. * Commands are executed sequentially. One command (sp->current_command)
  85. * is sent to the service processor. Once the interrupt handler gets a
  86. * message of type command_response, the message is copied into
  87. * the current commands buffer,
  88. */
  89. void ibmasm_exec_command(struct service_processor *sp, struct command *cmd)
  90. {
  91. unsigned long flags;
  92. spin_lock_irqsave(&sp->lock, flags);
  93. if (!sp->current_command) {
  94. command_get(cmd);
  95. sp->current_command = cmd;
  96. spin_unlock_irqrestore(&sp->lock, flags);
  97. do_exec_command(sp);
  98. } else {
  99. enqueue_command(sp, cmd);
  100. spin_unlock_irqrestore(&sp->lock, flags);
  101. }
  102. }
  103. static void exec_next_command(struct service_processor *sp)
  104. {
  105. unsigned long flags;
  106. wake_up(&sp->current_command->wait);
  107. command_put(sp->current_command);
  108. spin_lock_irqsave(&sp->lock, flags);
  109. sp->current_command = dequeue_command(sp);
  110. if (sp->current_command) {
  111. command_get(sp->current_command);
  112. spin_unlock_irqrestore(&sp->lock, flags);
  113. do_exec_command(sp);
  114. } else {
  115. spin_unlock_irqrestore(&sp->lock, flags);
  116. }
  117. }
  118. /**
  119. * Sleep until a command has failed or a response has been received
  120. * and the command status been updated by the interrupt handler.
  121. * (see receive_response).
  122. */
  123. void ibmasm_wait_for_response(struct command *cmd, int timeout)
  124. {
  125. wait_event_interruptible_timeout(cmd->wait,
  126. cmd->status == IBMASM_CMD_COMPLETE ||
  127. cmd->status == IBMASM_CMD_FAILED,
  128. timeout * HZ);
  129. }
  130. /**
  131. * receive_command_response
  132. * called by the interrupt handler when a dot command of type command_response
  133. * was received.
  134. */
  135. void ibmasm_receive_command_response(struct service_processor *sp, void *response, size_t size)
  136. {
  137. struct command *cmd = sp->current_command;
  138. if (!sp->current_command)
  139. return;
  140. memcpy(cmd->buffer, response, min(size, cmd->buffer_size));
  141. cmd->status = IBMASM_CMD_COMPLETE;
  142. exec_next_command(sp);
  143. }