Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* -*- linux-c -*- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
| 3 | * iSeries Virtual I/O Message Path code |
| 4 | * |
| 5 | * Authors: Dave Boutcher <boutcher@us.ibm.com> |
| 6 | * Ryan Arnold <ryanarn@us.ibm.com> |
| 7 | * Colin Devilbiss <devilbis@us.ibm.com> |
| 8 | * |
Stephen Rothwell | d223e72 | 2005-09-28 03:12:35 +1000 | [diff] [blame] | 9 | * (C) Copyright 2000-2005 IBM Corporation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * |
| 11 | * This code is used by the iSeries virtual disk, cd, |
| 12 | * tape, and console to communicate with OS/400 in another |
| 13 | * partition. |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or |
| 16 | * modify it under the terms of the GNU General Public License as |
| 17 | * published by the Free Software Foundation; either version 2 of the |
| 18 | * License, or (at your option) anyu later version. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, but |
| 21 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 | * General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License |
| 26 | * along with this program; if not, write to the Free Software Foundation, |
| 27 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 28 | * |
| 29 | */ |
| 30 | #include <linux/module.h> |
| 31 | #include <linux/kernel.h> |
| 32 | #include <linux/errno.h> |
| 33 | #include <linux/vmalloc.h> |
| 34 | #include <linux/string.h> |
| 35 | #include <linux/proc_fs.h> |
| 36 | #include <linux/dma-mapping.h> |
| 37 | #include <linux/wait.h> |
| 38 | #include <linux/seq_file.h> |
| 39 | #include <linux/smp_lock.h> |
| 40 | #include <linux/interrupt.h> |
| 41 | |
| 42 | #include <asm/system.h> |
| 43 | #include <asm/uaccess.h> |
Kelly Daly | 1ec65d7 | 2005-11-02 13:46:07 +1100 | [diff] [blame] | 44 | #include <asm/iseries/hv_types.h> |
Kelly Daly | 7b487bb | 2005-11-02 13:48:25 +1100 | [diff] [blame] | 45 | #include <asm/iseries/it_exp_vpd_panel.h> |
Kelly Daly | e45423e | 2005-11-02 12:08:31 +1100 | [diff] [blame] | 46 | #include <asm/iseries/hv_lp_event.h> |
Kelly Daly | 15b1718 | 2005-11-02 11:55:28 +1100 | [diff] [blame] | 47 | #include <asm/iseries/hv_lp_config.h> |
Kelly Daly | bbc8b62 | 2005-11-02 15:10:38 +1100 | [diff] [blame] | 48 | #include <asm/iseries/mf.h> |
Kelly Daly | b420677 | 2005-11-02 15:13:57 +1100 | [diff] [blame] | 49 | #include <asm/iseries/vio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | /* Status of the path to each other partition in the system. |
| 52 | * This is overkill, since we will only ever establish connections |
| 53 | * to our hosting partition and the primary partition on the system. |
| 54 | * But this allows for other support in the future. |
| 55 | */ |
| 56 | static struct viopathStatus { |
| 57 | int isOpen; /* Did we open the path? */ |
| 58 | int isActive; /* Do we have a mon msg outstanding */ |
| 59 | int users[VIO_MAX_SUBTYPES]; |
| 60 | HvLpInstanceId mSourceInst; |
| 61 | HvLpInstanceId mTargetInst; |
| 62 | int numberAllocated; |
| 63 | } viopathStatus[HVMAXARCHITECTEDLPS]; |
| 64 | |
| 65 | static DEFINE_SPINLOCK(statuslock); |
| 66 | |
| 67 | /* |
| 68 | * For each kind of event we allocate a buffer that is |
| 69 | * guaranteed not to cross a page boundary |
| 70 | */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 71 | static unsigned char event_buffer[VIO_MAX_SUBTYPES * 256] |
| 72 | __attribute__((__aligned__(4096))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | static atomic_t event_buffer_available[VIO_MAX_SUBTYPES]; |
| 74 | static int event_buffer_initialised; |
| 75 | |
| 76 | static void handleMonitorEvent(struct HvLpEvent *event); |
| 77 | |
| 78 | /* |
| 79 | * We use this structure to handle asynchronous responses. The caller |
| 80 | * blocks on the semaphore and the handler posts the semaphore. However, |
| 81 | * if system_state is not SYSTEM_RUNNING, then wait_atomic is used ... |
| 82 | */ |
| 83 | struct alloc_parms { |
| 84 | struct semaphore sem; |
| 85 | int number; |
| 86 | atomic_t wait_atomic; |
| 87 | int used_wait_atomic; |
| 88 | }; |
| 89 | |
| 90 | /* Put a sequence number in each mon msg. The value is not |
| 91 | * important. Start at something other than 0 just for |
| 92 | * readability. wrapping this is ok. |
| 93 | */ |
| 94 | static u8 viomonseq = 22; |
| 95 | |
| 96 | /* Our hosting logical partition. We get this at startup |
| 97 | * time, and different modules access this variable directly. |
| 98 | */ |
| 99 | HvLpIndex viopath_hostLp = HvLpIndexInvalid; |
| 100 | EXPORT_SYMBOL(viopath_hostLp); |
| 101 | HvLpIndex viopath_ourLp = HvLpIndexInvalid; |
| 102 | EXPORT_SYMBOL(viopath_ourLp); |
| 103 | |
| 104 | /* For each kind of incoming event we set a pointer to a |
| 105 | * routine to call. |
| 106 | */ |
| 107 | static vio_event_handler_t *vio_handler[VIO_MAX_SUBTYPES]; |
| 108 | |
| 109 | #define VIOPATH_KERN_WARN KERN_WARNING "viopath: " |
| 110 | #define VIOPATH_KERN_INFO KERN_INFO "viopath: " |
| 111 | |
| 112 | static int proc_viopath_show(struct seq_file *m, void *v) |
| 113 | { |
| 114 | char *buf; |
| 115 | u16 vlanMap; |
| 116 | dma_addr_t handle; |
| 117 | HvLpEvent_Rc hvrc; |
| 118 | DECLARE_MUTEX_LOCKED(Semaphore); |
| 119 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 120 | buf = kmalloc(HW_PAGE_SIZE, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | if (!buf) |
| 122 | return 0; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 123 | memset(buf, 0, HW_PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 125 | handle = dma_map_single(iSeries_vio_dev, buf, HW_PAGE_SIZE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | DMA_FROM_DEVICE); |
| 127 | |
| 128 | hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp, |
| 129 | HvLpEvent_Type_VirtualIo, |
| 130 | viomajorsubtype_config | vioconfigget, |
| 131 | HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck, |
| 132 | viopath_sourceinst(viopath_hostLp), |
| 133 | viopath_targetinst(viopath_hostLp), |
| 134 | (u64)(unsigned long)&Semaphore, VIOVERSION << 16, |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 135 | ((u64)handle) << 32, HW_PAGE_SIZE, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | if (hvrc != HvLpEvent_Rc_Good) |
| 138 | printk(VIOPATH_KERN_WARN "hv error on op %d\n", (int)hvrc); |
| 139 | |
| 140 | down(&Semaphore); |
| 141 | |
| 142 | vlanMap = HvLpConfig_getVirtualLanIndexMap(); |
| 143 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 144 | buf[HW_PAGE_SIZE-1] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | seq_printf(m, "%s", buf); |
| 146 | seq_printf(m, "AVAILABLE_VETH=%x\n", vlanMap); |
| 147 | seq_printf(m, "SRLNBR=%c%c%c%c%c%c%c\n", |
| 148 | e2a(xItExtVpdPanel.mfgID[2]), |
| 149 | e2a(xItExtVpdPanel.mfgID[3]), |
| 150 | e2a(xItExtVpdPanel.systemSerial[1]), |
| 151 | e2a(xItExtVpdPanel.systemSerial[2]), |
| 152 | e2a(xItExtVpdPanel.systemSerial[3]), |
| 153 | e2a(xItExtVpdPanel.systemSerial[4]), |
| 154 | e2a(xItExtVpdPanel.systemSerial[5])); |
| 155 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 156 | dma_unmap_single(iSeries_vio_dev, handle, HW_PAGE_SIZE, |
| 157 | DMA_FROM_DEVICE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | kfree(buf); |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | static int proc_viopath_open(struct inode *inode, struct file *file) |
| 164 | { |
| 165 | return single_open(file, proc_viopath_show, NULL); |
| 166 | } |
| 167 | |
| 168 | static struct file_operations proc_viopath_operations = { |
| 169 | .open = proc_viopath_open, |
| 170 | .read = seq_read, |
| 171 | .llseek = seq_lseek, |
| 172 | .release = single_release, |
| 173 | }; |
| 174 | |
| 175 | static int __init vio_proc_init(void) |
| 176 | { |
| 177 | struct proc_dir_entry *e; |
| 178 | |
| 179 | e = create_proc_entry("iSeries/config", 0, NULL); |
| 180 | if (e) |
| 181 | e->proc_fops = &proc_viopath_operations; |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | __initcall(vio_proc_init); |
| 186 | |
| 187 | /* See if a given LP is active. Allow for invalid lps to be passed in |
| 188 | * and just return invalid |
| 189 | */ |
| 190 | int viopath_isactive(HvLpIndex lp) |
| 191 | { |
| 192 | if (lp == HvLpIndexInvalid) |
| 193 | return 0; |
| 194 | if (lp < HVMAXARCHITECTEDLPS) |
| 195 | return viopathStatus[lp].isActive; |
| 196 | else |
| 197 | return 0; |
| 198 | } |
| 199 | EXPORT_SYMBOL(viopath_isactive); |
| 200 | |
| 201 | /* |
| 202 | * We cache the source and target instance ids for each |
| 203 | * partition. |
| 204 | */ |
| 205 | HvLpInstanceId viopath_sourceinst(HvLpIndex lp) |
| 206 | { |
| 207 | return viopathStatus[lp].mSourceInst; |
| 208 | } |
| 209 | EXPORT_SYMBOL(viopath_sourceinst); |
| 210 | |
| 211 | HvLpInstanceId viopath_targetinst(HvLpIndex lp) |
| 212 | { |
| 213 | return viopathStatus[lp].mTargetInst; |
| 214 | } |
| 215 | EXPORT_SYMBOL(viopath_targetinst); |
| 216 | |
| 217 | /* |
| 218 | * Send a monitor message. This is a message with the acknowledge |
| 219 | * bit on that the other side will NOT explicitly acknowledge. When |
| 220 | * the other side goes down, the hypervisor will acknowledge any |
| 221 | * outstanding messages....so we will know when the other side dies. |
| 222 | */ |
| 223 | static void sendMonMsg(HvLpIndex remoteLp) |
| 224 | { |
| 225 | HvLpEvent_Rc hvrc; |
| 226 | |
| 227 | viopathStatus[remoteLp].mSourceInst = |
| 228 | HvCallEvent_getSourceLpInstanceId(remoteLp, |
| 229 | HvLpEvent_Type_VirtualIo); |
| 230 | viopathStatus[remoteLp].mTargetInst = |
| 231 | HvCallEvent_getTargetLpInstanceId(remoteLp, |
| 232 | HvLpEvent_Type_VirtualIo); |
| 233 | |
| 234 | /* |
| 235 | * Deliberately ignore the return code here. if we call this |
| 236 | * more than once, we don't care. |
| 237 | */ |
| 238 | vio_setHandler(viomajorsubtype_monitor, handleMonitorEvent); |
| 239 | |
| 240 | hvrc = HvCallEvent_signalLpEventFast(remoteLp, HvLpEvent_Type_VirtualIo, |
| 241 | viomajorsubtype_monitor, HvLpEvent_AckInd_DoAck, |
| 242 | HvLpEvent_AckType_DeferredAck, |
| 243 | viopathStatus[remoteLp].mSourceInst, |
| 244 | viopathStatus[remoteLp].mTargetInst, |
| 245 | viomonseq++, 0, 0, 0, 0, 0); |
| 246 | |
| 247 | if (hvrc == HvLpEvent_Rc_Good) |
| 248 | viopathStatus[remoteLp].isActive = 1; |
| 249 | else { |
| 250 | printk(VIOPATH_KERN_WARN "could not connect to partition %d\n", |
| 251 | remoteLp); |
| 252 | viopathStatus[remoteLp].isActive = 0; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | static void handleMonitorEvent(struct HvLpEvent *event) |
| 257 | { |
| 258 | HvLpIndex remoteLp; |
| 259 | int i; |
| 260 | |
| 261 | /* |
| 262 | * This handler is _also_ called as part of the loop |
| 263 | * at the end of this routine, so it must be able to |
| 264 | * ignore NULL events... |
| 265 | */ |
| 266 | if (!event) |
| 267 | return; |
| 268 | |
| 269 | /* |
| 270 | * First see if this is just a normal monitor message from the |
| 271 | * other partition |
| 272 | */ |
| 273 | if (event->xFlags.xFunction == HvLpEvent_Function_Int) { |
| 274 | remoteLp = event->xSourceLp; |
| 275 | if (!viopathStatus[remoteLp].isActive) |
| 276 | sendMonMsg(remoteLp); |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * This path is for an acknowledgement; the other partition |
| 282 | * died |
| 283 | */ |
| 284 | remoteLp = event->xTargetLp; |
| 285 | if ((event->xSourceInstanceId != viopathStatus[remoteLp].mSourceInst) || |
| 286 | (event->xTargetInstanceId != viopathStatus[remoteLp].mTargetInst)) { |
| 287 | printk(VIOPATH_KERN_WARN "ignoring ack....mismatched instances\n"); |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | printk(VIOPATH_KERN_WARN "partition %d ended\n", remoteLp); |
| 292 | |
| 293 | viopathStatus[remoteLp].isActive = 0; |
| 294 | |
| 295 | /* |
| 296 | * For each active handler, pass them a NULL |
| 297 | * message to indicate that the other partition |
| 298 | * died |
| 299 | */ |
| 300 | for (i = 0; i < VIO_MAX_SUBTYPES; i++) { |
| 301 | if (vio_handler[i] != NULL) |
| 302 | (*vio_handler[i])(NULL); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | int vio_setHandler(int subtype, vio_event_handler_t *beh) |
| 307 | { |
| 308 | subtype = subtype >> VIOMAJOR_SUBTYPE_SHIFT; |
| 309 | if ((subtype < 0) || (subtype >= VIO_MAX_SUBTYPES)) |
| 310 | return -EINVAL; |
| 311 | if (vio_handler[subtype] != NULL) |
| 312 | return -EBUSY; |
| 313 | vio_handler[subtype] = beh; |
| 314 | return 0; |
| 315 | } |
| 316 | EXPORT_SYMBOL(vio_setHandler); |
| 317 | |
| 318 | int vio_clearHandler(int subtype) |
| 319 | { |
| 320 | subtype = subtype >> VIOMAJOR_SUBTYPE_SHIFT; |
| 321 | if ((subtype < 0) || (subtype >= VIO_MAX_SUBTYPES)) |
| 322 | return -EINVAL; |
| 323 | if (vio_handler[subtype] == NULL) |
| 324 | return -EAGAIN; |
| 325 | vio_handler[subtype] = NULL; |
| 326 | return 0; |
| 327 | } |
| 328 | EXPORT_SYMBOL(vio_clearHandler); |
| 329 | |
| 330 | static void handleConfig(struct HvLpEvent *event) |
| 331 | { |
| 332 | if (!event) |
| 333 | return; |
| 334 | if (event->xFlags.xFunction == HvLpEvent_Function_Int) { |
| 335 | printk(VIOPATH_KERN_WARN |
| 336 | "unexpected config request from partition %d", |
| 337 | event->xSourceLp); |
| 338 | |
| 339 | if ((event->xFlags.xFunction == HvLpEvent_Function_Int) && |
| 340 | (event->xFlags.xAckInd == HvLpEvent_AckInd_DoAck)) { |
| 341 | event->xRc = HvLpEvent_Rc_InvalidSubtype; |
| 342 | HvCallEvent_ackLpEvent(event); |
| 343 | } |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | up((struct semaphore *)event->xCorrelationToken); |
| 348 | } |
| 349 | |
| 350 | /* |
| 351 | * Initialization of the hosting partition |
| 352 | */ |
| 353 | void vio_set_hostlp(void) |
| 354 | { |
| 355 | /* |
| 356 | * If this has already been set then we DON'T want to either change |
| 357 | * it or re-register the proc file system |
| 358 | */ |
| 359 | if (viopath_hostLp != HvLpIndexInvalid) |
| 360 | return; |
| 361 | |
| 362 | /* |
| 363 | * Figure out our hosting partition. This isn't allowed to change |
| 364 | * while we're active |
| 365 | */ |
| 366 | viopath_ourLp = HvLpConfig_getLpIndex(); |
Stephen Rothwell | dd61ce9 | 2005-06-21 17:15:37 -0700 | [diff] [blame] | 367 | viopath_hostLp = HvLpConfig_getHostingLpIndex(viopath_ourLp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
| 369 | if (viopath_hostLp != HvLpIndexInvalid) |
| 370 | vio_setHandler(viomajorsubtype_config, handleConfig); |
| 371 | } |
| 372 | EXPORT_SYMBOL(vio_set_hostlp); |
| 373 | |
| 374 | static void vio_handleEvent(struct HvLpEvent *event, struct pt_regs *regs) |
| 375 | { |
| 376 | HvLpIndex remoteLp; |
| 377 | int subtype = (event->xSubtype & VIOMAJOR_SUBTYPE_MASK) |
| 378 | >> VIOMAJOR_SUBTYPE_SHIFT; |
| 379 | |
| 380 | if (event->xFlags.xFunction == HvLpEvent_Function_Int) { |
| 381 | remoteLp = event->xSourceLp; |
| 382 | /* |
| 383 | * The isActive is checked because if the hosting partition |
| 384 | * went down and came back up it would not be active but it |
| 385 | * would have different source and target instances, in which |
| 386 | * case we'd want to reset them. This case really protects |
| 387 | * against an unauthorized active partition sending interrupts |
| 388 | * or acks to this linux partition. |
| 389 | */ |
| 390 | if (viopathStatus[remoteLp].isActive |
| 391 | && (event->xSourceInstanceId != |
| 392 | viopathStatus[remoteLp].mTargetInst)) { |
| 393 | printk(VIOPATH_KERN_WARN |
| 394 | "message from invalid partition. " |
| 395 | "int msg rcvd, source inst (%d) doesnt match (%d)\n", |
| 396 | viopathStatus[remoteLp].mTargetInst, |
| 397 | event->xSourceInstanceId); |
| 398 | return; |
| 399 | } |
| 400 | |
| 401 | if (viopathStatus[remoteLp].isActive |
| 402 | && (event->xTargetInstanceId != |
| 403 | viopathStatus[remoteLp].mSourceInst)) { |
| 404 | printk(VIOPATH_KERN_WARN |
| 405 | "message from invalid partition. " |
| 406 | "int msg rcvd, target inst (%d) doesnt match (%d)\n", |
| 407 | viopathStatus[remoteLp].mSourceInst, |
| 408 | event->xTargetInstanceId); |
| 409 | return; |
| 410 | } |
| 411 | } else { |
| 412 | remoteLp = event->xTargetLp; |
| 413 | if (event->xSourceInstanceId != |
| 414 | viopathStatus[remoteLp].mSourceInst) { |
| 415 | printk(VIOPATH_KERN_WARN |
| 416 | "message from invalid partition. " |
| 417 | "ack msg rcvd, source inst (%d) doesnt match (%d)\n", |
| 418 | viopathStatus[remoteLp].mSourceInst, |
| 419 | event->xSourceInstanceId); |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | if (event->xTargetInstanceId != |
| 424 | viopathStatus[remoteLp].mTargetInst) { |
| 425 | printk(VIOPATH_KERN_WARN |
| 426 | "message from invalid partition. " |
| 427 | "viopath: ack msg rcvd, target inst (%d) doesnt match (%d)\n", |
| 428 | viopathStatus[remoteLp].mTargetInst, |
| 429 | event->xTargetInstanceId); |
| 430 | return; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | if (vio_handler[subtype] == NULL) { |
| 435 | printk(VIOPATH_KERN_WARN |
| 436 | "unexpected virtual io event subtype %d from partition %d\n", |
| 437 | event->xSubtype, remoteLp); |
| 438 | /* No handler. Ack if necessary */ |
| 439 | if ((event->xFlags.xFunction == HvLpEvent_Function_Int) && |
| 440 | (event->xFlags.xAckInd == HvLpEvent_AckInd_DoAck)) { |
| 441 | event->xRc = HvLpEvent_Rc_InvalidSubtype; |
| 442 | HvCallEvent_ackLpEvent(event); |
| 443 | } |
| 444 | return; |
| 445 | } |
| 446 | |
| 447 | /* This innocuous little line is where all the real work happens */ |
| 448 | (*vio_handler[subtype])(event); |
| 449 | } |
| 450 | |
| 451 | static void viopath_donealloc(void *parm, int number) |
| 452 | { |
| 453 | struct alloc_parms *parmsp = parm; |
| 454 | |
| 455 | parmsp->number = number; |
| 456 | if (parmsp->used_wait_atomic) |
| 457 | atomic_set(&parmsp->wait_atomic, 0); |
| 458 | else |
| 459 | up(&parmsp->sem); |
| 460 | } |
| 461 | |
| 462 | static int allocateEvents(HvLpIndex remoteLp, int numEvents) |
| 463 | { |
| 464 | struct alloc_parms parms; |
| 465 | |
| 466 | if (system_state != SYSTEM_RUNNING) { |
| 467 | parms.used_wait_atomic = 1; |
| 468 | atomic_set(&parms.wait_atomic, 1); |
| 469 | } else { |
| 470 | parms.used_wait_atomic = 0; |
| 471 | init_MUTEX_LOCKED(&parms.sem); |
| 472 | } |
| 473 | mf_allocate_lp_events(remoteLp, HvLpEvent_Type_VirtualIo, 250, /* It would be nice to put a real number here! */ |
| 474 | numEvents, &viopath_donealloc, &parms); |
| 475 | if (system_state != SYSTEM_RUNNING) { |
| 476 | while (atomic_read(&parms.wait_atomic)) |
| 477 | mb(); |
| 478 | } else |
| 479 | down(&parms.sem); |
| 480 | return parms.number; |
| 481 | } |
| 482 | |
| 483 | int viopath_open(HvLpIndex remoteLp, int subtype, int numReq) |
| 484 | { |
| 485 | int i; |
| 486 | unsigned long flags; |
| 487 | int tempNumAllocated; |
| 488 | |
Stephen Rothwell | c670b1ac | 2005-06-21 17:15:41 -0700 | [diff] [blame] | 489 | if ((remoteLp >= HVMAXARCHITECTEDLPS) || (remoteLp == HvLpIndexInvalid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | return -EINVAL; |
| 491 | |
| 492 | subtype = subtype >> VIOMAJOR_SUBTYPE_SHIFT; |
| 493 | if ((subtype < 0) || (subtype >= VIO_MAX_SUBTYPES)) |
| 494 | return -EINVAL; |
| 495 | |
| 496 | spin_lock_irqsave(&statuslock, flags); |
| 497 | |
| 498 | if (!event_buffer_initialised) { |
| 499 | for (i = 0; i < VIO_MAX_SUBTYPES; i++) |
| 500 | atomic_set(&event_buffer_available[i], 1); |
| 501 | event_buffer_initialised = 1; |
| 502 | } |
| 503 | |
| 504 | viopathStatus[remoteLp].users[subtype]++; |
| 505 | |
| 506 | if (!viopathStatus[remoteLp].isOpen) { |
| 507 | viopathStatus[remoteLp].isOpen = 1; |
| 508 | HvCallEvent_openLpEventPath(remoteLp, HvLpEvent_Type_VirtualIo); |
| 509 | |
| 510 | /* |
| 511 | * Don't hold the spinlock during an operation that |
| 512 | * can sleep. |
| 513 | */ |
| 514 | spin_unlock_irqrestore(&statuslock, flags); |
| 515 | tempNumAllocated = allocateEvents(remoteLp, 1); |
| 516 | spin_lock_irqsave(&statuslock, flags); |
| 517 | |
| 518 | viopathStatus[remoteLp].numberAllocated += tempNumAllocated; |
| 519 | |
| 520 | if (viopathStatus[remoteLp].numberAllocated == 0) { |
| 521 | HvCallEvent_closeLpEventPath(remoteLp, |
| 522 | HvLpEvent_Type_VirtualIo); |
| 523 | |
| 524 | spin_unlock_irqrestore(&statuslock, flags); |
| 525 | return -ENOMEM; |
| 526 | } |
| 527 | |
| 528 | viopathStatus[remoteLp].mSourceInst = |
| 529 | HvCallEvent_getSourceLpInstanceId(remoteLp, |
| 530 | HvLpEvent_Type_VirtualIo); |
| 531 | viopathStatus[remoteLp].mTargetInst = |
| 532 | HvCallEvent_getTargetLpInstanceId(remoteLp, |
| 533 | HvLpEvent_Type_VirtualIo); |
| 534 | HvLpEvent_registerHandler(HvLpEvent_Type_VirtualIo, |
| 535 | &vio_handleEvent); |
| 536 | sendMonMsg(remoteLp); |
| 537 | printk(VIOPATH_KERN_INFO "opening connection to partition %d, " |
| 538 | "setting sinst %d, tinst %d\n", |
| 539 | remoteLp, viopathStatus[remoteLp].mSourceInst, |
| 540 | viopathStatus[remoteLp].mTargetInst); |
| 541 | } |
| 542 | |
| 543 | spin_unlock_irqrestore(&statuslock, flags); |
| 544 | tempNumAllocated = allocateEvents(remoteLp, numReq); |
| 545 | spin_lock_irqsave(&statuslock, flags); |
| 546 | viopathStatus[remoteLp].numberAllocated += tempNumAllocated; |
| 547 | spin_unlock_irqrestore(&statuslock, flags); |
| 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | EXPORT_SYMBOL(viopath_open); |
| 552 | |
| 553 | int viopath_close(HvLpIndex remoteLp, int subtype, int numReq) |
| 554 | { |
| 555 | unsigned long flags; |
| 556 | int i; |
| 557 | int numOpen; |
| 558 | struct alloc_parms parms; |
| 559 | |
Stephen Rothwell | c670b1ac | 2005-06-21 17:15:41 -0700 | [diff] [blame] | 560 | if ((remoteLp >= HVMAXARCHITECTEDLPS) || (remoteLp == HvLpIndexInvalid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | return -EINVAL; |
| 562 | |
| 563 | subtype = subtype >> VIOMAJOR_SUBTYPE_SHIFT; |
| 564 | if ((subtype < 0) || (subtype >= VIO_MAX_SUBTYPES)) |
| 565 | return -EINVAL; |
| 566 | |
| 567 | spin_lock_irqsave(&statuslock, flags); |
| 568 | /* |
| 569 | * If the viopath_close somehow gets called before a |
| 570 | * viopath_open it could decrement to -1 which is a non |
| 571 | * recoverable state so we'll prevent this from |
| 572 | * happening. |
| 573 | */ |
| 574 | if (viopathStatus[remoteLp].users[subtype] > 0) |
| 575 | viopathStatus[remoteLp].users[subtype]--; |
| 576 | |
| 577 | spin_unlock_irqrestore(&statuslock, flags); |
| 578 | |
| 579 | parms.used_wait_atomic = 0; |
| 580 | init_MUTEX_LOCKED(&parms.sem); |
| 581 | mf_deallocate_lp_events(remoteLp, HvLpEvent_Type_VirtualIo, |
| 582 | numReq, &viopath_donealloc, &parms); |
| 583 | down(&parms.sem); |
| 584 | |
| 585 | spin_lock_irqsave(&statuslock, flags); |
| 586 | for (i = 0, numOpen = 0; i < VIO_MAX_SUBTYPES; i++) |
| 587 | numOpen += viopathStatus[remoteLp].users[i]; |
| 588 | |
| 589 | if ((viopathStatus[remoteLp].isOpen) && (numOpen == 0)) { |
| 590 | printk(VIOPATH_KERN_INFO "closing connection to partition %d", |
| 591 | remoteLp); |
| 592 | |
| 593 | HvCallEvent_closeLpEventPath(remoteLp, |
| 594 | HvLpEvent_Type_VirtualIo); |
| 595 | viopathStatus[remoteLp].isOpen = 0; |
| 596 | viopathStatus[remoteLp].isActive = 0; |
| 597 | |
| 598 | for (i = 0; i < VIO_MAX_SUBTYPES; i++) |
| 599 | atomic_set(&event_buffer_available[i], 0); |
| 600 | event_buffer_initialised = 0; |
| 601 | } |
| 602 | spin_unlock_irqrestore(&statuslock, flags); |
| 603 | return 0; |
| 604 | } |
| 605 | EXPORT_SYMBOL(viopath_close); |
| 606 | |
| 607 | void *vio_get_event_buffer(int subtype) |
| 608 | { |
| 609 | subtype = subtype >> VIOMAJOR_SUBTYPE_SHIFT; |
| 610 | if ((subtype < 0) || (subtype >= VIO_MAX_SUBTYPES)) |
| 611 | return NULL; |
| 612 | |
| 613 | if (atomic_dec_if_positive(&event_buffer_available[subtype]) == 0) |
| 614 | return &event_buffer[subtype * 256]; |
| 615 | else |
| 616 | return NULL; |
| 617 | } |
| 618 | EXPORT_SYMBOL(vio_get_event_buffer); |
| 619 | |
| 620 | void vio_free_event_buffer(int subtype, void *buffer) |
| 621 | { |
| 622 | subtype = subtype >> VIOMAJOR_SUBTYPE_SHIFT; |
| 623 | if ((subtype < 0) || (subtype >= VIO_MAX_SUBTYPES)) { |
| 624 | printk(VIOPATH_KERN_WARN |
| 625 | "unexpected subtype %d freeing event buffer\n", subtype); |
| 626 | return; |
| 627 | } |
| 628 | |
| 629 | if (atomic_read(&event_buffer_available[subtype]) != 0) { |
| 630 | printk(VIOPATH_KERN_WARN |
| 631 | "freeing unallocated event buffer, subtype %d\n", |
| 632 | subtype); |
| 633 | return; |
| 634 | } |
| 635 | |
| 636 | if (buffer != &event_buffer[subtype * 256]) { |
| 637 | printk(VIOPATH_KERN_WARN |
| 638 | "freeing invalid event buffer, subtype %d\n", subtype); |
| 639 | } |
| 640 | |
| 641 | atomic_set(&event_buffer_available[subtype], 1); |
| 642 | } |
| 643 | EXPORT_SYMBOL(vio_free_event_buffer); |
| 644 | |
| 645 | static const struct vio_error_entry vio_no_error = |
| 646 | { 0, 0, "Non-VIO Error" }; |
| 647 | static const struct vio_error_entry vio_unknown_error = |
| 648 | { 0, EIO, "Unknown Error" }; |
| 649 | |
| 650 | static const struct vio_error_entry vio_default_errors[] = { |
| 651 | {0x0001, EIO, "No Connection"}, |
| 652 | {0x0002, EIO, "No Receiver"}, |
| 653 | {0x0003, EIO, "No Buffer Available"}, |
| 654 | {0x0004, EBADRQC, "Invalid Message Type"}, |
| 655 | {0x0000, 0, NULL}, |
| 656 | }; |
| 657 | |
| 658 | const struct vio_error_entry *vio_lookup_rc( |
| 659 | const struct vio_error_entry *local_table, u16 rc) |
| 660 | { |
| 661 | const struct vio_error_entry *cur; |
| 662 | |
| 663 | if (!rc) |
| 664 | return &vio_no_error; |
| 665 | if (local_table) |
| 666 | for (cur = local_table; cur->rc; ++cur) |
| 667 | if (cur->rc == rc) |
| 668 | return cur; |
| 669 | for (cur = vio_default_errors; cur->rc; ++cur) |
| 670 | if (cur->rc == rc) |
| 671 | return cur; |
| 672 | return &vio_unknown_error; |
| 673 | } |
| 674 | EXPORT_SYMBOL(vio_lookup_rc); |