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>
33 #define OSM_NAME "i2o"
34 #define OSM_VERSION "1.288"
35 #define OSM_DESCRIPTION "I2O subsystem"
37 /* global I2O controller list */
38 LIST_HEAD(i2o_controllers);
41 * global I2O System Table. Contains information about all the IOPs in the
42 * system. Used to inform IOPs about each others existence.
44 static struct i2o_dma i2o_systab;
46 static int i2o_hrt_get(struct i2o_controller *c);
49 * i2o_msg_nop - Returns a message which is not used
50 * @c: I2O controller from which the message was created
51 * @m: message which should be returned
53 * If you fetch a message via i2o_msg_get, and can't use it, you must
54 * return the message with this function. Otherwise the message frame
57 void i2o_msg_nop(struct i2o_controller *c, u32 m)
59 struct i2o_message __iomem *msg = i2o_msg_in_to_virt(c, m);
61 writel(THREE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
62 writel(I2O_CMD_UTIL_NOP << 24 | HOST_TID << 12 | ADAPTER_TID,
64 writel(0, &msg->u.head[2]);
65 writel(0, &msg->u.head[3]);
70 * i2o_msg_get_wait - obtain an I2O message from the IOP
72 * @msg: pointer to a I2O message pointer
73 * @wait: how long to wait until timeout
75 * This function waits up to wait seconds for a message slot to be
78 * On a success the message is returned and the pointer to the message is
79 * set in msg. The returned message is the physical page frame offset
80 * address from the read port (see the i2o spec). If no message is
81 * available returns I2O_QUEUE_EMPTY and msg is leaved untouched.
83 u32 i2o_msg_get_wait(struct i2o_controller *c,
84 struct i2o_message __iomem ** msg, int wait)
86 unsigned long timeout = jiffies + wait * HZ;
89 while ((m = i2o_msg_get(c, msg)) == I2O_QUEUE_EMPTY) {
90 if (time_after(jiffies, timeout)) {
91 osm_debug("%s: Timeout waiting for message frame.\n",
93 return I2O_QUEUE_EMPTY;
95 set_current_state(TASK_UNINTERRUPTIBLE);
102 #if BITS_PER_LONG == 64
104 * i2o_cntxt_list_add - Append a pointer to context list and return a id
105 * @c: controller to which the context list belong
106 * @ptr: pointer to add to the context list
108 * Because the context field in I2O is only 32-bit large, on 64-bit the
109 * pointer is to large to fit in the context field. The i2o_cntxt_list
110 * functions therefore map pointers to context fields.
112 * Returns context id > 0 on success or 0 on failure.
114 u32 i2o_cntxt_list_add(struct i2o_controller * c, void *ptr)
116 struct i2o_context_list_element *entry;
120 printk(KERN_ERR "%s: couldn't add NULL pointer to context list!"
123 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
125 printk(KERN_ERR "%s: Could not allocate memory for context "
126 "list element\n", c->name);
131 entry->timestamp = jiffies;
132 INIT_LIST_HEAD(&entry->list);
134 spin_lock_irqsave(&c->context_list_lock, flags);
136 if (unlikely(atomic_inc_and_test(&c->context_list_counter)))
137 atomic_inc(&c->context_list_counter);
139 entry->context = atomic_read(&c->context_list_counter);
141 list_add(&entry->list, &c->context_list);
143 spin_unlock_irqrestore(&c->context_list_lock, flags);
145 pr_debug("%s: Add context to list %p -> %d\n", c->name, ptr, context);
147 return entry->context;
151 * i2o_cntxt_list_remove - Remove a pointer from the context list
152 * @c: controller to which the context list belong
153 * @ptr: pointer which should be removed from the context list
155 * Removes a previously added pointer from the context list and returns
156 * the matching context id.
158 * Returns context id on succes or 0 on failure.
160 u32 i2o_cntxt_list_remove(struct i2o_controller * c, void *ptr)
162 struct i2o_context_list_element *entry;
166 spin_lock_irqsave(&c->context_list_lock, flags);
167 list_for_each_entry(entry, &c->context_list, list)
168 if (entry->ptr == ptr) {
169 list_del(&entry->list);
170 context = entry->context;
174 spin_unlock_irqrestore(&c->context_list_lock, flags);
177 printk(KERN_WARNING "%s: Could not remove nonexistent ptr "
178 "%p\n", c->name, ptr);
180 pr_debug("%s: remove ptr from context list %d -> %p\n", c->name,
187 * i2o_cntxt_list_get - Get a pointer from the context list and remove it
188 * @c: controller to which the context list belong
189 * @context: context id to which the pointer belong
191 * Returns pointer to the matching context id on success or NULL on
194 void *i2o_cntxt_list_get(struct i2o_controller *c, u32 context)
196 struct i2o_context_list_element *entry;
200 spin_lock_irqsave(&c->context_list_lock, flags);
201 list_for_each_entry(entry, &c->context_list, list)
202 if (entry->context == context) {
203 list_del(&entry->list);
208 spin_unlock_irqrestore(&c->context_list_lock, flags);
211 printk(KERN_WARNING "%s: context id %d not found\n", c->name,
214 pr_debug("%s: get ptr from context list %d -> %p\n", c->name, context,
221 * i2o_cntxt_list_get_ptr - Get a context id from the context list
222 * @c: controller to which the context list belong
223 * @ptr: pointer to which the context id should be fetched
225 * Returns context id which matches to the pointer on succes or 0 on
228 u32 i2o_cntxt_list_get_ptr(struct i2o_controller * c, void *ptr)
230 struct i2o_context_list_element *entry;
234 spin_lock_irqsave(&c->context_list_lock, flags);
235 list_for_each_entry(entry, &c->context_list, list)
236 if (entry->ptr == ptr) {
237 context = entry->context;
240 spin_unlock_irqrestore(&c->context_list_lock, flags);
243 printk(KERN_WARNING "%s: Could not find nonexistent ptr "
244 "%p\n", c->name, ptr);
246 pr_debug("%s: get context id from context list %p -> %d\n", c->name,
254 * i2o_iop_find - Find an I2O controller by id
255 * @unit: unit number of the I2O controller to search for
257 * Lookup the I2O controller on the controller list.
259 * Returns pointer to the I2O controller on success or NULL if not found.
261 struct i2o_controller *i2o_find_iop(int unit)
263 struct i2o_controller *c;
265 list_for_each_entry(c, &i2o_controllers, list) {
274 * i2o_iop_find_device - Find a I2O device on an I2O controller
275 * @c: I2O controller where the I2O device hangs on
276 * @tid: TID of the I2O device to search for
278 * Searches the devices of the I2O controller for a device with TID tid and
281 * Returns a pointer to the I2O device if found, otherwise NULL.
283 struct i2o_device *i2o_iop_find_device(struct i2o_controller *c, u16 tid)
285 struct i2o_device *dev;
287 list_for_each_entry(dev, &c->devices, list)
288 if (dev->lct_data.tid == tid)
295 * i2o_quiesce_controller - quiesce controller
298 * Quiesce an IOP. Causes IOP to make external operation quiescent
299 * (i2o 'READY' state). Internal operation of the IOP continues normally.
301 * Returns 0 on success or negative error code on failure.
303 static int i2o_iop_quiesce(struct i2o_controller *c)
305 struct i2o_message __iomem *msg;
307 i2o_status_block *sb = c->status_block.virt;
312 /* SysQuiesce discarded if IOP not in READY or OPERATIONAL state */
313 if ((sb->iop_state != ADAPTER_STATE_READY) &&
314 (sb->iop_state != ADAPTER_STATE_OPERATIONAL))
317 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
318 if (m == I2O_QUEUE_EMPTY)
321 writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
322 writel(I2O_CMD_SYS_QUIESCE << 24 | HOST_TID << 12 | ADAPTER_TID,
325 /* Long timeout needed for quiesce if lots of devices */
326 if ((rc = i2o_msg_post_wait(c, m, 240)))
327 printk(KERN_INFO "%s: Unable to quiesce (status=%#x).\n",
330 pr_debug("%s: Quiesced.\n", c->name);
332 i2o_status_get(c); // Entered READY state
338 * i2o_iop_enable - move controller from ready to OPERATIONAL
341 * Enable IOP. This allows the IOP to resume external operations and
342 * reverses the effect of a quiesce. Returns zero or an error code if
345 static int i2o_iop_enable(struct i2o_controller *c)
347 struct i2o_message __iomem *msg;
349 i2o_status_block *sb = c->status_block.virt;
354 /* Enable only allowed on READY state */
355 if (sb->iop_state != ADAPTER_STATE_READY)
358 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
359 if (m == I2O_QUEUE_EMPTY)
362 writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
363 writel(I2O_CMD_SYS_ENABLE << 24 | HOST_TID << 12 | ADAPTER_TID,
366 /* How long of a timeout do we need? */
367 if ((rc = i2o_msg_post_wait(c, m, 240)))
368 printk(KERN_ERR "%s: Could not enable (status=%#x).\n",
371 pr_debug("%s: Enabled.\n", c->name);
373 i2o_status_get(c); // entered OPERATIONAL state
379 * i2o_iop_quiesce_all - Quiesce all I2O controllers on the system
381 * Quiesce all I2O controllers which are connected to the system.
383 static inline void i2o_iop_quiesce_all(void)
385 struct i2o_controller *c, *tmp;
387 list_for_each_entry_safe(c, tmp, &i2o_controllers, list) {
394 * i2o_iop_enable_all - Enables all controllers on the system
396 * Enables all I2O controllers which are connected to the system.
398 static inline void i2o_iop_enable_all(void)
400 struct i2o_controller *c, *tmp;
402 list_for_each_entry_safe(c, tmp, &i2o_controllers, list)
407 * i2o_clear_controller - Bring I2O controller into HOLD state
410 * Clear an IOP to HOLD state, ie. terminate external operations, clear all
411 * input queues and prepare for a system restart. IOP's internal operation
412 * continues normally and the outbound queue is alive. The IOP is not
413 * expected to rebuild its LCT.
415 * Returns 0 on success or negative error code on failure.
417 static int i2o_iop_clear(struct i2o_controller *c)
419 struct i2o_message __iomem *msg;
423 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
424 if (m == I2O_QUEUE_EMPTY)
427 /* Quiesce all IOPs first */
428 i2o_iop_quiesce_all();
430 writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
431 writel(I2O_CMD_ADAPTER_CLEAR << 24 | HOST_TID << 12 | ADAPTER_TID,
434 if ((rc = i2o_msg_post_wait(c, m, 30)))
435 printk(KERN_INFO "%s: Unable to clear (status=%#x).\n",
438 pr_debug("%s: Cleared.\n", c->name);
440 /* Enable all IOPs */
441 i2o_iop_enable_all();
447 * i2o_iop_init_outbound_queue - setup the outbound message queue
450 * Clear and (re)initialize IOP's outbound queue and post the message
453 * Returns 0 on success or a negative errno code on failure.
455 static int i2o_iop_init_outbound_queue(struct i2o_controller *c)
457 volatile u8 *status = c->status.virt;
459 struct i2o_message __iomem *msg;
463 osm_debug("%s: Initializing Outbound Queue...\n", c->name);
465 memset(c->status.virt, 0, 4);
467 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
468 if (m == I2O_QUEUE_EMPTY)
471 writel(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_6, &msg->u.head[0]);
472 writel(I2O_CMD_OUTBOUND_INIT << 24 | HOST_TID << 12 | ADAPTER_TID,
474 writel(i2o_exec_driver.context, &msg->u.s.icntxt);
475 writel(0x00000000, &msg->u.s.tcntxt);
476 writel(PAGE_SIZE, &msg->body[0]);
477 /* Outbound msg frame size in words and Initcode */
478 writel(I2O_OUTBOUND_MSG_FRAME_SIZE << 16 | 0x80, &msg->body[1]);
479 writel(0xd0000004, &msg->body[2]);
480 writel(i2o_dma_low(c->status.phys), &msg->body[3]);
481 writel(i2o_dma_high(c->status.phys), &msg->body[4]);
485 timeout = jiffies + I2O_TIMEOUT_INIT_OUTBOUND_QUEUE * HZ;
486 while (*status <= I2O_CMD_IN_PROGRESS) {
487 if (time_after(jiffies, timeout)) {
488 osm_warn("%s: Timeout Initializing\n", c->name);
491 set_current_state(TASK_UNINTERRUPTIBLE);
495 m = c->out_queue.phys;
498 for (i = 0; i < I2O_MAX_OUTBOUND_MSG_FRAMES; i++) {
499 i2o_flush_reply(c, m);
500 udelay(1); /* Promise */
501 m += I2O_OUTBOUND_MSG_FRAME_SIZE * sizeof(u32);
508 * i2o_iop_reset - reset an I2O controller
509 * @c: controller to reset
511 * Reset the IOP into INIT state and wait until IOP gets into RESET state.
512 * Terminate all external operations, clear IOP's inbound and outbound
513 * queues, terminate all DDMs, and reload the IOP's operating environment
514 * and all local DDMs. The IOP rebuilds its LCT.
516 static int i2o_iop_reset(struct i2o_controller *c)
518 volatile u8 *status = c->status.virt;
519 struct i2o_message __iomem *msg;
521 unsigned long timeout;
522 i2o_status_block *sb = c->status_block.virt;
525 osm_debug("%s: Resetting controller\n", c->name);
527 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
528 if (m == I2O_QUEUE_EMPTY)
531 memset(c->status_block.virt, 0, 8);
533 /* Quiesce all IOPs first */
534 i2o_iop_quiesce_all();
536 writel(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
537 writel(I2O_CMD_ADAPTER_RESET << 24 | HOST_TID << 12 | ADAPTER_TID,
539 writel(i2o_exec_driver.context, &msg->u.s.icntxt);
540 writel(0, &msg->u.s.tcntxt); //FIXME: use reasonable transaction context
541 writel(0, &msg->body[0]);
542 writel(0, &msg->body[1]);
543 writel(i2o_dma_low(c->status.phys), &msg->body[2]);
544 writel(i2o_dma_high(c->status.phys), &msg->body[3]);
548 /* Wait for a reply */
549 timeout = jiffies + I2O_TIMEOUT_RESET * HZ;
551 if (time_after(jiffies, timeout))
554 set_current_state(TASK_UNINTERRUPTIBLE);
559 case I2O_CMD_REJECTED:
560 osm_warn("%s: IOP reset rejected\n", c->name);
564 case I2O_CMD_IN_PROGRESS:
566 * Once the reset is sent, the IOP goes into the INIT state
567 * which is indeterminate. We need to wait until the IOP has
568 * rebooted before we can let the system talk to it. We read
569 * the inbound Free_List until a message is available. If we
570 * can't read one in the given ammount of time, we assume the
571 * IOP could not reboot properly.
573 pr_debug("%s: Reset in progress, waiting for reboot...\n",
576 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_RESET);
577 while (m == I2O_QUEUE_EMPTY) {
578 if (time_after(jiffies, timeout)) {
579 printk(KERN_ERR "%s: IOP reset timeout.\n",
584 set_current_state(TASK_UNINTERRUPTIBLE);
587 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_RESET);
591 /* from here all quiesce commands are safe */
594 /* verify if controller is in state RESET */
597 if (!c->promise && (sb->iop_state != ADAPTER_STATE_RESET))
598 osm_warn("%s: reset completed, but adapter not in RESET"
599 " state.\n", c->name);
601 osm_debug("%s: reset completed.\n", c->name);
606 osm_err("%s: IOP reset timeout.\n", c->name);
612 /* Enable all IOPs */
613 i2o_iop_enable_all();
619 * i2o_iop_activate - Bring controller up to HOLD
622 * This function brings an I2O controller into HOLD state. The adapter
623 * is reset if necessary and then the queues and resource table are read.
625 * Returns 0 on success or negative error code on failure.
627 static int i2o_iop_activate(struct i2o_controller *c)
629 i2o_status_block *sb = c->status_block.virt;
633 /* In INIT state, Wait Inbound Q to initialize (in i2o_status_get) */
634 /* In READY state, Get status */
636 rc = i2o_status_get(c);
638 printk(KERN_INFO "%s: Unable to obtain status, "
639 "attempting a reset.\n", c->name);
640 rc = i2o_iop_reset(c);
645 if (sb->i2o_version > I2OVER15) {
646 printk(KERN_ERR "%s: Not running version 1.5 of the I2O "
647 "Specification.\n", c->name);
651 switch (sb->iop_state) {
652 case ADAPTER_STATE_FAULTED:
653 printk(KERN_CRIT "%s: hardware fault\n", c->name);
656 case ADAPTER_STATE_READY:
657 case ADAPTER_STATE_OPERATIONAL:
658 case ADAPTER_STATE_HOLD:
659 case ADAPTER_STATE_FAILED:
660 pr_debug("%s: already running, trying to reset...\n", c->name);
661 rc = i2o_iop_reset(c);
667 state = sb->iop_state;
669 rc = i2o_iop_init_outbound_queue(c);
673 /* if adapter was not in RESET state clear now */
674 if (state != ADAPTER_STATE_RESET)
679 if (sb->iop_state != ADAPTER_STATE_HOLD) {
680 osm_err("%s: failed to bring IOP into HOLD state\n", c->name);
684 return i2o_hrt_get(c);
688 * i2o_iop_systab_set - Set the I2O System Table of the specified IOP
689 * @c: I2O controller to which the system table should be send
691 * Before the systab could be set i2o_systab_build() must be called.
693 * Returns 0 on success or negative error code on failure.
695 static int i2o_iop_systab_set(struct i2o_controller *c)
697 struct i2o_message __iomem *msg;
699 i2o_status_block *sb = c->status_block.virt;
700 struct device *dev = &c->pdev->dev;
701 struct resource *root;
704 if (sb->current_mem_size < sb->desired_mem_size) {
705 struct resource *res = &c->mem_resource;
706 res->name = c->pdev->bus->name;
707 res->flags = IORESOURCE_MEM;
710 printk(KERN_INFO "%s: requires private memory resources.\n",
712 root = pci_find_parent_resource(c->pdev, res);
714 printk(KERN_WARNING "%s: Can't find parent resource!\n",
716 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 */
719 sb->current_mem_size = 1 + res->end - res->start;
720 sb->current_mem_base = res->start;
721 printk(KERN_INFO "%s: allocated %ld bytes of PCI memory"
722 " at 0x%08lX.\n", c->name,
723 1 + res->end - res->start, res->start);
727 if (sb->current_io_size < sb->desired_io_size) {
728 struct resource *res = &c->io_resource;
729 res->name = c->pdev->bus->name;
730 res->flags = IORESOURCE_IO;
733 printk(KERN_INFO "%s: requires private memory resources.\n",
735 root = pci_find_parent_resource(c->pdev, res);
737 printk(KERN_WARNING "%s: Can't find parent resource!\n",
739 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 */
742 sb->current_io_size = 1 + res->end - res->start;
743 sb->current_mem_base = res->start;
744 printk(KERN_INFO "%s: allocated %ld bytes of PCI I/O at"
745 " 0x%08lX.\n", c->name,
746 1 + res->end - res->start, res->start);
750 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
751 if (m == I2O_QUEUE_EMPTY)
754 i2o_systab.phys = dma_map_single(dev, i2o_systab.virt, i2o_systab.len,
756 if (!i2o_systab.phys) {
761 writel(I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6, &msg->u.head[0]);
762 writel(I2O_CMD_SYS_TAB_SET << 24 | HOST_TID << 12 | ADAPTER_TID,
766 * Provide three SGL-elements:
767 * System table (SysTab), Private memory space declaration and
768 * Private i/o space declaration
770 * FIXME: is this still true?
771 * Nasty one here. We can't use dma_alloc_coherent to send the
772 * same table to everyone. We have to go remap it for them all
775 writel(c->unit + 2, &msg->body[0]);
776 writel(0, &msg->body[1]);
777 writel(0x54000000 | i2o_systab.len, &msg->body[2]);
778 writel(i2o_systab.phys, &msg->body[3]);
779 writel(0x54000000 | sb->current_mem_size, &msg->body[4]);
780 writel(sb->current_mem_base, &msg->body[5]);
781 writel(0xd4000000 | sb->current_io_size, &msg->body[6]);
782 writel(sb->current_io_base, &msg->body[6]);
784 rc = i2o_msg_post_wait(c, m, 120);
786 dma_unmap_single(dev, i2o_systab.phys, i2o_systab.len,
790 printk(KERN_ERR "%s: Unable to set SysTab (status=%#x).\n",
793 pr_debug("%s: SysTab set.\n", c->name);
795 i2o_status_get(c); // Entered READY state
801 * i2o_iop_online - Bring a controller online into OPERATIONAL state.
804 * Send the system table and enable the I2O controller.
806 * Returns 0 on success or negativer error code on failure.
808 static int i2o_iop_online(struct i2o_controller *c)
812 rc = i2o_iop_systab_set(c);
817 pr_debug("%s: Attempting to enable...\n", c->name);
818 rc = i2o_iop_enable(c);
826 * i2o_iop_remove - Remove the I2O controller from the I2O core
829 * Remove the I2O controller from the I2O core. If devices are attached to
830 * the controller remove these also and finally reset the controller.
832 void i2o_iop_remove(struct i2o_controller *c)
834 struct i2o_device *dev, *tmp;
836 pr_debug("%s: deleting controller\n", c->name);
838 i2o_driver_notify_controller_remove_all(c);
842 list_for_each_entry_safe(dev, tmp, &c->devices, list)
843 i2o_device_remove(dev);
845 device_del(&c->device);
847 /* Ask the IOP to switch to RESET state */
850 put_device(&c->device);
854 * i2o_systab_build - Build system table
856 * The system table contains information about all the IOPs in the system
857 * (duh) and is used by the Executives on the IOPs to establish peer2peer
858 * connections. We're not supporting peer2peer at the moment, but this
859 * will be needed down the road for things like lan2lan forwarding.
861 * Returns 0 on success or negative error code on failure.
863 static int i2o_systab_build(void)
865 struct i2o_controller *c, *tmp;
866 int num_controllers = 0;
869 struct i2o_sys_tbl *systab = i2o_systab.virt;
871 list_for_each_entry_safe(c, tmp, &i2o_controllers, list)
875 change_ind = systab->change_ind;
876 kfree(i2o_systab.virt);
880 i2o_systab.len = sizeof(struct i2o_sys_tbl) + num_controllers *
881 sizeof(struct i2o_sys_tbl_entry);
883 systab = i2o_systab.virt = kmalloc(i2o_systab.len, GFP_KERNEL);
885 printk(KERN_ERR "i2o: unable to allocate memory for System "
889 memset(systab, 0, i2o_systab.len);
891 systab->version = I2OVERSION;
892 systab->change_ind = change_ind + 1;
894 list_for_each_entry_safe(c, tmp, &i2o_controllers, list) {
895 i2o_status_block *sb;
897 if (count >= num_controllers) {
898 printk(KERN_ERR "i2o: controller added while building "
903 sb = c->status_block.virt;
906 * Get updated IOP state so we have the latest information
908 * We should delete the controller at this point if it
909 * doesn't respond since if it's not on the system table
910 * it is techninically not part of the I2O subsystem...
912 if (unlikely(i2o_status_get(c))) {
913 printk(KERN_ERR "%s: Deleting b/c could not get status"
914 " while attempting to build system table\n",
917 continue; // try the next one
920 systab->iops[count].org_id = sb->org_id;
921 systab->iops[count].iop_id = c->unit + 2;
922 systab->iops[count].seg_num = 0;
923 systab->iops[count].i2o_version = sb->i2o_version;
924 systab->iops[count].iop_state = sb->iop_state;
925 systab->iops[count].msg_type = sb->msg_type;
926 systab->iops[count].frame_size = sb->inbound_frame_size;
927 systab->iops[count].last_changed = change_ind;
928 systab->iops[count].iop_capabilities = sb->iop_capabilities;
929 systab->iops[count].inbound_low =
930 i2o_dma_low(c->base.phys + I2O_IN_PORT);
931 systab->iops[count].inbound_high =
932 i2o_dma_high(c->base.phys + I2O_IN_PORT);
937 systab->num_entries = count;
943 * i2o_parse_hrt - Parse the hardware resource table.
946 * We don't do anything with it except dumping it (in debug mode).
950 static int i2o_parse_hrt(struct i2o_controller *c)
957 * i2o_status_get - Get the status block from the I2O controller
960 * Issue a status query on the controller. This updates the attached
961 * status block. The status block could then be accessed through
964 * Returns 0 on sucess or negative error code on failure.
966 int i2o_status_get(struct i2o_controller *c)
968 struct i2o_message __iomem *msg;
970 volatile u8 *status_block;
971 unsigned long timeout;
973 status_block = (u8 *) c->status_block.virt;
974 memset(c->status_block.virt, 0, sizeof(i2o_status_block));
976 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
977 if (m == I2O_QUEUE_EMPTY)
980 writel(NINE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
981 writel(I2O_CMD_STATUS_GET << 24 | HOST_TID << 12 | ADAPTER_TID,
983 writel(i2o_exec_driver.context, &msg->u.s.icntxt);
984 writel(0, &msg->u.s.tcntxt); // FIXME: use resonable transaction context
985 writel(0, &msg->body[0]);
986 writel(0, &msg->body[1]);
987 writel(i2o_dma_low(c->status_block.phys), &msg->body[2]);
988 writel(i2o_dma_high(c->status_block.phys), &msg->body[3]);
989 writel(sizeof(i2o_status_block), &msg->body[4]); /* always 88 bytes */
993 /* Wait for a reply */
994 timeout = jiffies + I2O_TIMEOUT_STATUS_GET * HZ;
995 while (status_block[87] != 0xFF) {
996 if (time_after(jiffies, timeout)) {
997 printk(KERN_ERR "%s: Get status timeout.\n", c->name);
1001 set_current_state(TASK_UNINTERRUPTIBLE);
1002 schedule_timeout(1);
1013 * i2o_hrt_get - Get the Hardware Resource Table from the I2O controller
1014 * @c: I2O controller from which the HRT should be fetched
1016 * The HRT contains information about possible hidden devices but is
1017 * mostly useless to us.
1019 * Returns 0 on success or negativer error code on failure.
1021 static int i2o_hrt_get(struct i2o_controller *c)
1025 i2o_hrt *hrt = c->hrt.virt;
1026 u32 size = sizeof(i2o_hrt);
1027 struct device *dev = &c->pdev->dev;
1029 for (i = 0; i < I2O_HRT_GET_TRIES; i++) {
1030 struct i2o_message __iomem *msg;
1033 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
1034 if (m == I2O_QUEUE_EMPTY)
1037 writel(SIX_WORD_MSG_SIZE | SGL_OFFSET_4, &msg->u.head[0]);
1038 writel(I2O_CMD_HRT_GET << 24 | HOST_TID << 12 | ADAPTER_TID,
1040 writel(0xd0000000 | c->hrt.len, &msg->body[0]);
1041 writel(c->hrt.phys, &msg->body[1]);
1043 rc = i2o_msg_post_wait_mem(c, m, 20, &c->hrt);
1046 printk(KERN_ERR "%s: Unable to get HRT (status=%#x)\n",
1051 size = hrt->num_entries * hrt->entry_len << 2;
1052 if (size > c->hrt.len) {
1053 if (i2o_dma_realloc(dev, &c->hrt, size, GFP_KERNEL))
1058 return i2o_parse_hrt(c);
1061 printk(KERN_ERR "%s: Unable to get HRT after %d tries, giving up\n",
1062 c->name, I2O_HRT_GET_TRIES);
1068 * i2o_iop_free - Free the i2o_controller struct
1069 * @c: I2O controller to free
1071 void i2o_iop_free(struct i2o_controller *c)
1078 * i2o_iop_release - release the memory for a I2O controller
1079 * @dev: I2O controller which should be released
1081 * Release the allocated memory. This function is called if refcount of
1082 * device reaches 0 automatically.
1084 static void i2o_iop_release(struct device *dev)
1086 struct i2o_controller *c = to_i2o_controller(dev);
1091 /* I2O controller class */
1092 static struct class i2o_controller_class = {
1093 .name = "i2o_controller",
1097 * i2o_iop_alloc - Allocate and initialize a i2o_controller struct
1099 * Allocate the necessary memory for a i2o_controller struct and
1100 * initialize the lists.
1102 * Returns a pointer to the I2O controller or a negative error code on
1105 struct i2o_controller *i2o_iop_alloc(void)
1107 static int unit = 0; /* 0 and 1 are NULL IOP and Local Host */
1108 struct i2o_controller *c;
1110 c = kmalloc(sizeof(*c), GFP_KERNEL);
1112 printk(KERN_ERR "i2o: Insufficient memory to allocate a I2O "
1114 return ERR_PTR(-ENOMEM);
1116 memset(c, 0, sizeof(*c));
1118 INIT_LIST_HEAD(&c->devices);
1119 spin_lock_init(&c->lock);
1120 init_MUTEX(&c->lct_lock);
1122 sprintf(c->name, "iop%d", c->unit);
1124 device_initialize(&c->device);
1125 class_device_initialize(&c->classdev);
1127 c->device.release = &i2o_iop_release;
1128 c->classdev.class = &i2o_controller_class;
1129 c->classdev.dev = &c->device;
1131 snprintf(c->device.bus_id, BUS_ID_SIZE, "iop%d", c->unit);
1132 snprintf(c->classdev.class_id, BUS_ID_SIZE, "iop%d", c->unit);
1134 #if BITS_PER_LONG == 64
1135 spin_lock_init(&c->context_list_lock);
1136 atomic_set(&c->context_list_counter, 0);
1137 INIT_LIST_HEAD(&c->context_list);
1144 * i2o_iop_add - Initialize the I2O controller and add him to the I2O core
1147 * Initialize the I2O controller and if no error occurs add him to the I2O
1150 * Returns 0 on success or negative error code on failure.
1152 int i2o_iop_add(struct i2o_controller *c)
1156 if ((rc = device_add(&c->device))) {
1157 osm_err("%s: could not add controller\n", c->name);
1161 if ((rc = class_device_add(&c->classdev))) {
1162 osm_err("%s: could not add controller class\n", c->name);
1166 osm_info("%s: Activating I2O controller...\n", c->name);
1167 osm_info("%s: This may take a few minutes if there are many devices\n",
1170 if ((rc = i2o_iop_activate(c))) {
1171 osm_err("%s: could not activate controller\n", c->name);
1175 osm_debug("%s: building sys table...\n", c->name);
1177 if ((rc = i2o_systab_build()))
1180 osm_debug("%s: online controller...\n", c->name);
1182 if ((rc = i2o_iop_online(c)))
1185 osm_debug("%s: getting LCT...\n", c->name);
1187 if ((rc = i2o_exec_lct_get(c)))
1190 list_add(&c->list, &i2o_controllers);
1192 i2o_driver_notify_controller_add_all(c);
1194 osm_info("%s: Controller added\n", c->name);
1199 class_device_del(&c->classdev);
1202 device_del(&c->device);
1211 * i2o_event_register - Turn on/off event notification for a I2O device
1212 * @dev: I2O device which should receive the event registration request
1213 * @drv: driver which want to get notified
1214 * @tcntxt: transaction context to use with this notifier
1215 * @evt_mask: mask of events
1217 * Create and posts an event registration message to the task. No reply
1218 * is waited for, or expected. If you do not want further notifications,
1219 * call the i2o_event_register again with a evt_mask of 0.
1221 * Returns 0 on success or -ETIMEDOUT if no message could be fetched for
1222 * sending the request.
1224 int i2o_event_register(struct i2o_device *dev, struct i2o_driver *drv,
1225 int tcntxt, u32 evt_mask)
1227 struct i2o_controller *c = dev->iop;
1228 struct i2o_message __iomem *msg;
1231 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
1232 if (m == I2O_QUEUE_EMPTY)
1235 writel(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
1236 writel(I2O_CMD_UTIL_EVT_REGISTER << 24 | HOST_TID << 12 | dev->lct_data.
1237 tid, &msg->u.head[1]);
1238 writel(drv->context, &msg->u.s.icntxt);
1239 writel(tcntxt, &msg->u.s.tcntxt);
1240 writel(evt_mask, &msg->body[0]);
1248 * i2o_iop_init - I2O main initialization function
1250 * Initialize the I2O drivers (OSM) functions, register the Executive OSM,
1251 * initialize the I2O PCI part and finally initialize I2O device stuff.
1253 * Returns 0 on success or negative error code on failure.
1255 static int __init i2o_iop_init(void)
1259 printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
1261 rc = i2o_device_init();
1265 if ((rc = class_register(&i2o_controller_class))) {
1266 osm_err("can't register class i2o_controller\n");
1270 if ((rc = i2o_driver_init()))
1273 if ((rc = i2o_exec_init()))
1276 if ((rc = i2o_pci_init()))
1288 class_unregister(&i2o_controller_class);
1298 * i2o_iop_exit - I2O main exit function
1300 * Removes I2O controllers from PCI subsystem and shut down OSMs.
1302 static void __exit i2o_iop_exit(void)
1307 class_unregister(&i2o_controller_class);
1311 module_init(i2o_iop_init);
1312 module_exit(i2o_iop_exit);
1314 MODULE_AUTHOR("Red Hat Software");
1315 MODULE_LICENSE("GPL");
1316 MODULE_DESCRIPTION(OSM_DESCRIPTION);
1317 MODULE_VERSION(OSM_VERSION);
1319 #if BITS_PER_LONG == 64
1320 EXPORT_SYMBOL(i2o_cntxt_list_add);
1321 EXPORT_SYMBOL(i2o_cntxt_list_get);
1322 EXPORT_SYMBOL(i2o_cntxt_list_remove);
1323 EXPORT_SYMBOL(i2o_cntxt_list_get_ptr);
1325 EXPORT_SYMBOL(i2o_msg_get_wait);
1326 EXPORT_SYMBOL(i2o_msg_nop);
1327 EXPORT_SYMBOL(i2o_find_iop);
1328 EXPORT_SYMBOL(i2o_iop_find_device);
1329 EXPORT_SYMBOL(i2o_event_register);
1330 EXPORT_SYMBOL(i2o_status_get);
1331 EXPORT_SYMBOL(i2o_controllers);