3 * Copyright (C) 2004 Ryan S Arnold, IBM Corporation
5 * PPC64 virtual I/O console server support.
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.
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.
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
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
27 #include <asm/hvcall.h>
28 #include <asm/hvcserver.h>
31 #define HVCS_ARCH_VERSION "1.0.0"
33 MODULE_AUTHOR("Ryan S. Arnold <rsa@us.ibm.com>");
34 MODULE_DESCRIPTION("IBM hvcs ppc64 API");
35 MODULE_LICENSE("GPL");
36 MODULE_VERSION(HVCS_ARCH_VERSION);
39 * Convert arch specific return codes into relevant errnos. The hvcs
40 * functions aren't performance sensitive, so this conversion isn't an
43 int hvcs_convert(long to_convert)
53 case H_LongBusyOrder1msec:
54 case H_LongBusyOrder10msec:
55 case H_LongBusyOrder100msec:
56 case H_LongBusyOrder1sec:
57 case H_LongBusyOrder10sec:
58 case H_LongBusyOrder100sec:
60 case H_Function: /* fall through */
67 * hvcs_free_partner_info - free pi allocated by hvcs_get_partner_info
68 * @head: list_head pointer for an allocated list of partner info structs to
71 * This function is used to free the partner info list that was returned by
72 * calling hvcs_get_partner_info().
74 int hvcs_free_partner_info(struct list_head *head)
76 struct hvcs_partner_info *pi;
77 struct list_head *element;
82 while (!list_empty(head)) {
84 pi = list_entry(element, struct hvcs_partner_info, node);
91 EXPORT_SYMBOL(hvcs_free_partner_info);
93 /* Helper function for hvcs_get_partner_info */
94 int hvcs_next_partner(uint32_t unit_address,
95 unsigned long last_p_partition_ID,
96 unsigned long last_p_unit_address, unsigned long *pi_buff)
100 retval = plpar_hcall_norets(H_VTERM_PARTNER_INFO, unit_address,
102 last_p_unit_address, virt_to_phys(pi_buff));
103 return hvcs_convert(retval);
107 * hvcs_get_partner_info - Get all of the partner info for a vty-server adapter
108 * @unit_address: The unit_address of the vty-server adapter for which this
109 * function is fetching partner info.
110 * @head: An initialized list_head pointer to an empty list to use to return the
111 * list of partner info fetched from the hypervisor to the caller.
112 * @pi_buff: A page sized buffer pre-allocated prior to calling this function
113 * that is to be used to be used by firmware as an iterator to keep track
114 * of the partner info retrieval.
116 * This function returns non-zero on success, or if there is no partner info.
118 * The pi_buff is pre-allocated prior to calling this function because this
119 * function may be called with a spin_lock held and kmalloc of a page is not
120 * recommended as GFP_ATOMIC.
122 * The first long of this buffer is used to store a partner unit address. The
123 * second long is used to store a partner partition ID and starting at
124 * pi_buff[2] is the 79 character Converged Location Code (diff size than the
125 * unsigned longs, hence the casting mumbo jumbo you see later).
127 * Invocation of this function should always be followed by an invocation of
128 * hvcs_free_partner_info() using a pointer to the SAME list head instance
129 * that was passed as a parameter to this function.
131 int hvcs_get_partner_info(uint32_t unit_address, struct list_head *head,
132 unsigned long *pi_buff)
135 * Dealt with as longs because of the hcall interface even though the
136 * values are uint32_t.
138 unsigned long last_p_partition_ID;
139 unsigned long last_p_unit_address;
140 struct hvcs_partner_info *next_partner_info = NULL;
144 memset(pi_buff, 0x00, PAGE_SIZE);
145 /* invalid parameters */
146 if (!head || !pi_buff)
149 last_p_partition_ID = last_p_unit_address = ~0UL;
150 INIT_LIST_HEAD(head);
153 retval = hvcs_next_partner(unit_address, last_p_partition_ID,
154 last_p_unit_address, pi_buff);
157 * Don't indicate that we've failed if we have
160 if (!list_empty(head))
165 last_p_partition_ID = pi_buff[0];
166 last_p_unit_address = pi_buff[1];
168 /* This indicates that there are no further partners */
169 if (last_p_partition_ID == ~0UL
170 && last_p_unit_address == ~0UL)
173 /* This is a very small struct and will be freed soon in
174 * hvcs_free_partner_info(). */
175 next_partner_info = kmalloc(sizeof(struct hvcs_partner_info),
178 if (!next_partner_info) {
179 printk(KERN_WARNING "HVCONSOLE: kmalloc() failed to"
180 " allocate partner info struct.\n");
181 hvcs_free_partner_info(head);
185 next_partner_info->unit_address
186 = (unsigned int)last_p_unit_address;
187 next_partner_info->partition_ID
188 = (unsigned int)last_p_partition_ID;
190 /* copy the Null-term char too */
191 strncpy(&next_partner_info->location_code[0],
193 strlen((char *)&pi_buff[2]) + 1);
195 list_add_tail(&(next_partner_info->node), head);
196 next_partner_info = NULL;
202 EXPORT_SYMBOL(hvcs_get_partner_info);
205 * hvcs_register_connection - establish a connection between this vty-server and
207 * @unit_address: The unit address of the vty-server adapter that is to be
208 * establish a connection.
209 * @p_partition_ID: The partition ID of the vty adapter that is to be connected.
210 * @p_unit_address: The unit address of the vty adapter to which the vty-server
211 * is to be connected.
213 * If this function is called once and -EINVAL is returned it may
214 * indicate that the partner info needs to be refreshed for the
215 * target unit address at which point the caller must invoke
216 * hvcs_get_partner_info() and then call this function again. If,
217 * for a second time, -EINVAL is returned then it indicates that
218 * there is probably already a partner connection registered to a
219 * different vty-server adapter. It is also possible that a second
220 * -EINVAL may indicate that one of the parms is not valid, for
221 * instance if the link was removed between the vty-server adapter
222 * and the vty adapter that you are trying to open. Don't shoot the
223 * messenger. Firmware implemented it this way.
225 int hvcs_register_connection( uint32_t unit_address,
226 uint32_t p_partition_ID, uint32_t p_unit_address)
229 retval = plpar_hcall_norets(H_REGISTER_VTERM, unit_address,
230 p_partition_ID, p_unit_address);
231 return hvcs_convert(retval);
233 EXPORT_SYMBOL(hvcs_register_connection);
236 * hvcs_free_connection - free the connection between a vty-server and vty
237 * @unit_address: The unit address of the vty-server that is to have its
238 * connection severed.
240 * This function is used to free the partner connection between a vty-server
241 * adapter and a vty adapter.
243 * If -EBUSY is returned continue to call this function until 0 is returned.
245 int hvcs_free_connection(uint32_t unit_address)
248 retval = plpar_hcall_norets(H_FREE_VTERM, unit_address);
249 return hvcs_convert(retval);
251 EXPORT_SYMBOL(hvcs_free_connection);