[PATCH] aic7xxx_osm build fix
[cris-mirror.git] / drivers / message / i2o / iop.c
blob50c8cedf7a2db58fd670b851849a646579f72ab1
1 /*
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
16 * Fixes/additions:
17 * Philipp Rumpf
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);
60 /**
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
67 * is lost.
69 void i2o_msg_nop(struct i2o_controller *c, u32 m)
71 struct i2o_message __iomem *msg = c->in_queue.virt + 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,
75 &msg->u.head[1]);
76 writel(0, &msg->u.head[2]);
77 writel(0, &msg->u.head[3]);
78 i2o_msg_post(c, m);
81 /**
82 * i2o_msg_get_wait - obtain an I2O message from the IOP
83 * @c: I2O controller
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
88 * available.
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,
96 int wait)
98 unsigned long timeout = jiffies + wait * HZ;
99 u32 m;
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",
104 c->name);
105 return I2O_QUEUE_EMPTY;
107 set_current_state(TASK_UNINTERRUPTIBLE);
108 schedule_timeout(1);
111 return m;
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;
129 unsigned long flags;
131 if (!ptr)
132 printk(KERN_ERR "%s: couldn't add NULL pointer to context list!"
133 "\n", c->name);
135 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
136 if (!entry) {
137 printk(KERN_ERR "%s: Could not allocate memory for context "
138 "list element\n", c->name);
139 return 0;
142 entry->ptr = ptr;
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;
175 u32 context = 0;
176 unsigned long flags;
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;
183 kfree(entry);
184 break;
186 spin_unlock_irqrestore(&c->context_list_lock, flags);
188 if (!context)
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,
193 context, ptr);
195 return context;
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
204 * failure.
206 void *i2o_cntxt_list_get(struct i2o_controller *c, u32 context)
208 struct i2o_context_list_element *entry;
209 unsigned long flags;
210 void *ptr = NULL;
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);
216 ptr = entry->ptr;
217 kfree(entry);
218 break;
220 spin_unlock_irqrestore(&c->context_list_lock, flags);
222 if (!ptr)
223 printk(KERN_WARNING "%s: context id %d not found\n", c->name,
224 context);
226 pr_debug("%s: get ptr from context list %d -> %p\n", c->name, context,
227 ptr);
229 return ptr;
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
238 * failure.
240 u32 i2o_cntxt_list_get_ptr(struct i2o_controller * c, void *ptr)
242 struct i2o_context_list_element *entry;
243 u32 context = 0;
244 unsigned long flags;
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;
250 break;
252 spin_unlock_irqrestore(&c->context_list_lock, flags);
254 if (!context)
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,
259 ptr, context);
261 return context;
263 #endif
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) {
278 if (c->unit == unit)
279 return c;
282 return NULL;
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
291 * returns it.
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)
301 return dev;
303 return NULL;
307 * i2o_quiesce_controller - quiesce controller
308 * @c: 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;
318 u32 m;
319 i2o_status_block *sb = c->status_block.virt;
320 int rc;
322 i2o_status_get(c);
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))
327 return 0;
329 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
330 if (m == I2O_QUEUE_EMPTY)
331 return -ETIMEDOUT;
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,
335 &msg->u.head[1]);
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",
340 c->name, -rc);
341 else
342 pr_debug("%s: Quiesced.\n", c->name);
344 i2o_status_get(c); // Entered READY state
346 return rc;
350 * i2o_iop_enable - move controller from ready to OPERATIONAL
351 * @c: I2O controller
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
355 * an error occurs.
357 static int i2o_iop_enable(struct i2o_controller *c)
359 struct i2o_message __iomem *msg;
360 u32 m;
361 i2o_status_block *sb = c->status_block.virt;
362 int rc;
364 i2o_status_get(c);
366 /* Enable only allowed on READY state */
367 if (sb->iop_state != ADAPTER_STATE_READY)
368 return -EINVAL;
370 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
371 if (m == I2O_QUEUE_EMPTY)
372 return -ETIMEDOUT;
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,
376 &msg->u.head[1]);
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",
381 c->name, -rc);
382 else
383 pr_debug("%s: Enabled.\n", c->name);
385 i2o_status_get(c); // entered OPERATIONAL state
387 return rc;
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) {
400 if (!c->no_quiesce)
401 i2o_iop_quiesce(c);
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)
415 i2o_iop_enable(c);
419 * i2o_clear_controller - Bring I2O controller into HOLD state
420 * @c: controller
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;
432 u32 m;
433 int rc;
435 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
436 if (m == I2O_QUEUE_EMPTY)
437 return -ETIMEDOUT;
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,
444 &msg->u.head[1]);
446 if ((rc = i2o_msg_post_wait(c, m, 30)))
447 printk(KERN_INFO "%s: Unable to clear (status=%#x).\n",
448 c->name, -rc);
449 else
450 pr_debug("%s: Cleared.\n", c->name);
452 /* Enable all IOPs */
453 i2o_iop_enable_all();
455 i2o_status_get(c);
457 return rc;
461 * i2o_iop_reset - reset an I2O controller
462 * @c: controller to reset
464 * Reset the IOP into INIT state and wait until IOP gets into RESET state.
465 * Terminate all external operations, clear IOP's inbound and outbound
466 * queues, terminate all DDMs, and reload the IOP's operating environment
467 * and all local DDMs. The IOP rebuilds its LCT.
469 static int i2o_iop_reset(struct i2o_controller *c)
471 u8 *status = c->status.virt;
472 struct i2o_message __iomem *msg;
473 u32 m;
474 unsigned long timeout;
475 i2o_status_block *sb = c->status_block.virt;
476 int rc = 0;
478 pr_debug("%s: Resetting controller\n", c->name);
480 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
481 if (m == I2O_QUEUE_EMPTY)
482 return -ETIMEDOUT;
484 memset(status, 0, 8);
486 /* Quiesce all IOPs first */
487 i2o_iop_quiesce_all();
489 writel(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
490 writel(I2O_CMD_ADAPTER_RESET << 24 | HOST_TID << 12 | ADAPTER_TID,
491 &msg->u.head[1]);
492 writel(i2o_exec_driver.context, &msg->u.s.icntxt);
493 writel(0, &msg->u.s.tcntxt); //FIXME: use reasonable transaction context
494 writel(0, &msg->body[0]);
495 writel(0, &msg->body[1]);
496 writel(i2o_ptr_low((void *)c->status.phys), &msg->body[2]);
497 writel(i2o_ptr_high((void *)c->status.phys), &msg->body[3]);
499 i2o_msg_post(c, m);
501 /* Wait for a reply */
502 timeout = jiffies + I2O_TIMEOUT_RESET * HZ;
503 while (!*status) {
504 if (time_after(jiffies, timeout)) {
505 printk(KERN_ERR "%s: IOP reset timeout.\n", c->name);
506 rc = -ETIMEDOUT;
507 goto exit;
510 /* Promise bug */
511 if (status[1] || status[4]) {
512 *status = 0;
513 break;
516 set_current_state(TASK_UNINTERRUPTIBLE);
517 schedule_timeout(1);
519 rmb();
522 if (*status == I2O_CMD_IN_PROGRESS) {
524 * Once the reset is sent, the IOP goes into the INIT state
525 * which is indeterminate. We need to wait until the IOP
526 * has rebooted before we can let the system talk to
527 * it. We read the inbound Free_List until a message is
528 * available. If we can't read one in the given ammount of
529 * time, we assume the IOP could not reboot properly.
531 pr_debug("%s: Reset in progress, waiting for reboot...\n",
532 c->name);
534 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_RESET);
535 while (m == I2O_QUEUE_EMPTY) {
536 if (time_after(jiffies, timeout)) {
537 printk(KERN_ERR "%s: IOP reset timeout.\n",
538 c->name);
539 rc = -ETIMEDOUT;
540 goto exit;
542 set_current_state(TASK_UNINTERRUPTIBLE);
543 schedule_timeout(1);
545 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_RESET);
547 i2o_msg_nop(c, m);
550 /* from here all quiesce commands are safe */
551 c->no_quiesce = 0;
553 /* If IopReset was rejected or didn't perform reset, try IopClear */
554 i2o_status_get(c);
555 if (*status == I2O_CMD_REJECTED || sb->iop_state != ADAPTER_STATE_RESET) {
556 printk(KERN_WARNING "%s: Reset rejected, trying to clear\n",
557 c->name);
558 i2o_iop_clear(c);
559 } else
560 pr_debug("%s: Reset completed.\n", c->name);
562 exit:
563 /* Enable all IOPs */
564 i2o_iop_enable_all();
566 return rc;
570 * i2o_iop_init_outbound_queue - setup the outbound message queue
571 * @c: I2O controller
573 * Clear and (re)initialize IOP's outbound queue and post the message
574 * frames to the IOP.
576 * Returns 0 on success or a negative errno code on failure.
578 static int i2o_iop_init_outbound_queue(struct i2o_controller *c)
580 u8 *status = c->status.virt;
581 u32 m;
582 struct i2o_message __iomem *msg;
583 ulong timeout;
584 int i;
586 pr_debug("%s: Initializing Outbound Queue...\n", c->name);
588 memset(status, 0, 4);
590 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
591 if (m == I2O_QUEUE_EMPTY)
592 return -ETIMEDOUT;
594 writel(EIGHT_WORD_MSG_SIZE | TRL_OFFSET_6, &msg->u.head[0]);
595 writel(I2O_CMD_OUTBOUND_INIT << 24 | HOST_TID << 12 | ADAPTER_TID,
596 &msg->u.head[1]);
597 writel(i2o_exec_driver.context, &msg->u.s.icntxt);
598 writel(0x0106, &msg->u.s.tcntxt); /* FIXME: why 0x0106, maybe in
599 Spec? */
600 writel(PAGE_SIZE, &msg->body[0]);
601 writel(MSG_FRAME_SIZE << 16 | 0x80, &msg->body[1]); /* Outbound msg frame
602 size in words and Initcode */
603 writel(0xd0000004, &msg->body[2]);
604 writel(i2o_ptr_low((void *)c->status.phys), &msg->body[3]);
605 writel(i2o_ptr_high((void *)c->status.phys), &msg->body[4]);
607 i2o_msg_post(c, m);
609 timeout = jiffies + I2O_TIMEOUT_INIT_OUTBOUND_QUEUE * HZ;
610 while (*status <= I2O_CMD_IN_PROGRESS) {
611 if (time_after(jiffies, timeout)) {
612 printk(KERN_WARNING "%s: Timeout Initializing\n",
613 c->name);
614 return -ETIMEDOUT;
616 set_current_state(TASK_UNINTERRUPTIBLE);
617 schedule_timeout(1);
619 rmb();
622 m = c->out_queue.phys;
624 /* Post frames */
625 for (i = 0; i < NMBR_MSG_FRAMES; i++) {
626 i2o_flush_reply(c, m);
627 udelay(1); /* Promise */
628 m += MSG_FRAME_SIZE * 4;
631 return 0;
635 * i2o_iop_send_nop - send a core NOP message
636 * @c: controller
638 * Send a no-operation message with a reply set to cause no
639 * action either. Needed for bringing up promise controllers.
641 static int i2o_iop_send_nop(struct i2o_controller *c)
643 struct i2o_message __iomem *msg;
644 u32 m = i2o_msg_get_wait(c, &msg, HZ);
645 if (m == I2O_QUEUE_EMPTY)
646 return -ETIMEDOUT;
647 i2o_msg_nop(c, m);
648 return 0;
652 * i2o_iop_activate - Bring controller up to HOLD
653 * @c: controller
655 * This function brings an I2O controller into HOLD state. The adapter
656 * is reset if necessary and then the queues and resource table are read.
658 * Returns 0 on success or negative error code on failure.
660 static int i2o_iop_activate(struct i2o_controller *c)
662 struct pci_dev *i960 = NULL;
663 i2o_status_block *sb = c->status_block.virt;
664 int rc;
666 if (c->promise) {
667 /* Beat up the hardware first of all */
668 i960 =
669 pci_find_slot(c->pdev->bus->number,
670 PCI_DEVFN(PCI_SLOT(c->pdev->devfn), 0));
671 if (i960)
672 pci_write_config_word(i960, 0x42, 0);
674 /* Follow this sequence precisely or the controller
675 ceases to perform useful functions until reboot */
676 if ((rc = i2o_iop_send_nop(c)))
677 return rc;
679 if ((rc = i2o_iop_reset(c)))
680 return rc;
683 /* In INIT state, Wait Inbound Q to initialize (in i2o_status_get) */
684 /* In READY state, Get status */
686 rc = i2o_status_get(c);
687 if (rc) {
688 printk(KERN_INFO "%s: Unable to obtain status, "
689 "attempting a reset.\n", c->name);
690 if (i2o_iop_reset(c))
691 return rc;
694 if (sb->i2o_version > I2OVER15) {
695 printk(KERN_ERR "%s: Not running version 1.5 of the I2O "
696 "Specification.\n", c->name);
697 return -ENODEV;
700 switch (sb->iop_state) {
701 case ADAPTER_STATE_FAULTED:
702 printk(KERN_CRIT "%s: hardware fault\n", c->name);
703 return -ENODEV;
705 case ADAPTER_STATE_READY:
706 case ADAPTER_STATE_OPERATIONAL:
707 case ADAPTER_STATE_HOLD:
708 case ADAPTER_STATE_FAILED:
709 pr_debug("%s: already running, trying to reset...\n", c->name);
710 if (i2o_iop_reset(c))
711 return -ENODEV;
714 rc = i2o_iop_init_outbound_queue(c);
715 if (rc)
716 return rc;
718 if (c->promise) {
719 if ((rc = i2o_iop_send_nop(c)))
720 return rc;
722 if ((rc = i2o_status_get(c)))
723 return rc;
725 if (i960)
726 pci_write_config_word(i960, 0x42, 0x3FF);
729 /* In HOLD state */
731 rc = i2o_hrt_get(c);
733 return rc;
737 * i2o_iop_systab_set - Set the I2O System Table of the specified IOP
738 * @c: I2O controller to which the system table should be send
740 * Before the systab could be set i2o_systab_build() must be called.
742 * Returns 0 on success or negative error code on failure.
744 static int i2o_iop_systab_set(struct i2o_controller *c)
746 struct i2o_message __iomem *msg;
747 u32 m;
748 i2o_status_block *sb = c->status_block.virt;
749 struct device *dev = &c->pdev->dev;
750 struct resource *root;
751 int rc;
753 if (sb->current_mem_size < sb->desired_mem_size) {
754 struct resource *res = &c->mem_resource;
755 res->name = c->pdev->bus->name;
756 res->flags = IORESOURCE_MEM;
757 res->start = 0;
758 res->end = 0;
759 printk(KERN_INFO "%s: requires private memory resources.\n",
760 c->name);
761 root = pci_find_parent_resource(c->pdev, res);
762 if (root == NULL)
763 printk(KERN_WARNING "%s: Can't find parent resource!\n",
764 c->name);
765 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 */
766 NULL, NULL) >= 0) {
767 c->mem_alloc = 1;
768 sb->current_mem_size = 1 + res->end - res->start;
769 sb->current_mem_base = res->start;
770 printk(KERN_INFO "%s: allocated %ld bytes of PCI memory"
771 " at 0x%08lX.\n", c->name,
772 1 + res->end - res->start, res->start);
776 if (sb->current_io_size < sb->desired_io_size) {
777 struct resource *res = &c->io_resource;
778 res->name = c->pdev->bus->name;
779 res->flags = IORESOURCE_IO;
780 res->start = 0;
781 res->end = 0;
782 printk(KERN_INFO "%s: requires private memory resources.\n",
783 c->name);
784 root = pci_find_parent_resource(c->pdev, res);
785 if (root == NULL)
786 printk(KERN_WARNING "%s: Can't find parent resource!\n",
787 c->name);
788 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 */
789 NULL, NULL) >= 0) {
790 c->io_alloc = 1;
791 sb->current_io_size = 1 + res->end - res->start;
792 sb->current_mem_base = res->start;
793 printk(KERN_INFO "%s: allocated %ld bytes of PCI I/O at"
794 " 0x%08lX.\n", c->name,
795 1 + res->end - res->start, res->start);
799 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
800 if (m == I2O_QUEUE_EMPTY)
801 return -ETIMEDOUT;
803 i2o_systab.phys = dma_map_single(dev, i2o_systab.virt, i2o_systab.len,
804 PCI_DMA_TODEVICE);
805 if (!i2o_systab.phys) {
806 i2o_msg_nop(c, m);
807 return -ENOMEM;
810 writel(I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6, &msg->u.head[0]);
811 writel(I2O_CMD_SYS_TAB_SET << 24 | HOST_TID << 12 | ADAPTER_TID,
812 &msg->u.head[1]);
815 * Provide three SGL-elements:
816 * System table (SysTab), Private memory space declaration and
817 * Private i/o space declaration
819 * FIXME: is this still true?
820 * Nasty one here. We can't use dma_alloc_coherent to send the
821 * same table to everyone. We have to go remap it for them all
824 writel(c->unit + 2, &msg->body[0]);
825 writel(0, &msg->body[1]);
826 writel(0x54000000 | i2o_systab.len, &msg->body[2]);
827 writel(i2o_systab.phys, &msg->body[3]);
828 writel(0x54000000 | sb->current_mem_size, &msg->body[4]);
829 writel(sb->current_mem_base, &msg->body[5]);
830 writel(0xd4000000 | sb->current_io_size, &msg->body[6]);
831 writel(sb->current_io_base, &msg->body[6]);
833 rc = i2o_msg_post_wait(c, m, 120);
835 dma_unmap_single(dev, i2o_systab.phys, i2o_systab.len,
836 PCI_DMA_TODEVICE);
838 if (rc < 0)
839 printk(KERN_ERR "%s: Unable to set SysTab (status=%#x).\n",
840 c->name, -rc);
841 else
842 pr_debug("%s: SysTab set.\n", c->name);
844 i2o_status_get(c); // Entered READY state
846 return rc;
850 * i2o_iop_online - Bring a controller online into OPERATIONAL state.
851 * @c: I2O controller
853 * Send the system table and enable the I2O controller.
855 * Returns 0 on success or negativer error code on failure.
857 static int i2o_iop_online(struct i2o_controller *c)
859 int rc;
861 rc = i2o_iop_systab_set(c);
862 if (rc)
863 return rc;
865 /* In READY state */
866 pr_debug("%s: Attempting to enable...\n", c->name);
867 rc = i2o_iop_enable(c);
868 if (rc)
869 return rc;
871 return 0;
875 * i2o_iop_remove - Remove the I2O controller from the I2O core
876 * @c: I2O controller
878 * Remove the I2O controller from the I2O core. If devices are attached to
879 * the controller remove these also and finally reset the controller.
881 void i2o_iop_remove(struct i2o_controller *c)
883 struct i2o_device *dev, *tmp;
885 pr_debug("%s: deleting controller\n", c->name);
887 i2o_driver_notify_controller_remove_all(c);
889 list_del(&c->list);
891 list_for_each_entry_safe(dev, tmp, &c->devices, list)
892 i2o_device_remove(dev);
894 /* Ask the IOP to switch to RESET state */
895 i2o_iop_reset(c);
899 * i2o_systab_build - Build system table
901 * The system table contains information about all the IOPs in the system
902 * (duh) and is used by the Executives on the IOPs to establish peer2peer
903 * connections. We're not supporting peer2peer at the moment, but this
904 * will be needed down the road for things like lan2lan forwarding.
906 * Returns 0 on success or negative error code on failure.
908 static int i2o_systab_build(void)
910 struct i2o_controller *c, *tmp;
911 int num_controllers = 0;
912 u32 change_ind = 0;
913 int count = 0;
914 struct i2o_sys_tbl *systab = i2o_systab.virt;
916 list_for_each_entry_safe(c, tmp, &i2o_controllers, list)
917 num_controllers++;
919 if (systab) {
920 change_ind = systab->change_ind;
921 kfree(i2o_systab.virt);
924 /* Header + IOPs */
925 i2o_systab.len = sizeof(struct i2o_sys_tbl) + num_controllers *
926 sizeof(struct i2o_sys_tbl_entry);
928 systab = i2o_systab.virt = kmalloc(i2o_systab.len, GFP_KERNEL);
929 if (!systab) {
930 printk(KERN_ERR "i2o: unable to allocate memory for System "
931 "Table\n");
932 return -ENOMEM;
934 memset(systab, 0, i2o_systab.len);
936 systab->version = I2OVERSION;
937 systab->change_ind = change_ind + 1;
939 list_for_each_entry_safe(c, tmp, &i2o_controllers, list) {
940 i2o_status_block *sb;
942 if (count >= num_controllers) {
943 printk(KERN_ERR "i2o: controller added while building "
944 "system table\n");
945 break;
948 sb = c->status_block.virt;
951 * Get updated IOP state so we have the latest information
953 * We should delete the controller at this point if it
954 * doesn't respond since if it's not on the system table
955 * it is techninically not part of the I2O subsystem...
957 if (unlikely(i2o_status_get(c))) {
958 printk(KERN_ERR "%s: Deleting b/c could not get status"
959 " while attempting to build system table\n",
960 c->name);
961 i2o_iop_remove(c);
962 continue; // try the next one
965 systab->iops[count].org_id = sb->org_id;
966 systab->iops[count].iop_id = c->unit + 2;
967 systab->iops[count].seg_num = 0;
968 systab->iops[count].i2o_version = sb->i2o_version;
969 systab->iops[count].iop_state = sb->iop_state;
970 systab->iops[count].msg_type = sb->msg_type;
971 systab->iops[count].frame_size = sb->inbound_frame_size;
972 systab->iops[count].last_changed = change_ind;
973 systab->iops[count].iop_capabilities = sb->iop_capabilities;
974 systab->iops[count].inbound_low = i2o_ptr_low(c->post_port);
975 systab->iops[count].inbound_high = i2o_ptr_high(c->post_port);
977 count++;
980 systab->num_entries = count;
982 return 0;
986 * i2o_parse_hrt - Parse the hardware resource table.
987 * @c: I2O controller
989 * We don't do anything with it except dumping it (in debug mode).
991 * Returns 0.
993 static int i2o_parse_hrt(struct i2o_controller *c)
995 i2o_dump_hrt(c);
996 return 0;
1000 * i2o_status_get - Get the status block from the I2O controller
1001 * @c: I2O controller
1003 * Issue a status query on the controller. This updates the attached
1004 * status block. The status block could then be accessed through
1005 * c->status_block.
1007 * Returns 0 on sucess or negative error code on failure.
1009 int i2o_status_get(struct i2o_controller *c)
1011 struct i2o_message __iomem *msg;
1012 u32 m;
1013 u8 *status_block;
1014 unsigned long timeout;
1016 status_block = (u8 *) c->status_block.virt;
1017 memset(status_block, 0, sizeof(i2o_status_block));
1019 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
1020 if (m == I2O_QUEUE_EMPTY)
1021 return -ETIMEDOUT;
1023 writel(NINE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
1024 writel(I2O_CMD_STATUS_GET << 24 | HOST_TID << 12 | ADAPTER_TID,
1025 &msg->u.head[1]);
1026 writel(i2o_exec_driver.context, &msg->u.s.icntxt);
1027 writel(0, &msg->u.s.tcntxt); // FIXME: use resonable transaction context
1028 writel(0, &msg->body[0]);
1029 writel(0, &msg->body[1]);
1030 writel(i2o_ptr_low((void *)c->status_block.phys), &msg->body[2]);
1031 writel(i2o_ptr_high((void *)c->status_block.phys), &msg->body[3]);
1032 writel(sizeof(i2o_status_block), &msg->body[4]); /* always 88 bytes */
1034 i2o_msg_post(c, m);
1036 /* Wait for a reply */
1037 timeout = jiffies + I2O_TIMEOUT_STATUS_GET * HZ;
1038 while (status_block[87] != 0xFF) {
1039 if (time_after(jiffies, timeout)) {
1040 printk(KERN_ERR "%s: Get status timeout.\n", c->name);
1041 return -ETIMEDOUT;
1044 set_current_state(TASK_UNINTERRUPTIBLE);
1045 schedule_timeout(1);
1047 rmb();
1050 #ifdef DEBUG
1051 i2o_debug_state(c);
1052 #endif
1054 return 0;
1058 * i2o_hrt_get - Get the Hardware Resource Table from the I2O controller
1059 * @c: I2O controller from which the HRT should be fetched
1061 * The HRT contains information about possible hidden devices but is
1062 * mostly useless to us.
1064 * Returns 0 on success or negativer error code on failure.
1066 static int i2o_hrt_get(struct i2o_controller *c)
1068 int rc;
1069 int i;
1070 i2o_hrt *hrt = c->hrt.virt;
1071 u32 size = sizeof(i2o_hrt);
1072 struct device *dev = &c->pdev->dev;
1074 for (i = 0; i < I2O_HRT_GET_TRIES; i++) {
1075 struct i2o_message __iomem *msg;
1076 u32 m;
1078 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
1079 if (m == I2O_QUEUE_EMPTY)
1080 return -ETIMEDOUT;
1082 writel(SIX_WORD_MSG_SIZE | SGL_OFFSET_4, &msg->u.head[0]);
1083 writel(I2O_CMD_HRT_GET << 24 | HOST_TID << 12 | ADAPTER_TID,
1084 &msg->u.head[1]);
1085 writel(0xd0000000 | c->hrt.len, &msg->body[0]);
1086 writel(c->hrt.phys, &msg->body[1]);
1088 rc = i2o_msg_post_wait_mem(c, m, 20, &c->hrt);
1090 if (rc < 0) {
1091 printk(KERN_ERR "%s: Unable to get HRT (status=%#x)\n",
1092 c->name, -rc);
1093 return rc;
1096 size = hrt->num_entries * hrt->entry_len << 2;
1097 if (size > c->hrt.len) {
1098 if (i2o_dma_realloc(dev, &c->hrt, size, GFP_KERNEL))
1099 return -ENOMEM;
1100 else
1101 hrt = c->hrt.virt;
1102 } else
1103 return i2o_parse_hrt(c);
1106 printk(KERN_ERR "%s: Unable to get HRT after %d tries, giving up\n",
1107 c->name, I2O_HRT_GET_TRIES);
1109 return -EBUSY;
1113 * i2o_iop_alloc - Allocate and initialize a i2o_controller struct
1115 * Allocate the necessary memory for a i2o_controller struct and
1116 * initialize the lists.
1118 * Returns a pointer to the I2O controller or a negative error code on
1119 * failure.
1121 struct i2o_controller *i2o_iop_alloc(void)
1123 static int unit = 0; /* 0 and 1 are NULL IOP and Local Host */
1124 struct i2o_controller *c;
1126 c = kmalloc(sizeof(*c), GFP_KERNEL);
1127 if (!c) {
1128 printk(KERN_ERR "i2o: Insufficient memory to allocate a I2O "
1129 "controller.\n");
1130 return ERR_PTR(-ENOMEM);
1132 memset(c, 0, sizeof(*c));
1134 INIT_LIST_HEAD(&c->devices);
1135 spin_lock_init(&c->lock);
1136 init_MUTEX(&c->lct_lock);
1137 c->unit = unit++;
1138 sprintf(c->name, "iop%d", c->unit);
1140 #if BITS_PER_LONG == 64
1141 spin_lock_init(&c->context_list_lock);
1142 atomic_set(&c->context_list_counter, 0);
1143 INIT_LIST_HEAD(&c->context_list);
1144 #endif
1146 return c;
1150 * i2o_iop_free - Free the i2o_controller struct
1151 * @c: I2O controller to free
1153 void i2o_iop_free(struct i2o_controller *c)
1155 kfree(c);
1159 * i2o_iop_add - Initialize the I2O controller and add him to the I2O core
1160 * @c: controller
1162 * Initialize the I2O controller and if no error occurs add him to the I2O
1163 * core.
1165 * Returns 0 on success or negative error code on failure.
1167 int i2o_iop_add(struct i2o_controller *c)
1169 int rc;
1171 printk(KERN_INFO "%s: Activating I2O controller...\n", c->name);
1172 printk(KERN_INFO "%s: This may take a few minutes if there are many "
1173 "devices\n", c->name);
1175 if ((rc = i2o_iop_activate(c))) {
1176 printk(KERN_ERR "%s: could not activate controller\n",
1177 c->name);
1178 i2o_iop_reset(c);
1179 return rc;
1182 pr_debug("%s: building sys table...\n", c->name);
1184 if ((rc = i2o_systab_build())) {
1185 i2o_iop_reset(c);
1186 return rc;
1189 pr_debug("%s: online controller...\n", c->name);
1191 if ((rc = i2o_iop_online(c))) {
1192 i2o_iop_reset(c);
1193 return rc;
1196 pr_debug("%s: getting LCT...\n", c->name);
1198 if ((rc = i2o_exec_lct_get(c))) {
1199 i2o_iop_reset(c);
1200 return rc;
1203 list_add(&c->list, &i2o_controllers);
1205 i2o_driver_notify_controller_add_all(c);
1207 printk(KERN_INFO "%s: Controller added\n", c->name);
1209 return 0;
1213 * i2o_event_register - Turn on/off event notification for a I2O device
1214 * @dev: I2O device which should receive the event registration request
1215 * @drv: driver which want to get notified
1216 * @tcntxt: transaction context to use with this notifier
1217 * @evt_mask: mask of events
1219 * Create and posts an event registration message to the task. No reply
1220 * is waited for, or expected. If you do not want further notifications,
1221 * call the i2o_event_register again with a evt_mask of 0.
1223 * Returns 0 on success or -ETIMEDOUT if no message could be fetched for
1224 * sending the request.
1226 int i2o_event_register(struct i2o_device *dev, struct i2o_driver *drv,
1227 int tcntxt, u32 evt_mask)
1229 struct i2o_controller *c = dev->iop;
1230 struct i2o_message __iomem *msg;
1231 u32 m;
1233 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
1234 if (m == I2O_QUEUE_EMPTY)
1235 return -ETIMEDOUT;
1237 writel(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
1238 writel(I2O_CMD_UTIL_EVT_REGISTER << 24 | HOST_TID << 12 | dev->lct_data.
1239 tid, &msg->u.head[1]);
1240 writel(drv->context, &msg->u.s.icntxt);
1241 writel(tcntxt, &msg->u.s.tcntxt);
1242 writel(evt_mask, &msg->body[0]);
1244 i2o_msg_post(c, m);
1246 return 0;
1250 * i2o_iop_init - I2O main initialization function
1252 * Initialize the I2O drivers (OSM) functions, register the Executive OSM,
1253 * initialize the I2O PCI part and finally initialize I2O device stuff.
1255 * Returns 0 on success or negative error code on failure.
1257 static int __init i2o_iop_init(void)
1259 int rc = 0;
1261 printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
1263 rc = i2o_device_init();
1264 if (rc)
1265 goto exit;
1267 rc = i2o_driver_init();
1268 if (rc)
1269 goto device_exit;
1271 rc = i2o_exec_init();
1272 if (rc)
1273 goto driver_exit;
1275 rc = i2o_pci_init();
1276 if (rc < 0)
1277 goto exec_exit;
1279 return 0;
1281 exec_exit:
1282 i2o_exec_exit();
1284 driver_exit:
1285 i2o_driver_exit();
1287 device_exit:
1288 i2o_device_exit();
1290 exit:
1291 return rc;
1295 * i2o_iop_exit - I2O main exit function
1297 * Removes I2O controllers from PCI subsystem and shut down OSMs.
1299 static void __exit i2o_iop_exit(void)
1301 i2o_pci_exit();
1302 i2o_exec_exit();
1303 i2o_driver_exit();
1304 i2o_device_exit();
1307 module_init(i2o_iop_init);
1308 module_exit(i2o_iop_exit);
1310 MODULE_AUTHOR("Red Hat Software");
1311 MODULE_LICENSE("GPL");
1312 MODULE_DESCRIPTION(OSM_DESCRIPTION);
1313 MODULE_VERSION(OSM_VERSION);
1315 #if BITS_PER_LONG == 64
1316 EXPORT_SYMBOL(i2o_cntxt_list_add);
1317 EXPORT_SYMBOL(i2o_cntxt_list_get);
1318 EXPORT_SYMBOL(i2o_cntxt_list_remove);
1319 EXPORT_SYMBOL(i2o_cntxt_list_get_ptr);
1320 #endif
1321 EXPORT_SYMBOL(i2o_msg_get_wait);
1322 EXPORT_SYMBOL(i2o_msg_nop);
1323 EXPORT_SYMBOL(i2o_find_iop);
1324 EXPORT_SYMBOL(i2o_iop_find_device);
1325 EXPORT_SYMBOL(i2o_event_register);
1326 EXPORT_SYMBOL(i2o_status_get);
1327 EXPORT_SYMBOL(i2o_controllers);