hvcserver.c 7.7 KB

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