This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / arch / ppc64 / kernel / hvcserver.c
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
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <asm/hvcall.h>
26 #include <asm/hvcserver.h>
27 #include <asm/io.h>
28
29 #define HVCS_ARCH_VERSION "1.0.0"
30
31 MODULE_AUTHOR("Ryan S. Arnold <rsa@us.ibm.com>");
32 MODULE_DESCRIPTION("IBM hvcs ppc64 API");
33 MODULE_LICENSE("GPL");
34 MODULE_VERSION(HVCS_ARCH_VERSION);
35
36 /*
37  * Convert arch specific return codes into relevant errnos.  The hvcs
38  * functions aren't performance sensitive, so this conversion isn't an
39  * issue.
40  */
41 int hvcs_convert(long to_convert)
42 {
43         switch (to_convert) {
44                 case H_Success:
45                         return 0;
46                 case H_Parameter:
47                         return -EINVAL;
48                 case H_Hardware:
49                         return -EIO;
50                 case H_Busy:
51                 case H_LongBusyOrder1msec:
52                 case H_LongBusyOrder10msec:
53                 case H_LongBusyOrder100msec:
54                 case H_LongBusyOrder1sec:
55                 case H_LongBusyOrder10sec:
56                 case H_LongBusyOrder100sec:
57                         return -EBUSY;
58                 case H_Function: /* fall through */
59                 default:
60                         return -EPERM;
61         }
62 }
63
64 int hvcs_free_partner_info(struct list_head *head)
65 {
66         struct hvcs_partner_info *pi;
67         struct list_head *element;
68
69         if (!head) {
70                 return -EINVAL;
71         }
72
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
80         return 0;
81 }
82 EXPORT_SYMBOL(hvcs_free_partner_info);
83
84 /* Helper function for hvcs_get_partner_info */
85 int hvcs_next_partner(unsigned int unit_address,
86                 unsigned long last_p_partition_ID,
87                 unsigned long last_p_unit_address, unsigned long *pi_buff)
88
89 {
90         long retval;
91         retval = plpar_hcall_norets(H_VTERM_PARTNER_INFO, unit_address,
92                         last_p_partition_ID,
93                                 last_p_unit_address, virt_to_phys(pi_buff));
94         return hvcs_convert(retval);
95 }
96
97 /*
98  * The unit_address parameter is the unit address of the vty-server vdevice
99  * in whose partner information the caller is interested.  This function
100  * uses a pointer to a list_head instance in which to store the partner info.
101  * This function returns non-zero on success, or if there is no partner info.
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 used to store the partner_info list.
106  */
107 int hvcs_get_partner_info(unsigned int unit_address, struct list_head *head,
108                 unsigned long *pi_buff)
109 {
110         /*
111          * This is a page sized buffer to be passed to hvcall per invocation.
112          * NOTE: the first long returned is unit_address.  The second long
113          * returned is the partition ID and starting with pi_buff[2] are
114          * HVCS_CLC_LENGTH characters, which are diff size than the unsigned
115          * long, hence the casting mumbojumbo you see later.
116          */
117         unsigned long   last_p_partition_ID;
118         unsigned long   last_p_unit_address;
119         struct hvcs_partner_info *next_partner_info = NULL;
120         int more = 1;
121         int retval;
122
123         memset(pi_buff, 0x00, PAGE_SIZE);
124         /* invalid parameters */
125         if (!head)
126                 return -EINVAL;
127
128         last_p_partition_ID = last_p_unit_address = ~0UL;
129         INIT_LIST_HEAD(head);
130
131         if (!pi_buff)
132                 return -ENOMEM;
133
134         do {
135                 retval = hvcs_next_partner(unit_address, last_p_partition_ID,
136                                 last_p_unit_address, pi_buff);
137                 if (retval) {
138                         /*
139                          * Don't indicate that we've failed if we have
140                          * any list elements.
141                          */
142                         if (!list_empty(head))
143                                 return 0;
144                         return retval;
145                 }
146
147                 last_p_partition_ID = pi_buff[0];
148                 last_p_unit_address = pi_buff[1];
149
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
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
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
167                 next_partner_info->unit_address
168                         = (unsigned int)last_p_unit_address;
169                 next_partner_info->partition_ID
170                         = (unsigned int)last_p_partition_ID;
171
172                 /* copy the Null-term char too */
173                 strncpy(&next_partner_info->location_code[0],
174                         (char *)&pi_buff[2],
175                         strlen((char *)&pi_buff[2]) + 1);
176
177                 list_add_tail(&(next_partner_info->node), head);
178                 next_partner_info = NULL;
179
180         } while (more);
181
182         return 0;
183 }
184 EXPORT_SYMBOL(hvcs_get_partner_info);
185
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@ vdevice.  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@ vdevice
196  * and the vty@ vdevice that you are trying to open.  Don't shoot the
197  * messenger.  Firmware implemented it this way.
198  */
199 int hvcs_register_connection( unsigned int unit_address,
200                 unsigned int p_partition_ID, unsigned int 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 /*
210  * If -EBUSY is returned continue to call this function
211  * until 0 is returned.
212  */
213 int hvcs_free_connection(unsigned int unit_address)
214 {
215         long retval;
216         retval = plpar_hcall_norets(H_FREE_VTERM, unit_address);
217         return hvcs_convert(retval);
218 }
219 EXPORT_SYMBOL(hvcs_free_connection);