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