v2.4.8 -> v2.4.8.1
[opensuse:kernel.git] / drivers / i2o / i2o_core.c
1 /*
2  * Core I2O structure management 
3  * 
4  * (C) Copyright 1999   Red Hat Software 
5  *
6  * Written by Alan Cox, Building Number Three Ltd 
7  * 
8  * This program is free software; you can redistribute it and/or 
9  * modify it under the terms of the GNU General Public License 
10  * as published by the Free Software Foundation; either version 
11  * 2 of the License, or (at your option) any later version.  
12  * 
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 
15  * 
16  * Fixes by: 
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  * 
23  */
24
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/pci.h>
29
30 #include <linux/i2o.h>
31
32 #include <linux/errno.h>
33 #include <linux/init.h>
34 #include <linux/slab.h>
35 #include <linux/spinlock.h>
36 #include <linux/smp_lock.h>
37
38 #include <linux/bitops.h>
39 #include <linux/wait.h>
40 #include <linux/delay.h>
41 #include <linux/timer.h>
42 #include <linux/tqueue.h>
43 #include <linux/interrupt.h>
44 #include <linux/sched.h>
45 #include <asm/semaphore.h>
46 #include <linux/completion.h>
47
48 #include <asm/io.h>
49 #include <linux/reboot.h>
50
51 #include "i2o_lan.h"
52
53 //#define DRIVERDEBUG
54
55 #ifdef DRIVERDEBUG
56 #define dprintk(s, args...) printk(s, ## args)
57 #else
58 #define dprintk(s, args...)
59 #endif
60
61 /* OSM table */
62 static struct i2o_handler *i2o_handlers[MAX_I2O_MODULES];
63
64 /* Controller list */
65 static struct i2o_controller *i2o_controllers[MAX_I2O_CONTROLLERS];
66 struct i2o_controller *i2o_controller_chain;
67 int i2o_num_controllers;
68
69 /* Initiator Context for Core message */
70 static int core_context;
71
72 /* Initialization && shutdown functions */
73 static void i2o_sys_init(void);
74 static void i2o_sys_shutdown(void);
75 static int i2o_reset_controller(struct i2o_controller *);
76 static int i2o_reboot_event(struct notifier_block *, unsigned long , void *);
77 static int i2o_online_controller(struct i2o_controller *);
78 static int i2o_init_outbound_q(struct i2o_controller *);
79 static int i2o_post_outbound_messages(struct i2o_controller *);
80
81 /* Reply handler */
82 static void i2o_core_reply(struct i2o_handler *, struct i2o_controller *,
83                            struct i2o_message *);
84
85 /* Various helper functions */
86 static int i2o_lct_get(struct i2o_controller *);
87 static int i2o_lct_notify(struct i2o_controller *);
88 static int i2o_hrt_get(struct i2o_controller *);
89
90 static int i2o_build_sys_table(void);
91 static int i2o_systab_send(struct i2o_controller *c);
92
93 /* I2O core event handler */
94 static int i2o_core_evt(void *);
95 static int evt_pid;
96 static int evt_running;
97
98 /* Dynamic LCT update handler */
99 static int i2o_dyn_lct(void *);
100
101 void i2o_report_controller_unit(struct i2o_controller *, struct i2o_device *);
102
103 /*
104  * I2O System Table.  Contains information about
105  * all the IOPs in the system.  Used to inform IOPs
106  * about each other's existence.
107  *
108  * sys_tbl_ver is the CurrentChangeIndicator that is
109  * used by IOPs to track changes.
110  */
111 static struct i2o_sys_tbl *sys_tbl;
112 static int sys_tbl_ind;
113 static int sys_tbl_len;
114
115 /*
116  * This spin lock is used to keep a device from being
117  * added and deleted concurrently across CPUs or interrupts.
118  * This can occur when a user creates a device and immediatelly
119  * deletes it before the new_dev_notify() handler is called.
120  */
121 static spinlock_t i2o_dev_lock = SPIN_LOCK_UNLOCKED;
122
123 #ifdef MODULE
124 /* 
125  * Function table to send to bus specific layers
126  * See <include/linux/i2o.h> for explanation of this
127  */
128 static struct i2o_core_func_table i2o_core_functions =
129 {
130         i2o_install_controller,
131         i2o_activate_controller,
132         i2o_find_controller,
133         i2o_unlock_controller,
134         i2o_run_queue,
135         i2o_delete_controller
136 };
137
138 #ifdef CONFIG_I2O_PCI_MODULE
139 extern int i2o_pci_core_attach(struct i2o_core_func_table *);
140 extern void i2o_pci_core_detach(void);
141 #endif /* CONFIG_I2O_PCI_MODULE */
142
143 #endif /* MODULE */
144
145 /*
146  * Structures and definitions for synchronous message posting.
147  * See i2o_post_wait() for description.
148  */ 
149 struct i2o_post_wait_data
150 {
151         int *status;            /* Pointer to status block on caller stack */
152         int *complete;          /* Pointer to completion flag on caller stack */
153         u32 id;                 /* Unique identifier */
154         wait_queue_head_t *wq;  /* Wake up for caller (NULL for dead) */
155         struct i2o_post_wait_data *next;        /* Chain */
156         void *mem[2];           /* Memory blocks to recover on failure path */
157 };
158 static struct i2o_post_wait_data *post_wait_queue;
159 static u32 post_wait_id;        // Unique ID for each post_wait
160 static spinlock_t post_wait_lock = SPIN_LOCK_UNLOCKED;
161 static void i2o_post_wait_complete(u32, int);
162
163 /* OSM descriptor handler */ 
164 static struct i2o_handler i2o_core_handler =
165 {
166         (void *)i2o_core_reply,
167         NULL,
168         NULL,
169         NULL,
170         "I2O core layer",
171         0,
172         I2O_CLASS_EXECUTIVE
173 };
174
175 /*
176  * Used when queueing a reply to be handled later
177  */
178  
179 struct reply_info
180 {
181         struct i2o_controller *iop;
182         u32 msg[MSG_FRAME_SIZE];
183 };
184 static struct reply_info evt_reply;
185 static struct reply_info events[I2O_EVT_Q_LEN];
186 static int evt_in;
187 static int evt_out;
188 static int evt_q_len;
189 #define MODINC(x,y) ((x) = ((x) + 1) % (y))
190
191 /*
192  * I2O configuration spinlock. This isnt a big deal for contention
193  * so we have one only
194  */
195
196 static DECLARE_MUTEX(i2o_configuration_lock);
197
198 /* 
199  * Event spinlock.  Used to keep event queue sane and from
200  * handling multiple events simultaneously.
201  */
202 static spinlock_t i2o_evt_lock = SPIN_LOCK_UNLOCKED;
203
204 /*
205  * Semaphore used to synchronize event handling thread with 
206  * interrupt handler.
207  */
208  
209 static DECLARE_MUTEX(evt_sem);
210 static DECLARE_COMPLETION(evt_dead);
211 DECLARE_WAIT_QUEUE_HEAD(evt_wait);
212
213 static struct notifier_block i2o_reboot_notifier =
214 {
215         i2o_reboot_event,
216         NULL,
217         0
218 };
219
220 /*
221  *      Config options
222  */
223
224 static int verbose;
225 MODULE_PARM(verbose, "i");
226
227 /*
228  * I2O Core reply handler
229  */
230 static void i2o_core_reply(struct i2o_handler *h, struct i2o_controller *c,
231                     struct i2o_message *m)
232 {
233         u32 *msg=(u32 *)m;
234         u32 status;
235         u32 context = msg[2];
236
237         if (msg[0] & MSG_FAIL) // Fail bit is set
238         {
239                 u32 *preserved_msg = (u32*)(c->mem_offset + msg[7]);
240
241                 i2o_report_status(KERN_INFO, "i2o_core", msg);
242                 i2o_dump_message(preserved_msg);
243
244                 /* If the failed request needs special treatment,
245                  * it should be done here. */
246
247                 /* Release the preserved msg by resubmitting it as a NOP */
248
249                 preserved_msg[0] = THREE_WORD_MSG_SIZE | SGL_OFFSET_0;
250                 preserved_msg[1] = I2O_CMD_UTIL_NOP << 24 | HOST_TID << 12 | 0;
251                 preserved_msg[2] = 0;
252                 i2o_post_message(c, msg[7]);
253
254                 /* If reply to i2o_post_wait failed, return causes a timeout */
255
256                 return;
257         }       
258
259 #ifdef DRIVERDEBUG
260         i2o_report_status(KERN_INFO, "i2o_core", msg);
261 #endif
262
263         if(msg[2]&0x80000000)   // Post wait message
264         {
265                 if (msg[4] >> 24)
266                         status = (msg[4] & 0xFFFF);
267                 else
268                         status = I2O_POST_WAIT_OK;
269         
270                 i2o_post_wait_complete(context, status);
271                 return;
272         }
273
274         if(m->function == I2O_CMD_UTIL_EVT_REGISTER)
275         {
276                 memcpy(events[evt_in].msg, msg, (msg[0]>>16)<<2);
277                 events[evt_in].iop = c;
278
279                 spin_lock(&i2o_evt_lock);
280                 MODINC(evt_in, I2O_EVT_Q_LEN);
281                 if(evt_q_len == I2O_EVT_Q_LEN)
282                         MODINC(evt_out, I2O_EVT_Q_LEN);
283                 else
284                         evt_q_len++;
285                 spin_unlock(&i2o_evt_lock);
286
287                 up(&evt_sem);
288                 wake_up_interruptible(&evt_wait);
289                 return;
290         }
291
292         if(m->function == I2O_CMD_LCT_NOTIFY)
293         {
294                 up(&c->lct_sem);
295                 return;
296         }
297
298         /*
299          * If this happens, we want to dump the message to the syslog so
300          * it can be sent back to the card manufacturer by the end user
301          * to aid in debugging.
302          * 
303          */
304         printk(KERN_WARNING "%s: Unsolicited message reply sent to core!"
305                         "Message dumped to syslog\n", 
306                         c->name);
307         i2o_dump_message(msg);
308
309         return;
310 }
311
312 /**
313  *      i2o_install_handler - install a message handler
314  *      @h: Handler structure
315  *
316  *      Install an I2O handler - these handle the asynchronous messaging
317  *      from the card once it has initialised. If the table of handlers is
318  *      full then -ENOSPC is returned. On a success 0 is returned and the
319  *      context field is set by the function. The structure is part of the
320  *      system from this time onwards. It must not be freed until it has
321  *      been uninstalled
322  */
323  
324 int i2o_install_handler(struct i2o_handler *h)
325 {
326         int i;
327         down(&i2o_configuration_lock);
328         for(i=0;i<MAX_I2O_MODULES;i++)
329         {
330                 if(i2o_handlers[i]==NULL)
331                 {
332                         h->context = i;
333                         i2o_handlers[i]=h;
334                         up(&i2o_configuration_lock);
335                         return 0;
336                 }
337         }
338         up(&i2o_configuration_lock);
339         return -ENOSPC;
340 }
341
342 /**
343  *      i2o_remove_handler - remove an i2o message handler
344  *      @h: handler
345  *
346  *      Remove a message handler previously installed with i2o_install_handler.
347  *      After this function returns the handler object can be freed or re-used
348  */
349  
350 int i2o_remove_handler(struct i2o_handler *h)
351 {
352         i2o_handlers[h->context]=NULL;
353         return 0;
354 }
355         
356
357 /*
358  *      Each I2O controller has a chain of devices on it.
359  * Each device has a pointer to it's LCT entry to be used
360  * for fun purposes.
361  */
362
363 /**
364  *      i2o_install_device      -       attach a device to a controller
365  *      @c: controller
366  *      @d: device
367  *      
368  *      Add a new device to an i2o controller. This can be called from
369  *      non interrupt contexts only. It adds the device and marks it as
370  *      unclaimed. The device memory becomes part of the kernel and must
371  *      be uninstalled before being freed or reused. Zero is returned
372  *      on success.
373  */
374  
375 int i2o_install_device(struct i2o_controller *c, struct i2o_device *d)
376 {
377         int i;
378
379         down(&i2o_configuration_lock);
380         d->controller=c;
381         d->owner=NULL;
382         d->next=c->devices;
383         d->prev=NULL;
384         if (c->devices != NULL)
385                 c->devices->prev=d;
386         c->devices=d;
387         *d->dev_name = 0;
388
389         for(i = 0; i < I2O_MAX_MANAGERS; i++)
390                 d->managers[i] = NULL;
391
392         up(&i2o_configuration_lock);
393         return 0;
394 }
395
396 /* we need this version to call out of i2o_delete_controller */
397
398 int __i2o_delete_device(struct i2o_device *d)
399 {
400         struct i2o_device **p;
401         int i;
402
403         p=&(d->controller->devices);
404
405         /*
406          *      Hey we have a driver!
407          * Check to see if the driver wants us to notify it of 
408          * device deletion. If it doesn't we assume that it
409          * is unsafe to delete a device with an owner and 
410          * fail.
411          */
412         if(d->owner)
413         {
414                 if(d->owner->dev_del_notify)
415                 {
416                         dprintk(KERN_INFO "Device has owner, notifying\n");
417                         d->owner->dev_del_notify(d->controller, d);
418                         if(d->owner)
419                         {
420                                 printk(KERN_WARNING 
421                                         "Driver \"%s\" did not release device!\n", d->owner->name);
422                                 return -EBUSY;
423                         }
424                 }
425                 else
426                         return -EBUSY;
427         }
428
429         /*
430          * Tell any other users who are talking to this device
431          * that it's going away.  We assume that everything works.
432          */
433         for(i=0; i < I2O_MAX_MANAGERS; i++)
434         {
435                 if(d->managers[i] && d->managers[i]->dev_del_notify)
436                         d->managers[i]->dev_del_notify(d->controller, d);
437         }
438                                 
439         while(*p!=NULL)
440         {
441                 if(*p==d)
442                 {
443                         /*
444                          *      Destroy
445                          */
446                         *p=d->next;
447                         kfree(d);
448                         return 0;
449                 }
450                 p=&((*p)->next);
451         }
452         printk(KERN_ERR "i2o_delete_device: passed invalid device.\n");
453         return -EINVAL;
454 }
455
456 /**
457  *      i2o_delete_device       -       remove an i2o device
458  *      @d: device to remove
459  *
460  *      This function unhooks a device from a controller. The device
461  *      will not be unhooked if it has an owner who does not wish to free
462  *      it, or if the owner lacks a dev_del_notify function. In that case
463  *      -EBUSY is returned. On success 0 is returned. Other errors cause
464  *      negative errno values to be returned
465  */
466  
467 int i2o_delete_device(struct i2o_device *d)
468 {
469         int ret;
470
471         down(&i2o_configuration_lock);
472
473         /*
474          *      Seek, locate
475          */
476
477         ret = __i2o_delete_device(d);
478
479         up(&i2o_configuration_lock);
480
481         return ret;
482 }
483
484 /**
485  *      i2o_install_controller  -       attach a controller
486  *      @c: controller
487  *      
488  *      Add a new controller to the i2o layer. This can be called from
489  *      non interrupt contexts only. It adds the controller and marks it as
490  *      unused with no devices. If the tables are full or memory allocations
491  *      fail then a negative errno code is returned. On success zero is
492  *      returned and the controller is bound to the system. The structure
493  *      must not be freed or reused until being uninstalled.
494  */
495  
496 int i2o_install_controller(struct i2o_controller *c)
497 {
498         int i;
499         down(&i2o_configuration_lock);
500         for(i=0;i<MAX_I2O_CONTROLLERS;i++)
501         {
502                 if(i2o_controllers[i]==NULL)
503                 {
504                         c->dlct = (i2o_lct*)kmalloc(8192, GFP_KERNEL);
505                         if(c->dlct==NULL)
506                         {
507                                 up(&i2o_configuration_lock);
508                                 return -ENOMEM;
509                         }
510                         i2o_controllers[i]=c;
511                         c->devices = NULL;
512                         c->next=i2o_controller_chain;
513                         i2o_controller_chain=c;
514                         c->unit = i;
515                         c->page_frame = NULL;
516                         c->hrt = NULL;
517                         c->lct = NULL;
518                         c->status_block = NULL;
519                         sprintf(c->name, "i2o/iop%d", i);
520                         i2o_num_controllers++;
521                         init_MUTEX_LOCKED(&c->lct_sem);
522                         up(&i2o_configuration_lock);
523                         return 0;
524                 }
525         }
526         printk(KERN_ERR "No free i2o controller slots.\n");
527         up(&i2o_configuration_lock);
528         return -EBUSY;
529 }
530
531 /**
532  *      i2o_delete_controller   - delete a controller
533  *      @c: controller
534  *      
535  *      Remove an i2o controller from the system. If the controller or its
536  *      devices are busy then -EBUSY is returned. On a failure a negative
537  *      errno code is returned. On success zero is returned.
538  */
539   
540 int i2o_delete_controller(struct i2o_controller *c)
541 {
542         struct i2o_controller **p;
543         int users;
544         char name[16];
545         int stat;
546
547         dprintk(KERN_INFO "Deleting controller %s\n", c->name);
548
549         /*
550          * Clear event registration as this can cause weird behavior
551          */
552         if(c->status_block->iop_state == ADAPTER_STATE_OPERATIONAL)
553                 i2o_event_register(c, core_context, 0, 0, 0);
554
555         down(&i2o_configuration_lock);
556         if((users=atomic_read(&c->users)))
557         {
558                 dprintk(KERN_INFO "I2O: %d users for controller %s\n", users,
559                         c->name);
560                 up(&i2o_configuration_lock);
561                 return -EBUSY;
562         }
563         while(c->devices)
564         {
565                 if(__i2o_delete_device(c->devices)<0)
566                 {
567                         /* Shouldnt happen */
568                         c->bus_disable(c);
569                         up(&i2o_configuration_lock);
570                         return -EBUSY;
571                 }
572         }
573
574         /*
575          * If this is shutdown time, the thread's already been killed
576          */
577         if(c->lct_running) {
578                 stat = kill_proc(c->lct_pid, SIGTERM, 1);
579                 if(!stat) {
580                         int count = 10 * 100;
581                         while(c->lct_running && --count) {
582                                 current->state = TASK_INTERRUPTIBLE;
583                                 schedule_timeout(1);
584                         }
585                 
586                         if(!count)
587                                 printk(KERN_ERR 
588                                         "%s: LCT thread still running!\n", 
589                                         c->name);
590                 }
591         }
592
593         p=&i2o_controller_chain;
594
595         while(*p)
596         {
597                 if(*p==c)
598                 {
599                         /* Ask the IOP to switch to RESET state */
600                         i2o_reset_controller(c);
601
602                         /* Release IRQ */
603                         c->destructor(c);
604
605                         *p=c->next;
606                         up(&i2o_configuration_lock);
607
608                         if(c->page_frame)
609                                 kfree(c->page_frame);
610                         if(c->hrt)
611                                 kfree(c->hrt);
612                         if(c->lct)
613                                 kfree(c->lct);
614                         if(c->status_block)
615                                 kfree(c->status_block);
616                         if(c->dlct)
617                                 kfree(c->dlct);
618
619                         i2o_controllers[c->unit]=NULL;
620                         memcpy(name, c->name, strlen(c->name)+1);
621                         kfree(c);
622                         dprintk(KERN_INFO "%s: Deleted from controller chain.\n", name);
623                         
624                         i2o_num_controllers--;
625                         return 0;
626                 }
627                 p=&((*p)->next);
628         }
629         up(&i2o_configuration_lock);
630         printk(KERN_ERR "i2o_delete_controller: bad pointer!\n");
631         return -ENOENT;
632 }
633
634 /**
635  *      i2o_unlock_controller   -       unlock a controller
636  *      @c: controller to unlock
637  *
638  *      Take a lock on an i2o controller. This prevents it being deleted.
639  *      i2o controllers are not refcounted so a deletion of an in use device
640  *      will fail, not take affect on the last dereference.
641  */
642  
643 void i2o_unlock_controller(struct i2o_controller *c)
644 {
645         atomic_dec(&c->users);
646 }
647
648 /**
649  *      i2o_find_controller - return a locked controller
650  *      @n: controller number
651  *
652  *      Returns a pointer to the controller object. The controller is locked
653  *      on return. NULL is returned if the controller is not found.
654  */
655  
656 struct i2o_controller *i2o_find_controller(int n)
657 {
658         struct i2o_controller *c;
659         
660         if(n<0 || n>=MAX_I2O_CONTROLLERS)
661                 return NULL;
662         
663         down(&i2o_configuration_lock);
664         c=i2o_controllers[n];
665         if(c!=NULL)
666                 atomic_inc(&c->users);
667         up(&i2o_configuration_lock);
668         return c;
669 }
670
671 /**
672  *      i2o_issue_claim - claim or release a device
673  *      @cmd: command
674  *      @c: controller to claim for
675  *      @tid: i2o task id
676  *      @type: type of claim
677  *
678  *      Issue I2O UTIL_CLAIM and UTIL_RELEASE messages. The message to be sent
679  *      is set by cmd. The tid is the task id of the object to claim and the
680  *      type is the claim type (see the i2o standard)
681  *
682  *      Zero is returned on success.
683  */
684  
685 static int i2o_issue_claim(u32 cmd, struct i2o_controller *c, int tid, u32 type)
686 {
687         u32 msg[5];
688
689         msg[0] = FIVE_WORD_MSG_SIZE | SGL_OFFSET_0;
690         msg[1] = cmd << 24 | HOST_TID<<12 | tid;
691         msg[3] = 0;
692         msg[4] = type;
693         
694         return i2o_post_wait(c, msg, sizeof(msg), 60);
695 }
696
697 /*
698  *      i2o_claim_device - claim a device for use by an OSM
699  *      @d: device to claim
700  *      @h: handler for this device
701  *
702  *      Do the leg work to assign a device to a given OSM on Linux. The
703  *      kernel updates the internal handler data for the device and then
704  *      performs an I2O claim for the device, attempting to claim the
705  *      device as primary. If the attempt fails a negative errno code
706  *      is returned. On success zero is returned.
707  */
708  
709 int i2o_claim_device(struct i2o_device *d, struct i2o_handler *h)
710 {
711         down(&i2o_configuration_lock);
712         if (d->owner) {
713                 printk(KERN_INFO "Device claim called, but dev already owned by %s!",
714                        h->name);
715                 up(&i2o_configuration_lock);
716                 return -EBUSY;
717         }
718         d->owner=h;
719
720         if(i2o_issue_claim(I2O_CMD_UTIL_CLAIM ,d->controller,d->lct_data.tid, 
721                            I2O_CLAIM_PRIMARY))
722         {
723                 d->owner = NULL;
724                 return -EBUSY;
725         }
726         up(&i2o_configuration_lock);
727         return 0;
728 }
729
730 /**
731  *      i2o_release_device - release a device that the OSM is using
732  *      @d: device to claim
733  *      @h: handler for this device
734  *
735  *      Drop a claim by an OSM on a given I2O device. The handler is cleared
736  *      and 0 is returned on success.
737  *
738  *      AC - some devices seem to want to refuse an unclaim until they have
739  *      finished internal processing. It makes sense since you don't want a
740  *      new device to go reconfiguring the entire system until you are done.
741  *      Thus we are prepared to wait briefly.
742  */
743
744 int i2o_release_device(struct i2o_device *d, struct i2o_handler *h)
745 {
746         int err = 0;
747         int tries;
748
749         down(&i2o_configuration_lock);
750         if (d->owner != h) {
751                 printk(KERN_INFO "Claim release called, but not owned by %s!\n",
752                        h->name);
753                 up(&i2o_configuration_lock);
754                 return -ENOENT;
755         }       
756
757         for(tries=0;tries<10;tries++)
758         {
759                 d->owner = NULL;
760
761                 /*
762                  *      If the controller takes a nonblocking approach to
763                  *      releases we have to sleep/poll for a few times.
764                  */
765                  
766                 if((err=i2o_issue_claim(I2O_CMD_UTIL_RELEASE, d->controller, d->lct_data.tid, I2O_CLAIM_PRIMARY)) )
767                 {
768                         err = -ENXIO;
769                         current->state = TASK_UNINTERRUPTIBLE;
770                         schedule_timeout(HZ);
771                 }
772                 else
773                 {
774                         err=0;
775                         break;
776                 }
777         }
778         up(&i2o_configuration_lock);
779         return err;
780 }
781
782 /**
783  *      i2o_device_notify_on    -       Enable deletion notifiers
784  *      @d: device for notification
785  *      @h: handler to install
786  *
787  *      Called by OSMs to let the core know that they want to be
788  *      notified if the given device is deleted from the system.
789  */
790
791 int i2o_device_notify_on(struct i2o_device *d, struct i2o_handler *h)
792 {
793         int i;
794
795         if(d->num_managers == I2O_MAX_MANAGERS)
796                 return -ENOSPC;
797
798         for(i = 0; i < I2O_MAX_MANAGERS; i++)
799         {
800                 if(!d->managers[i])
801                 {
802                         d->managers[i] = h;
803                         break;
804                 }
805         }
806         
807         d->num_managers++;
808         
809         return 0;
810 }
811
812 /**
813  *      i2o_device_notify_off   -       Remove deletion notifiers
814  *      @d: device for notification
815  *      @h: handler to remove
816  *
817  * Called by OSMs to let the core know that they no longer
818  * are interested in the fate of the given device.
819  */
820 int i2o_device_notify_off(struct i2o_device *d, struct i2o_handler *h)
821 {
822         int i;
823
824         for(i=0; i < I2O_MAX_MANAGERS; i++)
825         {
826                 if(d->managers[i] == h)
827                 {
828                         d->managers[i] = NULL;
829                         d->num_managers--;
830                         return 0;
831                 }
832         }
833
834         return -ENOENT;
835 }
836
837 /**
838  *      i2o_event_register      -       register interest in an event
839  *      @c: Controller to register interest with
840  *      @tid: I2O task id
841  *      @init_context: initiator context to use with this notifier
842  *      @tr_context: transaction context to use with this notifier
843  *      @evt_mask: mask of events
844  *
845  *      Create and posts an event registration message to the task. No reply
846  *      is waited for, or expected. Errors in posting will be reported.
847  */
848  
849 int i2o_event_register(struct i2o_controller *c, u32 tid, 
850                 u32 init_context, u32 tr_context, u32 evt_mask)
851 {
852         u32 msg[5];     // Not performance critical, so we just 
853                         // i2o_post_this it instead of building it
854                         // in IOP memory
855         
856         msg[0] = FIVE_WORD_MSG_SIZE|SGL_OFFSET_0;
857         msg[1] = I2O_CMD_UTIL_EVT_REGISTER<<24 | HOST_TID<<12 | tid;
858         msg[2] = init_context;
859         msg[3] = tr_context;
860         msg[4] = evt_mask;
861
862         return i2o_post_this(c, msg, sizeof(msg));
863 }
864
865 /*
866  *      i2o_event_ack   -       acknowledge an event
867  *      @c: controller 
868  *      @msg: pointer to the UTIL_EVENT_REGISTER reply we received
869  *
870  *      We just take a pointer to the original UTIL_EVENT_REGISTER reply
871  *      message and change the function code since that's what spec
872  *      describes an EventAck message looking like.
873  */
874  
875 int i2o_event_ack(struct i2o_controller *c, u32 *msg)
876 {
877         struct i2o_message *m = (struct i2o_message *)msg;
878
879         m->function = I2O_CMD_UTIL_EVT_ACK;
880
881         return i2o_post_wait(c, msg, m->size * 4, 2);
882 }
883
884 /*
885  * Core event handler.  Runs as a separate thread and is woken
886  * up whenever there is an Executive class event.
887  */
888 static int i2o_core_evt(void *reply_data)
889 {
890         struct reply_info *reply = (struct reply_info *) reply_data;
891         u32 *msg = reply->msg;
892         struct i2o_controller *c = NULL;
893         int flags;
894
895         lock_kernel();
896         daemonize();
897         unlock_kernel();
898
899         strcpy(current->comm, "i2oevtd");
900         evt_running = 1;
901
902         while(1)
903         {
904                 if(down_interruptible(&evt_sem))
905                 {
906                         dprintk(KERN_INFO "I2O event thread dead\n");
907                         printk("exiting...");
908                         evt_running = 0;
909                         complete_and_exit(&evt_dead, 0);
910                 }
911
912                 /* 
913                  * Copy the data out of the queue so that we don't have to lock
914                  * around the whole function and just around the qlen update
915                  */
916                 spin_lock_irqsave(&i2o_evt_lock, flags);
917                 memcpy(reply, &events[evt_out], sizeof(struct reply_info));
918                 MODINC(evt_out, I2O_EVT_Q_LEN);
919                 evt_q_len--;
920                 spin_unlock_irqrestore(&i2o_evt_lock, flags);
921         
922                 c = reply->iop;
923                 dprintk(KERN_INFO "I2O IRTOS EVENT: iop%d, event %#10x\n", c->unit, msg[4]);
924
925                 /* 
926                  * We do not attempt to delete/quiesce/etc. the controller if
927                  * some sort of error indidication occurs.  We may want to do
928                  * so in the future, but for now we just let the user deal with 
929                  * it.  One reason for this is that what to do with an error
930                  * or when to send what ærror is not really agreed on, so
931                  * we get errors that may not be fatal but just look like they
932                  * are...so let the user deal with it.
933                  */
934                 switch(msg[4])
935                 {
936                         case I2O_EVT_IND_EXEC_RESOURCE_LIMITS:
937                                 printk(KERN_ERR "%s: Out of resources\n", c->name);
938                                 break;
939
940                         case I2O_EVT_IND_EXEC_POWER_FAIL:
941                                 printk(KERN_ERR "%s: Power failure\n", c->name);
942                                 break;
943
944                         case I2O_EVT_IND_EXEC_HW_FAIL:
945                         {
946                                 char *fail[] = 
947                                         { 
948                                                 "Unknown Error",
949                                                 "Power Lost",
950                                                 "Code Violation",
951                                                 "Parity Error",
952                                                 "Code Execution Exception",
953                                                 "Watchdog Timer Expired" 
954                                         };
955
956                                 if(msg[5] <= 6)
957                                         printk(KERN_ERR "%s: Hardware Failure: %s\n", 
958                                                 c->name, fail[msg[5]]);
959                                 else
960                                         printk(KERN_ERR "%s: Unknown Hardware Failure\n", c->name);
961
962                                 break;
963                         }
964
965                         /*
966                          * New device created
967                          * - Create a new i2o_device entry
968                          * - Inform all interested drivers about this device's existence
969                          */
970                         case I2O_EVT_IND_EXEC_NEW_LCT_ENTRY:
971                         {
972                                 struct i2o_device *d = (struct i2o_device *)
973                                         kmalloc(sizeof(struct i2o_device), GFP_KERNEL);
974                                 int i;
975
976                                 if (d == NULL) {
977                                         printk(KERN_EMERG "i2oevtd: out of memory\n");
978                                         break;
979                                 }
980                                 memcpy(&d->lct_data, &msg[5], sizeof(i2o_lct_entry));
981         
982                                 d->next = NULL;
983                                 d->controller = c;
984                                 d->flags = 0;
985         
986                                 i2o_report_controller_unit(c, d);
987                                 i2o_install_device(c,d);
988         
989                                 for(i = 0; i < MAX_I2O_MODULES; i++)
990                                 {
991                                         if(i2o_handlers[i] && 
992                                                 i2o_handlers[i]->new_dev_notify &&
993                                                 (i2o_handlers[i]->class&d->lct_data.class_id))
994                                                 {
995                                                 spin_lock(&i2o_dev_lock);
996                                                 i2o_handlers[i]->new_dev_notify(c,d);
997                                                 spin_unlock(&i2o_dev_lock);
998                                                 }
999                                 }
1000                         
1001                                 break;
1002                         }
1003         
1004                         /*
1005                          * LCT entry for a device has been modified, so update it
1006                          * internally.
1007                          */
1008                         case I2O_EVT_IND_EXEC_MODIFIED_LCT:
1009                         {
1010                                 struct i2o_device *d;
1011                                 i2o_lct_entry *new_lct = (i2o_lct_entry *)&msg[5];
1012
1013                                 for(d = c->devices; d; d = d->next)
1014                                 {
1015                                         if(d->lct_data.tid == new_lct->tid)
1016                                         {
1017                                                 memcpy(&d->lct_data, new_lct, sizeof(i2o_lct_entry));
1018                                                 break;
1019                                         }
1020                                 }
1021                                 break;
1022                         }
1023         
1024                         case I2O_EVT_IND_CONFIGURATION_FLAG:
1025                                 printk(KERN_WARNING "%s requires user configuration\n", c->name);
1026                                 break;
1027         
1028                         case I2O_EVT_IND_GENERAL_WARNING:
1029                                 printk(KERN_WARNING "%s: Warning notification received!"
1030                                         "Check configuration for errors!\n", c->name);
1031                                 break;
1032                                 
1033                         case I2O_EVT_IND_EVT_MASK_MODIFIED:
1034                                 /* Well I guess that was us hey .. */
1035                                 break;
1036                                         
1037                         default:
1038                                 printk(KERN_WARNING "%s: No handler for event (0x%08x)\n", c->name, msg[4]);
1039                                 break;
1040                 }
1041         }
1042
1043         return 0;
1044 }
1045
1046 /*
1047  * Dynamic LCT update.  This compares the LCT with the currently
1048  * installed devices to check for device deletions..this needed b/c there
1049  * is no DELETED_LCT_ENTRY EventIndicator for the Executive class so
1050  * we can't just have the event handler do this...annoying
1051  *
1052  * This is a hole in the spec that will hopefully be fixed someday.
1053  */
1054 static int i2o_dyn_lct(void *foo)
1055 {
1056         struct i2o_controller *c = (struct i2o_controller *)foo;
1057         struct i2o_device *d = NULL;
1058         struct i2o_device *d1 = NULL;
1059         int i = 0;
1060         int found = 0;
1061         int entries;
1062         void *tmp;
1063         char name[16];
1064
1065         lock_kernel();
1066         daemonize();
1067         unlock_kernel();
1068
1069         sprintf(name, "iop%d_lctd", c->unit);
1070         strcpy(current->comm, name);    
1071         
1072         c->lct_running = 1;
1073
1074         while(1)
1075         {
1076                 down_interruptible(&c->lct_sem);
1077                 if(signal_pending(current))
1078                 {
1079                         dprintk(KERN_ERR "%s: LCT thread dead\n", c->name);
1080                         c->lct_running = 0;
1081                         return 0;
1082                 }
1083
1084                 entries = c->dlct->table_size;
1085                 entries -= 3;
1086                 entries /= 9;
1087
1088                 dprintk(KERN_INFO "%s: Dynamic LCT Update\n",c->name);
1089                 dprintk(KERN_INFO "%s: Dynamic LCT contains %d entries\n", c->name, entries);
1090
1091                 if(!entries)
1092                 {
1093                         printk(KERN_INFO "%s: Empty LCT???\n", c->name);
1094                         continue;
1095                 }
1096
1097                 /*
1098                  * Loop through all the devices on the IOP looking for their
1099                  * LCT data in the LCT.  We assume that TIDs are not repeated.
1100                  * as that is the only way to really tell.  It's been confirmed
1101                  * by the IRTOS vendor(s?) that TIDs are not reused until they 
1102                  * wrap arround(4096), and I doubt a system will up long enough
1103                  * to create/delete that many devices.
1104                  */
1105                 for(d = c->devices; d; )
1106                 {
1107                         found = 0;
1108                         d1 = d->next;
1109                         
1110                         for(i = 0; i < entries; i++) 
1111                         { 
1112                                 if(d->lct_data.tid == c->dlct->lct_entry[i].tid) 
1113                                 { 
1114                                         found = 1; 
1115                                         break; 
1116                                 } 
1117                         } 
1118                         if(!found) 
1119                         {
1120                                 dprintk(KERN_INFO "i2o_core: Deleted device!\n"); 
1121                                 spin_lock(&i2o_dev_lock);
1122                                 i2o_delete_device(d); 
1123                                 spin_unlock(&i2o_dev_lock);
1124                         } 
1125                         d = d1; 
1126                 }
1127
1128                 /* 
1129                  * Tell LCT to renotify us next time there is a change
1130                  */
1131                 i2o_lct_notify(c);
1132
1133                 /*
1134                  * Copy new LCT into public LCT
1135                  *
1136                  * Possible race if someone is reading LCT while  we are copying 
1137                  * over it. If this happens, we'll fix it then. but I doubt that
1138                  * the LCT will get updated often enough or will get read by
1139                  * a user often enough to worry.
1140                  */
1141                 if(c->lct->table_size < c->dlct->table_size)
1142                 {
1143                         tmp = c->lct;
1144                         c->lct = kmalloc(c->dlct->table_size<<2, GFP_KERNEL);
1145                         if(!c->lct)
1146                         {
1147                                 printk(KERN_ERR "%s: No memory for LCT!\n", c->name);
1148                                 c->lct = tmp;
1149                                 continue;
1150                         }
1151                         kfree(tmp);
1152                 }
1153                 memcpy(c->lct, c->dlct, c->dlct->table_size<<2);
1154         }
1155
1156         return 0;
1157 }
1158
1159 /**
1160  *      i2o_run_queue   -       process pending events on a controller
1161  *      @c: controller to process
1162  *
1163  *      This is called by the bus specific driver layer when an interrupt
1164  *      or poll of this card interface is desired.
1165  */
1166  
1167 void i2o_run_queue(struct i2o_controller *c)
1168 {
1169         struct i2o_message *m;
1170         u32 mv;
1171         u32 *msg;
1172
1173         /*
1174          * Old 960 steppings had a bug in the I2O unit that caused
1175          * the queue to appear empty when it wasn't.
1176          */
1177         if((mv=I2O_REPLY_READ32(c))==0xFFFFFFFF)
1178                 mv=I2O_REPLY_READ32(c);
1179
1180         while(mv!=0xFFFFFFFF)
1181         {
1182                 struct i2o_handler *i;
1183                 m=(struct i2o_message *)bus_to_virt(mv);
1184                 msg=(u32*)m;
1185
1186                 i=i2o_handlers[m->initiator_context&(MAX_I2O_MODULES-1)];
1187                 if(i && i->reply)
1188                         i->reply(i,c,m);
1189                 else
1190                 {
1191                         printk(KERN_WARNING "I2O: Spurious reply to handler %d\n", 
1192                                 m->initiator_context&(MAX_I2O_MODULES-1));
1193                 }       
1194                 i2o_flush_reply(c,mv);
1195                 mb();
1196
1197                 /* That 960 bug again... */     
1198                 if((mv=I2O_REPLY_READ32(c))==0xFFFFFFFF)
1199                         mv=I2O_REPLY_READ32(c);
1200         }               
1201 }
1202
1203
1204 /**
1205  *      i2o_get_class_name -    do i2o class name lookup
1206  *      @class: class number
1207  *
1208  *      Return a descriptive string for an i2o class
1209  */
1210  
1211 const char *i2o_get_class_name(int class)
1212 {
1213         int idx = 16;
1214         static char *i2o_class_name[] = {
1215                 "Executive",
1216                 "Device Driver Module",
1217                 "Block Device",
1218                 "Tape Device",
1219                 "LAN Interface",
1220                 "WAN Interface",
1221                 "Fibre Channel Port",
1222                 "Fibre Channel Device",
1223                 "SCSI Device",
1224                 "ATE Port",
1225                 "ATE Device",
1226                 "Floppy Controller",
1227                 "Floppy Device",
1228                 "Secondary Bus Port",
1229                 "Peer Transport Agent",
1230                 "Peer Transport",
1231                 "Unknown"
1232         };
1233         
1234         switch(class&0xFFF)
1235         {
1236                 case I2O_CLASS_EXECUTIVE:
1237                         idx = 0; break;
1238                 case I2O_CLASS_DDM:
1239                         idx = 1; break;
1240                 case I2O_CLASS_RANDOM_BLOCK_STORAGE:
1241                         idx = 2; break;
1242                 case I2O_CLASS_SEQUENTIAL_STORAGE:
1243                         idx = 3; break;
1244                 case I2O_CLASS_LAN:
1245                         idx = 4; break;
1246                 case I2O_CLASS_WAN:
1247                         idx = 5; break;
1248                 case I2O_CLASS_FIBRE_CHANNEL_PORT:
1249                         idx = 6; break;
1250                 case I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL:
1251                         idx = 7; break;
1252                 case I2O_CLASS_SCSI_PERIPHERAL:
1253                         idx = 8; break;
1254                 case I2O_CLASS_ATE_PORT:
1255                         idx = 9; break;
1256                 case I2O_CLASS_ATE_PERIPHERAL:
1257                         idx = 10; break;
1258                 case I2O_CLASS_FLOPPY_CONTROLLER:
1259                         idx = 11; break;
1260                 case I2O_CLASS_FLOPPY_DEVICE:
1261                         idx = 12; break;
1262                 case I2O_CLASS_BUS_ADAPTER_PORT:
1263                         idx = 13; break;
1264                 case I2O_CLASS_PEER_TRANSPORT_AGENT:
1265                         idx = 14; break;
1266                 case I2O_CLASS_PEER_TRANSPORT:
1267                         idx = 15; break;
1268         }
1269
1270         return i2o_class_name[idx];
1271 }
1272
1273
1274 /**
1275  *      i2o_wait_message        -       obtain an i2o message from the IOP
1276  *      @c: controller
1277  *      @why: explanation 
1278  *
1279  *      This function waits up to 5 seconds for a message slot to be
1280  *      available. If no message is available it prints an error message
1281  *      that is expected to be what the message will be used for (eg
1282  *      "get_status"). 0xFFFFFFFF is returned on a failure.
1283  *
1284  *      On a success the message is returned. This is the physical page
1285  *      frame offset address from the read port. (See the i2o spec)
1286  */
1287  
1288 u32 i2o_wait_message(struct i2o_controller *c, char *why)
1289 {
1290         long time=jiffies;
1291         u32 m;
1292         while((m=I2O_POST_READ32(c))==0xFFFFFFFF)
1293         {
1294                 if((jiffies-time)>=5*HZ)
1295                 {
1296                         dprintk(KERN_ERR "%s: Timeout waiting for message frame to send %s.\n", 
1297                                 c->name, why);
1298                         return 0xFFFFFFFF;
1299                 }
1300                 schedule();
1301                 barrier();
1302         }
1303         return m;
1304 }
1305         
1306 /**
1307  *      i2o_report_controller_unit - print information about a tid
1308  *      @c: controller
1309  *      @d: device
1310  *      
1311  *      Dump an information block associated with a given unit (TID). The
1312  *      tables are read and a block of text is output to printk that is
1313  *      formatted intended for the user.
1314  */
1315  
1316 void i2o_report_controller_unit(struct i2o_controller *c, struct i2o_device *d)
1317 {
1318         char buf[64];
1319         char str[22];
1320         int ret;
1321         int unit = d->lct_data.tid;
1322
1323         if(verbose==0)
1324                 return;
1325                 
1326         printk(KERN_INFO "Target ID %d.\n", unit);
1327         if((ret=i2o_query_scalar(c, unit, 0xF100, 3, buf, 16))>=0)
1328         {
1329                 buf[16]=0;
1330                 printk(KERN_INFO "     Vendor: %s\n", buf);
1331         }
1332         if((ret=i2o_query_scalar(c, unit, 0xF100, 4, buf, 16))>=0)
1333         {
1334                 buf[16]=0;
1335                 printk(KERN_INFO "     Device: %s\n", buf);
1336         }
1337         if(i2o_query_scalar(c, unit, 0xF100, 5, buf, 16)>=0)
1338         {
1339                 buf[16]=0;
1340                 printk(KERN_INFO "     Description: %s\n", buf);
1341         }
1342         if((ret=i2o_query_scalar(c, unit, 0xF100, 6, buf, 8))>=0)
1343         {
1344                 buf[8]=0;
1345                 printk(KERN_INFO "        Rev: %s\n", buf);
1346         }
1347
1348         printk(KERN_INFO "    Class: ");
1349         sprintf(str, "%-21s", i2o_get_class_name(d->lct_data.class_id));
1350         printk("%s\n", str);
1351                 
1352         printk(KERN_INFO "  Subclass: 0x%04X\n", d->lct_data.sub_class);
1353         printk(KERN_INFO "     Flags: ");
1354                 
1355         if(d->lct_data.device_flags&(1<<0))
1356                 printk("C");            // ConfigDialog requested
1357         if(d->lct_data.device_flags&(1<<1))
1358                 printk("U");            // Multi-user capable
1359         if(!(d->lct_data.device_flags&(1<<4)))
1360                 printk("P");            // Peer service enabled!
1361         if(!(d->lct_data.device_flags&(1<<5)))
1362                 printk("M");            // Mgmt service enabled!
1363         printk("\n");
1364                         
1365 }
1366
1367
1368 /*
1369  *      Parse the hardware resource table. Right now we print it out
1370  *      and don't do a lot with it. We should collate these and then
1371  *      interact with the Linux resource allocation block.
1372  *
1373  *      Lets prove we can read it first eh ?
1374  *
1375  *      This is full of endianisms!
1376  */
1377  
1378 static int i2o_parse_hrt(struct i2o_controller *c)
1379 {
1380 #ifdef DRIVERDEBUG
1381         u32 *rows=(u32*)c->hrt;
1382         u8 *p=(u8 *)c->hrt;
1383         u8 *d;
1384         int count;
1385         int length;
1386         int i;
1387         int state;
1388         
1389         if(p[3]!=0)
1390         {
1391                 printk(KERN_ERR "%s: HRT table for controller is too new a version.\n",
1392                         c->name);
1393                 return -1;
1394         }
1395                 
1396         count=p[0]|(p[1]<<8);
1397         length = p[2];
1398         
1399         printk(KERN_INFO "%s: HRT has %d entries of %d bytes each.\n",
1400                 c->name, count, length<<2);
1401
1402         rows+=2;
1403         
1404         for(i=0;i<count;i++)
1405         {
1406                 printk(KERN_INFO "Adapter %08X: ", rows[0]);
1407                 p=(u8 *)(rows+1);
1408                 d=(u8 *)(rows+2);
1409                 state=p[1]<<8|p[0];
1410                 
1411                 printk("TID %04X:[", state&0xFFF);
1412                 state>>=12;
1413                 if(state&(1<<0))
1414                         printk("H");            /* Hidden */
1415                 if(state&(1<<2))
1416                 {
1417                         printk("P");            /* Present */
1418                         if(state&(1<<1))
1419                                 printk("C");    /* Controlled */
1420                 }
1421                 if(state>9)
1422                         printk("*");            /* Hard */
1423                 
1424                 printk("]:");
1425                 
1426                 switch(p[3]&0xFFFF)
1427                 {
1428                         case 0:
1429                                 /* Adapter private bus - easy */
1430                                 printk("Local bus %d: I/O at 0x%04X Mem 0x%08X", 
1431                                         p[2], d[1]<<8|d[0], *(u32 *)(d+4));
1432                                 break;
1433                         case 1:
1434                                 /* ISA bus */
1435                                 printk("ISA %d: CSN %d I/O at 0x%04X Mem 0x%08X",
1436                                         p[2], d[2], d[1]<<8|d[0], *(u32 *)(d+4));
1437                                 break;
1438                                         
1439                         case 2: /* EISA bus */
1440                                 printk("EISA %d: Slot %d I/O at 0x%04X Mem 0x%08X",
1441                                         p[2], d[3], d[1]<<8|d[0], *(u32 *)(d+4));
1442                                 break;
1443
1444                         case 3: /* MCA bus */
1445                                 printk("MCA %d: Slot %d I/O at 0x%04X Mem 0x%08X",
1446                                         p[2], d[3], d[1]<<8|d[0], *(u32 *)(d+4));
1447                                 break;
1448
1449                         case 4: /* PCI bus */
1450                                 printk("PCI %d: Bus %d Device %d Function %d",
1451                                         p[2], d[2], d[1], d[0]);
1452                                 break;
1453
1454                         case 0x80: /* Other */
1455                         default:
1456                                 printk("Unsupported bus type.");
1457                                 break;
1458                 }
1459                 printk("\n");
1460                 rows+=length;
1461         }
1462 #endif
1463         return 0;
1464 }
1465         
1466 /*
1467  *      The logical configuration table tells us what we can talk to
1468  *      on the board. Most of the stuff isn't interesting to us. 
1469  */
1470
1471 static int i2o_parse_lct(struct i2o_controller *c)
1472 {
1473         int i;
1474         int max;
1475         int tid;
1476         struct i2o_device *d;
1477         i2o_lct *lct = c->lct;
1478
1479         if (lct == NULL) {
1480                 printk(KERN_ERR "%s: LCT is empty???\n", c->name);
1481                 return -1;
1482         }
1483
1484         max = lct->table_size;
1485         max -= 3;
1486         max /= 9;
1487         
1488         printk(KERN_INFO "%s: LCT has %d entries.\n", c->name, max);
1489         
1490         if(lct->iop_flags&(1<<0))
1491                 printk(KERN_WARNING "%s: Configuration dialog desired.\n", c->name);
1492                 
1493         for(i=0;i<max;i++)
1494         {
1495                 d = (struct i2o_device *)kmalloc(sizeof(struct i2o_device), GFP_KERNEL);
1496                 if(d==NULL)
1497                 {
1498                         printk(KERN_CRIT "i2o_core: Out of memory for I2O device data.\n");
1499                         return -ENOMEM;
1500                 }
1501                 
1502                 d->controller = c;
1503                 d->next = NULL;
1504
1505                 memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
1506
1507                 d->flags = 0;
1508                 tid = d->lct_data.tid;
1509                 
1510                 i2o_report_controller_unit(c, d);
1511                 
1512                 i2o_install_device(c, d);
1513         }
1514         return 0;
1515 }
1516
1517
1518 /**
1519  *      i2o_quiesce_controller - quiesce controller
1520  *      @c: controller 
1521  *
1522  *      Quiesce an IOP. Causes IOP to make external operation quiescent
1523  *      (i2o 'READY' state). Internal operation of the IOP continues normally.
1524  */
1525  
1526 int i2o_quiesce_controller(struct i2o_controller *c)
1527 {
1528         u32 msg[4];
1529         int ret;
1530
1531         i2o_status_get(c);
1532
1533         /* SysQuiesce discarded if IOP not in READY or OPERATIONAL state */
1534
1535         if ((c->status_block->iop_state != ADAPTER_STATE_READY) &&
1536                 (c->status_block->iop_state != ADAPTER_STATE_OPERATIONAL))
1537         {
1538                 return 0;
1539         }
1540
1541         msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
1542         msg[1] = I2O_CMD_SYS_QUIESCE<<24|HOST_TID<<12|ADAPTER_TID;
1543         msg[3] = 0;
1544
1545         /* Long timeout needed for quiesce if lots of devices */
1546
1547         if ((ret = i2o_post_wait(c, msg, sizeof(msg), 240)))
1548                 printk(KERN_INFO "%s: Unable to quiesce (status=%#x).\n",
1549                         c->name, -ret);
1550         else
1551                 dprintk(KERN_INFO "%s: Quiesced.\n", c->name);
1552
1553         i2o_status_get(c); // Entered READY state
1554         return ret;
1555 }
1556
1557 /**
1558  *      i2o_enable_controller - move controller from ready to operational
1559  *      @c: controller
1560  *
1561  *      Enable IOP. This allows the IOP to resume external operations and
1562  *      reverses the effect of a quiesce. In the event of an error a negative
1563  *      errno code is returned.
1564  */
1565  
1566 int i2o_enable_controller(struct i2o_controller *c)
1567 {
1568         u32 msg[4];
1569         int ret;
1570
1571         i2o_status_get(c);
1572         
1573         /* Enable only allowed on READY state */        
1574         if(c->status_block->iop_state != ADAPTER_STATE_READY)
1575                 return -EINVAL;
1576
1577         msg[0]=FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
1578         msg[1]=I2O_CMD_SYS_ENABLE<<24|HOST_TID<<12|ADAPTER_TID;
1579
1580         /* How long of a timeout do we need? */
1581
1582         if ((ret = i2o_post_wait(c, msg, sizeof(msg), 240)))
1583                 printk(KERN_ERR "%s: Could not enable (status=%#x).\n",
1584                         c->name, -ret);
1585         else
1586                 dprintk(KERN_INFO "%s: Enabled.\n", c->name);
1587
1588         i2o_status_get(c); // entered OPERATIONAL state
1589
1590         return ret;
1591 }
1592
1593 /**
1594  *      i2o_clear_controller    -       clear a controller
1595  *      @c: controller
1596  *
1597  *      Clear an IOP to HOLD state, ie. terminate external operations, clear all
1598  *      input queues and prepare for a system restart. IOP's internal operation
1599  *      continues normally and the outbound queue is alive.
1600  *      The IOP is not expected to rebuild its LCT.
1601  */
1602  
1603 int i2o_clear_controller(struct i2o_controller *c)
1604 {
1605         struct i2o_controller *iop;
1606         u32 msg[4];
1607         int ret;
1608
1609         /* Quiesce all IOPs first */
1610
1611         for (iop = i2o_controller_chain; iop; iop = iop->next)
1612                 i2o_quiesce_controller(iop);
1613
1614         msg[0]=FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
1615         msg[1]=I2O_CMD_ADAPTER_CLEAR<<24|HOST_TID<<12|ADAPTER_TID;
1616         msg[3]=0;
1617
1618         if ((ret=i2o_post_wait(c, msg, sizeof(msg), 30)))
1619                 printk(KERN_INFO "%s: Unable to clear (status=%#x).\n",
1620                         c->name, -ret);
1621         else
1622                 dprintk(KERN_INFO "%s: Cleared.\n",c->name);
1623
1624         i2o_status_get(c);
1625
1626         /* Enable other IOPs */
1627
1628         for (iop = i2o_controller_chain; iop; iop = iop->next)
1629                 if (iop != c)
1630                         i2o_enable_controller(iop);
1631
1632         return ret;
1633 }
1634
1635
1636 /**
1637  *      i2o_reset_controller    -       reset an IOP
1638  *      @c: controller to reset
1639  *
1640  *      Reset the IOP into INIT state and wait until IOP gets into RESET state.
1641  *      Terminate all external operations, clear IOP's inbound and outbound
1642  *      queues, terminate all DDMs, and reload the IOP's operating environment
1643  *      and all local DDMs. The IOP rebuilds its LCT.
1644  */
1645  
1646 static int i2o_reset_controller(struct i2o_controller *c)
1647 {
1648         struct i2o_controller *iop;
1649         u32 m;
1650         u8 *status;
1651         u32 *msg;
1652         long time;
1653
1654         /* Quiesce all IOPs first */
1655
1656         for (iop = i2o_controller_chain; iop; iop = iop->next)
1657         {
1658                 if(iop->type != I2O_TYPE_PCI || !iop->bus.pci.dpt)
1659                         i2o_quiesce_controller(iop);
1660         }
1661
1662         m=i2o_wait_message(c, "AdapterReset");
1663         if(m==0xFFFFFFFF)       
1664                 return -ETIMEDOUT;
1665         msg=(u32 *)(c->mem_offset+m);
1666         
1667         status=(void *)kmalloc(4, GFP_KERNEL);
1668         if(status==NULL) {
1669                 printk(KERN_ERR "IOP reset failed - no free memory.\n");
1670                 return -ENOMEM;
1671         }
1672         memset(status, 0, 4);
1673         
1674         msg[0]=EIGHT_WORD_MSG_SIZE|SGL_OFFSET_0;
1675         msg[1]=I2O_CMD_ADAPTER_RESET<<24|HOST_TID<<12|ADAPTER_TID;
1676         msg[2]=core_context;
1677         msg[3]=0;
1678         msg[4]=0;
1679         msg[5]=0;
1680         msg[6]=virt_to_bus(status);
1681         msg[7]=0;       /* 64bit host FIXME */
1682
1683         i2o_post_message(c,m);
1684
1685         /* Wait for a reply */
1686         time=jiffies;
1687         while(*status==0)
1688         {
1689                 if((jiffies-time)>=20*HZ)
1690                 {
1691                         printk(KERN_ERR "IOP reset timeout.\n");
1692                         // Better to leak this for safety: kfree(status);
1693                         return -ETIMEDOUT;
1694                 }
1695                 schedule();
1696                 barrier();
1697         }
1698
1699         if (*status==I2O_CMD_IN_PROGRESS)
1700         { 
1701                 /* 
1702                  * Once the reset is sent, the IOP goes into the INIT state 
1703                  * which is indeterminate.  We need to wait until the IOP 
1704                  * has rebooted before we can let the system talk to 
1705                  * it. We read the inbound Free_List until a message is 
1706                  * available.  If we can't read one in the given ammount of 
1707                  * time, we assume the IOP could not reboot properly.  
1708                  */ 
1709
1710                 dprintk(KERN_INFO "%s: Reset in progress, waiting for reboot...\n",
1711                         c->name); 
1712
1713                 time = jiffies; 
1714                 m = I2O_POST_READ32(c); 
1715                 while(m == 0XFFFFFFFF) 
1716                 { 
1717                         if((jiffies-time) >= 30*HZ)
1718                         {
1719                                 printk(KERN_ERR "%s: Timeout waiting for IOP reset.\n", 
1720                                                 c->name); 
1721                                 return -ETIMEDOUT; 
1722                         } 
1723                         schedule(); 
1724                         barrier(); 
1725                         m = I2O_POST_READ32(c); 
1726                 }
1727                 i2o_flush_reply(c,m);
1728         }
1729
1730         /* If IopReset was rejected or didn't perform reset, try IopClear */
1731
1732         i2o_status_get(c);
1733         if (status[0] == I2O_CMD_REJECTED || 
1734                 c->status_block->iop_state != ADAPTER_STATE_RESET)
1735         {
1736                 printk(KERN_WARNING "%s: Reset rejected, trying to clear\n",c->name);
1737                 i2o_clear_controller(c);
1738         }
1739         else
1740                 dprintk(KERN_INFO "%s: Reset completed.\n", c->name);
1741
1742         /* Enable other IOPs */
1743
1744         for (iop = i2o_controller_chain; iop; iop = iop->next)
1745                 if (iop != c)
1746                         i2o_enable_controller(iop);
1747
1748         kfree(status);
1749         return 0;
1750 }
1751
1752
1753 /**
1754  *      i2o_status_get  -       get the status block for the IOP
1755  *      @c: controller
1756  *
1757  *      Issue a status query on the controller. This updates the
1758  *      attached status_block. If the controller fails to reply or an
1759  *      error occurs then a negative errno code is returned. On success
1760  *      zero is returned and the status_blok is updated.
1761  */
1762  
1763 int i2o_status_get(struct i2o_controller *c)
1764 {
1765         long time;
1766         u32 m;
1767         u32 *msg;
1768         u8 *status_block;
1769
1770         if (c->status_block == NULL) 
1771         {
1772                 c->status_block = (i2o_status_block *)
1773                         kmalloc(sizeof(i2o_status_block),GFP_KERNEL);
1774                 if (c->status_block == NULL)
1775                 {
1776                         printk(KERN_CRIT "%s: Get Status Block failed; Out of memory.\n",
1777                                 c->name);
1778                         return -ENOMEM;
1779                 }
1780         }
1781
1782         status_block = (u8*)c->status_block;
1783         memset(c->status_block,0,sizeof(i2o_status_block));
1784         
1785         m=i2o_wait_message(c, "StatusGet");
1786         if(m==0xFFFFFFFF)
1787                 return -ETIMEDOUT;      
1788         msg=(u32 *)(c->mem_offset+m);
1789
1790         msg[0]=NINE_WORD_MSG_SIZE|SGL_OFFSET_0;
1791         msg[1]=I2O_CMD_STATUS_GET<<24|HOST_TID<<12|ADAPTER_TID;
1792         msg[2]=core_context;
1793         msg[3]=0;
1794         msg[4]=0;
1795         msg[5]=0;
1796         msg[6]=virt_to_bus(c->status_block);
1797         msg[7]=0;   /* 64bit host FIXME */
1798         msg[8]=sizeof(i2o_status_block); /* always 88 bytes */
1799
1800         i2o_post_message(c,m);
1801
1802         /* Wait for a reply */
1803
1804         time=jiffies;
1805         while(status_block[87]!=0xFF)
1806         {
1807                 if((jiffies-time)>=5*HZ)
1808                 {
1809                         printk(KERN_ERR "%s: Get status timeout.\n",c->name);
1810                         return -ETIMEDOUT;
1811                 }
1812                 schedule();
1813                 barrier();
1814         }
1815
1816 #ifdef DRIVERDEBUG
1817         printk(KERN_INFO "%s: State = ", c->name);
1818         switch (c->status_block->iop_state) {
1819                 case 0x01:  
1820                         printk("INIT\n");
1821                         break;
1822                 case 0x02:
1823                         printk("RESET\n");
1824                         break;
1825                 case 0x04:
1826                         printk("HOLD\n");
1827                         break;
1828                 case 0x05:
1829                         printk("READY\n");
1830                         break;
1831                 case 0x08:
1832                         printk("OPERATIONAL\n");
1833                         break;
1834                 case 0x10:
1835                         printk("FAILED\n");
1836                         break;
1837                 case 0x11:
1838                         printk("FAULTED\n");
1839                         break;
1840                 default: 
1841                         printk("%x (unknown !!)\n",c->status_block->iop_state);
1842 }     
1843 #endif   
1844
1845         return 0;
1846 }
1847
1848 /*
1849  * Get the Hardware Resource Table for the device.
1850  * The HRT contains information about possible hidden devices
1851  * but is mostly useless to us 
1852  */
1853 int i2o_hrt_get(struct i2o_controller *c)
1854 {
1855         u32 msg[6];
1856         int ret, size = sizeof(i2o_hrt);
1857
1858         /* First read just the header to figure out the real size */
1859
1860         do  {
1861                 if (c->hrt == NULL) {
1862                         c->hrt=kmalloc(size, GFP_KERNEL);
1863                         if (c->hrt == NULL) {
1864                                 printk(KERN_CRIT "%s: Hrt Get failed; Out of memory.\n", c->name);
1865                                 return -ENOMEM;
1866                         }
1867                 }
1868
1869                 msg[0]= SIX_WORD_MSG_SIZE| SGL_OFFSET_4;
1870                 msg[1]= I2O_CMD_HRT_GET<<24 | HOST_TID<<12 | ADAPTER_TID;
1871                 msg[3]= 0;
1872                 msg[4]= (0xD0000000 | size);    /* Simple transaction */
1873                 msg[5]= virt_to_bus(c->hrt);    /* Dump it here */
1874
1875                 ret = i2o_post_wait_mem(c, msg, sizeof(msg), 20, c->hrt, NULL);
1876                 
1877                 if(ret == -ETIMEDOUT)
1878                 {
1879                         /* The HRT block we used is in limbo somewhere. When the iop wakes up
1880                            we will recover it */
1881                         c->hrt = NULL;
1882                         return ret;
1883                 }
1884                 
1885                 if(ret<0)
1886                 {
1887                         printk(KERN_ERR "%s: Unable to get HRT (status=%#x)\n",
1888                                 c->name, -ret); 
1889                         return ret;
1890                 }
1891
1892                 if (c->hrt->num_entries * c->hrt->entry_len << 2 > size) {
1893                         size = c->hrt->num_entries * c->hrt->entry_len << 2;
1894                         kfree(c->hrt);
1895                         c->hrt = NULL;
1896                 }
1897         } while (c->hrt == NULL);
1898
1899         i2o_parse_hrt(c); // just for debugging
1900
1901         return 0;
1902 }
1903
1904 /*
1905  * Send the I2O System Table to the specified IOP
1906  *
1907  * The system table contains information about all the IOPs in the
1908  * system.  It is build and then sent to each IOP so that IOPs can
1909  * establish connections between each other.
1910  *
1911  */
1912 static int i2o_systab_send(struct i2o_controller *iop)
1913 {
1914         u32 msg[12];
1915         int ret;
1916         u32 *privbuf = kmalloc(16, GFP_KERNEL);
1917         if(privbuf == NULL)
1918                 return -ENOMEM;
1919         
1920         if(iop->type == I2O_TYPE_PCI)
1921         {
1922                 struct resource *root;
1923                 
1924                 if(iop->status_block->current_mem_size < iop->status_block->desired_mem_size)
1925                 {
1926                         struct resource *res = &iop->mem_resource;
1927                         res->name = iop->bus.pci.pdev->bus->name;
1928                         res->flags = IORESOURCE_MEM;
1929                         res->start = 0;
1930                         res->end = 0;
1931                         printk("%s: requires private memory resources.\n", iop->name);
1932                         root = pci_find_parent_resource(iop->bus.pci.pdev, res);
1933                         if(root==NULL)
1934                                 printk("Can't find parent resource!\n");
1935                         if(root && allocate_resource(root, res, 
1936                                         iop->status_block->desired_mem_size,
1937                                         iop->status_block->desired_mem_size,
1938                                         iop->status_block->desired_mem_size,
1939                                         1<<20,  /* Unspecified, so use 1Mb and play safe */
1940                                         NULL,
1941                                         NULL)>=0)
1942                         {
1943                                 iop->mem_alloc = 1;
1944                                 iop->status_block->current_mem_size = 1 + res->end - res->start;
1945                                 iop->status_block->current_mem_base = res->start;
1946                                 printk(KERN_INFO "%s: allocated %ld bytes of PCI memory at 0x%08lX.\n", 
1947                                         iop->name, 1+res->end-res->start, res->start);
1948                         }
1949                 }
1950                 if(iop->status_block->current_io_size < iop->status_block->desired_io_size)
1951                 {
1952                         struct resource *res = &iop->io_resource;
1953                         res->name = iop->bus.pci.pdev->bus->name;
1954                         res->flags = IORESOURCE_IO;
1955                         res->start = 0;
1956                         res->end = 0;
1957                         printk("%s: requires private memory resources.\n", iop->name);
1958                         root = pci_find_parent_resource(iop->bus.pci.pdev, res);
1959                         if(root==NULL)
1960                                 printk("Can't find parent resource!\n");
1961                         if(root &&  allocate_resource(root, res, 
1962                                         iop->status_block->desired_io_size,
1963                                         iop->status_block->desired_io_size,
1964                                         iop->status_block->desired_io_size,
1965                                         1<<20,  /* Unspecified, so use 1Mb and play safe */
1966                                         NULL,
1967                                         NULL)>=0)
1968                         {
1969                                 iop->io_alloc = 1;
1970                                 iop->status_block->current_io_size = 1 + res->end - res->start;
1971                                 iop->status_block->current_mem_base = res->start;
1972                                 printk(KERN_INFO "%s: allocated %ld bytes of PCI I/O at 0x%08lX.\n", 
1973                                         iop->name, 1+res->end-res->start, res->start);
1974                         }
1975                 }
1976         }
1977         else
1978         {       
1979                 privbuf[0] = iop->status_block->current_mem_base;
1980                 privbuf[1] = iop->status_block->current_mem_size;
1981                 privbuf[2] = iop->status_block->current_io_base;
1982                 privbuf[3] = iop->status_block->current_io_size;
1983         }
1984
1985         msg[0] = I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6;
1986         msg[1] = I2O_CMD_SYS_TAB_SET<<24 | HOST_TID<<12 | ADAPTER_TID;
1987         msg[3] = 0;
1988         msg[4] = (0<<16) | ((iop->unit+2) << 12); /* Host 0 IOP ID (unit + 2) */
1989         msg[5] = 0;                               /* Segment 0 */
1990
1991         /* 
1992          * Provide three SGL-elements:
1993          * System table (SysTab), Private memory space declaration and 
1994          * Private i/o space declaration  
1995          *
1996          * FIXME: provide these for controllers needing them
1997          */
1998         msg[6] = 0x54000000 | sys_tbl_len;
1999         msg[7] = virt_to_bus(sys_tbl);
2000         msg[8] = 0x54000000 | 8;
2001         msg[9] = virt_to_bus(privbuf);
2002         msg[10] = 0xD4000000 | 8;
2003         msg[11] = virt_to_bus(privbuf+2);
2004
2005         ret=i2o_post_wait_mem(iop, msg, sizeof(msg), 120, privbuf, NULL);
2006         
2007         if(ret==-ETIMEDOUT)
2008         {
2009                 printk(KERN_ERR "%s: SysTab setup timed out.\n", iop->name);
2010         }
2011         else if(ret<0)
2012         {
2013                 printk(KERN_ERR "%s: Unable to set SysTab (status=%#x).\n", 
2014                         iop->name, -ret);
2015                 kfree(privbuf);
2016         }
2017         else
2018         {
2019                 dprintk(KERN_INFO "%s: SysTab set.\n", iop->name);
2020                 kfree(privbuf);
2021         }
2022         i2o_status_get(iop); // Entered READY state
2023
2024         return ret;     
2025
2026  }
2027
2028 /*
2029  * Initialize I2O subsystem.
2030  */
2031 static void __init i2o_sys_init(void)
2032 {
2033         struct i2o_controller *iop, *niop = NULL;
2034
2035         printk(KERN_INFO "Activating I2O controllers...\n");
2036         printk(KERN_INFO "This may take a few minutes if there are many devices\n");
2037         
2038         /* In INIT state, Activate IOPs */
2039         for (iop = i2o_controller_chain; iop; iop = niop) {
2040                 dprintk(KERN_INFO "Calling i2o_activate_controller for %s...\n", 
2041                         iop->name);
2042                 niop = iop->next;
2043                 if (i2o_activate_controller(iop) < 0)
2044                         i2o_delete_controller(iop);
2045         }
2046
2047         /* Active IOPs in HOLD state */
2048
2049 rebuild_sys_tab:
2050         if (i2o_controller_chain == NULL)
2051                 return;
2052
2053         /*
2054          * If build_sys_table fails, we kill everything and bail
2055          * as we can't init the IOPs w/o a system table
2056          */     
2057         dprintk(KERN_INFO "i2o_core: Calling i2o_build_sys_table...\n");
2058         if (i2o_build_sys_table() < 0) {
2059                 i2o_sys_shutdown();
2060                 return;
2061         }
2062
2063         /* If IOP don't get online, we need to rebuild the System table */
2064         for (iop = i2o_controller_chain; iop; iop = niop) {
2065                 niop = iop->next;
2066                 dprintk(KERN_INFO "Calling i2o_online_controller for %s...\n", iop->name);
2067                 if (i2o_online_controller(iop) < 0) {
2068                         i2o_delete_controller(iop);     
2069                         goto rebuild_sys_tab;
2070                 }
2071         }
2072         
2073         /* Active IOPs now in OPERATIONAL state */
2074
2075         /*
2076          * Register for status updates from all IOPs
2077          */
2078         for(iop = i2o_controller_chain; iop; iop=iop->next) {
2079
2080                 /* Create a kernel thread to deal with dynamic LCT updates */
2081                 iop->lct_pid = kernel_thread(i2o_dyn_lct, iop, CLONE_SIGHAND);
2082         
2083                 /* Update change ind on DLCT */
2084                 iop->dlct->change_ind = iop->lct->change_ind;
2085
2086                 /* Start dynamic LCT updates */
2087                 i2o_lct_notify(iop);
2088
2089                 /* Register for all events from IRTOS */
2090                 i2o_event_register(iop, core_context, 0, 0, 0xFFFFFFFF);
2091         }
2092 }
2093
2094 /**
2095  *      i2o_sys_shutdown - shutdown I2O system
2096  *
2097  *      Bring down each i2o controller and then return. Each controller
2098  *      is taken through an orderly shutdown
2099  */
2100  
2101 static void i2o_sys_shutdown(void)
2102 {
2103         struct i2o_controller *iop, *niop;
2104
2105         /* Delete all IOPs from the controller chain */
2106         /* that will reset all IOPs too */
2107
2108         for (iop = i2o_controller_chain; iop; iop = niop) {
2109                 niop = iop->next;
2110                 i2o_delete_controller(iop);
2111         }
2112 }
2113
2114 /**
2115  *      i2o_activate_controller -       bring controller up to HOLD
2116  *      @iop: controller
2117  *
2118  *      This function brings an I2O controller into HOLD state. The adapter
2119  *      is reset if neccessary and then the queues and resource table
2120  *      are read. -1 is returned on a failure, 0 on success.
2121  *      
2122  */
2123  
2124 int i2o_activate_controller(struct i2o_controller *iop)
2125 {
2126         /* In INIT state, Wait Inbound Q to initialize (in i2o_status_get) */
2127         /* In READY state, Get status */
2128
2129         if (i2o_status_get(iop) < 0) {
2130                 printk(KERN_INFO "Unable to obtain status of %s, "
2131                         "attempting a reset.\n", iop->name);
2132                 if (i2o_reset_controller(iop) < 0)
2133                         return -1;
2134         }
2135
2136         if(iop->status_block->iop_state == ADAPTER_STATE_FAULTED) {
2137                 printk(KERN_CRIT "%s: hardware fault\n", iop->name);
2138                 return -1;
2139         }
2140
2141         if (iop->status_block->i2o_version > I2OVER15) {
2142                 printk(KERN_ERR "%s: Not running vrs. 1.5. of the I2O Specification.\n",
2143                         iop->name);
2144                 return -1;
2145         }
2146
2147         if (iop->status_block->iop_state == ADAPTER_STATE_READY ||
2148             iop->status_block->iop_state == ADAPTER_STATE_OPERATIONAL ||
2149             iop->status_block->iop_state == ADAPTER_STATE_HOLD ||
2150             iop->status_block->iop_state == ADAPTER_STATE_FAILED)
2151         {
2152                 dprintk(KERN_INFO "%s: Already running, trying to reset...\n",
2153                         iop->name);
2154                 if (i2o_reset_controller(iop) < 0)
2155                         return -1;
2156         }
2157
2158         if (i2o_init_outbound_q(iop) < 0)
2159                 return -1;
2160
2161         if (i2o_post_outbound_messages(iop)) 
2162                 return -1;
2163
2164         /* In HOLD state */
2165         
2166         if (i2o_hrt_get(iop) < 0)
2167                 return -1;
2168
2169         return 0;
2170 }
2171
2172
2173 /**
2174  *      i2o_init_outbound_queue - setup the outbound queue
2175  *      @c: controller
2176  *
2177  *      Clear and (re)initialize IOP's outbound queue. Returns 0 on
2178  *      success or a negative errno code on a failure.
2179  */
2180  
2181 int i2o_init_outbound_q(struct i2o_controller *c)
2182 {
2183         u8 *status;
2184         u32 m;
2185         u32 *msg;
2186         u32 time;
2187
2188         dprintk(KERN_INFO "%s: Initializing Outbound Queue...\n", c->name);
2189         m=i2o_wait_message(c, "OutboundInit");
2190         if(m==0xFFFFFFFF)
2191                 return -ETIMEDOUT;
2192         msg=(u32 *)(c->mem_offset+m);
2193
2194         status = kmalloc(4,GFP_KERNEL);
2195         if (status==NULL) {
2196                 printk(KERN_ERR "%s: Outbound Queue initialization failed - no free memory.\n",
2197                         c->name);
2198                 return -ENOMEM;
2199         }
2200         memset(status, 0, 4);
2201
2202         msg[0]= EIGHT_WORD_MSG_SIZE| TRL_OFFSET_6;
2203         msg[1]= I2O_CMD_OUTBOUND_INIT<<24 | HOST_TID<<12 | ADAPTER_TID;
2204         msg[2]= core_context;
2205         msg[3]= 0x0106;                         /* Transaction context */
2206         msg[4]= 4096;                           /* Host page frame size */
2207         /* Frame size is in words. Pick 128, its what everyone elses uses and
2208                 other sizes break some adapters. */
2209         msg[5]= MSG_FRAME_SIZE<<16|0x80;        /* Outbound msg frame size and Initcode */
2210         msg[6]= 0xD0000004;                     /* Simple SG LE, EOB */
2211         msg[7]= virt_to_bus(status);
2212
2213         i2o_post_message(c,m);
2214         
2215         barrier();
2216         time=jiffies;
2217         while(status[0] < I2O_CMD_REJECTED)
2218         {
2219                 if((jiffies-time)>=30*HZ)
2220                 {
2221                         if(status[0]==0x00)
2222                                 printk(KERN_ERR "%s: Ignored queue initialize request.\n",
2223                                         c->name);
2224                         else  
2225                                 printk(KERN_ERR "%s: Outbound queue initialize timeout.\n",
2226                                         c->name);
2227                         kfree(status);
2228                         return -ETIMEDOUT;
2229                 }  
2230                 schedule();
2231                 barrier();
2232         }  
2233
2234         if(status[0] != I2O_CMD_COMPLETED)
2235         {
2236                 printk(KERN_ERR "%s: IOP outbound initialise failed.\n", c->name);
2237                 kfree(status);
2238                 return -ETIMEDOUT;
2239         }
2240
2241         return 0;
2242 }
2243
2244 /**
2245  *      i2o_post_outbound_messages      -       fill message queue
2246  *      @c: controller
2247  *
2248  *      Allocate a message frame and load the messages into the IOP. The
2249  *      function returns zero on success or a negative errno code on
2250  *      failure.
2251  */
2252
2253 int i2o_post_outbound_messages(struct i2o_controller *c)
2254 {
2255         int i;
2256         u32 m;
2257         /* Alloc space for IOP's outbound queue message frames */
2258
2259         c->page_frame = kmalloc(MSG_POOL_SIZE, GFP_KERNEL);
2260         if(c->page_frame==NULL) {
2261                 printk(KERN_CRIT "%s: Outbound Q initialize failed; out of memory.\n",
2262                         c->name);
2263                 return -ENOMEM;
2264         }
2265         m=virt_to_bus(c->page_frame);
2266
2267         /* Post frames */
2268
2269         for(i=0; i< NMBR_MSG_FRAMES; i++) {
2270                 I2O_REPLY_WRITE32(c,m);
2271                 mb();
2272                 m += MSG_FRAME_SIZE;
2273         }
2274
2275         return 0;
2276 }
2277
2278 /*
2279  * Get the IOP's Logical Configuration Table
2280  */
2281 int i2o_lct_get(struct i2o_controller *c)
2282 {
2283         u32 msg[8];
2284         int ret, size = c->status_block->expected_lct_size;
2285
2286         do {
2287                 if (c->lct == NULL) {
2288                         c->lct = kmalloc(size, GFP_KERNEL);
2289                         if(c->lct == NULL) {
2290                                 printk(KERN_CRIT "%s: Lct Get failed. Out of memory.\n",
2291                                         c->name);
2292                                 return -ENOMEM;
2293                         }
2294                 }
2295                 memset(c->lct, 0, size);
2296
2297                 msg[0] = EIGHT_WORD_MSG_SIZE|SGL_OFFSET_6;
2298                 msg[1] = I2O_CMD_LCT_NOTIFY<<24 | HOST_TID<<12 | ADAPTER_TID;
2299                 /* msg[2] filled in i2o_post_wait */
2300                 msg[3] = 0;
2301                 msg[4] = 0xFFFFFFFF;    /* All devices */
2302                 msg[5] = 0x00000000;    /* Report now */
2303                 msg[6] = 0xD0000000|size;
2304                 msg[7] = virt_to_bus(c->lct);
2305
2306                 ret=i2o_post_wait_mem(c, msg, sizeof(msg), 120, c->lct, NULL);
2307                 
2308                 if(ret == -ETIMEDOUT)
2309                 {
2310                         c->lct = NULL;
2311                         return ret;
2312                 }
2313                 
2314                 if(ret<0)
2315                 {
2316                         printk(KERN_ERR "%s: LCT Get failed (status=%#x.\n", 
2317                                 c->name, -ret); 
2318                         return ret;
2319                 }
2320
2321                 if (c->lct->table_size << 2 > size) {
2322                         size = c->lct->table_size << 2;
2323                         kfree(c->lct);
2324                         c->lct = NULL;
2325                 }
2326         } while (c->lct == NULL);
2327
2328         if ((ret=i2o_parse_lct(c)) < 0)
2329                 return ret;
2330
2331         return 0;
2332 }
2333
2334 /*
2335  * Like above, but used for async notification.  The main
2336  * difference is that we keep track of the CurrentChangeIndiicator
2337  * so that we only get updates when it actually changes.
2338  *
2339  */
2340 int i2o_lct_notify(struct i2o_controller *c)
2341 {
2342         u32 msg[8];
2343
2344         msg[0] = EIGHT_WORD_MSG_SIZE|SGL_OFFSET_6;
2345         msg[1] = I2O_CMD_LCT_NOTIFY<<24 | HOST_TID<<12 | ADAPTER_TID;
2346         msg[2] = core_context;
2347         msg[3] = 0xDEADBEEF;    
2348         msg[4] = 0xFFFFFFFF;    /* All devices */
2349         msg[5] = c->dlct->change_ind+1; /* Next change */
2350         msg[6] = 0xD0000000|8192;
2351         msg[7] = virt_to_bus(c->dlct);
2352
2353         return i2o_post_this(c, msg, sizeof(msg));
2354 }
2355                 
2356 /*
2357  *      Bring a controller online into OPERATIONAL state. 
2358  */
2359  
2360 int i2o_online_controller(struct i2o_controller *iop)
2361 {
2362         u32 v;
2363         
2364         if (i2o_systab_send(iop) < 0)
2365                 return -1;
2366
2367         /* In READY state */
2368
2369         dprintk(KERN_INFO "%s: Attempting to enable...\n", iop->name);
2370         if (i2o_enable_controller(iop) < 0)
2371                 return -1;
2372
2373         /* In OPERATIONAL state  */
2374
2375         dprintk(KERN_INFO "%s: Attempting to get/parse lct...\n", iop->name);
2376         if (i2o_lct_get(iop) < 0)
2377                 return -1;
2378
2379         /* Check battery status */
2380          
2381         iop->battery = 0;
2382         if(i2o_query_scalar(iop, ADAPTER_TID, 0x0000, 4, &v, 4)>=0)
2383         {
2384                 if(v&16)
2385                         iop->battery = 1;
2386         }
2387
2388         return 0;
2389 }
2390
2391 /*
2392  * Build system table
2393  *
2394  * The system table contains information about all the IOPs in the
2395  * system (duh) and is used by the Executives on the IOPs to establish
2396  * peer2peer connections.  We're not supporting peer2peer at the moment,
2397  * but this will be needed down the road for things like lan2lan forwarding.
2398  */
2399 static int i2o_build_sys_table(void)
2400 {
2401         struct i2o_controller *iop = NULL;
2402         struct i2o_controller *niop = NULL;
2403         int count = 0;
2404
2405         sys_tbl_len = sizeof(struct i2o_sys_tbl) +      // Header + IOPs
2406                                 (i2o_num_controllers) *
2407                                         sizeof(struct i2o_sys_tbl_entry);
2408
2409         if(sys_tbl)
2410                 kfree(sys_tbl);
2411
2412         sys_tbl = kmalloc(sys_tbl_len, GFP_KERNEL);
2413         if(!sys_tbl) {
2414                 printk(KERN_CRIT "SysTab Set failed. Out of memory.\n");
2415                 return -ENOMEM;
2416         }
2417         memset((void*)sys_tbl, 0, sys_tbl_len);
2418
2419         sys_tbl->num_entries = i2o_num_controllers;
2420         sys_tbl->version = I2OVERSION; /* TODO: Version 2.0 */
2421         sys_tbl->change_ind = sys_tbl_ind++;
2422
2423         for(iop = i2o_controller_chain; iop; iop = niop)
2424         {
2425                 niop = iop->next;
2426
2427                 /* 
2428                  * Get updated IOP state so we have the latest information
2429                  *
2430                  * We should delete the controller at this point if it
2431                  * doesn't respond since  if it's not on the system table 
2432                  * it is techninically not part of the I2O subsyßtem...
2433                  */
2434                 if(i2o_status_get(iop)) {
2435                         printk(KERN_ERR "%s: Deleting b/c could not get status while"
2436                                 "attempting to build system table\n", iop->name);
2437                         i2o_delete_controller(iop);             
2438                         sys_tbl->num_entries--;
2439                         continue; // try the next one
2440                 }
2441
2442                 sys_tbl->iops[count].org_id = iop->status_block->org_id;
2443                 sys_tbl->iops[count].iop_id = iop->unit + 2;
2444                 sys_tbl->iops[count].seg_num = 0;
2445                 sys_tbl->iops[count].i2o_version = 
2446                                 iop->status_block->i2o_version;
2447                 sys_tbl->iops[count].iop_state = 
2448                                 iop->status_block->iop_state;
2449                 sys_tbl->iops[count].msg_type = 
2450                                 iop->status_block->msg_type;
2451                 sys_tbl->iops[count].frame_size = 
2452                                 iop->status_block->inbound_frame_size;
2453                 sys_tbl->iops[count].last_changed = sys_tbl_ind - 1; // ??
2454                 sys_tbl->iops[count].iop_capabilities = 
2455                                 iop->status_block->iop_capabilities;
2456                 sys_tbl->iops[count].inbound_low = 
2457                                 (u32)virt_to_bus(iop->post_port);
2458                 sys_tbl->iops[count].inbound_high = 0;  // TODO: 64-bit support
2459
2460                 count++;
2461         }
2462
2463 #ifdef DRIVERDEBUG
2464 {
2465         u32 *table;
2466         table = (u32*)sys_tbl;
2467         for(count = 0; count < (sys_tbl_len >>2); count++)
2468                 printk(KERN_INFO "sys_tbl[%d] = %0#10x\n", count, table[count]);
2469 }
2470 #endif
2471
2472         return 0;
2473 }
2474
2475
2476 /*
2477  *      Run time support routines
2478  */
2479  
2480 /*
2481  *      Generic "post and forget" helpers. This is less efficient - we do
2482  *      a memcpy for example that isnt strictly needed, but for most uses
2483  *      this is simply not worth optimising
2484  */
2485
2486 int i2o_post_this(struct i2o_controller *c, u32 *data, int len)
2487 {
2488         u32 m;
2489         u32 *msg;
2490         unsigned long t=jiffies;
2491
2492         do
2493         {
2494                 mb();
2495                 m = I2O_POST_READ32(c);
2496         }
2497         while(m==0xFFFFFFFF && (jiffies-t)<HZ);
2498         
2499         if(m==0xFFFFFFFF)
2500         {
2501                 printk(KERN_ERR "%s: Timeout waiting for message frame!\n",
2502                        c->name);
2503                 return -ETIMEDOUT;
2504         }
2505         msg = (u32 *)(c->mem_offset + m);
2506         memcpy_toio(msg, data, len);
2507         i2o_post_message(c,m);
2508         return 0;
2509 }
2510
2511 /**
2512  *      i2o_post_wait_mem       -       I2O query/reply with DMA buffers
2513  *      @c: controller
2514  *      @msg: message to send
2515  *      @len: length of message
2516  *      @timeout: time in seconds to wait
2517  *      @mem1: attached memory buffer 1
2518  *      @mem2: attached memory buffer 2
2519  *
2520  *      This core API allows an OSM to post a message and then be told whether
2521  *      or not the system received a successful reply. 
2522  *
2523  *      If the message times out then the value '-ETIMEDOUT' is returned. This
2524  *      is a special case. In this situation the message may (should) complete
2525  *      at an indefinite time in the future. When it completes it will use the
2526  *      memory buffers attached to the request. If -ETIMEDOUT is returned then
2527  *      the memory buffers must not be freed. Instead the event completion will
2528  *      free them for you. In all other cases the buffers are your problem.
2529  *
2530  *      Pass NULL for unneeded buffers.
2531  */
2532  
2533 int i2o_post_wait_mem(struct i2o_controller *c, u32 *msg, int len, int timeout, void *mem1, void *mem2)
2534 {
2535         DECLARE_WAIT_QUEUE_HEAD(wq_i2o_post);
2536         int complete = 0;
2537         int status;
2538         int flags = 0;
2539         struct i2o_post_wait_data *wait_data =
2540                 kmalloc(sizeof(struct i2o_post_wait_data), GFP_KERNEL);
2541
2542         if(!wait_data)
2543                 return -ENOMEM;
2544
2545         /*
2546          *      Create a new notification object
2547          */
2548         wait_data->status = &status;
2549         wait_data->complete = &complete;
2550         wait_data->mem[0] = mem1;
2551         wait_data->mem[1] = mem2;
2552         /* 
2553          *      Queue the event with its unique id
2554          */
2555         spin_lock_irqsave(&post_wait_lock, flags);
2556
2557         wait_data->next = post_wait_queue;
2558         post_wait_queue = wait_data;
2559         wait_data->id = (++post_wait_id) & 0x7fff;
2560         wait_data->wq = &wq_i2o_post;
2561
2562         spin_unlock_irqrestore(&post_wait_lock, flags);
2563
2564         /*
2565          *      Fill in the message id
2566          */
2567          
2568         msg[2] = 0x80000000|(u32)core_context|((u32)wait_data->id<<16);
2569         
2570         /*
2571          *      Post the message to the controller. At some point later it 
2572          *      will return. If we time out before it returns then
2573          *      complete will be zero.  From the point post_this returns
2574          *      the wait_data may have been deleted.
2575          */
2576         if ((status = i2o_post_this(c, msg, len))==0) {
2577                 sleep_on_timeout(&wq_i2o_post, HZ * timeout);
2578         }  
2579         else
2580                 return -EIO;
2581                 
2582         if(signal_pending(current))
2583                 status = -EINTR;
2584                 
2585         spin_lock_irqsave(&post_wait_lock, flags);
2586         barrier();      /* Be sure we see complete as it is locked */
2587         if(!complete)
2588         {
2589                 /* 
2590                  *      Mark the entry dead. We cannot remove it. This is important.
2591                  *      When it does terminate (which it must do if the controller hasnt
2592                  *      died..) then it will otherwise scribble on stuff.
2593                  *      !complete lets us safely check if the entry is still
2594                  *      allocated and thus we can write into it
2595                  */
2596                 wait_data->wq = NULL;
2597                 status = -ETIMEDOUT;
2598         }
2599         else
2600         {
2601                 /* Debugging check - remove me soon */
2602                 if(status == -ETIMEDOUT)
2603                 {
2604                         printk("TIMEDOUT BUG!\n");
2605                         status = -EIO;
2606                 }
2607         }
2608         /* And the wait_data is not leaked either! */    
2609         spin_unlock_irqrestore(&post_wait_lock, flags);
2610         return status;
2611 }
2612
2613 /**
2614  *      i2o_post_wait           -       I2O query/reply
2615  *      @c: controller
2616  *      @msg: message to send
2617  *      @len: length of message
2618  *      @timeout: time in seconds to wait
2619  *
2620  *      This core API allows an OSM to post a message and then be told whether
2621  *      or not the system received a successful reply. 
2622  */
2623  
2624 int i2o_post_wait(struct i2o_controller *c, u32 *msg, int len, int timeout)
2625 {
2626         return i2o_post_wait_mem(c, msg, len, timeout, NULL, NULL);
2627 }
2628
2629 /*
2630  * i2o_post_wait is completed and we want to wake up the 
2631  * sleeping proccess. Called by core's reply handler.
2632  */
2633
2634 static void i2o_post_wait_complete(u32 context, int status)
2635 {
2636         struct i2o_post_wait_data **p1, *q;
2637         unsigned long flags;
2638         
2639         /* 
2640          * We need to search through the post_wait 
2641          * queue to see if the given message is still
2642          * outstanding.  If not, it means that the IOP 
2643          * took longer to respond to the message than we 
2644          * had allowed and timer has already expired.  
2645          * Not much we can do about that except log
2646          * it for debug purposes, increase timeout, and recompile
2647          *
2648          * Lock needed to keep anyone from moving queue pointers 
2649          * around while we're looking through them.
2650          */
2651
2652         spin_lock_irqsave(&post_wait_lock, flags);
2653
2654         for(p1 = &post_wait_queue; *p1!=NULL; p1 = &((*p1)->next)) 
2655         {
2656                 q = (*p1);
2657                 if(q->id == ((context >> 16) & 0x7fff)) {
2658                         /*
2659                          *      Delete it 
2660                          */
2661                          
2662                         *p1 = q->next;
2663                         
2664                         /*
2665                          *      Live or dead ?
2666                          */
2667                          
2668                         if(q->wq)
2669                         {
2670                                 /* Live entry - wakeup and set status */
2671                                 *q->status = status;
2672                                 *q->complete = 1;
2673                                 wake_up(q->wq);
2674                         }
2675                         else
2676                         {
2677                                 /*
2678                                  *      Free resources. Caller is dead
2679                                  */
2680                                 if(q->mem[0])
2681                                         kfree(q->mem[0]);
2682                                 if(q->mem[1])
2683                                         kfree(q->mem[1]);
2684                                 printk(KERN_WARNING "i2o_post_wait event completed after timeout.\n");
2685                         }
2686                         kfree(q);
2687                         spin_unlock(&post_wait_lock);
2688                         return;
2689                 }
2690         }
2691         spin_unlock(&post_wait_lock);
2692
2693         printk(KERN_DEBUG "i2o_post_wait: Bogus reply!\n");
2694 }
2695
2696 /*      Issue UTIL_PARAMS_GET or UTIL_PARAMS_SET
2697  *
2698  *      This function can be used for all UtilParamsGet/Set operations.
2699  *      The OperationList is given in oplist-buffer, 
2700  *      and results are returned in reslist-buffer.
2701  *      Note that the minimum sized reslist is 8 bytes and contains
2702  *      ResultCount, ErrorInfoSize, BlockStatus and BlockSize.
2703  */
2704 int i2o_issue_params(int cmd, struct i2o_controller *iop, int tid, 
2705                 void *oplist, int oplen, void *reslist, int reslen)
2706 {
2707         u32 msg[9]; 
2708         u32 *res32 = (u32*)reslist;
2709         u32 *restmp = (u32*)reslist;
2710         int len = 0;
2711         int i = 0;
2712         int wait_status;
2713         u32 *opmem, *resmem;
2714         
2715         /* Get DMAable memory */
2716         opmem = kmalloc(oplen, GFP_KERNEL);
2717         if(opmem == NULL)
2718                 return -ENOMEM;
2719         memcpy(opmem, oplist, oplen);
2720         
2721         resmem = kmalloc(reslen, GFP_KERNEL);
2722         if(resmem == NULL)
2723         {
2724                 kfree(opmem);
2725                 return -ENOMEM;
2726         }
2727         
2728         msg[0] = NINE_WORD_MSG_SIZE | SGL_OFFSET_5;
2729         msg[1] = cmd << 24 | HOST_TID << 12 | tid; 
2730         msg[3] = 0;
2731         msg[4] = 0;
2732         msg[5] = 0x54000000 | oplen;    /* OperationList */
2733         msg[6] = virt_to_bus(opmem);
2734         msg[7] = 0xD0000000 | reslen;   /* ResultList */