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