hvcserver.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * hvcserver.c
  4. * Copyright (C) 2004 Ryan S Arnold, IBM Corporation
  5. *
  6. * PPC64 virtual I/O console server support.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/list.h>
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/string.h>
  13. #include <asm/hvcall.h>
  14. #include <asm/hvcserver.h>
  15. #include <asm/io.h>
  16. #define HVCS_ARCH_VERSION "1.0.0"
  17. MODULE_AUTHOR("Ryan S. Arnold <[email protected]>");
  18. MODULE_DESCRIPTION("IBM hvcs ppc64 API");
  19. MODULE_LICENSE("GPL");
  20. MODULE_VERSION(HVCS_ARCH_VERSION);
  21. /*
  22. * Convert arch specific return codes into relevant errnos. The hvcs
  23. * functions aren't performance sensitive, so this conversion isn't an
  24. * issue.
  25. */
  26. static int hvcs_convert(long to_convert)
  27. {
  28. switch (to_convert) {
  29. case H_SUCCESS:
  30. return 0;
  31. case H_PARAMETER:
  32. return -EINVAL;
  33. case H_HARDWARE:
  34. return -EIO;
  35. case H_BUSY:
  36. case H_LONG_BUSY_ORDER_1_MSEC:
  37. case H_LONG_BUSY_ORDER_10_MSEC:
  38. case H_LONG_BUSY_ORDER_100_MSEC:
  39. case H_LONG_BUSY_ORDER_1_SEC:
  40. case H_LONG_BUSY_ORDER_10_SEC:
  41. case H_LONG_BUSY_ORDER_100_SEC:
  42. return -EBUSY;
  43. case H_FUNCTION:
  44. default:
  45. return -EPERM;
  46. }
  47. }
  48. /**
  49. * hvcs_free_partner_info - free pi allocated by hvcs_get_partner_info
  50. * @head: list_head pointer for an allocated list of partner info structs to
  51. * free.
  52. *
  53. * This function is used to free the partner info list that was returned by
  54. * calling hvcs_get_partner_info().
  55. */
  56. int hvcs_free_partner_info(struct list_head *head)
  57. {
  58. struct hvcs_partner_info *pi;
  59. struct list_head *element;
  60. if (!head)
  61. return -EINVAL;
  62. while (!list_empty(head)) {
  63. element = head->next;
  64. pi = list_entry(element, struct hvcs_partner_info, node);
  65. list_del(element);
  66. kfree(pi);
  67. }
  68. return 0;
  69. }
  70. EXPORT_SYMBOL(hvcs_free_partner_info);
  71. /* Helper function for hvcs_get_partner_info */
  72. static int hvcs_next_partner(uint32_t unit_address,
  73. unsigned long last_p_partition_ID,
  74. unsigned long last_p_unit_address, unsigned long *pi_buff)
  75. {
  76. long retval;
  77. retval = plpar_hcall_norets(H_VTERM_PARTNER_INFO, unit_address,
  78. last_p_partition_ID,
  79. last_p_unit_address, virt_to_phys(pi_buff));
  80. return hvcs_convert(retval);
  81. }
  82. /**
  83. * hvcs_get_partner_info - Get all of the partner info for a vty-server adapter
  84. * @unit_address: The unit_address of the vty-server adapter for which this
  85. * function is fetching partner info.
  86. * @head: An initialized list_head pointer to an empty list to use to return the
  87. * list of partner info fetched from the hypervisor to the caller.
  88. * @pi_buff: A page sized buffer pre-allocated prior to calling this function
  89. * that is to be used to be used by firmware as an iterator to keep track
  90. * of the partner info retrieval.
  91. *
  92. * This function returns non-zero on success, or if there is no partner info.
  93. *
  94. * The pi_buff is pre-allocated prior to calling this function because this
  95. * function may be called with a spin_lock held and kmalloc of a page is not
  96. * recommended as GFP_ATOMIC.
  97. *
  98. * The first long of this buffer is used to store a partner unit address. The
  99. * second long is used to store a partner partition ID and starting at
  100. * pi_buff[2] is the 79 character Converged Location Code (diff size than the
  101. * unsigned longs, hence the casting mumbo jumbo you see later).
  102. *
  103. * Invocation of this function should always be followed by an invocation of
  104. * hvcs_free_partner_info() using a pointer to the SAME list head instance
  105. * that was passed as a parameter to this function.
  106. */
  107. int hvcs_get_partner_info(uint32_t unit_address, struct list_head *head,
  108. unsigned long *pi_buff)
  109. {
  110. /*
  111. * Dealt with as longs because of the hcall interface even though the
  112. * values are uint32_t.
  113. */
  114. unsigned long last_p_partition_ID;
  115. unsigned long last_p_unit_address;
  116. struct hvcs_partner_info *next_partner_info = NULL;
  117. int more = 1;
  118. int retval;
  119. /* invalid parameters */
  120. if (!head || !pi_buff)
  121. return -EINVAL;
  122. memset(pi_buff, 0x00, PAGE_SIZE);
  123. last_p_partition_ID = last_p_unit_address = ~0UL;
  124. INIT_LIST_HEAD(head);
  125. do {
  126. retval = hvcs_next_partner(unit_address, last_p_partition_ID,
  127. last_p_unit_address, pi_buff);
  128. if (retval) {
  129. /*
  130. * Don't indicate that we've failed if we have
  131. * any list elements.
  132. */
  133. if (!list_empty(head))
  134. return 0;
  135. return retval;
  136. }
  137. last_p_partition_ID = be64_to_cpu(pi_buff[0]);
  138. last_p_unit_address = be64_to_cpu(pi_buff[1]);
  139. /* This indicates that there are no further partners */
  140. if (last_p_partition_ID == ~0UL
  141. && last_p_unit_address == ~0UL)
  142. break;
  143. /* This is a very small struct and will be freed soon in
  144. * hvcs_free_partner_info(). */
  145. next_partner_info = kmalloc(sizeof(struct hvcs_partner_info),
  146. GFP_ATOMIC);
  147. if (!next_partner_info) {
  148. printk(KERN_WARNING "HVCONSOLE: kmalloc() failed to"
  149. " allocate partner info struct.\n");
  150. hvcs_free_partner_info(head);
  151. return -ENOMEM;
  152. }
  153. next_partner_info->unit_address
  154. = (unsigned int)last_p_unit_address;
  155. next_partner_info->partition_ID
  156. = (unsigned int)last_p_partition_ID;
  157. /* copy the Null-term char too */
  158. strscpy(&next_partner_info->location_code[0],
  159. (char *)&pi_buff[2],
  160. sizeof(next_partner_info->location_code));
  161. list_add_tail(&(next_partner_info->node), head);
  162. next_partner_info = NULL;
  163. } while (more);
  164. return 0;
  165. }
  166. EXPORT_SYMBOL(hvcs_get_partner_info);
  167. /**
  168. * hvcs_register_connection - establish a connection between this vty-server and
  169. * a vty.
  170. * @unit_address: The unit address of the vty-server adapter that is to be
  171. * establish a connection.
  172. * @p_partition_ID: The partition ID of the vty adapter that is to be connected.
  173. * @p_unit_address: The unit address of the vty adapter to which the vty-server
  174. * is to be connected.
  175. *
  176. * If this function is called once and -EINVAL is returned it may
  177. * indicate that the partner info needs to be refreshed for the
  178. * target unit address at which point the caller must invoke
  179. * hvcs_get_partner_info() and then call this function again. If,
  180. * for a second time, -EINVAL is returned then it indicates that
  181. * there is probably already a partner connection registered to a
  182. * different vty-server adapter. It is also possible that a second
  183. * -EINVAL may indicate that one of the parms is not valid, for
  184. * instance if the link was removed between the vty-server adapter
  185. * and the vty adapter that you are trying to open. Don't shoot the
  186. * messenger. Firmware implemented it this way.
  187. */
  188. int hvcs_register_connection( uint32_t unit_address,
  189. uint32_t p_partition_ID, uint32_t p_unit_address)
  190. {
  191. long retval;
  192. retval = plpar_hcall_norets(H_REGISTER_VTERM, unit_address,
  193. p_partition_ID, p_unit_address);
  194. return hvcs_convert(retval);
  195. }
  196. EXPORT_SYMBOL(hvcs_register_connection);
  197. /**
  198. * hvcs_free_connection - free the connection between a vty-server and vty
  199. * @unit_address: The unit address of the vty-server that is to have its
  200. * connection severed.
  201. *
  202. * This function is used to free the partner connection between a vty-server
  203. * adapter and a vty adapter.
  204. *
  205. * If -EBUSY is returned continue to call this function until 0 is returned.
  206. */
  207. int hvcs_free_connection(uint32_t unit_address)
  208. {
  209. long retval;
  210. retval = plpar_hcall_norets(H_FREE_VTERM, unit_address);
  211. return hvcs_convert(retval);
  212. }
  213. EXPORT_SYMBOL(hvcs_free_connection);