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/smp_lock.h>
40 #include <linux/interrupt.h>
42 #include <asm/system.h>
43 #include <asm/uaccess.h>
44 #include <asm/iseries/hv_types.h>
45 #include <asm/iseries/it_exp_vpd_panel.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
);
120 buf
= kmalloc(HW_PAGE_SIZE
, GFP_KERNEL
);
123 memset(buf
, 0, HW_PAGE_SIZE
);
125 handle
= dma_map_single(iSeries_vio_dev
, buf
, HW_PAGE_SIZE
,
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,
135 ((u64
)handle
) << 32, HW_PAGE_SIZE
, 0, 0);
137 if (hvrc
!= HvLpEvent_Rc_Good
)
138 printk(VIOPATH_KERN_WARN
"hv error on op %d\n", (int)hvrc
);
142 vlanMap
= HvLpConfig_getVirtualLanIndexMap();
144 buf
[HW_PAGE_SIZE
-1] = '\0';
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]));
156 dma_unmap_single(iSeries_vio_dev
, handle
, HW_PAGE_SIZE
,
163 static int proc_viopath_open(struct inode
*inode
, struct file
*file
)
165 return single_open(file
, proc_viopath_show
, NULL
);
168 static struct file_operations proc_viopath_operations
= {
169 .open
= proc_viopath_open
,
172 .release
= single_release
,
175 static int __init
vio_proc_init(void)
177 struct proc_dir_entry
*e
;
179 e
= create_proc_entry("iSeries/config", 0, NULL
);
181 e
->proc_fops
= &proc_viopath_operations
;
185 __initcall(vio_proc_init
);
187 /* See if a given LP is active. Allow for invalid lps to be passed in
188 * and just return invalid
190 int viopath_isactive(HvLpIndex lp
)
192 if (lp
== HvLpIndexInvalid
)
194 if (lp
< HVMAXARCHITECTEDLPS
)
195 return viopathStatus
[lp
].isActive
;
199 EXPORT_SYMBOL(viopath_isactive
);
202 * We cache the source and target instance ids for each
205 HvLpInstanceId
viopath_sourceinst(HvLpIndex lp
)
207 return viopathStatus
[lp
].mSourceInst
;
209 EXPORT_SYMBOL(viopath_sourceinst
);
211 HvLpInstanceId
viopath_targetinst(HvLpIndex lp
)
213 return viopathStatus
[lp
].mTargetInst
;
215 EXPORT_SYMBOL(viopath_targetinst
);
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.
223 static void sendMonMsg(HvLpIndex remoteLp
)
227 viopathStatus
[remoteLp
].mSourceInst
=
228 HvCallEvent_getSourceLpInstanceId(remoteLp
,
229 HvLpEvent_Type_VirtualIo
);
230 viopathStatus
[remoteLp
].mTargetInst
=
231 HvCallEvent_getTargetLpInstanceId(remoteLp
,
232 HvLpEvent_Type_VirtualIo
);
235 * Deliberately ignore the return code here. if we call this
236 * more than once, we don't care.
238 vio_setHandler(viomajorsubtype_monitor
, handleMonitorEvent
);
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);
247 if (hvrc
== HvLpEvent_Rc_Good
)
248 viopathStatus
[remoteLp
].isActive
= 1;
250 printk(VIOPATH_KERN_WARN
"could not connect to partition %d\n",
252 viopathStatus
[remoteLp
].isActive
= 0;
256 static void handleMonitorEvent(struct HvLpEvent
*event
)
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...
270 * First see if this is just a normal monitor message from the
273 if (hvlpevent_is_int(event
)) {
274 remoteLp
= event
->xSourceLp
;
275 if (!viopathStatus
[remoteLp
].isActive
)
276 sendMonMsg(remoteLp
);
281 * This path is for an acknowledgement; the other partition
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");
291 printk(VIOPATH_KERN_WARN
"partition %d ended\n", remoteLp
);
293 viopathStatus
[remoteLp
].isActive
= 0;
296 * For each active handler, pass them a NULL
297 * message to indicate that the other partition
300 for (i
= 0; i
< VIO_MAX_SUBTYPES
; i
++) {
301 if (vio_handler
[i
] != NULL
)
302 (*vio_handler
[i
])(NULL
);
306 int vio_setHandler(int subtype
, vio_event_handler_t
*beh
)
308 subtype
= subtype
>> VIOMAJOR_SUBTYPE_SHIFT
;
309 if ((subtype
< 0) || (subtype
>= VIO_MAX_SUBTYPES
))
311 if (vio_handler
[subtype
] != NULL
)
313 vio_handler
[subtype
] = beh
;
316 EXPORT_SYMBOL(vio_setHandler
);
318 int vio_clearHandler(int subtype
)
320 subtype
= subtype
>> VIOMAJOR_SUBTYPE_SHIFT
;
321 if ((subtype
< 0) || (subtype
>= VIO_MAX_SUBTYPES
))
323 if (vio_handler
[subtype
] == NULL
)
325 vio_handler
[subtype
] = NULL
;
328 EXPORT_SYMBOL(vio_clearHandler
);
330 static void handleConfig(struct HvLpEvent
*event
)
334 if (hvlpevent_is_int(event
)) {
335 printk(VIOPATH_KERN_WARN
336 "unexpected config request from partition %d",
339 if (hvlpevent_need_ack(event
)) {
340 event
->xRc
= HvLpEvent_Rc_InvalidSubtype
;
341 HvCallEvent_ackLpEvent(event
);
346 up((struct semaphore
*)event
->xCorrelationToken
);
350 * Initialization of the hosting partition
352 void vio_set_hostlp(void)
355 * If this has already been set then we DON'T want to either change
356 * it or re-register the proc file system
358 if (viopath_hostLp
!= HvLpIndexInvalid
)
362 * Figure out our hosting partition. This isn't allowed to change
365 viopath_ourLp
= HvLpConfig_getLpIndex();
366 viopath_hostLp
= HvLpConfig_getHostingLpIndex(viopath_ourLp
);
368 if (viopath_hostLp
!= HvLpIndexInvalid
)
369 vio_setHandler(viomajorsubtype_config
, handleConfig
);
371 EXPORT_SYMBOL(vio_set_hostlp
);
373 static void vio_handleEvent(struct HvLpEvent
*event
, struct pt_regs
*regs
)
376 int subtype
= (event
->xSubtype
& VIOMAJOR_SUBTYPE_MASK
)
377 >> VIOMAJOR_SUBTYPE_SHIFT
;
379 if (hvlpevent_is_int(event
)) {
380 remoteLp
= event
->xSourceLp
;
382 * The isActive is checked because if the hosting partition
383 * went down and came back up it would not be active but it
384 * would have different source and target instances, in which
385 * case we'd want to reset them. This case really protects
386 * against an unauthorized active partition sending interrupts
387 * or acks to this linux partition.
389 if (viopathStatus
[remoteLp
].isActive
390 && (event
->xSourceInstanceId
!=
391 viopathStatus
[remoteLp
].mTargetInst
)) {
392 printk(VIOPATH_KERN_WARN
393 "message from invalid partition. "
394 "int msg rcvd, source inst (%d) doesnt match (%d)\n",
395 viopathStatus
[remoteLp
].mTargetInst
,
396 event
->xSourceInstanceId
);
400 if (viopathStatus
[remoteLp
].isActive
401 && (event
->xTargetInstanceId
!=
402 viopathStatus
[remoteLp
].mSourceInst
)) {
403 printk(VIOPATH_KERN_WARN
404 "message from invalid partition. "
405 "int msg rcvd, target inst (%d) doesnt match (%d)\n",
406 viopathStatus
[remoteLp
].mSourceInst
,
407 event
->xTargetInstanceId
);
411 remoteLp
= event
->xTargetLp
;
412 if (event
->xSourceInstanceId
!=
413 viopathStatus
[remoteLp
].mSourceInst
) {
414 printk(VIOPATH_KERN_WARN
415 "message from invalid partition. "
416 "ack msg rcvd, source inst (%d) doesnt match (%d)\n",
417 viopathStatus
[remoteLp
].mSourceInst
,
418 event
->xSourceInstanceId
);
422 if (event
->xTargetInstanceId
!=
423 viopathStatus
[remoteLp
].mTargetInst
) {
424 printk(VIOPATH_KERN_WARN
425 "message from invalid partition. "
426 "viopath: ack msg rcvd, target inst (%d) doesnt match (%d)\n",
427 viopathStatus
[remoteLp
].mTargetInst
,
428 event
->xTargetInstanceId
);
433 if (vio_handler
[subtype
] == NULL
) {
434 printk(VIOPATH_KERN_WARN
435 "unexpected virtual io event subtype %d from partition %d\n",
436 event
->xSubtype
, remoteLp
);
437 /* No handler. Ack if necessary */
438 if (hvlpevent_is_int(event
) && hvlpevent_need_ack(event
)) {
439 event
->xRc
= HvLpEvent_Rc_InvalidSubtype
;
440 HvCallEvent_ackLpEvent(event
);
445 /* This innocuous little line is where all the real work happens */
446 (*vio_handler
[subtype
])(event
);
449 static void viopath_donealloc(void *parm
, int number
)
451 struct alloc_parms
*parmsp
= parm
;
453 parmsp
->number
= number
;
454 if (parmsp
->used_wait_atomic
)
455 atomic_set(&parmsp
->wait_atomic
, 0);
460 static int allocateEvents(HvLpIndex remoteLp
, int numEvents
)
462 struct alloc_parms parms
;
464 if (system_state
!= SYSTEM_RUNNING
) {
465 parms
.used_wait_atomic
= 1;
466 atomic_set(&parms
.wait_atomic
, 1);
468 parms
.used_wait_atomic
= 0;
469 init_MUTEX_LOCKED(&parms
.sem
);
471 mf_allocate_lp_events(remoteLp
, HvLpEvent_Type_VirtualIo
, 250, /* It would be nice to put a real number here! */
472 numEvents
, &viopath_donealloc
, &parms
);
473 if (system_state
!= SYSTEM_RUNNING
) {
474 while (atomic_read(&parms
.wait_atomic
))
481 int viopath_open(HvLpIndex remoteLp
, int subtype
, int numReq
)
485 int tempNumAllocated
;
487 if ((remoteLp
>= HVMAXARCHITECTEDLPS
) || (remoteLp
== HvLpIndexInvalid
))
490 subtype
= subtype
>> VIOMAJOR_SUBTYPE_SHIFT
;
491 if ((subtype
< 0) || (subtype
>= VIO_MAX_SUBTYPES
))
494 spin_lock_irqsave(&statuslock
, flags
);
496 if (!event_buffer_initialised
) {
497 for (i
= 0; i
< VIO_MAX_SUBTYPES
; i
++)
498 atomic_set(&event_buffer_available
[i
], 1);
499 event_buffer_initialised
= 1;
502 viopathStatus
[remoteLp
].users
[subtype
]++;
504 if (!viopathStatus
[remoteLp
].isOpen
) {
505 viopathStatus
[remoteLp
].isOpen
= 1;
506 HvCallEvent_openLpEventPath(remoteLp
, HvLpEvent_Type_VirtualIo
);
509 * Don't hold the spinlock during an operation that
512 spin_unlock_irqrestore(&statuslock
, flags
);
513 tempNumAllocated
= allocateEvents(remoteLp
, 1);
514 spin_lock_irqsave(&statuslock
, flags
);
516 viopathStatus
[remoteLp
].numberAllocated
+= tempNumAllocated
;
518 if (viopathStatus
[remoteLp
].numberAllocated
== 0) {
519 HvCallEvent_closeLpEventPath(remoteLp
,
520 HvLpEvent_Type_VirtualIo
);
522 spin_unlock_irqrestore(&statuslock
, flags
);
526 viopathStatus
[remoteLp
].mSourceInst
=
527 HvCallEvent_getSourceLpInstanceId(remoteLp
,
528 HvLpEvent_Type_VirtualIo
);
529 viopathStatus
[remoteLp
].mTargetInst
=
530 HvCallEvent_getTargetLpInstanceId(remoteLp
,
531 HvLpEvent_Type_VirtualIo
);
532 HvLpEvent_registerHandler(HvLpEvent_Type_VirtualIo
,
534 sendMonMsg(remoteLp
);
535 printk(VIOPATH_KERN_INFO
"opening connection to partition %d, "
536 "setting sinst %d, tinst %d\n",
537 remoteLp
, viopathStatus
[remoteLp
].mSourceInst
,
538 viopathStatus
[remoteLp
].mTargetInst
);
541 spin_unlock_irqrestore(&statuslock
, flags
);
542 tempNumAllocated
= allocateEvents(remoteLp
, numReq
);
543 spin_lock_irqsave(&statuslock
, flags
);
544 viopathStatus
[remoteLp
].numberAllocated
+= tempNumAllocated
;
545 spin_unlock_irqrestore(&statuslock
, flags
);
549 EXPORT_SYMBOL(viopath_open
);
551 int viopath_close(HvLpIndex remoteLp
, int subtype
, int numReq
)
556 struct alloc_parms parms
;
558 if ((remoteLp
>= HVMAXARCHITECTEDLPS
) || (remoteLp
== HvLpIndexInvalid
))
561 subtype
= subtype
>> VIOMAJOR_SUBTYPE_SHIFT
;
562 if ((subtype
< 0) || (subtype
>= VIO_MAX_SUBTYPES
))
565 spin_lock_irqsave(&statuslock
, flags
);
567 * If the viopath_close somehow gets called before a
568 * viopath_open it could decrement to -1 which is a non
569 * recoverable state so we'll prevent this from
572 if (viopathStatus
[remoteLp
].users
[subtype
] > 0)
573 viopathStatus
[remoteLp
].users
[subtype
]--;
575 spin_unlock_irqrestore(&statuslock
, flags
);
577 parms
.used_wait_atomic
= 0;
578 init_MUTEX_LOCKED(&parms
.sem
);
579 mf_deallocate_lp_events(remoteLp
, HvLpEvent_Type_VirtualIo
,
580 numReq
, &viopath_donealloc
, &parms
);
583 spin_lock_irqsave(&statuslock
, flags
);
584 for (i
= 0, numOpen
= 0; i
< VIO_MAX_SUBTYPES
; i
++)
585 numOpen
+= viopathStatus
[remoteLp
].users
[i
];
587 if ((viopathStatus
[remoteLp
].isOpen
) && (numOpen
== 0)) {
588 printk(VIOPATH_KERN_INFO
"closing connection to partition %d",
591 HvCallEvent_closeLpEventPath(remoteLp
,
592 HvLpEvent_Type_VirtualIo
);
593 viopathStatus
[remoteLp
].isOpen
= 0;
594 viopathStatus
[remoteLp
].isActive
= 0;
596 for (i
= 0; i
< VIO_MAX_SUBTYPES
; i
++)
597 atomic_set(&event_buffer_available
[i
], 0);
598 event_buffer_initialised
= 0;
600 spin_unlock_irqrestore(&statuslock
, flags
);
603 EXPORT_SYMBOL(viopath_close
);
605 void *vio_get_event_buffer(int subtype
)
607 subtype
= subtype
>> VIOMAJOR_SUBTYPE_SHIFT
;
608 if ((subtype
< 0) || (subtype
>= VIO_MAX_SUBTYPES
))
611 if (atomic_dec_if_positive(&event_buffer_available
[subtype
]) == 0)
612 return &event_buffer
[subtype
* 256];
616 EXPORT_SYMBOL(vio_get_event_buffer
);
618 void vio_free_event_buffer(int subtype
, void *buffer
)
620 subtype
= subtype
>> VIOMAJOR_SUBTYPE_SHIFT
;
621 if ((subtype
< 0) || (subtype
>= VIO_MAX_SUBTYPES
)) {
622 printk(VIOPATH_KERN_WARN
623 "unexpected subtype %d freeing event buffer\n", subtype
);
627 if (atomic_read(&event_buffer_available
[subtype
]) != 0) {
628 printk(VIOPATH_KERN_WARN
629 "freeing unallocated event buffer, subtype %d\n",
634 if (buffer
!= &event_buffer
[subtype
* 256]) {
635 printk(VIOPATH_KERN_WARN
636 "freeing invalid event buffer, subtype %d\n", subtype
);
639 atomic_set(&event_buffer_available
[subtype
], 1);
641 EXPORT_SYMBOL(vio_free_event_buffer
);
643 static const struct vio_error_entry vio_no_error
=
644 { 0, 0, "Non-VIO Error" };
645 static const struct vio_error_entry vio_unknown_error
=
646 { 0, EIO
, "Unknown Error" };
648 static const struct vio_error_entry vio_default_errors
[] = {
649 {0x0001, EIO
, "No Connection"},
650 {0x0002, EIO
, "No Receiver"},
651 {0x0003, EIO
, "No Buffer Available"},
652 {0x0004, EBADRQC
, "Invalid Message Type"},
656 const struct vio_error_entry
*vio_lookup_rc(
657 const struct vio_error_entry
*local_table
, u16 rc
)
659 const struct vio_error_entry
*cur
;
662 return &vio_no_error
;
664 for (cur
= local_table
; cur
->rc
; ++cur
)
667 for (cur
= vio_default_errors
; cur
->rc
; ++cur
)
670 return &vio_unknown_error
;
672 EXPORT_SYMBOL(vio_lookup_rc
);