2 * Legacy iSeries specific vio initialisation
3 * that needs to be built in (not a module).
5 * © Copyright 2007 IBM Corporation
6 * Author: Stephen Rothwell
7 * Some parts collected from various other files
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/completion.h>
27 #include <linux/proc_fs.h>
28 #include <linux/module.h>
30 #include <asm/firmware.h>
32 #include <asm/iseries/vio.h>
33 #include <asm/iseries/iommu.h>
34 #include <asm/iseries/hv_types.h>
35 #include <asm/iseries/hv_lp_event.h>
39 #define FIRST_VSCSI (FIRST_VTY + NUM_VTYS)
41 #define FIRST_VLAN (FIRST_VSCSI + NUM_VSCSIS)
42 #define NUM_VLANS HVMAXARCHITECTEDVIRTUALLANS
43 #define FIRST_VIODASD (FIRST_VLAN + NUM_VLANS)
44 #define NUM_VIODASDS HVMAXARCHITECTEDVIRTUALDISKS
45 #define FIRST_VIOCD (FIRST_VIODASD + NUM_VIODASDS)
46 #define NUM_VIOCDS HVMAXARCHITECTEDVIRTUALCDROMS
47 #define FIRST_VIOTAPE (FIRST_VIOCD + NUM_VIOCDS)
48 #define NUM_VIOTAPES HVMAXARCHITECTEDVIRTUALTAPES
50 struct vio_waitevent
{
51 struct completion com
;
62 static struct property
*new_property(const char *name
, int length
,
65 struct property
*np
= kzalloc(sizeof(*np
) + strlen(name
) + 1 + length
,
70 np
->name
= (char *)(np
+ 1);
71 np
->value
= np
->name
+ strlen(name
) + 1;
72 strcpy(np
->name
, name
);
73 memcpy(np
->value
, value
, length
);
78 static void free_property(struct property
*np
)
83 static struct device_node
*new_node(const char *path
,
84 struct device_node
*parent
)
86 struct device_node
*np
= kzalloc(sizeof(*np
), GFP_KERNEL
);
90 np
->full_name
= kstrdup(path
, GFP_KERNEL
);
95 of_node_set_flag(np
, OF_DYNAMIC
);
97 np
->parent
= of_node_get(parent
);
101 static void free_node(struct device_node
*np
)
103 struct property
*next
;
104 struct property
*prop
;
106 next
= np
->properties
;
112 of_node_put(np
->parent
);
113 kfree(np
->full_name
);
117 static int add_string_property(struct device_node
*np
, const char *name
,
120 struct property
*nprop
= new_property(name
, strlen(value
) + 1, value
);
124 prom_add_property(np
, nprop
);
128 static int add_raw_property(struct device_node
*np
, const char *name
,
129 int length
, const void *value
)
131 struct property
*nprop
= new_property(name
, length
, value
);
135 prom_add_property(np
, nprop
);
139 static struct device_node
*do_device_node(struct device_node
*parent
,
140 const char *name
, u32 reg
, u32 unit
, const char *type
,
141 const char *compat
, struct vio_resource
*res
)
143 struct device_node
*np
;
146 snprintf(path
, sizeof(path
), "/vdevice/%s@%08x", name
, reg
);
147 np
= new_node(path
, parent
);
150 if (!add_string_property(np
, "name", name
) ||
151 !add_string_property(np
, "device_type", type
) ||
152 !add_string_property(np
, "compatible", compat
) ||
153 !add_raw_property(np
, "reg", sizeof(reg
), ®
) ||
154 !add_raw_property(np
, "linux,unit_address",
155 sizeof(unit
), &unit
)) {
159 if (!add_raw_property(np
, "linux,vio_rsrcname",
160 sizeof(res
->rsrcname
), res
->rsrcname
) ||
161 !add_raw_property(np
, "linux,vio_type",
162 sizeof(res
->type
), res
->type
) ||
163 !add_raw_property(np
, "linux,vio_model",
164 sizeof(res
->model
), res
->model
))
167 np
->name
= of_get_property(np
, "name", NULL
);
168 np
->type
= of_get_property(np
, "device_type", NULL
);
170 #ifdef CONFIG_PROC_DEVICETREE
172 struct proc_dir_entry
*ent
;
174 ent
= proc_mkdir(strrchr(np
->full_name
, '/') + 1, parent
->pde
);
176 proc_device_tree_add_node(np
, ent
);
187 * This is here so that we can dynamically add viodasd
188 * devices without exposing all the above infrastructure.
190 struct vio_dev
*vio_create_viodasd(u32 unit
)
192 struct device_node
*vio_root
;
193 struct device_node
*np
;
194 struct vio_dev
*vdev
= NULL
;
196 vio_root
= of_find_node_by_path("/vdevice");
199 np
= do_device_node(vio_root
, "viodasd", FIRST_VIODASD
+ unit
, unit
,
200 "block", "IBM,iSeries-viodasd", NULL
);
201 of_node_put(vio_root
);
203 vdev
= vio_register_device_node(np
);
209 EXPORT_SYMBOL_GPL(vio_create_viodasd
);
211 static void __init
handle_block_event(struct HvLpEvent
*event
)
213 struct vioblocklpevent
*bevent
= (struct vioblocklpevent
*)event
;
214 struct vio_waitevent
*pwe
;
217 /* Notification that a partition went away! */
219 /* First, we should NEVER get an int here...only acks */
220 if (hvlpevent_is_int(event
)) {
221 printk(KERN_WARNING
"handle_viod_request: "
222 "Yikes! got an int in viodasd event handler!\n");
223 if (hvlpevent_need_ack(event
)) {
224 event
->xRc
= HvLpEvent_Rc_InvalidSubtype
;
225 HvCallEvent_ackLpEvent(event
);
230 switch (event
->xSubtype
& VIOMINOR_SUBTYPE_MASK
) {
233 * Handle a response to an open request. We get all the
234 * disk information in the response, so update it. The
235 * correlation token contains a pointer to a waitevent
236 * structure that has a completion in it. update the
237 * return code in the waitevent structure and post the
238 * completion to wake up the guy who sent the request
240 pwe
= (struct vio_waitevent
*)event
->xCorrelationToken
;
241 pwe
->rc
= event
->xRc
;
242 pwe
->sub_result
= bevent
->sub_result
;
248 printk(KERN_WARNING
"handle_viod_request: unexpected subtype!");
249 if (hvlpevent_need_ack(event
)) {
250 event
->xRc
= HvLpEvent_Rc_InvalidSubtype
;
251 HvCallEvent_ackLpEvent(event
);
256 static void __init
probe_disk(struct device_node
*vio_root
, u32 unit
)
259 struct vio_waitevent we
;
263 init_completion(&we
.com
);
265 /* Send the open event to OS/400 */
266 hvrc
= HvCallEvent_signalLpEventFast(viopath_hostLp
,
267 HvLpEvent_Type_VirtualIo
,
268 viomajorsubtype_blockio
| vioblockopen
,
269 HvLpEvent_AckInd_DoAck
, HvLpEvent_AckType_ImmediateAck
,
270 viopath_sourceinst(viopath_hostLp
),
271 viopath_targetinst(viopath_hostLp
),
272 (u64
)(unsigned long)&we
, VIOVERSION
<< 16,
273 ((u64
)unit
<< 48) | ((u64
)flags
<< 32),
276 printk(KERN_WARNING
"probe_disk: bad rc on HV open %d\n",
281 wait_for_completion(&we
.com
);
286 /* try again with read only flag set */
287 flags
= vioblockflags_ro
;
291 /* Send the close event to OS/400. We DON'T expect a response */
292 hvrc
= HvCallEvent_signalLpEventFast(viopath_hostLp
,
293 HvLpEvent_Type_VirtualIo
,
294 viomajorsubtype_blockio
| vioblockclose
,
295 HvLpEvent_AckInd_NoAck
, HvLpEvent_AckType_ImmediateAck
,
296 viopath_sourceinst(viopath_hostLp
),
297 viopath_targetinst(viopath_hostLp
),
299 ((u64
)unit
<< 48) | ((u64
)flags
<< 32),
302 printk(KERN_WARNING
"probe_disk: "
303 "bad rc sending event to OS/400 %d\n", (int)hvrc
);
307 do_device_node(vio_root
, "viodasd", FIRST_VIODASD
+ unit
, unit
,
308 "block", "IBM,iSeries-viodasd", NULL
);
311 static void __init
get_viodasd_info(struct device_node
*vio_root
)
316 rc
= viopath_open(viopath_hostLp
, viomajorsubtype_blockio
, 2);
318 printk(KERN_WARNING
"get_viodasd_info: "
319 "error opening path to host partition %d\n",
324 /* Initialize our request handler */
325 vio_setHandler(viomajorsubtype_blockio
, handle_block_event
);
327 for (unit
= 0; unit
< HVMAXARCHITECTEDVIRTUALDISKS
; unit
++)
328 probe_disk(vio_root
, unit
);
330 vio_clearHandler(viomajorsubtype_blockio
);
331 viopath_close(viopath_hostLp
, viomajorsubtype_blockio
, 2);
334 static void __init
handle_cd_event(struct HvLpEvent
*event
)
336 struct viocdlpevent
*bevent
;
337 struct vio_waitevent
*pwe
;
340 /* Notification that a partition went away! */
343 /* First, we should NEVER get an int here...only acks */
344 if (hvlpevent_is_int(event
)) {
345 printk(KERN_WARNING
"handle_cd_event: got an unexpected int\n");
346 if (hvlpevent_need_ack(event
)) {
347 event
->xRc
= HvLpEvent_Rc_InvalidSubtype
;
348 HvCallEvent_ackLpEvent(event
);
353 bevent
= (struct viocdlpevent
*)event
;
355 switch (event
->xSubtype
& VIOMINOR_SUBTYPE_MASK
) {
357 pwe
= (struct vio_waitevent
*)event
->xCorrelationToken
;
358 pwe
->rc
= event
->xRc
;
359 pwe
->sub_result
= bevent
->sub_result
;
364 printk(KERN_WARNING
"handle_cd_event: "
365 "message with unexpected subtype %0x04X!\n",
366 event
->xSubtype
& VIOMINOR_SUBTYPE_MASK
);
367 if (hvlpevent_need_ack(event
)) {
368 event
->xRc
= HvLpEvent_Rc_InvalidSubtype
;
369 HvCallEvent_ackLpEvent(event
);
374 static void __init
get_viocd_info(struct device_node
*vio_root
)
378 struct vio_waitevent we
;
379 struct vio_resource
*unitinfo
;
380 dma_addr_t unitinfo_dmaaddr
;
383 ret
= viopath_open(viopath_hostLp
, viomajorsubtype_cdio
, 2);
386 "get_viocd_info: error opening path to host partition %d\n",
391 /* Initialize our request handler */
392 vio_setHandler(viomajorsubtype_cdio
, handle_cd_event
);
394 unitinfo
= iseries_hv_alloc(
395 sizeof(*unitinfo
) * HVMAXARCHITECTEDVIRTUALCDROMS
,
396 &unitinfo_dmaaddr
, GFP_ATOMIC
);
399 "get_viocd_info: error allocating unitinfo\n");
403 memset(unitinfo
, 0, sizeof(*unitinfo
) * HVMAXARCHITECTEDVIRTUALCDROMS
);
405 init_completion(&we
.com
);
407 hvrc
= HvCallEvent_signalLpEventFast(viopath_hostLp
,
408 HvLpEvent_Type_VirtualIo
,
409 viomajorsubtype_cdio
| viocdgetinfo
,
410 HvLpEvent_AckInd_DoAck
, HvLpEvent_AckType_ImmediateAck
,
411 viopath_sourceinst(viopath_hostLp
),
412 viopath_targetinst(viopath_hostLp
),
413 (u64
)&we
, VIOVERSION
<< 16, unitinfo_dmaaddr
, 0,
414 sizeof(*unitinfo
) * HVMAXARCHITECTEDVIRTUALCDROMS
, 0);
415 if (hvrc
!= HvLpEvent_Rc_Good
) {
417 "get_viocd_info: cdrom error sending event. rc %d\n",
422 wait_for_completion(&we
.com
);
425 printk(KERN_WARNING
"get_viocd_info: bad rc %d:0x%04X\n",
426 we
.rc
, we
.sub_result
);
430 for (unit
= 0; (unit
< HVMAXARCHITECTEDVIRTUALCDROMS
) &&
431 unitinfo
[unit
].rsrcname
[0]; unit
++) {
432 if (!do_device_node(vio_root
, "viocd", FIRST_VIOCD
+ unit
, unit
,
433 "block", "IBM,iSeries-viocd", &unitinfo
[unit
]))
438 iseries_hv_free(sizeof(*unitinfo
) * HVMAXARCHITECTEDVIRTUALCDROMS
,
439 unitinfo
, unitinfo_dmaaddr
);
441 vio_clearHandler(viomajorsubtype_cdio
);
442 viopath_close(viopath_hostLp
, viomajorsubtype_cdio
, 2);
445 /* Handle interrupt events for tape */
446 static void __init
handle_tape_event(struct HvLpEvent
*event
)
448 struct vio_waitevent
*we
;
449 struct viotapelpevent
*tevent
= (struct viotapelpevent
*)event
;
452 /* Notification that a partition went away! */
455 we
= (struct vio_waitevent
*)event
->xCorrelationToken
;
456 switch (event
->xSubtype
& VIOMINOR_SUBTYPE_MASK
) {
458 we
->rc
= tevent
->sub_type_result
;
462 printk(KERN_WARNING
"handle_tape_event: weird ack\n");
466 static void __init
get_viotape_info(struct device_node
*vio_root
)
470 struct vio_resource
*unitinfo
;
471 dma_addr_t unitinfo_dmaaddr
;
472 size_t len
= sizeof(*unitinfo
) * HVMAXARCHITECTEDVIRTUALTAPES
;
473 struct vio_waitevent we
;
476 init_completion(&we
.com
);
478 ret
= viopath_open(viopath_hostLp
, viomajorsubtype_tape
, 2);
480 printk(KERN_WARNING
"get_viotape_info: "
481 "error on viopath_open to hostlp %d\n", ret
);
485 vio_setHandler(viomajorsubtype_tape
, handle_tape_event
);
487 unitinfo
= iseries_hv_alloc(len
, &unitinfo_dmaaddr
, GFP_ATOMIC
);
491 memset(unitinfo
, 0, len
);
493 hvrc
= HvCallEvent_signalLpEventFast(viopath_hostLp
,
494 HvLpEvent_Type_VirtualIo
,
495 viomajorsubtype_tape
| viotapegetinfo
,
496 HvLpEvent_AckInd_DoAck
, HvLpEvent_AckType_ImmediateAck
,
497 viopath_sourceinst(viopath_hostLp
),
498 viopath_targetinst(viopath_hostLp
),
499 (u64
)(unsigned long)&we
, VIOVERSION
<< 16,
500 unitinfo_dmaaddr
, len
, 0, 0);
501 if (hvrc
!= HvLpEvent_Rc_Good
) {
502 printk(KERN_WARNING
"get_viotape_info: hv error on op %d\n",
507 wait_for_completion(&we
.com
);
509 for (unit
= 0; (unit
< HVMAXARCHITECTEDVIRTUALTAPES
) &&
510 unitinfo
[unit
].rsrcname
[0]; unit
++) {
511 if (!do_device_node(vio_root
, "viotape", FIRST_VIOTAPE
+ unit
,
512 unit
, "byte", "IBM,iSeries-viotape",
518 iseries_hv_free(len
, unitinfo
, unitinfo_dmaaddr
);
520 vio_clearHandler(viomajorsubtype_tape
);
521 viopath_close(viopath_hostLp
, viomajorsubtype_tape
, 2);
524 static int __init
iseries_vio_init(void)
526 struct device_node
*vio_root
;
529 if (!firmware_has_feature(FW_FEATURE_ISERIES
))
534 vio_root
= of_find_node_by_path("/vdevice");
538 if (viopath_hostLp
== HvLpIndexInvalid
) {
540 /* If we don't have a host, bail out */
541 if (viopath_hostLp
== HvLpIndexInvalid
)
545 get_viodasd_info(vio_root
);
546 get_viocd_info(vio_root
);
547 get_viotape_info(vio_root
);
552 of_node_put(vio_root
);
556 arch_initcall(iseries_vio_init
);