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_reset - reset an I2O controller
460 * @c: controller to reset
462 * Reset the IOP into INIT state and wait until IOP gets into RESET state.
463 * Terminate all external operations, clear IOP's inbound and outbound
464 * queues, terminate all DDMs, and reload the IOP's operating environment
465 * and all local DDMs. The IOP rebuilds its LCT.
467 static int i2o_iop_reset(struct i2o_controller *c)
469 u8 *status = c->status.virt;
470 struct i2o_message __iomem *msg;
472 unsigned long timeout;
473 i2o_status_block *sb = c->status_block.virt;
476 pr_debug("%s: Resetting controller\n", c->name);
478 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
479 if (m == I2O_QUEUE_EMPTY)
482 memset(status, 0, 8);
484 /* Quiesce all IOPs first */
485 i2o_iop_quiesce_all();
487 writel(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
488 writel(I2O_CMD_ADAPTER_RESET << 24 | HOST_TID << 12 | ADAPTER_TID,
490 writel(i2o_exec_driver.context, &msg->u.s.icntxt);
491 writel(0, &msg->u.s.tcntxt); //FIXME: use reasonable transaction context
492 writel(0, &msg->body[0]);
493 writel(0, &msg->body[1]);
494 writel(i2o_ptr_low((void *)c->status.phys), &msg->body[2]);
495 writel(i2o_ptr_high((void *)c->status.phys), &msg->body[3]);
499 /* Wait for a reply */
500 timeout = jiffies + I2O_TIMEOUT_RESET * HZ;
502 if (time_after(jiffies, timeout)) {
503 printk(KERN_ERR "%s: IOP reset timeout.\n", c->name);
509 if (status[1] || status[4]) {
514 set_current_state(TASK_UNINTERRUPTIBLE);
520 if (*status == I2O_CMD_IN_PROGRESS) {
522 * Once the reset is sent, the IOP goes into the INIT state
523 * which is indeterminate. We need to wait until the IOP
524 * has rebooted before we can let the system talk to
525 * it. We read the inbound Free_List until a message is
526 * available. If we can't read one in the given ammount of
527 * time, we assume the IOP could not reboot properly.
529 pr_debug("%s: Reset in progress, waiting for reboot...\n",
532 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_RESET);
533 while (m == I2O_QUEUE_EMPTY) {
534 if (time_after(jiffies, timeout)) {
535 printk(KERN_ERR "%s: IOP reset timeout.\n",
540 set_current_state(TASK_UNINTERRUPTIBLE);
543 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_RESET);
548 /* from here all quiesce commands are safe */
551 /* If IopReset was rejected or didn't perform reset, try IopClear */
553 if (*status == I2O_CMD_REJECTED || sb->iop_state != ADAPTER_STATE_RESET) {
554 printk(KERN_WARNING "%s: Reset rejected, trying to clear\n",
558 pr_debug("%s: Reset completed.\n", c->name);
561 /* Enable all IOPs */
562 i2o_iop_enable_all();
568 * i2o_iop_init_outbound_queue - setup the outbound message queue
571 * Clear and (re)initialize IOP's outbound queue and post the message
574 * Returns 0 on success or a negative errno code on failure.
576 static int i2o_iop_init_outbound_queue(struct i2o_controller *c)
578 u8 *status = c->status.virt;
580 struct i2o_message __iomem *msg;
584 pr_debug("%s: Initializing Outbound Queue...\n", c->name);
586 memset(status, 0, 4);
588 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
589 if (m == I2O_QUEUE_EMPTY)
592 writel(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_6, &msg->u.head[0]);
593 writel(I2O_CMD_OUTBOUND_INIT << 24 | HOST_TID << 12 | ADAPTER_TID,
595 writel(i2o_exec_driver.context, &msg->u.s.icntxt);
596 writel(0x00000000, &msg->u.s.tcntxt);
597 writel(PAGE_SIZE, &msg->body[0]);
598 writel(MSG_FRAME_SIZE << 16 | 0x80, &msg->body[1]); /* Outbound msg frame
599 size in words and Initcode */
600 writel(0xd0000004, &msg->body[2]);
601 writel(i2o_ptr_low((void *)c->status.phys), &msg->body[3]);
602 writel(i2o_ptr_high((void *)c->status.phys), &msg->body[4]);
606 timeout = jiffies + I2O_TIMEOUT_INIT_OUTBOUND_QUEUE * HZ;
607 while (*status <= I2O_CMD_IN_PROGRESS) {
608 if (time_after(jiffies, timeout)) {
609 printk(KERN_WARNING "%s: Timeout Initializing\n",
613 set_current_state(TASK_UNINTERRUPTIBLE);
619 m = c->out_queue.phys;
622 for (i = 0; i < NMBR_MSG_FRAMES; i++) {
623 i2o_flush_reply(c, m);
624 udelay(1); /* Promise */
625 m += MSG_FRAME_SIZE * 4;
632 * i2o_iop_send_nop - send a core NOP message
635 * Send a no-operation message with a reply set to cause no
636 * action either. Needed for bringing up promise controllers.
638 static int i2o_iop_send_nop(struct i2o_controller *c)
640 struct i2o_message __iomem *msg;
641 u32 m = i2o_msg_get_wait(c, &msg, HZ);
642 if (m == I2O_QUEUE_EMPTY)
649 * i2o_iop_activate - Bring controller up to HOLD
652 * This function brings an I2O controller into HOLD state. The adapter
653 * is reset if necessary and then the queues and resource table are read.
655 * Returns 0 on success or negative error code on failure.
657 static int i2o_iop_activate(struct i2o_controller *c)
659 struct pci_dev *i960 = NULL;
660 i2o_status_block *sb = c->status_block.virt;
664 /* Beat up the hardware first of all */
666 pci_find_slot(c->pdev->bus->number,
667 PCI_DEVFN(PCI_SLOT(c->pdev->devfn), 0));
669 pci_write_config_word(i960, 0x42, 0);
671 /* Follow this sequence precisely or the controller
672 ceases to perform useful functions until reboot */
673 if ((rc = i2o_iop_send_nop(c)))
676 if ((rc = i2o_iop_reset(c)))
680 /* In INIT state, Wait Inbound Q to initialize (in i2o_status_get) */
681 /* In READY state, Get status */
683 rc = i2o_status_get(c);
685 printk(KERN_INFO "%s: Unable to obtain status, "
686 "attempting a reset.\n", c->name);
687 if (i2o_iop_reset(c))
691 if (sb->i2o_version > I2OVER15) {
692 printk(KERN_ERR "%s: Not running version 1.5 of the I2O "
693 "Specification.\n", c->name);
697 switch (sb->iop_state) {
698 case ADAPTER_STATE_FAULTED:
699 printk(KERN_CRIT "%s: hardware fault\n", c->name);
702 case ADAPTER_STATE_READY:
703 case ADAPTER_STATE_OPERATIONAL:
704 case ADAPTER_STATE_HOLD:
705 case ADAPTER_STATE_FAILED:
706 pr_debug("%s: already running, trying to reset...\n", c->name);
707 if (i2o_iop_reset(c))
711 rc = i2o_iop_init_outbound_queue(c);
716 if ((rc = i2o_iop_send_nop(c)))
719 if ((rc = i2o_status_get(c)))
723 pci_write_config_word(i960, 0x42, 0x3FF);
734 * i2o_iop_systab_set - Set the I2O System Table of the specified IOP
735 * @c: I2O controller to which the system table should be send
737 * Before the systab could be set i2o_systab_build() must be called.
739 * Returns 0 on success or negative error code on failure.
741 static int i2o_iop_systab_set(struct i2o_controller *c)
743 struct i2o_message __iomem *msg;
745 i2o_status_block *sb = c->status_block.virt;
746 struct device *dev = &c->pdev->dev;
747 struct resource *root;
750 if (sb->current_mem_size < sb->desired_mem_size) {
751 struct resource *res = &c->mem_resource;
752 res->name = c->pdev->bus->name;
753 res->flags = IORESOURCE_MEM;
756 printk(KERN_INFO "%s: requires private memory resources.\n",
758 root = pci_find_parent_resource(c->pdev, res);
760 printk(KERN_WARNING "%s: Can't find parent resource!\n",
762 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 */
765 sb->current_mem_size = 1 + res->end - res->start;
766 sb->current_mem_base = res->start;
767 printk(KERN_INFO "%s: allocated %ld bytes of PCI memory"
768 " at 0x%08lX.\n", c->name,
769 1 + res->end - res->start, res->start);
773 if (sb->current_io_size < sb->desired_io_size) {
774 struct resource *res = &c->io_resource;
775 res->name = c->pdev->bus->name;
776 res->flags = IORESOURCE_IO;
779 printk(KERN_INFO "%s: requires private memory resources.\n",
781 root = pci_find_parent_resource(c->pdev, res);
783 printk(KERN_WARNING "%s: Can't find parent resource!\n",
785 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 */
788 sb->current_io_size = 1 + res->end - res->start;
789 sb->current_mem_base = res->start;
790 printk(KERN_INFO "%s: allocated %ld bytes of PCI I/O at"
791 " 0x%08lX.\n", c->name,
792 1 + res->end - res->start, res->start);
796 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
797 if (m == I2O_QUEUE_EMPTY)
800 i2o_systab.phys = dma_map_single(dev, i2o_systab.virt, i2o_systab.len,
802 if (!i2o_systab.phys) {
807 writel(I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6, &msg->u.head[0]);
808 writel(I2O_CMD_SYS_TAB_SET << 24 | HOST_TID << 12 | ADAPTER_TID,
812 * Provide three SGL-elements:
813 * System table (SysTab), Private memory space declaration and
814 * Private i/o space declaration
816 * FIXME: is this still true?
817 * Nasty one here. We can't use dma_alloc_coherent to send the
818 * same table to everyone. We have to go remap it for them all
821 writel(c->unit + 2, &msg->body[0]);
822 writel(0, &msg->body[1]);
823 writel(0x54000000 | i2o_systab.len, &msg->body[2]);
824 writel(i2o_systab.phys, &msg->body[3]);
825 writel(0x54000000 | sb->current_mem_size, &msg->body[4]);
826 writel(sb->current_mem_base, &msg->body[5]);
827 writel(0xd4000000 | sb->current_io_size, &msg->body[6]);
828 writel(sb->current_io_base, &msg->body[6]);
830 rc = i2o_msg_post_wait(c, m, 120);
832 dma_unmap_single(dev, i2o_systab.phys, i2o_systab.len,
836 printk(KERN_ERR "%s: Unable to set SysTab (status=%#x).\n",
839 pr_debug("%s: SysTab set.\n", c->name);
841 i2o_status_get(c); // Entered READY state
847 * i2o_iop_online - Bring a controller online into OPERATIONAL state.
850 * Send the system table and enable the I2O controller.
852 * Returns 0 on success or negativer error code on failure.
854 static int i2o_iop_online(struct i2o_controller *c)
858 rc = i2o_iop_systab_set(c);
863 pr_debug("%s: Attempting to enable...\n", c->name);
864 rc = i2o_iop_enable(c);
872 * i2o_iop_remove - Remove the I2O controller from the I2O core
875 * Remove the I2O controller from the I2O core. If devices are attached to
876 * the controller remove these also and finally reset the controller.
878 void i2o_iop_remove(struct i2o_controller *c)
880 struct i2o_device *dev, *tmp;
882 pr_debug("%s: deleting controller\n", c->name);
884 i2o_driver_notify_controller_remove_all(c);
888 list_for_each_entry_safe(dev, tmp, &c->devices, list)
889 i2o_device_remove(dev);
891 device_del(&c->device);
893 /* Ask the IOP to switch to RESET state */
896 put_device(&c->device);
900 * i2o_systab_build - Build system table
902 * The system table contains information about all the IOPs in the system
903 * (duh) and is used by the Executives on the IOPs to establish peer2peer
904 * connections. We're not supporting peer2peer at the moment, but this
905 * will be needed down the road for things like lan2lan forwarding.
907 * Returns 0 on success or negative error code on failure.
909 static int i2o_systab_build(void)
911 struct i2o_controller *c, *tmp;
912 int num_controllers = 0;
915 struct i2o_sys_tbl *systab = i2o_systab.virt;
917 list_for_each_entry_safe(c, tmp, &i2o_controllers, list)
921 change_ind = systab->change_ind;
922 kfree(i2o_systab.virt);
926 i2o_systab.len = sizeof(struct i2o_sys_tbl) + num_controllers *
927 sizeof(struct i2o_sys_tbl_entry);
929 systab = i2o_systab.virt = kmalloc(i2o_systab.len, GFP_KERNEL);
931 printk(KERN_ERR "i2o: unable to allocate memory for System "
935 memset(systab, 0, i2o_systab.len);
937 systab->version = I2OVERSION;
938 systab->change_ind = change_ind + 1;
940 list_for_each_entry_safe(c, tmp, &i2o_controllers, list) {
941 i2o_status_block *sb;
943 if (count >= num_controllers) {
944 printk(KERN_ERR "i2o: controller added while building "
949 sb = c->status_block.virt;
952 * Get updated IOP state so we have the latest information
954 * We should delete the controller at this point if it
955 * doesn't respond since if it's not on the system table
956 * it is techninically not part of the I2O subsystem...
958 if (unlikely(i2o_status_get(c))) {
959 printk(KERN_ERR "%s: Deleting b/c could not get status"
960 " while attempting to build system table\n",
963 continue; // try the next one
966 systab->iops[count].org_id = sb->org_id;
967 systab->iops[count].iop_id = c->unit + 2;
968 systab->iops[count].seg_num = 0;
969 systab->iops[count].i2o_version = sb->i2o_version;
970 systab->iops[count].iop_state = sb->iop_state;
971 systab->iops[count].msg_type = sb->msg_type;
972 systab->iops[count].frame_size = sb->inbound_frame_size;
973 systab->iops[count].last_changed = change_ind;
974 systab->iops[count].iop_capabilities = sb->iop_capabilities;
975 systab->iops[count].inbound_low =
976 i2o_dma_low(c->base.phys + I2O_IN_PORT);
977 systab->iops[count].inbound_high =
978 i2o_dma_high(c->base.phys + I2O_IN_PORT);
983 systab->num_entries = count;
989 * i2o_parse_hrt - Parse the hardware resource table.
992 * We don't do anything with it except dumping it (in debug mode).
996 static int i2o_parse_hrt(struct i2o_controller *c)
1003 * i2o_status_get - Get the status block from the I2O controller
1004 * @c: I2O controller
1006 * Issue a status query on the controller. This updates the attached
1007 * status block. The status block could then be accessed through
1010 * Returns 0 on sucess or negative error code on failure.
1012 int i2o_status_get(struct i2o_controller *c)
1014 struct i2o_message __iomem *msg;
1017 unsigned long timeout;
1019 status_block = (u8 *) c->status_block.virt;
1020 memset(status_block, 0, sizeof(i2o_status_block));
1022 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
1023 if (m == I2O_QUEUE_EMPTY)
1026 writel(NINE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
1027 writel(I2O_CMD_STATUS_GET << 24 | HOST_TID << 12 | ADAPTER_TID,
1029 writel(i2o_exec_driver.context, &msg->u.s.icntxt);
1030 writel(0, &msg->u.s.tcntxt); // FIXME: use resonable transaction context
1031 writel(0, &msg->body[0]);
1032 writel(0, &msg->body[1]);
1033 writel(i2o_ptr_low((void *)c->status_block.phys), &msg->body[2]);
1034 writel(i2o_ptr_high((void *)c->status_block.phys), &msg->body[3]);
1035 writel(sizeof(i2o_status_block), &msg->body[4]); /* always 88 bytes */
1039 /* Wait for a reply */
1040 timeout = jiffies + I2O_TIMEOUT_STATUS_GET * HZ;
1041 while (status_block[87] != 0xFF) {
1042 if (time_after(jiffies, timeout)) {
1043 printk(KERN_ERR "%s: Get status timeout.\n", c->name);
1047 set_current_state(TASK_UNINTERRUPTIBLE);
1048 schedule_timeout(1);
1061 * i2o_hrt_get - Get the Hardware Resource Table from the I2O controller
1062 * @c: I2O controller from which the HRT should be fetched
1064 * The HRT contains information about possible hidden devices but is
1065 * mostly useless to us.
1067 * Returns 0 on success or negativer error code on failure.
1069 static int i2o_hrt_get(struct i2o_controller *c)
1073 i2o_hrt *hrt = c->hrt.virt;
1074 u32 size = sizeof(i2o_hrt);
1075 struct device *dev = &c->pdev->dev;
1077 for (i = 0; i < I2O_HRT_GET_TRIES; i++) {
1078 struct i2o_message __iomem *msg;
1081 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
1082 if (m == I2O_QUEUE_EMPTY)
1085 writel(SIX_WORD_MSG_SIZE | SGL_OFFSET_4, &msg->u.head[0]);
1086 writel(I2O_CMD_HRT_GET << 24 | HOST_TID << 12 | ADAPTER_TID,
1088 writel(0xd0000000 | c->hrt.len, &msg->body[0]);
1089 writel(c->hrt.phys, &msg->body[1]);
1091 rc = i2o_msg_post_wait_mem(c, m, 20, &c->hrt);
1094 printk(KERN_ERR "%s: Unable to get HRT (status=%#x)\n",
1099 size = hrt->num_entries * hrt->entry_len << 2;
1100 if (size > c->hrt.len) {
1101 if (i2o_dma_realloc(dev, &c->hrt, size, GFP_KERNEL))
1106 return i2o_parse_hrt(c);
1109 printk(KERN_ERR "%s: Unable to get HRT after %d tries, giving up\n",
1110 c->name, I2O_HRT_GET_TRIES);
1116 * i2o_iop_free - Free the i2o_controller struct
1117 * @c: I2O controller to free
1119 void i2o_iop_free(struct i2o_controller *c)
1126 * i2o_iop_release - release the memory for a I2O controller
1127 * @dev: I2O controller which should be released
1129 * Release the allocated memory. This function is called if refcount of
1130 * device reaches 0 automatically.
1132 static void i2o_iop_release(struct device *dev)
1134 struct i2o_controller *c = to_i2o_controller(dev);
1140 * i2o_iop_alloc - Allocate and initialize a i2o_controller struct
1142 * Allocate the necessary memory for a i2o_controller struct and
1143 * initialize the lists.
1145 * Returns a pointer to the I2O controller or a negative error code on
1148 struct i2o_controller *i2o_iop_alloc(void)
1150 static int unit = 0; /* 0 and 1 are NULL IOP and Local Host */
1151 struct i2o_controller *c;
1153 c = kmalloc(sizeof(*c), GFP_KERNEL);
1155 printk(KERN_ERR "i2o: Insufficient memory to allocate a I2O "
1157 return ERR_PTR(-ENOMEM);
1159 memset(c, 0, sizeof(*c));
1161 INIT_LIST_HEAD(&c->devices);
1162 spin_lock_init(&c->lock);
1163 init_MUTEX(&c->lct_lock);
1165 sprintf(c->name, "iop%d", c->unit);
1167 device_initialize(&c->device);
1168 c->device.release = &i2o_iop_release;
1169 snprintf(c->device.bus_id, BUS_ID_SIZE, "iop%d", c->unit);
1171 #if BITS_PER_LONG == 64
1172 spin_lock_init(&c->context_list_lock);
1173 atomic_set(&c->context_list_counter, 0);
1174 INIT_LIST_HEAD(&c->context_list);
1181 * i2o_iop_add - Initialize the I2O controller and add him to the I2O core
1184 * Initialize the I2O controller and if no error occurs add him to the I2O
1187 * Returns 0 on success or negative error code on failure.
1189 int i2o_iop_add(struct i2o_controller *c)
1193 if((rc = device_add(&c->device))) {
1194 printk(KERN_ERR "%s: could not register controller\n", c->name);
1198 printk(KERN_INFO "%s: Activating I2O controller...\n", c->name);
1199 printk(KERN_INFO "%s: This may take a few minutes if there are many "
1200 "devices\n", c->name);
1202 if ((rc = i2o_iop_activate(c))) {
1203 printk(KERN_ERR "%s: could not activate controller\n",
1208 pr_debug("%s: building sys table...\n", c->name);
1210 if ((rc = i2o_systab_build()))
1213 pr_debug("%s: online controller...\n", c->name);
1215 if ((rc = i2o_iop_online(c)))
1218 pr_debug("%s: getting LCT...\n", c->name);
1220 if ((rc = i2o_exec_lct_get(c)))
1223 list_add(&c->list, &i2o_controllers);
1225 i2o_driver_notify_controller_add_all(c);
1227 printk(KERN_INFO "%s: Controller added\n", c->name);
1238 * i2o_event_register - Turn on/off event notification for a I2O device
1239 * @dev: I2O device which should receive the event registration request
1240 * @drv: driver which want to get notified
1241 * @tcntxt: transaction context to use with this notifier
1242 * @evt_mask: mask of events
1244 * Create and posts an event registration message to the task. No reply
1245 * is waited for, or expected. If you do not want further notifications,
1246 * call the i2o_event_register again with a evt_mask of 0.
1248 * Returns 0 on success or -ETIMEDOUT if no message could be fetched for
1249 * sending the request.
1251 int i2o_event_register(struct i2o_device *dev, struct i2o_driver *drv,
1252 int tcntxt, u32 evt_mask)
1254 struct i2o_controller *c = dev->iop;
1255 struct i2o_message __iomem *msg;
1258 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
1259 if (m == I2O_QUEUE_EMPTY)
1262 writel(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
1263 writel(I2O_CMD_UTIL_EVT_REGISTER << 24 | HOST_TID << 12 | dev->lct_data.
1264 tid, &msg->u.head[1]);
1265 writel(drv->context, &msg->u.s.icntxt);
1266 writel(tcntxt, &msg->u.s.tcntxt);
1267 writel(evt_mask, &msg->body[0]);
1275 * i2o_iop_init - I2O main initialization function
1277 * Initialize the I2O drivers (OSM) functions, register the Executive OSM,
1278 * initialize the I2O PCI part and finally initialize I2O device stuff.
1280 * Returns 0 on success or negative error code on failure.
1282 static int __init i2o_iop_init(void)
1286 printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
1288 rc = i2o_device_init();
1292 rc = i2o_driver_init();
1296 rc = i2o_exec_init();
1300 rc = i2o_pci_init();
1320 * i2o_iop_exit - I2O main exit function
1322 * Removes I2O controllers from PCI subsystem and shut down OSMs.
1324 static void __exit i2o_iop_exit(void)
1332 module_init(i2o_iop_init);
1333 module_exit(i2o_iop_exit);
1335 MODULE_AUTHOR("Red Hat Software");
1336 MODULE_LICENSE("GPL");
1337 MODULE_DESCRIPTION(OSM_DESCRIPTION);
1338 MODULE_VERSION(OSM_VERSION);
1340 #if BITS_PER_LONG == 64
1341 EXPORT_SYMBOL(i2o_cntxt_list_add);
1342 EXPORT_SYMBOL(i2o_cntxt_list_get);
1343 EXPORT_SYMBOL(i2o_cntxt_list_remove);
1344 EXPORT_SYMBOL(i2o_cntxt_list_get_ptr);
1346 EXPORT_SYMBOL(i2o_msg_get_wait);
1347 EXPORT_SYMBOL(i2o_msg_nop);
1348 EXPORT_SYMBOL(i2o_find_iop);
1349 EXPORT_SYMBOL(i2o_iop_find_device);
1350 EXPORT_SYMBOL(i2o_event_register);
1351 EXPORT_SYMBOL(i2o_status_get);
1352 EXPORT_SYMBOL(i2o_controllers);