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>
31 /* global I2O controller list */
32 LIST_HEAD(i2o_controllers);
35 * global I2O System Table. Contains information about all the IOPs in the
36 * system. Used to inform IOPs about each others existence.
38 static struct i2o_dma i2o_systab;
40 /* Module internal functions from other sources */
41 extern struct i2o_driver i2o_exec_driver;
42 extern int i2o_exec_lct_get(struct i2o_controller *);
43 extern void i2o_device_remove(struct i2o_device *);
45 extern int __init i2o_driver_init(void);
46 extern void __exit i2o_driver_exit(void);
47 extern int __init i2o_exec_init(void);
48 extern void __exit i2o_exec_exit(void);
49 extern int __init i2o_pci_init(void);
50 extern void __exit i2o_pci_exit(void);
51 extern int i2o_device_init(void);
52 extern void i2o_device_exit(void);
55 * i2o_msg_nop - Returns a message which is not used
56 * @c: I2O controller from which the message was created
57 * @m: message which should be returned
59 * If you fetch a message via i2o_msg_get, and can't use it, you must
60 * return the message with this function. Otherwise the message frame
63 void i2o_msg_nop(struct i2o_controller *c, u32 m)
65 struct i2o_message *msg = c->in_queue.virt + m;
67 writel(THREE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
68 writel(I2O_CMD_UTIL_NOP << 24 | HOST_TID << 12 | ADAPTER_TID,
70 writel(0, &msg->u.head[2]);
71 writel(0, &msg->u.head[3]);
76 * i2o_msg_get_wait - obtain an I2O message from the IOP
78 * @msg: pointer to a I2O message pointer
79 * @wait: how long to wait until timeout
81 * This function waits up to wait seconds for a message slot to be
84 * On a success the message is returned and the pointer to the message is
85 * set in msg. The returned message is the physical page frame offset
86 * address from the read port (see the i2o spec). If no message is
87 * available returns I2O_QUEUE_EMPTY and msg is leaved untouched.
89 u32 i2o_msg_get_wait(struct i2o_controller *c, struct i2o_message **msg,
92 unsigned long timeout = jiffies + wait * HZ;
95 while ((m = i2o_msg_get(c, msg)) == I2O_QUEUE_EMPTY) {
96 if (time_after(jiffies, timeout)) {
97 pr_debug("%s: Timeout waiting for message frame.\n",
99 return I2O_QUEUE_EMPTY;
101 set_current_state(TASK_UNINTERRUPTIBLE);
108 #if BITS_PER_LONG == 64
110 * i2o_cntxt_list_add - Append a pointer to context list and return a id
111 * @c: controller to which the context list belong
112 * @ptr: pointer to add to the context list
114 * Because the context field in I2O is only 32-bit large, on 64-bit the
115 * pointer is to large to fit in the context field. The i2o_cntxt_list
116 * functions therefore map pointers to context fields.
118 * Returns context id > 0 on success or 0 on failure.
120 u32 i2o_cntxt_list_add(struct i2o_controller *c, void *ptr)
122 struct i2o_context_list_element *entry;
126 printk(KERN_ERR "NULL pointer found!\n");
128 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
130 printk(KERN_ERR "i2o: Could not allocate memory for context "
136 entry->timestamp = jiffies;
137 INIT_LIST_HEAD(&entry->list);
139 spin_lock_irqsave(&c->context_list_lock, flags);
141 if (unlikely(atomic_inc_and_test(&c->context_list_counter)))
142 atomic_inc(&c->context_list_counter);
144 entry->context = atomic_read(&c->context_list_counter);
146 list_add(&entry->list, &c->context_list);
148 spin_unlock_irqrestore(&c->context_list_lock, flags);
150 pr_debug("Add context to list %p -> %d\n", ptr, context);
152 return entry->context;
156 * i2o_cntxt_list_remove - Remove a pointer from the context list
157 * @c: controller to which the context list belong
158 * @ptr: pointer which should be removed from the context list
160 * Removes a previously added pointer from the context list and returns
161 * the matching context id.
163 * Returns context id on succes or 0 on failure.
165 u32 i2o_cntxt_list_remove(struct i2o_controller *c, void *ptr)
167 struct i2o_context_list_element *entry;
171 spin_lock_irqsave(&c->context_list_lock, flags);
172 list_for_each_entry(entry, &c->context_list, list)
173 if (entry->ptr == ptr) {
174 list_del(&entry->list);
175 context = entry->context;
179 spin_unlock_irqrestore(&c->context_list_lock, flags);
182 printk(KERN_WARNING "i2o: Could not remove nonexistent ptr "
185 pr_debug("remove ptr from context list %d -> %p\n", context, ptr);
191 * i2o_cntxt_list_get - Get a pointer from the context list and remove it
192 * @c: controller to which the context list belong
193 * @context: context id to which the pointer belong
195 * Returns pointer to the matching context id on success or NULL on
198 void *i2o_cntxt_list_get(struct i2o_controller *c, u32 context)
200 struct i2o_context_list_element *entry;
204 spin_lock_irqsave(&c->context_list_lock, flags);
205 list_for_each_entry(entry, &c->context_list, list)
206 if (entry->context == context) {
207 list_del(&entry->list);
212 spin_unlock_irqrestore(&c->context_list_lock, flags);
215 printk(KERN_WARNING "i2o: context id %d not found\n", context);
217 pr_debug("get ptr from context list %d -> %p\n", context, ptr);
223 * i2o_cntxt_list_get_ptr - Get a context id from the context list
224 * @c: controller to which the context list belong
225 * @ptr: pointer to which the context id should be fetched
227 * Returns context id which matches to the pointer on succes or 0 on
230 u32 i2o_cntxt_list_get_ptr(struct i2o_controller * c, void *ptr)
232 struct i2o_context_list_element *entry;
236 spin_lock_irqsave(&c->context_list_lock, flags);
237 list_for_each_entry(entry, &c->context_list, list)
238 if (entry->ptr == ptr) {
239 context = entry->context;
242 spin_unlock_irqrestore(&c->context_list_lock, flags);
245 printk(KERN_WARNING "i2o: Could not find nonexistent ptr "
248 pr_debug("get context id from context list %p -> %d\n", ptr, context);
255 * i2o_iop_find - Find an I2O controller by id
256 * @unit: unit number of the I2O controller to search for
258 * Lookup the I2O controller on the controller list.
260 * Returns pointer to the I2O controller on success or NULL if not found.
262 struct i2o_controller *i2o_find_iop(int unit)
264 struct i2o_controller *c;
266 list_for_each_entry(c, &i2o_controllers, list) {
275 * i2o_iop_find_device - Find a I2O device on an I2O controller
276 * @c: I2O controller where the I2O device hangs on
277 * @tid: TID of the I2O device to search for
279 * Searches the devices of the I2O controller for a device with TID tid and
282 * Returns a pointer to the I2O device if found, otherwise NULL.
284 struct i2o_device *i2o_iop_find_device(struct i2o_controller *c, u16 tid)
286 struct i2o_device *dev;
288 list_for_each_entry(dev, &c->devices, list)
289 if (dev->lct_data.tid == tid)
296 * i2o_quiesce_controller - quiesce controller
299 * Quiesce an IOP. Causes IOP to make external operation quiescent
300 * (i2o 'READY' state). Internal operation of the IOP continues normally.
302 * Returns 0 on success or negative error code on failure.
304 static int i2o_iop_quiesce(struct i2o_controller *c)
306 struct i2o_message *msg;
308 i2o_status_block *sb = c->status_block.virt;
313 /* SysQuiesce discarded if IOP not in READY or OPERATIONAL state */
314 if ((sb->iop_state != ADAPTER_STATE_READY) &&
315 (sb->iop_state != ADAPTER_STATE_OPERATIONAL))
318 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
319 if (m == I2O_QUEUE_EMPTY)
322 writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
323 writel(I2O_CMD_SYS_QUIESCE << 24 | HOST_TID << 12 | ADAPTER_TID,
326 /* Long timeout needed for quiesce if lots of devices */
327 if ((rc = i2o_msg_post_wait(c, m, 240)))
328 printk(KERN_INFO "%s: Unable to quiesce (status=%#x).\n",
331 pr_debug("%s: Quiesced.\n", c->name);
333 i2o_status_get(c); // Entered READY state
339 * i2o_iop_enable - move controller from ready to OPERATIONAL
342 * Enable IOP. This allows the IOP to resume external operations and
343 * reverses the effect of a quiesce. Returns zero or an error code if
346 static int i2o_iop_enable(struct i2o_controller *c)
348 struct i2o_message *msg;
350 i2o_status_block *sb = c->status_block.virt;
355 /* Enable only allowed on READY state */
356 if (sb->iop_state != ADAPTER_STATE_READY)
359 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
360 if (m == I2O_QUEUE_EMPTY)
363 writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
364 writel(I2O_CMD_SYS_ENABLE << 24 | HOST_TID << 12 | ADAPTER_TID,
367 /* How long of a timeout do we need? */
368 if ((rc = i2o_msg_post_wait(c, m, 240)))
369 printk(KERN_ERR "%s: Could not enable (status=%#x).\n",
372 pr_debug("%s: Enabled.\n", c->name);
374 i2o_status_get(c); // entered OPERATIONAL state
380 * i2o_iop_quiesce_all - Quiesce all I2O controllers on the system
382 * Quiesce all I2O controllers which are connected to the system.
384 static inline void i2o_iop_quiesce_all(void)
386 struct i2o_controller *c, *tmp;
388 list_for_each_entry_safe(c, tmp, &i2o_controllers, list) {
395 * i2o_iop_enable_all - Enables all controllers on the system
397 * Enables all I2O controllers which are connected to the system.
399 static inline void i2o_iop_enable_all(void)
401 struct i2o_controller *c, *tmp;
403 list_for_each_entry_safe(c, tmp, &i2o_controllers, list)
408 * i2o_clear_controller - Bring I2O controller into HOLD state
411 * Clear an IOP to HOLD state, ie. terminate external operations, clear all
412 * input queues and prepare for a system restart. IOP's internal operation
413 * continues normally and the outbound queue is alive. The IOP is not
414 * expected to rebuild its LCT.
416 * Returns 0 on success or negative error code on failure.
418 static int i2o_iop_clear(struct i2o_controller *c)
420 struct i2o_message *msg;
424 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
425 if (m == I2O_QUEUE_EMPTY)
428 /* Quiesce all IOPs first */
429 i2o_iop_quiesce_all();
431 writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
432 writel(I2O_CMD_ADAPTER_CLEAR << 24 | HOST_TID << 12 | ADAPTER_TID,
435 if ((rc = i2o_msg_post_wait(c, m, 30)))
436 printk(KERN_INFO "%s: Unable to clear (status=%#x).\n",
439 pr_debug("%s: Cleared.\n", c->name);
441 /* Enable all IOPs */
442 i2o_iop_enable_all();
450 * i2o_iop_reset - reset an I2O controller
451 * @c: controller to reset
453 * Reset the IOP into INIT state and wait until IOP gets into RESET state.
454 * Terminate all external operations, clear IOP's inbound and outbound
455 * queues, terminate all DDMs, and reload the IOP's operating environment
456 * and all local DDMs. The IOP rebuilds its LCT.
458 static int i2o_iop_reset(struct i2o_controller *c)
460 u8 *status = c->status.virt;
461 struct i2o_message *msg;
463 unsigned long timeout;
464 i2o_status_block *sb = c->status_block.virt;
467 pr_debug("Resetting controller\n");
469 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
470 if (m == I2O_QUEUE_EMPTY)
473 memset(status, 0, 4);
475 /* Quiesce all IOPs first */
476 i2o_iop_quiesce_all();
478 writel(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
479 writel(I2O_CMD_ADAPTER_RESET << 24 | HOST_TID << 12 | ADAPTER_TID,
481 writel(i2o_exec_driver.context, &msg->u.s.icntxt);
482 writel(0, &msg->u.s.tcntxt); //FIXME: use reasonable transaction context
483 writel(0, &msg->body[0]);
484 writel(0, &msg->body[1]);
485 writel(i2o_ptr_low((void *)c->status.phys), &msg->body[2]);
486 writel(i2o_ptr_high((void *)c->status.phys), &msg->body[3]);
490 /* Wait for a reply */
491 timeout = jiffies + I2O_TIMEOUT_RESET * HZ;
493 if (time_after(jiffies, timeout)) {
494 printk(KERN_ERR "IOP reset timeout.\n");
498 set_current_state(TASK_UNINTERRUPTIBLE);
504 if (*status == I2O_CMD_IN_PROGRESS) {
506 * Once the reset is sent, the IOP goes into the INIT state
507 * which is indeterminate. We need to wait until the IOP
508 * has rebooted before we can let the system talk to
509 * it. We read the inbound Free_List until a message is
510 * available. If we can't read one in the given ammount of
511 * time, we assume the IOP could not reboot properly.
513 pr_debug("%s: Reset in progress, waiting for reboot...\n",
516 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_RESET);
517 while (m == I2O_QUEUE_EMPTY) {
518 if (time_after(jiffies, timeout)) {
519 printk(KERN_ERR "IOP reset timeout.\n");
523 set_current_state(TASK_UNINTERRUPTIBLE);
526 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_RESET);
531 /* from here all quiesce commands are safe */
534 /* If IopReset was rejected or didn't perform reset, try IopClear */
536 if (*status == I2O_CMD_REJECTED || sb->iop_state != ADAPTER_STATE_RESET) {
537 printk(KERN_WARNING "%s: Reset rejected, trying to clear\n",
541 pr_debug("%s: Reset completed.\n", c->name);
544 /* Enable all IOPs */
545 i2o_iop_enable_all();
551 * i2o_iop_init_outbound_queue - setup the outbound message queue
554 * Clear and (re)initialize IOP's outbound queue and post the message
557 * Returns 0 on success or a negative errno code on failure.
559 int i2o_iop_init_outbound_queue(struct i2o_controller *c)
561 u8 *status = c->status.virt;
563 struct i2o_message *msg;
567 pr_debug("%s: Initializing Outbound Queue...\n", c->name);
569 memset(status, 0, 4);
571 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
572 if (m == I2O_QUEUE_EMPTY)
575 writel(EIGHT_WORD_MSG_SIZE | TRL_OFFSET_6, &msg->u.head[0]);
576 writel(I2O_CMD_OUTBOUND_INIT << 24 | HOST_TID << 12 | ADAPTER_TID,
578 writel(i2o_exec_driver.context, &msg->u.s.icntxt);
579 writel(0x0106, &msg->u.s.tcntxt); /* FIXME: why 0x0106, maybe in
581 writel(PAGE_SIZE, &msg->body[0]);
582 writel(MSG_FRAME_SIZE << 16 | 0x80, &msg->body[1]); /* Outbound msg frame
583 size in words and Initcode */
584 writel(0xd0000004, &msg->body[2]);
585 writel(i2o_ptr_low((void *)c->status.phys), &msg->body[3]);
586 writel(i2o_ptr_high((void *)c->status.phys), &msg->body[4]);
590 timeout = jiffies + I2O_TIMEOUT_INIT_OUTBOUND_QUEUE * HZ;
591 while (*status <= I2O_CMD_IN_PROGRESS) {
592 if (time_after(jiffies, timeout)) {
593 printk(KERN_WARNING "%s: Timeout Initializing\n",
597 set_current_state(TASK_UNINTERRUPTIBLE);
603 m = c->out_queue.phys;
606 for (i = 0; i < NMBR_MSG_FRAMES; i++) {
607 i2o_flush_reply(c, m);
608 m += MSG_FRAME_SIZE * 4;
615 * i2o_iop_activate - Bring controller up to HOLD
618 * This function brings an I2O controller into HOLD state. The adapter
619 * is reset if necessary and then the queues and resource table are read.
621 * Returns 0 on success or negative error code on failure.
623 static int i2o_iop_activate(struct i2o_controller *c)
625 i2o_status_block *sb = c->status_block.virt;
627 /* In INIT state, Wait Inbound Q to initialize (in i2o_status_get) */
628 /* In READY state, Get status */
630 rc = i2o_status_get(c);
632 printk(KERN_INFO "Unable to obtain status of %s, "
633 "attempting a reset.\n", c->name);
634 if (i2o_iop_reset(c))
638 if (sb->i2o_version > I2OVER15) {
639 printk(KERN_ERR "%s: Not running vrs. 1.5. of the I2O "
640 "Specification.\n", c->name);
644 switch (sb->iop_state) {
645 case ADAPTER_STATE_FAULTED:
646 printk(KERN_CRIT "%s: hardware fault\n", c->name);
649 case ADAPTER_STATE_READY:
650 case ADAPTER_STATE_OPERATIONAL:
651 case ADAPTER_STATE_HOLD:
652 case ADAPTER_STATE_FAILED:
653 pr_debug("already running, trying to reset...\n");
654 if (i2o_iop_reset(c))
658 rc = i2o_iop_init_outbound_queue(c);
672 * i2o_iop_systab_set - Set the I2O System Table of the specified IOP
673 * @c: I2O controller to which the system table should be send
675 * Before the systab could be set i2o_systab_build() must be called.
677 * Returns 0 on success or negative error code on failure.
679 static int i2o_iop_systab_set(struct i2o_controller *c)
681 struct i2o_message *msg;
683 i2o_status_block *sb = c->status_block.virt;
684 struct device *dev = &c->pdev->dev;
685 struct resource *root;
688 if (sb->current_mem_size < sb->desired_mem_size) {
689 struct resource *res = &c->mem_resource;
690 res->name = c->pdev->bus->name;
691 res->flags = IORESOURCE_MEM;
694 printk("%s: requires private memory resources.\n", c->name);
695 root = pci_find_parent_resource(c->pdev, res);
697 printk("Can't find parent resource!\n");
698 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 */
701 sb->current_mem_size = 1 + res->end - res->start;
702 sb->current_mem_base = res->start;
704 "%s: allocated %ld bytes of PCI memory at 0x%08lX.\n",
705 c->name, 1 + res->end - res->start, res->start);
709 if (sb->current_io_size < sb->desired_io_size) {
710 struct resource *res = &c->io_resource;
711 res->name = c->pdev->bus->name;
712 res->flags = IORESOURCE_IO;
715 printk("%s: requires private memory resources.\n", c->name);
716 root = pci_find_parent_resource(c->pdev, res);
718 printk("Can't find parent resource!\n");
719 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 */
722 sb->current_io_size = 1 + res->end - res->start;
723 sb->current_mem_base = res->start;
725 "%s: allocated %ld bytes of PCI I/O at 0x%08lX.\n",
726 c->name, 1 + res->end - res->start, res->start);
730 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
731 if (m == I2O_QUEUE_EMPTY)
734 i2o_systab.phys = dma_map_single(dev, i2o_systab.virt, i2o_systab.len,
736 if (!i2o_systab.phys) {
741 writel(I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6, &msg->u.head[0]);
742 writel(I2O_CMD_SYS_TAB_SET << 24 | HOST_TID << 12 | ADAPTER_TID,
746 * Provide three SGL-elements:
747 * System table (SysTab), Private memory space declaration and
748 * Private i/o space declaration
750 * FIXME: is this still true?
751 * Nasty one here. We can't use dma_alloc_coherent to send the
752 * same table to everyone. We have to go remap it for them all
755 writel(c->unit + 2, &msg->body[0]);
756 writel(0, &msg->body[1]);
757 writel(0x54000000 | i2o_systab.phys, &msg->body[2]);
758 writel(i2o_systab.phys, &msg->body[3]);
759 writel(0x54000000 | sb->current_mem_size, &msg->body[4]);
760 writel(sb->current_mem_base, &msg->body[5]);
761 writel(0xd4000000 | sb->current_io_size, &msg->body[6]);
762 writel(sb->current_io_base, &msg->body[6]);
764 rc = i2o_msg_post_wait(c, m, 120);
766 dma_unmap_single(dev, i2o_systab.phys, i2o_systab.len,
770 printk(KERN_ERR "%s: Unable to set SysTab (status=%#x).\n",
773 pr_debug("%s: SysTab set.\n", c->name);
775 i2o_status_get(c); // Entered READY state
781 * i2o_iop_online - Bring a controller online into OPERATIONAL state.
784 * Send the system table and enable the I2O controller.
786 * Returns 0 on success or negativer error code on failure.
788 static int i2o_iop_online(struct i2o_controller *c)
792 rc = i2o_iop_systab_set(c);
797 pr_debug("%s: Attempting to enable...\n", c->name);
798 rc = i2o_iop_enable(c);
806 * i2o_iop_remove - Remove the I2O controller from the I2O core
809 * Remove the I2O controller from the I2O core. If devices are attached to
810 * the controller remove these also and finally reset the controller.
812 void i2o_iop_remove(struct i2o_controller *c)
814 struct i2o_device *dev, *tmp;
816 pr_debug("Deleting controller %s\n", c->name);
818 i2o_driver_notify_controller_remove_all(c);
822 list_for_each_entry_safe(dev, tmp, &c->devices, list)
823 i2o_device_remove(dev);
825 /* Ask the IOP to switch to RESET state */
830 * i2o_systab_build - Build system table
832 * The system table contains information about all the IOPs in the system
833 * (duh) and is used by the Executives on the IOPs to establish peer2peer
834 * connections. We're not supporting peer2peer at the moment, but this
835 * will be needed down the road for things like lan2lan forwarding.
837 * Returns 0 on success or negative error code on failure.
839 static int i2o_systab_build(void)
841 struct i2o_controller *c, *tmp;
842 int num_controllers = 0;
845 struct i2o_sys_tbl *systab = i2o_systab.virt;
847 list_for_each_entry_safe(c, tmp, &i2o_controllers, list)
851 change_ind = systab->change_ind;
852 kfree(i2o_systab.virt);
856 i2o_systab.len = sizeof(struct i2o_sys_tbl) + num_controllers *
857 sizeof(struct i2o_sys_tbl_entry);
859 systab = i2o_systab.virt = kmalloc(i2o_systab.len, GFP_KERNEL);
861 printk(KERN_ERR "i2o: unable to allocate memory for System "
865 memset(systab, 0, i2o_systab.len);
867 systab->version = I2OVERSION;
868 systab->change_ind = change_ind + 1;
870 list_for_each_entry_safe(c, tmp, &i2o_controllers, list) {
871 i2o_status_block *sb;
873 if (count >= num_controllers) {
874 printk(KERN_ERR "i2o: controller added while building "
879 sb = c->status_block.virt;
882 * Get updated IOP state so we have the latest information
884 * We should delete the controller at this point if it
885 * doesn't respond since if it's not on the system table
886 * it is techninically not part of the I2O subsystem...
888 if (unlikely(i2o_status_get(c))) {
889 printk(KERN_ERR "%s: Deleting b/c could not get status"
890 " while attempting to build system table\n",
893 continue; // try the next one
896 systab->iops[count].org_id = sb->org_id;
897 systab->iops[count].iop_id = c->unit + 2;
898 systab->iops[count].seg_num = 0;
899 systab->iops[count].i2o_version = sb->i2o_version;
900 systab->iops[count].iop_state = sb->iop_state;
901 systab->iops[count].msg_type = sb->msg_type;
902 systab->iops[count].frame_size = sb->inbound_frame_size;
903 systab->iops[count].last_changed = change_ind;
904 systab->iops[count].iop_capabilities = sb->iop_capabilities;
905 systab->iops[count].inbound_low = i2o_ptr_low(c->post_port);
906 systab->iops[count].inbound_high = i2o_ptr_high(c->post_port);
911 systab->num_entries = count;
917 * i2o_parse_hrt - Parse the hardware resource table.
920 * We don't do anything with it except dumping it (in debug mode).
924 static int i2o_parse_hrt(struct i2o_controller *c)
931 * i2o_status_get - Get the status block from the I2O controller
934 * Issue a status query on the controller. This updates the attached
935 * status block. The status block could then be accessed through
938 * Returns 0 on sucess or negative error code on failure.
940 int i2o_status_get(struct i2o_controller *c)
942 struct i2o_message *msg;
945 unsigned long timeout;
947 status_block = (u8 *) c->status_block.virt;
948 memset(status_block, 0, sizeof(i2o_status_block));
950 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
951 if (m == I2O_QUEUE_EMPTY)
954 writel(NINE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
955 writel(I2O_CMD_STATUS_GET << 24 | HOST_TID << 12 | ADAPTER_TID,
957 writel(i2o_exec_driver.context, &msg->u.s.icntxt);
958 writel(0, &msg->u.s.tcntxt); // FIXME: use resonable transaction context
959 writel(0, &msg->body[0]);
960 writel(0, &msg->body[1]);
961 writel(i2o_ptr_low((void *)c->status_block.phys), &msg->body[2]);
962 writel(i2o_ptr_high((void *)c->status_block.phys), &msg->body[3]);
963 writel(sizeof(i2o_status_block), &msg->body[4]); /* always 88 bytes */
967 /* Wait for a reply */
968 timeout = jiffies + I2O_TIMEOUT_STATUS_GET * HZ;
969 while (status_block[87] != 0xFF) {
970 if (time_after(jiffies, timeout)) {
971 printk(KERN_ERR "%s: Get status timeout.\n", c->name);
975 set_current_state(TASK_UNINTERRUPTIBLE);
989 * i2o_hrt_get - Get the Hardware Resource Table from the I2O controller
990 * @c: I2O controller from which the HRT should be fetched
992 * The HRT contains information about possible hidden devices but is
993 * mostly useless to us.
995 * Returns 0 on success or negativer error code on failure.
997 int i2o_hrt_get(struct i2o_controller *c)
1001 i2o_hrt *hrt = c->hrt.virt;
1002 u32 size = sizeof(i2o_hrt);
1003 struct device *dev = &c->pdev->dev;
1005 for (i = 0; i < I2O_HRT_GET_TRIES; i++) {
1006 struct i2o_message *msg;
1009 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
1010 if (m == I2O_QUEUE_EMPTY)
1013 writel(SIX_WORD_MSG_SIZE | SGL_OFFSET_4, &msg->u.head[0]);
1014 writel(I2O_CMD_HRT_GET << 24 | HOST_TID << 12 | ADAPTER_TID,
1016 writel(0xd0000000 | c->hrt.len, &msg->body[0]);
1017 writel(c->hrt.phys, &msg->body[1]);
1019 rc = i2o_msg_post_wait_mem(c, m, 20, &c->hrt);
1022 printk(KERN_ERR "%s: Unable to get HRT (status=%#x)\n",
1027 size = hrt->num_entries * hrt->entry_len << 2;
1028 if (size > c->hrt.len) {
1029 if (i2o_dma_realloc(dev, &c->hrt, size, GFP_KERNEL))
1034 return i2o_parse_hrt(c);
1037 printk(KERN_ERR "%s: Unable to get HRT after %d tries, giving up\n",
1038 c->name, I2O_HRT_GET_TRIES);
1044 * i2o_iop_alloc - Allocate and initialize a i2o_controller struct
1046 * Allocate the necessary memory for a i2o_controller struct and
1047 * initialize the lists.
1049 * Returns a pointer to the I2O controller or a negative error code on
1052 struct i2o_controller *i2o_iop_alloc(void)
1054 static int unit = 0; /* 0 and 1 are NULL IOP and Local Host */
1055 struct i2o_controller *c;
1057 c = kmalloc(sizeof(*c), GFP_KERNEL);
1059 printk(KERN_ERR "i2o: Insufficient memory to allocate the "
1061 return ERR_PTR(-ENOMEM);
1063 memset(c, 0, sizeof(*c));
1065 INIT_LIST_HEAD(&c->devices);
1066 c->lock = SPIN_LOCK_UNLOCKED;
1067 init_MUTEX(&c->lct_lock);
1069 sprintf(c->name, "iop%d", c->unit);
1071 #if BITS_PER_LONG == 64
1072 c->context_list_lock = SPIN_LOCK_UNLOCKED;
1073 atomic_set(&c->context_list_counter, 0);
1074 INIT_LIST_HEAD(&c->context_list);
1081 * i2o_iop_free - Free the i2o_controller struct
1082 * @c: I2O controller to free
1084 void i2o_iop_free(struct i2o_controller *c)
1090 * i2o_iop_add - Initialize the I2O controller and add him to the I2O core
1093 * Initialize the I2O controller and if no error occurs add him to the I2O
1096 * Returns 0 on success or negative error code on failure.
1098 int i2o_iop_add(struct i2o_controller *c)
1102 printk(KERN_INFO "%s: Activating I2O controller...\n", c->name);
1103 printk(KERN_INFO "%s: This may take a few minutes if there are many "
1104 "devices\n", c->name);
1106 if ((rc = i2o_iop_activate(c))) {
1107 printk(KERN_ERR "%s: controller could not activated\n",
1113 pr_debug("building sys table %s...\n", c->name);
1115 if ((rc = i2o_systab_build())) {
1120 pr_debug("online controller %s...\n", c->name);
1122 if ((rc = i2o_iop_online(c))) {
1127 pr_debug("getting LCT %s...\n", c->name);
1129 if ((rc = i2o_exec_lct_get(c))) {
1134 list_add(&c->list, &i2o_controllers);
1136 i2o_driver_notify_controller_add_all(c);
1138 printk(KERN_INFO "%s: Controller added\n", c->name);
1144 * i2o_event_register - Turn on/off event notification for a I2O device
1145 * @dev: I2O device which should receive the event registration request
1146 * @drv: driver which want to get notified
1147 * @tcntxt: transaction context to use with this notifier
1148 * @evt_mask: mask of events
1150 * Create and posts an event registration message to the task. No reply
1151 * is waited for, or expected. If you do not want further notifications,
1152 * call the i2o_event_register again with a evt_mask of 0.
1154 * Returns 0 on success or -ETIMEDOUT if no message could be fetched for
1155 * sending the request.
1157 int i2o_event_register(struct i2o_device *dev, struct i2o_driver *drv,
1158 int tcntxt, u32 evt_mask)
1160 struct i2o_controller *c = dev->iop;
1161 struct i2o_message *msg;
1164 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
1165 if (m == I2O_QUEUE_EMPTY)
1168 writel(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
1169 writel(I2O_CMD_UTIL_EVT_REGISTER << 24 | HOST_TID << 12 | dev->lct_data.
1170 tid, &msg->u.head[1]);
1171 writel(drv->context, &msg->u.s.icntxt);
1172 writel(tcntxt, &msg->u.s.tcntxt);
1173 writel(evt_mask, &msg->body[0]);
1181 * i2o_iop_init - I2O main initialization function
1183 * Initialize the I2O drivers (OSM) functions, register the Executive OSM,
1184 * initialize the I2O PCI part and finally initialize I2O device stuff.
1186 * Returns 0 on success or negative error code on failure.
1188 static int __init i2o_iop_init(void)
1192 printk(KERN_INFO "I2O Core - (C) Copyright 1999 Red Hat Software\n");
1194 rc = i2o_device_init();
1198 rc = i2o_driver_init();
1202 rc = i2o_exec_init();
1206 rc = i2o_pci_init();
1226 * i2o_iop_exit - I2O main exit function
1228 * Removes I2O controllers from PCI subsystem and shut down OSMs.
1230 static void __exit i2o_iop_exit(void)
1238 module_init(i2o_iop_init);
1239 module_exit(i2o_iop_exit);
1241 MODULE_AUTHOR("Red Hat Software");
1242 MODULE_DESCRIPTION("I2O Core");
1243 MODULE_LICENSE("GPL");
1245 #if BITS_PER_LONG == 64
1246 EXPORT_SYMBOL(i2o_cntxt_list_add);
1247 EXPORT_SYMBOL(i2o_cntxt_list_get);
1248 EXPORT_SYMBOL(i2o_cntxt_list_remove);
1249 EXPORT_SYMBOL(i2o_cntxt_list_get_ptr);
1251 EXPORT_SYMBOL(i2o_msg_get_wait);
1252 EXPORT_SYMBOL(i2o_msg_nop);
1253 EXPORT_SYMBOL(i2o_find_iop);
1254 EXPORT_SYMBOL(i2o_iop_find_device);
1255 EXPORT_SYMBOL(i2o_event_register);
1256 EXPORT_SYMBOL(i2o_status_get);
1257 EXPORT_SYMBOL(i2o_hrt_get);
1258 EXPORT_SYMBOL(i2o_controllers);