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 /* global I2O controller list */
33 LIST_HEAD(i2o_controllers);
36 * global I2O System Table. Contains information about all the IOPs in the
37 * system. Used to inform IOPs about each others existence.
39 static struct i2o_dma i2o_systab;
41 static int i2o_hrt_get(struct i2o_controller *c);
43 /* Module internal functions from other sources */
44 extern struct i2o_driver i2o_exec_driver;
45 extern int i2o_exec_lct_get(struct i2o_controller *);
46 extern void i2o_device_remove(struct i2o_device *);
48 extern int __init i2o_driver_init(void);
49 extern void __exit i2o_driver_exit(void);
50 extern int __init i2o_exec_init(void);
51 extern void __exit i2o_exec_exit(void);
52 extern int __init i2o_pci_init(void);
53 extern void __exit i2o_pci_exit(void);
54 extern int i2o_device_init(void);
55 extern void i2o_device_exit(void);
58 * i2o_msg_nop - Returns a message which is not used
59 * @c: I2O controller from which the message was created
60 * @m: message which should be returned
62 * If you fetch a message via i2o_msg_get, and can't use it, you must
63 * return the message with this function. Otherwise the message frame
66 void i2o_msg_nop(struct i2o_controller *c, u32 m)
68 struct i2o_message __iomem *msg = c->in_queue.virt + m;
70 writel(THREE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
71 writel(I2O_CMD_UTIL_NOP << 24 | HOST_TID << 12 | ADAPTER_TID,
73 writel(0, &msg->u.head[2]);
74 writel(0, &msg->u.head[3]);
79 * i2o_msg_get_wait - obtain an I2O message from the IOP
81 * @msg: pointer to a I2O message pointer
82 * @wait: how long to wait until timeout
84 * This function waits up to wait seconds for a message slot to be
87 * On a success the message is returned and the pointer to the message is
88 * set in msg. The returned message is the physical page frame offset
89 * address from the read port (see the i2o spec). If no message is
90 * available returns I2O_QUEUE_EMPTY and msg is leaved untouched.
92 u32 i2o_msg_get_wait(struct i2o_controller *c, struct i2o_message __iomem **msg,
95 unsigned long timeout = jiffies + wait * HZ;
98 while ((m = i2o_msg_get(c, msg)) == I2O_QUEUE_EMPTY) {
99 if (time_after(jiffies, timeout)) {
100 pr_debug("%s: Timeout waiting for message frame.\n",
102 return I2O_QUEUE_EMPTY;
104 set_current_state(TASK_UNINTERRUPTIBLE);
111 #if BITS_PER_LONG == 64
113 * i2o_cntxt_list_add - Append a pointer to context list and return a id
114 * @c: controller to which the context list belong
115 * @ptr: pointer to add to the context list
117 * Because the context field in I2O is only 32-bit large, on 64-bit the
118 * pointer is to large to fit in the context field. The i2o_cntxt_list
119 * functions therefore map pointers to context fields.
121 * Returns context id > 0 on success or 0 on failure.
123 u32 i2o_cntxt_list_add(struct i2o_controller * c, void *ptr)
125 struct i2o_context_list_element *entry;
129 printk(KERN_ERR "NULL pointer found!\n");
131 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
133 printk(KERN_ERR "i2o: Could not allocate memory for context "
139 entry->timestamp = jiffies;
140 INIT_LIST_HEAD(&entry->list);
142 spin_lock_irqsave(&c->context_list_lock, flags);
144 if (unlikely(atomic_inc_and_test(&c->context_list_counter)))
145 atomic_inc(&c->context_list_counter);
147 entry->context = atomic_read(&c->context_list_counter);
149 list_add(&entry->list, &c->context_list);
151 spin_unlock_irqrestore(&c->context_list_lock, flags);
153 pr_debug("Add context to list %p -> %d\n", ptr, context);
155 return entry->context;
159 * i2o_cntxt_list_remove - Remove a pointer from the context list
160 * @c: controller to which the context list belong
161 * @ptr: pointer which should be removed from the context list
163 * Removes a previously added pointer from the context list and returns
164 * the matching context id.
166 * Returns context id on succes or 0 on failure.
168 u32 i2o_cntxt_list_remove(struct i2o_controller * c, void *ptr)
170 struct i2o_context_list_element *entry;
174 spin_lock_irqsave(&c->context_list_lock, flags);
175 list_for_each_entry(entry, &c->context_list, list)
176 if (entry->ptr == ptr) {
177 list_del(&entry->list);
178 context = entry->context;
182 spin_unlock_irqrestore(&c->context_list_lock, flags);
185 printk(KERN_WARNING "i2o: Could not remove nonexistent ptr "
188 pr_debug("remove ptr from context list %d -> %p\n", context, ptr);
194 * i2o_cntxt_list_get - Get a pointer from the context list and remove it
195 * @c: controller to which the context list belong
196 * @context: context id to which the pointer belong
198 * Returns pointer to the matching context id on success or NULL on
201 void *i2o_cntxt_list_get(struct i2o_controller *c, u32 context)
203 struct i2o_context_list_element *entry;
207 spin_lock_irqsave(&c->context_list_lock, flags);
208 list_for_each_entry(entry, &c->context_list, list)
209 if (entry->context == context) {
210 list_del(&entry->list);
215 spin_unlock_irqrestore(&c->context_list_lock, flags);
218 printk(KERN_WARNING "i2o: context id %d not found\n", context);
220 pr_debug("get ptr from context list %d -> %p\n", context, ptr);
226 * i2o_cntxt_list_get_ptr - Get a context id from the context list
227 * @c: controller to which the context list belong
228 * @ptr: pointer to which the context id should be fetched
230 * Returns context id which matches to the pointer on succes or 0 on
233 u32 i2o_cntxt_list_get_ptr(struct i2o_controller * c, void *ptr)
235 struct i2o_context_list_element *entry;
239 spin_lock_irqsave(&c->context_list_lock, flags);
240 list_for_each_entry(entry, &c->context_list, list)
241 if (entry->ptr == ptr) {
242 context = entry->context;
245 spin_unlock_irqrestore(&c->context_list_lock, flags);
248 printk(KERN_WARNING "i2o: Could not find nonexistent ptr "
251 pr_debug("get context id from context list %p -> %d\n", ptr, context);
258 * i2o_iop_find - Find an I2O controller by id
259 * @unit: unit number of the I2O controller to search for
261 * Lookup the I2O controller on the controller list.
263 * Returns pointer to the I2O controller on success or NULL if not found.
265 struct i2o_controller *i2o_find_iop(int unit)
267 struct i2o_controller *c;
269 list_for_each_entry(c, &i2o_controllers, list) {
278 * i2o_iop_find_device - Find a I2O device on an I2O controller
279 * @c: I2O controller where the I2O device hangs on
280 * @tid: TID of the I2O device to search for
282 * Searches the devices of the I2O controller for a device with TID tid and
285 * Returns a pointer to the I2O device if found, otherwise NULL.
287 struct i2o_device *i2o_iop_find_device(struct i2o_controller *c, u16 tid)
289 struct i2o_device *dev;
291 list_for_each_entry(dev, &c->devices, list)
292 if (dev->lct_data.tid == tid)
299 * i2o_quiesce_controller - quiesce controller
302 * Quiesce an IOP. Causes IOP to make external operation quiescent
303 * (i2o 'READY' state). Internal operation of the IOP continues normally.
305 * Returns 0 on success or negative error code on failure.
307 static int i2o_iop_quiesce(struct i2o_controller *c)
309 struct i2o_message __iomem *msg;
311 i2o_status_block *sb = c->status_block.virt;
316 /* SysQuiesce discarded if IOP not in READY or OPERATIONAL state */
317 if ((sb->iop_state != ADAPTER_STATE_READY) &&
318 (sb->iop_state != ADAPTER_STATE_OPERATIONAL))
321 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
322 if (m == I2O_QUEUE_EMPTY)
325 writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
326 writel(I2O_CMD_SYS_QUIESCE << 24 | HOST_TID << 12 | ADAPTER_TID,
329 /* Long timeout needed for quiesce if lots of devices */
330 if ((rc = i2o_msg_post_wait(c, m, 240)))
331 printk(KERN_INFO "%s: Unable to quiesce (status=%#x).\n",
334 pr_debug("%s: Quiesced.\n", c->name);
336 i2o_status_get(c); // Entered READY state
342 * i2o_iop_enable - move controller from ready to OPERATIONAL
345 * Enable IOP. This allows the IOP to resume external operations and
346 * reverses the effect of a quiesce. Returns zero or an error code if
349 static int i2o_iop_enable(struct i2o_controller *c)
351 struct i2o_message __iomem *msg;
353 i2o_status_block *sb = c->status_block.virt;
358 /* Enable only allowed on READY state */
359 if (sb->iop_state != ADAPTER_STATE_READY)
362 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
363 if (m == I2O_QUEUE_EMPTY)
366 writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
367 writel(I2O_CMD_SYS_ENABLE << 24 | HOST_TID << 12 | ADAPTER_TID,
370 /* How long of a timeout do we need? */
371 if ((rc = i2o_msg_post_wait(c, m, 240)))
372 printk(KERN_ERR "%s: Could not enable (status=%#x).\n",
375 pr_debug("%s: Enabled.\n", c->name);
377 i2o_status_get(c); // entered OPERATIONAL state
383 * i2o_iop_quiesce_all - Quiesce all I2O controllers on the system
385 * Quiesce all I2O controllers which are connected to the system.
387 static inline void i2o_iop_quiesce_all(void)
389 struct i2o_controller *c, *tmp;
391 list_for_each_entry_safe(c, tmp, &i2o_controllers, list) {
398 * i2o_iop_enable_all - Enables all controllers on the system
400 * Enables all I2O controllers which are connected to the system.
402 static inline void i2o_iop_enable_all(void)
404 struct i2o_controller *c, *tmp;
406 list_for_each_entry_safe(c, tmp, &i2o_controllers, list)
411 * i2o_clear_controller - Bring I2O controller into HOLD state
414 * Clear an IOP to HOLD state, ie. terminate external operations, clear all
415 * input queues and prepare for a system restart. IOP's internal operation
416 * continues normally and the outbound queue is alive. The IOP is not
417 * expected to rebuild its LCT.
419 * Returns 0 on success or negative error code on failure.
421 static int i2o_iop_clear(struct i2o_controller *c)
423 struct i2o_message __iomem *msg;
427 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
428 if (m == I2O_QUEUE_EMPTY)
431 /* Quiesce all IOPs first */
432 i2o_iop_quiesce_all();
434 writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
435 writel(I2O_CMD_ADAPTER_CLEAR << 24 | HOST_TID << 12 | ADAPTER_TID,
438 if ((rc = i2o_msg_post_wait(c, m, 30)))
439 printk(KERN_INFO "%s: Unable to clear (status=%#x).\n",
442 pr_debug("%s: Cleared.\n", c->name);
444 /* Enable all IOPs */
445 i2o_iop_enable_all();
453 * i2o_iop_reset - reset an I2O controller
454 * @c: controller to reset
456 * Reset the IOP into INIT state and wait until IOP gets into RESET state.
457 * Terminate all external operations, clear IOP's inbound and outbound
458 * queues, terminate all DDMs, and reload the IOP's operating environment
459 * and all local DDMs. The IOP rebuilds its LCT.
461 static int i2o_iop_reset(struct i2o_controller *c)
463 u8 *status = c->status.virt;
464 struct i2o_message __iomem *msg;
466 unsigned long timeout;
467 i2o_status_block *sb = c->status_block.virt;
470 pr_debug("Resetting controller\n");
472 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
473 if (m == I2O_QUEUE_EMPTY)
476 memset(status, 0, 8);
478 /* Quiesce all IOPs first */
479 i2o_iop_quiesce_all();
481 writel(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
482 writel(I2O_CMD_ADAPTER_RESET << 24 | HOST_TID << 12 | ADAPTER_TID,
484 writel(i2o_exec_driver.context, &msg->u.s.icntxt);
485 writel(0, &msg->u.s.tcntxt); //FIXME: use reasonable transaction context
486 writel(0, &msg->body[0]);
487 writel(0, &msg->body[1]);
488 writel(i2o_ptr_low((void *)c->status.phys), &msg->body[2]);
489 writel(i2o_ptr_high((void *)c->status.phys), &msg->body[3]);
493 /* Wait for a reply */
494 timeout = jiffies + I2O_TIMEOUT_RESET * HZ;
496 if (time_after(jiffies, timeout)) {
497 printk(KERN_ERR "IOP reset timeout.\n");
503 if (status[1] || status[4]) {
508 set_current_state(TASK_UNINTERRUPTIBLE);
514 if (*status == I2O_CMD_IN_PROGRESS) {
516 * Once the reset is sent, the IOP goes into the INIT state
517 * which is indeterminate. We need to wait until the IOP
518 * has rebooted before we can let the system talk to
519 * it. We read the inbound Free_List until a message is
520 * available. If we can't read one in the given ammount of
521 * time, we assume the IOP could not reboot properly.
523 pr_debug("%s: Reset in progress, waiting for reboot...\n",
526 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_RESET);
527 while (m == I2O_QUEUE_EMPTY) {
528 if (time_after(jiffies, timeout)) {
529 printk(KERN_ERR "IOP reset timeout.\n");
533 set_current_state(TASK_UNINTERRUPTIBLE);
536 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_RESET);
541 /* from here all quiesce commands are safe */
544 /* If IopReset was rejected or didn't perform reset, try IopClear */
546 if (*status == I2O_CMD_REJECTED || sb->iop_state != ADAPTER_STATE_RESET) {
547 printk(KERN_WARNING "%s: Reset rejected, trying to clear\n",
551 pr_debug("%s: Reset completed.\n", c->name);
554 /* Enable all IOPs */
555 i2o_iop_enable_all();
561 * i2o_iop_init_outbound_queue - setup the outbound message queue
564 * Clear and (re)initialize IOP's outbound queue and post the message
567 * Returns 0 on success or a negative errno code on failure.
569 static int i2o_iop_init_outbound_queue(struct i2o_controller *c)
571 u8 *status = c->status.virt;
573 struct i2o_message __iomem *msg;
577 pr_debug("%s: Initializing Outbound Queue...\n", c->name);
579 memset(status, 0, 4);
581 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
582 if (m == I2O_QUEUE_EMPTY)
585 writel(EIGHT_WORD_MSG_SIZE | TRL_OFFSET_6, &msg->u.head[0]);
586 writel(I2O_CMD_OUTBOUND_INIT << 24 | HOST_TID << 12 | ADAPTER_TID,
588 writel(i2o_exec_driver.context, &msg->u.s.icntxt);
589 writel(0x0106, &msg->u.s.tcntxt); /* FIXME: why 0x0106, maybe in
591 writel(PAGE_SIZE, &msg->body[0]);
592 writel(MSG_FRAME_SIZE << 16 | 0x80, &msg->body[1]); /* Outbound msg frame
593 size in words and Initcode */
594 writel(0xd0000004, &msg->body[2]);
595 writel(i2o_ptr_low((void *)c->status.phys), &msg->body[3]);
596 writel(i2o_ptr_high((void *)c->status.phys), &msg->body[4]);
600 timeout = jiffies + I2O_TIMEOUT_INIT_OUTBOUND_QUEUE * HZ;
601 while (*status <= I2O_CMD_IN_PROGRESS) {
602 if (time_after(jiffies, timeout)) {
603 printk(KERN_WARNING "%s: Timeout Initializing\n",
607 set_current_state(TASK_UNINTERRUPTIBLE);
613 m = c->out_queue.phys;
616 for (i = 0; i < NMBR_MSG_FRAMES; i++) {
617 i2o_flush_reply(c, m);
618 udelay(1); /* Promise */
619 m += MSG_FRAME_SIZE * 4;
626 * i2o_iop_send_nop - send a core NOP message
629 * Send a no-operation message with a reply set to cause no
630 * action either. Needed for bringing up promise controllers.
632 static int i2o_iop_send_nop(struct i2o_controller *c)
634 struct i2o_message __iomem *msg;
635 u32 m = i2o_msg_get_wait(c, &msg, HZ);
636 if (m == I2O_QUEUE_EMPTY)
643 * i2o_iop_activate - Bring controller up to HOLD
646 * This function brings an I2O controller into HOLD state. The adapter
647 * is reset if necessary and then the queues and resource table are read.
649 * Returns 0 on success or negative error code on failure.
651 static int i2o_iop_activate(struct i2o_controller *c)
653 struct pci_dev *i960 = NULL;
654 i2o_status_block *sb = c->status_block.virt;
658 /* Beat up the hardware first of all */
660 pci_find_slot(c->pdev->bus->number,
661 PCI_DEVFN(PCI_SLOT(c->pdev->devfn), 0));
663 pci_write_config_word(i960, 0x42, 0);
665 /* Follow this sequence precisely or the controller
666 ceases to perform useful functions until reboot */
667 if ((rc = i2o_iop_send_nop(c)))
670 if ((rc = i2o_iop_reset(c)))
674 /* In INIT state, Wait Inbound Q to initialize (in i2o_status_get) */
675 /* In READY state, Get status */
677 rc = i2o_status_get(c);
679 printk(KERN_INFO "Unable to obtain status of %s, "
680 "attempting a reset.\n", c->name);
681 if (i2o_iop_reset(c))
685 if (sb->i2o_version > I2OVER15) {
686 printk(KERN_ERR "%s: Not running vrs. 1.5. of the I2O "
687 "Specification.\n", c->name);
691 switch (sb->iop_state) {
692 case ADAPTER_STATE_FAULTED:
693 printk(KERN_CRIT "%s: hardware fault\n", c->name);
696 case ADAPTER_STATE_READY:
697 case ADAPTER_STATE_OPERATIONAL:
698 case ADAPTER_STATE_HOLD:
699 case ADAPTER_STATE_FAILED:
700 pr_debug("already running, trying to reset...\n");
701 if (i2o_iop_reset(c))
705 rc = i2o_iop_init_outbound_queue(c);
710 if ((rc = i2o_iop_send_nop(c)))
713 if ((rc = i2o_status_get(c)))
717 pci_write_config_word(i960, 0x42, 0x3FF);
728 * i2o_iop_systab_set - Set the I2O System Table of the specified IOP
729 * @c: I2O controller to which the system table should be send
731 * Before the systab could be set i2o_systab_build() must be called.
733 * Returns 0 on success or negative error code on failure.
735 static int i2o_iop_systab_set(struct i2o_controller *c)
737 struct i2o_message __iomem *msg;
739 i2o_status_block *sb = c->status_block.virt;
740 struct device *dev = &c->pdev->dev;
741 struct resource *root;
744 if (sb->current_mem_size < sb->desired_mem_size) {
745 struct resource *res = &c->mem_resource;
746 res->name = c->pdev->bus->name;
747 res->flags = IORESOURCE_MEM;
750 printk(KERN_INFO "%s: requires private memory resources.\n",
752 root = pci_find_parent_resource(c->pdev, res);
754 printk(KERN_WARNING "Can't find parent resource!\n");
755 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 */
758 sb->current_mem_size = 1 + res->end - res->start;
759 sb->current_mem_base = res->start;
761 "%s: allocated %ld bytes of PCI memory at 0x%08lX.\n",
762 c->name, 1 + res->end - res->start, res->start);
766 if (sb->current_io_size < sb->desired_io_size) {
767 struct resource *res = &c->io_resource;
768 res->name = c->pdev->bus->name;
769 res->flags = IORESOURCE_IO;
772 printk(KERN_INFO "%s: requires private memory resources.\n",
774 root = pci_find_parent_resource(c->pdev, res);
776 printk(KERN_WARNING "Can't find parent resource!\n");
777 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 */
780 sb->current_io_size = 1 + res->end - res->start;
781 sb->current_mem_base = res->start;
783 "%s: allocated %ld bytes of PCI I/O at 0x%08lX.\n",
784 c->name, 1 + res->end - res->start, res->start);
788 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
789 if (m == I2O_QUEUE_EMPTY)
792 i2o_systab.phys = dma_map_single(dev, i2o_systab.virt, i2o_systab.len,
794 if (!i2o_systab.phys) {
799 writel(I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6, &msg->u.head[0]);
800 writel(I2O_CMD_SYS_TAB_SET << 24 | HOST_TID << 12 | ADAPTER_TID,
804 * Provide three SGL-elements:
805 * System table (SysTab), Private memory space declaration and
806 * Private i/o space declaration
808 * FIXME: is this still true?
809 * Nasty one here. We can't use dma_alloc_coherent to send the
810 * same table to everyone. We have to go remap it for them all
813 writel(c->unit + 2, &msg->body[0]);
814 writel(0, &msg->body[1]);
815 writel(0x54000000 | i2o_systab.len, &msg->body[2]);
816 writel(i2o_systab.phys, &msg->body[3]);
817 writel(0x54000000 | sb->current_mem_size, &msg->body[4]);
818 writel(sb->current_mem_base, &msg->body[5]);
819 writel(0xd4000000 | sb->current_io_size, &msg->body[6]);
820 writel(sb->current_io_base, &msg->body[6]);
822 rc = i2o_msg_post_wait(c, m, 120);
824 dma_unmap_single(dev, i2o_systab.phys, i2o_systab.len,
828 printk(KERN_ERR "%s: Unable to set SysTab (status=%#x).\n",
831 pr_debug("%s: SysTab set.\n", c->name);
833 i2o_status_get(c); // Entered READY state
839 * i2o_iop_online - Bring a controller online into OPERATIONAL state.
842 * Send the system table and enable the I2O controller.
844 * Returns 0 on success or negativer error code on failure.
846 static int i2o_iop_online(struct i2o_controller *c)
850 rc = i2o_iop_systab_set(c);
855 pr_debug("%s: Attempting to enable...\n", c->name);
856 rc = i2o_iop_enable(c);
864 * i2o_iop_remove - Remove the I2O controller from the I2O core
867 * Remove the I2O controller from the I2O core. If devices are attached to
868 * the controller remove these also and finally reset the controller.
870 void i2o_iop_remove(struct i2o_controller *c)
872 struct i2o_device *dev, *tmp;
874 pr_debug("Deleting controller %s\n", c->name);
876 i2o_driver_notify_controller_remove_all(c);
880 list_for_each_entry_safe(dev, tmp, &c->devices, list)
881 i2o_device_remove(dev);
883 /* Ask the IOP to switch to RESET state */
888 * i2o_systab_build - Build system table
890 * The system table contains information about all the IOPs in the system
891 * (duh) and is used by the Executives on the IOPs to establish peer2peer
892 * connections. We're not supporting peer2peer at the moment, but this
893 * will be needed down the road for things like lan2lan forwarding.
895 * Returns 0 on success or negative error code on failure.
897 static int i2o_systab_build(void)
899 struct i2o_controller *c, *tmp;
900 int num_controllers = 0;
903 struct i2o_sys_tbl *systab = i2o_systab.virt;
905 list_for_each_entry_safe(c, tmp, &i2o_controllers, list)
909 change_ind = systab->change_ind;
910 kfree(i2o_systab.virt);
914 i2o_systab.len = sizeof(struct i2o_sys_tbl) + num_controllers *
915 sizeof(struct i2o_sys_tbl_entry);
917 systab = i2o_systab.virt = kmalloc(i2o_systab.len, GFP_KERNEL);
919 printk(KERN_ERR "i2o: unable to allocate memory for System "
923 memset(systab, 0, i2o_systab.len);
925 systab->version = I2OVERSION;
926 systab->change_ind = change_ind + 1;
928 list_for_each_entry_safe(c, tmp, &i2o_controllers, list) {
929 i2o_status_block *sb;
931 if (count >= num_controllers) {
932 printk(KERN_ERR "i2o: controller added while building "
937 sb = c->status_block.virt;
940 * Get updated IOP state so we have the latest information
942 * We should delete the controller at this point if it
943 * doesn't respond since if it's not on the system table
944 * it is techninically not part of the I2O subsystem...
946 if (unlikely(i2o_status_get(c))) {
947 printk(KERN_ERR "%s: Deleting b/c could not get status"
948 " while attempting to build system table\n",
951 continue; // try the next one
954 systab->iops[count].org_id = sb->org_id;
955 systab->iops[count].iop_id = c->unit + 2;
956 systab->iops[count].seg_num = 0;
957 systab->iops[count].i2o_version = sb->i2o_version;
958 systab->iops[count].iop_state = sb->iop_state;
959 systab->iops[count].msg_type = sb->msg_type;
960 systab->iops[count].frame_size = sb->inbound_frame_size;
961 systab->iops[count].last_changed = change_ind;
962 systab->iops[count].iop_capabilities = sb->iop_capabilities;
963 systab->iops[count].inbound_low = i2o_ptr_low(c->post_port);
964 systab->iops[count].inbound_high = i2o_ptr_high(c->post_port);
969 systab->num_entries = count;
975 * i2o_parse_hrt - Parse the hardware resource table.
978 * We don't do anything with it except dumping it (in debug mode).
982 static int i2o_parse_hrt(struct i2o_controller *c)
989 * i2o_status_get - Get the status block from the I2O controller
992 * Issue a status query on the controller. This updates the attached
993 * status block. The status block could then be accessed through
996 * Returns 0 on sucess or negative error code on failure.
998 int i2o_status_get(struct i2o_controller *c)
1000 struct i2o_message __iomem *msg;
1003 unsigned long timeout;
1005 status_block = (u8 *) c->status_block.virt;
1006 memset(status_block, 0, sizeof(i2o_status_block));
1008 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
1009 if (m == I2O_QUEUE_EMPTY)
1012 writel(NINE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
1013 writel(I2O_CMD_STATUS_GET << 24 | HOST_TID << 12 | ADAPTER_TID,
1015 writel(i2o_exec_driver.context, &msg->u.s.icntxt);
1016 writel(0, &msg->u.s.tcntxt); // FIXME: use resonable transaction context
1017 writel(0, &msg->body[0]);
1018 writel(0, &msg->body[1]);
1019 writel(i2o_ptr_low((void *)c->status_block.phys), &msg->body[2]);
1020 writel(i2o_ptr_high((void *)c->status_block.phys), &msg->body[3]);
1021 writel(sizeof(i2o_status_block), &msg->body[4]); /* always 88 bytes */
1025 /* Wait for a reply */
1026 timeout = jiffies + I2O_TIMEOUT_STATUS_GET * HZ;
1027 while (status_block[87] != 0xFF) {
1028 if (time_after(jiffies, timeout)) {
1029 printk(KERN_ERR "%s: Get status timeout.\n", c->name);
1033 set_current_state(TASK_UNINTERRUPTIBLE);
1034 schedule_timeout(1);
1047 * i2o_hrt_get - Get the Hardware Resource Table from the I2O controller
1048 * @c: I2O controller from which the HRT should be fetched
1050 * The HRT contains information about possible hidden devices but is
1051 * mostly useless to us.
1053 * Returns 0 on success or negativer error code on failure.
1055 static int i2o_hrt_get(struct i2o_controller *c)
1059 i2o_hrt *hrt = c->hrt.virt;
1060 u32 size = sizeof(i2o_hrt);
1061 struct device *dev = &c->pdev->dev;
1063 for (i = 0; i < I2O_HRT_GET_TRIES; i++) {
1064 struct i2o_message __iomem *msg;
1067 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
1068 if (m == I2O_QUEUE_EMPTY)
1071 writel(SIX_WORD_MSG_SIZE | SGL_OFFSET_4, &msg->u.head[0]);
1072 writel(I2O_CMD_HRT_GET << 24 | HOST_TID << 12 | ADAPTER_TID,
1074 writel(0xd0000000 | c->hrt.len, &msg->body[0]);
1075 writel(c->hrt.phys, &msg->body[1]);
1077 rc = i2o_msg_post_wait_mem(c, m, 20, &c->hrt);
1080 printk(KERN_ERR "%s: Unable to get HRT (status=%#x)\n",
1085 size = hrt->num_entries * hrt->entry_len << 2;
1086 if (size > c->hrt.len) {
1087 if (i2o_dma_realloc(dev, &c->hrt, size, GFP_KERNEL))
1092 return i2o_parse_hrt(c);
1095 printk(KERN_ERR "%s: Unable to get HRT after %d tries, giving up\n",
1096 c->name, I2O_HRT_GET_TRIES);
1102 * i2o_iop_alloc - Allocate and initialize a i2o_controller struct
1104 * Allocate the necessary memory for a i2o_controller struct and
1105 * initialize the lists.
1107 * Returns a pointer to the I2O controller or a negative error code on
1110 struct i2o_controller *i2o_iop_alloc(void)
1112 static int unit = 0; /* 0 and 1 are NULL IOP and Local Host */
1113 struct i2o_controller *c;
1115 c = kmalloc(sizeof(*c), GFP_KERNEL);
1117 printk(KERN_ERR "i2o: Insufficient memory to allocate the "
1119 return ERR_PTR(-ENOMEM);
1121 memset(c, 0, sizeof(*c));
1123 INIT_LIST_HEAD(&c->devices);
1124 spin_lock_init(&c->lock);
1125 init_MUTEX(&c->lct_lock);
1127 sprintf(c->name, "iop%d", c->unit);
1129 #if BITS_PER_LONG == 64
1130 spin_lock_init(&c->context_list_lock);
1131 atomic_set(&c->context_list_counter, 0);
1132 INIT_LIST_HEAD(&c->context_list);
1139 * i2o_iop_free - Free the i2o_controller struct
1140 * @c: I2O controller to free
1142 void i2o_iop_free(struct i2o_controller *c)
1148 * i2o_iop_add - Initialize the I2O controller and add him to the I2O core
1151 * Initialize the I2O controller and if no error occurs add him to the I2O
1154 * Returns 0 on success or negative error code on failure.
1156 int i2o_iop_add(struct i2o_controller *c)
1160 printk(KERN_INFO "%s: Activating I2O controller...\n", c->name);
1161 printk(KERN_INFO "%s: This may take a few minutes if there are many "
1162 "devices\n", c->name);
1164 if ((rc = i2o_iop_activate(c))) {
1165 printk(KERN_ERR "%s: controller could not activated\n",
1171 pr_debug("building sys table %s...\n", c->name);
1173 if ((rc = i2o_systab_build())) {
1178 pr_debug("online controller %s...\n", c->name);
1180 if ((rc = i2o_iop_online(c))) {
1185 pr_debug("getting LCT %s...\n", c->name);
1187 if ((rc = i2o_exec_lct_get(c))) {
1192 list_add(&c->list, &i2o_controllers);
1194 i2o_driver_notify_controller_add_all(c);
1196 printk(KERN_INFO "%s: Controller added\n", c->name);
1202 * i2o_event_register - Turn on/off event notification for a I2O device
1203 * @dev: I2O device which should receive the event registration request
1204 * @drv: driver which want to get notified
1205 * @tcntxt: transaction context to use with this notifier
1206 * @evt_mask: mask of events
1208 * Create and posts an event registration message to the task. No reply
1209 * is waited for, or expected. If you do not want further notifications,
1210 * call the i2o_event_register again with a evt_mask of 0.
1212 * Returns 0 on success or -ETIMEDOUT if no message could be fetched for
1213 * sending the request.
1215 int i2o_event_register(struct i2o_device *dev, struct i2o_driver *drv,
1216 int tcntxt, u32 evt_mask)
1218 struct i2o_controller *c = dev->iop;
1219 struct i2o_message __iomem *msg;
1222 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
1223 if (m == I2O_QUEUE_EMPTY)
1226 writel(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
1227 writel(I2O_CMD_UTIL_EVT_REGISTER << 24 | HOST_TID << 12 | dev->lct_data.
1228 tid, &msg->u.head[1]);
1229 writel(drv->context, &msg->u.s.icntxt);
1230 writel(tcntxt, &msg->u.s.tcntxt);
1231 writel(evt_mask, &msg->body[0]);
1239 * i2o_iop_init - I2O main initialization function
1241 * Initialize the I2O drivers (OSM) functions, register the Executive OSM,
1242 * initialize the I2O PCI part and finally initialize I2O device stuff.
1244 * Returns 0 on success or negative error code on failure.
1246 static int __init i2o_iop_init(void)
1250 printk(KERN_INFO "I2O Core - (C) Copyright 1999 Red Hat Software\n");
1252 rc = i2o_device_init();
1256 rc = i2o_driver_init();
1260 rc = i2o_exec_init();
1264 rc = i2o_pci_init();
1284 * i2o_iop_exit - I2O main exit function
1286 * Removes I2O controllers from PCI subsystem and shut down OSMs.
1288 static void __exit i2o_iop_exit(void)
1296 module_init(i2o_iop_init);
1297 module_exit(i2o_iop_exit);
1299 MODULE_AUTHOR("Red Hat Software");
1300 MODULE_DESCRIPTION("I2O Core");
1301 MODULE_LICENSE("GPL");
1303 #if BITS_PER_LONG == 64
1304 EXPORT_SYMBOL(i2o_cntxt_list_add);
1305 EXPORT_SYMBOL(i2o_cntxt_list_get);
1306 EXPORT_SYMBOL(i2o_cntxt_list_remove);
1307 EXPORT_SYMBOL(i2o_cntxt_list_get_ptr);
1309 EXPORT_SYMBOL(i2o_msg_get_wait);
1310 EXPORT_SYMBOL(i2o_msg_nop);
1311 EXPORT_SYMBOL(i2o_find_iop);
1312 EXPORT_SYMBOL(i2o_iop_find_device);
1313 EXPORT_SYMBOL(i2o_event_register);
1314 EXPORT_SYMBOL(i2o_status_get);
1315 EXPORT_SYMBOL(i2o_controllers);