hvcserver.c 7.7 KB

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