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 = i2o_msg_in_to_virt(c, 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();
459 * i2o_iop_init_outbound_queue - setup the outbound message queue
462 * Clear and (re)initialize IOP's outbound queue and post the message
465 * Returns 0 on success or a negative errno code on failure.
467 static int i2o_iop_init_outbound_queue(struct i2o_controller *c)
469 u8 *status = c->status.virt;
471 struct i2o_message __iomem *msg;
475 osm_debug("%s: Initializing Outbound Queue...\n", c->name);
477 memset(status, 0, 4);
479 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
480 if (m == I2O_QUEUE_EMPTY)
483 writel(EIGHT_WORD_MSG_SIZE | TRL_OFFSET_6, &msg->u.head[0]);
484 writel(I2O_CMD_OUTBOUND_INIT << 24 | HOST_TID << 12 | ADAPTER_TID,
486 writel(i2o_exec_driver.context, &msg->u.s.icntxt);
487 writel(0x0106, &msg->u.s.tcntxt); /* FIXME: why 0x0106, maybe in
489 writel(PAGE_SIZE, &msg->body[0]);
490 /* Outbound msg frame size in words and Initcode */
491 writel(MSG_FRAME_SIZE << 16 | 0x80, &msg->body[1]);
492 writel(0xd0000004, &msg->body[2]);
493 writel(i2o_dma_low(c->status.phys), &msg->body[3]);
494 writel(i2o_dma_high(c->status.phys), &msg->body[4]);
498 timeout = jiffies + I2O_TIMEOUT_INIT_OUTBOUND_QUEUE * HZ;
499 while (*status <= I2O_CMD_IN_PROGRESS) {
500 if (time_after(jiffies, timeout)) {
501 osm_warn("%s: Timeout Initializing\n", c->name);
504 set_current_state(TASK_UNINTERRUPTIBLE);
510 m = c->out_queue.phys;
513 for (i = 0; i < NMBR_MSG_FRAMES; i++) {
514 i2o_flush_reply(c, m);
515 udelay(1); /* Promise */
516 m += MSG_FRAME_SIZE * 4;
523 * i2o_iop_reset - reset an I2O controller
524 * @c: controller to reset
526 * Reset the IOP into INIT state and wait until IOP gets into RESET state.
527 * Terminate all external operations, clear IOP's inbound and outbound
528 * queues, terminate all DDMs, and reload the IOP's operating environment
529 * and all local DDMs. The IOP rebuilds its LCT.
531 static int i2o_iop_reset(struct i2o_controller *c)
533 u8 *status = c->status.virt;
534 struct i2o_message __iomem *msg;
536 unsigned long timeout;
537 i2o_status_block *sb = c->status_block.virt;
540 pr_debug("%s: Resetting controller\n", c->name);
542 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
543 if (m == I2O_QUEUE_EMPTY)
546 memset(status, 0, 8);
548 /* Quiesce all IOPs first */
549 i2o_iop_quiesce_all();
551 writel(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
552 writel(I2O_CMD_ADAPTER_RESET << 24 | HOST_TID << 12 | ADAPTER_TID,
554 writel(i2o_exec_driver.context, &msg->u.s.icntxt);
555 writel(0, &msg->u.s.tcntxt); //FIXME: use reasonable transaction context
556 writel(0, &msg->body[0]);
557 writel(0, &msg->body[1]);
558 writel(i2o_dma_low(c->status.phys), &msg->body[2]);
559 writel(i2o_dma_high(c->status.phys), &msg->body[3]);
563 /* Wait for a reply */
564 timeout = jiffies + I2O_TIMEOUT_RESET * HZ;
566 if (time_after(jiffies, timeout))
569 set_current_state(TASK_UNINTERRUPTIBLE);
576 case I2O_CMD_REJECTED:
577 osm_warn("%s: IOP reset rejected\n", c->name);
581 case I2O_CMD_IN_PROGRESS:
583 * Once the reset is sent, the IOP goes into the INIT state
584 * which is indeterminate. We need to wait until the IOP has
585 * rebooted before we can let the system talk to it. We read
586 * the inbound Free_List until a message is available. If we
587 * can't read one in the given ammount of time, we assume the
588 * IOP could not reboot properly.
590 pr_debug("%s: Reset in progress, waiting for reboot...\n",
593 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_RESET);
594 while (m == I2O_QUEUE_EMPTY) {
595 if (time_after(jiffies, timeout)) {
596 printk(KERN_ERR "%s: IOP reset timeout.\n",
601 set_current_state(TASK_UNINTERRUPTIBLE);
604 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_RESET);
608 /* from here all quiesce commands are safe */
611 /* verify if controller is in state RESET */
614 if (!c->promise && (sb->iop_state != ADAPTER_STATE_RESET))
615 osm_warn("%s: reset completed, but adapter not in RESET"
616 " state.\n", c->name);
618 osm_debug("%s: reset completed.\n", c->name);
623 osm_err("%s: IOP reset timeout.\n", c->name);
629 /* Enable all IOPs */
630 i2o_iop_enable_all();
636 * i2o_iop_activate - Bring controller up to HOLD
639 * This function brings an I2O controller into HOLD state. The adapter
640 * is reset if necessary and then the queues and resource table are read.
642 * Returns 0 on success or negative error code on failure.
644 static int i2o_iop_activate(struct i2o_controller *c)
646 i2o_status_block *sb = c->status_block.virt;
650 /* In INIT state, Wait Inbound Q to initialize (in i2o_status_get) */
651 /* In READY state, Get status */
653 rc = i2o_status_get(c);
655 printk(KERN_INFO "%s: Unable to obtain status, "
656 "attempting a reset.\n", c->name);
657 rc = i2o_iop_reset(c);
662 if (sb->i2o_version > I2OVER15) {
663 printk(KERN_ERR "%s: Not running version 1.5 of the I2O "
664 "Specification.\n", c->name);
668 switch (sb->iop_state) {
669 case ADAPTER_STATE_FAULTED:
670 printk(KERN_CRIT "%s: hardware fault\n", c->name);
673 case ADAPTER_STATE_READY:
674 case ADAPTER_STATE_OPERATIONAL:
675 case ADAPTER_STATE_HOLD:
676 case ADAPTER_STATE_FAILED:
677 pr_debug("%s: already running, trying to reset...\n", c->name);
678 rc = i2o_iop_reset(c);
684 state = sb->iop_state;
686 rc = i2o_iop_init_outbound_queue(c);
690 /* if adapter was not in RESET state clear now */
691 if (state != ADAPTER_STATE_RESET)
696 if (sb->iop_state != ADAPTER_STATE_HOLD) {
697 osm_err("%s: failed to bring IOP into HOLD state\n", c->name);
701 return i2o_hrt_get(c);
705 * i2o_iop_systab_set - Set the I2O System Table of the specified IOP
706 * @c: I2O controller to which the system table should be send
708 * Before the systab could be set i2o_systab_build() must be called.
710 * Returns 0 on success or negative error code on failure.
712 static int i2o_iop_systab_set(struct i2o_controller *c)
714 struct i2o_message __iomem *msg;
716 i2o_status_block *sb = c->status_block.virt;
717 struct device *dev = &c->pdev->dev;
718 struct resource *root;
721 if (sb->current_mem_size < sb->desired_mem_size) {
722 struct resource *res = &c->mem_resource;
723 res->name = c->pdev->bus->name;
724 res->flags = IORESOURCE_MEM;
727 printk(KERN_INFO "%s: requires private memory resources.\n",
729 root = pci_find_parent_resource(c->pdev, res);
731 printk(KERN_WARNING "%s: Can't find parent resource!\n",
733 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 */
736 sb->current_mem_size = 1 + res->end - res->start;
737 sb->current_mem_base = res->start;
738 printk(KERN_INFO "%s: allocated %ld bytes of PCI memory"
739 " at 0x%08lX.\n", c->name,
740 1 + res->end - res->start, res->start);
744 if (sb->current_io_size < sb->desired_io_size) {
745 struct resource *res = &c->io_resource;
746 res->name = c->pdev->bus->name;
747 res->flags = IORESOURCE_IO;
750 printk(KERN_INFO "%s: requires private memory resources.\n",
752 root = pci_find_parent_resource(c->pdev, res);
754 printk(KERN_WARNING "%s: Can't find parent resource!\n",
756 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 */
759 sb->current_io_size = 1 + res->end - res->start;
760 sb->current_mem_base = res->start;
761 printk(KERN_INFO "%s: allocated %ld bytes of PCI I/O at"
762 " 0x%08lX.\n", c->name,
763 1 + res->end - res->start, res->start);
767 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
768 if (m == I2O_QUEUE_EMPTY)
771 i2o_systab.phys = dma_map_single(dev, i2o_systab.virt, i2o_systab.len,
773 if (!i2o_systab.phys) {
778 writel(I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6, &msg->u.head[0]);
779 writel(I2O_CMD_SYS_TAB_SET << 24 | HOST_TID << 12 | ADAPTER_TID,
783 * Provide three SGL-elements:
784 * System table (SysTab), Private memory space declaration and
785 * Private i/o space declaration
787 * FIXME: is this still true?
788 * Nasty one here. We can't use dma_alloc_coherent to send the
789 * same table to everyone. We have to go remap it for them all
792 writel(c->unit + 2, &msg->body[0]);
793 writel(0, &msg->body[1]);
794 writel(0x54000000 | i2o_systab.len, &msg->body[2]);
795 writel(i2o_systab.phys, &msg->body[3]);
796 writel(0x54000000 | sb->current_mem_size, &msg->body[4]);
797 writel(sb->current_mem_base, &msg->body[5]);
798 writel(0xd4000000 | sb->current_io_size, &msg->body[6]);
799 writel(sb->current_io_base, &msg->body[6]);
801 rc = i2o_msg_post_wait(c, m, 120);
803 dma_unmap_single(dev, i2o_systab.phys, i2o_systab.len,
807 printk(KERN_ERR "%s: Unable to set SysTab (status=%#x).\n",
810 pr_debug("%s: SysTab set.\n", c->name);
812 i2o_status_get(c); // Entered READY state
818 * i2o_iop_online - Bring a controller online into OPERATIONAL state.
821 * Send the system table and enable the I2O controller.
823 * Returns 0 on success or negativer error code on failure.
825 static int i2o_iop_online(struct i2o_controller *c)
829 rc = i2o_iop_systab_set(c);
834 pr_debug("%s: Attempting to enable...\n", c->name);
835 rc = i2o_iop_enable(c);
843 * i2o_iop_remove - Remove the I2O controller from the I2O core
846 * Remove the I2O controller from the I2O core. If devices are attached to
847 * the controller remove these also and finally reset the controller.
849 void i2o_iop_remove(struct i2o_controller *c)
851 struct i2o_device *dev, *tmp;
853 pr_debug("%s: deleting controller\n", c->name);
855 i2o_driver_notify_controller_remove_all(c);
859 list_for_each_entry_safe(dev, tmp, &c->devices, list)
860 i2o_device_remove(dev);
862 device_del(&c->device);
864 /* Ask the IOP to switch to RESET state */
867 put_device(&c->device);
871 * i2o_systab_build - Build system table
873 * The system table contains information about all the IOPs in the system
874 * (duh) and is used by the Executives on the IOPs to establish peer2peer
875 * connections. We're not supporting peer2peer at the moment, but this
876 * will be needed down the road for things like lan2lan forwarding.
878 * Returns 0 on success or negative error code on failure.
880 static int i2o_systab_build(void)
882 struct i2o_controller *c, *tmp;
883 int num_controllers = 0;
886 struct i2o_sys_tbl *systab = i2o_systab.virt;
888 list_for_each_entry_safe(c, tmp, &i2o_controllers, list)
892 change_ind = systab->change_ind;
893 kfree(i2o_systab.virt);
897 i2o_systab.len = sizeof(struct i2o_sys_tbl) + num_controllers *
898 sizeof(struct i2o_sys_tbl_entry);
900 systab = i2o_systab.virt = kmalloc(i2o_systab.len, GFP_KERNEL);
902 printk(KERN_ERR "i2o: unable to allocate memory for System "
906 memset(systab, 0, i2o_systab.len);
908 systab->version = I2OVERSION;
909 systab->change_ind = change_ind + 1;
911 list_for_each_entry_safe(c, tmp, &i2o_controllers, list) {
912 i2o_status_block *sb;
914 if (count >= num_controllers) {
915 printk(KERN_ERR "i2o: controller added while building "
920 sb = c->status_block.virt;
923 * Get updated IOP state so we have the latest information
925 * We should delete the controller at this point if it
926 * doesn't respond since if it's not on the system table
927 * it is techninically not part of the I2O subsystem...
929 if (unlikely(i2o_status_get(c))) {
930 printk(KERN_ERR "%s: Deleting b/c could not get status"
931 " while attempting to build system table\n",
934 continue; // try the next one
937 systab->iops[count].org_id = sb->org_id;
938 systab->iops[count].iop_id = c->unit + 2;
939 systab->iops[count].seg_num = 0;
940 systab->iops[count].i2o_version = sb->i2o_version;
941 systab->iops[count].iop_state = sb->iop_state;
942 systab->iops[count].msg_type = sb->msg_type;
943 systab->iops[count].frame_size = sb->inbound_frame_size;
944 systab->iops[count].last_changed = change_ind;
945 systab->iops[count].iop_capabilities = sb->iop_capabilities;
946 systab->iops[count].inbound_low =
947 i2o_dma_low(c->base.phys + I2O_IN_PORT);
948 systab->iops[count].inbound_high =
949 i2o_dma_high(c->base.phys + I2O_IN_PORT);
954 systab->num_entries = count;
960 * i2o_parse_hrt - Parse the hardware resource table.
963 * We don't do anything with it except dumping it (in debug mode).
967 static int i2o_parse_hrt(struct i2o_controller *c)
974 * i2o_status_get - Get the status block from the I2O controller
977 * Issue a status query on the controller. This updates the attached
978 * status block. The status block could then be accessed through
981 * Returns 0 on sucess or negative error code on failure.
983 int i2o_status_get(struct i2o_controller *c)
985 struct i2o_message __iomem *msg;
988 unsigned long timeout;
990 status_block = (u8 *) c->status_block.virt;
991 memset(status_block, 0, sizeof(i2o_status_block));
993 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
994 if (m == I2O_QUEUE_EMPTY)
997 writel(NINE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
998 writel(I2O_CMD_STATUS_GET << 24 | HOST_TID << 12 | ADAPTER_TID,
1000 writel(i2o_exec_driver.context, &msg->u.s.icntxt);
1001 writel(0, &msg->u.s.tcntxt); // FIXME: use resonable transaction context
1002 writel(0, &msg->body[0]);
1003 writel(0, &msg->body[1]);
1004 writel(i2o_dma_low(c->status_block.phys), &msg->body[2]);
1005 writel(i2o_dma_high(c->status_block.phys), &msg->body[3]);
1006 writel(sizeof(i2o_status_block), &msg->body[4]); /* always 88 bytes */
1010 /* Wait for a reply */
1011 timeout = jiffies + I2O_TIMEOUT_STATUS_GET * HZ;
1012 while (status_block[87] != 0xFF) {
1013 if (time_after(jiffies, timeout)) {
1014 printk(KERN_ERR "%s: Get status timeout.\n", c->name);
1018 set_current_state(TASK_UNINTERRUPTIBLE);
1019 schedule_timeout(1);
1032 * i2o_hrt_get - Get the Hardware Resource Table from the I2O controller
1033 * @c: I2O controller from which the HRT should be fetched
1035 * The HRT contains information about possible hidden devices but is
1036 * mostly useless to us.
1038 * Returns 0 on success or negativer error code on failure.
1040 static int i2o_hrt_get(struct i2o_controller *c)
1044 i2o_hrt *hrt = c->hrt.virt;
1045 u32 size = sizeof(i2o_hrt);
1046 struct device *dev = &c->pdev->dev;
1048 for (i = 0; i < I2O_HRT_GET_TRIES; i++) {
1049 struct i2o_message __iomem *msg;
1052 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
1053 if (m == I2O_QUEUE_EMPTY)
1056 writel(SIX_WORD_MSG_SIZE | SGL_OFFSET_4, &msg->u.head[0]);
1057 writel(I2O_CMD_HRT_GET << 24 | HOST_TID << 12 | ADAPTER_TID,
1059 writel(0xd0000000 | c->hrt.len, &msg->body[0]);
1060 writel(c->hrt.phys, &msg->body[1]);
1062 rc = i2o_msg_post_wait_mem(c, m, 20, &c->hrt);
1065 printk(KERN_ERR "%s: Unable to get HRT (status=%#x)\n",
1070 size = hrt->num_entries * hrt->entry_len << 2;
1071 if (size > c->hrt.len) {
1072 if (i2o_dma_realloc(dev, &c->hrt, size, GFP_KERNEL))
1077 return i2o_parse_hrt(c);
1080 printk(KERN_ERR "%s: Unable to get HRT after %d tries, giving up\n",
1081 c->name, I2O_HRT_GET_TRIES);
1087 * i2o_iop_free - Free the i2o_controller struct
1088 * @c: I2O controller to free
1090 void i2o_iop_free(struct i2o_controller *c)
1097 * i2o_iop_release - release the memory for a I2O controller
1098 * @dev: I2O controller which should be released
1100 * Release the allocated memory. This function is called if refcount of
1101 * device reaches 0 automatically.
1103 static void i2o_iop_release(struct device *dev)
1105 struct i2o_controller *c = to_i2o_controller(dev);
1111 * i2o_iop_alloc - Allocate and initialize a i2o_controller struct
1113 * Allocate the necessary memory for a i2o_controller struct and
1114 * initialize the lists.
1116 * Returns a pointer to the I2O controller or a negative error code on
1119 struct i2o_controller *i2o_iop_alloc(void)
1121 static int unit = 0; /* 0 and 1 are NULL IOP and Local Host */
1122 struct i2o_controller *c;
1124 c = kmalloc(sizeof(*c), GFP_KERNEL);
1126 printk(KERN_ERR "i2o: Insufficient memory to allocate a I2O "
1128 return ERR_PTR(-ENOMEM);
1130 memset(c, 0, sizeof(*c));
1132 INIT_LIST_HEAD(&c->devices);
1133 spin_lock_init(&c->lock);
1134 init_MUTEX(&c->lct_lock);
1136 sprintf(c->name, "iop%d", c->unit);
1138 device_initialize(&c->device);
1139 c->device.release = &i2o_iop_release;
1140 snprintf(c->device.bus_id, BUS_ID_SIZE, "iop%d", c->unit);
1142 #if BITS_PER_LONG == 64
1143 spin_lock_init(&c->context_list_lock);
1144 atomic_set(&c->context_list_counter, 0);
1145 INIT_LIST_HEAD(&c->context_list);
1152 * i2o_iop_add - Initialize the I2O controller and add him to the I2O core
1155 * Initialize the I2O controller and if no error occurs add him to the I2O
1158 * Returns 0 on success or negative error code on failure.
1160 int i2o_iop_add(struct i2o_controller *c)
1164 if((rc = device_add(&c->device))) {
1165 printk(KERN_ERR "%s: could not register controller\n", c->name);
1169 printk(KERN_INFO "%s: Activating I2O controller...\n", c->name);
1170 printk(KERN_INFO "%s: This may take a few minutes if there are many "
1171 "devices\n", c->name);
1173 if ((rc = i2o_iop_activate(c))) {
1174 printk(KERN_ERR "%s: could not activate controller\n",
1179 pr_debug("%s: building sys table...\n", c->name);
1181 if ((rc = i2o_systab_build()))
1184 pr_debug("%s: online controller...\n", c->name);
1186 if ((rc = i2o_iop_online(c)))
1189 pr_debug("%s: getting LCT...\n", c->name);
1191 if ((rc = i2o_exec_lct_get(c)))
1194 list_add(&c->list, &i2o_controllers);
1196 i2o_driver_notify_controller_add_all(c);
1198 printk(KERN_INFO "%s: Controller added\n", c->name);
1209 * i2o_event_register - Turn on/off event notification for a I2O device
1210 * @dev: I2O device which should receive the event registration request
1211 * @drv: driver which want to get notified
1212 * @tcntxt: transaction context to use with this notifier
1213 * @evt_mask: mask of events
1215 * Create and posts an event registration message to the task. No reply
1216 * is waited for, or expected. If you do not want further notifications,
1217 * call the i2o_event_register again with a evt_mask of 0.
1219 * Returns 0 on success or -ETIMEDOUT if no message could be fetched for
1220 * sending the request.
1222 int i2o_event_register(struct i2o_device *dev, struct i2o_driver *drv,
1223 int tcntxt, u32 evt_mask)
1225 struct i2o_controller *c = dev->iop;
1226 struct i2o_message __iomem *msg;
1229 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
1230 if (m == I2O_QUEUE_EMPTY)
1233 writel(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
1234 writel(I2O_CMD_UTIL_EVT_REGISTER << 24 | HOST_TID << 12 | dev->lct_data.
1235 tid, &msg->u.head[1]);
1236 writel(drv->context, &msg->u.s.icntxt);
1237 writel(tcntxt, &msg->u.s.tcntxt);
1238 writel(evt_mask, &msg->body[0]);
1246 * i2o_iop_init - I2O main initialization function
1248 * Initialize the I2O drivers (OSM) functions, register the Executive OSM,
1249 * initialize the I2O PCI part and finally initialize I2O device stuff.
1251 * Returns 0 on success or negative error code on failure.
1253 static int __init i2o_iop_init(void)
1257 printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
1259 rc = i2o_device_init();
1263 rc = i2o_driver_init();
1267 rc = i2o_exec_init();
1271 rc = i2o_pci_init();
1291 * i2o_iop_exit - I2O main exit function
1293 * Removes I2O controllers from PCI subsystem and shut down OSMs.
1295 static void __exit i2o_iop_exit(void)
1303 module_init(i2o_iop_init);
1304 module_exit(i2o_iop_exit);
1306 MODULE_AUTHOR("Red Hat Software");
1307 MODULE_LICENSE("GPL");
1308 MODULE_DESCRIPTION(OSM_DESCRIPTION);
1309 MODULE_VERSION(OSM_VERSION);
1311 #if BITS_PER_LONG == 64
1312 EXPORT_SYMBOL(i2o_cntxt_list_add);
1313 EXPORT_SYMBOL(i2o_cntxt_list_get);
1314 EXPORT_SYMBOL(i2o_cntxt_list_remove);
1315 EXPORT_SYMBOL(i2o_cntxt_list_get_ptr);
1317 EXPORT_SYMBOL(i2o_msg_get_wait);
1318 EXPORT_SYMBOL(i2o_msg_nop);
1319 EXPORT_SYMBOL(i2o_find_iop);
1320 EXPORT_SYMBOL(i2o_iop_find_device);
1321 EXPORT_SYMBOL(i2o_event_register);
1322 EXPORT_SYMBOL(i2o_status_get);
1323 EXPORT_SYMBOL(i2o_controllers);