hvcserver.c 7.8 KB

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