command.c 4.1 KB

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