3 * iSeries Virtual I/O Message Path code
5 * Authors: Dave Boutcher <boutcher@us.ibm.com>
6 * Ryan Arnold <ryanarn@us.ibm.com>
7 * Colin Devilbiss <devilbis@us.ibm.com>
9 * (C) Copyright 2000-2005 IBM Corporation
11 * This code is used by the iSeries virtual disk, cd,
12 * tape, and console to communicate with OS/400 in another
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.
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.
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
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/interrupt.h>
41 #include <asm/system.h>
42 #include <asm/uaccess.h>
44 #include <asm/firmware.h>
45 #include <asm/iseries/hv_types.h>
46 #include <asm/iseries/hv_lp_event.h>
47 #include <asm/iseries/hv_lp_config.h>
48 #include <asm/iseries/mf.h>
49 #include <asm/iseries/vio.h>
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.
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;
63 } viopathStatus[HVMAXARCHITECTEDLPS];
65 static DEFINE_SPINLOCK(statuslock);
68 * For each kind of event we allocate a buffer that is
69 * guaranteed not to cross a page boundary
71 static unsigned char event_buffer[VIO_MAX_SUBTYPES * 256]
72 __attribute__((__aligned__(4096)));
73 static atomic_t event_buffer_available[VIO_MAX_SUBTYPES];
74 static int event_buffer_initialised;
76 static void handleMonitorEvent(struct HvLpEvent *event);
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 ...
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.
94 static u8 viomonseq = 22;
96 /* Our hosting logical partition. We get this at startup
97 * time, and different modules access this variable directly.
99 HvLpIndex viopath_hostLp = HvLpIndexInvalid;
100 EXPORT_SYMBOL(viopath_hostLp);
101 HvLpIndex viopath_ourLp = HvLpIndexInvalid;
102 EXPORT_SYMBOL(viopath_ourLp);
104 /* For each kind of incoming event we set a pointer to a
107 static vio_event_handler_t *vio_handler[VIO_MAX_SUBTYPES];
109 #define VIOPATH_KERN_WARN KERN_WARNING "viopath: "
110 #define VIOPATH_KERN_INFO KERN_INFO "viopath: "
112 static int proc_viopath_show(struct seq_file *m, void *v)
118 DECLARE_MUTEX_LOCKED(Semaphore);
119 struct device_node *node;
122 buf = kzalloc(HW_PAGE_SIZE, GFP_KERNEL);
126 handle = dma_map_single(iSeries_vio_dev, buf, HW_PAGE_SIZE,
129 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
130 HvLpEvent_Type_VirtualIo,
131 viomajorsubtype_config | vioconfigget,
132 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
133 viopath_sourceinst(viopath_hostLp),
134 viopath_targetinst(viopath_hostLp),
135 (u64)(unsigned long)&Semaphore, VIOVERSION << 16,
136 ((u64)handle) << 32, HW_PAGE_SIZE, 0, 0);
138 if (hvrc != HvLpEvent_Rc_Good)
139 printk(VIOPATH_KERN_WARN "hv error on op %d\n", (int)hvrc);
143 vlanMap = HvLpConfig_getVirtualLanIndexMap();
145 buf[HW_PAGE_SIZE-1] = '\0';
146 seq_printf(m, "%s", buf);
148 dma_unmap_single(iSeries_vio_dev, handle, HW_PAGE_SIZE,
152 seq_printf(m, "AVAILABLE_VETH=%x\n", vlanMap);
154 node = of_find_node_by_path("/");
157 sysid = of_get_property(node, "system-id", NULL);
160 seq_printf(m, "SRLNBR=<UNKNOWN>\n");
162 /* Skip "IBM," on front of serial number, see dt.c */
163 seq_printf(m, "SRLNBR=%s\n", sysid + 4);
170 static int proc_viopath_open(struct inode *inode, struct file *file)
172 return single_open(file, proc_viopath_show, NULL);
175 static const struct file_operations proc_viopath_operations = {
176 .open = proc_viopath_open,
179 .release = single_release,
182 static int __init vio_proc_init(void)
184 struct proc_dir_entry *e;
186 if (!firmware_has_feature(FW_FEATURE_ISERIES))
189 e = create_proc_entry("iSeries/config", 0, NULL);
191 e->proc_fops = &proc_viopath_operations;
195 __initcall(vio_proc_init);
197 /* See if a given LP is active. Allow for invalid lps to be passed in
198 * and just return invalid
200 int viopath_isactive(HvLpIndex lp)
202 if (lp == HvLpIndexInvalid)
204 if (lp < HVMAXARCHITECTEDLPS)
205 return viopathStatus[lp].isActive;
209 EXPORT_SYMBOL(viopath_isactive);
212 * We cache the source and target instance ids for each
215 HvLpInstanceId viopath_sourceinst(HvLpIndex lp)
217 return viopathStatus[lp].mSourceInst;
219 EXPORT_SYMBOL(viopath_sourceinst);
221 HvLpInstanceId viopath_targetinst(HvLpIndex lp)
223 return viopathStatus[lp].mTargetInst;
225 EXPORT_SYMBOL(viopath_targetinst);
228 * Send a monitor message. This is a message with the acknowledge
229 * bit on that the other side will NOT explicitly acknowledge. When
230 * the other side goes down, the hypervisor will acknowledge any
231 * outstanding messages....so we will know when the other side dies.
233 static void sendMonMsg(HvLpIndex remoteLp)
237 viopathStatus[remoteLp].mSourceInst =
238 HvCallEvent_getSourceLpInstanceId(remoteLp,
239 HvLpEvent_Type_VirtualIo);
240 viopathStatus[remoteLp].mTargetInst =
241 HvCallEvent_getTargetLpInstanceId(remoteLp,
242 HvLpEvent_Type_VirtualIo);
245 * Deliberately ignore the return code here. if we call this
246 * more than once, we don't care.
248 vio_setHandler(viomajorsubtype_monitor, handleMonitorEvent);
250 hvrc = HvCallEvent_signalLpEventFast(remoteLp, HvLpEvent_Type_VirtualIo,
251 viomajorsubtype_monitor, HvLpEvent_AckInd_DoAck,
252 HvLpEvent_AckType_DeferredAck,
253 viopathStatus[remoteLp].mSourceInst,
254 viopathStatus[remoteLp].mTargetInst,
255 viomonseq++, 0, 0, 0, 0, 0);
257 if (hvrc == HvLpEvent_Rc_Good)
258 viopathStatus[remoteLp].isActive = 1;
260 printk(VIOPATH_KERN_WARN "could not connect to partition %d\n",
262 viopathStatus[remoteLp].isActive = 0;
266 static void handleMonitorEvent(struct HvLpEvent *event)
272 * This handler is _also_ called as part of the loop
273 * at the end of this routine, so it must be able to
274 * ignore NULL events...
280 * First see if this is just a normal monitor message from the
283 if (hvlpevent_is_int(event)) {
284 remoteLp = event->xSourceLp;
285 if (!viopathStatus[remoteLp].isActive)
286 sendMonMsg(remoteLp);
291 * This path is for an acknowledgement; the other partition
294 remoteLp = event->xTargetLp;
295 if ((event->xSourceInstanceId != viopathStatus[remoteLp].mSourceInst) ||
296 (event->xTargetInstanceId != viopathStatus[remoteLp].mTargetInst)) {
297 printk(VIOPATH_KERN_WARN "ignoring ack....mismatched instances\n");
301 printk(VIOPATH_KERN_WARN "partition %d ended\n", remoteLp);
303 viopathStatus[remoteLp].isActive = 0;
306 * For each active handler, pass them a NULL
307 * message to indicate that the other partition
310 for (i = 0; i < VIO_MAX_SUBTYPES; i++) {
311 if (vio_handler[i] != NULL)
312 (*vio_handler[i])(NULL);
316 int vio_setHandler(int subtype, vio_event_handler_t *beh)
318 subtype = subtype >> VIOMAJOR_SUBTYPE_SHIFT;
319 if ((subtype < 0) || (subtype >= VIO_MAX_SUBTYPES))
321 if (vio_handler[subtype] != NULL)
323 vio_handler[subtype] = beh;
326 EXPORT_SYMBOL(vio_setHandler);
328 int vio_clearHandler(int subtype)
330 subtype = subtype >> VIOMAJOR_SUBTYPE_SHIFT;
331 if ((subtype < 0) || (subtype >= VIO_MAX_SUBTYPES))
333 if (vio_handler[subtype] == NULL)
335 vio_handler[subtype] = NULL;
338 EXPORT_SYMBOL(vio_clearHandler);
340 static void handleConfig(struct HvLpEvent *event)
344 if (hvlpevent_is_int(event)) {
345 printk(VIOPATH_KERN_WARN
346 "unexpected config request from partition %d",
349 if (hvlpevent_need_ack(event)) {
350 event->xRc = HvLpEvent_Rc_InvalidSubtype;
351 HvCallEvent_ackLpEvent(event);
356 up((struct semaphore *)event->xCorrelationToken);
360 * Initialization of the hosting partition
362 void vio_set_hostlp(void)
365 * If this has already been set then we DON'T want to either change
366 * it or re-register the proc file system
368 if (viopath_hostLp != HvLpIndexInvalid)
372 * Figure out our hosting partition. This isn't allowed to change
375 viopath_ourLp = HvLpConfig_getLpIndex();
376 viopath_hostLp = HvLpConfig_getHostingLpIndex(viopath_ourLp);
378 if (viopath_hostLp != HvLpIndexInvalid)
379 vio_setHandler(viomajorsubtype_config, handleConfig);
381 EXPORT_SYMBOL(vio_set_hostlp);
383 static void vio_handleEvent(struct HvLpEvent *event)
386 int subtype = (event->xSubtype & VIOMAJOR_SUBTYPE_MASK)
387 >> VIOMAJOR_SUBTYPE_SHIFT;
389 if (hvlpevent_is_int(event)) {
390 remoteLp = event->xSourceLp;
392 * The isActive is checked because if the hosting partition
393 * went down and came back up it would not be active but it
394 * would have different source and target instances, in which
395 * case we'd want to reset them. This case really protects
396 * against an unauthorized active partition sending interrupts
397 * or acks to this linux partition.
399 if (viopathStatus[remoteLp].isActive
400 && (event->xSourceInstanceId !=
401 viopathStatus[remoteLp].mTargetInst)) {
402 printk(VIOPATH_KERN_WARN
403 "message from invalid partition. "
404 "int msg rcvd, source inst (%d) doesnt match (%d)\n",
405 viopathStatus[remoteLp].mTargetInst,
406 event->xSourceInstanceId);
410 if (viopathStatus[remoteLp].isActive
411 && (event->xTargetInstanceId !=
412 viopathStatus[remoteLp].mSourceInst)) {
413 printk(VIOPATH_KERN_WARN
414 "message from invalid partition. "
415 "int msg rcvd, target inst (%d) doesnt match (%d)\n",
416 viopathStatus[remoteLp].mSourceInst,
417 event->xTargetInstanceId);
421 remoteLp = event->xTargetLp;
422 if (event->xSourceInstanceId !=
423 viopathStatus[remoteLp].mSourceInst) {
424 printk(VIOPATH_KERN_WARN
425 "message from invalid partition. "
426 "ack msg rcvd, source inst (%d) doesnt match (%d)\n",
427 viopathStatus[remoteLp].mSourceInst,
428 event->xSourceInstanceId);
432 if (event->xTargetInstanceId !=
433 viopathStatus[remoteLp].mTargetInst) {
434 printk(VIOPATH_KERN_WARN
435 "message from invalid partition. "
436 "viopath: ack msg rcvd, target inst (%d) doesnt match (%d)\n",
437 viopathStatus[remoteLp].mTargetInst,
438 event->xTargetInstanceId);
443 if (vio_handler[subtype] == NULL) {
444 printk(VIOPATH_KERN_WARN
445 "unexpected virtual io event subtype %d from partition %d\n",
446 event->xSubtype, remoteLp);
447 /* No handler. Ack if necessary */
448 if (hvlpevent_is_int(event) && hvlpevent_need_ack(event)) {
449 event->xRc = HvLpEvent_Rc_InvalidSubtype;
450 HvCallEvent_ackLpEvent(event);
455 /* This innocuous little line is where all the real work happens */
456 (*vio_handler[subtype])(event);
459 static void viopath_donealloc(void *parm, int number)
461 struct alloc_parms *parmsp = parm;
463 parmsp->number = number;
464 if (parmsp->used_wait_atomic)
465 atomic_set(&parmsp->wait_atomic, 0);
470 static int allocateEvents(HvLpIndex remoteLp, int numEvents)
472 struct alloc_parms parms;
474 if (system_state != SYSTEM_RUNNING) {
475 parms.used_wait_atomic = 1;
476 atomic_set(&parms.wait_atomic, 1);
478 parms.used_wait_atomic = 0;
479 init_MUTEX_LOCKED(&parms.sem);
481 mf_allocate_lp_events(remoteLp, HvLpEvent_Type_VirtualIo, 250, /* It would be nice to put a real number here! */
482 numEvents, &viopath_donealloc, &parms);
483 if (system_state != SYSTEM_RUNNING) {
484 while (atomic_read(&parms.wait_atomic))
491 int viopath_open(HvLpIndex remoteLp, int subtype, int numReq)
495 int tempNumAllocated;
497 if ((remoteLp >= HVMAXARCHITECTEDLPS) || (remoteLp == HvLpIndexInvalid))
500 subtype = subtype >> VIOMAJOR_SUBTYPE_SHIFT;
501 if ((subtype < 0) || (subtype >= VIO_MAX_SUBTYPES))
504 spin_lock_irqsave(&statuslock, flags);
506 if (!event_buffer_initialised) {
507 for (i = 0; i < VIO_MAX_SUBTYPES; i++)
508 atomic_set(&event_buffer_available[i], 1);
509 event_buffer_initialised = 1;
512 viopathStatus[remoteLp].users[subtype]++;
514 if (!viopathStatus[remoteLp].isOpen) {
515 viopathStatus[remoteLp].isOpen = 1;
516 HvCallEvent_openLpEventPath(remoteLp, HvLpEvent_Type_VirtualIo);
519 * Don't hold the spinlock during an operation that
522 spin_unlock_irqrestore(&statuslock, flags);
523 tempNumAllocated = allocateEvents(remoteLp, 1);
524 spin_lock_irqsave(&statuslock, flags);
526 viopathStatus[remoteLp].numberAllocated += tempNumAllocated;
528 if (viopathStatus[remoteLp].numberAllocated == 0) {
529 HvCallEvent_closeLpEventPath(remoteLp,
530 HvLpEvent_Type_VirtualIo);
532 spin_unlock_irqrestore(&statuslock, flags);
536 viopathStatus[remoteLp].mSourceInst =
537 HvCallEvent_getSourceLpInstanceId(remoteLp,
538 HvLpEvent_Type_VirtualIo);
539 viopathStatus[remoteLp].mTargetInst =
540 HvCallEvent_getTargetLpInstanceId(remoteLp,
541 HvLpEvent_Type_VirtualIo);
542 HvLpEvent_registerHandler(HvLpEvent_Type_VirtualIo,
544 sendMonMsg(remoteLp);
545 printk(VIOPATH_KERN_INFO "opening connection to partition %d, "
546 "setting sinst %d, tinst %d\n",
547 remoteLp, viopathStatus[remoteLp].mSourceInst,
548 viopathStatus[remoteLp].mTargetInst);
551 spin_unlock_irqrestore(&statuslock, flags);
552 tempNumAllocated = allocateEvents(remoteLp, numReq);
553 spin_lock_irqsave(&statuslock, flags);
554 viopathStatus[remoteLp].numberAllocated += tempNumAllocated;
555 spin_unlock_irqrestore(&statuslock, flags);
559 EXPORT_SYMBOL(viopath_open);
561 int viopath_close(HvLpIndex remoteLp, int subtype, int numReq)
566 struct alloc_parms parms;
568 if ((remoteLp >= HVMAXARCHITECTEDLPS) || (remoteLp == HvLpIndexInvalid))
571 subtype = subtype >> VIOMAJOR_SUBTYPE_SHIFT;
572 if ((subtype < 0) || (subtype >= VIO_MAX_SUBTYPES))
575 spin_lock_irqsave(&statuslock, flags);
577 * If the viopath_close somehow gets called before a
578 * viopath_open it could decrement to -1 which is a non
579 * recoverable state so we'll prevent this from
582 if (viopathStatus[remoteLp].users[subtype] > 0)
583 viopathStatus[remoteLp].users[subtype]--;
585 spin_unlock_irqrestore(&statuslock, flags);
587 parms.used_wait_atomic = 0;
588 init_MUTEX_LOCKED(&parms.sem);
589 mf_deallocate_lp_events(remoteLp, HvLpEvent_Type_VirtualIo,
590 numReq, &viopath_donealloc, &parms);
593 spin_lock_irqsave(&statuslock, flags);
594 for (i = 0, numOpen = 0; i < VIO_MAX_SUBTYPES; i++)
595 numOpen += viopathStatus[remoteLp].users[i];
597 if ((viopathStatus[remoteLp].isOpen) && (numOpen == 0)) {
598 printk(VIOPATH_KERN_INFO "closing connection to partition %d",
601 HvCallEvent_closeLpEventPath(remoteLp,
602 HvLpEvent_Type_VirtualIo);
603 viopathStatus[remoteLp].isOpen = 0;
604 viopathStatus[remoteLp].isActive = 0;
606 for (i = 0; i < VIO_MAX_SUBTYPES; i++)
607 atomic_set(&event_buffer_available[i], 0);
608 event_buffer_initialised = 0;
610 spin_unlock_irqrestore(&statuslock, flags);
613 EXPORT_SYMBOL(viopath_close);
615 void *vio_get_event_buffer(int subtype)
617 subtype = subtype >> VIOMAJOR_SUBTYPE_SHIFT;
618 if ((subtype < 0) || (subtype >= VIO_MAX_SUBTYPES))
621 if (atomic_dec_if_positive(&event_buffer_available[subtype]) == 0)
622 return &event_buffer[subtype * 256];
626 EXPORT_SYMBOL(vio_get_event_buffer);
628 void vio_free_event_buffer(int subtype, void *buffer)
630 subtype = subtype >> VIOMAJOR_SUBTYPE_SHIFT;
631 if ((subtype < 0) || (subtype >= VIO_MAX_SUBTYPES)) {
632 printk(VIOPATH_KERN_WARN
633 "unexpected subtype %d freeing event buffer\n", subtype);
637 if (atomic_read(&event_buffer_available[subtype]) != 0) {
638 printk(VIOPATH_KERN_WARN
639 "freeing unallocated event buffer, subtype %d\n",
644 if (buffer != &event_buffer[subtype * 256]) {
645 printk(VIOPATH_KERN_WARN
646 "freeing invalid event buffer, subtype %d\n", subtype);
649 atomic_set(&event_buffer_available[subtype], 1);
651 EXPORT_SYMBOL(vio_free_event_buffer);
653 static const struct vio_error_entry vio_no_error =
654 { 0, 0, "Non-VIO Error" };
655 static const struct vio_error_entry vio_unknown_error =
656 { 0, EIO, "Unknown Error" };
658 static const struct vio_error_entry vio_default_errors[] = {
659 {0x0001, EIO, "No Connection"},
660 {0x0002, EIO, "No Receiver"},
661 {0x0003, EIO, "No Buffer Available"},
662 {0x0004, EBADRQC, "Invalid Message Type"},
666 const struct vio_error_entry *vio_lookup_rc(
667 const struct vio_error_entry *local_table, u16 rc)
669 const struct vio_error_entry *cur;
672 return &vio_no_error;
674 for (cur = local_table; cur->rc; ++cur)
677 for (cur = vio_default_errors; cur->rc; ++cur)
680 return &vio_unknown_error;
682 EXPORT_SYMBOL(vio_lookup_rc);