2 * Functions to handle I2O controllers and I2O message handling
4 * Copyright (C) 1999-2002 Red Hat Software
6 * Written by Alan Cox, Building Number Three Ltd
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
13 * A lot of the I2O message side code from this is taken from the
14 * Red Creek RCPCI45 adapter driver by Red Creek Communications
18 * Juha Sievänen <Juha.Sievanen@cs.Helsinki.FI>
19 * Auvo Häkkinen <Auvo.Hakkinen@cs.Helsinki.FI>
20 * Deepak Saxena <deepak@plexity.net>
21 * Boji T Kannanthanam <boji.t.kannanthanam@intel.com>
22 * Alan Cox <alan@redhat.com>:
23 * Ported to Linux 2.5.
24 * Markus Lidel <Markus.Lidel@shadowconnect.com>:
25 * Minor fixes for 2.6.
28 #include <linux/module.h>
29 #include <linux/i2o.h>
30 #include <linux/delay.h>
32 #define OSM_VERSION "$Rev$"
33 #define OSM_DESCRIPTION "I2O subsystem"
35 /* global I2O controller list */
36 LIST_HEAD(i2o_controllers
);
39 * global I2O System Table. Contains information about all the IOPs in the
40 * system. Used to inform IOPs about each others existence.
42 static struct i2o_dma i2o_systab
;
44 static int i2o_hrt_get(struct i2o_controller
*c
);
46 /* Module internal functions from other sources */
47 extern struct i2o_driver i2o_exec_driver
;
48 extern int i2o_exec_lct_get(struct i2o_controller
*);
49 extern void i2o_device_remove(struct i2o_device
*);
51 extern int __init
i2o_driver_init(void);
52 extern void __exit
i2o_driver_exit(void);
53 extern int __init
i2o_exec_init(void);
54 extern void __exit
i2o_exec_exit(void);
55 extern int __init
i2o_pci_init(void);
56 extern void __exit
i2o_pci_exit(void);
57 extern int i2o_device_init(void);
58 extern void i2o_device_exit(void);
61 * i2o_msg_nop - Returns a message which is not used
62 * @c: I2O controller from which the message was created
63 * @m: message which should be returned
65 * If you fetch a message via i2o_msg_get, and can't use it, you must
66 * return the message with this function. Otherwise the message frame
69 void i2o_msg_nop(struct i2o_controller
*c
, u32 m
)
71 struct i2o_message __iomem
*msg
= c
->in_queue
.virt
+ m
;
73 writel(THREE_WORD_MSG_SIZE
| SGL_OFFSET_0
, &msg
->u
.head
[0]);
74 writel(I2O_CMD_UTIL_NOP
<< 24 | HOST_TID
<< 12 | ADAPTER_TID
,
76 writel(0, &msg
->u
.head
[2]);
77 writel(0, &msg
->u
.head
[3]);
82 * i2o_msg_get_wait - obtain an I2O message from the IOP
84 * @msg: pointer to a I2O message pointer
85 * @wait: how long to wait until timeout
87 * This function waits up to wait seconds for a message slot to be
90 * On a success the message is returned and the pointer to the message is
91 * set in msg. The returned message is the physical page frame offset
92 * address from the read port (see the i2o spec). If no message is
93 * available returns I2O_QUEUE_EMPTY and msg is leaved untouched.
95 u32
i2o_msg_get_wait(struct i2o_controller
*c
, struct i2o_message __iomem
**msg
,
98 unsigned long timeout
= jiffies
+ wait
* HZ
;
101 while ((m
= i2o_msg_get(c
, msg
)) == I2O_QUEUE_EMPTY
) {
102 if (time_after(jiffies
, timeout
)) {
103 pr_debug("%s: Timeout waiting for message frame.\n",
105 return I2O_QUEUE_EMPTY
;
107 set_current_state(TASK_UNINTERRUPTIBLE
);
114 #if BITS_PER_LONG == 64
116 * i2o_cntxt_list_add - Append a pointer to context list and return a id
117 * @c: controller to which the context list belong
118 * @ptr: pointer to add to the context list
120 * Because the context field in I2O is only 32-bit large, on 64-bit the
121 * pointer is to large to fit in the context field. The i2o_cntxt_list
122 * functions therefore map pointers to context fields.
124 * Returns context id > 0 on success or 0 on failure.
126 u32
i2o_cntxt_list_add(struct i2o_controller
* c
, void *ptr
)
128 struct i2o_context_list_element
*entry
;
132 printk(KERN_ERR
"%s: couldn't add NULL pointer to context list!"
135 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
137 printk(KERN_ERR
"%s: Could not allocate memory for context "
138 "list element\n", c
->name
);
143 entry
->timestamp
= jiffies
;
144 INIT_LIST_HEAD(&entry
->list
);
146 spin_lock_irqsave(&c
->context_list_lock
, flags
);
148 if (unlikely(atomic_inc_and_test(&c
->context_list_counter
)))
149 atomic_inc(&c
->context_list_counter
);
151 entry
->context
= atomic_read(&c
->context_list_counter
);
153 list_add(&entry
->list
, &c
->context_list
);
155 spin_unlock_irqrestore(&c
->context_list_lock
, flags
);
157 pr_debug("%s: Add context to list %p -> %d\n", c
->name
, ptr
, context
);
159 return entry
->context
;
163 * i2o_cntxt_list_remove - Remove a pointer from the context list
164 * @c: controller to which the context list belong
165 * @ptr: pointer which should be removed from the context list
167 * Removes a previously added pointer from the context list and returns
168 * the matching context id.
170 * Returns context id on succes or 0 on failure.
172 u32
i2o_cntxt_list_remove(struct i2o_controller
* c
, void *ptr
)
174 struct i2o_context_list_element
*entry
;
178 spin_lock_irqsave(&c
->context_list_lock
, flags
);
179 list_for_each_entry(entry
, &c
->context_list
, list
)
180 if (entry
->ptr
== ptr
) {
181 list_del(&entry
->list
);
182 context
= entry
->context
;
186 spin_unlock_irqrestore(&c
->context_list_lock
, flags
);
189 printk(KERN_WARNING
"%s: Could not remove nonexistent ptr "
190 "%p\n", c
->name
, ptr
);
192 pr_debug("%s: remove ptr from context list %d -> %p\n", c
->name
,
199 * i2o_cntxt_list_get - Get a pointer from the context list and remove it
200 * @c: controller to which the context list belong
201 * @context: context id to which the pointer belong
203 * Returns pointer to the matching context id on success or NULL on
206 void *i2o_cntxt_list_get(struct i2o_controller
*c
, u32 context
)
208 struct i2o_context_list_element
*entry
;
212 spin_lock_irqsave(&c
->context_list_lock
, flags
);
213 list_for_each_entry(entry
, &c
->context_list
, list
)
214 if (entry
->context
== context
) {
215 list_del(&entry
->list
);
220 spin_unlock_irqrestore(&c
->context_list_lock
, flags
);
223 printk(KERN_WARNING
"%s: context id %d not found\n", c
->name
,
226 pr_debug("%s: get ptr from context list %d -> %p\n", c
->name
, context
,
233 * i2o_cntxt_list_get_ptr - Get a context id from the context list
234 * @c: controller to which the context list belong
235 * @ptr: pointer to which the context id should be fetched
237 * Returns context id which matches to the pointer on succes or 0 on
240 u32
i2o_cntxt_list_get_ptr(struct i2o_controller
* c
, void *ptr
)
242 struct i2o_context_list_element
*entry
;
246 spin_lock_irqsave(&c
->context_list_lock
, flags
);
247 list_for_each_entry(entry
, &c
->context_list
, list
)
248 if (entry
->ptr
== ptr
) {
249 context
= entry
->context
;
252 spin_unlock_irqrestore(&c
->context_list_lock
, flags
);
255 printk(KERN_WARNING
"%s: Could not find nonexistent ptr "
256 "%p\n", c
->name
, ptr
);
258 pr_debug("%s: get context id from context list %p -> %d\n", c
->name
,
266 * i2o_iop_find - Find an I2O controller by id
267 * @unit: unit number of the I2O controller to search for
269 * Lookup the I2O controller on the controller list.
271 * Returns pointer to the I2O controller on success or NULL if not found.
273 struct i2o_controller
*i2o_find_iop(int unit
)
275 struct i2o_controller
*c
;
277 list_for_each_entry(c
, &i2o_controllers
, list
) {
286 * i2o_iop_find_device - Find a I2O device on an I2O controller
287 * @c: I2O controller where the I2O device hangs on
288 * @tid: TID of the I2O device to search for
290 * Searches the devices of the I2O controller for a device with TID tid and
293 * Returns a pointer to the I2O device if found, otherwise NULL.
295 struct i2o_device
*i2o_iop_find_device(struct i2o_controller
*c
, u16 tid
)
297 struct i2o_device
*dev
;
299 list_for_each_entry(dev
, &c
->devices
, list
)
300 if (dev
->lct_data
.tid
== tid
)
307 * i2o_quiesce_controller - quiesce controller
310 * Quiesce an IOP. Causes IOP to make external operation quiescent
311 * (i2o 'READY' state). Internal operation of the IOP continues normally.
313 * Returns 0 on success or negative error code on failure.
315 static int i2o_iop_quiesce(struct i2o_controller
*c
)
317 struct i2o_message __iomem
*msg
;
319 i2o_status_block
*sb
= c
->status_block
.virt
;
324 /* SysQuiesce discarded if IOP not in READY or OPERATIONAL state */
325 if ((sb
->iop_state
!= ADAPTER_STATE_READY
) &&
326 (sb
->iop_state
!= ADAPTER_STATE_OPERATIONAL
))
329 m
= i2o_msg_get_wait(c
, &msg
, I2O_TIMEOUT_MESSAGE_GET
);
330 if (m
== I2O_QUEUE_EMPTY
)
333 writel(FOUR_WORD_MSG_SIZE
| SGL_OFFSET_0
, &msg
->u
.head
[0]);
334 writel(I2O_CMD_SYS_QUIESCE
<< 24 | HOST_TID
<< 12 | ADAPTER_TID
,
337 /* Long timeout needed for quiesce if lots of devices */
338 if ((rc
= i2o_msg_post_wait(c
, m
, 240)))
339 printk(KERN_INFO
"%s: Unable to quiesce (status=%#x).\n",
342 pr_debug("%s: Quiesced.\n", c
->name
);
344 i2o_status_get(c
); // Entered READY state
350 * i2o_iop_enable - move controller from ready to OPERATIONAL
353 * Enable IOP. This allows the IOP to resume external operations and
354 * reverses the effect of a quiesce. Returns zero or an error code if
357 static int i2o_iop_enable(struct i2o_controller
*c
)
359 struct i2o_message __iomem
*msg
;
361 i2o_status_block
*sb
= c
->status_block
.virt
;
366 /* Enable only allowed on READY state */
367 if (sb
->iop_state
!= ADAPTER_STATE_READY
)
370 m
= i2o_msg_get_wait(c
, &msg
, I2O_TIMEOUT_MESSAGE_GET
);
371 if (m
== I2O_QUEUE_EMPTY
)
374 writel(FOUR_WORD_MSG_SIZE
| SGL_OFFSET_0
, &msg
->u
.head
[0]);
375 writel(I2O_CMD_SYS_ENABLE
<< 24 | HOST_TID
<< 12 | ADAPTER_TID
,
378 /* How long of a timeout do we need? */
379 if ((rc
= i2o_msg_post_wait(c
, m
, 240)))
380 printk(KERN_ERR
"%s: Could not enable (status=%#x).\n",
383 pr_debug("%s: Enabled.\n", c
->name
);
385 i2o_status_get(c
); // entered OPERATIONAL state
391 * i2o_iop_quiesce_all - Quiesce all I2O controllers on the system
393 * Quiesce all I2O controllers which are connected to the system.
395 static inline void i2o_iop_quiesce_all(void)
397 struct i2o_controller
*c
, *tmp
;
399 list_for_each_entry_safe(c
, tmp
, &i2o_controllers
, list
) {
406 * i2o_iop_enable_all - Enables all controllers on the system
408 * Enables all I2O controllers which are connected to the system.
410 static inline void i2o_iop_enable_all(void)
412 struct i2o_controller
*c
, *tmp
;
414 list_for_each_entry_safe(c
, tmp
, &i2o_controllers
, list
)
419 * i2o_clear_controller - Bring I2O controller into HOLD state
422 * Clear an IOP to HOLD state, ie. terminate external operations, clear all
423 * input queues and prepare for a system restart. IOP's internal operation
424 * continues normally and the outbound queue is alive. The IOP is not
425 * expected to rebuild its LCT.
427 * Returns 0 on success or negative error code on failure.
429 static int i2o_iop_clear(struct i2o_controller
*c
)
431 struct i2o_message __iomem
*msg
;
435 m
= i2o_msg_get_wait(c
, &msg
, I2O_TIMEOUT_MESSAGE_GET
);
436 if (m
== I2O_QUEUE_EMPTY
)
439 /* Quiesce all IOPs first */
440 i2o_iop_quiesce_all();
442 writel(FOUR_WORD_MSG_SIZE
| SGL_OFFSET_0
, &msg
->u
.head
[0]);
443 writel(I2O_CMD_ADAPTER_CLEAR
<< 24 | HOST_TID
<< 12 | ADAPTER_TID
,
446 if ((rc
= i2o_msg_post_wait(c
, m
, 30)))
447 printk(KERN_INFO
"%s: Unable to clear (status=%#x).\n",
450 pr_debug("%s: Cleared.\n", c
->name
);
452 /* Enable all IOPs */
453 i2o_iop_enable_all();
461 * i2o_iop_reset - reset an I2O controller
462 * @c: controller to reset
464 * Reset the IOP into INIT state and wait until IOP gets into RESET state.
465 * Terminate all external operations, clear IOP's inbound and outbound
466 * queues, terminate all DDMs, and reload the IOP's operating environment
467 * and all local DDMs. The IOP rebuilds its LCT.
469 static int i2o_iop_reset(struct i2o_controller
*c
)
471 u8
*status
= c
->status
.virt
;
472 struct i2o_message __iomem
*msg
;
474 unsigned long timeout
;
475 i2o_status_block
*sb
= c
->status_block
.virt
;
478 pr_debug("%s: Resetting controller\n", c
->name
);
480 m
= i2o_msg_get_wait(c
, &msg
, I2O_TIMEOUT_MESSAGE_GET
);
481 if (m
== I2O_QUEUE_EMPTY
)
484 memset(status
, 0, 8);
486 /* Quiesce all IOPs first */
487 i2o_iop_quiesce_all();
489 writel(EIGHT_WORD_MSG_SIZE
| SGL_OFFSET_0
, &msg
->u
.head
[0]);
490 writel(I2O_CMD_ADAPTER_RESET
<< 24 | HOST_TID
<< 12 | ADAPTER_TID
,
492 writel(i2o_exec_driver
.context
, &msg
->u
.s
.icntxt
);
493 writel(0, &msg
->u
.s
.tcntxt
); //FIXME: use reasonable transaction context
494 writel(0, &msg
->body
[0]);
495 writel(0, &msg
->body
[1]);
496 writel(i2o_ptr_low((void *)c
->status
.phys
), &msg
->body
[2]);
497 writel(i2o_ptr_high((void *)c
->status
.phys
), &msg
->body
[3]);
501 /* Wait for a reply */
502 timeout
= jiffies
+ I2O_TIMEOUT_RESET
* HZ
;
504 if (time_after(jiffies
, timeout
)) {
505 printk(KERN_ERR
"%s: IOP reset timeout.\n", c
->name
);
511 if (status
[1] || status
[4]) {
516 set_current_state(TASK_UNINTERRUPTIBLE
);
522 if (*status
== I2O_CMD_IN_PROGRESS
) {
524 * Once the reset is sent, the IOP goes into the INIT state
525 * which is indeterminate. We need to wait until the IOP
526 * has rebooted before we can let the system talk to
527 * it. We read the inbound Free_List until a message is
528 * available. If we can't read one in the given ammount of
529 * time, we assume the IOP could not reboot properly.
531 pr_debug("%s: Reset in progress, waiting for reboot...\n",
534 m
= i2o_msg_get_wait(c
, &msg
, I2O_TIMEOUT_RESET
);
535 while (m
== I2O_QUEUE_EMPTY
) {
536 if (time_after(jiffies
, timeout
)) {
537 printk(KERN_ERR
"%s: IOP reset timeout.\n",
542 set_current_state(TASK_UNINTERRUPTIBLE
);
545 m
= i2o_msg_get_wait(c
, &msg
, I2O_TIMEOUT_RESET
);
550 /* from here all quiesce commands are safe */
553 /* If IopReset was rejected or didn't perform reset, try IopClear */
555 if (*status
== I2O_CMD_REJECTED
|| sb
->iop_state
!= ADAPTER_STATE_RESET
) {
556 printk(KERN_WARNING
"%s: Reset rejected, trying to clear\n",
560 pr_debug("%s: Reset completed.\n", c
->name
);
563 /* Enable all IOPs */
564 i2o_iop_enable_all();
570 * i2o_iop_init_outbound_queue - setup the outbound message queue
573 * Clear and (re)initialize IOP's outbound queue and post the message
576 * Returns 0 on success or a negative errno code on failure.
578 static int i2o_iop_init_outbound_queue(struct i2o_controller
*c
)
580 u8
*status
= c
->status
.virt
;
582 struct i2o_message __iomem
*msg
;
586 pr_debug("%s: Initializing Outbound Queue...\n", c
->name
);
588 memset(status
, 0, 4);
590 m
= i2o_msg_get_wait(c
, &msg
, I2O_TIMEOUT_MESSAGE_GET
);
591 if (m
== I2O_QUEUE_EMPTY
)
594 writel(EIGHT_WORD_MSG_SIZE
| TRL_OFFSET_6
, &msg
->u
.head
[0]);
595 writel(I2O_CMD_OUTBOUND_INIT
<< 24 | HOST_TID
<< 12 | ADAPTER_TID
,
597 writel(i2o_exec_driver
.context
, &msg
->u
.s
.icntxt
);
598 writel(0x0106, &msg
->u
.s
.tcntxt
); /* FIXME: why 0x0106, maybe in
600 writel(PAGE_SIZE
, &msg
->body
[0]);
601 writel(MSG_FRAME_SIZE
<< 16 | 0x80, &msg
->body
[1]); /* Outbound msg frame
602 size in words and Initcode */
603 writel(0xd0000004, &msg
->body
[2]);
604 writel(i2o_ptr_low((void *)c
->status
.phys
), &msg
->body
[3]);
605 writel(i2o_ptr_high((void *)c
->status
.phys
), &msg
->body
[4]);
609 timeout
= jiffies
+ I2O_TIMEOUT_INIT_OUTBOUND_QUEUE
* HZ
;
610 while (*status
<= I2O_CMD_IN_PROGRESS
) {
611 if (time_after(jiffies
, timeout
)) {
612 printk(KERN_WARNING
"%s: Timeout Initializing\n",
616 set_current_state(TASK_UNINTERRUPTIBLE
);
622 m
= c
->out_queue
.phys
;
625 for (i
= 0; i
< NMBR_MSG_FRAMES
; i
++) {
626 i2o_flush_reply(c
, m
);
627 udelay(1); /* Promise */
628 m
+= MSG_FRAME_SIZE
* 4;
635 * i2o_iop_send_nop - send a core NOP message
638 * Send a no-operation message with a reply set to cause no
639 * action either. Needed for bringing up promise controllers.
641 static int i2o_iop_send_nop(struct i2o_controller
*c
)
643 struct i2o_message __iomem
*msg
;
644 u32 m
= i2o_msg_get_wait(c
, &msg
, HZ
);
645 if (m
== I2O_QUEUE_EMPTY
)
652 * i2o_iop_activate - Bring controller up to HOLD
655 * This function brings an I2O controller into HOLD state. The adapter
656 * is reset if necessary and then the queues and resource table are read.
658 * Returns 0 on success or negative error code on failure.
660 static int i2o_iop_activate(struct i2o_controller
*c
)
662 struct pci_dev
*i960
= NULL
;
663 i2o_status_block
*sb
= c
->status_block
.virt
;
667 /* Beat up the hardware first of all */
669 pci_find_slot(c
->pdev
->bus
->number
,
670 PCI_DEVFN(PCI_SLOT(c
->pdev
->devfn
), 0));
672 pci_write_config_word(i960
, 0x42, 0);
674 /* Follow this sequence precisely or the controller
675 ceases to perform useful functions until reboot */
676 if ((rc
= i2o_iop_send_nop(c
)))
679 if ((rc
= i2o_iop_reset(c
)))
683 /* In INIT state, Wait Inbound Q to initialize (in i2o_status_get) */
684 /* In READY state, Get status */
686 rc
= i2o_status_get(c
);
688 printk(KERN_INFO
"%s: Unable to obtain status, "
689 "attempting a reset.\n", c
->name
);
690 if (i2o_iop_reset(c
))
694 if (sb
->i2o_version
> I2OVER15
) {
695 printk(KERN_ERR
"%s: Not running version 1.5 of the I2O "
696 "Specification.\n", c
->name
);
700 switch (sb
->iop_state
) {
701 case ADAPTER_STATE_FAULTED
:
702 printk(KERN_CRIT
"%s: hardware fault\n", c
->name
);
705 case ADAPTER_STATE_READY
:
706 case ADAPTER_STATE_OPERATIONAL
:
707 case ADAPTER_STATE_HOLD
:
708 case ADAPTER_STATE_FAILED
:
709 pr_debug("%s: already running, trying to reset...\n", c
->name
);
710 if (i2o_iop_reset(c
))
714 rc
= i2o_iop_init_outbound_queue(c
);
719 if ((rc
= i2o_iop_send_nop(c
)))
722 if ((rc
= i2o_status_get(c
)))
726 pci_write_config_word(i960
, 0x42, 0x3FF);
737 * i2o_iop_systab_set - Set the I2O System Table of the specified IOP
738 * @c: I2O controller to which the system table should be send
740 * Before the systab could be set i2o_systab_build() must be called.
742 * Returns 0 on success or negative error code on failure.
744 static int i2o_iop_systab_set(struct i2o_controller
*c
)
746 struct i2o_message __iomem
*msg
;
748 i2o_status_block
*sb
= c
->status_block
.virt
;
749 struct device
*dev
= &c
->pdev
->dev
;
750 struct resource
*root
;
753 if (sb
->current_mem_size
< sb
->desired_mem_size
) {
754 struct resource
*res
= &c
->mem_resource
;
755 res
->name
= c
->pdev
->bus
->name
;
756 res
->flags
= IORESOURCE_MEM
;
759 printk(KERN_INFO
"%s: requires private memory resources.\n",
761 root
= pci_find_parent_resource(c
->pdev
, res
);
763 printk(KERN_WARNING
"%s: Can't find parent resource!\n",
765 if (root
&& allocate_resource(root
, res
, sb
->desired_mem_size
, sb
->desired_mem_size
, sb
->desired_mem_size
, 1 << 20, /* Unspecified, so use 1Mb and play safe */
768 sb
->current_mem_size
= 1 + res
->end
- res
->start
;
769 sb
->current_mem_base
= res
->start
;
770 printk(KERN_INFO
"%s: allocated %ld bytes of PCI memory"
771 " at 0x%08lX.\n", c
->name
,
772 1 + res
->end
- res
->start
, res
->start
);
776 if (sb
->current_io_size
< sb
->desired_io_size
) {
777 struct resource
*res
= &c
->io_resource
;
778 res
->name
= c
->pdev
->bus
->name
;
779 res
->flags
= IORESOURCE_IO
;
782 printk(KERN_INFO
"%s: requires private memory resources.\n",
784 root
= pci_find_parent_resource(c
->pdev
, res
);
786 printk(KERN_WARNING
"%s: Can't find parent resource!\n",
788 if (root
&& allocate_resource(root
, res
, sb
->desired_io_size
, sb
->desired_io_size
, sb
->desired_io_size
, 1 << 20, /* Unspecified, so use 1Mb and play safe */
791 sb
->current_io_size
= 1 + res
->end
- res
->start
;
792 sb
->current_mem_base
= res
->start
;
793 printk(KERN_INFO
"%s: allocated %ld bytes of PCI I/O at"
794 " 0x%08lX.\n", c
->name
,
795 1 + res
->end
- res
->start
, res
->start
);
799 m
= i2o_msg_get_wait(c
, &msg
, I2O_TIMEOUT_MESSAGE_GET
);
800 if (m
== I2O_QUEUE_EMPTY
)
803 i2o_systab
.phys
= dma_map_single(dev
, i2o_systab
.virt
, i2o_systab
.len
,
805 if (!i2o_systab
.phys
) {
810 writel(I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6
, &msg
->u
.head
[0]);
811 writel(I2O_CMD_SYS_TAB_SET
<< 24 | HOST_TID
<< 12 | ADAPTER_TID
,
815 * Provide three SGL-elements:
816 * System table (SysTab), Private memory space declaration and
817 * Private i/o space declaration
819 * FIXME: is this still true?
820 * Nasty one here. We can't use dma_alloc_coherent to send the
821 * same table to everyone. We have to go remap it for them all
824 writel(c
->unit
+ 2, &msg
->body
[0]);
825 writel(0, &msg
->body
[1]);
826 writel(0x54000000 | i2o_systab
.len
, &msg
->body
[2]);
827 writel(i2o_systab
.phys
, &msg
->body
[3]);
828 writel(0x54000000 | sb
->current_mem_size
, &msg
->body
[4]);
829 writel(sb
->current_mem_base
, &msg
->body
[5]);
830 writel(0xd4000000 | sb
->current_io_size
, &msg
->body
[6]);
831 writel(sb
->current_io_base
, &msg
->body
[6]);
833 rc
= i2o_msg_post_wait(c
, m
, 120);
835 dma_unmap_single(dev
, i2o_systab
.phys
, i2o_systab
.len
,
839 printk(KERN_ERR
"%s: Unable to set SysTab (status=%#x).\n",
842 pr_debug("%s: SysTab set.\n", c
->name
);
844 i2o_status_get(c
); // Entered READY state
850 * i2o_iop_online - Bring a controller online into OPERATIONAL state.
853 * Send the system table and enable the I2O controller.
855 * Returns 0 on success or negativer error code on failure.
857 static int i2o_iop_online(struct i2o_controller
*c
)
861 rc
= i2o_iop_systab_set(c
);
866 pr_debug("%s: Attempting to enable...\n", c
->name
);
867 rc
= i2o_iop_enable(c
);
875 * i2o_iop_remove - Remove the I2O controller from the I2O core
878 * Remove the I2O controller from the I2O core. If devices are attached to
879 * the controller remove these also and finally reset the controller.
881 void i2o_iop_remove(struct i2o_controller
*c
)
883 struct i2o_device
*dev
, *tmp
;
885 pr_debug("%s: deleting controller\n", c
->name
);
887 i2o_driver_notify_controller_remove_all(c
);
891 list_for_each_entry_safe(dev
, tmp
, &c
->devices
, list
)
892 i2o_device_remove(dev
);
894 /* Ask the IOP to switch to RESET state */
899 * i2o_systab_build - Build system table
901 * The system table contains information about all the IOPs in the system
902 * (duh) and is used by the Executives on the IOPs to establish peer2peer
903 * connections. We're not supporting peer2peer at the moment, but this
904 * will be needed down the road for things like lan2lan forwarding.
906 * Returns 0 on success or negative error code on failure.
908 static int i2o_systab_build(void)
910 struct i2o_controller
*c
, *tmp
;
911 int num_controllers
= 0;
914 struct i2o_sys_tbl
*systab
= i2o_systab
.virt
;
916 list_for_each_entry_safe(c
, tmp
, &i2o_controllers
, list
)
920 change_ind
= systab
->change_ind
;
921 kfree(i2o_systab
.virt
);
925 i2o_systab
.len
= sizeof(struct i2o_sys_tbl
) + num_controllers
*
926 sizeof(struct i2o_sys_tbl_entry
);
928 systab
= i2o_systab
.virt
= kmalloc(i2o_systab
.len
, GFP_KERNEL
);
930 printk(KERN_ERR
"i2o: unable to allocate memory for System "
934 memset(systab
, 0, i2o_systab
.len
);
936 systab
->version
= I2OVERSION
;
937 systab
->change_ind
= change_ind
+ 1;
939 list_for_each_entry_safe(c
, tmp
, &i2o_controllers
, list
) {
940 i2o_status_block
*sb
;
942 if (count
>= num_controllers
) {
943 printk(KERN_ERR
"i2o: controller added while building "
948 sb
= c
->status_block
.virt
;
951 * Get updated IOP state so we have the latest information
953 * We should delete the controller at this point if it
954 * doesn't respond since if it's not on the system table
955 * it is techninically not part of the I2O subsystem...
957 if (unlikely(i2o_status_get(c
))) {
958 printk(KERN_ERR
"%s: Deleting b/c could not get status"
959 " while attempting to build system table\n",
962 continue; // try the next one
965 systab
->iops
[count
].org_id
= sb
->org_id
;
966 systab
->iops
[count
].iop_id
= c
->unit
+ 2;
967 systab
->iops
[count
].seg_num
= 0;
968 systab
->iops
[count
].i2o_version
= sb
->i2o_version
;
969 systab
->iops
[count
].iop_state
= sb
->iop_state
;
970 systab
->iops
[count
].msg_type
= sb
->msg_type
;
971 systab
->iops
[count
].frame_size
= sb
->inbound_frame_size
;
972 systab
->iops
[count
].last_changed
= change_ind
;
973 systab
->iops
[count
].iop_capabilities
= sb
->iop_capabilities
;
974 systab
->iops
[count
].inbound_low
= i2o_ptr_low(c
->post_port
);
975 systab
->iops
[count
].inbound_high
= i2o_ptr_high(c
->post_port
);
980 systab
->num_entries
= count
;
986 * i2o_parse_hrt - Parse the hardware resource table.
989 * We don't do anything with it except dumping it (in debug mode).
993 static int i2o_parse_hrt(struct i2o_controller
*c
)
1000 * i2o_status_get - Get the status block from the I2O controller
1001 * @c: I2O controller
1003 * Issue a status query on the controller. This updates the attached
1004 * status block. The status block could then be accessed through
1007 * Returns 0 on sucess or negative error code on failure.
1009 int i2o_status_get(struct i2o_controller
*c
)
1011 struct i2o_message __iomem
*msg
;
1014 unsigned long timeout
;
1016 status_block
= (u8
*) c
->status_block
.virt
;
1017 memset(status_block
, 0, sizeof(i2o_status_block
));
1019 m
= i2o_msg_get_wait(c
, &msg
, I2O_TIMEOUT_MESSAGE_GET
);
1020 if (m
== I2O_QUEUE_EMPTY
)
1023 writel(NINE_WORD_MSG_SIZE
| SGL_OFFSET_0
, &msg
->u
.head
[0]);
1024 writel(I2O_CMD_STATUS_GET
<< 24 | HOST_TID
<< 12 | ADAPTER_TID
,
1026 writel(i2o_exec_driver
.context
, &msg
->u
.s
.icntxt
);
1027 writel(0, &msg
->u
.s
.tcntxt
); // FIXME: use resonable transaction context
1028 writel(0, &msg
->body
[0]);
1029 writel(0, &msg
->body
[1]);
1030 writel(i2o_ptr_low((void *)c
->status_block
.phys
), &msg
->body
[2]);
1031 writel(i2o_ptr_high((void *)c
->status_block
.phys
), &msg
->body
[3]);
1032 writel(sizeof(i2o_status_block
), &msg
->body
[4]); /* always 88 bytes */
1036 /* Wait for a reply */
1037 timeout
= jiffies
+ I2O_TIMEOUT_STATUS_GET
* HZ
;
1038 while (status_block
[87] != 0xFF) {
1039 if (time_after(jiffies
, timeout
)) {
1040 printk(KERN_ERR
"%s: Get status timeout.\n", c
->name
);
1044 set_current_state(TASK_UNINTERRUPTIBLE
);
1045 schedule_timeout(1);
1058 * i2o_hrt_get - Get the Hardware Resource Table from the I2O controller
1059 * @c: I2O controller from which the HRT should be fetched
1061 * The HRT contains information about possible hidden devices but is
1062 * mostly useless to us.
1064 * Returns 0 on success or negativer error code on failure.
1066 static int i2o_hrt_get(struct i2o_controller
*c
)
1070 i2o_hrt
*hrt
= c
->hrt
.virt
;
1071 u32 size
= sizeof(i2o_hrt
);
1072 struct device
*dev
= &c
->pdev
->dev
;
1074 for (i
= 0; i
< I2O_HRT_GET_TRIES
; i
++) {
1075 struct i2o_message __iomem
*msg
;
1078 m
= i2o_msg_get_wait(c
, &msg
, I2O_TIMEOUT_MESSAGE_GET
);
1079 if (m
== I2O_QUEUE_EMPTY
)
1082 writel(SIX_WORD_MSG_SIZE
| SGL_OFFSET_4
, &msg
->u
.head
[0]);
1083 writel(I2O_CMD_HRT_GET
<< 24 | HOST_TID
<< 12 | ADAPTER_TID
,
1085 writel(0xd0000000 | c
->hrt
.len
, &msg
->body
[0]);
1086 writel(c
->hrt
.phys
, &msg
->body
[1]);
1088 rc
= i2o_msg_post_wait_mem(c
, m
, 20, &c
->hrt
);
1091 printk(KERN_ERR
"%s: Unable to get HRT (status=%#x)\n",
1096 size
= hrt
->num_entries
* hrt
->entry_len
<< 2;
1097 if (size
> c
->hrt
.len
) {
1098 if (i2o_dma_realloc(dev
, &c
->hrt
, size
, GFP_KERNEL
))
1103 return i2o_parse_hrt(c
);
1106 printk(KERN_ERR
"%s: Unable to get HRT after %d tries, giving up\n",
1107 c
->name
, I2O_HRT_GET_TRIES
);
1113 * i2o_iop_alloc - Allocate and initialize a i2o_controller struct
1115 * Allocate the necessary memory for a i2o_controller struct and
1116 * initialize the lists.
1118 * Returns a pointer to the I2O controller or a negative error code on
1121 struct i2o_controller
*i2o_iop_alloc(void)
1123 static int unit
= 0; /* 0 and 1 are NULL IOP and Local Host */
1124 struct i2o_controller
*c
;
1126 c
= kmalloc(sizeof(*c
), GFP_KERNEL
);
1128 printk(KERN_ERR
"i2o: Insufficient memory to allocate a I2O "
1130 return ERR_PTR(-ENOMEM
);
1132 memset(c
, 0, sizeof(*c
));
1134 INIT_LIST_HEAD(&c
->devices
);
1135 spin_lock_init(&c
->lock
);
1136 init_MUTEX(&c
->lct_lock
);
1138 sprintf(c
->name
, "iop%d", c
->unit
);
1140 #if BITS_PER_LONG == 64
1141 spin_lock_init(&c
->context_list_lock
);
1142 atomic_set(&c
->context_list_counter
, 0);
1143 INIT_LIST_HEAD(&c
->context_list
);
1150 * i2o_iop_free - Free the i2o_controller struct
1151 * @c: I2O controller to free
1153 void i2o_iop_free(struct i2o_controller
*c
)
1159 * i2o_iop_add - Initialize the I2O controller and add him to the I2O core
1162 * Initialize the I2O controller and if no error occurs add him to the I2O
1165 * Returns 0 on success or negative error code on failure.
1167 int i2o_iop_add(struct i2o_controller
*c
)
1171 printk(KERN_INFO
"%s: Activating I2O controller...\n", c
->name
);
1172 printk(KERN_INFO
"%s: This may take a few minutes if there are many "
1173 "devices\n", c
->name
);
1175 if ((rc
= i2o_iop_activate(c
))) {
1176 printk(KERN_ERR
"%s: could not activate controller\n",
1182 pr_debug("%s: building sys table...\n", c
->name
);
1184 if ((rc
= i2o_systab_build())) {
1189 pr_debug("%s: online controller...\n", c
->name
);
1191 if ((rc
= i2o_iop_online(c
))) {
1196 pr_debug("%s: getting LCT...\n", c
->name
);
1198 if ((rc
= i2o_exec_lct_get(c
))) {
1203 list_add(&c
->list
, &i2o_controllers
);
1205 i2o_driver_notify_controller_add_all(c
);
1207 printk(KERN_INFO
"%s: Controller added\n", c
->name
);
1213 * i2o_event_register - Turn on/off event notification for a I2O device
1214 * @dev: I2O device which should receive the event registration request
1215 * @drv: driver which want to get notified
1216 * @tcntxt: transaction context to use with this notifier
1217 * @evt_mask: mask of events
1219 * Create and posts an event registration message to the task. No reply
1220 * is waited for, or expected. If you do not want further notifications,
1221 * call the i2o_event_register again with a evt_mask of 0.
1223 * Returns 0 on success or -ETIMEDOUT if no message could be fetched for
1224 * sending the request.
1226 int i2o_event_register(struct i2o_device
*dev
, struct i2o_driver
*drv
,
1227 int tcntxt
, u32 evt_mask
)
1229 struct i2o_controller
*c
= dev
->iop
;
1230 struct i2o_message __iomem
*msg
;
1233 m
= i2o_msg_get_wait(c
, &msg
, I2O_TIMEOUT_MESSAGE_GET
);
1234 if (m
== I2O_QUEUE_EMPTY
)
1237 writel(FIVE_WORD_MSG_SIZE
| SGL_OFFSET_0
, &msg
->u
.head
[0]);
1238 writel(I2O_CMD_UTIL_EVT_REGISTER
<< 24 | HOST_TID
<< 12 | dev
->lct_data
.
1239 tid
, &msg
->u
.head
[1]);
1240 writel(drv
->context
, &msg
->u
.s
.icntxt
);
1241 writel(tcntxt
, &msg
->u
.s
.tcntxt
);
1242 writel(evt_mask
, &msg
->body
[0]);
1250 * i2o_iop_init - I2O main initialization function
1252 * Initialize the I2O drivers (OSM) functions, register the Executive OSM,
1253 * initialize the I2O PCI part and finally initialize I2O device stuff.
1255 * Returns 0 on success or negative error code on failure.
1257 static int __init
i2o_iop_init(void)
1261 printk(KERN_INFO OSM_DESCRIPTION
" v" OSM_VERSION
"\n");
1263 rc
= i2o_device_init();
1267 rc
= i2o_driver_init();
1271 rc
= i2o_exec_init();
1275 rc
= i2o_pci_init();
1295 * i2o_iop_exit - I2O main exit function
1297 * Removes I2O controllers from PCI subsystem and shut down OSMs.
1299 static void __exit
i2o_iop_exit(void)
1307 module_init(i2o_iop_init
);
1308 module_exit(i2o_iop_exit
);
1310 MODULE_AUTHOR("Red Hat Software");
1311 MODULE_LICENSE("GPL");
1312 MODULE_DESCRIPTION(OSM_DESCRIPTION
);
1313 MODULE_VERSION(OSM_VERSION
);
1315 #if BITS_PER_LONG == 64
1316 EXPORT_SYMBOL(i2o_cntxt_list_add
);
1317 EXPORT_SYMBOL(i2o_cntxt_list_get
);
1318 EXPORT_SYMBOL(i2o_cntxt_list_remove
);
1319 EXPORT_SYMBOL(i2o_cntxt_list_get_ptr
);
1321 EXPORT_SYMBOL(i2o_msg_get_wait
);
1322 EXPORT_SYMBOL(i2o_msg_nop
);
1323 EXPORT_SYMBOL(i2o_find_iop
);
1324 EXPORT_SYMBOL(i2o_iop_find_device
);
1325 EXPORT_SYMBOL(i2o_event_register
);
1326 EXPORT_SYMBOL(i2o_status_get
);
1327 EXPORT_SYMBOL(i2o_controllers
);