Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * hvcserver.c
0004  * Copyright (C) 2004 Ryan S Arnold, IBM Corporation
0005  *
0006  * PPC64 virtual I/O console server support.
0007  */
0008 
0009 #include <linux/kernel.h>
0010 #include <linux/list.h>
0011 #include <linux/module.h>
0012 #include <linux/slab.h>
0013 #include <linux/string.h>
0014 
0015 #include <asm/hvcall.h>
0016 #include <asm/hvcserver.h>
0017 #include <asm/io.h>
0018 
0019 #define HVCS_ARCH_VERSION "1.0.0"
0020 
0021 MODULE_AUTHOR("Ryan S. Arnold <rsa@us.ibm.com>");
0022 MODULE_DESCRIPTION("IBM hvcs ppc64 API");
0023 MODULE_LICENSE("GPL");
0024 MODULE_VERSION(HVCS_ARCH_VERSION);
0025 
0026 /*
0027  * Convert arch specific return codes into relevant errnos.  The hvcs
0028  * functions aren't performance sensitive, so this conversion isn't an
0029  * issue.
0030  */
0031 static int hvcs_convert(long to_convert)
0032 {
0033     switch (to_convert) {
0034         case H_SUCCESS:
0035             return 0;
0036         case H_PARAMETER:
0037             return -EINVAL;
0038         case H_HARDWARE:
0039             return -EIO;
0040         case H_BUSY:
0041         case H_LONG_BUSY_ORDER_1_MSEC:
0042         case H_LONG_BUSY_ORDER_10_MSEC:
0043         case H_LONG_BUSY_ORDER_100_MSEC:
0044         case H_LONG_BUSY_ORDER_1_SEC:
0045         case H_LONG_BUSY_ORDER_10_SEC:
0046         case H_LONG_BUSY_ORDER_100_SEC:
0047             return -EBUSY;
0048         case H_FUNCTION:
0049         default:
0050             return -EPERM;
0051     }
0052 }
0053 
0054 /**
0055  * hvcs_free_partner_info - free pi allocated by hvcs_get_partner_info
0056  * @head: list_head pointer for an allocated list of partner info structs to
0057  *  free.
0058  *
0059  * This function is used to free the partner info list that was returned by
0060  * calling hvcs_get_partner_info().
0061  */
0062 int hvcs_free_partner_info(struct list_head *head)
0063 {
0064     struct hvcs_partner_info *pi;
0065     struct list_head *element;
0066 
0067     if (!head)
0068         return -EINVAL;
0069 
0070     while (!list_empty(head)) {
0071         element = head->next;
0072         pi = list_entry(element, struct hvcs_partner_info, node);
0073         list_del(element);
0074         kfree(pi);
0075     }
0076 
0077     return 0;
0078 }
0079 EXPORT_SYMBOL(hvcs_free_partner_info);
0080 
0081 /* Helper function for hvcs_get_partner_info */
0082 static int hvcs_next_partner(uint32_t unit_address,
0083         unsigned long last_p_partition_ID,
0084         unsigned long last_p_unit_address, unsigned long *pi_buff)
0085 
0086 {
0087     long retval;
0088     retval = plpar_hcall_norets(H_VTERM_PARTNER_INFO, unit_address,
0089             last_p_partition_ID,
0090                 last_p_unit_address, virt_to_phys(pi_buff));
0091     return hvcs_convert(retval);
0092 }
0093 
0094 /**
0095  * hvcs_get_partner_info - Get all of the partner info for a vty-server adapter
0096  * @unit_address: The unit_address of the vty-server adapter for which this
0097  *  function is fetching partner info.
0098  * @head: An initialized list_head pointer to an empty list to use to return the
0099  *  list of partner info fetched from the hypervisor to the caller.
0100  * @pi_buff: A page sized buffer pre-allocated prior to calling this function
0101  *  that is to be used to be used by firmware as an iterator to keep track
0102  *  of the partner info retrieval.
0103  *
0104  * This function returns non-zero on success, or if there is no partner info.
0105  *
0106  * The pi_buff is pre-allocated prior to calling this function because this
0107  * function may be called with a spin_lock held and kmalloc of a page is not
0108  * recommended as GFP_ATOMIC.
0109  *
0110  * The first long of this buffer is used to store a partner unit address.  The
0111  * second long is used to store a partner partition ID and starting at
0112  * pi_buff[2] is the 79 character Converged Location Code (diff size than the
0113  * unsigned longs, hence the casting mumbo jumbo you see later).
0114  *
0115  * Invocation of this function should always be followed by an invocation of
0116  * hvcs_free_partner_info() using a pointer to the SAME list head instance
0117  * that was passed as a parameter to this function.
0118  */
0119 int hvcs_get_partner_info(uint32_t unit_address, struct list_head *head,
0120         unsigned long *pi_buff)
0121 {
0122     /*
0123      * Dealt with as longs because of the hcall interface even though the
0124      * values are uint32_t.
0125      */
0126     unsigned long   last_p_partition_ID;
0127     unsigned long   last_p_unit_address;
0128     struct hvcs_partner_info *next_partner_info = NULL;
0129     int more = 1;
0130     int retval;
0131 
0132     /* invalid parameters */
0133     if (!head || !pi_buff)
0134         return -EINVAL;
0135 
0136     memset(pi_buff, 0x00, PAGE_SIZE);
0137     last_p_partition_ID = last_p_unit_address = ~0UL;
0138     INIT_LIST_HEAD(head);
0139 
0140     do {
0141         retval = hvcs_next_partner(unit_address, last_p_partition_ID,
0142                 last_p_unit_address, pi_buff);
0143         if (retval) {
0144             /*
0145              * Don't indicate that we've failed if we have
0146              * any list elements.
0147              */
0148             if (!list_empty(head))
0149                 return 0;
0150             return retval;
0151         }
0152 
0153         last_p_partition_ID = be64_to_cpu(pi_buff[0]);
0154         last_p_unit_address = be64_to_cpu(pi_buff[1]);
0155 
0156         /* This indicates that there are no further partners */
0157         if (last_p_partition_ID == ~0UL
0158                 && last_p_unit_address == ~0UL)
0159             break;
0160 
0161         /* This is a very small struct and will be freed soon in
0162          * hvcs_free_partner_info(). */
0163         next_partner_info = kmalloc(sizeof(struct hvcs_partner_info),
0164                 GFP_ATOMIC);
0165 
0166         if (!next_partner_info) {
0167             printk(KERN_WARNING "HVCONSOLE: kmalloc() failed to"
0168                 " allocate partner info struct.\n");
0169             hvcs_free_partner_info(head);
0170             return -ENOMEM;
0171         }
0172 
0173         next_partner_info->unit_address
0174             = (unsigned int)last_p_unit_address;
0175         next_partner_info->partition_ID
0176             = (unsigned int)last_p_partition_ID;
0177 
0178         /* copy the Null-term char too */
0179         strlcpy(&next_partner_info->location_code[0],
0180             (char *)&pi_buff[2],
0181             sizeof(next_partner_info->location_code));
0182 
0183         list_add_tail(&(next_partner_info->node), head);
0184         next_partner_info = NULL;
0185 
0186     } while (more);
0187 
0188     return 0;
0189 }
0190 EXPORT_SYMBOL(hvcs_get_partner_info);
0191 
0192 /**
0193  * hvcs_register_connection - establish a connection between this vty-server and
0194  *  a vty.
0195  * @unit_address: The unit address of the vty-server adapter that is to be
0196  *  establish a connection.
0197  * @p_partition_ID: The partition ID of the vty adapter that is to be connected.
0198  * @p_unit_address: The unit address of the vty adapter to which the vty-server
0199  *  is to be connected.
0200  *
0201  * If this function is called once and -EINVAL is returned it may
0202  * indicate that the partner info needs to be refreshed for the
0203  * target unit address at which point the caller must invoke
0204  * hvcs_get_partner_info() and then call this function again.  If,
0205  * for a second time, -EINVAL is returned then it indicates that
0206  * there is probably already a partner connection registered to a
0207  * different vty-server adapter.  It is also possible that a second
0208  * -EINVAL may indicate that one of the parms is not valid, for
0209  * instance if the link was removed between the vty-server adapter
0210  * and the vty adapter that you are trying to open.  Don't shoot the
0211  * messenger.  Firmware implemented it this way.
0212  */
0213 int hvcs_register_connection( uint32_t unit_address,
0214         uint32_t p_partition_ID, uint32_t p_unit_address)
0215 {
0216     long retval;
0217     retval = plpar_hcall_norets(H_REGISTER_VTERM, unit_address,
0218                 p_partition_ID, p_unit_address);
0219     return hvcs_convert(retval);
0220 }
0221 EXPORT_SYMBOL(hvcs_register_connection);
0222 
0223 /**
0224  * hvcs_free_connection - free the connection between a vty-server and vty
0225  * @unit_address: The unit address of the vty-server that is to have its
0226  *  connection severed.
0227  *
0228  * This function is used to free the partner connection between a vty-server
0229  * adapter and a vty adapter.
0230  *
0231  * If -EBUSY is returned continue to call this function until 0 is returned.
0232  */
0233 int hvcs_free_connection(uint32_t unit_address)
0234 {
0235     long retval;
0236     retval = plpar_hcall_norets(H_FREE_VTERM, unit_address);
0237     return hvcs_convert(retval);
0238 }
0239 EXPORT_SYMBOL(hvcs_free_connection);